spinalhdl

Is it possible to convert from Bits to SpinalEnum?


I have a SlaveFactory and I'd like to save some flags in registers as SpinalEnums. Is it possible to cast Bits to my SpinalEnum on write operation?


Solution

  • I figured it out. Basically on AxiLite4SlaveFactory side I have something like this:

    val gen_stop_type = RegInit(StopType.stop_frame.asBits)
    busCtrl.write(gen_stop_type, JumboBridgeAXIReg.GEN_STOP.id)
    io.gen_stop_type_o.assignFromBits(gen_stop_type)
    

    StopType is my SpinalEnum. So going line by line:

    1. The key here is .asBits, when initialising with a default value – it is in Bits type, but with correct bit length according to StopType length when encoded in hardware.
    2. A regular SlaveFactory.write method (which now references to Bits register, not StopType register, what caused errors before).
    3. The key is .assignFromBits, which I found, when looking through docs. Basically every hardware type has this method (and SpinalEnum is a hardware type), so it's possible to "convert" Bits (with width previously derived from the enum) to StopType enum.