 lib environment
 lib inttab
 data
 sttl Device Interrupt Handlers
 pag
 name inthan

 global irqhan,frqhan,swi3han,idle_tsk

BDATA  equ 0 via b side data register offset
IFR    equ 13 via interrupt flag register
IER    equ 14 via interrupt enable register

*
* irqhan
*
* The irg interrupt handler is called after initial
* system setup to process an irq type interrupt.
*

irqhan lda #CLKMSK get clock mask
 bita CLKSEL check clock int
 beq irqha0 no - try something else
 ldd CLKLAT reset timer interrupt
 jmp clkint go process interrupt
irqha0
 ldu CDSdev check CDS drive 0
 lda CDSFLG,u is it marksman?
 lbmi cdsint
 ldu CDSdev+2 check CDS drive 1
 lda CDSFLG,u is it marksman?
 lbmi cdsint
 lda W5STAT is it 5" Winchester?
 lbmi w5int
 lda DMF3CCR check for disk dma
 lbmi dmf3int
 lda CAT_C2 check for archive read
 lbmi ATint
 lda CAT_C3 archive write DMA comp?
 lbmi ATint

*   do strange decode on VIA interrupt

 lda VIA+IFR read VIA flags
 bpl not_via
 ldb VIA+BDATA read the DMF3 status lines
 anda VIA+IER
*bita #%00101000 check for archive int
 bita #%00100000 Archive timer 2?
 lbne ATint
*bita #%00010000 check for board int
 bita #%00001000 check for board int (CB2)
 beq via_err
 lda #%00001000 clear the CB2 interrupt
 sta VIA+IFR by stuffing the flag register
 bitb #%00100000 check for archive exception
 lbeq ATint
 bitb #%00000100 look only at 1797 irq line
 lbne dmf3int yea! branch if good disk irq
 jmp w5int must be WD1000 completion interrupt
via_err
 sta VIA+IFR reset spurious int
not_via

*  commence polling the stuff in the interrupt list

irqha1 ldx #inttab point to table
 lda 0,x+ get count
irqha2 ldb intype,x check device type
 cmpb #5 special code for 8274
 beq irqha3
 ldb inmask,x get mask
 andb [instat,x] check status
 beq irqha99 jump if not interrupt
 ldd indev,x get device number
 jmp [inhand,x] goto routine
*
* Special interrupt handler for 8274 type device
*
irqha3 pshs a save count
 ldu instat,x get device address
 lda #2 poll the device
 sta 3,u
 lda 3,u get response
 ldb 2,u
 bitb inmask,x interrupt pending?
 beq 10f no - jump
 leas 1,s yes - clean up stack
 pshs d place device status on stack for handler
 bita #$04 channel B (second device)?
 bne 0f
 leax INTSIZ,x yes - bump pointer
 ldu instat,x
 ldb 2,u get proper status for B side
 stb 1,s place on stack
0 lda 0,s
 anda #!$04
 sta 0,s
 ldd indev,x get device #
 ldu instat,x get device address
 jsr [inhand,x] go process interrupt
 leas 2,s clean up stack (remove device status)
 rts return from interrupt
10 leax INTSIZ,x skip over second device
 puls a restore count
 deca also skip second device
*
irqha99 leax INTSIZ,x get to next entry
 deca dec the count
 bne irqha2 repeat til done
*
 rts *** Unexpected interrupt - what else to do?? **

 global Pdata,Phex,Pspace,Phex2

*
*  These routines map onto ROM routines
*
Pdata jmp [$F800]
Pspace jmp [$F804]
Phex2 pshs b
 bsr Phex
 puls a
*-- Fall through to Phex
Phex pshs a
 lsra
 lsra
 lsra
 lsra
 bsr phex
 puls a
*
phex jmp [$F802]

 pag

*
* frqhan
*
* Handle the firq interrupt.  Works like irq.
*

frqhan ldx #fnttab point to table
frqha2 ldb inmask,x get mask
 andb instat,x check status
 beq frqha99
 ldd indev,x
 jmp [inhand,x]
frqha99 leax INTSIZ,x next entry
 cmpx #fntend end of table?
 bne frqha2 loop til done
 rts return

 pag

*
* SWI3 (System call) interrupt handler
*  -- Fetch arguments and place in User Block
*  -- Call system call handler
*  -- Store return parameters on stack
*  -- Return to program (via ROM)
*
swi3han ldx usp get user stack pointer
 leax 10,x point to user pc
 jsr gtuwrd get the pc
 pshs d save it
 tfr d,x point to pc location
 jsr gtubyt get post byte
 stb upostb save in user block
 puls d get pc back
 addd #1 bump past post byte
 std urglst+UPC save in user block
 ldx usp get stack pointer
 leax 10,x point to pc
 jsr ptuwrd set back on stack
*
 jsr syscl process system call
*
 ldx usp point to user stack
 ldb urglst+UCC get c-codes
 jsr ptubyt put on stack
 leax 1,x point to d reg
 ldd urglst+UD get d reg
 jsr ptuwrd put on stack
 leax 3,x point to x reg
 ldd urglst+UX get x reg
 jsr ptuwrd put on stack
 leax 6,x point to pc
 ldd urglst+UPC get user pc
 jsr ptuwrd put on stack
 rts return

 pag

*
* idle_tsk
*
* CPU Idle task
*
idle_tsk pshs d,x,y,u save registers
99 puls d,x,y,u,pc return
