Skip to content

Incorrectly emulated undocumented ANC instruction #13

@drfiemost

Description

@drfiemost

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;
}

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