From a2f5b2e08ad0e11aef1d116be9bcf8c8bc1559a0 Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Mon, 10 Aug 2015 01:35:47 +0200 Subject: [PATCH] Fix signed integer parsing --- Makefile | 3 +++ _tags | 1 + src/cpu.ml | 13 +++++++++++-- test/test_cpu.ml | 19 +++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 test/test_cpu.ml diff --git a/Makefile b/Makefile index 2788214..40d7f17 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,9 @@ all: debug: ocamlbuild -use-ocamlfind -I src oboy.d.byte +check: + ocamlbuild -use-ocamlfind -I src -lflag -g test/test_cpu.byte -- + clean: rm -rf _build rm -f *.byte diff --git a/_tags b/_tags index 46fd50f..46e4883 100644 --- a/_tags +++ b/_tags @@ -1 +1,2 @@ true: package(unix), package(graphics), warn(A-40-42) +: package(ounit) diff --git a/src/cpu.ml b/src/cpu.ml index 102c11d..784b988 100644 --- a/src/cpu.ml +++ b/src/cpu.ml @@ -127,6 +127,13 @@ let pop_stack cpu mem = cpu.reg.sp <- (cpu.reg.sp + 1) mod 0xFFFF; merge_bytes low high +let two_complement n = + (lnot n) + 1 + +let signed_byte n = + if n land 0x80 <> 0 then -((two_complement n) land 0xFF) + else n + (** @@ -165,10 +172,12 @@ let run cpu (mem: Memory.map) = (* jump *) | '\x20' -> let n = read_pc_byte cpu mem in - let inst = sprintf "JR \tNZ, 0x%02X" n in + let s = signed_byte n in + let inst = sprintf "JR \tNZ, 0x%02X (%d)" n s in if cpu.flag.z = false then begin - inc_pc cpu (n - 1); inc_cycles cpu 12; + inc_pc cpu (s - 2); (* we read 2 bytes from PC, opcode and n *) + inc_cycles cpu 12; end else inc_cycles cpu 8; inst diff --git a/test/test_cpu.ml b/test/test_cpu.ml new file mode 100644 index 0000000..ea6790b --- /dev/null +++ b/test/test_cpu.ml @@ -0,0 +1,19 @@ +open OUnit2 + + +let test_signed test_ctx = + let n = 0xFA in + let s = Cpu.signed_byte n in + assert_equal s (-6) + + +(* Name the test cases and group them together *) +let suite = +"suite">::: + [ + "test signed">:: test_signed + ] + +let () = + run_test_tt_main suite +