diff --git a/src/main.zig b/src/main.zig index 6ae442b..59a1b95 100644 --- a/src/main.zig +++ b/src/main.zig @@ -2,8 +2,6 @@ const std = @import("std"); const Uxn = @import("uxn.zig"); const Varvara = @import("varvara.zig"); -// TODO combine with uxn.zig -const UxnFmt = @import("uxn-instructions.zig"); pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; @@ -28,7 +26,7 @@ pub fn main() !void { std.debug.print("pc={X} code={X} op={s}\n", .{ varvara.uxn.pc, varvara.uxn.mem.m[varvara.uxn.pc], - UxnFmt.fmtInstrs(varvara.uxn.mem.m[varvara.uxn.pc .. varvara.uxn.pc +% 1]), + Uxn.fmtInstrs(varvara.uxn.mem.m[varvara.uxn.pc .. varvara.uxn.pc +% 1]), }); if (varvara.uxn.eval()) running = false; } diff --git a/src/uxn-instructions.zig b/src/uxn-instructions.zig deleted file mode 100644 index 41c1f64..0000000 --- a/src/uxn-instructions.zig +++ /dev/null @@ -1,61 +0,0 @@ -const std = @import("std"); - -pub fn formatInstruction( - bytes: []const u8, - comptime fmt: []const u8, - options: std.fmt.FormatOptions, - writer: anytype, -) !void { - _ = fmt; - _ = options; - std.debug.assert(bytes.len == 1); - switch (bytes[0] & 0x1F) { - 0x00 => switch (bytes[0]) { - 0x00 => try writer.writeAll("BRK"), - 0x20 => try writer.writeAll("JCI"), - 0x40 => try writer.writeAll("JMI"), - 0x60 => try writer.writeAll("JSI"), - 0x80, 0xA0, 0xC0, 0xE0 => try writer.writeAll("LIT"), - else => unreachable, - }, - 0x01 => try writer.writeAll("INC"), - 0x02 => try writer.writeAll("POP"), - 0x03 => try writer.writeAll("NIP"), - 0x04 => try writer.writeAll("SWP"), - 0x05 => try writer.writeAll("ROT"), - 0x06 => try writer.writeAll("DUP"), - 0x07 => try writer.writeAll("OVR"), - 0x08 => try writer.writeAll("EQU"), - 0x09 => try writer.writeAll("NEQ"), - 0x0A => try writer.writeAll("GTH"), - 0x0B => try writer.writeAll("LTH"), - 0x0C => try writer.writeAll("JMP"), - 0x0D => try writer.writeAll("JCN"), - 0x0E => try writer.writeAll("JSR"), - 0x0F => try writer.writeAll("STH"), - 0x10 => try writer.writeAll("LDZ"), - 0x11 => try writer.writeAll("STZ"), - 0x12 => try writer.writeAll("LDR"), - 0x13 => try writer.writeAll("STR"), - 0x14 => try writer.writeAll("LDA"), - 0x15 => try writer.writeAll("STA"), - 0x16 => try writer.writeAll("DEI"), - 0x17 => try writer.writeAll("DEO"), - 0x18 => try writer.writeAll("ADD"), - 0x19 => try writer.writeAll("SUB"), - 0x1A => try writer.writeAll("MUL"), - 0x1B => try writer.writeAll("DIV"), - 0x1C => try writer.writeAll("AND"), - 0x1D => try writer.writeAll("ORA"), - 0x1E => try writer.writeAll("EOR"), - 0x1F => try writer.writeAll("SFT"), - else => unreachable, - } - if (bytes[0] & 0x20 == 0x20) try writer.writeByte('2'); - if (bytes[0] & 0x40 == 0x40) try writer.writeByte('k'); - if (bytes[0] & 0x80 == 0x80) try writer.writeByte('r'); -} - -pub fn fmtInstrs(bytes: []const u8) std.fmt.Formatter(formatInstruction) { - return .{ .data = bytes }; -} diff --git a/src/uxn.zig b/src/uxn.zig index d88cc3a..91a546a 100644 --- a/src/uxn.zig +++ b/src/uxn.zig @@ -635,3 +635,63 @@ test "stack push/pop" { // uxn.loop(); // try std.testing.expectEqual(18, uxn.ws.pop(u8)); // } + +pub fn formatInstruction( + bytes: []const u8, + comptime fmt: []const u8, + options: std.fmt.FormatOptions, + writer: anytype, +) !void { + _ = fmt; + _ = options; + std.debug.assert(bytes.len == 1); + switch (bytes[0] & 0x1F) { + 0x00 => switch (bytes[0]) { + 0x00 => try writer.writeAll("BRK"), + 0x20 => try writer.writeAll("JCI"), + 0x40 => try writer.writeAll("JMI"), + 0x60 => try writer.writeAll("JSI"), + 0x80, 0xA0, 0xC0, 0xE0 => try writer.writeAll("LIT"), + else => unreachable, + }, + 0x01 => try writer.writeAll("INC"), + 0x02 => try writer.writeAll("POP"), + 0x03 => try writer.writeAll("NIP"), + 0x04 => try writer.writeAll("SWP"), + 0x05 => try writer.writeAll("ROT"), + 0x06 => try writer.writeAll("DUP"), + 0x07 => try writer.writeAll("OVR"), + 0x08 => try writer.writeAll("EQU"), + 0x09 => try writer.writeAll("NEQ"), + 0x0A => try writer.writeAll("GTH"), + 0x0B => try writer.writeAll("LTH"), + 0x0C => try writer.writeAll("JMP"), + 0x0D => try writer.writeAll("JCN"), + 0x0E => try writer.writeAll("JSR"), + 0x0F => try writer.writeAll("STH"), + 0x10 => try writer.writeAll("LDZ"), + 0x11 => try writer.writeAll("STZ"), + 0x12 => try writer.writeAll("LDR"), + 0x13 => try writer.writeAll("STR"), + 0x14 => try writer.writeAll("LDA"), + 0x15 => try writer.writeAll("STA"), + 0x16 => try writer.writeAll("DEI"), + 0x17 => try writer.writeAll("DEO"), + 0x18 => try writer.writeAll("ADD"), + 0x19 => try writer.writeAll("SUB"), + 0x1A => try writer.writeAll("MUL"), + 0x1B => try writer.writeAll("DIV"), + 0x1C => try writer.writeAll("AND"), + 0x1D => try writer.writeAll("ORA"), + 0x1E => try writer.writeAll("EOR"), + 0x1F => try writer.writeAll("SFT"), + else => unreachable, + } + if (bytes[0] & 0x20 == 0x20) try writer.writeByte('2'); + if (bytes[0] & 0x40 == 0x40) try writer.writeByte('k'); + if (bytes[0] & 0x80 == 0x80) try writer.writeByte('r'); +} + +pub fn fmtInstrs(bytes: []const u8) std.fmt.Formatter(formatInstruction) { + return .{ .data = bytes }; +}