1
0
mirror of https://github.com/jborza/emu6502.git synced 2024-11-25 03:34:40 +00:00

memory function name refactoring (pop->fetch)

This commit is contained in:
jborza 2019-04-27 13:57:47 +02:00
parent 682cde7f7d
commit ddac9a14d0
3 changed files with 37 additions and 30 deletions

32
cpu.c
View File

@ -53,9 +53,7 @@ void clear_state(State6502 * state) {
state->running = 1; state->running = 1;
} }
byte pop_byte(State6502 * state) {
return state->memory[state->pc++];
}
void push_byte_to_stack(State6502 * state, byte value) { void push_byte_to_stack(State6502 * state, byte value) {
//stack located between $0100 to $01FF //stack located between $0100 to $01FF
@ -351,17 +349,11 @@ void PHP_(State6502* state) {
push_byte_to_stack(state, flags_value); push_byte_to_stack(state, flags_value);
} }
word pop_word(State6502 * state) {
byte low = pop_byte(state);
byte high = pop_byte(state);
word result = (high << 8) | low;
return result;
}
int emulate_6502_op(State6502 * state) { int emulate_6502_op(State6502 * state) {
byte* opcode = &state->memory[state->pc++]; byte* opcode = &state->memory[state->pc++];
switch (*opcode) { switch (*opcode) {
case ADC_IMM: ADC(state, pop_byte(state)); break; case ADC_IMM: ADC(state, fetch_byte(state)); break;
case ADC_ZP: ADC(state, get_byte_zero_page(state)); break; case ADC_ZP: ADC(state, get_byte_zero_page(state)); break;
case ADC_ZPX: ADC(state, get_byte_zero_page_x(state)); break; case ADC_ZPX: ADC(state, get_byte_zero_page_x(state)); break;
case ADC_ABS: ADC(state, get_byte_absolute(state)); break; case ADC_ABS: ADC(state, get_byte_absolute(state)); break;
@ -369,7 +361,7 @@ int emulate_6502_op(State6502 * state) {
case ADC_ABSY: ADC(state, get_byte_absolute_y(state)); break; case ADC_ABSY: ADC(state, get_byte_absolute_y(state)); break;
case ADC_INDX: ADC(state, get_byte_indirect_x(state)); break; case ADC_INDX: ADC(state, get_byte_indirect_x(state)); break;
case ADC_INDY: ADC(state, get_byte_indirect_y(state)); break; case ADC_INDY: ADC(state, get_byte_indirect_y(state)); break;
case AND_IMM: AND(state, pop_byte(state)); break; case AND_IMM: AND(state, fetch_byte(state)); break;
case AND_ZP: AND(state, get_byte_zero_page(state)); break; case AND_ZP: AND(state, get_byte_zero_page(state)); break;
case AND_ZPX: AND(state, get_byte_zero_page_x(state)); break; case AND_ZPX: AND(state, get_byte_zero_page_x(state)); break;
case AND_ABS: AND(state, get_byte_absolute(state)); break; case AND_ABS: AND(state, get_byte_absolute(state)); break;
@ -415,7 +407,7 @@ int emulate_6502_op(State6502 * state) {
case TYA: state->a = state->y; set_NZ_flags(state, state->a); break; case TYA: state->a = state->y; set_NZ_flags(state, state->a); break;
case TSX: state->x = state->sp; set_NZ_flags(state, state->x); break; case TSX: state->x = state->sp; set_NZ_flags(state, state->x); break;
case TXS: state->sp = state->x; set_NZ_flags(state, state->x); break; case TXS: state->sp = state->x; set_NZ_flags(state, state->x); break;
case CMP_IMM: CMP(state, pop_byte(state)); break; //TODO test case CMP_IMM: CMP(state, fetch_byte(state)); break; //TODO test
case CMP_ZP: CMP(state, get_byte_zero_page(state)); break; //TODO test case CMP_ZP: CMP(state, get_byte_zero_page(state)); break; //TODO test
case CMP_ZPX: CMP(state, get_byte_zero_page_x(state)); break; //TODO test case CMP_ZPX: CMP(state, get_byte_zero_page_x(state)); break; //TODO test
case CMP_ABS: CMP(state, get_byte_absolute(state)); break; case CMP_ABS: CMP(state, get_byte_absolute(state)); break;
@ -423,10 +415,10 @@ int emulate_6502_op(State6502 * state) {
case CMP_ABSY: CMP(state, get_byte_absolute_y(state)); break;//TODO test case CMP_ABSY: CMP(state, get_byte_absolute_y(state)); break;//TODO test
case CMP_INDX: CMP(state, get_byte_indirect_x(state)); break;//TODO test case CMP_INDX: CMP(state, get_byte_indirect_x(state)); break;//TODO test
case CMP_INDY: CMP(state, get_byte_indirect_y(state)); break;//TODO test case CMP_INDY: CMP(state, get_byte_indirect_y(state)); break;//TODO test
case CPX_IMM: CPX(state, pop_byte(state)); break;//TODO test case CPX_IMM: CPX(state, fetch_byte(state)); break;//TODO test
case CPX_ZP: CPX(state, get_byte_zero_page(state)); break;//TODO test case CPX_ZP: CPX(state, get_byte_zero_page(state)); break;//TODO test
case CPX_ABS: CPX(state, get_byte_absolute(state)); break;//TODO test case CPX_ABS: CPX(state, get_byte_absolute(state)); break;//TODO test
case CPY_IMM: CPY(state, pop_byte(state)); break;//TODO test case CPY_IMM: CPY(state, fetch_byte(state)); break;//TODO test
case CPY_ZP: CPY(state, get_byte_zero_page(state)); break;//TODO test case CPY_ZP: CPY(state, get_byte_zero_page(state)); break;//TODO test
case CPY_ABS: CPY(state, get_byte_absolute(state)); break;//TODO test case CPY_ABS: CPY(state, get_byte_absolute(state)); break;//TODO test
case DEC_ZP: DEC(state, get_address_zero_page(state)); break; case DEC_ZP: DEC(state, get_address_zero_page(state)); break;
@ -437,7 +429,7 @@ int emulate_6502_op(State6502 * state) {
case DEY: state->y -= 1; set_NZ_flags(state, state->y); break; case DEY: state->y -= 1; set_NZ_flags(state, state->y); break;
case INX: state->x += 1; set_NZ_flags(state, state->x); break; case INX: state->x += 1; set_NZ_flags(state, state->x); break;
case INY: state->y += 1; set_NZ_flags(state, state->y); break; case INY: state->y += 1; set_NZ_flags(state, state->y); break;
case EOR_IMM: EOR(state, pop_byte(state)); break; case EOR_IMM: EOR(state, fetch_byte(state)); break;
case EOR_ZP: EOR(state, get_byte_zero_page(state)); break; case EOR_ZP: EOR(state, get_byte_zero_page(state)); break;
case EOR_ZPX: EOR(state, get_byte_zero_page_x(state)); break; case EOR_ZPX: EOR(state, get_byte_zero_page_x(state)); break;
case EOR_ABS: EOR(state, get_byte_absolute(state)); break; case EOR_ABS: EOR(state, get_byte_absolute(state)); break;
@ -452,7 +444,7 @@ int emulate_6502_op(State6502 * state) {
case JMP_ABS: JMP(state, get_address_absolute(state)); break; case JMP_ABS: JMP(state, get_address_absolute(state)); break;
case JMP_IND: JMP(state, get_address_indirect_jmp(state)); break; case JMP_IND: JMP(state, get_address_indirect_jmp(state)); break;
case JSR_ABS: JSR(state, get_address_absolute(state)); break; case JSR_ABS: JSR(state, get_address_absolute(state)); break;
case LDA_IMM: LDA(state, pop_byte(state)); break; case LDA_IMM: LDA(state, fetch_byte(state)); break;
case LDA_ZP: LDA(state, get_byte_zero_page(state)); break; case LDA_ZP: LDA(state, get_byte_zero_page(state)); break;
case LDA_ZPX: LDA(state, get_byte_zero_page_x(state)); break; case LDA_ZPX: LDA(state, get_byte_zero_page_x(state)); break;
case LDA_ABS: LDA(state, get_byte_absolute(state)); break; case LDA_ABS: LDA(state, get_byte_absolute(state)); break;
@ -460,12 +452,12 @@ int emulate_6502_op(State6502 * state) {
case LDA_ABSY: LDA(state, get_byte_absolute_y(state)); break; case LDA_ABSY: LDA(state, get_byte_absolute_y(state)); break;
case LDA_INDX: LDA(state, get_byte_indirect_x(state)); break; case LDA_INDX: LDA(state, get_byte_indirect_x(state)); break;
case LDA_INDY: LDA(state, get_byte_indirect_y(state)); break; case LDA_INDY: LDA(state, get_byte_indirect_y(state)); break;
case LDX_IMM: LDX(state, pop_byte(state)); break; case LDX_IMM: LDX(state, fetch_byte(state)); break;
case LDX_ZP: LDX(state, get_byte_zero_page(state)); break; case LDX_ZP: LDX(state, get_byte_zero_page(state)); break;
case LDX_ZPY: LDX(state, get_byte_zero_page_y(state)); break; case LDX_ZPY: LDX(state, get_byte_zero_page_y(state)); break;
case LDX_ABS: LDX(state, get_byte_absolute(state)); break; case LDX_ABS: LDX(state, get_byte_absolute(state)); break;
case LDX_ABSY: LDX(state, get_byte_absolute_y(state)); break; case LDX_ABSY: LDX(state, get_byte_absolute_y(state)); break;
case LDY_IMM: LDY(state, pop_byte(state)); break; case LDY_IMM: LDY(state, fetch_byte(state)); break;
case LDY_ZP: LDY(state, get_byte_zero_page(state)); break; case LDY_ZP: LDY(state, get_byte_zero_page(state)); break;
case LDY_ZPX: LDY(state, get_byte_zero_page_x(state)); break; case LDY_ZPX: LDY(state, get_byte_zero_page_x(state)); break;
case LDY_ABS: LDY(state, get_byte_absolute(state)); break; case LDY_ABS: LDY(state, get_byte_absolute(state)); break;
@ -475,7 +467,7 @@ int emulate_6502_op(State6502 * state) {
case LSR_ZPX: LSR_MEM(state, get_address_zero_page_x(state)); break; case LSR_ZPX: LSR_MEM(state, get_address_zero_page_x(state)); break;
case LSR_ABS: LSR_MEM(state, get_address_absolute(state)); break; case LSR_ABS: LSR_MEM(state, get_address_absolute(state)); break;
case LSR_ABSX: LSR_MEM(state, get_address_absolute_x(state)); break; case LSR_ABSX: LSR_MEM(state, get_address_absolute_x(state)); break;
case ORA_IMM: ORA(state, pop_byte(state)); break; case ORA_IMM: ORA(state, fetch_byte(state)); break;
case ORA_ZP: ORA(state, get_byte_zero_page(state)); break; case ORA_ZP: ORA(state, get_byte_zero_page(state)); break;
case ORA_ZPX: ORA(state, get_byte_zero_page_x(state)); break; case ORA_ZPX: ORA(state, get_byte_zero_page_x(state)); break;
case ORA_ABS: ORA(state, get_byte_absolute(state)); break; case ORA_ABS: ORA(state, get_byte_absolute(state)); break;
@ -493,7 +485,7 @@ int emulate_6502_op(State6502 * state) {
case ROR_ZPX: ROR_MEM(state, get_address_zero_page_x(state)); break; case ROR_ZPX: ROR_MEM(state, get_address_zero_page_x(state)); break;
case ROR_ABS: ROR_MEM(state, get_address_absolute(state)); break; case ROR_ABS: ROR_MEM(state, get_address_absolute(state)); break;
case ROR_ABSX: ROR_MEM(state, get_address_absolute_x(state)); break; case ROR_ABSX: ROR_MEM(state, get_address_absolute_x(state)); break;
case SBC_IMM: SBC(state, pop_byte(state)); break; case SBC_IMM: SBC(state, fetch_byte(state)); break;
case SBC_ZP: SBC(state, get_byte_zero_page(state)); break; case SBC_ZP: SBC(state, get_byte_zero_page(state)); break;
case SBC_ZPX: SBC(state, get_byte_zero_page_x(state)); break; case SBC_ZPX: SBC(state, get_byte_zero_page_x(state)); break;
case SBC_ABS: SBC(state, get_byte_absolute(state)); break; case SBC_ABS: SBC(state, get_byte_absolute(state)); break;

View File

@ -1,11 +1,22 @@
#include "memory.h" #include "memory.h"
byte fetch_byte(State6502* state) {
return state->memory[state->pc++];
}
word fetch_word(State6502* state) {
byte low = fetch_byte(state);
byte high = fetch_byte(state);
word result = (high << 8) | low;
return result;
}
word read_word(State6502* state, word address) { word read_word(State6502* state, word address) {
return state->memory[address] | state->memory[address + 1] << 8; return state->memory[address] | state->memory[address + 1] << 8;
} }
word get_address_zero_page(State6502* state) { word get_address_zero_page(State6502* state) {
return pop_byte(state); return fetch_byte(state);
} }
byte get_byte_zero_page(State6502* state) { byte get_byte_zero_page(State6502* state) {
@ -15,7 +26,7 @@ byte get_byte_zero_page(State6502* state) {
word get_address_zero_page_x(State6502* state) { word get_address_zero_page_x(State6502* state) {
//address is zero page, so wraparound byte //address is zero page, so wraparound byte
byte address = pop_byte(state) + state->x; byte address = fetch_byte(state) + state->x;
return address; return address;
} }
@ -25,7 +36,7 @@ byte get_byte_zero_page_x(State6502* state) {
word get_address_zero_page_y(State6502* state) { word get_address_zero_page_y(State6502* state) {
//address is zero page, so wraparound byte //address is zero page, so wraparound byte
byte address = pop_byte(state) + state->y; byte address = fetch_byte(state) + state->y;
return address; return address;
} }
@ -35,7 +46,7 @@ byte get_byte_zero_page_y(State6502* state) {
word get_address_absolute(State6502* state) { word get_address_absolute(State6502* state) {
//absolute indexed, 16 bits //absolute indexed, 16 bits
word address = pop_word(state); word address = fetch_word(state);
return address; return address;
} }
@ -47,7 +58,7 @@ byte get_byte_absolute(State6502* state)
word get_address_absolute_x(State6502* state) { word get_address_absolute_x(State6502* state) {
//absolute added with the contents of x register //absolute added with the contents of x register
word address = pop_word(state) + state->x; word address = fetch_word(state) + state->x;
return address; return address;
} }
@ -57,7 +68,7 @@ byte get_byte_absolute_x(State6502* state) {
word get_address_absolute_y(State6502* state) { word get_address_absolute_y(State6502* state) {
//absolute added with the contents of x register //absolute added with the contents of x register
word address = pop_word(state) + state->y; word address = fetch_word(state) + state->y;
return address; return address;
} }
@ -68,7 +79,7 @@ byte get_byte_absolute_y(State6502* state) {
word get_address_indirect_jmp(State6502* state) { word get_address_indirect_jmp(State6502* state) {
//AN INDIRECT JUMP MUST NEVER USE A VECTOR BEGINNING ON THE LAST BYTE OF A PAGE //AN INDIRECT JUMP MUST NEVER USE A VECTOR BEGINNING ON THE LAST BYTE OF A PAGE
word indirect_address = pop_word(state); word indirect_address = fetch_word(state);
if ((indirect_address & 0xFF) == 0xFF) { if ((indirect_address & 0xFF) == 0xFF) {
//avoid crossing the page boundary //avoid crossing the page boundary
return state->memory[indirect_address] | state->memory[indirect_address - 0xFF] << 8; return state->memory[indirect_address] | state->memory[indirect_address - 0xFF] << 8;
@ -81,7 +92,7 @@ word get_address_indirect_jmp(State6502* state) {
word get_address_indirect_x(State6502* state) { word get_address_indirect_x(State6502* state) {
//pre-indexed indirect with the X register //pre-indexed indirect with the X register
//zero-page address is added to x register //zero-page address is added to x register
byte indirect_address = pop_byte(state) + state->x; byte indirect_address = fetch_byte(state) + state->x;
//pointing to address of a word holding the address of the operand //pointing to address of a word holding the address of the operand
word address = read_word(state, indirect_address); word address = read_word(state, indirect_address);
return address; return address;
@ -95,7 +106,7 @@ byte get_byte_indirect_x(State6502* state) {
word get_address_indirect_y(State6502* state) { word get_address_indirect_y(State6502* state) {
//post-indexed indirect //post-indexed indirect
//zero-page address as an argument //zero-page address as an argument
byte indirect_address = pop_byte(state); byte indirect_address = fetch_byte(state);
//the address and the following byte is read as a word, adding Y register //the address and the following byte is read as a word, adding Y register
word address = read_word(state, indirect_address) + state->y; word address = read_word(state, indirect_address) + state->y;
return address; return address;
@ -106,6 +117,6 @@ byte get_byte_indirect_y(State6502* state) {
} }
word get_address_relative(State6502* state) { word get_address_relative(State6502* state) {
int8_t address = (int8_t)pop_byte(state); int8_t address = (int8_t)fetch_byte(state);
return state->pc + address; return state->pc + address;
} }

View File

@ -1,6 +1,10 @@
#pragma once #pragma once
#include "state.h" #include "state.h"
byte fetch_byte(State6502* state);
word fetch_word(State6502* state);
word read_word(State6502* state, word address); word read_word(State6502* state, word address);
word get_address_zero_page(State6502* state); word get_address_zero_page(State6502* state);