| 9120e874 Andy McCall |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
#include "support/cpu_fixture.h" |
| 11 |
#include "support/harness.h" |
| 12 |
|
| 13 |
#define PROG 0x0200 |
| 14 |
|
| 15 |
static void |
| 16 |
test_reset_vector(void) |
| 17 |
{ |
| 18 |
cpu_reset_to(CPU_65C02, PROG); |
| 19 |
check_eq(regs.pc, PROG, "reset takes PC from the vector at $FFFC"); |
| 20 |
check_eq(clockticks6502, 0, "cycle counter starts at zero"); |
| 21 |
} |
| 22 |
|
| 23 |
static void |
| 24 |
test_executes_an_instruction(void) |
| 25 |
{ |
| 26 |
|
| 27 |
const uint8_t prog[] = { 0xA9, 0x42 }; |
| 28 |
|
| 29 |
cpu_reset_to(CPU_65C02, PROG); |
| 30 |
cpu_load(PROG, prog, sizeof prog); |
| 31 |
cpu_steps(1); |
| 32 |
|
| 33 |
check_eq(regs.a, 0x42, "LDA #$42 loads the accumulator"); |
| 34 |
check_eq(regs.pc, PROG + 2, "PC advances past a two-byte instruction"); |
| 35 |
check_eq(cpu_last_cycles(), 2, "LDA immediate costs two cycles"); |
| 36 |
} |
| 37 |
|
| 38 |
static void |
| 39 |
test_memory_round_trip(void) |
| 40 |
{ |
| 41 |
|
| 42 |
const uint8_t prog[] = { 0xA9, 0x7E, 0x8D, 0x00, 0x04 }; |
| 43 |
|
| 44 |
cpu_reset_to(CPU_65C02, PROG); |
| 45 |
cpu_load(PROG, prog, sizeof prog); |
| 46 |
cpu_steps(2); |
| 47 |
|
| 48 |
check_eq(cpu_mem[0x0400], 0x7E, "a store reaches the fixture's memory"); |
| 49 |
|
| 50 |
|
| 51 |
cpu_reset_to(CPU_65C02, PROG); |
| 52 |
cpu_mem[0x0400] = 0x39; |
| 53 |
const uint8_t load[] = { 0xAD, 0x00, 0x04 }; |
| 54 |
cpu_load(PROG, load, sizeof load); |
| 55 |
cpu_steps(1); |
| 56 |
|
| 57 |
check_eq(regs.a, 0x39, "a load sees a byte seeded by the test"); |
| 58 |
} |
| 59 |
|
| 60 |
static void |
| 61 |
test_reset_isolates_scenarios(void) |
| 62 |
{ |
| 63 |
const uint8_t prog[] = { 0xA9, 0x11, 0x8D, 0x00, 0x04 }; |
| 64 |
|
| 65 |
cpu_reset_to(CPU_65C02, PROG); |
| 66 |
cpu_load(PROG, prog, sizeof prog); |
| 67 |
cpu_steps(2); |
| 68 |
check_eq(cpu_mem[0x0400], 0x11, "first scenario wrote to memory"); |
| 69 |
|
| 70 |
cpu_reset_to(CPU_65C02, PROG); |
| 71 |
check_eq(cpu_mem[0x0400], 0x00, "reset clears memory from the last scenario"); |
| 72 |
check_eq(cpu_mem[PROG], 0x00, "reset clears the previous program"); |
| 73 |
check_eq(cpu_stop_count, 0, "reset clears the stop counter"); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
static void |
| 81 |
test_reset_clears_flags(void) |
| 82 |
{ |
| 83 |
|
| 84 |
const uint8_t dirty[] = { 0x38, 0xA9, 0xFF, 0x69, 0xFF }; |
| 85 |
cpu_reset_to(CPU_65C02, PROG); |
| 86 |
cpu_load(PROG, dirty, sizeof dirty); |
| 87 |
cpu_steps(3); |
| 88 |
check(regs.status & FLAG_CARRY, "the dirtying scenario left C set"); |
| 89 |
check(regs.status & FLAG_SIGN, "and N set"); |
| 90 |
|
| 91 |
cpu_reset_to(CPU_65C02, PROG); |
| 92 |
check(!(regs.status & FLAG_CARRY), "reset clears C"); |
| 93 |
check(!(regs.status & FLAG_SIGN), "reset clears N"); |
| 94 |
check(!(regs.status & FLAG_OVERFLOW), "reset clears V"); |
| 95 |
check(!(regs.status & FLAG_ZERO), "reset clears Z"); |
| 96 |
check(regs.status & FLAG_INTERRUPT, "reset leaves interrupts disabled"); |
| 97 |
check(!(regs.status & FLAG_DECIMAL), "reset clears decimal mode"); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
static void |
| 105 |
test_direct_page_penalty_does_not_persist(void) |
| 106 |
{ |
| 107 |
|
| 108 |
|
| 109 |
const uint8_t prog[] = { |
| 110 |
0xA9, 0x01, 0xEB, 0xA9, 0x34, 0x5B, |
| 111 |
0xA5, 0x10, |
| 112 |
0xEA, |
| 113 |
}; |
| 114 |
cpu_reset_to(CPU_816_NATIVE_8, PROG); |
| 115 |
cpu_load(PROG, prog, sizeof prog); |
| 116 |
cpu_steps(4); |
| 117 |
|
| 118 |
uint32_t dp_cost = cpu_step(); |
| 119 |
uint32_t nop_cost = cpu_step(); |
| 120 |
|
| 121 |
check_eq(nop_cost, 2, "a NOP after a misaligned direct-page access still costs 2"); |
| 122 |
check(dp_cost > nop_cost, "and the direct-page access itself paid the penalty"); |
| 123 |
} |
| 124 |
|
| 125 |
static void |
| 126 |
test_cycles_are_per_run(void) |
| 127 |
{ |
| 128 |
const uint8_t prog[] = { 0xA9, 0x01, 0xA9, 0x02 }; |
| 129 |
|
| 130 |
cpu_reset_to(CPU_65C02, PROG); |
| 131 |
cpu_load(PROG, prog, sizeof prog); |
| 132 |
|
| 133 |
cpu_steps(1); |
| 134 |
check_eq(cpu_last_cycles(), 2, "cycles are counted for one step"); |
| 135 |
cpu_steps(1); |
| 136 |
check_eq(cpu_last_cycles(), 2, "the count covers only the latest run"); |
| 137 |
check_eq(clockticks6502, 4, "the total keeps accumulating"); |
| 138 |
} |
| 139 |
|
| 140 |
static void |
| 141 |
test_modes_select_register_width(void) |
| 142 |
{ |
| 143 |
|
| 144 |
|
| 145 |
check(cpu_mode_is_16bit(CPU_816_NATIVE_16), "native 16-bit mode reports 16-bit"); |
| 146 |
check(!cpu_mode_is_16bit(CPU_65C02), "65C02 does not report 16-bit"); |
| 147 |
check(!cpu_mode_is_16bit(CPU_816_EMU), "emulation mode does not report 16-bit"); |
| 148 |
|
| 149 |
cpu_reset_to(CPU_816_NATIVE_16, PROG); |
| 150 |
check_eq(regs.e, 0, "native mode clears the emulation bit"); |
| 151 |
check_eq(regs.status & (FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH), 0, |
| 152 |
"native 16-bit clears both width flags"); |
| 153 |
|
| 154 |
cpu_reset_to(CPU_816_NATIVE_8, PROG); |
| 155 |
check_eq(regs.status & (FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH), |
| 156 |
FLAG_MEMORY_WIDTH | FLAG_INDEX_WIDTH, |
| 157 |
"native 8-bit sets both width flags"); |
| 158 |
|
| 159 |
cpu_reset_to(CPU_816_EMU, PROG); |
| 160 |
check_eq(regs.e, 1, "emulation mode leaves the emulation bit set"); |
| 161 |
} |
| 162 |
|
| 163 |
int |
| 164 |
main(void) |
| 165 |
{ |
| 166 |
test_reset_vector(); |
| 167 |
test_executes_an_instruction(); |
| 168 |
test_memory_round_trip(); |
| 169 |
test_reset_isolates_scenarios(); |
| 170 |
test_reset_clears_flags(); |
| 171 |
test_direct_page_penalty_does_not_persist(); |
| 172 |
test_cycles_are_per_run(); |
| 173 |
test_modes_select_register_width(); |
| 174 |
return x16_test_summary("cpu_smoke"); |
| 175 |
} |