nes-ssh.asm$FCC0 · city.bin

nes-ssh

A VT100 terminal for the NES in 6502 assembly. A serial line on the controller port joins it to a Linux pty, so the console is the screen and keyboard of a real shell.

coverThree NES screens of white text on black: a shell after uname, ls and wc, vim showing the top of an assembly source file, and top with a reverse-video header row
Three NES screens of white text on black: a shell after uname, ls and wc, vim showing the top of an assembly source file, and top with a reverse-video header row
README.mdnes-ssh/index.mdx

A VT100 terminal emulator for the NES, written in 6502 assembly. It draws 32×28 characters from an 8×8 font, reads a Family BASIC keyboard, and talks to the outside over a three-wire serial line on the controller port. Join that line to a pseudo-terminal on a Linux machine and the console becomes the screen and keyboard of a real shell: bash, vim, top, or an ssh session.

The clip is the ROM at real NES speed in the Mesen emulator, with a real bash behind it. The keystrokes are scripted and sent from the host side. Nothing here has run on a physical console yet; the last sections say what that would take.

What is and is not happening

The NES is not running Linux, and nobody logs in to the NES. A PC runs Linux. The NES does the job of a terminal on a 1970s minicomputer: it shows the bytes it is sent and sends back the keys that are pressed.

piece what it does
Family BASIC keyboard scanned by the ROM once a frame
NES running vt100.nes parses escape sequences, draws the screen
controller port 2 three wires of serial, clocked by the NES
serial adapter in the emulator, a Lua script
TCP on 127.0.0.1:6502 the only network in the system
bridge.py owns a 32×28 pty with TERM=vt100
bash, vim, ssh … ordinary Linux programs

So “SSH” means bridge.py ssh somewhere. The SSH client and its cryptography run on Linux, and the NES is where you read and type.

Scrolling with the PPU

The PPU can scroll a whole picture for free, and that is all it can do. A terminal needs more: scroll regions, reverse index, insert and delete line. Copying text is not an option, because one vertical blank has time for about 200 tile writes and a screen has 896.

The line table. The two nametables hold 60 physical rows of text. A table says which physical row each of the 28 screen rows is shown from, and the rest are kept blank. Scrolling, in any direction and over any region, permutes the table and blanks one row. Characters never move.

The ring. Vertical scroll wraps within a nametable, so its 30 rows form a ring. When text scrolls up, the new bottom row is taken from the next position on the ring whenever that is free. A plain full-screen scroll therefore walks around the ring and is displayed with the Y scroll register alone.

Seams. Where neighbouring screen rows are not neighbours on the ring, such as a status line that stays put or a scroll region, the frame has a seam. An MMC3 scanline interrupt rewrites the PPU address at each seam, timed so the write lands in horizontal blank. vim typically needs one or two. One per text row, the worst case, also works.

Atomic frames. The main thread never touches the PPU. It queues updates and keeps count of the vblank time they will need. An interrupt late in the frame tells it to commit, and the NMI handler then applies the queue, the new seam list and the cursor in one vblank. No frame shows a half-finished scroll. While it waits, the terminal clocks in no serial data, which is all the flow control there is.

vim on the NES showing the opening comment of src/video.s, wrapped at 32 columns, with the file name and cursor position on the bottom line
vim reading the terminal's own source. Scrolling the text while the bottom line stays put costs one or two seams.

The serial line

A terminal needs a stream of bytes in and out, and the controller port is the only general-purpose I/O a NES has. The port is already serial: a controller is a shift register that the console latches and then reads one button per clock pulse. The terminal uses the same three signals to shift whole bytes both ways.

wire register use
OUT0 $4016 bit 0, write data to the adapter
CLK pulses on each $4017 read clock
D0 $4017 bit 0, read data from the adapter

An exchange starts with a sync: OUT0 pulsed high and low with no clock in between. On the first clock each side says whether it has a byte. If either does, eight more clocks move one byte each way. The keyboard shares these wires and never produces a sync, so the adapter ignores it.

The NES supplies the clock, so there is no baud rate and nothing is lost when the NES is busy drawing: the other side waits until it is clocked. Throughput is about 3 to 4.7 kB/s, limited by parsing and drawing and not by the link.

Where the network is

The NES has no network stack and is not on a network. The one network connection is between the adapter and bridge.py, and by default it stays on the loopback address.

That port has no authentication. Whoever connects gets a shell as the user who started the bridge. On loopback that means local processes. The bridge can be told to listen on other addresses, and then it means anyone who can reach the port.

On a real console

Everything above has only run in an emulator. No cartridge and no adapter have been built. This is the plan, with what is known kept apart from what is open. The README has the longer version.

The cartridge is the easy part. The ROM is a plain MMC3 image (32K program, 8K font, 8K of cartridge RAM), so it should load from a flash cartridge or fit a TKROM or TSROM board. It does not depend on power-on state: the tests pass with the emulator set to random memory, random mapper state and random CPU/PPU alignment. That run found one bug, since fixed, where sprite memory was shown for a frame before it had been loaded. The mid-frame writes land on dots 275 to 296 of the scanline, inside a safe window of roughly 258 to 318. It is NTSC only.

The serial adapter uses five pins of controller port 2: power, ground, OUT0, CLK and D0. It has four things to get right.

  • The port is 5 V logic.
  • The console inverts D0, so the adapter pulls the line low to send a 1. An empty port reads as “no byte”, so the ROM runs without an adapter.
  • While receiving, the ROM clocks a bit every 11 CPU cycles, about 6 µs. That suits a shift register, a PIO program on an RP2040 or an SPI-slave peripheral, and it is too fast for bit-banging from an interrupt handler. The ROM’s bit loop can be slowed down if needed.
  • It holds bytes from the host until the NES asks for them, and makes the host wait when it is full.

The keyboard is the awkward part. The Family BASIC keyboard is a real Nintendo accessory that was made for the Japanese Famicom. It plugs into the Famicom’s 15-pin expansion port and needs three output lines and four input lines. A NES controller port carries one of those outputs and two of those inputs.

console keyboard serial adapter
AV Famicom expansion port, as designed controller port 2
Original Famicom expansion port needs a redesign: the built-in player 2 pad drives the adapter’s data line
Front-loading NES needs a breakout for the 48-pin connector under the console controller port 2
any none: type on the host controller port 2

The AV Famicom is the one machine where everything plugs in as designed. Typing on the host works everywhere and makes the NES a display only.

SSH without a PC would put Wi-Fi and the SSH client inside the adapter, so that a console, a keyboard and the adapter make a terminal on their own. The cryptography would still not be on the 6502. With 2K of RAM, 8K more on the cartridge and no multiply instruction, the adapter is the practical place for it.

What it understands

Cursor addressing, erase, insert and delete of characters and lines, scroll regions, autowrap with the VT100’s pending-wrap behaviour, origin mode, tabs, save and restore cursor, the DEC line-drawing set, reverse video, and the status and cursor-position reports. Bold, underline and colours are parsed and ignored, because the NES cannot colour single 8×8 cells.

top on the NES: five summary lines, a reverse-video header row, then a list of process IDs and users
top fits, more or less, in 32 columns. The header row is the reverse-video half of the font.

The tests run the ROM in a headless emulator and read the screen back from the rendered picture, matching each 8×8 cell against the font, so the raster tricks are covered. Hand-written cases and about two thousand random escape-sequence streams are compared with a reference terminal emulator. The fuzzing found three bugs in the ROM and, in the reference, a longer list of places where it is not a VT100.

Limits

  • 32 columns. Most programs cope, some do not.
  • No alternate screen, so quitting vim leaves its last page behind.
  • NTSC timing only.
  • The port the bridge listens on has no authentication.
  • It has never run on hardware. The emulator is cycle-accurate, which makes the timing margins a good prediction and not a result.