ewm/cpu_test.c
2016-11-23 22:17:24 -05:00

86 lines
3.1 KiB
C

// The MIT License (MIT)
//
// Copyright (c) 2015 Stefan Arentz - http://github.com/st3fan/ewm
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "cpu.h"
#include "mem.h"
int test(int model, uint16_t start_addr, uint16_t success_addr, char *rom_path) {
struct cpu_t cpu;
cpu_init(&cpu, model);
cpu_add_ram_file(&cpu, 0x0000, rom_path);
cpu_reset(&cpu);
cpu.state.pc = start_addr;
uint16_t last_pc = cpu.state.pc;
while (true) {
int ret = cpu_step(&cpu);
if (ret != 0) {
switch (ret) {
case EWM_CPU_ERR_UNIMPLEMENTED_INSTRUCTION:
fprintf(stderr, "TEST Unimplemented instruction 0x%.2x at 0x%.4x\n",
mem_get_byte(&cpu, cpu.state.pc), cpu.state.pc);
return -1;
default:
fprintf(stderr, "TEST Unexpected error %d\n", ret);
return -1;
}
}
// End of the tests is at 0x3399. This is hard coded and not
// ideal. Is there a better way to detect this?
if (cpu.state.pc == success_addr) {
fprintf(stderr, "TEST Success\n");
return 0;
}
// We detect a test failure because we are in a branch deadlock,
// which we can easily detect by remembering the previous pc and
// then looking at what we are about to execute.
if (cpu.state.pc == last_pc) {
uint8_t i = mem_get_byte(&cpu, cpu.state.pc);
if (i == 0x10 || i == 0x30 || i == 0x50 || i == 0x70 || i == 0x90 || i == 0xb0 || i == 0xd0 || i == 0xf0) {
if (mem_get_byte(&cpu, cpu.state.pc + 1) == 0xfe) {
fprintf(stderr, "TEST Failure at 0x%.4x \n", cpu.state.pc);
return -1;
}
}
}
last_pc = cpu.state.pc;
}
}
int main(int argc, char **argv) {
cpu_setup();
fprintf(stderr, "TEST Running 6502 tests\n");
test(EWM_CPU_MODEL_6502, 0x0400, 0x3399, "roms/6502_functional_test.bin");
fprintf(stderr, "TEST Running 65C02 tests\n");
test(EWM_CPU_MODEL_65C02, 0x0400, 0x24a8, "roms/65C02_extended_opcodes_test.bin");
}