I’m discovering the world of electronics with my son. We encountered to some nice videos that are about recreating a whole CPU with only logic gates. The most well known one being the one from Ben Eater and the one from the NAND2Tetris project.
That said, as anyone soon notices, all the combinations of the logic gates become enormous very soon. Therefore, Ben showed a very nice trick : replacing combined logic gates with a simple EEPROM.
The idea itself is brilliant, and is the famous space versus time optimisation. Effectively precalculation of all the poasible iutputs, and sorting them. Note that using an EEPROM has several drawbacks. Regular 74HCxx logic gates operate in the 5ns range, which is very fast. Beware that if you have multiple stages of logic, which is the case here, you have to multiply by at least the number of stagss. The EEPROM reads in about 150ns, so we can afford 30 stages. Not so bad.
But that assumes a parallel EEPROM, and the one that Ben uses is the AT28C64B that is easy to source, but not really cheap.
I just happen to have a whole bunch of unused ATmega328P in a DIP packaging. This MCU is very well known, as it is the basis of the Arduino Uno. So it is ubiquitous and rather cheap. The fact that it is more versatile than an EEPROM is a nice side-effect.
So, lets see if can emulate that EEPROM1 with the MCU. I know it is a little cheating, but we are also emulating old game consoles for convenience. So I guess it is similar here.
The first problem is very simple : the EEPROM is a DIP-28 and the MCU is a DIP-28 also. Which means that no matter how you will look at it, there won’t be enough pins.
Reduce Pin Count
We therefore have to see how much pins we have in the MCU that we can actually use. There’s 3 GND, 4 VCC, 1 RESET & 2 for the external quartz. We could leverage those 2 if needed, but let’s try not to at first.
The EEPROM can be reduced to 4 buses at runtime, since we don’t write to it. We actually use it as a ROM.
- Power, is ignored as every IC is powered anyway ;)
- Chip Enable, otherwise the outputs are all high impedance.
The Chip Enable & the address are IN, and Data is OUT. Also, the Data needs to be 8-bit wide, otherwise it will be too complext.
We want to keep the control pins, such as the RESET & TOSC1/TOSC2.
Which fits perfectly, as we have 8 bits in (A) and 8 bits out (D).
In case we’d need a pin for carry, we could reuse either the TX/RX or the TOSCx ones. I’d prefer to leverage the TX/RX ones as having an external oscillator is easier to program, since the UART is much more precise, and the bootloader can still leverage TX/RX while programming.
Programming the MCU
this will be in part 2.
Actually not an EEPROM, but a ROM, as we won’t emulate the writing part. The writing part is done by uploading a new firmware on the MCU. So it is definitely not at runtime. ↩