|The Bluespec environment strictly checks both bit-width compatibility and type. This behavior differs from typical Verilog tools in that conversion is automatic in Verilog tools, whereas Bluespec requires explicit conversions.|
To convert across types in Bluespec, the following overloaded functions should suffice. During type checking, the compiler resolves these functions to a particular type instance. Excessive type conversion usually indicates a poor choice of the basic object types used in the design. These conversion utilities do not incur logic overhead.
- pack converts (packs) from various types, including Bool, Int, and UInt to Bit.
- unpack converts from Bit to various types, including Bool, Int, and UInt.
- zeroExtend increases the bit width of an expression by padding the most significant end with zeros. This function and the following two functions are overloaded in type Bool, Int, and UInt.
- signExtend increases the bit width of an expression by replicating the most significant bit.
- truncate shortens the bit width to a particular width.
- fromInteger converts from an Integer to any type where this function is provided in the Literal type-class. Integers are most often used during static elaboration since they cannot be turned into bit, hence there is no corresponding toInteger function.
- valueOf converts from a numeric type to an Integer. Numeric types are the n's as used in Bit#(n) or the number 17 as used in Bit#(17).