Now, I developing emulator nes for my https://flathub.org/apps/io.github.xver ... riteEditor application and more.
In a game "mario bros" after sbc $01 where value is #$00 and carry flag is 1, fceux give carry flag after execution. How to implementing it?
This is my code.
The fact is that the function clears the carry flag if there was no transfer.
In a game "mario bros" after sbc $01 where value is #$00 and carry flag is 1, fceux give carry flag after execution. How to implementing it?
This is my code.
Code:
void sbc_zeropage (struct NESEmu *emu){ struct CPUNes *cpu = &emu->cpu; uint16_t addr = zeropage (emu); uint8_t val = addr < RAM_MAX? emu->ram[addr]: emu->mem[addr - 0x8000]; repetitive_acts (emu, STATUS_FLAG_NF|STATUS_FLAG_ZF|STATUS_FLAG_CF|STATUS_FLAG_VF, &cpu->A, cpu->A - val - ((emu->cpu.P & STATUS_FLAG_CF)? 0: 1), eq, (3 << 8) | 2);}static void repetitive_acts (struct NESEmu *emu, uint8_t flags, uint8_t *reg, uint8_t result, void (*eq) (uint8_t *r0, uint8_t r1), uint16_t cycles_and_bytes){ struct CPUNes *cpu = &emu->cpu; uint8_t carry = 0; if (cpu->P & STATUS_FLAG_CF) carry = 1; cpu->P &= ~(flags); check_flags (cpu, flags, *reg, result); eq (reg, result); wait_cycles (emu, cycles_and_bytes >> 8); emu->cpu.PC += (cycles_and_bytes & 0xff);}static void eq (uint8_t *r0, uint8_t r1){ *r0 = r1; }static void check_flags (struct CPUNes *cpu, uint8_t flags, uint8_t reg, uint8_t ret){ if (flags & STATUS_FLAG_NF) { if (ret & 0x80) { cpu->P |= STATUS_FLAG_NF; } } if (flags & STATUS_FLAG_ZF) { if (ret == 0) { cpu->P |= STATUS_FLAG_ZF; } } if (flags & STATUS_FLAG_CF) { if (ret < reg) { cpu->P |= STATUS_FLAG_CF; } } if ((ret & 0x80) && (!(reg & 0x80))) cpu->P |= STATUS_FLAG_VF; else if ((!(ret & 0x80)) && (reg & 0x80)) cpu->P |= STATUS_FLAG_VF;}
Statistics: Posted by xverizex — Thu Jan 30, 2025 12:15 pm — Replies 3 — Views 49