Add interrupt handlers and redo vector locations

Most of these are taken from
https://cc65.github.io/doc/customizing.html, but modified to suit this
setup.
This commit is contained in:
Byron Lathi
2022-03-14 11:54:43 -05:00
parent ff78fd0179
commit fe45331e7a
4 changed files with 68 additions and 7 deletions

View File

@@ -14,12 +14,6 @@
.include "zeropage.inc"
.segment "VECTORS"
.addr _init
.addr _init
.addr _init
; ---------------------------------------------------------------------------
; Place the startup code in a special segment
@@ -49,7 +43,7 @@ _init: LDX #$FF ; Initialize stack pointer to $01FF
; ---------------------------------------------------------------------------
; Call main()
cli
JSR _main
; ---------------------------------------------------------------------------

46
sw/interrupt.s Normal file
View File

@@ -0,0 +1,46 @@
; ---------------------------------------------------------------------------
; interrupt.s
; ---------------------------------------------------------------------------
;
; Interrupt handler.
;
; Checks for a BRK instruction and returns from all valid interrupts.
.import _handle_irq
.export _irq_int, _nmi_int
.segment "CODE"
.PC02 ; Force 65C02 assembly mode
; ---------------------------------------------------------------------------
; Non-maskable interrupt (NMI) service routine
_nmi_int: RTI ; Return from all NMI interrupts
; ---------------------------------------------------------------------------
; Maskable interrupt (IRQ) service routine
_irq_int: PHX ; Save X register contents to stack
TSX ; Transfer stack pointer to X
PHA ; Save accumulator contents to stack
INX ; Increment X so it points to the status
INX ; register value saved on the stack
LDA $100,X ; Load status register contents
AND #$10 ; Isolate B status bit
BNE break ; If B = 1, BRK detected
; ---------------------------------------------------------------------------
; IRQ detected, return
irq: PLA ; Restore accumulator contents
PLX ; Restore X register contents
jsr _handle_irq ; Handle the IRQ
RTI ; Return from all IRQ interrupts
; ---------------------------------------------------------------------------
; BRK detected, stop
break: JMP break ; If BRK is detected, something very bad
; has happened, so stop running

7
sw/irq.c Normal file
View File

@@ -0,0 +1,7 @@
// This is defined in main.c
void puts(const char* s);
void handle_irq() {
puts("Interrupt Detected!\n");
}

14
sw/vectors.s Normal file
View File

@@ -0,0 +1,14 @@
; ---------------------------------------------------------------------------
; vectors.s
; ---------------------------------------------------------------------------
;
; Defines the interrupt vector table.
.import _init
.import _nmi_int, _irq_int
.segment "VECTORS"
.addr _nmi_int ; NMI vector
.addr _init ; Reset vector
.addr _irq_int ; IRQ/BRK vector