Question

I'm using C++ for hardware-based model design with SystemC. SystemC as a C++ extension introduces specific datatypes useful for signal and byte descriptions.

How can I access the first bits of a datatype in general, like:

sc_bv<16> R0;

or access the first four bits of tmp.

int my_array[42];
int tmp = my_array[1];

sc_bv is a bit-vector data-type, that's storing binary sequences. Now I want the first four bits of that data-type e. g.. My background is C# and Java, therefore I miss some of the OOP and Reflexion based API constructs in general. I need to perform conversion on this low-level stuff. Useful introductory stuff would help a lot.

Thanks :), wishi

Was it helpful?

Solution

For sc_bv, you can use the indexing operator []

For the int, just use normal bitwise operations with constants, e.g. the least significant bit in tmp is tmp & 1

OTHER TIPS

I can't really speak for SystemC (sounds interesting though). In normal C you'd read out the lower four bits with a mask like so:

temp = R0 & 0xf;

and write into only the lower four bits (assuming a 32-bit register, and temp<16) like so:

R0 = (R0 & 0xfffffff0) | temp;

To access the first four (i assume you mean four highest bits) bits of tmp (ie to get their values) you use bit masks. So if you want to know if for example the second bit is set you do the following:

int second_bit = (tmp & 0x4000000) >> 30;

now second_bit is 1 if the bit is set and zero otherwise. The idea behind this is the following:

Imagine tmp is (in binary)
1101 0000 0000 0000 0000 0000 0000 0000
Now you use bitwise AND ( the & ) with the following value
0100 0000 0000 0000 0000 0000 0000 0000 // which is 0x40000000 in hex
ANDing produces a 1 on the given bit if and only if both operands have corresponding bits set (they are both 1). So the result will be:
0100 0000 0000 0000 0000 0000 0000 0000
Then you shift this 30 bits to the right, which makes it be:
0000 0000 0000 0000 0000 0000 0000 0001 \\ which is 1
Note that if the original value had the tested bit zero, the result would be zero.

This way you can test any bit you like, you just need to provide correct mask. Note that i assumed here that int is 32bits wide, which should be true in most cases.

You will have to know a bit more about sc_bv to amke sure you get the right information. Also, when you say the "first four bytes" I assume you mean the "first four bits." However, that is misleading as well, because you really want to delineate between the low-order or high-order bits.

In any event, you use the C bitwise operators for this kind of thing. However, you will need to know the size of the integer values AND the "endian-ness" of the runtime architecture to get that right.

But, if you REALLY want just the first four bits, then you would do something like this...

inline unsigned char
first_4_bits(void const * ptr)
{
  return (*reinterpret_cast<unsigned char const *>(ptr) & 0xf0) >> 4;
}

and that will grab the very first 4 bits of what it being pointed at. So, if the first byte pointed-to is 0x38, then this function will return the first 4 bits, so the result will be 3.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top