@ SPDX-License-Identifier: MIT @ The probes, the escape and the IRQ handler. Everything here runs from @ IWRAM (one cycle per access, 32-bit bus), so cycle counts are free of @ cartridge wait states and the prefetch buffer. @ @ The retail BIOS's undefined-instruction handler (vector 0x04 -> 0x1C), @ with the debugging-enable byte at 0x0800009C not 0xA5, saves its registers @ at 0x03007FE0-0x03007FEF and returns with SUBS PC, R14, #4. For a Thumb @ instruction R14_und is its address + 2, so the return lands 2 bytes @ *before* it: the instruction ahead of the probe runs again, then the @ probe traps again, forever. The probe is preceded by ADDS R7, #1, so a @ trap shows as R7 climbing. Timer 0 is armed before each probe with an @ overflow IRQ about 4096 cycles out; if the probe has not finished by then, @ irq_handler records where the CPU was and returns to `landing` instead. .syntax unified .equ TM0CNT, 0x04000100 .equ TM1CNT_L, 0x04000104 .equ REG_IE, 0x04000200 .equ LR_PAD, 0x02020000 @ zero-filled by main.c .equ TIMER0_ARM, (0x00C0 << 16) | 0xF000 @ enable + IRQ, 4096 cycles @ probe_io (main.c) word offsets .equ IO_RET, 0 @ 1 fell through, 2 branched into the sled, 3 caught by the IRQ .equ IO_T0, 4 .equ IO_T1, 8 .equ IO_R7, 12 .equ IO_R6, 16 .equ IO_PC, 20 @ caught: address of the next instruction .equ IO_CPSR, 24 @ caught: the interrupted CPSR .equ IO_SP, 28 .section .iwram, "ax", %progbits .align 2 @ u32 thumb_probe(void): run the halfword at thumb_slot once. .thumb .global thumb_probe .type thumb_probe, %function .thumb_func thumb_probe: push {r4-r7, lr} ldr r3, =probe_io mov r2, sp str r2, [r3, #IO_SP] movs r6, #0 movs r7, #0 ldr r4, =LR_PAD @ a BL-suffix or BLX-suffix reading of mov lr, r4 @ E8xx would go to LR + offset * 2 ldr r5, =TM0CNT ldr r4, =TIMER0_ARM ldr r0, =TM1CNT_L str r4, [r5] @ arm the escape ldrh r1, [r0] @ t0 adds r7, #1 @ re-runs on every trap return .global thumb_slot thumb_slot: .hword 0x46C0 @ patched by main.c b t_fell @ slot + 2: falling through, or a branch to here t_sled: @ slot + 4: a branch with offset j lands .rept 128 @ at slot + 4 + 2j and leaves r6 = 128 - j adds r6, #1 .endr b t_branched t_fell: ldrh r2, [r0] @ t1, reached through one B movs r4, #1 b t_done t_branched: ldrh r2, [r0] movs r4, #2 t_done: movs r0, #0 str r0, [r5] @ disarm the escape str r4, [r3, #IO_RET] str r1, [r3, #IO_T0] str r2, [r3, #IO_T1] str r7, [r3, #IO_R7] str r6, [r3, #IO_R6] movs r0, r4 pop {r4-r7} pop {r1} bx r1 .pool @ Where irq_handler sends a caught probe: Thumb, System mode, IRQs masked. @ r4-r11 are as the probe left them (the BIOS IRQ path saves only r0-r3, @ r12 and lr, and irq_handler touches only r0-r2). .align 2 .global landing landing: ldr r3, =probe_io ldr r2, [r3, #IO_SP] mov sp, r2 movs r4, #3 str r4, [r3, #IO_RET] str r7, [r3, #IO_R7] str r6, [r3, #IO_R6] movs r0, #3 pop {r4-r7} pop {r1} bx r1 .pool @ u32 arm_probe(void): the same for the word at arm_slot, in ARM state. @ An ARM trap returns to the faulting instruction itself (R14_und = address @ + 4), so here R7 stays at 1 while the trap repeats. .arm .align 2 .global arm_probe .type arm_probe, %function arm_probe: push {r4-r7, lr} ldr r3, =probe_io str sp, [r3, #IO_SP] mov r6, #0 mov r7, #0 ldr r5, =TM0CNT ldr r4, =TIMER0_ARM ldr r0, =TM1CNT_L str r4, [r5] ldrh r1, [r0] add r7, r7, #1 .global arm_slot arm_slot: .word 0xE7F000F0 @ patched by main.c b a_fell a_fell: ldrh r2, [r0] mov r4, #1 mov r0, #0 str r0, [r5] str r4, [r3, #IO_RET] str r1, [r3, #IO_T0] str r2, [r3, #IO_T1] str r7, [r3, #IO_R7] str r6, [r3, #IO_R6] mov r0, r4 pop {r4-r7, lr} bx lr .pool @ The user IRQ handler, called by the BIOS (vector 0x18 -> 0x128) in IRQ mode @ after it pushed r0-r3, r12, lr; the saved lr is at [sp, #20] and the BIOS @ returns with SUBS PC, LR, #4 after popping it. .global irq_handler .type irq_handler, %function irq_handler: ldr r0, =TM0CNT mov r1, #0 str r1, [r0] @ stop timer 0 ldr r0, =REG_IE mov r1, #0x08 strh r1, [r0, #2] @ acknowledge IF bit 3 (timer 0) ldr r2, =probe_io ldr r1, [sp, #20] sub r1, r1, #4 @ the next instruction, either state str r1, [r2, #IO_PC] mrs r1, spsr str r1, [r2, #IO_CPSR] ldr r1, =landing bic r1, r1, #1 add r1, r1, #4 str r1, [sp, #20] mov r1, #0xBF @ System mode, Thumb, IRQs masked msr spsr_c, r1 bx lr @ void irq_unmask(void): clear CPSR I (a caught probe returns with it set). .global irq_unmask .type irq_unmask, %function irq_unmask: mrs r0, cpsr bic r0, r0, #0x80 msr cpsr_c, r0 bx lr .pool