6502: add LDX and LDY

This commit is contained in:
g1n 2021-09-04 18:03:54 +03:00
parent 106320bd14
commit 55fb75f6ad
2 changed files with 60 additions and 4 deletions

View File

@ -11,6 +11,11 @@ typedef unsigned short word; // 16 bit
#define INS_LDX_IM 0xA2 // LDX Immediate
#define INS_LDX_ZP 0xA6 // LDX Zero Page
#define INS_LDX_ZPY 0xB6 // LDX Zero Page.Y
#define INS_LDY_IM 0xA0 // LDY Immediate
#define INS_LDY_ZP 0xA4 // LDY Zero Page
#define INS_LDY_ZPX 0xB4 // LDY Zero Page.X
#define INS_JSR 0x20 // JSR
#define INS_RTS 0x60 // RTS

View File

@ -99,6 +99,48 @@ void execute(byte exec_cycles) {
cpu.Z = (cpu.A == 0);
cpu.N = (cpu.A & 0b1000000) > 0;
break;
case INS_LDX_IM:
value = fetch_byte();
cpu.X = value;
cpu.Z = (cpu.X == 0);
cpu.N = (cpu.X & 0b1000000) > 0;
break;
case INS_LDX_ZP:
zero_page_addr = fetch_byte();
cpu.X = read_byte(zero_page_addr);
cpu.Z = (cpu.X == 0);
cpu.N = (cpu.X & 0b1000000) > 0;
break;
case INS_LDX_ZPY:
byte zero_page_y_addr = cpu.Y + fetch_byte();
cycles--;
cpu.X = read_byte(zero_page_y_addr);
cpu.Z = (cpu.X == 0);
cpu.N = (cpu.X & 0b1000000) > 0;
printf("test\n");
break;
case INS_LDY_IM:
value = fetch_byte();
cpu.Y = value;
cpu.Z = (cpu.Y == 0);
cpu.N = (cpu.Y & 0b1000000) > 0;
break;
case INS_LDY_ZP:
zero_page_addr = fetch_byte();
cpu.Y = read_byte(zero_page_addr);
cpu.Z = (cpu.Y == 0);
cpu.N = (cpu.Y & 0b1000000) > 0;
break;
case INS_LDY_ZPX:
zero_page_x_addr = cpu.Y + fetch_byte();
cycles--;
cpu.Y = read_byte(zero_page_x_addr);
cpu.Z = (cpu.Y == 0);
cpu.N = (cpu.Y & 0b1000000) > 0;
break;
case INS_JSR:
word jsr_addr = fetch_word();
write_word(cpu.PC - 1, cpu.SP);
@ -122,12 +164,21 @@ int main() {
mem.memory[0xFFFD] = 0x42;
mem.memory[0xFFFE] = 0x42;
mem.memory[0x4242] = INS_LDA_IM;
mem.memory[0x4242] = INS_LDX_IM;
mem.memory[0x4243] = 0x66;
mem.memory[0x4244] = INS_LDA_IM;
mem.memory[0x4245] = 0x68;
execute(10);
mem.memory[0x4244] = INS_LDY_IM;
mem.memory[0x4245] = 0x11;
mem.memory[0x4246] = INS_LDX_ZP;
mem.memory[0x4247] = 0x68;
mem.memory[0x68] = 0x75;
mem.memory[0x4248] = INS_LDY_ZP;
mem.memory[0x4249] = 0x69;
mem.memory[0x69] = 0x88;
execute(18);
dump_cpu(cpu);
return 0;
}