Added the 8 conditional branch instructions

This commit is contained in:
Ivan Izaguirre 2019-01-29 23:45:01 +01:00
parent e6535c2778
commit ff1b116163
2 changed files with 41 additions and 8 deletions

View File

@ -157,6 +157,16 @@ func buildOpUpdateFlag(flag uint8, value bool) opFunc {
}
}
func buildOpBranch(flag uint8, value bool) opFunc {
return func(s *state, line []uint8, opcode opcode) {
if s.registers.getFlag(flag) == value {
pc := s.registers.getPC()
pc += uint16(int8(line[1]))
s.registers.setPC(pc)
}
}
}
/*
TODO:
@ -177,14 +187,6 @@ CPY
BRK
BCC
BCS
BEQ
BMI
BPL
BVC
BVS
JMP
JSR
RTI
@ -266,6 +268,15 @@ var opcodes = [256]opcode{
0xAC: opcode{"LDY", 3, 4, buildOpLoad(modeAbsolute, regY)},
0xBC: opcode{"LDY", 3, 4, buildOpLoad(modeAbsoluteX, regY)}, // Extra cycles
0x90: opcode{"BCC", 2, 2, buildOpBranch(flagC, false)}, // Extra cycles
0xB0: opcode{"BCS", 2, 2, buildOpBranch(flagC, true)}, // Extra cycles
0xD0: opcode{"BNE", 2, 2, buildOpBranch(flagZ, false)}, // Extra cycles
0xF0: opcode{"BEQ", 2, 2, buildOpBranch(flagZ, true)}, // Extra cycles
0x10: opcode{"BPL", 2, 2, buildOpBranch(flagN, false)}, // Extra cycles
0x30: opcode{"BMI", 2, 2, buildOpBranch(flagN, true)}, // Extra cycles
0x50: opcode{"BVC", 2, 2, buildOpBranch(flagV, false)}, // Extra cycles
0x70: opcode{"BVS", 2, 2, buildOpBranch(flagV, true)}, // Extra cycles
0xEA: opcode{"NOP", 1, 2, opNOP},
}

View File

@ -185,3 +185,25 @@ func TestClearSetFlag(t *testing.T) {
}
}
func TestBranch(t *testing.T) {
var s state
s.registers.setPC(0xC600)
s.registers.setFlag(flagV)
executeLine(&s, []uint8{0x50, 0x20})
if s.registers.getPC() != 0xC600 {
t.Errorf("Error in BVC, %v", s.registers)
}
executeLine(&s, []uint8{0x70, 0x20})
if s.registers.getPC() != 0xC620 {
t.Errorf("Error in BVS, %v", s.registers)
}
s.registers.setPC(0xD600)
s.registers.clearFlag(flagC)
executeLine(&s, []uint8{0x90, 0xA0})
if s.registers.getPC() != 0xD5A0 {
t.Errorf("Error in BCC, %v", s.registers)
}
}