Skip to content

Commit

Permalink
Implement browser UI via js_of_ocaml (using Brr). Runs!! But is super…
Browse files Browse the repository at this point in the history
… slow..
  • Loading branch information
linoscope committed Oct 24, 2021
1 parent eaed1b7 commit ac04c5d
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 10 deletions.
File renamed without changes.
5 changes: 2 additions & 3 deletions bin/main.ml → bin_sdl2/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ let render_framebuffer ~texture ~renderer ~fb =
Sdl.render_present renderer

let () =
Printexc.record_backtrace true;
let rom_bytes = Read_rom_file.f "./resource/private/pokemon-aka.gb" in
(* let rom_bytes = Read_rom_file.f "./resource/private/tobu.gb" in *)
(* let rom_bytes = Read_rom_file.f "./resource/private/pokemon-aka.gb" in *)
let rom_bytes = Read_rom_file.f "./resource/private/tobu.gb" in
(* let rom_bytes = Read_rom_file.f "./resource/test_roms/blargg/instr_timing/instr_timing.gb" in *)
(* let rom_bytes = Read_rom_file.f "./resource/test_roms/mooneye/bits_bank2.gb" in *)
(* let rom_bytes = Read_rom_file.f "./resource/test_roms/blargg/cpu_instrs/individual/02-interrupts.gb" in *)
Expand Down
14 changes: 14 additions & 0 deletions bin_web/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(executables
(names main)
(libraries brr bigstringaf js_of_ocaml-lwt camlboy_lib)
(link_flags (:standard -no-check-prims))
(modes js))

(rule
(targets main.js)
(deps main.bc.js)
(action (run cp %{deps} %{targets})))

(alias
(name app)
(deps main.js main.html))
13 changes: 13 additions & 0 deletions bin_web/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<script type="text/javascript" defer="defer" src="main.js"></script>
<title>CAMLBOY</title>
</head>
<body>
<canvas class="game" width="160" height="144"></canvas>
<noscript>Sorry, you need to enable JavaScript to see this page.</noscript>
</body>
</html>
89 changes: 89 additions & 0 deletions bin_web/main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
open Camlboy_lib
open Brr
open Brr_canvas

let gb_w = 160
let gb_h = 144

let canvas_id = "screen"

let draw_framebuffer ctx image_data fb =
let d = C2d.Image_data.data image_data in
for y = 0 to gb_h - 1 do
for x = 0 to gb_w - 1 do
let off = 4 * (y * gb_w + x) in
match fb.(y).(x) with
| `White ->
Tarray.set d (off ) 0xFF;
Tarray.set d (off + 1) 0xFF;
Tarray.set d (off + 2) 0xFF;
Tarray.set d (off + 3) 0xFF;
| `Light_gray ->
Tarray.set d (off ) 0xAA;
Tarray.set d (off + 1) 0xAA;
Tarray.set d (off + 2) 0xAA;
Tarray.set d (off + 3) 0xFF;
| `Dark_gray ->
Tarray.set d (off ) 0x77;
Tarray.set d (off + 1) 0x77;
Tarray.set d (off + 2) 0x77;
Tarray.set d (off + 3) 0xFF;
| `Black ->
Tarray.set d (off ) 0x00;
Tarray.set d (off + 1) 0x00;
Tarray.set d (off + 2) 0x00;
Tarray.set d (off + 3) 0xFF;
done
done;
C2d.put_image_data ctx image_data ~x:0 ~y:0

let load_rom_button ctx image_data =
let on_change i =
let file = El.Input.files i |> List.hd in
let blob = File.as_blob file in
let buf_fut = Blob.array_buffer blob in
Fut.await buf_fut (function
| Ok buf ->
let rom_bytes =
Tarray.of_buffer Uint8 buf
|> Tarray.to_bigarray1
(* Convert uint8 bigarray to char bigarray *)
|> Obj.magic
in
let cartridge = Detect_cartridge.f ~rom_bytes in
let module C = Camlboy.Make(val cartridge) in
let t = C.create_with_rom ~print_serial_port:true ~rom_bytes in
Console.profile (Jstr.v "foo");
let rec run_instr () =
begin match C.run_instruction t with
| In_frame ->
run_instr ()
| Frame_ended fb ->
draw_framebuffer ctx image_data fb;
end;
in
ignore @@ G.set_interval ~ms:10 run_instr
| Error e ->
Console.(log [Jv.Error.message e])
)
in
let i = El.input ~at:At.[type' (Jstr.v "file")] () in
let b = El.button [ El.txt' "Load Rom" ] in
El.set_inline_style El.Style.display (Jstr.v "none") i;
Ev.listen Ev.click (fun _ -> El.click i) (El.as_target b);
Ev.listen Ev.change (fun _ -> on_change i) (El.as_target i);
El.span [i; b]

let () =
let cnv = Canvas.create ~w:gb_w ~h:gb_h ~at:At.[id (Jstr.v canvas_id)] [] in
let ctx = C2d.create cnv in
let image_data = C2d.create_image_data ctx ~w:gb_w ~h:gb_h in
let fb = Array.make_matrix gb_h gb_w `Dark_gray in
let fb2 = Array.make_matrix gb_h gb_w `Black in
draw_framebuffer ctx image_data fb;
draw_framebuffer ctx image_data fb2;
draw_framebuffer ctx image_data fb;
El.set_children (Document.body G.document) [
Canvas.to_el cnv;
load_rom_button ctx image_data;
]
14 changes: 7 additions & 7 deletions test/unit_tests/test_cartridge_header.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@ let f (rom_file_name : string) =

let create file =
let rom_bytes = f file in
Cartridge_header2.create ~rom_bytes
Cartridge_header.create ~rom_bytes

let%expect_test "test rom only" =
let t = create "../../resource/test_roms/hello.gb" in

t
|> Cartridge_header2.get_cartridge_type
|> Cartridge_header.get_cartridge_type
|> Cartridge_type.show
|> print_endline;

t
|> Cartridge_header2.get_rom_bank_count
|> Cartridge_header.get_rom_bank_count
|> Printf.printf "%d\n";

t
|> Cartridge_header2.get_ram_bank_count
|> Cartridge_header.get_ram_bank_count
|> Printf.printf "%d\n";

[%expect {|
Expand All @@ -35,16 +35,16 @@ let%expect_test "test mbc1" =
let t = create "../../resource/test_roms/blargg/cpu_instrs/cpu_instrs.gb" in

t
|> Cartridge_header2.get_cartridge_type
|> Cartridge_header.get_cartridge_type
|> Cartridge_type.show
|> print_endline;

t
|> Cartridge_header2.get_rom_bank_count
|> Cartridge_header.get_rom_bank_count
|> Printf.printf "%d\n";

t
|> Cartridge_header2.get_ram_bank_count
|> Cartridge_header.get_ram_bank_count
|> Printf.printf "%d\n";

[%expect {|
Expand Down

0 comments on commit ac04c5d

Please sign in to comment.