-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathops.go
More file actions
487 lines (397 loc) · 7.17 KB
/
ops.go
File metadata and controls
487 lines (397 loc) · 7.17 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
"unicode"
)
var KBINTERRUPT = make(chan bool)
const (
// memRoot is squared to get the memory size
memRoot = 32
memSize = memRoot * memRoot // 1024 addresses
)
type memory [memSize]value
type addr uint32
type value uint32
type Opcode uint16
const (
IX addr = memSize - 1 - iota
ACC
PC
COMP
O_LDM Opcode = 1
O_LDD = 2
O_LDI = 3
O_LDX = 4
O_LDR = 5
O_STO = 6
O_STA = 7
O_STX = 8
O_ADD = 9
O_INC = 10
O_DEC = 11
O_JMP = 12
O_JMPA = 13
O_CMPA = 14
O_CMPV = 15
O_JPE = 16
O_JPN = 17
O_JGT = 18
O_JLT = 19
O_IN = 20
O_OUT = 21
O_END = 22
O_AND = 23
O_OR = 24
O_WMI = 25
O_XOR = 26
)
type Op interface {
Exec()
}
type LDM struct {
val value
mem *memory
}
func newLDM(val value, mem *memory) LDM {
return LDM{val, mem}
}
func (op LDM) Exec() {
op.mem[ACC] = op.val
}
type LDD struct {
src addr
mem *memory
}
func newLDD(src addr, mem *memory) LDD {
return LDD{src, mem}
}
func (op LDD) Exec() {
op.mem[ACC] = op.mem[op.src]
}
type LDI struct {
addressSrc addr
mem *memory
}
func newLDI(addressSrc addr, mem *memory) LDI {
return LDI{addressSrc, mem}
}
func (op LDI) Exec() {
op.mem[ACC] = op.mem[op.mem[op.addressSrc]]
}
type LDX struct {
index addr
mem *memory
}
func newLDX(index addr, mem *memory) LDX {
return LDX{index, mem}
}
func (op LDX) Exec() {
op.mem[ACC] = op.mem[op.index+addr(op.mem[IX])]
}
type LDR struct {
val value
mem *memory
}
func newLDR(val value, mem *memory) LDR {
return LDR{val, mem}
}
func (op LDR) Exec() {
op.mem[IX] = op.val
}
type STO struct {
dest addr
mem *memory
}
func newSTO(dest addr, mem *memory) STO {
return STO{dest, mem}
}
func (op STO) Exec() {
op.mem[op.dest] = op.mem[ACC]
}
type STX struct {
dest addr
mem *memory
}
func newSTX(dest addr, mem *memory) STX {
return STX{dest, mem}
}
func (op STX) Exec() {
op.mem[op.dest+addr(op.mem[IX])] = op.mem[ACC]
}
type STA struct {
val value
mem *memory
}
func newSTA(val value, mem *memory) STA {
return STA{val, mem}
}
func (op STA) Exec() {
op.mem[addr(op.mem[ACC])] = op.val
}
type ADD struct {
src addr
mem *memory
}
func newADD(src addr, mem *memory) ADD {
return ADD{src, mem}
}
func (op ADD) Exec() {
op.mem[ACC] += op.mem[op.src]
}
type INC struct {
reg addr
mem *memory
}
func newINC(reg addr, mem *memory) INC {
if reg != ACC && reg != IX {
reg = ACC
}
return INC{reg, mem}
}
func (op INC) Exec() {
op.mem[op.reg] += 1
if DEBUG {
reg := "ACC"
if op.reg == IX {
reg = "IX"
}
Println("INC'd", reg, "to", op.mem[op.reg])
}
}
type DEC struct {
reg addr
mem *memory
}
func newDEC(reg addr, mem *memory) DEC {
if reg != ACC && reg != IX {
reg = ACC
}
return DEC{reg, mem}
}
func (op DEC) Exec() {
op.mem[op.reg] -= 1
if DEBUG {
reg := "ACC"
if op.reg == IX {
reg = "IX"
}
Println("DEC'd", reg, "to", op.mem[op.reg])
}
}
type JMP struct {
loc addr
mem *memory
}
func newJMP(loc addr, mem *memory) JMP {
return JMP{loc, mem}
}
func (op JMP) Exec() {
op.mem[PC] = value(op.loc)
}
type JMPA struct {
mem *memory
}
func newJMPA(mem *memory) JMPA {
return JMPA{mem}
}
func (op JMPA) Exec() {
op.mem[PC] = value(op.mem[ACC])
}
// CMP #n
type CMPval struct {
val value
mem *memory
}
func newCMPval(val value, mem *memory) CMPval {
return CMPval{val, mem}
}
func (op CMPval) Exec() {
if op.mem[ACC] > op.val {
op.mem[COMP] = 2
} else if op.mem[ACC] < op.val {
op.mem[COMP] = 0
} else if op.mem[ACC] == op.val {
op.mem[COMP] = 1
}
}
type CMPaddr struct {
src addr
mem *memory
}
func newCMPaddr(src addr, mem *memory) CMPaddr {
return CMPaddr{src, mem}
}
func (op CMPaddr) Exec() {
(&CMPval{
val: op.mem[op.src],
mem: op.mem,
}).Exec()
}
type JPE struct {
loc addr
mem *memory
}
func newJPE(loc addr, mem *memory) JPE {
return JPE{loc, mem}
}
func (op JPE) Exec() {
if op.mem[COMP] == 1 {
op.mem[PC] = value(op.loc)
}
}
type JPN struct {
loc addr
mem *memory
}
func newJPN(loc addr, mem *memory) JPN {
return JPN{loc, mem}
}
func (op JPN) Exec() {
if op.mem[COMP] != 1 {
op.mem[PC] = value(op.loc)
}
}
type JGT struct {
loc addr
mem *memory
}
func newJGT(loc addr, mem *memory) JGT {
return JGT{loc, mem}
}
func (op JGT) Exec() {
if op.mem[COMP] == 2 {
op.mem[PC] = value(op.loc)
}
}
type JLT struct {
loc addr
mem *memory
}
func newJLT(loc addr, mem *memory) JLT {
return JLT{loc, mem}
}
func (op JLT) Exec() {
if op.mem[COMP] == 0 {
op.mem[PC] = value(op.loc)
}
}
// When IN is called, the character from the position (pos) in the buffer is loaded into the ACC. pos is then incremented.
// The buffer stores the most recent line.
// When a new line is entered, pos is set to zero and the line is written to buffer.
type stdinBuffer struct {
buffer []byte
pos int
}
// Capture continously writes new stdin input to the buffer.
func (b *stdinBuffer) Capture() {
var err error
reader := bufio.NewReader(os.Stdin)
for {
b.buffer, err = reader.ReadBytes('\n')
b.pos = 0
Println("NEW INPUT:", strings.ReplaceAll(string(b.buffer), "\n", "\\n"))
if err != nil {
panic(fmt.Sprintf("Failed to read from os.Stdin: %v", err))
}
}
}
type IN struct {
mem *memory
}
func newIN(mem *memory) IN {
return IN{mem}
}
func (op IN) Exec() {
// Last comparison skips the newline. Might break some programs, idk.
for StdinBuffer.buffer == nil || len(StdinBuffer.buffer) == 0 || (len(StdinBuffer.buffer) > 0 && StdinBuffer.buffer[StdinBuffer.pos] == '\n') {
continue
}
char := string(StdinBuffer.buffer)[StdinBuffer.pos]
Println("GOT IN", char)
if char > unicode.MaxASCII {
panic(fmt.Errorf("Character was outside ASCII range"))
}
op.mem[ACC] = value(char)
StdinBuffer.pos++
}
type stdout struct{}
func (w stdout) Write(p []byte) (n int, err error) {
if TABLE {
outContent += string(p)
} else {
return os.Stdout.Write(p)
}
return len(p), nil
}
type OUT struct {
mem *memory
}
func newOUT(mem *memory) OUT {
return OUT{mem}
}
func (op OUT) Exec() {
Println("OUTING", op.mem[ACC])
out := []byte{byte(op.mem[ACC])}
n, err := Out.Write(out)
if n != 1 || err != nil {
panic(err)
}
}
type END struct{}
func newEND() END { return END{} }
func (op END) Exec() {
os.Exit(0)
}
type AND struct {
src addr
mem *memory
}
func newAND(src addr, mem *memory) AND {
return AND{src, mem}
}
func (op AND) Exec() {
op.mem[ACC] = (op.mem[ACC]) & (op.mem[op.src])
}
type OR struct {
src addr
mem *memory
}
func newOR(src addr, mem *memory) OR {
return OR{src, mem}
}
func (op OR) Exec() {
op.mem[ACC] = (op.mem[ACC]) | (op.mem[op.src])
}
type XOR struct {
src addr
mem *memory
}
func newXOR(src addr, mem *memory) XOR {
return XOR{src, mem}
}
func (op XOR) Exec() {
op.mem[ACC] = (op.mem[ACC]) & (op.mem[op.src])
}
type WMI struct {
wait time.Duration
}
func newWMI(ms value) WMI {
return WMI{time.Duration(ms) * time.Millisecond}
}
func (op WMI) Exec() {
timechan := make(chan bool)
go func() {
time.Sleep(op.wait)
timechan <- true
}()
select {
case <-KBINTERRUPT:
break
case <-timechan:
break
}
}