m20 · part 1 of 4

A retro-style computer on the Tang Nano 20k

m9b squeezed a RISC-V computer into the 8,640 lookup tables of a Tang Nano 9K. m20 is the same computer on the Tang Nano 20K, with almost two and a half times the logic to spend: a CPU two thirds faster (60 MHz), with a floating-point unit, a 1280×720 display output, and a basic 3D accelerator, hardware that rasterizes, textures, lights and depth-tests polygons. For testing it out, it runs Quake, Descent, System Shock and Tomb Raider, all of them 640 pixels wide: we wanted a clear difference from m9b, and the 3D accelerator is what makes that affordable.

Quake
Tomb Raider (TRX)

A lot of m9b hardware was carried over unchanged: the USB host, the HDMI encoder, the video engine’s line table and the audio subsystem. These posts are about what’s new. This first part is the overview: what the bigger chip changed, the system and its clocks, and where the cells went. Then the CPU, the 3D accelerator, and porting notes for the four games.

What the new board provides

The Gowin GW2AR-18 FPGA provides more LUTs and block RAM, and a different type of memory: it has an 8MB SDRAM with 32 bit bus rather than the dual 4MB DDR PSRAM subsystem of the GW1NR-9.

20,7364-input lookup tables, 2.4× the 9K
92 KBblock RAM, in 46 blocks of 2 KB
8 MBSDRAM in the package, 32 bits wide
480 MB/sSDRAM peak at m20's clock: 120 MHz × 4 bytes

The RAM looks slower on paper, but as we saw with the m9b controller, that doesn’t mean much - the memory subsystem using the SDRAM is much more efficient than the PSRAM, The overall chip design is also faster, allowing us to push frequencies higher. One feature that is lost is the user flash, so we’ll have to fall back on the board’s SPI flash instead (the SPI flash on the Tang Nano 9k was connected but not used in the m9b design).

The new system specs

  m9b (Tang Nano 9K) m20 (Tang Nano 20K)
FPGA GW1NR-9: 8,640 LUT4, 26 block RAMs GW2AR-18: 20,736 LUT4, 46 block RAMs, 24 DSP blocks
CPU VexiiRiscv RV32IM at 36 MHz VexiiRiscv RV32IMF at 60 MHz
Memory 2 × 4 MB HyperRAM dies, DDR, 144 MHz 8 MB SDRAM, 32-bit SDR, 120 MHz
Firmware in the chip’s 76 KB user flash a 4 KB boot ROM copies it from the SPI flash into SDRAM
Video 720×480 at 59.94 Hz 1280×720 at 60 Hz
Graphics line table, palette, 2× line scaler the same, plus the 3D accelerator
Disk SD card SD card, or a FAT image in the 8 MB SPI flash
USB, sound, UART 2 USB ports, stereo HDMI out, UART 2 USB ports, stereo HDMI out, UART

Some of these are worth a paragraph each.

720p. We tried 1280×720 on m9b first and settled for 720×480: on the 9K’s emulated LVDS outputs the faster link failed to synchronize with some monitors due to bad timing. The 20K had exactluy the same issue, the picture still came and went on one monitor: the 371.25 MHz serializer clock, made from the 27 MHz crystal as 27 × 55 ÷ 4, had too much jitter. This board however has a way out, an MS5351 clock generator, programmable from the BL616’s console and wired to an FPGA clock pin. Set to 74.25 MHz, the pixel clock itself, it makes the PLL’s job a plain ×5, and the picture has been solid since. It requires one additional programming step, with a command to be sent over the boards UART.

The firmware. This is another point where we need to use an external chip. Where Gw1NR-9 had internal flash, both for its own bitstream and user data, the GW2AR-18 has none. The Tang Nano 20k board has an external 8MB SPI flash which we can use for both of these. Since the CPU cannot execute directly from the SPI memory, we have to use a small bootloader in a ROM in the logic to set up a minimal working system, with SPI and SDRAM, and copy the actual boot ROM over to the SDRAM and run it from there.

SDRAM. The GW2AR-18’s package holds a 64 Mbit SDRAM die instead of the 9K’s two HyperRAMs: single data rate, but 32 bits wide, so on paper it’s 480 MB/s at m20’s 120 MHz, against 576 for m9b’s two dies at 144. The difference that matters is latency and simplicity: commands have their own pins, and a read’s data follows a few clocks after it, where each HyperRAM transfer began with a command on the data pins and a fixed 12-clock wait. The controller ended up the simplest part of the design. It runs on one clock and drives the SDRAM’s clock pin with that same clock, without any phase shift; only the read data goes through a delay line, and the boot ROM sweeps it and keeps the middle of the window that works.

USB. Unlike the GW1NR-9 on the m9b with its internal flash, the GW2AR-18 is programmed at startup from the external flash, and this is not instantaneous. This causes its pins to stay tri-stated for a while, before the programmed pull-downs and other state takes effect. This disturbs the USB startup process, since the ports are powered up immediately, but the D+/D- lines are left floating, which confuses the devices which then do not initialize. When the pull-downs from the FPGA go live it is too late. So for this board we do need real 15 kΩ pull-downs on D+ and D−, as the USB spec asks of a host: without them, a keyboard or mouse plugged in at power-on never came up.

All of these lead to a major change from m9b, where everything resided within the FPGA, and the m20, where we need to program 3 chips on the board before anything will work. It is thus less self-contained than the m9b design.

The system

m20 system diagram by clock domain
Dashed regions are clock domains; yellow boxes are block RAM or SDRAM. The 3D accelerator sits beside the CPU, on its clock, with its own port into the SDRAM controller.

It’s m9b’s layout with three changes. A boot ROM takes the user flash’s place at address 0. The SDRAM controller replaces the HyperRAM controller behind the same bridge and the same 64-byte line protocol, so the CPU’s caches didn’t notice. And the 3D accelerator joins as the controller’s third client, after the CPU’s cache lines and before the video DMA.

The CPU pushes commands into the accelerator through an I/O window. The accelerator reads textures and writes pixels straight to SDRAM, and the CPU only has to clean or flush the relevant cache lines around it. Nothing else in the system knows the accelerator exists: the video engine just shows whatever is in the frame buffer.

Clocks

Clock MHz Made by Why that number
SDRAM controller 120 pll_main: 27 × 40 ÷ 9 The SDRAM runs on the same clock (it’s rated well above), and the CPU at a clean half
CPU, I/O, 3D accelerator 60 120 ÷ 2 (CLKDIV) Synchronous with the controller, as on m9b; the current layout closes timing at 64.7 MHz
USB 12 120 ÷ 10 Low-speed USB, sampled 8 times per bit
TMDS bit clock 371.25 pll_hdmi: 74.25 × 5 1280 × 720 at 60 Hz, 10 bits per pixel per channel, on both edges
Pixel clock 74.25 371.25 ÷ 5 (CLKDIV) The 720p pixel rate, in phase with the serializers by construction

Two PLLs again: one for the system from the board’s crystal, one for HDMI from the MS5351. The pixel side is asynchronous to everything else and talks to it only through the transfer RAM and a few synchronized flags.

For a while it ran at 54/108: at 60/120 Quake ran and the memory tests passed, but the frame-hash timedemo drew a few wrong frames per run, more under load, more on one board than the other. That turned out to depend on the layout rather than the clock, and with the right layout both boards now draw identical frames at 60/120. Finding that layout is a search, repeated for every change of the design. Part 2 has the details.

Booting

The 9K ran its firmware in place from the FPGA’s own user flash. The GW2AR-18 has none, so:

  1. A boot ROM, 2.3 KB of code in a 4 KB block RAM at address 0 (its last 256 bytes describe the bitstream: an id, the clocks, the layout and the source revision; the id is also the bitstream’s user code, so the boot screen shows whether the running bitstream is the one in the flash), finds where to catch the SDRAM’s read data: for each capture cycle, clock edge and delay setting it writes a 7 KB pattern, flushes it from the data cache, reads it back, and keeps the middle of the longest passing run. Its own stack sits in cache sets the pattern never touches, so it keeps working while the latency is still wrong.
  2. It copies the firmware image from the 8 MB SPI flash (offset 1 MB, after the bitstream) to the top of SDRAM, checks its sum, and jumps.
  3. The firmware runs the board tests, plays a chime and shows the program browser, as on m9b. Without an SD card it reads a FAT image from the SPI flash instead, so a board with just a USB cable can still run something.
SPI flash offset Contents
0 the FPGA’s bitstream, boot ROM included (about 0.9 MB)
1 MB the firmware image (up to 512 KB)
2 MB a FAT disk image, up to 6 MB

Where the cells go

After place and route: 17,866 of the 20,736 logic cells (87%; slices at 94%), 40 of the 46 block RAMs, and 13 of the 24 DSP blocks. That’s more headroom than m9b ever had, and the CPU closes timing with 8% margin instead of 5% (though on this chip the report isn’t the whole story: part 2).

Block Logic cells Block RAMs DSP Notes
CPU (with caches and FPU) ≈ 8,800 16 2 m9b’s was ≈ 4,300, with the same caches and divider: the FPU is the difference
3D accelerator ≈ 5,300 15 11 texel cache, z cache, lightmap, two palettes, reciprocal table, counters; the division, shading and lightmap blend in DSPs
HDMI encoder ≈ 610 – – unchanged
Video engine, video RAM ≈ 550 2 + 4 – 720 lines in the table now
SDRAM controller, bridge ≈ 730 – – the bridge’s line buffers are LUT RAM
USB host, two ports ≈ 500 1 – unchanged
UART, SPI, boot ROM ≈ 240 2 –  
Glue: I/O decode, crossings, resets ≈ 820 – –  

Per-block numbers are from the current build’s synthesis (lookup tables, ALUs, and 6 cells per LUT RAM), which adds up to 17,594; the total above is place and route’s.

The CPU is still the biggest block, but no longer more than half the chip: the 3D accelerator is the second biggest thing in it. Between them they’re four fifths of the design, and everything else is m9b’s peripherals at about the size they were there.

The rest of the series

  1. The CPU: the floating-point unit, 60 MHz and why the layout decides it, and two hardware faults that no simulation showed: block RAM read and written at the same address, and a word-write bypass.
  2. The 3D accelerator: textured, lit, depth-tested polygons from their edges or spans at one pixel per clock, checked bit for bit against a C model, and what its texture cache likes and doesn’t.
  3. Porting notes: Quake on single-precision floats, and how Tomb Raider, Descent and System Shock, written for 8-bit screens and palette lighting, came to share a 16-bit frame with the accelerator.

The source, as for m9b, is on GitHub in repositories of its own: m20-system ties together the SoC (m20-core) and the game ports (m20-quake, m20-descent, m20-sshock, m20-trx).

Credits

Everything credited in m9b’s overview is still here: VexiiRiscv, the HDMI encoder, the USB host, FatFs, picolibc and libADLMIDI. New on m20: