Compare commits
3 Commits
0feb428985
...
400e155502
Author | SHA1 | Date |
---|---|---|
Rekai Nyangadzayi Musuka | 400e155502 | |
Rekai Nyangadzayi Musuka | 26aad8d1ae | |
Rekai Nyangadzayi Musuka | f1e1efc5e5 |
82
build.zig
82
build.zig
|
@ -1,38 +1,60 @@
|
|||
const std = @import("std");
|
||||
|
||||
pub fn build(b: *std.build.Builder) void {
|
||||
// Standard target options allows the person running `zig build` to choose
|
||||
// what target to build for. Here we do not override the defaults, which
|
||||
// means any target is allowed, and the default is native. Other options
|
||||
// for restricting supported target set are available.
|
||||
const target = b.standardTargetOptions(.{});
|
||||
fn path(comptime suffix: []const u8) []const u8 {
|
||||
if (suffix[0] == '/') @compileError("expected a relative path");
|
||||
return comptime (std.fs.path.dirname(@src().file) orelse ".") ++ std.fs.path.sep_str ++ suffix;
|
||||
}
|
||||
|
||||
// Standard release options allow the person running `zig build` to select
|
||||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
|
||||
const mode = b.standardReleaseOptions();
|
||||
const pkgs = struct {
|
||||
const Pkg = std.build.Pkg;
|
||||
|
||||
const exe = b.addExecutable("zba-gdbstub", "src/main.zig");
|
||||
pub const gdbstub: Pkg = .{
|
||||
.name = "gdbstub",
|
||||
.source = .{ .path = path("src/lib.zig") },
|
||||
.dependencies = &[_]Pkg{network},
|
||||
};
|
||||
|
||||
// https://github.com/MasterQ32/zig-network
|
||||
exe.addPackagePath("network", "lib/zig-network/network.zig");
|
||||
pub const network: Pkg = .{
|
||||
.name = "network",
|
||||
.source = .{ .path = path("lib/zig-network/network.zig") },
|
||||
};
|
||||
};
|
||||
|
||||
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_step = b.step("run", "Run the app");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
|
||||
const exe_tests = b.addTest("src/main.zig");
|
||||
exe_tests.setTarget(target);
|
||||
exe_tests.setBuildMode(mode);
|
||||
|
||||
const test_step = b.step("test", "Run unit tests");
|
||||
test_step.dependOn(&exe_tests.step);
|
||||
pub fn link(exe: *std.build.LibExeObjStep) void {
|
||||
exe.addPackage(pkgs.gdbstub);
|
||||
}
|
||||
|
||||
pub fn build(b: *std.build.Builder) void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
_ = target;
|
||||
const mode = b.standardReleaseOptions();
|
||||
|
||||
// -- library --
|
||||
const lib = b.addStaticLibrary("gdbstub", "src/lib.zig");
|
||||
lib.addPackage(pkgs.network);
|
||||
|
||||
lib.setBuildMode(mode);
|
||||
lib.install();
|
||||
|
||||
const lib_tests = b.addTest("src/lib.zig");
|
||||
lib_tests.setBuildMode(mode);
|
||||
|
||||
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);
|
||||
|
||||
// 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_step = b.step("run", "Run the app");
|
||||
// run_step.dependOn(&run_cmd.step);
|
||||
}
|
||||
|
|
|
@ -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 });
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
/// Re-export of the server interface
|
||||
pub const Server = @import("Server.zig");
|
||||
|
||||
/// Interface for interacting between GDB and a GBA emu
|
||||
pub const Emulator = struct {
|
||||
const Self = @This();
|
||||
|
||||
ptr: *anyopaque,
|
||||
|
||||
readFn: *const fn (*anyopaque, u32) u8,
|
||||
writeFn: *const fn (*anyopaque, u32, u8) void,
|
||||
|
||||
// FIXME: Expensive copy
|
||||
registersFn: *const fn (*anyopaque) [16]u32,
|
||||
cpsrFn: *const fn (*anyopaque) u32,
|
||||
|
||||
pub fn init(ptr: anytype) Self {
|
||||
const Ptr = @TypeOf(ptr);
|
||||
const ptr_info = @typeInfo(Ptr);
|
||||
|
||||
if (ptr_info != .Pointer) @compileError("ptr must be a pointer");
|
||||
if (ptr_info.Pointer.size != .One) @compileError("ptr must be a single-item pointer");
|
||||
|
||||
const alignment = ptr_info.Pointer.alignment;
|
||||
|
||||
const gen = 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, .{ 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.write, .{ self, addr, value });
|
||||
}
|
||||
|
||||
pub fn registersImpl(pointer: *anyopaque) [16]u32 {
|
||||
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
||||
|
||||
return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.registers, .{self});
|
||||
}
|
||||
|
||||
pub fn cpsrImpl(pointer: *anyopaque) u32 {
|
||||
const self = @ptrCast(Ptr, @alignCast(alignment, pointer));
|
||||
|
||||
return @call(.{ .modifier = .always_inline }, ptr_info.Pointer.child.cpsr, .{self});
|
||||
}
|
||||
};
|
||||
|
||||
return .{
|
||||
.ptr = ptr,
|
||||
.readFn = gen.readImpl,
|
||||
.writeFn = gen.writeImpl,
|
||||
.registersFn = gen.registersImpl,
|
||||
.cpsrFn = gen.cpsrImpl,
|
||||
};
|
||||
}
|
||||
|
||||
pub inline fn read(self: Self, addr: u32) u8 {
|
||||
return self.readFn(self.ptr, addr);
|
||||
}
|
||||
|
||||
pub inline fn write(self: Self, addr: u32, value: u8) void {
|
||||
self.writeFn(self.ptr, addr, value);
|
||||
}
|
||||
|
||||
pub inline fn registers(self: Self) [16]u32 {
|
||||
return self.registersFn(self.ptr);
|
||||
}
|
||||
|
||||
pub inline fn cpsr(self: Self) u32 {
|
||||
return self.cpsrFn(self.ptr);
|
||||
}
|
||||
};
|
|
@ -1,9 +1,5 @@
|
|||
const std = @import("std");
|
||||
const network = @import("network");
|
||||
const Server = @import("Server.zig");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Socket = network.Socket;
|
||||
const Server = @import("gdbstub").Server;
|
||||
|
||||
pub fn main() !void {
|
||||
const log = std.log.scoped(.Main);
|
||||
|
|
Loading…
Reference in New Issue