Fix signed integer parsing

This commit is contained in:
Fabien Freling 2015-08-10 01:35:47 +02:00
parent eb98415994
commit a2f5b2e08a
4 changed files with 34 additions and 2 deletions

View file

@ -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

1
_tags
View file

@ -1 +1,2 @@
true: package(unix), package(graphics), warn(A-40-42)
<test/*>: package(ounit)

View file

@ -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

19
test/test_cpu.ml Normal file
View file

@ -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