all repos — mgba @ 076855deb184cee21c75b691acc7a1d35fdc744a

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/lr35902.h>
 12
 13static void _printStatus(struct CLIDebuggerSystem*);
 14
 15static void _disassemble(struct CLIDebuggerSystem* debugger, struct CLIDebugVector* dv);
 16static uint16_t _printLine(struct CLIDebugger* debugger, uint16_t address, int segment);
 17
 18static struct CLIDebuggerCommandSummary _lr35902Commands[] = {
 19	{ 0, 0, 0, 0 }
 20};
 21
 22static inline void _printFlags(struct CLIDebuggerBackend* be, union FlagRegister f) {
 23	be->printf(be, "[%c%c%c%c]\n",
 24	           f.z ? 'Z' : '-',
 25	           f.n ? 'N' : '-',
 26	           f.h ? 'H' : '-',
 27	           f.c ? 'C' : '-');
 28}
 29
 30static void _disassemble(struct CLIDebuggerSystem* debugger, struct CLIDebugVector* dv) {
 31	struct LR35902Core* cpu = debugger->p->d.core->cpu;
 32
 33	uint16_t address;
 34	size_t size;
 35	if (!dv || dv->type != CLIDV_INT_TYPE) {
 36		address = cpu->pc;
 37	} else {
 38		address = dv->intValue;
 39		dv = dv->next;
 40	}
 41
 42	if (!dv || dv->type != CLIDV_INT_TYPE) {
 43		size = 1;
 44	} else {
 45		size = dv->intValue;
 46		dv = dv->next; // TODO: Check for excess args
 47	}
 48
 49	size_t i;
 50	for (i = 0; i < size; ++i) {
 51		address = _printLine(debugger->p, address, -1);
 52	}
 53}
 54
 55static inline uint16_t _printLine(struct CLIDebugger* debugger, uint16_t address, int segment) {
 56	struct CLIDebuggerBackend* be = debugger->backend;
 57	struct LR35902InstructionInfo info = {};
 58	char disassembly[48];
 59	char* disPtr = disassembly;
 60	if (segment >= 0) {
 61		be->printf(be, "%02X:  ", segment);
 62	}
 63	be->printf(be, "%04X:  ", address);
 64	uint8_t instruction;
 65	size_t bytesRemaining = 1;
 66	for (bytesRemaining = 1; bytesRemaining; --bytesRemaining) {
 67		instruction = debugger->d.core->rawRead8(debugger->d.core, address, segment);
 68		disPtr += snprintf(disPtr, sizeof(disassembly) - (disPtr - disassembly), "%02X", instruction);
 69		++address;
 70		bytesRemaining += LR35902Decode(instruction, &info);
 71	};
 72	disPtr[0] = '\t';
 73	++disPtr;
 74	LR35902Disassemble(&info, disPtr, sizeof(disassembly) - (disPtr - disassembly));
 75	be->printf(be, "%s\n", disassembly);
 76	return address;
 77}
 78
 79static void _printStatus(struct CLIDebuggerSystem* debugger) {
 80	struct CLIDebuggerBackend* be = debugger->p->backend;
 81	struct LR35902Core* cpu = debugger->p->d.core->cpu;
 82	be->printf(be, "A: %02X F: %02X (AF: %04X)\n", cpu->a, cpu->f.packed, cpu->af);
 83	be->printf(be, "B: %02X C: %02X (BC: %04X)\n", cpu->b, cpu->c, cpu->bc);
 84	be->printf(be, "D: %02X E: %02X (DE: %04X)\n", cpu->d, cpu->e, cpu->de);
 85	be->printf(be, "H: %02X L: %02X (HL: %04X)\n", cpu->h, cpu->l, cpu->hl);
 86	be->printf(be, "PC: %04X SP: %04X\n", cpu->pc, cpu->sp);
 87	_printFlags(be, cpu->f);
 88	_printLine(debugger->p, cpu->pc, -1);
 89}
 90
 91static uint32_t _lookupPlatformIdentifier(struct CLIDebuggerSystem* debugger, const char* name, struct CLIDebugVector* dv) {
 92	struct LR35902Core* cpu = debugger->p->d.core->cpu;
 93	if (strcmp(name, "a") == 0) {
 94		return cpu->a;
 95	}
 96	if (strcmp(name, "b") == 0) {
 97		return cpu->b;
 98	}
 99	if (strcmp(name, "c") == 0) {
100		return cpu->c;
101	}
102	if (strcmp(name, "d") == 0) {
103		return cpu->d;
104	}
105	if (strcmp(name, "e") == 0) {
106		return cpu->e;
107	}
108	if (strcmp(name, "h") == 0) {
109		return cpu->h;
110	}
111	if (strcmp(name, "l") == 0) {
112		return cpu->l;
113	}
114	if (strcmp(name, "bc") == 0) {
115		return cpu->bc;
116	}
117	if (strcmp(name, "de") == 0) {
118		return cpu->de;
119	}
120	if (strcmp(name, "hl") == 0) {
121		return cpu->hl;
122	}
123	if (strcmp(name, "af") == 0) {
124		return cpu->af;
125	}
126	if (strcmp(name, "pc") == 0) {
127		return cpu->pc;
128	}
129	if (strcmp(name, "sp") == 0) {
130		return cpu->sp;
131	}
132	if (strcmp(name, "f") == 0) {
133		return cpu->f.packed;
134	}
135	dv->type = CLIDV_ERROR_TYPE;
136	return 0;
137}
138
139void LR35902CLIDebuggerCreate(struct CLIDebuggerSystem* debugger) {
140	debugger->printStatus = _printStatus;
141	debugger->disassemble = _disassemble;
142	debugger->lookupPlatformIdentifier = _lookupPlatformIdentifier;
143	debugger->platformName = "GB-Z80";
144	debugger->platformCommands = _lr35902Commands;
145}