fix: make gdbstub more modular

in prep for supporting zba and turbo with the same codebase
This commit is contained in:
Rekai Nyangadzayi Musuka 2023-12-27 21:01:02 -06:00
parent 479319e7ca
commit eb8e5175bd
2 changed files with 60 additions and 52 deletions

View File

@ -5,9 +5,6 @@ const Emulator = @import("lib.zig").Emulator;
const State = @import("State.zig"); const State = @import("State.zig");
const Server = @import("Server.zig"); const Server = @import("Server.zig");
const target = @import("Server.zig").target;
const memory_map = @import("Server.zig").memory_map;
const Self = @This(); const Self = @This();
const log = std.log.scoped(.Packet); const log = std.log.scoped(.Packet);
pub const max_len: usize = 0x1000; 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 // + 2 to account for the "m " in the response
// subtract offset so that the allocated buffer isn't // subtract offset so that the allocated buffer isn't
// larger than it needs to be TODO: Test this? // 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); const ret = try allocator.alloc(u8, len);
ret[0] = if (ret.len < length) 'l' else 'm'; 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 }; return .{ .alloc = ret };
} else { } 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); const length = try std.fmt.parseInt(usize, length_str, 16);
// see above // 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); const ret = try allocator.alloc(u8, len);
ret[0] = if (ret.len < length) 'l' else 'm'; 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 }; return .{ .alloc = ret };
} }

View File

@ -11,64 +11,75 @@ const Self = @This();
const log = std.log.scoped(.Server); const log = std.log.scoped(.Server);
const port: u16 = 2424; const port: u16 = 2424;
pub const target: []const u8 = // TODO: move to ZBA
\\<target version="1.0"> // pub const target: []const u8 =
\\ <architecture>armv4t</architecture> // \\<target version="1.0">
\\ <feature name="org.gnu.gdb.arm.core"> // \\ <architecture>armv4t</architecture>
\\ <reg name="r0" bitsize="32" type="uint32"/> // \\ <feature name="org.gnu.gdb.arm.core">
\\ <reg name="r1" bitsize="32" type="uint32"/> // \\ <reg name="r0" bitsize="32" type="uint32"/>
\\ <reg name="r2" bitsize="32" type="uint32"/> // \\ <reg name="r1" bitsize="32" type="uint32"/>
\\ <reg name="r3" bitsize="32" type="uint32"/> // \\ <reg name="r2" bitsize="32" type="uint32"/>
\\ <reg name="r4" bitsize="32" type="uint32"/> // \\ <reg name="r3" bitsize="32" type="uint32"/>
\\ <reg name="r5" bitsize="32" type="uint32"/> // \\ <reg name="r4" bitsize="32" type="uint32"/>
\\ <reg name="r6" bitsize="32" type="uint32"/> // \\ <reg name="r5" bitsize="32" type="uint32"/>
\\ <reg name="r7" bitsize="32" type="uint32"/> // \\ <reg name="r6" bitsize="32" type="uint32"/>
\\ <reg name="r8" bitsize="32" type="uint32"/> // \\ <reg name="r7" bitsize="32" type="uint32"/>
\\ <reg name="r9" bitsize="32" type="uint32"/> // \\ <reg name="r8" bitsize="32" type="uint32"/>
\\ <reg name="r10" bitsize="32" type="uint32"/> // \\ <reg name="r9" bitsize="32" type="uint32"/>
\\ <reg name="r11" bitsize="32" type="uint32"/> // \\ <reg name="r10" bitsize="32" type="uint32"/>
\\ <reg name="r12" bitsize="32" type="uint32"/> // \\ <reg name="r11" bitsize="32" type="uint32"/>
\\ <reg name="sp" bitsize="32" type="data_ptr"/> // \\ <reg name="r12" bitsize="32" type="uint32"/>
\\ <reg name="lr" bitsize="32"/> // \\ <reg name="sp" bitsize="32" type="data_ptr"/>
\\ <reg name="pc" bitsize="32" type="code_ptr"/> // \\ <reg name="lr" bitsize="32"/>
\\ // \\ <reg name="pc" bitsize="32" type="code_ptr"/>
\\ <reg name="cpsr" bitsize="32" regnum="25"/> // \\
\\ </feature> // \\ <reg name="cpsr" bitsize="32" regnum="25"/>
\\</target> // \\ </feature>
; // \\</target>
// ;
// Game Pak SRAM isn't included // // Game Pak SRAM isn't included
// TODO: Can i be more specific here? // // TODO: Can i be more specific here?
pub const memory_map: []const u8 = // pub const memory_map: []const u8 =
\\ <memory-map version="1.0"> // \\ <memory-map version="1.0">
\\ <memory type="rom" start="0x00000000" length="0x00004000"/> // \\ <memory type="rom" start="0x00000000" length="0x00004000"/>
\\ <memory type="ram" start="0x02000000" length="0x00040000"/> // \\ <memory type="ram" start="0x02000000" length="0x00040000"/>
\\ <memory type="ram" start="0x03000000" length="0x00008000"/> // \\ <memory type="ram" start="0x03000000" length="0x00008000"/>
\\ <memory type="ram" start="0x04000000" length="0x00000400"/> // \\ <memory type="ram" start="0x04000000" length="0x00000400"/>
\\ <memory type="ram" start="0x05000000" length="0x00000400"/> // \\ <memory type="ram" start="0x05000000" length="0x00000400"/>
\\ <memory type="ram" start="0x06000000" length="0x00018000"/> // \\ <memory type="ram" start="0x06000000" length="0x00018000"/>
\\ <memory type="ram" start="0x07000000" length="0x00000400"/> // \\ <memory type="ram" start="0x07000000" length="0x00000400"/>
\\ <memory type="rom" start="0x08000000" length="0x02000000"/> // \\ <memory type="rom" start="0x08000000" length="0x02000000"/>
\\ <memory type="rom" start="0x0A000000" length="0x02000000"/> // \\ <memory type="rom" start="0x0A000000" length="0x02000000"/>
\\ <memory type="rom" start="0x0C000000" length="0x02000000"/> // \\ <memory type="rom" start="0x0C000000" length="0x02000000"/>
\\ </memory-map> // \\ </memory-map>
; // ;
// FIXME: Shouldn't this be a Packet Struct? // FIXME: Shouldn't this be a Packet Struct?
pkt_cache: ?[]const u8 = null, pkt_cache: ?[]const u8 = null,
socket: Server, socket: Server,
state: State = .{}, state: State,
emu: Emulator, 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(.{}); var server = std.net.StreamServer.init(.{});
try server.listen(std.net.Address.initIp4([_]u8{0} ** 4, port)); 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 { pub fn deinit(self: *Self, allocator: Allocator) void {