started op table

This commit is contained in:
radumaco
2025-04-19 22:26:59 +02:00
parent d26e6b7436
commit 28243ac5d5
11 changed files with 246 additions and 1 deletions

11
cpu/operations/add.go Normal file
View File

@@ -0,0 +1,11 @@
package operations
import "radu.macocian.me/goboy/memory"
func ADD(r1 *byte, r2 *byte) {
*r1 += *r2
}
func ADDFromMem(r1 *byte, r2 uint) {
*r1 += memory.Read8(r2)
}

View File

@@ -0,0 +1,33 @@
package operations
import (
"radu.macocian.me/goboy/memory"
"testing"
)
func TestADD(t *testing.T) {
r1 := byte(3)
r2 := byte(4)
ADD(&r1, &r2)
expected := byte(7)
actual := r1
if actual != expected {
t.Errorf("actual %x != expected %x", actual, expected)
}
}
func TestADDFromMem(t *testing.T) {
r1 := byte(3)
addr := uint(1000)
memory.Write8(addr, byte(6))
ADDFromMem(&r1, addr)
expected := byte(9)
actual := r1
if actual != expected {
t.Errorf("actual %x != expected %x", actual, expected)
}
}

14
cpu/operations/dec.go Normal file
View File

@@ -0,0 +1,14 @@
package operations
func DEC(r1 *byte) {
*r1--
}
func DEC16(r1 *byte, r2 *byte) {
if *r2 == 0x00 {
*r2 = 0xFF
*r1--
return
}
*r2--
}

View File

@@ -0,0 +1,37 @@
package operations
import "testing"
func TestDEC(t *testing.T) {
r1 := byte(3)
DEC(&r1)
expected := byte(2)
actual := r1
if actual != expected {
t.Errorf("actual %x != expected %x", actual, expected)
}
}
func TestDEC16(t *testing.T) {
r1 := byte(0xAB)
r2 := byte(0x11)
DEC16(&r1, &r2)
expected := uint16(0xAB10)
actual := (uint16(r1) << 8) | uint16(r2)
if actual != expected {
t.Errorf("actual %x != expected %x", actual, expected)
}
r1 = byte(0x11)
r2 = byte(0x00)
DEC16(&r1, &r2)
expected = uint16(0x1000)
actual = (uint16(r1) << 8) | uint16(r2)
if actual != expected {
t.Errorf("actual %x != expected %x", actual, expected)
}
}

14
cpu/operations/inc.go Normal file
View File

@@ -0,0 +1,14 @@
package operations
func INC(r1 *byte) {
*r1++
}
func INC16(r1 *byte, r2 *byte) {
if *r2 == 0xFF {
*r2 = 0
*r1++
return
}
*r2++
}

View File

@@ -0,0 +1,39 @@
package operations
import (
"testing"
)
func TestINC(t *testing.T) {
r1 := byte(3)
INC(&r1)
expected := byte(4)
actual := r1
if actual != expected {
t.Errorf("actual %x != expected %x", actual, expected)
}
}
func TestINC16(t *testing.T) {
r1 := byte(0xAB)
r2 := byte(0x11)
INC16(&r1, &r2)
expected := uint16(0xAB12)
actual := (uint16(r1) << 8) | uint16(r2)
if actual != expected {
t.Errorf("actual %x != expected %x", actual, expected)
}
r1 = byte(0x11)
r2 = byte(0xFF)
INC16(&r1, &r2)
expected = uint16(0x1200)
actual = (uint16(r1) << 8) | uint16(r2)
if actual != expected {
t.Errorf("actual %x != expected %x", actual, expected)
}
}

15
cpu/operations/load.go Normal file
View File

@@ -0,0 +1,15 @@
package operations
import "radu.macocian.me/goboy/memory"
func LD(r1 *byte, val byte) {
*r1 = val
}
func LDFromMem(r1 *byte, r2 uint) {
*r1 = memory.Read8(r2)
}
func LDInMemory8(addr uint16, val byte) {
memory.Write8(uint(addr), val)
}