summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Pavone <pavone@retrodev.com>2016-12-28 12:28:52 -0800
committerMichael Pavone <pavone@retrodev.com>2016-12-28 12:28:52 -0800
commit96d5581219aec6c49fadaaf1d30623c0f2eed358 (patch)
tree560c0cfaa52f1183cd4b2b6adec9247abc921eb0
parente2e00fe60bcc9ff0e8f76cf3f9e4ef17113b4519 (diff)
Enabled Z80 debugger in PBC mode
-rw-r--r--debug.c54
-rw-r--r--debug.h2
-rw-r--r--sms.c10
3 files changed, 47 insertions, 19 deletions
diff --git a/debug.c b/debug.c
index 4e7b504..e0b8d56 100644
--- a/debug.c
+++ b/debug.c
@@ -343,17 +343,9 @@ z80_context * zdebugger(z80_context * context, uint16_t address)
} 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 @@ z80_context * zdebugger(z80_context * context, uint16_t address)
} 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 @@ z80_context * zdebugger(z80_context * context, uint16_t address)
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;
+ }
+ 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);
}
- fclose(f);
} else {
- fprintf(stderr, "Could not open %s for writing\n", param);
+ fputs("Failed to find a RAM memory chunk\n", stderr);
}
break;
}
diff --git a/debug.h b/debug.h
index f0f7644..8120c88 100644
--- a/debug.h
+++ b/debug.h
@@ -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 @@ bp_def ** find_breakpoint_idx(bp_def ** cur, uint32_t index);
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_
diff --git a/sms.c b/sms.c
index 5e3b9a9..79fb78b 100644
--- a/sms.c
+++ b/sms.c
@@ -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 @@ static void run_sms(system_header *system)
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 @@ static void start_sms(system_header *system, char *statefile)
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);