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:
1 parent
1e4e2101b8
commit
59d5e617f2
20 files changed
+68756
-48
No files matched your search
+134
-36
@@ -1,8 +1,9 @@
|
||||
`timescale 1ns / 1ps
|
||||
|
||||
// Basys 3 VT220 terminal with ANSI colour.
|
||||
// Keyboard (USB HID port) -> UART TX (USB-UART port)
|
||||
// UART RX -> 80x30 colour text on VGA
|
||||
// Basys 3 VT220 terminal with ANSI colour, and a Z-80 computer running BASIC.
|
||||
// SW15 down: keyboard (USB HID port) -> UART TX (USB-UART port),
|
||||
// UART RX -> 80x30 colour text on VGA
|
||||
// SW15 up: the terminal is the Z-80's console
|
||||
//
|
||||
// SW1:0 baud rate: 00 115200, 01 9600, 10 19200, 11 57600 (8N1)
|
||||
// SW2 local echo
|
||||
@@ -12,21 +13,29 @@
|
||||
// SW7 Enter sends CR LF instead of CR
|
||||
// SW8 smooth scrolling (also turned on by ESC [ ? 4 h)
|
||||
// SW9 fast smooth scrolling
|
||||
// BTNC reset BTNU clear screen
|
||||
// SW14 in Z-80 mode, bytes from the PC's UART also go to the Z-80 (paste programs)
|
||||
// SW15 Z-80 mode
|
||||
// BTNC reset BTNU clear screen BTND reset the Z-80 only
|
||||
// LED7:0 last byte received LED11:8 VT220 LEDs L4..L1 (DECLL)
|
||||
// LED12 bell LED13 Caps Lock LED14 UART RX activity LED15 keyboard activity
|
||||
// 7-seg last scan code (left), last byte sent (right)
|
||||
// 7-seg last scan code (left), last byte sent (right); in Z-80 mode, ports 21h/20h
|
||||
//
|
||||
// The terminal sends XOFF when its input buffer passes 256 bytes and XON when
|
||||
// it drains below 32, so smooth scrolling can hold the host back.
|
||||
// it drains below 32, so smooth scrolling can hold the host back. In Z-80 mode
|
||||
// it does the same for the Z-80's input buffer, for text pasted over the UART.
|
||||
|
||||
module basys3_terminal #(
|
||||
parameter FONT_FILE = "font.mem",
|
||||
parameter CLK_HZ = 100_000_000
|
||||
parameter MEM_FILE = "basic.mem",
|
||||
parameter CLK_HZ = 100_000_000,
|
||||
parameter real CPU_MULT = 9.0, // Z-80 clock = 100 MHz * CPU_MULT / CPU_DIV
|
||||
parameter real CPU_DIV = 10.0,
|
||||
parameter CPU_HZ = 90_000_000
|
||||
) (
|
||||
input clk,
|
||||
input btnC,
|
||||
input btnU,
|
||||
input btnD,
|
||||
input [15:0] sw,
|
||||
output [15:0] led,
|
||||
output [6:0] seg,
|
||||
@@ -55,12 +64,15 @@ module basys3_terminal #(
|
||||
rst <= !por[15];
|
||||
end
|
||||
|
||||
reg [9:0] sw_r;
|
||||
reg [1:0] clr_sync;
|
||||
reg [15:0] sw_r;
|
||||
reg [1:0] clr_sync, zrst_sync;
|
||||
always @(posedge clk) begin
|
||||
sw_r <= sw[9:0];
|
||||
clr_sync <= {clr_sync[0], btnU};
|
||||
sw_r <= sw;
|
||||
clr_sync <= {clr_sync[0], btnU};
|
||||
zrst_sync <= {zrst_sync[0], btnD};
|
||||
end
|
||||
wire z80_mode = sw_r[15];
|
||||
wire z80_link = sw_r[14];
|
||||
|
||||
reg [13:0] baud_div;
|
||||
always @(posedge clk)
|
||||
@@ -100,10 +112,11 @@ module basys3_terminal #(
|
||||
.ascii(key), .ascii_valid(key_valid), .caps(caps)
|
||||
);
|
||||
|
||||
// ------------------------------------------------------------- transmit
|
||||
// Sources: flow control (first), then keyboard and terminal replies. The
|
||||
// keyboard/reply choice sticks until that queue is empty, so multi-byte
|
||||
// sequences are never interleaved.
|
||||
// ----------------------------------------------------------- host input
|
||||
// Keyboard bytes and terminal replies go to the host: the PC's UART, or in
|
||||
// Z-80 mode the Z-80's input FIFO. The keyboard/reply choice sticks until
|
||||
// that queue is empty, so multi-byte sequences are never interleaved.
|
||||
// Flow control bytes for the PC go first.
|
||||
wire [7:0] kq, rq;
|
||||
wire kq_empty, rq_empty, rq_full;
|
||||
wire [7:0] resp_byte;
|
||||
@@ -124,7 +137,27 @@ module basys3_terminal #(
|
||||
.rd(rq_rd), .dout(rq), .empty(rq_empty), .full(rq_full), .count()
|
||||
);
|
||||
|
||||
wire [7:0] h_byte = from_resp ? rq : kq;
|
||||
wire h_avail = from_resp ? !rq_empty : !kq_empty;
|
||||
wire pop_pend = kq_rd | rq_rd; // last byte taken pops this clock
|
||||
|
||||
// Z-80 input FIFO: keyboard/replies, and the PC's UART when linked
|
||||
wire [7:0] rx_byte;
|
||||
wire rx_valid;
|
||||
wire [7:0] zin;
|
||||
wire zin_empty, zin_full;
|
||||
wire [10:0] zin_count;
|
||||
reg zin_rd;
|
||||
wire rx_to_z = z80_mode && z80_link && rx_valid;
|
||||
wire take_z = z80_mode && h_avail && !zin_full && !rx_to_z && !pop_pend;
|
||||
|
||||
fifo #(.W(8), .A(10)) z80_in_fifo (
|
||||
.clk(clk), .rst(rst), .din(rx_to_z ? rx_byte : h_byte), .wr(rx_to_z || take_z),
|
||||
.rd(zin_rd), .dout(zin), .empty(zin_empty), .full(zin_full), .count(zin_count)
|
||||
);
|
||||
|
||||
wire [10:0] tq_count;
|
||||
wire [10:0] flow_count = z80_mode ? zin_count : tq_count;
|
||||
reg xoff_sent;
|
||||
|
||||
always @(posedge clk) begin
|
||||
@@ -136,27 +169,29 @@ module basys3_terminal #(
|
||||
flow_pend <= 1'b0;
|
||||
xoff_sent <= 1'b0;
|
||||
end else begin
|
||||
if (!flow_pend && !xoff_sent && tq_count >= 256) begin
|
||||
if (!flow_pend && !xoff_sent && flow_count >= 256) begin
|
||||
flow_pend <= 1'b1; flow_byte <= XOFF; xoff_sent <= 1'b1;
|
||||
end else if (!flow_pend && xoff_sent && tq_count < 32) begin
|
||||
end else if (!flow_pend && xoff_sent && flow_count < 32) begin
|
||||
flow_pend <= 1'b1; flow_byte <= XON; xoff_sent <= 1'b0;
|
||||
end
|
||||
|
||||
if (take_z) begin
|
||||
kq_rd <= !from_resp;
|
||||
rq_rd <= from_resp;
|
||||
end else if (!h_avail)
|
||||
from_resp <= !from_resp;
|
||||
|
||||
if (!tx_busy && !tx_start) begin
|
||||
if (flow_pend) begin
|
||||
tx_byte <= flow_byte;
|
||||
tx_start <= 1'b1;
|
||||
flow_pend <= 1'b0;
|
||||
end else if (!from_resp && !kq_empty) begin
|
||||
tx_byte <= kq;
|
||||
end else if (!z80_mode && h_avail && !pop_pend) begin
|
||||
tx_byte <= h_byte;
|
||||
tx_start <= 1'b1;
|
||||
kq_rd <= 1'b1;
|
||||
end else if (from_resp && !rq_empty) begin
|
||||
tx_byte <= rq;
|
||||
tx_start <= 1'b1;
|
||||
rq_rd <= 1'b1;
|
||||
end else
|
||||
from_resp <= !from_resp;
|
||||
kq_rd <= !from_resp;
|
||||
rq_rd <= from_resp;
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -167,9 +202,6 @@ module basys3_terminal #(
|
||||
);
|
||||
|
||||
// -------------------------------------------------------------- receive
|
||||
wire [7:0] rx_byte;
|
||||
wire rx_valid;
|
||||
|
||||
uart_rx urx (
|
||||
.clk(clk), .rst(rst), .div(baud_div), .rx(RsRx),
|
||||
.data(rx_byte), .valid(rx_valid)
|
||||
@@ -182,10 +214,69 @@ module basys3_terminal #(
|
||||
"\033[31mc\033[33mo\033[32ml\033[36mo\033[34mu\033[35mr\033[0m ",
|
||||
"\033[4munderline\033[0m\015\012",
|
||||
"UART RX -> screen, keyboard -> UART TX TERM=basys3 or vt220\015\012\012"};
|
||||
// (not shown in Z-80 mode, where BASIC prints its own)
|
||||
reg [7:0] ban_i;
|
||||
wire [7:0] ban_c = BANNER[ban_i*8 +: 8];
|
||||
|
||||
// terminal input: received bytes, local echo, banner (in that priority)
|
||||
// ---------------------------------------------------------------- Z-80
|
||||
wire cpu_clk, locked;
|
||||
`ifdef SIMULATION
|
||||
assign cpu_clk = clk;
|
||||
assign locked = 1'b1;
|
||||
`else
|
||||
wire cpu_fb, cpu_clk_mmcm;
|
||||
MMCME2_BASE #(
|
||||
.CLKIN1_PERIOD(10.0), .DIVCLK_DIVIDE(1),
|
||||
.CLKFBOUT_MULT_F(CPU_MULT), .CLKOUT0_DIVIDE_F(CPU_DIV)
|
||||
) cpu_mmcm (
|
||||
.CLKIN1(clk), .CLKFBIN(cpu_fb), .CLKFBOUT(cpu_fb), .CLKFBOUTB(),
|
||||
.CLKOUT0(cpu_clk_mmcm), .CLKOUT0B(), .CLKOUT1(), .CLKOUT1B(), .CLKOUT2(),
|
||||
.CLKOUT2B(), .CLKOUT3(), .CLKOUT3B(), .CLKOUT4(), .CLKOUT5(), .CLKOUT6(),
|
||||
.LOCKED(locked), .PWRDWN(1'b0), .RST(1'b0)
|
||||
);
|
||||
BUFG cpu_bufg (.I(cpu_clk_mmcm), .O(cpu_clk));
|
||||
`endif
|
||||
|
||||
(* ASYNC_REG = "TRUE" *) reg [2:0] cpu_rst_s;
|
||||
always @(posedge cpu_clk) cpu_rst_s <= {cpu_rst_s[1:0], rst | zrst_sync[1] | !locked};
|
||||
|
||||
// Z-80 input: one byte at a time from z80_in_fifo
|
||||
reg zrx_req, zrx_pend;
|
||||
reg [7:0] zrx_data;
|
||||
wire zrx_ack;
|
||||
(* ASYNC_REG = "TRUE" *) reg [1:0] zrx_ack_s;
|
||||
always @(posedge clk) begin
|
||||
zin_rd <= 1'b0;
|
||||
zrx_ack_s <= {zrx_ack_s[0], zrx_ack};
|
||||
if (rst)
|
||||
zrx_pend <= 1'b0;
|
||||
else if (!zrx_pend && !zin_empty && !zin_rd) begin
|
||||
zrx_data <= zin;
|
||||
zin_rd <= 1'b1;
|
||||
zrx_req <= ~zrx_req;
|
||||
zrx_pend <= 1'b1;
|
||||
end else if (zrx_pend && zrx_ack_s[1] == zrx_req)
|
||||
zrx_pend <= 1'b0;
|
||||
end
|
||||
initial zrx_req = 1'b0;
|
||||
|
||||
// Z-80 output
|
||||
wire ztx_req;
|
||||
wire [7:0] ztx_data;
|
||||
reg ztx_ack = 1'b0;
|
||||
(* ASYNC_REG = "TRUE" *) reg [1:0] ztx_req_s;
|
||||
always @(posedge clk) ztx_req_s <= {ztx_req_s[0], ztx_req};
|
||||
|
||||
wire [15:0] z80_seg;
|
||||
|
||||
z80_system #(.MEM_FILE(MEM_FILE), .CPU_HZ(CPU_HZ)) z80 (
|
||||
.clk(cpu_clk), .rst(cpu_rst_s[2]),
|
||||
.rx_req(zrx_req), .rx_data(zrx_data), .rx_ack(zrx_ack),
|
||||
.tx_req(ztx_req), .tx_data(ztx_data), .tx_ack(ztx_ack),
|
||||
.seg_val(z80_seg), .halted()
|
||||
);
|
||||
|
||||
// terminal input: Z-80 output or received bytes, local echo, banner
|
||||
reg echo_pend;
|
||||
reg [7:0] echo_byte;
|
||||
reg [7:0] tin;
|
||||
@@ -194,20 +285,26 @@ module basys3_terminal #(
|
||||
|
||||
always @(posedge clk) begin
|
||||
tin_wr <= 1'b0;
|
||||
if (kq_rd && sw_r[2]) begin
|
||||
if (kq_rd && sw_r[2] && !z80_mode) begin
|
||||
echo_pend <= 1'b1;
|
||||
echo_byte <= kq;
|
||||
end
|
||||
if (rst) begin
|
||||
echo_pend <= 1'b0;
|
||||
ban_i <= 8'd199;
|
||||
ban_i <= z80_mode ? 8'hFF : 8'd199;
|
||||
end else if (z80_mode) begin
|
||||
if (ztx_req_s[1] != ztx_ack && !tin_wr && tq_count < 1000) begin
|
||||
tin <= ztx_data;
|
||||
tin_wr <= 1'b1;
|
||||
ztx_ack <= ~ztx_ack;
|
||||
end
|
||||
end else if (rx_valid) begin
|
||||
tin <= rx_byte;
|
||||
tin_wr <= 1'b1;
|
||||
end else if (echo_pend && !tin_full) begin
|
||||
tin <= echo_byte;
|
||||
tin_wr <= 1'b1;
|
||||
echo_pend <= kq_rd && sw_r[2];
|
||||
echo_pend <= kq_rd && sw_r[2] && !z80_mode;
|
||||
end else if (ban_i != 8'hFF && !tin_full && !tin_wr) begin
|
||||
ban_i <= ban_i - 1'b1;
|
||||
if (ban_c != 0) begin
|
||||
@@ -273,10 +370,10 @@ module basys3_terminal #(
|
||||
reg [22:0] rx_led, kb_led, bell_led; // ~84 ms pulse stretchers
|
||||
|
||||
always @(posedge clk) begin
|
||||
if (rx_valid) last_rx <= rx_byte;
|
||||
if (z80_mode ? tin_wr : rx_valid) last_rx <= z80_mode ? tin : rx_byte;
|
||||
if (scan_valid) last_scan <= scan;
|
||||
if (tx_start) last_tx <= tx_byte;
|
||||
rx_led <= rx_valid ? 23'h7FFFFF : rx_led - (rx_led != 0);
|
||||
rx_led <= (z80_mode ? tin_wr : rx_valid) ? 23'h7FFFFF : rx_led - (rx_led != 0);
|
||||
kb_led <= scan_valid ? 23'h7FFFFF : kb_led - (kb_led != 0);
|
||||
bell_led <= bell ? 23'h7FFFFF : bell_led - (bell_led != 0);
|
||||
end
|
||||
@@ -284,5 +381,6 @@ module basys3_terminal #(
|
||||
assign led = {kb_led != 0, rx_led != 0, caps, bell_led != 0, dec_leds[3:0], last_rx};
|
||||
assign dp = 1'b1;
|
||||
|
||||
sevenseg digits (.clk(clk), .value({last_scan, last_tx}), .seg(seg), .an(an));
|
||||
sevenseg digits (.clk(clk), .value(z80_mode ? z80_seg : {last_scan, last_tx}),
|
||||
.seg(seg), .an(an));
|
||||
endmodule
|
||||
Reference in new issue
Block a user