30.04.2013 Views

BSV by Example - Computation Structures Group

BSV by Example - Computation Structures Group

BSV by Example - Computation Structures Group

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Since a UInt#(n) is an unsigned int, you cannot initialize it with a negative number. The first line<br />

in rule step0 is commented out in the code; uncomment it and you will get the error message:<br />

Error: "Tb.bsv", line 40, column 19: (T0051)<br />

Literal ‘-1’ is not a valid UInt#(16).<br />

UInt#(n) does not allow access to individual bits, as such [ ] is not a legal construct for UInt#(n)<br />

as it is for Bit#(n).<br />

foo = foo & 5;<br />

$display("foo[0] = %x", foo[0]); //invalid statement<br />

foo = ’hffff;<br />

$display("foo = %x", foo); //valid statement<br />

// and it wraps just like you would expect it to<br />

foo = foo + 1;<br />

$display("foo = %x", foo);<br />

Because this is an unsigned number, the value can never be less than zero. This display:<br />

$display("fooneg = %x", foo < 0);<br />

is displayed the same as:<br />

$display("fooneg = %x", 1’d0 ) ;<br />

One way to get min and max values from UInts is to use unpack:<br />

UInt#(16) maxUInt16 = unpack(’1); // all ones<br />

UInt#(16) minUInt16 = unpack(0);<br />

The next rule, step1, examines the Int#(n) data type. Int#(n) has similar restrictions as UInt#(n),<br />

but it does include negative numbers. Again, the quick way to get min and max values is use unpack:<br />

Int#(16) maxInt16 = unpack({1’b0,’1}); // ’h011111<br />

Int#(16) minInt16 = unpack({1’b1,’0}); // ’h100000<br />

As with UInt#(n), you cannot perform bit manipulation directly. The following statement will give<br />

an error:<br />

$display("error1 = %x", minInt16[4]);<br />

Because the type Int#(n) (as well as the type UInt#(n) is in the Arith type class, you can perform<br />

the operations +, -, /, *, %. But be careful when using multiplication and division functions as<br />

they may create long timing paths.<br />

28

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!