BSV by Example - Computation Structures Group
BSV by Example - Computation Structures Group
BSV by Example - Computation Structures Group
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Data Type Conversion Functions<br />
Function Type Class Description<br />
pack Bits Converts (packs) from types in the Bits class, including<br />
Bool, Int, and UInt to Bit.<br />
unpack Bits Converts (unpacks) from Bit to other types in Bits class,<br />
including Bool, Int and UInt.<br />
extend BitExtend Performs either a zeroExtend or a signExtend as appropriate,<br />
depending on the data type of the argument.<br />
zeroExtend BitExtend Increases the bit width of an expression <strong>by</strong> padding the<br />
most significant end with zeros. Normally used for unsigned<br />
values.<br />
signExtend BitExtend Increases the bit width of an expression <strong>by</strong> replicating the<br />
most significant bit. Normally used for signed values.<br />
truncate BitExtend Shortens the bit width to a particular width.<br />
fromInteger Literal Converts from an Integer type to any type defined in the<br />
Literal type class. Integers are most often used during<br />
static elaboration.<br />
fromReal RealLiteral Converts from a Real type to any type defined in the<br />
valueOf Any numeric<br />
type<br />
3.6 Some common scalar types<br />
RealLiteral type class.<br />
Converts from a numeric type to an Integer. Numeric<br />
types are the n used in types such as Bit#(n) and<br />
Vector#(n, t).<br />
The following pre-defined scalar types are all in the Bits typeclass. All hardware types (values<br />
carried on wires, stored in registers, FIFOs or memories, etc.) must be in the Bits typeclass (but<br />
note, arbitrary types can be used during static elaboration).<br />
3.6.1 Bit#(n)<br />
The most basic type is Bit#(n), a polymorphic data type defining a type consisting of n bits.<br />
Verilog programmers will naturally want to only ever use Bit#(n) as this gives them the complete<br />
bit selecting and operating freedom they are used to with Verilog. But Verilog also allows a lot of<br />
dubious operations on data, for example, automatic truncation. And more importantly, a Bit#(n)<br />
doesn’t really indicate anything about the semantics of the data.<br />
In <strong>BSV</strong>, we want to take advantage of strong typing <strong>by</strong> using meaningful data types. This decreases<br />
bugs and helps find bugs earlier in the design cycle, at compile time, rather than at debug or tapeout<br />
time.<br />
The familiar Verilog type bit is a synonym for Bit#(1).<br />
3.6.2 Bool<br />
We use Bool as opposed to bit or Bit#(1) to define true/false logical operations. In <strong>BSV</strong>, all<br />
“conditions” (in rules, methods, if statements, while statements, etc.) require Bool types.<br />
typedef enum {False, True} Bool;<br />
The implementation of a Bool is one bit. But, be aware, Bool is not considered type-equivalent to<br />
Bit#(1). Use Bool for True or False values and Bit#(1) for variables with a value of 0 or 1. You<br />
26