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
+111 -16

No files matched your search

+4 -1
View File
@@ -56,7 +56,10 @@ wedge_arrow_L:
wedge_arrow_H: wedge_arrow_H:
jsr tape_head_align jsr tape_head_align
jmp basic_main_loop
jsr print_return
ldx #B_ERR_BREAK
jmp do_basic_error
#endif #endif
@@ -20,4 +20,8 @@ return_from_interrupt:
plx_trash_a plx_trash_a
pla pla
// FALLTROUGH
return_from_interrupt_rti:
rti rti
+2 -3
View File
@@ -13,9 +13,8 @@ hw_entry_nmi:
// Call interrupt routine (only if initialised) // Call interrupt routine (only if initialised)
pha pha
lda NMINV lda NMINV+1 // consider zeropage NMI address as uninitialized vector
ora NMINV+1 beq !+ // this allows safer interrupt vector modifications
beq !+
pla pla
jmp (NMINV) 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
+3
View File
@@ -23,6 +23,9 @@ vector_real:
sty MEMUSS + 1 sty MEMUSS + 1
stx MEMUSS + 0 stx MEMUSS + 0
lda #$00
sta NMINV + 1 // our routine considers this a disabled interrupt
// Select routine variant - store or restore vectors // Select routine variant - store or restore vectors
ldy #$1F ldy #$1F
bcc vector_restore bcc vector_restore
+10 -3
View File
@@ -22,11 +22,18 @@ panic:
jsr setup_vicii jsr setup_vicii
jsr clear_screen jsr clear_screen
// Disable NMIs - our routine checks for NULL vector // Disable NMIs
#if CONFIG_TAPE_HEAD_ALIGN
jsr nmi_lock
#else
lda #$00 lda #$00
sta NMINV+0 sta NMINV + 1 // our routine does not allow zeropage vectors
sta NMINV+1
#endif
// Display KERNAL PANIC message // Display KERNAL PANIC message
+3
View File
@@ -45,6 +45,9 @@ load_tape_normal:
// Start playing // Start playing
jsr tape_common_prepare_cia jsr tape_common_prepare_cia
jsr tape_ask_play jsr tape_ask_play
#if CONFIG_TAPE_HEAD_ALIGN
jsr tape_prepare_reading
#endif
// FALLTROUGH // FALLTROUGH
+3
View File
@@ -32,6 +32,9 @@ load_tape_auto:
// Start playing // Start playing
jsr tape_common_prepare_cia jsr tape_common_prepare_cia
jsr tape_ask_play jsr tape_ask_play
#if CONFIG_TAPE_HEAD_ALIGN
jsr tape_prepare_reading
#endif
// FALLTROUGH // FALLTROUGH
+11 -3
View File
@@ -104,6 +104,17 @@ tape_wait_first_pulse:
tape_wait_play_done: tape_wait_play_done:
lda #$01
sta CAS1 // set the interlock
#if CONFIG_TAPE_HEAD_ALIGN
rts
tape_prepare_reading:
#endif // CONFIG_TAPE_HEAD_ALIGN
#if CONFIG_MB_MEGA_65 #if CONFIG_MB_MEGA_65
// Display confirmation // Display confirmation
@@ -115,9 +126,6 @@ tape_wait_play_done:
// Prepare for reading // Prepare for reading
lda #$01
sta CAS1 // set the interlock
jsr print_return jsr print_return
jmp tape_screen_off_motor_on jmp tape_screen_off_motor_on
+11 -6
View File
@@ -12,9 +12,7 @@
// XXX for Mega65 it can be done more easily, by just disabling the badlines // XXX for Mega65 it can be done more easily, by just disabling the badlines
// XXX consider "PRESS PLAY ON TAPE" before start, with option to stop
// XXX document CONFIG_TAPE_HEAD_ALIGN option // XXX document CONFIG_TAPE_HEAD_ALIGN option
// XXX block NMIs
// XXX fix Mega65 build compilation issues // XXX fix Mega65 build compilation issues
@@ -50,12 +48,22 @@ tape_head_align:
#else #else
// Make sure user really wants to launch the tool
jsr tape_ask_play
// Disable interrupts, set proper I/O values // Disable interrupts, set proper I/O values
jsr CLALL jsr CLALL
sei sei
jsr IOINIT jsr IOINIT
jsr cint_legacy jsr CINT
jsr nmi_lock
// Prepare CIA timers
ldx #$02 // set timer A to 3 ticks ldx #$02 // set timer A to 3 ticks
jsr tape_common_prepare_cia_by_x jsr tape_common_prepare_cia_by_x
@@ -111,8 +119,6 @@ tape_head_align:
dey dey
bne !- bne !-
// Set graphics mode // Set graphics mode
lda #$28 // graphics start at $2000, color at $0800 lda #$28 // graphics start at $2000, color at $0800
@@ -252,7 +258,6 @@ tape_head_align_drawn:
beq tape_head_align_loop_1 beq tape_head_align_loop_1
tape_head_align_quit: tape_head_align_quit:
// It makes no sense to return to system, as we already destroyed // It makes no sense to return to system, as we already destroyed