Machine-code monitor, fast serial, memory files, Ctrl-Alt-Del

Monitor: the CPU gets an NMI (taken at an instruction start, or from
HALT) raised by the Pause key, BASIC's MONITOR, two hardware breakpoint
comparators or a step counter. It enters through a trampoline at 0066h
(the same bytes in BASIC's ROM and the monitor's, basic/tramp.s) that
switches in a 32 KB monitor bank over 0000-7FFF. The monitor (monitor/,
C) runs on the terminal's new alternate screen (CSI ?47 h/l, a second
screen page; graphics hidden) and shows registers, flags, a disassembly
from PC, the stack and memory. Commands: Enter/S n step, O step over,
G/X go, B breakpoint, D/M windows, E edit, R registers, and SAVE/LOAD/
DIR for memory files. The disassembler covers every opcode;
monitor/test-disasm.py round-trips all forms through sdasz80. Lockstep
CPU tests still match the reference emulator.

BASIC: MONITOR, CALL addr, BSAVE "N",start,len and BLOAD "N"[,addr]
(memory files: load address then bytes). The DATE clock moves to
F3F8h so the monitor can date files. F000-F3F7 is kept free for
machine code (program area 21076 bytes).

Serial: SW1:0 = 10 is now 921600 and 11 is 3 Mbaud. A 17 KB burst to
BASIC lists back identical at both on the board. send-bas.py reads
the echo while sending; unread, it could stall the PC's flow control.

Ctrl-Alt-Del in Z-80 mode restarts the Z-80 like BTND.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-28 11:17:54 -08:00
1 parent 767377f38d
commit 634e314010
27 files changed
+37481 -3039

No files matched your search

+26 -11
View File
@@ -5,7 +5,7 @@
// 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)
// SW1:0 baud rate: 00 115200, 01 9600, 10 921600, 11 3000000 (8N1)
// SW2 local echo
// SW3 received LF also does CR
// SW5:4 default text colour: 00 green, 01 yellow, 10 white, 11 cyan
@@ -28,6 +28,7 @@
module basys3_terminal #(
parameter FONT_FILE = "font.mem",
parameter MEM_FILE = "basic.mem",
parameter MON_FILE = "monitor.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,
@@ -82,8 +83,8 @@ module basys3_terminal #(
case (sw_r[1:0])
2'b00: baud_div <= CLK_HZ / 115200;
2'b01: baud_div <= CLK_HZ / 9600;
2'b10: baud_div <= CLK_HZ / 19200;
2'b11: baud_div <= CLK_HZ / 57600;
2'b10: baud_div <= CLK_HZ / 921600; // 108: 0.5% fast
2'b11: baud_div <= CLK_HZ / 3000000; // 33: 1% fast
endcase
reg [3:0] def_fg;
@@ -108,13 +109,18 @@ module basys3_terminal #(
.code(scan), .valid(scan_valid)
);
wire cad;
ps2_keymap kbd_map (
.clk(clk), .rst(rst), .code(scan), .code_valid(scan_valid),
.bs_del(sw_r[6]), .enter_crlf(sw_r[7] | lnm),
.cursor_app(cursor_app), .keypad_app(keypad_app),
.ascii(key), .ascii_valid(key_valid), .caps(caps)
.ascii(key), .ascii_valid(key_valid), .caps(caps), .cad(cad)
);
// Ctrl-Alt-Del in Z-80 mode restarts the Z-80, like BTND (held 1 us)
reg [6:0] cad_t = 7'd0;
always @(posedge clk) cad_t <= cad && z80_mode ? 7'd100 : cad_t - (cad_t != 0);
// ----------------------------------------------------------- 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
@@ -259,7 +265,7 @@ module basys3_terminal #(
`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};
always @(posedge cpu_clk) cpu_rst_s <= {cpu_rst_s[1:0], rst | zrst_sync[1] | (cad_t != 0) | !locked};
// Z-80 input: one byte at a time from z80_in_fifo
reg zrx_req, zrx_pend;
@@ -293,11 +299,18 @@ module basys3_terminal #(
wire [16:0] gfx_addr;
wire [3:0] gfx_pix;
z80_system #(.MEM_FILE(MEM_FILE), .CPU_HZ(CPU_HZ)) z80 (
// The Pause key (its scan codes start E1, used by nothing else) enters the
// Z-80 monitor: a toggle crosses to the CPU clock.
reg brk_t = 1'b0;
always @(posedge clk) if (scan_valid && scan == 8'hE1 && z80_mode) brk_t <= ~brk_t;
(* ASYNC_REG = "TRUE" *) reg [2:0] brk_s = 3'd0;
always @(posedge cpu_clk) brk_s <= {brk_s[1:0], brk_t};
z80_system #(.MEM_FILE(MEM_FILE), .MON_FILE(MON_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(),
.seg_val(z80_seg), .halted(), .brk_key(brk_s[2] ^ brk_s[1]),
.vclk(clk), .gfx_vaddr(gfx_addr), .gfx_vpix(gfx_pix),
.fl_cs_n(QspiCSn), .fl_sck(fl_sck), .fl_mosi(QspiDB[0]), .fl_miso(QspiDB[1])
);
@@ -367,6 +380,7 @@ module basys3_terminal #(
// ------------------------------------------------------------- terminal
wire [11:0] t_addr;
wire alt_scr;
wire t_we;
wire [17:0] t_wdata, t_rdata;
wire [4:0] cur_row, stop, sbot, sm_off;
@@ -385,22 +399,23 @@ module basys3_terminal #(
.stop(stop), .sbot(sbot), .sm_on(sm_on), .sm_dir(sm_dir), .sm_off(sm_off),
.resp_byte(resp_byte), .resp_wr(resp_wr), .resp_full(rq_full),
.cursor_app(cursor_app), .keypad_app(keypad_app), .lnm(lnm),
.dec_leds(dec_leds), .bell(bell)
.dec_leds(dec_leds), .bell(bell), .alt_scr(alt_scr)
);
wire [11:0] v_addr;
wire [17:0] v_data;
// the alternate screen (the Z-80 monitor uses it) has no graphics layer
char_ram screen (
.clk(clk),
.a_addr(t_addr), .a_we(t_we), .a_wdata(t_wdata), .a_rdata(t_rdata),
.b_addr(v_addr), .b_rdata(v_data)
.a_addr({alt_scr, t_addr}), .a_we(t_we), .a_wdata(t_wdata), .a_rdata(t_rdata),
.b_addr({alt_scr, v_addr}), .b_rdata(v_data)
);
text_display #(.FONT_FILE(FONT_FILE)) video (
.clk(clk), .rst(rst), .cur_row(cur_row), .cur_col(cur_col), .cur_vis(cur_vis),
.stop(stop), .sbot(sbot), .sm_on(sm_on), .sm_dir(sm_dir), .sm_off(sm_off),
.ram_addr(v_addr), .ram_data(v_data), .gfx_addr(gfx_addr), .gfx_pix(gfx_pix),
.ram_addr(v_addr), .ram_data(v_data), .gfx_addr(gfx_addr), .gfx_pix(alt_scr ? 4'd0 : gfx_pix),
.red(vgaRed), .green(vgaGreen), .blue(vgaBlue), .hsync(Hsync), .vsync(Vsync),
.frame_tick(frame_tick)
);
+4 -1
View File
@@ -76,7 +76,8 @@ module ps2_keymap (
input keypad_app, // DECKPAM
output reg [7:0] ascii,
output reg ascii_valid,
output reg caps
output reg caps,
output reg cad // Ctrl-Alt-Del was pressed (one clock)
);
localparam [7:0] ESC = 8'h1B;
@@ -189,6 +190,7 @@ module ps2_keymap (
always @(posedge clk) begin
ascii_valid <= 1'b0;
cad <= 1'b0;
if (rst) begin
{e0, f0, lsh, rsh, lct, rct, alt, caps} <= 8'd0;
skip <= 3'd0;
@@ -212,6 +214,7 @@ module ps2_keymap (
else begin
e0 <= 1'b0;
f0 <= 1'b0;
cad <= !f0 && code == 8'h71 && (lct || rct) && alt; // Delete or keypad .
if (code == 8'h12 && !e0) lsh <= !f0; // E0 12 / E0 59 are fake shifts
else if (code == 8'h59 && !e0) rsh <= !f0;
else if (code == 8'h14) begin
+5 -1
View File
@@ -11,6 +11,7 @@
// CSI A B C D E F G H I J K L M P S T X Z @ ` a d e f g h l m n q r s u c
// CSI ? J K h l n CSI > c CSI ! p
// Modes: IRM(4) LNM(20) DECCKM(?1) DECSCLM(?4) DECOM(?6) DECAWM(?7) DECTCEM(?25)
// alternate screen ?47 (parameters stop at 255, so not ?1047/?1049)
// SGR 0 1 4 5 7 8 21-28 30-49 90-107, 38/48;5;n and 38/48;2;r;g;b (approximated)
// DECLL (CSI n q) drives four LEDs.
// OSC, DCS, APC, PM and SOS strings (e.g. titles, OSC 3008) are discarded.
@@ -52,7 +53,8 @@ module term_ctrl (
output reg keypad_app,
output reg lnm,
output reg [3:0] dec_leds,
output reg bell
output reg bell,
output reg alt_scr // showing and writing the alternate screen
);
localparam [4:0] LAST_ROW = 5'd29;
localparam [6:0] LAST_COL = 7'd79;
@@ -319,6 +321,7 @@ module term_ctrl (
{g0, g1} <= 4'b0;
awm <= 1'b1;
cur_vis <= 1'b1;
alt_scr <= 1'b0;
stop <= 5'd0;
sbot <= LAST_ROW;
{sv_row, sv_col, sv_pend, sv_om, sv_gl, sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal} <= 0;
@@ -415,6 +418,7 @@ module term_ctrl (
if (v == 6) begin om <= on; row <= on ? stop : 5'd0; col <= 7'd0; pend <= 1'b0; end
if (v == 7) awm <= on;
if (v == 25) cur_vis <= on;
if (v == 47) alt_scr <= on;
end else begin
if (v == 4) irm <= on;
if (v == 20) lnm <= on;
+6 -5
View File
@@ -115,20 +115,21 @@ module text_display #(
end
endmodule
// 2400-cell character buffer: read/write port for the terminal engine,
// Two 2400-cell character buffers (the normal and the alternate screen; the
// top address bit picks one): read/write port for the terminal engine,
// read-only port for video.
module char_ram (
input clk,
input [11:0] a_addr,
input [12:0] a_addr,
input a_we,
input [17:0] a_wdata,
output reg [17:0] a_rdata,
input [11:0] b_addr,
input [12:0] b_addr,
output reg [17:0] b_rdata
);
(* ram_style = "block" *) reg [17:0] mem [0:4095];
(* ram_style = "block" *) reg [17:0] mem [0:8191];
integer i;
initial for (i = 0; i < 4096; i = i + 1) mem[i] = {10'h00A, 8'h20};
initial for (i = 0; i < 8192; i = i + 1) mem[i] = {10'h00A, 8'h20};
always @(posedge clk) begin
if (a_we) mem[a_addr] <= a_wdata;
+23 -5
View File
@@ -11,8 +11,11 @@
//
// The full documented instruction set is implemented, plus the undocumented
// IXH/IXL/IYH/IYL forms, SLL and the DDCB register copies. Block instructions
// loop internally (LDIR moves a byte every 3 clocks). There are no interrupts:
// EI/DI/IM only set their registers, and HALT stops the CPU until reset.
// loop internally (LDIR moves a byte every 3 clocks). The only interrupt is a
// non-maskable one for the monitor: a pulse on `nmi` is taken at the next
// instruction start from that clock on (or in HALT), `nmi_now` at this one. It
// pushes the address of the instruction not yet run, jumps to 0066h and clears
// IFF1 (RETN copies IFF2 back). EI/DI/IM only set their registers.
module z80_cpu (
input clk,
input rst,
@@ -28,6 +31,8 @@ module z80_cpu (
output reg [7:0] io_wd,
input [7:0] io_din,
output halted,
input nmi, // pulse: take at the next instruction start
input nmi_now, // level: take at this instruction start
// debug: ins_start is high on the first clock of each instruction
output ins_start,
output [15:0] dbg_pc,
@@ -70,12 +75,14 @@ module z80_cpu (
reg take;
reg [15:0] ea, tmp;
reg [7:0] tlo;
reg nmi_p; // NMI waiting
wire [15:0] BC = {rB, rC}, DE = {rD, rE}, HL = {rH, rL};
assign halted = st == S_HALT;
assign ins_start = st == S_D && xm == 2'd0;
assign dbg_pc = PC - 1'b1;
wire take_nmi = xm == 2'd0 && (nmi_p || nmi || nmi_now);
assign dbg_af = {rA, rF}; assign dbg_bc = BC; assign dbg_de = DE;
assign dbg_hl = HL; assign dbg_ix = IX; assign dbg_iy = IY; assign dbg_sp = SP;
@@ -317,6 +324,7 @@ module z80_cpu (
io_wr <= 1'b0;
pd <= 1'b0;
if (pd) nxt <= din;
if (nmi) nmi_p <= 1'b1;
if (rst) begin
st <= S_F; ma <= 16'h0000; PC <= 16'h0000;
@@ -325,12 +333,17 @@ module z80_cpu (
{aA, aF, aB, aC, aD, aE, aH, aL} <= 64'd0;
IX <= 16'hFFFF; IY <= 16'hFFFF;
rI <= 8'h00; rR <= 8'h00; iff1 <= 1'b0; iff2 <= 1'b0; im <= 2'd0;
xm <= 2'd0; un <= 1'b0; rsm <= 1'b0; nbad <= 1'b0;
xm <= 2'd0; un <= 1'b0; rsm <= 1'b0; nbad <= 1'b0; nmi_p <= 1'b0;
end else case (st)
S_F: next_stream;
// ------------------------------------------------------ main opcodes
S_D: begin
S_D: if (take_nmi) begin // NMI: like RST 66h, before this opcode
tmp <= dbg_pc; ea <= 16'h0066;
SP <= SP - 1'b1; wr(SP - 1'b1, dbg_pc[15:8]);
iff2 <= iff1; iff1 <= 1'b0; nmi_p <= 1'b0;
ic <= I_CALL; st <= S_W1;
end else begin
op = un ? nxt : din;
x = op[7:6]; y = op[5:3]; z = op[2:0]; p = op[5:4]; q = op[3];
rR <= {rR[7], rR[6:0] + 7'd1};
@@ -716,7 +729,12 @@ module z80_cpu (
st <= S_W;
end
S_HALT: ;
S_HALT: if (nmi_p || nmi) begin // NMI wakes HALT, returning after it
tmp <= dbg_pc; ea <= 16'h0066;
SP <= SP - 1'b1; wr(SP - 1'b1, dbg_pc[15:8]);
iff2 <= iff1; iff1 <= 1'b0; nmi_p <= 1'b0;
ic <= I_CALL; st <= S_W1;
end
default: st <= S_F;
endcase
end
+97 -5
View File
@@ -13,11 +13,29 @@
// 33 out plot colour at (X,Y), then X+1 in colour at (X,Y)
// 34 out fill the whole graphics screen in bit 0 = fill busy
// 40 SPI flash control, 41 SPI flash data (see flash_spi.v)
// 60 out bit 0 = monitor bank in; bit 1 (with bit 0 = 0) = stop again after
// one instruction of the program
// in why the monitor was entered: 1 Pause key, 2 port 61, 3 step,
// 4 breakpoint 0, 5 breakpoint 1
// 61 out enter the monitor (NMI)
// 62 out debug address low 63 out debug address high
// 64 in main-memory byte at the debug address, then address + 1
// 65 out breakpoint 0 low 66 high 68 out breakpoint 1 low 69 high
// 67 out bit 0 = breakpoint 0 on, bit 1 = breakpoint 1 on
//
// The monitor: an NMI (Pause key, port 61, a breakpoint or the step counter)
// pushes the program's PC and jumps to 0066h, where both BASIC and the monitor
// ROM hold the same few bytes: they switch the monitor bank in, which replaces
// 0000-7FFF with the monitor's ROM (0000-6FFF) and RAM (7000-7FFF), and leave
// through 0070h, which switches it out again and returns. No NMI is taken while
// the bank is in, or between the request and the bank coming in.
//
// Graphics are 320x240 in 16 colours (0 = transparent), shown over the text.
// Writes below ROM_TOP are ignored so a crashed program cannot damage BASIC.
module z80_system #(
parameter MEM_FILE = "basic.mem",
parameter MON_FILE = "monitor.mem",
parameter MON_RAM = 16'h7000,
parameter CPU_HZ = 100_000_000,
parameter ROM_TOP = 16'h8000
) (
@@ -33,6 +51,7 @@ module z80_system #(
input tx_ack,
output reg [15:0] seg_val,
output halted,
input brk_key, // pulse: the Pause key, enter the monitor
// graphics layer read port, on the video clock
input vclk,
input [16:0] gfx_vaddr,
@@ -43,25 +62,40 @@ module z80_system #(
output fl_mosi,
input fl_miso
);
wire [15:0] ma, io_addr;
wire we, io_rd, io_wr;
wire [15:0] ma, io_addr, pc_dbg;
wire we, io_rd, io_wr, ins_start;
wire nmi, nmi_now;
wire [7:0] wd, io_wd;
reg [7:0] din, io_din;
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(), .dbg_pc(),
.halted(halted), .nmi(nmi), .nmi_now(nmi_now), .ins_start(ins_start), .dbg_pc(pc_dbg),
.dbg_af(), .dbg_bc(), .dbg_de(), .dbg_hl(), .dbg_ix(), .dbg_iy(), .dbg_sp()
);
reg [7:0] ram [0:65535];
initial $readmemh(MEM_FILE, ram);
reg [7:0] ram_q, mon_q, dbg_q;
reg [15:0] dbg_a;
reg mon, sel_mon;
always @(posedge clk) begin
if (we && ma >= ROM_TOP) ram[ma] <= wd;
din <= ram[ma];
if (we && ma >= ROM_TOP && !(mon && !ma[15])) ram[ma] <= wd;
ram_q <= ram[ma];
dbg_q <= ram[dbg_a];
end
// the monitor bank, 0000-7FFF while mon is set
(* ram_style = "block" *) reg [7:0] mrom [0:32767];
initial $readmemh(MON_FILE, mrom);
always @(posedge clk) begin
if (we && mon && !ma[15] && ma >= MON_RAM) mrom[ma[14:0]] <= wd;
mon_q <= mrom[ma[14:0]];
sel_mon <= mon && !ma[15];
end
always @* din = sel_mon ? mon_q : ram_q;
// ------------------------------------------------------------- console
(* ASYNC_REG = "TRUE" *) reg [1:0] rx_req_s, tx_ack_s;
always @(posedge clk) begin
@@ -132,6 +166,62 @@ module z80_system #(
end else
ms_div <= ms_div + 1'b1;
// ------------------------------------------------------------- monitor
reg [15:0] bp0, bp1; // breakpoint addresses
reg [1:0] bp_on;
reg [2:0] why;
reg nmi_busy, step_arm, nmi_r, io_rd_d;
reg [1:0] step_n;
wire can_nmi = !mon && !nmi_busy;
wire hit0 = bp_on[0] && pc_dbg == bp0;
wire hit1 = bp_on[1] && pc_dbg == bp1;
assign nmi_now = can_nmi && ins_start && (hit0 || hit1);
assign nmi = nmi_r;
always @(posedge clk) begin
nmi_r <= 1'b0;
io_rd_d <= io_rd && io_addr[7:0] == 8'h64;
if (io_rd_d) dbg_a <= dbg_a + 1'b1;
if (rst) begin
mon <= 1'b0; nmi_busy <= 1'b0; step_arm <= 1'b0; bp_on <= 2'b00; why <= 3'd0;
end else begin
if (io_wr) case (io_addr[7:0])
8'h60: begin
mon <= io_wd[0];
if (io_wd[0]) nmi_busy <= 1'b0;
step_arm <= !io_wd[0] && io_wd[1];
step_n <= ins_start ? 2'd1 : 2'd0; // POP AF may start on this clock
end
8'h62: dbg_a[7:0] <= io_wd;
8'h63: dbg_a[15:8] <= io_wd;
8'h65: bp0[7:0] <= io_wd;
8'h66: bp0[15:8] <= io_wd;
8'h68: bp1[7:0] <= io_wd;
8'h69: bp1[15:8] <= io_wd;
8'h67: bp_on <= io_wd[1:0];
default: ;
endcase
// step: the exit path runs POP AF and RETN, then one instruction
// of the program; ask for the NMI as that one starts
if (step_arm && ins_start) begin
step_n <= step_n + 1'b1;
if (step_n == 2'd2) begin
step_arm <= 1'b0;
nmi_r <= 1'b1; nmi_busy <= 1'b1; why <= 3'd3;
end
end
if (can_nmi && !nmi_r) begin
if (nmi_now) begin
nmi_busy <= 1'b1; why <= hit0 ? 3'd4 : 3'd5;
end else if (brk_key) begin
nmi_r <= 1'b1; nmi_busy <= 1'b1; why <= 3'd1;
end else if (io_wr && io_addr[7:0] == 8'h61) begin
nmi_r <= 1'b1; nmi_busy <= 1'b1; why <= 3'd2;
end
end
end
end
always @* case (io_addr[7:0])
8'h00: io_din = {6'd0, tx_ready, rx_avail};
8'h01: io_din = rx_data;
@@ -143,6 +233,8 @@ module z80_system #(
8'h34: io_din = {7'd0, fill_busy};
8'h40: io_din = fl_ctl_q;
8'h41: io_din = fl_dat_q;
8'h60: io_din = {5'd0, why};
8'h64: io_din = dbg_q;
default: io_din = 8'hFF;
endcase