Remove redundant inc_cycles()
inc_cycles() was called in every case of the CPU pattern matching. Since we return the number of cycles of each instructions, we can increment only afterwards.master
parent
1b7c60e94d
commit
27efc19125
30
src/cpu.ml
30
src/cpu.ml
|
@ -154,20 +154,17 @@ let run cpu (mem: Memory.map) =
|
|||
| '\x7E' -> let inst = sprintf "LD \tA, (HL)" in
|
||||
let hl = merge_bytes cpu.reg.l cpu.reg.h in
|
||||
cpu.reg.a <- Memory.get mem hl;
|
||||
inc_cycles cpu 8;
|
||||
inst, 8
|
||||
|
||||
| '\x36' -> let n = read_pc_byte cpu mem in
|
||||
let inst = sprintf "LD \t(HL) 0x%02X" n in
|
||||
let hl = merge_bytes cpu.reg.l cpu.reg.h in
|
||||
Memory.set mem hl (char_of_int n);
|
||||
inc_cycles cpu 12;
|
||||
inst, 12
|
||||
|
||||
|
||||
(* CPU control *)
|
||||
| '\x00' -> let inst = sprintf "NOP" in
|
||||
inc_cycles cpu 4;
|
||||
inst, 4
|
||||
|
||||
|
||||
|
@ -178,26 +175,21 @@ let run cpu (mem: Memory.map) =
|
|||
if cpu.flag.z = false then
|
||||
begin
|
||||
inc_pc cpu (s - 2); (* we read 2 bytes from PC, opcode and n *)
|
||||
inc_cycles cpu 12;
|
||||
inst, 12
|
||||
end else begin
|
||||
inc_cycles cpu 8;
|
||||
inst, 8
|
||||
end
|
||||
|
||||
| '\xC9' -> let inst = sprintf "RET" in
|
||||
cpu.reg.pc <- pop_stack cpu mem;
|
||||
inc_cycles cpu 16;
|
||||
inst, 16
|
||||
|
||||
| '\xC0' -> let inst = sprintf "RET \tNZ" in
|
||||
if cpu.flag.z = false then
|
||||
begin
|
||||
cpu.reg.pc <- pop_stack cpu mem;
|
||||
inc_cycles cpu 20;
|
||||
inst, 20
|
||||
end else begin
|
||||
inc_cycles cpu 8;
|
||||
inst, 8
|
||||
end
|
||||
|
||||
|
@ -205,7 +197,6 @@ let run cpu (mem: Memory.map) =
|
|||
|
||||
| '\x03' -> let inst = sprintf "INC \tBC" in
|
||||
inc_BC cpu;
|
||||
inc_cycles cpu 8;
|
||||
inst, 8
|
||||
|
||||
| '\x05' -> let inst = sprintf "DEC \tB" in
|
||||
|
@ -214,18 +205,16 @@ let run cpu (mem: Memory.map) =
|
|||
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;
|
||||
inst, 4
|
||||
|
||||
| '\x06' -> let n = read_pc_byte cpu mem in
|
||||
let inst = sprintf "LD \tB, 0x%02X" n in
|
||||
cpu.reg.b <- char_of_int n;
|
||||
inc_cycles cpu 8;
|
||||
inst, 8
|
||||
|
||||
| '\x18' -> let n = read_pc_byte cpu mem in
|
||||
let inst = sprintf "JP \t0x%02X" n in
|
||||
inc_pc cpu (n - 1); inc_cycles cpu 12;
|
||||
inc_pc cpu (n - 1);
|
||||
inst, 12
|
||||
|
||||
| '\x21' -> let nn = read_pc_2bytes cpu mem in
|
||||
|
@ -233,7 +222,6 @@ let run cpu (mem: Memory.map) =
|
|||
let high, low = split_2B nn in
|
||||
cpu.reg.h <- high;
|
||||
cpu.reg.l <- low;
|
||||
inc_cycles cpu 12;
|
||||
inst, 12
|
||||
|
||||
| '\x22' -> let inst = sprintf "LDI \t(HL), A" in
|
||||
|
@ -242,7 +230,6 @@ let run cpu (mem: Memory.map) =
|
|||
let high, low = split_2B (hl + 1) in
|
||||
cpu.reg.h <- high;
|
||||
cpu.reg.l <- low;
|
||||
inc_cycles cpu 8;
|
||||
inst, 8
|
||||
|
||||
|
||||
|
@ -250,10 +237,9 @@ let run cpu (mem: Memory.map) =
|
|||
let inst = sprintf "JR \tZ, 0x%02X" n in
|
||||
if cpu.flag.z = true then
|
||||
begin
|
||||
inc_pc cpu (n - 1); inc_cycles cpu 12;
|
||||
inc_pc cpu (n - 1);
|
||||
inst, 12
|
||||
end else begin
|
||||
inc_cycles cpu 8;
|
||||
inst, 8
|
||||
end
|
||||
|
||||
|
@ -264,60 +250,51 @@ let run cpu (mem: Memory.map) =
|
|||
cpu.flag.n <- false;
|
||||
cpu.flag.h <- v = 0b00001111;
|
||||
Memory.set mem hl (char_of_int (v + 1));
|
||||
inc_cycles cpu 12;
|
||||
inst, 12
|
||||
|
||||
|
||||
| '\x3E' -> let n = read_pc_byte cpu mem in
|
||||
let inst = sprintf "LD \tA, 0x%02X" n in
|
||||
cpu.reg.a <- char_of_int n;
|
||||
inc_cycles cpu 8;
|
||||
inst, 8
|
||||
|
||||
| '\xAF' -> let inst = sprintf "XOR \tA, A" in
|
||||
let int_A = int_of_char cpu.reg.a in
|
||||
cpu.reg.a <- char_of_int @@ int_A lxor int_A;
|
||||
inc_cycles cpu 4;
|
||||
inst, 4
|
||||
|
||||
| '\xC3' -> let addr = read_pc_2bytes cpu mem in
|
||||
let inst = sprintf "JP \t0x%04X" addr in
|
||||
cpu.reg.pc <- addr; inc_cycles cpu 16;
|
||||
cpu.reg.pc <- addr;
|
||||
inst, 16
|
||||
|
||||
| '\xE0' -> let n = read_pc_byte cpu mem in
|
||||
let inst = sprintf "LDH \t(0xFF%02X), A" n in
|
||||
Memory.set mem (0xFF00 + n) cpu.reg.a;
|
||||
inc_cycles cpu 12;
|
||||
inst, 12
|
||||
|
||||
| '\xEA' -> let addr = read_pc_2bytes cpu mem in
|
||||
let inst = sprintf "LD \t(0X%04X), A" addr in
|
||||
Memory.set mem addr cpu.reg.a;
|
||||
inc_cycles cpu 16;
|
||||
inst, 16
|
||||
|
||||
|
||||
| '\xF0' -> let n = read_pc_byte cpu mem in
|
||||
let inst = sprintf "LDH \tA, (0xFF%02X)" n in
|
||||
cpu.reg.a <- Memory.get mem (0xFF00 + n);
|
||||
inc_cycles cpu 12;
|
||||
inst, 12
|
||||
|
||||
| '\xF3' -> let inst = sprintf "DI" in
|
||||
(* fixme *)
|
||||
inc_cycles cpu 4;
|
||||
inst, 4
|
||||
|
||||
| '\xFE' -> let n = read_pc_byte cpu mem in
|
||||
let inst = sprintf "CP \t0x%02X" n in
|
||||
cmp_A cpu n;
|
||||
inc_cycles cpu 8;
|
||||
inst, 8
|
||||
|
||||
| '\xFB' -> let inst = sprintf "EI" in
|
||||
(* enable interrupts, IME=1 *)
|
||||
inc_cycles cpu 4;
|
||||
inst, 4
|
||||
|
||||
| x -> eprintf "opcode 0x%02X\n" (int_of_char x);
|
||||
|
@ -325,4 +302,5 @@ let run cpu (mem: Memory.map) =
|
|||
failwith "Unimplemented opcode."
|
||||
|
||||
in
|
||||
inc_cycles cpu cycles;
|
||||
inst, cycles
|
||||
|
|
Loading…
Reference in New Issue