---
title: "nes-ssh"
url: https://manvsmidi.com/projects/nes-ssh/
category: experiment
status: writeup
date: 2026-12-02
tags: [nes, 6502, assembly, terminal, linux, emulation]
related: [magic-bytes, dns-drive, streetkombat, supersonic, viper, rob, rommage, picrossh, society, pxl, shellscape]
---

# 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.

Experiment · Write-up · 2026-12-02

- [Source](https://github.com/sadovsky/nes-ssh)
- [Demo video](https://manvsmidi.com/nes-ssh/demo.mp4)

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.

<video controls muted playsinline preload="metadata" src="/nes-ssh/demo.mp4" style="display:block;width:100%;max-width:768px;margin:0 auto 1.75em;image-rendering:pixelated"></video>

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](#on-a-real-console) 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](https://manvsmidi.com/_astro/vim.Sb3lgNUr.png)
*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](https://github.com/sadovsky/nes-ssh#on-a-real-console) 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](https://manvsmidi.com/_astro/top.CDzN9GjL.png)
*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.

## Connected projects

- [MAGIC BYTES](https://manvsmidi.com/projects/magic-bytes/index.md): shared tags: linux
- [dns-drive](https://manvsmidi.com/projects/dns-drive/index.md): shared tags: linux
- [Mortal Kombat vs Street Fighter II](https://manvsmidi.com/projects/streetkombat/index.md): shared tags: emulation
- [Super Sonic Bros.](https://manvsmidi.com/projects/supersonic/index.md): shared tags: emulation
- [viper](https://manvsmidi.com/projects/viper/index.md): shared tags: terminal
- [R.O.B. (Reverse-engineering Opcode Buddy)](https://manvsmidi.com/projects/rob/index.md): shared tags: emulation
- [rommage](https://manvsmidi.com/projects/rommage/index.md): shared tags: emulation
- [picrossh](https://manvsmidi.com/projects/picrossh/index.md): shared tags: terminal
- [Society](https://manvsmidi.com/projects/society/index.md): shared tags: terminal
- [pxl](https://manvsmidi.com/projects/pxl/index.md): shared tags: terminal
- [shellscape](https://manvsmidi.com/projects/shellscape/index.md): shared tags: terminal
