Refactoring a large C ++ class

I am writing a nes emulator and have written one class representing 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 transition table that accepts the operation code and performs the corresponding member function - changing the internal state of the processor. There are other tasks that determine the correct type of addressing.

I realized that the class definition is too large and difficult to parse, as well as verify. However, dividing a class into separate classes seems problematic, since almost every function changes the internal state of the processor.

I was thinking about classifying command types as such:

 class AInstructionHandler {/*...*/}; class CArithmeticInstHandler : public AInstructionHandler{/*...*/}; class CBranchInstHandler : public AInstructionHandler{/*...*/}; /*memory accessors, logical, etc. */ 

However, I would have to give each class handler class access to the internal state of the processor, making each class different, which seems bad.

I am 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 has flaws to start with.

thanks

+6
source share
1 answer

Well, if there are not enough large logical fragments, perhaps you can find smaller bits to place in your classes. You mentioned JumpTable , which is a candidate for its class. He can take the operation code and return the address, maybe?

Then there are OpCodes themselves, which can become objects that make some changes to state 6502. Thus, for this you probably want to pack the related bits in the internal state of the CPU.

Once you start removing smaller chunks, it might seem that other refactorings may occur.

There are also related StackOverflow answers that may help. Here are some additional thoughts from Dr. Dobb.

+3
source

Source: https://habr.com/ru/post/987583/


All Articles