add byte-reverse helper function
authorLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 22 Jun 2020 10:30:57 +0000 (11:30 +0100)
committerLuke Kenneth Casson Leighton <lkcl@lkcl.net>
Mon, 22 Jun 2020 10:30:57 +0000 (11:30 +0100)
src/nmutil/byterev.py [new file with mode: 0644]

diff --git a/src/nmutil/byterev.py b/src/nmutil/byterev.py
new file mode 100644 (file)
index 0000000..c38f694
--- /dev/null
@@ -0,0 +1,21 @@
+from nmigen import Signal
+
+# TODO: turn this into a module
+def byte_reverse(m, name, data, length):
+    """byte_reverse: unlike nmigen word_select this takes a dynamic length
+
+    nmigen Signal.word_select may only take a fixed length.  we need
+    bigendian byte-reverse, half-word reverse, word and dword reverse.
+    """
+    comb = m.d.comb
+    data_r = Signal.like(data, name=name)
+    with m.Switch(length):
+        for j in [1,2,4,8]:
+            with m.Case(j):
+                for i in range(j):
+                    dest = data_r.word_select(i, 8)
+                    src = data.word_select(j-1-i, 8)
+                    comb += dest.eq(src)
+    return data_r
+
+