sharpie7 / circuitjs1

Electronic Circuit Simulator in the Browser
GNU General Public License v2.0
2.23k stars 621 forks source link

Bug- Possible Bug with D Type Flip Flops? #864

Open Seiynn opened 1 year ago

Seiynn commented 1 year ago

I have made a circuit that seems to break the simulator. I have included the text file for the circuit that creates the issue. If the diode is removed the issue disappears, If I replace the switches for Logic Inputs the issue disappears, however there is a large current that will be calculated to be flowing through the diode and rightfully so. I used the schmitt triggers thinking that was the issue but it doesn't fix the bugged circuit. The input pins of the flip flop can be observed and the voltages appear to be consistent across all circuits bugged and not bugged that I have tested. However in the bugged circuit, the flip flop will change states. To do this Turn on the bottom switch, then the top switch, then turn off the bottom switch. Q inverted will go from HIGH to LOW. if the switches are replaced with Logical inputs and the same sequence is inputted there is no bug and the Q inverted will not change from HIGH to LOW it will stay permanently HIGH as it is supposed to.

$ 1 0.000005 114.32915054483153 50 5 50 5e-11 R 352 368 272 368 0 0 40 5 0 0 0.5 w 352 368 352 320 0 w 352 368 352 416 0 r 432 320 432 256 0 10000 r 432 416 432 496 0 10000 g 432 496 432 512 0 0 g 432 256 432 224 0 0 w 432 320 496 320 0 155 752 384 848 384 0 5 w 496 416 640 416 0 w 496 320 640 320 0 z 496 320 496 416 2 1N5711 w 640 320 640 384 0 w 720 416 752 416 0 w 720 384 752 384 0 s 352 416 432 416 0 1 false s 352 320 432 320 0 0 false w 432 416 496 416 0 182 640 384 720 384 0 0.5 1.66 3.33 5 0 182 640 416 720 416 0 0.5 1.66 3.33 5 0 o 7 64 0 4099 5 0.00009765625 0 2 17 0 o 14 64 0 4099 5 0.00009765625 1 2 13 0

pfalstad commented 1 year ago

I think what's happening is that the zener diode is requiring multiple iterations and the flip-flop is seeing the clock transition high on one of those iterations and so it's updating its outputs. The flip-flops (and many other chips) should be executed once per iteration (at the start or end) rather than multiple times. So in startIteration() or stepFinished(), not doStep().

To work around this, you need a one-step delay instead. https://tinyurl.com/2pfhe5fn