Files
Basys3Terminal/README.md
T
barantandClaude Opus 5.5 6e54d75c56 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>
2026-11-17 11:18:36 -08:00

186 lines
7.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Basys 3 VT220 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,
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
received there are drawn on the screen. Keystrokes are sent out there.
Plug the micro-USB into a PC and open the serial port (`/dev/ttyUSB1` is usually
the UART; `ttyUSB0` is JTAG). For example:
```bash
picocom -b 115200 /dev/ttyUSB1
```
## Linux login with colour
Install the `basys3` terminfo entry once (as your user, or with `sudo` and no
`-o` for every user):
```bash
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 |
|---|---|
| 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 | 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. |
## Terminal features
- **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`).
### 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
Requires Vivado. The scripts use the newest install in `~/vivado/<version>/Vivado`;
set `VIVADO=/path/to/Vivado` to pick another. On this machine 2026.1 has no
license, so use 2025.2 (`export VIVADO=~/vivado/2025.2/Vivado`).
```bash
./scripts/build.sh # bitstream -> build/basys3_terminal.bit (+ .mcs)
```
```bash
./scripts/build.sh program # build and load into the FPGA (until power-off)
```
```bash
./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, 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
```
`scripts/vivado_env.sh` adds an ncurses 5 compatibility link under `build/`,
which Vivado needs on newer distros.
## Layout
| Path | Contents |
|---|---|
| `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 |