Specifying a range of variables in verilog using a loop

I need to write this code:

for (i = 0; i <= CONST - 1'b1; i = i + 1'b1) begin : loop_inst if (i < 3) begin if (changed[i] & !done_q[i]) begin writedata[3-i] = en[i]; writedata[2-i:0] = readdata[2-i:0]; writedata[15:4-i] = readdata[15:4-i]; end end else ... 

Basically, the location of the bits I'm trying to write to (en) varies depending on what address I'm talking to, depending on i. This code is not synthesized because "i" is not a constant.

Is there any other job? The only work in question is writing these three statements CONST times. I hope it SHOULD NOT do this at the end. Is there any other solution?

+6
source share
1 answer

It looks like you always copy readdata to writedata , but populate LSB with en if certain special conditions are met. I also assume that the for loop you have is in the always block and that you are going to build combo logic.

The for loop, as you wrote it, doesn't make much sense to me in terms of hardware. The for loop is used to build logic arrays, and since in this case you will have at least 3 logical cones trying to set values ​​on the entire writedata bus. (If it generates anything at all, it will be some kind of weird priority structure).

However, probably the range chooses what your compiler complains about, i.e. writedata[2-i:0] , not writedata[3-i] = en[i]; (anything with : in the choice of party). If you want to do something along these lines, you can use the "indexed parts" ( +: or -: , but in this case there are better solutions.

I would rewrite it as follows - assuming that I understood correctly :)

 always @( /*whatever*/ ) begin // default assignment writedata = readdata; // overwrite some bits in writedata for special cases for(i=0; i<3; i++) begin if( changed[i] & !done_q[i] ) writedata[3-i] = en[i]; end end 

In this code, I set writedata to readdata , and then adjust the resulting writedata value if special cases are in the game. The for loop creates 3 logical cones, one for each bit in writedata[3:1] . I would double check if the bitmap is what you intend - the mappings en[2:0] to writedata[1:3] .

+4
source

Source: https://habr.com/ru/post/893699/


All Articles