diff options
Diffstat (limited to 'emu/tests/test_cpu_6502.c')
-rw-r--r-- | emu/tests/test_cpu_6502.c | 100 |
1 files changed, 94 insertions, 6 deletions
diff --git a/emu/tests/test_cpu_6502.c b/emu/tests/test_cpu_6502.c index 0eb6a2b..6f53ff0 100644 --- a/emu/tests/test_cpu_6502.c +++ b/emu/tests/test_cpu_6502.c @@ -2,31 +2,119 @@ #include <stdint.h> #include <check.h> +#include "6502.h" +#include "bus.h" +#include "memory.h" + +enum { + ROM_START = 0xf800, + ROM_END = 0xffff, + ROM_SIZE = 2048, + + RAM_START = 0x0000, + RAM_END = 0x01ff, + RAM_SIZE = 512 +}; + +static cpu_6502_t cpu; +static bus_t bus; +static memory_t rom; +static memory_t ram; +static uint8_t code[ROM_SIZE]; +static int codesize; + void setup( void ) { + bus_init( &bus ); + + cpu_6502_init( &cpu, &bus, true ); + cpu.debug_flags |= DEBUG_STATUS | DEBUG_STACK | DEBUG_ZERO_PAGE; + + memory_init( &rom, MEMORY_ROM, ROM_START, ROM_SIZE, true ); + bus_register( &bus, &rom.base, ROM_START, ROM_END ); + + memory_init( &ram, MEMORY_RAM, RAM_START, RAM_SIZE, true ); + bus_register( &bus, &ram.base, RAM_START, RAM_END ); + + // install code start vector + code[0] = ROM_START & 0x00ff; + code[1] = ( ROM_START & 0xff00 ) >> 8; + codesize = 2; + memory_set( &rom, 0x07fc, code, codesize ); } void teardown( void ) { + bus_deinit( &bus ); } START_TEST( test_cpu_6502_nop ) { + cpu_6502_t before_cpu; + + cpu_6502_reset( &cpu ); + memory_reset( &rom ); + memory_reset( &ram ); + + code[0] = 0xea; // NOP + codesize = 1; + memory_set( &rom, 0x00, code, codesize ); + + memcpy( &before_cpu, &cpu, sizeof( cpu ) ); + cpu_6502_run( &cpu, 1 ); + + ck_assert_int_eq( cpu.error_state, ERROR_STATE_OK ); + ck_assert_int_eq( cpu.PC, before_cpu.PC+1 ); + ck_assert_int_eq( cpu.SP, before_cpu.SP ); + ck_assert_int_eq( cpu.PS, before_cpu.PS ); + ck_assert_int_eq( cpu.A, before_cpu.A ); + ck_assert_int_eq( cpu.X, before_cpu.X ); + ck_assert_int_eq( cpu.Y, before_cpu.Y ); +} +END_TEST + +START_TEST( test_cpu_6502_txs ) +{ + cpu_6502_t before_cpu; + + cpu_6502_reset( &cpu ); + memory_reset( &rom ); + memory_reset( &ram ); + + code[0] = 0x9a; // TXS + codesize = 1; + memory_set( &rom, 0x00, code, codesize ); + + cpu.X = 0xff; + + memcpy( &before_cpu, &cpu, sizeof( cpu ) ); + cpu_6502_run( &cpu, 1 ); + + ck_assert_int_eq( cpu.error_state, ERROR_STATE_OK ); + ck_assert_int_eq( cpu.PC, before_cpu.PC+1 ); + ck_assert_int_eq( cpu.SP, cpu.X ); + ck_assert_int_eq( cpu.PS, before_cpu.PS ); + ck_assert_int_eq( cpu.A, before_cpu.A ); + ck_assert_int_eq( cpu.X, 0xff ); + ck_assert_int_eq( cpu.Y, before_cpu.Y ); } END_TEST Suite *make_cpu_6502_testsuite( void ) { Suite *suite; - TCase *tc_opcodes; suite = suite_create( "6502 CPU tests" ); - tc_opcodes = tcase_create( "test opcodes" ); - - tcase_add_test( tc_opcodes, test_cpu_6502_nop ); - - suite_add_tcase( suite, tc_opcodes ); + TCase *tc_nop = tcase_create( "NOP" ); + tcase_add_checked_fixture( tc_nop, setup, teardown); + tcase_add_test( tc_nop, test_cpu_6502_nop ); + suite_add_tcase( suite, tc_nop ); + + TCase *tc_txs = tcase_create( "TXS" ); + tcase_add_checked_fixture( tc_txs, setup, teardown); + tcase_add_test( tc_txs, test_cpu_6502_txs ); + suite_add_tcase( suite, tc_txs ); return suite; } |