From 107a01a39cf030c61977d6cd399fe06058052266 Mon Sep 17 00:00:00 2001 From: Lynn Leichtle Date: Wed, 18 Sep 2024 20:32:41 +0200 Subject: [PATCH] prompt is optional --- README.org | 2 +- src/main.zig | 41 +++++++++++++++++++++++++++++++++++------ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/README.org b/README.org index 581811f..3db1550 100644 --- a/README.org +++ b/README.org @@ -9,7 +9,7 @@ The intent behind this project--beyond learning x86 assembly--is to fully emulat * Build Currently there are no dependencies for Miharu. To build and run the CLI REPL, simply: -#+begin_src shell +#+begin_src guix shell --pure -m=manifest.scm zig build run #+end_src diff --git a/src/main.zig b/src/main.zig index 7647250..9786406 100644 --- a/src/main.zig +++ b/src/main.zig @@ -17,7 +17,7 @@ const std = @import("std"); const mem = @import("memory.zig"); const cpu_ = @import("cpu.zig"); - +var long_prompt = false; pub fn main() !void { var memory = mem.Memory.init(); var cpu = cpu_.Cpu.init(); @@ -43,7 +43,11 @@ pub fn main() !void { , .{}); var buffer: [256]u8 = undefined; while (true) { - prompt(&cpu, &memory); + if (long_prompt) { + promptLong(&cpu, &memory); + } else { + promptShort(); + } const line = try input.readUntilDelimiterOrEof(&buffer, '\n'); const clean = std.mem.trim(u8, line.?, " \n\r"); var it = std.mem.splitSequence(u8, clean, " "); @@ -52,7 +56,9 @@ pub fn main() !void { break; } else if (std.mem.eql(u8, first_argument, "sr")) { try handleSegmentRegisterCommand(&cpu, &it); - } + } else if (std.mem.eql(u8, first_argument, "prompt")) { + try handlePromptCommand(&it); + } else if (std.mem.eql(u8, first_argument, "?")) {} } else { // noop } @@ -64,14 +70,18 @@ pub fn main() !void { _ = try output.writeAll("\x1b[?25h"); } -fn prompt(cpu: *cpu_.Cpu, memory: *mem.Memory) void { +fn promptShort() void { + std.debug.print("> ", .{}); +} + +fn promptLong(cpu: *cpu_.Cpu, memory: *mem.Memory) void { _ = memory; std.debug.print( \\ \\IP : {x:0>8} \\EAX: {x:0>8} EBX: {x:0>8} ECX: {x:0>8} EDX: {x:0>8} \\ESP: {x:0>8} EBP: {x:0>8} ESI: {x:0>8} EDI: {x:0>8} - \\ + \\> , .{ cpu.instruction_pointer, cpu.registers.get(.EAX), @@ -84,7 +94,6 @@ fn prompt(cpu: *cpu_.Cpu, memory: *mem.Memory) void { cpu.registers.get(.ESI), cpu.registers.get(.EDI), }); - std.debug.print("> ", .{}); } fn printSr(cpu: *cpu_.Cpu) void { @@ -104,6 +113,26 @@ fn printSr(cpu: *cpu_.Cpu) void { }); } +fn handlePromptCommand(it: anytype) !void { + if (it.next()) |next_argument| { + if (std.mem.eql(u8, next_argument, "long")) { + long_prompt = true; + } else if (std.mem.eql(u8, next_argument, "short")) { + long_prompt = false; + } else { + std.debug.print( + \\Invalid argument. Usage: prompt + \\ + , .{}); + } + } else { + std.debug.print( + \\Invalid number of arguments. Usage: prompt + \\ + , .{}); + } +} + fn handleSegmentRegisterCommand(cpu: *cpu_.Cpu, it: anytype) !void { if (it.next()) |_| { std.debug.print("Wrong number of arguments.\n", .{});