2

I am trying to port some SystemVerilog code to C++/SystemC, and am a bit stuck on a couple lines where I see strange array indexing. Here is a simplified version of what I see.

typedef enum bit [2:0] {enu_red, enu_blue, enu_green} typ_enum; 

typedef struct packed {
    bit [3:0] field1;
    bit [3:0] field2;
} typ_struct;

...

var int arr_ints[typ_struct];
var int que_ints[$];

typ_struct obj_struct;
typ_enum   obj_enum;

int i = 3;

// assume all the declared variables hold valid values at this point
// also assume que_ints[i] is valid

if ((!arr_ints[obj_struct][1]) // these two lines are the problem
    && (que_ints[i][obj_struct])
)
begin
    // do something
end

Now after I port this code I get some compiler errors which I completely understand, because the original code doesn't look exactly right to me. In the first line of the if statement, it looks like trying to index an integer type with a boolean value. In the second it looks like trying to index an integer type with an enumerated value. Yet this code apparently works. Can someone please explain what it is doing?

2
  • What is true defined as? Commented Dec 11, 2012 at 16:53
  • I replaced true with 1. In the real code it is a function used as a true/false boolean, but is implemented as a bit. I was mixed up with the C++ equivalent, actually using a bool type Commented Dec 11, 2012 at 18:11

2 Answers 2

2

This does bit slicing of the integer type. You will access the actual bits of the underlying int representation.

If que_ints[5] is integer 0xdeadbeef, then:

  • que_ints[5][3] is 1
  • que_ints[5][7:4] is 0xe.

In SystemC the range() function is the corollary.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I did not realize ints could be implicitly bit-indexed like bit/logic arrays.
1

arr_ints is an associative array of type int, where the type used as the key is typ_struct.

So arr_ints[obj_struct] will give you an integer.

Indexing an integer type using [n] will give you the bit at index n.

So arr_ints[obj_struct][1] will give you bit 1 of the integer at arr_ints[obj_struct]


On the second line in question:

que_ints is a queue of type int.

So que_ints[i] will give you the integer at location i in the queue.

In que_ints[i][obj_struct], it will implicitly convert the enum type to an integer value (actually bit[2:0]) and give you the bit index based of that.

Comments

Your Answer

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.