Skip to content

Commit 254c80c

Browse files
refactor a lil bit
1 parent c587047 commit 254c80c

2 files changed

Lines changed: 36 additions & 46 deletions

File tree

src/exec.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
const std = @import("std");
22
const Cpu = @import("cpu.zig").Cpu;
3-
const Memory = @import("memory.zig").Memory;
3+
const Memory = @import("memory.zig");
44
const Instruction = @import("instruction.zig").Instruction;
55
const Register = @import("cpu.zig").Register;
66
const LabelTable = @import("labels.zig").LabelTable;
77

88
const builtin = @import("builtin");
99

10-
pub fn execute(instr: Instruction, cpu: *Cpu, mem: *Memory, labels: *const LabelTable) void {
10+
pub fn execute(instr: Instruction, cpu: *Cpu, mem: *Memory.Memory, labels: *const LabelTable) void {
1111
switch (instr) {
1212
.Add => |i| cpu.regs[i.rd] = cpu.regs[i.rs] + cpu.regs[i.rt],
1313
.Addi => |i| cpu.regs[i.rt] = cpu.regs[i.rs] +% @as(u32, @bitCast(@as(i32, i.imm))),
@@ -70,7 +70,7 @@ var stdin_reader = std.fs.File.stdin().reader(&stdin_buffer);
7070
const stdin = &stdin_reader.interface;
7171

7272
// table of syscall handlers
73-
fn handleSyscall(cpu: *Cpu, mem: *Memory) void {
73+
fn handleSyscall(cpu: *Cpu, mem: *Memory.Memory) void {
7474
const v0 = cpu.regs[@intFromEnum(Register.v0)];
7575
const a0 = cpu.regs[@intFromEnum(Register.a0)];
7676
switch (v0) {
@@ -82,7 +82,7 @@ fn handleSyscall(cpu: *Cpu, mem: *Memory) void {
8282
4 => { // print_str
8383
var addr = a0;
8484
while (true) {
85-
const c = mem.data[(addr - @import("memory.zig").DATA_START)];
85+
const c = mem.data[(addr - Memory.DATA_START)];
8686
if (c == 0) {
8787
break;
8888
}

src/wasm.zig

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,32 @@ pub fn handleSyscallWasm(cpu_ptr: *Cpu, mem_ptr: *Memory) void {
9393
}
9494
}
9595

96+
pub fn runUntilBlockOrExit() ?i32 {
97+
while (true) {
98+
if (cpu.pc < TEXT_START) break;
99+
const pc_offset = cpu.pc - TEXT_START;
100+
const instr_idx = pc_offset / 4;
101+
102+
if (instr_idx >= instructions_list.items.len) break;
103+
104+
const instr = instructions_list.items[instr_idx];
105+
const old_pc = cpu.pc;
106+
107+
executeInstruction(instr, &cpu, &mem, &parsed_labels);
108+
109+
if (waiting_for_input) {
110+
// save state and return, will continue from next instruction
111+
cpu.pc += 4;
112+
return 1; // signal that we're waiting for input
113+
}
114+
115+
if (cpu.pc == old_pc) {
116+
cpu.pc += 4;
117+
}
118+
}
119+
return null;
120+
}
121+
96122
export fn run(code_ptr: [*]const u8, code_len: usize) i32 {
97123
init();
98124

@@ -117,27 +143,9 @@ export fn run(code_ptr: [*]const u8, code_len: usize) i32 {
117143
};
118144
}
119145

120-
while (true) {
121-
if (cpu.pc < TEXT_START) break;
122-
const pc_offset = cpu.pc - TEXT_START;
123-
const instr_idx = pc_offset / 4;
124-
125-
if (instr_idx >= instructions_list.items.len) break;
126-
127-
const instr = instructions_list.items[instr_idx];
128-
const old_pc = cpu.pc;
129-
130-
executeInstruction(instr, &cpu, &mem, &parsed_labels);
131-
132-
if (waiting_for_input) {
133-
// save state and return, will continue from next instruction
134-
cpu.pc += 4;
135-
return 1; // signal that we're waiting for input
136-
}
137-
138-
if (cpu.pc == old_pc) {
139-
cpu.pc += 4;
140-
}
146+
const result = runUntilBlockOrExit();
147+
if (result) |r| {
148+
return r;
141149
}
142150

143151
allocator.free(parsed.text);
@@ -151,27 +159,9 @@ export fn continueAfterInput() i32 {
151159
cpu.regs[@intFromEnum(Register.v0)] = @bitCast(value);
152160
waiting_for_input = false;
153161

154-
// continue execution from where we left off
155-
while (true) {
156-
if (cpu.pc < TEXT_START) break;
157-
const pc_offset = cpu.pc - TEXT_START;
158-
const instr_idx = pc_offset / 4;
159-
160-
if (instr_idx >= instructions_list.items.len) break;
161-
162-
const instr = instructions_list.items[instr_idx];
163-
const old_pc = cpu.pc;
164-
165-
executeInstruction(instr, &cpu, &mem, &parsed_labels);
166-
167-
if (waiting_for_input) {
168-
cpu.pc += 4;
169-
return 1;
170-
}
171-
172-
if (cpu.pc == old_pc) {
173-
cpu.pc += 4;
174-
}
162+
const result = runUntilBlockOrExit();
163+
if (result) |r| {
164+
return r;
175165
}
176166

177167
instructions_list.deinit(allocator);

0 commit comments

Comments
 (0)