# Auto-joypad family: spec for CPU-measured, SRAM-recorded console tests Source: undisbeliever `snes-test-roms` @ ac6ef800, `src/hardware-tests/auto-joypad/` (zlib, Marcus Rowe). All 14 `.asm` files plus `_modify-autojoy-during-autojoy.inc` were read in full. None of the auto-joypad tests are in the prebuilt ROM set (`tests/test_roms/undisbeliever/snes-test-roms/` only has the 2021 HDMA/INIDISP/DMA-bug ROMs). They exist only as source. Target: Super Famicom, 5A22 v1, PPU1 v1, PPU2 v1, NTSC, one standard pad in port 1. The author's recorded console runs were on other models: a "1-CHIP SFC" and a "2/1/3 SFC" (CPU 2 / PPU1 1 / PPU2 3). Differences from his notes may be real model differences, so they are not necessarily bugs. --------------------------------------------------------------------------------------- ## 0. Common ground (applies to every test below) ### 0.1 Environment his tests assume - FastROM: `MEMSEL=1`. Main code runs in bank `$80`. Native mode, `DB=$80`. - **IRQ handler runs in bank `$00`, so it runs from SlowROM.** The native IRQ vector (`_common/snes_header.inc:135`, `dw IrqHandler`) is 16 bits. The handler is assembled at `$80:8xxx` but is entered at `$00:8xxx`, at 8 master clocks per byte. - NMI is never enabled (`constant NmiHandler = BreakHandler`). No HDMA, no DMA during a measurement. `WRIO $4201 = $FF` (`reset_registers.inc:56-57`), which the `$2137` latch needs. - The hot loops run with `D=$4200`, so `bit.b HVBJOY` is `bit $12` and reads `$4212`. DL=0, so there is no direct-page penalty. ### 0.2 Cycle costs (master clocks, "mclk") used in all the reasoning | access | mclk | |---|---| | FastROM byte (banks $80+, MEMSEL=1) | 6 | | SlowROM byte (bank $00, the IRQ handler and vectors) | 8 | | WRAM (incl. stack, `$00-3F:0000-1FFF`) | 8 | | `$4016/$4017` (JOYSER0/1, XSlow) | 12 | | `$4200-$421F`, `$2100-$21FF` | 6 | | internal (IO) cycle | 6 | | WRAM refresh, once per line at about mclk 538 (dot ~134) | +40 | Instruction totals in FastROM, 8-bit A, `D=$4200`: - `bit.b HVBJOY`: 18. The read of `$4212` is its last cycle. - `beq/bne`: 12 not taken, 18 taken. Native mode has no page-cross penalty. - `nop`, `iny`, `dex`: 12 each. `rep #0`: 18. - `bit.b RDMPYL`: 18. `bit.w $0000` (WRAM): 26. `sta.l wram`: 32. - `sty.w wram` with 16-bit index: 34. - `stz.b/sta.b NMITIMEN`: 18, with the write in the last 6. - `ldx.w JOYSER0` (8-bit index): 30, with the read in the last 12. - `lda.w $2137` / `bit.w $2137`: 24. The same instructions in the SlowROM IRQ handler: - `stz.b`/`sta.b NMITIMEN`: 22. - `stz.w`/`sta.w`/`bit.w JOYSER0`: 36. - `bit.w $2137`: 30. Native IRQ entry: dummy opcode read (6) + IO (6) + 4 stack pushes (32) + vector from `$00:FFEE` (16) = 60 mclk. Entry starts at the end of the current instruction; the IRQ is sampled before that instruction's last cycle (anomie_timing.txt:134-137). H/V IRQ trigger point (anomie_timing.txt:113-123): - HTIME≠0: 14+4·HTIME mclk after dot 0 of line VTIME. - HTIME=0: 1374 mclk after dot 0 of line VTIME-1, which is about 10 mclk into line VTIME. ### 0.3 Auto-joypad reference model (doc / emulator, not console) - anomie (anomie_timing.txt:275-288): starts between dots 32.5 and 95.5 of line 225 (mclk 130-384). It runs on a 256-mclk grid and lasts 4224 mclk. `$4212.0` is set for that time. - ares 9408cb4 (ISC), `sfc/cpu/timing.cpp` `CPU::joypadEdge()` (called every 128 mclk from `CPU::step()`), with `sfc/cpu/io.cpp` `readCPU` `$4212` and `writeCPU` `$4200`, uses 128-mclk steps: - start: on line `vdisp` (225), on a 256-mclk boundary of the CPU's clock counter, with H counter 130-384 (anomie's window). - step 0: latch raised (if enabled), JOY1-4 zeroed if enabled; `$4212.0` reads 1 from here while enabled. - step 1: latch released. - from step 2, even steps read the port, odd steps shift the bit in (16 bits, one per 256 mclk); the sequence ends at step 33 (4224 / 128). - enable = 0 at any step but 1 ends the read for the frame; a `$4200` write with bit 0 = 0 at step 2 or later ends it at once, and a write at step 0 sets the latch to the new enable bit. - (A second model's step layout, with the latch one step before the flag, was struck: it rested on reading GPL source.) - Consequence: the start phase relative to the line moves from frame to frame. NTSC non-interlace frames alternate 357368 / 357364 mclk, about -20 mclk per 2 frames mod 256. Consecutive trials therefore sample different start dots. - Open item U11 (`docs/undocumented-behavior.md`): MAME and Mesen2, run as closed boxes (OBSERVED), read no bit; the first build's board (archived) clocked one bit without shifting; the author's console printed `0001 0001`. ### 0.4 Pad bit order (JOY1, and the manual 16-bit read below) B=`$8000`, Y=`$4000`, Select=`$2000`, Start=`$1000`, Up=`$0800`, Down=`$0400`, Left=`$0200`, Right=`$0100`, A=`$0080`, X=`$0040`, L=`$0020`, R=`$0010`, ID bits `$000F`=0. After the 16th clock a standard pad returns 1. **Manual read** (`_modify-autojoy-during-autojoy.inc:268-279`, SNESdev wiki code): ``` manual := $0001 repeat until carry out: lda JOYSER0 ; lsr ; rol manual_lo ; rol manual_hi ``` This is exactly 16 `$4016` reads, with no latch pulse first. The first bit read ends up in bit 15, and 1 means pressed. With B held and a freshly latched pad the result is `$8000`. A pad already fully clocked gives `$FFFF`. ### 0.5 H/V latch protocol for all stored positions 1. Latch by reading `$2137` (`WRIO` bit 7 must be 1). 2. Later, outside any timing-critical region: read `$213F` once to reset both hi/lo flip-flops. 3. Read `$213C` twice (OPHCT lo, then hi; only bit 0 of hi is valid, bits 1-7 are PPU2 open bus) and `$213D` twice. 4. Store OPHCT lo, OPHCT hi&1, OPVCT lo. V≤261, but every V in this family is <256 except the controls. Don't latch again, and don't toggle `$4201` bit 7, between the latch and the read-out. ### 0.6 Common traps - **Stale-detection trap.** In his poll-for-start tests (all except auto-joy-timing-test) the "wait for flag set" loop begins wherever the previous trial left off. If it begins while auto-read is already active, it "detects" mid-read and the trial is garbage. - Fix: before every trial, wait for V==200 (poll `$2137`/OPVCT like `auto-joy-timing-test.asm:149-158`), then enter the detection loop. - **Detection jitter.** `bit.b HVBJOY / beq` is a 36-mclk loop. - The read that first sees the flag is 0-35 mclk after the flag actually rose. - All "delay after detection" sweeps are therefore smeared by a uniform 36-mclk window, plus 40 mclk when a refresh lands inside the delay. The refresh lands at dot ~134, inside the auto-read window. - Treat each trial as a sample. The fraction of outcomes against delay is a ramp whose midpoint is the transition. 2-mclk steps × 2 passes is about as good as 4 trials per value. - Optional dither: add k·6 mclk (k = trial mod 6) before entering the V==200 wait. This varies the loop phase. - **Hangs.** Every wait (V==200, flag set, flag clear) needs a timeout of about 2 frames (16-bit counter). On timeout store `$FF` in the trial's bytes and count it in the header. - Flag never set: auto-read disabled, or the write killed it. - Flag never clear: possible if a blip restarts auto-read. - **JOY1 is never cleared by software.** It keeps the last completed (or partial) read. So a test where the write prevents auto-read shows the previous frame's value. This matters for the IRQ family; see the poison frame in T6. - **Reading JOY1 / JOYSER0 while `$4212.0`=1 is meaningless.** Always read after the flag is clear and after the fixed wait loops below. --------------------------------------------------------------------------------------- ## 1. Per-file analysis ### 1.1 `auto-joy-timing-test.asm`: dot where the flag rises and falls 1. **What it tests and how.** - It finds the H position where `$4212.0` sets and clears. - Auto-joy is enabled once (`NMITIMEN=$01`, l.119-120). Each frame it polls OPVCT (via SLHV latch) until V==200 (l.149-158), then sets `D=$4200`. - Start: `lda #$01; L: bit.b HVBJOY; beq L` (36 mclk/iteration). Then `lda.w SLHV`, and reads OPHCT lo and hi (l.170-185, with `beq`). - End: the same, with `bne` (l.188). - Timing: the `$2137` read cycle starts 36 mclk after the start of the `$4212` read that saw the change (beq not-taken 12 + lda.w fetch 18, measured from the end of that read's 6). So latched H ≈ (transition + [0..35] + 36)/4 dots. - He keeps cur/min/max across frames. 2. **Recorded result.** None. l.92: "NOTE: These values are the OPHCT values after the test has completed." 3. **Measurement (T1).** - 16 trials, one every 2 frames, so the start drifts across the 256-mclk grid (§0.3). - Per trial: OPHCT lo/hi and OPVCT lo at the start; the same at the end. 6 bytes. Total **96 B**. - No button needed. 4. **Traps.** - He never resets `$213F`, so his OPHCT lo/hi order rests on the flip-flop state. Use §0.5. - Each OPHCT unit is 4 mclk, except dots 323 and 327, which are 6 mclk. - Refresh near dot 134 can delay the post-loop latch by 40 mclk. A start reading near dot ~134-145 may be refresh-shifted. ### 1.2 `clear-autojoy-timing-test.asm`: when the flag drops after enable is cleared mid-read 1. **What it tests and how.** - `NMITIMEN=$01` (l.163-164), then the 36-mclk detection loop. - Delay N mclk via `sta.l` (32 each), N ∈ {0,64,128,192,256,384,512} (l.75, l.173-181). Then `stz.b NMITIMEN` (l.184). - Then 31 unrolled `bit.b HVBJOY; beq Clear_i` polls at 30 mclk spacing (70 when a refresh hits; comment l.186-190). The result is i = index of the first poll that reads 0. `$FF` means none. - Timing: - Detection read starts at t. - stz write starts at w = t + 6 + 12 + N + 12 = t + 30 + N. - Poll k's read starts at w + 18 + 30(k-1). - So result i means the flag fell in (w+18+30(i-2), w+18+30(i-1)]. 2. **Recorded result.** None. The screen shows RAW/MIN/MAX per delay. 3. **Measurement.** Merged with 1.3 into T2 (see 1.3). 4. **Traps.** The stale-detection trap applies (he has no V==200 wait). `lda #$FF` is also the timeout value. ### 1.3 `clear-autojoy-after-autojoy-active.asm`: U11, clear shortly after the flag sets, then manual read 1. **What it tests and how.** - Detection loop as above. Then 11×`nop` (132) + `bit.w manualRead` (26) = **158 mclk code delay** (l.140-152), then `stz.b NMITIMEN` (l.156). - So w = t + 18 + 158 + 12 = **t + 188**, which is 188-223 mclk after the flag rose. - Then a 600-iteration `dex/bne` wait (30 mclk each, 18000 mclk, about 13 lines; l.160-163). - Then the 16-bit manual JOYSER0 read (§0.4, without latching) and `JOY1` (l.194). It prints `JOY1 MANUAL`. - Prompt: "Hold B on controller 1" (l.39). 2. **Recorded result.** clear-autojoy-after-autojoy-active.asm:133-139, 1-CHIP SFC: > "Delay determined experimentally. On my 1-CHIP SFC: > * 156 m-cycles = No `0008 1000` > * 158 m-cycles = Occasionally outputs `0008 1000`, sometimes `0001 0001` > * 160 m-cycles = Occasionally outputs `0008 1000`, lots of `0001 0001` > * 162 m-cycles = Lots of `0001 0001`, I did not see any `0008 1000`" Reading it: `0001 0001` = one auto clock shifted B into JOY1, and the manual read saw the 15 remaining zero bits and then 1. `0008 1000` is not a simple "n bits then rest" split, so it is worth catching on our console. The "m-cycles" are his code delay P between `beq` and `stz` (156 = 13 nops, 158 the shipped build, 160/162 variants). 3. **Measurement: T2, merging 1.2 and 1.3.** Per trial: 1. Wait V==200. 2. `NMITIMEN=$01`. 3. Detect. 4. Delay P (code delay from the end of the `beq` to the first fetch of `stz`). 5. `stz.b NMITIMEN`. 6. 31 unrolled polls, giving index i (1 byte). 7. 600× `dex/bne`. 8. Manual 16-bit JOYSER0 read (2 B, lo then hi). 9. `JOY1` (2 B). The polls only read `$4212` and cannot disturb the pad, so the U11 data is still valid. - P values: - coarse {0, 64, 128, 192, 256, 384, 512} (his clear-timing set); - fine {140, 142, ..., 180}, 21 values, bracketing his 156-162. - 2 passes of the 28-value list = 56 trials × 5 B = **280 B**. - Hold **B**. - Building P in 2-mclk steps with the §0.2 table (12, 18, 26, 32, 34): - 140 = 32+9×12; 142 = 26+26+18+6×12; - 156 = 13×12; 158 = 11×12+26 (his exact code); 160 = 9×12+18+34; 162 = 12×12+18; - every even value ≥ 12 (and 0) is reachable. 4. **Traps.** - Stale detection (§0.6). - The refresh at about dot 134 often lands inside P≈140-180 for a start at dots 32-95, which adds 40 mclk to some trials. That is expected scatter. - JOY1 must be read only after the 600-loop. - With B held, the console's "no-clock" case would read JOY1=`$0000` and manual `$8000` or similar. The manual read is the shift-register state, so store it raw. ### 1.4 `blip-autojoy-timing-test.asm`: how long enable may be 0 during the first auto-joy cycle 1. **What it tests and how.** - `X=600, Y=0, A=$01`; `sta.b NMITIMEN`; detect; `stz.b NMITIMEN` (l.183) immediately (P=0). - Delay D ∈ {0,96,128,200,202,204,206,208,210} (l.78), built from `sta.l` 32, `nop` 12, `sty.w` 34, `bit.w $0000` 26, `bit.b RDMPYL` 18 (l.189-210). - `sta.b NMITIMEN` (l.215). Then count `$4212.0`=1 over 600 iterations of `bit.b HVBJOY; beq +; iny; +; dex; bne` (66 mclk when clear, 72 when set). - The count is stored as the 8-bit `tya`. Normal auto-read gives about 58-59. - Timing: - First write w1 = t+30. - Second write w2 = w1 + 6 + D + 12 = **w1 + 18 + D**. - So D=0 is an 18-mclk blip. D=200-210 is a 218-228 mclk gap, ending at about 248-293 mclk after the flag rose. 2. **Recorded result.** None. Instruction: "Let this run for a minute or three" (l.44); min/max shown. 3. **Measurement: T3.** Same sequence, plus `JOY1` after the loop. Per trial: count (1 B, saturating at `$FF`) + JOY1 (2 B). - D ∈ {0, 32, 64, 96, 128, 160, 192, 196, 200, 202, 204, 206, 208, 210, 214, 220, 230, 256}, 18 values. - 2 passes = 36 trials × 3 B = **108 B**. - Hold **B**. JOY1 tells whether the restarted read re-latched (`$8000`), was corrupted, or did not run (`$0000` / partial). 4. **Traps.** - Stale detection. - The 600-loop takes about 40k mclk (about 29 lines). Started at line 225, it ends around line 254, still in VBlank. Read JOY1 after it. - His `tya` with 8-bit A truncates. Keep 1 B but saturate explicitly. ### 1.5 `blip-autojoy-test-automatic.asm`: blip at varying delay after detection 1. **What it tests and how.** - `NMITIMEN=$01`, `X=200, Y=0`; detect. - Wait N "FastROM cycles" (6 mclk each, built from `nop`=2 cycles and `rep #0`=3, l.229-242). N ∈ {0, 20..36, 42} (l.293-305), i.e. P' ∈ {0, 120, 126, ..., 216, 252} mclk. - `stz.b NMITIMEN; sta.b NMITIMEN` (l.245-246): write gap 18 mclk (58 with a refresh; header l.10). - Count `$4212.0` over 200 iterations of the poll loop. - It prints R (count>0) / - per column, and the minimum non-zero count. 2. **Recorded result.** None (header l.14-20 describes the display only). 3. **Measurement: T4.** Same timing: w1 = t + 30 + P', w2 = w1 + 18; X=200. Per trial: count (1 B) + JOY1 (2 B). - His 19 P' values × 2 passes = 38 × 3 B = **114 B**. Hold **B**. 4. **Traps.** - Stale detection. - The comment at l.262 says "pld // D = $4200" but D is actually back to 0 there. Don't copy the comment's assumption. ### 1.6 `blip-autojoy-latches-joypad-test.asm`: does the blip re-latch the pad? 1. **What it tests and how.** - 8-bit index; `stz.w JOYSER0`; `NMITIMEN=$01`; detect. - Then `stz.b NMITIMEN` (18), `ldx.w JOYSER0` (30), `ldy.w JOYSER0` (30), `sta.b NMITIMEN` (18) (l.169-178). - Wait for the flag to clear, then print bit 0 of X, bit 0 of Y, and the 16 bits of JOY1. - Timing: w1 = t+30; reads start at t+54 and t+84 (12 mclk each); w2 = t+108. That is **108-143 mclk after the flag rose**, against his 128-mclk deadline. 2. **Recorded result.** No console values. l.165-167: > "This test should complete 128 master-cycles after HVBJOY auto-joy active flag is > set. There is not enough CPU time to meet this deadline on every test iteration. > I'm guessing half of the test iterations meet the deadline." l.22-24: if it re-latches, "the two manual reads should match the two most-significant bits of the auto-joy read and the auto-joy read should be uncorrupted". 3. **Measurement: T5.** Same instruction sequence. Per trial: raw `$4016` byte #1, raw byte #2, JOY1 lo/hi = 4 B. 16 trials = **64 B**. - He asks for "Hold B, then Y, then SELECT" (l.63), but **B alone is sufficient**: - re-latched: x=1, y=0 (Y), JOY1=`$8000`; - latch left high: x=y=1; - not re-latched: JOY1 shifted or `$0000`. 4. **Traps.** - Deadline is met in only about half the trials (by design). Do not add any instruction between detection and w2. - Ensure `JOYSER0=0` before the trial. ### 1.7 `_modify-autojoy-during-autojoy.inc`: the IRQ family engine (7 tests) 1. **How it works** (DoTest l.173-309): 1. `D=$4200`. `stz NMITIMEN`, `sei`. 2. Sync IRQ at V=210, H=0: HTIME=0, VTIME=210, `NMITIMEN=$30`, `bit TIMEUP`; `A=0; cli; wai` (l.186-204). The handler runs IrqCode with **A=0**. Then `sei; stz NMITIMEN` (l.208-209). 3. `JOYSER0 = JOYSER_LATCH_START_VALUE` (l.215-216). 4. HTIME/VTIME = row; `NMITIMEN = $30 | AUTO_JOY_ENABLED_BEFORE_IRQ`; `bit TIMEUP` (l.223-233). 5. `X=600, Y=0, A=$01; cli`. Then the poll loop `bit.b HVBJOY; beq +; iny; +; dex; bne` (66/72 mclk per iteration, l.246-259). This runs about 40k mclk, from about V=210 to V≈240, and the operative IRQ fires inside it. 6. Handler (l.327-337): IrqCode with **A=$01** (the "HACK": A is preloaded, D=$4200, DB=$80), then `bit.b TIMEUP`, `rti`. 7. After the loop: manual 16-bit JOYSER0 read (l.271-279) and `ldy.w JOY1` (l.291). It prints COUNT (16-bit), JOY1, MANUAL. Prompt: "Press and hold a Joypad 1 button" (l.41). The header (l.9-10) notes: "the VTIME/HTIME is the time of the IRQ interrupt, it is not the position of the NMITIMEN/JOYSER0 write." Operative write timing: - IRQ line low at 14+4·HT mclk into line V (HT=0: about 10 mclk into line V). - Plus 6-24 to finish the current loop instruction, plus 60 entry. - Plus 16 (`.b`) or 24 (`.w`) SlowROM fetch. - So the **write starts IRQ+82..100 mclk** for NMITIMEN writes and **IRQ+84..102** for JOYSER0 accesses. | test file | IrqCode | latch start | autojoy before IRQ | rows (V,H) | |---|---|---|---|---| | blip-autojoy-test.asm | `stz.b NMITIMEN; sta.b NMITIMEN` (l.64-66). The gap is **22 mclk** (SlowROM), not 18 | 0 | 1 | 224/200, 225/70, 225/80, 225/90, 226/0,64,128,192,256,320, 227/20, 228/20 | | clear-autojoy-1st-vb-sl.asm | `stz.b NMITIMEN` (l.65) | 0 | 1 | 0/0, 224/308, 224/324, 225/0,16,...,112, 225/144,160,176,192 | | clear-autojoy-during-autojoy.asm | `stz.b NMITIMEN` | 0 | 1 | 224/200, 225-228 × 20/200 | | enable-autojoy-late-test-2.asm | `sta.b NMITIMEN` (A=$01) (l.64) | 0 | **0** | same rows as clear-1st-vb-sl | | joypad-latch-clear-during-autojoy.asm | `stz.w JOYSER0` (l.64) | **1** | 1 | as clear-during | | joypad-latch-set-during-autojoy.asm | `sta.w JOYSER0` (A=$01) | 0 | 1 | 229/0, 224/200, 225/20,200, 226/20,200, 227/20,200, 228/20 | | joyser0-read-during-autojoy.asm | `bit.w JOYSER0` (one pad clock) | 0 | 1 | as blip | 2. **Recorded results.** None in any of these sources. 3. **Measurement.** See T6 (§1.9). 4. **Traps.** - **Hang in latch-clear / latch-set / joyser0-read.** IrqCode doesn't touch NMITIMEN, so the H/V IRQ stays armed and refires next frame. His code survives only because the next DoTest writes `stz NMITIMEN` before then. If it fires with D≠$4200, `bit.b TIMEUP` reads `$0011`, never acks, and the IRQ storms forever. **Fix: right after the loop, `sei; stz $4200; lda $4211`.** - The `(0,0)` row is a control. Its IRQ would fire at the end of line 261, after the loop. It never fires in his ROM (cancelled by the next DoTest), so it records an undisturbed frame. - The sync IRQ runs IrqCode with A=0. In latch-clear / latch-set it writes `JOYSER0=0` at V≈210; in joyser0-read it clocks the pad once at V≈210. Both are harmless because the start latch is rewritten after, but replicate them for fidelity. - **latch-set leaves `JOYSER0=1`**, and **latch-clear's control row also leaves it at 1**. The manual read then happens with the latch high (returns B every time: `$FFFF`). Restore `JOYSER0=0` after the manual read. - enable variant: auto-read is off until the IRQ. If the enable is too late, JOY1 is stale from the previous frame. That is why T6 uses the poison frame. - The handler time (about 150 mclk) during an active window lowers COUNT by about 2. That is expected. ### 1.8 `enable-autojoy-late-test.asm`: earlier version of enable-late 1. **What it tests and how.** - Rows: V ∈ {224..228} × H ∈ {20, 200} (l.110-124). - DoTest: `stz NMITIMEN`; HTIME/VTIME; `NMITIMEN=$30`; `bit TIMEUP`; `cli`; `X=500, Y=0, A=$01; wai` (l.199-222). - Handler: `sta.b NMITIMEN; bit.b TIMEUP; rti` (l.279-284). Entered from WAI, so there is no instruction-completion jitter. - Then the count loop (500 iterations) and JOY1 (l.253). - No JOYSER0 setup. 2. **Recorded result.** enable-autojoy-late-test.asm:112-113: > "Manually tweaked to be somewhere in the middle of the auto-joypad-clock (tested > on my 2/1/3 SFC console, not the Mesen event viewer)" That is row placement only; no values. Prompt "Press and hold Joypad 1" (l.43). 3. **Measurement.** Merged into T6's enable variant, which uses the IRQ-during-loop engine. The latched H/V makes the WAI-vs-loop entry difference visible. - The engine difference matters only for the count. In WAI mode the count starts after the enable. - If desired, add one WAI-mode row at (226,20) to the enable variant. Not included in the budget. 4. **Traps.** Same IRQ-stays-armed hang as 1.7: the handler writes `$01`, which disables the IRQ, so it is actually safe. But D is 0 after the loop (l.233 comment "D = $4200" is wrong), so `sei` before leaving. ### 1.9 T6: the IRQ family as CPU measurements **Variants (6).** Merge clear-1st-vb-sl + clear-during, and enable-late-2 + enable-late: - BLIP - CLEAR - ENABLE - LATCH-CLR - LATCH-SET - JS0-READ **Rows (15, the same for every variant):** | # | V,H | est. operative write (mclk into line), for orientation | |---|---|---| | 1 | 0,0 | control: the IRQ must not fire before the cancel | | 2 | 224,200 | ~line 224 + 900 | | 3 | 224,308 | line 224 + ~1328 (straddles into 225) | | 4 | 225,0 | 225 + 92..110 | | 5 | 225,16 | 225 + 160..178 | | 6 | 225,32 | 225 + 224..242 | | 7 | 225,48 | 225 + 288..306 | | 8 | 225,64 | 225 + 352..370 | | 9 | 225,80 | 225 + 416..434 | | 10 | 225,96 | 225 + 480..498 | | 11 | 225,192 | 225 + 864..882 | | 12 | 226,20 | 226 + ~180 | | 13 | 227,20 | 227 + ~180 | | 14 | 228,20 | 228 + ~180 (near the end of auto-read: 4224+130..384 ≈ line 228 + 262..516) | | 15 | 229,0 | post control | These cover his 1st-scanline sweep (16-mclk HT steps across the 130-384 start window and the 128-before strobe), the mid-read points, and both controls. **Frames per row:** 1. **Poison frame.** Wait V==200; `JOYSER0=1`; `NMITIMEN=$01`; wait flag set, then clear (with timeout); `JOYSER0=0`; `NMITIMEN=0`. - The latch is held high through the read, so the pad repeats B and **JOY1 = `$FFFF`** (with B held). - This makes "auto-read did not run" (JOY1 stays `$FFFF`) distinct from a good read (`$8000`) and from partial reads. 2. **Test frame**, exactly as §1.7 steps 1-7, with: - Handler = `[IrqCode]; bit.w $2137; inc.w irqFired; bit.b TIMEUP; rti`, located in bank `$00` (SlowROM), entered directly from the `$FFEE` vector. - Use `bit` for the latch so A survives. - If your ROM dispatches through a trampoline, the operative write moves later by the dispatch cost. It still works because the latch records it; add that cost to the offsets below. - Zero `irqFired` after the sync `wai` returns. - After the loop: `sei; stz $4200; lda $4211`. Store Y (16-bit count). Manual 16-bit JOYSER0 read. `ldy JOY1`. Restore `JOYSER0=0`. Read the latched OPHCT/OPVCT per §0.5. - **Latch offset:** the `$2137` read starts **30 mclk after the start of the operative write** for NMITIMEN writes (BLIP: after the second write; the first is 22 earlier), and **36 mclk after** for JOYSER0 accesses. So the write's H ≈ latched H − 7.5 dots (−9 dots for JOYSER0). **Per row: 9 B** - COUNT lo, COUNT hi - JOY1 lo, hi - MANUAL lo, hi - OPHCT lo - OPHCT hi (bit 0 = H bit 8; **bit 7 = 1 if `irqFired`==0**, i.e. the operative IRQ did not fire, as expected for row 1) - OPVCT lo **Per variant:** 2 B = JOY1 after the first poison frame (expect `$FFFF`; checks the poison and that B is held). **Total** 6 × (2 + 15×9) = **822 B**. Hold **B**. --------------------------------------------------------------------------------------- ## 2. Family plan (ordered) Single prompt for the whole family: **"Hold B on controller 1"**, until the ROM says release. Nothing else may be pressed (header check below). - B alone matches the author's only console note (U11) and is enough for T5. - Avoid button combos the EverDrive menu might react to. | order | id | source files merged | trials | bytes | |---|---|---|---|---| | 0 | HDR | baseline | - | 16 | | 1 | T1 START/END dot | auto-joy-timing-test | 16 (every 2nd frame) | 96 | | 2 | T2 CLEAR sweep + U11 | clear-autojoy-timing-test, clear-autojoy-after-autojoy-active | 28 P × 2 passes | 280 | | 3 | T3 BLIP gap | blip-autojoy-timing-test | 18 D × 2 | 108 | | 4 | T4 BLIP pre-delay | blip-autojoy-test-automatic | 19 P' × 2 | 114 | | 5 | T5 BLIP latches pad | blip-autojoy-latches-joypad-test | 16 | 64 | | 6 | T6 IRQ family | blip-autojoy-test, clear-autojoy-1st-vb-sl, clear-autojoy-during-autojoy, enable-autojoy-late-test(-2), joypad-latch-clear/-set-during-autojoy, joyser0-read-during-autojoy | 6 variants × 15 rows (2 frames each) | 822 | | | **total** | | | **1500 B** | **Header (16 B):** - `"AJOY"` (4) - layout version (1) - timeout count (1) - JOY1 after a normal read (2; expect `$8000`) - manual read after a `JOYSER0` 1→0 pulse (2; expect `$8000`) - JOY2 (2; expect `$0000`, port 2 empty) - 4 reserved Run time is roughly 450 frames, about 8 s, plus the prompt. **Global setup for the family:** - NMI off, HDMA off, no DMA. - `MEMSEL=1`, main code in bank $80+, `WRIO=$FF`. - IRQ handler at a bank-$00 SlowROM address. - `JOYSER0=0` between tests. - Leave `NMITIMEN` as your ROM expects afterwards. The 5A22 v1's DMA/HDMA crash bug is a further reason to keep HDMA off here. **Interpreting results across the family:** - T1 gives the start/end dot distribution: compare with the 32.5-95.5 window and the 4224 length. - T2 gives the abort point and U11 directly: compare with `0001 0001` / `0008 1000`. - T3/T4/T5 give the blip semantics: restart, re-latch, deadline. - T6 gives the same questions keyed to an exact latched write position, plus the JOYSER0 write/read interactions, which only T6 covers.