safer NMI handling

This commit is contained in:
FeralChild64 committed 2020-06-04 22:19:10 +02:00
1 parent 1c118d9bc9
commit f8e57f1cf2
10 files changed
+114 -19

No files matched your search

@@ -20,4 +20,8 @@ return_from_interrupt:
plx_trash_a
pla
// FALLTROUGH
return_from_interrupt_rti:
rti
+4 -5
View File
@@ -5,17 +5,16 @@
hw_entry_nmi:
sei // do not allow IRQ to interfere, see https://www.c64-wiki.com/wiki/Interrupt
sei // do not allow IRQ to interfere, see https://www.c64-wiki.com/wiki/Interrupt
#if CONFIG_CPU_MOS_6502 && CONFIG_BCD_SAFE_INTERRUPTS
cld // clear decimal flag to allow using it without disabling interrupts
cld // clear decimal flag to allow using it without disabling interrupts
#endif
// Call interrupt routine (only if initialised)
pha
lda NMINV
ora NMINV+1
beq !+
lda NMINV+1 // consider zeropage NMI address as uninitialized vector
beq !+ // this allows safer interrupt vector modifications
pla
jmp (NMINV)
!:
+60
View File
@@ -0,0 +1,60 @@
// #LAYOUT# STD * #TAKE
// #LAYOUT# * KERNAL_0 #TAKE
// #LAYOUT# * * #IGNORE
//
// Routine to lock NMI interrupts, using a trick described here:
// - https://codebase64.org/doku.php?id=base:nmi_lock
//
#if CONFIG_TAPE_HEAD_ALIGN
// XXX consider broader usage of this method to make IO safer
nmi_lock:
// We will have to change NMI vector to point empty routine,
// which will not acknowledge the interrupt - thus preventing
// the NMI to be triggered again
// Our code considers zeropage address invalid, so put
// it there as a preparation
ldx #$00
stx NMINV+1
// We can now safelly replace the vector
lda #<return_from_interrupt_rti
sta NMINV+0
lda #>return_from_interrupt_rti
sta NMINV+1
// Stop timer A
stx CIA2_CRA // $DD0E
// Set timer A to 0, after starting NMI will occur immediately
stx CIA2_TIMALO // $DD04
stx CIA2_TIMAHI // $DD05
// Set timer A as source for NMI
lda #$81
sta CIA2_ICR // $DD0D
// Start timer A to trigger the NMI
lda #$01
sta CIA2_CRA // $DD0E
// All done. Unmaskable interrupts are now masked.
rts
#endif