m20 · part 2 of 4

The CPU: floats, and faults the simulator never saw

The CPU is m9b’s VexiiRiscv, configured the same way, with the same caches and the same 24-bit address trick (m9b part 2 has all of that). Three things are new: a floating-point unit, two thirds again the clock, and memory behind it that behaves differently. Getting there was mostly a hunt for faults that only appeared on the board, never in simulation.

A floating-point unit

Quake does all its geometry in floats. Without hardware floats, every fadd and fmul is a call into libgcc’s soft-float routines, dozens of instructions each. VexiiRiscv can generate the F extension (single precision) with one more option, and on the 20K there finally was room for it:

--with-rvf --fma-reduced-accuracy
  Without the FPU With it
Logic 37% of the chip 55%
Block RAM 27 of 46 30 (the float register file, since moved to LUT RAM: below)
Multipliers   4 more DSP blocks
CPU Fmax   58–66 MHz depending on the layout, against 60 needed

That’s about 3,700 logic cells, almost as much as m9b’s whole CPU. --fma-reduced-accuracy lets the fused multiply-add round twice, as an ordinary multiply then add would, which saves a wide adder and doesn’t matter to any game.

Two lessons came with it:

The runtime needed two small things too: picolibc’s start-up leaves the FPU off (mstatus.FS), so the first float instruction trapped until a constructor turned it on, and the trap handler now emulates misaligned flw and fsw along with the integer loads and stores.

54 MHz, then 60 MHz

On m9b the CPU ran at half the memory controller’s clock, synchronously, so the bridge between them needs no slow synchronizers. m20 keeps that: initally 108 MHz for the SDRAM, 54 MHz for the CPU. The CPU closes timing at about 63 MHz, so the obvious next step was 60/120, and it got a long try.

At 60/120 the board booted, the memory tests passed, and Quake ran its timedemos. But Quake’s timedemo plays back a recording, and real-time interpolation means a slightly different machine renders slightly different frames: “it finished” says little. So I made it deterministic, with fixed time steps and a fixed random sequence, and a hash of every frame, comparable between builds, clocks and boards.

Clocks Wrong frames per run Notes
54/108 MHz 0, on both boards, every run  
60/120 MHz 0–2 idle; 94–203 under extra memory load always in the 3D view, never in the status bar; more on one board than the other
57/114 MHz crashes within the first 500 frames three different layouts, both boards

Memory patterns, arithmetic tables and instruction fetch checks, all running alongside the game, stayed clean at 60 MHz. Whatever goes wrong is outside what they cover, and the difference between boards points at physical margin, unproven. So 54 MHz it was, for a while, with the frame hashes as the test for the next attempt.

60 MHz after all

I really didn’t want to leave it at 54 MHz, 60 MHz always felt tantalizingly close (m9b played the same game with me, initially it was 27 MHz for quite a while until it stabilized at 36 MHz). Digging in deeper found three things. First, the SDRAM’s reads (next section): caught on the rising edge alone, they sat about 1 ns from the early end of their window, and a warm board now and then lost single bits of fetched instructions (yes, these things change their timings as temperature rises). So the read inputs went back through IODELAYs into IDDRs (using the IDDR blocks allows sampling on both the rising and falling clock), and the boot ROM now sweeps the capture cycle, edge and delay and keeps the middle of the longest passing run. That fixed some of the errors causing CPU traps, but not the rendering errors on wrong frames.

The wrong frames followed the layout, not the clock or the timing report. The same RTL placed differently was clean, or drew a hundred wrong frames, or trapped at the same instruction every run, and layouts with more reported margin were often worse. One failing layout was clean at 57 MHz and failed at 58.5, with nothing changed but the PLL; its trap bit was the slowest bit of a ten-level load-to-use bypass path routed right across the die. On long routes the silicon seems to be about 12% slower than Gowin’s model says.

Third, a layout that works can be found, but each change of the design needs a new search: a few overconstraint and placement variants, the ones that meet timing hash-tested twice on both boards, then a moving game for ten minutes or more. The first layout to pass went into the boards’ flash in mid-November; as more features were added to the 3D accelerator (part 3) no passing layout were found for a week until some logic was trimmed to make room. 60/120 has been the mainline since.

Quake’s frame hashes turned out not to be enough on their own: some layouts that passed them twice trapped at the same instruction every time in other games. A layout now has to pass the Quake hash timedemo twice, Tomb Raider drawn by the CPU alone, and a new game in Descent and in System Shock, on both boards, before a soak and the flash. The timing report doesn’t predict which layouts pass: a passing layout reports 64.7 MHz, while failing ones report as much as 69; of the 17 layouts of the current design that met timing, 2 passed. Changing only the boot ROM’s contents needs no new search, though: they don’t take part in place and route.

You might have guessed it, running all of this takes a lot of time…

SDRAM: putting phase shifts in the right place

The memory side changed completely, from two HyperRAM dies on a DDR bus to one 32-bit SDRAM, and the controller went through a few versions before it got to where it is now.

The first was conventional: the SDRAM’s clock from a PLL output with an adjustable phase, swept by the boot ROM to find a working window (8 of 16 phases at 108 MHz). Then reads moved to IODELAY lines and falling-edge capture, when the phase window looked like the limit for 120 MHz. Then a bare test design, with one clock for both the controller and the SDRAM’s clock pin, no phase shift and no delays, turned out to work up to 162 MHz with bursts alone. In the SoC it just worked: the routing delay of the clock pin gives writes their margin, and reads only needed the right number of whole cycles, which the boot ROM picked from four. That turned out to leave too little margin on warm boards: reads now go through IODELAY into IDDR after all, both in the pin’s I/O cell, and the boot ROM centres the capture in a window of about 7 ns at 120 MHz.

   
Transfers ACTIVE, then 8-word bursts back to back, the last with auto-precharge: no row is ever left open, so there’s nothing to track, and a refresh fits between any two transfers
A cache line 2 bursts (64 bytes)
A DMA block 8 bursts (256 bytes), one at a time so cache lines get in between
Clients refresh, then the CPU’s cache lines, then the 3D accelerator, then video and audio DMA
Boot benchmark read 92 MB/s, fill 72 MB/s, copy 24 MB/s (through the caches, 128 KB each)

A 162 MHz attempt (3:1 to the 54 MHz CPU) closed timing but had a 3-in-16 phase window, and would have gained 0 to 4%: a cache miss waits mostly on the SDRAM’s fixed timings, not on its clock, so that was discarded pretty quickly.

Faults the simulator never saw

All of these timing issues are completely invisible in simulation. They only appeared on the board, intermittently, depending on how the tools happened to place the design. Debugging this required hundreds of runs, sometimes looking like a timing problem, a power problem, a bad chip, or a software bug.

One of the really puzzling issues was silent corruption on word writes to RAM, which already caused issues on the m9b (the byte enables don’t work consistently on the gowin FPGAs, which is probably why they were removed in later versions of the documentation, the exact behaviour is documented in the m9b posts). This time we hit another issue, well documented this time but not entirely obvious: Gowin’s documentation (UG285) has one line on it mentioning that performing read and write operations to the same address at the same time is not allowed. SpinalHDL’s memories assume read-first behaviour, the simulation model agrees, and VexiiRiscv relies on it all the time. Obviously nothing in the synthesis is going to detect whether you are going to do same-address reads and writes, and gowins simulation model does not call it out - it just behaves the way you would expect (read old data, write new data). On the hardware the actual outcome is timing-dependent, which is probably why this went entirely unnoticed on the slower m9b system - here it bit hard and required many runs to find.

The fix is a script run on the generated Verilog: every block RAM write goes through a one-entry pending register, and a read of that address takes the register instead of the RAM. The logic sees exactly the same values, cycle for cycle, and the two ports never meet (this works due to the VexiiRiscV access patterns). Other parts of the system like the register file were moved to LUTs, which has no such rule (and freed 5 block RAMs).

The second fault came from the workaround for the byte enables: the data cache writes whole words (an option of our fork), and on this chip a younger access in the same cycle could take the word’s other bytes from such a collision. The fork’s bypass now carries the whole merged word.

The key to finding such issues is finding a really small bit of code that can be run in a loop to study the behaviour - in this case it was a small loop of integer code with an fsqrt in flight: 3,000 failures in 3,000 runs on one layout, none on others, and none on any layout after the fix.

Lessons for intermittent faults

These ate more time than everything else in the CPU put together. What helped:

The bill

  m9b m20
ISA RV32IM + Zba, Zicbom, Zicbop, Zicboz the same + F
Clock 36 MHz (Fmax 37.8) 60 MHz (Fmax 64.7)
Caches 8 KB fetch, 16 KB 2-way data the same
Logic ≈ 4,300 cells ≈ 8,800 cells
Block RAM 18 16 (register files in LUT RAM)
Doom timedemo ≈ 22 fps not ported: Quake is the benchmark now

Part of what was learned here flowed back into VexiiRiscv’s fork, and part into habits. The next part is the block that sits beside the CPU and does a large share of the drawing in every game: the 3D accelerator.