1
0
mirror of https://github.com/jborza/emu6502.git synced 2024-06-01 04:41:41 +00:00

refactored the read_word function to allow for wrap

This commit is contained in:
jborza 2019-05-07 21:04:04 +02:00
parent 94aea533a9
commit c4b2b70d96
3 changed files with 32 additions and 36 deletions

View File

@ -11,105 +11,100 @@ word fetch_word(State6502* state) {
return result; return result;
} }
word read_word(State6502* state, word address) { word read_word(State6502 * state, word address) {
if ((address & 0xFF) == 0xFF)
{
return state->memory[address] | state->memory[address - 0xFF] << 8;
}
else {
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 read_word_wrap(State6502 * state, word address) {
word address_low = address;
//page wraparound
word address_high = (address_low & 0xFF) == 0xFF ? address - 0xFF : address_low + 1;
return state->memory[address_low] | state->memory[address_high] << 8;
}
word get_address_zero_page(State6502 * state) {
return fetch_byte(state); return fetch_byte(state);
} }
byte get_byte_zero_page(State6502* state) { byte get_byte_zero_page(State6502 * state) {
//8 bit addressing, only the first 256 bytes of the memory //8 bit addressing, only the first 256 bytes of the memory
return state->memory[get_address_zero_page(state)]; return state->memory[get_address_zero_page(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 = fetch_byte(state) + state->x; byte address = fetch_byte(state) + state->x;
return address; return address;
} }
byte get_byte_zero_page_x(State6502* state) { byte get_byte_zero_page_x(State6502 * state) {
return state->memory[get_address_zero_page_x(state)]; return state->memory[get_address_zero_page_x(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 = fetch_byte(state) + state->y; byte address = fetch_byte(state) + state->y;
return address; return address;
} }
byte get_byte_zero_page_y(State6502* state) { byte get_byte_zero_page_y(State6502 * state) {
return state->memory[get_address_zero_page_y(state)]; return state->memory[get_address_zero_page_y(state)];
} }
word get_address_absolute(State6502* state) { word get_address_absolute(State6502 * state) {
//absolute indexed, 16 bits //absolute indexed, 16 bits
word address = fetch_word(state); word address = fetch_word(state);
return address; return address;
} }
byte get_byte_absolute(State6502* state) byte get_byte_absolute(State6502 * state)
{ {
//absolute indexed, 16 bits //absolute indexed, 16 bits
return state->memory[get_address_absolute(state)]; return state->memory[get_address_absolute(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 = fetch_word(state) + state->x; word address = fetch_word(state) + state->x;
return address; return address;
} }
byte get_byte_absolute_x(State6502* state) { byte get_byte_absolute_x(State6502 * state) {
return state->memory[get_address_absolute_x(state)]; return state->memory[get_address_absolute_x(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 = fetch_word(state) + state->y; word address = fetch_word(state) + state->y;
return address; return address;
} }
byte get_byte_absolute_y(State6502* state) { byte get_byte_absolute_y(State6502 * state) {
//absolute added with the contents of y register //absolute added with the contents of y register
return state->memory[get_address_absolute_y(state)]; return state->memory[get_address_absolute_y(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
word indirect_address = fetch_word(state); word indirect_address = fetch_word(state);
if ((indirect_address & 0xFF) == 0xFF) { //AN INDIRECT JUMP MUST NEVER USE A VECTOR BEGINNING ON THE LAST BYTE OF A PAGE
//avoid crossing the page boundary return read_word_wrap(state, indirect_address);
return state->memory[indirect_address] | state->memory[indirect_address - 0xFF] << 8;
}
else {
return read_word(state, indirect_address);
}
} }
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 = fetch_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_wrap(state, indirect_address);
return address; return address;
} }
byte get_byte_indirect_x(State6502* state) { byte get_byte_indirect_x(State6502 * state) {
//pre-indexed indirect with the X register //pre-indexed indirect with the X register
return state->memory[get_address_indirect_x(state)]; return state->memory[get_address_indirect_x(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 = fetch_byte(state); byte indirect_address = fetch_byte(state);
@ -118,11 +113,11 @@ word get_address_indirect_y(State6502* state) {
return address; return address;
} }
byte get_byte_indirect_y(State6502* state) { byte get_byte_indirect_y(State6502 * state) {
return state->memory[get_address_indirect_y(state)]; return state->memory[get_address_indirect_y(state)];
} }
word get_address_relative(State6502* state) { word get_address_relative(State6502 * state) {
int8_t address = (int8_t)fetch_byte(state); int8_t address = (int8_t)fetch_byte(state);
return state->pc + address; return state->pc + address;
} }

View File

@ -7,6 +7,8 @@ word fetch_word(State6502* state);
word read_word(State6502* state, word address); word read_word(State6502* state, word address);
word read_word_wrap(State6502* state, word address);
word get_address_zero_page(State6502* state); word get_address_zero_page(State6502* state);
byte get_byte_zero_page(State6502* state); byte get_byte_zero_page(State6502* state);

View File

@ -2533,7 +2533,6 @@ void run_suite(fp * *suite, int size) {
} }
void run_tests() { void run_tests() {
test_LDA_INDX_wraparound();
RUN(tests_ror); RUN(tests_ror);
RUN(tests_asl); RUN(tests_asl);
RUN(tests_rti); RUN(tests_rti);