2

I'm currently learning Computer Science and I thought I'd try my hand at simulating how a computer works at the level of the computer architecture. The idea is that I would start with the basic components and combine them to create more and more complex parts, like an ALU or a small amount of RAM until I can run simple programs on it. This is being built in Python.

The two most basic kinds of objects I built are:

  1. Signals, which are nodes with a constant value and no input. Their value can also be flipped with an object method some_signal.flip().

  2. Gates. Each gate is a node with a value that depends on the value of its children. They all inherit from a Component class. The calculation for a gate is performed when gate.get_output() is called on any Component gate, which also calls get_output on its children. I'll include the code at the end.

The Signals were no problem. It took me a while but I got each of the logic gates to work as expected. I had to add special code to make the logic gates work when connected to themselves. Now they work as expected.

I used the Gates to construct an SR latch.

class Latch(And):
    def __init__(self, set, reset):
        self.not_reset = Not(reset)
        self.latch_or = Or(None, set)
        super().__init__(None, self.not_reset)
        self.receiving_from_a = self.latch_or
        self.latch_or.receiving_from_a = self
        self.this_type = "Latch"

It works as expected.

The problem happens when I try to attach a data line and a write-enable line (I'm sorry, I don't know what that's called).

and1 = And(data, enable)
not_data = Not(data)
and2 = And(not_data, enable)
not_and2 = Not(and2)
bit = Latch(and1, not_and2)

For some reason I am not able to write the signal from data to bit regardless of the state of enable. I'm baffled because everything worked as expected until this point. Is it a problem with my implementation of the code or did I just construct the final gate wrong?

Here is the full code if you want to run it (If you know how to make this hideable please do):

##GENERIC COMPONENT CLASS (BASE FOR OTHER OBJECTS)
class Component:

#MAGIC
    def __init__(self, output, receiving_from_a = None, receiving_from_b = None, this_type = "Component"): #output is value, receiving_from is next_node
        self.output = output
        self.receiving_from_a = receiving_from_a
        self.receiving_from_b = receiving_from_b
        self.this_type = this_type

    def __repr__(self):
        return self.this_type + " with output = " + str(self.get_output())

#SETTERS
    #generic for one input, sets a
    def set_receiving_from(self, receiving_from):
        self.receiving_from_a = receiving_from
    #for 2 inputs   
    def set_receiving_from_a(self, receiving_from_a): #set component self receives from 
        self.receiving_from_a = receiving_from_a

    def set_receiving_from_b(self, receiving_from_b):
        self.receiving_from_b = receiving_from_b

#GETTERS
    #generic for one input, gets a
    def get_receiving_from(self):
        return self.receiving_from_a
    #for components with 2 inputs
    def get_receiving_from_a(self):
        return self.receiving_from_a

    def get_receiving_from_b(self):
        return self.receiving_from_b

    def get_output(self):
        return self.output
    
    #gets what receiving from is showing
    #generic for one input, gets a
    def get_input(self):
        return self.get_receiving_from_a().get_output()
    #for components with 2 inputs
    def get_input_a(self):
        return self.get_receiving_from_a().get_output()

    def get_input_b(self):
        return self.get_receiving_from_b().get_output()
#FUNCTIONAL
    #Creates a Component to itself
#---------------------------------------------------------------------------------------

###LOGIC GATES
#-----------------------------------------------------------------------------------------------------------------------
#-----------------------------------------------------------------------------------------------------------------------
## TEST SIGNAL DEFINITIONS
class Signal: # test signals, nodes with outputs but no input, can be flipped
    
    def __init__(self, output):
        if type(output) == bool:
            self.output = output
        else:
            print("Error! Signal has wrong type.")

    def __repr__(self):
        return "Signal with output = " + str(self.output)

    def get_output(self):
        return self.output

    def flip(self):
        if self.output == True:
            self.output = False
        else:
            self.output = True
        return self.output
#------------------------------------------------
#NOT GATE DEFINITION
class Not(Component):
    def __init__(self, receiving_from):
        super().__init__(not(receiving_from.get_output()), receiving_from, None, "NOT Gate")

    def set_output(self):
        self.output = not (self.get_input())

    def get_output(self):
        self.set_output()
        return self.output
#-------------------------------------------------------------------------------
##OR GATE DEFINITION
class Or(Component):
    def __init__(self, receiving_from_a = None, receiving_from_b = None):
        if (receiving_from_a != None) and (receiving_from_b != None):
          super().__init__(receiving_from_a.get_output() or receiving_from_b.get_output(), receiving_from_a, receiving_from_b, "OR Gate")
        else:
            super().__init__(False, receiving_from_a, receiving_from_b)
        self.prev_out = False

    def set_output(self):
        try:
            self.prev_out = self.output
            if self.get_receiving_from_a() == self:
                self.output = self.prev_out or self.get_input_b()
            elif self.get_receiving_from_b() == self:
                self.output = self.get_input_a() or self.prev_out
            
            else:
                self.output = (self.get_input_a() or self.get_input_b())
        except RecursionError:
            self.output = self.prev_out

    def get_output(self):
        self.set_output()
        return self.output

    def or_connected_to_self_a(signal):
        target = Or(None, signal)
        target.set_receiving_from_a(target)
        target.get_output()
        return target


    def or_connected_to_self_b(signal):
        target = Or(signal, None)
        target.set_receiving_from_b(target)
        target.get_output()
        return target

    def or_connected_to_self(signal, char):
        if char == "a":
            return Or.or_connected_to_self_a(signal)
        elif char == "b":
            return Or.or_connected_to_self_b(signal)
        else:
            print("ERROR! Did not specify which side to connect OR gate to")
#-------------------------------------------------------------------------------
##AND GATE DEFINITION
class And(Component):
    def __init__(self, receiving_from_a = None, receiving_from_b = None):
        if (receiving_from_a != None) and (receiving_from_b != None):
          super().__init__(receiving_from_a.get_output() and receiving_from_b.get_output(), receiving_from_a, receiving_from_b, "AND Gate")
        else:
            super().__init__(True, receiving_from_a, receiving_from_b)
        self.prev_out = True

    def set_output(self):
        self.prev_out = self.output
        try:
            if self.get_receiving_from_a() == self:
                self.output = self.prev_out and self.get_input_b()
            elif self.get_receiving_from_b() == self:
                self.output = self.get_input_a() and self.prev_out
            else:
                self.output = (self.get_input_a() and self.get_input_b())
        except RecursionError:
            self.output = self.prev_out

    def get_output(self):
        self.set_output()
        return self.output
    
    def and_connected_to_self_a(signal):
        target = And(None, signal)
        target.set_receiving_from_a(target)
        target.get_output()
        return target


    def and_connected_to_self_b(signal):
        target = And(signal, None)
        target.set_receiving_from_b(target)
        target.get_output()
        return target

    def and_connected_to_self(signal, char):
        if char == "a":
            return And.and_connected_to_self_a(signal)
        elif char == "b":
            return And.and_connected_to_self_b(signal)
        else:
            print("ERROR! Did not specify which side to connect AND gate to")

#-------------------------------------------------------------------------------------
##XOR GATE
class XOr(And): #Top node is and
    def __init__(self, receiving_from_a, receiving_from_b):
       ##(A or B) and ~(A and B)
        self.A_or_B = Or(receiving_from_a, receiving_from_b)
        self.A_and_B = And(receiving_from_a, receiving_from_b)
        self.not_A_and_B = Not(self.A_and_B)
        self.this_type = "XOR Gate"
        super().__init__(self.A_or_B, self.not_A_and_B)

#-------------------------------------------------------------------------------------
##LATCH
class Latch(And):
    def __init__(self, set, reset):
        self.not_reset = Not(reset)
        self.latch_or = Or(None, set)
        super().__init__(None, self.not_reset)
        self.receiving_from_a = self.latch_or
        self.latch_or.receiving_from_a = self
        self.this_type = "Latch"
        
        
#-------------------------------------------------------------------------------------
##FUNCTIONS
        
def print_truth_table(gate, A, B = None):
    print("{} {} {}".format(A.get_output(), B.get_output(), gate.get_output()))
    A.flip()
    print("{} {} {}".format(A.get_output(), B.get_output(), gate.get_output()))
    A.flip()
    B.flip()
    print("{} {} {}".format(A.get_output(), B.get_output(), gate.get_output()))
    A.flip()
    print("{} {} {}".format(A.get_output(), B.get_output(), gate.get_output()))
    A.flip()
    B.flip()
#-------------------------------------------------------------------------------------
#Constant signals
false = Signal(False)
true = Signal(True)
#-------------------------------------------------------------------------------------
##TEST CODE HERE

# You can use signal.flip() to flip a signal.
# Some_Gate(signal1, signal2) constructs the specified gate connected to signal1 on the left and signal2 on the right.
# 

data = Signal(True)
enable = Signal(False)

and1 = And(data, enable)
not_data = Not(data)
and2 = And(not_data, enable)
not_and2 = Not(and2)
bit = Latch(and1, not_and2)

##Uncomment to test an SR latch
#set = Signal(False)
#reset = Signal(False)
#latch = Latch(set, reset)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.