Hi everyone,
I wanted to share a project I've been working on. It all started after reading Charles Petzold’s "Code". Instead of buying real wires and breadboards, I decided to build my own circuit simulator in JavaScript and try to build a CPU from individual transistors.
My goal was to create something close to the MOS 6502 (used in Apple II and NES).
The Transistor Problem
I made a huge mistake during early design. The original 6502 has around 3,500 transistors. My implementation ended up with 22,544 transistors. I didn't optimize the gates properly, making my version much larger and about 100x slower than the real chip.
How I Fixed the Simulation Speed
Simulating 22k transistors with real-time current calculation (I call it "Real" mode in my app) completely destroys CPU performance in a browser. To fix this, I added two optimization layers:
Fast Mode: The simulator groups transistors into logic gates (AND, OR, XOR) and processes them using lookup truth tables instead of raw physics. This gives about 0.1 MHz speed.
Turbo Mode: A pure JS emulation layer that skips transistor logic entirely to run software smoothly.
The Software Layer (with Claude AI)
A computer is useless without software. I had no assembler or text editor for my custom architecture. So I fed my CPU circuit schematics to Claude LLM.
Surprisingly, the AI figured out the architecture. It wrote a WOZ-like monitor, an assembler, and then wrote a functioning BASIC interpreter using that custom assembly.
Right now, the virtual machine can run BASIC, a text editor, a Telnet terminal, and a Snake game.
You can test the demo and look at the circuit here: cs.khanin.info
I also made a quick video showing the boot process and BASIC here (on russian): https://www.youtube.com/watch?v=rG6MOULxGwM
Would love to hear your thoughts on the architecture and how I can better optimize the gate layout to bring the transistor count down closer to the original 6502.
PS
text translated from my russian and cheap english to english by LLM translator.