summaryrefslogtreecommitdiff
path: root/emu/tests/test_cpu_6502.c
blob: 6f53ff050b97050fb4fa3807e5a5da9e5c2d40ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdlib.h>
#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;
	
	suite = suite_create( "6502 CPU tests" );
	
	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;
}

int main( void )
{
	SRunner *runner;
	int tests_failed = 0;
	
	runner = srunner_create( make_cpu_6502_testsuite( ) );
	srunner_set_fork_status( runner, CK_NOFORK );
	
	srunner_set_log( runner, "test.log" );
	srunner_set_xml( runner, "test.xml" );
	srunner_run_all( runner, CK_VERBOSE );
	
	tests_failed = srunner_ntests_failed( runner );
	srunner_free( runner );
	
	exit( ( tests_failed == 0 ) ? EXIT_SUCCESS : EXIT_FAILURE );
}