adding writing memory
This commit is contained in:
parent
2f16975be7
commit
a96ff208bd
2 changed files with 32 additions and 4 deletions
30
src/main.zig
30
src/main.zig
|
@ -4,7 +4,11 @@ const mem = @import("memory.zig");
|
|||
pub fn main() !void {
|
||||
var memory = mem.Memory.init();
|
||||
var input = std.io.getStdIn().reader();
|
||||
std.debug.print("MI-HA-RU (386) REPL\nType 'exit' to quit.\n", .{});
|
||||
std.debug.print(
|
||||
\\MI-HA-RU (386) REPL
|
||||
\\Type 'exit' to quit.
|
||||
\\
|
||||
, .{});
|
||||
var buffer: [256]u8 = undefined;
|
||||
while (true) {
|
||||
std.debug.print("> ", .{});
|
||||
|
@ -25,8 +29,28 @@ pub fn main() !void {
|
|||
|
||||
fn handleMemoryCommand(memory: *mem.Memory, it: anytype) !void {
|
||||
if (it.next()) |next_argument| {
|
||||
const addr = try std.fmt.parseUnsigned(u8, next_argument, 10);
|
||||
if (std.mem.eql(u8, next_argument, "read")) {
|
||||
if (it.next()) |address| {
|
||||
const addr = try std.fmt.parseUnsigned(u8, address, 10);
|
||||
const value = try memory.readByte(addr);
|
||||
std.debug.print("{}\n", .{value});
|
||||
std.debug.print(
|
||||
\\MEM @ {}: {}
|
||||
\\
|
||||
, .{ addr, value });
|
||||
}
|
||||
} else if (std.mem.eql(u8, next_argument, "write")) {
|
||||
const maybe_addr = it.next();
|
||||
const maybe_value = it.next();
|
||||
if (maybe_addr == null or maybe_value == null) {
|
||||
std.debug.print(
|
||||
\\Invalid arguments. Usage: mem write <addr> <value>
|
||||
\\
|
||||
, .{});
|
||||
} else {
|
||||
const addr = try std.fmt.parseUnsigned(u8, maybe_addr.?, 10);
|
||||
const value = try std.fmt.parseUnsigned(u8, maybe_value.?, 10);
|
||||
_ = try memory.writeByte(addr, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,4 +12,8 @@ pub const Memory = struct {
|
|||
pub fn readByte(self: *Memory, addr: u8) !u8 {
|
||||
return self.data[addr];
|
||||
}
|
||||
|
||||
pub fn writeByte(self: *Memory, addr: u8, value: u8) !void {
|
||||
self.data[addr] = value;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue