# sh2-python: synchronous DRAM cycles and refresh (a PR description) Written for sh2-python by sega32x-python's milestone 5b (docs/worklog.md, Session 7). The board cannot do this itself: HM 7.2.3 says "With synchronous DRAM, external wait input is ignored regardless of any setting", so an SDRAM cycle's length is the bus state controller's alone, and the board's only lever, `chip.wait`, is the WAIT pin. sega32x-python does not work around it (`Quirks.sh2_sdram`: the board adds nothing to an SDRAM cycle). Nothing in the board waits on this; it is the largest bus cost the board cannot place. ## Title BSC: synchronous DRAM cycle shapes (HM 7.5) and auto-refresh (HM 7.5.7) ## What the core does now (d55bbb0) `bsc.py` says so in its docstring: "Not yet modelled, for later rungs: SDRAM/DRAM/burst-ROM cycle shapes (HM 7.5-7.8) ... the refresh timer and its REFRESH cycles". Every external cycle is an ordinary one, `2 + WCR's waits + chip.wait` states per port-width cycle. On the 32X, whose boot ROMs leave BCR1 = $0001 (CS3 synchronous DRAM), BCR2 = $00A8 (16 bits), WCR = $0055 and MCR = $0AB8 (bank active, refresh on, 2 Mbit x 16, TRAS 3 cycles), and RTCSR = $08 / RTCOR = $59 (CKIO / 4, every 89 counts), that gives: | Access to CS3 | the core now | HM 7.5 with those registers | Sega's 32X Manual 4.1 | | --- | ---: | ---: | ---: | | line fill (16 bytes, 16-bit port) | 8 cycles x 3 = 24 | 10 same row, 11 first after refresh or precharge, 12 other row | 12 | | cache-through longword read | 2 x 3 = 6 | the same 10-12: "the access operation ... is 8-word-burst-read-fixed" (HM 7.5.4) | 12 | | cache-through word or byte read | 3 | 10-12 | 12 | | word write | 3 | 1 same row (bank active, figure 7.23), 2 first, 3 other row (figure 7.24) | 2 | | longword write | 6 | two single writes | 4 | | refresh | none | every 356 states: Tp, Trr, then TRAS + 2 = 5 with no command (figure 7.25); banks closed | - | WCR's W31/W30 are the CAS latency for synchronous DRAM (HM 7.2.3: 01 = 2 cycles), not waits, and the WAIT pin is not sampled there. jgenesis (issue 493, against the 32xspd benchmark's photograph of a hardware run, sh2-python `reference/docs/32xspd-hardware.jpg`) read HM 7.5 the same way: bursts 10-12, writes 1-3. The photograph's SDRAM read loop runs at 22.1 SH-2 clocks per 16-byte line, loop included; sega32x-python's board at this pin gives about 35 (docs/validation.md, "Milestone 5b"). ## Proposal 1. In `BSC._recompute`, note which areas are synchronous DRAM (BCR1 DRAM2-DRAM0, HM 7.2.1), with MCR's SZ, RASD, RCD, TRP, TRWL, TRAS and the area's CAS latency from WCR. 2. A read from such an area (a line fill, a cache-through read, a DMA read) is one burst of 4 (32-bit) or 8 (16-bit) data cycles: the callbacks still see one call per port cycle (the contract stays: `FILL`, `FIRST`), but the burst's states are HM's: Tr (or Tp Tr on a row change in bank-active mode, a Tnop with CAS latency 1 on the same row), Trw if RCD, Tc, CAS latency - 1 Tw, then the data cycles, then Tap; a cache-through or DMA read of less than 16 bytes still makes the whole burst (HM 7.5.4) and takes the wanted data from its first cycles. Bank-active state per bank (two banks, row address from MCR's AMX), closed by a refresh or by bus release. 3. A write is a single write: Tr Tc (Trwl Tap hidden unless the same bank is next), or Tc alone on an active row, Tp Tr Tc on another (HM 7.5.5-7.5.6). 4. `chip.wait` from the callback is ignored on these cycles (HM 7.2.3), or kept as the contract's escape hatch and documented so. 5. Auto-refresh (HM 7.5.7): RTCNT counts CKIO / CKS; at RTCOR a refresh cycle is made (through `read` with `REFRESH`, as `bus.py` already reserves) between bus cycles, taking Tp, Trr and TRAS + 2; it closes both banks. In slave mode the refresh setting is ignored (HM 7.10.2), so only the master makes them. ## Tests - With the 32X's register values: a 16-bit line fill to an idle row takes 11 states, the next fill in the same row 10, one in the other row of that bank 12; a cache-through word read takes the same as a fill; a word write 1/2/3 by row state. - A 32-bit SDRAM port (SZ = 1): 4 data cycles. - Refresh: a cycle every RTCOR x CKS states, `REFRESH` passed to `read`, banks closed. - `chip.wait` set by a callback on an SDRAM cycle changes nothing (if item 4 drops it). ## What the board does when this lands Nothing to change in sega32x-python's code: `Quirks.sh2_sdram` becomes "the core's synchronous DRAM cycles (HM 7.5)", the pin moves, and the rickroll player and 32xspd figures are rerun (`scripts/run_32xspd.py`). The arbitration model already holds a line fill's cycles together.