Repository navigation
Expand file tree
/
Copy pathparser.go
More file actions
416 lines (410 loc) · 9.6 KB
/
Copy pathparser.go
File metadata and controls
416 lines (410 loc) · 9.6 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
package main
import (
"errors"
"fmt"
"strconv"
"strings"
"unicode"
)
type unsatisfiedLabel struct {
address addr
lineNum int
}
func populateMemory(file []string) (*memory, map[string]addr) {
var lineCount uint16 = 1
labels := map[string]addr{}
labeledValues := map[string]addr{}
// If usage of a label is encountered before creation,
// unsatisfiedLabels[name] = line num
// At end, if any remaining 65536's, error.
unsatisfiedLabels := map[string][]unsatisfiedLabel{}
firstInstruction := true
showAddresses := map[string]addr{}
var mem memory
for lineNum, l := range file {
isLabeledValue := false
if l == "" {
continue
}
for l[0] == ' ' || l[0] == '\t' {
l = l[1:]
}
if l[0] == ';' || l[0] == '#' {
continue
}
labelSects := strings.Split(l, ":")
if strings.Contains(l, ":") {
if len(labelSects) == 1 || labelSects[1] == "" {
// labeled section
labels[labelSects[0]] = addr(lineCount)
if labels, ok := unsatisfiedLabels[labelSects[0]]; ok {
for _, label := range labels {
Printf("Satisfying unsatisfied label \"%s\" on line %d with address %d\n", labelSects[0], label.lineNum, label.address)
mem[label.address] += value(lineCount)
delete(unsatisfiedLabels, labelSects[0])
}
}
for _, v := range SHOWMEM {
if labelSects[0] == v {
showAddresses[labelSects[0]] = addr(lineCount)
break
}
}
continue
} else {
// labeled value
isLabeledValue = true
}
}
var sects []string
out := ""
inQuote := false
for _, c := range l {
if c == '"' {
inQuote = !inQuote
}
if c == ' ' && !inQuote {
sects = append(sects, out)
out = ""
continue
}
out += string(c)
}
if out != "" {
sects = append(sects, out)
}
var code string
i := 0
for _, c := range sects[0] {
if !unicode.IsUpper(c) {
break
}
i++
}
code = sects[0][0:i]
var arg []uint32
hasArg := false
if len(sects) != 1 {
hasArg = true
argString := ""
inQuote := false
for _, c := range sects[1] {
if inQuote || c != ' ' {
argString += string(c)
if c == '"' {
inQuote = !inQuote
}
}
}
switch argString[0] {
case '#':
i, err := strconv.ParseUint(argString[1:], 10, 16)
if err != nil {
panic(fmt.Errorf("%d: Error parsing number: %v", lineNum, err))
}
arg = []uint32{uint32(i)}
case '"':
i := len(argString) - 1
for argString[i] != '"' && i >= 0 {
i--
}
if i == 0 {
continue
}
replacer := strings.NewReplacer("\\n", "\n", "\\\"", "\"")
content := replacer.Replace(argString[1:i])
arg = make([]uint32, len(content)+1)
for i := range content {
arg[i] = uint32(content[i])
}
default:
if argString[0] == 'B' && (argString[1] == '0' || argString[1] == '1') {
i, err := strconv.ParseUint(argString[1:], 2, 16)
if err != nil {
panic(fmt.Errorf("%d: Error parsing binary constant: %v", lineNum, err))
}
arg = []uint32{uint32(i)}
} else {
i, err := strconv.ParseUint(argString, 2, 16)
if err != nil {
i, err = strconv.ParseUint(argString, 10, 16)
}
if err != nil {
var address addr
if argString == "ACC" {
address = ACC
} else if argString == "IX" {
address = IX
} else {
var ok bool
address, ok = labels[argString]
if !ok {
a, ok := labeledValues[argString]
if !ok {
unsatisfiedLabels[argString] = append(unsatisfiedLabels[argString], unsatisfiedLabel{address: addr(lineCount), lineNum: lineNum})
address = 0
} else {
address = addr(a)
}
}
}
arg = []uint32{uint32(address)}
} else {
arg = []uint32{uint32(i)}
}
}
}
}
if code != "IN" && code != "OUT" && code != "END" && code != "JMPA" {
if !hasArg {
panic(fmt.Errorf("%d: No argument given when required: %s", lineNum, l))
}
i := lineCount
for _, v := range arg {
mem[i] = value(v)
i++
}
}
isInstruction := true
switch code {
case "LDM":
mem[lineCount] += value(O_LDM) << 15
case "LDD":
mem[lineCount] += value(O_LDD) << 15
case "LDI":
mem[lineCount] += value(O_LDI) << 15
case "LDX":
mem[lineCount] += value(O_LDX) << 15
case "LDR":
mem[lineCount] += value(O_LDR) << 15
case "STO":
mem[lineCount] += value(O_STO) << 15
case "STA":
mem[lineCount] += value(O_STA) << 15
case "STX":
mem[lineCount] += value(O_STX) << 15
case "ADD":
mem[lineCount] += value(O_ADD) << 15
case "INC":
mem[lineCount] += value(O_INC) << 15
case "DEC":
mem[lineCount] += value(O_DEC) << 15
case "JMP":
mem[lineCount] += value(O_JMP) << 15
case "JMPA":
mem[lineCount] += value(O_JMPA) << 15
case "CMPA":
mem[lineCount] += value(O_CMPA) << 15
case "CMPV":
mem[lineCount] += value(O_CMPV) << 15
case "JPE":
mem[lineCount] += value(O_JPE) << 15
case "JPN":
mem[lineCount] += value(O_JPN) << 15
case "JGT":
mem[lineCount] += value(O_JGT) << 15
case "JLT":
mem[lineCount] += value(O_JLT) << 15
case "IN":
mem[lineCount] += value(O_IN) << 15
case "OUT":
mem[lineCount] += value(O_OUT) << 15
case "END":
mem[lineCount] += value(O_END) << 15
case "AND":
mem[lineCount] += value(O_AND) << 15
case "OR":
mem[lineCount] += value(O_OR) << 15
case "XOR":
mem[lineCount] += value(O_XOR) << 15
case "WMI":
mem[lineCount] += value(O_WMI) << 15
case "PRINT":
/*
LDR #0
LOOP:
LDI <address>
OUT
INC IX)
CMPV #10
JPN LOOP
*/
mem[lineCount] -= value(arg[0])
mem[lineCount] += value(O_LDR) << 15
lineCount++
loop := lineCount
mem[loop] += (value(O_LDX) << 15) + value(arg[0])
lineCount++
mem[lineCount] += value(O_OUT) << 15
lineCount++
mem[lineCount] += (value(O_INC) << 15) + value(IX)
lineCount++
mem[lineCount] += (value(O_CMPV) << 15) + 0
lineCount++
mem[lineCount] += (value(O_JPN) << 15) + value(loop)
default:
isInstruction = false
if !isLabeledValue {
Printf("%d: Skipping line: %v\n", lineNum, l)
continue
}
}
if firstInstruction && isInstruction {
Println("First instruction @", lineCount)
mem[0] = value(O_JMP<<15) + value(lineCount)
firstInstruction = false
}
if isLabeledValue {
labeledValues[labelSects[0]] = addr(lineCount)
if labels, ok := unsatisfiedLabels[labelSects[0]]; ok {
for _, label := range labels {
Printf("Satisfying unsatisfied label \"%s\" on line %d with address %d\n", labelSects[0], label.lineNum, label.address)
mem[label.address] += value(lineCount)
delete(unsatisfiedLabels, labelSects[0])
}
}
for _, v := range SHOWMEM {
if labelSects[0] == v {
showAddresses[labelSects[0]] = addr(lineCount)
break
}
}
if len(arg) > 1 {
lineCount += uint16(len(arg) - 1)
}
isLabeledValue = false
}
// Only increment lineCount if line was valid op.
lineCount++
}
errOut := ""
for label, line := range unsatisfiedLabels {
errOut += fmt.Sprintf("%d: Label \"%s\" not defined\n", line, label)
}
if errOut != "" {
panic(errors.New(errOut))
}
return &mem, showAddresses
}
func parseInstruction(val value, mem *memory) (*Op, bool) {
Println("COD", strconv.FormatUint(uint64(val), 2))
opc := uint16(val >> 15)
isConstant := opc&uint16(1<<15) == uint16(1<<15)
if isConstant {
opc -= uint16(1 << 15)
}
arg := uint16(val) - (opc << 15)
opcode := Opcode(opc)
var op Op
ok := true
switch opcode {
case O_LDM:
op = newLDM(value(arg), mem)
Println("LDM")
case O_LDD:
op = newLDD(addr(arg), mem)
Println("LDD")
case O_LDI:
op = newLDI(addr(arg), mem)
Println("LDI")
case O_LDX:
op = newLDX(addr(arg), mem)
Println("LDX")
case O_LDR:
op = newLDR(value(arg), mem)
Println("LDR")
case O_STO:
op = newSTO(addr(arg), mem)
Println("STO")
case O_STA:
op = newSTA(value(arg), mem)
Println("STA")
case O_STX:
op = newSTX(addr(arg), mem)
Println("STX")
case O_ADD:
op = newADD(addr(arg), mem)
Println("ADD")
case O_INC:
op = newINC(addr(arg), mem)
Println("INC")
case O_DEC:
op = newDEC(addr(arg), mem)
Println("DEC")
case O_JMP:
op = newJMP(addr(arg), mem)
Println("JMP")
case O_JMPA:
op = newJMPA(mem)
Println("JMPA")
case O_CMPA:
Println("CMP Address")
op = newCMPaddr(addr(arg), mem)
case O_CMPV:
Println("CMP Value")
op = newCMPval(value(arg), mem)
case O_JPE:
op = newJPE(addr(arg), mem)
Println("JPE")
case O_JPN:
op = newJPN(addr(arg), mem)
Println("JPN")
case O_JGT:
op = newJGT(addr(arg), mem)
Println("JGT")
case O_JLT:
op = newJLT(addr(arg), mem)
Println("JLT")
case O_IN:
op = newIN(mem)
Println("IN")
case O_OUT:
op = newOUT(mem)
Println("OUT")
case O_END:
op = newEND()
Println("END")
case O_AND:
op = newAND(addr(arg), mem)
Println("AND")
case O_OR:
op = newOR(addr(arg), mem)
Println("OR")
case O_XOR:
op = newXOR(addr(arg), mem)
Println("XOR")
case O_WMI:
op = newWMI(value(arg))
Println("WMI")
default:
ok = false
}
return &op, ok
}
func MarshalMemory(mem *memory) []byte {
out := make([]byte, 4*memSize)
for j, v := range mem {
i := j * 4
b1 := byte((v & 0b11111111000000000000000000000000) >> 24)
b2 := byte((v & 0b00000000111111110000000000000000) >> 16)
b3 := byte((v & 0b00000000000000001111111100000000) >> 8)
b4 := byte((v & 0b00000000000000000000000011111111))
out[i] = b1
out[i+1] = b2
out[i+2] = b3
out[i+3] = b4
}
return out
}
func UnmarshalMemory(mem []byte) *memory {
var out memory
for j := range out {
i := j * 4
val := value(mem[i]) << 24
val += value(mem[i+1]) << 16
val += value(mem[i+2]) << 8
val += value(mem[i+3])
out[j] = val
}
return &out
}