we are using a struct and a union to map a 32bit microcode instruction to its corresponding bit fields. We have used this method before and it worked fine, except we did it in C++ before, and this one has to be in C.I've removed everything from the program except for the struct for testing purposes, and for some reasone it keeps telling me in Visual Studio:
error C2061: syntax error : identifier 'microFields'
or in Linux:
error: expected specifier-qualifier-list before 'microFields'
and
error: two or more data types in declaration specifiers
As yet, no one has mentioned that the thing the OP is trying to do is dangerous.
The order of bitfields in a structure, in both C and C++, is (you'd better sit down) not defined. That's right, upgrade your compiler and your program could break. The whole union thing is therefore a mistake.
You are much better off writing a couple of routines to encode and decode the bitfields:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
unsignedint micro_encode( struct microFields fields )
{
unsignedint result = 0;
result |= (fields.addr & 0x1FF) << 23;
result |= (fields.cond & 0x003) << 21;
result |= (fields.wr & 0x001) << 20;
result |= (fields.rd & 0x001) << 19;
result |= (fields.mar & 0x001) << 18;
result |= (fields.alu & 0x007) << 15;
result |= (fields.b & 0x01F) << 10;
result |= (fields.a & 0x01F) << 5;
result |= (fields.c & 0x01F);
return result;
}
struct microFields micro_decode( unsignedint code )
{
...
}
It costs you nothing more than a few lines of code and saves you against fragile code that will break with your next compiler upgrade, etc.
BTW, I don't know anything about microcode. I could very well have ordered the bitfields the wrong way in the above example. That's easy enough for you to fix.