; DMA and HDMA: undisbeliever's hardware-glitch tests (scpu-a-dma-bug, ; dma-ends-at-hdma-start, quick-model-1-dma-crash, hdma-timing-demonstration, ; hdmaen_latch_test(_2); zlib, Marcus Rowe) redone as CPU measurements. The ; family spec is docs/probe-suite.md, "DMA and HDMA". Which bug is whose ; (wiki/Errata l.96-97): CPU version 1 can crash when a DMA ends right before ; HDMA; version 2 (S-CPU-A) can drop a DMA after an HDMA to $2100. The ; console under test is version 1, so SCPA is a negative control there and ; CRSH, last in the suite, is the one that can freeze it. HDMAEN_ = HDMAEN ; --- HDOR: does every channel transfer before any reloads? 8 bytes ------------- ; ; A read-direction HDMA (B-bus $2134, MPYL = 5) writes into the byte that is ; another channel's next header. Variant 0: ch0 sends T+1 to $21FF and then ; reloads its header from T+2; ch1 reads MPYL into T+2. If all transfers come ; before any reload (hdma-timing-demonstration.asm l.72-73; ares hdmaRun()), ; ch0's new line counter is 5; if each channel reloads right after its own ; transfer, 3. Variant 1 swaps the channels (the reader goes first: 5 either ; way). Per variant at line 1: NLTR0, A2A0 low, NLTR1, A2A1 low. HD_T = $7E2400 hd_var = $50 ; the variant (AjWaitLine uses tmp) TestHdmaOrder: .a8 stz hd_var ; variant @variant: jsr WaitVblankStart stz HDMAEN lda #$01 sta f:HD_T sta f:HD_T+1 lda #$03 sta f:HD_T+2 lda #$00 sta f:HD_T+3 sta f:HD_T+4 sta f:HD_T+5 lda #$05 ; MPYL = 5 x 1 sta M7A stz M7A lda #$01 sta M7B ldx #$0000 ; writer channel ldy #$0010 ; reader channel lda hd_var beq :+ ldx #$0010 ldy #$0000 : stz $4300,x ; writer: A to B, direct, one register, $21FF lda #$FF sta $4301,x lda #HD_T sta $4303,x lda #^HD_T sta $4304,x tyx lda #$80 ; reader: B to A, from MPYL sta $4300,x lda #$34 sta $4301,x lda #<(HD_T+1) sta $4302,x lda #>(HD_T+1) sta $4303,x lda #^HD_T sta $4304,x lda #$03 sta HDMAEN lda #1 jsr AjWaitLine lda hd_var asl a asl a tax lda $430A sta RES,x lda $4308 sta RES+1,x lda $431A sta RES+2,x lda $4318 sta RES+3,x stz HDMAEN inc hd_var lda hd_var cmp #2 beq :+ jmp @variant : rts ; --- HDSW: what HDMA costs a line, and what its init costs, 324 bytes ----------- ; ; A stopwatch around the per-line HDMA point (ares scanline(): master clock 1104, ; dot 276; anomie: dot 278): an H+V IRQ at line 50+s dot 225+s (s = 0-7), ; a latch, 8 NOPs (96 clocks, FastROM), a latch: per sample H1 (16), H2 (16). ; Configs, set before V = 0, force blank: 0 HDMA off; 1 one channel, direct, ; a 127-line non-repeat entry (no transfer, no reload on these lines); 2 the ; same, repeat (one byte a line); 3 repeat, mode 3 (four bytes); 4 one-line ; entries (a transfer and a reload every line); 5 eight channels as 2; 6 one ; indirect channel, one-line entries (224). Then init at line 0: H+V IRQ at ; 261/300+s (s = 0-3), the same stopwatch, H1, H2, V2: configs HDMA off, one ; direct, one indirect, eight direct, eight indirect (100). anomie: init ; costs 18 + 8 per direct + 24 per indirect channel. hs_cfg = $50 ; 16-bit hs_s = $52 ; 16-bit hs_out = $54 ; 16-bit HS_RELOAD = $7E2600 ; [$01, $00] x 225, $00 HS_INDIRECT = $7E2800 ; [$01, address] x 225, $00 TestHdmaStopwatch: .a8 GO_FAST ldx #$0000 ; the WRAM tables : lda #$01 sta f:HS_RELOAD,x lda #$00 sta f:HS_RELOAD+1,x inx inx cpx #450 bne :- sta f:HS_RELOAD,x ldx #$0000 : lda #$01 sta f:HS_INDIRECT,x lda #$00 sta f:HS_INDIRECT+1,x lda #$29 sta f:HS_INDIRECT+2,x inx inx inx cpx #675 bne :- lda #$00 sta f:HS_INDIRECT,x ldx #$0000 stx hs_out stx hs_cfg @cfg: jsr WaitVblankStart lda hs_cfg jsr HsSetup ldx #$0000 stx hs_s @sample: rep #$20 .a16 lda hs_s clc adc #50 sta sync_v lda hs_s clc adc #225 sta sync_h sep #$20 .a8 jsr HsSample inc hs_s lda hs_s cmp #8 bne @sample inc hs_cfg lda hs_cfg cmp #7 bne @cfg stz hs_cfg ; init: HDMA off, 1 direct, 1 indirect, 8 direct, 8 indirect @init: lda #200 jsr AjWaitLine lda hs_cfg tax lda HsInitCfg,x jsr HsSetup stz hs_s @isample: rep #$20 .a16 lda #261 sta sync_v lda hs_s and #$00FF clc adc #300 sta sync_h sep #$20 .a8 jsr HsSample ldx hs_out ; and V2 lda OPVCT sta RES,x inx stx hs_out lda #200 jsr AjWaitLine inc hs_s lda hs_s cmp #4 bne @isample inc hs_cfg lda hs_cfg cmp #5 bne @init stz HDMAEN GO_SLOW rts ; One stopwatch sample at (sync_v, sync_h): H1, H2 to RES (4 bytes). HsSample: .a8 lda #$30 sta sync_m ldx hs_out jsr SyncWai lda a:SLHV lda STAT78 lda OPHCT sta RES,x lda OPHCT sta RES+1,x NOPS 8 lda a:SLHV lda STAT78 lda OPHCT sta RES+2,x lda OPHCT sta RES+3,x lda TIMEUP stz NMITIMEN inx inx inx inx stx hs_out rts ; A = config 0-6 (HDSW's list), or 7 = eight indirect channels. HsSetup: .a8 stz HDMAEN cmp #0 bne :+ rts : cmp #5 beq @eight cmp #7 beq @eight_indirect ldx #$0000 jsr HsChannel lda #$01 sta HDMAEN rts @eight: lda #1 bra :+ @eight_indirect: lda #6 : sta tmp ldx #$0000 : lda tmp phx jsr HsChannel plx rep #$20 .a16 txa clc adc #$0010 tax sep #$20 .a8 cpx #$0080 bne :- lda #$FF sta HDMAEN rts ; X = channel offset, A = 1 non-repeat, 2 repeat, 3 mode 3 repeat, 4 one-line, ; 6 indirect one-line (5 is treated as 2). HsChannel: .a8 cmp #5 bne :+ lda #2 : pha stz $4300,x lda #$FF sta $4301,x pla cmp #1 bne :+ lda #HsNonRep sta $4303,x stz $4304,x rts : cmp #2 bne :+ lda #HsRep sta $4303,x stz $4304,x rts : cmp #3 bne :+ lda #$03 sta $4300,x lda #$FE sta $4301,x lda #HsRep4 sta $4303,x stz $4304,x rts : cmp #4 bne :+ lda #HS_RELOAD sta $4303,x lda #^HS_RELOAD sta $4304,x rts : lda #$40 ; indirect sta $4300,x lda #HS_INDIRECT sta $4303,x lda #^HS_INDIRECT sta $4304,x sta $4307,x rts ; --- HDLT: when does an HDMAEN write take effect? 672 bytes --------------------- ; ; hdmaen_latch_test(_2): the channel set up mid-frame with NLTR = 0 and A2A = ; a 4-byte table (DMAP $03 to CGADD/CGDATA), then an H-IRQ at line L, a ; latch, and `stx a:HDMAEN` (8-bit X) 24 master clocks later. Early on line ; L+1: NLTR and A2A. NLTR still 0: the write missed line L's HDMA point; $FF: ; it latched (and A2A + 4 if it transferred, which needs the hidden ; transfer flag that V = 0 clears). Sweep: channels 0 and 7, HTIME 240-279, ; L = 100 and 101; per entry H at the latch (2), NLTR, A2A low minus the ; table's (640). Then the transfer flag: per channel 0-7 the write at ; 100/100, NLTR and A2A delta read on lines 101 and 102 (32). hl_h = $50 ; 16-bit HTIME hl_out = $52 ; 16-bit hl_chan = $54 ; 16-bit register offset ($00, $10 ... $70) hl_mask = $56 hl_par = $57 TestHdmaenLatch: .a8 GO_FAST ldx #$0000 stx hl_out stx hl_chan lda #$01 sta hl_mask @chan: stz hl_par @parity: rep #$20 .a16 lda #240 sta hl_h sep #$20 .a8 @h: lda #80 jsr AjWaitLine jsr HlPrepare rep #$20 .a16 lda hl_h sta sync_h lda hl_par and #$0001 clc adc #100 sta sync_v sep #$20 .a8 lda #$30 sta sync_m ARM_IRQ sep #$10 .i8 ldx hl_mask wai lda a:SLHV stx a:HDMAEN_ rep #$10 .i16 lda TIMEUP stz NMITIMEN ldx hl_out lda STAT78 lda OPHCT sta RES,x lda OPHCT sta RES+1,x lda sync_v inc a jsr AjWaitLine stz HDMAEN ldx hl_out jsr HlRead rep #$20 .a16 lda hl_out clc adc #4 sta hl_out inc hl_h lda hl_h cmp #280 sep #$20 .a8 beq :+ jmp @h : inc hl_par lda hl_par cmp #2 beq :+ jmp @parity : lda hl_mask ; channel 0, then 7 cmp #$80 beq @flag lda #$80 sta hl_mask ldx #$0070 stx hl_chan jmp @chan @flag: ; the transfer flag, channels 0-7 ldx #$0000 stx hl_chan lda #$01 sta hl_mask @flagchan: lda #80 jsr AjWaitLine jsr HlPrepare rep #$20 .a16 lda #100 sta sync_h sta sync_v sep #$20 .a8 lda #$30 sta sync_m ARM_IRQ wai lda hl_mask sta HDMAEN lda TIMEUP stz NMITIMEN lda #101 jsr AjWaitLine ldx hl_out dex dex jsr HlRead ; lands at RES+hl_out lda #102 jsr AjWaitLine ldx hl_out jsr HlRead ; RES+hl_out+2 stz HDMAEN rep #$20 .a16 lda hl_out clc adc #4 sta hl_out lda hl_chan clc adc #$0010 sta hl_chan sep #$20 .a8 asl hl_mask bcc @flagchan GO_SLOW rts ; Channel hl_chan: DMAP $03 to CGADD, A1T = A2A = HlTable, NLTR = 0. HlPrepare: .a8 stz HDMAEN ldx hl_chan lda #$03 sta $4300,x lda #$21 sta $4301,x lda #HlTable sta $4303,x sta $4309,x stz $4304,x stz $430A,x rts ; RES,x+2 = NLTR, RES,x+3 = A2A low - HlTable low (for HlRead from the sweep); ; for the flag probe X points 2 earlier, so the pair lands at RES,x. HlRead: .a8 phx ldx hl_chan lda $430A,x xba lda $4308,x sec sbc # WMDATA runs, DAS0 is read back ; (non-zero: the DMA did not happen), retried once, HDMA back on. 16 frames. ; Stores: tests (16), first failures (16), failures again on the retry (16), ; V of the first failure (16), its DAS (16), WRAM bytes not 1 (16). sc_tests = $50 ; 16-bit sc_fail = $52 sc_fail2 = $54 sc_v = $56 sc_das = $58 SC_BUF = $7F0000 TestScpuA: .a8 ldx #$0000 stx sc_tests stx sc_fail stx sc_fail2 dex stx sc_v stx sc_das jsr WaitVblankStart lda #$80 sta INIDISP stz $4310 ; HDMA ch1: direct, one register, INIDISP stz $4311 ldx #ScHdmaTable stx $4312 stz $4314 lda #$08 ; DMA ch0: fixed source, one register, WMDATA sta $4300 lda #$80 sta $4301 ldx #ScSuccess stx $4302 stz $4304 ldx #.loword(SC_BUF) stx WMADDL lda #(^SC_BUF) & 1 sta WMADDH lda #$02 sta HDMAEN ldx #20 stx HTIMEL lda #$10 sta NMITIMEN @line: lda TIMEUP wai bit HVBJOY bmi @line stz HDMAEN lda #1 sta $4305 stz $4306 sta MDMAEN ldx $4305 beq @ok stx sc_das sta MDMAEN ; retry ldx $4305 beq :+ inc sc_fail2 : rep #$20 .a16 inc sc_fail lda sc_v cmp #$FFFF sep #$20 .a8 bne :+ lda SLHV lda STAT78 lda OPVCT sta sc_v lda OPVCT and #$01 sta sc_v+1 : lda #$02 sta WMDATA @ok: lda #$02 sta HDMAEN rep #$20 .a16 inc sc_tests lda sc_tests cmp #16 * 225 sep #$20 .a8 bne @line stz NMITIMEN stz HDMAEN lda #$80 sta INIDISP rep #$20 .a16 lda sc_tests sta RES lda sc_fail sta RES+2 lda sc_fail2 sta RES+4 lda sc_v sta RES+6 lda sc_das sta RES+8 stz RES+10 sep #$20 .a8 ldx #$0000 : lda f:SC_BUF,x cmp #1 beq @good rep #$20 .a16 inc RES+10 sep #$20 .a8 @good: inx cpx #16 * 225 bne :- rts ; --- DEND: a DMA that ends at HDMA init, 578 bytes ------------------------------ ; ; dma-ends-hdma-start-1-ch: HDMA ch2 writes BG2HOFS twice per line from a ; table of two 120-line repeat entries; a long DMA (MPYL = 0 -> one WRAM ; byte, B to A, fixed) started from an H+V IRQ at 232/100 ends near line 0, ; where HDMA init runs. undisbeliever's 1/1/1 console: "No crash. HDMA ; Failure detected." (dma-ends-hdma-start-1-ch.asm l.47-50). First, HDMA ; off, the length N0 whose end is the first at V = 0, H >= 8 (2), and the end ; H of N0-24 .. N0+7 (64). Then HDMA on, each length twice (both field ; parities): end H lo, end H hi | (V != 0) << 7, A2A2 - table low and NLTR2 ; on line 0 at H >= 100, A2A2 - table (16) and NLTR2 at the next V-Blank ; (expected 452, $8F) = 8 bytes x 64. de_n = $50 ; 16-bit DMA length de_lo = $52 de_hi = $54 de_out = $56 de_i = $58 de_en = $5A ; HDMAEN for the frame de_v = $5C ; scratch: V while polling for line 0 DE_BYTE = $1E90 ; WRAM byte the DMA writes TestDmaEnd: .a8 GO_FAST stz M7A stz M7A stz M7B stz de_en ; binary search: the smallest N whose end is at V = 0, H >= 8 ldx #3000 stx de_lo ldx #8000 stx de_hi @search: rep #$20 .a16 lda de_lo cmp de_hi bcs @found clc adc de_hi lsr a sta de_n sep #$20 .a8 jsr DeFrame jsr DePastInit ; C set if the end is at V = 0, H >= 8 or later rep #$20 .a16 bcc :+ lda de_n sta de_hi bra @search : lda de_n inc a sta de_lo bra @search @found: lda de_lo sta RES sec sbc #24 sta de_n lda #2 sta de_out stz de_i @cal: sep #$20 .a8 jsr DeFrame ldx de_out lda hval sta RES,x lda hval+1 sta RES+1,x rep #$20 .a16 inx inx stx de_out inc de_n inc de_i lda de_i cmp #32 bne @cal ; HDMA on: N0-24 .. N0+7, twice each lda RES sec sbc #24 sta de_n stz de_i sep #$20 .a8 lda #$04 sta de_en @sample: jsr DeFrame ldx de_out lda hval sta RES,x lda vval ora vval+1 beq :+ lda #$80 : ora hval+1 sta RES+1,x lda tmp ; line-0 A2A2 low - table low, NLTR2 sta RES+2,x lda tmp+1 sta RES+3,x lda #226 ; the next V-Blank jsr AjWaitLine rep #$20 .a16 lda $4328 sec sbc #DeHdmaTable ldx de_out sta RES+4,x sep #$20 .a8 lda $432A sta RES+6,x stz RES+7,x rep #$20 .a16 txa clc adc #8 sta de_out lda de_i and #$0001 beq :+ inc de_n ; each length twice : inc de_i lda de_i cmp #64 sep #$20 .a8 bne @sample stz HDMAEN stz NMITIMEN GO_SLOW rts ; One frame: at line 226 reset A2A2/NLTR2, HDMAEN = de_en; an H+V IRQ at ; 232/100; the DMA of de_n bytes; the end latched (hval, vval); then on line ; 0 at H >= 100, tmp = A2A2 low - table low, tmp+1 = NLTR2. DeFrame: .a8 lda #226 jsr AjWaitLine stz HDMAEN lda #$02 ; HDMA ch2: two writes, BG2HOFS sta $4320 lda #$0F sta $4321 ldx #DeHdmaTable stx $4322 stz $4324 ldx #$0000 stx $4328 stz $432A lda de_en sta HDMAEN lda #$88 ; DMA ch1: B to A, fixed, MPYL -> DE_BYTE sta $4310 lda #$34 sta $4311 ldx #DE_BYTE stx $4312 stz $4314 ldx de_n stx $4315 rep #$20 .a16 lda #100 sta sync_h lda #232 sta sync_v sep #$20 .a8 lda #$30 sta sync_m jsr SyncWai lda #$02 sta MDMAEN lda a:SLHV lda TIMEUP stz NMITIMEN jsr ReadCounters @line0: ; wait for line 0, H >= 100 lda SLHV lda STAT78 lda OPHCT sta tmp lda OPHCT and #$01 sta tmp+1 lda OPVCT sta de_v lda OPVCT and #$01 ora de_v bne @line0 ; V != 0 lda tmp+1 bne @read lda tmp cmp #100 bcc @line0 @read: lda $4328 sec sbc #= 8 @after: sec rts @before: clc rts ; --- CRSH: the version-1 DMA/HDMA crash, last in the suite ---------------------- ; ; quick-model-1-dma-crash's IRQ variant, swept: from an H+V IRQ at line 1 or ; 2, HTIME 21-24, the handler (bank $00 SlowROM, as the original) sets HDMA ; ch7 to a mid-frame repeat state (A2A7 = table + 1, NLTR7 = $FF, mode 3 to ; CGADD/CGDATA) and starts DMA ch0 of N = 1-12 bytes ($FF) to OAMDATA, then ; 20 NOPs and the NLTR7 check (HDMA ran once: $FE). 96 attempts, first all ; with HDMA off (calibration: the DMA's end H latched), then with HDMA on. ; No console result is recorded; the wiki says only that a v1 CPU "can crash ; if DMA finishes right before HDMA happens". ; ; This test writes battery RAM itself (cur_slot) and resumes after a reset: ; +0 'K' once set up, +1 next attempt (0-191), +2 attempts lost, +3 the ; attempt running ($FF none), +4-7 0 ; +8 per attempt 2 bytes: calibration H lo, H hi; real attempts status ; (b0 NLTR7 = $FE at the end, b2 DAS0 = 0, b7 finished), NLTR7 ; +392 up to 8 records of 16 bytes for lost attempts: attempt, kind (1 BRK/COP ; reached, 2 still running at the NMI after it, 3 never came back: reset), ; stacked P, PC (16), PBR, A, X, Y, S (16 each), H lo, V lo ; A runaway CPU that reaches BRK/COP/ABORT or is still running at the next ; NMI is recorded and soft-resets; a hard freeze needs the reset button. ; After 4 lost attempts the rest are skipped. CR_ATTEMPTS = 192 CR_ARMED = $1E50 ; WRAM: 1 while an attempt runs CR_SAVE_S = $1E52 ; WRAM: S before the attempt's WAI CR_REGS = $1E54 ; WRAM: A, X, Y, S at the trap (8) cr_n = $50 ; 16-bit DMA length cr_hdmaen = $52 cr_i = $54 ; 16-bit attempt TestCrash: .a8 lda #$00 ; WRAM keeps its value through a reset sta CR_ARMED ldx cur_slot lda f:SRAM,x cmp #'K' beq @resume lda #$00 ; first time this run: clear the slot ldy #520 : sta f:SRAM,x inx dey bne :- ldx cur_slot lda #'K' sta f:SRAM,x lda #$FF sta f:SRAM+3,x @resume: ldx cur_slot lda f:SRAM+3,x cmp #$FF beq @next ; an attempt was running when the console was reset: record it lda #3 jsr CrRecord @next: ldx cur_slot lda f:SRAM+2,x cmp #4 bcs @done lda f:SRAM+1,x cmp #CR_ATTEMPTS bcs @done sta cr_i stz cr_i+1 sta f:SRAM+3,x ; running jsr CrAttempt ldx cur_slot lda #$FF sta f:SRAM+3,x lda f:SRAM+1,x inc a sta f:SRAM+1,x bra @next @done: stz NMITIMEN stz HDMAEN rts ; One attempt cr_i: j = i mod 96, N = 1 + j mod 12, HTIME = 21 + (j / 12) ; mod 4, line 1 + j / 48. The handler jumps to CrDone, which returns from ; here. CrAttempt: .a8 rep #$20 .a16 lda cr_i cmp #96 bcc :+ sbc #96 : pha ldx #$0000 ; X = j / 12, A = j mod 12 : cmp #12 bcc :+ sbc #12 inx bra :- : inc a sta cr_n txa and #$0003 clc adc #21 sta sync_h pla ldx #1 cmp #48 bcc :+ inx : stx sync_v sep #$20 .a8 lda #$80 ldx cr_i cpx #96 bcs :+ lda #$00 ; calibration: HDMA off : sta cr_hdmaen jsr WaitVblankStart lda #$80 sta INIDISP stz HDMAEN stz OAMADDL stz OAMADDH rep #$20 .a16 lda #.loword(CrIrqCal) ldx cr_i cpx #96 bcc :+ lda #.loword(CrIrqHit) : sta IRQ_VEC lda #.loword(CrTrap) sta BRK_VEC lda #.loword(CrWatch) sta NMI_VEC sep #$20 .a8 stz IRQ_VEC+2 stz BRK_VEC+2 stz NMI_VEC+2 lda #$30 ldx cr_i cpx #96 bcc :+ lda #$B0 ; real attempts: the NMI watchdog too : sta sync_m lda RDNMI ; no NMI pending when NMI is enabled ARM_IRQ lda #1 sta CR_ARMED rep #$20 .a16 tsc sta CR_SAVE_S sep #$20 .a8 cli wai ; not reached: the handler ends in CrDone CrDone: sei stz NMITIMEN rep #$30 .a16 .i16 lda CR_SAVE_S tcs lda #$0000 tcd sep #$20 .a8 pha plb stz CR_ARMED lda TIMEUP ldx cr_i cpx #96 bcs @real jsr ReadCounters ; the DMA's end, latched in the handler rep #$20 .a16 lda cr_i asl a clc adc cur_slot tax sep #$20 .a8 lda hval sta f:SRAM+8,x lda hval+1 sta f:SRAM+9,x rts @real: lda #$80 sta tmp ldx $4305 ; DAS0 bne :+ lda #$04 tsb tmp : lda $437A cmp #$FE bne :+ lda #$01 tsb tmp : lda tmp pha rep #$20 .a16 lda cr_i asl a clc adc cur_slot tax sep #$20 .a8 pla sta f:SRAM+8,x lda $437A sta f:SRAM+9,x stz HDMAEN rts ; The attempt handlers (bank $00 SlowROM, via IrqTrampoline): the original's ; body; the calibration copy latches H right after MDMAEN. .macro CR_BODY calibrate lda a:SLHV stz a:CGADD lda #$FF sta a:CGDATA sta a:CGDATA lda #$03 sta a:$4370 lda #$21 sta a:$4371 ldx #CrHdmaTable stx a:$4372 stz a:$4374 ldx #CrHdmaTable + 1 stx a:$4378 lda #$FF sta a:$437A lda z:cr_hdmaen sta a:HDMAEN_ stz a:$4300 lda #$04 sta a:$4301 ldx #CrDmaBlock stx a:$4302 stz a:$4304 ldx z:cr_n stx a:$4305 lda #$01 sta a:MDMAEN .if calibrate lda a:SLHV .endif .ifdef CRASH_SELFTEST ; test builds only: attempt 101 runs into BRK .if !calibrate lda z:cr_i cmp #101 bne :+ brk .byte 0 : .endif .endif .repeat 20 nop .endrepeat lda a:$437A cmp #$FE beq :+ stz a:HDMAEN_ : jml CrDone .endmacro CrIrqCal: CR_BODY 1 CrIrqHit: CR_BODY 0 ; BRK, COP, ABORT (or an emulation-mode vector) during an attempt: record ; and soft-reset. Outside an attempt: return. CrTrap: clc xce ; native again; C = the old E rep #$30 sta f:CR_REGS txa sta f:CR_REGS+2 tya sta f:CR_REGS+4 tsc sta f:CR_REGS+6 sep #$20 lda f:CR_ARMED bne :+ rep #$30 lda f:CR_REGS rti : lda #1 bra CrLost ; NMI: an attempt still armed at the V-Blank after its line is lost in ; running code. CrWatch: rep #$30 sta f:CR_REGS txa sta f:CR_REGS+2 tya sta f:CR_REGS+4 tsc sta f:CR_REGS+6 sep #$20 lda f:CR_ARMED bne :+ rep #$30 lda f:CR_REGS sep #$20 bit a:RDNMI rti : lda #2 CrLost: .a8 .i16 ldx #$1FFF ; S may be anywhere: the registers are saved txs pha rep #$20 lda #$0000 tcd sep #$20 lda #$00 pha plb pla jsr CrRecord lda #$00 sta f:CR_ARMED jml Reset ; A = kind. Record the running attempt (cur_slot), count it lost, move on. CrRecord: .a8 .i16 sta tmp lda a:SLHV ldx cur_slot lda f:SRAM+2,x ; lost so far cmp #8 bcc :+ jmp @count : rep #$20 .a16 and #$00FF asl a asl a asl a asl a clc adc cur_slot adc #392 tax ; X = the record sep #$20 .a8 phx ldx cur_slot lda f:SRAM+3,x ; the attempt plx sta f:SRAM,x lda tmp sta f:SRAM+1,x cmp #3 beq @latch ; a reset leaves no registers rep #$20 .a16 lda f:CR_REGS+6 ; S at the trap: P, PC, PBR just above it tay sep #$20 .a8 lda a:1,y sta f:SRAM+2,x lda a:2,y sta f:SRAM+3,x lda a:3,y sta f:SRAM+4,x lda a:4,y sta f:SRAM+5,x rep #$20 .a16 lda f:CR_REGS sta f:SRAM+6,x lda f:CR_REGS+2 sta f:SRAM+8,x lda f:CR_REGS+4 sta f:SRAM+10,x lda f:CR_REGS+6 sta f:SRAM+12,x sep #$20 .a8 @latch: lda STAT78 lda OPHCT sta f:SRAM+14,x lda OPHCT lda OPVCT sta f:SRAM+15,x @count: ldx cur_slot lda f:SRAM+2,x inc a sta f:SRAM+2,x lda f:SRAM+3,x ; go on after the lost attempt inc a sta f:SRAM+1,x lda #$FF sta f:SRAM+3,x rts .segment "RODATA" HsInitCfg: .byte 0, 2, 6, 5, 7 HsNonRep: .byte $7F, 0, $7F, 0, 0 HsRep: .byte $FF .res 127, 0 .byte 0 HsRep4: .byte $FF .res 508, 0 .byte 0 HlTable: .word 0, $001F .byte 0 ScSuccess: .byte 1 ScHdmaTable: .repeat 64 .byte $02, $0F, $02, $08 .endrepeat .byte 0 DeHdmaTable: .byte $F8 .res 240, 0 .byte $F8 .res 240, 0 .byte 0 CrDmaBlock: .res 16, $FF CrHdmaTable: .byte $FF .repeat 127, II .byte 0, 0, <(II << 5), >(II << 5) .endrepeat .byte 0 .segment "CODE"