I need to make an application that simulates a circuit with logic gates (AND, OR, NOT, NAND, NOR, XOR). Can someone help me figure out the "skeleton" of such an application?
I'm not asking for code on a silver platter. Just an outline of what classes I would need to implement and some general advice.
To solve/create something you need to define the problem. For example:
- assume that each gate is black box with input/s and output/s. For now assume each box has always 2 input, 1 output
- each input/output can has only one state at given time: low, high or hi-z
- boxes can be connected, ie: input to output, output to input
Now that you know what to do, you must create interfaces, for example:
enum ePinState
{
LOW = 0,
HIGH,
HI_Z
};
class CPin
{
protected:
ePinState = m_state; // pin can be in 1 of 3 states
CPin * m_pOtherPin; // pin can be connected to other pin
public:
CPin() : m_state(HI_Z), m_pOtherPin(NULL) // assume pin is not connected when created
{}
virtual ~CPin() {}
virtualvoid SetState(ePinState a_newState) = 0;
};
class COutputPin : public CPin
{
public:
void SetState(ePinState a_newState)
{
// when output change, and if it is connected to other input
// that input state should be changed accordingly
}
};
class CInputPin : public CPin
{
// when input change, it should somehow indicate to owned CGate,
// that output should change according to gate type
};
// here you must think of, how to change output when one of inputs state changes
class CGate
{
public:
CInputPin input1;
CInputPin input2;
COutputPin ouput;
};
class CNANDGate : public CGate
{
// specific nand gate calculation, when input/s state change
};