473,247 Members | 3,145 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,247 software developers and data experts.

Accessing high and low bytes of a unsigned short in a struct.

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
Jul 23 '05 #1
4 4366

James Roberge wrote:
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

You could write a wrapper class containing a union and define members
SetHi(unsigned char h) and SetLo(unsigned char l), as well as
corresponding Get methods. An overloaded operator= could be used to
assign unsigned shorts to the register as a whole. Of course, it's a
matter of taste whether you think it is prettier to say
regs1.SetLo(0xCA) than regs1.B.L = 0xCA. Of course you could write much
more sophisticated methods that behave more like you want.

Some people consider unions not proper C++, but this seems like a
proper use for them. You could define a class that stores the data as
two bytes and calculates the conversions to and from a 16 bit word on
the fly, or do it the other way around - store as a word, and have
member functions calculate or manipulate the byte values. I think a
union would do nicely here though.

regards Mark

Jul 23 '05 #2
On Thu, 12 May 2005 06:41:02 +0000, James Roberge wrote:
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++?


Seems like you could use an alias (aka "reference"):

typedef union {
struct {
unsigned char L; //Swapped cause i am little endian
unsigned char H;
} B;
unsigned short W;
} PAIR;

struct Registers
{
Registers() : DE(DEReg.W), D(DEReg.B.H), E(DEReg.B.L)
{
}

unsigned char A;
unsigned short& DE;
unsigned char& D;
unsigned char& E;

private:
PAIR DEReg; // Don't want direct access.
};

int main()
{
Registers r;

r.A = 0x71;
r.D = 0xa0;
r.E = 0x65;
return 0;
}

HTH

- Jay
Jul 23 '05 #3
James Roberge wrote:
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.


there are annonymous unions:

struct register {
unsigned short W;
union {
unsigned char h;
unsigned char l;
};
};

you use it:

int main()
{
register x;
x.W = 1; // 16 bit access
x.h = 3; // 8 bit access
x.l = 1;
}

cheers,
Michael

Jul 23 '05 #4
James Roberge wrote:
I am having a little trouble getting my union/struct to work correctly.

[SNIP]

Ok, welli seem to have it working a bit more to my liking. here is what
i have.

typedef unsigned char BYTE;
typedef unsigned short WORD;

typedef union Z80RegisterPair {
struct {
BYTE L;
BYTE H;
};
WORD W;
} PAIR;

struct Z80Registers {
BYTE A, Ax;
PAIR BC, BCx;
PAIR DE, DEx;
PAIR HL, HLx;
};

This allows me to access like this

Z80Registers regs1;

regs1.DE.W = 0xCAFE; //For the 16 bit value.
//therefore
regs1.DE.H == 0xCA;
regs1.DE.L == 0xFE;
Any other suggestions would be great though.

James
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: J. Campbell | last post by:
I have a feeling that I'm doing things all ass-backwards (again ;-), and would like some advice. What I want to do is: put some data to memory and then access that memory space as an array of...
27
by: Daniel Lidström | last post by:
Hello! I want to work with individual bytes of integers. I know that ints are 32-bit and will always be. Sometimes I want to work with the entire 32-bits, and other times I want to modify just...
2
by: Steven T. Hatton | last post by:
I'm trying to parse an ELF file to build a human readable representation. I know it's been done before, and there are tools such as objdump, nm, readelf, c++filt, etc. I can look at the source to...
19
by: Lorenzo J. Lucchini | last post by:
My code contains this declaration: : typedef union { : word Word; : struct { : byte Low; : byte High; : } Bytes; : } reg;
5
by: Kobu | last post by:
In embedded systems (programmed in C), often times structure declarations are used to group together several status/control/data registers of external hardware (or even internal registers). The...
5
by: Andy | last post by:
I'm having trouble accessing an unmanaged long from a managed class in VC++.NET When I do, the contents of the variable seem to be mangled. If I access the same variable byte-by-byte, I get the...
21
by: Hallvard B Furuseth | last post by:
Is the code below valid? Generally a value must be accessed through the same type it was stored as, but there is an exception for data stored through a character type. I'm not sure if that...
33
by: Hahnemann | last post by:
Does anybody know the answer to the following? An unsigned short is 2 bytes long. Why is the following file created as 4 bytes instead of 2? file = fopen("data.bin", "wb"); if (file != NULL)...
6
by: ssubbarayan | last post by:
Dear all, I developed the following program: void parsebytes(unsigned char* data); struct info { unsigned char day; unsigned char month; short year;
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.