Advice for designing a logic simulator
Patrick M (2888) 115 posts |
A while ago I wrote a logic simulating program in BASIC. You can place sections of wire and logic gates as tiles on a grid, and then start and stop the logic simulation. The way I designed it is that ‘top level’ items such as switches & clock sources send ‘pulses’ that travel along the wires, one ‘piece’ of wire at a time, until they reach logic gates, where they stay. Today I tried making a JK flip flop inside my program, and I noticed that it didn’t seem to work unless I put some extra NOT gates inside it in order to add some delay. I recorded a video to demonstrate this problem: https://www.youtube.com/watch?v=CQlVydimwus This makes me think that my design is very flawed, so I thought I would post here and ask, what would be a more sensible design? Or is there a way I could make my current design better? I want the simulator to be fairly simple, but I also want it to work well enough that you could build a simple computer in it. |
Rick Murray (539) 13850 posts |
Disclaimer: I know sod all about logic simulation… :-) What seems to be an issue for me, when I think about it, is that the state of the logic circuit is “instantaneous”, that is to say that in reality everything happens at the same time. This can cause problems with a simulation where you are only able to examine one “thing” at a time. As such – while I may be wrong, I would flag everything with two values – inputsatisfied and outputsatisfied. Then simply step through each element in turn checking its inputs and seeing if this can lead to a valid output. Repeat the entire process over and over until all outputs are satisfied. Easy example: A piece of wire. Whatever the input is, the output becomes. Hard example: .--- A -----| \ |AND |--.e B -----| 1 / | .--- '--- '--| \ |AND |--- G .--- .--| 3 / C -----| \ | '--- |AND |--'f D -----| 2 / '--- So what we have here are three AND gates. Four inputs (A-D) are ANDed in pairs (A/B and C/D) to create the two outputs e and f, which are ANDed to create the final output G. Looking at A first, easy. Input = Output. Satisfied. This leads to the AND 1 gate. This does not have inputs satisfied, so ignore it. Depending upon how the elements are stored in memory, we may look at e next, or input C. If e, then input is output as it’s a connection. But AND 3 does not have inputs satisfied so we must skip it. This may take us to C, then AND 2, then D… See where I’m going here? Initially neither the inputs nor the outputs will be satisfied. So we simply step through looking at the inputs (is there something?) and working out if this means we can determine an output. And just keep iterating through the items over and over until everything has a valid output. That’s probably the most asinine thing you’ve ever heard, but, you know, that’s how I’d try approaching it. ;-) |
jan de boer (472) 78 posts |
Implementing logic with electronics implies that things take some time to happen. Inductance and capacity of wires and gates, etc. Moreover, with some components (e.g. edge-triggered) timing is important. So probably you really have to include timing. |