From eb8e5175bd9738e92d10c47f75abb174f3624082 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Wed, 27 Dec 2023 21:01:02 -0600 Subject: [PATCH] fix: make gdbstub more modular in prep for supporting zba and turbo with the same codebase --- src/Packet.zig | 11 ++---- src/Server.zig | 101 +++++++++++++++++++++++++++---------------------- 2 files changed, 60 insertions(+), 52 deletions(-) diff --git a/src/Packet.zig b/src/Packet.zig index cc322ef..55e7116 100644 --- a/src/Packet.zig +++ b/src/Packet.zig @@ -5,9 +5,6 @@ const Emulator = @import("lib.zig").Emulator; const State = @import("State.zig"); const Server = @import("Server.zig"); -const target = @import("Server.zig").target; -const memory_map = @import("Server.zig").memory_map; - const Self = @This(); const log = std.log.scoped(.Packet); pub const max_len: usize = 0x1000; @@ -236,11 +233,11 @@ pub fn parse(self: *Self, allocator: Allocator, state: *Server.State, emu: *Emul // + 2 to account for the "m " in the response // subtract offset so that the allocated buffer isn't // larger than it needs to be TODO: Test this? - const len = @min(length, (target.len + 1) - offset); + const len = @min(length, (state.target_xml.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..], target[offset..]); + std.mem.copy(u8, ret[1..], state.target_xml[offset..]); return .{ .alloc = ret }; } else { @@ -263,11 +260,11 @@ pub fn parse(self: *Self, allocator: Allocator, state: *Server.State, emu: *Emul const length = try std.fmt.parseInt(usize, length_str, 16); // see above - const len = @min(length, (memory_map.len + 1) - offset); + const len = @min(length, (state.memmap_xml.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..]); + std.mem.copy(u8, ret[1..], state.memmap_xml[offset..]); return .{ .alloc = ret }; } diff --git a/src/Server.zig b/src/Server.zig index f3197fc..e5eef0a 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -11,64 +11,75 @@ const Self = @This(); const log = std.log.scoped(.Server); const port: u16 = 2424; -pub const target: []const u8 = - \\ - \\ armv4t - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ -; +// TODO: move to ZBA +// pub const target: []const u8 = +// \\ +// \\ armv4t +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// ; -// Game Pak SRAM isn't included -// TODO: Can i be more specific here? -pub const memory_map: []const u8 = - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ - \\ -; +// // Game Pak SRAM isn't included +// // TODO: Can i be more specific here? +// pub const memory_map: []const u8 = +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// \\ +// ; // FIXME: Shouldn't this be a Packet Struct? pkt_cache: ?[]const u8 = null, socket: Server, -state: State = .{}, +state: State, emu: Emulator, -pub const State = struct { should_quit: bool = false }; +pub const State = struct { + should_quit: bool = false, + target_xml: []const u8, + memmap_xml: []const u8, +}; -pub fn init(emulator: Emulator) !Self { +const Xml = struct { target: []const u8, memory_map: []const u8 }; + +pub fn init(emulator: Emulator, xml: Xml) !Self { var server = std.net.StreamServer.init(.{}); try server.listen(std.net.Address.initIp4([_]u8{0} ** 4, port)); - return .{ .emu = emulator, .socket = server }; + return .{ + .emu = emulator, + .socket = server, + .state = .{ .target_xml = xml.target, .memmap_xml = xml.memory_map }, + }; } pub fn deinit(self: *Self, allocator: Allocator) void {