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:
parent
26aad8d1ae
commit
400e155502
23
build.zig
23
build.zig
|
@ -27,6 +27,7 @@ pub fn link(exe: *std.build.LibExeObjStep) void {
|
||||||
|
|
||||||
pub fn build(b: *std.build.Builder) void {
|
pub fn build(b: *std.build.Builder) void {
|
||||||
const target = b.standardTargetOptions(.{});
|
const target = b.standardTargetOptions(.{});
|
||||||
|
_ = target;
|
||||||
const mode = b.standardReleaseOptions();
|
const mode = b.standardReleaseOptions();
|
||||||
|
|
||||||
// -- library --
|
// -- library --
|
||||||
|
@ -42,18 +43,18 @@ pub fn build(b: *std.build.Builder) void {
|
||||||
const test_step = b.step("lib-test", "Run Library Tests");
|
const test_step = b.step("lib-test", "Run Library Tests");
|
||||||
test_step.dependOn(&lib_tests.step);
|
test_step.dependOn(&lib_tests.step);
|
||||||
|
|
||||||
// -- Executable --
|
// // -- Executable --
|
||||||
const exe = b.addExecutable("gdbserver", "src/main.zig");
|
// const exe = b.addExecutable("gdbserver", "src/main.zig");
|
||||||
link(exe);
|
// link(exe);
|
||||||
|
|
||||||
exe.setTarget(target);
|
// exe.setTarget(target);
|
||||||
exe.setBuildMode(mode);
|
// exe.setBuildMode(mode);
|
||||||
exe.install();
|
// exe.install();
|
||||||
|
|
||||||
const run_cmd = exe.run();
|
// const run_cmd = exe.run();
|
||||||
run_cmd.step.dependOn(b.getInstallStep());
|
// run_cmd.step.dependOn(b.getInstallStep());
|
||||||
if (b.args) |args| run_cmd.addArgs(args);
|
// if (b.args) |args| run_cmd.addArgs(args);
|
||||||
|
|
||||||
const run_step = b.step("run", "Run the app");
|
// const run_step = b.step("run", "Run the app");
|
||||||
run_step.dependOn(&run_cmd.step);
|
// run_step.dependOn(&run_cmd.step);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@ const std = @import("std");
|
||||||
|
|
||||||
const target = @import("Server.zig").target;
|
const target = @import("Server.zig").target;
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
|
const Emulator = @import("lib.zig").Emulator;
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
const log = std.log.scoped(.Packet);
|
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]) {
|
switch (self.contents[0]) {
|
||||||
// Required
|
// Required
|
||||||
'?' => {
|
'?' => {
|
||||||
|
@ -55,8 +56,8 @@ pub fn parse(self: *Self, allocator: Allocator) !String {
|
||||||
},
|
},
|
||||||
'g' => {
|
'g' => {
|
||||||
// TODO: Actually reference GBA Registers
|
// TODO: Actually reference GBA Registers
|
||||||
const r = [_]u32{0xDEAD_BEEF} ** 0x10;
|
const r = emu.registers();
|
||||||
const cpsr: u32 = 0xCAFE_B0BA;
|
const cpsr = emu.cpsr();
|
||||||
|
|
||||||
const char_len = 2;
|
const char_len = 2;
|
||||||
const reg_len = @sizeOf(u32) * char_len; // Every byte is represented by 2 characters
|
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
|
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) {
|
while (i < r.len + 1) : (i += 1) {
|
||||||
const reg: u32 = if (i < r.len) r[i] else cpsr;
|
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 addr = try std.fmt.parseInt(u32, addr_str, 16);
|
||||||
const len = try std.fmt.parseInt(u32, length_str, 16);
|
const len = try std.fmt.parseInt(u32, length_str, 16);
|
||||||
_ = addr;
|
|
||||||
|
|
||||||
const ret = try allocator.alloc(u8, len * 2);
|
const ret = try allocator.alloc(u8, len * 2);
|
||||||
|
|
||||||
{
|
{
|
||||||
var i: usize = 0;
|
var i: u32 = 0;
|
||||||
while (i < len) : (i += 1) {
|
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 });
|
_ = std.fmt.bufPrintIntToSlice(ret[i * 2 ..][0..2], value, 16, .lower, .{ .fill = '0', .width = 2 });
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ const Packet = @import("Packet.zig");
|
||||||
|
|
||||||
const Socket = network.Socket;
|
const Socket = network.Socket;
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
|
const Emulator = @import("lib.zig").Emulator;
|
||||||
|
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
const log = std.log.scoped(.Server);
|
const log = std.log.scoped(.Server);
|
||||||
|
@ -41,7 +42,9 @@ pkt_cache: ?[]const u8 = null,
|
||||||
client: Socket,
|
client: Socket,
|
||||||
_socket: Socket,
|
_socket: Socket,
|
||||||
|
|
||||||
pub fn init() !Self {
|
emu: Emulator,
|
||||||
|
|
||||||
|
pub fn init(emulator: Emulator) !Self {
|
||||||
try network.init();
|
try network.init();
|
||||||
|
|
||||||
var socket = try Socket.create(.ipv4, .tcp);
|
var socket = try Socket.create(.ipv4, .tcp);
|
||||||
|
@ -53,7 +56,7 @@ pub fn init() !Self {
|
||||||
const endpoint = try client.getLocalEndPoint();
|
const endpoint = try client.getLocalEndPoint();
|
||||||
log.info("client connected from {}", .{endpoint});
|
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 {
|
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);
|
const len = try self.client.receive(&buf);
|
||||||
if (len == 0) break;
|
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);
|
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]) {
|
return switch (input[0]) {
|
||||||
'+' => .nothing,
|
'+' => .nothing,
|
||||||
'-' => .retry,
|
'-' => .retry,
|
||||||
|
@ -95,7 +98,7 @@ fn parse(allocator: Allocator, input: []const u8) !Action {
|
||||||
var packet = Packet.from(allocator, input) catch return .nack;
|
var packet = Packet.from(allocator, input) catch return .nack;
|
||||||
defer packet.deinit(allocator);
|
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);
|
defer string.deinit(allocator);
|
||||||
|
|
||||||
const reply = string.inner();
|
const reply = string.inner();
|
||||||
|
|
16
src/lib.zig
16
src/lib.zig
|
@ -11,8 +11,8 @@ pub const Emulator = struct {
|
||||||
writeFn: *const fn (*anyopaque, u32, u8) void,
|
writeFn: *const fn (*anyopaque, u32, u8) void,
|
||||||
|
|
||||||
// FIXME: Expensive copy
|
// FIXME: Expensive copy
|
||||||
registersFn: *const fn (*const anyopaque) [16]u32,
|
registersFn: *const fn (*anyopaque) [16]u32,
|
||||||
cpsrFn: *const fn (*const anyopaque) u32,
|
cpsrFn: *const fn (*anyopaque) u32,
|
||||||
|
|
||||||
pub fn init(ptr: anytype) Self {
|
pub fn init(ptr: anytype) Self {
|
||||||
const Ptr = @TypeOf(ptr);
|
const Ptr = @TypeOf(ptr);
|
||||||
|
@ -27,25 +27,25 @@ pub const Emulator = struct {
|
||||||
pub fn readImpl(pointer: *anyopaque, addr: u32) u8 {
|
pub fn readImpl(pointer: *anyopaque, addr: u32) u8 {
|
||||||
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
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 {
|
pub fn writeImpl(pointer: *anyopaque, addr: u32, value: u8) void {
|
||||||
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
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));
|
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));
|
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
||||||
|
|
||||||
return self.cpsr.raw;
|
return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.cpsr, .{self});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue