 sttl FIO Simulation Routines
 pag

 if UniFLEX
 define
*** TEMP
MAP_state equ $F140
MSG_out   equ $F150
MSG_in    equ $F160
 enddef
 endif

 ifn UniFLEX -- ROM special code --
*
* Wait for IOP to reset interface
*
fio_reset
 lbsr map_iop
00 lda irq_gen wait until magic value appears
 cmpa #$99
 bne 00b
 clr irq_gen let IOP know I saw it...
10 lda iop_cpuF wait for Version response
 beq 10b
 lda iop_cpu1 -- Version #
 sta rom_version
 tst irq_gen -- kill interrupt
 clr iop_cpuF let IOP know I'm ready
 lbsr map_io
 rts

*
* fio_msg - Send a message via the FIO Mailbox
*   B - Message code to send (one byte)
*   A - Message specific data
*   bsr fio_msg
*   B - Error code response
*   A - Message specific data
*
fio_msg pshs x save register
 lbsr map_iop
 stb cpu_iop set up mailbox value
 sta cpu_iop2 message specific data
 lda #$FF sequence #
 sta cpu_iop1
 clr cpu_iop3 terminal 00
 lda #$FF tell IOP mailbox full
 sta cpu_iopF
 sta irq_gen let the IOP know it's there
05 ldx #$FFFF time-out counter
10 lda cpu_iopF wait till value consumed
 beq 20f jump if consumed
 leax -1,x time-out yet?
 bne 10b
 bra 05b try again
*-- Wait for response
20 ldb iop_cpuF wait for data available
 beq 20b
 tst irq_gen clear any interrupts
 lda iop_cpu2 get message data
 ldb iop_cpu get primary message response
 clr iop_cpuF tell IOP we've got it all
 lbsr map_io
 puls x,pc

 else

*
* fio_send - Send a message via the FIO Mailbox
*   B - Message code to send (one byte)
*   A - Message specific data
*   U - Sequence #/Terminal #
*   bsr fio_send
*
 global fio_send
fio_send pshs cc,d,x,u save register
 seti mask interrupts!
 lbsr map_iop
 stb cpu_iop set up mailbox value
 sta cpu_iop2 message specific data
 ldd 5,s sequence #/terminal #
 sta cpu_iop1
 stb cpu_iop3
*-- TEMP
 lda cpu_iop
 sta MSG_out
 lda cpu_iop1
 sta MSG_out+1
 lda cpu_iop2
 sta MSG_out+2
 lda cpu_iop3
 sta MSG_out+3
*-- TEMP
 lda #$FF tell IOP mailbox full
*-- TEMP
 sta MSG_out+4
*-- TEMP
 sta cpu_iopF
 sta irq_gen let the IOP know it's there
05 ldx #$FFFF time-out counter
10 lda cpu_iopF wait till value consumed
 beq 20f jump if consumed
 leax -1,x time-out yet?
 bne 10b
 bra 05b try again
20
*-- TEMP
 clr MSG_out+4
*-- TEMP
 lbsr map_io
 puls cc,d,x,u,pc

 endif

*
* FIFO_get - Fetch character from FIFO
*   B - Character fetched
*
FIFOgeta pshs b
 bsr FIFO_get
 puls a
 exg a,b
 rts
*
*-- Pointers in FIFO are relative to 0
*
FIFO_get pshs cc,x
 seti -- interrupts must be masked
 lbsr map_iop
 ldb fifo_cnt any data?
 beq 99f no - exit
 decb yes - adjust count
 stb fifo_cnt
 ldx fifo_get get consumer pointer
 ldb fifo,x fetch byte
 leax 1,x bump pointer
 cmpx #FIFO_SIZE end of fifo?
 bne 10f
 ldx #0 reset pointer
10 stx fifo_get
99 lbsr map_io
 puls cc,x,pc

*
* FIFO_put - Place character into FIFO
*   B - Character fetched
*
FIFOputa pshs b
 tfr a,b
 bsr FIFO_put
 puls b,pc
*
FIFO_put pshs cc,x
 seti -- interrups must be masked
 lbsr map_iop
 ldx fifo_put get consumer pointer
 stb fifo,x store byte
 leax 1,x bump pointer
 cmpx #FIFO_SIZE end of fifo?
 bne 10f
 ldx #0 reset pointer
10 stx fifo_put
 inc fifo_cnt update count
99 lbsr map_io
 puls cc,x,pc

*
* map_iop - Map in the IOP shared RAM
*
map_iop pshs a
 lda #IOP_MAP
 sta IO_DAT
 if UniFLEX
 sta MAP_state
 endif
 puls a,pc

*
* map_io - Map in all other I/O
*
map_io pshs a
 lda #IO_MAP
 sta IO_DAT
 if UniFLEX
 sta MAP_state
 endif
 puls a,pc
