 sttl FIO Simulation Routines
 pag

*
* Reset CPU-IOP Interface
*
fio_reset
 ldx #iop_cpu clear control structure
00 clr ,x+
 cmpx #fifo
 bne 00b
 ldx #0
 stx fifo_get set Q pointers
 stx fifo_put
 lda #$99 tell CPU I'm ready
 sta irq_gen
 ldu #1024 -- Reset Time-Out value
fio_wait ldb #255 Spin counter
00 tst irq_gen wait for CPU to clear
 beq 10f
 decb
 bne 00b jump back if not ready
 leau -1,u
 cmpu #0
 bne fio_wait
 ldx #CPU_down
 lbsr DB_pdata
 lbsr DB_main
 bra fio_reset
10 rts

*
* fio_msg - Send a message via the FIO Mailbox
*   B - Message code to send (one byte)
*   iop_cpu1..3 already set up
*
 if DBG_MSG&DEBUG_CONTROL
00 fcc $d,'IOP Send: ',0
 endif

fio_msg pshs b,x save register
 stb iop_cpu set up mailbox value
 if DO_HISTORY
 cmpb #R_CLOCK -- don't record clock transactions
 beq 05f
 jsr H_iop record outgoing traffic
05 nop
 endif
 if DBG_MSG&DEBUG_CONTROL
 jsr DB_msg
 fdb DBG_MSG,5f
 pshs d,x,y,u
 ldx #00b
 lbsr DB_pdata
 ldu #iop_cpu
0 lda ,u+
 lbsr DB_phex
 lbsr DB_outsp
 cmpu #iop_cpu3
 bls 0b
 puls d,x,y,u
 endif
5 lda #$FF tell CPU mailbox full
 sta iop_cpuF
 sta irq_gen let the CPU know it's there
05 ldx #$FFFF time-out counter
10 lda iop_cpuF wait till value consumed
 beq 20f jump if consumed
 leax -1,x time-out yet?
 bne 10b
 ldx #00f
 jsr DB_pdata
 lda 0,s
 jsr DB_phex
 lbsr DB_main
 bra 05b try again
20 puls b,x,pc
00 fcc $d,'FIO Time-out, Value = $',0

*
* fio_response - Return a response code/sequence #
*    B - Response code
*    A - Transaction specific value
*
fio_response pshs d,x
 bsr FIO_get access FIO
 ldx utask
 lda tsseq,x
 sta iop_cpu1
 lda 0,s get transaction specific value
 sta iop_cpu2
 jsr fio_msg
 bsr FIO_rel release FIO
 puls d,x,pc return

*
* FIO_get - Get access to FIO device
* -- Sleep till available
*
FIO_get pshs d,x,y,u save registers
10 ldy #FIO_lock is the device locked
 tst 0,y
 beq 20f no - go get it
 ldb #FIOPRI waiting for the FIO
 jsr sleep
 bra 10b try again
20 inc 0,y mark in use
 puls d,x,y,u,pc return

*
* FIO_rel - Release access to FIO
*
FIO_rel pshs d,x,y,u
 ldy #FIO_lock
 clr 0,y
 jsr wakeup
 puls d,x,y,u,pc return

*
* FIFO_get - Fetch character from FIFO
*   B - Character fetched
*
FIFOgeta pshs b
 bsr FIFO_get
 puls a
 exg a,b
 rts
*
FIFO_get pshs x
 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 puls 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 x
 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 puls x,pc
