I am writing a nes emulator and wrote a single class representing the 6502 cpu:
class CCpu6502 {
public:
/*....*/
void fetch8();
void fetch16();
void ADC();
void AND();
/* Around 50+ cpu instruction types */
private:
/*register state, jump table definition*/
};
Basically, I have a jump table that takes in opcode and executes the appropriate member function - changing the cpu's internal state. There are some other tasks that determine the correct addressing type.
I've realized the class definition is way too large and difficult to parse as well as test. However, splitting up the class into separate classes seems problematic because almost every function will alter the cpu's internal state.
I thought about classifying the instruction types as such:
class AInstructionHandler {/*...*/};
class CArithmeticInstHandler : public AInstructionHandler{/*...*/};
class CBranchInstHandler : public AInstructionHandler{/*...*/};
/*memory accessors, logical, etc. */
However,I would then have to give each instruction handler class access to the internal state of the cpu by making each class a friend, which seems like a bad idea.
I wondering if there is a preferred way to refactor a large class where almost all methods affect the state of the object or if my design is flawed to begin with.
Thanks
Well, if there are not large logical chunks to break off, then perhaps you can find smaller bits to place into their own classes. You mentioned the JumpTable, which is a candidate for its own class. It could take an opcode and return an address, perhaps?
Then, there are the OpCodes themselves, which could become objects that do some altering of the state of the 6502. So, to do that, you would probably want to package up related bits of the internal state of the CPU.
Once you begin removing smaller pieces, it may become obvious what other refactorings can take place.
There are also some related answers on StackOverflow that may help. Here are some additional thoughts from Dr. Dobb's.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With