From b4eb0d734ead66e64bc0b0006ab45065c7ce5fb6 Mon Sep 17 00:00:00 2001 From: Luke Kenneth Casson Leighton Date: Mon, 22 Jun 2020 11:30:57 +0100 Subject: [PATCH] add byte-reverse helper function --- src/nmutil/byterev.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/nmutil/byterev.py diff --git a/src/nmutil/byterev.py b/src/nmutil/byterev.py new file mode 100644 index 0000000..c38f694 --- /dev/null +++ b/src/nmutil/byterev.py @@ -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 + + -- 2.30.2