This commit is contained in:
Lobo 2025-11-27 16:53:43 -03:00
parent ad589d2894
commit 0c0bcb7e73
3 changed files with 19 additions and 20 deletions

View file

@ -6,7 +6,9 @@ let print_stack ?(name = "wst") (Machine.Stack stack) =
let stack = Bytes.to_seq stack.data |> Seq.take stack.sp |> Bytes.of_seq in
Fmt.epr "@[%s: [@[%a@]]@]@." name (Fmt.on_bytes (Fmt.octets ())) stack
let print_instruction i pc = Fmt.epr "%6s (PC = %04x)@." (Instr.to_string i) pc
let print_instruction i pc =
Fmt.epr "%6s (%02x) (PC = %04x)@." (Instr.to_string i) (Instr.to_int i) pc
let debug = Option.is_some (Sys.getenv_opt "DBG")
let console_vector = ref 0
@ -22,29 +24,27 @@ let dispatch =
else Machine.dispatch ~dbg:None
let eval m pc =
let console_input mach ch ty k =
Bytes.set_uint8 (Machine.dev mach) 0x12 ch;
Bytes.set_uint8 (Machine.dev mach) 0x17 ty;
if !console_vector != 0 && Bytes.get_uint8 (Machine.dev mach) 0x0f = 0 then
continue k !console_vector
let dev = Machine.dev m in
let continue_with_console ch ty k =
Bytes.set_uint8 dev 0x12 ch;
Bytes.set_uint8 dev 0x17 ty;
if !console_vector != 0 && Bytes.get_uint8 dev 0x0f = 0 then (
Fmt.epr "Continuing with console vector!\n";
continue k !console_vector)
in
try dispatch m pc with
| effect Machine.BRK, k ->
if !console_vector != 0 then (
| effect Machine.BRK, _ when Bytes.get_uint8 dev 0x0f != 0 -> ()
| effect Machine.BRK, k -> (
if !console_vector != 0 then
try
while Bytes.get_uint8 (Machine.dev m) 0x0f = 0 do
while Bytes.get_uint8 dev 0x0f = 0 do
match In_channel.input_char stdin with
| None -> raise Exit
| Some c -> console_input m (Char.code c) 1 k
| Some c -> continue_with_console (Char.code c) 1 k
done
with Exit ->
Bytes.set_uint8 (Machine.dev m) 0x12 0;
Bytes.set_uint8 (Machine.dev m) 0x17 4;
continue k !console_vector)
| effect Machine.DEI port, k ->
continue k (Bytes.get_uint8 (Machine.dev m) port)
| effect Machine.DEI2 port, k ->
continue k (Util.get_uint16_wrap (Machine.dev m) port)
with Exit -> continue_with_console 0 4 k)
| effect Machine.DEI port, k -> continue k (Bytes.get_uint8 dev port)
| effect Machine.DEI2 port, k -> continue k (Util.get_uint16_wrap dev port)
| effect Machine.DEO (port, value), k ->
(match port with
| 0x10 -> console_vector := value
@ -69,7 +69,6 @@ let main () =
Out_channel.set_binary_mode stdout true;
let mach = Machine.create code in
Bytes.set_uint8 (Machine.dev mach) 0x17 0;
eval mach 0x100;
if debug then (