Skip to content

AF2inuse and BC2inuse never change, JAF and JAR don't work #9

@Konamiman

Description

@Konamiman

This is a duplicate of #7, created again because I accidentally referenced it as "fixes" in Konamiman#1, and thus when it was merged the issue was automatically closed

There's a bug in the implementation of the EX AF,AF' and EXX instructions that causes the AF2inuse and BC2inuse fields in the z280_state struct to never be updated, this causes the JAF and JAR instructions to fail (they never jump). This is how EX AF,AF' is implemented:

#define EX_AF {                                                 \
	union PAIR tmp;                                                   \
	tmp = cpustate->AF; cpustate->AF = cpustate->AF2; cpustate->AF2 = tmp;          \
	cpustate->AF2inuse = !!cpustate->AF2inuse;                  \
}

That !! in the last line should be ~. And the same for EXX.

I have unit tests in my .NET wrapper project to verify this behavior: with the original code the tests fail, replacing !! with ~ the tests pass. For reference, here's one of the tests:

[Test]
public void TestJarAndJaf()
{
    AssembleAndRun(@"
        ld b,1
        jaf JUMP_1
        ld b,2
    JUMP_1:

        ex af,af

        ld c,1
        jaf JUMP_2
        ld c,2
    JUMP_2:

        ld ix,1
        jar JUMP_3
        ld ix,2
    JUMP_3:

        exx

        ld iy,1
        jar JUMP_4
        ld iy,2
    JUMP_4:

        exx
        ret");

    Assert.That(z280.B, Is.EqualTo(2));
    Assert.That(z280.C, Is.EqualTo(1));
    Assert.That(z280.IX, Is.EqualTo(2));
    Assert.That(z280.IY, Is.EqualTo(1));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions