Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrectly emulated undocumented ANC instruction #13

Open
drfiemost opened this issue Mar 19, 2021 · 0 comments
Open

Incorrectly emulated undocumented ANC instruction #13

drfiemost opened this issue Mar 19, 2021 · 0 comments

Comments

@drfiemost
Copy link
Contributor

As reported at forum6502:

This instruction is called stable, so I'd expect that visual6502 simulates it as described. However, it seems that A doesn't get updated at all (this seems to be incorrect, as ANC should behave almost like AND).

Here's the C code to reproduce the issue (slightly updated version of that posted in the above thread):

#include <stdio.h>
#include "types.h"
#include "perfect6502.h"
#include "netlist_sim.h"

#include <assert.h>

// gcc -Wall -o testANC testANC.c perfect6502.c netlist_sim.c


/*
 * $0B ANC #imm
 * $2B ANC #imm
 * 
 * ANDs the contents of the A register with an immediate value and then moves bit 7 of A
 * into the Carry flag.
 * 
 * See https://csdb.dk/release/?id=198357
 */

int main(void)
{
    memory[0] = 0xa9; /* LDA */
    memory[1] = 0xff; /* $ff */
    memory[2] = 0x38; /* SEC */
    memory[3] = 0x0b; /* ANC */ // 0x2b
    memory[4] = 0x00; /* $00 */
    memory[5] = 0xea; /* NOP */
    memory[6] = 0x00; /* BRK */

    state_t *state = initAndResetChip();

    /* Cycle through the loading of the RESET vector. */
    for (int i = 0; i < 16; i++)
        step(state);

    printf("-- Fetching LDA:\n");
    for (int i = 0; i < 2; i++) {
        step(state);
        chipStatus(state);
    }

    printf("-- Executing LDA:\n");
    for (int i = 0; i < 4; i++) {
        step(state);
        chipStatus(state);
    }

    printf("-- Executing SEC:\n");
    for (int i = 0; i < 4; i++) {
        step(state);
        chipStatus(state);
    }

    printf("-- Executing ANC:\n");
    for (int i = 0; i < 4; i++) {
        step(state);
        chipStatus(state);
    }

    unsigned char regA = readA(state);
    assert(regA == 0x00);

    unsigned char flags = readP(state);
    assert((flags & 0x01) == 0x00);

    printf("-- Executing NOP:\n");
    for (int i = 0; i < 4; i++) {
        step(state);
        chipStatus(state);
    }

    printf("-- Executing BRK:\n");
    for (int i = 0; i < 10; i++) {
        step(state);
        chipStatus(state);
    }

    destroyChip(state);
    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant