diff --git a/examples/even_odd_sort.py b/examples/even_odd_sort.py new file mode 100644 index 000000000..c06179144 --- /dev/null +++ b/examples/even_odd_sort.py @@ -0,0 +1,8 @@ +import magma as m + + +class Swap(m.Circuit): + io = m.IO(I=m.In(m.Bits[2]), O=m.Out(m.Bits[2])) + # io.O @= m.bits([io.I.reduce_and(), io.I.reduce_or()) + print(m.fork(m.Bit.And, m.Bit.Or)) + io.O @= m.uncurry(m.fork(m.Bit.And(), m.Bit.Or()))(io.I) diff --git a/examples/tests/test_even_odd_sort.py b/examples/tests/test_even_odd_sort.py new file mode 100644 index 000000000..4fefa3c8f --- /dev/null +++ b/examples/tests/test_even_odd_sort.py @@ -0,0 +1,17 @@ +import fault as f +from ..even_odd_sort import Swap +from hwtypes import BitVector + + +def swap(I: BitVector[2]): + return BitVector[2]([I[0] & I[1], I[0] | I[1]]) + + +def test_swap(): + tester = f.Tester(Swap) + for i in range(2): + for j in range(2): + tester.circuit.I = I = BitVector[2]([i, j]) + tester.eval() + tester.circuit.O.expect(swap(I)) + tester.compile_and_run('verilator', magma_output="mlir-verilog") diff --git a/magma/bit.py b/magma/bit.py index b2a441fd9..51c5000f2 100644 --- a/magma/bit.py +++ b/magma/bit.py @@ -94,7 +94,8 @@ def __ne__(self, other): @magma_helper_function @bit_cast def __and__(self, other): - return type(self).undirected_t._declare_binary_op("and")()(self, other) + return self.And()(self, other) + @magma_helper_function @bit_cast @@ -104,7 +105,7 @@ def __rand__(self, other): @magma_helper_function @bit_cast def __or__(self, other): - return type(self).undirected_t._declare_binary_op("or")()(self, other) + return self.Or()(self, other) @magma_helper_function @bit_cast diff --git a/magma/bit_primitives.py b/magma/bit_primitives.py index 122099f68..343d5e0e7 100644 --- a/magma/bit_primitives.py +++ b/magma/bit_primitives.py @@ -70,6 +70,8 @@ def simulate(self, value_store, state_store): # NOTE(leonardt): Monkey patched functions. Bit._declare_unary_op = _declare_unary_op Bit._declare_binary_op = _declare_binary_op +Bit.And = Bit._declare_binary_op("and") +Bit.Or = Bit._declare_binary_op("or") def make_Define(_name, port, direction): diff --git a/magma/braid.py b/magma/braid.py index 64ccffd3b..28f5fd514 100644 --- a/magma/braid.py +++ b/magma/braid.py @@ -311,6 +311,7 @@ def uncurry(circuit, prefix='I'): otherargs = [] uncurryargs = [] for name, port in circuit.interface.ports.items(): + print(port.driven(), name) # should we insert the argument in the position of the first match? if not port.driven() and name.startswith(prefix) and \ circuit.interface.is_input(port):