I am about to embark on a project, enspired by Nand2Tetris (http://www.nand2tetris.org/), to fully simulate a computer, building the entire thing up from NAND gates.
I want to simulate everything from scratch, starting with a primitive implementation of a NAND gate that simulates how a NAND gate would work on the physical level, and then connect them together to build a computer.
The thing is, I don't understand enough about how a NAND gate would work on the physical level in order to write this.
To be clear, this is certainly not what I am looking for:
def nand(a, b)
return !(a && b)
end
I'd like to implement the NAND gate as a class, so it functions more like an actual object. I'd like it to have pins, and I'd like to be able to connect the pins together and apply voltage, etc.
Can someone show me how you might implement a proper simulation of a physical NAND gate, one that could be linked together to ultimately build a computer?
Best Answer
The best way is to implement an event wheel and define a propagation delay for the gate. Without this delay it is impossible to simulate any kind of memory element. You can look up the term "event wheel" in google, it is the basis for HDL simulators. Alternatively you can write the whole thing in verilog which has the advantage of actually realising the hardware later with the right tools and the simulator can be free (look up "Icarus").
If you really want to apply voltages you will have to implement a transistor model or use BSIM3 in some kind of custom simulator, but you will eventually have to reimplement SPICE since the object model you propose will not work well for circuits where "inputs" are actually wires connected to gates that can load the outputs and change the behaviour of the circuit.
Good luck.