forked from n6il/eou_ide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakebootfileide.py
350 lines (309 loc) · 10.9 KB
/
makebootfileide.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
#!/usr/bin/python
import os
import sys
import argparse
import subprocess
import shlex
import json
def parseArgs():
cpus = ['6809', '6309']
emulators = ['xroar', 'mame', 'coco3fpga']
dwTypes = ['becker', 'bitBanger', 'bbCoCo1', 'rs232', 'mmmpi']
parser = argparse.ArgumentParser(
description='EOU Ide Disk Creator')
parser.add_argument(
'-c',
'--cpu',
dest='cpu',
help='CPU Type: %s' % (cpus))
parser.add_argument(
'-i',
'--vhd',
dest='EOUVHD',
help='Source EOU VHD File')
parser.add_argument(
'-d',
'--dw',
dest='dw',
help='Make Dw Image in addition to IDE image: %s' % (dwTypes))
parser.add_argument(
'-e',
'--emulator',
dest='emulator',
help='Which Emulator to build for: %s ' % (emulators))
parser.add_argument(
'-k',
'--kwikgen',
dest='kwikgen',
help='Build this specific kwikgen, ignores --dw and --emulation')
args = parser.parse_args()
# error handling
err = False
# required options
# cpu
if args.cpu is None:
print('ERROR: Must specify cpu type: %s' %(cpus))
err = True
else:
if args.cpu not in cpus:
print('ERROR: %s: Invalid CPU. Pick from %s' % (args.cpu, cpus))
err = True
else:
args.cpuShort = args.cpu[:2]
# vhd path
if args.EOUVHD is None:
print('ERROR: Must specify source EOU VHD File')
err = True
else:
p = os.path.abspath(os.path.expanduser(args.EOUVHD))
if os.path.exists(p):
args.EOUVHD = p
else:
print('ERROR: %s: no such file' % args.EOUVHD)
err = True
# dw
if args.dw and args.dw not in dwTypes:
print('ERROR: %s: not a valid dw type. Pick from %s' % (args.dw, dwTypes))
err = 1
# cpu
if args.emulator is None and args.kwikgen is None:
print('ERROR: Must specify emulator type: %s' %(emulators))
err = True
elif args.kwikgen is None:
if args.emulator not in emulators:
print('ERROR: %s: Invalid Emulator. Pick from %s' % (args.emulator, emulators))
err = True
if err:
parser.print_usage()
sys.exit(1)
return args
def pathSetup(args, subDir=None):
buildSubDir = args.cpu
if subDir is not None:
buildSubDir = '%s_%s' % (args.cpu, subDir)
args.scriptDir = os.path.dirname(os.path.abspath(__file__))
args.buildDir = os.path.join(args.scriptDir, 'build', buildSubDir)
args.moduleDir = os.path.join(args.buildDir, 'modules')
args.localModuleDir = os.path.join(args.scriptDir, "modules", args.emulator, args.cpu)
args.IDEHDR = os.path.join(args.scriptDir, 'header.ide')
args.IDEDSK = os.path.join(args.buildDir, '%sIDE.dsk' % (args.cpuShort))
args.IDEVHD = os.path.join(args.buildDir, '%sIDE.VHD' % (args.cpuShort))
args.CC3FPGAVHD = os.path.join(args.buildDir, '%sCOCO3FPGA.VHD' % (args.cpuShort))
args.IDEIDE = os.path.join(args.buildDir, '%sIDE.ide' % (args.cpuShort))
return args
def runCmd(cmd):
cp = shlex.split(cmd)
print(cmd)
p = subprocess.Popen(cp, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()
if p.returncode != 0:
raise Exception('%s: %s' % (cp[0], p.stderr.read()))
return(p.returncode, p.stdout.read())
args = parseArgs()
EOUMODDIR='MODULES/%sL2/MODULES' % (args.cpu)
os9_format='os9 format -e %s'
os9_copy='os9 copy %s,%s %s'
os9_copy_to='os9 copy -r %s %s,%s'
os9_modbust='os9 modbust %s,OS9Boot'
os9_gen='os9 gen -b%s -t%s %s'
os9_gen_frag='os9 gen -e -b=%s -t=%s %s'
kernel_ide='kernel_ide'
bootfile_ide='OS9Boot'
miscFiles = ('README.txt', 'hdblba.rom', 'start-ide.sh')
scriptFiles = ('start-ide.sh',)
padding=b'\x00'*256
def copyPath(fnl, spn, dpn):
for fn in fnl:
sfn = os.path.join(spn, fn)
dfn = os.path.join(dpn, fn)
print('cp %s %s' % (sfn, dfn))
f=open(sfn, 'rb')
g=open(dfn, 'wb')
g.write(f.read())
f.close()
g.close()
def copyFile(fnl, gn, pad=False, mode='wb'):
g=open(gn, mode)
dd = '>'
if mode.startswith('a'):
dd = '>>'
for fn in fnl:
print('cat %s %s %s' % (fn, dd, gn))
f=open(fn, 'rb')
data = f.read(256)
i=256
while data:
g.write(data)
if pad:
g.write(padding)
data = f.read(256)
i += 256
f.close()
dd = '>>'
g.close()
def copyFileToVhd(sfn, vhd, dfn):
cmd = os9_copy_to % (sfn, vhd, dfn)
rc, output = runCmd(cmd)
def addPath(path, files):
return [os.path.join(path, m) for m in files]
# KwikGen:
# Starting with an existing Nitros9 VHD, use modbust to get all of the
# modules out of the OS9Boot file on that VHD. Modules are placed in the
# specified path. Create a new OS9Boot file using the povided
# instructions. The result is placed at the specified dst location.
# Path: Working directory where modules should go
# srcVhd - Nitros9 VHD
# dst - path to new OS9Boot file
# instList - list of instructions
def kwikGen(path, srcVhd, dst, instList):
pwd = os.getcwd()
os.chdir(path)
cmd = os9_modbust % (srcVhd)
rc, output = runCmd(cmd)
mods = []
for l in output.decode('ascii').split('\n'):
lp = l.split(' ')
if len(lp) != 3:
continue
mods.append(lp[2].replace('...', ''))
for inst in instList:
action = inst['action']
if action == 'del':
module = inst['module']
try:
i = mods.index(module)
print('KwikGen: del module: %s' % module)
del mods[i]
except ValueError:
raise Exception('KwikGen: %s: Module not found' % module)
elif action == 'ins':
module = inst['module']
after = inst['after']
try:
i = mods.index(after)
print('KwikGen: ins module: %s after %s' % (module, after))
mods.insert(i+1, module)
except ValueError:
raise Exception('KwikGen: %s: Module not found' % after)
else:
raise Exception('KwinGen: Unknown action: %s' % action)
copyFile(mods, dst)
os.chdir(pwd)
def doBuild(buildDir, localMods, copyMods, kernelMods, kwMods, copyFiles):
print('-' * 50)
print("0. setup")
if not os.path.exists(os.path.dirname(buildDir)) or not os.path.exists(buildDir):
os.makedirs(buildDir)
moduleDir = os.path.join(buildDir, "modules")
if not os.path.exists(moduleDir):
os.makedirs(moduleDir)
pwd = os.getcwd()
print('-' * 50)
print("1. Copy some modules out of the EOU Disk")
os.chdir(moduleDir)
#for d, mod in modsCopy:
for d, mod in copyMods:
path='/'.join([EOUMODDIR, d, mod])
cmd = os9_copy % (args.EOUVHD, path, mod)
runCmd(cmd)
print('-' * 50)
print("2. Copy local modules into module dir")
os.chdir(moduleDir)
copyPath(localMods, args.localModuleDir, moduleDir)
print('-' * 50)
print("3. Create kernel containing boot_ide")
os.chdir(buildDir)
copyFile(addPath(moduleDir, kernelMods), kernel_ide)
print('-' * 50)
print("4. Make new boot file which has IDE drivers inserted")
newBootFile = os.path.join(buildDir, 'OS9Boot')
#kwikGen(moduleDir, args.EOUVHD, newBootFile, instns)
kwikGen(moduleDir, args.EOUVHD, newBootFile, kwMods)
if args.emulator == 'coco3fpga':
print('-' * 50)
print("5. Write new kernel and OS9Boot to VHD")
copyFile([args.EOUVHD], args.CC3FPGAVHD)
runCmd('os9 copy %s,LIB/sys.l sys.l' % args.CC3FPGAVHD)
runCmd('os9 del %s,LIB/sys.l' % args.CC3FPGAVHD)
cmd = os9_gen % (bootfile_ide, kernel_ide, args.CC3FPGAVHD)
runCmd(cmd)
runCmd('os9 copy sys.l %s,LIB/sys.l' % args.CC3FPGAVHD)
else:
print('-' * 50)
print("5. Make a new boot disk using the new kernel and OS9Boot")
cmd = os9_format % (args.IDEDSK)
if (os.path.exists(args.IDEDSK)):
print('rm %s' % args.IDEDSK)
os.unlink(args.IDEDSK)
runCmd(cmd)
cmd = os9_gen % (bootfile_ide, kernel_ide, args.IDEDSK)
runCmd(cmd)
if args.emulator == 'xroar':
print('-' * 50)
print("6. Start making the new IDE VHD Disk. Copy the floppy disk image to the")
print(" drive with padding to IDE's 512-byte sectors")
copyFile([args.IDEDSK], args.IDEVHD, pad=True)
copyFile([args.IDEDSK], args.IDEDSK+'.pad', pad=True)
print('-' * 50)
print("7. Nitros9 knows how to do 512-byte sectors, copy the contents of the")
print(" EOU disk into the IDE VHD.")
copyFile([args.EOUVHD], args.IDEVHD, mode='ab')
print('-' * 50)
print("8. Make the XRoar IDE Image. XRoar needs a header, so put that on first")
copyFile([args.IDEHDR], args.IDEIDE)
copyFile([args.IDEVHD], args.IDEIDE, mode='ab')
elif args.emulator == 'mame':
print('-' * 50)
print("6. Use the Original VHD File in your emulator:")
print(" %s" % (args.EOUVHD))
elif args.emulator == 'coco3fpga':
print('-' * 50)
print("6. Copy files to image")
os.chdir(args.localModuleDir)
for sfn, dfn in copyFiles:
copyFileToVhd(sfn, args.CC3FPGAVHD, dfn)
runCmd('os9 attr %s,sysgo -e -pe' % args.CC3FPGAVHD)
print('-' * 50)
print("7. Use the coco3fpga VHD File in your coco3fpga:")
print(" %s" % (args.CC3FPGAVHD))
os.chdir(args.buildDir)
print('-' * 50)
print("9. Copy Misc files to build dir")
copyPath(miscFiles, args.scriptDir, buildDir)
copyPath(scriptFiles, args.localModuleDir, buildDir)
os.chdir(pwd)
## main
if args.kwikgen is not None:
kwikGenList = [[args.kwikgen]]
else:
kwikGenList = [['ide_%s' % args.emulator]]
if args.dw:
kwikGenList += [['ide_%s' % args.emulator, 'dw_%s' % (args.dw)]]
msgs = []
for kwikGens in kwikGenList:
localMods = []
copyMods = []
kernelMods = []
instns = []
copyFiles = []
subDir = '_'.join(kwikGens)
args = pathSetup(args, subDir=subDir)
for kw in kwikGens:
with open(os.path.join(args.scriptDir, 'kwikgens', kw+'.json')) as f:
t = json.load(f)
localMods += t.get('localMods', [])
copyMods += t.get('copyMods', [])
kernelMods += t.get('kernelMods', [])
instns += t.get('instructions', [])
copyFiles += t.get('copyFiles', [])
msg = []
print('')
msg.append('='*50)
msg.append('KwikGens: %s' % kwikGens)
msg.append('Output Dir: %s' % args.buildDir)
msg.append('='*50)
print('\n'.join(msg))
msgs += msg
doBuild(args.buildDir, localMods, copyMods, kernelMods, instns, copyFiles)
print('')
print('\n'.join(msgs))