--- /dev/null
+from nmigen import Array, Module, Signal
+from nmigen.lib.coding import PriorityEncoder, Decoder
+
+class WalkingPriorityEncoder():
+
+ def __init__(self, width):
+ # Internal
+ self.current = Signal(width)
+ self.encoder = PriorityEncoder(width)
+
+ # Input
+ self.write = Signal(1)
+ self.input = Signal(width)
+
+ # Output
+ self.match = Signal(1)
+ self.output = Signal(width)
+
+ def elaborate(self, platform=None):
+ m = Module()
+
+ m.submodules += self.encoder
+
+ with m.If(self.write == 0):
+ with m.If(self.encoder.n == 0):
+ m.d.sync += [
+ self.output.eq(self.encoder.o),
+ self.match.eq(1)
+ ]
+ m.d.sync += self.current.eq(self.current ^ (1 << self.encoder.o))
+ m.d.sync += self.encoder.i.eq(self.current ^ (1 << self.encoder.o))
+
+ with m.Else():
+ m.d.sync += self.match.eq(0)
+ m.d.sync += self.encoder.i.eq(0)
+
+ with m.Else():
+ m.d.sync += self.encoder.i.eq(self.input)
+ m.d.sync += self.current.eq(self.input)
+ m.d.sync += self.encoder.i.eq(self.input)
+ m.d.sync += self.match.eq(0)
+
+
+
+ return m
\ No newline at end of file
--- /dev/null
+import sys
+sys.path.append("../src")
+sys.path.append("../../TestUtil")
+
+from nmigen.compat.sim import run_simulation
+
+from WalkingPriorityEncoder import WalkingPriorityEncoder
+
+from test_helper import assert_eq, assert_ne
+
+def testbench(dut):
+ yield dut.write.eq(1)
+ yield dut.input.eq(5)
+ yield dut.output.eq(3)
+ yield
+ yield dut.write.eq(0)
+ yield dut.input.eq(0)
+ yield
+ yield
+ yield
+ yield
+ yield
+ yield
+ yield
+ yield
+ output = yield dut.output
+ #assert_eq("Output", output, 1)
+
+if __name__ == "__main__":
+ dut = WalkingPriorityEncoder(4)
+ run_simulation(dut, testbench(dut), vcd_name="Waveforms/cam_walking_priority_encoder.vcd")
+ print("WalkingPriorityEncoder Unit Test Success")
\ No newline at end of file