For my multi-platform emulator, I want the following
Code:
if (System == PlayStation)
{
struct _CPU {
unsigned long int A;
unsigned long Register[32];
...
}
}
if (System == NES)
{
struct _CPU {
unsigned char A;
unsigned char S;
...
}
}
Basically, I need to define the registers in memory in real time, can I use new/malloc to do this? I know that they can be used to create arrays, but all I need is registers.
Wouldn't it be easier to just take entirely different code paths depending on the system (implement each cpu emulator as a separate class or something)?
mic_ wrote:
Wouldn't it be easier to just take entirely different code paths depending on the system (implement each cpu emulator as a separate class or something)?
I have that at the moment. But I hate having to have NES.A and SNES.A and PS1.A etc. CPU.A is so much easier to write with.
Look into the
union keyword.
Or better yet, look into C++ and having CPUs extend an abstract class.
This is a great idea:
Code:
struct _CPU {
unsigned char A8;
unsigned short int A16;
unsigned long int A32;
...
Just another quick question, can struct exist inside of a class? If so how.
In the C++ language, a 'struct' and a 'class' are exactly the same thing, except for two minor details. Classes can have constructors, copy constructors, and destructors, and structs can have conclassors, copy conclassors, and declassors, and they do the same thing.
The first difference is the privilege of the first member: 'public' in a struct or 'private' in a class. This makes it harder to forget to specify privilege (public, protected, or private) if you use 'class'.
The second difference comes into play only in an environment that mixes code in the C language with code in the C++ language. Common examples of such an environment involve a library in one language and an application in the other. C recognizes only the 'struct' keyword and only POD (plain old data) fields. So a lot of programmers use 'struct' for POD types and 'class' for other types, so that others know which types are usable from C code.
So a struct can exist in a class in exactly the same way as a class in a class or a struct in a struct.
Code:
class foo
{
public:
foo() {}
~foo() {}
struct
{
int a;
}bar;
};
WedNESday wrote:
This is a great idea:
Code:
struct _CPU {
unsigned char A8;
unsigned short int A16;
unsigned long int A32;
...
...if you want your A32 to be 64 bits on common Linux compilers for a 64-bit processor. Try uint8_t A8; uint16_t A16; uint32_t A32; But what are these for, anyway?