From a48bc64312999e47f8d21341107763d9e1e03f37 Mon Sep 17 00:00:00 2001 From: whitequark Date: Sun, 20 Jan 2019 01:59:09 +0000 Subject: [PATCH] lib.coding: add width as attribute to all coders. --- nmigen/lib/coding.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/nmigen/lib/coding.py b/nmigen/lib/coding.py index f9c2c63..081858b 100644 --- a/nmigen/lib/coding.py +++ b/nmigen/lib/coding.py @@ -24,6 +24,8 @@ class Encoder: Invalid: either none or multiple input bits are asserted. """ def __init__(self, width): + self.width = width + self.i = Signal(width) self.o = Signal(max=max(2, width)) self.n = Signal() @@ -31,7 +33,7 @@ class Encoder: def get_fragment(self, platform): m = Module() with m.Switch(self.i): - for j in range(len(self.i)): + for j in range(self.width): with m.Case(1 << j): m.d.comb += self.o.eq(j) with m.Case(): @@ -61,15 +63,17 @@ class PriorityEncoder: Invalid: no input bits are asserted. """ def __init__(self, width): + self.width = width + self.i = Signal(width) self.o = Signal(max=max(2, width)) self.n = Signal() def get_fragment(self, platform): m = Module() - for j, b in enumerate(reversed(self.i)): - with m.If(b): - m.d.comb += self.o.eq(len(self.i) - j - 1) + for j in reversed(range(self.width)): + with m.If(self.i[j]): + m.d.comb += self.o.eq(j) m.d.comb += self.n.eq(self.i == 0) return m.lower(platform) @@ -95,6 +99,8 @@ class Decoder: Invalid, no output bits are to be asserted. """ def __init__(self, width): + self.width = width + self.i = Signal(max=max(2, width)) self.n = Signal() self.o = Signal(width) -- 2.30.2