forked from ThomasTheSpaceFox/SBTCVM-Mark-2
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSBTCVM_MK2.py
executable file
·2714 lines (2564 loc) · 79.9 KB
/
SBTCVM_MK2.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/env python
import VMSYSTEM.libtrom as libtrom
import pygame
import subprocess
from pygame.locals import *
import time
import os
import VMSYSTEM.libSBTCVM as libSBTCVM
import VMSYSTEM.libbaltcalc as libbaltcalc
import VMSYSTEM.libvmui as vmui
import sys
import VMSYSTEM.libvmconf as libvmconf
import VMSYSTEM.libthemeconf as libthemeconf
import VMSYSTEM.libSBTGA as libSBTGA
from random import randint
pygame.display.init()
import VMSYSTEM.libTSAM as TSAM
print "SBTCVM Mark 2 Starting up..."
#SBTCVM Mark 2
#Simple Balanced Ternary Computer Virtual Machine
#
#v2.0.3
#
# Copyright (c) 2016-2018 Thomas Leathers and Contributors
#
# SBTCVM Mark 2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# SBTCVM Mark 2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with SBTCVM Mark 2. If not, see <http://www.gnu.org/licenses/>
#thread data storage class
#used to store thread-specific data in BTSTACK dictionary when thread is inactive.
class BTTHREAD:
def __init__(self, qxtactg, EXECADDRg, REG1g, REG2g, contaddrg, EXECADDRrawg, regsetpointg, TTYBGCOLREGg, TTYBGCOLg, colvectorregg, monovectorregg, colorregg, tritloadleng, tritoffsetg, tritdestgndg, threadrefg, ROMFILEg, ROMLAMPFLGg, mempointg, timewaitg, waittillg):
self.qxtact=qxtactg
self.EXECADDR=EXECADDRg
self.REG1=REG1g
self.REG2=REG2g
self.contaddr=contaddrg
self.EXECADDRraw=EXECADDRrawg
self.regsetpoint=regsetpointg
self.TTYBGCOLREG=TTYBGCOLREGg
self.TTYBGCOL=TTYBGCOLg
self.colvectorreg=colvectorregg
self.monovectorreg=monovectorregg
self.colorreg=colorregg
self.tritloadlen=tritloadleng
self.tritoffset=tritoffsetg
self.tritdestgnd=tritdestgndg
self.threadref=threadrefg
self.ROMFILE=ROMFILEg
self.ROMLAMPFLG=ROMLAMPFLGg
self.mempoint=mempointg
self.timewait=timewaitg
self.waittill=waittillg
#ROMFILE=TROMA
#ROMLAMPFLG="A"
windowicon=pygame.image.load(os.path.join(os.path.join('VMSYSTEM', 'GFX'), 'icon64.png'))
pygame.display.set_icon(windowicon)
disablereadouts=int(libvmconf.getconf("video", "disablereadouts"))
screensurf=pygame.display.set_mode((950, 600))
simplefont = pygame.font.SysFont(None, 16)
#used for smaller data displays (inst. data etc.)
#smldispfont = pygame.font.SysFont(None, 16)
smldispfont = pygame.font.Font(os.path.join("VMSYSTEM", "SBTCVMreadout.ttf"), 16)
#used in larger data displays (register displays, etc.)
#lgdispfont = pygame.font.SysFont(None, 20)
lgdispfont = pygame.font.Font(os.path.join("VMSYSTEM", "SBTCVMreadout.ttf"), 16)
#
#position bank:
hudsize=44
pausebtnx=3
pausebtny=3
sysmx=46
sysmy=3
ttyyoffset=44
#ttyxoffset=0
colordispx=649
colordispy=1 + hudsize
cdispscale=148 + 148
mdispscale=144
mdispx=649
mdispy=150 + 148 + hudsize
statybegin=360
statx=800
statjump=30
statrect=(statx, statybegin - 15, 150, 300)
hudrect=pygame.Rect(0, 0, 950, hudsize)
vmbg=pygame.Surface((950, 600)).convert()
vmbg.fill(libthemeconf.vmbg)
#vmbg=pygame.image.load(os.path.join(os.path.join('VMSYSTEM', 'GFX'), 'VMBG.png')).convert()
pygame.draw.rect(vmbg, libthemeconf.hudbg, hudrect, 0)
pygame.draw.rect(vmbg, libthemeconf.vmstatbg, statrect, 0)
staty=statybegin-15
for label in ["instruction", "data", "register 1", "register 2", "address", "current rom", "thread"]:
labgx=simplefont.render(label, True, libthemeconf.vmstattext, libthemeconf.vmstatbg).convert()
vmbg.blit(labgx, (statx, staty))
staty += statjump
#pauseicon=pygame.image.load(os.path.join(os.path.join('VMSYSTEM', 'GFX'), 'PAUSEBTN.png')).convert()
sbtcvmbadge=pygame.image.load(os.path.join("VMSYSTEM", "GFX", 'SBTCVMbadge.png')).convert()
vmbg.blit(sbtcvmbadge, ((950-120), 0))
fmicon=pygame.image.load(os.path.join("VMSYSTEM", "GFX", 'filemenuicon.png')).convert_alpha()
pauseicon=vmui.makemenubtn("FILE", icon=fmicon)
sysmenuicon=vmui.makemenubtn("SYSTEM", width=80)
pausex=vmbg.blit(pauseicon, (pausebtnx, pausebtny))
sysmenux=vmbg.blit(sysmenuicon, (sysmx, sysmy))
screensurf.blit(vmbg, (0, 0))
#init in non-kiosk mode for now, SBTCVM will re-init once it knows the kioskmode state.
vmui.initui(screensurf, 0)
pygame.display.update()
libSBTCVM.glyphoptim(screensurf)
pygame.display.set_caption("SBTCVM Mark 2", "SBTCVM Mark 2")
pygame.font.init()
pixcnt1=40
pixjmp=14
USRYN=0
USRWAIT=0
keyintreg="0000"
#ttyfont = pygame.font.SysFont("Mono", 10)
#ttyfontB =pygame.font.SysFont("Mono", 18)
#graphics:
#background pixmap
#indicator lamps
#GREEN
#LEDGREENON=pygame.image.load(os.path.join(os.path.join('VMSYSTEM', 'GFX'), 'LAMP-GREEN.png')).convert()
#LEDGREENOFF=pygame.image.load(os.path.join(os.path.join('VMSYSTEM', 'GFX'), 'LAMP-GREEN-OFF.png')).convert()
#CPU
#CPULEDACT=pygame.image.load(os.path.join(os.path.join('VMSYSTEM', 'GFX'), 'LAMP-BLUE.png')).convert()
#CPULEDSTANDBY=pygame.image.load(os.path.join(os.path.join('VMSYSTEM', 'GFX'), 'LAMP-ORANGE.png')).convert()
COLORDISP=pygame.Surface((27, 27)).convert()
MONODISP=pygame.Surface((9, 9)).convert()
COLORDISP.fill((127, 127, 127))
MONODISP.fill((127, 127, 127))
#this list is what is displayed on the TTY on VM boot.
#the header text is so far in this list so it appears correct in 27 line mode
abt=["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "SBTCVM", "Mark 2", "v2.0.3", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "ready", ""]
abtpref=["This is different", "Mark 2", "v2.0.0", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "ready"]
abtclear=["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
#abt54clear=["", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]
#ttysize:
#0=72x54 (9x9 chars)
#1=72x27 (9x18 chars)
TTYMODE=0
TTYSIZE=1
mixrate=int(libvmconf.getconf("audio", "mixrate"))
pygame.mixer.init(frequency=mixrate , size=-16, channels=2)
extradraw=0
wavsp=libSBTCVM.mk1buzz("0-----")
snf=pygame.mixer.Sound(wavsp)
snf.play()
#config defaults
TROMA="intro.trom"
#these are dud roms full of soft stops
TROMB=("DEFAULT.TROM")
TROMC=("DEFAULT.TROM")
TROMD=("DEFAULT.TROM")
TROME=("DEFAULT.TROM")
TROMF=("DEFAULT.TROM")
CPUWAIT=(0.0005)
stepbystep=0
DEFAULTSTREG="intro.streg"
tuibig=1
logromexit=0
logIOexit=0
vmexeclogflg=0
ttystyle=0
fskip=1
#set this to 1 as SBTCVM now runs too fast with default setting for these to be useful in normal execution.
#user can overide this in USERBOOT.CFG and toggle it using F4 key.
disablereadouts=1
CPUWAIT=float(libvmconf.getconf("cpu", "cpuwait"))
stepbystep=int(libvmconf.getconf("cpu", "stepbystep"))
vmexeclogflg=int(libvmconf.getconf("log", "vmexec"))
logromexit=int(libvmconf.getconf("log", "romexit"))
logIOexit=int(libvmconf.getconf("log", "ioexit"))
disablereadouts=int(libvmconf.getconf("video", "disablereadouts"))
fskip=int(libvmconf.getconf("video", "fskip"))
ttystyle=int(libvmconf.getconf("video", "ttystyle"))
DEFAULTSTREG=libvmconf.getconf("bootup", "DEFAULTSTREG")
fskipcnt=fskip
def vmexeclog(texttolog):
if vmexeclogflg==1:
vmexlogf.write(texttolog + "\n")
#vmexeclog("SYSTEM STARTUP. EXECUTION LOG ENABLED.")
key1=0
key2=0
key3=0
key4=0
key5=0
key6=0
key7=0
key8=0
key9=0
key0=0
keypos=0
keyhyp=0
keyq=0
keyw=0
keye=0
keyr=0
keyt=0
keyy=0
keyu=0
keyi=0
keyo=0
keyp=0
keya=0
keysx=0
keyd=0
keyf=0
keyg=0
keyh=0
keyj=0
keyk=0
keyl=0
keyz=0
keyx=0
keyc=0
keyv=0
keyb=0
keyn=0
keym=0
keyspace=0
keyret=0
btstopthread=0
TTYrenderflg="0"
if 'GLOBKIOSK' in globals():
print "RUNNING IN KIOSK MODE."
KIOSKMODE=GLOBKIOSK
else:
KIOSKMODE=0
stepcont=0
#initalize VMUI system
vmui.initui(screensurf, KIOSKMODE)
if 'GLOBRUNFLG' in globals():
TROMA=GLOBRUNFLG
libtrom.redefA(TROMA)
runtype=0
LOGBASE=TROMA
print ("GLOBRUNFLG found... \n running trom: \"" + TROMA + "\" as TROMA")
elif 'GLOBSTREG' in globals():
streg_subtitle="Unnamed"
#scstreg=open(GLOBSTREG, 'r')
ncstreg=libSBTCVM.stregload(GLOBSTREG)
scstreg=open(ncstreg, 'r')
exstreg=compile(scstreg.read(), ncstreg, 'exec')
exec(exstreg)
scstreg.close()
print ("streg program title:" + streg_subtitle)
libtrom.redefA(TROMA)
libtrom.redefB(TROMB)
libtrom.redefC(TROMC)
libtrom.redefD(TROMD)
libtrom.redefE(TROME)
libtrom.redefF(TROMF)
runtype=1
LOGBASE=GLOBSTREG
pygame.display.set_caption("SBTCVM Mark 2 | " + streg_subtitle, "SBTCVM Mark 2 | " + streg_subtitle)
print ("GLOBSTREG found... \n Starting SBTCVM with setup in streg file: \"" + GLOBSTREG + "\"")
else:
streg_subtitle="Unnamed"
#scstreg=open(DEFAULTSTREG, 'r')
ncstreg=libSBTCVM.stregload(DEFAULTSTREG)
scstreg=open(ncstreg, 'r')
exstreg=compile(scstreg.read(), ncstreg, 'exec')
exec(exstreg)
print ("streg program title:" + streg_subtitle)
libtrom.redefA(TROMA)
libtrom.redefB(TROMA)
libtrom.redefC(TROMA)
libtrom.redefD(TROMA)
libtrom.redefE(TROMA)
libtrom.redefF(TROMA)
runtype=1
LOGBASE=DEFAULTSTREG
pygame.display.set_caption("SBTCVM Mark 2 | " + streg_subtitle, "SBTCVM Mark 2 | " + streg_subtitle)
print ("nither GLOBSTREG or GLOBRUNFLG have been found... \nStarting SBTCVM with setup in default streg file: \"" + DEFAULTSTREG + "\"")
if vmexeclogflg==1:
vmexlogf=open(os.path.join("CAP", libSBTCVM.namecrunch(LOGBASE, "-vmexeclog.log")), "w")
if 'GLOBLOGEXEC' in globals():
if GLOBLOGEXEC==1:
vmexeclogflg=1
vmexlogf=open(os.path.join("CAP", libSBTCVM.namecrunch(LOGBASE, "-vmexeclog.log")), "w")
if 'GLOBOPSEC' in globals():
if GLOBOPSEC==1:
trackopsec=1
else:
trackopsec=0
else:
trackopsec=0
sysmdisread0=vmui.menuitem("status readouts (off)", "READ0")
sysmdisread1=vmui.menuitem("status readouts (on)", "READ1")
sysmstep0=vmui.menuitem("step-by-step debugging (off)", "STEP0")
sysmstep1=vmui.menuitem("step-by-step debugging (on)", "STEP1")
sysmtty0=vmui.menuitem("TTY (on)", "TTY0")
sysmtty1=vmui.menuitem("TTY (off)", "TTY1")
sysmmemorydmp=vmui.menuitem("Generate Memory *.DMPs", "DMP")
sysmcapplot=vmui.menuitem("Capture plotters", "DISPDMP")
sysmcapscr=vmui.menuitem("Capture screenshot", "SCSHOT")
sysmhalted=vmui.menuitem("(System halted)", "SYSHLT", noclick=1)
def sysmenu(x=sysmx, y=(sysmy+40), posthalt=0):
pygame.mixer.pause()
global disablereadouts
global stepbystep
global ttystyle
global ttyredraw
global ttyredrawfull
if disablereadouts==1:
sysmdisread=sysmdisread0
else:
sysmdisread=sysmdisread1
if stepbystep==1:
sysmstep=sysmstep1
else:
sysmstep=sysmstep0
if ttystyle==0:
sysmtty=sysmtty0
else:
sysmtty=sysmtty1
if posthalt==1:
sysmmen=[sysmhalted, sysmmemorydmp, sysmcapplot, sysmcapscr]
else:
sysmmen=[sysmdisread, sysmstep, sysmtty, sysmmemorydmp, sysmcapplot, sysmcapscr]
menuret=vmui.menuset(sysmmen, x, y, reclick=0, scrndest='SCREENSHOT.png', fontsize=26)
if menuret=="READ0":
disablereadouts=0
if menuret=="READ1":
disablereadouts=1
if menuret=="STEP0":
stepbystep=1
if menuret=="STEP1":
stepbystep=0
if menuret=="TTY0":
ttystyle=1
if menuret=="TTY1":
ttystyle=0
ttyredraw=1
ttyredrawfull=1
if menuret=="DISPDMP":
pygame.image.save(COLORDISP, (os.path.join('CAP', 'COLORDISP-OUT.png')))
pygame.image.save(MONODISP, (os.path.join('CAP', 'MONODISP-OUT.png')))
if menuret=="SCSHOT":
pygame.image.save(screensurf, (os.path.join('CAP', 'SCREENSHOT.png')))
if menuret=="DMP":
ramdmp=open((os.path.join('CAP', 'IOBUSman.dmp')), 'w')
for IOitm in IOdumplist:
ramdmp.write(RAMbank[IOitm] + "\n")
ramdmp.close()
for threaddex in BTSTACK:
print (str(BTSTACK[threaddex].qxtact) + " " + threaddex)
libtrom.manualdumptroms()
pygame.mixer.unpause()
return
#tritlength defaults
tritloadlen=9
tritoffset=0
tritdestgnd=0
def tritlen(srcdata, destdata):
#just return srcdata if tritloadlen=9 and tritoffset=0
if tritdestgnd==1:
destdata="000000000"
if tritloadlen==9 and tritoffset==0:
#print srcdata
return (srcdata)
destdict={}
destcnt=0
tritstart=(8 - tritoffset)
tritstop=(tritstart - tritloadlen)
#print tritstop
tritstack=1
iterlist=[0, 1, 2, 3, 4, 5, 6, 7, 8]
#parse destination data into a dict
for f in destdata:
destdict[destcnt]=f
destcnt += 1
#modify destination dict
for f in iterlist:
#print tritstart
destdict[tritstart]=srcdata[tritstart]
tritstart -= 1
tritstack += 1
if tritstart==tritstop or tritstart==-1:
break
#print destdict
destdataout=""
#parse destination dict back into string and return result.
for f in iterlist:
destdataout=(destdataout + destdict[f])
#print destdataout
return destdataout
#def stactdebug1():
if 'GLOBKIOSK' in globals():
if KIOSKMODE==1:
disablereadouts=0
ttystyle=0
print "Kiosk mode active... enabling readouts and TTY mode 0..."
#if disablereadouts==1:
#screensurf=pygame.display.set_mode((950, 600))
#screensurf.blit(vmbg, (0, 0))
#pygame.display.update()
TTYBGCOL=libSBTCVM.colorfind("------")
TTYBGCOLREG="------"
quickquit=0
colvectorreg="000000"
monovectorreg="000000"
#keep unused events out of queue
pygame.event.set_allowed([QUIT, KEYDOWN, KEYUP, MOUSEBUTTONDOWN])
libSBTCVMsurf=pygame.Surface((648, 486)).convert()
libSBTCVMsurf.fill(TTYBGCOL)
libSBTCVM.glyphoptim(libSBTCVMsurf)
libSBTCVMsurfshort=pygame.Surface((648, 90)).convert(libSBTCVMsurf)
libSBTCVMsurfshort.fill(TTYBGCOL)
#RAMBANK startup begin
RAMbank = {}
#calmlst = open("ORDEREDLIST6.txt")
#screensurf.fill((0,127,255))
shortttybgfix=pygame.Surface((648, 96)).convert()
shortttybgfix.fill(libthemeconf.vmbg)
#build IObus dictionary.
IOdumplist=list()
IOgen="---------"
RAMbank["---------"] = "000000000"
#IOdumplist.extend([IOgen])
while IOgen!="+++++++++":
IOdumplist.extend([IOgen])
IOgen=libSBTCVM.trunkto6(libbaltcalc.btadd(IOgen, "+"))
RAMbank[IOgen] = "000000000"
RAMbank["+++++++++"] = "000000000"
IOdumplist.extend([IOgen])
#set Random integer port with an inital random integer
RAMbank["--0------"]=libSBTCVM.trunkto6(libbaltcalc.DECTOBT(randint(-9841,9841)))
ttyredraw=1
#IO read only list. IO addresses in this list are treated as read only. for example:
#the random integer port is read only.
IOreadonly=["--0------"]
MONODISPBIG=pygame.transform.scale(MONODISP, (mdispscale, mdispscale))
COLORDISPBIG=pygame.transform.scale(COLORDISP, (cdispscale, cdispscale))
#RAMBANK startup end
colorreg="++++++"
ROMFILE=TROMA
ROMLAMPFLG="A"
stopflag=0
EXECCHANGE=0
#ROMFILE=open(BOOTUPFILE)
mempoint="---------"
EXECADDR="---------"
contaddr="---------"
EXECADDRfall=0
EXECADDRraw=EXECADDR
REG1="000000000"
REG2="000000000"
#status display optimization.
prevREG1="diff"
prevREG2="diff"
prevEXECADDR="diff"
prevROM="diff"
prevINST="diff"
prevDATA="diff"
regsetpoint="000000000"
updtcdisp=1
ttyredrawfull=1
updtmdisp=1
updtblits=list()
updtttyprev=list()
updtrandport=1
#upt=screensurf.blit(CPULEDACT, (749, 505))
#updtblits.extend([upt])
#upt=screensurf.blit(STEPLED, (750, 512))
#updtblits.extend([upt])
print "Prep threading system..."
threadref="00"
#vmexeclog("Preping threading system...")
prevCLOCK=2242
ttyredrawfull=1
timewait=0
waittill=time.time()
BTSTACK={"--": BTTHREAD(1, EXECADDR, REG1, REG2, contaddr, EXECADDRraw, regsetpoint, TTYBGCOLREG, TTYBGCOL, colvectorreg, monovectorreg, colorreg, tritloadlen, tritoffset, tritdestgnd, threadref, ROMFILE, ROMLAMPFLG, mempoint, timewait, waittill)}
for cur_id in ["-0","-+","0-","00","0+","+-","+0","++"]:
BTSTACK[cur_id] = BTTHREAD(0, EXECADDR, REG1, REG2, contaddr, EXECADDRraw, regsetpoint, TTYBGCOLREG, TTYBGCOL, colvectorreg, monovectorreg, colorreg, tritloadlen, tritoffset, tritdestgnd, threadref, ROMFILE, ROMLAMPFLG, mempoint, timewait, waittill)
btthreadcnt=1
btcurthread="--"
curinst="000000"
curdata="000000000"
staty=statybegin
insttext=smldispfont.render(curinst, True, libthemeconf.vminst, libthemeconf.vmstatbg)
datatext=smldispfont.render(curdata, True, libthemeconf.vmdata, libthemeconf.vmstatbg)
upt=screensurf.blit(insttext, (statx, staty))
updtblits.extend([upt])
staty += statjump
upt=screensurf.blit(datatext, (statx, staty))
updtblits.extend([upt])
staty += statjump
#these draw the register displays :)
reg1text=lgdispfont.render(REG1, True, libthemeconf.vmreg1, libthemeconf.vmstatbg)
reg2text=lgdispfont.render(REG2, True, libthemeconf.vmreg2, libthemeconf.vmstatbg)
upt=screensurf.blit(reg1text, (statx, staty))
updtblits.extend([upt])
staty += statjump
upt=screensurf.blit(reg2text, (statx, staty))
updtblits.extend([upt])
staty += statjump
#and here is what draws the ROM address display :)
ROMadrtex=lgdispfont.render(EXECADDR, True, libthemeconf.vmaddr, libthemeconf.vmstatbg)
upt=screensurf.blit(ROMadrtex, (statx, staty))
updtblits.extend([upt])
staty += statjump
#and the current rom display :)
CURROMTEXT=(ROMLAMPFLG)
curROMtex=lgdispfont.render(CURROMTEXT, True, libthemeconf.vmrom, libthemeconf.vmstatbg)
upt=screensurf.blit(curROMtex, (statx, staty))
updtblits.extend([upt])
staty += statjump
staty += statjump
if stepbystep==0:
labgx=simplefont.render("Clock: Step by Step", True, libthemeconf.vmstatbg, libthemeconf.vmstatbg).convert()
upt2=screensurf.blit(labgx, (statx, staty))
labgx=simplefont.render("Clock: Normal run", True, libthemeconf.vmstattext, libthemeconf.vmstatbg).convert()
upt=screensurf.blit(labgx, (statx, staty))
else:
labgx=simplefont.render("Clock: Normal run", True, libthemeconf.vmstatbg, libthemeconf.vmstatbg).convert()
upt2=screensurf.blit(labgx, (statx, staty))
labgx=simplefont.render("Clock: Step by Step", True, libthemeconf.vmstattext, libthemeconf.vmstatbg).convert()
upt=screensurf.blit(labgx, (statx, staty))
updtblits.extend([upt])
updtblits.extend([upt2])
curthrtex=lgdispfont.render(btcurthread, True, libthemeconf.vmcurth, libthemeconf.vmstatbg).convert()
upt=screensurf.blit(curthrtex, (statx, (statybegin + (statjump*6))))
updtblits.extend([upt])
ttyblitsprev=list()
ttyblits=list()
exlogclockticnum=0
#SBTGA setup
#SBTGA Bus
dispmembus=libtrom.AROM
#SBTGA display mode SB should be default
dispmode="SB"
#master valid mode list
displist=["G0", "G2", "SB", "G3", "G4"]
#all Modes that should disable TTY rendering need to be in here!
dispttyover=["G0", "G2", "G3", "G4"]
#default SBTGA offset.
dispoffset="000000000"
updatedisp=0
SBTGADEV=libSBTGA.buffdisplay(screensurf, dispmembus, dispoffset, mode=dispmode)
#init sample channel class objects.
samchan1=TSAM.SampleChannnel(1, TROMA)
samchan2=TSAM.SampleChannnel(1, TROMA)
samchan3=TSAM.SampleChannnel(1, TROMA)
samchan4=TSAM.SampleChannnel(1, TROMA)
#to do: migrate rest of IO notify conditionals to this function.
#used by IOwrite
def IOnotify(curdatax, REGQ):
if ROMFILE==TROMA:
RBUS=libtrom.AROM
elif ROMFILE==TROMB:
RBUS=libtrom.BROM
elif ROMFILE==TROMC:
RBUS=libtrom.CROM
elif ROMFILE==TROMD:
RBUS=libtrom.DROM
elif ROMFILE==TROME:
RBUS=libtrom.EROM
elif ROMFILE==TROMF:
RBUS=libtrom.FROM
#sample chan 1
if curdatax=="--0--0000":
samchan1.play()
if curdatax=="--0--000+":
samchan1.stop()
if curdatax=="--0--00+-":
samchan1.setoffset(REGQ, RBUS)
if curdatax=="--0--00+0":
samchan1.setlen(REGQ)
if curdatax=="--0--00++":
samchan1.setfreq(REGQ)
#sample chan 2
if curdatax=="--0--0+--":
samchan2.play()
if curdatax=="--0--0+-0":
samchan2.stop()
if curdatax=="--0--0+-+":
samchan2.setoffset(REGQ, RBUS)
if curdatax=="--0--0+0-":
samchan2.setlen(REGQ)
if curdatax=="--0--0+00":
samchan2.setfreq(REGQ)
#sample chan 3
if curdatax=="--0--0+0+":
samchan3.play()
if curdatax=="--0--0++-":
samchan3.stop()
if curdatax=="--0--0++0":
samchan3.setoffset(REGQ, RBUS)
if curdatax=="--0--0+++":
samchan3.setlen(REGQ)
if curdatax=="--0--+---":
samchan3.setfreq(REGQ)
#sample chan 4
if curdata=="--0--+--0":
samchan4.play()
if curdata=="--0--+--+":
samchan4.stop()
if curdata=="--0--+-0-":
samchan4.setoffset(REGQ, RBUS)
if curdata=="--0--+-00":
samchan4.setlen(REGQ)
if curdata=="--0--+-0+":
samchan4.setfreq(REGQ)
return
#3 voice sound gen volume control (fine tune to prevent clipping!)
voicechanvol=0.5
svoice1sam=pygame.mixer.Sound(libSBTCVM.mk23voicesample("0000000"))
svoice2sam=pygame.mixer.Sound(libSBTCVM.mk23voicesample("0000000"))
svoice3sam=pygame.mixer.Sound(libSBTCVM.mk23voicesample("0000000"))
svoice1sam.set_volume(voicechanvol)
svoice2sam.set_volume(voicechanvol)
svoice3sam.set_volume(voicechanvol)
sv1bak=svoice1sam
sv2bak=svoice2sam
sv3bak=svoice3sam
#for f in BTSTACK:
# print str(BTSTACK[f].qxtact) + " " + f
#for f in BTSTACK:
# print str(BTSTACK[f].qxtact) + " " + f
print "SBTCVM Mark 2 Ready. the VM will now begin."
#vmexeclog("SBTCVM Mark 2 Ready. the VM will now begin.")
initaltime=time.time()
while stopflag==0:
#instruction load
EXECADDRNUM=libSBTCVM.numstruct(EXECADDR)
if ROMFILE==TROMA:
fdelta=libtrom.AROM[EXECADDRNUM]
elif ROMFILE==TROMB:
fdelta=libtrom.BROM[EXECADDRNUM]
elif ROMFILE==TROMC:
fdelta=libtrom.CROM[EXECADDRNUM]
elif ROMFILE==TROMD:
fdelta=libtrom.DROM[EXECADDRNUM]
elif ROMFILE==TROME:
fdelta=libtrom.EROM[EXECADDRNUM]
elif ROMFILE==TROMF:
fdelta=libtrom.FROM[EXECADDRNUM]
curinst=((fdelta[0]) + (fdelta[1]) + (fdelta[2]) + (fdelta[3]) + (fdelta[4]) + (fdelta[5]))
curdata=((fdelta[6]) + (fdelta[7]) + (fdelta[8]) + (fdelta[9]) + (fdelta[10]) + (fdelta[11]) + (fdelta[12]) + (fdelta[13]) + (fdelta[14]))
#logging and track operations/second
if trackopsec==1 and timewait==0:
exlogclockticnum += 1
exlogcurtime=(time.time() - initaltime)
elif vmexeclogflg==1 and timewait==0:
exlogclockticnum += 1
exlogcurtime=(time.time() - initaltime)
vmexeclog("data: " + curdata + " |Inst: " + curinst + " |adr: " + EXECADDR + " |Mem point: " + mempoint +" |thread: " + btcurthread + " |exec bank: " + ROMLAMPFLG + " |reg1: " + REG1 + " |reg2: " + REG2 + " |tic #: " + str(exlogclockticnum) + " |secs: " + format((exlogcurtime), '.11f'))
#main render
if fskipcnt == fskip or stepbystep==1:
if disablereadouts==0 or stepbystep==1:
#screensurf.blit(vmbg, (0, 0))
#these show the instruction and data in the instruction/data box :)
staty=statybegin
if prevINST!=curinst:
insttext=smldispfont.render(curinst, True, libthemeconf.vminst, libthemeconf.vmstatbg).convert()
prevINST=curinst
upt=screensurf.blit(insttext, (statx, staty))
updtblits.extend([upt])
staty += statjump
if prevDATA!=curdata:
datatext=smldispfont.render(curdata, True, libthemeconf.vmdata, libthemeconf.vmstatbg).convert()
prevDATA=curdata
upt=screensurf.blit(datatext, (statx, staty))
updtblits.extend([upt])
staty += statjump
#these draw the register displays :)
if prevREG1!=REG1:
reg1text=lgdispfont.render(REG1, True, libthemeconf.vmreg1, libthemeconf.vmstatbg).convert()
prevREG1=REG1
upt=screensurf.blit(reg1text, (statx, staty))
updtblits.extend([upt])
staty += statjump
if prevREG2!=REG2:
reg2text=lgdispfont.render(REG2, True, libthemeconf.vmreg2, libthemeconf.vmstatbg).convert()
prevREG2=REG2
upt=screensurf.blit(reg2text, (statx, staty))
updtblits.extend([upt])
staty += statjump
#and here is what draws the ROM address display :)
ROMadrtex=lgdispfont.render(EXECADDR, True, libthemeconf.vmaddr, libthemeconf.vmstatbg).convert()
upt=screensurf.blit(ROMadrtex, (statx, staty))
updtblits.extend([upt])
staty += statjump
#and the current rom display :)
CURROMTEXT=(ROMLAMPFLG)
if prevROM!=CURROMTEXT:
curROMtex=lgdispfont.render(CURROMTEXT, True, libthemeconf.vmrom, libthemeconf.vmstatbg).convert()
prevROM=CURROMTEXT
upt=screensurf.blit(curROMtex, (statx, staty))
updtblits.extend([upt])
staty += statjump
staty += statjump
if prevCLOCK!=stepbystep:
prevCLOCK=stepbystep
if stepbystep==0:
labgx=simplefont.render("Clock: Step by Step", True, libthemeconf.vmstatbg, libthemeconf.vmstatbg).convert()
upt2=screensurf.blit(labgx, (statx, staty))
labgx=simplefont.render("Clock: Normal run", True, libthemeconf.vmstattext, libthemeconf.vmstatbg).convert()
upt=screensurf.blit(labgx, (statx, staty))
else:
labgx=simplefont.render("Clock: Normal run", True, libthemeconf.vmstatbg, libthemeconf.vmstatbg).convert()
upt2=screensurf.blit(labgx, (statx, staty))
labgx=simplefont.render("Clock: Step by Step", True, libthemeconf.vmstattext, libthemeconf.vmstatbg).convert()
upt=screensurf.blit(labgx, (statx, staty))
updtblits.extend([upt])
updtblits.extend([upt2])
else:
staty=statybegin + (statjump * 7)
if prevCLOCK!=stepbystep:
prevCLOCK=stepbystep
if stepbystep==0:
labgx=simplefont.render("Clock: Step by Step", True, libthemeconf.vmstatbg, libthemeconf.vmstatbg).convert()
upt2=screensurf.blit(labgx, (statx, staty))
labgx=simplefont.render("Clock: Normal run", True, libthemeconf.vmstattext, libthemeconf.vmstatbg).convert()
upt=screensurf.blit(labgx, (statx, staty))
else:
labgx=simplefont.render("Clock: Normal run", True, libthemeconf.vmstatbg, libthemeconf.vmstatbg).convert()
upt2=screensurf.blit(labgx, (statx, staty))
labgx=simplefont.render("Clock: Step by Step", True, libthemeconf.vmstattext, libthemeconf.vmstatbg).convert()
upt=screensurf.blit(labgx, (statx, staty))
updtblits.extend([upt])
updtblits.extend([upt2])
#LED LAMPS
#CPU
#screensurf.blit(CPULEDACT, (749, 505))
#STEP
if updtcdisp==1:
updtcdisp=0
COLORDISPBIG=pygame.transform.scale(COLORDISP, (cdispscale, cdispscale))
upt=screensurf.blit(COLORDISPBIG, (colordispx, colordispy))
updtblits.extend([upt])
if updtmdisp==1:
updtmdisp=0
MONODISPBIG=pygame.transform.scale(MONODISP, (mdispscale, mdispscale))
upt=screensurf.blit(MONODISPBIG, (mdispx, mdispy))
updtblits.extend([upt])
if abtpref!=abt or ttyredraw==1:
abtpref=abt
ttyredraw=0
lineq=0
linexq=0
if ttystyle==0 and dispmode not in dispttyover:
libSBTCVMsurf.fill(TTYBGCOL)
for fnx in abt:
fnx=fnx.replace('\n', '')
colq=0
if TTYSIZE==0 or linexq>26:
for qlin in fnx:
#print qlin
charq=libSBTCVM.charlookupdict.get(qlin)
#print charq
if TTYSIZE==1:
xq=libSBTCVM.charblit2(libSBTCVMsurf, colq, lineq, charq)
else:
xq=libSBTCVM.charblit(libSBTCVMsurf, colq, lineq, charq)
xq[1].y += ttyyoffset
ttyblits.extend([xq[1]])
colq +=1
lineq +=1
linexq +=1
upt=screensurf.blit(libSBTCVMsurf, (0, ttyyoffset))
if ttyredrawfull==1:
updtblits.extend([upt])
ttyredrawfull=0
else:
updtblits = updtblits + ttyblits + ttyblitsprev
#updtblits.extend([upt])
#updtblits.append(ttyblits)
#updtblits.append(ttyblitsprev)
#print updtblits
ttyblitsprev=ttyblits
ttyblits=list()
lineq=0
linexq=0
#if ttystyle==1:
#print "-----newpage---"
#for fnx in abt:
#if TTYSIZE==0 or linexq>26:
#fnx=fnx.replace('\n', '')
#colq=0
#print ("TTY|" + fnx)
#lineq +=1
#linexq +=1
lineq=0
linexq=0
#disabled as its far too buggy
#if ttystyle==2:
#for fnx in abt:
#fnx=fnx.replace('\n', '')
#colq=0
#if TTYSIZE==0 or linexq>26:
#if TTYSIZE==1:
#ttyfn=ttyfontB.render(fnx, True, (255, 255, 255), (0, 0, 0)).convert()
#upt=screensurf.blit(ttyfn, (0, (lineq*18)))
##updtblits.extend([upt])
#else:
#ttyfn=ttyfont.render(fnx, True, (255, 255, 255), (0, 0, 0)).convert()
#upt=screensurf.blit(ttyfn, (0, (lineq*9)))
##updtblits.extend([upt])
#lineq +=1
#linexq +=1
#print ("ttystyle" + str(ttystyle))
#screensurf.blit(libSBTCVMsurf, (45, 40))
#biglibSBTCVM=pygame.transform.scale(libSBTCVMsurf, (648, 486))
if updtblits!=list():
pygame.display.update(updtblits)
updtblits=list()
fskipcnt=0
#print "sc update"
else:
fskipcnt+=1
#print "sc skip"
#bypass instruction parser in entirety if in timewait mode.
if timewait==1:
pass
#ROM READ (first register)
elif curinst=="------":
REG1=(tritlen(libtrom.tromreaddata(curdata,ROMFILE), REG1))
#print("----")
#ROM READ (second register)
elif curinst=="-----0":
REG2=(tritlen(libtrom.tromreaddata(curdata,ROMFILE), REG2))
#print("---0")
#IO READ REG1
elif curinst=="-----+":
REG1=tritlen(RAMbank[curdata], REG1)
if curdata=="--0------":
updtrandport=1
#print("---+")
#IO READ REG2
elif curinst=="----0-":
REG2=tritlen(RAMbank[curdata], REG2)
if curdata=="--0------":
updtrandport=1
#print("--0-")
#IO WRITE REG1
elif curinst=="----00":
if curdata not in IOreadonly:
rambnkcur=RAMbank[curdata]
RAMbank[curdata] = tritlen(REG1, rambnkcur)
if curdata=="--0---+-+":
if REG1=="---------":
dispmode="G0"
elif REG1=="--------+":
dispmode="G2"
elif REG1=="-------0-":
dispmode="G3"
elif REG1=="-------00":
dispmode="G4"
else:
dispmode="SB"
ttyredraw=1
ttyredrawfull=1
upt=screensurf.blit(shortttybgfix, (0, 504))
updtblits.extend([upt])
SBTGADEV.setmode(dispmode)
if curdata=="--0---+0-":
dispoffset=REG1
SBTGADEV.setoffset(dispoffset)
if curdata=="--0---+00":
updatedisp=1
IOnotify(curdata, REG1)
else:
print "address \"" + curdata + "\" is read-only."
#IO WRITE REG2
elif curinst=="----0+":
if curdata not in IOreadonly:
rambnkcur=RAMbank[curdata]
RAMbank[curdata] = tritlen(REG2, rambnkcur)
if curdata=="--0---+-+":
if REG2=="---------":
dispmode="G0"
elif REG2=="--------+":
dispmode="G2"
elif REG2=="-------0-":
dispmode="G3"
elif REG2=="-------00":
dispmode="G4"
else:
dispmode="SB"
ttyredraw=1
ttyredrawfull=1
upt=screensurf.blit(shortttybgfix, (0, 504))
updtblits.extend([upt])
SBTGADEV.setmode(dispmode)