feat: implement instruction-granular stepping

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-12-15 19:31:24 -04:00
parent 21565a9726
commit 460fcec370
3 changed files with 49 additions and 39 deletions

View File

@ -51,10 +51,8 @@ pub fn parse(self: *Self, allocator: Allocator, emu: Emulator) !String {
switch (self.contents[0]) { switch (self.contents[0]) {
// Required // Required
'?' => { '?' => {
const ret: Signal = .Trap; const ret = try std.fmt.allocPrint(allocator, "S{x:0>2}", .{@enumToInt(Signal.Int)});
return .{ .alloc = ret };
// Deallocated by the caller
return .{ .alloc = try std.fmt.allocPrint(allocator, "T{x:0>2}thread:1;", .{@enumToInt(ret)}) };
}, },
'g' => { 'g' => {
const r = emu.registers(); const r = emu.registers();
@ -66,12 +64,12 @@ pub fn parse(self: *Self, allocator: Allocator, emu: Emulator) !String {
{ {
var i: u32 = 0; var i: u32 = 0;
while (i < r.len + 1) : (i += 1) { while (i < r.len + 1) : (i += 1) {
const reg: u32 = @byteSwap(if (i < r.len) r[i] else cpsr); // Seems like GDB expects this var reg: u32 = if (i < r.len) r[i] else cpsr;
if (i == 15) reg -= if (cpsr >> 5 & 1 == 1) 4 else 8; // PC is ahead
// bufPrintIntToSlice writes to the provided slice, which is all we want from this // writes the formatted integer to the buffer, returns a slice to the buffer but we ignore that
// consequentially, we ignore the slice it returns since it just references the slice // GDB also expects the bytes to be in the opposite order for whatever reason
// passed as an argument _ = std.fmt.bufPrintIntToSlice(ret[i * 8 ..][0..8], @byteSwap(reg), 16, .lower, .{ .fill = '0', .width = 8 });
_ = std.fmt.bufPrintIntToSlice(ret[i * 8 ..][0..8], reg, 16, .lower, .{ .fill = '0', .width = 8 });
} }
} }
@ -79,9 +77,6 @@ pub fn parse(self: *Self, allocator: Allocator, emu: Emulator) !String {
}, },
'G' => @panic("TODO: Register Write"), 'G' => @panic("TODO: Register Write"),
'm' => { 'm' => {
// TODO: Actually reference GBA Memory
log.err("{s}", .{self.contents});
var tokens = std.mem.tokenize(u8, self.contents[1..], ","); var tokens = std.mem.tokenize(u8, self.contents[1..], ",");
const addr_str = tokens.next() orelse return .{ .static = "E9999" }; // EUNKNOWN const addr_str = tokens.next() orelse return .{ .static = "E9999" }; // EUNKNOWN
const length_str = tokens.next() orelse return .{ .static = "E9999" }; // EUNKNOWN const length_str = tokens.next() orelse return .{ .static = "E9999" }; // EUNKNOWN
@ -94,9 +89,8 @@ pub fn parse(self: *Self, allocator: Allocator, emu: Emulator) !String {
{ {
var i: u32 = 0; var i: u32 = 0;
while (i < len) : (i += 1) { while (i < len) : (i += 1) {
const value: u8 = emu.read(addr + i); // writes the formatted integer to the buffer, returns a slice to the buffer but we ignore that
_ = std.fmt.bufPrintIntToSlice(ret[i * 2 ..][0..2], emu.read(addr + i), 16, .lower, .{ .fill = '0', .width = 2 });
_ = std.fmt.bufPrintIntToSlice(ret[i * 2 ..][0..2], value, 16, .lower, .{ .fill = '0', .width = 2 });
} }
} }
@ -104,24 +98,34 @@ pub fn parse(self: *Self, allocator: Allocator, emu: Emulator) !String {
}, },
'M' => @panic("TODO: Memory Write"), 'M' => @panic("TODO: Memory Write"),
'c' => @panic("TODO: Continue"), 'c' => @panic("TODO: Continue"),
's' => @panic("TODO: Step"), 's' => {
var tokens = std.mem.tokenize(u8, self.contents[1..], " ");
const addr = if (tokens.next()) |s| try std.fmt.parseInt(u32, s, 16) else null;
log.debug("s addr: {?X:0>8}", .{addr});
emu.step();
const ret = try std.fmt.allocPrint(allocator, "S{x:0>2}", .{@enumToInt(Signal.Trap)});
return .{ .alloc = ret };
},
// Optional // Optional
// 'S' => {
// var tokens = std.mem.tokenize(u8, self.contents[1..], " ");
// const signal = if (tokens.next()) |s| try std.fmt.parseInt(u8, s, 16) else null;
// log.debug("S signal: {?}", .{signal});
// emu.step();
// const ret = try std.fmt.allocPrint(allocator, "S{x:0>2}", .{@enumToInt(Signal.Trap)});
// return .{ .alloc = ret };
// },
'D' => { 'D' => {
log.info("Disconneccting...", .{}); log.info("Disconneccting...", .{});
return .{ .static = "OK" }; return .{ .static = "OK" };
}, },
'H' => { 'H' => return .{ .static = "" },
log.warn("{s}", .{self.contents});
switch (self.contents[1]) {
'g', 'c' => return .{ .static = "OK" },
else => {
log.warn("Unimplemented: {s}", .{self.contents});
return .{ .static = "" };
},
}
},
'v' => { 'v' => {
if (substr(self.contents[1..], "MustReplyEmpty")) { if (substr(self.contents[1..], "MustReplyEmpty")) {
return .{ .static = "" }; return .{ .static = "" };
@ -232,7 +236,7 @@ fn verify(input: []const u8, chksum: u8) bool {
} }
const Signal = enum(u32) { const Signal = enum(u32) {
Hup, // Hangup Hup = 1, // Hangup
Int, // Interrupt Int, // Interrupt
Quit, // Quit Quit, // Quit
Ill, // Illegal Instruction Ill, // Illegal Instruction

View File

@ -125,6 +125,7 @@ fn parse(self: *Self, allocator: Allocator, input: []const u8) !Action {
break :blk .{ .send = response }; break :blk .{ .send = response };
}, },
'\x03' => .nothing,
else => std.debug.panic("Unknown: {s}", .{input}), else => std.debug.panic("Unknown: {s}", .{input}),
}; };
} }

View File

@ -10,10 +10,11 @@ pub const Emulator = struct {
readFn: *const fn (*anyopaque, u32) u8, readFn: *const fn (*anyopaque, u32) u8,
writeFn: *const fn (*anyopaque, u32, u8) void, writeFn: *const fn (*anyopaque, u32, u8) void,
// FIXME: Expensive copy registersFn: *const fn (*anyopaque) *[16]u32,
registersFn: *const fn (*anyopaque) [16]u32,
cpsrFn: *const fn (*anyopaque) u32, cpsrFn: *const fn (*anyopaque) u32,
stepFn: *const fn (*anyopaque) void,
pub fn init(ptr: anytype) Self { pub fn init(ptr: anytype) Self {
const Ptr = @TypeOf(ptr); const Ptr = @TypeOf(ptr);
const ptr_info = @typeInfo(Ptr); const ptr_info = @typeInfo(Ptr);
@ -36,7 +37,7 @@ pub const Emulator = struct {
return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.write, .{ self, addr, value }); return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.write, .{ self, addr, value });
} }
pub fn registersImpl(pointer: *anyopaque) [16]u32 { pub fn registersImpl(pointer: *anyopaque) *[16]u32 {
const self = @ptrCast(Ptr, @alignCast(alignment, pointer)); const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.registers, .{self}); return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.registers, .{self});
@ -47,15 +48,15 @@ pub const Emulator = struct {
return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.cpsr, .{self}); return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.cpsr, .{self});
} }
pub fn stepImpl(pointer: *anyopaque) void {
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.step, .{self});
}
}; };
return .{ return .{ .ptr = ptr, .readFn = gen.readImpl, .writeFn = gen.writeImpl, .registersFn = gen.registersImpl, .cpsrFn = gen.cpsrImpl, .stepFn = gen.stepImpl };
.ptr = ptr,
.readFn = gen.readImpl,
.writeFn = gen.writeImpl,
.registersFn = gen.registersImpl,
.cpsrFn = gen.cpsrImpl,
};
} }
pub inline fn read(self: Self, addr: u32) u8 { pub inline fn read(self: Self, addr: u32) u8 {
@ -66,11 +67,15 @@ pub const Emulator = struct {
self.writeFn(self.ptr, addr, value); self.writeFn(self.ptr, addr, value);
} }
pub inline fn registers(self: Self) [16]u32 { pub inline fn registers(self: Self) *[16]u32 {
return self.registersFn(self.ptr); return self.registersFn(self.ptr);
} }
pub inline fn cpsr(self: Self) u32 { pub inline fn cpsr(self: Self) u32 {
return self.cpsrFn(self.ptr); return self.cpsrFn(self.ptr);
} }
pub inline fn step(self: Self) void {
self.stepFn(self.ptr);
}
}; };