From b21b48cac0fa22bca3fbac66ebf67cc344cb147f Mon Sep 17 00:00:00 2001 From: Peter Evans Date: Mon, 8 Jan 2018 21:22:29 -0600 Subject: [PATCH] Fix a couple of issues... 1. The execute function should not be incrementing the PC register, as this happens in other places. 2. The push_ and pop_stack functions were not considering that the data should be in little-endian order. --- src/mos6502.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/mos6502.c b/src/mos6502.c index 9346f32..fe3a793 100644 --- a/src/mos6502.c +++ b/src/mos6502.c @@ -207,11 +207,11 @@ mos6502_push_stack(mos6502 *cpu, vm_16bit addr) { // First we need to set the hi byte, by shifting the address right 8 // positions and using the base offset of the S register. - vm_segment_set(cpu->memory, 0x0100 + cpu->S, addr >> 8); + vm_segment_set(cpu->memory, 0x0100 + cpu->S, addr & 0xff); // Next we must record the lo byte, this time by using a bitmask to // capture just the low end of addr, but recording it in S + 1. - vm_segment_set(cpu->memory, 0x0100 + cpu->S + 1, addr & 0xFF); + vm_segment_set(cpu->memory, 0x0100 + cpu->S + 1, addr >> 8); // And finally we need to increment S by 2 (since we've used two // bytes in the stack). @@ -231,9 +231,7 @@ mos6502_pop_stack(mos6502 *cpu) // We need to use a bitwise-or operation to combine the hi and lo // bytes we retrieve from the stack into the actual position we // would use for the PC register. - return - (vm_segment_get(cpu->memory, 0x0100 + cpu->S) << 8) | - vm_segment_get(cpu->memory, 0x0100 + cpu->S + 1); + return vm_segment_get16(cpu->memory, 0x0100 + cpu->S); } /* @@ -354,18 +352,14 @@ void mos6502_execute(mos6502 *cpu, vm_8bit opcode) { vm_8bit operand = 0; - int cycles, bytes; + int cycles; mos6502_address_resolver resolver; mos6502_instruction_handler handler; - // We want to know how many bytes this opcode and its operand - // consume. We know the opcode is one byte. - bytes = 1; - // The disassembler knows how many bytes each operand requires // (maybe this code doesn't belong in the disassembler); let's use // that to figure out the total number of bytes to skip. - bytes += mos6502_dis_expected_bytes(mos6502_addr_mode(opcode)); + mos6502_dis_expected_bytes(mos6502_addr_mode(opcode)); // First, we need to know how to resolve our effective address and // how to execute anything. @@ -403,9 +397,6 @@ mos6502_execute(mos6502 *cpu, vm_8bit opcode) // something. usleep(cycles * 100000); - // Update the counter to move beyond the opcode and its operand. - cpu->PC += bytes; - // Ok -- we're done! This wasn't so hard, was it? return; }