aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-01-30 10:08:56 +0100
committerDimitri Sokolyuk <demon@dim13.org>2018-01-30 10:08:56 +0100
commit3cba252945176fe2e0ab44445143068e793a7245 (patch)
tree28b874c1b8298fc9de76cd34e98147cb1073adcf
parentf686063b66c97fa65e556c8c56b9beadc6b35eca (diff)
kiss
-rw-r--r--core.go24
-rw-r--r--core_test.go6
-rw-r--r--stack.go12
3 files changed, 19 insertions, 23 deletions
diff --git a/core.go b/core.go
index 8e33280..795ccd3 100644
--- a/core.go
+++ b/core.go
@@ -8,9 +8,7 @@ import (
"io/ioutil"
)
-const memSize = 0x4000
-
-var errStop = errors.New("stop")
+var ErrStop = errors.New("stop")
// Console i/o
type Console interface {
@@ -21,11 +19,11 @@ type Console interface {
// Core of J1 Forth CPU
type Core struct {
- memory [memSize]uint16 // 0..0x3fff main memory, 0x4000 .. 0x7fff mem-mapped i/o
- pc uint16 // 13 bit
- st0 uint16 // top of data stack
- d, r stack // data and return stacks
- console Console // console i/o
+ memory [0x4000]uint16 // 0..0x3fff main memory, 0x4000 .. 0x7fff mem-mapped i/o
+ pc uint16 // 13 bit
+ st0 uint16 // top of data stack
+ d, r stack // data and return stacks
+ console Console // console i/o
}
// New core with console i/o
@@ -41,7 +39,7 @@ func (c *Core) Reset() {
// LoadBytes into memory
func (c *Core) LoadBytes(data []byte) error {
size := len(data) >> 1
- if size >= memSize {
+ if size >= len(c.memory) {
return errors.New("too big")
}
return binary.Read(bytes.NewReader(data), binary.LittleEndian, c.memory[:size])
@@ -64,20 +62,20 @@ func (c *Core) String() string {
}
func (c *Core) writeAt(addr, value uint16) error {
- if off := int(addr >> 1); off < memSize {
+ if off := int(addr >> 1); off < len(c.memory) {
c.memory[addr>>1] = value
}
switch addr {
case 0xf000: // key
c.console.Write(value)
case 0xf002: // bye
- return errStop
+ return ErrStop
}
return nil
}
func (c *Core) readAt(addr uint16) uint16 {
- if off := int(addr >> 1); off < memSize {
+ if off := int(addr >> 1); off < len(c.memory) {
return c.memory[off]
}
switch addr {
@@ -94,7 +92,7 @@ func (c *Core) Run() {
for {
ins := c.Decode()
err := c.Eval(ins)
- if err == errStop {
+ if err == ErrStop {
return
}
}
diff --git a/core_test.go b/core_test.go
index ab4e05a..cbc8f2f 100644
--- a/core_test.go
+++ b/core_test.go
@@ -112,7 +112,7 @@ func TestEval(t *testing.T) {
},
{ // !
ins: []Instruction{Lit(1), Lit(0), ALU{Opcode: opN, NtoAtT: true, Ddir: -1}},
- end: Core{pc: 3, st0: 1, d: stack{data: [0x20]uint16{0, 0, 1}, sp: 1}, memory: [memSize]uint16{1}},
+ end: Core{pc: 3, st0: 1, d: stack{data: [0x20]uint16{0, 0, 1}, sp: 1}, memory: [0x4000]uint16{1}},
},
}
@@ -147,7 +147,7 @@ func TestNextST0(t *testing.T) {
{ins: ALU{Opcode: opNrshiftT}, st0: 0x3f, state: Core{st0: 0x02, d: stack{data: [0x20]uint16{0, 0xaa, 0xff}, sp: 2}}},
{ins: ALU{Opcode: opTminus1}, st0: 0x54, state: Core{st0: 0x55}},
{ins: ALU{Opcode: opR}, st0: 0x5, state: Core{r: stack{data: [0x20]uint16{0, 0x05}, sp: 1}}},
- {ins: ALU{Opcode: opAtT}, st0: 0x5, state: Core{st0: 0x02, memory: [memSize]uint16{0, 5, 10}}},
+ {ins: ALU{Opcode: opAtT}, st0: 0x5, state: Core{st0: 0x02, memory: [0x4000]uint16{0, 5, 10}}},
{ins: ALU{Opcode: opNlshiftT}, st0: 0x3fc, state: Core{st0: 0x02, d: stack{data: [0x20]uint16{0, 0xaa, 0xff}, sp: 2}}},
{ins: ALU{Opcode: opDepth}, st0: 0x305, state: Core{r: stack{sp: 3}, d: stack{sp: 5}}},
{ins: ALU{Opcode: opNuleT}, st0: 0xffff, state: Core{st0: 0xff, d: stack{data: [0x20]uint16{0, 0xaa, 0xbb}, sp: 2}}},
@@ -170,7 +170,7 @@ func TestLoadBytes(t *testing.T) {
if err := j1.LoadBytes(data); err != nil {
t.Fatal(err)
}
- expect := [memSize]uint16{0x0201, 0x0804}
+ expect := [0x4000]uint16{0x0201, 0x0804}
if j1.memory != expect {
t.Errorf("got %v, want %v", j1.memory[:2], expect)
}
diff --git a/stack.go b/stack.go
index 324e16d..da87aa9 100644
--- a/stack.go
+++ b/stack.go
@@ -1,24 +1,22 @@
package j1
-const stackSize = 0x20
-
type stack struct {
- data [stackSize]uint16 // stack
- sp int8 // 5 bit stack pointer
+ data [0x20]uint16 // stack
+ sp int8 // 5 bit stack pointer
}
func (s *stack) move(dir int8) {
- s.sp = (s.sp + dir) & (stackSize - 1)
+ s.sp = (s.sp + dir) & int8(len(s.data)-1)
}
func (s *stack) push(v uint16) {
- s.sp = (s.sp + 1) & (stackSize - 1)
+ s.sp = (s.sp + 1) & int8(len(s.data)-1)
s.data[s.sp] = v
}
func (s *stack) pop() uint16 {
sp := s.sp
- s.sp = (s.sp - 1) & (stackSize - 1)
+ s.sp = (s.sp - 1) & int8(len(s.data)-1)
return s.data[sp]
}