From d36c030269663b892091f209a578daecefb12c67 Mon Sep 17 00:00:00 2001 From: Ivan Izaguirre Date: Sat, 26 Jan 2019 16:59:36 +0100 Subject: [PATCH] First Commit --- execute.go | 0 memory.go | 0 registers.go | 41 +++++++++++++++++++++++++++++++++++++++++ registers_test.go | 20 ++++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 execute.go create mode 100644 memory.go create mode 100644 registers.go create mode 100644 registers_test.go diff --git a/execute.go b/execute.go new file mode 100644 index 0000000..e69de29 diff --git a/memory.go b/memory.go new file mode 100644 index 0000000..e69de29 diff --git a/registers.go b/registers.go new file mode 100644 index 0000000..360f55f --- /dev/null +++ b/registers.go @@ -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 +} diff --git a/registers_test.go b/registers_test.go new file mode 100644 index 0000000..b9d873c --- /dev/null +++ b/registers_test.go @@ -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") + } +} \ No newline at end of file