From 229171040950fcb48afcf6c07642cbe59f52e70c Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Thu, 15 Dec 2022 04:12:26 -0400 Subject: [PATCH] feat: integrate emulator interface while I figure out the interface with zba, disable the exe example since it doesn't have an emu to pass to gdbstub --- build.zig | 23 ++++++++++++----------- src/Packet.zig | 10 +++++----- src/Server.zig | 13 ++++++++----- src/lib.zig | 8 ++++---- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/build.zig b/build.zig index 461252e..49897fb 100644 --- a/build.zig +++ b/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); } diff --git a/src/Packet.zig b/src/Packet.zig index 6d1035f..d63a79e 100644 --- a/src/Packet.zig +++ b/src/Packet.zig @@ -2,6 +2,7 @@ const std = @import("std"); const target = @import("Server.zig").target; const Allocator = std.mem.Allocator; +const Emulator = @import("lib.zig").Emulator; const Self = @This(); const log = std.log.scoped(.Packet); @@ -44,7 +45,7 @@ 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 '?' => { @@ -55,8 +56,8 @@ pub fn parse(self: *Self, allocator: Allocator) !String { }, 'g' => { // TODO: Actually reference GBA Registers - const r = [_]u32{0xDEAD_BEEF} ** 0x10; - const cpsr: u32 = 0xCAFE_B0BA; + const r = emu.registers(); + const cpsr = emu.cpsr(); const char_len = 2; const reg_len = @sizeOf(u32) * char_len; // Every byte is represented by 2 characters @@ -88,14 +89,13 @@ pub fn parse(self: *Self, allocator: Allocator) !String { 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; while (i < len) : (i += 1) { - const value: u8 = 0; + const value: u8 = emu.read(addr + i); _ = std.fmt.bufPrintIntToSlice(ret[i * 2 ..][0..2], value, 16, .lower, .{ .fill = '0', .width = 2 }); } diff --git a/src/Server.zig b/src/Server.zig index 72f5d90..da51e40 100644 --- a/src/Server.zig +++ b/src/Server.zig @@ -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); @@ -41,7 +42,9 @@ 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 +56,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,12 +84,12 @@ 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 { return switch (input[0]) { '+' => .nothing, '-' => .retry, @@ -95,7 +98,7 @@ fn parse(allocator: Allocator, input: []const u8) !Action { var packet = Packet.from(allocator, input) catch return .nack; defer packet.deinit(allocator); - var string = packet.parse(allocator) catch return .nack; + var string = packet.parse(allocator, self.emu) catch return .nack; defer string.deinit(allocator); const reply = string.inner(); diff --git a/src/lib.zig b/src/lib.zig index 50c90a5..dcb9a6c 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -27,25 +27,25 @@ 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(.{ .modifier = .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(.{ .modifier = .always_inline }, ptr_info.Pointer.child.read, .{ self, addr, value }); } pub fn registersImpl(pointer: *const anyopaque) [16]u32 { const self = @ptrCast(Ptr, @alignCast(alignment, pointer)); - return self.r; + return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.registers, .{self}); } pub fn cpsrImpl(pointer: *const anyopaque) u32 { const self = @ptrCast(Ptr, @alignCast(alignment, pointer)); - return self.cpsr.raw; + return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.cpsr, .{self}); } };