In Verilog numbers are declared using the following notation:
[[<size>]'<base format>]<number>
The value <size> is the size in bits (not digits) of memory allocated to the number. e.g.
test = 2'd27; // incorrect assignment (warning #1), test = 3 (last
2 bits)
test = 5'd27; // correct assigment, test = 27
If no size is declared or if the number is the output of an expression then Verilog uses a set of rules to size the number.
Test Program 1module test1; initial begin $display("Binary output"); $display("--------------------------------"); $display("%b",7); // unsized constant, output bit length=32 $display("%b",4'd7); // sized constant $display("%b",4'd13+2'd3); // output bit length = 4, output incorrect! $display("%b",4'd13+5'd3); // output bit length = 5, output correct! $display("%b",(4'd13>5'd2)); // output bit length = 1 end endmodule // test1 OutputBinary output -------------------------------- 00000000000000000000000000000111 0111 0000 10000 1 |
If the number is subsequently assigned to a variable then the variable's predefined size is used to store the number and the above rules do not apply.
Test Program 2module test2; integer test; initial $monitor ("%b %d",test,test); initial begin $display("Binary Decimal"); $display("--------------------------------------------"); test=4'd10; // 4 bits assigned for number but integer stored in 32 bits #10; test=test-20; // negative numbers no problem #10; test='b1000_0000_0000_0000_0000_0000_0000_0000; // largest negative #10; test='b0111_1111_1111_1111_1111_1111_1111_1111; // largest positive #10; test=(4'd13+2'd3); // bit length of output is no longer determined by // max bit length of operand #10; test=2'd27; // incorrect assignment #10; test=13.7; // decimal points rounded #10; $finish; end // initial begin endmodule // test2 OutputBinary Decimal -------------------------------------------- 00000000000000000000000000001010 10 11111111111111111111111111110110 -10 10000000000000000000000000000000 -2147483648 01111111111111111111111111111111 2147483647 00000000000000000000000000010000 16 00000000000000000000000000000011 3 00000000000000000000000000001110 14 |
Test Program 3module test3; reg test; reg [3:0] test2; initial $monitor (" %b %b %d",test,test2,test2); initial begin $display ("test(binary) test2(binary) test2(decimal)"); $display ("-----------------------------------------"); test=4'd7; // incorrect assignment - this does not generate a warning!! // the LSB is assigned to the 1 bit register. test2=4'd7; // correct assignment #10; test2=test2-5; // arithmetic is carried out as expected on positive numbers #10; test2=test2-3; // registers do not store negative numbers #10; test2=test2+5; // exceeding the predefined size of the register does not // generate a warning #10; $finish; end // initial begin endmodule // test3 Outputtest(binary) test2(binary) test2(decimal) ----------------------------------------- 1 0111 7 1 0010 2 1 1111 15 1 0100 4 |
The Verilog compiler will not generate warnings for all sizing errors. Any warnings that are reported will not halt the execution of the program but should be corrected.
No. | Problem | Warning |
---|---|---|
#1 | A number has been assigned that is larger than the number of bits allocated to it. | Warning! Redundant digits given in (base) number |
#2 | The port sizes for the module instance and definition do not match. | Warning! Port sizes differ in port connection (port x) |
#3 | A number is assigned to a variable that has too little memory allocated to store it. | |
#4 | A negative number is assigned to a register. | |
#5 | Bits outwith the size of a variable are being accessed. |