473,607 Members | 2,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4438

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(0xC A) 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
2060
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 data-types of my choosing (eg an array of char, short, or int). The application has to do with generating checksum-type values for files or strings, so speed is important, as I just want to quickly get this value then move on to the next task. As...
27
4205
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 the first 8-bits for example. For me, I think it would be best if I can declare the 32-bits like this: unsigned char bits;
2
1973
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 see how they are implemented. In most cases, that means reading C, not C++. I do have an example of C++ code that works. I thought it might be a good idea to take a different approach than that code takes. Rather than processing the data...
19
5850
by: Lorenzo J. Lucchini | last post by:
My code contains this declaration: : typedef union { : word Word; : struct { : byte Low; : byte High; : } Bytes; : } reg;
5
2383
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 example below (from GBD's THE C BOOK) uses this. Is this a portable method? . /*
5
2397
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 correct value. Regardless what I set the variable to, the value that is returned for a long is always the same value. What's going on...can anyone help me? A short version of the code follows:
21
3872
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 applies in this case though: #include <limits.h> unsigned foo(void) { static const union { unsigned char str;
33
1804
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) { unsigned short s = 65535; printf("Size of unsigned short: %d bytes\n", sizeof(unsigned short)); // 2 bytes - OK
6
1679
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
8049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8469
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8128
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8322
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6803
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5997
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3953
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1574
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.