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

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