Hi,
Im sorry if this is in the wrong board or is a bit confusing!
Years ago I used to mess around with a Logic Circuit Simulator called Digital Works. Recently I discovered an online alternative called "Logicly" (I cant post a link as im not allowed, its on the Google lol).
Basically, you can drag and drop logic gates onto a blank canvas, link them all up with wires, and then 'Run' the circuit to see what happens.
I am attempting to do the same in Javascript in an OO manner (or at least as close to OO as JS will approximate). Writing an interface and the general mish-mash of code that ill need to write is not a problem.
My question concerns the actual logic of the simulator. Not the logic inside the actual gates (that's easy), but the logic behind the whole setup. I'm trying to think of how the simulator will 'work out' what the circuit will do when the user clicks the 'Run' button.
Im sticking to just the most basic gates right now (AND, OR, NOT). Each logic gate generally has two inputs and one output 'node / connection point' - Im thinking that a 'node' would be a seperate class to the gate it is 'connected' to. So, an AND gate would have 2 input nodes A and B, and one output node C.
Somehow it will need to track the voltage down each virtual wire to each logic gate, which in turn will spit out a simulated voltage (or not) depending on the type of gate it is.
My problem; I dont know how the algorithm will know that for example:
'input node A is attached to the AND logic gate #1 and is receiving a voltage level of 1.'
'input node B is attached to the AND logic gate #1 and is receiving a voltage level of 0.'
(Therefore, the AND gate is receiving a 1 and a 0, so spitting out a 0 to its output node - the class instance would handle the actual gate logic):
'output node C is attached to the AND logic gate #1 and is receiving a voltage level of 0.'
Hope that makes sense.
Any comments or ideas about the best / most efficient way to do this would be appreciated. Im hoping there is some well known centuries old algorithm that will handle this sort of thing that Im just oblivious to :D
Dave
Logic Circuit Algorithm
Started by dk1983, Jan 11 2010 04:45 AM
3 replies to this topic
#1
Posted 11 January 2010 - 04:45 AM
|
|
|
#2
Posted 11 January 2010 - 08:56 AM
Why not think of them as functions calling each other?
#3
Posted 12 January 2010 - 10:05 AM
Woha!! I just had the scariest moment today! I wrote all this using the quick reply..then I realized going advanced was smarter. The problem was that i had been logged out, and i'm using LastPass, and when that thing did the auto-login, i just came to a blank page. All my text was gone! I fiddled around for a while, clicking back and forward several times, logging in several times. But when I turned off LastPass auto-login and logged in manually, all my text reappeared in the edit box! :D
Back on topic:
I think a "You point to Me and I point to Him"-relationship would be perfect in this type of thing. Something like:
Abstract base class Gate have a pointer (given that a gate have two inputs), two state variables (should NOT be BOOL since you need three states! True, False and Undefined) and pure virtual functions for update for each input.
Derived class ANDgate should then have a pointer of type Node, pointing at the Node-object connected to the output. It also inherits and defines the functions and state variables from the ABC (i.e. defines the logical function). When either update function is called, the update function would update the corresponding state-variable, AND the state variables, and call the pointed-to Node-objects update function with the result. Another approach would be using argumentless methods, that would be neat. The idea is the same though.
Class Node contains a vector/array/LL that holds Gate-objects (then it could logically hold childs of Gate class, right?), the update function basicly calls the update functions for the pointed-to gates.
Another approach would be to skip the different classes for different kinds of gates, and instead use a member variable to determine what kind of gate it is, or any other method.
In both variants, the classes would be really simple. And if you're going to use a GUI, you can derive new classes for your gates that for example automagically updates the GUI (in case you want your gates to show the states on different input and outputs for example).
That's some initial thoughts anyway. Oh dang, I just realized a small flaw in my description of class "ANDgate". Two different update functions for each input makes no sense if the nodes are just going to point to the object. How would the node know what update function to call? That *could* be solved with function pointers, but in this case, I think that would be infinitely ugly. The simplest would maybe be to let Node know which input it's connected to, but that would also be pretty ugly and error-prone. (what if two nodes tell the gate that they are connected to the same inputs?).
Okay, reverse the whole process, and it should all make sense! :)
I.e. instead of letting the preceding gate or node tell the next, do the opposite! Start at the output, and ask forward "what's your status?".
I.e. Say an AND-gates a-input is connected to a source, the b-input to the gnd. The output is connected to some sort of indicator. Then the indicator ask the node it's connected to for it's status: "Are you high?". The node has no idea, so it's like: "I dunno, hold on", and asks the gate: "are you high?". The gate then asks the node at the a-input: "are you high?", and the node asks the source, which replies: "You bet!". The gnd is more like "No :(". When both the source and gnd have "been asked", the iteration starts to un-iterate, until the node connected to the indicator have been given it's answer, and can then give the answer back to the indicator: "Yes, i'm high!" (Liar!).
My class suggestions above would only need minor changes to reverse the process. In either way, the base classes would be really simple!
Edit: I thought about this some more and came up with a solution to the problems with my first solution. Having the node object pass a pointer to self when it calls the gate would solve the problem. That would also make it possible to create multi-input gates "on the fly" (i.e. several nodes could point to the same gate, given the appropriate code in the update function in the specific gate class, new inputs could be created "on the fly" as new nodes announce their existance and status to the gate.
Back on topic:
I think a "You point to Me and I point to Him"-relationship would be perfect in this type of thing. Something like:
Abstract base class Gate have a pointer (given that a gate have two inputs), two state variables (should NOT be BOOL since you need three states! True, False and Undefined) and pure virtual functions for update for each input.
Derived class ANDgate should then have a pointer of type Node, pointing at the Node-object connected to the output. It also inherits and defines the functions and state variables from the ABC (i.e. defines the logical function). When either update function is called, the update function would update the corresponding state-variable, AND the state variables, and call the pointed-to Node-objects update function with the result. Another approach would be using argumentless methods, that would be neat. The idea is the same though.
Class Node contains a vector/array/LL that holds Gate-objects (then it could logically hold childs of Gate class, right?), the update function basicly calls the update functions for the pointed-to gates.
Another approach would be to skip the different classes for different kinds of gates, and instead use a member variable to determine what kind of gate it is, or any other method.
In both variants, the classes would be really simple. And if you're going to use a GUI, you can derive new classes for your gates that for example automagically updates the GUI (in case you want your gates to show the states on different input and outputs for example).
That's some initial thoughts anyway. Oh dang, I just realized a small flaw in my description of class "ANDgate". Two different update functions for each input makes no sense if the nodes are just going to point to the object. How would the node know what update function to call? That *could* be solved with function pointers, but in this case, I think that would be infinitely ugly. The simplest would maybe be to let Node know which input it's connected to, but that would also be pretty ugly and error-prone. (what if two nodes tell the gate that they are connected to the same inputs?).
Okay, reverse the whole process, and it should all make sense! :)
I.e. instead of letting the preceding gate or node tell the next, do the opposite! Start at the output, and ask forward "what's your status?".
I.e. Say an AND-gates a-input is connected to a source, the b-input to the gnd. The output is connected to some sort of indicator. Then the indicator ask the node it's connected to for it's status: "Are you high?". The node has no idea, so it's like: "I dunno, hold on", and asks the gate: "are you high?". The gate then asks the node at the a-input: "are you high?", and the node asks the source, which replies: "You bet!". The gnd is more like "No :(". When both the source and gnd have "been asked", the iteration starts to un-iterate, until the node connected to the indicator have been given it's answer, and can then give the answer back to the indicator: "Yes, i'm high!" (Liar!).
My class suggestions above would only need minor changes to reverse the process. In either way, the base classes would be really simple!
Edit: I thought about this some more and came up with a solution to the problems with my first solution. Having the node object pass a pointer to self when it calls the gate would solve the problem. That would also make it possible to create multi-input gates "on the fly" (i.e. several nodes could point to the same gate, given the appropriate code in the update function in the specific gate class, new inputs could be created "on the fly" as new nodes announce their existance and status to the gate.
Edited by Walle, 12 January 2010 - 02:22 PM.
New idea
________________________________________________
"I'm not young enough to know everything." - Oscar Wilde
"I'm not young enough to know everything." - Oscar Wilde
#4
Posted 17 January 2010 - 12:20 AM
Im not familiar with JS, but how about implementing it in as B+ tree pattern? Unless you also wish to implement flipflops, it seems to me it fits your task.


Sign In
Create Account

Back to top









