aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2017-06-17 21:14:40 +0200
committerDimitri Sokolyuk <demon@dim13.org>2017-06-17 21:14:40 +0200
commitf3ac15a5016f931ff38a2a18910e0c6881c84aa0 (patch)
treeb20dfe467f21ef8058fb283ecee63a99ce92653e
parentd04b1be3ba0b06a0bf987da0514aaf7c4e8754b7 (diff)
Better names
-rw-r--r--eval.go92
-rw-r--r--eval_test.go8
-rw-r--r--parse.go24
3 files changed, 62 insertions, 62 deletions
diff --git a/eval.go b/eval.go
index 421704f..e30ac8e 100644
--- a/eval.go
+++ b/eval.go
@@ -20,102 +20,102 @@ type J1 struct {
}
// Reset VM
-func (vm *J1) Reset() {
- vm.pc = 0
- vm.st0 = 0
- vm.dsp = 0
- vm.rsp = 0
+func (j1 *J1) Reset() {
+ j1.pc = 0
+ j1.st0 = 0
+ j1.dsp = 0
+ j1.rsp = 0
}
// LoadBytes into memory
-func (vm *J1) LoadBytes(data []byte) error {
+func (j1 *J1) LoadBytes(data []byte) error {
size := len(data) >> 1
- if size > len(vm.memory) {
+ if size > len(j1.memory) {
return fmt.Errorf("too big")
}
- return binary.Read(bytes.NewReader(data), binary.BigEndian, vm.memory[:size])
+ return binary.Read(bytes.NewReader(data), binary.BigEndian, j1.memory[:size])
}
// LoadFile into memory
-func (vm *J1) LoadFile(fname string) error {
+func (j1 *J1) LoadFile(fname string) error {
data, err := ioutil.ReadFile(fname)
if err != nil {
return err
}
- return vm.LoadBytes(data)
+ return j1.LoadBytes(data)
}
// Eval evaluates content of memory
-func (vm *J1) Eval() {
+func (j1 *J1) Eval() {
var cycle int
ticker := time.NewTicker(time.Second / 10)
defer ticker.Stop()
for range ticker.C {
cycle++
- ins := Decode(vm.memory[vm.pc])
+ ins := Decode(j1.memory[j1.pc])
if ins == Jump(0) {
return
}
- vm.eval(ins)
+ j1.eval(ins)
fmt.Printf("%4d %v\n", cycle, ins)
- fmt.Printf("%v\n", vm)
+ fmt.Printf("%v\n", j1)
}
}
-func (vm *J1) String() string {
+func (j1 *J1) String() string {
var rstack [32]uint16
- for i, v := range vm.rstack {
+ for i, v := range j1.rstack {
rstack[i] = v << 1
}
- s := fmt.Sprintf("\tPC=%0.4X ST=%0.4X\n", vm.pc<<1, vm.st0)
- s += fmt.Sprintf("\tD=%0.4X\n", vm.dstack[:vm.dsp+1])
- s += fmt.Sprintf("\tR=%0.4X\n", rstack[:vm.rsp+1])
+ s := fmt.Sprintf("\tPC=%0.4X ST=%0.4X\n", j1.pc<<1, j1.st0)
+ s += fmt.Sprintf("\tD=%0.4X\n", j1.dstack[:j1.dsp+1])
+ s += fmt.Sprintf("\tR=%0.4X\n", rstack[:j1.rsp+1])
return s
}
-func (vm *J1) eval(ins Instruction) {
+func (j1 *J1) eval(ins Instruction) {
switch v := ins.(type) {
case Lit:
- vm.pc++
- vm.dsp++
- vm.dstack[vm.dsp] = vm.st0
- vm.st0 = v.Value()
+ j1.pc++
+ j1.dsp++
+ j1.dstack[j1.dsp] = j1.st0
+ j1.st0 = v.Value()
case Jump:
- vm.pc = v.Value()
+ j1.pc = v.Value()
case Call:
- vm.rsp++
- vm.rstack[vm.rsp] = vm.pc + 1
- vm.pc = v.Value()
+ j1.rsp++
+ j1.rstack[j1.rsp] = j1.pc + 1
+ j1.pc = v.Value()
case Cond:
- vm.pc++
- if vm.st0 == 0 {
- vm.pc = v.Value()
+ j1.pc++
+ if j1.st0 == 0 {
+ j1.pc = v.Value()
}
- vm.st0 = vm.dstack[vm.dsp] // N
- vm.dsp--
+ j1.st0 = j1.dstack[j1.dsp] // N
+ j1.dsp--
case ALU:
- st0 := vm.newST0(v.Opcode)
- vm.pc++
+ st0 := j1.newST0(v.Opcode)
+ j1.pc++
if v.RtoPC {
- vm.pc = vm.rstack[vm.rsp]
+ j1.pc = j1.rstack[j1.rsp]
}
if v.NtoAtT {
- vm.memory[vm.st0] = vm.dstack[vm.dsp]
+ j1.memory[j1.st0] = j1.dstack[j1.dsp]
}
- vm.dsp += v.Ddir
- vm.rsp += v.Rdir
+ j1.dsp += v.Ddir
+ j1.rsp += v.Rdir
if v.TtoR {
- vm.rstack[vm.rsp] = vm.st0
+ j1.rstack[j1.rsp] = j1.st0
}
if v.TtoN {
- vm.dstack[vm.dsp] = vm.st0
+ j1.dstack[j1.dsp] = j1.st0
}
- vm.st0 = st0
+ j1.st0 = st0
}
}
-func (vm *J1) newST0(opcode uint16) uint16 {
- T, N, R := vm.st0, vm.dstack[vm.dsp], vm.rstack[vm.rsp]
+func (j1 *J1) newST0(opcode uint16) uint16 {
+ T, N, R := j1.st0, j1.dstack[j1.dsp], j1.rstack[j1.rsp]
switch opcode {
case opT: // T
return T
@@ -148,11 +148,11 @@ func (vm *J1) newST0(opcode uint16) uint16 {
case opR: // R (rT)
return R
case opAtT: // [T]
- return vm.memory[T]
+ return j1.memory[T]
case opNlshiftT: // N<<T
return N << (T & 0xf)
case opDepth: // depth (dsp)
- return (uint16(vm.rsp) << 8) | uint16(vm.dsp)
+ return (uint16(j1.rsp) << 8) | uint16(j1.dsp)
case opNuleT: // Nu<T
if N < T {
return 1
diff --git a/eval_test.go b/eval_test.go
index 8404674..be3f630 100644
--- a/eval_test.go
+++ b/eval_test.go
@@ -153,9 +153,9 @@ func TestLoadBytes(t *testing.T) {
}
func TestRest(t *testing.T) {
- vm := &J1{pc: 100, dsp: 2, rsp: 3, st0: 5}
- vm.Reset()
- if vm.pc != 0 || vm.dsp != 0 || vm.rsp != 0 || vm.st0 != 0 {
- t.Errorf("got %v", vm)
+ j1 := &J1{pc: 100, dsp: 2, rsp: 3, st0: 5}
+ j1.Reset()
+ if j1.pc != 0 || j1.dsp != 0 || j1.rsp != 0 || j1.st0 != 0 {
+ t.Errorf("got %v", j1)
}
}
diff --git a/parse.go b/parse.go
index bf96844..3b8bea2 100644
--- a/parse.go
+++ b/parse.go
@@ -6,15 +6,15 @@ import "fmt"
func Decode(v uint16) Instruction {
switch {
case v&(1<<15) != 0:
- return newLit(v)
+ return ParseLit(v)
case v&(7<<13) == 0:
- return newJump(v)
+ return ParseJump(v)
case v&(7<<13) == 1<<13:
- return newCond(v)
+ return ParseCond(v)
case v&(7<<13) == 2<<13:
- return newCall(v)
+ return ParseCall(v)
case v&(7<<13) == 3<<13:
- return newALU(v)
+ return ParseALU(v)
}
return nil
}
@@ -29,7 +29,7 @@ type Instruction interface {
// Lit is a literal
type Lit uint16
-func newLit(v uint16) Lit { return Lit(v &^ uint16(1<<15)) }
+func ParseLit(v uint16) Lit { return Lit(v &^ uint16(1<<15)) }
func (v Lit) String() string { return fmt.Sprintf("LIT %0.4X", uint16(v)) }
func (v Lit) Value() uint16 { return uint16(v) }
func (v Lit) Compile() uint16 { return v.Value() | (1 << 15) }
@@ -37,7 +37,7 @@ func (v Lit) Compile() uint16 { return v.Value() | (1 << 15) }
// Jump is an unconditional branch
type Jump uint16
-func newJump(v uint16) Jump { return Jump(v &^ uint16(7<<13)) }
+func ParseJump(v uint16) Jump { return Jump(v &^ uint16(7<<13)) }
func (v Jump) String() string { return fmt.Sprintf("UBRANCH %0.4X", uint16(v<<1)) }
func (v Jump) Value() uint16 { return uint16(v) }
func (v Jump) Compile() uint16 { return v.Value() }
@@ -45,7 +45,7 @@ func (v Jump) Compile() uint16 { return v.Value() }
// Cond is a conditional branch
type Cond uint16
-func newCond(v uint16) Cond { return Cond(v &^ uint16(7<<13)) }
+func ParseCond(v uint16) Cond { return Cond(v &^ uint16(7<<13)) }
func (v Cond) String() string { return fmt.Sprintf("0BRANCH %0.4X", uint16(v<<1)) }
func (v Cond) Value() uint16 { return uint16(v) }
func (v Cond) Compile() uint16 { return v.Value() | (1 << 13) }
@@ -53,7 +53,7 @@ func (v Cond) Compile() uint16 { return v.Value() | (1 << 13) }
// Call procedure
type Call uint16
-func newCall(v uint16) Call { return Call(v &^ uint16(7<<13)) }
+func ParseCall(v uint16) Call { return Call(v &^ uint16(7<<13)) }
func (v Call) String() string { return fmt.Sprintf("CALL %0.4X", uint16(v<<1)) }
func (v Call) Value() uint16 { return uint16(v) }
func (v Call) Compile() uint16 { return v.Value() | (2 << 13) }
@@ -69,7 +69,7 @@ type ALU struct {
Ddir int8
}
-func newALU(v uint16) ALU {
+func ParseALU(v uint16) ALU {
return ALU{
Opcode: (v >> 8) & 15,
RtoPC: v&(1<<12) != 0,
@@ -128,13 +128,13 @@ const (
opNuleT // 15
)
-var opcodes = []string{
+var opcodeNames = []string{
"T", "N", "T+N", "T&N", "T|N", "T^N", "~T", "N==T",
"N<T", "N>>T", "T-1", "R", "[T]", "N<<T", "depth", "Nu<T",
}
func (v ALU) String() string {
- s := "ALU " + opcodes[v.Opcode]
+ s := "ALU " + opcodeNames[v.Opcode]
if v.RtoPC {
s += " R→PC"
}