From 6e54d75c56c8eee14fe4660fcb9e67b7d8f3fb5a Mon Sep 17 00:00:00 2001 From: brad Arant Date: Tue, 17 Nov 2026 11:18:36 -0800 Subject: [PATCH] VT220 emulation with ANSI colour, underline and smooth scrolling - Terminal engine rewritten for VT220: scroll regions, IL/DL/ICH/DCH/ECH, SU/SD, origin mode, insert mode, tab stops, DEC special graphics with G0/G1 and SO/SI, DECSC/DECRC, DECSTR, RIS, DECALN, DECLL LEDs - Replies to DA, secondary DA, DSR and CPR over the UART - 16-colour SGR (256-colour and RGB approximated), bold, underline, blink, reverse and conceal; cells are now 18 bits - Smooth scrolling (SW8 or DECSCLM): the region slides per frame and rows are moved during vertical blanking; XON/XOFF holds the host back - Keyboard: DECCKM, keypad application mode, F5-F12, VT220 Home/End, Alt as ESC prefix - basys3 terminfo entry (vt220 + 16 colours, 30 lines) - Flash script reboots the FPGA from flash after programming - Testbench covers replies, modes, attributes, editing and smooth scroll Co-Authored-By: Claude Opus 5.5 --- README.md | 146 ++++++-- rtl/basys3_terminal.v | 162 ++++++--- rtl/fifo.v | 4 +- rtl/ps2_keyboard.v | 122 ++++--- rtl/term_ctrl.v | 784 ++++++++++++++++++++++++++++++++++-------- rtl/text_display.v | 92 +++-- scripts/program.tcl | 4 +- scripts/sim.sh | 5 +- sim/tb_terminal.v | 184 +++++++--- terminfo/basys3.ti | 20 ++ 10 files changed, 1173 insertions(+), 350 deletions(-) create mode 100644 terminfo/basys3.ti diff --git a/README.md b/README.md index d8ec810..7df6c50 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,10 @@ -# Basys 3 ASCII Terminal +# Basys 3 VT220 Terminal -Turns a Digilent Basys 3 (Artix-7 XC7A35T) into a stand-alone serial terminal: +Turns a Digilent Basys 3 (Artix-7 XC7A35T) into a stand-alone VT220-compatible +serial terminal with ANSI colour: -- **Screen**: VGA port, 640x480 @ 60 Hz, 80 columns x 30 rows, 8x16 CP437 font - (box-drawing characters included), green/amber/white/cyan. +- **Screen**: VGA port, 640x480 @ 60 Hz, 80 columns x 30 rows, 8x16 CP437 font, + 16 colours, underline, blink, reverse video, optional smooth scrolling. - **Keyboard**: a USB keyboard in the **USB HID host** port (J2). The board's PIC24 presents it to the FPGA as a PS/2 keyboard. - **Serial line**: the micro-USB **PROG/UART** port (the FTDI chip), 8N1. Bytes @@ -16,12 +17,45 @@ the UART; `ttyUSB0` is JTAG). For example: picocom -b 115200 /dev/ttyUSB1 ``` -To get a Linux login on the terminal, run a getty on that port: +## Linux login with colour + +Install the `basys3` terminfo entry once (as your user, or with `sudo` and no +`-o` for every user): ```bash -sudo systemctl start serial-getty@ttyUSB1.service +tic -x -o ~/.terminfo terminfo/basys3.ti ``` +Run a getty on the port at a fixed speed with that terminal type: + +```bash +sudo mkdir -p /etc/systemd/system/serial-getty@ttyUSB1.service.d +``` + +```bash +printf '[Service]\nExecStart=\nExecStart=-/usr/sbin/agetty -L 115200 %%I basys3\n' | sudo tee /etc/systemd/system/serial-getty@ttyUSB1.service.d/override.conf +``` + +```bash +sudo systemctl daemon-reload && sudo systemctl restart serial-getty@ttyUSB1.service +``` + +The getty sets `TERM=basys3` for the login session. Programs find the entry in +`~/.terminfo` once you're logged in. Without the entry, `TERM=vt220` works but +has no colour. + +Without the override, Ubuntu's getty keeps whatever speed the port had when it +appeared, which is usually 9600. Then set SW1:0 to `01` (9600). + +To start the getty whenever the board is plugged in, add a udev rule (use your +board's serial number from `udevadm info -q property -n /dev/ttyUSB1`): + +```bash +echo 'ACTION=="add", SUBSYSTEM=="tty", ENV{ID_SERIAL_SHORT}=="210183BB7B5B", ENV{ID_USB_INTERFACE_NUM}=="01", TAG+="systemd", ENV{SYSTEMD_WANTS}+="serial-getty@%k.service"' | sudo tee /etc/udev/rules.d/99-basys3-getty.rules +``` + +Set SW6 up so Backspace sends DEL, which the `basys3` entry expects. + ## Controls | Control | Function | @@ -29,35 +63,72 @@ sudo systemctl start serial-getty@ttyUSB1.service | SW1:0 | Baud: `00` 115200, `01` 9600, `10` 19200, `11` 57600 | | SW2 | Local echo | | SW3 | Received LF also does CR (for hosts that send bare `\n`) | -| SW5:4 | Colour: `00` green, `01` amber, `10` white, `11` cyan | +| SW5:4 | Default text colour: `00` green, `01` yellow, `10` white, `11` cyan | | SW6 | Backspace sends DEL (`7F`) instead of BS (`08`) | | SW7 | Enter sends CR LF instead of CR | +| SW8 | Smooth scrolling (the host can also turn it on with `ESC[?4h`) | +| SW9 | Fast smooth scrolling: 15 lines/s instead of 7.5 | | BTNC | Reset (clears screen, shows banner) | | BTNU | Clear screen | | LED7:0 | Last byte received | +| LED11:8 | VT220 keyboard LEDs L4..L1 (`ESC[n q`) | | LED12 / 13 / 14 / 15 | Bell / Caps Lock / UART RX activity / keyboard activity | | 7-segment | Left: last keyboard scan code. Right: last byte sent. | -For a Linux getty, set SW6 on (Backspace = DEL). - ## Terminal features -- Control characters: CR, LF, VT, FF (clear screen), BS, TAB (every 8 columns), - BEL (LED12). -- Auto-wrap at column 80 (deferred, like a VT100), and hardware scrolling. -- VT100/ANSI subset: - - Cursor movement `ESC[nA/B/C/D`, `ESC[r;cH`, `ESC[r;cf`, `ESC[nd`, `ESC[nG` - - Erasing with `ESC[nJ` and `ESC[nK` - - Reverse video with `ESC[7m`, `ESC[27m` and `ESC[0m` - - Show/hide cursor with `ESC[?25h` and `ESC[?25l` - - `ESC c`, `ESC D`, `ESC E`, `ESC M`, `ESC 7`, `ESC 8` -- Keyboard: Shift, Caps Lock, Ctrl (`^A`..`^Z`, `^[` etc.). - - The arrow keys, Home, End, Insert, Delete, PgUp and PgDn send xterm sequences. - - F1 to F4 send `ESC O P` to `ESC O S`. - - The keypad always types digits. +- **Controls**: BEL, BS, HT, LF/VT/FF, CR, SO/SI, CAN/SUB. Controls act even in + the middle of an escape sequence, as on a real VT220. +- **Cursor**: + - Movement: `CUU CUD CUF CUB CNL CPL CHA HPA VPA CUP HVP` + - Position save and restore: `DECSC`/`DECRC` (`ESC 7`/`ESC 8`), also `CSI s`/`CSI u` + - `IND`, `NEL` and `RI` +- **Editing**: + - Insert and delete characters: `ICH DCH ECH` + - Insert and delete lines: `IL DL` + - Erase in display and line: `ED EL`, including `DECSED`/`DECSEL` + - Scroll up and down: `SU SD` + - Insert mode (`IRM`) +- **Scroll regions** (`DECSTBM`), origin mode (`DECOM`), auto-wrap (`DECAWM`, + deferred wrap like a real VT). +- **Tabs**: settable stops with `HTS`, `TBC`, `CHT` and `CBT`. +- **Character sets**: G0/G1 designation with `ESC ( 0`, `ESC ) 0` and `ESC ( A`, + and SO/SI switching. DEC special graphics are drawn with the CP437 line-drawing + characters. +- **SGR attributes**: bold (bright colour), underline, blink, reverse, + concealed, and their resets. +- **SGR colours**: the 8 standard and 8 bright colours, foreground and + background. 256-colour (`38;5;n`) and RGB (`38;2;r;g;b`) codes are + approximated to the 16 colours. +- **Modes**: `DECCKM` (cursor keys), `DECKPAM`/`DECKPNM` (keypad), `LNM`, + `DECTCEM` (cursor on/off), `DECSCLM` (smooth scroll). +- **Replies to the host**: + - `DA` gets `ESC[?62;22c` (a VT220 with colour) + - Secondary `DA` gets `ESC[>1;10;0c` + - `DSR 5` and `DSR 6`/`CPR` are answered +- **Resets**: `RIS` (`ESC c`), `DECSTR` (`ESC[!p`), `DECALN` (`ESC # 8`). +- **Flow control**: the terminal sends XOFF when its 1 KB input buffer passes 256 + bytes and XON when it drains below 32. Smooth scrolling relies on this. Linux + serial ports honour it by default (`stty ixon`). -`TERM=vt100` is a good setting on the host. Colour SGR codes and scroll regions -are not implemented, so full-screen programs that need them may draw incorrectly. +### Keyboard + +| Key | Sends | +|---|---| +| Arrows | `ESC [ A`..`D`, or `ESC O A`..`D` when the host sets `DECCKM` | +| Home / End | `ESC [ 1 ~` / `ESC [ 4 ~` (VT220 Find / Select) | +| Insert / Delete / PgUp / PgDn | `ESC [ 2 ~` / `3 ~` / `5 ~` / `6 ~` | +| F1–F4 | `ESC O P` … `ESC O S` | +| F5–F12 | `ESC [ 15 ~`, `17 ~`, `18 ~`, `19 ~`, `20 ~`, `21 ~`, `23 ~`, `24 ~` | +| Keypad | Digits; in application mode (`ESC =`) `ESC O p`..`y`, `ESC O M` for Enter | +| Alt + key | `ESC` then the key | +| Ctrl + key | `^@`, `^A`..`^Z`, `^[ ^\ ^] ^^ ^_` | + +### Limits + +- Text from the host is drawn as CP437 bytes; there is no UTF-8 decoding. Use a + non-UTF-8 locale such as `LANG=C`, or ASCII-only output, for clean line drawing. +- No 132-column mode, double-width/height lines, soft fonts, or printer port. ## Build and program @@ -74,15 +145,23 @@ license, so use 2025.2 (`export VIVADO=~/vivado/2025.2/Vivado`). ``` ```bash -./scripts/build.sh flash # build and write QSPI flash; set JP1 to QSPI to boot from it +./scripts/build.sh flash # build, write QSPI flash and reboot from it (JP1 on QSPI) ``` +The flash script handles both Basys 3 flash chips (Spansion `s25fl032p` on +older boards, Macronix `mx25l3273f` on newer ones). + ## Simulation -`sim/tb_terminal.v` covers the whole design. It types keys over PS/2 and checks -the bytes sent on the UART. It sends text and escape sequences into the UART -and prints the character buffer. It also checks VGA sync timing and saves one -frame as `sim/work/frame.png`. +`sim/tb_terminal.v` covers the whole design: + +- It types keys over PS/2 and checks the bytes sent on the UART, including + `DECCKM` and keypad application mode. +- It checks the `DA`, secondary `DA` and `CPR` replies, and the `DECLL` LEDs. +- It draws colours, line graphics, insert/delete, scroll regions and a smooth + scroll, then checks the screen buffer. +- It checks VGA sync timing and saves two frames, `sim/work/frame_1.png` and + `sim/work/frame_2.png` (the second is taken mid-scroll). ```bash VIVADO=~/vivado/2025.2/Vivado ./scripts/sim.sh @@ -95,11 +174,12 @@ which Vivado needs on newer distros. | Path | Contents | |---|---| -| `rtl/basys3_terminal.v` | Top level: reset, switches, FIFOs, banner, LEDs | -| `rtl/term_ctrl.v` | Terminal engine (control codes, ANSI parser, scrolling) | -| `rtl/text_display.v` | VGA timing, character RAM, font lookup | -| `rtl/ps2_keyboard.v` | PS/2 receiver and scan code set 2 to ASCII | +| `rtl/basys3_terminal.v` | Top level: reset, switches, FIFOs, flow control, banner, LEDs | +| `rtl/term_ctrl.v` | VT220 engine (parser, modes, attributes, copy/fill engine, smooth scroll) | +| `rtl/text_display.v` | VGA timing, colour palette, character RAM, font lookup | +| `rtl/ps2_keyboard.v` | PS/2 receiver and scan code set 2 to ASCII / VT220 keys | | `rtl/uart.v` | UART receiver and transmitter | | `rtl/fifo.v`, `rtl/sevenseg.v` | Helpers | | `font/font.mem` | 256x16 font ROM, made by `tools/psf2mem.py` from the Linux console fonts | +| `terminfo/basys3.ti` | terminfo entry: VT220 plus 16 colours, 30 lines | | `constraints/basys3.xdc` | Pin assignments | diff --git a/rtl/basys3_terminal.v b/rtl/basys3_terminal.v index 96e73a8..3c8923e 100644 --- a/rtl/basys3_terminal.v +++ b/rtl/basys3_terminal.v @@ -1,19 +1,24 @@ `timescale 1ns / 1ps -// Basys 3 ASCII terminal. +// Basys 3 VT220 terminal with ANSI colour. // Keyboard (USB HID port) -> UART TX (USB-UART port) -// UART RX -> 80x30 text on VGA +// UART RX -> 80x30 colour text on VGA // // SW1:0 baud rate: 00 115200, 01 9600, 10 19200, 11 57600 (8N1) // SW2 local echo // SW3 received LF also does CR -// SW5:4 colour: 00 green, 01 amber, 10 white, 11 cyan +// SW5:4 default text colour: 00 green, 01 yellow, 10 white, 11 cyan // SW6 Backspace sends DEL (7F) instead of BS (08) // 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 -// LED7:0 last byte received LED12 bell LED13 Caps Lock -// LED14 UART RX activity LED15 keyboard activity +// 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) +// +// 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. module basys3_terminal #( parameter FONT_FILE = "font.mem", @@ -37,6 +42,8 @@ module basys3_terminal #( input PS2Clk, input PS2Data ); + localparam [7:0] XON = 8'h11, XOFF = 8'h13; + // ---------------------------------------------------------------- reset reg [1:0] btn_sync; reg [15:0] por = 16'd0; @@ -48,10 +55,10 @@ module basys3_terminal #( rst <= !por[15]; end - reg [7:0] sw_r; + reg [9:0] sw_r; reg [1:0] clr_sync; always @(posedge clk) begin - sw_r <= sw[7:0]; + sw_r <= sw[9:0]; clr_sync <= {clr_sync[0], btnU}; end @@ -64,12 +71,22 @@ module basys3_terminal #( 2'b11: baud_div <= CLK_HZ / 57600; endcase + reg [3:0] def_fg; + always @(posedge clk) + case (sw_r[5:4]) + 2'b00: def_fg <= 4'd10; // bright green + 2'b01: def_fg <= 4'd11; // yellow + 2'b10: def_fg <= 4'd7; // white + 2'b11: def_fg <= 4'd14; // bright cyan + endcase + // ------------------------------------------------------------- keyboard wire [7:0] scan; wire scan_valid; wire [7:0] key; wire key_valid; wire caps; + wire cursor_app, keypad_app, lnm; ps2_rx kbd_rx ( .clk(clk), .rst(rst), .ps2_clk(PS2Clk), .ps2_data(PS2Data), @@ -78,27 +95,69 @@ module basys3_terminal #( ps2_keymap kbd_map ( .clk(clk), .rst(rst), .code(scan), .code_valid(scan_valid), - .bs_del(sw_r[6]), .enter_crlf(sw_r[7]), + .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) ); // ------------------------------------------------------------- transmit - wire [7:0] kq; - wire kq_empty; + // 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. + wire [7:0] kq, rq; + wire kq_empty, rq_empty, rq_full; + wire [7:0] resp_byte; + wire resp_wr; wire tx_busy; - reg tx_start; + reg tx_start, kq_rd, rq_rd, from_resp; reg [7:0] tx_byte; + reg flow_pend; + reg [7:0] flow_byte; fifo #(.W(8), .A(4)) key_fifo ( .clk(clk), .rst(rst), .din(key), .wr(key_valid), - .rd(tx_start), .dout(kq), .empty(kq_empty), .full() + .rd(kq_rd), .dout(kq), .empty(kq_empty), .full(), .count() ); + fifo #(.W(8), .A(4)) resp_fifo ( + .clk(clk), .rst(rst), .din(resp_byte), .wr(resp_wr), + .rd(rq_rd), .dout(rq), .empty(rq_empty), .full(rq_full), .count() + ); + + wire [10:0] tq_count; + reg xoff_sent; + always @(posedge clk) begin tx_start <= 1'b0; - if (!kq_empty && !tx_busy && !tx_start && !rst) begin - tx_start <= 1'b1; - tx_byte <= kq; + kq_rd <= 1'b0; + rq_rd <= 1'b0; + if (rst) begin + from_resp <= 1'b0; + flow_pend <= 1'b0; + xoff_sent <= 1'b0; + end else begin + if (!flow_pend && !xoff_sent && tq_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 + flow_pend <= 1'b1; flow_byte <= XON; xoff_sent <= 1'b0; + end + + 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; + 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; + end end end @@ -118,10 +177,11 @@ module basys3_terminal #( // Start-up banner, fed in like received text. Bytes are read from the // high end of the string; the unused high bytes are zero and skipped. - localparam [8*160-1:0] BANNER = { - "Basys 3 ASCII Terminal 80x30\015\012", - "UART RX -> screen, keyboard -> UART TX\015\012", - "SW1:0 baud 00=115200 01=9600 10=19200 11=57600 SW2 echo\015\012\012"}; + localparam [8*200-1:0] BANNER = { + "\033[1mBasys 3 VT220 Terminal\033[0m 80x30 ", + "\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"}; reg [7:0] ban_i; wire [7:0] ban_c = BANNER[ban_i*8 +: 8]; @@ -134,20 +194,20 @@ module basys3_terminal #( always @(posedge clk) begin tin_wr <= 1'b0; - if (tx_start && sw_r[2]) begin + if (kq_rd && sw_r[2]) begin echo_pend <= 1'b1; echo_byte <= kq; end if (rst) begin echo_pend <= 1'b0; - ban_i <= 8'd159; + ban_i <= 8'd199; 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 <= tx_start && sw_r[2]; + echo_pend <= kq_rd && sw_r[2]; end else if (ban_i != 8'hFF && !tin_full && !tin_wr) begin ban_i <= ban_i - 1'b1; if (ban_c != 0) begin @@ -161,49 +221,49 @@ module basys3_terminal #( wire tq_empty; wire tq_rd; - fifo #(.W(8), .A(6)) term_fifo ( + fifo #(.W(8), .A(10)) term_fifo ( .clk(clk), .rst(rst), .din(tin), .wr(tin_wr), - .rd(tq_rd), .dout(tq), .empty(tq_empty), .full(tin_full) + .rd(tq_rd), .dout(tq), .empty(tq_empty), .full(tin_full), .count(tq_count) ); // ------------------------------------------------------------- terminal - wire we; - wire [11:0] waddr; - wire [8:0] wdata; - wire [4:0] top, cur_row; + wire [11:0] t_addr; + wire t_we; + wire [17:0] t_wdata, t_rdata; + wire [4:0] cur_row, stop, sbot, sm_off; wire [6:0] cur_col; - wire cur_vis; + wire cur_vis, sm_on, sm_dir; + wire [3:0] dec_leds; wire bell; + wire frame_tick; term_ctrl term ( .clk(clk), .rst(rst), .in_byte(tq), .in_empty(tq_empty), .in_rd(tq_rd), - .lf_cr(sw_r[3]), .clear_req(clr_sync[1]), - .we(we), .waddr(waddr), .wdata(wdata), - .top(top), .row(cur_row), .col(cur_col), .cur_vis(cur_vis), .bell(bell) + .lf_cr(sw_r[3]), .clear_req(clr_sync[1]), .def_fg(def_fg), + .smooth_sw(sw_r[8]), .smooth_fast(sw_r[9]), .frame_tick(frame_tick), + .addr(t_addr), .we(t_we), .wdata(t_wdata), .rdata(t_rdata), + .row(cur_row), .col(cur_col), .cur_vis(cur_vis), + .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) ); - wire [11:0] raddr; - wire [8:0] rdata; + wire [11:0] v_addr; + wire [17:0] v_data; char_ram screen ( - .clk(clk), .we(we), .waddr(waddr), .wdata(wdata), - .raddr(raddr), .rdata(rdata) + .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) ); - reg [11:0] fg; - always @(posedge clk) - case (sw_r[5:4]) - 2'b00: fg <= 12'h3F3; // green - 2'b01: fg <= 12'hFA0; // amber - 2'b10: fg <= 12'hDDD; // white - 2'b11: fg <= 12'h0EF; // cyan - endcase - text_display #(.FONT_FILE(FONT_FILE)) video ( - .clk(clk), .rst(rst), .top(top), .cur_row(cur_row), .cur_col(cur_col), - .cur_vis(cur_vis), .fg(fg), .bg(12'h000), - .ram_addr(raddr), .ram_data(rdata), - .red(vgaRed), .green(vgaGreen), .blue(vgaBlue), .hsync(Hsync), .vsync(Vsync) + .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), + .red(vgaRed), .green(vgaGreen), .blue(vgaBlue), .hsync(Hsync), .vsync(Vsync), + .frame_tick(frame_tick) ); // ----------------------------------------------------- status displays @@ -215,13 +275,13 @@ module basys3_terminal #( always @(posedge clk) begin if (rx_valid) last_rx <= rx_byte; if (scan_valid) last_scan <= scan; - if (tx_start) last_tx <= kq; + if (tx_start) last_tx <= tx_byte; rx_led <= 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 - assign led = {kb_led != 0, rx_led != 0, caps, bell_led != 0, 4'b0000, last_rx}; + 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)); diff --git a/rtl/fifo.v b/rtl/fifo.v index ab0377e..6ec8cd5 100644 --- a/rtl/fifo.v +++ b/rtl/fifo.v @@ -12,7 +12,8 @@ module fifo #( input rd, output [W-1:0] dout, output empty, - output full + output full, + output [A:0] count ); reg [W-1:0] mem [0:(1<= "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 - reg [2:0] k_len; - reg [7:0] k0, k1, k2, k3; + // 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, k0, k1, k2, k3} = 35'd0; + k_len = 3'd0; + k_seq = 48'd0; if (e0) case (code) - 8'h75: {k_len, k0, k1, k2} = {3'd3, ESC, "[A"}; // up - 8'h72: {k_len, k0, k1, k2} = {3'd3, ESC, "[B"}; // down - 8'h74: {k_len, k0, k1, k2} = {3'd3, ESC, "[C"}; // right - 8'h6B: {k_len, k0, k1, k2} = {3'd3, ESC, "[D"}; // left - 8'h6C: {k_len, k0, k1, k2} = {3'd3, ESC, "[H"}; // home - 8'h69: {k_len, k0, k1, k2} = {3'd3, ESC, "[F"}; // end - 8'h70: {k_len, k0, k1, k2, k3} = {3'd4, ESC, "[2~"}; // insert - 8'h71: {k_len, k0, k1, k2, k3} = {3'd4, ESC, "[3~"}; // delete - 8'h7D: {k_len, k0, k1, k2, k3} = {3'd4, ESC, "[5~"}; // page up - 8'h7A: {k_len, k0, k1, k2, k3} = {3'd4, ESC, "[6~"}; // page down - 8'h4A: {k_len, k0} = {3'd1, "/"}; // keypad / - 8'h5A: {k_len, k0, k1} = enter_crlf ? {3'd2, 8'h0D, 8'h0A} : {3'd1, 8'h0D, 8'h00}; + 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, k0} = {3'd1, bs_del ? 8'h7F : 8'h08}; - 8'h5A: {k_len, k0, k1} = enter_crlf ? {3'd2, 8'h0D, 8'h0A} : {3'd1, 8'h0D, 8'h00}; - 8'h05: {k_len, k0, k1, k2} = {3'd3, ESC, "OP"}; // F1 - 8'h06: {k_len, k0, k1, k2} = {3'd3, ESC, "OQ"}; // F2 - 8'h04: {k_len, k0, k1, k2} = {3'd3, ESC, "OR"}; // F3 - 8'h0C: {k_len, k0, k1, k2} = {3'd3, ESC, "OS"}; // F4 + 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 (ch != 0) begin + 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) k0 = ch; - else if (ch == " ") k0 = 8'h00; - else if (ch >= 8'h40) k0 = ch & 8'h1F; // ^A..^Z, ^[ ^\ ^] ^^ ^_ - else k0 = ch; + 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 @@ -157,14 +190,14 @@ module ps2_keymap ( always @(posedge clk) begin ascii_valid <= 1'b0; if (rst) begin - {e0, f0, lsh, rsh, lct, rct, caps} <= 7'd0; + {e0, f0, lsh, rsh, lct, rct, alt, caps} <= 8'd0; skip <= 3'd0; cnt <= 3'd0; end else begin if (cnt != 0) begin - ascii <= s0; + ascii <= sq[47:40]; ascii_valid <= 1'b1; - {s0, s1, s2} <= {s1, s2, s3}; + sq <= sq << 8; cnt <= cnt - 1'b1; end if (code_valid) begin @@ -183,11 +216,18 @@ module ps2_keymap ( 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'h58 && !e0) begin + 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 - {s0, s1, s2, s3} <= {k0, k1, k2, k3}; - cnt <= k_len; + 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 diff --git a/rtl/term_ctrl.v b/rtl/term_ctrl.v index ad277d0..a29a3f5 100644 --- a/rtl/term_ctrl.v +++ b/rtl/term_ctrl.v @@ -1,15 +1,20 @@ `timescale 1ns / 1ps -// Terminal engine: consumes received bytes and updates the 80x30 character -// buffer. Supports CR LF BS TAB BEL FF, deferred auto-wrap, hardware scrolling -// (the buffer is a ring of rows; `top` is the physical row shown first) and a -// VT100/ANSI subset: -// ESC [ n A/B/C/D cursor up/down/right/left ESC [ r;c H / f position -// ESC [ n d / G row / column absolute ESC [ n J / K erase -// ESC [ n m 0 normal, 7 reverse, 27 not reverse -// ESC [ ? 25 h/l show/hide cursor -// ESC c reset ESC D index ESC E next line ESC M reverse index ESC 7/8 save/restore -// Cell format in the buffer: {reverse, CP437 code}. +// VT220 terminal engine with ANSI colour. Consumes received bytes and keeps +// the 80x30 new_cell buffer (row-major, address = row*80 + col) up to date. +// +// Cell format: {blink, underline, bg[3:0], fg[3:0], CP437 code}. Reverse, +// bold and concealed text are resolved into the colours when a new_cell is written. +// +// Supported (beyond C0 controls): +// ESC 7 8 D E H M Z c = > ESC ( ) <0|A|B> ESC # 8 +// 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) +// 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. +// Scrolling copies rows in memory. With smooth scroll on, a line feed at the +// bottom margin first slides the region up a few pixels per frame. module term_ctrl ( input clk, @@ -17,45 +22,102 @@ module term_ctrl ( input [7:0] in_byte, input in_empty, output reg in_rd, - input lf_cr, // LF also returns the carriage + input lf_cr, // LF also returns the carriage input clear_req, + input [3:0] def_fg, // default foreground colour index + input smooth_sw, // force smooth scrolling on + input smooth_fast, + input frame_tick, // start of vertical blanking + // character RAM port + output reg [11:0] addr, output reg we, - output reg [11:0] waddr, - output reg [8:0] wdata, - output reg [4:0] top, + output reg [17:0] wdata, + input [17:0] rdata, + // to the display output reg [4:0] row, output reg [6:0] col, output reg cur_vis, + output reg [4:0] stop, // scroll region, inclusive + output reg [4:0] sbot, + output reg sm_on, + output reg sm_dir, // 0 = scrolling up, 1 = down + output reg [4:0] sm_off, + // replies to the host (DA, DSR) + output reg [7:0] resp_byte, + output reg resp_wr, + input resp_full, + // modes the keyboard needs + output reg cursor_app, + output reg keypad_app, + output reg lnm, + output reg [3:0] dec_leds, output reg bell ); - localparam LAST_ROW = 5'd29, LAST_COL = 7'd79; + localparam [4:0] LAST_ROW = 5'd29; + localparam [6:0] LAST_COL = 7'd79; + localparam [11:0] CELLS = 12'd2400; - localparam S_IDLE = 2'd0, S_DISP = 2'd1, S_FILL = 2'd2; - localparam P_NORM = 2'd0, P_ESC = 2'd1, P_CSI = 2'd2; + localparam [3:0] S_IDLE = 4'd0, S_DISP = 4'd1, S_FILL = 4'd2, S_CRD = 4'd3, + S_CWAIT = 4'd4, S_CWR = 4'd5, S_PLOOP = 4'd6, S_RESP = 4'd7, + S_PUT = 4'd8, S_TAB = 4'd9, S_SMOOTH = 4'd10, S_SMEND = 4'd11, + S_OP = 4'd12; + localparam [1:0] P_NORM = 2'd0, P_ESC = 2'd1, P_ESCI = 2'd2, P_CSI = 2'd3; + localparam [1:0] K_SGR = 2'd0, K_SET = 2'd1, K_RST = 2'd2, K_LL = 2'd3; - reg [1:0] st, ret_st, ps; - reg [7:0] c; - reg pend; // a character went in the last column - reg rev; - reg [7:0] p0, p1; - reg [1:0] pidx; - reg priv; - reg [4:0] sv_row; - reg [6:0] sv_col; - reg [4:0] fr, er; // fill range, in screen coordinates - reg [6:0] fc, ec; + reg [3:0] st, ret_st, sm_ret; + reg [1:0] ps; + reg [7:0] c; - function [11:0] phys(input [4:0] r, input [6:0] cc, input [4:0] t); - reg [5:0] s; - reg [4:0] pr; - begin - s = r + t; - pr = (s >= 6'd30) ? s - 6'd30 : s; - phys = {1'b0, pr, 6'b0} + {3'b0, pr, 4'b0} + {5'b0, cc}; // pr*80 + cc - end + // modes and attributes + reg pend, awm, irm, om, sclm; + reg bold, ul, blink, rev, conceal; + reg [3:0] fg, bg; + reg [1:0] g0, g1; // 0 ASCII, 1 DEC special graphics, 2 UK + reg gl; // 0 = G0 active, 1 = G1 (SO) + reg [79:0] tabs; + + // saved cursor (DECSC) + reg [4:0] sv_row; + reg [6:0] sv_col; + reg sv_pend, sv_om, sv_gl, sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal; + reg [3:0] sv_fg, sv_bg; + reg [1:0] sv_g0, sv_g1; + + // parser + reg [7:0] par [0:15]; + reg [3:0] pidx; + reg priv, gtm, ign; + reg [7:0] cint, inter; + + // memory copy / fill engine + reg [11:0] c_src, c_dst, c_cnt, f_addr, f_cnt; + reg c_dir; + reg [17:0] f_cell; + + // parameter loop (SGR, SM/RM, DECLL) + reg [1:0] pk; + reg [3:0] pi; + reg [1:0] ext; // 38/48 extended colour state + reg ext_bg; + reg [1:0] ext_n; + reg [7:0] ext_r, ext_g; + + reg [95:0] rbuf; + reg [3:0] rlen; + reg [7:0] rep; + reg tdir; + + integer i; + + // ------------------------------------------------------------ helpers + function [11:0] rc(input [4:0] r, input [6:0] cc); + rc = {1'b0, r, 6'b0} + {3'b0, r, 4'b0} + {5'b0, cc}; + endfunction + + function [11:0] rows80(input [5:0] n); + rows80 = {n, 6'b0} + {2'b0, n, 4'b0}; endfunction - // CSI parameter accumulate, saturating at 255 function [7:0] acc(input [7:0] p, input [7:0] d); reg [11:0] v; begin @@ -64,172 +126,596 @@ module term_ctrl ( end endfunction - wire [7:0] n0 = p0 == 0 ? 8'd1 : p0; - wire [8:0] row_dn = row + n0; - wire [8:0] col_rt = col + n0; - wire [4:0] row_p0 = p0 == 0 ? 5'd0 : p0 > 30 ? LAST_ROW : p0 - 1'b1; - wire [6:0] col_p0 = p0 == 0 ? 7'd0 : p0 > 80 ? LAST_COL : p0 - 1'b1; - wire [6:0] col_p1 = p1 == 0 ? 7'd0 : p1 > 80 ? LAST_COL : p1 - 1'b1; + // DEC special graphics to CP437 + function [7:0] gfx(input [7:0] ch); + case (ch) + 8'h5F: gfx = 8'h20; 8'h60: gfx = 8'h04; 8'h61: gfx = 8'hB1; + 8'h66: gfx = 8'hF8; 8'h67: gfx = 8'hF1; 8'h6A: gfx = 8'hD9; + 8'h6B: gfx = 8'hBF; 8'h6C: gfx = 8'hDA; 8'h6D: gfx = 8'hC0; + 8'h6E: gfx = 8'hC5; 8'h6F: gfx = 8'hC4; 8'h70: gfx = 8'hC4; + 8'h71: gfx = 8'hC4; 8'h72: gfx = 8'hC4; 8'h73: gfx = 8'hC4; + 8'h74: gfx = 8'hC3; 8'h75: gfx = 8'hB4; 8'h76: gfx = 8'hC1; + 8'h77: gfx = 8'hC2; 8'h78: gfx = 8'hB3; 8'h79: gfx = 8'hF3; + 8'h7A: gfx = 8'hF2; 8'h7B: gfx = 8'hE3; 8'h7C: gfx = 8'hD8; + 8'h7D: gfx = 8'h9C; 8'h7E: gfx = 8'hFA; + default: gfx = 8'hFE; // control-code pictures + endcase + endfunction - task fill(input [4:0] r0, input [6:0] c0, input [4:0] r1, input [6:0] c1, input [1:0] rs); + function [1:0] charset(input [7:0] ch); + charset = ch == "0" ? 2'd1 : ch == "A" ? 2'd2 : 2'd0; + endfunction + + // colour index from levels: bit0 red, bit1 green, bit2 blue, bit3 bright + function [3:0] approx(input hr, input hg, input hb, input bright, input dim); + approx = (hr | hg | hb) ? {bright, hb, hg, hr} : (dim ? 4'd8 : 4'd0); + endfunction + + function [3:0] map256(input [7:0] n); + reg [7:0] k; + reg [2:0] r, g, b, mx; begin - fr <= r0; fc <= c0; er <= r1; ec <= c1; + if (n < 16) + map256 = n[3:0]; + else if (n >= 232) + map256 = n < 238 ? 4'd0 : n < 244 ? 4'd8 : n < 250 ? 4'd7 : 4'd15; + else begin + k = n - 8'd16; + r = k / 36; + g = (k / 6) % 6; + b = k % 6; + mx = r > g ? (r > b ? r : b) : (g > b ? g : b); + map256 = approx(r >= 3, g >= 3, b >= 3, mx >= 4, mx >= 2); + end + end + endfunction + + function [3:0] rgb16(input [7:0] r, input [7:0] g, input [7:0] b); + reg [7:0] mx; + begin + mx = r > g ? (r > b ? r : b) : (g > b ? g : b); + rgb16 = approx(r[7], g[7], b[7], mx >= 192, mx >= 64); + end + endfunction + + function [6:0] next_tab(input [79:0] t, input [6:0] cc); + integer k; + begin + next_tab = LAST_COL; + for (k = 79; k >= 0; k = k - 1) + if (k > cc && t[k]) next_tab = k; + end + endfunction + + function [6:0] prev_tab(input [79:0] t, input [6:0] cc); + integer k; + begin + prev_tab = 7'd0; + for (k = 0; k < 80; k = k + 1) + if (k < cc && t[k]) prev_tab = k; + end + endfunction + + // --------------------------------------------------- derived values + wire [17:0] BLANK = {2'b00, 4'd0, def_fg, 8'h20}; + wire [7:0] p0 = par[0]; + wire [7:0] p1 = par[1]; + wire [7:0] n8 = p0 == 0 ? 8'd1 : p0; + wire [6:0] room = 7'd80 - col; // cells from cursor to end of line + wire [6:0] nc = n8 > room ? room : n8[6:0]; + + wire [3:0] fgb = bold ? (fg | 4'd8) : fg; + wire [3:0] dfg = rev ? bg : fgb; + wire [3:0] dbg = rev ? fgb : bg; + wire [1:0] cs = gl ? g1 : g0; + wire [7:0] glyph = (cs == 2'd1 && c >= 8'h5F && c <= 8'h7E) ? gfx(c) : + (cs == 2'd2 && c == "#") ? 8'h9C : c; + wire [17:0] new_cell = {blink, ul, dbg, conceal ? dbg : dfg, glyph}; + + wire smooth = sclm | smooth_sw; + wire [4:0] sm_step = smooth_fast ? 5'd4 : 5'd2; + + wire [4:0] up_lim = row >= stop ? stop : 5'd0; + wire [4:0] dn_lim = row <= sbot ? sbot : LAST_ROW; + wire [4:0] row_up = (row - up_lim > n8) ? row - n8 : up_lim; + wire [4:0] row_dn = (dn_lim - row > n8) ? row + n8 : dn_lim; + wire [6:0] col_rt = (LAST_COL - col > n8) ? col + n8 : LAST_COL; + wire [6:0] col_lt = (col > n8) ? col - n8 : 7'd0; + + wire [7:0] r_rel = p0 == 0 ? 8'd0 : p0 - 1'b1; + wire [8:0] r_abs = (om ? stop : 5'd0) + r_rel; + wire [4:0] r_max = om ? sbot : LAST_ROW; + wire [4:0] row_p0 = r_abs > r_max ? r_max : r_abs[4:0]; + wire [6:0] col_p0 = p0 == 0 ? 7'd0 : p0 > 80 ? LAST_COL : p0 - 1'b1; + wire [6:0] col_p1 = p1 == 0 ? 7'd0 : p1 > 80 ? LAST_COL : p1 - 1'b1; + + wire [11:0] here = rc(row, col); + wire [4:0] rep_row = (om ? row - stop : row) + 1'b1; + wire [6:0] rep_col = col + 1'b1; + reg [7:0] pv; // parameter being processed, registered + reg pv_ok; + + // ------------------------------------------------------------ tasks + task op(input [11:0] src, input [11:0] dst, input [11:0] ccnt, input dir, + input [11:0] fa, input [11:0] fcnt, input [17:0] fcell, input [3:0] rs); + begin + c_src <= src; c_dst <= dst; c_cnt <= ccnt; c_dir <= dir; + f_addr <= fa; f_cnt <= fcnt; f_cell <= fcell; ret_st <= rs; - st <= S_FILL; + st <= S_OP; // decided next clock, off the critical path end endtask - // line feed; scrolls by advancing `top` and blanking the new bottom row - task line_feed(input [1:0] rs); + task fill(input [11:0] fa, input [11:0] fcnt); + op(12'd0, 12'd0, 12'd0, 1'b0, fa, fcnt, BLANK, S_IDLE); + endtask + + // scroll rows t..b up (dir 0) or down (dir 1) by n lines + task scroll(input dir, input [4:0] t, input [4:0] b, input [7:0] n, input [3:0] rs); + reg [5:0] h, k; begin - if (row == LAST_ROW) begin - top <= top == LAST_ROW ? 5'd0 : top + 1'b1; - fill(LAST_ROW, 0, LAST_ROW, LAST_COL, rs); + h = b - t + 1'b1; + k = n > h ? h : n[5:0]; + if (!dir) + op(rc(t + k, 0), rc(t, 0), rows80(h - k), 1'b0, + rc(b - k + 1'b1, 0), rows80(k), BLANK, rs); + else + op(rc(b - k + 1'b1, 0) - 1'b1, rc(b + 1'b1, 0) - 1'b1, rows80(h - k), 1'b1, + rc(t, 0), rows80(k), BLANK, rs); + end + endtask + + task line_feed(input [3:0] rs); + begin + if (row == sbot) begin + if (smooth) begin + sm_on <= 1'b1; sm_dir <= 1'b0; sm_off <= 5'd0; sm_ret <= rs; + st <= S_SMOOTH; + end else + scroll(1'b0, stop, sbot, 8'd1, rs); end else begin - row <= row + 1'b1; - st <= rs; + if (row != LAST_ROW) row <= row + 1'b1; + st <= rs; end end endtask - reg r_sgr; + task reverse_index; + begin + if (row == stop) begin + if (smooth) begin + sm_on <= 1'b1; sm_dir <= 1'b1; sm_off <= 5'd0; sm_ret <= S_IDLE; + st <= S_SMOOTH; + end else + scroll(1'b1, stop, sbot, 8'd1, S_IDLE); + end else if (row != 0) + row <= row - 1'b1; + end + endtask - always @(posedge clk) begin - in_rd <= 1'b0; - we <= 1'b0; - bell <= 1'b0; - if (rst) begin - ps <= P_NORM; - {top, row, col, sv_row, sv_col} <= 0; - {pend, rev} <= 2'b00; + task reset_attrs; + begin + {bold, ul, blink, rev, conceal} <= 5'b0; + fg <= def_fg; + bg <= 4'd0; + end + endtask + + task soft_reset; + begin + reset_attrs; + {pend, irm, om, cursor_app, keypad_app, gl} <= 6'b0; + {g0, g1} <= 4'b0; + awm <= 1'b1; cur_vis <= 1'b1; - fill(0, 0, LAST_ROW, LAST_COL, S_IDLE); + 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; + {sv_g0, sv_g1, sv_bg} <= 0; + sv_fg <= def_fg; + end + endtask + + task full_reset; + begin + soft_reset; + {row, col} <= 0; + {sclm, lnm} <= 2'b0; + dec_leds <= 4'd0; + sm_on <= 1'b0; + sm_off <= 5'd0; + ps <= P_NORM; + for (i = 0; i < 80; i = i + 1) tabs[i] <= i != 0 && i % 8 == 0; + fill(12'd0, CELLS); + end + endtask + + task respond(input [95:0] s, input [3:0] n); + begin + rbuf <= s; + rlen <= n; + st <= S_RESP; + end + endtask + + function [7:0] tens(input [6:0] v); + tens = "0" + v / 7'd10; + endfunction + + function [7:0] ones(input [6:0] v); + ones = "0" + v % 7'd10; + endfunction + + task cursor_report; + respond({8'h1B, "[", tens(rep_row), ones(rep_row), ";", + tens(rep_col), ones(rep_col), "R", 32'd0}, 4'd8); + endtask + + // one SGR parameter + task sgr(input [7:0] v); + begin + case (ext) + 2'd0: + if (v == 0) reset_attrs; + else if (v == 1) bold <= 1'b1; + else if (v == 4 || v == 21) ul <= 1'b1; + else if (v == 5 || v == 6) blink <= 1'b1; + else if (v == 7) rev <= 1'b1; + else if (v == 8) conceal <= 1'b1; + else if (v == 22) bold <= 1'b0; + else if (v == 24) ul <= 1'b0; + else if (v == 25) blink <= 1'b0; + else if (v == 27) rev <= 1'b0; + else if (v == 28) conceal <= 1'b0; + else if (v >= 30 && v <= 37) fg <= v - 8'd30; + else if (v == 38 || v == 48) begin ext <= 2'd1; ext_bg <= v == 48; end + else if (v == 39) fg <= def_fg; + else if (v >= 40 && v <= 47) bg <= v - 8'd40; + else if (v == 49) bg <= 4'd0; + else if (v >= 90 && v <= 97) fg <= v - 8'd82; + else if (v >= 100 && v <= 107) bg <= v - 8'd92; + 2'd1: // after 38/48: 5 = indexed, 2 = RGB + if (v == 5) ext <= 2'd2; + else if (v == 2) begin ext <= 2'd3; ext_n <= 2'd0; end + else ext <= 2'd0; + 2'd2: begin + if (ext_bg) bg <= map256(v); else fg <= map256(v); + ext <= 2'd0; + end + 2'd3: begin + ext_n <= ext_n + 1'b1; + if (ext_n == 0) ext_r <= v; + else if (ext_n == 1) ext_g <= v; + else begin + if (ext_bg) bg <= rgb16(ext_r, ext_g, v); else fg <= rgb16(ext_r, ext_g, v); + ext <= 2'd0; + end + end + endcase + end + endtask + + // one SM/RM parameter + task set_mode(input [7:0] v, input on); + begin + if (priv) begin + if (v == 1) cursor_app <= on; + if (v == 4) sclm <= on; + 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; + end else begin + if (v == 4) irm <= on; + if (v == 20) lnm <= on; + end + end + endtask + + task plist(input [1:0] kind); + begin + pk <= kind; + pi <= 4'd0; + ext <= 2'd0; + pv_ok <= 1'b0; + st <= S_PLOOP; + end + endtask + + // ------------------------------------------------------------ main + always @(posedge clk) begin + in_rd <= 1'b0; + we <= 1'b0; + bell <= 1'b0; + resp_wr <= 1'b0; + if (rst) begin + full_reset; + cursor_app <= 1'b0; end else case (st) S_IDLE: if (clear_req) begin {row, col, pend} <= 0; - fill(0, 0, LAST_ROW, LAST_COL, S_IDLE); + fill(12'd0, CELLS); end else if (!in_empty && !in_rd) begin c <= in_byte; in_rd <= 1'b1; st <= S_DISP; end - S_FILL: begin + // ---- copy c_cnt cells, then fill f_cnt cells, then go to ret_st + S_OP: + st <= c_cnt != 0 ? S_CRD : f_cnt != 0 ? S_FILL : ret_st; + S_CRD: begin + addr <= c_src; + st <= S_CWAIT; + end + S_CWAIT: + st <= S_CWR; + S_CWR: begin + addr <= c_dst; + wdata <= rdata; we <= 1'b1; - waddr <= phys(fr, fc, top); - wdata <= 9'h020; - if (fr == er && fc == ec) - st <= ret_st; - else if (fc == LAST_COL) begin - fc <= 7'd0; - fr <= fr + 1'b1; - end else - fc <= fc + 1'b1; + c_src <= c_dir ? c_src - 1'b1 : c_src + 1'b1; + c_dst <= c_dir ? c_dst - 1'b1 : c_dst + 1'b1; + c_cnt <= c_cnt - 1'b1; + st <= c_cnt != 1 ? S_CRD : f_cnt != 0 ? S_FILL : ret_st; + end + S_FILL: begin + addr <= f_addr; + wdata <= f_cell; + we <= 1'b1; + f_addr <= f_addr + 1'b1; + f_cnt <= f_cnt - 1'b1; + if (f_cnt == 1) st <= ret_st; + end + + S_SMOOTH: + // the region slides during active video; the copy runs in blanking + if (frame_tick) begin + if (sm_off + sm_step >= 16) + scroll(sm_dir, stop, sbot, 8'd1, S_SMEND); + else + sm_off <= sm_off + sm_step; + end + S_SMEND: begin + sm_on <= 1'b0; + sm_off <= 5'd0; + st <= sm_ret; + end + + S_RESP: + if (!resp_full) begin + resp_byte <= rbuf[95:88]; + resp_wr <= 1'b1; + rbuf <= rbuf << 8; + rlen <= rlen - 1'b1; + if (rlen == 1) st <= S_IDLE; + end + + S_TAB: begin + col <= tdir ? prev_tab(tabs, col) : next_tab(tabs, col); + rep <= rep - 1'b1; + if (rep == 1) st <= S_IDLE; + end + + S_PLOOP: if (!pv_ok) begin + // fetch the parameter one clock ahead, then act on it + pv <= par[pi]; + pv_ok <= 1'b1; + end else begin + pv_ok <= 1'b0; + case (pk) + K_SGR: sgr(pv); + K_SET: set_mode(pv, 1'b1); + K_RST: set_mode(pv, 1'b0); + K_LL: + if (pv == 0) dec_leds <= 4'd0; + else if (pv >= 1 && pv <= 4) dec_leds[pv - 1] <= 1'b1; + else if (pv >= 21 && pv <= 24) dec_leds[pv - 21] <= 1'b0; + endcase + pi <= pi + 1'b1; + if (pi == pidx) st <= S_IDLE; + end + + S_PUT: begin // write the character at the cursor + we <= 1'b1; + addr <= here; + wdata <= new_cell; + if (col == LAST_COL) pend <= awm; + else col <= col + 1'b1; + st <= S_IDLE; end S_DISP: begin st <= S_IDLE; - case (ps) - P_NORM: - if (c >= 8'h20 && c != 8'h7F) begin - if (pend) begin - // wrap first, then come back to print this byte - pend <= 1'b0; - col <= 7'd0; - line_feed(S_DISP); - end else begin - we <= 1'b1; - waddr <= phys(row, col, top); - wdata <= {rev, c}; - if (col == LAST_COL) pend <= 1'b1; - else col <= col + 1'b1; - end - end else case (c) + if (c == 8'h1B) begin + ps <= P_ESC; + end else if (c == 8'h18 || c == 8'h1A) begin + ps <= P_NORM; // CAN / SUB cancel a sequence + end else if (c < 8'h20) begin + // C0 controls act immediately, even inside an escape sequence + case (c) 8'h07: bell <= 1'b1; 8'h08: begin pend <= 1'b0; if (col != 0) col <= col - 1'b1; end - 8'h09: begin pend <= 1'b0; col <= col >= 7'd72 ? LAST_COL : (col | 7'd7) + 1'b1; end - 8'h0A, 8'h0B: begin + 8'h09: begin pend <= 1'b0; col <= next_tab(tabs, col); end + 8'h0A, 8'h0B, 8'h0C: begin pend <= 1'b0; - if (lf_cr) col <= 7'd0; + if (lf_cr || lnm) col <= 7'd0; line_feed(S_IDLE); end - 8'h0C: begin - {row, col, pend} <= 0; - fill(0, 0, LAST_ROW, LAST_COL, S_IDLE); - end 8'h0D: begin pend <= 1'b0; col <= 7'd0; end - 8'h1B: ps <= P_ESC; + 8'h0E: gl <= 1'b1; + 8'h0F: gl <= 1'b0; default: ; endcase + end else if (c == 8'h7F) begin + // DEL is ignored + end else case (ps) + P_NORM: + if (pend && awm) begin + pend <= 1'b0; + col <= 7'd0; + line_feed(S_DISP); // wrap, then print this byte + end else if (irm && col != LAST_COL) + // insert mode: shift the rest of the line right first + op(rc(row, LAST_COL - 1'b1), rc(row, LAST_COL), {5'd0, LAST_COL - col}, 1'b1, + 12'd0, 12'd0, BLANK, S_PUT); + else + st <= S_PUT; P_ESC: begin ps <= P_NORM; - case (c) + if (c >= 8'h20 && c <= 8'h2F) begin + inter <= c; + ps <= P_ESCI; + end else case (c) "[": begin ps <= P_CSI; - {p0, p1, pidx, priv} <= 0; - end - "c": begin - {row, col, pend, rev} <= 0; - cur_vis <= 1'b1; - fill(0, 0, LAST_ROW, LAST_COL, S_IDLE); + for (i = 0; i < 16; i = i + 1) par[i] <= 8'd0; + {pidx, priv, gtm, ign} <= 0; + cint <= 8'd0; end "D": begin pend <= 1'b0; line_feed(S_IDLE); end "E": begin pend <= 1'b0; col <= 7'd0; line_feed(S_IDLE); end - "M": begin pend <= 1'b0; if (row != 0) row <= row - 1'b1; end - "7": begin sv_row <= row; sv_col <= col; end - "8": begin pend <= 1'b0; row <= sv_row; col <= sv_col; end - 8'h1B: ps <= P_ESC; + "M": begin pend <= 1'b0; reverse_index; end + "H": tabs[col] <= 1'b1; + "7": begin + {sv_row, sv_col, sv_pend, sv_om, sv_gl} <= {row, col, pend, om, gl}; + {sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal} <= {bold, ul, blink, rev, conceal}; + {sv_fg, sv_bg, sv_g0, sv_g1} <= {fg, bg, g0, g1}; + end + "8": begin + {row, col, pend, om, gl} <= {sv_row, sv_col, sv_pend, sv_om, sv_gl}; + {bold, ul, blink, rev, conceal} <= {sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal}; + {fg, bg, g0, g1} <= {sv_fg, sv_bg, sv_g0, sv_g1}; + end + "=": keypad_app <= 1'b1; + ">": keypad_app <= 1'b0; + "Z": respond({8'h1B, "[?62;22c", 24'd0}, 4'd9); + "c": begin full_reset; cursor_app <= 1'b0; end + default: ; + endcase + end + + P_ESCI: begin + ps <= P_NORM; + if (c >= 8'h20 && c <= 8'h2F) + ps <= P_ESCI; // further intermediates + else case (inter) + "(": g0 <= charset(c); + ")": g1 <= charset(c); + "#": if (c == "8") begin // DECALN: fill the screen with E + {row, col, pend, stop} <= 0; + sbot <= LAST_ROW; + op(12'd0, 12'd0, 12'd0, 1'b0, 12'd0, CELLS, {2'b00, 4'd0, def_fg, "E"}, S_IDLE); + end default: ; endcase end P_CSI: - if (c >= "0" && c <= "9") begin - if (pidx == 0) p0 <= acc(p0, c); - else if (pidx == 1) p1 <= acc(p1, c); - end else if (c == ";") begin - if (pidx != 2'd3) pidx <= pidx + 1'b1; - end else if (c == "?") begin + if (c >= "0" && c <= "9") + par[pidx] <= acc(par[pidx], c); + else if (c == ";" || c == ":") begin + if (pidx != 4'd15) pidx <= pidx + 1'b1; + end else if (c == "?") priv <= 1'b1; - end else if (c == 8'h18 || c == 8'h1A) begin - ps <= P_NORM; // CAN / SUB abort the sequence - end else if (c >= 8'h40 && c <= 8'h7E) begin + else if (c == ">") + gtm <= 1'b1; + else if (c == "<" || c == "=") + ign <= 1'b1; + else if (c >= 8'h20 && c <= 8'h2F) + cint <= c; + else if (c >= 8'h40 && c <= 8'h7E) begin ps <= P_NORM; - if (priv) begin - if (p0 == 25 && c == "h") cur_vis <= 1'b1; - if (p0 == 25 && c == "l") cur_vis <= 1'b0; - end else case (c) - "A": begin pend <= 1'b0; row <= row > n0 ? row - n0 : 5'd0; end - "B": begin pend <= 1'b0; row <= row_dn > LAST_ROW ? LAST_ROW : row_dn[4:0]; end - "C": begin pend <= 1'b0; col <= col_rt > LAST_COL ? LAST_COL : col_rt[6:0]; end - "D": begin pend <= 1'b0; col <= col > n0 ? col - n0 : 7'd0; end - "H", "f": begin pend <= 1'b0; row <= row_p0; col <= col_p1; end - "d": begin pend <= 1'b0; row <= row_p0; end - "G", "`": begin pend <= 1'b0; col <= col_p0; end - "J": case (p0) - 8'd0: fill(row, col, LAST_ROW, LAST_COL, S_IDLE); - 8'd1: fill(0, 0, row, col, S_IDLE); - 8'd2, 8'd3: fill(0, 0, LAST_ROW, LAST_COL, S_IDLE); - default: ; - endcase - "K": case (p0) - 8'd0: fill(row, col, row, LAST_COL, S_IDLE); - 8'd1: fill(row, 0, row, col, S_IDLE); - 8'd2: fill(row, 0, row, LAST_COL, S_IDLE); - default: ; - endcase - "m": begin - r_sgr = rev; - if (p0 == 0 || p0 == 27) r_sgr = 1'b0; - else if (p0 == 7) r_sgr = 1'b1; - if (pidx != 0) begin - if (p1 == 0 || p1 == 27) r_sgr = 1'b0; - else if (p1 == 7) r_sgr = 1'b1; + if (ign) begin + end else if (cint == "!") begin + if (c == "p") soft_reset; + end else if (cint != 0) begin + end else if (gtm) begin + if (c == "c") respond({8'h1B, "[>1;10;0c", 16'd0}, 4'd10); + end else if (priv) begin + case (c) + "h": plist(K_SET); + "l": plist(K_RST); + "J": begin + pend <= 1'b0; + if (p0 == 0) fill(here, CELLS - here); + else if (p0 == 1) fill(12'd0, here + 1'b1); + else if (p0 == 2) fill(12'd0, CELLS); end - rev <= r_sgr; + "K": begin + pend <= 1'b0; + if (p0 == 0) fill(here, room); + else if (p0 == 1) fill(rc(row, 0), col + 1'b1); + else if (p0 == 2) fill(rc(row, 0), 12'd80); + end + "n": if (p0 == 6) cursor_report; + default: ; + endcase + end else case (c) + "A": begin pend <= 1'b0; row <= row_up; end + "B", "e": begin pend <= 1'b0; row <= row_dn; end + "C", "a": begin pend <= 1'b0; col <= col_rt; end + "D": begin pend <= 1'b0; col <= col_lt; end + "E": begin pend <= 1'b0; row <= row_dn; col <= 7'd0; end + "F": begin pend <= 1'b0; row <= row_up; col <= 7'd0; end + "G", "`": begin pend <= 1'b0; col <= col_p0; end + "d": begin pend <= 1'b0; row <= row_p0; end + "H", "f": begin pend <= 1'b0; row <= row_p0; col <= col_p1; end + "I": begin pend <= 1'b0; rep <= n8; tdir <= 1'b0; st <= S_TAB; end + "Z": begin pend <= 1'b0; rep <= n8; tdir <= 1'b1; st <= S_TAB; end + "J": begin + pend <= 1'b0; + if (p0 == 0) fill(here, CELLS - here); + else if (p0 == 1) fill(12'd0, here + 1'b1); + else if (p0 == 2 || p0 == 3) fill(12'd0, CELLS); end + "K": begin + pend <= 1'b0; + if (p0 == 0) fill(here, room); + else if (p0 == 1) fill(rc(row, 0), col + 1'b1); + else if (p0 == 2) fill(rc(row, 0), 12'd80); + end + "L": if (row >= stop && row <= sbot) begin + pend <= 1'b0; col <= 7'd0; + scroll(1'b1, row, sbot, n8, S_IDLE); + end + "M": if (row >= stop && row <= sbot) begin + pend <= 1'b0; col <= 7'd0; + scroll(1'b0, row, sbot, n8, S_IDLE); + end + "@": begin + pend <= 1'b0; + op(rc(row, LAST_COL - nc), rc(row, LAST_COL), room - nc, 1'b1, + here, nc, BLANK, S_IDLE); + end + "P": begin + pend <= 1'b0; + op(rc(row, col + nc), here, room - nc, 1'b0, + rc(row, 7'd80 - nc), nc, BLANK, S_IDLE); + end + "X": begin pend <= 1'b0; fill(here, nc); end + "S": scroll(1'b0, stop, sbot, n8, S_IDLE); + "T": scroll(1'b1, stop, sbot, n8, S_IDLE); + "g": if (p0 == 0) tabs[col] <= 1'b0; else if (p0 == 3) tabs <= 80'd0; + "h": plist(K_SET); + "l": plist(K_RST); + "m": plist(K_SGR); + "q": plist(K_LL); + "r": begin + if ((p0 == 0 ? 8'd1 : p0) < (p1 == 0 || p1 > 30 ? 8'd30 : p1)) begin + stop <= p0 == 0 ? 5'd0 : p0 - 1'b1; + sbot <= p1 == 0 || p1 > 30 ? LAST_ROW : p1 - 1'b1; + row <= om ? (p0 == 0 ? 5'd0 : p0 - 1'b1) : 5'd0; + col <= 7'd0; + pend <= 1'b0; + end + end + "s": {sv_row, sv_col, sv_pend} <= {row, col, pend}; + "u": {row, col, pend} <= {sv_row, sv_col, sv_pend}; + "n": if (p0 == 5) respond({8'h1B, "[0n", 64'd0}, 4'd4); + else if (p0 == 6) cursor_report; + "c": if (p0 == 0) respond({8'h1B, "[?62;22c", 24'd0}, 4'd9); default: ; endcase end diff --git a/rtl/text_display.v b/rtl/text_display.v index f155872..4ba2ca9 100644 --- a/rtl/text_display.v +++ b/rtl/text_display.v @@ -1,27 +1,34 @@ `timescale 1ns / 1ps -// 640x480 @ 60 Hz VGA text display: 80x30 cells of 8x16 pixels. -// Runs on the 100 MHz clock with a 25 MHz pixel enable; the character RAM -// and font ROM reads finish well inside the four clocks of each pixel. +// 640x480 @ 60 Hz VGA text display: 80x30 cells of 8x16 pixels, 16 colours, +// underline, blink, block cursor and smooth-scroll offset for the scroll region. +// Runs on the 100 MHz clock with a 25 MHz pixel enable. After each pixel tick: +// clock 1 scanline values registered (row base address, glyph line) +// clock 2 character RAM read +// clock 3 font ROM read +// clock 4 pixel out (next tick) module text_display #( parameter FONT_FILE = "font.mem" ) ( input clk, input rst, - input [4:0] top, // physical row shown at the top of the screen input [4:0] cur_row, input [6:0] cur_col, input cur_vis, - input [11:0] fg, // 4:4:4 RGB - input [11:0] bg, + input [4:0] stop, // scroll region (rows, inclusive) + input [4:0] sbot, + input sm_on, // smooth scroll in progress + input sm_dir, // 0 = up, 1 = down + input [4:0] sm_off, // pixels the region has moved output [11:0] ram_addr, - input [8:0] ram_data, // {reverse, code}, one clock after ram_addr + input [17:0] ram_data, // {blink, underline, bg, fg, code} output reg [3:0] red, output reg [3:0] green, output reg [3:0] blue, output reg hsync, - output reg vsync + output reg vsync, + output reg frame_tick // one clock at the start of vertical blanking ); localparam H_VIS = 640, H_FP = 16, H_SYNC = 96, H_TOTAL = 800; localparam V_VIS = 480, V_FP = 10, V_SYNC = 2, V_TOTAL = 525; @@ -33,6 +40,7 @@ module text_display #( always @(posedge clk) begin div <= rst ? 2'd0 : div + 1'b1; + frame_tick <= tick && h == 0 && v == V_VIS; if (rst) begin h <= 0; v <= 0; @@ -49,46 +57,76 @@ module text_display #( end end - // character cell under the beam + // which buffer row and glyph line this scanline shows + wire in_region = sm_on && v < V_VIS && v[8:4] >= stop && v[8:4] <= sbot; + wire [9:0] ys = !in_region ? v : sm_dir ? v - sm_off : v + sm_off; + wire gap = in_region && (sm_dir ? v < {stop, 4'b0} + sm_off : ys[9:4] > sbot); + + reg [11:0] row_base; + reg [4:0] srow; + reg [3:0] line; + reg blank_line; + always @(posedge clk) begin + srow <= ys[8:4]; + row_base <= {1'b0, ys[8:4], 6'b0} + {3'b0, ys[8:4], 4'b0}; + line <= ys[3:0]; + blank_line <= gap; + end + wire [6:0] tcol = h[9:3]; - wire [4:0] trow = v[8:4]; - wire [5:0] psum = trow + top; - wire [4:0] prow = psum >= 6'd30 ? psum - 6'd30 : psum; - assign ram_addr = {1'b0, prow, 6'b0} + {3'b0, prow, 4'b0} + {5'b0, tcol}; + assign ram_addr = row_base + {5'b0, tcol}; reg [7:0] font [0:4095]; reg [7:0] glyph; initial $readmemh(FONT_FILE, font); - always @(posedge clk) glyph <= font[{ram_data[7:0], v[3:0]}]; + always @(posedge clk) glyph <= font[{ram_data[7:0], line}]; + + function [11:0] palette(input [3:0] n); + case (n) + 4'd0: palette = 12'h000; 4'd1: palette = 12'hA00; + 4'd2: palette = 12'h0A0; 4'd3: palette = 12'hA50; + 4'd4: palette = 12'h00A; 4'd5: palette = 12'hA0A; + 4'd6: palette = 12'h0AA; 4'd7: palette = 12'hAAA; + 4'd8: palette = 12'h555; 4'd9: palette = 12'hF55; + 4'd10: palette = 12'h5F5; 4'd11: palette = 12'hFF5; + 4'd12: palette = 12'h55F; 4'd13: palette = 12'hF5F; + 4'd14: palette = 12'h5FF; 4'd15: palette = 12'hFFF; + endcase + endfunction wire visible = h < H_VIS && v < V_VIS; - wire cursor = cur_vis && frame[5] && tcol == cur_col && trow == cur_row; - wire on = glyph[~h[2:0]] ^ ram_data[8] ^ cursor; + wire cursor = cur_vis && frame[5] && tcol == cur_col && srow == cur_row; + wire ink = (glyph[~h[2:0]] | (ram_data[16] && line == 4'd15)) && !(ram_data[17] && frame[5]); + wire [3:0] idx = blank_line ? 4'd0 : (ink ^ cursor) ? ram_data[11:8] : ram_data[15:12]; always @(posedge clk) begin if (tick) begin - {red, green, blue} <= !visible ? 12'h000 : on ? fg : bg; + {red, green, blue} <= visible ? palette(idx) : 12'h000; hsync <= !(h >= H_VIS + H_FP && h < H_VIS + H_FP + H_SYNC); vsync <= !(v >= V_VIS + V_FP && v < V_VIS + V_FP + V_SYNC); end end endmodule -// 2400-cell character buffer: write port for the terminal, read port for video. +// 2400-cell character buffer: read/write port for the terminal engine, +// read-only port for video. module char_ram ( input clk, - input we, - input [11:0] waddr, - input [8:0] wdata, - input [11:0] raddr, - output reg [8:0] rdata + input [11:0] a_addr, + input a_we, + input [17:0] a_wdata, + output reg [17:0] a_rdata, + input [11:0] b_addr, + output reg [17:0] b_rdata ); - (* ram_style = "block" *) reg [8:0] mem [0:4095]; + (* ram_style = "block" *) reg [17:0] mem [0:4095]; integer i; - initial for (i = 0; i < 4096; i = i + 1) mem[i] = 9'h020; + initial for (i = 0; i < 4096; i = i + 1) mem[i] = {10'h00A, 8'h20}; always @(posedge clk) begin - if (we) mem[waddr] <= wdata; - rdata <= mem[raddr]; + if (a_we) mem[a_addr] <= a_wdata; + a_rdata <= mem[a_addr]; end + + always @(posedge clk) b_rdata <= mem[b_addr]; endmodule diff --git a/scripts/program.tcl b/scripts/program.tcl index bba0d5a..b256a4e 100644 --- a/scripts/program.tcl +++ b/scripts/program.tcl @@ -32,7 +32,9 @@ if {$mode eq "flash"} { delete_hw_cfgmem $mem } if {!$done} { error "flash part not recognised" } - puts "Flash programmed; set JP1 to QSPI and power-cycle (or press PROG)." + # reload the FPGA from flash (works when JP1 is set to QSPI) + boot_hw_device $dev + puts "Flash programmed; with JP1 on QSPI the board now boots from it." } else { set_property PROGRAM.FILE $root/build/basys3_terminal.bit $dev program_hw_devices $dev diff --git a/scripts/sim.sh b/scripts/sim.sh index 8eb466e..9cca8ef 100755 --- a/scripts/sim.sh +++ b/scripts/sim.sh @@ -10,5 +10,8 @@ xvlog -d FONT_FILE="\"$ROOT/font/font.mem\"" "$ROOT"/rtl/*.v "$ROOT"/sim/tb_term xelab -debug off tb_terminal -s tb_terminal xsim tb_terminal -R if command -v python3 >/dev/null; then - python3 -c "from PIL import Image; Image.open('frame.ppm').save('frame.png')" && echo "wrote sim/work/frame.png" + for f in frame_*.ppm; do + python3 -c "import sys; from PIL import Image; Image.open(sys.argv[1]).save(sys.argv[2])" \ + "$f" "${f%.ppm}.png" && echo "wrote sim/work/${f%.ppm}.png" + done fi diff --git a/sim/tb_terminal.v b/sim/tb_terminal.v index d16988a..3ef7d08 100644 --- a/sim/tb_terminal.v +++ b/sim/tb_terminal.v @@ -1,12 +1,13 @@ `timescale 1ns / 1ps -// Whole-design test: keystrokes on PS/2 -> UART TX bytes, UART RX text -> -// screen buffer (printed as text) and one VGA frame (written as frame.ppm). +// Whole-design test: keystrokes on PS/2 -> UART TX bytes, host queries -> +// replies, UART RX text -> screen buffer (printed as text) and VGA frames +// (written as frame_*.ppm). module tb_terminal; reg clk = 0; always #5 clk = ~clk; - reg [15:0] sw = 16'h0004; // 115200 baud, local echo on + reg [15:0] sw = 16'h0200; // 115200 baud, fast smooth scroll when enabled reg btnC = 0, btnU = 0; reg rsrx = 1; reg ps2c = 1, ps2d = 1; @@ -21,6 +22,7 @@ module tb_terminal; ); localparam BIT_NS = 1_000_000_000 / 115200; + integer errors = 0; task uart_send(input [7:0] d); integer i; @@ -31,10 +33,10 @@ module tb_terminal; end endtask - task uart_str(input [8*160-1:0] s); + task uart_str(input [8*200-1:0] s); integer i; begin - for (i = 159; i >= 0; i = i - 1) + for (i = 199; i >= 0; i = i - 1) if (s[i*8 +: 8] != 0) uart_send(s[i*8 +: 8]); end endtask @@ -53,8 +55,16 @@ module tb_terminal; end endtask - // decode what the terminal transmits - reg [8*16-1:0] tx_log = 0; + task key(input [7:0] code); // press and release + begin ps2_send(code); ps2_send(8'hF0); ps2_send(code); end + endtask + + task ext_key(input [7:0] code); + begin ps2_send(8'hE0); ps2_send(code); ps2_send(8'hE0); ps2_send(8'hF0); ps2_send(code); end + endtask + + // everything the terminal transmits + reg [8*32-1:0] tx_log = 0; integer tx_n = 0; initial forever begin : txmon integer i; @@ -64,27 +74,71 @@ module tb_terminal; for (i = 0; i < 8; i = i + 1) begin #(BIT_NS); d[i] = rstx; end #(BIT_NS); if (!rstx) $display("TX framing error"); - $display("%t TX %02h", $time, d); - tx_log = {tx_log[8*15-1:0], d}; + tx_log = {tx_log[8*31-1:0], d}; tx_n = tx_n + 1; end + task expect_tx(input [8*32-1:0] want, input integer n, input [8*40-1:0] what); + reg [8*32-1:0] mask; + begin + #1_200_000; // a 10-byte reply takes ~0.9 ms + mask = {256{1'b1}} >> (256 - 8 * n); + if (tx_n == n && (tx_log & mask) == (want & mask)) + $display("PASS %0s", what); + else begin + $display("FAIL %0s: got %0d bytes %h", what, tx_n, tx_log & mask); + errors = errors + 1; + end + tx_n = 0; + tx_log = 0; + end + endtask + + function [7:0] cellraw(input integer rr, input integer cc); + reg [17:0] v; + begin + v = dut.screen.mem[rr * 80 + cc]; + cellraw = v[7:0]; + end + endfunction + + function [7:0] cellch(input integer rr, input integer cc); + reg [7:0] v; + begin + v = cellraw(rr, cc); + cellch = v < 8'h20 || v >= 8'h7F ? "~" : v; + end + endfunction + task dump_screen; - integer rr, cc, p; + integer rr, cc; reg [8*80-1:0] line; - reg [8:0] cv; begin $display("+--------------------------------------------------------------------------------+"); for (rr = 0; rr < 30; rr = rr + 1) begin - p = (rr + dut.term.top) % 30; - for (cc = 0; cc < 80; cc = cc + 1) begin - cv = dut.screen.mem[p * 80 + cc]; - line[(79 - cc) * 8 +: 8] = cv[8] ? (cv[7:0] == " " ? "#" : cv[7:0]) : cv[7:0]; - end + for (cc = 0; cc < 80; cc = cc + 1) + line[(79 - cc) * 8 +: 8] = cellch(rr, cc); $display("|%s| %0d", line, rr); end $display("+--------------------------------------------------------------------------------+"); - $display("cursor row %0d col %0d, top %0d", dut.term.row, dut.term.col, dut.term.top); + $display("cursor row %0d col %0d, region %0d-%0d", dut.term.row, dut.term.col, + dut.term.stop, dut.term.sbot); + end + endtask + + task expect_row(input integer rr, input [8*40-1:0] want); + integer cc, n; + reg [8*40-1:0] got; + begin + n = 0; + for (cc = 0; cc < 40; cc = cc + 1) if (want[cc*8 +: 8] != 0) n = cc + 1; + got = 0; + for (cc = 0; cc < n; cc = cc + 1) got[(n - 1 - cc) * 8 +: 8] = cellraw(rr, cc); + if (got == want) $display("PASS row %0d", rr); + else begin + $display("FAIL row %0d: want %h got %h", rr, want, got); + errors = errors + 1; + end end endtask @@ -98,20 +152,19 @@ module tb_terminal; smp = 0; end - task capture_frame; + task capture_frame(input [8*16-1:0] name); integer fd, i; begin - @(negedge vs); @(posedge vs); cap = 1; @(negedge vs); cap = 0; - fd = $fopen("frame.ppm", "w"); + fd = $fopen(name, "w"); $fwrite(fd, "P3\n640 480\n15\n"); for (i = 0; i < 640 * 480; i = i + 1) $fwrite(fd, "%0d %0d %0d\n", fb[i][11:8], fb[i][7:4], fb[i][3:0]); $fclose(fd); - $display("wrote frame.ppm"); + $display("wrote %0s", name); end endtask @@ -126,36 +179,75 @@ module tb_terminal; t_vs = $realtime; end - integer k; initial begin #2_000_000; // reset, clear, banner - // keyboard: a, shift+b, Enter, up arrow, ctrl+c - ps2_send(8'h1C); ps2_send(8'hF0); ps2_send(8'h1C); - ps2_send(8'h12); ps2_send(8'h32); ps2_send(8'hF0); ps2_send(8'h32); ps2_send(8'hF0); ps2_send(8'h12); - ps2_send(8'h5A); ps2_send(8'hF0); ps2_send(8'h5A); - ps2_send(8'hE0); ps2_send(8'h75); ps2_send(8'hE0); ps2_send(8'hF0); ps2_send(8'h75); - ps2_send(8'h14); ps2_send(8'h21); ps2_send(8'hF0); ps2_send(8'h21); ps2_send(8'hF0); ps2_send(8'h14); - #200_000; - if (tx_n == 7 && tx_log[8*7-1:0] == {8'h61, 8'h42, 8'h0D, 8'h1B, "[A", 8'h03}) - $display("PASS keyboard -> UART: 61 42 0D 1B 5B 41 03"); - else - $display("FAIL keyboard -> UART: %0d bytes %h", tx_n, tx_log); + // ---- keyboard: a, shift+b, Enter, up, ctrl+c, F5 + key(8'h1C); + ps2_send(8'h12); key(8'h32); ps2_send(8'hF0); ps2_send(8'h12); + key(8'h5A); + ext_key(8'h75); + ps2_send(8'h14); key(8'h21); ps2_send(8'hF0); ps2_send(8'h14); + key(8'h03); + expect_tx({8'h61, 8'h42, 8'h0D, 8'h1B, "[A", 8'h03, 8'h1B, "[15~"}, 12, "keyboard -> UART"); - uart_str("\033[2J\033[HHello, world!\015\012"); - uart_str("tab\tstop\tX\015\012abc\010Z \033[7m reverse \033[0m normal\015\012"); - uart_str("erase-to-end: KEEP\033[4DXXXX\033[4D\033[K\015\012"); - for (k = 0; k < 20; k = k + 1) begin - uart_str("line "); - uart_send("0" + k / 10); - uart_send("0" + k % 10); - uart_str("\015\012"); - end - uart_str("\033[12;30H\033[7m+------------------+\033[13;30H| ANSI positioning |\033[14;30H+------------------+\033[0m"); - uart_str("\033[30;1H\033[?25lwrap test: 0123456789012345678901234567890123456789012345678901234567890123456789"); - #100_000; + // ---- host queries and modes + uart_str("\033[c"); + expect_tx({8'h1B, "[?62;22c"}, 9, "DA reply"); + uart_str("\033[>c"); + expect_tx({8'h1B, "[>1;10;0c"}, 10, "secondary DA reply"); + uart_str("\033[5;10H\033[6n"); + expect_tx({8'h1B, "[05;10R"}, 8, "cursor position report"); + uart_str("\033[?1h"); + ext_key(8'h75); + expect_tx({8'h1B, "OA"}, 3, "DECCKM up arrow"); + uart_str("\033[?1l\033="); + key(8'h69); + expect_tx({8'h1B, "Oq"}, 3, "DECKPAM keypad 1"); + uart_str("\033>\033[1;3q"); + #50_000; + if (led[11:8] == 4'b0101) $display("PASS DECLL LEDs"); + else begin $display("FAIL DECLL LEDs %b", led[11:8]); errors = errors + 1; end + + // ---- screen + uart_str({"\033[2J\033[H\033[1mBold\033[0m \033[31mred \033[32mgreen \033[44;33myellow-on-blue\033[0m ", + "\033[4munderline\033[0m \033[7mreverse\033[0m \033[5mblink\033[0m\015\012"}); + uart_str("\033)0\016lqqqqk\015\012x x\015\012mqqqqj\017\015\012"); + uart_str("abcdefgh\033[4D\033[2@XY\033[3P\015\012"); + uart_str("\033[38;5;196m256-red \033[38;2;0;0;255mrgb-blue \033[90mgrey \033[103;30mon-yellow\033[0m\015\012"); + uart_str("line A\015\012line B\015\012line C\015\012line D\015\012"); + uart_str("\033[8;1H\033[Linserted\033[10;1H\033[M"); + uart_str("\033[13;1Habove region\033[19;1Hbelow region\033[15;18r\033[15;1H"); + uart_str("r1\015\012r2\015\012r3\015\012r4\015\012r5\015\012r6"); + uart_str("\033[1;1H\033[4h>\033[4l\033[30;70Hcorner\033[1;1H"); + #200_000; + expect_row(0, ">Bold red green yellow-on-blue"); + expect_row(1, {8'hDA, 8'hC4, 8'hC4, 8'hC4, 8'hC4, 8'hBF}); + expect_row(2, {8'hB3, " ", 8'hB3}); + expect_row(4, "abcdXYh"); + expect_row(6, "line A"); + expect_row(7, "inserted"); + expect_row(8, "line B"); + expect_row(9, "line D"); + expect_row(12, "above region"); + expect_row(14, "r3"); + expect_row(17, "r6"); + expect_row(18, "below region"); dump_screen; - capture_frame; + capture_frame("frame_1.ppm"); + + // ---- smooth scroll inside the region + uart_str("\033[18;1H\033[?4h\015\012r7"); + capture_frame("frame_2.ppm"); // mid-scroll + #40_000_000; + expect_row(14, "r4"); + expect_row(17, "r7"); + expect_row(18, "below region"); + uart_str("\033[?4l\033[r"); + + dump_screen; + if (errors == 0) $display("ALL TESTS PASSED"); + else $display("%0d TESTS FAILED", errors); $finish; end endmodule diff --git a/terminfo/basys3.ti b/terminfo/basys3.ti new file mode 100644 index 0000000..e8328c7 --- /dev/null +++ b/terminfo/basys3.ti @@ -0,0 +1,20 @@ +# terminfo for the Basys 3 FPGA terminal: a VT220 with 16 ANSI colours, +# underline, blink, 30 lines. +# tic -x -o ~/.terminfo terminfo/basys3.ti then export TERM=basys3 +basys3|Basys 3 FPGA VT220 terminal with ANSI colour, + lines#30, cols#80, + colors#16, pairs#256, + op=\E[39;49m, + setaf=\E[%?%p1%{8}%<%t3%p1%d%e9%p1%{8}%-%d%;m, + setab=\E[%?%p1%{8}%<%t4%p1%d%e10%p1%{8}%-%d%;m, + invis=\E[8m, + rmso=\E[27m, rmul=\E[24m, + sgr0=\E[m\017\E(B, + kbs=\177, + khome=\E[1~, kend=\E[4~, + kf5=\E[15~, + indn=\E[%p1%dS, rin=\E[%p1%dT, + cnl=\E[%p1%dE, cpl=\E[%p1%dF, + hpa=\E[%i%p1%dG, vpa=\E[%i%p1%dd, + cbt=\E[Z, + use=vt220,