Add CPU instructions.
This commit is contained in:
parent
763c81a1e2
commit
c8b231d48a
29
src/cpu.ml
29
src/cpu.ml
|
@ -112,6 +112,15 @@ let run cpu (mem: Memory.map) =
|
||||||
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
|
match opcode with
|
||||||
|
|
||||||
|
(* 8-bit load *)
|
||||||
|
(* LD r,(HL) *)
|
||||||
|
| '\x7E' -> printf " LD \tA, (HL)\n";
|
||||||
|
let hl = merge_bytes cpu.reg.l cpu.reg.h in
|
||||||
|
cpu.reg.a <- Memory.get mem hl;
|
||||||
|
inc_cycles cpu 8;
|
||||||
|
|
||||||
|
|
||||||
| '\x00' -> printf " NOP\n";
|
| '\x00' -> printf " NOP\n";
|
||||||
inc_cycles cpu 4
|
inc_cycles cpu 4
|
||||||
|
|
||||||
|
@ -172,6 +181,15 @@ let run cpu (mem: Memory.map) =
|
||||||
inc_cycles cpu 8
|
inc_cycles cpu 8
|
||||||
end
|
end
|
||||||
|
|
||||||
|
| '\x34' -> printf " INC \t(HL)\n";
|
||||||
|
let hl = merge_bytes cpu.reg.l cpu.reg.h in
|
||||||
|
let v = Memory.get mem hl |> int_of_char in
|
||||||
|
cpu.flag.z <- v + 1 = 0;
|
||||||
|
cpu.flag.n <- false;
|
||||||
|
cpu.flag.h <- v = 0b00001111;
|
||||||
|
Memory.set mem hl (char_of_int (v + 1));
|
||||||
|
inc_cycles cpu 12
|
||||||
|
|
||||||
|
|
||||||
| '\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;
|
printf " LD \tA, 0x%02X\n" n;
|
||||||
|
@ -200,6 +218,12 @@ let run cpu (mem: Memory.map) =
|
||||||
Memory.set mem (0xFF00 + n) cpu.reg.a;
|
Memory.set mem (0xFF00 + n) cpu.reg.a;
|
||||||
inc_cycles cpu 12
|
inc_cycles cpu 12
|
||||||
|
|
||||||
|
| '\xEA' -> let addr = read_pc_2bytes cpu mem in
|
||||||
|
printf " LD \t(0X%04X), A\n" addr;
|
||||||
|
Memory.set mem addr cpu.reg.a;
|
||||||
|
inc_cycles cpu 16
|
||||||
|
|
||||||
|
|
||||||
| '\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;
|
printf " LDH \tA, (0xFF%02X)\n" n;
|
||||||
cpu.reg.a <- Memory.get mem (0xFF00 + n);
|
cpu.reg.a <- Memory.get mem (0xFF00 + n);
|
||||||
|
@ -214,5 +238,10 @@ let run cpu (mem: Memory.map) =
|
||||||
cmp_A cpu n;
|
cmp_A cpu n;
|
||||||
inc_cycles cpu 8
|
inc_cycles cpu 8
|
||||||
|
|
||||||
|
| '\xFB' -> printf " EI\n";
|
||||||
|
(* enable interrupts, IME=1 *)
|
||||||
|
inc_cycles cpu 4
|
||||||
|
|
||||||
| 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;
|
||||||
failwith "Unimplemented opcode."
|
failwith "Unimplemented opcode."
|
||||||
|
|
Loading…
Reference in a new issue