Skip to content

Commit

Permalink
Add fault micro code
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardt committed Oct 30, 2024
1 parent ecf2bc7 commit 475dee9
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions fault-micro/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
37 changes: 37 additions & 0 deletions fault-micro/config_alu.py
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")

32 changes: 32 additions & 0 deletions fault-micro/opt_alu.py
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")

45 changes: 45 additions & 0 deletions fault-micro/pe.py
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")

31 changes: 31 additions & 0 deletions fault-micro/simple_alu.py
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")

0 comments on commit 475dee9

Please sign in to comment.