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
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
zig build run
#+end_src

View file

@ -18,28 +18,6 @@ const std = @import("std");
const mem = @import("memory.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 {
var memory = mem.Memory.init();
var cpu = cpu_.Cpu.init();
@ -65,11 +43,7 @@ pub fn main() !void {
, .{});
var buffer: [256]u8 = undefined;
while (true) {
if (long_prompt) {
promptLong(&cpu, &memory);
} else {
promptShort();
}
modeLine(&cpu, &memory);
const line = try input.readUntilDelimiterOrEof(&buffer, '\n');
const clean = std.mem.trim(u8, line.?, " \n\r");
var it = std.mem.splitSequence(u8, clean, " ");
@ -78,12 +52,6 @@ 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, "?")) {
try handleHelpCommand(&it);
} else if (std.mem.eql(u8, first_argument, CLEAR_COMMAND)) {
try handleClearScreenCommand(&it);
}
} else {
// noop
@ -96,18 +64,14 @@ pub fn main() !void {
_ = try output.writeAll("\x1b[?25h");
}
fn promptShort() void {
std.debug.print("> ", .{});
}
fn promptLong(cpu: *cpu_.Cpu, memory: *mem.Memory) void {
fn modeLine(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),
@ -120,6 +84,7 @@ fn promptLong(cpu: *cpu_.Cpu, memory: *mem.Memory) void {
cpu.registers.get(.ESI),
cpu.registers.get(.EDI),
});
std.debug.print("> ", .{});
}
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 {
if (it.next()) |_| {
std.debug.print(
\\{s}, usage: {s}
\\
, .{ TOO_MANY_ARGUMENTS, SEGMENT_REGISTER_USAGE });
std.debug.print("Wrong number of arguments.\n", .{});
} else {
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");
}
}