summaryrefslogtreecommitdiff
path: root/ztestrun.c
blob: 42c934ff378190b23bb505da3a92cf70cde845f0 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 Copyright 2013 Michael Pavone
 This file is part of BlastEm.
 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
*/
#include "z80inst.h"
#ifdef NEW_CORE
#include "z80.h"
#include <string.h>
#else
#include "z80_to_x86.h"
#endif
#include "mem.h"
#include "vdp.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>

void fatal_error(char *format, ...)
{
	va_list args;
	va_start(args, format);
	vfprintf(stderr, format, args);
	va_end(args);
	exit(1);
}

uint8_t z80_ram[0x2000];

uint8_t z80_unmapped_read(uint32_t location, void * context)
{
	return 0xFF;
}

void * z80_unmapped_write(uint32_t location, void * context, uint8_t value)
{
	return context;
}

const memmap_chunk z80_map[] = {
	{ 0x0000, 0x4000,  0x1FFF, 0, 0, MMAP_READ | MMAP_WRITE | MMAP_CODE, z80_ram, NULL, NULL, NULL,              NULL },
	{ 0x4000, 0x10000, 0xFFFF, 0, 0, 0,                                  NULL,    NULL, NULL, z80_unmapped_read, z80_unmapped_write}
};

const memmap_chunk port_map[] = {
	{ 0x0000, 0x100, 0xFF, 0, 0, 0,                                  NULL,    NULL, NULL, z80_unmapped_read, z80_unmapped_write}
};

#ifndef NEW_CORE
void z80_next_int_pulse(z80_context * context)
{
	context->int_pulse_start = context->int_pulse_end = CYCLE_NEVER;
}
#endif

int main(int argc, char ** argv)
{
	long filesize;
	uint8_t *filebuf;
	z80_options opts;
	z80_context *context;
	char *fname = NULL;
	uint8_t retranslate = 0;
	for (int i = 1; i < argc; i++)
	{
		if (argv[i][0] == '-') {
			switch(argv[i][1])
			{
			case 'r':
				retranslate = 1;
				break;
			default:
				fprintf(stderr, "Unrecognized switch -%c\n", argv[i][1]);
				exit(1);
			}
		} else if (!fname) {
			fname = argv[i];
		}
	}
	if (!fname) {
		fputs("usage: ztestrun zrom [cartrom]\n", stderr);
		exit(1);
	}
	FILE * f = fopen(fname, "rb");
	if (!f) {
		fprintf(stderr, "unable to open file %s\n", fname);
		exit(1);
	}
	fseek(f, 0, SEEK_END);
	filesize = ftell(f);
	fseek(f, 0, SEEK_SET);
	filesize = filesize < sizeof(z80_ram) ? filesize : sizeof(z80_ram);
	if (fread(z80_ram, 1, filesize, f) != filesize) {
		fprintf(stderr, "error reading %s\n",fname);
		exit(1);
	}
	fclose(f);
#ifdef NEW_CORE
	memset(&opts, 0, sizeof(opts));
	opts.gen.memmap = z80_map;
	opts.gen.memmap_chunks = 2;
	opts.gen.address_mask = 0xFFFF;
	opts.gen.max_address = 0xFFFF;
	opts.gen.clock_divider = 1;
	context = calloc(1, sizeof(z80_context));
	context->opts = &opts;
	z80_execute(context, 1000);
	printf("A: %X\nB: %X\nC: %X\nD: %X\nE: %X\nHL: %X\nIX: %X\nIY: %X\nSP: %X\n\nIM: %d, IFF1: %d, IFF2: %d\n",
		context->main[7], context->main[0], context->main[1],
		context->main[2], context->main[3],
		(context->main[4] << 8) | context->main[5],
		context->ix,
		context->iy,
		context->sp, context->imode, context->iff1, context->iff2);
	printf("Flags: SZYHXVNC\n"
	       "       %d%d%d%d%d%d%d%d\n", 
			context->last_flag_result >> 7, context->zflag != 0, context->last_flag_result >> 5 & 1, context->chflags >> 3 & 1, 
			context->last_flag_result >> 3 & 1, context->pvflag != 0, context->nflag != 0, context->chflags >> 7 & 1
	);
	puts("--Alternate Regs--");
	printf("A: %X\nB: %X\nC: %X\nD: %X\nE: %X\nHL: %X\n",
		context->alt[7], context->alt[0], context->alt[1],
		context->alt[2], context->alt[3],
		(context->alt[4] << 8) | context->alt[5]);
#else
	init_z80_opts(&opts, z80_map, 2, port_map, 1, 1, 0xFF);
	context = init_z80_context(&opts);
	//Z80 RAM
	context->mem_pointers[0] = z80_ram;
	if (retranslate) {
		//run core long enough to translate code
		z80_run(context, 1);
		for (int i = 0; i < filesize; i++)
		{
			z80_handle_code_write(i, context);
		}
		z80_assert_reset(context, context->current_cycle);
		z80_clear_reset(context, context->current_cycle + 3);
		z80_adjust_cycles(context, context->current_cycle);
	}
	z80_run(context, 1000);
	printf("A: %X\nB: %X\nC: %X\nD: %X\nE: %X\nHL: %X\nIX: %X\nIY: %X\nSP: %X\n\nIM: %d, IFF1: %d, IFF2: %d\n",
		context->regs[Z80_A], context->regs[Z80_B], context->regs[Z80_C],
		context->regs[Z80_D], context->regs[Z80_E],
		(context->regs[Z80_H] << 8) | context->regs[Z80_L],
		(context->regs[Z80_IXH] << 8) | context->regs[Z80_IXL],
		(context->regs[Z80_IYH] << 8) | context->regs[Z80_IYL],
		context->sp, context->im, context->iff1, context->iff2);
	printf("Flags: SZYHXVNC\n"
	       "       %d%d%d%d%d%d%d%d\n", 
			context->flags[ZF_S], context->flags[ZF_Z], context->flags[ZF_XY] >> 5 & 1, context->flags[ZF_H], 
			context->flags[ZF_XY] >> 3 & 1, context->flags[ZF_PV], context->flags[ZF_N], context->flags[ZF_C]
	);
	puts("--Alternate Regs--");
	printf("A: %X\nB: %X\nC: %X\nD: %X\nE: %X\nHL: %X\n",
		context->alt_regs[Z80_A], context->alt_regs[Z80_B], context->alt_regs[Z80_C],
		context->alt_regs[Z80_D], context->alt_regs[Z80_E],
		(context->alt_regs[Z80_H] << 8) | context->alt_regs[Z80_L]);
#endif
	return 0;
}