aboutsummaryrefslogtreecommitdiff
path: root/eval.go
blob: ee83e4ce26a6c97c135abda4e95bcd2e9194bca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package j1

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"io/ioutil"
	"time"
)

// J1 Forth processor VM
type J1 struct {
	dsp    uint16         // 5 bit data stack pointer
	st0    uint16         // top of data stack
	pc     uint16         // 13 bit
	rsp    uint16         // 5 bit retrun stack pointer
	dstack [0x20]uint16   // data stack
	rstack [0x20]uint16   // deturn stack
	memory [0x8000]uint16 // memory
}

// Reset VM
func (vm *J1) Reset() {
	vm.dsp = 0
	vm.st0 = 0
	vm.pc = 0
	vm.rsp = 0
}

func (vm *J1) String() string {
	var rstack [32]uint16
	for i, v := range vm.rstack {
		rstack[i] = v << 1
	}
	return fmt.Sprintf("PC=%0.4X ST=%0.4X D=%0.4X R=%0.4X",
		vm.pc<<1, vm.st0, vm.dstack[:vm.dsp+1], rstack[:vm.rsp+1])
}

// LoadBytes into memory
func (vm *J1) LoadBytes(data []byte) error {
	size := len(data) >> 1
	if size > len(vm.memory) {
		return fmt.Errorf("too big")
	}
	return binary.Read(bytes.NewReader(data), binary.BigEndian, vm.memory[:size])
}

// LoadFile into memory
func (vm *J1) LoadFile(fname string) error {
	data, err := ioutil.ReadFile(fname)
	if err != nil {
		return err
	}
	return vm.LoadBytes(data)
}

// Eval evaluates content of memory
func (vm *J1) Eval() {
	ticker := time.NewTicker(time.Second)
	defer ticker.Stop()
	for range ticker.C {
		ins := Decode(vm.memory[vm.pc])
		if ins == Jump(0) {
			break
		}
		vm.eval(ins)
		fmt.Printf("%-30s%s\n", ins, vm)
	}
}

func (vm *J1) eval(ins Instruction) {
	dsp := vm.dsp
	pc := vm.pc + 1
	st0 := vm.st0
	rsp := vm.rsp

	switch v := ins.(type) {
	case Lit:
		st0 = uint16(v)
		dsp = vm.dsp + 1
		vm.dstack[dsp] = vm.st0
	case Jump:
		st0 = vm.st0 // T
		pc = uint16(v)
	case Call:
		st0 = vm.st0 // T
		rsp = vm.rsp + 1
		vm.rstack[rsp] = pc
		pc = uint16(v)
	case Cond:
		st0 = vm.dstack[vm.dsp] // N
		dsp = vm.dsp - 1
		if vm.st0 == 0 {
			pc = uint16(v)
		}
	case ALU:
		st0 = vm.newST0(v)
		if v.RtoPC {
			pc = vm.rstack[vm.rsp]
		}
		if v.NtoAtT {
			vm.memory[vm.st0] = vm.dstack[vm.dsp]
		}
		dsp = uint16(int8(vm.dsp) + v.Ddir)
		rsp = uint16(int8(vm.rsp) + v.Rdir)
		if v.TtoR {
			vm.rstack[rsp] = vm.st0
		}
		if v.TtoN {
			vm.dstack[dsp] = vm.st0
		}
	}

	vm.dsp = dsp
	vm.pc = pc
	vm.st0 = st0
	vm.rsp = rsp
}

func (vm *J1) newST0(v ALU) uint16 {
	T, N, R := vm.st0, vm.dstack[vm.dsp], vm.rstack[vm.rsp]
	switch v.Opcode {
	case 0: // T
		return T
	case 1: // N
		return N
	case 2: // T+N
		return T + N
	case 3: // T&N
		return T & N
	case 4: // T|N
		return T | N
	case 5: // T^N
		return T ^ N
	case 6: // ~T
		return ^T
	case 7: // N==T
		if N == T {
			return 1
		}
		return 0
	case 8: // N<T
		if int16(N) < int16(T) {
			return 1
		}
		return 0
	case 9: // N>>T
		return N >> (T & 0xf)
	case 10: // T-1
		return T - 1
	case 11: // R (rT)
		return R
	case 12: // [T]
		return vm.memory[T]
	case 13: // N<<T
		return N << (T & 0xf)
	case 14: // depth (dsp)
		return (vm.rsp << 8) | vm.dsp
	case 15: // Nu<T
		if N < T {
			return 1
		}
		return 0
	default:
		panic("invalid instruction")
	}
}