open Kestrel open Effect.Deep let devices_deo = Hashtbl.create 256 let devices_dei = Hashtbl.create 256 let register_device (module D : Device.DEVICE) = Device.Int_set.iter (fun port -> Hashtbl.add devices_dei port (module D : Device.DEVICE)) D.dei_ports; Device.Int_set.iter (fun port -> Hashtbl.add devices_deo port (module D : Device.DEVICE)) D.deo_ports module System = Kestrel_varvara.System.Make () module Console = Kestrel_varvara.Console.Make () module Datetime = Kestrel_varvara.Datetime.Make () module File_a = Kestrel_varvara.File.Make (struct let start = 0xa0 end) module File_b = Kestrel_varvara.File.Make (struct let start = 0xb0 end) let run m pc = let dev = Machine.dev m in try Machine.dispatch m pc with | effect Machine.BRK, _ -> () | effect Machine.DEI port, k -> begin try let module Device = (val Hashtbl.find devices_dei port : Device.DEVICE) in continue k (Device.dei m port) with Not_found -> continue k (Bytes.get_uint8 dev port) end | effect Machine.DEI2 port, k -> begin try let module Device = (val Hashtbl.find devices_dei port : Device.DEVICE) in continue k (Device.dei2 m port) with Not_found -> continue k (Util.get_uint16_wrap dev port) end | effect Machine.DEO (port, value), k -> begin try let module Device = (val Hashtbl.find devices_deo port : Device.DEVICE) in Device.deo m port value with Not_found -> () end; continue k () let main () = if Array.length Sys.argv < 2 then ( Printf.eprintf "usage: kestrel file.rom ...\n"; exit 1); register_device (module System : Device.DEVICE); register_device (module Console : Device.DEVICE); register_device (module File_a : Device.DEVICE); register_device (module File_b : Device.DEVICE); register_device (module Datetime : Device.DEVICE); let code = In_channel.with_open_bin Sys.argv.(1) (fun i -> In_channel.input_all i) in In_channel.set_binary_mode stdin true; Out_channel.set_binary_mode stdout true; let mach = Machine.create code in let dev = Machine.dev mach in let has_args = Array.length Sys.argv > 2 in Bytes.set_uint8 dev 0x17 (if has_args then 1 else 0); run mach 0x100; if Console.state.console_vector <> 0 then begin let console_input ch ty = Bytes.set_uint8 dev 0x12 ch; Bytes.set_uint8 dev 0x17 ty; if Bytes.get_uint8 dev 0x0f = 0 then run mach Console.state.console_vector in if has_args then begin for i = 2 to Array.length Sys.argv - 1 do let arg = Sys.argv.(i) in String.iter (fun c -> if Bytes.get_uint8 dev 0x0f = 0 then console_input (Char.code c) 2) arg; if Bytes.get_uint8 dev 0x0f = 0 then console_input 0 (if i = Array.length Sys.argv - 1 then 4 else 3) done end; try while Bytes.get_uint8 dev 0x0f = 0 do match In_channel.input_byte stdin with | None -> raise Exit | Some c -> console_input c 1 done with Exit -> console_input 0 4 end; exit (Bytes.get_uint8 dev 0x0f land 0x7f) let _ = main ()