summaryrefslogtreecommitdiff
path: root/cpu_opcodes.c
diff options
context:
space:
mode:
Diffstat (limited to 'cpu_opcodes.c')
-rw-r--r--cpu_opcodes.c73
1 files changed, 38 insertions, 35 deletions
diff --git a/cpu_opcodes.c b/cpu_opcodes.c
index b401910..1a70cce 100644
--- a/cpu_opcodes.c
+++ b/cpu_opcodes.c
@@ -88,7 +88,7 @@ static void opcode_adc_indy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T4 (dummy if crossed)
}
@@ -117,7 +117,7 @@ static void opcode_adc_absy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T3 dummy
}
@@ -134,7 +134,7 @@ static void opcode_adc_absx(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->x;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T3 dummy
}
@@ -205,7 +205,7 @@ static void opcode_and_indy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T4 dummy
}
@@ -236,7 +236,7 @@ static void opcode_and_absy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T3 dummy
}
@@ -254,7 +254,7 @@ static void opcode_and_absx(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->x;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T3 dummy
}
@@ -406,9 +406,9 @@ static void opcode_brk(struct nes_state *state) {
// BRANCHES
-static inline int page_crossed(uint16_t a, uint16_t b) {
- return (a & 0xff00) != (b & 0xff00);
-}
+// static inline int page_crossed(uint16_t a, uint16_t b) {
+// return (a & 0xff00) != (b & 0xff00);
+// }
static void opcode_bpl(struct nes_state *state) {
struct cpu_state * restrict cpu = &state->cpu;
@@ -418,7 +418,7 @@ static void opcode_bpl(struct nes_state *state) {
if(!cpu->n) {
memory_read_dummy(state, cpu->pc); // T2
- if(page_crossed(cpu->pc, new_pc)) {
+ if(PAGE_CROSSED(cpu->pc, new_pc)) {
memory_read_dummy(state, (cpu->pc & 0xff00) | (new_pc & 0x00ff)); // T3
}
cpu->pc = new_pc; // T3 or T4
@@ -433,7 +433,7 @@ static void opcode_bmi(struct nes_state *state) {
if(cpu->n) {
memory_read_dummy(state, cpu->pc);
- if(page_crossed(cpu->pc, new_pc)) {
+ if(PAGE_CROSSED(cpu->pc, new_pc)) {
memory_read_dummy(state, (cpu->pc & 0xff00) | (new_pc & 0x00ff));
}
cpu->pc = new_pc;
@@ -448,7 +448,7 @@ static void opcode_bvc(struct nes_state *state) {
if(!cpu->v) {
memory_read_dummy(state, cpu->pc);
- if(page_crossed(cpu->pc, new_pc)) {
+ if(PAGE_CROSSED(cpu->pc, new_pc)) {
memory_read_dummy(state, (cpu->pc & 0xff00) | (new_pc & 0x00ff));
}
cpu->pc = new_pc;
@@ -463,7 +463,7 @@ static void opcode_bvs(struct nes_state *state) {
if(cpu->v) {
memory_read_dummy(state, cpu->pc);
- if(page_crossed(cpu->pc, new_pc)) {
+ if(PAGE_CROSSED(cpu->pc, new_pc)) {
memory_read_dummy(state, (cpu->pc & 0xff00) | (new_pc & 0x00ff));
}
cpu->pc = new_pc;
@@ -478,7 +478,7 @@ static void opcode_bcc(struct nes_state *state) {
if(!cpu->c) {
memory_read_dummy(state, cpu->pc);
- if(page_crossed(cpu->pc, new_pc)) {
+ if(PAGE_CROSSED(cpu->pc, new_pc)) {
memory_read_dummy(state, (cpu->pc & 0xff00) | (new_pc & 0x00ff));
}
cpu->pc = new_pc;
@@ -493,7 +493,7 @@ static void opcode_bcs(struct nes_state *state) {
if(cpu->c) {
memory_read_dummy(state, cpu->pc);
- if(page_crossed(cpu->pc, new_pc)) {
+ if(PAGE_CROSSED(cpu->pc, new_pc)) {
memory_read_dummy(state, (cpu->pc & 0xff00) | (new_pc & 0x00ff));
}
cpu->pc = new_pc;
@@ -508,7 +508,7 @@ static void opcode_bne(struct nes_state *state) {
if(!cpu->z) {
memory_read_dummy(state, cpu->pc);
- if(page_crossed(cpu->pc, new_pc)) {
+ if(PAGE_CROSSED(cpu->pc, new_pc)) {
memory_read_dummy(state, (cpu->pc & 0xff00) | (new_pc & 0x00ff));
}
cpu->pc = new_pc;
@@ -523,7 +523,7 @@ static void opcode_beq(struct nes_state *state) {
if(cpu->z) {
memory_read_dummy(state, cpu->pc);
- if(page_crossed(cpu->pc, new_pc)) {
+ if(PAGE_CROSSED(cpu->pc, new_pc)) {
memory_read_dummy(state, (cpu->pc & 0xff00) | (new_pc & 0x00ff));
}
cpu->pc = new_pc;
@@ -584,7 +584,9 @@ static void opcode_sei(struct nes_state *state) {
static inline void cmp(struct cpu_state * restrict cpu, uint8_t value) {
uint8_t result = cpu->a - value;
cpu->c = (cpu->a >= value);
- update_zn(cpu, result);
+ cpu->z = (result == 0);
+ cpu->n = (result & 0x80) != 0;
+ // update_zn(cpu, result);
}
static void opcode_cmp_indx(struct nes_state *state) {
@@ -640,7 +642,7 @@ static void opcode_cmp_indy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T4 dummy
}
@@ -667,7 +669,7 @@ static void opcode_cmp_absy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T3 dummy
}
@@ -683,7 +685,7 @@ static void opcode_cmp_absx(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->x;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T3 dummy
}
@@ -885,7 +887,7 @@ static void opcode_eor_indy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T4 dummy
}
@@ -914,7 +916,7 @@ static void opcode_eor_absy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T3 dummy
}
@@ -931,7 +933,7 @@ static void opcode_eor_absx(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->x;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T3 dummy
}
@@ -1024,6 +1026,7 @@ static void opcode_jmp_ind(struct nes_state *state) {
uint8_t lo = memory_read(state, ptr); // T3
uint8_t hi;
+
if((ptr & 0x00ff) == 0x00ff) {
hi = memory_read(state, ptr & 0xff00); // Emulate 6502 bug
} else {
@@ -1108,7 +1111,7 @@ static void opcode_lda_indy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T4 dummy
}
@@ -1137,7 +1140,7 @@ static void opcode_lda_absy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T3
}
@@ -1154,7 +1157,7 @@ static void opcode_lda_absx(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->x;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff)); // T3
}
@@ -1216,7 +1219,7 @@ static void opcode_ldx_absy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff));
}
@@ -1278,7 +1281,7 @@ static void opcode_ldy_absx(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->x;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff));
}
@@ -1436,7 +1439,7 @@ static void opcode_ora_indy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff));
}
@@ -1465,7 +1468,7 @@ static void opcode_ora_absy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff));
}
@@ -1482,7 +1485,7 @@ static void opcode_ora_absx(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->x;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff));
}
@@ -1828,7 +1831,7 @@ static void opcode_sbc_indy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff));
}
@@ -1855,7 +1858,7 @@ static void opcode_sbc_absy(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->y;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff));
}
@@ -1871,7 +1874,7 @@ static void opcode_sbc_absx(struct nes_state *state) {
uint16_t base = lo | (hi << 8);
uint16_t addr = base + cpu->x;
- if((base & 0xff00) != (addr & 0xff00)) {
+ if(PAGE_CROSSED(base, addr)) {
memory_read_dummy(state, (base & 0xff00) | (addr & 0x00ff));
}