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

+118
View File
@@ -0,0 +1,118 @@
`timescale 1ns / 1ps
// Runs a list of test images on rv32_cpu, one after another.
// +list=<file>: lines of "<image.mem> <tohost address, hex> <name>"
// A test ends when it writes tohost: 1 is a pass, anything else a failure
// (riscv-tests put the failing test number in bits 31:1).
// The data bus answers after a pseudo-random 1-3 cycles to exercise d_ack.
// Address FFFF_FF00 is a test device: writing N raises the interrupt lines
// in N[5:0] (bit 0 soft, 1 timer, 2 external, 3 local 16, 4 nmi, 5 nmi_step)
// N[31:8] cycles later; any write first drops them. NMI_VEC is 8000h.
module tb_cpu;
reg clk = 0, rst = 1;
always #5 clk = !clk;
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, d_rdata;
reg d_ack;
reg [5:0] irq;
wire retire;
rv32_cpu #(.NMI_VEC(32'h8000)) cpu (
.clk(clk), .rst(rst),
.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(d_rdata), .d_ack(d_ack),
.irq_soft(irq[0]), .irq_timer(irq[1]), .irq_ext(irq[2]),
.irq_local({15'd0, irq[3]}), .nmi(irq[4]), .nmi_step(irq[5]), .nmi_taken(),
.dbg_pc(), .dbg_decode(), .retire(retire)
);
localparam WORDS = 16384; // 64 KB
reg [31:0] mem [0:WORDS-1];
reg [31:0] tohost;
reg done;
reg [31:0] result;
always @(posedge clk) if (i_en) i_rdata <= mem[i_addr[15:2]];
// data bus with random wait states
reg [1:0] wait_n;
reg busy, bwe;
reg [31:0] baddr, bdata;
reg [3:0] bstrb;
reg [31:0] lfsr = 32'h1234_5678;
reg [5:0] irq_set;
reg [23:0] irq_delay;
always @(posedge clk) begin
lfsr <= {lfsr[30:0], lfsr[31] ^ lfsr[21] ^ lfsr[1] ^ lfsr[0]};
d_ack <= 1'b0;
if (d_req) begin
busy <= 1'b1; baddr <= d_addr; bwe <= d_we; bdata <= d_wdata; bstrb <= d_wstrb;
wait_n <= lfsr[1:0] == 2'd3 ? 2'd2 : lfsr[1:0];
end else if (busy) begin
if (wait_n != 0) wait_n <= wait_n - 2'd1;
else begin
busy <= 1'b0;
d_ack <= 1'b1;
d_rdata <= mem[baddr[15:2]];
if (bwe) begin
if (baddr == 32'hFFFF_FF00) begin
irq_set <= bdata[5:0]; irq_delay <= bdata[31:8];
irq <= 6'd0;
end else if (baddr == tohost) begin
done <= 1'b1; result <= bdata;
end else if (baddr[31:16] == 16'd0) begin
if (bstrb[0]) mem[baddr[15:2]][7:0] <= bdata[7:0];
if (bstrb[1]) mem[baddr[15:2]][15:8] <= bdata[15:8];
if (bstrb[2]) mem[baddr[15:2]][23:16] <= bdata[23:16];
if (bstrb[3]) mem[baddr[15:2]][31:24] <= bdata[31:24];
end
end
end
end
if (irq_set != 6'd0) begin
if (irq_delay == 0) begin irq <= irq_set; irq_set <= 6'd0; end
else irq_delay <= irq_delay - 1;
end
if (rst) begin
busy <= 1'b0; done <= 1'b0; irq <= 6'd0; irq_set <= 6'd0;
end
end
integer fd, n, cycles, pass = 0, fail = 0, i;
reg [8*200-1:0] memf, name, list;
initial begin
if (!$value$plusargs("list=%s", list)) begin
$display("need +list=file"); $finish;
end
fd = $fopen(list, "r");
while (!$feof(fd)) begin
n = $fscanf(fd, "%s %h %s\n", memf, tohost, name);
if (n == 3) begin
for (i = 0; i < WORDS; i = i + 1) mem[i] = 32'd0;
$readmemh(memf, mem);
rst = 1;
repeat (4) @(posedge clk);
rst = 0;
cycles = 0;
while (!done && cycles < 200000) begin
@(posedge clk);
cycles = cycles + 1;
end
if (done && result == 32'd1) begin
pass = pass + 1;
$display("PASS %0s (%0d cycles)", name, cycles);
end else begin
fail = fail + 1;
if (done) $display("FAIL %0s test %0d", name, result >> 1);
else $display("FAIL %0s timeout, pc=%h", name, cpu.pc);
end
end
end
$display("%0d passed, %0d failed", pass, fail);
$finish;
end
endmodule