changeset 1129:6b5c92b6205c

Enabled Z80 debugger in PBC mode
author Michael Pavone <pavone@retrodev.com>
date Wed, 28 Dec 2016 12:28:52 -0800
parents 093c19f34dfd
children 8f14767661fa
files debug.c debug.h sms.c
diffstat 3 files changed, 47 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/debug.c	Tue Dec 27 18:58:31 2016 -0800
+++ b/debug.c	Wed Dec 28 12:28:52 2016 -0800
@@ -343,17 +343,9 @@
 	} else {
 		zremove_breakpoint(context, address);
 	}
-	uint8_t * pc;
-	if (address < 0x4000) {
-		pc = system->zram + (address & 0x1FFF);
-	} else if (address >= 0x8000) {
-		if (context->bank_reg < (0x400000 >> 15)) {
-			fatal_error("Entered Z80 debugger in banked memory address %X, which is not yet supported\n", address);
-		} else {
-			fatal_error("Entered Z80 debugger in banked memory address %X, but the bank is not pointed to a cartridge address\n", address);
-		}
-	} else {
-		fatal_error("Entered Z80 debugger at address %X\n", address);
+	uint8_t * pc = get_native_pointer(address, (void **)context->mem_pointers, &context->options->gen);
+	if (!pc) {
+		fatal_error("Failed to get native pointer on entering Z80 debugger at address %X\n", address);
 	}
 	for (disp_def * cur = zdisplays; cur; cur = cur->next) {
 		zdebugger_print(context, cur->format_char, cur->param);
@@ -470,8 +462,13 @@
 				} else if(inst.op == Z80_JR) {
 					after += inst.immed;
 				} else if(inst.op == Z80_RET) {
-					if (context->sp < 0x4000) {
-						after = system->zram[context->sp & 0x1FFF] | system->zram[(context->sp+1) & 0x1FFF] << 8;
+					uint8_t *sp = get_native_pointer(context->sp, (void **)context->mem_pointers, &context->options->gen);
+					if (sp) {
+						after = *sp;
+						sp = get_native_pointer((context->sp + 1) & 0xFFFF, (void **)context->mem_pointers, &context->options->gen);
+						if (sp) {
+							after |= *sp << 8;
+						}
 					}
 				}
 				zinsert_breakpoint(context, after, (uint8_t *)zdebugger);
@@ -495,14 +492,33 @@
 					fputs("s command requires a file name\n", stderr);
 					break;
 				}
-				FILE * f = fopen(param, "wb");
-				if (f) {
-					if(fwrite(system->zram, 1, Z80_RAM_BYTES, f) != Z80_RAM_BYTES) {
-						fputs("Error writing file\n", stderr);
+				memmap_chunk const *ram_chunk = NULL;
+				for (int i = 0; i < context->options->gen.memmap_chunks; i++)
+				{
+					memmap_chunk const *cur = context->options->gen.memmap + i;
+					if (cur->flags & MMAP_WRITE) {
+						ram_chunk = cur;
+						break;
+					}
+				}
+				if (ram_chunk) {
+					uint32_t size = ram_chunk->end - ram_chunk->start;
+					if (size > ram_chunk->mask) {
+						size = ram_chunk->mask+1;
 					}
-					fclose(f);
+					uint8_t *buf = get_native_pointer(ram_chunk->start, (void **)context->mem_pointers, &context->options->gen);
+					FILE * f = fopen(param, "wb");
+					if (f) {
+						if(fwrite(buf, 1, size, f) != size) {
+							fputs("Error writing file\n", stderr);
+						}
+						fclose(f);
+						printf("Wrote %d bytes to %s\n", size, param);
+					} else {
+						fprintf(stderr, "Could not open %s for writing\n", param);
+					}
 				} else {
-					fprintf(stderr, "Could not open %s for writing\n", param);
+					fputs("Failed to find a RAM memory chunk\n", stderr);
 				}
 				break;
 			}
--- a/debug.h	Tue Dec 27 18:58:31 2016 -0800
+++ b/debug.h	Wed Dec 28 12:28:52 2016 -0800
@@ -3,6 +3,7 @@
 
 #include <stdint.h>
 #include "m68k_core.h"
+#include "z80_to_x86.h"
 
 typedef struct disp_def {
 	struct disp_def * next;
@@ -23,5 +24,6 @@
 void add_display(disp_def ** head, uint32_t *index, char format_char, char * param);
 void remove_display(disp_def ** head, uint32_t index);
 m68k_context * debugger(m68k_context * context, uint32_t address);
+z80_context * zdebugger(z80_context * context, uint16_t address);
 
 #endif //DEBUG_H_
--- a/sms.c	Tue Dec 27 18:58:31 2016 -0800
+++ b/sms.c	Wed Dec 28 12:28:52 2016 -0800
@@ -5,6 +5,7 @@
 #include "blastem.h"
 #include "render.h"
 #include "util.h"
+#include "debug.h"
 
 static void *memory_io_write(uint32_t location, void *vcontext, uint8_t value)
 {
@@ -119,6 +120,10 @@
 	uint32_t target_cycle = sms->z80->current_cycle + 3420*262;
 	while (!sms->should_return)
 	{
+		if (system->enter_debugger && sms->z80->pc) {
+			system->enter_debugger = 0;
+			zdebugger(sms->z80, sms->z80->pc);
+		}
 		z80_run(sms->z80, target_cycle);
 		target_cycle = sms->z80->current_cycle;
 		vdp_run_context(sms->vdp, target_cycle);
@@ -143,6 +148,11 @@
 	sms_context *sms = (sms_context *)system;
 	set_keybindings(&sms->io);
 	
+	if (system->enter_debugger) {
+		system->enter_debugger = 0;
+		zinsert_breakpoint(sms->z80, 0, (uint8_t *)zdebugger);
+	}
+	
 	z80_assert_reset(sms->z80, 0);
 	z80_clear_reset(sms->z80, 128*15);