Actually, it hasn't swapped nibbles, it has swapped bytes. But more
accurately, you
have swapped bytes.
When you declare:
nibble.buffb[0]=0x12;
nibble.buffb[1]=0x34;
You are declaring them backwards.
The output is accurate:
nibble1 2
nibble2 1
nibble3 4
nibble4 3
Mem location Zero is the first one, and mem location one is the second. The
output is
exactly the same as the second example except w/ the bytes reversed.
As you have the union laid out, you have nibbles 1 and 2 corresponding to
buffb[1], and
3 and 4 corresponding to buffb[0]. Which is correct. In looking at the
documentation on
struct, it's not totally obvious that the elements are actually allocated in
reverse
order. For instance, in your case, the first mem location is nibble4, then
nibble3, and
so on. With this knowledge, it is obvious that the declaration is correct.
The only way I was able to surmise this was by looking at the struct section of
my C++
book. It references the function biosequipt( ) which returns an int that
contains a
bunch of useful bits. The documentation for that function in my Borland
Compiler gave
this:
Return Value:
The return value is interpreted as a collection of bit-sized fields.
+------------------- Number of parallel printers installed:
| 00 = 0; 01 = 1; 10 = 2; 11 = 3
| +--------------- Serial printer attached
| | +------------ Game I/O attached
| | | +------ Number of COM ports: 000 = 0, 001 = 1,..., 111 = 7
| | | | + Direct memory access (DMA)
| | | | | 0 = Machine has DMA; 1 = Machine doesn't have DMA
+-----+--+--+--------+--+-----------------------+
|15|14|13|12|11|10| 9| 8| 7| 6| 5| 4| 3| 2| 1| 0|
+-----------------------+-----+-----+-----+--+--+
Number of disk drives: ---+ | | | |
00 = 0; 01 = 1; 10 = 2; 11 = 3 | | | |
Initial video mode -------------+ | | |
00 = Unused | | |
01 = 40x25 BW with color card | | |
10 = 80x25 BW with color card | | |
11 = 80x25 BW with mono card | | |
Motherboard RAM size -----------------+ | |
00 = 16K; 01 = 32K; 10 = 48K; 11 = 64K | |
Floating-point coprocessor ----------------+ |
Boot from disk -------------------------------+
(obviously quite an outdated function)
And the struct that accesses that is declared as:
struct equipment {
unsigned hasdiskette : 1;
unsigned hascoprocessor: 1;
unsigned planar : 2;
unsigned videomode : 2;
unsigned numfloppy : 2;
unsigned hasdma: 1;
unsigned numserial : 3;
unsigned gameadaptor : 1;
unsigned serialprinter: 1;
unsigned numprinters : 2;
};
union twotypes {
struct equipment eq; /* The bit field structure */
int k; /* Same bytes as an integer */
};
Hope this cleares it up.
-Mark Stokes
-----Original Message-----
From: Ibrahim Saidi [mailto:[email protected]]
Sent: Thursday, November 20, 2003 5:46 AM
To: [email protected]
Subject: Re: [Mspgcc-users] [Fwd: Structure help]
Hi,
refrence to the problem.....
I compiled the following code on just a regular GNU gcc compiler... (for my
pentium machine target, not the msp430)
see the output below:
union nibble_t {
struct nibbles {
unsigned nibble1:4;
unsigned nibble2:4;
unsigned nibble3:4;
unsigned nibble4:4;
};
unsigned char buffb[2];
unsigned buff;
};
int main(void)
{
int i;
union nibble_t nibble;
nibble.buffb[0]=0x12;
nibble.buffb[1]=0x34;
printf("\n2 bytes\n");
printf("nibble1 %x \n",nibble.nibble1);
printf("nibble2 %x \n",nibble.nibble2);
printf("nibble3 %x \n",nibble.nibble3);
printf("nibble4 %x \n",nibble.nibble4);
printf("\none word\n");
nibble.buff=0x1234;
printf("nibble1 %x \n",nibble.nibble1);
printf("nibble2 %x \n",nibble.nibble2);
printf("nibble3 %x \n",nibble.nibble3);
printf("nibble4 %x \n",nibble.nibble4);
return 0;
}
------------------------------- OUTPUT-----------------------
2 bytes
nibble1 2
nibble2 1
nibble3 4
nibble4 3
one word
nibble1 4
nibble2 3
nibble3 2
nibble4 1
the nibbles are swapped.... depending on access type.... how do we fix
this... that cuasing me problems in my structure.... write to it using a
one byte buff[i].. and then i want to access the low-nibble of the 5th
byte... but it gives me the high-nibble of the 5th byte... becuase of
swaping... i thought endian problems are only for accessing structures
larger than bytes.... any one have any ideas how can I get over this
problem...
thanks...
Abe
On November 19, 2003 12:27 pm, Garst R. Reese wrote:
> Sorry, they were in the original fwd.
> G.
>
> Dmitry wrote:
> > Oops...
> > where are bitfields?
> > ~d
> >
> > On Wednesday 19 November 2003 18:29, Garst R. Reese wrote:
> > > Dmitry wrote:
> > > > hm...
> > > > any code snippet?
> > > > ~d
> > >
> > > Sure, let me know if you need more.
> > > Thanks,
> > > Garst
> > >
> > > #ifdef _MMC_TEST
> > > int main(void)
> > > {
> > > unsigned int i;
> > > union reg16b_t reg16b;
> > > mcu_init();
> > > spi_init();
> > > lcd_init();
> > > clear_display();
> > > lcd_set_pos(0,0);
> > > get_register(MMC_SEND_CSD,®16b);
> > > print_register(®16b);
> > > print_response(0xEE);
> > > print_response(reg16b.csd.READ_BL_LEN);
> > > SPI_NONE;
> > > }
> > > #endif
> > >
> > >
> > > int get_register( unsigned char reg_cmd, union reg16b_t *reg16) {
> > > unsigned char arguments[4];
> > > unsigned char temp25;
> > > int i;
> > >
> > > arguments[0]=0x00;
> > > arguments[1]=0x00;
> > > arguments[2]=0x00;
> > > arguments[3]=0x00;
> > > temp25=mmc_sendcmd(reg_cmd,arguments);
> > > if(temp25)
> > > {
> > > SPI_NONE;
> > > SPI_LCD_EN;
> > > lcd_write_string("GETREG");
> > > print_response(temp25);
> > > BREAKPNT;
> > > return FALSE;
> > > }
> > > mmc_get_data((unsigned char*) reg16);
> > > return TRUE;
> > > }
> > >
> > >
> > > int mmc_get_data(unsigned char* data)
> > > {
> > > unsigned char temp25;
> > > int i ;
> > >
> > > while((temp25=mmc_get_response())==0); //if card is busy
> > > if(temp25!=0xFE)
> > > {
> > > BREAKPNT;
> > > return FALSE;
> > > }
> > >
> > > /*read the data*/
> > > for(i=0;i<16;i++)
> > > {
> > > data[i]=spi_read();
> > > }
> > >
> > > //CRC TOKENs
> > > spi_read();
> > > spi_read();
> > >
> > > return TRUE;
> > > }
> > >
> > >
> > > -------------------------------------------------------
> > > This SF.net email is sponsored by: SF.net Giveback Program. Does
> > > SourceForge.net help you be more productive? Does it help you
> > > create better code? SHARE THE LOVE, and help us help YOU! Click
> > > Here: http://sourceforge.net/donate/
> > > _______________________________________________
> > > Mspgcc-users mailing list [email protected]
> > > https://lists.sourceforge.net/lists/listinfo/mspgcc-users
> >
> > --
> > /*****************************************************************
> > ("`-''-/").___..--''"`-._ (\ Dimmy the Wild UA1ACZ
> > `6_ 6 ) `-. ( ).`-.__.`) State Polytechnical Univ.
> > (_Y_.)' ._ ) `._ `. ``-..-' Radio-Physics Departament
> > _..`--'_..-_/ /--'_.' ,' Saint Petersburg, Russia
> > (il),-'' (li),' ((!.-' +7 (812) 5403923, 5585314
> > *****************************************************************/
> >
> > -------------------------------------------------------
> > This SF.net email is sponsored by: SF.net Giveback Program. Does
> > SourceForge.net help you be more productive? Does it help you
> > create better code? SHARE THE LOVE, and help us help YOU! Click
> > Here: http://sourceforge.net/donate/
> > _______________________________________________
> > Mspgcc-users mailing list [email protected]
> > https://lists.sourceforge.net/lists/listinfo/mspgcc-users
-------------------------------------------------------
This SF.net email is sponsored by: SF.net Giveback Program. Does
SourceForge.net help
you be more productive? Does it help you create better code? SHARE THE LOVE,
and help
us help YOU! Click Here: http://sourceforge.net/donate/
_______________________________________________
Mspgcc-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mspgcc-users