all repos — mgba @ 60577e83948647d36a2e6a8b4ec8f8556df3f72f

mGBA Game Boy Advance Emulator

src/lr35902/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/lr35902/debugger/cli-debugger.h>
  7
  8#include <mgba/core/core.h>
  9#include <mgba/internal/debugger/cli-debugger.h>
 10#include <mgba/internal/lr35902/decoder.h>
 11#include <mgba/internal/lr35902/debugger/debugger.h>
 12#include <mgba/internal/lr35902/lr35902.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 _lr35902Commands[] = {
 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 LR35902Core* 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		dv = dv->next; // 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 LR35902InstructionInfo info = {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 += LR35902Decode(instruction, &info);
 74	};
 75	disPtr[0] = '\t';
 76	++disPtr;
 77	LR35902Disassemble(&info, 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 LR35902Core* 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
 91	struct LR35902Debugger* platDebugger = (struct LR35902Debugger*) debugger->p->d.platform;
 92	size_t i;
 93	for (i = 0; platDebugger->segments[i].name; ++i) {
 94		be->printf(be, "%s%s: %02X", i ? "  " : "", platDebugger->segments[i].name, cpu->memory.currentSegment(cpu, platDebugger->segments[i].start));
 95	}
 96	if (i) {
 97		be->printf(be, "\n");
 98	}
 99	_printFlags(be, cpu->f);
100	_printLine(debugger->p, cpu->pc, cpu->memory.currentSegment(cpu, cpu->pc));
101}
102
103void LR35902CLIDebuggerCreate(struct CLIDebuggerSystem* debugger) {
104	debugger->printStatus = _printStatus;
105	debugger->disassemble = _disassemble;
106	debugger->platformName = "GB-Z80";
107	debugger->platformCommands = _lr35902Commands;
108}