# Window-7 mapper test: does a Z80 banked read see a page the 68000 just mapped? A 5 MB ROM and a 58-byte Z80 program that isolate one question. It exists because the answer differs between emulators, and that difference cost a day of debugging in this project: the stream player's audio played 1.86 s of music from 29.8 s earlier at every 512 KB page crossing, in one emulator only. ## The question Extended SSF window 7 (`$A130FE`) maps a 512 KB page of the cartridge at 68000 address `$380000`. The Z80's bank register (`$6000`) points its `$8000-$FFFF` window at a 32 KB slice of 68000 space - here `$380000`. So a Z80 read of `$8000` goes out over the 68000 bus to `$380000`, and the cartridge decodes that address with whatever page the mapper currently holds. If the 68000 changes the page *after* the Z80's last bank-register write, what does the next Z80 read return - the new page, or the old one? ## Running it `mapper_test.bin` needs no controller input and prints nothing. The whole screen is the verdict: | colour | meaning | |---|---| | **GREEN** | the read saw the newly mapped page | | **RED** | the read saw the page mapped *before*, i.e. a stale window | | **BLUE** | neither marker: unexpected, inspect the result bytes | | **WHITE** | the control failed - the mapper write did not take effect even for the 68000, so the test says nothing about the Z80 path | Four result bytes are also left at `$FF0000..$FF0003` for a debugger: | | step | expected | |---|---|---| | `r0` | Z80 read, page 1 mapped, bank register just written | `$A1` | | `r1` | **68000** read of `$380000` after mapping page 2 (control) | `$B2` | | `r2` | Z80 read after mapping page 2, **no** bank rewrite | `$B2` | | `r3` | Z80 read after rewriting the bank register | `$B2` | `r2` is the answer; `r1` exists so a failure of the mapper write itself cannot be mistaken for a Z80-path failure. Rebuild with `python tools/mapper_test/build.py` (needs the project's WSL toolchain). `out/check_mapper.py`-style inspection of the result bytes under megadrive-python is more precise than reading the colour, if you have the emulator to hand. ## Observed | emulator / machine | version | result | |---|---|---| | megadrive-python | 2026-10 working copy | **GREEN** - all four bytes as expected | | BlastEm | 1.0.1-pre (win64, 9b71c8bd2065) | **RED** - `r2` returned the previous page | | real hardware | not yet tested | **unknown** - see PLAN.md phase 6 | The hardware row is the one that matters and is still empty. Everything below is inference until it is filled in: a Z80 banked read becomes a 68000-bus access that the cartridge decodes live, with nothing to latch, so hardware is *expected* to behave like the GREEN column. That has not been measured here. ## Mechanism in BlastEm Read from the current tip of the project's Mercurial repository, so line numbers should be taken as a pointer rather than a citation: - `write_bank_reg_w` / `write_bank_reg_b` (`sega_mapper.c` ~328-396) handle the mapper registers. They change the 68000 map and call `m68k_invalidate_code_range()`, but do not touch the Z80's banked-window pointer. - `gen_update_z80_bank_pointer` (`genesis.c` ~1193) resolves that window to a host pointer *through* the 68000 map, so it does honour the mapper - but it is reached only from `genesis_deserialize` (~276) and from `z80_write_bank_reg` (~1852). So the Z80's window keeps resolving to the page that was mapped when the Z80 last wrote `$6000`, until it writes `$6000` again. In this project that was one 32 KB bank later: exactly 32,768 samples of audio, which is how the symptom was first identified. Note that the 68000-side handler for a write to `$A06000` (`genesis.c` ~1318) computes the pointer as `(bank_reg << 15) + mem_pointers[2]`, bypassing the mapper, so having the 68000 poke the Z80's bank register is **not** a workaround. ## Workaround, if you hit this Have the **Z80** rewrite its own bank register after the 68000 has mapped the new page, and keep the Z80 from reading the window in between. Only a write by the Z80 re-resolves it. This is redundant on hardware and costs nothing there. `player/pcm.z80` does this: `next_bank` asks for the page, shuts the gate and sets `V_REBANK`; `g_rebank` writes the bank register once the 68000 acknowledges. There is a second trap in the same area - the gate is tested once per group, so iterations that had already been routed down the filling path copied 6 more bytes through the stale window. `slow1`/`slow2` re-read the gate after a slow copy for that reason.