@@ -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+
96122export 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