Compare commits
12 Commits
08bf0f9201
...
4bca44e5f2
Author | SHA1 | Date | |
---|---|---|---|
4bca44e5f2 | |||
1bd9964f58 | |||
460fcec370 | |||
21565a9726 | |||
2bc5bdc310 | |||
51082186d7 | |||
8a58251ef6 | |||
400e155502 | |||
26aad8d1ae | |||
f1e1efc5e5 | |||
a85874d364 | |||
8195012573 |
23
build.zig
23
build.zig
@@ -27,6 +27,7 @@ pub fn link(exe: *std.build.LibExeObjStep) void {
|
||||
|
||||
pub fn build(b: *std.build.Builder) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
_ = target;
|
||||
const mode = b.standardReleaseOptions();
|
||||
|
||||
// -- library --
|
||||
@@ -42,18 +43,18 @@ pub fn build(b: *std.build.Builder) void {
|
||||
const test_step = b.step("lib-test", "Run Library Tests");
|
||||
test_step.dependOn(&lib_tests.step);
|
||||
|
||||
// -- Executable --
|
||||
const exe = b.addExecutable("gdbserver", "src/main.zig");
|
||||
link(exe);
|
||||
// // -- Executable --
|
||||
// const exe = b.addExecutable("gdbserver", "src/main.zig");
|
||||
// link(exe);
|
||||
|
||||
exe.setTarget(target);
|
||||
exe.setBuildMode(mode);
|
||||
exe.install();
|
||||
// exe.setTarget(target);
|
||||
// exe.setBuildMode(mode);
|
||||
// exe.install();
|
||||
|
||||
const run_cmd = exe.run();
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
if (b.args) |args| run_cmd.addArgs(args);
|
||||
// const run_cmd = exe.run();
|
||||
// run_cmd.step.dependOn(b.getInstallStep());
|
||||
// if (b.args) |args| run_cmd.addArgs(args);
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
// const run_step = b.step("run", "Run the app");
|
||||
// run_step.dependOn(&run_cmd.step);
|
||||
}
|
||||
|
Submodule lib/zig-network updated: caa31ef878...a8c4502538
107
src/Packet.zig
107
src/Packet.zig
@@ -1,7 +1,10 @@
|
||||
const std = @import("std");
|
||||
|
||||
const target = @import("Server.zig").target;
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Emulator = @import("lib.zig").Emulator;
|
||||
|
||||
const target = @import("Server.zig").target;
|
||||
const memory_map = @import("Server.zig").memory_map;
|
||||
|
||||
const Self = @This();
|
||||
const log = std.log.scoped(.Packet);
|
||||
@@ -16,8 +19,6 @@ pub fn from(allocator: Allocator, str: []const u8) !Self {
|
||||
const chksum_str = tokens.next() orelse return error.MissingCheckSum;
|
||||
const chksum = std.fmt.parseInt(u8, chksum_str, 16) catch return error.InvalidChecksum;
|
||||
|
||||
// log.info("Contents: {s}", .{contents});
|
||||
|
||||
if (!Self.verify(contents, chksum)) return error.ChecksumMismatch;
|
||||
|
||||
return .{ .contents = try allocator.dupe(u8, contents) };
|
||||
@@ -44,34 +45,29 @@ const String = union(enum) {
|
||||
}
|
||||
};
|
||||
|
||||
pub fn parse(self: *Self, allocator: Allocator) !String {
|
||||
pub fn parse(self: *Self, allocator: Allocator, emu: Emulator) !String {
|
||||
switch (self.contents[0]) {
|
||||
// Required
|
||||
'?' => {
|
||||
const ret: Signal = .Trap;
|
||||
|
||||
// Deallocated by the caller
|
||||
return .{ .alloc = try std.fmt.allocPrint(allocator, "T{x:0>2}thread:1;", .{@enumToInt(ret)}) };
|
||||
const ret = try std.fmt.allocPrint(allocator, "S{x:0>2}", .{@enumToInt(Signal.Int)});
|
||||
return .{ .alloc = ret };
|
||||
},
|
||||
'g' => {
|
||||
// TODO: Actually reference GBA Registers
|
||||
const r = [_]u32{0xDEAD_BEEF} ** 0x10;
|
||||
const cpsr: u32 = 0xCAFE_B0BA;
|
||||
|
||||
const char_len = 2;
|
||||
const reg_len = @sizeOf(u32) * char_len; // Every byte is represented by 2 characters
|
||||
const r = emu.registers();
|
||||
const cpsr = emu.cpsr();
|
||||
|
||||
const reg_len = @sizeOf(u32) * 2; // Every byte is represented by 2 characters
|
||||
const ret = try allocator.alloc(u8, r.len * reg_len + reg_len); // r0 -> r15 + CPSR
|
||||
|
||||
{
|
||||
var i: usize = 0;
|
||||
var i: u32 = 0;
|
||||
while (i < r.len + 1) : (i += 1) {
|
||||
const reg: u32 = if (i < r.len) r[i] else cpsr;
|
||||
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
|
||||
// consequentially, we ignore the slice it returns since it just references the slice
|
||||
// passed as an argument
|
||||
_ = std.fmt.bufPrintIntToSlice(ret[i * 8 ..][0..8], reg, 16, .lower, .{ .fill = '0', .width = 8 });
|
||||
// writes the formatted integer to the buffer, returns a slice to the buffer but we ignore that
|
||||
// GDB also expects the bytes to be in the opposite order for whatever reason
|
||||
_ = std.fmt.bufPrintIntToSlice(ret[i * 8 ..][0..8], @byteSwap(reg), 16, .lower, .{ .fill = '0', .width = 8 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,25 +75,23 @@ pub fn parse(self: *Self, allocator: Allocator) !String {
|
||||
},
|
||||
'G' => @panic("TODO: Register Write"),
|
||||
'm' => {
|
||||
// TODO: Actually reference GBA Memory
|
||||
log.err("{s}", .{self.contents});
|
||||
|
||||
var tokens = std.mem.tokenize(u8, self.contents[1..], ",");
|
||||
const addr_str = tokens.next() orelse return .{ .static = "E9999" }; // EUNKNOWN
|
||||
const length_str = tokens.next() orelse return .{ .static = "E9999" }; // EUNKNOWN
|
||||
|
||||
const addr = try std.fmt.parseInt(u32, addr_str, 16);
|
||||
const len = try std.fmt.parseInt(u32, length_str, 16);
|
||||
_ = addr;
|
||||
|
||||
const ret = try allocator.alloc(u8, len * 2);
|
||||
|
||||
{
|
||||
var i: usize = 0;
|
||||
var i: u32 = 0;
|
||||
while (i < len) : (i += 1) {
|
||||
const value: u8 = 0;
|
||||
const value = emu.read(addr + i);
|
||||
log.debug("read 0x{X:0>2} from 0x{X:0>8}", .{ value, addr + i });
|
||||
|
||||
_ = std.fmt.bufPrintIntToSlice(ret[i * 2 ..][0..2], value, 16, .lower, .{ .fill = '0', .width = 2 });
|
||||
// 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 });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,20 +99,22 @@ pub fn parse(self: *Self, allocator: Allocator) !String {
|
||||
},
|
||||
'M' => @panic("TODO: Memory Write"),
|
||||
'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;
|
||||
|
||||
emu.step();
|
||||
|
||||
const ret = try std.fmt.allocPrint(allocator, "S{x:0>2}", .{@enumToInt(Signal.Trap)});
|
||||
return .{ .alloc = ret };
|
||||
},
|
||||
|
||||
// Optional
|
||||
'H' => {
|
||||
log.warn("{s}", .{self.contents});
|
||||
|
||||
switch (self.contents[1]) {
|
||||
'g', 'c' => return .{ .static = "OK" },
|
||||
else => {
|
||||
log.warn("Unimplemented: {s}", .{self.contents});
|
||||
return .{ .static = "" };
|
||||
},
|
||||
}
|
||||
'D' => {
|
||||
log.info("Disconnecting...", .{});
|
||||
return .{ .static = "OK" };
|
||||
},
|
||||
'H' => return .{ .static = "" },
|
||||
'v' => {
|
||||
if (substr(self.contents[1..], "MustReplyEmpty")) {
|
||||
return .{ .static = "" };
|
||||
@@ -143,16 +139,14 @@ pub fn parse(self: *Self, allocator: Allocator) !String {
|
||||
|
||||
if (substr(self.contents[1..], "Xfer:features:read")) {
|
||||
var tokens = std.mem.tokenize(u8, self.contents[1..], ":,");
|
||||
_ = tokens.next(); // qXfer
|
||||
_ = tokens.next(); // Xfer
|
||||
_ = tokens.next(); // features
|
||||
_ = tokens.next(); // read
|
||||
const annex = tokens.next() orelse return .{ .static = "E00" };
|
||||
const offset_str = tokens.next() orelse return .{ .static = "E00" };
|
||||
const length_str = tokens.next() orelse return .{ .static = "E00" };
|
||||
const annex = tokens.next() orelse return .{ .static = "E9999" };
|
||||
const offset_str = tokens.next() orelse return .{ .static = "E99999" };
|
||||
const length_str = tokens.next() orelse return .{ .static = "E9999" };
|
||||
|
||||
if (std.mem.eql(u8, annex, "target.xml")) {
|
||||
log.info("Providing ARMv4T target description", .{});
|
||||
|
||||
const offset = try std.fmt.parseInt(usize, offset_str, 16);
|
||||
const length = try std.fmt.parseInt(usize, length_str, 16);
|
||||
|
||||
@@ -168,12 +162,33 @@ pub fn parse(self: *Self, allocator: Allocator) !String {
|
||||
return .{ .alloc = ret };
|
||||
} else {
|
||||
log.err("Unexpected Annex: {s}", .{annex});
|
||||
return .{ .static = "E00" };
|
||||
return .{ .static = "E9999" };
|
||||
}
|
||||
|
||||
return .{ .static = "" };
|
||||
}
|
||||
|
||||
if (substr(self.contents[1..], "Xfer:memory-map:read")) {
|
||||
var tokens = std.mem.tokenize(u8, self.contents[1..], ":,");
|
||||
_ = tokens.next(); // Xfer
|
||||
_ = tokens.next(); // memory-map
|
||||
_ = tokens.next(); // read
|
||||
const offset_str = tokens.next() orelse return .{ .static = "E9999" };
|
||||
const length_str = tokens.next() orelse return .{ .static = "E9999" };
|
||||
|
||||
const offset = try std.fmt.parseInt(usize, offset_str, 16);
|
||||
const length = try std.fmt.parseInt(usize, length_str, 16);
|
||||
|
||||
// see above
|
||||
const len = @min(length, (memory_map.len + 1) - offset);
|
||||
const ret = try allocator.alloc(u8, len);
|
||||
|
||||
ret[0] = if (ret.len < length) 'l' else 'm';
|
||||
std.mem.copy(u8, ret[1..], memory_map[offset..]);
|
||||
|
||||
return .{ .alloc = ret };
|
||||
}
|
||||
|
||||
log.warn("Unimplemented: {s}", .{self.contents});
|
||||
return .{ .static = "" };
|
||||
},
|
||||
@@ -206,7 +221,7 @@ fn verify(input: []const u8, chksum: u8) bool {
|
||||
}
|
||||
|
||||
const Signal = enum(u32) {
|
||||
Hup, // Hangup
|
||||
Hup = 1, // Hangup
|
||||
Int, // Interrupt
|
||||
Quit, // Quit
|
||||
Ill, // Illegal Instruction
|
||||
|
@@ -4,6 +4,7 @@ const Packet = @import("Packet.zig");
|
||||
|
||||
const Socket = network.Socket;
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Emulator = @import("lib.zig").Emulator;
|
||||
|
||||
const Self = @This();
|
||||
const log = std.log.scoped(.Server);
|
||||
@@ -35,13 +36,32 @@ pub const target: []const u8 =
|
||||
\\</target>
|
||||
;
|
||||
|
||||
// Game Pak SRAM isn't included
|
||||
// TODO: Can i be more specific here?
|
||||
pub const memory_map: []const u8 =
|
||||
\\ <memory-map version="1.0">
|
||||
\\ <memory type="rom" start="0x00000000" length="0x00004000"/>
|
||||
\\ <memory type="ram" start="0x02000000" length="0x00040000"/>
|
||||
\\ <memory type="ram" start="0x03000000" length="0x00008000"/>
|
||||
\\ <memory type="ram" start="0x04000000" length="0x00000400"/>
|
||||
\\ <memory type="ram" start="0x05000000" length="0x00000400"/>
|
||||
\\ <memory type="ram" start="0x06000000" length="0x00018000"/>
|
||||
\\ <memory type="ram" start="0x07000000" length="0x00000400"/>
|
||||
\\ <memory type="rom" start="0x08000000" length="0x02000000"/>
|
||||
\\ <memory type="rom" start="0x0A000000" length="0x02000000"/>
|
||||
\\ <memory type="rom" start="0x0C000000" length="0x02000000"/>
|
||||
\\ </memory-map>
|
||||
;
|
||||
|
||||
// FIXME: Shouldn't this be a Packet Struct?
|
||||
pkt_cache: ?[]const u8 = null,
|
||||
|
||||
client: Socket,
|
||||
_socket: Socket,
|
||||
|
||||
pub fn init() !Self {
|
||||
emu: Emulator,
|
||||
|
||||
pub fn init(emulator: Emulator) !Self {
|
||||
try network.init();
|
||||
|
||||
var socket = try Socket.create(.ipv4, .tcp);
|
||||
@@ -53,7 +73,7 @@ pub fn init() !Self {
|
||||
const endpoint = try client.getLocalEndPoint();
|
||||
log.info("client connected from {}", .{endpoint});
|
||||
|
||||
return .{ ._socket = socket, .client = client };
|
||||
return .{ .emu = emulator, ._socket = socket, .client = client };
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Self, allocator: Allocator) void {
|
||||
@@ -81,39 +101,50 @@ pub fn run(self: *Self, allocator: Allocator) !void {
|
||||
const len = try self.client.receive(&buf);
|
||||
if (len == 0) break;
|
||||
|
||||
const action = try Self.parse(allocator, buf[0..len]);
|
||||
const action = try self.parse(allocator, buf[0..len]);
|
||||
try self.send(allocator, action);
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(allocator: Allocator, input: []const u8) !Action {
|
||||
fn parse(self: *Self, allocator: Allocator, input: []const u8) !Action {
|
||||
// log.debug("-> {s}", .{input});
|
||||
|
||||
return switch (input[0]) {
|
||||
'+' => .nothing,
|
||||
'-' => .retry,
|
||||
'$' => blk: {
|
||||
// Packet
|
||||
var packet = Packet.from(allocator, input) catch return .nack;
|
||||
defer packet.deinit(allocator);
|
||||
'+' => blk: {
|
||||
if (input.len == 1) break :blk .nothing;
|
||||
|
||||
var string = packet.parse(allocator) catch return .nack;
|
||||
defer string.deinit(allocator);
|
||||
|
||||
const reply = string.inner();
|
||||
|
||||
// deallocated by the caller
|
||||
const response = try std.fmt.allocPrint(allocator, "${s}#{x:0>2}", .{ reply, Packet.checksum(reply) });
|
||||
|
||||
break :blk .{ .send = response };
|
||||
break :blk switch (input[1]) {
|
||||
'$' => self.handlePacket(allocator, input[1..]),
|
||||
else => std.debug.panic("Unknown: {s}", .{input}),
|
||||
};
|
||||
},
|
||||
'-' => .retry,
|
||||
'$' => try self.handlePacket(allocator, input),
|
||||
'\x03' => .nothing,
|
||||
else => std.debug.panic("Unknown: {s}", .{input}),
|
||||
};
|
||||
}
|
||||
|
||||
fn handlePacket(self: *Self, allocator: Allocator, input: []const u8) !Action {
|
||||
var packet = Packet.from(allocator, input) catch return .nack;
|
||||
defer packet.deinit(allocator);
|
||||
|
||||
var string = packet.parse(allocator, self.emu) catch return .nack;
|
||||
defer string.deinit(allocator);
|
||||
|
||||
const reply = string.inner();
|
||||
|
||||
// deallocated by the caller
|
||||
const response = try std.fmt.allocPrint(allocator, "+${s}#{x:0>2}", .{ reply, Packet.checksum(reply) });
|
||||
|
||||
return .{ .send = response };
|
||||
}
|
||||
|
||||
fn send(self: *Self, allocator: Allocator, action: Action) !void {
|
||||
switch (action) {
|
||||
.send => |pkt| {
|
||||
_ = try self.client.send("+"); // ACK
|
||||
_ = try self.client.send(pkt);
|
||||
// log.debug("<- {s}", .{pkt});
|
||||
|
||||
self.reset(allocator);
|
||||
self.pkt_cache = pkt;
|
||||
@@ -121,14 +152,21 @@ fn send(self: *Self, allocator: Allocator, action: Action) !void {
|
||||
.retry => {
|
||||
log.warn("received nack, resending: \"{?s}\"", .{self.pkt_cache});
|
||||
|
||||
if (self.pkt_cache) |pkt| _ = try self.client.send(pkt); // FIXME: is an ack to a nack necessary?
|
||||
if (self.pkt_cache) |pkt| {
|
||||
_ = try self.client.send(pkt);
|
||||
// log.debug("<- {s}", .{pkt});
|
||||
}
|
||||
},
|
||||
.ack => {
|
||||
_ = try self.client.send("+");
|
||||
// log.debug("<- +", .{});
|
||||
|
||||
self.reset(allocator);
|
||||
},
|
||||
.nack => {
|
||||
_ = try self.client.send("-");
|
||||
// log.debug("<- -", .{});
|
||||
|
||||
self.reset(allocator);
|
||||
},
|
||||
.nothing => self.reset(allocator),
|
||||
|
39
src/lib.zig
39
src/lib.zig
@@ -10,9 +10,10 @@ pub const Emulator = struct {
|
||||
readFn: *const fn (*anyopaque, u32) u8,
|
||||
writeFn: *const fn (*anyopaque, u32, u8) void,
|
||||
|
||||
// FIXME: Expensive copy
|
||||
registersFn: *const fn (*const anyopaque) [16]u32,
|
||||
cpsrFn: *const fn (*const anyopaque) u32,
|
||||
registersFn: *const fn (*anyopaque) *[16]u32,
|
||||
cpsrFn: *const fn (*anyopaque) u32,
|
||||
|
||||
stepFn: *const fn (*anyopaque) void,
|
||||
|
||||
pub fn init(ptr: anytype) Self {
|
||||
const Ptr = @TypeOf(ptr);
|
||||
@@ -27,35 +28,35 @@ pub const Emulator = struct {
|
||||
pub fn readImpl(pointer: *anyopaque, addr: u32) u8 {
|
||||
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
||||
|
||||
return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.read, .{ u8, self, addr });
|
||||
return @call(.always_inline, ptr_info.Pointer.child.read, .{ self, addr });
|
||||
}
|
||||
|
||||
pub fn writeImpl(pointer: *anyopaque, addr: u32, value: u8) void {
|
||||
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
||||
|
||||
return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.read, .{ u8, self, addr, value });
|
||||
return @call(.always_inline, ptr_info.Pointer.child.write, .{ self, addr, value });
|
||||
}
|
||||
|
||||
pub fn registersImpl(pointer: *const anyopaque) [16]u32 {
|
||||
pub fn registersImpl(pointer: *anyopaque) *[16]u32 {
|
||||
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
||||
|
||||
return self.r;
|
||||
return @call(.always_inline, ptr_info.Pointer.child.registers, .{self});
|
||||
}
|
||||
|
||||
pub fn cpsrImpl(pointer: *const anyopaque) u32 {
|
||||
pub fn cpsrImpl(pointer: *anyopaque) u32 {
|
||||
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
||||
|
||||
return self.cpsr.raw;
|
||||
return @call(.always_inline, ptr_info.Pointer.child.cpsr, .{self});
|
||||
}
|
||||
|
||||
pub fn stepImpl(pointer: *anyopaque) void {
|
||||
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
||||
|
||||
return @call(.always_inline, ptr_info.Pointer.child.step, .{self});
|
||||
}
|
||||
};
|
||||
|
||||
return .{
|
||||
.ptr = ptr,
|
||||
.readFn = gen.readImpl,
|
||||
.writeFn = gen.writeImpl,
|
||||
.registersFn = gen.registersImpl,
|
||||
.cpsrFn = gen.cpsrImpl,
|
||||
};
|
||||
return .{ .ptr = ptr, .readFn = gen.readImpl, .writeFn = gen.writeImpl, .registersFn = gen.registersImpl, .cpsrFn = gen.cpsrImpl, .stepFn = gen.stepImpl };
|
||||
}
|
||||
|
||||
pub inline fn read(self: Self, addr: u32) u8 {
|
||||
@@ -66,11 +67,15 @@ pub const Emulator = struct {
|
||||
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);
|
||||
}
|
||||
|
||||
pub inline fn cpsr(self: Self) u32 {
|
||||
return self.cpsrFn(self.ptr);
|
||||
}
|
||||
|
||||
pub inline fn step(self: Self) void {
|
||||
self.stepFn(self.ptr);
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user