|
| 1 | +from migen.fhdl import verilog |
| 2 | +from migen.fhdl.std import * |
| 3 | +from migen.genlib.mhamgen import HammingGenerator, HammingChecker |
| 4 | + |
| 5 | + |
| 6 | +# Instantiates Hamming code generator and checker modules back |
| 7 | +# to back. Also creates an intermediate bus between generator |
| 8 | +# and checker and injects a single-bit error on the bus, to |
| 9 | +# demonstrate the correction. |
| 10 | +class gen_check(Module): |
| 11 | + def __init__(self, width=8): |
| 12 | + # Save module parameters and instantiate generator and checker |
| 13 | + self.width = width |
| 14 | + hg = HammingGenerator(self.width) |
| 15 | + hc = HammingChecker(self.width, correct=True) |
| 16 | + self.submodules += hg |
| 17 | + self.submodules += hc |
| 18 | + |
| 19 | + # Create the intermediate bus and inject a single-bit error on |
| 20 | + # the bus. Position of the error bit is controllable by the |
| 21 | + # error_bit input. |
| 22 | + data = Signal(width) |
| 23 | + error_bit = Signal(bits_for(width)) |
| 24 | + self.comb += data.eq(hg.data_in ^ (1 << error_bit)) |
| 25 | + self.comb += hc.code_in.eq(hg.code_out) |
| 26 | + self.comb += hc.data_in.eq(data) |
| 27 | + |
| 28 | + # Call out I/O necessary for testing the generator/checker |
| 29 | + self.io = set() |
| 30 | + self.io.add(hg.data_in) |
| 31 | + self.io.add(hc.enable) |
| 32 | + self.io.add(error_bit) |
| 33 | + self.io.add(hc.code_out) |
| 34 | + self.io.add(hc.data_out) |
| 35 | + |
| 36 | +gc = gen_check() |
| 37 | +print(verilog.convert(gc, gc.io, name="gen_check")) |
0 commit comments