Stella CDFJ+ bug

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andrew Davie

Stella currently has a bug in the implementation of CDFJ+ fast LDX/LDY/LDA
I've submitted a fix/issue. Meanwhile, here's a test ROM and what to do to correct it...



32K CDFJ+ test ROM

It should show a red screen in stock Stella and a green one in the corrected patched build

• ldxy_bug.asm: the source. Its header comment explains the bug.
• ldxy_bug.bin: the binary to send.
• ldxy_bug.lst: the listing.
• Copies of the v48 driver blobs, cdfjplus.h and vcs.h, so the folder builds on its own:

dasm ldxy_bug.asm -f3 -oldxy_bug.bin.

How the test works:
ldx #0
lda $A2 ; $A5 $A2: the operand byte $A2 arms the override
inx ; $E8 = 200+32: Stella returns a DSCOMM byte instead of the opcode
cpx #1
beq Pass ; green
; otherwise red

• The bug: CartridgeCDF::peek() arms the LDA#/LDX#/LDY# override whenever any byte read from the cart is $A9, $A2 or $A0, even if that byte is an operand rather than an opcode.
• Why it fires here: the CPU's next access is the read of zero-page $A2, which goes to RIOT RAM and never reaches the cart. So the next cart read Stella sees is the inx opcode fetch. With FF_OFFSET=200, $E8 falls inside the fast-fetch range, and Stella returns a datastream byte in place of the opcode.
• Why the result is predictable: before turning fast fetch on, the ROM uses DSPTR/DSWRITE to put $EA (NOP) at the DSCOMM pointer. Under the bug, inx runs as a NOP, X stays 0, and the screen goes red. The BRK vector also points at the red path, in case some other stray byte gets executed.
• What real hardware does: it only substitutes the operand fetched on the cycle right after an LDA#/LDX#/LDY# opcode fetch. Your Stella patch enforces that with myLDAXYarmCycle.
The ROM uses the same driver settings as WenHop: LDX# and LDY# fast fetch on, offset 200. I checked that the binary contains everything Stella's setupVersion() looks for: the PLUSCDFJ ID, the LDX/LDY enable words at $34C/$350, and the offset word at $3E0. Stella should therefore set it up exactly as it does for the game.

The trigger isn't specific to LDX/LDY. Any instruction whose operand byte is $A9 works, and so does $A2 or $A0 when the driver enables LDX/LDY. Zero-page variables at $A0, $A2 and $A9 are common, so it's easy to hit.


How to fix...

Record the CPU cycle when arming, and require the matching peek to land exactly one cycle later. For example:

// when arming
myLDAXYimmediateOperandAddress = address + 1;
myLDAXYarmCycle = mySystem->cycles();

// when testing
fastfetch = FAST_FETCH_ON(myMode)
            && myLDAXYimmediateOperandAddress == address
            && mySystem->cycles() == myLDAXYarmCycle + 1
            && /* existing range check */;

Alternatively, clear the arm on any non-cartridge access. The new field would need adding to save/load state as well.

CartDPCPlus.cxx:411 (myLDAimmediate = (peekvalue == 0xA9)) appears to use a similar value-based flag and may have the same issue.