From 0d7a3c032a3aa269098b26b08f37736f526d04b2 Mon Sep 17 00:00:00 2001 From: Daniel Loffgren Date: Fri, 18 Sep 2015 07:16:14 +0000 Subject: [PATCH] Use new interactive debugger module from v6502 git-svn-id: svn+ssh://svn.phoenixbox.net/svn/apple1/trunk@33 64f78de7-aa59-e511-a0e8-0002a5492df0 --- apple1.xcodeproj/project.pbxproj | 4 + apple1/main.c | 144 ++++++++++++++++++++----------- apple1/pia.c | 22 +++-- apple1/pia.h | 3 + 4 files changed, 118 insertions(+), 55 deletions(-) diff --git a/apple1.xcodeproj/project.pbxproj b/apple1.xcodeproj/project.pbxproj index 2209476..1424147 100644 --- a/apple1.xcodeproj/project.pbxproj +++ b/apple1.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ E668391B1BA64C5B008F0F06 /* liblibdis6502.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E66838FC1BA4F705008F0F06 /* liblibdis6502.a */; }; E668391E1BA64C61008F0F06 /* liblibas6502.a in Frameworks */ = {isa = PBXBuildFile; fileRef = E66838F81BA4F705008F0F06 /* liblibas6502.a */; }; E66839201BA659A6008F0F06 /* libcurses.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = E668391F1BA659A6008F0F06 /* libcurses.dylib */; }; + E6ECE7B91BABED0100D1812C /* libedit.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = E6ECE7B81BABED0100D1812C /* libedit.dylib */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -136,6 +137,7 @@ E66839041BA53A82008F0F06 /* pia.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pia.c; sourceTree = ""; }; E66839051BA53A82008F0F06 /* pia.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pia.h; sourceTree = ""; }; E668391F1BA659A6008F0F06 /* libcurses.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libcurses.dylib; path = usr/lib/libcurses.dylib; sourceTree = SDKROOT; }; + E6ECE7B81BABED0100D1812C /* libedit.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libedit.dylib; path = usr/lib/libedit.dylib; sourceTree = SDKROOT; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -143,6 +145,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + E6ECE7B91BABED0100D1812C /* libedit.dylib in Frameworks */, E66839201BA659A6008F0F06 /* libcurses.dylib in Frameworks */, E668391E1BA64C61008F0F06 /* liblibas6502.a in Frameworks */, E668391B1BA64C5B008F0F06 /* liblibdis6502.a in Frameworks */, @@ -184,6 +187,7 @@ E66838D21BA4F6F8008F0F06 /* Frameworks */ = { isa = PBXGroup; children = ( + E6ECE7B81BABED0100D1812C /* libedit.dylib */, E668391F1BA659A6008F0F06 /* libcurses.dylib */, E66838D31BA4F705008F0F06 /* v6502.xcodeproj */, ); diff --git a/apple1/main.c b/apple1/main.c index 2d6dc88..a429ded 100644 --- a/apple1/main.c +++ b/apple1/main.c @@ -6,77 +6,123 @@ // Copyright (c) 2015 Daniel Loffgren. All rights reserved. // -#include -#include #include -#include "pia.h" #include +#include +#include + #include #include +#include +#include +#include +#include + +#include "pia.h" #define ROM_START 0xF000 #define ROM_SIZE 0x00FF #define RESET_VECTOR 0xFF00 -void fault(void *ctx, const char *e) { +static int faulted = 0; +static v6502_cpu *cpu; +static a1pia *pia; + +static void fault(void *ctx, const char *e) { (*(int *)ctx)++; } -static void loadRomFile(v6502_memory *mem, const char *fname, uint16_t address) { - FILE *f = fopen(fname, "r"); - - if (!f) { - fprintf(stderr, "Could not read from \"%s\"!\n", fname); - return; - } - - uint8_t byte; - uint16_t offset = 0; - - while (fread(&byte, 1, 1, f)) { - mem->bytes[address + (offset++)] = byte; - } - - fprintf(stderr, "loaded \"%s\" at 0x%04x\n", fname, address); - - fclose(f); -} +//static uint8_t romMirrorCallback(struct _v6502_memory *memory, uint16_t offset, int trap, void *context) { +// return memory->bytes[offset % ROM_SIZE]; +//} -uint8_t romMirrorCallback(struct _v6502_memory *memory, uint16_t offset, int trap, void *context) { - return memory->bytes[offset % ROM_SIZE]; -} - -int main(int argc, const char * argv[]) -{ - int faulted = 0; - - v6502_cpu *cpu = v6502_createCPU(); - cpu->memory = v6502_createMemory(v6502_memoryStartCeiling + 1); - cpu->fault_callback = fault; - cpu->fault_context = &faulted; - - // Load Woz Monitor - for (uint16_t start = ROM_START; - start < v6502_memoryStartCeiling && start >= ROM_START; - start += ROM_SIZE + 1) { - loadRomFile(cpu->memory, "apple1.rom", start); - //v6502_map(cpu->memory, start, ROM_SIZE, romMirrorCallback, NULL, NULL); - } - - // Attach PIA - a1pia *pia = pia_create(cpu->memory); - - v6502_reset(cpu); - +static void run(v6502_cpu *cpu) { FILE *asmfile = fopen("runtime.s", "w"); + pia_start(pia); while (!faulted) { dis6502_printAnnotatedInstruction(asmfile, cpu, cpu->pc); v6502_step(cpu); v6502_printCpuState(asmfile, cpu); } + pia_stop(pia); fclose(asmfile); +} + +static const char *prompt() { + static char prompt[10]; + snprintf(prompt, 10, "(0x%04x) ", cpu->pc); + return prompt; +} + +int main(int argc, const char * argv[]) +{ + cpu = v6502_createCPU(); + cpu->memory = v6502_createMemory(v6502_memoryStartCeiling + 1); + cpu->fault_callback = fault; + cpu->fault_context = &faulted; + v6502_breakpoint_list *breakpoint_list = v6502_createBreakpointList(); + + // Load Woz Monitor + for (uint16_t start = ROM_START; + start < v6502_memoryStartCeiling && start >= ROM_START; + start += ROM_SIZE + 1) { + v6502_loadFileAtAddress(cpu->memory, "apple1.rom", start); + //v6502_map(cpu->memory, start, ROM_SIZE, romMirrorCallback, NULL, NULL); + } + + // Attach PIA + pia = pia_create(cpu->memory); + + v6502_reset(cpu); + + int verbose = 0; + int commandLen; + HistEvent ev; + History *hist = history_init(); + history(hist, &ev, H_SETSIZE, 100); + + EditLine *el = el_init("apple1", stdin, stdout, stderr); + el_set(el, EL_PROMPT, &prompt); + el_set(el, EL_SIGNAL, SIGWINCH); + el_set(el, EL_EDITOR, "emacs"); + el_set(el, EL_HIST, history, hist); + + char *command = NULL; + while (!feof(stdin)) { + currentLineNum++; + + const char *in = el_gets(el, &commandLen); + if (!in) { + break; + } + + history(hist, &ev, H_ENTER, in); + command = realloc(command, commandLen + 1); + memcpy(command, in, commandLen); + + // Trim newline, always the last char + command[commandLen - 1] = '\0'; + + if (command[0] == '\0') { + continue; + } + + if (v6502_handleDebuggerCommand(cpu, command, commandLen, breakpoint_list, run, &verbose)) { + continue; + } + else if (command[0] != ';') { + as6502_executeAsmLineOnCPU(cpu, command, strlen(command)); + } + } + + v6502_destroyBreakpointList(breakpoint_list); + history_end(hist); + el_end(el); + free(command); pia_destroy(pia); v6502_destroyMemory(cpu->memory); v6502_destroyCPU(cpu); + printf("\n"); + return EXIT_SUCCESS; } diff --git a/apple1/pia.c b/apple1/pia.c index e1d6bf9..4dfd840 100644 --- a/apple1/pia.c +++ b/apple1/pia.c @@ -121,18 +121,14 @@ static void _doCoolVideoStart(a1pia *pia) { a1pia *pia_create(v6502_memory *mem) { a1pia *pia = malloc(sizeof(a1pia)); pia->memory = mem; - pia->screen = initscr(); - //nodelay(stdscr, true); - crmode(); - noecho(); - nonl(); + pia->screen = NULL; v6502_map(mem, A1PIA_KEYBOARD_INPUT, 1, (v6502_readFunction *)keyboardReadCharacterCallback, NULL, pia); v6502_map(mem, A1PIA_KEYBOARD_CRLF_REG, 1, (v6502_readFunction *)keyboardReadReadyCallback, NULL, pia); v6502_map(mem, A1PIA_VIDEO_OUTPUT, 1, FIXME_I_SHOULDNT_BE_NULL, (v6502_writeFunction *)videoWriteCharCallback, pia); v6502_map(mem, A1PIA_VIDEO_CRLF_REG, 1, FIXME_I_SHOULDNT_BE_NULL, (v6502_writeFunction *)videoWriteNewlineCallback, pia); - _doCoolVideoStart(pia); +// _doCoolVideoStart(pia); return pia; } @@ -140,3 +136,17 @@ void pia_destroy(a1pia *pia) { endwin(); free(pia); } + +void pia_start(a1pia *pia) { + if (!pia->screen) { + pia->screen = initscr(); + } + //nodelay(stdscr, true); + crmode(); + noecho(); + nonl(); +} + +void pia_stop(a1pia *pia) { + +} diff --git a/apple1/pia.h b/apple1/pia.h index d382a8b..149f037 100644 --- a/apple1/pia.h +++ b/apple1/pia.h @@ -28,4 +28,7 @@ typedef struct { a1pia *pia_create(v6502_memory *mem); void pia_destroy(a1pia *pia); +void pia_start(a1pia *pia); +void pia_stop(a1pia *pia); + #endif /* defined(__apple1__pia__) */