def generate_test(Circuit, func): #nfuncargs = check(circuit, func) sig = signature(func) nfuncargs = len(sig.parameters) assert nfuncargs + 1 < 8 rom = testvectors(func) romb = ROMB(rom) counter = Counter(9) wire(1, romb.RE) wire(1, romb.RCLKE) wire(counter.O, romb.RADDR) circuit = Circuit(nfuncargs) circuit(*[romb.RDATA[i] for i in range(nfuncargs)]) orr = Or(2) finished = DFF() f = finished(orr(counter.COUT, finished.O)) xor = XOr(2) xor(circuit.O, romb.RDATA[nfuncargs]) orr = Or(2) error = DFF() e = error(orr(xor.O, error.O)) return f, e
def f(y): or_ = uncurry(Or(n // 2)) os = [] for i in range(n): if i & (1 << y): os.append(Enc.I[i]) wire(array(os), or_) return AnonymousCircuit("O", or_.O)
class CounterModM(Circuit): name = name_ io = IO(**dict(zip(args[::2], args[1::2]))) counter = Counter(n, cin=cin, cout=False, incr=incr, has_ce=has_ce, has_reset=True) reset = Decode(m - 1, n)(counter.O) if has_reset: reset = Or(2)(reset, io.RESET) if has_ce: CE = io.CE reset = And(2)(reset, CE) # reset is sometimes called rollover or RO # note that we don't return RO in Counter # should also handle r in the definition wire(magma.reset(reset), counter.RESET) # synchronous reset if has_ce: wire(CE, counter.CE) if cin: wire(io.CIN, counter.CIN) wire(counter.O, io.O) if cout: wire(reset, io.COUT)
def definition(io): # Generate the sum m.wire(XOr(3)(io.I0, io.I1, io.CIN), io.O) # Generate the carry m.wire( Or(3)(And(2)(io.I0, io.I1), And(2)(io.I1, io.CIN), And(2)(io.I0, io.CIN)), io.COUT)
def DefineCounterModM(m, n, cin=False, cout=True, incr=1, has_ce=False, has_reset=False): name = _CounterName(f'Counter{n}_Mod{m}', incr, has_ce, has_reset, cin, cout) args = [] if cin: args += ['CIN', In(Bit)] args += ["O", Out(UInt(n))] if cout: args += ["COUT", Out(Bit)] args += ClockInterface(has_ce, has_reset) CounterModM = DefineCircuit(name, *args) counter = Counter(n, cin=cin, cout=False, incr=incr, has_ce=has_ce, has_reset=True) reset = Decode(m - 1, n)(counter.O) if has_reset: reset = Or(2)(reset, CounterModM.RESET) if has_ce: CE = CounterModM.CE reset = And(2)(reset, CE) # reset is sometimes called rollover or RO # note that we don't return RO in Counter # should also handle r in the definition wire(reset, counter.RESET) # synchronous reset if has_ce: wire(CE, counter.CE) if cin: wire(CounterModM.CIN, counter.CIN) wire(counter.O, CounterModM.O) if cout: wire(reset, CounterModM.COUT) EndCircuit() return CounterModM
def Define3to2Op(): Op = DefineCircuit("Op", "I0", In(Bit), "I1", In(Bit), "I2", In(Bit), "O", Out(Bit)) a = And(2)(Op.I0, Op.I1) b = And(2)(Op.I1, Op.I2) c = And(2)(Op.I2, Op.I0) d = Or(3)(a, b, c) wire(d, Op.O) EndDefine() return Op
class FullAdder(m.Circuit): io = m.IO(I0=m.In(m.Bit), I1=m.In(m.Bit), CIN=m.In(m.Bit), O=m.Out(m.Bit), COUT=m.Out(m.Bit)) # Generate the sum m.wire(XOr(3)(io.I0, io.I1, io.CIN), io.O) # Generate the carry m.wire( Or(3)(And(2)(io.I0, io.I1), And(2)(io.I1, io.CIN), And(2)(io.I0, io.CIN)), io.COUT)
def test_bitwise(): check_unary_operator(invert, "Invert", wrapped=False) check_unary_overloaded_operator(operator.invert, Invert(4)) binary_bits_ops = [ (and_, operator.and_, And(2, 4)), (or_, operator.or_, Or(2, 4)), (xor, operator.xor, XOr(2, 4)), ] for args in binary_bits_ops: print("Testing {}".format(args)) check_binary_operator(args[0], args[2]) check_binary_overloaded_operator(args[1], args[2]) check_unary_operator(neg, "Negate", wrapped=False) check_unary_overloaded_operator(operator.neg, Negate(4), T=SInt) binary_arith_ops = [ (add, operator.add, Add(4, cin=False)), (sub, operator.sub, Sub(4)), ] for args in binary_arith_ops: print("Testing {}".format(args)) check_binary_operator(args[0], args[2], T=Bits) check_binary_overloaded_operator(args[1], args[2], T=UInt) check_binary_overloaded_operator(args[1], args[2], T=SInt) compare_ops = [ # (eq, operator.eq, EQ(4) ), (lt, operator.lt, ULT(4), SLT(4)), (le, operator.le, ULE(4), SLE(4)), (gt, operator.gt, UGT(4), SGT(4)), (ge, operator.ge, UGE(4), SGE(4)) ] for args in compare_ops: print("Testing {}".format(args)) check_binary_operator(args[0], args[2], out_type=Bit) if args[0] == eq: check_binary_overloaded_operator(args[1], args[2], out_type=Bit) else: check_binary_overloaded_operator(args[1], args[2], T=UInt, out_type=Bit) check_binary_overloaded_operator(args[1], args[3], T=SInt, out_type=Bit)
def definition(io): swap = uncurry(fork(And(2), Or(2)), prefix="I") #swap = uncurry( fork( And(2), Or(2) ) , prefix="in") wire(swap(io.I), io.O)
def or_(*args, **kwargs): width = get_length(args[0]) return Or(len(args), width, **kwargs)(*args)
from magma import wire, compile, EndCircuit from mantle import Or from loam.boards.icestick import IceStick N = 2 icestick = IceStick() for i in range(2 * N): icestick.J1[i].input().on() for i in range(N): icestick.J3[i].output().on() main = icestick.main() or2 = Or(2, N) wire(or2(main.J1[0:N], main.J1[N:2 * N]), main.J3) EndCircuit()
from magma import wire, compile, EndCircuit from mantle import Or from loam.boards.icestick import IceStick icestick = IceStick() for i in range(2): icestick.J1[i].input().on() icestick.D5.on() main = icestick.main() or2 = Or(2) or2(main.J1[0], main.J1[1]) wire(or2.O, main.D5) EndCircuit()