1
\$\begingroup\$

In VHDL I can write this: slv_1(slv_2'range) so that I select slice of slv_1 that has same range as slv_2. In this example both slv_1 and slv_2 are actually std_logic_vectors.

I am writing SystemVerilog code. How is this done in SystemVerilog? I shall be grateful if you could guide me to the relevant LRM section.

logic [WORD0_TEXFORMAT_W-1:0] slv_1 = '0;
logic [$clog2(YUV_FORMATS_LIST_LENGTH)-1:0] slv_2 = '0;

I need to do slv_1[slv_2'range].

asked Apr 23, 2024 at 11:07
\$\endgroup\$
0

2 Answers 2

1
\$\begingroup\$

Refer to the $bits function in IEEE Std 1800-2023, section 20.6.2 Expression size system function.

module tb;
logic [31:0] slv = 'h1234_5678;
logic [ 7:0] yuv_slv = '0;
initial begin
 #1 $display("'h%x", slv[$bits(yuv_slv)-1:0]);
end
endmodule

Output:

'h78
answered Apr 23, 2024 at 11:48
\$\endgroup\$
1
\$\begingroup\$

Technically, the equivalent of VHDL slv_2'range is [$left(slv_2):$right(slv_2)]. See section 20.7 Array query functions in the IEEE 1800-2023 SystemVerilog LRM

But if you are just trying to make an assignment from a larger width to a smaller width, there is no need to do anything as Verilog will just implicitly truncate it:

slv_2 = slv_1;

You can also do an explicit truncate using a cast:

slv_2 = type(slv_2)'(slv_1);
answered Apr 23, 2024 at 15:45
\$\endgroup\$
5
  • \$\begingroup\$ What about slv_1[slv_2]? \$\endgroup\$ Commented Apr 23, 2024 at 19:17
  • \$\begingroup\$ @quantum231. What about it?. you need to be consistent. You description shows one set of names, but your code example shows another set of names. \$\endgroup\$ Commented Apr 23, 2024 at 21:54
  • \$\begingroup\$ you are right, I have made it consistent. Is slv_1[slv_2] legal SV or [$left(yuv_slv):$right(yuv_slv)]? \$\endgroup\$ Commented Apr 24, 2024 at 17:44
  • \$\begingroup\$ VHDL does not let larger to smaller, it is very specific about widths. SV allows different sizes and even different types to be assigned from one to another without throwing errors. Strange. \$\endgroup\$ Commented Apr 24, 2024 at 17:44
  • \$\begingroup\$ slv_1[slv_2] is not legal. I have updated my answer to match the identifiers in your question. To get stronger typing in SystemVerilog, see verificationacademy.com/topics/systemverilog/…. \$\endgroup\$ Commented Apr 24, 2024 at 18:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.