MOS 6502 emulator implemented entirely in PostgreSQL

A GitHub project called pg_6502 reimagines retro computing as a database exercise: the MOS Technology 6502 CPU — the chip that powered the Commodore 64, Apple II, and NES — is implemented entirely in PostgreSQL. CPU registers, flags and the full 64KB of memory live as tables; every opcode is a stored procedure. It has been reported that the project surfaced on Hacker News, drawing a mix of admiration and head-scratching from the dev community.
How it works
The repo maps hardware to rows and SQL. A single-row table, pg6502.cpu, holds A, X, Y, SP, PC and the status flags. Memory is pg6502.mem — 64KB represented as one row per byte. Each 6502 opcode is implemented as a stored procedure, so executing code becomes a sequence of SQL calls. The quick start is intentionally simple: docker compose up -d, make reset to load the schema and a test binary, and make test to run the Klaus 6502 functional test. Postgres 16+ and Docker are required. The project is MIT-licensed and available on GitHub as lasect/pg_6502.
Why it matters
Why run a 6502 in SQL? For fun, nostalgia, and to push the boundaries of what people expect databases to do. It’s a neat thought experiment in tooling: can a relational engine model and step a CPU? Turns out, yes — and the result is oddly satisfying. Beyond novelty, this is a clever teaching tool for both systems programmers and SQL folks who want to understand state and instruction flow without leaving the database.
If you want to poke at it, the code and instructions are in the repo. Try not to stare at the mem table too long — you might start dreaming in bytes.
Sources: github.com/lasect, Hacker News
Comments