-
Notifications
You must be signed in to change notification settings - Fork 0
/
coretest.c
170 lines (132 loc) · 3.85 KB
/
coretest.c
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
spiffy - ZX spectrum emulator
Copyright Edward Cree, 2010-13
coretest.c - Z80 core tests
Acknowledgements: Based on the FUSE coretests by Phil Kendall <[email protected]>
<http://fuse-emulator.sourceforge.net/>
*/
#include "coretest.h"
#include <string.h>
#include <errno.h>
#include "ops.h"
#include "vchips.h"
int read_test( FILE *f, unsigned int *end_tstates, z80 *cpu, uint8_t *memory )
{
const char *progname="spiffy";
unsigned af, bc, de, hl, af_, bc_, de_, hl_, ix, iy, sp, pc;
unsigned i, r, iff1, iff2, im;
unsigned end_tstates2;
unsigned address;
char test_name[ 80 ];
do {
if( !fgets( test_name, sizeof( test_name ), f ) ) {
if( feof( f ) ) return 1;
fprintf( stderr, "%s: error reading test description from file: %s\n",
progname, strerror( errno ) );
return 1;
}
} while( test_name[0] == '\n' );
if( fscanf( f, "%x %x %x %x %x %x %x %x %x %x %x %x", &af, &bc,
&de, &hl, &af_, &bc_, &de_, &hl_, &ix, &iy, &sp, &pc ) != 12 ) {
fprintf( stderr, "%s: first registers line in file corrupt\n", progname);
return 1;
}
*AF = af; *BC = bc; *DE = de; *HL = hl;
*AF_ = af_; *BC_ = bc_; *DE_ = de_; *HL_ = hl_;
*Ix = ix; *Iy = iy; *SP = sp; *PC = pc;
int halted;
if( fscanf( f, "%x %x %u %u %u %d %u", &i, &r, &iff1, &iff2, &im,
&halted, &end_tstates2 ) != 7 ) {
fprintf( stderr, "%s: second registers line in file corrupt\n", progname);
return 1;
}
*Intvec = i; *Refresh = r; cpu->IFF[0] = iff1; cpu->IFF[1] = iff2; cpu->intmode = im; cpu->halt=halted;
*end_tstates = end_tstates2;
while( 1 ) {
if( fscanf( f, "%x", &address ) != 1 ) {
fprintf( stderr, "%s: no address found in file\n", progname);
return 1;
}
if( address >= 0x10000 ) break;
while( 1 ) {
unsigned byte;
if( fscanf( f, "%x", &byte ) != 1 ) {
fprintf( stderr, "%s: no data byte found in file\n", progname);
return 1;
}
if( byte >= 0x100 ) break;
memory[ address++ ] = byte;
}
}
printf( "%s", test_name );
return 0;
}
void dump_z80_state( z80 *cpu, unsigned int tstates )
{
printf( "%04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x %04x\n",
*AF, *BC, *DE, *HL, *AF_, *BC_, *DE_, *HL_, *Ix, *Iy, *SP, *PC );
printf( "%02x %02x %d %d %d %d %d\n", *Intvec, *Refresh,
cpu->IFF[0], cpu->IFF[1], cpu->intmode, cpu->halt, tstates );
}
void dump_memory_state( uint8_t *memory, uint8_t *initial_memory )
{
size_t i;
for( i = 0; i < 0x10000; i++ ) {
if( memory[ i ] == initial_memory[ i ] ) continue;
printf( "%04x ", (unsigned)i );
while( i < 0x10000 && memory[ i ] != initial_memory[ i ] )
printf( "%02x ", memory[ i++ ] );
printf( "-1\n" );
}
}
int run_test(FILE *f)
{
size_t i;
unsigned int tstates=0;
uint8_t memory[0x10000], initial_memory[0x10000];
for( i = 0; i < 0x10000; i += 4 ) {
memory[ i ] = 0xde; memory[ i + 1 ] = 0xad;
memory[ i + 2 ] = 0xbe; memory[ i + 3 ] = 0xef;
}
z80 _cpu, *cpu=&_cpu;
bus_t _bus, *bus=&_bus;
ram_t _ram, *ram=&_ram;
z80_reset(cpu, bus);
ram_init(ram, NULL, MACHINE_48);
unsigned int end_tstates;
if( read_test( f, &end_tstates, cpu, memory ) ) return 0;
/* fill in the ram_t */
for(unsigned int i=0;i<0x10000;i++)
ram->bank[i>>14][i&0x3fff]=memory[i];
/* Grab a copy of the memory for comparison at the end */
memcpy( initial_memory, memory, 0x10000 );
int errupt=0;
while(!errupt)
{
do_ram(ram, bus);
fflush(stdout);
if(cpu->nothing)
{
cpu->nothing--;
if(cpu->steps)
cpu->steps--;
cpu->dT++;
}
else
errupt=z80_tstep(cpu, bus, errupt);
fflush(stderr);
if(++tstates>=end_tstates)
{
if((cpu->M==0)&&(cpu->dT==0)&&!cpu->block_ints)
errupt++;
}
}
/* copy memory back out of the ram_t */
for(unsigned int i=0;i<0x10000;i++)
memory[i]=ram->bank[i>>14][i&0x3fff];
/* And dump our final state */
dump_z80_state(cpu, tstates);
dump_memory_state(memory, initial_memory);
printf( "\n" );
return 1;
}