1

Can someone explain me this syntax? It comes from the Hi Tech C include file

    /*  STATUS bits */
static volatile bit IRP     @ (unsigned)&STATUS*8+7;
static volatile bit RP1     @ (unsigned)&STATUS*8+6;
static volatile bit RP0     @ (unsigned)&STATUS*8+5;
static volatile bit TO  @ (unsigned)&STATUS*8+4;
static volatile bit PD  @ (unsigned)&STATUS*8+3;
static volatile bit ZERO    @ (unsigned)&STATUS*8+2;
static volatile bit DC      @ (unsigned)&STATUS*8+1;
static volatile bit CARRY   @ (unsigned)&STATUS*8+0;
tcop
  • 381
  • 1
  • 2
  • 11
  • I guess is setting the exact "address" of each single bit in STATUS. – LPs Aug 27 '15 at 15:07
  • The non-standard `@` symbol is usually used for allocating a variable at a specific address. In this case, it seems to be used to name individual bits of a bit field. `(unsigned)&STATUS` will give a pointer to unsigned integer, and an integer is several bytes wide. Then for some reason they multiply this address by 8 and add an offset. It is always best to avoid non-standard crap headers like this: if possible, roll out your own register definitions instead, preferably by using `#define` and the bitwise operators. – Lundin Aug 27 '15 at 15:09
  • There is a former question that answers about the "@" symbol. I don't get the multiplication. – tcop Aug 27 '15 at 16:37
  • Olaf states that the compiler probably uses an 11bit word where the upper 8 bits hold the address while the lower 3 bits hold the bit number. This is NOT stated in the Hi tech C manual for 10/12/16 series but I came to the conclusion that this is the most probable explanation. – tcop Aug 27 '15 at 18:47

1 Answers1

1

I presume these are peripherail hardware registers. The bit type and @ are non-standard. @ places them at absolute addresses given by STATUS. bit tells the compiler that the addresses are actually single bits, so it possibly has to use appropriate instructions (bit-operations).

According to @LPs' comment (and after some thought), this looks like PIC-MCU (you did not state the CPU used). The bit type tells the compiler the addresses of the objects (ZERO, etc.) address single bits in the "RAM" (STATUS is actually a CPU register memory-mapped) address space. The bit-number is packed into the lower 3 bits (bit 0..7) and the byte-address is in the upper bits.

The right side of the @ calculats this bit-address: (8 bits/byte, hence the multiplication) and bit-number (lower 3 bits, hence the addition. Alternatively one could use bit-operators (identical result):

static volatile bit IRP @ ((unsigned)&STATUS << 3) | 7;
...

I'm very sure, @ and bit are explained in the compiler documentation.

Note that the bit-type is actually violating the C standard, as this mandates the smallest addressable type to be char with at least 8 bits and sizeof(char) == 1.

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
  • Hi Tech sounds more like Microchip MCUs – LPs Aug 27 '15 at 15:08
  • 1
    @LPs: Hmm... you might be right (long time not looked at PIC SR), the RP bits for the RAM-Pages. But PIC does not allow to bit-address memory (please read my edit). – too honest for this site Aug 27 '15 at 15:14
  • I am sure this is not explained in the Hi Tech C manual. I searched for it. I don't get the multiplication + bit number. Can you explain it? – tcop Aug 27 '15 at 16:35
  • 1
    And why the downvote?? I hate these dumba**es who downvote without even leaving a comment. – too honest for this site Aug 27 '15 at 16:39
  • @tcop: As I wrote: You have 8 bits/byte, so to get the per-bit-address (`bitaddr`), you have to multiply by 8 and add the bit-index. When accessing the bit, the compiler does the reverse: `bit_number = bitaddr % 8; byte_addr = bitaddr / 8;`. – too honest for this site Aug 27 '15 at 16:41
  • I really cannot find this any where in the Hi tech C manual. I expected to shift 8 times and add by bit number or something like this. – tcop Aug 27 '15 at 16:54
  • @tcop: 1) did you downvote? 2) if yes, please state the rason. "I do not understand" is no reason. 3) I do not understand what your problem in understanding is. If it is **not** in the manual, so be it. And shifting 8 times is a multiplication by 256, but to encode the bit-number in a 8-bit-byte, you only need 3 bits for values 0..7. They could very well have `<< 3` instead of `* 8`, it doesn't matter. Perhaps you should read about binary representation. – too honest for this site Aug 27 '15 at 17:11
  • 1) no...I even gave you a positive vote on your comment. Why the hell are you asking me that? 2) what? 3) I don't understand WHY you have to shift the bits 3 times??? Where did you find this info? I said that it would be more logical to shift it 8 time thus multiply by 256 if for some strange reason the compiler reads the upper 8 bits of the UI. – tcop Aug 27 '15 at 17:20
  • @tcop: Sorry, no offence, I just had such cases already. – too honest for this site Aug 27 '15 at 17:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88112/discussion-between-olaf-and-tcop). – too honest for this site Aug 27 '15 at 17:35
  • @Olaf. I've already voted for your answer but someone has voted negativly so your score is still zero. – tcop Aug 27 '15 at 18:48
  • @tcop: I know (with some reps you can see the number of UV/DV, but not who). I get these unjustified DVs from time to time. I seem to have something unpleasant under my fingernails. You cannot become friends with everyone if you prefer to speak a clear language. – too honest for this site Aug 27 '15 at 18:54