all repos — mgba @ d7ecdb5e4eda3e195d6d319f94280486d01d0bc3

mGBA Game Boy Advance Emulator

src/sm83/debugger/cli-debugger.c (view raw)

  1/* Copyright (c) 2013-2016 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include <mgba/internal/sm83/debugger/cli-debugger.h>
  7
  8#include <mgba/core/core.h>
  9#include <mgba/internal/debugger/cli-debugger.h>
 10#include <mgba/internal/sm83/decoder.h>
 11#include <mgba/internal/sm83/debugger/debugger.h>
 12#include <mgba/internal/sm83/sm83.h>
 13
 14static void _printStatus(struct CLIDebuggerSystem*);
 15
 16static void _disassemble(struct CLIDebuggerSystem* debugger, struct CLIDebugVector* dv);
 17static uint16_t _printLine(struct CLIDebugger* debugger, uint16_t address, int segment);
 18
 19static struct CLIDebuggerCommandSummary _sm83Commands[] = {
 20	{ 0, 0, 0, 0 }
 21};
 22
 23static inline void _printFlags(struct CLIDebuggerBackend* be, union FlagRegister f) {
 24	be->printf(be, "F: [%c%c%c%c]\n",
 25	           f.z ? 'Z' : '-',
 26	           f.n ? 'N' : '-',
 27	           f.h ? 'H' : '-',
 28	           f.c ? 'C' : '-');
 29}
 30
 31static void _disassemble(struct CLIDebuggerSystem* debugger, struct CLIDebugVector* dv) {
 32	struct SM83Core* cpu = debugger->p->d.core->cpu;
 33
 34	uint16_t address;
 35	int segment = -1;
 36	size_t size;
 37	if (!dv || dv->type != CLIDV_INT_TYPE) {
 38		address = cpu->pc;
 39	} else {
 40		address = dv->intValue;
 41		segment = dv->segmentValue;
 42		dv = dv->next;
 43	}
 44
 45	if (!dv || dv->type != CLIDV_INT_TYPE) {
 46		size = 1;
 47	} else {
 48		size = dv->intValue;
 49		// TODO: Check for excess args
 50	}
 51
 52	size_t i;
 53	for (i = 0; i < size; ++i) {
 54		address = _printLine(debugger->p, address, segment);
 55	}
 56}
 57
 58static inline uint16_t _printLine(struct CLIDebugger* debugger, uint16_t address, int segment) {
 59	struct CLIDebuggerBackend* be = debugger->backend;
 60	struct SM83InstructionInfo info = {{0}, 0};
 61	char disassembly[48];
 62	char* disPtr = disassembly;
 63	if (segment >= 0) {
 64		be->printf(be, "%02X:", segment);
 65	}
 66	be->printf(be, "%04X:  ", address);
 67	uint8_t instruction;
 68	size_t bytesRemaining = 1;
 69	for (bytesRemaining = 1; bytesRemaining; --bytesRemaining) {
 70		instruction = debugger->d.core->rawRead8(debugger->d.core, address, segment);
 71		disPtr += snprintf(disPtr, sizeof(disassembly) - (disPtr - disassembly), "%02X", instruction);
 72		++address;
 73		bytesRemaining += SM83Decode(instruction, &info);
 74	};
 75	disPtr[0] = '\t';
 76	++disPtr;
 77	SM83Disassemble(&info, address, disPtr, sizeof(disassembly) - (disPtr - disassembly));
 78	be->printf(be, "%s\n", disassembly);
 79	return address;
 80}
 81
 82static void _printStatus(struct CLIDebuggerSystem* debugger) {
 83	struct CLIDebuggerBackend* be = debugger->p->backend;
 84	struct SM83Core* cpu = debugger->p->d.core->cpu;
 85	be->printf(be, "A: %02X  F: %02X  (AF: %04X)\n", cpu->a, cpu->f.packed, cpu->af);
 86	be->printf(be, "B: %02X  C: %02X  (BC: %04X)\n", cpu->b, cpu->c, cpu->bc);
 87	be->printf(be, "D: %02X  E: %02X  (DE: %04X)\n", cpu->d, cpu->e, cpu->de);
 88	be->printf(be, "H: %02X  L: %02X  (HL: %04X)\n", cpu->h, cpu->l, cpu->hl);
 89	be->printf(be, "PC: %04X  SP: %04X\n", cpu->pc, cpu->sp);
 90	_printFlags(be, cpu->f);
 91
 92	struct SM83Debugger* platDebugger = (struct SM83Debugger*) debugger->p->d.platform;
 93	size_t i;
 94	for (i = 0; platDebugger->segments[i].name; ++i) {
 95		be->printf(be, "%s%s: %02X", i ? "  " : "", platDebugger->segments[i].name, cpu->memory.currentSegment(cpu, platDebugger->segments[i].start));
 96	}
 97	if (i) {
 98		be->printf(be, "\n");
 99	}
100	if (platDebugger->printStatus) {
101		platDebugger->printStatus(debugger);
102	}
103	_printLine(debugger->p, cpu->pc, cpu->memory.currentSegment(cpu, cpu->pc));
104}
105
106void SM83CLIDebuggerCreate(struct CLIDebuggerSystem* debugger) {
107	debugger->printStatus = _printStatus;
108	debugger->disassemble = _disassemble;
109	debugger->platformName = "SM83";
110	debugger->platformCommands = _sm83Commands;
111	debugger->platformCommandAliases = NULL;
112}