6809 CPU Emulator project started

CpuEmulator is a CPU emulator project that aims to emulate a CPU (the 6809 to start with), it’s registers, and system memory. It will allow you to monitor registers, flags and memory as you step through code. Having done SharpiEmu for the Sharp PC–1360 Pocket Computer, the concept seems pretty simple. The 6809 has a number of addressing modes, and different opcodes for the same mnemonic. So I’m expecting it to be somewhat more of a challenge.

Initial version will emulate the CPU, memory, and text video mode.

Features

  • Emulate 6809 instructions
  • Emulate 32×16 & 80×24 text modes
  • Emulate the Coco3’s MMU
  • 4K all the way up to 512KB memory options
  • Debugger that steps through code, shows current state of registers, selected memory block, and source code from a listing file.
  • Break points. They are remembered between runs.
  • Memory watcher. When memory at a certain addresses are modified, a breakpoint is triggered so you can see what modified it.

 


Ruff draft of what the UI will look like

I cut my teeth in C & C++. Since I’ve been programming in Java since 2001, it will be nice to get reacquainted with C++ and get familiar with the newer C++11 and C++14. I am also using it as an opportunity to learn the Qt framework for building cross platform GUI aps. So far I’m very impressed with Qt. It has a full featured C++ IDE to boot! Xcode, Objective C and Swift are nice, but I don’t want to have to recode for each OS. That’s actually why I learned Java in the first place. Cross platform language.

As I plan to use the full 128KB (perhaps 512K) of the Coco 3 in my upcoming Bounce Ball 2 game, I will be emulating the MMU.

Both the 32×16 and 80×24 text modes to allow for some program output to the user. Emulation will be good enough to play BB1. If I find the text modes easy enough, I may do a graphics mode as well.

So far I have been spending time figuring out how to structure the code to handle the addressing modes, and the large number of instructions.

Since there are a large number of op codes, I don’t want to have a huge switch block for each one. So I created a map, and pass in a pointer to the method that should be called for the opcode.

This way, when emulate() is called, I can look at a byte of memory, lookup the opcode, and just call the method directly.

class Opcode
{
protected:
    Cpu6809* m_cpu;
    void (Cpu6809::*m_method)();

public:
    Opcode(Cpu6809* cpu,
           void (Cpu6809::*method)())
        : m_cpu(cpu),
          m_method(method)
    {
        ...
    }

    void method() {(m_cpu->*m_method)();}
    ...
}

class Cpu6809
{
public:
    Cpu6809() : m_regs() 
    {
        op = new Opcode(0x3a,"abx",INHERENT,3,  
            this,&Cpu6809::abx); 
        m_opcodes[op->opcode()] = op;

        op = new Opcode(0xcc,"ldd",IMMEDIATE,2, 
            this,&Cpu6809::ldd); 
        m_opcodes[op->opcode()] = op;

        op = new Opcode(0xdc,"ldd",DIRECT,2,    
            this,&Cpu6809::ldd); 
        m_opcodes[op->opcode()] = op;
        ...
    }

    void emulate() 
    {
        Opcode* op = m_opcodes[peek(m_regs.pc())];
        if(op)
        {
            op->method();
        }
    }

    void abx() {...}
    void ldd(){...}
}