feat: implement gdb memory map

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-12-15 05:06:50 -04:00
parent 400e155502
commit 8a58251ef6
2 changed files with 47 additions and 2 deletions

View File

@ -1,9 +1,11 @@
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);
pub const max_len: usize = 0x1000;
@ -143,7 +145,7 @@ pub fn parse(self: *Self, allocator: Allocator, emu: Emulator) !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" };
@ -174,6 +176,27 @@ pub fn parse(self: *Self, allocator: Allocator, emu: Emulator) !String {
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 = "" };
},

View File

@ -36,6 +36,28 @@ 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 =
\\ <?xml version="1.0"?>
\\ <!DOCTYPE memory-map
\\ PUBLIC "+//IDN gnu.org//DTD GDB Memory Map V1.0//EN"
\\ "http://sourceware.org/gdb/gdb-memory-map.dtd">
\\
\\ <memory-map>
\\ <memory type="rom" start="0" length="4000">
\\ <memory type="ram" start="2000000" length="40000">
\\ <memory type="ram" start="3000000" length="8000">
\\ <memory type="ram" start="4000000" length="400">
\\ <memory type="ram" start="5000000" length="400">
\\ <memory type="ram" start="6000000" length="18000">
\\ <memory type="ram" start="7000000" length="400">
\\ <memory type="rom" start="8000000" length="20000000">
\\ <memory type="rom" start="A000000" length="20000000">
\\ <memory type="rom" start="C000000" length="20000000">
\\ </memory-map>;
;
// FIXME: Shouldn't this be a Packet Struct?
pkt_cache: ?[]const u8 = null,