Kuree / kratos

:crossed_swords: Debuggable hardware generator
https://kratos-doc.readthedocs.io
BSD 2-Clause "Simplified" License
67 stars 9 forks source link

PackedStruct named assignmens do not work #157

Closed philipaxer closed 4 years ago

philipaxer commented 4 years ago

Named assignments for PackedStructs fail, this is because the width attribute of the underlying PackedSlice will return 1 instead of the actual slice width. See an example below:

from kratos import *

class Bug(Generator):
    def __init__(self, nitems):
        super().__init__("Bug_%d" % nitems, False)

        width = clog2(nitems)
        width2 = clog2(nitems+1)

        struct = PackedStruct("mystruct", [("a", width),
                                          ("b", width2)])

        clk = self.clock("clk")
        rst = self.reset("rst")

        s_r = self.var("s_r", struct)
        s_v = self.var("s_v", struct)

        print("expected: %02d %02d %02d" % (width+width2, width, width2))
        print("got     : %02d %02d %02d" % (s_r.width, s_r['a'].width, s_r['b'].width))

        print(str(s_r['a']))

        @always_comb
        def comb(self):
            s_v = s_r
            s_v['b'] = nitems # <-- this fails

        @always_ff((posedge, "clk"), (posedge, "rst"))
        def seq(self):
            if rst:
                s_r = 0
            else:
                s_r = s_v

        self.add_always(comb)
        self.add_always(seq)

#mod = SliceMux([1,2], 128, 3*8)
try:
    b = Bug(1024)
    verilog(b, filename="bug_packed_struct.sv", optimize_passthrough=False)
except Exception as e:
    print(e)
Kuree commented 4 years ago

Thanks! It has been fixed in 70b1f99. I've also added a unit test to ensure it won't happen again.