-
Notifications
You must be signed in to change notification settings - Fork 0
/
octave.go
344 lines (294 loc) · 6.21 KB
/
octave.go
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
)
func main() {
fmt.Println("Initializing Octave CPU...")
cpu := &CPU{running: true, sp: 0xFFFF}
file, err := os.Open(os.Args[1])
if err != nil {
return
}
defer file.Close()
mem, err := ioutil.ReadAll(file)
for i, val := range mem {
cpu.memory[i] = val
}
cpu.stack = stack{cpu}
cpu.devices[0] = cpu.stack
cpu.devices[1] = tty{bufio.NewReader(os.Stdin)}
cpu.devices[4] = register{}
cpu.devices[5] = register{}
cpu.devices[6] = register{}
cpu.devices[7] = register{}
if err != nil {
return
}
for cpu.running {
inst_byte := fetch(cpu)
inst_func := decode(inst_byte)
inst_func(inst_byte, cpu)
}
}
type CPU struct {
memory [1 << 16]uint8
registers [4]uint8
pc uint16
sp uint16
running bool
result uint8
devices [8]Device
stack stack
}
type instruction func(uint8, *CPU)
type Device interface {
read() uint8
write(uint8)
}
type tty struct {
reader *bufio.Reader
}
func (t tty) read() uint8 {
b, _ := t.reader.ReadByte()
return b
}
func (t tty) write(char uint8) {
fmt.Printf("%c", char)
}
type stack struct {
cpu *CPU
}
func (s stack) read() uint8 {
s.cpu.sp++
value := s.cpu.memory[s.cpu.sp]
return value
}
func (s stack) write(char uint8) {
s.cpu.memory[s.cpu.sp] = char
s.cpu.sp--
}
func fetch(cpu *CPU) uint8 {
inst := cpu.memory[cpu.pc]
cpu.pc = cpu.pc + 1
return inst
}
type register struct {
value uint8
}
func (r register) read() uint8 {
return r.value
}
func (r register) write(char uint8) {
r.value = char
}
func decode(i uint8) instruction {
inst := illegal
switch i >> 5 {
case 0:
fmt.Fprint(os.Stderr, "jmp\n")
inst = jmp
case 1:
fmt.Fprint(os.Stderr, "loadi\n")
inst = loadi
case 2:
fmt.Fprint(os.Stderr, "math\n")
inst = math
case 3:
fmt.Fprint(os.Stderr, "logic\n")
inst = logic
case 4:
fmt.Fprint(os.Stderr, "mem\n")
inst = mem
case 5:
fmt.Fprint(os.Stderr, "stack\n")
inst = stacki
case 6:
fmt.Fprint(os.Stderr, "in\n")
inst = in
case 7:
fmt.Fprint(os.Stderr, "out\n")
inst = out
}
return inst
}
func jmp(i uint8, cpu *CPU) {
if i == 0 {
cpu.running = false
}
register := i << 3 >> 6
n := i << 5 >> 7
z := i << 6 >> 7
p := i << 7 >> 7
if (n == 1 && cpu.result < 0) || (z == 1 && cpu.result == 0) || (p == 1 && cpu.result > 0) {
offset := int8(cpu.registers[register])
fmt.Fprintf(os.Stderr, "Taking jump to %v\n", offset)
cpu.pc = uint16(int32(cpu.pc) + int32(offset))
}
}
func loadi(i uint8, cpu *CPU) {
location := i << 3 >> 7
if location == 0 {
cpu.registers[0] = (i << 4) | (cpu.registers[0] << 4 >> 4)
} else {
cpu.registers[0] = (i << 4 >> 4) | (cpu.registers[0] >> 4 << 4)
}
}
func math(i uint8, cpu *CPU) {
operation := i << 3 >> 7
destination := i << 4 >> 6
source := i << 6 >> 6
if operation == 0 {
cpu.registers[destination] = cpu.registers[destination] + cpu.registers[source]
} else {
cpu.registers[destination] = cpu.registers[source]
}
cpu.result = cpu.registers[destination]
}
func logic(i uint8, cpu *CPU) {
operation := i << 3 >> 7
destination := i << 4 >> 6
source := i << 6 >> 6
if operation == 0 {
cpu.registers[destination] = cpu.registers[destination] & cpu.registers[source]
} else {
cpu.registers[destination] = cpu.registers[destination] ^ cpu.registers[source]
}
cpu.result = cpu.registers[destination]
}
func mem(i uint8, cpu *CPU) {
operation := i << 3 >> 7
address_high := i << 4 >> 6
address_low := i << 6 >> 6
address := uint16(cpu.registers[address_high])<<8 + uint16(cpu.registers[address_low])
if operation == 0 {
// LOAD
fmt.Fprintf(os.Stderr, "Loading %v\n to R0", address)
cpu.registers[0] = cpu.memory[address]
} else {
// STORE
fmt.Fprintf(os.Stderr, "Storing R0 to %v\n", address)
cpu.memory[address] = cpu.registers[0]
}
}
func pop16(s stack) uint16 {
byte_1 := s.read()
byte_2 := s.read()
return uint16(byte_1)<<8 + uint16(byte_2)
}
func pop32(s stack) uint32 {
byte_1 := s.read()
byte_2 := s.read()
byte_3 := s.read()
byte_4 := s.read()
return uint32(byte_1)<<24 + uint32(byte_2)<<16 + uint32(byte_3)<<8 + uint32(byte_4)
}
func push16(s stack, value uint16) {
byte_1 := uint8(value >> 8)
byte_2 := uint8(value << 8 >> 8)
s.write(byte_2)
s.write(byte_1)
}
func stacki(i uint8, cpu *CPU) {
stacki := i << 3 >> 3
switch stacki {
case 0:
// add16
b := cpu.stack.read()
a := cpu.stack.read()
cpu.stack.write(a + b)
case 1:
// sub16
b := cpu.stack.read()
a := cpu.stack.read()
cpu.stack.write(a - b)
case 2:
// mul16
b := cpu.stack.read()
a := cpu.stack.read()
cpu.stack.write(a * b)
case 3:
// div16
b := cpu.stack.read()
a := cpu.stack.read()
cpu.stack.write(a / b)
case 4:
// mod16
b := cpu.stack.read()
a := cpu.stack.read()
cpu.stack.write(a % b)
case 5:
// neg16
a := cpu.stack.read()
cpu.stack.write(uint8(int8(a) * -1))
case 6:
// and16
b := cpu.stack.read()
a := cpu.stack.read()
cpu.stack.write(a & b)
case 7:
// or16
b := cpu.stack.read()
a := cpu.stack.read()
cpu.stack.write(a | b)
case 8:
// xor16
b := cpu.stack.read()
a := cpu.stack.read()
cpu.stack.write(a ^ b)
case 9:
// not16
a := cpu.stack.read()
cpu.stack.write(^a)
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
// Get jump address off the stack
new_pc_high := cpu.devices[0].read()
new_pc_low := cpu.devices[0].read()
new_pc := uint16(new_pc_high)<<8 + uint16(new_pc_low)
// Push next address to the stack
pc_high := cpu.pc >> 8
pc_low := cpu.pc << 8 >> 8
cpu.devices[0].write(uint8(pc_low))
cpu.devices[0].write(uint8(pc_high))
// Jump
cpu.pc = new_pc
case 21:
// trap
case 22:
// Get return address off the stack
pc_high := cpu.devices[0].read()
pc_low := cpu.devices[0].read()
pc := uint16(pc_high)<<8 + uint16(pc_low)
// Jump
cpu.pc = pc
case 23:
// iret
default:
// device := stacki - 24
// TODO: enable device
}
}
func in(i uint8, cpu *CPU) {
device := i << 5 >> 5
destination := i << 3 >> 6
cpu.registers[destination] = cpu.devices[device].read()
}
func out(i uint8, cpu *CPU) {
device := i << 3 >> 5
source := i << 6 >> 6
cpu.devices[device].write(cpu.registers[source])
}
func illegal(i uint8, cpu *CPU) {
}