aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2018-01-16 23:59:44 +0100
committerDimitri Sokolyuk <demon@dim13.org>2018-01-16 23:59:44 +0100
commitbc1a7a271ba5a3f971e9dbed774b01b43af2642e (patch)
tree3332120066250a645535d397345414fdd876cceb
parent98d8ae9eda1c8de9b538299da61e0668c77b15d5 (diff)
...
-rw-r--r--core.go21
-rw-r--r--core_test.go6
2 files changed, 13 insertions, 14 deletions
diff --git a/core.go b/core.go
index 9a82b36..c3108b6 100644
--- a/core.go
+++ b/core.go
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/binary"
+ "errors"
"fmt"
"io/ioutil"
)
@@ -39,7 +40,7 @@ func (c *Core) Reset() {
func (c *Core) LoadBytes(data []byte) error {
size := len(data) >> 1
if size >= memSize {
- return fmt.Errorf("too big")
+ return errors.New("too big")
}
return binary.Read(bytes.NewReader(data), binary.LittleEndian, c.memory[:size])
}
@@ -136,6 +137,11 @@ func (c *Core) Eval(ins Instruction) {
}
}
+var boolValue = map[bool]uint16{
+ false: 0,
+ true: ^uint16(0),
+}
+
func (c *Core) newST0(opcode uint16) uint16 {
T, N, R := c.st0, c.d.peek(), c.r.peek()
switch opcode {
@@ -154,9 +160,9 @@ func (c *Core) newST0(opcode uint16) uint16 {
case opNotT: // ~T
return ^T
case opNeqT: // N==T
- return bool2int(N == T)
+ return boolValue[N == T]
case opNleT: // N<T
- return bool2int(int16(N) < int16(T))
+ return boolValue[int16(N) < int16(T)]
case opNrshiftT: // N>>T
return N >> (T & 0xf)
case opTminus1: // T-1
@@ -170,15 +176,8 @@ func (c *Core) newST0(opcode uint16) uint16 {
case opDepth: // depth (dsp)
return (c.r.depth() << 8) | c.d.depth()
case opNuleT: // Nu<T
- return bool2int(N < T)
+ return boolValue[N < T]
default:
panic("invalid instruction")
}
}
-
-func bool2int(b bool) uint16 {
- if b {
- return ^uint16(0)
- }
- return 0
-}
diff --git a/core_test.go b/core_test.go
index 12ae6f9..be0773e 100644
--- a/core_test.go
+++ b/core_test.go
@@ -106,7 +106,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: [0x4000]uint16{1}},
+ end: Core{pc: 3, st0: 1, d: stack{data: [0x20]uint16{0, 0, 1}, sp: 1}, memory: [memSize]uint16{1}},
},
}
@@ -141,7 +141,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: [0x4000]uint16{0, 5, 10}}},
+ {ins: ALU{Opcode: opAtT}, st0: 0x5, state: Core{st0: 0x02, memory: [memSize]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}}},
@@ -164,7 +164,7 @@ func TestLoadBytes(t *testing.T) {
if err := j1.LoadBytes(data); err != nil {
t.Fatal(err)
}
- expect := [0x4000]uint16{0x0201, 0x0804}
+ expect := [memSize]uint16{0x0201, 0x0804}
if j1.memory != expect {
t.Errorf("got %v, want %v", j1.memory[:2], expect)
}