Chris
|
Pointers and Structures
Posted: Mon Jul 12, 1999 1:13 am
|
C is pissing me off! I know and understand what it takes to emulate a CPU but I can't express
my codes in C and everyday I'm wasting hours of valuable time flipping throughout these C
books figuring out this stuff.
My code is simple, all aspects of the system is contained in a definable structure called Z80.
The structure uses pointers...
/*Little Endian Word Register*/
typedef struct {
BYTE low;
BYTE high;
} REG;
/*Z80 Processor*/
typedef struct {
REG AF;
REG BC;
REG DE;
REG HL;
BYTE *IX;
BYTE *IY;
BYTE *SP;
WORD *PC;
WORD cycles;
} Z80;
Now, my emulator uses a function called ExecInst and it's paremeters include the Z80 structure,
the ROM, and the RAM. ROM and RAM are both pointers. I can't even get it to check the fucking
program counter because I can't get my codes to get the value the program counter is pointing to
and not the value of the program counter itself. For Example:
I declare a variable that's not contained in a structure. So if I pass it's parameters into a function
I simply do this:
ExecInst (BYTE *zrom); /*Just declaring*/
...
...
Later on down in the code I use that function...
ExecInst(ROM);
I can get the information that ROM is pointing to by writing...
data = *zrom;
If I want to work with the ROM's value, I simply write...
data = zrom;
Sorry, getting back to my problem. In my emu I call the function by writing this...
ExecInst(CPU,ROM,RAM);
Now, I can't even interpret the instructions contained in ROM because my compiler says my code
is wrong. This is what I wrote...
switch(zcpu->PC)
{
}
My compiler says that I'm not need a pointer or an * sign or something or another. I'm all confused
and pissed. Help.
Better yet, if you really want to help me, E-Mail me and I'll send you my source.
Chris :o(
|
Chris
|
My E-Mail
Posted: Mon Jul 12, 1999 1:14 am
|
I forgot to add that in last message.
Chris :o)
|
Eric
|
Posted: Mon Jul 12, 1999 2:45 am
|
Quote > switch(zcpu->PC)
> {
> }
First, if zcpu is NOT a POINTER to a Z80 structure then you can not use the -> operator. (I'm not even sure that the -> operator can be used in C, I thought it was a C++ operator. Any one out there know for sure?)
If zcpu IS a POINTER to a Z80 structure, did you actually allocate the Z80 structure using malloc? If you didn't your program will crash.
Second, (this is where your problem is,) PC is a POINTER to a WORD. If you want to use the value pointed to by PC (i.e., dereference PC) then you need to use the * operator.
For example, to dereference PC do this:
*(zcpu->PC)
This example assumes zcpu is a POINTER to a Z80 structure which has been allocated (and that the -> operator is a valid C operator).
If zcpu is just a Z80 structure, do this:
*(zcpu.PC)
The -> operator is used to deference a pointer to a structure (or class in C++), and access a field all at once (a common operation). It is NOT used to dereference a field that is itself a pointer to something. If the field (PC in your case) is a pointer, you need to use a * to dereference it.
Eric Quinn
|
Chris
|
Posted: Mon Jul 12, 1999 5:30 am
|
Well, using...
*(zcpu.PC)
fixed the problems within the ExecInst function but now it says bad parameters or something. I'll send
you my source code.
Chris :o)
|