-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import magma as m | ||
import fault as f | ||
import hwtypes as ht | ||
|
||
import operator | ||
|
||
|
||
class ConfigALU(m.Circuit): | ||
io = m.IO( | ||
a=m.In(m.UInt[16]), | ||
b=m.In(m.UInt[16]), | ||
c=m.Out(m.UInt[16]), | ||
config_data=m.In(m.Bits[2]), | ||
config_en=m.In(m.Enable) | ||
) + m.ClockIO() | ||
|
||
opcode = m.Register(m.Bits[2], has_enable=True)()( | ||
io.config_data, CE=io.config_en | ||
) | ||
io.c @= m.mux( | ||
[io.a + io.b, io.a - io.b, io.a * io.b, io.b ^ io.a], | ||
opcode | ||
) | ||
|
||
ops = [operator.add, operator.sub, operator.mul, operator.xor] | ||
tester = f.SynchronousTester(ConfigALU) | ||
tester.circuit.config_en = 1 | ||
for i, op in enumerate(ops): | ||
tester.circuit.config_data = i | ||
tester.circuit.a = a = ht.BitVector.random(16) | ||
tester.circuit.b = b = ht.BitVector.random(16) | ||
tester.step(2) | ||
tester.circuit.c.expect(op(a, b)) | ||
|
||
tester.compile_and_run("verilator", flags=["-Wno-fatal"], | ||
directory="build") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import operator | ||
|
||
import magma as m | ||
import fault as f | ||
import hwtypes as ht | ||
|
||
class OptALU(m.Circuit): | ||
io = m.IO( | ||
a=m.In(m.UInt[16]), | ||
b=m.In(m.UInt[16]), | ||
c=m.Out(m.UInt[16]), | ||
opcode=m.In(m.Bits[2]) | ||
) | ||
sum_ = io.a + m.mux([io.b, -io.b], io.opcode[0]) | ||
|
||
io.c @= m.mux( | ||
[sum_, sum_, io.a * io.b, io.b ^ io.a], | ||
io.opcode | ||
) | ||
|
||
ops = [operator.add, operator.sub, operator.mul, operator.xor] | ||
tester = f.Tester(OptALU) | ||
for i, op in enumerate(ops): | ||
tester.circuit.opcode = i | ||
tester.circuit.a = a = ht.BitVector.random(16) | ||
tester.circuit.b = b = ht.BitVector.random(16) | ||
tester.eval() | ||
tester.circuit.c.expect(op(a, b)) | ||
|
||
tester.compile_and_run("verilator", flags=["-Wno-fatal"], | ||
directory="build") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import magma as m | ||
import fault as f | ||
import hwtypes as ht | ||
|
||
import operator | ||
|
||
|
||
class PE(m.Generator2): | ||
def __init__(self, instr_op_map: dict): | ||
n_cfg_bits = max(x.bit_length() for x in instr_op_map.keys()) | ||
self.io = m.IO( | ||
a=m.In(m.UInt[16]), b=m.In(m.UInt[16]), c=m.Out(m.UInt[16]), | ||
config_data=m.In(m.Bits[n_cfg_bits]), config_en=m.In(m.Enable) | ||
) + m.ClockIO() | ||
|
||
opcode = m.Register(m.Bits[n_cfg_bits], has_enable=True)()( | ||
self.io.config_data, CE=self.io.config_en | ||
) | ||
curr = None | ||
for instr, op in instr_op_map.items(): | ||
next = op(self.io.a, self.io.b) | ||
if curr is not None: | ||
next = m.mux([curr, next], opcode == instr) | ||
curr = next | ||
self.io.c @= curr | ||
|
||
|
||
ops = m.common.ParamDict({ | ||
0xDE: operator.add, | ||
0xAD: operator.sub, | ||
0xBE: operator.mul, | ||
0xEF: operator.xor | ||
}) | ||
tester = f.SynchronousTester(PE(ops)) | ||
tester.circuit.config_en = 1 | ||
for inst, op in ops.items(): | ||
tester.circuit.config_data = inst | ||
tester.circuit.a = a = ht.BitVector.random(16) | ||
tester.circuit.b = b = ht.BitVector.random(16) | ||
tester.step(2) | ||
tester.circuit.c.expect(op(a, b)) | ||
|
||
tester.compile_and_run("verilator", flags=["-Wno-fatal"], | ||
directory="build") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import operator | ||
|
||
import magma as m | ||
import fault as f | ||
import hwtypes as ht | ||
|
||
class SimpleALU(m.Circuit): | ||
io = m.IO( | ||
a=m.In(m.UInt[16]), | ||
b=m.In(m.UInt[16]), | ||
c=m.Out(m.UInt[16]), | ||
opcode=m.In(m.Bits[2]) | ||
) | ||
|
||
io.c @= m.mux( | ||
[io.a + io.b, io.a - io.b, io.a * io.b, io.b ^ io.a], | ||
io.opcode | ||
) | ||
|
||
ops = [operator.add, operator.sub, operator.mul, operator.xor] | ||
tester = f.Tester(SimpleALU) | ||
for i, op in enumerate(ops): | ||
tester.circuit.opcode = i | ||
tester.circuit.a = a = ht.BitVector.random(16) | ||
tester.circuit.b = b = ht.BitVector.random(16) | ||
tester.eval() | ||
tester.circuit.c.expect(op(a, b)) | ||
|
||
tester.compile_and_run("verilator", flags=["-Wno-fatal"], | ||
directory="build") | ||
|