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
(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 |