Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define and initialize a vector containing only ones in Verilog?

If I want to declare a 128 bit vector of all ones, which one of these methods is always correct?

wire [127:0] mywire;

assign mywire = 128'b1;
assign mywire = {128{1'b1}};
assign mywire = 128'hFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;
like image 278
Veridian Avatar asked Sep 30 '13 23:09

Veridian


People also ask

What does #1 do in Verilog?

Verilog provides 4 basic values, a) 0 — logic zero or false condition b) 1 — logic one, or true condition c) x — unknown/undefined logic value. Only for physical data types. d) z — high-impedance/floating state.

What does 1 BX mean in Verilog?

The right side 1'bX is a 1-bit wide literal that has all bits set to X . The operator === compares the sides, including equality of X and Z states.


2 Answers

I would use the following statement instead:

assign mywire = ~0;

in a simple expression like this, the width on the left-hand side of the assignment sets the width for the expression on the right hand side. So 0, which is a 32 bit constant, is first extended to the full 128 bit of mywire, then all the bits are flipped and the resulting all-ones vector is assigned.

I'd prefer this version because it does not require you to specify the width of mywire anywhere in the assignment.

like image 34
CliffordVienna Avatar answered Oct 03 '22 12:10

CliffordVienna


As a quick simulation would prove, assign mywire = 128'b1; does not assign all bits of mywire to 1. Only bit 0 is assigned 1.

Both of the following always assign all 128 bits to 1:

assign mywire = {128{1'b1}};
assign mywire = 128'hFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF;

One advantage of the 1st line is that it is more easily scalable to widths greater than and less than 128.

With SystemVerilog, the following syntax also always assigns all 128 bits to 1:

assign mywire = '1;
like image 105
toolic Avatar answered Oct 03 '22 12:10

toolic