Basys 3 RISC-V computer

The Basys 3 Z-80 computer with the Z-80 replaced by an RV32IM processor
written from scratch (rtl/rv32_cpu.v): multi-cycle, 90 MHz, CSRs and
traps, interrupts, two hardware triggers and a resumable NMI for the
monitor. Passes riscv-tests rv32ui/um/mi.

rv32_system.v keeps all the Z-80 machine's peripherals, with I/O port N
at FFFFFF00h + N, 80 KB of RAM with BASIC preloaded, and a 32 KB monitor
ROM. BASIC, the five games and the monitor (with an RV32 disassembler,
assembler, breakpoints and single step) are ported to RISC-V GCC. Python
emulator and tests in tools/; full-system simulation in sim/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-30 18:09:18 -08:00
commit 5930b958b4
103 files changed
+55677

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
# Bench program: the LEDs count up about 5 times a second at 90 MHz, using
# mul and div so they are exercised.
.text
.globl _start
_start:
li s0, 0x10000 # LED register
li s1, 0 # count
1: li t0, 3000000 # ~ 6 cycles per loop
2: addi t0, t0, -1
bnez t0, 2b
addi s1, s1, 1
li t1, 7
mul t2, s1, t1 # count * 7 / 7 = count
divu t2, t2, t1
sw t2, 0(s0)
j 1b
+12
View File
@@ -0,0 +1,12 @@
00010437
00000493
002dc2b7
6c028293
fff28293
fe029ee3
00148493
00700313
026483b3
0263d3b3
00742023
fddff06f
+34
View File
@@ -0,0 +1,34 @@
set_property -dict { PACKAGE_PIN W5 IOSTANDARD LVCMOS33 } [get_ports clk]
create_clock -add -name sys_clk_pin -period 10.00 -waveform {0 5} [get_ports clk]
set_property -dict { PACKAGE_PIN V17 IOSTANDARD LVCMOS33 } [get_ports {sw[0]}]
set_property -dict { PACKAGE_PIN V16 IOSTANDARD LVCMOS33 } [get_ports {sw[1]}]
set_property -dict { PACKAGE_PIN W16 IOSTANDARD LVCMOS33 } [get_ports {sw[2]}]
set_property -dict { PACKAGE_PIN W17 IOSTANDARD LVCMOS33 } [get_ports {sw[3]}]
set_property -dict { PACKAGE_PIN W15 IOSTANDARD LVCMOS33 } [get_ports {sw[4]}]
set_property -dict { PACKAGE_PIN V15 IOSTANDARD LVCMOS33 } [get_ports {sw[5]}]
set_property -dict { PACKAGE_PIN W14 IOSTANDARD LVCMOS33 } [get_ports {sw[6]}]
set_property -dict { PACKAGE_PIN W13 IOSTANDARD LVCMOS33 } [get_ports {sw[7]}]
set_property -dict { PACKAGE_PIN V2 IOSTANDARD LVCMOS33 } [get_ports {sw[8]}]
set_property -dict { PACKAGE_PIN T3 IOSTANDARD LVCMOS33 } [get_ports {sw[9]}]
set_property -dict { PACKAGE_PIN T2 IOSTANDARD LVCMOS33 } [get_ports {sw[10]}]
set_property -dict { PACKAGE_PIN R3 IOSTANDARD LVCMOS33 } [get_ports {sw[11]}]
set_property -dict { PACKAGE_PIN W2 IOSTANDARD LVCMOS33 } [get_ports {sw[12]}]
set_property -dict { PACKAGE_PIN U1 IOSTANDARD LVCMOS33 } [get_ports {sw[13]}]
set_property -dict { PACKAGE_PIN T1 IOSTANDARD LVCMOS33 } [get_ports {sw[14]}]
set_property -dict { PACKAGE_PIN R2 IOSTANDARD LVCMOS33 } [get_ports {sw[15]}]
set_property -dict { PACKAGE_PIN U16 IOSTANDARD LVCMOS33 } [get_ports {led[0]}]
set_property -dict { PACKAGE_PIN E19 IOSTANDARD LVCMOS33 } [get_ports {led[1]}]
set_property -dict { PACKAGE_PIN U19 IOSTANDARD LVCMOS33 } [get_ports {led[2]}]
set_property -dict { PACKAGE_PIN V19 IOSTANDARD LVCMOS33 } [get_ports {led[3]}]
set_property -dict { PACKAGE_PIN W18 IOSTANDARD LVCMOS33 } [get_ports {led[4]}]
set_property -dict { PACKAGE_PIN U15 IOSTANDARD LVCMOS33 } [get_ports {led[5]}]
set_property -dict { PACKAGE_PIN U14 IOSTANDARD LVCMOS33 } [get_ports {led[6]}]
set_property -dict { PACKAGE_PIN V14 IOSTANDARD LVCMOS33 } [get_ports {led[7]}]
set_property -dict { PACKAGE_PIN V13 IOSTANDARD LVCMOS33 } [get_ports {led[8]}]
set_property -dict { PACKAGE_PIN V3 IOSTANDARD LVCMOS33 } [get_ports {led[9]}]
set_property -dict { PACKAGE_PIN W3 IOSTANDARD LVCMOS33 } [get_ports {led[10]}]
set_property -dict { PACKAGE_PIN U3 IOSTANDARD LVCMOS33 } [get_ports {led[11]}]
set_property -dict { PACKAGE_PIN P3 IOSTANDARD LVCMOS33 } [get_ports {led[12]}]
set_property -dict { PACKAGE_PIN N3 IOSTANDARD LVCMOS33 } [get_ports {led[13]}]
set_property -dict { PACKAGE_PIN P1 IOSTANDARD LVCMOS33 } [get_ports {led[14]}]
set_property -dict { PACKAGE_PIN L1 IOSTANDARD LVCMOS33 } [get_ports {led[15]}]
+25
View File
@@ -0,0 +1,25 @@
# Timing build of the core alone: vivado -mode batch -source bench/build.tcl
# MULT/DIV environment variables set the clock (default 9/10 = 90 MHz).
set root [file normalize [file dirname [info script]]/..]
set out $root/build/bench
file mkdir $out
set mult [expr {[info exists ::env(MULT)] ? $::env(MULT) : 9.0}]
set div [expr {[info exists ::env(DIV)] ? $::env(DIV) : 10.0}]
puts "CPU clock: [expr {100.0 * $mult / $div}] MHz"
read_verilog $root/rtl/rv32_cpu.v $root/bench/cpu_bench.v
read_mem $root/bench/bench.mem
read_xdc $root/bench/bench.xdc
synth_design -top cpu_bench -part xc7a35tcpg236-1 -generic MULT=$mult -generic DIV=$div
opt_design -directive Explore
place_design -directive ExtraTimingOpt
phys_opt_design -directive AggressiveExplore
route_design -directive Explore
phys_opt_design -directive AggressiveExplore
report_utilization -file $out/utilization.rpt
report_utilization -hierarchical -file $out/util_hier.rpt
report_timing_summary -file $out/timing.rpt
report_timing -max_paths 20 -nworst 1 -unique_pins -file $out/paths.rpt
write_bitstream -force $out/cpu_bench.bit
set wns [get_property SLACK [get_timing_paths -max_paths 1 -setup]]
puts "Worst setup slack: $wns ns"
+80
View File
@@ -0,0 +1,80 @@
`timescale 1ns / 1ps
// Timing bench for rv32_cpu: the core with 64 KB of block RAM (instruction
// fetch on one port, loads and stores on the other) at 100 MHz * MULT / DIV.
// The LEDs show a register at 0x10000; switches 3:0 drive the interrupts.
module cpu_bench #(
parameter real MULT = 9.0,
parameter real DIV = 10.0,
parameter MEM_FILE = "bench.mem"
) (
input clk,
input [15:0] sw,
output reg [15:0] led
);
wire fb, cclk_m, cclk, locked;
MMCME2_BASE #(
.CLKIN1_PERIOD(10.0), .DIVCLK_DIVIDE(1),
.CLKFBOUT_MULT_F(MULT), .CLKOUT0_DIVIDE_F(DIV)
) mmcm (
.CLKIN1(clk), .CLKFBIN(fb), .CLKFBOUT(fb), .CLKFBOUTB(),
.CLKOUT0(cclk_m), .CLKOUT0B(), .CLKOUT1(), .CLKOUT1B(), .CLKOUT2(),
.CLKOUT2B(), .CLKOUT3(), .CLKOUT3B(), .CLKOUT4(), .CLKOUT5(), .CLKOUT6(),
.LOCKED(locked), .PWRDWN(1'b0), .RST(1'b0)
);
BUFG bufg (.I(cclk_m), .O(cclk));
reg [3:0] rst_sr = 4'hF;
always @(posedge cclk) rst_sr <= {rst_sr[2:0], !locked};
reg [3:0] sw_r;
always @(posedge cclk) sw_r <= sw[3:0];
wire i_en, d_req, d_we;
wire [31:0] i_addr, d_addr, d_wdata;
wire [3:0] d_wstrb;
reg [31:0] i_rdata, ram_q;
reg d_ack, io_sel;
rv32_cpu cpu (
.clk(cclk), .rst(rst_sr[3]),
.i_en(i_en), .i_addr(i_addr), .i_rdata(i_rdata),
.d_req(d_req), .d_we(d_we), .d_wstrb(d_wstrb), .d_addr(d_addr),
.d_wdata(d_wdata), .d_rdata(io_sel ? {16'd0, led} : ram_q), .d_ack(d_ack),
.irq_soft(sw_r[0]), .irq_timer(sw_r[1]), .irq_ext(sw_r[2]),
.irq_local({15'd0, sw_r[3]}), .nmi(1'b0), .nmi_step(1'b0), .nmi_taken(),
.dbg_pc(), .dbg_decode(), .retire()
);
// Reads use the core's address directly (a read with no request is
// harmless); stores are registered and written the next cycle, so the
// late d_req never reaches the block RAM's write enables.
reg [31:0] ram [0:16383];
initial $readmemh(MEM_FILE, ram);
wire ram_sel = d_addr[31:16] == 16'd0;
reg w_go;
reg [13:0] w_a;
reg [31:0] w_d;
reg [3:0] w_s;
integer b;
always @(posedge cclk) begin
if (i_en) i_rdata <= ram[i_addr[15:2]];
end
// one address for the data port: the pending store's, else the core's
wire [13:0] b_a = w_go ? w_a : d_addr[15:2];
always @(posedge cclk) begin
if (w_go) begin
for (b = 0; b < 4; b = b + 1)
if (w_s[b]) ram[b_a][8*b +: 8] <= w_d[8*b +: 8];
end
ram_q <= ram[b_a];
end
always @(posedge cclk) begin
w_go <= d_req && d_we && ram_sel;
w_a <= d_addr[15:2];
w_d <= d_wdata;
w_s <= d_wstrb;
d_ack <= d_req; // the store is written as the ack goes back
if (d_req) io_sel <= !ram_sel;
if (d_req && !ram_sel && d_we) led <= d_wdata[15:0];
end
endmodule