HeadlinesBriefing favicon HeadlinesBriefing.com

Commodore 64 BASIC Runs Inside PostgreSQL via PL/CBMBASIC

Hacker News •
×

Commodore 64 BASIC V2 now executes inside PostgreSQL through the PL/CBMBASIC extension, a nostalgic yet functional fusion of retro computing and modern databases. This tool revives the 1982 Microsoft/Commodore interpreter via Michael Steil's cbmbasic project, statically recompiling the 6502 ROM into C. Every function call triggers a simulated power cycle—zeroing 64KB RAM, resetting CPU registers, and restarting the interpreter at address $E394. This process costs 15 to 20 microseconds, faster than the original hardware and quick enough for row-by-row operations in large tables. The extension injects BASIC variables into SQL queries, preserving line numbers and type-specific behavior. For example, a text parameter named `who` becomes `WHO$`, while a 16-bit integer `lives` remains fully functional. The validator enforces BASIC V2 rules at runtime, rejecting names like `TOTAL` due to keyword conflicts, a nod to 1980s C64 programming pain points.

The technical implementation mirrors Commodore 64 realities. Device 8, the database, replaces the physical disk drive, accepting SQL statements via SPI for OPEN, INPUT#, and PRINT# operations. OUT parameters work by parsing BASIC’s variable table (VARTAB/ARYTAB) to convert emulated RAM values into SQL outputs. A `divmod` function demonstrates this, returning quotient and remainder via OUT parameters by walking the variable table. The interpreter traps errors via its built-in KERNAL vector, surfacing authentic C64 error codes like `DIVISION BY ZERO`. Performance benchmarks show it’s 6x slower than PL/Python for 100-row queries but matches non-inlined SQL functions in cost. String handling is limited to 255 characters, and floats retain nine significant digits, reflecting 1982 hardware constraints. Notably, there’s no NULL support—empty strings act as placeholders, requiring `COALESCE` in queries if needed.

PL/CBMBASIC caters to superusers comfortable with its quirks. Strings must be uppercase, and keywords like `PRINT` dominate syntax. While impractical for production, it’s a unique artifact for retro enthusiasts or those testing edge cases. The extension’s repository includes a regression suite and documentation detailing its limitations. The project’s humor shines in its embrace of C64 absurdities: POKE can corrupt memory, and the RUN/STOP key now triggers PostgreSQL’s statement timeout. For developers, it’s a fascinating exercise in how software constraints shape code. The tool’s viability hinges on nostalgia—its purpose isn’t efficiency but preserving the C64 experience within a database. As the article concludes, deploying this in production would require both technical expertise and a sense of irony. The blend of historical recreation and functional utility makes PL/CBMBASIC a standout, if peculiar, addition to PostgreSQL’s ecosystem.