-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu.jl
721 lines (538 loc) · 15.8 KB
/
cpu.jl
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
# Julia virtual CPU created by Masterfoxify
# Called using: julia cpu.jl path/to/file.jlasm [optional debugging method, type "true" if you want debugging text turned on]
# Memory pulled out so subroutines can share memory space
memory = Array{UInt8}(undef, 5000)
for (byte, x) in enumerate(memory)
memory[byte] = 0x00
end
function debug(s::AbstractString)
if size(ARGS)[1] > 1
if ARGS[2] == "true"
println(s)
end
end
end
function cpu(filepath::String, pushes = undef, startat = undef)
global memory
# Registers. https://www.swansontec.com/sregisters.html is a good reference. Based mostly on this
A::UInt16 = 0x00 # Accumulator
B::UInt16 = 0x00 # Open Register
C::UInt16 = 0xff # Count Register
D::UInt16 = 0x00 # Data Register
RP::UInt16 = 0x01 # Read Pointer
WP::UInt16 = 0x01 # Write Pointer
SP::UInt16 = 0x00 # Stack Pointer
# Interrupts
UOI::Bool = false # User Input Overflow Interrupt. Goes back to line 1 in code
# Flags
OF::Bool = false # Open Flag, for any programmer use
OF2::Bool = false
OF3::Bool = false
# Stack inits (memory pulled out for constant memory)
stack::Array{UInt8} = Array{UInt8}(undef, 1000)
for (byte, x) in enumerate(stack)
stack[byte] = 0x00
end
if pushes !== undef
for value in pushes
SP += 1
stack[SP] = value
end
end
function wipeinput()
i = 0
while i != 255
memory[i] = 0
end
end
function isUInt(s::AbstractString)::Bool
return tryparse(UInt, s) !== nothing
end
# File Line
fileline::UInt64 = 1
file::Array{String} = readlines(open(filepath))
labels::Dict{String, Int} = Dict{String, Int}()
for (linecount, line) in enumerate(file)
try
labelcheck = split(uppercase(replace(split(line, ";")[1], "," => " ")))[1]
if labelcheck[1] == '.'
push!(labels, replace(labelcheck, "." => "") => linecount)
end
catch BoundsError
end
end
if startat !== undef
if tryparse(UInt64, startat) !== nothing
fileline = parseint(UInt64, startat)
else
fileline = labels[uppercase(startat)]
end
end
debug("Labels: $labels\n")
# Loads a value. Checks for register addressing, register values, characters, and numerical values. Note for characters, it will ONLY return one character
function loadvalue(stringvalue::AbstractString)::UInt
if isUInt(stringvalue)
return parse(UInt16, stringvalue)
else
potential_char = match(r"\"(\\\\|\\\"|[^\"])*\"", stringvalue)
if sizeof(potential_char) != 0
return UInt(potential_char[1])
end
end
if occursin("%", stringvalue)
register = replace(stringvalue, "%" => "")
if register == "A"
return memory[A]
elseif register == "B"
return memory[B]
elseif register == "C"
return memory[C]
elseif register == "D"
return memory[D]
elseif isUInt(replace(stringvalue, "%" => ""))
return memory[parse(UInt, replace(stringvalue, "%" => ""))]
else
throw("Incorrect register value")
end
else
if stringvalue == "A"
return A
elseif stringvalue == "B"
return B
elseif stringvalue == "C"
return C
elseif stringvalue == "D"
return D
elseif stringvalue == "RP"
return RP
elseif stringvalue == "WP"
return WP
else
throw("Incorrect register value")
end
end
end
# Writes to register or location. Note, pointer addresses cannot be written to outside of their opcodes
function writetolocation(location::AbstractString, value)
location = string(location)
if occursin("%", location)
register = replace(location, "%" => "")
if register == "A"
memory[A] = value
elseif register == "A"
memory[B] = value
elseif register == "C"
memory[C] = value
elseif register == "D"
memory[D] = value
else # Note, pointer addresses cannot be written to outside of their opcodes
throw("Incorrect register value. Did you try to use a pointer? Note that pointers cannot be used in a address write except through their respective opcodes")
end
else
if location == "A"
A = value
elseif location == "B"
B = value
elseif location == "C"
C = value
elseif location == "D"
D = value
elseif location == "RP"
RP = value
elseif location == "WP"
WP = value
else # Note, SP cannot be written to
throw("Incorrect location value")
end
end
end
while true
if UOI
fileline = 1
end
code::String = ""
instruction::String = ""
try
code = replace(uppercase(replace(split(file[fileline], ";")[1], "," => " ")), "\t" => "")
instruction = split(code)[1]
catch BoundsError
end
debug("$fileline: $code")
if size(split(code))[1] > 1
arguments::Array{String} = split(code)[2:end]
end
if instruction == "WRITE" # write [value, bit size]
if size(arguments)[1] > 2
throw("Too many arguments")
else
debug(" Writing $(arguments[1]) to $WP")
if isUInt(arguments[1]) || arguments[1] in ["A", "B", "C", "D", "%A", "%B", "%C", "%D"]
value = loadvalue(arguments[1])
if parse(Int, arguments[2]) == 16
lower = Int(value % 0x100)
upper = Int(value / 0x100)
memory[WP] = lower
WP += 1
memory[WP] = upper
elseif parse(Int, arguments[2]) == 8
memory[WP] = value
else
throw("Incorrect bit size argument")
end
else
for char in arguments[1]
memory[WP] = Int(char)
WP += 1
end
end
end
elseif instruction == "STRWRITE" # strwrite [string]
stringtowrite = replace(replace(split(file[fileline], ";")[1], "\t" => "")[10:end], "\\n" => "\n")
debugmessage = replace(" Wrote \"$(stringtowrite)\" to $WP through ", "\n" => "\n\t")
for char in stringtowrite
memory[WP] = Int(char)
WP += 1
end
debugmessage = string(debugmessage, "$WP")
debug(debugmessage)
elseif instruction == "READ" # read [register, bit size]
if size(arguments)[1] > 2
throw("Too many arguments")
else
if parse(Int, arguments[2]) == 16
value = memory[RP]
RP += 1
value += memory[RP]
elseif parse(Int, arguments[2]) == 8
value = memory[RP]
else
throw("Incorrect bit size argument")
end
register = arguments[1]
if register == "A"
A = value
elseif register == "B"
B = value
elseif register == "C"
C = value
elseif register == "D"
D = value
elseif register == "RP"
RP = value
elseif register == "WP"
WP = value
else
throw("Incorrect register value")
end
end
elseif instruction == "LOAD" # load [register/pointer, value/register/%register]
if size(arguments)[1] > 2
throw("Too many arguments")
else
location::String = arguments[1]
value = loadvalue(arguments[2])
writetolocation(location, value)
debug(" Wrote $value to $location")
end
elseif instruction == "PUSH" # push [value, bit size]
if size(arguments)[1] > 2
throw("Too many arguments")
else
value = loadvalue(arguments[1])
if parse(Int, arguments[2]) == 16
lower = Int(value % 0x100)
upper = Int(value / 0x100)
SP += 1
stack[SP] = lower
debug(" Wrote $value to stack at position $SP")
SP += 1
stack[SP] = upper
debug(" Wrote $value to stack at position $SP")
elseif parse(Int, arguments[2]) == 8
SP += 1
stack[SP] = value
debug(" Wrote $value to stack at position $SP")
end
end
elseif instruction == "POP" # pop [location, bit size]
if size(arguments)[1] > 2
throw("Too many arguments")
else
if parse(Int, arguments[2]) == 16
value = stack[SP]
SP -= 1
value += stack[SP]
SP -= 1
writetolocation(arguments[1], value)
debug(" Wrote $value to $(arguments[1]) from position $(SP + 2)")
debug(" SP now at: $SP")
elseif parse(Int, arguments[2]) == 8
value = stack[SP]
SP -= 1
writetolocation(arguments[1], value)
debug(" Wrote $value to $(arguments[1]) from position $(SP + 1)")
debug(" SP now at: $SP")
else
throw("Incorrect bit size argument")
end
end
elseif instruction == "GETIN"
if size(arguments)[1] > 0
throw("Too many arguments")
else
input::String = readline(STDIN)
if sizeof(input) > 255
OUI = true
else
for (i, char) in enumerate(input)
address = sizeof(memory)[1] - 255 + i
if !(address == sizeof(memory)[1])
memory[end - 255 + i] = char
else
break
end
end
end
end
elseif instruction == "WIPEIN"
if size(arguments)[1] > 0
throw("Too many arguments")
else
wipeinput()
end
elseif instruction == "ADD" # add [location, value]
if size(arguments)[1] > 2
throw("Too many arguments")
else
dvalue = loadvalue(arguments[1])
daddvalue = loadvalue(arguments[2])
writetolocation(arguments[1], loadvalue(arguments[1]) + loadvalue(arguments[2]))
debug(" Added $daddvalue to $(arguments[1])\n\tOriginal Value: $dvalue\n\tNew Value: $(loadvalue(arguments[1]))")
end
elseif instruction == "SUB" # sub [location, value]
if size(arguments)[1] > 2
throw("Too many arguments")
else
dvalue = loadvalue(arguments[1])
dsubvalue = loadvalue(arguments[2])
writetolocation(arguments[1], loadvalue(arguments[1]) - loadvalue(arguments[2]))
debug(" Subtracted $dsubvalue from $(arguments[1])\n\tOriginal Value: $dvalue\n\tNew Value: $(loadvalue(arguments[1]))")
end
elseif instruction == "ITER" # iter [pointer]
if size(arguments)[1] > 1
throw("Too many arguments")
else
if arguments[1] == "RP"
RP += 1
debug(" Iterated RP to $RP")
elseif arguments[1] == "WP"
WP += 1
debug(" Iterated WP to $WP")
end
end
elseif instruction == "JMP" # jmp [line]
if size(arguments)[1] > 1
throw("Too many arguments")
else
fileline = loadvalue(arguments[1])
continue
end
elseif instruction == "JEQ" # Jumps if two values are equal: jeq [location, value, line]
if size(arguments)[1] > 3
throw("Too many arguments")
else
debug(" ARG1: $(loadvalue(arguments[1]))\n\tARG2: $(loadvalue(arguments[2]))")
debug(" ARG1 == ARG2: $(loadvalue(arguments[1]) == loadvalue(arguments[2]))")
if loadvalue(arguments[1]) == loadvalue(arguments[2])
fileline = loadvalue(arguments[3])
continue
end
end
elseif instruction == "JNEQ" # Jumps if two values are not equal: jneq [location, value, line]
if size(arguments)[1] > 3
throw("Too many arguments")
else
debug(" ARG1: $(loadvalue(arguments[1]))\n\tARG2: $(loadvalue(arguments[2]))")
debug(" ARG1 != ARG2: $(loadvalue(arguments[1]) != loadvalue(arguments[2]))")
if loadvalue(arguments[1]) != loadvalue(arguments[2])
fileline = loadvalue(arguments[3])
continue
end
end
elseif instruction == "JGT" # Jumps if value 1 is greater than value 2: jgt [location, value, line]
if size(arguments)[1] > 3
throw("Too many arguments")
else
debug(" ARG1: $(loadvalue(arguments[1]))\n\tARG2: $(loadvalue(arguments[2]))")
debug(" ARG1 > ARG2: $(loadvalue(arguments[1]) > loadvalue(arguments[2]))")
if loadvalue(arguments[1]) > loadvalue(arguments[2])
fileline = loadvalue(arguments[3])
continue
end
end
elseif instruction == "JNGT" # Jumps if value 1 is not greater than value 2: jngt [location, value, line]
if size(arguments)[1] > 3
throw("Too many arguments")
else
debug(" ARG1: $(loadvalue(arguments[1]))\n\tARG2: $(loadvalue(arguments[2]))")
debug(" ARG1 !> ARG2: $(!(loadvalue(arguments[1]) > loadvalue(arguments[2])))")
if !(loadvalue(arguments[1]) > loadvalue(arguments[2]))
fileline = loadvalue(arguments[3])
continue
end
end
elseif instruction == "JLT" # Jumps if value 1 is less than value 2: jlt [location, value, line]
if size(arguments)[1] > 3
throw("Too many arguments")
else
debug(" ARG1: $(loadvalue(arguments[1]))\n\tARG2: $(loadvalue(arguments[2]))")
debug(" ARG1 < ARG2: $(loadvalue(arguments[1]) < loadvalue(arguments[2]))")
if loadvalue(arguments[1]) < loadvalue(arguments[2])
fileline = loadvalue(arguments[3])
continue
end
end
elseif instruction == "JNLT" # Jumps if value 1 is not less than value 2: jlt [location, value, line]
if size(arguments)[1] > 3
throw("Too many arguments")
else
debug(" ARG1: $(loadvalue(arguments[1]))\n\tARG2: $(loadvalue(arguments[2]))")
debug(" ARG1 !< ARG2: $((loadvalue(arguments[1]) < loadvalue(arguments[2])))")
if !(loadvalue(arguments[1]) < loadvalue(arguments[2]))
fileline = loadvalue(arguments[3])
continue
end
end
elseif instruction == "JIF" # Jumps if open flag is true: jif [flag, line]
if size(arguments)[1] > 2
throw("Too many arguments")
else
debug("$(arguments[1]): $(arguments[1] == "OF" ? OF : (arguments[1] == "OF2" ? OF2 : (arguments[1] == "OF3" ? OF3 : "Error")))")
flag = arguments[1]
line = parse(Int, arguments[2])
if flag == "OF"
if OF
fileline = line
continue
end
elseif flag == "OF2"
if OF2
fileline = line
continue
end
elseif flag == "OF3"
if OF3
fileline = line
continue
end
end
end
elseif instruction == "TOGGLE" # Toggles flag value: toggle [flag]
if size(arguments)[1] > 1
throw("Too many arguments")
else
flag = arguments[1]
if flag == "OF"
OF = !OF
debug(" OF: $OF")
elseif flag == "OF2"
OF2 = !OF2
debug(" OF2: $OF2")
elseif flag == "OF3"
OF3 = !OF3
debug(" OF3: $OF3")
end
end
elseif instruction == "PRINT" # print [char amount]
if size(arguments)[1] > 2
throw("Too many arguments")
else
i = RP + loadvalue(arguments[1])
while RP < i
print(Char(memory[RP]), "")
RP += 1
end
debug("")
end
elseif instruction == "GOTO" # goto [label]
fileline = labels[replace(arguments[1], "." => "")]
elseif instruction == "CALL" # call [filename, amount to pop, label/line to jump to]
if size(arguments)[1] > 3
throw("Too many arguments")
else
call::String = split(replace(split(file[fileline], ";")[1], "," => " "))[2]
debug(" Loading $call as potential call.")
debug(" $call does $(ifelse(in('/', call), "", "not "))have a folder.")
if ! in('/', call)
call = string("std/", call)
debug(" Loading $call as std module.")
end
debug(" $call does $(ifelse(in('/', call), "", "not "))have an extension.")
if ! in('.', call)
call = string(call, ".jlasm")
debug(" Loading $call as jlasm file.")
end
call = replace(call, ":" => "/")
if size(arguments)[1] >= 2
i = 1
if loadvalue(arguments[2]) != 0
vals_to_push::Array{UInt8} = Array{UInt8}(undef, loadvalue(arguments[2]))
while i <= loadvalue(arguments[2])
vals_to_push[i] = stack[SP]
SP -= 1
i += 1
end
debug(" Loading File: $(split(call, "/")[end])\n")
if size(arguments)[1] == 3
try
stackvals = cpu(call, vals_to_push, loadvalue(arguments[3]))
catch Exception
stackvals = cpu(call, vals_to_push, arguments[3])
end
else
stackvals = cpu(call, vals_to_push)
end
for value in stackvals
SP += 1
stack[SP] = value
end
else
if size(arguments)[1] == 3
try
cpu(call, loadvalue(arguments[3]))
catch Exception
cpu(call, arguments[3])
end
else
cpu(call)
end
end
else
cpu(call)
end
end
elseif instruction == "HLT"
break
end
fileline += 1
if fileline > size(file)[1]
break
end
end
if pushes !== undef
vals_to_return::Array{UInt8} = Array{UInt8}(undef, SP)
i = 0
while SP > 1
vals_to_return[i] = stack[SP]
SP -= 1
i += 1
end
return vals_to_return
end
end
if size(ARGS)[1] > 0
debug("Loading File: $(ARGS[1])")
cpu(ARGS[1])
else
debug("Error: No file loaded.\nPlease type: julia cpu.jl [path/to/file]")
end