1

I'm wondering how to set a 64-bit variable in C++ for my testbench. When I use the variable_name.io_int_write(0,value0) and variable_name.io_int_write(1,value1) (for the lower & upper bits) I can see the variables are set but in the reverse manner.

Ex: When I want it to be 000...002, I see 200...000

Is there an alternate command that would help? Thanks

Edit: I have a function void set_function (set_ * dut)

and inside this function, I need to set a 64-bit variable dut->variable_name

Thanks for your answers, how would I go about fixing the Endianness in this case

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
arthur
  • 81
  • 2
  • 6
  • https://secure.wikimedia.org/wikipedia/en/wiki/Endianness – Fred Foo Mar 28 '11 at 20:49
  • I believe this is related: http://en.wikipedia.org/wiki/Endianness – Chris Cooper Mar 28 '11 at 20:50
  • 7
    Can you elaborate what exactly you are trying to do? For simply setting a 64bit variable, you could declare something as int64_t or uint64_t (both types declared in or ), or even long long. They can be set and handled without any special functions. And yes, what you see there is an endianness problem. – Damon Mar 28 '11 at 20:52
  • I might be wrong, but to me this seems more likely an endianess issue. Could you give us more context? Which is the type of variable_name? hw running the code? what do you mean exactly by "I see..."? – sergico Mar 28 '11 at 20:53
  • Thanks all! I'm using this C++ testbench for my verilog code, and when I said "I see" it was from the wave I got on ModelSim. I have 2 32-bit variables, which make up the 64-bit variable. I want to change the values to see the change in outputs – arthur Mar 28 '11 at 20:58
  • are you aware of big and little endianess of a machine? – fabrizioM Mar 28 '11 at 23:00

1 Answers1

0

If you want to set a 64bit integer variable to a constant value, you can simply say something like:

long long var64bit = 1L;

The L at the end of 1L says that it is a 64bit constant value of 1.

If you are comfortable with hexadecimal and want a 64bit bit-field constant, then the usual way of setting a 64bit variable would look something like this:

unsigned long long bitfield64bit = 0xDEADBEEFDEADBEEFL;

Again, note the trailing L.

If you have two 32bit values that you want to pack into a 64bit value, then you can do this using shift and logical-OR operations as:

long long var64bit = (long long)(((unsigned long long)high_bits << 32) | (unsigned long long)low_bits);

where *high_bits* is the 32bit variable holding the high bits and *low_bits* is the 32bit variable containing the low bits. I do the bit-wise operations on unsigned values because of possibly excess paranoia regarding the sign bit and conversion between 32bit and 64bit integers.

The opposite operations, dividing a 64bit variable into high and low bits can be done as follows:

int high_bits = (int)(var64bit & 0xFFFFFFFFL);
int low_bits = (int)((var64bit >> 32) & 0xFFFFFFFFL);
Carl Staelin
  • 1,067
  • 10
  • 9
  • The `L` suffix just means `long`. `long long` and `unsigned long long` are not (yet) C++ types, – CB Bailey Mar 29 '11 at 06:56
  • 1
    Although it is not part of the official standard, most compilers support an `LL` suffix for long long constants. Check your compiler documentation OP. Also see http://stackoverflow.com/questions/1458923/long-long-in-c-c – GrahamS Mar 29 '11 at 09:20