Improve debug printing
This commit is contained in:
parent
b52a070087
commit
eb98415994
170
src/cpu.ml
170
src/cpu.ml
|
@ -31,19 +31,21 @@ type t = {
|
||||||
}
|
}
|
||||||
|
|
||||||
let print_cpu_state cpu =
|
let print_cpu_state cpu =
|
||||||
printf "Registers:\n";
|
printf "[Cpu state] Registers:\n";
|
||||||
printf " A: 0x%02X F: 0x%02X\n" (int_of_char cpu.reg.a) (int_of_char cpu.reg.f);
|
printf "[Cpu state] \tA: 0x%02X \tF: 0x%02X\n" (int_of_char cpu.reg.a) (int_of_char cpu.reg.f);
|
||||||
printf " B: 0x%02X C: 0x%02X\n" (int_of_char cpu.reg.b) (int_of_char cpu.reg.c);
|
printf "[Cpu state] \tB: 0x%02X \tC: 0x%02X\n" (int_of_char cpu.reg.b) (int_of_char cpu.reg.c);
|
||||||
printf " D: 0x%02X E: 0x%02X\n" (int_of_char cpu.reg.d) (int_of_char cpu.reg.e);
|
printf "[Cpu state] \tD: 0x%02X \tE: 0x%02X\n" (int_of_char cpu.reg.d) (int_of_char cpu.reg.e);
|
||||||
printf " H: 0x%02X L: 0x%02X\n" (int_of_char cpu.reg.h) (int_of_char cpu.reg.l);
|
printf "[Cpu state] \tH: 0x%02X \tL: 0x%02X\n" (int_of_char cpu.reg.h) (int_of_char cpu.reg.l);
|
||||||
printf " PC: 0x%04X\n" cpu.reg.pc;
|
printf "[Cpu state] \tPC: 0x%04X\n" cpu.reg.pc;
|
||||||
printf " SP: 0x%04X\n" cpu.reg.sp;
|
printf "[Cpu state] \tSP: 0x%04X\n" cpu.reg.sp;
|
||||||
|
|
||||||
printf "Flags:\n";
|
printf "[Cpu state] Flags:\n";
|
||||||
printf " Z: %b\n" cpu.flag.z;
|
printf "[Cpu state] \t ZNHC\n";
|
||||||
printf " N: %b\n" cpu.flag.n;
|
let z = if cpu.flag.z then 1 else 0 in
|
||||||
printf " H: %b\n" cpu.flag.h;
|
let n = if cpu.flag.n then 1 else 0 in
|
||||||
printf " C: %b\n" cpu.flag.c
|
let h = if cpu.flag.h then 1 else 0 in
|
||||||
|
let c = if cpu.flag.c then 1 else 0 in
|
||||||
|
printf "[Cpu state] \t[%d%d%d%d]\n" z n h c
|
||||||
|
|
||||||
(** http://bgb.bircd.org/pandocs.htm#powerupsequence *)
|
(** http://bgb.bircd.org/pandocs.htm#powerupsequence *)
|
||||||
let init_registers =
|
let init_registers =
|
||||||
|
@ -136,152 +138,176 @@ let run cpu (mem: Memory.map) =
|
||||||
print_cpu_state cpu;
|
print_cpu_state cpu;
|
||||||
let opcode = read_pc_byte cpu mem |> char_of_int in
|
let opcode = read_pc_byte cpu mem |> char_of_int in
|
||||||
(* Hexa.print_slice cartridge.full_rom cpu.reg.pc (cpu.reg.pc + 7); *)
|
(* Hexa.print_slice cartridge.full_rom cpu.reg.pc (cpu.reg.pc + 7); *)
|
||||||
match opcode with
|
|
||||||
|
let inst = match opcode with
|
||||||
|
|
||||||
(* 8-bit load *)
|
(* 8-bit load *)
|
||||||
(* LD r,(HL) *)
|
(* LD r,(HL) *)
|
||||||
| '\x7E' -> printf " LD \tA, (HL)\n";
|
| '\x7E' -> let inst = sprintf "LD \tA, (HL)" in
|
||||||
let hl = merge_bytes cpu.reg.l cpu.reg.h in
|
let hl = merge_bytes cpu.reg.l cpu.reg.h in
|
||||||
cpu.reg.a <- Memory.get mem hl;
|
cpu.reg.a <- Memory.get mem hl;
|
||||||
inc_cycles cpu 8
|
inc_cycles cpu 8;
|
||||||
|
inst
|
||||||
|
|
||||||
| '\x36' -> let n = read_pc_byte cpu mem in
|
| '\x36' -> let n = read_pc_byte cpu mem in
|
||||||
printf " LD \t(HL) 0x%02X\n" n;
|
let inst = sprintf "LD \t(HL) 0x%02X" n in
|
||||||
let hl = merge_bytes cpu.reg.l cpu.reg.h in
|
let hl = merge_bytes cpu.reg.l cpu.reg.h in
|
||||||
Memory.set mem hl (char_of_int n);
|
Memory.set mem hl (char_of_int n);
|
||||||
inc_cycles cpu 12
|
inc_cycles cpu 12;
|
||||||
|
inst
|
||||||
|
|
||||||
|
|
||||||
(* CPU control *)
|
(* CPU control *)
|
||||||
| '\x00' -> printf " NOP\n";
|
| '\x00' -> let inst = sprintf "NOP" in
|
||||||
inc_cycles cpu 4
|
inc_cycles cpu 4;
|
||||||
|
inst
|
||||||
|
|
||||||
|
|
||||||
(* jump *)
|
(* jump *)
|
||||||
| '\xC9' -> printf " RET\n";
|
| '\x20' -> let n = read_pc_byte cpu mem in
|
||||||
cpu.reg.pc <- pop_stack cpu mem;
|
let inst = sprintf "JR \tNZ, 0x%02X" n in
|
||||||
inc_cycles cpu 16
|
if cpu.flag.z = false then
|
||||||
|
begin
|
||||||
|
inc_pc cpu (n - 1); inc_cycles cpu 12;
|
||||||
|
end else
|
||||||
|
inc_cycles cpu 8;
|
||||||
|
inst
|
||||||
|
|
||||||
| '\xC0' -> printf " RET \tNZ\n";
|
| '\xC9' -> let inst = sprintf "RET" in
|
||||||
|
cpu.reg.pc <- pop_stack cpu mem;
|
||||||
|
inc_cycles cpu 16;
|
||||||
|
inst
|
||||||
|
|
||||||
|
| '\xC0' -> let inst = sprintf "RET \tNZ" in
|
||||||
if cpu.flag.z = false then
|
if cpu.flag.z = false then
|
||||||
begin
|
begin
|
||||||
cpu.reg.pc <- pop_stack cpu mem;
|
cpu.reg.pc <- pop_stack cpu mem;
|
||||||
inc_cycles cpu 20
|
inc_cycles cpu 20;
|
||||||
end else
|
end else
|
||||||
inc_cycles cpu 8
|
inc_cycles cpu 8;
|
||||||
|
inst
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
| '\x03' -> printf " INC \tBC\n";
|
| '\x03' -> let inst = sprintf "INC \tBC" in
|
||||||
inc_BC cpu;
|
inc_BC cpu;
|
||||||
inc_cycles cpu 8
|
inc_cycles cpu 8;
|
||||||
|
inst
|
||||||
|
|
||||||
| '\x05' -> printf " DEC \tB\n";
|
| '\x05' -> let inst = sprintf "DEC \tB" in
|
||||||
let dec = int_of_char(cpu.reg.b) - 1 in
|
let dec = int_of_char(cpu.reg.b) - 1 in
|
||||||
cpu.flag.z <- dec = 0;
|
cpu.flag.z <- dec = 0;
|
||||||
cpu.flag.n <- true;
|
cpu.flag.n <- true;
|
||||||
cpu.flag.h <- dec < 0;
|
cpu.flag.h <- dec < 0;
|
||||||
cpu.reg.b <- char_of_int @@ if dec >= 0 then dec else 0;
|
cpu.reg.b <- char_of_int @@ if dec >= 0 then dec else 0;
|
||||||
inc_cycles cpu 4
|
inc_cycles cpu 4;
|
||||||
|
inst
|
||||||
|
|
||||||
| '\x06' -> let n = read_pc_byte cpu mem in
|
| '\x06' -> let n = read_pc_byte cpu mem in
|
||||||
printf " LD \tB, 0x%02X\n" n;
|
let inst = sprintf "LD \tB, 0x%02X" n in
|
||||||
cpu.reg.b <- char_of_int n;
|
cpu.reg.b <- char_of_int n;
|
||||||
inc_cycles cpu 8
|
inc_cycles cpu 8;
|
||||||
|
inst
|
||||||
|
|
||||||
| '\x18' -> let n = read_pc_byte cpu mem in
|
| '\x18' -> let n = read_pc_byte cpu mem in
|
||||||
printf " JP \t0x%02X\n" n;
|
let inst = sprintf "JP \t0x%02X" n in
|
||||||
inc_pc cpu (n - 1); inc_cycles cpu 12
|
inc_pc cpu (n - 1); inc_cycles cpu 12;
|
||||||
|
inst
|
||||||
| '\x20' -> let n = read_pc_byte cpu mem in
|
|
||||||
printf " JR \tNZ, 0x%02X\n" n;
|
|
||||||
if cpu.flag.z = false then
|
|
||||||
begin
|
|
||||||
inc_pc cpu (n - 1); inc_cycles cpu 12
|
|
||||||
end else
|
|
||||||
begin
|
|
||||||
inc_cycles cpu 8
|
|
||||||
end
|
|
||||||
|
|
||||||
| '\x21' -> let nn = read_pc_2bytes cpu mem in
|
| '\x21' -> let nn = read_pc_2bytes cpu mem in
|
||||||
printf " LD \tHL, 0x%04X\n" nn;
|
let inst = sprintf "LD \tHL, 0x%04X" nn in
|
||||||
let high, low = split_2B nn in
|
let high, low = split_2B nn in
|
||||||
cpu.reg.h <- high;
|
cpu.reg.h <- high;
|
||||||
cpu.reg.l <- low;
|
cpu.reg.l <- low;
|
||||||
inc_cycles cpu 12
|
inc_cycles cpu 12;
|
||||||
|
inst
|
||||||
|
|
||||||
| '\x22' -> printf " LDI \t(HL), A\n";
|
| '\x22' -> let inst = sprintf "LDI \t(HL), A" in
|
||||||
let hl = merge_bytes cpu.reg.l cpu.reg.h in
|
let hl = merge_bytes cpu.reg.l cpu.reg.h in
|
||||||
Memory.set mem hl cpu.reg.a;
|
Memory.set mem hl cpu.reg.a;
|
||||||
let high, low = split_2B (hl + 1) in
|
let high, low = split_2B (hl + 1) in
|
||||||
cpu.reg.h <- high;
|
cpu.reg.h <- high;
|
||||||
cpu.reg.l <- low;
|
cpu.reg.l <- low;
|
||||||
inc_cycles cpu 8
|
inc_cycles cpu 8;
|
||||||
|
inst
|
||||||
|
|
||||||
|
|
||||||
| '\x28' -> let n = read_pc_byte cpu mem in
|
| '\x28' -> let n = read_pc_byte cpu mem in
|
||||||
printf " JR \tZ, 0x%02X\n" n;
|
let inst = sprintf "JR \tZ, 0x%02X" n in
|
||||||
if cpu.flag.z = true then
|
if cpu.flag.z = true then
|
||||||
begin
|
begin
|
||||||
inc_pc cpu (n - 1); inc_cycles cpu 12
|
inc_pc cpu (n - 1); inc_cycles cpu 12;
|
||||||
end else
|
end else
|
||||||
begin
|
inc_cycles cpu 8;
|
||||||
inc_cycles cpu 8
|
inst
|
||||||
end
|
|
||||||
|
|
||||||
| '\x34' -> printf " INC \t(HL)\n";
|
| '\x34' -> let inst = sprintf "INC \t(HL)" in
|
||||||
let hl = merge_bytes cpu.reg.l cpu.reg.h in
|
let hl = merge_bytes cpu.reg.l cpu.reg.h in
|
||||||
let v = Memory.get mem hl |> int_of_char in
|
let v = Memory.get mem hl |> int_of_char in
|
||||||
cpu.flag.z <- v + 1 = 0;
|
cpu.flag.z <- v + 1 = 0;
|
||||||
cpu.flag.n <- false;
|
cpu.flag.n <- false;
|
||||||
cpu.flag.h <- v = 0b00001111;
|
cpu.flag.h <- v = 0b00001111;
|
||||||
Memory.set mem hl (char_of_int (v + 1));
|
Memory.set mem hl (char_of_int (v + 1));
|
||||||
inc_cycles cpu 12
|
inc_cycles cpu 12;
|
||||||
|
inst
|
||||||
|
|
||||||
|
|
||||||
| '\x3E' -> let n = read_pc_byte cpu mem in
|
| '\x3E' -> let n = read_pc_byte cpu mem in
|
||||||
printf " LD \tA, 0x%02X\n" n;
|
let inst = sprintf "LD \tA, 0x%02X" n in
|
||||||
cpu.reg.a <- char_of_int n;
|
cpu.reg.a <- char_of_int n;
|
||||||
inc_cycles cpu 8
|
inc_cycles cpu 8;
|
||||||
|
inst
|
||||||
|
|
||||||
| '\xAF' -> printf " XOR \tA, A\n";
|
| '\xAF' -> let inst = sprintf "XOR \tA, A" in
|
||||||
let int_A = int_of_char cpu.reg.a in
|
let int_A = int_of_char cpu.reg.a in
|
||||||
cpu.reg.a <- char_of_int @@ int_A lxor int_A;
|
cpu.reg.a <- char_of_int @@ int_A lxor int_A;
|
||||||
inc_cycles cpu 4
|
inc_cycles cpu 4;
|
||||||
|
inst
|
||||||
|
|
||||||
| '\xC3' -> let addr = read_pc_2bytes cpu mem in
|
| '\xC3' -> let addr = read_pc_2bytes cpu mem in
|
||||||
printf " JP \t0x%04X\n" addr;
|
let inst = sprintf "JP \t0x%04X" addr in
|
||||||
cpu.reg.pc <- addr; inc_cycles cpu 16
|
cpu.reg.pc <- addr; inc_cycles cpu 16;
|
||||||
|
inst
|
||||||
|
|
||||||
| '\xE0' -> let n = read_pc_byte cpu mem in
|
| '\xE0' -> let n = read_pc_byte cpu mem in
|
||||||
printf " LDH \t(0xFF%02X), A\n" n;
|
let inst = sprintf "LDH \t(0xFF%02X), A" n in
|
||||||
Memory.set mem (0xFF00 + n) cpu.reg.a;
|
Memory.set mem (0xFF00 + n) cpu.reg.a;
|
||||||
inc_cycles cpu 12
|
inc_cycles cpu 12;
|
||||||
|
inst
|
||||||
|
|
||||||
| '\xEA' -> let addr = read_pc_2bytes cpu mem in
|
| '\xEA' -> let addr = read_pc_2bytes cpu mem in
|
||||||
printf " LD \t(0X%04X), A\n" addr;
|
let inst = sprintf "LD \t(0X%04X), A" addr in
|
||||||
Memory.set mem addr cpu.reg.a;
|
Memory.set mem addr cpu.reg.a;
|
||||||
inc_cycles cpu 16
|
inc_cycles cpu 16;
|
||||||
|
inst
|
||||||
|
|
||||||
|
|
||||||
| '\xF0' -> let n = read_pc_byte cpu mem in
|
| '\xF0' -> let n = read_pc_byte cpu mem in
|
||||||
printf " LDH \tA, (0xFF%02X)\n" n;
|
let inst = sprintf "LDH \tA, (0xFF%02X)" n in
|
||||||
cpu.reg.a <- Memory.get mem (0xFF00 + n);
|
cpu.reg.a <- Memory.get mem (0xFF00 + n);
|
||||||
inc_cycles cpu 12
|
inc_cycles cpu 12;
|
||||||
|
inst
|
||||||
|
|
||||||
| '\xF3' -> printf " DI\n";
|
| '\xF3' -> let inst = sprintf "DI" in
|
||||||
(* fixme *)
|
(* fixme *)
|
||||||
inc_cycles cpu 4
|
inc_cycles cpu 4;
|
||||||
|
inst
|
||||||
|
|
||||||
| '\xFE' -> let n = read_pc_byte cpu mem in
|
| '\xFE' -> let n = read_pc_byte cpu mem in
|
||||||
printf " CP \t0x%02X\n" n;
|
let inst = sprintf "CP \t0x%02X" n in
|
||||||
cmp_A cpu n;
|
cmp_A cpu n;
|
||||||
inc_cycles cpu 8
|
inc_cycles cpu 8;
|
||||||
|
inst
|
||||||
|
|
||||||
| '\xFB' -> printf " EI\n";
|
| '\xFB' -> let inst = sprintf "EI" in
|
||||||
(* enable interrupts, IME=1 *)
|
(* enable interrupts, IME=1 *)
|
||||||
inc_cycles cpu 4
|
inc_cycles cpu 4;
|
||||||
|
inst
|
||||||
|
|
||||||
| x -> eprintf "opcode 0x%02X\n" (int_of_char x);
|
| x -> eprintf "opcode 0x%02X\n" (int_of_char x);
|
||||||
eprintf "#cycles: %d\n" cpu.cycles;
|
eprintf "#cycles: %d\n" cpu.cycles;
|
||||||
failwith "Unimplemented opcode."
|
failwith "Unimplemented opcode."
|
||||||
|
|
||||||
|
in
|
||||||
|
|
||||||
|
printf "[Instruction] %s\n" inst
|
||||||
|
|
Loading…
Reference in a new issue