aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/cpu/opcodes/Calls.kt
blob: 70ce3f0f66c4ba829e60b2eb76451328c3c6cab6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package cpu.opcodes

import cpu.Operation
import cpu.Registers
import cpu.Registers.Flag
import ram.Ram
import BitManipulation as bm

val calls = mapOf(

  0xCD to Operation("CALL nn", 2, 12) { r, m, a -> call(bm.argsToWord(a), r, m)},

  0xC4 to Operation("CALL NZ,nn", 2, 12) { r, m, a -> if(r.getFlag(Flag.ZERO) == 0) call(bm.argsToWord(a), r, m)},
  0xCC to Operation("CALL Z,nn", 2, 12) { r, m, a -> if(r.getFlag(Flag.ZERO) == 1) call(bm.argsToWord(a), r, m)},
  0xD4 to Operation("CALL NC,nn", 2, 12) { r, m, a -> if(r.getFlag(Flag.CARRY) == 0) call(bm.argsToWord(a), r, m)},
  0xDC to Operation("CALL C,nn", 2, 12) { r, m, a -> if(r.getFlag(Flag.CARRY) == 1) call(bm.argsToWord(a), r, m)}

)

private fun call(word: Int, r: Registers, m: Ram) {

  val nextAddress = (r.PC + 3) and 0xFFFF

  m.writeByte(r.decrementAndGetSP(), bm.getMsb(nextAddress))
  m.writeByte(r.decrementAndGetSP(), bm.getLsb(nextAddress))

  r.PC = word
}