oboy/src/core/main.ml

74 lines
1.9 KiB
OCaml
Raw Normal View History

2016-02-02 21:25:53 +01:00
(**
* Copyright (c) 2015, Fabien Freling
* All rights reserved.
*
* This source code is licensed under the BSD 2-clause license found in the
* LICENSE file at the top level directory of this source tree.
*)
2015-02-23 16:10:40 +01:00
open Printf
2015-08-17 22:26:11 +02:00
let fps = 60
let cycles_per_frame = Cpu.frequence / fps
2016-01-23 13:16:18 +01:00
let rec run (cpu: Cpu.t) (mem: Memory.t) (screen: Screen.t) =
2015-08-17 22:26:11 +02:00
let start = Unix.gettimeofday () in
printf "start %f\n" start;
2016-01-23 13:16:18 +01:00
let rec run_for cpu (mem: Memory.t) cycles_remaining =
2016-03-01 00:22:56 +01:00
if cycles_remaining > 0 then begin
2016-01-23 13:16:18 +01:00
printf "\n";
let inst, cycles = Cpu.run cpu mem.map in
2015-08-17 22:26:11 +02:00
printf "[Instruction] %s\n" inst;
2016-01-23 13:16:18 +01:00
2016-03-01 23:15:56 +01:00
Cpu.handle_interrupts cpu mem.map;
2016-01-23 13:16:18 +01:00
Memory.update_timers mem cycles;
2016-02-29 23:58:29 +01:00
2015-08-17 22:26:11 +02:00
run_for cpu mem (cycles_remaining - cycles)
2016-03-01 00:22:56 +01:00
end
2015-08-17 22:26:11 +02:00
in
run_for cpu mem cycles_per_frame;
let stop = Unix.gettimeofday () in
let delay = (1. -. (stop -. start)) /. (float_of_int fps) in
printf "stop %f\n" stop;
printf "delta %f\n" (stop -. start);
printf "delay %f\n" delay;
flush_all ();
if delay > 0. then Thread.delay delay;
2015-08-11 23:06:32 +02:00
run cpu mem screen
2015-02-23 16:10:40 +01:00
(** Power up sequence
http://bgb.bircd.org/pandocs.htm#powerupsequence *)
let power_up cartridge =
2018-12-28 17:20:48 +01:00
(* Nintendo logo scrolling *)
if not (Cartridge.check_nintendo_logo cartridge)
then print_endline "Invalid ROM."
2015-02-23 16:10:40 +01:00
else
print_endline "Valid ROM.";
2015-03-05 23:00:28 +01:00
Cartridge.print_info cartridge;
let cpu = Cpu.init_cpu in
2015-03-22 13:56:32 +01:00
let mem = Memory.init cartridge in
2015-08-11 23:06:32 +02:00
let screen = Screen.init in
2015-08-17 22:26:11 +02:00
(*Graphics.open_graph "";
2015-08-11 23:06:32 +02:00
Graphics.resize_window Screen.width Screen.height;
2015-08-17 22:26:11 +02:00
*)
2015-08-11 23:06:32 +02:00
run cpu mem screen
2015-02-23 16:10:40 +01:00
2019-05-12 11:17:17 +02:00
let () =
2016-03-01 00:22:56 +01:00
if Array.length Sys.argv < 2 then begin
prerr_endline "Please specify a ROM.";
eprintf "Usage: %s path/to/rom\n" Sys.argv.(0);
exit 1;
2016-03-01 00:22:56 +01:00
end;
let cartridge = Cartridge.read_cartridge Sys.argv.(1) in
match cartridge with
| None -> print_endline "Invalid ROM file."
2019-05-12 11:17:17 +02:00
| Some c -> power_up c