From 4df4aea484ef97e861d579f5535698a39b90acc8 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Sun, 28 Feb 2016 23:41:06 +0100 Subject: [PATCH] Add Interrupt module --- Makefile | 1 + src/interrupt.ml | 39 +++++++++++++++++++++++++++++++++++++++ test/test_bit.ml | 2 +- test/test_interrupt.ml | 30 ++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/interrupt.ml create mode 100644 test/test_interrupt.ml diff --git a/Makefile b/Makefile index 68abafa..709bb2e 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ debug: check: ocamlbuild -use-ocamlfind -I src -lflag -g test/test_bit.byte -- + ocamlbuild -use-ocamlfind -I src -lflag -g test/test_interrupt.byte -- clean: rm -rf _build diff --git a/src/interrupt.ml b/src/interrupt.ml new file mode 100644 index 0000000..b175557 --- /dev/null +++ b/src/interrupt.ml @@ -0,0 +1,39 @@ +(** + * Copyright (c) 2016, 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. + *) + +(* let IME = ref false;; *) + +type t = + | V_Blank + | LCD_Stat + | Timer + | Serial + | Joypad + +let get_flags byte = + + let nth_interrupt i = + match i with + | 0 -> V_Blank + | 1 -> LCD_Stat + | 2 -> Timer + | 3 -> Serial + | 4 -> Joypad + | _ -> failwith "Invalid interrupt index." in + + let rec get_flag byte i accu = + match i with + | 0 | 1 | 2 | 3 | 4 -> + if Bit.is_set byte i then + let interrupt = nth_interrupt i in + get_flag byte (i + 1) (interrupt :: accu) + else + get_flag byte (i + 1) accu + | _ -> accu in + + get_flag byte 0 [] diff --git a/test/test_bit.ml b/test/test_bit.ml index a934154..d5beade 100644 --- a/test/test_bit.ml +++ b/test/test_bit.ml @@ -1,7 +1,7 @@ open OUnit2 -let test_signed test_ctx = +let test_signed _ = let n = 0xFA in let s = Bit.signed_byte n in assert_equal s (-6) diff --git a/test/test_interrupt.ml b/test/test_interrupt.ml new file mode 100644 index 0000000..7b37c63 --- /dev/null +++ b/test/test_interrupt.ml @@ -0,0 +1,30 @@ +open OUnit2 + + +let test_interrupt _ = + let input_refs = [ + ( 0b00000000, [] ); + ( 0b00000001, [ Interrupt.V_Blank ] ); + ( 0b00000010, [ Interrupt.LCD_Stat ] ); + ( 0b00000100, [ Interrupt.Timer ] ); + ( 0b00001000, [ Interrupt.Serial ] ); + ( 0b00010000, [ Interrupt.Joypad ] ); + ] in + + let test = + function ( byte, ref_flags ) -> + let computed_flags = Interrupt.get_flags byte in + List.iter2 (fun f1 f2 -> assert_equal f1 f2) ref_flags computed_flags in + + List.iter test input_refs + + +(* Name the test cases and group them together *) +let suite = +"suite">::: + [ + "test interrupt parsing">:: test_interrupt + ] + +let () = + run_test_tt_main suite