changeset 1116:fe8c79f82c22

More cleanup in preparation for SMS/Mark III support
author Michael Pavone <pavone@retrodev.com>
date Thu, 22 Dec 2016 10:51:33 -0800
parents c1e78a101912
children 928a65750345
files backend_x86.c blastcpm.c genesis.c io.c io.h z80_to_x86.c z80_to_x86.h ztestrun.c
diffstat 8 files changed, 37 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/backend_x86.c	Mon Dec 19 14:16:59 2016 -0800
+++ b/backend_x86.c	Thu Dec 22 10:51:33 2016 -0800
@@ -83,6 +83,8 @@
 	
 	if (opts->address_size == SZ_D && opts->address_mask != 0xFFFFFFFF) {
 		and_ir(code, opts->address_mask, adr_reg, SZ_D);
+	} else if (opts->address_size == SZ_W && opts->address_mask != 0xFFFF) {
+		and_ir(code, opts->address_mask, adr_reg, SZ_W);
 	}
 	code_ptr lb_jcc = NULL, ub_jcc = NULL;
 	uint16_t access_flag = is_write ? MMAP_WRITE : MMAP_READ;
--- a/blastcpm.c	Mon Dec 19 14:16:59 2016 -0800
+++ b/blastcpm.c	Thu Dec 22 10:51:33 2016 -0800
@@ -101,7 +101,7 @@
 	
 	z80_options opts;
 	z80_context context;
-	init_z80_opts(&opts, z80_map, 1, io_map, 3, 1);
+	init_z80_opts(&opts, z80_map, 1, io_map, 3, 1, 0xFF);
 	init_z80_context(&context, &opts);
 	for(;;)
 	{
--- a/genesis.c	Mon Dec 19 14:16:59 2016 -0800
+++ b/genesis.c	Thu Dec 22 10:51:33 2016 -0800
@@ -45,7 +45,7 @@
 	return read_dma_value(genesis->m68k->last_prefetch_address/2);
 }
 
-void adjust_int_cycle(m68k_context * context, vdp_context * v_context)
+static void adjust_int_cycle(m68k_context * context, vdp_context * v_context)
 {
 	//static int old_int_cycle = CYCLE_NEVER;
 	genesis_context *gen = context->system;
@@ -109,7 +109,7 @@
 	z_context->int_pulse_end = z_context->int_pulse_start + MCLKS_LINE;
 }
 
-void sync_z80(z80_context * z_context, uint32_t mclks)
+static void sync_z80(z80_context * z_context, uint32_t mclks)
 {
 #ifndef NO_Z80
 	if (z80_enabled) {
@@ -121,7 +121,7 @@
 	}
 }
 
-void sync_sound(genesis_context * gen, uint32_t target)
+static void sync_sound(genesis_context * gen, uint32_t target)
 {
 	//printf("YM | Cycle: %d, bpos: %d, PSG | Cycle: %d, bpos: %d\n", gen->ym->current_cycle, gen->ym->buffer_pos, gen->psg->cycles, gen->psg->buffer_pos * 2);
 	while (target > gen->psg->cycles && target - gen->psg->cycles > MAX_SOUND_CYCLES) {
@@ -137,7 +137,8 @@
 	//printf("Target: %d, YM bufferpos: %d, PSG bufferpos: %d\n", target, gen->ym->buffer_pos, gen->psg->buffer_pos * 2);
 }
 
-uint32_t last_frame_num;
+//TODO: move this inside the system context
+static uint32_t last_frame_num;
 
 //My refresh emulation isn't currently good enough and causes more problems than it solves
 #ifdef REFRESH_EMULATION
@@ -237,7 +238,7 @@
 	return context;
 }
 
-m68k_context * vdp_port_write(uint32_t vdp_port, m68k_context * context, uint16_t value)
+static m68k_context * vdp_port_write(uint32_t vdp_port, m68k_context * context, uint16_t value)
 {
 	if (vdp_port & 0x2700E0) {
 		fatal_error("machine freeze due to write to address %X\n", 0xC00000 | vdp_port);
@@ -332,12 +333,12 @@
 	return context;
 }
 
-m68k_context * vdp_port_write_b(uint32_t vdp_port, m68k_context * context, uint8_t value)
+static m68k_context * vdp_port_write_b(uint32_t vdp_port, m68k_context * context, uint8_t value)
 {
 	return vdp_port_write(vdp_port, context, vdp_port < 0x10 ? value | value << 8 : ((vdp_port & 1) ? value : 0));
 }
 
-void * z80_vdp_port_write(uint32_t vdp_port, void * vcontext, uint8_t value)
+static void * z80_vdp_port_write(uint32_t vdp_port, void * vcontext, uint8_t value)
 {
 	z80_context * context = vcontext;
 	genesis_context * gen = context->system;
@@ -364,7 +365,7 @@
 	return context;
 }
 
-uint16_t vdp_port_read(uint32_t vdp_port, m68k_context * context)
+static uint16_t vdp_port_read(uint32_t vdp_port, m68k_context * context)
 {
 	if (vdp_port & 0x2700E0) {
 		fatal_error("machine freeze due to read from address %X\n", 0xC00000 | vdp_port);
@@ -404,7 +405,7 @@
 	return value;
 }
 
-uint8_t vdp_port_read_b(uint32_t vdp_port, m68k_context * context)
+static uint8_t vdp_port_read_b(uint32_t vdp_port, m68k_context * context)
 {
 	uint16_t value = vdp_port_read(vdp_port, context);
 	if (vdp_port & 1) {
@@ -414,7 +415,7 @@
 	}
 }
 
-uint8_t z80_vdp_port_read(uint32_t vdp_port, void * vcontext)
+static uint8_t z80_vdp_port_read(uint32_t vdp_port, void * vcontext)
 {
 	z80_context * context = vcontext;
 	if (vdp_port & 0xE0) {
@@ -449,9 +450,10 @@
 	return vdp_port & 1 ? ret : ret >> 8;
 }
 
-uint32_t zram_counter = 0;
+//TODO: Move this inside the system context
+static uint32_t zram_counter = 0;
 
-m68k_context * io_write(uint32_t location, m68k_context * context, uint8_t value)
+static m68k_context * io_write(uint32_t location, m68k_context * context, uint8_t value)
 {
 	genesis_context * gen = context->system;
 	if (location < 0x10000) {
@@ -555,7 +557,7 @@
 	return context;
 }
 
-m68k_context * io_write_w(uint32_t location, m68k_context * context, uint16_t value)
+static m68k_context * io_write_w(uint32_t location, m68k_context * context, uint16_t value)
 {
 	if (location < 0x10000 || (location & 0x1FFF) >= 0x100) {
 		return io_write(location, context, value >> 8);
@@ -571,7 +573,7 @@
 #define EUR (HZ50|FOREIGN)
 #define NO_DISK 0x20
 
-uint8_t io_read(uint32_t location, m68k_context * context)
+static uint8_t io_read(uint32_t location, m68k_context * context)
 {
 	uint8_t value;
 	genesis_context *gen = context->system;
@@ -637,7 +639,7 @@
 	return value;
 }
 
-uint16_t io_read_w(uint32_t location, m68k_context * context)
+static uint16_t io_read_w(uint32_t location, m68k_context * context)
 {
 	uint16_t value = io_read(location, context);
 	if (location < 0x10000 || (location & 0x1FFF) < 0x100) {
@@ -649,7 +651,7 @@
 	return value;
 }
 
-void * z80_write_ym(uint32_t location, void * vcontext, uint8_t value)
+static void * z80_write_ym(uint32_t location, void * vcontext, uint8_t value)
 {
 	z80_context * context = vcontext;
 	genesis_context * gen = context->system;
@@ -664,7 +666,7 @@
 	return context;
 }
 
-uint8_t z80_read_ym(uint32_t location, void * vcontext)
+static uint8_t z80_read_ym(uint32_t location, void * vcontext)
 {
 	z80_context * context = vcontext;
 	genesis_context * gen = context->system;
@@ -672,7 +674,7 @@
 	return ym_read_status(gen->ym);
 }
 
-uint8_t z80_read_bank(uint32_t location, void * vcontext)
+static uint8_t z80_read_bank(uint32_t location, void * vcontext)
 {
 	z80_context * context = vcontext;
 	genesis_context *gen = context->system;
@@ -699,7 +701,7 @@
 	return 0;
 }
 
-void *z80_write_bank(uint32_t location, void * vcontext, uint8_t value)
+static void *z80_write_bank(uint32_t location, void * vcontext, uint8_t value)
 {
 	z80_context * context = vcontext;
 	genesis_context *gen = context->system;
@@ -726,7 +728,7 @@
 	return context;
 }
 
-void *z80_write_bank_reg(uint32_t location, void * vcontext, uint8_t value)
+static void *z80_write_bank_reg(uint32_t location, void * vcontext, uint8_t value)
 {
 	z80_context * context = vcontext;
 
@@ -748,7 +750,7 @@
 	context->master_clock = ((uint64_t)context->normal_clock * (uint64_t)percent) / 100;
 	while (context->ym->current_cycle != context->psg->cycles) {
 		sync_sound(context, context->psg->cycles + MCLKS_PER_PSG);
-}
+	}
 	ym_adjust_master_clock(context->ym, context->master_clock);
 	psg_adjust_master_clock(context->psg, context->master_clock);
 }
@@ -870,6 +872,7 @@
 	genesis_context *gen = (genesis_context *)system;
 	vdp_free(gen->vdp);
 	m68k_options_free(gen->m68k->options);
+	free(gen->cart);
 	free(gen->m68k);
 	free(gen->work_ram);
 	z80_options_free(gen->z80->options);
@@ -880,6 +883,7 @@
 	free(gen->save_storage);
 	free(gen->header.save_dir);
 	free(gen->lock_on);
+	free(gen);
 }
 
 genesis_context *alloc_init_genesis(rom_info *rom, void *main_rom, void *lock_on, uint32_t system_opts, uint8_t force_region)
@@ -923,7 +927,7 @@
 	z80_map[0].buffer = gen->zram = calloc(1, Z80_RAM_BYTES);
 #ifndef NO_Z80
 	z80_options *z_opts = malloc(sizeof(z80_options));
-	init_z80_opts(z_opts, z80_map, 5, NULL, 0, MCLKS_PER_Z80);
+	init_z80_opts(z_opts, z80_map, 5, NULL, 0, MCLKS_PER_Z80, 0xFFFF);
 	init_z80_context(gen->z80, z_opts);
 	z80_assert_reset(gen->z80, 0);
 #endif
@@ -935,7 +939,7 @@
 	gen->cart = main_rom;
 	gen->lock_on = lock_on;
 	gen->work_ram = calloc(2, RAM_WORDS);
-	setup_io_devices(config, rom, gen);
+	setup_io_devices(config, rom, &gen->io);
 
 	gen->save_type = rom->save_type;
 	gen->save_type = rom->save_type;
--- a/io.c	Mon Dec 19 14:16:59 2016 -0800
+++ b/io.c	Thu Dec 22 10:51:33 2016 -0800
@@ -713,9 +713,9 @@
 	unlink(sockfile_name);
 }
 
-void setup_io_devices(tern_node * config, rom_info *rom, genesis_context *gen)
+void setup_io_devices(tern_node * config, rom_info *rom, sega_io *io)
 {
-	current_io = &gen->io;
+	current_io = io;
 	io_port * ports = current_io->ports;
 	tern_node *io_nodes = tern_get_node(tern_find_path(config, "io\0devices\0"));
 	char * io_1 = rom->port1_override ? rom->port1_override : tern_find_ptr(io_nodes, "1");
--- a/io.h	Mon Dec 19 14:16:59 2016 -0800
+++ b/io.h	Thu Dec 22 10:51:33 2016 -0800
@@ -85,7 +85,7 @@
 
 void set_keybindings(sega_io *io);
 void map_all_bindings(sega_io *io);
-void setup_io_devices(tern_node * config, rom_info *rom, genesis_context * gen);
+void setup_io_devices(tern_node * config, rom_info *rom, sega_io *io);
 void io_adjust_cycles(io_port * pad, uint32_t current_cycle, uint32_t deduction);
 void io_data_write(io_port * pad, uint8_t value, uint32_t current_cycle);
 uint8_t io_data_read(io_port * pad, uint32_t current_cycle);
--- a/z80_to_x86.c	Mon Dec 19 14:16:59 2016 -0800
+++ b/z80_to_x86.c	Thu Dec 22 10:51:33 2016 -0800
@@ -3120,7 +3120,7 @@
 	} while (opts->gen.deferred);
 }
 
-void init_z80_opts(z80_options * options, memmap_chunk const * chunks, uint32_t num_chunks, memmap_chunk const * io_chunks, uint32_t num_io_chunks, uint32_t clock_divider)
+void init_z80_opts(z80_options * options, memmap_chunk const * chunks, uint32_t num_chunks, memmap_chunk const * io_chunks, uint32_t num_io_chunks, uint32_t clock_divider, uint32_t io_address_mask)
 {
 	memset(options, 0, sizeof(*options));
 
@@ -3386,7 +3386,7 @@
 
 	//HACK
 	options->gen.address_size = SZ_D;
-	options->gen.address_mask = 0xFF;
+	options->gen.address_mask = io_address_mask;
 	options->read_io = gen_mem_fun(&options->gen, io_chunks, num_io_chunks, READ_8, NULL);
 	options->write_io = gen_mem_fun(&options->gen, io_chunks, num_io_chunks, WRITE_8, NULL);
 	options->gen.address_size = SZ_W;
--- a/z80_to_x86.h	Mon Dec 19 14:16:59 2016 -0800
+++ b/z80_to_x86.h	Thu Dec 22 10:51:33 2016 -0800
@@ -88,7 +88,7 @@
 } z80_context;
 
 void translate_z80_stream(z80_context * context, uint32_t address);
-void init_z80_opts(z80_options * options, memmap_chunk const * chunks, uint32_t num_chunks, memmap_chunk const * io_chunks, uint32_t num_io_chunks, uint32_t clock_divider);
+void init_z80_opts(z80_options * options, memmap_chunk const * chunks, uint32_t num_chunks, memmap_chunk const * io_chunks, uint32_t num_io_chunks, uint32_t clock_divider, uint32_t io_address_mask);
 void z80_options_free(z80_options *opts);
 void init_z80_context(z80_context * context, z80_options * options);
 code_ptr z80_get_native_address(z80_context * context, uint32_t address);
--- a/ztestrun.c	Mon Dec 19 14:16:59 2016 -0800
+++ b/ztestrun.c	Thu Dec 22 10:51:33 2016 -0800
@@ -89,7 +89,7 @@
 		exit(1);
 	}
 	fclose(f);
-	init_z80_opts(&opts, z80_map, 2, port_map, 1, 1);
+	init_z80_opts(&opts, z80_map, 2, port_map, 1, 1, 0xFF);
 	init_z80_context(&context, &opts);
 	//Z80 RAM
 	context.mem_pointers[0] = z80_ram;