How to display a multidimensional slice of an array
Let's say I have a multidimensional array:
logic [7:0] mda [7:0];
What I am currently trying to do is assign the mda[7:4]
output port i.e. is defined as follows:
output [31:0] odata;
Of course, I can do it using concatenation:
assign odata = {mda[7], mda[6], mda[5], mda[4]};
But there should be (and probably is) an easier way to do this. First try:
assign odata = mda[7:4];
which is wrong because the types (unpacked ↔ packed array) do not match. All my casting attempts (for example 32'(mda[7:4])
) have failed. The question is: what's the best way to assign this cut to an output port?
source to share
You can use a for loop ... Most synthesis tools have no problem for constant range loops:
module dut(output [31:0] odata);
logic [7:0] mda [7:0];
reg[31:0] data;
always @* begin
data = 0;
for(int i=7; i >=4; i--) begin
data <<= 8;
data |= mda[i];
end
end
assign odata = data;
endmodule
Here's a quick test: http://www.edaplayground.com/x/GfM
source to share
You can use the streaming operator:
initial begin
logic[31:0] data;
mda[7] = 'hde;
mda[6] = 'had;
mda[5] = 'hbe;
mda[4] = 'hef;
data = { >> { mda[7:4] }};
$display("data = ", data);
end
This works great in a procedural context, but not in a continuous assignment context (for some reason). This means it doesn't work:
assign odata = { >> { mda[7:4] }};
You need to find a workaround where you stream in a procedural context (probably in a block always
) and then assign the result to a wire.
source to share