Building a custom SoC to run DOOM¶
This page describes the design of a custom SoC built for the sole purpose of running DOOM on a small FPGA: the Gowin Tang Nano 20K.
It includes a custom RISC-V CPU running a subset of the RV32 instruction set, with potential additional custom instructions that aid in the execution of of DOOM at full resolution and framerate of this FPGA. I refer to this custom instruction set as RV32doom.
When available, full details on the design of the CPU and the additional uncore components can be found in their respective pages linked below.
Overview¶
CPU¶
Initially, I planed to base the core on the two-cycle RV32E CPU I previously built: tiny-riscv
As it turns out, that core is basically worthless for any useful implementation. DOOMCore (v1) required a pretty drastic rewrite. This involved much more than I care to write about here.
More Details¶
Memory Controller¶
The Tang Nano 20K includes 8MB of SDRAM in the FPGA package. A basic memory controller acts as the interface between the memory bus (AXI) and the SDRAM. The SDRAM is configured with burst length of 8 for both read and writes. However, variable lengths bursts (up to 8) and single byte accesses are supported via the AXI interface.
More Details¶
Display Driver¶
The display driver implements a 320x200 pixel frame buffer, a 8-bit color palette, and all the logic necessary to drive the frame buffer to the display over HDMI (really DVI-D). The frame buffer and palette memory sit on the memory bus and are written with software from the CPU.
The display driver uses the data in the frame buffer and palette to calculate pixel values and upscale the frame to 640x480 before passing the pixels through a TDMS encoder, serializer and out to the display.
More Details¶
SD Card Interface¶
More Details¶
Bootloader¶
The bootloader is a small program sitting in a dedicated ROM that the CPU initially reads from when reset. It initializes the SD card, reads data from the SD card into the DRAM, and then jumps to executing the program (DOOM).
More Details¶
Memory Bus¶
Every component of this SoC is connected via AXI bus. This means I needed an AXI interconnect to connect my CPU to all the peripherals. Writing RTL for AXI busses is not mentally stimulating, but it is a lot of work and very easy to screw up. This is the only RTL in this design that I haven't written entirely by myself. Instead, I used the AXI crossbar written by/for the PULP platform. They have a paper which I haven't read, but their RTL seems to work alright.
The crossbar connects the two master ports on the CPU (icache and dcache), to four slaves: the bootrom, SDRAM, SD card interface, and the frame buffer.
Simulation¶
I used cocotb and Verilator to simulate the RTL. This method provides a few huge (in my opinion) benefits:
- Writing testbenches in Python is much more pleasant than writing them in SystemVerilog
- Writing simulation black boxes for modules like the DRAM or SD card is easy
- Simulation is really fast (if you don't heavily rely on the aforementioned blackbox modules)