def Sub(n, **kwargs): invert = Invert(n) adder = Adders(n, True, True, **kwargs) wire(invert.O, adder.I1) wire(1, adder.CIN) return AnonymousCircuit("I0", adder.I0, "I1", invert.I, "O", adder.O, "COUT", adder.COUT)
def Negate(n, **kwargs): invert = Invert(n) adder = Adders(n, True, False, **kwargs) wire(1, adder.CIN) wire(constarray(0, n), adder.I0) wire(invert.O, adder.I1) return AnonymousCircuit("I", invert.I, "O", adder.O)
def SubC(n, **kwargs): invert = Invert(n) adder = Adders(n, True, True, **kwargs) wire(invert.O, adder.I1) notCarry = Not() wire(notCarry.O, adder.CIN) return AnonymousCircuit("I0", adder.I0, "I1", invert.I, "CIN", notCarry.I, "O", adder.O, "COUT", adder.COUT)
def DefineCounterLoad(n, cin=False, cout=True, incr=1, next=False, ce=False, r=False, s=False): name = _CounterName('CounterLoad', n, ce, r, s) if name in CounterCache: return CounterCache[name] args = [] args += ['DATA', Array(n, In(Bit))] args += ['LOAD', In(Bit)] if cin: args += ['CIN', In(Bit)] args += ["O", Array(n, Out(Bit))] if cout: args += ["COUT", Out(Bit)] args += ClockInterface(ce, r, s) Counter = DefineCircuit(name, *args) add = Adders(n, cin=cin, cout=cout) mux = Mux(2, n) reg = Register(n, ce=ce, r=r, s=s) wire(reg.O, add.I0) wire(array(*int2seq(incr, n)), add.I1) wire(add.O, mux.I0) wire(Counter.DATA, mux.I1) wire(Counter.LOAD, mux.S) reg(mux) if next: wire(mux.O, Counter.O) else: wire(reg.O, Counter.O) if cin: wire(Counter.CIN, add.CIN) if cout: wire(add.COUT, Counter.COUT) # this is fishy because of the LOAD wireclock(Counter, reg) EndCircuit() CounterCache[name] = Counter return Counter
def DefineCounter(n, cin=False, cout=True, incr=1, next=False, ce=False, r=False, s=False): name = _CounterName('Counter', n, ce, r, s) if name in CounterCache: return CounterCache[name] args = [] if cin: args += ['CIN', In(Bit)] args += ["O", Array(n, Out(Bit))] if cout: args += ["COUT", Out(Bit)] args += ClockInterface(ce, r, s) Counter = DefineCircuit(name, *args) add = Adders(n, cin=cin, cout=cout) reg = Register(n, ce=ce, r=r, s=s) wire(reg.O, add.I0) wire(array(*int2seq(incr, n)), add.I1) reg(add) if next: wire(add.O, Counter.O) else: wire(reg.O, Counter.O) if cin: wire(Counter.CIN, add.CIN) if cout: wire(add.COUT, Counter.COUT) wireclock(Counter, reg) EndCircuit() CounterCache[name] = Counter return Counter
def DefineUpDownCounter(n, cout=True, next=False, ce=False, r=False, s=False): name = _CounterName('UpDownCounter', n, ce, r, s) if name in CounterCache: return CounterCache[name] args = [] args += ["U", In(Bit)] args += ["D", In(Bit)] args += ["O", Array(n, Out(Bit))] if cout: args += ["COUT", Out(Bit)] args += ClockInterface(ce, r, s) Counter = DefineCircuit(name, *args) add = Adders(n, cin=True, cout=cout) reg = Register(n, ce=ce, r=r, s=s) wire(reg.O, add.I0) wire(array(*(n * [Counter.D])), add.I1) wire(Counter.U, add.CIN) reg(add) if next: wire(add.O, Counter.O) else: wire(reg.O, Counter.O) if cout: wire(add.COUT, Counter.COUT) wireclock(Counter, reg) EndCircuit() CounterCache[name] = Counter return Counter
def Add(n, **kwargs): return Adders(n, False, True, **kwargs)
def AddC(n, **kwargs): return Adders(n, True, True, **kwargs)