prompt is optional

This commit is contained in:
Lynn Leichtle 2024-09-18 20:32:41 +02:00
parent 46169f8c6f
commit 107a01a39c
Signed by: lynn
GPG key ID: 55E797F631DDA03C
2 changed files with 36 additions and 7 deletions

View file

@ -9,7 +9,7 @@ The intent behind this project--beyond learning x86 assembly--is to fully emulat
* Build * Build
Currently there are no dependencies for Miharu. To build and run the CLI REPL, simply: 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 guix shell --pure -m=manifest.scm
zig build run zig build run
#+end_src #+end_src

View file

@ -17,7 +17,7 @@
const std = @import("std"); const std = @import("std");
const mem = @import("memory.zig"); const mem = @import("memory.zig");
const cpu_ = @import("cpu.zig"); const cpu_ = @import("cpu.zig");
var long_prompt = false;
pub fn main() !void { pub fn main() !void {
var memory = mem.Memory.init(); var memory = mem.Memory.init();
var cpu = cpu_.Cpu.init(); var cpu = cpu_.Cpu.init();
@ -43,7 +43,11 @@ pub fn main() !void {
, .{}); , .{});
var buffer: [256]u8 = undefined; var buffer: [256]u8 = undefined;
while (true) { while (true) {
prompt(&cpu, &memory); if (long_prompt) {
promptLong(&cpu, &memory);
} else {
promptShort();
}
const line = try input.readUntilDelimiterOrEof(&buffer, '\n'); const line = try input.readUntilDelimiterOrEof(&buffer, '\n');
const clean = std.mem.trim(u8, line.?, " \n\r"); const clean = std.mem.trim(u8, line.?, " \n\r");
var it = std.mem.splitSequence(u8, clean, " "); var it = std.mem.splitSequence(u8, clean, " ");
@ -52,7 +56,9 @@ pub fn main() !void {
break; break;
} else if (std.mem.eql(u8, first_argument, "sr")) { } else if (std.mem.eql(u8, first_argument, "sr")) {
try handleSegmentRegisterCommand(&cpu, &it); 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 { } else {
// noop // noop
} }
@ -64,14 +70,18 @@ pub fn main() !void {
_ = try output.writeAll("\x1b[?25h"); _ = 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; _ = memory;
std.debug.print( std.debug.print(
\\ \\
\\IP : {x:0>8} \\IP : {x:0>8}
\\EAX: {x:0>8} EBX: {x:0>8} ECX: {x:0>8} EDX: {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} \\ESP: {x:0>8} EBP: {x:0>8} ESI: {x:0>8} EDI: {x:0>8}
\\ \\>
, .{ , .{
cpu.instruction_pointer, cpu.instruction_pointer,
cpu.registers.get(.EAX), cpu.registers.get(.EAX),
@ -84,7 +94,6 @@ fn prompt(cpu: *cpu_.Cpu, memory: *mem.Memory) void {
cpu.registers.get(.ESI), cpu.registers.get(.ESI),
cpu.registers.get(.EDI), cpu.registers.get(.EDI),
}); });
std.debug.print("> ", .{});
} }
fn printSr(cpu: *cpu_.Cpu) void { 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 <long|short>
\\
, .{});
}
} else {
std.debug.print(
\\Invalid number of arguments. Usage: prompt <long|short>
\\
, .{});
}
}
fn handleSegmentRegisterCommand(cpu: *cpu_.Cpu, it: anytype) !void { fn handleSegmentRegisterCommand(cpu: *cpu_.Cpu, it: anytype) !void {
if (it.next()) |_| { if (it.next()) |_| {
std.debug.print("Wrong number of arguments.\n", .{}); std.debug.print("Wrong number of arguments.\n", .{});