Compare commits
3 Commits
400e155502
...
0feb428985
Author | SHA1 | Date |
---|---|---|
Rekai Nyangadzayi Musuka | 0feb428985 | |
Rekai Nyangadzayi Musuka | 2cb5c93eca | |
Rekai Nyangadzayi Musuka | d64b35fdf3 |
82
build.zig
82
build.zig
|
@ -1,38 +1,60 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
pub fn build(b: *std.build.Builder) void {
|
fn path(comptime suffix: []const u8) []const u8 {
|
||||||
// Standard target options allows the person running `zig build` to choose
|
if (suffix[0] == '/') @compileError("expected a relative path");
|
||||||
// what target to build for. Here we do not override the defaults, which
|
return comptime (std.fs.path.dirname(@src().file) orelse ".") ++ std.fs.path.sep_str ++ suffix;
|
||||||
// means any target is allowed, and the default is native. Other options
|
}
|
||||||
// for restricting supported target set are available.
|
|
||||||
const target = b.standardTargetOptions(.{});
|
|
||||||
|
|
||||||
// Standard release options allow the person running `zig build` to select
|
const pkgs = struct {
|
||||||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
|
const Pkg = std.build.Pkg;
|
||||||
const mode = b.standardReleaseOptions();
|
|
||||||
|
|
||||||
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
|
// 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);
|
pub fn link(exe: *std.build.LibExeObjStep) void {
|
||||||
exe.setBuildMode(mode);
|
exe.addPackage(pkgs.gdbstub);
|
||||||
exe.install();
|
}
|
||||||
|
|
||||||
const run_cmd = exe.run();
|
pub fn build(b: *std.build.Builder) void {
|
||||||
run_cmd.step.dependOn(b.getInstallStep());
|
const target = b.standardTargetOptions(.{});
|
||||||
if (b.args) |args| {
|
_ = target;
|
||||||
run_cmd.addArgs(args);
|
const mode = b.standardReleaseOptions();
|
||||||
}
|
|
||||||
|
// -- library --
|
||||||
const run_step = b.step("run", "Run the app");
|
const lib = b.addStaticLibrary("gdbstub", "src/lib.zig");
|
||||||
run_step.dependOn(&run_cmd.step);
|
lib.addPackage(pkgs.network);
|
||||||
|
|
||||||
const exe_tests = b.addTest("src/main.zig");
|
lib.setBuildMode(mode);
|
||||||
exe_tests.setTarget(target);
|
lib.install();
|
||||||
exe_tests.setBuildMode(mode);
|
|
||||||
|
const lib_tests = b.addTest("src/lib.zig");
|
||||||
const test_step = b.step("test", "Run unit tests");
|
lib_tests.setBuildMode(mode);
|
||||||
test_step.dependOn(&exe_tests.step);
|
|
||||||
|
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 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();
|
||||||
|
|
|
@ -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 std = @import("std");
|
||||||
const network = @import("network");
|
const Server = @import("gdbstub").Server;
|
||||||
const Server = @import("Server.zig");
|
|
||||||
|
|
||||||
const Allocator = std.mem.Allocator;
|
|
||||||
const Socket = network.Socket;
|
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
const log = std.log.scoped(.Main);
|
const log = std.log.scoped(.Main);
|
||||||
|
|
Loading…
Reference in New Issue