mirror of
https://github.com/macocianradu/goboy.git
synced 2026-03-18 21:10:07 +00:00
added flag read and set operations and tests
This commit is contained in:
43
cpu/cpu.go
43
cpu/cpu.go
@@ -37,6 +37,18 @@ func setDoubleRegis(r1 *byte, r2 *byte, value uint16) {
|
|||||||
*r2 = byte(value & 0x00FF)
|
*r2 = byte(value & 0x00FF)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readBit(r byte, pos uint8) bool {
|
||||||
|
return r>>pos&1 == 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func setBit(r *byte, pos uint8, value bool) {
|
||||||
|
if value {
|
||||||
|
*r |= 1 << pos
|
||||||
|
return
|
||||||
|
}
|
||||||
|
*r &= ^(1 << pos)
|
||||||
|
}
|
||||||
|
|
||||||
func (cpu *CPU_struct) AF() uint16 {
|
func (cpu *CPU_struct) AF() uint16 {
|
||||||
return readDoubleRegis(cpu.A, cpu.F)
|
return readDoubleRegis(cpu.A, cpu.F)
|
||||||
}
|
}
|
||||||
@@ -68,3 +80,34 @@ func (cpu *CPU_struct) HL() uint16 {
|
|||||||
func (cpu *CPU_struct) SetHL(value uint16) {
|
func (cpu *CPU_struct) SetHL(value uint16) {
|
||||||
setDoubleRegis(&cpu.H, &cpu.L, value)
|
setDoubleRegis(&cpu.H, &cpu.L, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cpu *CPU_struct) ZF() bool {
|
||||||
|
return readBit(cpu.F, 7)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cpu *CPU_struct) SetZF(value bool) {
|
||||||
|
setBit(&cpu.F, 7, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cpu *CPU_struct) NF() bool {
|
||||||
|
return readBit(cpu.F, 6)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cpu *CPU_struct) SetNF(value bool) {
|
||||||
|
setBit(&cpu.F, 6, value)
|
||||||
|
}
|
||||||
|
func (cpu *CPU_struct) HF() bool {
|
||||||
|
return readBit(cpu.F, 5)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cpu *CPU_struct) SetHF(value bool) {
|
||||||
|
setBit(&cpu.F, 5, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cpu *CPU_struct) CF() bool {
|
||||||
|
return readBit(cpu.F, 4)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cpu *CPU_struct) SetCF(value bool) {
|
||||||
|
setBit(&cpu.F, 4, value)
|
||||||
|
}
|
||||||
|
|||||||
@@ -44,3 +44,39 @@ func TestCPU_struct_GetAndSetAF(t *testing.T) {
|
|||||||
t.Errorf("actual %x != expected %x", actualAF, expectedAF)
|
t.Errorf("actual %x != expected %x", actualAF, expectedAF)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCPU_struct_SetCY(t *testing.T) {
|
||||||
|
cpu := CPU_struct{}
|
||||||
|
cpu.F = 0x00
|
||||||
|
|
||||||
|
cpu.SetCF(true)
|
||||||
|
|
||||||
|
expected := byte(0b00010000)
|
||||||
|
actual := cpu.F
|
||||||
|
|
||||||
|
if actual != expected {
|
||||||
|
t.Errorf("actual %x != expected %x", actual, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCPU_struct_GetCY(t *testing.T) {
|
||||||
|
cpu := CPU_struct{}
|
||||||
|
cpu.F = 0b01011000
|
||||||
|
|
||||||
|
actual := cpu.CF()
|
||||||
|
|
||||||
|
if actual != true {
|
||||||
|
t.Errorf("actual %v != expected %v", actual, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCPU_struct_SetAndGetCY(t *testing.T) {
|
||||||
|
cpu := CPU_struct{}
|
||||||
|
cpu.SetCF(true)
|
||||||
|
|
||||||
|
actual := cpu.CF()
|
||||||
|
|
||||||
|
if actual != true {
|
||||||
|
t.Errorf("actual %v != expected %v", actual, true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user