def n_bit_adder():
    clock = Signal()
    n = 200
    a, b = "01001" * 40, "01110" * 40

    d1 = [D_FlipFlop(clock, None, f"a{i}") for i in range(n)]
    d2 = [D_FlipFlop(clock, None, f"b{i}") for i in range(n)]

    adder = [FullAdder(None, None, f"adder{i}") for i in range(n)]

    res = [D_FlipFlop(clock, None, f"r{i}") for i in range(n)]

    for i in range(n):
        d1[i].set_input(d1[i])
        d2[i].set_input(d2[i])
        adder[i].set_input((d1[i], d2[i]))
        adder[i].set_cin(Zero() if i == 0 else adder[i - 1].cout)

        res[i].set_input(adder[i].sum)
        res[i].reset()

        if a[n - i - 1] == '0':
            d1[i].reset()
        else:
            d1[i].set()

        if b[n - 1 - i] == '0':
            d2[i].reset()
        else:
            d2[i].set()

    CircuitRunner.run(res, clock, 3, [res])
Example #2
0
 def build(self):
     size = int(math.log(self.length, 2))
     self.output = []
     zero_gate = Zero()
     for i in range(self.length):
         inputs = []
         for j in range(self.length):
             if i + j < self.length:
                 inputs.append(self.x[i + j])
             else:
                 inputs.append(zero_gate)
         self.output.append(Mux_mxn(inputs, self.y, size))
     return self.output
def multiplexer_test():
    mux = Mux4x2((One(), Zero(), One(), Zero()), (One(), Zero()), "my_mux")
    CircuitRunner.run([mux], None, None, [[mux]])
 def __init__(self, init_val=Zero()):
     self.output = init_val
 def pulse(self):
     self.output = One() if self.output.logic() == 0 else Zero()
def int_to_logic(a):
    if a == 0 or a == '0':
        return Zero()
    return One()