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 <noreply@anthropic.com>
This commit is contained in:
barantandClaude Opus 5.5 committed 2026-11-17 11:18:36 -08:00
1 parent a95062f42d
commit 6e54d75c56
10 files changed
+1173 -350

No files matched your search

+113 -33
View File
@@ -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 - **Screen**: VGA port, 640x480 @ 60 Hz, 80 columns x 30 rows, 8x16 CP437 font,
(box-drawing characters included), green/amber/white/cyan. 16 colours, underline, blink, reverse video, optional smooth scrolling.
- **Keyboard**: a USB keyboard in the **USB HID host** port (J2). The board's - **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. PIC24 presents it to the FPGA as a PS/2 keyboard.
- **Serial line**: the micro-USB **PROG/UART** port (the FTDI chip), 8N1. Bytes - **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 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 ```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 ## Controls
| Control | Function | | Control | Function |
@@ -29,35 +63,72 @@ sudo systemctl start serial-getty@ttyUSB1.service
| SW1:0 | Baud: `00` 115200, `01` 9600, `10` 19200, `11` 57600 | | SW1:0 | Baud: `00` 115200, `01` 9600, `10` 19200, `11` 57600 |
| SW2 | Local echo | | SW2 | Local echo |
| SW3 | Received LF also does CR (for hosts that send bare `\n`) | | 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`) | | SW6 | Backspace sends DEL (`7F`) instead of BS (`08`) |
| SW7 | Enter sends CR LF instead of CR | | 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) | | BTNC | Reset (clears screen, shows banner) |
| BTNU | Clear screen | | BTNU | Clear screen |
| LED7:0 | Last byte received | | 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 | | LED12 / 13 / 14 / 15 | Bell / Caps Lock / UART RX activity / keyboard activity |
| 7-segment | Left: last keyboard scan code. Right: last byte sent. | | 7-segment | Left: last keyboard scan code. Right: last byte sent. |
For a Linux getty, set SW6 on (Backspace = DEL).
## Terminal features ## Terminal features
- Control characters: CR, LF, VT, FF (clear screen), BS, TAB (every 8 columns), - **Controls**: BEL, BS, HT, LF/VT/FF, CR, SO/SI, CAN/SUB. Controls act even in
BEL (LED12). the middle of an escape sequence, as on a real VT220.
- Auto-wrap at column 80 (deferred, like a VT100), and hardware scrolling. - **Cursor**:
- VT100/ANSI subset: - Movement: `CUU CUD CUF CUB CNL CPL CHA HPA VPA CUP HVP`
- Cursor movement `ESC[nA/B/C/D`, `ESC[r;cH`, `ESC[r;cf`, `ESC[nd`, `ESC[nG` - Position save and restore: `DECSC`/`DECRC` (`ESC 7`/`ESC 8`), also `CSI s`/`CSI u`
- Erasing with `ESC[nJ` and `ESC[nK` - `IND`, `NEL` and `RI`
- Reverse video with `ESC[7m`, `ESC[27m` and `ESC[0m` - **Editing**:
- Show/hide cursor with `ESC[?25h` and `ESC[?25l` - Insert and delete characters: `ICH DCH ECH`
- `ESC c`, `ESC D`, `ESC E`, `ESC M`, `ESC 7`, `ESC 8` - Insert and delete lines: `IL DL`
- Keyboard: Shift, Caps Lock, Ctrl (`^A`..`^Z`, `^[` etc.). - Erase in display and line: `ED EL`, including `DECSED`/`DECSEL`
- The arrow keys, Home, End, Insert, Delete, PgUp and PgDn send xterm sequences. - Scroll up and down: `SU SD`
- F1 to F4 send `ESC O P` to `ESC O S`. - Insert mode (`IRM`)
- The keypad always types digits. - **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 ### Keyboard
are not implemented, so full-screen programs that need them may draw incorrectly.
| 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 ## Build and program
@@ -74,15 +145,23 @@ license, so use 2025.2 (`export VIVADO=~/vivado/2025.2/Vivado`).
``` ```
```bash ```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 ## Simulation
`sim/tb_terminal.v` covers the whole design. It types keys over PS/2 and checks `sim/tb_terminal.v` covers the whole design:
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 - It types keys over PS/2 and checks the bytes sent on the UART, including
frame as `sim/work/frame.png`. `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 ```bash
VIVADO=~/vivado/2025.2/Vivado ./scripts/sim.sh VIVADO=~/vivado/2025.2/Vivado ./scripts/sim.sh
@@ -95,11 +174,12 @@ which Vivado needs on newer distros.
| Path | Contents | | Path | Contents |
|---|---| |---|---|
| `rtl/basys3_terminal.v` | Top level: reset, switches, FIFOs, banner, LEDs | | `rtl/basys3_terminal.v` | Top level: reset, switches, FIFOs, flow control, banner, LEDs |
| `rtl/term_ctrl.v` | Terminal engine (control codes, ANSI parser, scrolling) | | `rtl/term_ctrl.v` | VT220 engine (parser, modes, attributes, copy/fill engine, smooth scroll) |
| `rtl/text_display.v` | VGA timing, character RAM, font lookup | | `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 | | `rtl/ps2_keyboard.v` | PS/2 receiver and scan code set 2 to ASCII / VT220 keys |
| `rtl/uart.v` | UART receiver and transmitter | | `rtl/uart.v` | UART receiver and transmitter |
| `rtl/fifo.v`, `rtl/sevenseg.v` | Helpers | | `rtl/fifo.v`, `rtl/sevenseg.v` | Helpers |
| `font/font.mem` | 256x16 font ROM, made by `tools/psf2mem.py` from the Linux console fonts | | `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 | | `constraints/basys3.xdc` | Pin assignments |
+111 -51
View File
@@ -1,19 +1,24 @@
`timescale 1ns / 1ps `timescale 1ns / 1ps
// Basys 3 ASCII terminal. // Basys 3 VT220 terminal with ANSI colour.
// Keyboard (USB HID port) -> UART TX (USB-UART port) // 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) // SW1:0 baud rate: 00 115200, 01 9600, 10 19200, 11 57600 (8N1)
// SW2 local echo // SW2 local echo
// SW3 received LF also does CR // 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) // SW6 Backspace sends DEL (7F) instead of BS (08)
// SW7 Enter sends CR LF instead of CR // 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 // BTNC reset BTNU clear screen
// LED7:0 last byte received LED12 bell LED13 Caps Lock // LED7:0 last byte received LED11:8 VT220 LEDs L4..L1 (DECLL)
// LED14 UART RX activity LED15 keyboard activity // LED12 bell LED13 Caps Lock LED14 UART RX activity LED15 keyboard activity
// 7-seg last scan code (left), last byte sent (right) // 7-seg last scan code (left), last byte sent (right)
//
// 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 #( module basys3_terminal #(
parameter FONT_FILE = "font.mem", parameter FONT_FILE = "font.mem",
@@ -37,6 +42,8 @@ module basys3_terminal #(
input PS2Clk, input PS2Clk,
input PS2Data input PS2Data
); );
localparam [7:0] XON = 8'h11, XOFF = 8'h13;
// ---------------------------------------------------------------- reset // ---------------------------------------------------------------- reset
reg [1:0] btn_sync; reg [1:0] btn_sync;
reg [15:0] por = 16'd0; reg [15:0] por = 16'd0;
@@ -48,10 +55,10 @@ module basys3_terminal #(
rst <= !por[15]; rst <= !por[15];
end end
reg [7:0] sw_r; reg [9:0] sw_r;
reg [1:0] clr_sync; reg [1:0] clr_sync;
always @(posedge clk) begin always @(posedge clk) begin
sw_r <= sw[7:0]; sw_r <= sw[9:0];
clr_sync <= {clr_sync[0], btnU}; clr_sync <= {clr_sync[0], btnU};
end end
@@ -64,12 +71,22 @@ module basys3_terminal #(
2'b11: baud_div <= CLK_HZ / 57600; 2'b11: baud_div <= CLK_HZ / 57600;
endcase 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 // ------------------------------------------------------------- keyboard
wire [7:0] scan; wire [7:0] scan;
wire scan_valid; wire scan_valid;
wire [7:0] key; wire [7:0] key;
wire key_valid; wire key_valid;
wire caps; wire caps;
wire cursor_app, keypad_app, lnm;
ps2_rx kbd_rx ( ps2_rx kbd_rx (
.clk(clk), .rst(rst), .ps2_clk(PS2Clk), .ps2_data(PS2Data), .clk(clk), .rst(rst), .ps2_clk(PS2Clk), .ps2_data(PS2Data),
@@ -78,27 +95,69 @@ module basys3_terminal #(
ps2_keymap kbd_map ( ps2_keymap kbd_map (
.clk(clk), .rst(rst), .code(scan), .code_valid(scan_valid), .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) .ascii(key), .ascii_valid(key_valid), .caps(caps)
); );
// ------------------------------------------------------------- transmit // ------------------------------------------------------------- transmit
wire [7:0] kq; // Sources: flow control (first), then keyboard and terminal replies. The
wire kq_empty; // 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; wire tx_busy;
reg tx_start; reg tx_start, kq_rd, rq_rd, from_resp;
reg [7:0] tx_byte; reg [7:0] tx_byte;
reg flow_pend;
reg [7:0] flow_byte;
fifo #(.W(8), .A(4)) key_fifo ( fifo #(.W(8), .A(4)) key_fifo (
.clk(clk), .rst(rst), .din(key), .wr(key_valid), .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 always @(posedge clk) begin
tx_start <= 1'b0; tx_start <= 1'b0;
if (!kq_empty && !tx_busy && !tx_start && !rst) begin kq_rd <= 1'b0;
tx_start <= 1'b1; rq_rd <= 1'b0;
tx_byte <= kq; 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
end end
@@ -118,10 +177,11 @@ module basys3_terminal #(
// Start-up banner, fed in like received text. Bytes are read from the // 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. // high end of the string; the unused high bytes are zero and skipped.
localparam [8*160-1:0] BANNER = { localparam [8*200-1:0] BANNER = {
"Basys 3 ASCII Terminal 80x30\015\012", "\033[1mBasys 3 VT220 Terminal\033[0m 80x30 ",
"UART RX -> screen, keyboard -> UART TX\015\012", "\033[31mc\033[33mo\033[32ml\033[36mo\033[34mu\033[35mr\033[0m ",
"SW1:0 baud 00=115200 01=9600 10=19200 11=57600 SW2 echo\015\012\012"}; "\033[4munderline\033[0m\015\012",
"UART RX -> screen, keyboard -> UART TX TERM=basys3 or vt220\015\012\012"};
reg [7:0] ban_i; reg [7:0] ban_i;
wire [7:0] ban_c = BANNER[ban_i*8 +: 8]; wire [7:0] ban_c = BANNER[ban_i*8 +: 8];
@@ -134,20 +194,20 @@ module basys3_terminal #(
always @(posedge clk) begin always @(posedge clk) begin
tin_wr <= 1'b0; tin_wr <= 1'b0;
if (tx_start && sw_r[2]) begin if (kq_rd && sw_r[2]) begin
echo_pend <= 1'b1; echo_pend <= 1'b1;
echo_byte <= kq; echo_byte <= kq;
end end
if (rst) begin if (rst) begin
echo_pend <= 1'b0; echo_pend <= 1'b0;
ban_i <= 8'd159; ban_i <= 8'd199;
end else if (rx_valid) begin end else if (rx_valid) begin
tin <= rx_byte; tin <= rx_byte;
tin_wr <= 1'b1; tin_wr <= 1'b1;
end else if (echo_pend && !tin_full) begin end else if (echo_pend && !tin_full) begin
tin <= echo_byte; tin <= echo_byte;
tin_wr <= 1'b1; 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 end else if (ban_i != 8'hFF && !tin_full && !tin_wr) begin
ban_i <= ban_i - 1'b1; ban_i <= ban_i - 1'b1;
if (ban_c != 0) begin if (ban_c != 0) begin
@@ -161,49 +221,49 @@ module basys3_terminal #(
wire tq_empty; wire tq_empty;
wire tq_rd; 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), .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 // ------------------------------------------------------------- terminal
wire we; wire [11:0] t_addr;
wire [11:0] waddr; wire t_we;
wire [8:0] wdata; wire [17:0] t_wdata, t_rdata;
wire [4:0] top, cur_row; wire [4:0] cur_row, stop, sbot, sm_off;
wire [6:0] cur_col; wire [6:0] cur_col;
wire cur_vis; wire cur_vis, sm_on, sm_dir;
wire [3:0] dec_leds;
wire bell; wire bell;
wire frame_tick;
term_ctrl term ( term_ctrl term (
.clk(clk), .rst(rst), .in_byte(tq), .in_empty(tq_empty), .in_rd(tq_rd), .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]), .lf_cr(sw_r[3]), .clear_req(clr_sync[1]), .def_fg(def_fg),
.we(we), .waddr(waddr), .wdata(wdata), .smooth_sw(sw_r[8]), .smooth_fast(sw_r[9]), .frame_tick(frame_tick),
.top(top), .row(cur_row), .col(cur_col), .cur_vis(cur_vis), .bell(bell) .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 [11:0] v_addr;
wire [8:0] rdata; wire [17:0] v_data;
char_ram screen ( char_ram screen (
.clk(clk), .we(we), .waddr(waddr), .wdata(wdata), .clk(clk),
.raddr(raddr), .rdata(rdata) .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 ( text_display #(.FONT_FILE(FONT_FILE)) video (
.clk(clk), .rst(rst), .top(top), .cur_row(cur_row), .cur_col(cur_col), .clk(clk), .rst(rst), .cur_row(cur_row), .cur_col(cur_col), .cur_vis(cur_vis),
.cur_vis(cur_vis), .fg(fg), .bg(12'h000), .stop(stop), .sbot(sbot), .sm_on(sm_on), .sm_dir(sm_dir), .sm_off(sm_off),
.ram_addr(raddr), .ram_data(rdata), .ram_addr(v_addr), .ram_data(v_data),
.red(vgaRed), .green(vgaGreen), .blue(vgaBlue), .hsync(Hsync), .vsync(Vsync) .red(vgaRed), .green(vgaGreen), .blue(vgaBlue), .hsync(Hsync), .vsync(Vsync),
.frame_tick(frame_tick)
); );
// ----------------------------------------------------- status displays // ----------------------------------------------------- status displays
@@ -215,13 +275,13 @@ module basys3_terminal #(
always @(posedge clk) begin always @(posedge clk) begin
if (rx_valid) last_rx <= rx_byte; if (rx_valid) last_rx <= rx_byte;
if (scan_valid) last_scan <= scan; 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); rx_led <= rx_valid ? 23'h7FFFFF : rx_led - (rx_led != 0);
kb_led <= scan_valid ? 23'h7FFFFF : kb_led - (kb_led != 0); kb_led <= scan_valid ? 23'h7FFFFF : kb_led - (kb_led != 0);
bell_led <= bell ? 23'h7FFFFF : bell_led - (bell_led != 0); bell_led <= bell ? 23'h7FFFFF : bell_led - (bell_led != 0);
end 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; assign dp = 1'b1;
sevenseg digits (.clk(clk), .value({last_scan, last_tx}), .seg(seg), .an(an)); sevenseg digits (.clk(clk), .value({last_scan, last_tx}), .seg(seg), .an(an));
+3 -1
View File
@@ -12,7 +12,8 @@ module fifo #(
input rd, input rd,
output [W-1:0] dout, output [W-1:0] dout,
output empty, output empty,
output full output full,
output [A:0] count
); );
reg [W-1:0] mem [0:(1<<A)-1]; reg [W-1:0] mem [0:(1<<A)-1];
reg [A:0] wp, rp; reg [A:0] wp, rp;
@@ -20,6 +21,7 @@ module fifo #(
assign empty = wp == rp; assign empty = wp == rp;
assign full = wp[A] != rp[A] && wp[A-1:0] == rp[A-1:0]; assign full = wp[A] != rp[A] && wp[A-1:0] == rp[A-1:0];
assign dout = mem[rp[A-1:0]]; assign dout = mem[rp[A-1:0]];
assign count = wp - rp;
always @(posedge clk) always @(posedge clk)
if (wr && !full) mem[wp[A-1:0]] <= din; if (wr && !full) mem[wp[A-1:0]] <= din;
+81 -41
View File
@@ -58,8 +58,13 @@ module ps2_rx (
end end
endmodule endmodule
// Turns scan codes into ASCII bytes. Cursor and editing keys send VT100/xterm // Turns scan codes into ASCII bytes and VT220 key sequences:
// escape sequences; F1-F4 send ESC O P..S. // 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 ( module ps2_keymap (
input clk, input clk,
input rst, input rst,
@@ -67,17 +72,19 @@ module ps2_keymap (
input code_valid, input code_valid,
input bs_del, // Backspace sends DEL (7F) instead of BS (08) input bs_del, // Backspace sends DEL (7F) instead of BS (08)
input enter_crlf, // Enter sends CR LF instead of CR 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 [7:0] ascii,
output reg ascii_valid, output reg ascii_valid,
output reg caps output reg caps
); );
localparam [7:0] ESC = 8'h1B; localparam [7:0] ESC = 8'h1B;
reg e0, f0; reg e0, f0;
reg [2:0] skip; reg [2:0] skip;
reg lsh, rsh, lct, rct; reg lsh, rsh, lct, rct, alt;
reg [7:0] s0, s1, s2, s3; reg [47:0] sq;
reg [2:0] cnt; reg [2:0] cnt;
// {unshifted, shifted} // {unshifted, shifted}
function [15:0] keymap(input [7:0] c); function [15:0] keymap(input [7:0] c);
@@ -100,7 +107,7 @@ module ps2_keymap (
8'h2A: keymap = "vV"; 8'h32: keymap = "bB"; 8'h31: keymap = "nN"; 8'h2A: keymap = "vV"; 8'h32: keymap = "bB"; 8'h31: keymap = "nN";
8'h3A: keymap = "mM"; 8'h41: keymap = ",<"; 8'h49: keymap = ".>"; 8'h3A: keymap = "mM"; 8'h41: keymap = ",<"; 8'h49: keymap = ".>";
8'h4A: keymap = "/?"; 8'h29: keymap = " "; 8'h76: keymap = {ESC, ESC}; 8'h4A: keymap = "/?"; 8'h29: keymap = " "; 8'h76: keymap = {ESC, ESC};
// keypad, always as if Num Lock were on // keypad, as if Num Lock were on
8'h70: keymap = "00"; 8'h69: keymap = "11"; 8'h72: keymap = "22"; 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'h7A: keymap = "33"; 8'h6B: keymap = "44"; 8'h73: keymap = "55";
8'h74: keymap = "66"; 8'h6C: keymap = "77"; 8'h75: keymap = "88"; 8'h74: keymap = "66"; 8'h6C: keymap = "77"; 8'h75: keymap = "88";
@@ -110,46 +117,72 @@ module ps2_keymap (
endcase endcase
endfunction 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 shift = lsh | rsh;
wire ctrl = lct | rct; wire ctrl = lct | rct;
wire [15:0] km = keymap(code); wire [15:0] km = keymap(code);
wire letter = km[15:8] >= "a" && km[15:8] <= "z"; 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] 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 // bytes a key press produces, first byte in k_seq[47:40]
reg [2:0] k_len; reg [2:0] k_len;
reg [7:0] k0, k1, k2, k3; reg [47:0] k_seq;
always @* begin always @* begin
{k_len, k0, k1, k2, k3} = 35'd0; k_len = 3'd0;
k_seq = 48'd0;
if (e0) case (code) if (e0) case (code)
8'h75: {k_len, k0, k1, k2} = {3'd3, ESC, "[A"}; // up 8'h75: {k_len, k_seq} = {3'd3, ESC, ckey, "A", 24'd0}; // up
8'h72: {k_len, k0, k1, k2} = {3'd3, ESC, "[B"}; // down 8'h72: {k_len, k_seq} = {3'd3, ESC, ckey, "B", 24'd0}; // down
8'h74: {k_len, k0, k1, k2} = {3'd3, ESC, "[C"}; // right 8'h74: {k_len, k_seq} = {3'd3, ESC, ckey, "C", 24'd0}; // right
8'h6B: {k_len, k0, k1, k2} = {3'd3, ESC, "[D"}; // left 8'h6B: {k_len, k_seq} = {3'd3, ESC, ckey, "D", 24'd0}; // left
8'h6C: {k_len, k0, k1, k2} = {3'd3, ESC, "[H"}; // home 8'h6C: {k_len, k_seq} = {3'd4, ESC, "[1~", 16'd0}; // home (Find)
8'h69: {k_len, k0, k1, k2} = {3'd3, ESC, "[F"}; // end 8'h70: {k_len, k_seq} = {3'd4, ESC, "[2~", 16'd0}; // insert
8'h70: {k_len, k0, k1, k2, k3} = {3'd4, ESC, "[2~"}; // insert 8'h71: {k_len, k_seq} = {3'd4, ESC, "[3~", 16'd0}; // delete (Remove)
8'h71: {k_len, k0, k1, k2, k3} = {3'd4, ESC, "[3~"}; // delete 8'h69: {k_len, k_seq} = {3'd4, ESC, "[4~", 16'd0}; // end (Select)
8'h7D: {k_len, k0, k1, k2, k3} = {3'd4, ESC, "[5~"}; // page up 8'h7D: {k_len, k_seq} = {3'd4, ESC, "[5~", 16'd0}; // page up
8'h7A: {k_len, k0, k1, k2, k3} = {3'd4, ESC, "[6~"}; // page down 8'h7A: {k_len, k_seq} = {3'd4, ESC, "[6~", 16'd0}; // page down
8'h4A: {k_len, k0} = {3'd1, "/"}; // keypad / 8'h4A: {k_len, k_seq} = keypad_app ? {3'd3, ESC, "Oo", 24'd0} : {3'd1, "/", 40'd0};
8'h5A: {k_len, k0, k1} = enter_crlf ? {3'd2, 8'h0D, 8'h0A} : {3'd1, 8'h0D, 8'h00}; 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: ; default: ;
endcase endcase
else case (code) else case (code)
8'h66: {k_len, k0} = {3'd1, bs_del ? 8'h7F : 8'h08}; 8'h66: {k_len, k_seq} = {3'd1, bs_del ? 8'h7F : 8'h08, 40'd0};
8'h5A: {k_len, k0, k1} = enter_crlf ? {3'd2, 8'h0D, 8'h0A} : {3'd1, 8'h0D, 8'h00}; 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, k0, k1, k2} = {3'd3, ESC, "OP"}; // F1 8'h05: {k_len, k_seq} = {3'd3, ESC, "OP", 24'd0}; // F1
8'h06: {k_len, k0, k1, k2} = {3'd3, ESC, "OQ"}; // F2 8'h06: {k_len, k_seq} = {3'd3, ESC, "OQ", 24'd0}; // F2
8'h04: {k_len, k0, k1, k2} = {3'd3, ESC, "OR"}; // F3 8'h04: {k_len, k_seq} = {3'd3, ESC, "OR", 24'd0}; // F3
8'h0C: {k_len, k0, k1, k2} = {3'd3, ESC, "OS"}; // F4 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: 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; k_len = 3'd1;
if (!ctrl) k0 = ch; if (!ctrl) k_seq[47:40] = ch;
else if (ch == " ") k0 = 8'h00; else if (ch == " ") k_seq[47:40] = 8'h00;
else if (ch >= 8'h40) k0 = ch & 8'h1F; // ^A..^Z, ^[ ^\ ^] ^^ ^_ else if (ch >= 8'h40) k_seq[47:40] = ch & 8'h1F; // ^A..^Z, ^[ ^\ ^] ^^ ^_
else k0 = ch; else k_seq[47:40] = ch;
end end
endcase endcase
end end
@@ -157,14 +190,14 @@ module ps2_keymap (
always @(posedge clk) begin always @(posedge clk) begin
ascii_valid <= 1'b0; ascii_valid <= 1'b0;
if (rst) begin 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; skip <= 3'd0;
cnt <= 3'd0; cnt <= 3'd0;
end else begin end else begin
if (cnt != 0) begin if (cnt != 0) begin
ascii <= s0; ascii <= sq[47:40];
ascii_valid <= 1'b1; ascii_valid <= 1'b1;
{s0, s1, s2} <= {s1, s2, s3}; sq <= sq << 8;
cnt <= cnt - 1'b1; cnt <= cnt - 1'b1;
end end
if (code_valid) begin if (code_valid) begin
@@ -183,11 +216,18 @@ module ps2_keymap (
else if (code == 8'h59 && !e0) rsh <= !f0; else if (code == 8'h59 && !e0) rsh <= !f0;
else if (code == 8'h14) begin else if (code == 8'h14) begin
if (e0) rct <= !f0; else lct <= !f0; 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; if (!f0) caps <= !caps;
end else if (!f0 && k_len != 0) begin end else if (!f0 && k_len != 0) begin
{s0, s1, s2, s3} <= {k0, k1, k2, k3}; if (alt && k_len != 3'd6) begin
cnt <= k_len; 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 end
+635 -149
View File
@@ -1,15 +1,20 @@
`timescale 1ns / 1ps `timescale 1ns / 1ps
// Terminal engine: consumes received bytes and updates the 80x30 character // VT220 terminal engine with ANSI colour. Consumes received bytes and keeps
// buffer. Supports CR LF BS TAB BEL FF, deferred auto-wrap, hardware scrolling // the 80x30 new_cell buffer (row-major, address = row*80 + col) up to date.
// (the buffer is a ring of rows; `top` is the physical row shown first) and a //
// VT100/ANSI subset: // Cell format: {blink, underline, bg[3:0], fg[3:0], CP437 code}. Reverse,
// ESC [ n A/B/C/D cursor up/down/right/left ESC [ r;c H / f position // bold and concealed text are resolved into the colours when a new_cell is written.
// ESC [ n d / G row / column absolute ESC [ n J / K erase //
// ESC [ n m 0 normal, 7 reverse, 27 not reverse // Supported (beyond C0 controls):
// ESC [ ? 25 h/l show/hide cursor // ESC 7 8 D E H M Z c = > ESC ( ) <0|A|B> ESC # 8
// ESC c reset ESC D index ESC E next line ESC M reverse index ESC 7/8 save/restore // 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
// Cell format in the buffer: {reverse, CP437 code}. // 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 ( module term_ctrl (
input clk, input clk,
@@ -17,45 +22,102 @@ module term_ctrl (
input [7:0] in_byte, input [7:0] in_byte,
input in_empty, input in_empty,
output reg in_rd, output reg in_rd,
input lf_cr, // LF also returns the carriage input lf_cr, // LF also returns the carriage
input clear_req, 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 we,
output reg [11:0] waddr, output reg [17:0] wdata,
output reg [8:0] wdata, input [17:0] rdata,
output reg [4:0] top, // to the display
output reg [4:0] row, output reg [4:0] row,
output reg [6:0] col, output reg [6:0] col,
output reg cur_vis, 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 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 [3:0] S_IDLE = 4'd0, S_DISP = 4'd1, S_FILL = 4'd2, S_CRD = 4'd3,
localparam P_NORM = 2'd0, P_ESC = 2'd1, P_CSI = 2'd2; 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 [3:0] st, ret_st, sm_ret;
reg [7:0] c; reg [1:0] ps;
reg pend; // a character went in the last column reg [7:0] c;
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;
function [11:0] phys(input [4:0] r, input [6:0] cc, input [4:0] t); // modes and attributes
reg [5:0] s; reg pend, awm, irm, om, sclm;
reg [4:0] pr; reg bold, ul, blink, rev, conceal;
begin reg [3:0] fg, bg;
s = r + t; reg [1:0] g0, g1; // 0 ASCII, 1 DEC special graphics, 2 UK
pr = (s >= 6'd30) ? s - 6'd30 : s; reg gl; // 0 = G0 active, 1 = G1 (SO)
phys = {1'b0, pr, 6'b0} + {3'b0, pr, 4'b0} + {5'b0, cc}; // pr*80 + cc reg [79:0] tabs;
end
// 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 endfunction
// CSI parameter accumulate, saturating at 255
function [7:0] acc(input [7:0] p, input [7:0] d); function [7:0] acc(input [7:0] p, input [7:0] d);
reg [11:0] v; reg [11:0] v;
begin begin
@@ -64,172 +126,596 @@ module term_ctrl (
end end
endfunction endfunction
wire [7:0] n0 = p0 == 0 ? 8'd1 : p0; // DEC special graphics to CP437
wire [8:0] row_dn = row + n0; function [7:0] gfx(input [7:0] ch);
wire [8:0] col_rt = col + n0; case (ch)
wire [4:0] row_p0 = p0 == 0 ? 5'd0 : p0 > 30 ? LAST_ROW : p0 - 1'b1; 8'h5F: gfx = 8'h20; 8'h60: gfx = 8'h04; 8'h61: gfx = 8'hB1;
wire [6:0] col_p0 = p0 == 0 ? 7'd0 : p0 > 80 ? LAST_COL : p0 - 1'b1; 8'h66: gfx = 8'hF8; 8'h67: gfx = 8'hF1; 8'h6A: gfx = 8'hD9;
wire [6:0] col_p1 = p1 == 0 ? 7'd0 : p1 > 80 ? LAST_COL : p1 - 1'b1; 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 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; ret_st <= rs;
st <= S_FILL; st <= S_OP; // decided next clock, off the critical path
end end
endtask endtask
// line feed; scrolls by advancing `top` and blanking the new bottom row task fill(input [11:0] fa, input [11:0] fcnt);
task line_feed(input [1:0] rs); 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 begin
if (row == LAST_ROW) begin h = b - t + 1'b1;
top <= top == LAST_ROW ? 5'd0 : top + 1'b1; k = n > h ? h : n[5:0];
fill(LAST_ROW, 0, LAST_ROW, LAST_COL, rs); 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 end else begin
row <= row + 1'b1; if (row != LAST_ROW) row <= row + 1'b1;
st <= rs; st <= rs;
end end
end end
endtask 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 task reset_attrs;
in_rd <= 1'b0; begin
we <= 1'b0; {bold, ul, blink, rev, conceal} <= 5'b0;
bell <= 1'b0; fg <= def_fg;
if (rst) begin bg <= 4'd0;
ps <= P_NORM; end
{top, row, col, sv_row, sv_col} <= 0; endtask
{pend, rev} <= 2'b00;
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; 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) end else case (st)
S_IDLE: S_IDLE:
if (clear_req) begin if (clear_req) begin
{row, col, pend} <= 0; {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 end else if (!in_empty && !in_rd) begin
c <= in_byte; c <= in_byte;
in_rd <= 1'b1; in_rd <= 1'b1;
st <= S_DISP; st <= S_DISP;
end 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; we <= 1'b1;
waddr <= phys(fr, fc, top); c_src <= c_dir ? c_src - 1'b1 : c_src + 1'b1;
wdata <= 9'h020; c_dst <= c_dir ? c_dst - 1'b1 : c_dst + 1'b1;
if (fr == er && fc == ec) c_cnt <= c_cnt - 1'b1;
st <= ret_st; st <= c_cnt != 1 ? S_CRD : f_cnt != 0 ? S_FILL : ret_st;
else if (fc == LAST_COL) begin end
fc <= 7'd0; S_FILL: begin
fr <= fr + 1'b1; addr <= f_addr;
end else wdata <= f_cell;
fc <= fc + 1'b1; 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 end
S_DISP: begin S_DISP: begin
st <= S_IDLE; st <= S_IDLE;
case (ps) if (c == 8'h1B) begin
P_NORM: ps <= P_ESC;
if (c >= 8'h20 && c != 8'h7F) begin end else if (c == 8'h18 || c == 8'h1A) begin
if (pend) begin ps <= P_NORM; // CAN / SUB cancel a sequence
// wrap first, then come back to print this byte end else if (c < 8'h20) begin
pend <= 1'b0; // C0 controls act immediately, even inside an escape sequence
col <= 7'd0; case (c)
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)
8'h07: bell <= 1'b1; 8'h07: bell <= 1'b1;
8'h08: begin pend <= 1'b0; if (col != 0) col <= col - 1'b1; end 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'h09: begin pend <= 1'b0; col <= next_tab(tabs, col); end
8'h0A, 8'h0B: begin 8'h0A, 8'h0B, 8'h0C: begin
pend <= 1'b0; pend <= 1'b0;
if (lf_cr) col <= 7'd0; if (lf_cr || lnm) col <= 7'd0;
line_feed(S_IDLE); line_feed(S_IDLE);
end 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'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: ; default: ;
endcase 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 P_ESC: begin
ps <= P_NORM; ps <= P_NORM;
case (c) if (c >= 8'h20 && c <= 8'h2F) begin
inter <= c;
ps <= P_ESCI;
end else case (c)
"[": begin "[": begin
ps <= P_CSI; ps <= P_CSI;
{p0, p1, pidx, priv} <= 0; for (i = 0; i < 16; i = i + 1) par[i] <= 8'd0;
end {pidx, priv, gtm, ign} <= 0;
"c": begin cint <= 8'd0;
{row, col, pend, rev} <= 0;
cur_vis <= 1'b1;
fill(0, 0, LAST_ROW, LAST_COL, S_IDLE);
end end
"D": begin pend <= 1'b0; line_feed(S_IDLE); end "D": begin pend <= 1'b0; line_feed(S_IDLE); end
"E": begin pend <= 1'b0; col <= 7'd0; 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 "M": begin pend <= 1'b0; reverse_index; end
"7": begin sv_row <= row; sv_col <= col; end "H": tabs[col] <= 1'b1;
"8": begin pend <= 1'b0; row <= sv_row; col <= sv_col; end "7": begin
8'h1B: ps <= P_ESC; {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: ; default: ;
endcase endcase
end end
P_CSI: P_CSI:
if (c >= "0" && c <= "9") begin if (c >= "0" && c <= "9")
if (pidx == 0) p0 <= acc(p0, c); par[pidx] <= acc(par[pidx], c);
else if (pidx == 1) p1 <= acc(p1, c); else if (c == ";" || c == ":") begin
end else if (c == ";") begin if (pidx != 4'd15) pidx <= pidx + 1'b1;
if (pidx != 2'd3) pidx <= pidx + 1'b1; end else if (c == "?")
end else if (c == "?") begin
priv <= 1'b1; priv <= 1'b1;
end else if (c == 8'h18 || c == 8'h1A) begin else if (c == ">")
ps <= P_NORM; // CAN / SUB abort the sequence gtm <= 1'b1;
end else if (c >= 8'h40 && c <= 8'h7E) begin 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; ps <= P_NORM;
if (priv) begin if (ign) begin
if (p0 == 25 && c == "h") cur_vis <= 1'b1; end else if (cint == "!") begin
if (p0 == 25 && c == "l") cur_vis <= 1'b0; if (c == "p") soft_reset;
end else case (c) end else if (cint != 0) begin
"A": begin pend <= 1'b0; row <= row > n0 ? row - n0 : 5'd0; end end else if (gtm) begin
"B": begin pend <= 1'b0; row <= row_dn > LAST_ROW ? LAST_ROW : row_dn[4:0]; end if (c == "c") respond({8'h1B, "[>1;10;0c", 16'd0}, 4'd10);
"C": begin pend <= 1'b0; col <= col_rt > LAST_COL ? LAST_COL : col_rt[6:0]; end end else if (priv) begin
"D": begin pend <= 1'b0; col <= col > n0 ? col - n0 : 7'd0; end case (c)
"H", "f": begin pend <= 1'b0; row <= row_p0; col <= col_p1; end "h": plist(K_SET);
"d": begin pend <= 1'b0; row <= row_p0; end "l": plist(K_RST);
"G", "`": begin pend <= 1'b0; col <= col_p0; end "J": begin
"J": case (p0) pend <= 1'b0;
8'd0: fill(row, col, LAST_ROW, LAST_COL, S_IDLE); if (p0 == 0) fill(here, CELLS - here);
8'd1: fill(0, 0, row, col, S_IDLE); else if (p0 == 1) fill(12'd0, here + 1'b1);
8'd2, 8'd3: fill(0, 0, LAST_ROW, LAST_COL, S_IDLE); else if (p0 == 2) fill(12'd0, CELLS);
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;
end 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 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: ; default: ;
endcase endcase
end end
+65 -27
View File
@@ -1,27 +1,34 @@
`timescale 1ns / 1ps `timescale 1ns / 1ps
// 640x480 @ 60 Hz VGA text display: 80x30 cells of 8x16 pixels. // 640x480 @ 60 Hz VGA text display: 80x30 cells of 8x16 pixels, 16 colours,
// Runs on the 100 MHz clock with a 25 MHz pixel enable; the character RAM // underline, blink, block cursor and smooth-scroll offset for the scroll region.
// and font ROM reads finish well inside the four clocks of each pixel. // 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 #( module text_display #(
parameter FONT_FILE = "font.mem" parameter FONT_FILE = "font.mem"
) ( ) (
input clk, input clk,
input rst, input rst,
input [4:0] top, // physical row shown at the top of the screen
input [4:0] cur_row, input [4:0] cur_row,
input [6:0] cur_col, input [6:0] cur_col,
input cur_vis, input cur_vis,
input [11:0] fg, // 4:4:4 RGB input [4:0] stop, // scroll region (rows, inclusive)
input [11:0] bg, 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, 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] red,
output reg [3:0] green, output reg [3:0] green,
output reg [3:0] blue, output reg [3:0] blue,
output reg hsync, 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 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; localparam V_VIS = 480, V_FP = 10, V_SYNC = 2, V_TOTAL = 525;
@@ -33,6 +40,7 @@ module text_display #(
always @(posedge clk) begin always @(posedge clk) begin
div <= rst ? 2'd0 : div + 1'b1; div <= rst ? 2'd0 : div + 1'b1;
frame_tick <= tick && h == 0 && v == V_VIS;
if (rst) begin if (rst) begin
h <= 0; h <= 0;
v <= 0; v <= 0;
@@ -49,46 +57,76 @@ module text_display #(
end end
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 [6:0] tcol = h[9:3];
wire [4:0] trow = v[8:4]; assign ram_addr = row_base + {5'b0, tcol};
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};
reg [7:0] font [0:4095]; reg [7:0] font [0:4095];
reg [7:0] glyph; reg [7:0] glyph;
initial $readmemh(FONT_FILE, font); 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 visible = h < H_VIS && v < V_VIS;
wire cursor = cur_vis && frame[5] && tcol == cur_col && trow == cur_row; wire cursor = cur_vis && frame[5] && tcol == cur_col && srow == cur_row;
wire on = glyph[~h[2:0]] ^ ram_data[8] ^ cursor; 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 always @(posedge clk) begin
if (tick) 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); 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); vsync <= !(v >= V_VIS + V_FP && v < V_VIS + V_FP + V_SYNC);
end end
end end
endmodule 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 ( module char_ram (
input clk, input clk,
input we, input [11:0] a_addr,
input [11:0] waddr, input a_we,
input [8:0] wdata, input [17:0] a_wdata,
input [11:0] raddr, output reg [17:0] a_rdata,
output reg [8:0] 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; 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 always @(posedge clk) begin
if (we) mem[waddr] <= wdata; if (a_we) mem[a_addr] <= a_wdata;
rdata <= mem[raddr]; a_rdata <= mem[a_addr];
end end
always @(posedge clk) b_rdata <= mem[b_addr];
endmodule endmodule
+3 -1
View File
@@ -32,7 +32,9 @@ if {$mode eq "flash"} {
delete_hw_cfgmem $mem delete_hw_cfgmem $mem
} }
if {!$done} { error "flash part not recognised" } 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 { } else {
set_property PROGRAM.FILE $root/build/basys3_terminal.bit $dev set_property PROGRAM.FILE $root/build/basys3_terminal.bit $dev
program_hw_devices $dev program_hw_devices $dev
+4 -1
View File
@@ -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 xelab -debug off tb_terminal -s tb_terminal
xsim tb_terminal -R xsim tb_terminal -R
if command -v python3 >/dev/null; then 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 fi
+138 -46
View File
@@ -1,12 +1,13 @@
`timescale 1ns / 1ps `timescale 1ns / 1ps
// Whole-design test: keystrokes on PS/2 -> UART TX bytes, UART RX text -> // Whole-design test: keystrokes on PS/2 -> UART TX bytes, host queries ->
// screen buffer (printed as text) and one VGA frame (written as frame.ppm). // replies, UART RX text -> screen buffer (printed as text) and VGA frames
// (written as frame_*.ppm).
module tb_terminal; module tb_terminal;
reg clk = 0; reg clk = 0;
always #5 clk = ~clk; 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 btnC = 0, btnU = 0;
reg rsrx = 1; reg rsrx = 1;
reg ps2c = 1, ps2d = 1; reg ps2c = 1, ps2d = 1;
@@ -21,6 +22,7 @@ module tb_terminal;
); );
localparam BIT_NS = 1_000_000_000 / 115200; localparam BIT_NS = 1_000_000_000 / 115200;
integer errors = 0;
task uart_send(input [7:0] d); task uart_send(input [7:0] d);
integer i; integer i;
@@ -31,10 +33,10 @@ module tb_terminal;
end end
endtask endtask
task uart_str(input [8*160-1:0] s); task uart_str(input [8*200-1:0] s);
integer i; integer i;
begin 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]); if (s[i*8 +: 8] != 0) uart_send(s[i*8 +: 8]);
end end
endtask endtask
@@ -53,8 +55,16 @@ module tb_terminal;
end end
endtask endtask
// decode what the terminal transmits task key(input [7:0] code); // press and release
reg [8*16-1:0] tx_log = 0; 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; integer tx_n = 0;
initial forever begin : txmon initial forever begin : txmon
integer i; integer i;
@@ -64,27 +74,71 @@ module tb_terminal;
for (i = 0; i < 8; i = i + 1) begin #(BIT_NS); d[i] = rstx; end for (i = 0; i < 8; i = i + 1) begin #(BIT_NS); d[i] = rstx; end
#(BIT_NS); #(BIT_NS);
if (!rstx) $display("TX framing error"); if (!rstx) $display("TX framing error");
$display("%t TX %02h", $time, d); tx_log = {tx_log[8*31-1:0], d};
tx_log = {tx_log[8*15-1:0], d};
tx_n = tx_n + 1; tx_n = tx_n + 1;
end 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; task dump_screen;
integer rr, cc, p; integer rr, cc;
reg [8*80-1:0] line; reg [8*80-1:0] line;
reg [8:0] cv;
begin begin
$display("+--------------------------------------------------------------------------------+"); $display("+--------------------------------------------------------------------------------+");
for (rr = 0; rr < 30; rr = rr + 1) begin for (rr = 0; rr < 30; rr = rr + 1) begin
p = (rr + dut.term.top) % 30; for (cc = 0; cc < 80; cc = cc + 1)
for (cc = 0; cc < 80; cc = cc + 1) begin line[(79 - cc) * 8 +: 8] = cellch(rr, cc);
cv = dut.screen.mem[p * 80 + cc];
line[(79 - cc) * 8 +: 8] = cv[8] ? (cv[7:0] == " " ? "#" : cv[7:0]) : cv[7:0];
end
$display("|%s| %0d", line, rr); $display("|%s| %0d", line, rr);
end end
$display("+--------------------------------------------------------------------------------+"); $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 end
endtask endtask
@@ -98,20 +152,19 @@ module tb_terminal;
smp = 0; smp = 0;
end end
task capture_frame; task capture_frame(input [8*16-1:0] name);
integer fd, i; integer fd, i;
begin begin
@(negedge vs);
@(posedge vs); @(posedge vs);
cap = 1; cap = 1;
@(negedge vs); @(negedge vs);
cap = 0; cap = 0;
fd = $fopen("frame.ppm", "w"); fd = $fopen(name, "w");
$fwrite(fd, "P3\n640 480\n15\n"); $fwrite(fd, "P3\n640 480\n15\n");
for (i = 0; i < 640 * 480; i = i + 1) 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]); $fwrite(fd, "%0d %0d %0d\n", fb[i][11:8], fb[i][7:4], fb[i][3:0]);
$fclose(fd); $fclose(fd);
$display("wrote frame.ppm"); $display("wrote %0s", name);
end end
endtask endtask
@@ -126,36 +179,75 @@ module tb_terminal;
t_vs = $realtime; t_vs = $realtime;
end end
integer k;
initial begin initial begin
#2_000_000; // reset, clear, banner #2_000_000; // reset, clear, banner
// keyboard: a, shift+b, Enter, up arrow, ctrl+c // ---- keyboard: a, shift+b, Enter, up, ctrl+c, F5
ps2_send(8'h1C); ps2_send(8'hF0); ps2_send(8'h1C); key(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'h12); key(8'h32); ps2_send(8'hF0); ps2_send(8'h12);
ps2_send(8'h5A); ps2_send(8'hF0); ps2_send(8'h5A); key(8'h5A);
ps2_send(8'hE0); ps2_send(8'h75); ps2_send(8'hE0); ps2_send(8'hF0); ps2_send(8'h75); ext_key(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); ps2_send(8'h14); key(8'h21); ps2_send(8'hF0); ps2_send(8'h14);
#200_000; key(8'h03);
if (tx_n == 7 && tx_log[8*7-1:0] == {8'h61, 8'h42, 8'h0D, 8'h1B, "[A", 8'h03}) expect_tx({8'h61, 8'h42, 8'h0D, 8'h1B, "[A", 8'h03, 8'h1B, "[15~"}, 12, "keyboard -> UART");
$display("PASS keyboard -> UART: 61 42 0D 1B 5B 41 03");
else
$display("FAIL keyboard -> UART: %0d bytes %h", tx_n, tx_log);
uart_str("\033[2J\033[HHello, world!\015\012"); // ---- host queries and modes
uart_str("tab\tstop\tX\015\012abc\010Z \033[7m reverse \033[0m normal\015\012"); uart_str("\033[c");
uart_str("erase-to-end: KEEP\033[4DXXXX\033[4D\033[K\015\012"); expect_tx({8'h1B, "[?62;22c"}, 9, "DA reply");
for (k = 0; k < 20; k = k + 1) begin uart_str("\033[>c");
uart_str("line "); expect_tx({8'h1B, "[>1;10;0c"}, 10, "secondary DA reply");
uart_send("0" + k / 10); uart_str("\033[5;10H\033[6n");
uart_send("0" + k % 10); expect_tx({8'h1B, "[05;10R"}, 8, "cursor position report");
uart_str("\015\012"); uart_str("\033[?1h");
end ext_key(8'h75);
uart_str("\033[12;30H\033[7m+------------------+\033[13;30H| ANSI positioning |\033[14;30H+------------------+\033[0m"); expect_tx({8'h1B, "OA"}, 3, "DECCKM up arrow");
uart_str("\033[30;1H\033[?25lwrap test: 0123456789012345678901234567890123456789012345678901234567890123456789"); uart_str("\033[?1l\033=");
#100_000; 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; 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; $finish;
end end
endmodule endmodule
+20
View File
@@ -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,