ben
ben

Reputation: 481

How to define a Bi-Directional bus as 'z' or as '0,1' in One Line?

I'm using the altera synthesizer with a 32-bit bi-directional bus :

module Test
(
   inout  [31:0]biBus
);

wire [31:0] biEna;
wire [31:0] biVal;

I'm wondering if there's a syntax that instead of doing this:

assign biBus [ 0 ]  = biEna [ 0 ] ? biVal [ 0 ] : 1'bz;
assign biBus [ 1 ]  = biEna [ 1 ] ? biVal [ 1 ] : 1'bz;
...
assign biBus [ 31 ]  = biEna [ 31 ] ? biVal [ 31 ] : 1'bz;

could instead be done in 1 line, something like:

assign biBus  = biEna ? biVal : *'bz;  

Where each bit of the bus is assigned either biVal or hi-z based on the corresponding value of biEna. How would I specify the hi-z portion above (if it's possible?

Upvotes: 1

Views: 2245

Answers (2)

lvd
lvd

Reputation: 812

Just {32{1'bZ}} or 32'hZZZZZZZZ.

Upvotes: 1

Ari
Ari

Reputation: 7556

You can do this in an always_comb or always @* block:

integer i;
for (i = 31 ; i >=0 ; i--)
    if (biEna[i]) biBus[i] = biVal[i];
    else biBus[i] = 1'bz; 

or:

biBus = 'z; //Initialize all bits to z
for (i = 31 ; i >=0 ; i--)
    if (biEna[i]) biBus[i] = biVal[i]; //Overwrite the z value for bits whose enable is 1

or if you want to keep using assign statements without an always block:

genvar i;
for (i=31 ; i>=0 ; i--)
   assign biBus [ i ]  = biEna [ i ] ? biVal [ i ] : 1'bz;

Upvotes: 1

Related Questions