Compare commits

..

No commits in common. "1b51632360c4ab84b7a8abffd9c38ea163f381c9" and "8e8f85fc5355b90ee4d61be8fdf0548f16c4ab4e" have entirely different histories.

2 changed files with 6 additions and 103 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 #+begin_src shell
guix shell --pure -m=manifest.scm guix shell --pure -m=manifest.scm
zig build run zig build run
#+end_src #+end_src

View file

@ -18,28 +18,6 @@ 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;
const TOO_MANY_ARGUMENTS = "Wrong number of arguments";
const WRONG_ARGUMENT = "Invalid argument";
// COMMAND
const PROMPT_COMMAND = "prompt";
const SEGMENT_REGISTER_COMMAND = "sr";
const REGISTER_COMMAND = "reg";
const CLEAR_COMMAND = "cls";
const HELP_COMMAND = "?";
// DESCRIPTION
const PROMPT_DESCRIPTION = "Change the prompt between Long-mode or Short-mode.";
const REGISTER_DESCRIPTION = "Print the current values for Registers.";
const SEGMENT_REGISTER_DESCRIPTION = "Print the current values for Segment Registers.";
const CLEAR_DESCRIPTION = "Clears the screen.";
// USAGE
const PROMPT_USAGE = "prompt {short|long}";
const REGISTER_USAGE = "reg";
const SEGMENT_REGISTER_USAGE = "sr";
const HELP_USAGE = "?";
const CLEAR_USAGE = "cls";
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();
@ -65,11 +43,7 @@ pub fn main() !void {
, .{}); , .{});
var buffer: [256]u8 = undefined; var buffer: [256]u8 = undefined;
while (true) { while (true) {
if (long_prompt) { modeLine(&cpu, &memory);
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, " ");
@ -78,12 +52,6 @@ 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, "?")) {
try handleHelpCommand(&it);
} else if (std.mem.eql(u8, first_argument, CLEAR_COMMAND)) {
try handleClearScreenCommand(&it);
} }
} else { } else {
// noop // noop
@ -96,18 +64,14 @@ pub fn main() !void {
_ = try output.writeAll("\x1b[?25h"); _ = try output.writeAll("\x1b[?25h");
} }
fn promptShort() void { fn modeLine(cpu: *cpu_.Cpu, memory: *mem.Memory) 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),
@ -120,6 +84,7 @@ fn promptLong(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 {
@ -139,72 +104,10 @@ fn printSr(cpu: *cpu_.Cpu) void {
}); });
} }
fn handleHelpCommand(it: anytype) !void {
if (it.next()) |_| {
std.debug.print(
\\{s}. {s}
\\
, .{ TOO_MANY_ARGUMENTS, HELP_USAGE });
} else {
std.debug.print(
\\{s: <20} {s}
\\{s: <20} {s}
\\{s: <20} {s}
\\{s: <20} {s}
\\
, .{
PROMPT_USAGE, PROMPT_DESCRIPTION,
REGISTER_USAGE, REGISTER_DESCRIPTION,
SEGMENT_REGISTER_USAGE, SEGMENT_REGISTER_DESCRIPTION,
CLEAR_USAGE, CLEAR_DESCRIPTION,
});
}
}
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(
\\{s}, usage: {s}
\\
, .{ WRONG_ARGUMENT, PROMPT_USAGE });
}
} else {
std.debug.print(
\\{s}, usage: {s}
\\
, .{ TOO_MANY_ARGUMENTS, PROMPT_USAGE });
}
}
fn handleRegisterCommand(cpu: *cpu_.Cpu, it: anytype) !void {
_ = cpu;
_ = it;
}
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( std.debug.print("Wrong number of arguments.\n", .{});
\\{s}, usage: {s}
\\
, .{ TOO_MANY_ARGUMENTS, SEGMENT_REGISTER_USAGE });
} else { } else {
printSr(cpu); printSr(cpu);
} }
} }
fn handleClearScreenCommand(it: anytype) !void {
var output = std.io.getStdOut().writer();
if (it.next()) |_| {
std.debug.print(
\\{s}, usage: {s}
\\
, .{ TOO_MANY_ARGUMENTS, CLEAR_USAGE });
} else {
try output.writeAll("\x1B[2J\x1B[H");
}
}