Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

It seems excessive and not very OO to do it with a class per op-codeclass per op-code.

It seems excessive and not very OO to do it with a class per op-code.

It seems excessive and not very OO to do it with a class per op-code.

added 114 characters in body; edited tags; edited title
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481

What's the best method of op Op-code decoding in an emulator?

I've written the larger part of an emulator for a 6502 CPUemulator for a 6502 CPU but my method of op-code decodingop-code decoding is giving me concern, mainly because of how long the method is getting but also because I need a variable for each code, I need to declare them three times and I've stuck them all in a very non-OO classnon-OO class. I wouldn't mind some scathing critique.

What's the best method of op-code decoding in an emulator?

I've written the larger part of an emulator for a 6502 CPU but my method of op-code decoding is giving me concern, mainly because of how long the method is getting but also because I need a variable for each code, I need to declare them three times and I've stuck them all in a very non-OO class. I wouldn't mind some scathing critique.

Op-code decoding in an emulator

I've written the larger part of an emulator for a 6502 CPU but my method of op-code decoding is giving me concern, mainly because of how long the method is getting but also because I need a variable for each code, I need to declare them three times and I've stuck them all in a very non-OO class. I wouldn't mind some scathing critique.

Source Link
Ross Drew
  • 319
  • 2
  • 12

What's the best method of op-code decoding in an emulator?

I've written the larger part of an emulator for a 6502 CPU but my method of op-code decoding is giving me concern, mainly because of how long the method is getting but also because I need a variable for each code, I need to declare them three times and I've stuck them all in a very non-OO class. I wouldn't mind some scathing critique.

I've written it in a TDD fashion (in an effort to understand pure TDD) which has led to the simplest implementation with refactors only when necessary. Even though I think this is probably the most efficient solution (making full use of the benefits of a switch statement), I'm thinking there's maybe a better way.

Extract:-

public void step() {
    System.out.println("\n*** STEP >>>");

    int accumulatorBeforeOperation = registers.getRegister(Registers.REG_ACCUMULATOR);
    int opCode = nextProgramByte();

    //Execute the opcode
    System.out.println("Instruction: " + InstructionSet.getOpCodeName(opCode) + "...");
    switch (opCode){
        case InstructionSet.OP_ASL_A: {
            int newFakeByte = registers.getRegister(Registers.REG_ACCUMULATOR) << 1;
            setCarryFlagBasedOn(newFakeByte);
            registers.setRegisterAndFlags(Registers.REG_ACCUMULATOR, newFakeByte);
        }
        break;

        case InstructionSet.OP_ASL_Z: {
            int location = nextProgramByte();
            int newFakeByte = memory.getByte(location) << 1;
            setCarryFlagBasedOn(newFakeByte);
            registers.setFlagsBasedOn(newFakeByte);
            memory.setByteAt(location, newFakeByte);
        }
        break;
        ...
        case InstructionSet.OP_DEY:
            registers.decrementRegisterWithFlags(Registers.REG_Y_INDEX);
            break;

        case InstructionSet.OP_LDX_I:
            registers.setRegisterAndFlags(Registers.REG_X_INDEX, nextProgramByte());
            break;

        case InstructionSet.OP_LDX_Z:
            registers.setRegisterAndFlags(Registers.REG_X_INDEX, getByteOfMemoryAt(nextProgramByte()));
            break;
        ...
        default:
            throw new UnknownOpCodeException("Unknown 6502 OpCode:" + opCode + " encountered.", opCode);
    }

Thoughts

I've looked at storing op-codes as Enums but it means reading bytes (in Javas case this is int represented bytes) and writing conversion code from raw byte to Enum but that seems unnecessary for the small maintainability improvement.

It seems excessive and not very OO to do it with a class per op-code.