Skip to content
CPU (B32P4)

CPU (B32P4)

B32P4 (Bart’s 32-bit Pipelined processor, version 4) is the CPU of the FPGC. It’s a 32-bit RISC CPU with a five-stage pipeline, designed from scratch, and runs at 100 MHz on a Cyclone IV EP4CE40 FPGA.

The goal of the CPU is not to make a competitor to RISC-V or ARM. Instead, the goal is to create a good CPU that is designed by a single person from scratch without using existing components, meaning it has to be simple enough to be understood and implemented by one person. Still, it needs to be fast enough to be able to run Doom at a playable frame rate with C compiler overhead.

The story of how it was designed, step by step, is in the CPU blog series.

Features

FeatureB32P4
Instruction set32-bit RISC, fixed 32-bit instructions, 16 opcodes (see Instruction set)
Registers16 registers of 32 bits, r0 is always 0
Memorybyte-addressed and little-endian, reads and writes of 8, 16 and 32 bits
Pipelinefive stages (IF, ID, EX, MEM, WB), at best one instruction per cycle
Clock100 MHz, one clock
Cachesan instruction cache (L1I) of 4 KiB and a data cache (L1D) of 16 KiB, direct-mapped, write-back, with lines of 8 words
Multiply and dividea pipelined multiplier that can start a multiply every cycle, and a divider that computes one bit per cycle, so a divide takes 34 cycles (50 for divfp)
Fixed-pointQ16.16 multiply and divide instructions
Hardware stack256 words, for push and pop
Interruptsa configurable number of inputs, taken at jumps and branches
Error registerrecords illegal instructions and accesses, and keeps its value during a reset

B32P4 is a one-person learning project, not a competitor for RISC-V or ARM. So it leaves out what the FPGC doesn’t need:

  • No branch prediction. The CPU always continues with the next instruction, and a taken branch throws away the instructions behind it.
  • No floating point. Fixed-point instructions are used instead.
  • No privilege levels, memory protection or MMU. There is one user, and every program can access everything.
  • No superscalar or out-of-order execution. One instruction enters the pipeline per cycle, in program order.

Block diagram

                                        interrupts
                                             v
            +----+    +----+    +----+    +-----+    +----+
ROM --------| IF |--->| ID |--->| EX |--->| MEM |--->| WB |
            +----+    +----+    +----+    +-----+    +----+
               |                          |    |
            +-----+                    +-----+ |
            | L1I |                    | L1D | +---- bus
            +-----+                    +-----+
               |                          |
            +--------------------------------+
            |        cache controller        |
            +--------------------------------+
                             |
                      SDRAM line port
PartWhat it does
IF (fetch)holds the program counter, and reads the instruction from the ROM or the L1I
ID (decode)decodes the instruction, reads its registers, and computes jump and branch targets
EX (execute)the ALU, the branch compare, the memory address, multiply and divide, and the decision to take an interrupt
MEM (memory)reads and writes memory, picks the result, and makes the instruction’s changes final. Jumps, taken branches and interrupts continue at their target from here.
WB (write back)writes the result into the register file
L1I, L1Dthe instruction and data caches for SDRAM. A hit costs no extra cycles.
cache controllerhandles misses: it writes a changed line back to SDRAM, and reads the missing line
ROMthe boot ROM, read directly by IF. The CPU starts here after a reset.
SDRAM line portthe connection to the SDRAM controller, which transfers whole cache lines
busthe connection for every other access: data reads from the ROM, and the I/O devices
interruptsthe interrupt inputs

Pages

Each page says at the top who it’s written for: programmers, hardware designers (who connect something to the CPU), or RTL readers (who read or change the Verilog).

PageForWhat it answers
Instruction setprogrammerswhat every instruction does, and how it’s encoded
Programming notesprogrammerswhat instructions cost in cycles, and what software must do for caches, interrupts and reset
Interfacehardware designersthe ports of the CPU and how to connect a ROM, an SDRAM controller and devices to them
PipelineRTL readershow instructions move through the stages, and how stalls, flushes and forwarding work
CachesRTL readershow the L1I, the L1D and the cache controller work
RTL structureRTL readersthe Verilog files and modules, and the rules that keep the design at 100 MHz
Historyeveryonethe CPUs before B32P4

The address space of the whole system, including the regions that the CPU reaches over the bus, is on the memory map page.

Parameters

ParameterDefaultMeaning
INTERRUPTS2the number of interrupt inputs, at most 255
L1I_LINES128the number of L1I lines, a power of two. A line is 32 bytes, so the default is 4 KiB.
L1D_LINES512the number of L1D lines, a power of two. The default is 16 KiB.

The FPGC uses the default cache sizes. Smaller caches are only used in tests, because they cause more misses.

Terms

The other CPU pages use these words without explaining them again.

TermMeaning
areg, breg, dregthe two source registers and the destination register of an instruction
bubblean empty slot in the pipeline: a pipeline register whose valid bit is 0
busthe CPU’s connection for every access that isn’t to SDRAM or to the CPU itself
busya stage that needs more cycles for its instruction. A busy stage stalls, and so do the stages before it.
cache line8 words (32 bytes) at an address that is a multiple of 32: what a cache stores and what a miss transfers
committhe moment an instruction changes the state of the machine. This happens in MEM, except for the register write, which happens in WB.
constanta value that is part of the instruction itself (c16 or c27 on the ISA page)
CPU-internal I/O registersthe registers inside the CPU at 0xFFFFFF00, such as PC_BACKUP, SP and the error register
currenta cache output that belongs to the instruction in the stage right now: it was read at that instruction’s address, and the cache controller hasn’t changed that cache since. Only a current output is used.
dirtya cache line that was written, and must be written back to SDRAM before it’s replaced
early restarttaking the needed word from SDRAM the moment it arrives, before the rest of the line
flushthrowing away the instructions in the earlier stages, by setting their valid bits to 0
forwardinggiving an instruction a result that isn’t in the register file yet
hit, misswhether the cache holds the line that an access needs
late resulta result that only exists at the end of MEM: reads, pops, multiplies, divides and intid
late-result stallthe one-cycle wait in ID of an instruction that needs the late result of the instruction directly ahead of it
line portthe connection between the cache controller and the SDRAM controller, which transfers one cache line at a time
pipeline registerthe registers between two stages that hold an instruction and what’s known about it so far (IF/ID, ID/EX, EX/MEM and MEM/WB)
push-then-pop stallthe one-cycle wait of a pop directly behind a push
redirectcontinuing at another address than the next instruction: a jump, a taken branch, halt, an interrupt, reti or ccache
retirean instruction finishing. For simulation, the CPU reports every retired instruction on the retire port.
stalla stage keeping its instruction for another cycle
valid bitthe bit in each pipeline register that says whether it holds an instruction
write-then-access stallthe one-cycle wait of a read or write directly behind a write
Last updated on