First Commit

This commit is contained in:
Ivan Izaguirre 2019-01-26 16:59:36 +01:00
commit d36c030269
4 changed files with 61 additions and 0 deletions

0
execute.go Normal file
View File

0
memory.go Normal file
View File

41
registers.go Normal file
View File

@ -0,0 +1,41 @@
package main
const (
regA = 0
regX = 1
regY = 2
// empty
regP = 4
regS = 5
regPC = 6 // 2 bytes
)
type registers struct {
data [8]uint8
}
func (r registers) getA() uint8 { return r.data[regA] }
func (r registers) getX() uint8 { return r.data[regX] }
func (r registers) getY() uint8 { return r.data[regY] }
func (r registers) getP() uint8 { return r.data[regP] }
func (r registers) getS() uint8 { return r.data[regS] }
func (r registers) setRegister(i int, v uint8) registers {
r.data[i] = v
return r
}
func (r registers) setA(v uint8) registers { return r.setRegister(regA, v) }
func (r registers) setX(v uint8) registers { return r.setRegister(regX, v) }
func (r registers) setY(v uint8) registers { return r.setRegister(regY, v) }
func (r registers) setP(v uint8) registers { return r.setRegister(regP, v) }
func (r registers) setS(v uint8) registers { return r.setRegister(regS, v) }
func (r registers) getPC() uint16 {
return uint16(r.data[regPC])*256 + uint16(r.data[regPC+1])
}
func (r registers) setPC(v uint16) registers {
r.data[regPC] = uint8(v >> 8)
r.data[regPC+1] = uint8(v)
return r
}

20
registers_test.go Normal file
View File

@ -0,0 +1,20 @@
package main
import "testing"
func TestRegA(t *testing.T){
var r registers
var data uint8
data = 200
if r.setA(data).getA() != data {
t.Error("Error storing and loading A")
}
}
func TestRegPC(t *testing.T){
var r registers
var data uint16
data = 0xc600
if r.setPC(data).getPC() != data {
t.Error("Error storing and loading PC")
}
}