J
James Roberge
I am having a little trouble getting my union/struct to work correctly.
I am creating a struct that will contain information about the status of
various Z80 cpu registers in an emulator i am trying to write.
some of the registers such as "DE" can be accessed as 16 bit data or the
high and low bytes can be accessed separately. SO, "DE" refers to the
16 bit data, where "D" and "E" refer to the high and low bytes respectively.
Here is what i have so far.
typedef union {
struct {
unsigned char L; //Swapped cause i am little endian
unsigned char H;
} B;
unsigned short W;
} PAIR;
struct Registers {
unsigned char A; // A Register, all is good here.
PAIR DE; // use the above union to emulate.
};
Now, all is ok, when i access the registers like this.
Registers regs1;
regs1.A = 0xFF; //Works as expected
regs1.DE.W = 0xCAFE; //Set 16 bit register
//therefore
regs1.DE.B.H == 0xCA
regs1.DE.B.L == 0xFE
The problem is i find this too ugly. and i would like for the above
code to look more like this and still work exactly the same.
Registers regs1;
regs1.A = 0xFF; //Works as expected
regs1.DE = 0xCAFE; //Set 16 bit register
//therefore
regs1.D == 0xCA
regs1.E == 0xFE
How would i go about defining this union/structure so that i can access
them more gracefully? Does it involve anonymous unions/structures, and
are anonymous unions and structs valid standard C++?
Thanks
James
I am creating a struct that will contain information about the status of
various Z80 cpu registers in an emulator i am trying to write.
some of the registers such as "DE" can be accessed as 16 bit data or the
high and low bytes can be accessed separately. SO, "DE" refers to the
16 bit data, where "D" and "E" refer to the high and low bytes respectively.
Here is what i have so far.
typedef union {
struct {
unsigned char L; //Swapped cause i am little endian
unsigned char H;
} B;
unsigned short W;
} PAIR;
struct Registers {
unsigned char A; // A Register, all is good here.
PAIR DE; // use the above union to emulate.
};
Now, all is ok, when i access the registers like this.
Registers regs1;
regs1.A = 0xFF; //Works as expected
regs1.DE.W = 0xCAFE; //Set 16 bit register
//therefore
regs1.DE.B.H == 0xCA
regs1.DE.B.L == 0xFE
The problem is i find this too ugly. and i would like for the above
code to look more like this and still work exactly the same.
Registers regs1;
regs1.A = 0xFF; //Works as expected
regs1.DE = 0xCAFE; //Set 16 bit register
//therefore
regs1.D == 0xCA
regs1.E == 0xFE
How would i go about defining this union/structure so that i can access
them more gracefully? Does it involve anonymous unions/structures, and
are anonymous unions and structs valid standard C++?
Thanks
James