Evolving the logic gates of a 6502
Can a genetic algorithm find a smaller circuit than the one a person drew, or than the tools engineers use? It started with small blocks such as adders and moved on to the logic of a real 6502, the chip at the heart of my first computer, a Commodore 64.
Where it stands
Evolution works, but it does not beat the best tool. On a gate-level 6502 that passes the
complete functional test, window evolution removed 1.6% of the gates that a
converged default run of the optimiser ABC leaves (1,994 down to 1,963), with every change
proven exact. But ABC's stronger &deepsyn script reaches 1,837 gates on its
own. The best result, 1,822 gates, comes from the combination: evolution
first, then &deepsyn. Measured in transistors, the best automated design
needs 4,484, against 4,306 for the chip MOS Technology
drew by hand in 1975: 4% larger. The code is
on GitHub.
Why this is hard
A random netlist executes no instructions, and neither do its mutants, so there is almost no partial credit for evolution to climb. The evolvable-hardware literature reports that evolving circuits from scratch stalls at about the size of a 4×4-bit multiplier, and a 6502 has thousands of gates and many internal states.
The approach that does work is seed, then shrink: start from a circuit that is already correct, and evolve small pieces of it. Each piece is checked on every input combination, so evolution can only remove gates and never break behaviour.
How a circuit is evolved
Encode
A circuit is a list of gates (AND, OR, XOR, NAND, NOR, XNOR, NOT), each reading two earlier signals. Because a gate can only read signals before it, every candidate is a valid circuit and a mutation can never create a loop. This is Cartesian Genetic Programming.
Simulate every input at once
Each signal is one big integer whose bit v is its value for input combination v. One operation per gate evaluates the whole circuit for every input, so fitness is exact. A circuit that scores 100% is proven correct, not sampled.
Score
Correct output bits come first. Among equally correct circuits, fewer gates wins. A correct circuit can therefore never be replaced by a wrong one, and once it is correct the search turns into pure gate-count minimisation.
Vary
One parent, four mutated children per generation, and the best child replaces the parent if it is at least as good. Accepting ties lets unused gates drift, and later they get reconnected. There is no crossover: swapping parts of netlists tends to break the wiring.
Small blocks
First the method on blocks small enough to evolve whole: adders, and a 6502-flavoured 4-bit ALU slice (add with carry, AND, OR, exclusive-OR, plus the carry and zero flags).
| Target | Start | Result |
|---|---|---|
| 2-bit adder | random gates | correct with 14 gates, after about 100,000 evaluations |
| 4-bit adder | random gates, 5 runs | all five found a correct circuit, with 22 to 36 gates |
| 4-bit adder | textbook ripple-carry, 20 gates | stays at 20: nothing smaller found |
| 4-bit ALU slice | random gates, 4 million evaluations | no correct circuit found |
| 4-bit ALU slice | textbook design, 67 gates, 3 seeds | all three shrank it to 58 gates (−13%) |
That matches the literature: from random gates evolution manages an adder but not the ALU. A good hand design is hard to beat on a block that small, though a mediocre one can still be trimmed. The textbook 4-bit adder is probably close to the minimum for this gate set.
A real 6502 in gates
The test subject is Arlet Ottens' Verilog 6502, synthesised to gates with Yosys: 3,026 gates and 143 flip-flops, which pass Klaus Dormann's complete 6502 functional test (96.2 million clock cycles). Breaking single gates makes the test fail, so the pass is not a false positive.
Cutting the CPU at its flip-flops turned out too coarse: only 57 of 168 logic cones have 20 inputs or fewer. Instead the optimiser cuts out small windows anywhere in the netlist, evolves each one smaller, and splices it back. Exact equivalence of each window means the whole CPU is unchanged.
The baseline matters
The first result looked good: 3,026 gates down to 2,609 (−13.8%). But most of that was redundancy that any conventional optimiser removes, and a single run of ABC alone gets to 2,075. Even that baseline was too generous: ABC keeps improving when it is run again on its own output, and plateaus at about 1,994. Against that converged baseline:
| Step | Gates |
|---|---|
| Yosys without ABC | 3,026 |
| Yosys with ABC, one run | 2,075 |
| ABC iterated until it stops improving | 1,994 |
| plus window evolution with SAT don't-cares | 1,968 |
| plus register-state don't-cares | 1,963 (−1.6% beyond converged ABC, −5.4% against a single ABC run) |
Don't-cares are input combinations of a window that no CPU state can ever produce; ignoring them is still exact for every state and input. They are where the gain comes from: bigger windows alone add almost nothing. Every netlist along the way passes the full functional test. Both optimisers now sit at a local optimum, and further runs find a gate or two per several hours.
A correction: a stronger baseline
"Converged ABC" meant ABC's default script, run again and again. ABC also has
&deepsyn, a much slower randomised search with SAT-based resynthesis. Given
two minutes it reaches 1,896 gates, and ten minutes 1,865: more than the evolution found.
So the 1.6% was measured against a weak baseline, and the window evolution does
not beat ABC.
| Start | deepsyn, 10 min | + 5 min | + 5 min | + 5 min |
|---|---|---|---|---|
| ABC default plateau (1,994) | 1,865 | 1,855 | 1,837 | 1,844 |
| Our evolved netlist (1,963) | 1,828 | 1,822 | 1,829 | 1,825 |
Starting from the evolved netlist, deepsyn ends 10 to 30 gates lower every time, about 1%. A plausible reason is the register-state facts found for the evolution (322 proven invariants, such as state-machine bits that are never set together): they are properties over time, which a purely combinational optimiser cannot discover itself. Deepsyn's own run-to-run noise is about ±10 gates, so this is a modest, repeated effect and not a proof. Every one of these netlists passes the full functional test. The lesson is the usual one: a baseline is only fair if the strongest settings of the tool have been tried.
Against the real 1975 chip
The real 6502 was laid out by hand in an NMOS process, and its complete transistor netlist was extracted from photographs of the die by the visual6502 project. Counting two-input gates is not fair against a transistor netlist, so each of the designs was mapped onto an NMOS cell library with transistors as the cost.
| Design | NMOS transistors |
|---|---|
| Yosys + ABC, one run | 5,949 |
| ABC iterated, mapped with ABC's default mapper | 5,847 |
| Evolved for fewest gates | 5,918 |
| Evolved with a transistor-based cost | 5,878 |
ABC iterated, mapped with its area mapper (amap) | 5,304 |
Evolved + &deepsyn + amap, then evolution on the mapped cells (best static gates only) | 4,867 |
| The same with wide gates and pass-transistor multiplexers | 4,657 |
| The same with buses and latches that absorb multiplexers (each bus proved safe with SAT) | 4,484 |
| Real 6502, hand layout, 1975 | 4,306 |
- The 1975 hand design still wins, but narrowly. With static gates only, the best automated result is 13% larger. With wide NOR and AOI gates (as in the decode array) and pass-transistor multiplexers it is 8% larger, and with buses and latches that absorb multiplexers, 4% larger. My first measurement said 36%; that was with weaker settings, the same mistake as with the gate counts.
- Fewer gates is not fewer transistors. With ABC's default mapper the evolved netlist, which has the fewest two-input gates, needs more transistors than the ABC one. Evolving with a transistor estimate as the cost helps a little (5,918 to 5,878), but a good mapper restructures the circuit first. Once deepsyn and the area mapper are used, the evolved line ends 49 transistors ahead (4,872 against 4,921).
- The techniques of the 1975 designers can be copied, by machine. The pass-transistor multiplexers and the shared buses are what moved the count from 4,867 to 4,484. The buses are only used where SAT proves that at most one driver is enabled in every reachable state. What is left is mostly the decode array, built as a regular grid (130 lines, 20% of the chip), and the precharged buses, which this model does not copy.
Caveats: Arlet's core is not the same machine as the real 6502 (no clock generator, not cycle-exact, no pin drivers), and the cell library is an estimate of what an NMOS designer could use, not a calibrated process.
Converting the real chip to gates
The transistor netlist was also converted mechanically into ordinary gates, so that it can run the same test and be optimised like any other netlist. Pass transistors, precharged buses and stored charge become static logic plus registers that settle over a few iterations. After one ABC run that is 8,888 gates and 556 registers, running in lockstep with a switch-level simulator of the real chip and passing Klaus Dormann's complete functional test: 96,241,378 cycles, 6 hours 18 minutes. Note what the number means: it is a simulation model, not "the 6502 in gates", so shrinking it says something about the model rather than about the 1975 layout.
| Step on the model | Gates |
|---|---|
| After one ABC run | 8,888 |
| ABC to its plateau, then window evolution and ABC again | 7,150 (−3.6% against the default script) |
&deepsyn alone | 4,721 |
| Window evolution, then two more deepsyn runs | 4,633 (1.2% below deepsyn alone, 4,691) |
Merging provably equal registers with ABC's scorr, alternated with deepsyn | 3,667 gates and 414 registers (−21%) |
The largest step comes from letting ABC see the registers: scorr proves which
registers are equal or constant in every reachable state and merges them. The result passes
the full functional test. On Arlet's core the same step merges only one register, so most of
what it finds is redundancy that comes from the conversion of the real chip.
Code, licence and credits
The code and the documentation are on GitHub under the MIT licence (copyright Marcel Meijer and Kynto Software), with the tested netlists in the repository. Third-party files keep their own licences. The Verilog 6502 is Arlet Ottens'. The functional test is Klaus Dormann's (GPL v3). The real chip's transistor netlist comes from the visual6502 project (Greg James, Brian Silverman, Barry Silverman; www.visual6502.org) via perfect6502, under a non-commercial licence (CC BY-NC-SA 3.0).
What is left
- Count the transistors of the decode array, the bus drivers and the register file separately, to measure each 1975 technique.
- Model precharged buses, the one big technique the cell library does not have.
- Find out whether a smarter search than windows with don't-cares can beat deepsyn on its own ground.
Not planned: the 6510, the Commodore 64's variant with an on-chip I/O port. The target is the plain 6502 core; the port would be extra logic around it, not a better test of gate optimisation.
Its sibling project, neural6502, tackles the same processor from the other side: networks that learn to be the CPU. Its write-up ends with the thought that shrinking such a network far enough would drift toward a logic circuit.
A toy: selection, crossover and mutation
This is not circuit evolution. It evolves a string of 256 bits so that more of the positions the toy checks become 1, which shows how selection, crossover and mutation behave and nothing about gates. The real circuit work runs offline.