-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathATR2.py
executable file
·3614 lines (3469 loc) · 116 KB
/
ATR2.py
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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
### A very incomplete to-do list, please add to this things you see wrong!##########################
# Robots aren't being compiled properly
# Need to figure out what keypressed is supposed to do, can use pygame for this, until then keypressed returns false
# keypressed basically shows whether a key was pressed or not but it
# unfortunately has something to do with checking the buffer (which is the
# reason for all of those calls to 'clear buffer'). I'm not sure whether or not
# this'll cause a problem once we use Pygame.
# More info: https://www.freepascal.org/docs-html/rtl/crt/keypressed.html
####################################################################################################
# Copyright (c) 1999, Ed T. Toton III. All rights reserved.
#
# Bug fixes and additional additions Copyright (c) 2014, William "Amos" Confer
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# All advertising materials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developed by Ed T. Toton III &
# NecroBones Enterprises.
# No modified or derivative copies or software may be distributed in the
# guise of official or original releases/versions of this software. Such
# works must contain acknowledgement that it is modified from the original.
# Neither the name of the author nor the name of the business or
# contributers may be used to endorse or promote products derived
# from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
def keypressed():
return False
from ATR2FUNC import *
import random
import time
import os
import pdb
import pygame
import sys
progname = 'AT-Robots'
version = '2.11'
cnotice1 = 'Copyright 1997 ''99, Ed T. Toton III'
cnotice2 = 'All Rights Reserved.'
cnotice3 = 'Copyright 2014, William "Amos" Confer'
main_filename = 'ATR2'
robot_ext = '.AT2'
locked_ext = '.ATL'
config_ext = '.ATS'
compile_ext = '.CMP'
report_ext = '.REP'
_T = True
_F = False
maxint = 32787
minint = -32768
# debugging/compiler
show_code = True
compile_by_line = False
max_var_len = 16
debugging_compiler = True
# robots
max_robots = 31 # starts at 0, so total is max_robots + 1
max_code = 1023 # same here
max_op = 3 # etc...
stack_size = 256
stack_base = 768
max_ram = 1023 # but this does start at 0 (odd #, 2 ^ n - 1)
max_vars = 256
max_labels = 256
acceleration = 4
turn_rate = 8
max_vel = 4
max_missiles = 1023
missile_spd = 32
hit_range = 14
blast_radius = 25
crash_range = 8
max_sonar = 250
com_queue = 512
max_queue = 255
max_config_points = 12
max_mines = 63
mine_blast = 35
# simulator & graphics
screen_scale = 0.46
screen_x = 5
screen_y = 5
robot_scale = 6
default_delay = 20
default_slice = 5
mine_circle = int(mine_blast * screen_scale) + 1
blast_circle = int(blast_radius * screen_scale) + 1
mis_radius = int(hit_range / 2.0) + 1
max_robot_lines = 8
# gray50 thing goes here
class op_rec:
op = [0 for i in range(0, max_op + 1)]
prog_type = [op_rec() for i in range(0, max_code + 1)]
# note: must be careful when accessing values of prog_type if we need attribute of class
class config_rec:
scanner = 0
weapon = 0
armor = 0
engine = 0
heatsinks = 0
shield = 0
mines = 0
class mine_rec:
x = 0
y = 0
detect = 0
_yield = 0
detonate = False
class robot_rec:
is_locked = False
mem_watch = 0
x = 0
y = 0
lx = 0
ly = 0
xv = 0
yv = 0
speed = 0
shotstrength = 0
damageadj = 0
speedadj = 0
meters = 0
hd = 0
thd = 0
lhd = 0
spd = 0
tspd = 0
armor = 0
larmor = 0
heat = 0
lheat = 0
ip = 0
plen = 0
scanarc = 0
accuracy = 0
shift = 0
err = 0
delay_left = 0
robot_time_limit = 0
max_time = 0
time_left = 0
lshift = 0
arc_count = 0
sonar_count = 0
scanrange = 0
last_damage = 0
last_hit = 0
transponder = 0
shutdown = 0
channel = 0
lendarc = 0
endarc = 0
lstartarc = 0
startarc = 0
mines = 0
tx = [0 for i in range(0, max_robot_lines)]
ltx = [0 for i in range(0, max_robot_lines)]
ty = [0 for i in range(0, max_robot_lines)]
lty = [0 for i in range(0, max_robot_lines)]
wins = 0
trials = 0
kills = 0
deaths = 0
startkills = 0
shots_fired = 0
match_shots = 0
hits = 0
damage_total = 0
cycles_lived = 0
error_count = 0
config = config_rec
name = ''
fn = ''
shields_up = False
lshields = False
overburn = False
keepshift = False
cooling = False
won = False
code = prog_type
ram = [0 for i in range(0, max_ram + 1)]
mine = [mine_rec() for i in range(0, max_mines + 1)]
errorlog = open('errorlog', 'a').close() # not sure who removed this/why, but it's needed by many functions
class missile_rec:
x = 0
y = 0
lx = 0
ly = 0
mult = 0
mspd = 0
source = 0
a = 0
hd = 0
rad = 0
lrad = 0
max_rad = 0
missile = []
missile = [missile_rec() for i in range(0, max_missiles + 1)]
robot = []
robot = [robot_rec() for i in range(-2, max_robots + 3)]
# NOTE: in the original code these were just declarations, I've picked values that make sense for them to have
# robot variables
num_robots = 2
# compiler variables
# f = open('f', 'a').close()
numvars = 0
numlabels = 0
maxcode = 0
lock_pos = 0
lock_dat = 0
varname = []
varname = ['' for i in range(1, max_vars + 1)]
varloc = []
varloc = [0 for i in range(1, max_vars + 1)]
labelname = []
labelname = ['' for i in range(1, max_vars + 1)]
labelnum = [x for x in range(max_labels)]
varnum = []
varnum = [0 for i in range(1, max_labels + 1)]
show_source = True
compile_only = False
lock_code = ''
# simulator/graphics variables
bout_over = False # made global from procedure bout
step_mode = 0 # 0=off, for (0<step_mode<=9) = #of game cycles per step
temp_mode = 0 # stores previous step_mode for return to step
step_count = 1 # step counter used as break flag
step_loop = False # break flag for stepping
# show_debugger = False # flag for viewing debugger panel vs. robot stats
matches = 1
played = 0
old_shields = False
insane_missiles = False
debug_info = False
windoze = False
no_gfx = False
logging_errors = False
timing = False
show_arcs = False
game_delay = 0
time_slice = 1
insanity = 0
update_timer = 1
max_gx = 1000
max_gy = 1000
stats_mode = 0
game_limit = 100
game_cycle = 0
# general settings
_quit = False
report = True
show_cnotice = False
kill_count = 0
report_type = 0
def operand(n,m):
s = chr(n)
# Microcode:
# 0 = instruction, number, constant
# 1 = variable, memory access
# 2 = :label
# 3 = !label (unresolved)
# 4 = !label (resolved)
# 8h mask = inderect addressing (enclosed in [])
x = m & 7
if x == 1:
s = '@' + s
if x == 2:
s = ':' + s
if x == 3:
s = '$' + s
if x == 4:
s = '!' + s
else:
s = cstr(n)
if (m and 8) > 0:
s = '[' + s + ']'
return s
def mnemonic(n,m):
s = chr(n)
if m == 0:
if n == 0:
s = 'NOP'
elif n == 1:
s = 'ADD'
elif n == 2:
s = 'SUB'
elif n == 3:
s = 'OR'
elif n == 4:
s = 'AND'
elif n == 5:
s = 'XOR'
elif n == 6:
s = 'NOT'
elif n == 7:
s = 'MPY'
elif n == 8:
s = 'DIV'
elif n == 9:
s = 'MOD'
elif n == 10:
s = 'RET'
elif n == 11:
s = 'CALL'
elif n == 12:
s = 'JMP'
elif n == 13:
s = 'JLS'
elif n == 14:
s = 'JGR'
elif n == 15:
s = 'JNE'
elif n == 16:
s = 'JE'
elif n == 17:
s = 'SWAP'
elif n == 18:
s = 'DO'
elif n == 19:
s = 'LOOP'
elif n == 20:
s = 'CMP'
elif n == 21:
s = 'TEST'
elif n == 22:
s = 'MOV'
elif n == 23:
s = 'LOC'
elif n == 24:
s = 'GET'
elif n == 25:
s = 'PUT'
elif n == 26:
s = 'INT'
elif n == 27:
s = 'IPO'
elif n == 28:
s = 'OPO'
elif n == 29:
s = 'DELAY'
elif n == 30:
s = 'PUSH'
elif n == 31:
s = 'POP'
elif n == 32:
s = 'ERR'
elif n == 33:
s = 'INC'
elif n == 34:
s = 'DEC'
elif n == 35:
s = 'SHL'
elif n == 36:
s = 'SHR'
elif n == 37:
s = 'ROL'
elif n == 38:
s = 'ROR'
elif n == 39:
s = 'JZ'
elif n == 40:
s = 'JNZ'
elif n == 41:
s = 'JGE'
elif n == 42:
s = 'JLE'
elif n == 43:
s = 'SAL'
elif n == 44:
s = 'SAR'
elif n == 45:
s = 'NEG'
elif n == 46:
s = 'JTL'
else:
s = 'XXX'
else:
s = operand(n,m)
return s
def log_error(n,i,ov):
if not logging_errors:
return
else:
for n in robot:
if i == 1:
s = 'Stack full - Too many CALLs?'
elif i == 2:
s = 'Label not found. Hmmm.'
elif i == 3:
s = 'Can\'t assign value - Tisk tisk.'
elif i == 4:
s = 'Illegal memory reference'
elif i == 5:
s = 'Stack empty - Too many RETs?'
elif i == 6:
s = 'Illegal instruction. How bizarre.'
elif i == 7:
s = 'Return out of range - Woops!'
elif i == 8:
s = 'Divide by zero'
elif i == 9:
s = 'Unresolved !label. WTF?'
elif i == 10:
s = 'Invalid Interrupt Call'
elif i == 11:
s = 'Invalid Port Access'
elif i == 12:
s = 'Com Queue empty'
elif i == 13:
s = 'No mine-layer, silly.'
elif i == 14:
s = 'No mines left'
elif i == 15:
s = 'No shield installed - Arm the photon torpedoes instead. :-)'
elif i == 16:
s = 'Invalid Microcode in instruction.'
else:
s = 'Unknown error'
print(robot[n].errorlog + '<' + i + '> ' + s + ' (Line #' + robot[n].ip + ') [Cycle: ' + game_cycle + ', Match: ' + played + '/' + matches + ']\n')
print(robot[n].errorlog + ' ' + mnemonic(robot[n].code[robot[n].ip].op[0] + robot[n].code[robot[n].ip].op[3] and 15) + ' ' +
operand(robot[n].code[robot[n].ip].op[1] + (robot[n].code[robot[n].ip].op[3] >> 4) & 15) + ' + ' +
operand(robot[n].code[robot[n].ip].op[2] + (robot[n].code[robot[n].ip].op[3] >> 8) & 15))
if ov != '':
print(robot[n].errorlog + ' (Values: ' + ov + ')')
else:
print(robot[n].errorlog)
print(robot[n].errorlog + ' AX=' + addrear(chr(robot[n].ram[65])+',',7))
print(robot[n].errorlog + ' BX=' + addrear(chr(robot[n].ram[66])+',',7))
print(robot[n].errorlog + ' CX=' + addrear(chr(robot[n].ram[67])+',',7))
print(robot[n].errorlog + ' DX=' + addrear(chr(robot[n].ram[68])+',',7))
print(robot[n].errorlog + ' EX=' + addrear(chr(robot[n].ram[69])+',',7))
print(robot[n].errorlog + ' FX=' + addrear(chr(robot[n].ram[70])+',',7))
print(robot[n].errorlog + ' Flags=' + robot[n].ram[64] + '\n')
print(robot[n].errorlog + ' AX=' + addrear(hex(robot[n].ram[65])+',',7))
print(robot[n].errorlog + ' BX=' + addrear(hex(robot[n].ram[66])+',',7))
print(robot[n].errorlog + ' CX=' + addrear(hex(robot[n].ram[67])+',',7))
print(robot[n].errorlog + ' DX=' + addrear(hex(robot[n].ram[68])+',',7))
print(robot[n].errorlog + ' EX=' + addrear(hex(robot[n].ram[69])+',',7))
print(robot[n].errorlog + ' FX=' + addrear(hex(robot[n].ram[70])+',',7))
print(robot[n].errorlog + ' Flags=' + hex(robot[n].ram[64]))
print(robot[n].errorlog)
def max_shown():
if stats_mode == 1:
return 12
elif stats_mode == 2:
return 32
else:
return 6
def graph_check(n):
ok = True
if (not graphix) or (n < 0) or (n > num_robots) or (n >= max_shown):
ok = False
return ok
def robot_graph(n):
if stats_mode == 1:
viewport(480,4+n*35,635,37+n*35)
max_gx = 155
max_gy = 33
elif stats_mode == 2:
viewport(480,4+n*13,635,15+n*13)
max_gx = 155
max_gy = 11
else:
viewport(480,4+n*70,n*70)
max_gx = 155
max_gy = 66
setfillstyle(1,robot_color(n))
setcolor(robot_color(n))
def update_armor(n):
if graph_check(n) & step_mode <=0:
#needs more work
robot[n].n
robot_graph(n)
if robot[n].armor>0:
if stats_mode == 1:
bar(30,13,29+robot[n].armor,18)
bar(88,3,87+(robot[n].armor >> 2),8)
else:
bar(30,25,29+robot[n].armor,30)
setfillstyle(1,8)
if robot[n].armor < 100:
if stats_mode == 1:
bar(30+robot[n].armor,13,129,18)
elif stats_mode == 2:
bar(88+(robot[n].armor >> 2), 3,111,8)
else:
bar(30+robot[n].armor,25,129,30)
def update_heat(n):
if graph_check(n) & step_mode <= 0:
robot[n].n
robot_graph(n)
if robot[n].heat > 5:
if stats_mode == 1:
bar(30,23,29+(robot[n].heat // 5), 28)
elif stats_mode == 2:
bar(127,3,126+(robot[n].heat // 20),8)
else:
bar(30,35,29+(robot[n].heat // 5),40)
setfillstyle(1,8)
if robot[n].heat<500:
if stats_mode == 1:
bar(30,(robot[n].heat // 5),23,129,28)
elif stats_mode == 2:
bar(127+(robot[n].heat // 20), 3,151,8) # find *(heat div 20)
else:
bar(30+(robot[n].heat // 5), 35,129,40)
def robot_error(n, i, ov):
if graph_check(n) and step_mode <= 0:
if stats_mode == 0:
robot_graph(n)
setfillstyle(1,0)
bar(66,56,154,64)
setcolor(robot_color(n))
outtextxy(66,56,addrear(cstr(i), 7) + hex(i))
chirp()
if logging_errors:
log_error(n,i,ov)
robot[n].error_count +=1
def update_lives(n):
if graph_check(n) and stats_mode == 0 and step_mode <= 0:
robot[n].n
robot_graph(n)
setcolor(robot_color(n)-8)
setfillstyle(1,0)
bar(11,46,'K:') # check K:
outtextxy(11,46,'K:')
outtextxy(29,46,zero_pad(robot[n].kills,4))
outtextxy(80,46,'D:') # check D:
outtextxy(98,46,zero_pad(robot[n].deaths,4))
def update_cycle_window():
if not graphix:
print('\t' + 'Match ' + str(played) + '/' + str(matches) + ', Cycle: ' + zero_pad(game_cycle,9))
# else:
# viewport(480,440,635,475)
# setfillstyle(1,0)
# bar(59,2,154,10)
# setcolor(7)
# outtextxy(75,03,zero_pad(game_cycle,9))
def setscreen():
if not graphix:
sys.exit()
# BIG GRAPHICAL PART GOES HERE
def graph_mode(on):
if on and not graphix:
Graph_VGA
cleardevice
graphix = True
else:
if (not on) and graphix:
closegraph
graphix = False
def prog_error(n, ss):
#graph_mode(False) # graphics related
#textcolor(15) # graphics related
print("Error #", n, ": ", end = '')
if n == 0:
s = ss
elif n == 1:
s = "Invalid :label - \"" + ss + "\", silly mortal."
elif n == 2:
s = "Undefined identifier - \"" + ss + "\". A typo perhaps?"
elif n == 3:
s = "Memory access out of range - \"" + ss + "\""
elif n == 4:
s = "Not enough robots for combat. Maybe we should just drive in circles."
elif n == 5:
s = "Robot names and settings must be specified. An empty arena is no fun."
elif n == 6:
s = "Config file not found - \"" + ss + "\""
elif n == 7:
s = "Cannot access a config file from a config file - \"" + ss + "\""
elif n == 8:
s = "Robot not found \"" + ss + "\". Perhaps you mistyped it?"
elif n == 9:
s = "Insufficient RAM to load robot: \"" + ss + "\"... This is not good."
elif n == 10:
s = "Too many robots! We can only handle " + str(max_robots + 1) + "! Blah.. limits are limits."
elif n == 11:
s = "You already have a perfectly good #def for \"" + ss + "\", silly."
elif n == 12:
s = "Variable name too long! (Max:" + str(max_var_len) + ") \"" + ss + "\""
elif n == 13:
s = "!Label already defined \"" + ss + "\", silly."
elif n == 14:
s = "Too many variables! (Var Limit: " + str(max_vars) + ")"
elif n == 15:
s = "Too many !labels! (!Label Limit: " + str(max_labels) + ")"
elif n == 16:
s = "Robot program too long! Boldly we simplify, simplify along..." + ss
elif n == 17:
s = "!Label missing error. !Label #" + ss + "."
elif n == 18:
s = "!Label out of range: " + ss
elif n == 19:
s = "!Label not found. " + ss
elif n == 20:
s = "Invalid config option: \"" + ss + "\". Inventing a new device?"
elif n == 21:
s = "Robot is attempting to cheat; Too many config points (" + ss + ")"
elif n == 22:
s = "Insufficient data in data statement: \"" + ss + "\""
elif n == 23:
s = "Too many asterisks: \"" + ss + "\""
elif n == 24:
s = "Invalid step count: \"" + ss + "\""
elif n == 25:
s = "\"" + ss + "\""
else:
s = ss
print(s)
print()
exit()
def print_code(n, p):
i = 0
print(hex(p)+': ')
for i in range(max_op):
sys.stdout.write(str(zero_pad(robot[n].code[p].op[i],5)) + ' ')
sys.stdout.write('= ')
for i in range(max_op):
sys.stdout.write(str(hex(robot[n].code[p].op[i])) + 'h ')
print('\n')
def parse1(n, p, s):
global numlabels
ss = ''
for i in range(max_op-1):
k = 0
found = False
opcode = 0
microcode = 0
s[i] = btrim(ucase(s[i]))
indirect = False
# Microcode:
# 0 = instruction, number, constant
# 1 = variable, memory access
# 2 = :label
# 3 = !label (unresolved)
# 4 = !label (resolved)
# 8h mask = inderect addressing (enclosed in [])
if s[i] == '':
opcode = 0
microcode = 0
found = True
if lstr(s[i], 1) == '[' and (rstr(s[i], 1) == ']'):
s[i] = copy(s[i], 2, len(s[i]) - 2)
indirect = True
if (not found) and (s[i][0] == '!'):
ss = s[i]
ss = btrim(rstr(ss,len(ss)-1))
if numlabels > 0:
for j in range(1,numlabels):
if ss == labelname[i]:
found = True
if labelnum[j] > 0:
opcode = labelnum[j]
microcode = 4 # resovled !label
else:
opcode = j
microcode = 3 # unresovled !label
if not found:
numlabels +=1
if numlabels > max_labels:
prog_error(15, ' ')
else:
labelname[numlabels] = ss
labelnum[numlabels] = -1
opcode = numlabels
microcode = 3 # unresolved !label
found = True
if numvars > 0 and not found:
for j in range(1,numvars):
if s[i] == varname[j]:
opcode = varloc[j]
microcode = 1
found = True
# instructions
if s[i] == 'NOP':
opcode = 000
found = True
elif s[i] == 'ADD':
opcode = 1
found = True
elif s[i] == 'SUB':
opcode = 2
found = True
elif s[i] == 'OR':
opcode = 3
found = True
elif s[i] == 'AND':
opcode = 4
found = True
elif s[i] == 'XOR':
opcode = 5
found = True
elif s[i] == 'NOT':
opcode = 6
found = True
elif s[i] == 'MPY':
opcode = 7
found = True
elif s[i] == 'DIV':
opcode == 7
found = True
elif s[i] == 'MOD':
opcode= 9
found= True
elif s[i] == 'RET':
opcode = 10
found = True
elif s[i] == 'RETURN':
opcode = 10
found = True
elif s[i] == 'GSB':
opcode = 11
found = True
elif s[i] == 'GOSUB':
opcode = 11
found = True
elif s[i] == 'CALL':
opcode = 11
found = True
elif s[i] == 'JMP':
opcode = 12
found = True
elif s[i] == 'JUMP':
opcode = 12
found = True
elif s[i] == 'GOTO':
opcode = 12
found = True
elif s[i] == 'JLS':
opcode = 13
found = True
elif s[i] == 'JB':
opcode = 13
found = True
elif s[i] == 'JGR':
opcode = 14
found = True
elif s[i] == 'JA':
opcode = 14
found = True
elif s[i] == 'JNE':
opcode = 15
found = True
elif s[i] == 'JEQ':
opcode = 16
found = True
elif s[i] == 'JE':
opcode = 16
found = True
elif s[i] == 'XCHG':
opcode = 17
found = True
elif s[i] == 'SWAP':
opcode = 17
found = True
elif s[i] == 'DO':
opcode = 18
found= True
elif s[i] == 'LOOP':
opcode = 19
found = True
elif s[i] == 'CMP':
opcode = 20
found = True
elif s[i] == 'TEST':
opcode = 21
found = True
elif s[i] == 'SET':
opcode = 22
found = True
elif s[i] == 'MOV':
opcode = 22
found = True
elif s[i] == 'LOC':
opcode = 23
found = True
elif s[i] == 'ADDR':
opcode = 23
found = True
elif s[i] == 'GET':
opcode = 24
found= True
elif s[i] == 'PUT':
opcode = 25
found = True
elif s[i] == 'INT':
opcode = 26
found = True
elif s[i] == 'IPO':
opcode = 27
found = True
elif s[i] == 'IN':
opcode = 27
found = True
elif s[i] == 'OPO':
opcode = 28
found = True
elif s[i] == 'OUT':
opcode = 28
found = True
elif s[i] == 'DEL':
opcode = 29
found = True
elif s[i] == 'DELAY':
opcode = 29
found = True
elif s[i] == 'PUSH':
opcode = 30
found = True
elif s[i] == 'POP':
opcode = 31
found = True
elif s[i] == 'ERR':
opcode = 32
found = True
elif s[i] == 'ERROR':
opcode = 32
found = True
elif s[i] == 'INC':
opcode = 33
found = True
elif s[i] == 'DEC':
opcode = 34
found = True
elif s[i] == 'SHL':
opcode = 35
found = True
elif s[i] == 'SHR':
opcode == 36
found = True
elif s[i] == 'ROL':
opcode = 37
found = True
elif s[i] == 'ROR':
opcode = 37
found = True
elif s[i] == 'JZ':
opcode = 39
found = True
elif s[i] == 'JNZ':
opcode = 40
found = True
elif s[i] == 'JAE':
opcode = 41
found = True
elif s[i] == 'JGE':
opcode = 41
found = True
elif s[i] == 'JLE':
opcode = 42
found = True
elif s[i] == 'JBE':
opcode = 42
found = True
elif s[i] == 'SAL':
opcode = 43
found = True
elif s[i] == 'SAR':
opcode = 44
found = True
elif s[i] == 'NEG':
opcode = 45
found = True
elif s[i] == 'JTL':
opcode = 46
found = True
# registers
elif s[i] == 'COLCNT':
opcode = 8
microcode = 1
found = True
elif s[i] == 'METERS':
opcode = 9
microcode = 1
found = True
elif s[i] == 'COMBASE':
opcode = 10
microcode = 1
found = True
elif s[i] == 'COMEND':
opcode = 11
microcode = 1
found = True
elif s[i] == 'FLAGS':
opcode = 64
microcode = 1
elif s[i] == 'AX':
opcode = 64
microcode = 1
found = True
elif s[i] == 'BX':
opcode = 66
microcode = 1
found = True
elif s[i] == 'CX':
opcode = 67
microcode = 1
found = True
elif s[i] == 'DX':
opcode = 68
microcode = 1
found = True
elif s[i] == 'EX':
opcode = 69
microcode = 1
found = True
elif s[i] == 'FX':
opcode = 70
microcode = 1
found = True
elif s[i] == 'SP':
opcode = 71
microcode = 1
found = True
# constants
elif s[i] == 'MAXINT':
opcode = 32767
microcode = 0
found = True
elif s[i] == 'MININT':
opcode = 32768
microcode = 0
found = True
elif s[i] == 'P_SPEDOMETER':
opcode = 1
microcode = 0
found = True
elif s[i] == 'P_HEAT':
opcode = 2
microcode = 0
found = True
elif s[i] == 'P_COMPASS':