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
This commit is contained in:
Rekai Nyangadzayi Musuka 2022-12-15 04:12:26 -04:00
parent 26aad8d1ae
commit 400e155502
4 changed files with 35 additions and 31 deletions

View File

@ -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);
}

View File

@ -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
@ -64,7 +65,7 @@ pub fn parse(self: *Self, allocator: Allocator) !String {
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;
@ -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;
var i: u32 = 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 });
}

View File

@ -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();

View File

@ -11,8 +11,8 @@ pub const Emulator = struct {
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,
pub fn init(ptr: anytype) Self {
const Ptr = @TypeOf(ptr);
@ -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.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(.{ .modifier = .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(.{ .modifier = .always_inline }, ptr_info.Pointer.child.cpsr, .{self});
}
};