prompt is optional
This commit is contained in:
parent
46169f8c6f
commit
107a01a39c
2 changed files with 36 additions and 7 deletions
|
@ -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
|
||||
|
|
41
src/main.zig
41
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 <long|short>
|
||||
\\
|
||||
, .{});
|
||||
}
|
||||
} else {
|
||||
std.debug.print(
|
||||
\\Invalid number of arguments. Usage: prompt <long|short>
|
||||
\\
|
||||
, .{});
|
||||
}
|
||||
}
|
||||
|
||||
fn handleSegmentRegisterCommand(cpu: *cpu_.Cpu, it: anytype) !void {
|
||||
if (it.next()) |_| {
|
||||
std.debug.print("Wrong number of arguments.\n", .{});
|
||||
|
|
Loading…
Reference in a new issue