Z-80 computer with Dartmouth-style BASIC as the terminal's console

SW15 switches the terminal from the PC's serial line to a Z-80 running at
90 MHz from its own clock, with 64 KB of block RAM preloaded with BASIC.

- rtl/z80_cpu.v: Z-80 built for speed (one clock per memory access, next
  byte read during decode), full documented instruction set plus the
  common undocumented forms; no interrupts.
- rtl/z80_system.v: RAM, console/timer/7-segment ports, toggle handshakes
  to the terminal clock.
- basic/: BASIC interpreter in C for SDCC, start-up code, prebuilt image.
- tools/z80emu.py, tools/cputest.py, scripts/cpusim.sh: reference emulator
  and random tests comparing the CPU after every instruction.
- SW14 feeds the PC's UART into the Z-80 for pasting programs; BTND
  resets the Z-80 only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-27 20:25:41 -08:00
1 parent 1e4e2101b8
commit 59d5e617f2
20 files changed
+68756 -48

No files matched your search

+67
View File
@@ -0,0 +1,67 @@
`timescale 1ns / 1ps
// CPU-only test: runs an image in 64 KB of RAM and writes a register trace at
// the start of every instruction (compared against tools/z80emu.py).
// Console: port 0 reads {tx ready, input available}, port 1 reads/writes data.
module tb_cpu;
reg clk = 0, rst = 1;
always #5 clk = ~clk;
wire [15:0] ma, io_addr;
wire we, io_rd, io_wr, halted, ins_start;
wire [7:0] wd, io_wd;
reg [7:0] din, io_din;
wire [15:0] pc, af, bc, de, hl, ix, iy, sp;
z80_cpu cpu (
.clk(clk), .rst(rst), .ma(ma), .we(we), .wd(wd), .din(din),
.io_addr(io_addr), .io_rd(io_rd), .io_wr(io_wr), .io_wd(io_wd), .io_din(io_din),
.halted(halted), .ins_start(ins_start), .dbg_pc(pc),
.dbg_af(af), .dbg_bc(bc), .dbg_de(de), .dbg_hl(hl), .dbg_ix(ix), .dbg_iy(iy), .dbg_sp(sp)
);
reg [7:0] mem [0:65535];
integer rom_top = 0;
always @(posedge clk) begin
if (we && ma >= rom_top) mem[ma] <= wd;
din <= mem[ma];
end
reg [7:0] inbuf [0:65535];
integer in_n = 0, in_p = 0, fd, tr, n = 0, cycles = 0, max_cycles;
always @* io_din = io_addr[7:0] == 8'd0 ? {6'd0, 1'b1, in_p < in_n} :
io_addr[7:0] == 8'd1 ? (in_p < in_n ? inbuf[in_p] : 8'd0) : 8'hFF;
always @(posedge clk) if (io_rd && io_addr[7:0] == 8'd1 && in_p < in_n) in_p <= in_p + 1;
always @(posedge clk) if (io_wr && io_addr[7:0] == 8'd1) $write("%c", io_wd);
always @(posedge clk) if (!rst) begin
cycles = cycles + 1;
if (ins_start) begin
if (tr) $fwrite(tr, "%h %h %h %h %h %h %h %h\n", pc, af, bc, de, hl, ix, iy, sp);
n = n + 1;
end
end
initial begin
if (!$value$plusargs("max=%d", max_cycles)) max_cycles = 10_000_000;
$value$plusargs("romtop=%d", rom_top);
for (n = 0; n < 65536; n = n + 1) mem[n] = 8'h00;
n = 0;
$readmemh(`IMAGE, mem);
fd = $fopen(`INPUT, "rb");
if (fd) begin
while (!$feof(fd)) begin inbuf[in_n] = $fgetc(fd); in_n = in_n + 1; end
in_n = in_n - 1;
end
tr = $fopen("trace_hw.txt", "w");
#20 rst = 0;
wait (halted || cycles >= max_cycles);
repeat (2) @(posedge clk);
$display("\n[%0d instructions, %0d clocks%0s]", n, cycles, halted ? ", halted" : "");
$fclose(tr);
fd = $fopen("mem_hw.txt", "w");
for (n = 32768; n < 65536; n = n + 1) $fwrite(fd, "%h\n", mem[n]);
$fclose(fd);
$finish;
end
endmodule
+21 -3
View File
@@ -8,15 +8,15 @@ module tb_terminal;
always #5 clk = ~clk;
reg [15:0] sw = 16'h0200; // 115200 baud, fast smooth scroll when enabled
reg btnC = 0, btnU = 0;
reg btnC = 0, btnU = 0, btnD = 0;
reg rsrx = 1;
reg ps2c = 1, ps2d = 1;
wire rstx, hs, vs;
wire [3:0] r, g, b;
wire [15:0] led;
basys3_terminal #(.FONT_FILE(`FONT_FILE)) dut (
.clk(clk), .btnC(btnC), .btnU(btnU), .sw(sw), .led(led), .seg(), .dp(), .an(),
basys3_terminal #(.FONT_FILE(`FONT_FILE), .MEM_FILE(`MEM_FILE)) dut (
.clk(clk), .btnC(btnC), .btnU(btnU), .btnD(btnD), .sw(sw), .led(led), .seg(), .dp(), .an(),
.vgaRed(r), .vgaGreen(g), .vgaBlue(b), .Hsync(hs), .Vsync(vs),
.RsRx(rsrx), .RsTx(rstx), .PS2Clk(ps2c), .PS2Data(ps2d)
);
@@ -245,6 +245,24 @@ module tb_terminal;
expect_row(17, "r7");
expect_row(18, "below region");
uart_str("\033[?4l\033[r");
dump_screen;
// ---- Z-80 BASIC as the console, input pasted over the UART
uart_str("\033[2J\033[H");
#100_000;
sw[15] = 1; sw[14] = 1; // the Z-80 has been waiting to print its banner
#3_000_000;
uart_str("PRINT 2+2\015");
#2_000_000;
uart_str("10 FOR I=1 TO 3: PRINT I*I;: NEXT I\015RUN\015");
#5_000_000;
expect_row(1, "DARTMOUTH-STYLE BASIC");
expect_row(3, "READY");
expect_row(4, "PRINT 2+2");
expect_row(5, " 4");
expect_row(7, "READY");
expect_row(10, " 1 4 9");
expect_row(11, "READY");
dump_screen;
if (errors == 0) $display("ALL TESTS PASSED");