From: Luke Kenneth Casson Leighton Date: Mon, 22 Jun 2020 10:30:57 +0000 (+0100) Subject: add byte-reverse helper function X-Git-Tag: 24jan2021_ls180~52 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=b4eb0d734ead66e64bc0b0006ab45065c7ce5fb6;p=nmutil.git add byte-reverse helper function --- 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 + +