As we are dealing with either registers or wires with fixed bit width, the the number of bits must be also specified. If the number of bits is not specified, compiler consider it as 32 bit number, truncation or zero padding can occur.
In general a number can be represented as
(width)'(base)(number)
8'b11110000 represents a 8 bit binary number
8'hFE represents a 8 bit hexadecimel number
4'd3 represents a 4 bit decimel number
3'o7 represents a 3 bit octal number
It is not a strict rule to specify the bit width and base when a number is given. Simulation may pass with some warnings, but some synthesizers may check for it and pop up errors. So better follow this style.
module display ;
initial begin
$display("8'b11110000 is %b",8'b11110000) ;
$display("8'hFE is %b",8'hFE) ;
$display("4'd3 is %b",4'd3) ;
$display("3'o7 is %b",3'o7) ;
$finish ;
end
endmodule
The output is given below.
initial begin
$display("8'b11110000 is %b",8'b11110000) ;
$display("8'hFE is %b",8'hFE) ;
$display("4'd3 is %b",4'd3) ;
$display("3'o7 is %b",3'o7) ;
$finish ;
end
endmodule

$ 8'b11110000 is 11110000
$ 8'hFE is 11111110
$ 4'd3 is 0011
$ 3'o7 is 111
$ 8'hFE is 11111110
$ 4'd3 is 0011
$ 3'o7 is 111
Assigning numbers
Assigning numbers may lead to truncation or padding. It can be explained with an example.
module assignm ;
reg [1:0] reg1 ;
reg [1:0] reg2 ;
reg [1:0] reg3 ;
initial begin
reg1 = 4 ;
reg2 = 1 ;
reg3 = {1'b1,1'b1} ;
$display("reg1 %b",reg1) ;
$display("reg2 %b",reg2) ;
$display("reg2 %b",reg3) ;
$finish ;
end
endmodule
The output is given below.reg [1:0] reg1 ;
reg [1:0] reg2 ;
reg [1:0] reg3 ;
initial begin
reg1 = 4 ;
reg2 = 1 ;
reg3 = {1'b1,1'b1} ;
$display("reg1 %b",reg1) ;
$display("reg2 %b",reg2) ;
$display("reg2 %b",reg3) ;
$finish ;
end
endmodule

$ reg1 00
$ reg2 01
$ reg3 11
Number 4 (binary 100) is assigned to a 2 bit register reg1 and it is clear that
the msb bit is truncated and the content of reg1 is displayed as 00. $ reg2 01
$ reg3 11
Number 1 (binary 1) is assigned to a 2 bit register reg2 and a msb bit 0 is added which makes the result 01.
In case of reg3, two single bit numbers are concatenated to form a two bit number and assigned to a two bit resgister. More about concatenation operator is available here
No comments:
Post a Comment