-
Notifications
You must be signed in to change notification settings - Fork 7
/
cur_sbtcvm.py
executable file
·272 lines (235 loc) · 7.86 KB
/
cur_sbtcvm.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
#!/usr/bin/env python
import os
if not os.path.isdir("vmsystem"):
print("changing to script location...")
os.chdir(os.path.dirname(os.path.abspath(__file__)))
import vmsystem.libbaltcalc as libbaltcalc
from vmsystem.libbaltcalc import btint
import vmsystem.MEM_G2x_9
import vmsystem.CPU_G2x_9
import vmsystem.COCPU_G2x_9 as ccpu
import vmsystem.IO_G2x_9
import vmsystem.UIO_CURSES_G2x_9 as UIO
import vmsystem.COMMON_IO_G2x_9 as devcommon
import vmsystem.SBTVDI_IO_G2x_9 as vdi
import vmsystem.SND_G2x_9 as snd
import vmsystem.tdisk1lib as td1
import vmsystem.iofuncts as iofuncts
import vmsystem.libvmconf as vmconf
import time
import sys
import curses
import signal
from threading import Thread
progrun=0
uiosys=None
#catch KeyboardInterrupts and shutdown cleanly.
sigintflg=0
def siginthandle(sig, frame):
global sigintflg
uiosys.powoff()
curses.echo()
curses.endwin()
sigintflg=1
signal.signal(signal.SIGINT, siginthandle)
try:
cmd=sys.argv[1]
except:
cmd=None
try:
arg=sys.argv[2]
except:
arg=None
if cmd in ['help', '-h', '--help']:
print('''SBTCVM Gen2-9 virtual machine. curses frontend.
help, -h, --help: this help.
-v, --version: VM version
-a, --about: about SBTCVM
[trom/tdsk1], -r [trom/tdsk1], --run [trom/tdsk1]: launch SBTCVM with the selected TROM/tdsk1 (disk) image
loaded into memory.
-s [trom/tdsk1] {CPU speed in Hz}, --slow [trom/tdsk1] {CPU speed in Hz}:
-s overrides the default CPU clock speed. You may specify a float/int Hz value
(after the trom/tdsk1 filename). defaults to 2Hz''')
elif cmd in ["-a", "--about"]:
print('''SBTCVM Gen2-9 virtual machine. curses frontend.
v2.1.0.alpha
Copyright (c) 2016-2022 Thomas Leathers and Contributors
see readme.md for more information and licensing of media.
SBTCVM Gen2-9 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 Gen2-9 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 Gen2-9. If not, see <http://www.gnu.org/licenses/>
''')
elif cmd in ['-v', '--version']:
print('v2.1.0.alpha')
else:
slow=0
if cmd==None:
#romfile='TESTSHORT.TROM'
romfile='VDIBOOT'
elif cmd in ['-r', '--run']:
if arg==None:
sys.exit("Error! Must specify trom to run!")
romfile=arg
elif cmd in ['-s', '--slow']:
if arg==None:
sys.exit("Error! Must specify trom to run!")
romfile=arg
slow=1
try:
targspeed=float(sys.argv[3])/1000.0
slowspeed=1/(targspeed*1000.0)
except IndexError:
print("Using default slow delay. (2 hz)")
targspeed=2/1000.0
slowspeed=1/(targspeed*1000.0)
except ValueError:
sys.exit("Error. please specify slow delay as float or int.")
elif cmd.startswith("-"):
print("Unknown option: '" + cmd + "' try cur_sbtcvm.py -h for help.")
sys.exit()
else:
romfile=cmd
if slow==1:
targtime=slowspeed
else:
#targspeed is in khz. acuracy may vary.
targspeed=6.5#approx. speed should be close.
#calculate approx speed.
targtime=1/(targspeed*1000.0)
try:
print("SBTCVM Generation 2 9-trit VM, v2.1.0.alpha\n")
print("Notice: somewhat buggy, incomplete. pygame frontend HIGHLY recommended.")
diska=None
diskb=None
garom=None
cocpuhalt=True
if vmconf.check_has_vmconf(romfile):
cfg_dict=vmconf.load_vmconf(romfile)
romfile=iofuncts.findtrom(cfg_dict["rom0"], ext=".trom", exitonfail=1, dirauto=1)
if cfg_dict["rom1"]!=None:
garom=iofuncts.findtrom(cfg_dict["rom1"], ext=".trom", exitonfail=1, dirauto=1)
if cfg_dict["vdi0"]!=None:
diska=td1.loaddisk(iofuncts.findtrom(cfg_dict["vdi0"], ext=".tdsk1", exitonfail=1, dirauto=1))
if cfg_dict["vdi1"]!=None:
diskb=td1.loaddisk(iofuncts.findtrom(cfg_dict["vdi1"], ext=".tdsk1", exitonfail=1, dirauto=1))
#if cfg_dict["name"]!=None:
# windowprefix=cfg_dict["name"]
if cfg_dict["cocpu_halt"]!=None:
cocpuhalt=cfg_dict["cocpu_halt"]
else:
diskfile=iofuncts.findtrom(romfile, ext=".tdsk1", exitonfail=0, dirauto=1)
if diskfile!=None:
retval=td1.loaddisk(diskfile, readonly=0)
windowprefix=retval.label
if not isinstance(retval, str):
diska=retval
romfile='VDIBOOT'
#initialize memory subsystem
memsys=vmsystem.MEM_G2x_9.memory(romfile)
#initialize IO subsystem
iosys=vmsystem.IO_G2x_9.io()
cpusys=vmsystem.CPU_G2x_9.cpu(memsys, iosys)
memsys2=vmsystem.MEM_G2x_9.memory(garom, mem_id=1)
iosys2=vmsystem.IO_G2x_9.io(io_id=1)
cpusys2=vmsystem.CPU_G2x_9.cpu(memsys2, iosys2, cpu_id=1)
cocpu=ccpu.cocpu_setup(cpusys2, iosys, iosys2, memsys2, targtime, targspeed)
devcommon.factorydevs(iosys)
devcommon.factorydevs(iosys2)
#init SBTVDI interface.
vdi.sbtvdi(iosys, cpusys, memsys, cocpu, iosys2, cpusys2, memsys2, diska=diska, diskb=diskb)
progrun=1
#start sound system
snd.initsnd(iosys, iosys2)
#curses startup
mainscr=curses.initscr()
curses.noecho()
#curses.raw()
curses.cbreak()
mainscr.keypad(1)
mainscr.nodelay(1)
mhig, mwid = mainscr.getmaxyx()
statwin = curses.newwin(2, mwid, 0, 0)
ttywin = curses.newwin(mhig-2, mwid, 2, 0)
maxy, maxx=ttywin.getmaxyx()
ttywin.addstr(maxy-1, 0, "SBTCVM Curses frontend. SBTCVM Gen2-9 v2.1.0")
ttywin.scrollok(1)
ttywin.scroll(1)
ttywin.refresh()
#uio startup
uiosys = UIO.uio(cpusys, memsys, iosys, statwin, ttywin, mainscr)
dispthr=Thread(target = uiosys.statup, args = [])
dispthr.daemon=True
dispthr.start()
uiosys.ttyraw("ready.")
time.sleep(0.5)
ttywin.redrawwin()
ttywin.refresh()
statwin.redrawwin()
statwin.refresh()
if cocpuhalt==False:
cocpu.engage_on_start()
#main loop
clcnt=0.0
starttime=time.time()
while progrun:
#CPU Parse
retval=cpusys.cycle()
#increment clock tick.
clcnt+=1
#project when the next cycle should start, then subtract current time.
xtime=(starttime + (clcnt - 1.0) * targtime) - time.time()
#sleep for remaining time (xtime) if it is above 0
if xtime>0.0:
time.sleep(xtime)
#exit code:
if sigintflg:
#benchmark session first, as uiosys.powoff() has to wait for the statup thread to terminate.
#(Store text in variables, as they need to be printed after curses has been shut down.
postout1=("VMSYSHALT -50: User Stop.")
postout2=("Approx. Speed: '" + str((float(clcnt)/(time.time()-starttime))/1000) + "' KHz")
postout3=("Target Speed : '" + str(targspeed) + "' Khz")
#shutdown UIO & curses
print(postout1)
print(postout2)
print(postout3)
progrun=0
cocpu.powoff()
cocpu.printstats()
elif retval!=None:
#benchmark session first, as uiosys.powoff() has to wait for the statup thread to terminate.
#(Store text in variables, as they need to be printed after curses has been shut down.
postout1=("VMSYSHALT " + str(retval[1]) + ": " + retval[2])
postout2=("Approx. Speed: '" + str((float(clcnt)/(time.time()-starttime))/1000) + "' KHz")
postout3=("Target Speed : '" + str(targspeed) + "' Khz")
#shutdown UIO & curses
uiosys.powoff()
curses.echo()
curses.endwin()
print(postout1)
print(postout2)
print(postout3)
cocpu.powoff()
cocpu.printstats()
progrun=0
elif cocpu.progrun==0:
postout2=("Approx. Speed: '" + str((float(clcnt)/(time.time()-starttime))/1000) + "' KHz")
postout3=("Target Speed : '" + str(targspeed) + "' Khz")
uiosys.powoff()
curses.echo()
curses.endwin()
cocpu.printstats()
print(postout2)
print(postout3)
progrun=0
#in case of drastic failure, shutdown curses!
finally:
if progrun:
curses.echo()
curses.endwin()