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>
240 lines
10 KiB
Verilog
240 lines
10 KiB
Verilog
`timescale 1ns / 1ps
|
|
|
|
// USB keyboard input. The Basys 3's PIC24 USB host turns a USB keyboard into a
|
|
// PS/2 device (scan code set 2) on PS2Clk/PS2Data; this decodes that to ASCII.
|
|
|
|
module ps2_rx (
|
|
input clk,
|
|
input rst,
|
|
input ps2_clk,
|
|
input ps2_data,
|
|
output reg [7:0] code,
|
|
output reg valid
|
|
);
|
|
reg [2:0] cs = 3'b111, ds = 3'b111;
|
|
reg [5:0] fcnt;
|
|
reg fclk = 1'b1, fclk_d = 1'b1;
|
|
reg [10:0] sh;
|
|
reg [3:0] n;
|
|
reg [17:0] idle;
|
|
|
|
always @(posedge clk) begin
|
|
cs <= {cs[1:0], ps2_clk};
|
|
ds <= {ds[1:0], ps2_data};
|
|
// the clock only changes after 64 stable samples, which filters glitches
|
|
if (cs[2] == fclk)
|
|
fcnt <= 6'd0;
|
|
else begin
|
|
fcnt <= fcnt + 1'b1;
|
|
if (&fcnt) fclk <= cs[2];
|
|
end
|
|
fclk_d <= fclk;
|
|
end
|
|
|
|
wire fall = fclk_d & ~fclk;
|
|
wire [10:0] frame = {ds[2], sh[10:1]};
|
|
|
|
always @(posedge clk) begin
|
|
valid <= 1'b0;
|
|
if (rst) begin
|
|
n <= 4'd0;
|
|
idle <= 18'd0;
|
|
end else if (fall) begin
|
|
sh <= frame;
|
|
idle <= 18'd0;
|
|
if (n == 4'd10) begin
|
|
n <= 4'd0;
|
|
// start 0, stop 1, odd parity over data + parity bit
|
|
if (!frame[0] && frame[10] && ^frame[9:1]) begin
|
|
code <= frame[8:1];
|
|
valid <= 1'b1;
|
|
end
|
|
end else
|
|
n <= n + 1'b1;
|
|
end else if (n != 0) begin
|
|
idle <= idle + 1'b1;
|
|
if (&idle) n <= 4'd0; // ~2.6 ms without a clock: resync
|
|
end
|
|
end
|
|
endmodule
|
|
|
|
// Turns scan codes into ASCII bytes and VT220 key sequences:
|
|
// arrows ESC [ A..D, or ESC O A..D in cursor-key application mode (DECCKM)
|
|
// Home End ESC [ 1 ~ ESC [ 4 ~ (VT220 Find / Select)
|
|
// Ins Del PgUp PgDn ESC [ 2 ~ 3 ~ 5 ~ 6 ~
|
|
// F1-F4 ESC O P..S F5-F12 ESC [ 15 17 18 19 20 21 23 24 ~
|
|
// keypad digits, or ESC O p..y etc. in keypad application mode (DECKPAM)
|
|
// Alt+key ESC prefix
|
|
module ps2_keymap (
|
|
input clk,
|
|
input rst,
|
|
input [7:0] code,
|
|
input code_valid,
|
|
input bs_del, // Backspace sends DEL (7F) instead of BS (08)
|
|
input enter_crlf, // Enter sends CR LF instead of CR
|
|
input cursor_app, // DECCKM
|
|
input keypad_app, // DECKPAM
|
|
output reg [7:0] ascii,
|
|
output reg ascii_valid,
|
|
output reg caps,
|
|
output reg cad // Ctrl-Alt-Del was pressed (one clock)
|
|
);
|
|
localparam [7:0] ESC = 8'h1B;
|
|
|
|
reg e0, f0;
|
|
reg [2:0] skip;
|
|
reg lsh, rsh, lct, rct, alt;
|
|
reg [47:0] sq;
|
|
reg [2:0] cnt;
|
|
|
|
// {unshifted, shifted}
|
|
function [15:0] keymap(input [7:0] c);
|
|
case (c)
|
|
8'h0E: keymap = "`~"; 8'h16: keymap = "1!"; 8'h1E: keymap = "2@";
|
|
8'h26: keymap = "3#"; 8'h25: keymap = "4$"; 8'h2E: keymap = "5%";
|
|
8'h36: keymap = "6^"; 8'h3D: keymap = "7&"; 8'h3E: keymap = "8*";
|
|
8'h46: keymap = "9("; 8'h45: keymap = "0)"; 8'h4E: keymap = "-_";
|
|
8'h55: keymap = "=+"; 8'h0D: keymap = {8'h09, 8'h09};
|
|
8'h15: keymap = "qQ"; 8'h1D: keymap = "wW"; 8'h24: keymap = "eE";
|
|
8'h2D: keymap = "rR"; 8'h2C: keymap = "tT"; 8'h35: keymap = "yY";
|
|
8'h3C: keymap = "uU"; 8'h43: keymap = "iI"; 8'h44: keymap = "oO";
|
|
8'h4D: keymap = "pP"; 8'h54: keymap = "[{"; 8'h5B: keymap = "]}";
|
|
8'h5D: keymap = "\\|";
|
|
8'h1C: keymap = "aA"; 8'h1B: keymap = "sS"; 8'h23: keymap = "dD";
|
|
8'h2B: keymap = "fF"; 8'h34: keymap = "gG"; 8'h33: keymap = "hH";
|
|
8'h3B: keymap = "jJ"; 8'h42: keymap = "kK"; 8'h4B: keymap = "lL";
|
|
8'h4C: keymap = ";:"; 8'h52: keymap = "'\"";
|
|
8'h1A: keymap = "zZ"; 8'h22: keymap = "xX"; 8'h21: keymap = "cC";
|
|
8'h2A: keymap = "vV"; 8'h32: keymap = "bB"; 8'h31: keymap = "nN";
|
|
8'h3A: keymap = "mM"; 8'h41: keymap = ",<"; 8'h49: keymap = ".>";
|
|
8'h4A: keymap = "/?"; 8'h29: keymap = " "; 8'h76: keymap = {ESC, ESC};
|
|
// keypad, as if Num Lock were on
|
|
8'h70: keymap = "00"; 8'h69: keymap = "11"; 8'h72: keymap = "22";
|
|
8'h7A: keymap = "33"; 8'h6B: keymap = "44"; 8'h73: keymap = "55";
|
|
8'h74: keymap = "66"; 8'h6C: keymap = "77"; 8'h75: keymap = "88";
|
|
8'h7D: keymap = "99"; 8'h71: keymap = ".."; 8'h7C: keymap = "**";
|
|
8'h7B: keymap = "--"; 8'h79: keymap = "++";
|
|
default: keymap = 16'h0000;
|
|
endcase
|
|
endfunction
|
|
|
|
// final byte of ESC O x for the keypad in application mode (0 = not a keypad key)
|
|
function [7:0] kp_app(input [7:0] c);
|
|
case (c)
|
|
8'h70: kp_app = "p"; 8'h69: kp_app = "q"; 8'h72: kp_app = "r";
|
|
8'h7A: kp_app = "s"; 8'h6B: kp_app = "t"; 8'h73: kp_app = "u";
|
|
8'h74: kp_app = "v"; 8'h6C: kp_app = "w"; 8'h75: kp_app = "x";
|
|
8'h7D: kp_app = "y"; 8'h71: kp_app = "n"; 8'h7B: kp_app = "m";
|
|
8'h79: kp_app = "l"; 8'h7C: kp_app = "j";
|
|
default: kp_app = 8'h00;
|
|
endcase
|
|
endfunction
|
|
|
|
wire shift = lsh | rsh;
|
|
wire ctrl = lct | rct;
|
|
wire [15:0] km = keymap(code);
|
|
wire letter = km[15:8] >= "a" && km[15:8] <= "z";
|
|
wire [7:0] ch = (letter ? shift ^ caps : shift) ? km[7:0] : km[15:8];
|
|
wire [7:0] ckey = cursor_app ? "O" : "[";
|
|
wire [7:0] kpa = kp_app(code);
|
|
|
|
// bytes a key press produces, first byte in k_seq[47:40]
|
|
reg [2:0] k_len;
|
|
reg [47:0] k_seq;
|
|
always @* begin
|
|
k_len = 3'd0;
|
|
k_seq = 48'd0;
|
|
if (e0) case (code)
|
|
8'h75: {k_len, k_seq} = {3'd3, ESC, ckey, "A", 24'd0}; // up
|
|
8'h72: {k_len, k_seq} = {3'd3, ESC, ckey, "B", 24'd0}; // down
|
|
8'h74: {k_len, k_seq} = {3'd3, ESC, ckey, "C", 24'd0}; // right
|
|
8'h6B: {k_len, k_seq} = {3'd3, ESC, ckey, "D", 24'd0}; // left
|
|
8'h6C: {k_len, k_seq} = {3'd4, ESC, "[1~", 16'd0}; // home (Find)
|
|
8'h70: {k_len, k_seq} = {3'd4, ESC, "[2~", 16'd0}; // insert
|
|
8'h71: {k_len, k_seq} = {3'd4, ESC, "[3~", 16'd0}; // delete (Remove)
|
|
8'h69: {k_len, k_seq} = {3'd4, ESC, "[4~", 16'd0}; // end (Select)
|
|
8'h7D: {k_len, k_seq} = {3'd4, ESC, "[5~", 16'd0}; // page up
|
|
8'h7A: {k_len, k_seq} = {3'd4, ESC, "[6~", 16'd0}; // page down
|
|
8'h4A: {k_len, k_seq} = keypad_app ? {3'd3, ESC, "Oo", 24'd0} : {3'd1, "/", 40'd0};
|
|
8'h5A: {k_len, k_seq} = keypad_app ? {3'd3, ESC, "OM", 24'd0} :
|
|
enter_crlf ? {3'd2, 8'h0D, 8'h0A, 32'd0} : {3'd1, 8'h0D, 40'd0};
|
|
default: ;
|
|
endcase
|
|
else case (code)
|
|
8'h66: {k_len, k_seq} = {3'd1, bs_del ? 8'h7F : 8'h08, 40'd0};
|
|
8'h5A: {k_len, k_seq} = enter_crlf ? {3'd2, 8'h0D, 8'h0A, 32'd0} : {3'd1, 8'h0D, 40'd0};
|
|
8'h05: {k_len, k_seq} = {3'd3, ESC, "OP", 24'd0}; // F1
|
|
8'h06: {k_len, k_seq} = {3'd3, ESC, "OQ", 24'd0}; // F2
|
|
8'h04: {k_len, k_seq} = {3'd3, ESC, "OR", 24'd0}; // F3
|
|
8'h0C: {k_len, k_seq} = {3'd3, ESC, "OS", 24'd0}; // F4
|
|
8'h03: {k_len, k_seq} = {3'd5, ESC, "[15~", 8'd0}; // F5
|
|
8'h0B: {k_len, k_seq} = {3'd5, ESC, "[17~", 8'd0}; // F6
|
|
8'h83: {k_len, k_seq} = {3'd5, ESC, "[18~", 8'd0}; // F7
|
|
8'h0A: {k_len, k_seq} = {3'd5, ESC, "[19~", 8'd0}; // F8
|
|
8'h01: {k_len, k_seq} = {3'd5, ESC, "[20~", 8'd0}; // F9
|
|
8'h09: {k_len, k_seq} = {3'd5, ESC, "[21~", 8'd0}; // F10
|
|
8'h78: {k_len, k_seq} = {3'd5, ESC, "[23~", 8'd0}; // F11
|
|
8'h07: {k_len, k_seq} = {3'd5, ESC, "[24~", 8'd0}; // F12
|
|
default:
|
|
if (keypad_app && kpa != 0)
|
|
{k_len, k_seq} = {3'd3, ESC, "O", kpa, 24'd0};
|
|
else if (ch != 0) begin
|
|
k_len = 3'd1;
|
|
if (!ctrl) k_seq[47:40] = ch;
|
|
else if (ch == " ") k_seq[47:40] = 8'h00;
|
|
else if (ch >= 8'h40) k_seq[47:40] = ch & 8'h1F; // ^A..^Z, ^[ ^\ ^] ^^ ^_
|
|
else k_seq[47:40] = ch;
|
|
end
|
|
endcase
|
|
end
|
|
|
|
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;
|
|
cnt <= 3'd0;
|
|
end else begin
|
|
if (cnt != 0) begin
|
|
ascii <= sq[47:40];
|
|
ascii_valid <= 1'b1;
|
|
sq <= sq << 8;
|
|
cnt <= cnt - 1'b1;
|
|
end
|
|
if (code_valid) begin
|
|
if (skip != 0)
|
|
skip <= skip - 1'b1;
|
|
else if (code == 8'hE1)
|
|
skip <= 3'd7; // Pause: E1 14 77 E1 F0 14 F0 77
|
|
else if (code == 8'hE0)
|
|
e0 <= 1'b1;
|
|
else if (code == 8'hF0)
|
|
f0 <= 1'b1;
|
|
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
|
|
if (e0) rct <= !f0; else lct <= !f0;
|
|
end else if (code == 8'h11)
|
|
alt <= !f0;
|
|
else if (code == 8'h58 && !e0) begin
|
|
if (!f0) caps <= !caps;
|
|
end else if (!f0 && k_len != 0) begin
|
|
if (alt && k_len != 3'd6) begin
|
|
sq <= {ESC, k_seq[47:8]};
|
|
cnt <= k_len + 1'b1;
|
|
end else begin
|
|
sq <= k_seq;
|
|
cnt <= k_len;
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
endmodule
|