This project is a 16-bit CPU in VHDL for the Terasic DE10-Lite board (Intel MAX 10 FPGA). The project includes:
an instruction set of my own design
a multi-cycle CPU core
a two-pass assembler in Python
a self-test program that shows PASS or FAIL on an ST7735 screen.
Instruction set
Each instruction is one 32-bit word. The word has an 8-bit opcode, two 4-bit register fields and a 16-bit immediate value. Data and addresses are 16 bits wide.The CPU has 16 registers. Register R0 always contains zero. The instruction set has 22 instructions:
load a constant, and copy a register
add and subtract
logic operations and shifts
load and store, with an absolute address or with a base register and an offset
three branches relative to the program counter.
The CPU has no flags. The only comparison is equality, with BEQ and BNE. The CPU has no stack and no call instruction.
Core
The core is a multi-cycle state machine. It does not use a pipeline. Each instruction goes through fetch, decode and execute. Loads and stores also have a memory phase.Instructions and data are in two separate block RAMs. The reads from these RAMs are registered. Thus, fetch and load each need one wait cycle. An ALU instruction takes 4 clock cycles. A load takes 6 clock cycles. At 50 MHz, the CPU does 12.5 million ALU operations per second.
Memory map
Address
Contents
0x2000–0x2FFF
4K words of RAM
0x3000–0x7FFF
128 × 160 framebuffer, 16-bit colour
0xFFFC
test status: test number and RUN, PASS or FAIL
0xFFFD
display source: colour bars or framebuffer
0xFFFE
LEDs
The framebuffer is a dual-port RAM. The CPU writes to one port. A VHDL display driver reads the other port and sends the image to the ST7735 TFT through SPI. The display refreshes at approximately 18 frames per second.
The ROM contains a self-test program of 358 instructions. The program has 19 tests. They cover all types of instruction and check that R0 stays zero. Each test does these steps:
It writes the test number and RUN to the status register.
It executes the instruction under test.
It compares the result with the expected value.
It writes PASS or FAIL to the status register.
If the result is FAIL, the CPU stops.
The display driver shows the results as a scoreboard. The hardware draws the text with a 5×7 font from a lookup table. It does not use a character buffer. The 7-segment displays and the LEDs also show the test number and the result.
Assembler
A two-pass assembler in Python converts the assembly code into VHDL for the ROM. This loop from the test program clears the screen at start-up:
Text
CLR_FB:
STX R7, [R5 + 0] -- write one black pixel
ADDI R5, R5, 1 -- next address
ADDI R6, R6, -1 -- decrement the pixel count
BNE R6, R0, CLR_FB -- repeat for all 20,480 pixels
Limitations
The tests use BEQ to check the results, but the program tests BEQ only at the end. If BEQ always branches, all tests show PASS.
The program is part of the FPGA bitstream. A change to the program needs a new synthesis.
The timing constraints use the clock name of the board template, not the clock name of the design. Thus, the timing analysis does not check the 50 MHz paths.