I had an issue that made me scratching my head for a few days…

I was having some bytes of memory (supposed to be ‘data’) tagged as ‘instruction’…
And it was giving bad results, false positives, etc. in the resulting memory map produced when loading a binary!

Producing a bad representation of the memory or the instructions in the debugger. Whereas running directly the code was correct…

I ended up executing the memory mapper step by step until I found the bug.

Instructions are made of one byte for the opcode and potentially – based on the opcode (and the addressing mode) – one or two bytes for the operand(s).

And the memory is mapped in consequence:

typedef enum
{
    kMemoryTypeUnknown      = 0,

    kMemoryTypeData         = 1 << 0,                                   // 001

    kMemoryTypeInstruction  = 1 << 1,                                   // 010
    kMemoryTypeOperand      = kMemoryTypeInstruction + kMemoryTypeData, // 011

    kMemoryTypeBreakPoint   = 1 << 2,                                   // 100

} MemoryType;

(note the operand type made of instruction + data ^^)

And I noticed a tiny single difference for the Bxx instructions: the memory mapper was not taking into account the opcode byte in the relative branch, just the operand count…

This is now fixed and I enhanced the debugger a bit…

The following source code:

; functional test
; branch offset test

        ORG $400

bra_test:
        LDX #5
        JMP back_test

        DC.B 0,1,2,3

forward_test:
        LDY #5
        BNE forward
        JMP bra_err

; forward landing zone
        DEY
        DEY
        DEY
        DEY
        DEY
forward:
        DEY
        DEY
        DEY
        DEY
        DEY
        BEQ bra_ok
        JMP bra_err

; backward landing zone
        DEX
        DEX
        DEX
        DEX
        DEX
backward:
        DEX
        DEX
        DEX
        DEX
        DEX
        BEQ forward_test
        JMP bra_err

backward_test:
        BNE back
bra_err:
        LDA #0
        RTS
bra_ok:
        LDA #1
        RTS

Gives the following result:

I can advance at a better pace now ^^