Add CPU instructions.
This commit is contained in:
parent
7805e50062
commit
763c81a1e2
31
src/cpu.ml
31
src/cpu.ml
|
@ -119,12 +119,25 @@ let run cpu (mem: Memory.map) =
|
|||
inc_BC cpu;
|
||||
inc_cycles cpu 8
|
||||
|
||||
| '\x05' -> printf " DEC \tB\n";
|
||||
let dec = int_of_char(cpu.reg.b) - 1 in
|
||||
cpu.flag.z <- dec = 0;
|
||||
cpu.flag.n <- true;
|
||||
cpu.flag.h <- dec < 0;
|
||||
cpu.reg.b <- char_of_int @@ if dec >= 0 then dec else 0;
|
||||
inc_cycles cpu 4
|
||||
|
||||
| '\x06' -> let n = read_pc_byte cpu mem in
|
||||
printf " LD \tB, 0x%02X\n" n;
|
||||
cpu.reg.b <- char_of_int n;
|
||||
inc_cycles cpu 8
|
||||
|
||||
| '\x18' -> let n = read_pc_byte cpu mem in
|
||||
printf " JP\t 0x%02X\n" n;
|
||||
printf " JP \t0x%02X\n" n;
|
||||
inc_pc cpu (n - 1); inc_cycles cpu 12
|
||||
|
||||
| '\x20' -> let n = read_pc_byte cpu mem in
|
||||
printf " JR\t NZ, 0x%02X\n" n;
|
||||
printf " JR \tNZ, 0x%02X\n" n;
|
||||
if cpu.flag.z = false then
|
||||
begin
|
||||
inc_pc cpu (n - 1); inc_cycles cpu 12
|
||||
|
@ -134,15 +147,23 @@ let run cpu (mem: Memory.map) =
|
|||
end
|
||||
|
||||
| '\x21' -> let nn = read_pc_2bytes cpu mem in
|
||||
printf " LD\t HL, 0x%04X\n" nn;
|
||||
printf " LD \tHL, 0x%04X\n" nn;
|
||||
let high, low = split_2B nn in
|
||||
cpu.reg.h <- high;
|
||||
cpu.reg.l <- low;
|
||||
inc_cycles cpu 12
|
||||
|
||||
| '\x22' -> printf " LDI \t(HL), A\n";
|
||||
let hl = merge_bytes cpu.reg.l cpu.reg.h in
|
||||
Memory.set mem hl cpu.reg.a;
|
||||
let high, low = split_2B (hl + 1) in
|
||||
cpu.reg.h <- high;
|
||||
cpu.reg.l <- low;
|
||||
inc_cycles cpu 8
|
||||
|
||||
|
||||
| '\x28' -> let n = read_pc_byte cpu mem in
|
||||
printf " JR\t Z, 0x%02X\n" n;
|
||||
printf " JR \tZ, 0x%02X\n" n;
|
||||
if cpu.flag.z = true then
|
||||
begin
|
||||
inc_pc cpu (n - 1); inc_cycles cpu 12
|
||||
|
@ -189,7 +210,7 @@ let run cpu (mem: Memory.map) =
|
|||
inc_cycles cpu 4
|
||||
|
||||
| '\xFE' -> let n = read_pc_byte cpu mem in
|
||||
printf " CP\t0x%02X\n" n;
|
||||
printf " CP \t0x%02X\n" n;
|
||||
cmp_A cpu n;
|
||||
inc_cycles cpu 8
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
open Printf
|
||||
|
||||
let rec run (cpu: Cpu.t) (mem: Memory.map) =
|
||||
let command = read_line () in
|
||||
Cpu.run cpu mem;
|
||||
run cpu mem
|
||||
|
||||
|
@ -17,8 +16,6 @@ let power_up cartridge =
|
|||
|
||||
let cpu = Cpu.init_cpu in
|
||||
let mem = Memory.init cartridge in
|
||||
Graphics.open_graph "";
|
||||
Graphics.resize_window 256 256;
|
||||
run cpu mem
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue