From b972469d89115e9c15e2b63803b5f95bcaf1a09c Mon Sep 17 00:00:00 2001 From: Daniel Benusovich Date: Fri, 1 Mar 2019 01:20:57 -0800 Subject: [PATCH] Add unique Priority Encoder to allow for multiple matches in the CAM. Name is up for debate. Include test for basic verification. --- TLB/src/WalkingPriorityEncoder.py | 45 +++++++++++++++++++++++ TLB/test/test_walking_priority_encoder.py | 32 ++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 TLB/src/WalkingPriorityEncoder.py create mode 100644 TLB/test/test_walking_priority_encoder.py diff --git a/TLB/src/WalkingPriorityEncoder.py b/TLB/src/WalkingPriorityEncoder.py new file mode 100644 index 00000000..c5207734 --- /dev/null +++ b/TLB/src/WalkingPriorityEncoder.py @@ -0,0 +1,45 @@ +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 diff --git a/TLB/test/test_walking_priority_encoder.py b/TLB/test/test_walking_priority_encoder.py new file mode 100644 index 00000000..6f1faa04 --- /dev/null +++ b/TLB/test/test_walking_priority_encoder.py @@ -0,0 +1,32 @@ +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 -- 2.30.2