main ztestrun.c
  1/*
  2 Copyright 2013 Michael Pavone
  3 This file is part of BlastEm.
  4 BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
  5*/
  6#include "z80inst.h"
  7#include "z80_to_x86.h"
  8#include "mem.h"
  9#include "vdp.h"
 10#include <stdio.h>
 11#include <stdlib.h>
 12#include <stddef.h>
 13#include <stdarg.h>
 14
 15void fatal_error(char *format, ...)
 16{
 17	va_list args;
 18	va_start(args, format);
 19	vfprintf(stderr, format, args);
 20	va_end(args);
 21	exit(1);
 22}
 23
 24uint8_t z80_ram[0x2000];
 25
 26uint8_t z80_unmapped_read(uint32_t location, void * context)
 27{
 28	return 0xFF;
 29}
 30
 31void * z80_unmapped_write(uint32_t location, void * context, uint8_t value)
 32{
 33	return context;
 34}
 35
 36const memmap_chunk z80_map[] = {
 37	{ 0x0000, 0x4000,  0x1FFF, 0, 0, MMAP_READ | MMAP_WRITE | MMAP_CODE, z80_ram, NULL, NULL, NULL,              NULL },
 38	{ 0x4000, 0x10000, 0xFFFF, 0, 0, 0,                                  NULL,    NULL, NULL, z80_unmapped_read, z80_unmapped_write}
 39};
 40
 41const memmap_chunk port_map[] = {
 42	{ 0x0000, 0x100, 0xFF, 0, 0, 0,                                  NULL,    NULL, NULL, z80_unmapped_read, z80_unmapped_write}
 43};
 44
 45void z80_next_int_pulse(z80_context * context)
 46{
 47	context->int_pulse_start = context->int_pulse_end = CYCLE_NEVER;
 48}
 49
 50int main(int argc, char ** argv)
 51{
 52	long filesize;
 53	uint8_t *filebuf;
 54	z80_options opts;
 55	z80_context *context;
 56	char *fname = NULL;
 57	uint8_t retranslate = 0;
 58	for (int i = 1; i < argc; i++)
 59	{
 60		if (argv[i][0] == '-') {
 61			switch(argv[i][1])
 62			{
 63			case 'r':
 64				retranslate = 1;
 65				break;
 66			default:
 67				fprintf(stderr, "Unrecognized switch -%c\n", argv[i][1]);
 68				exit(1);
 69			}
 70		} else if (!fname) {
 71			fname = argv[i];
 72		}
 73	}
 74	if (!fname) {
 75		fputs("usage: ztestrun zrom [cartrom]\n", stderr);
 76		exit(1);
 77	}
 78	FILE * f = fopen(fname, "rb");
 79	if (!f) {
 80		fprintf(stderr, "unable to open file %s\n", fname);
 81		exit(1);
 82	}
 83	fseek(f, 0, SEEK_END);
 84	filesize = ftell(f);
 85	fseek(f, 0, SEEK_SET);
 86	filesize = filesize < sizeof(z80_ram) ? filesize : sizeof(z80_ram);
 87	if (fread(z80_ram, 1, filesize, f) != filesize) {
 88		fprintf(stderr, "error reading %s\n",fname);
 89		exit(1);
 90	}
 91	fclose(f);
 92	init_z80_opts(&opts, z80_map, 2, port_map, 1, 1, 0xFF);
 93	context = init_z80_context(&opts);
 94	//Z80 RAM
 95	context->mem_pointers[0] = z80_ram;
 96	if (retranslate) {
 97		//run core long enough to translate code
 98		z80_run(context, 1);
 99		for (int i = 0; i < filesize; i++)
100		{
101			z80_handle_code_write(i, context);
102		}
103		z80_assert_reset(context, context->current_cycle);
104		z80_clear_reset(context, context->current_cycle + 3);
105		z80_adjust_cycles(context, context->current_cycle);
106	}
107	z80_run(context, 1000);
108	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",
109		context->regs[Z80_A], context->regs[Z80_B], context->regs[Z80_C],
110		context->regs[Z80_D], context->regs[Z80_E],
111		(context->regs[Z80_H] << 8) | context->regs[Z80_L],
112		(context->regs[Z80_IXH] << 8) | context->regs[Z80_IXL],
113		(context->regs[Z80_IYH] << 8) | context->regs[Z80_IYL],
114		context->sp, context->im, context->iff1, context->iff2);
115	printf("Flags: SZYHXVNC\n"
116	       "       %d%d%d%d%d%d%d%d\n", 
117			context->flags[ZF_S], context->flags[ZF_Z], context->flags[ZF_XY] >> 5 & 1, context->flags[ZF_H], 
118			context->flags[ZF_XY] >> 3 & 1, context->flags[ZF_PV], context->flags[ZF_N], context->flags[ZF_C]
119	);
120	puts("--Alternate Regs--");
121	printf("A: %X\nB: %X\nC: %X\nD: %X\nE: %X\nHL: %X\n",
122		context->alt_regs[Z80_A], context->alt_regs[Z80_B], context->alt_regs[Z80_C],
123		context->alt_regs[Z80_D], context->alt_regs[Z80_E],
124		(context->alt_regs[Z80_H] << 8) | context->alt_regs[Z80_L]);
125	return 0;
126}