cnpemu/test/test_CPU_6502_klaus.c

78 lines
2.4 KiB
C

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include "unity.h"
#include "CPU_6502.h"
#include "memory.h"
#include "exceptions.h"
CEXCEPTION_T e;
void setUp(void) { }
void tearDown(void) { }
void test_run_klaus(void) {
Try
{
struct MEMORY_instance *memory = MEMORY_new(65536, false);
MEMORY_load(memory, 0, "6502_functional_test.bin");
struct CPU_6502_instance *cpu = CPU_6502_new(memory, (COMMON_write_func) MEMORY_write,
(COMMON_read_func) MEMORY_read);
CPU_6502_reset(cpu);
cpu->state.pc = 0x400;
uint16_t last_pc = cpu->state.pc;
uint8_t last_test_case = 0;
uint32_t total_steps = 0;
/* http://forum.6502.org/viewtopic.php?f=1&t=7012
* http://forum.6502.org/viewtopic.php?f=8&t=6202 */
uint32_t max_steps = 100000000;
for (;;) {
uint8_t test_case = MEMORY_read(memory, 0x0200);
if (test_case != last_test_case) {
printf("Executing test case %02X (Cycles: %d)\n", test_case, total_steps);
last_test_case = test_case;
}
#ifdef DEBUG
CPU_6502_PRINT_STATE_WITH_STATUS(cpu->state);
char decode_buffer[16];
CPU_6502_disassemble_instruction(cpu, cpu->state.pc, decode_buffer, sizeof(decode_buffer));
printf("%-15s", decode_buffer);
#endif
total_steps += CPU_6502_step(cpu);
#ifdef DEBUG
CPU_6502_PRINT_STATE_WITH_STATUS(cpu->state);
#endif
if (cpu->state.pc == 0x3469 || cpu->state.pc == 0x346B ) {
printf("Performance counters:\n");
CPU_6502_PRINT_PERFORMANCE_COUNTERS(*cpu);
TEST_PASS_MESSAGE("Passed Klaus test");
break;
}
if (last_pc == cpu->state.pc) {
printf("Trapped at %04X executing test case %02X, exiting\n", cpu->state.pc, test_case);
TEST_FAIL();
}
TEST_ASSERT_LESS_THAN_UINT32_MESSAGE(max_steps, total_steps, "Exceeded maximum number of steps");
last_pc = cpu->state.pc;
}
CPU_6502_delete(&cpu);
MEMORY_delete(&memory);
} Catch(e) {
printf("Exception thrown: %s(%d)\n", EXCEPTIONS_messages[e], e);
TEST_FAIL();
}
}
int main(void)
{
UNITY_BEGIN();
RUN_TEST(test_run_klaus);
return UNITY_END();
}