*
* :*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*
*
* :*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*
*
*
*
romsize equ $010000
rombot  equ $000000
romtop  equ $00FFFF

ramsize equ $020000
rambot  equ $020000
ramtop  equ $03FFFF

* FIFO register addresses
fifo    equ $060000

* filter addresses
dlbar   equ $050001
dgbar   equ $050000
slbar   equ $050003
sgbar   equ $050002
contr   equ $050005


* DUART register addresses
base    equ $040001
modea   equ base+0
stata   equ base+2
clka    equ base+2
cmda    equ base+4
thra    equ base+6
rhra    equ base+6
ipcr    equ base+8
acr     equ base+8
isr     equ base+10
imr     equ base+10
countl  equ base+12
counth  equ base+14
modeb   equ base+16
statb   equ base+18
clkb    equ base+18
cmdb    equ base+20
rhrb    equ base+22
thrb    equ base+22
ivr     equ base+24
iport   equ base+26
opcr    equ base+26
stcnt   equ base+28
spcnt   equ base+30
bitset  equ base+28
bitres  equ base+30



* mode reg 1

rxrts   equ $80
norrts  equ $0

rirdy   equ $0
rifful  equ $40

cerr    equ $0
blerr   equ $20

wthpar  equ $0
forcep  equ $8
nopar   equ $10
multi   equ $18

evenp   equ $0
oddp    equ $4

bits5   equ $0
bits6   equ $1
bits7   equ $2
bits8   equ $3

* mode reg 2 

norop  equ $0
echo    equ $40
lloop   equ $80
rloop   equ $C0

txrts   equ $20
notrts  equ $0

cts     equ $10
nocts   equ $0

stop1   equ $7
stop2   equ $f

* clk sel reg

b9600   equ $bb

* Command REG
* Miscell

nocmd   equ $0
point1  equ $10
rstrx   equ $20
rsttx   equ $30
rsterr  equ $40
rstint  equ $50
brkon   equ $60
brkoff  equ $70

* transmitter

tnoop   equ $0
ten     equ $4
tdis    equ $8

* receiver

rnoop   equ $0
ren     equ $1
rdis    equ $2

* interupts
noints  equ $0

* output config
allout  equ $0

* status register bits

break   equ $80
frame   equ $40
parity  equ $20
overrun equ $10
txempty equ $8
txready equ $4
ffull equ $2
rxready equ $1

** Derived memory addresses
*vecsize equ $200
*stack   equ ramtop-vecsize-1

*
null	equ $00
cr	equ $0d
lf	equ $0a
space	equ $20
bell	equ $07
tab	equ $09
*
netbrk	equ $1c		* Control-\


        org  $01000

*
* :*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*:*
*
* INCHR: inputs a character from port 1
*        returns with carry set if not available
*
inchr:  btst   #0,stata	* Rx ready?
        beq    inchr1	* no, return with carry
nchr:   move.b rhra,d0	* get char
        btst.b #0,stata
        bne    nchr
        and.b  #$0fe,ccr	* clear carry no error
        rts		* return
inchr1: or.b #$01,ccr		* set carry
        rts		* return
*
* INCH: inputs a character from port 1
*	no echo, char in D0
*
inch:   jsr inchr	* get character
        bcs inch		* if not available, try again
        rts		* return
*
* INCHR2: as INCH for port 2
*
inchr2:	btst #0,statb_* Rx ready?
	beq inch21	* no, return with carry
ichr2: 	move.b rhrb,d0	* get data
       btst.b #0,statb
       bne ichr2
	and.b #$0fe,ccr	* clear carry no error
	rts
inch21:	or.b #$01,ccr	* error - set carry
	rts
*
* INCH2: as INCH for port 2
*
inch2:	jsr inchr2	* try for char
	bcs inch2	* loop if no char available
	rts
*
* INCHU: inputs a character to D0, [a-z] converted to [A-Z]
*	 <LF> converted to <CR>, no echo, uses INCH, register D0 used
*
inchu:  jsr inch	* get a character
upcse:  cmp.b #$61,d0	* is it less than "a"?
        blt inchu1	* yes, continue
        cmp.b #$7a,d0	* is it greater than "z"?
        bgt inchu1	* yes, continue
        sub.b #$20,d0	* subtract offset
        rts		* return
inchu1: cmp.b #lf,d0	* is it LF?
        bne inchu3	* no, continue
        move.b #cr,d0	* char is CR
inchu3: rts		* return

* OUTCH: output a character using duart port 1
*	 character in D0, all registers unaffected
*
outch: btst #2,stata	* Tx ready?
       beq outch	* no, try again
       move.b   #ten,cmda
       move.b d0,thra	* output character
       move.b   #ren,cmda
       rts		* return
*
* OUTCH2: as OUTCH for auxiliary RS232 port
*
outch2:	btst #2,statb	* Tx ready?
	beq outch2	* no - loop
     move.b   #ten,cmdb
	move.b d0,thrb	* send char
     move.b   #ren,cmdb
	rts
*
* PDATA: outputs a string starting at A4, length in D2
*	 uses OUTCH 
*
pdata: move.l d0,-(sp)		* save D0
       bra pdatst	* go to count test
pdatlp:move.b (a4)+,d0	* get op char
       jsr outch	* print char
pdatst:dbra d1,pdatlp	* decrement D2, repeat loop if not zero
       move.l (sp)+,d0		* restore D0
       rts		* return
*
* NEWLIN: output #cr,lf; uses OUTCH and D0
*
newlin:move.b #cr,d0	* get CR
       jsr outch	* output char
       move.b #lf,d0	* get LF
       jsr outch	* output char
       rts		* return
*
* NEWLN2: output two newlines
*
newln2:jsr newlin	* do #cr,lf; fall through
       jsr newlin
       rts

* HINCH: gets a character - if lnkflg = 00 gets char from network,
* echos and acknowledges it; if lnkflg <> 00 gets char from
* auxiliary RS232 port;
* char got to d0, all other registers unaffected, clears top bit of char
*
hinch:	jsr inch2	* get char from aux RS232
	and.b #$7f,d0	* clear top bit
	rts		* return
*
* HINCHR: gets an UPPER-CASE character from network interface to D0
*         <LF> converted to <CR>, uses HINCH routine
*
hinchr:	jsr hinch	* get character
	cmp.b #$61,d0	* is it less than "a"?
	blt hincu1	* yes, continue
	cmp.b #$7a,d0	* is it greater than "z"?
	bgt hincu1	* yes, continue
	sub.b #$20,d0	* subtract character offset
	rts		* return
hincu1:	cmp.b #lf,d0	* is it <LF>?
	bne hincu3	* no, continue
	move.b #cr,d0	*  character is <CR>
hincu3:	rts		* return
*
* HIN1HX: gets a hexadecimal character from network interface to D0
*         uses HINCHR routine, return with carry set if <CR>
*         other characters ignored
*
hin1hx:	jsr hinchr	* get character
	cmp.b #$30,d0	* is it less than "0"?
	blt hin1e1	* yes, possible error
	cmp.b #$39,d0	* is it greater than "9"?
	bgt hin1e2	* yes, possible error
	sub.b #$30,d0	* subtract offset
	and.b #$0fe,ccr		* clear carry
	rts		* return
hin1e2:	cmp.b #$41,d0	* is it less than "A"?
	blt hin1er	* yes, error
	cmp.b #$46,d0	* is it greater than "F"?
	bgt hin1er	* yes, error
	sub.b #$41-10,d0	* subtract offset
	and.b #$0fe,ccr		* clear carry
	rts		* return
hin1e1:	cmp.b #cr,d0	* is it <CR>?
	bne hin1er	* no, error
	or.b #$01,ccr		* set carry
	rts		* return
hin1er:	bra hin1hx	* try again
*
* HIN2HX: gets 2 hexadecimal characters into D0 from network
*         All other registers preserved
*         return with carry set if <CR>
*
hin2hx:	jsr hin1hx	* get 1 hex char
	bcs hin2h9	* if carry set, return
	move.l d1,-(sp)		* save D1
	and.b #$0f,d0	* clear ms nibble
	rol.b #4,d0	* swap nibbles
	move.l d0,-(sp)		* save D0 (1st char)
	jsr hin1hx	* get second hex char
	bcs hin2h8	* return if carry set
	move.b d0,d1	* save ls nibble
	move.l (sp)+,d0		* restore D0
	and.b #$0f,d1	* clear ms nibble
	add.b d1,d0	* combine nibbles
	move.l (sp)+,d1		* restore D1
	and.b #$0fe,ccr		* clear carry
hin2h9:	rts		*
hin2h8:	move.l (sp)+,d0		*
	move.l (sp)+,d0		* clean up stack
	or.b #$01,ccr		* set carry
	rts		* return
*
* HIN4HX: gets 4 hex characters from network into D1
*         uses HIN2HX routine, returns with carry set if <CR>
*
hin4hx:	jsr hin2hx	* get 2 chars
	bcs hin4hr	* return if carry
	move.b d0,d1	
	lsl.w #8,d1	* shift to msb of d1
	jsr hin2hx	* get 2 chars
	bcs hin4hr	* return if carry
	move.b d0,d1	* move to lsb
hin4hr:	rts		* return
*
*
*
* PBELL: echo bell character, uses OUTCH and D0
*
pbell:  move.l d0,-(a7)
        move.b #bell,d0	* 
        jsr    outch	* output bell
        move.l (a7)+,d0
        rts		* return
*
*
* DECPT: prints a 24 bit number in D2 as a signed decimal
*
decval: dc.l  100000000,10000000,100000,10000,1000,100,10,1
decpt:  move.l  a1,-(a7)
        move.l #$07,d1	* count
        lea    decval,a1
        move.l (a1)+,d3	* compare value
declp1: move.b #$2f,d0	* digit offset
declp2: add.b #1,d0	* increment digit
        sub.l d3,d2	* subtract compare value
        bcc declp2	* repeat if not negative
        add.l d3,d2	* overshoot, restore value
        move.b d0,(a0)+
        move.l (a1)+,d3	* divide by 10
        dbra d1,declp1	* decrement count and repeat if not zero
        move.l (a7)+,a1
        rts		* return
*
* IN1HEX: get 1 hexadecimal character into D0* sets carry if <CR>
*
in1hex:	jsr inchu	* get upper-case character
	cmp.b #$30,d0	* is it less than "0"?
	blt in1er1	* yes, possible error
	cmp.b #$39,d0	* is it greater than "9"?
	bgt in1er2	* yes, possible error
	jsr outch	* echo char
	sub.b #$30,d0	* subtract offset
	and.b #$0fe,ccr		* clear carry bit
	rts		* return
in1er2:	cmp.b #$41,d0	* is it less than "A"?
	blt in1err	* yes, error
	cmp.b #$46,d0	* is it greater than "F"?
	bgt in1err	* yes, error
	jsr outch	* output char
	sub.b #$41-10,d0	* subtract char offset
	and.b #$0fe,ccr		* clear carry
	rts		* return
in1er1:	cmp.b #cr,d0	* is it CR?
	beq in1cr	* yes, continue
in1err:	jsr pbell	* output bell
	bra in1hex	* try again
in1cr:	jsr outch	* echo character
	or.b #$01,ccr		* set carry
	rts		* return
*
*
*
* IN2HEX: get 2 hex digits into D0* return with carry set if <CR>
*
in2hex:	jsr in1hex	* get first hex digit
	bcs in2hx9	* return if carry set
in2hax:	move.l d1,-(sp)		* save D1
	and.b #$0f,d0	* clear ms nibble
	rol.b #4,d0	* swap nibbles
	move.l d0,-(sp)		* save D0
	jsr in1hex	* get second hex digit
	bcs in2hx8	* return if carry set
	move.b d0,d1	* save LS nibble
	move.l (sp)+,d0		* get MS nibble
	and.b #$0f,d1	* clear MS bits
	add.b d1,d0	* add nibbles
	move.l (sp)+,d1		* restore D1
	and.b #$0fe,ccr		* clear carry bit
	rts		* return
in2hx8:	move.l (sp)+,d0		*
	move.l (sp)+,d0		* clean up stack
in2hx9: or.b #$01,ccr		* set carry
	rts		* return
	end
