1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-21 23:30:58 +00:00

python 2->3 for tools

This commit is contained in:
Steven Hugg 2018-11-22 14:11:56 -05:00
parent 6e50cbb20e
commit 149ee4a2eb
14 changed files with 90 additions and 96 deletions

View File

@ -10,7 +10,7 @@ for root, dirs, files in os.walk("./presets"):
with open(path,'r') as f:
data = f.read()
if data[0] != '\n':
print(path,'no initial newline')
print((path,'no initial newline'))
except:
print(path,sys.exc_info()[0])
print((path,sys.exc_info()[0]))

View File

@ -27,4 +27,4 @@ for n in range(1,MAXBITS):
seqindex = seq.index(x)
seqlen = len(seq) - seqindex
if seqlen>1:
print(seqlen, "#(%d,%d'%s,%d)" % (n,n,bin(i)[1:],invert), seqindex)
print((seqlen, "#(%d,%d'%s,%d)" % (n,n,bin(i)[1:],invert), seqindex))

View File

@ -71,7 +71,7 @@ def find_common_substrings(s):
def get_best_substring(ss):
best = (0,0,0)
for k,v in ss.items():
for k,v in list(ss.items()):
if v[0] > best[2]:
best = (k,v[1],v[0])
return best
@ -88,7 +88,7 @@ def replace_substrings(s, bss):
i,l,n = bss
count = (n+1)/(l-1)
match = s[i:i+l]
print i,l,n,count,repr(match)
print((i,l,n,count,repr(match)))
r = s[0:i]
while i<len(s):
r += chr(g_code)
@ -99,7 +99,7 @@ def replace_substrings(s, bss):
i = p
g_subs.append(match + chr(0xff))
g_code += 1
print len(s), len(r)+n+l
print((len(s), len(r)+n+l))
assert len(s) == len(r)+n+l
return r
@ -111,10 +111,10 @@ def channels_for_track(track):
return list(channels)
if not args.midichannels:
print mid
print mid.length, 'seconds'
print(mid)
print((mid.length, 'seconds'))
for i, track in enumerate(mid.tracks):
print('Track {}: {} ({}) {}'.format(i, track.name, len(track), channels_for_track(track)))
print(('Track {}: {} ({}) {}'.format(i, track.name, len(track), channels_for_track(track))))
#for msg in track:
# print(msg)
else:
@ -123,9 +123,9 @@ else:
nnotes = 0
nvoices = 0
curchans = 0
channels = [int(x) for x in string.split(args.midichannels, ',')]
print
print "// %s %s" % (mid, channels)
channels = [int(x) for x in args.midichannels.split(',')]
print ('')
print(("// %s %s" % (mid, channels)))
output = []
for msg in mid:
gtime += msg.time * tempo
@ -150,22 +150,22 @@ else:
curchans |= 1<<msg.channel
output.append(0xff)
if coutput:
print string.join([hex2(x) for x in output], ',')
print((','.join([hex2(x) for x in output])))
else:
bighex = string.join([hex1(x) for x in output], '')
bighex = ''.join([hex1(x) for x in output])
for i in range(0,len(bighex)+32,32):
print '\thex', bighex[i:i+32]
print(('\thex', bighex[i:i+32]))
if compress:
# replace common substrings
bout = ''.join(chr(i) for i in output)
for iter in range(0,32):
ss = find_common_substrings(bout)
bss = get_best_substring(ss)
print bss
print(bss)
if bss[1] == 0:
break
bout = replace_substrings(bout, bss)
print repr(bout)
print((repr(bout)))
# build substring table
ofs = (len(g_subs)+1)*2
zout = offset2str(ofs)
@ -178,5 +178,5 @@ else:
for ss in g_subs:
zout += ss
# print output
print string.join([hex2(ord(x)) for x in zout], ',')
print "// compressed %d -> %d bytes" % (len(output), len(zout))
print((','.join([hex2(ord(x)) for x in zout])))
print(("// compressed %d -> %d bytes" % (len(output), len(zout))))

View File

@ -35,14 +35,14 @@ for a440 in range(4300,4500):
results.sort()
best_error, best_a440 = results[0]
best_a440 /= 10.0
print '//', args
print '//', best_a440, best_error, test_notes
print('//', args)
print('//', best_a440, best_error, test_notes)
print "const int note_table[%d] = {" % final_notes
print("const int note_table[%d] = {" % final_notes)
for note in range(0,final_notes):
notehz = best_a440 * math.pow(2.0, (note - 49) / 12.0);
period = int(round(basehz / notehz)) - bias
while period > maxval:
period /= 2
print '%d,' % period,
print "};"
print('%d,' % period, end='')
print("};")

View File

@ -45,21 +45,18 @@ for a440 in range(4200,4600):
if period3 < s or period3 > 32*s:
tonehz3 = -10000
error += min(abs(notehz-tonehz), abs(notehz-tonehz2), abs(notehz-tonehz3))
#print a440,note,notehz,notehz-tonehz,period
#if a440 == 4405:
# print '%d,%f,%d' % (note,tonehz-notehz,period)
results.append((error, a440))
results.sort()
best_error, best_a440 = results[0]
best_a440 /= 10.0
print '//', best_a440, best_error, test_notes
print('//', best_a440, best_error, test_notes)
periods = []
tones = []
bits = []
print "const int note_table[%d] = {" % final_notes
print("const int note_table[%d] = {" % final_notes)
for note in range(0,final_notes):
notehz = best_a440 * math.pow(2.0, (note - 49) / 12.0);
bestperiod = 255
@ -75,8 +72,8 @@ for note in range(0,final_notes):
bestperiod = period
besthz = hz
#print '%d,' % period,
print note, besthz, bestperiod, notehz
#print(note, besthz, bestperiod, notehz)
print('%d,' % period, end='')
periods.append(bestperiod / s - 1)
bits.append(bittable[bestperiod & (s-1)])
if besthz==basehz:
@ -87,8 +84,8 @@ for note in range(0,final_notes):
tones.append(6)
else:
tones.append(0)
print "};"
print("};")
print periods
print bits
print tones
print(periods)
print(bits)
print(tones)

View File

@ -23,15 +23,15 @@ def out(i, pix, lb, hb, reverse=0, shift=0):
with open(sys.argv[1],'rb') as f:
# read PBM header
header = f.readline().strip()
assert(header == 'P4')
assert(header == b'P4')
dims = f.readline().strip()
if dims[0] == '#':
if dims[0:1] == b'#':
dims = f.readline().strip()
width,height = map(int, dims.split())
width,height = list(map(int, dims.split()))
assert(width==48)
# read bitmap rows
for y in range(0,height):
row = bytes(f.read(6) + '\0\0') # pad to 8 bytes
row = bytes(f.read(6) + b'\0\0') # pad to 8 bytes
# convert to 64-bit integer
pix = struct.unpack('<q',row)[0]
#print '%010lx' % pix
@ -45,14 +45,14 @@ with open(sys.argv[1],'rb') as f:
# output bitmap tables
for c in range(0,6):
print "\talign $100"
print "Bitmap%d" % c
print "\thex 00"
print("\talign $100")
print(("Bitmap%d" % c))
print("\thex 00")
s = '\thex '
for i in range(0,height):
s += '%02x' % output[c][height-i-1]
if i % 16 == 15:
print s
print(s)
s = '\thex '
if len(s)>5:
print s
print(s)

View File

@ -23,22 +23,22 @@ def out(i, pix, lb, hb, reverse=0, shift=0):
with open(sys.argv[1],'rb') as f:
# read PBM header
header = f.readline().strip()
assert(header == 'P4')
assert(header == b'P4')
dims = f.readline().strip()
if dims[0] == '#':
if dims[0:1] == b'#':
dims = f.readline().strip()
width,height = map(int, dims.split())
print(dims)
width,height = list(map(int, dims.split()))
assert(width==40)
# read bitmap rows
for y in range(0,height):
row = bytes(f.read(5) + '\0\0\0') # pad to 8 bytes
row = bytes(f.read(5) + b'\0\0\0') # pad to 8 bytes
# convert bytes from MSB first to LSB first
row2 = ''
row2 = []
for i in range(0,8):
row2 += chr(rev(ord(row[i])))
row2.append(rev(row[i]))
# convert to 64-bit integer
pix = struct.unpack('<q',row2)[0]
#print '%010lx' % pix
pix = struct.unpack('<q',bytes(row2))[0]
# generate playfield bytes
out(0, pix, 0, 4, 0, 4)
out(1, pix, 4, 12, 1)
@ -49,14 +49,12 @@ with open(sys.argv[1],'rb') as f:
# output bitmap tables
for c in range(0,6):
#print "\talign $100"
print "PFBitmap%d" % c
#print "\thex 00"
print(("PFBitmap%d" % c))
s = '\thex '
for i in range(0,height):
s += '%02x' % output[c][height-i-1]
if i % 16 == 15:
print s
print(s)
s = '\thex '
if len(s)>5:
print s
print(s)

View File

@ -13,7 +13,6 @@ with open(sys.argv[1],'r') as f:
for l in lines:
l = l.strip()
toks = l.split()
#print l,toks
if toks[0] == 'ENCODING':
chord = int(toks[1])
elif toks[0] == 'BITMAP':
@ -26,7 +25,7 @@ with open(sys.argv[1],'r') as f:
bytes.insert(0,0)
assert(len(bytes) == height)
bytes.reverse()
print chord,bytes
print((chord,bytes))
chars[chord] = bytes
elif inbitmap and len(toks) == 1:
byte = int(toks[0],16)
@ -63,10 +62,10 @@ def tohex2(v):
def tobin(v):
return "bitarray[0][0]=3'b{0:3b};\n".format(v)
print '\thex ' + string.join(map(tohex,output),'')
print string.join(map(tohex2,output),',')
print '\thex ' + string.join(map(tohex,outputlo),'')
print '\thex ' + string.join(map(tohex,outputhi),'')
print string.join(map(tobin,output),'')
print(('\thex ' + ''.join(map(tohex,output))))
print((''.join(map(tohex2,output))))
print(('\thex ' + ''.join(map(tohex,outputlo))))
print(('\thex ' + ''.join(map(tohex,outputhi))))
print((''.join(map(tobin,output))))
print len(output),len(outputlo),len(outputhi)
print((len(output),len(outputlo),len(outputhi)))

View File

@ -50,16 +50,16 @@ with open(args.bdffile,'r') as f:
if bytes[y] & (0x40 >> x):
b |= 0x0f
output.append(b)
print 'const char CH_%d[] = { %s };' % ( chord, string.join([tohex2(x) for x in output], ',') )
print('const char CH_%d[] = { %s };' % ( chord, string.join([tohex2(x) for x in output], ',') ))
chars[chord] = 'CH_%d' % chord
elif inbitmap and len(toks) == 1:
byte = int(toks[0],16)
bytes.append(byte)
print 'const char* const FONT_TABLE[%d] = {' % (hichar-lochar+1),
print('const char* const FONT_TABLE[%d] = {' % (hichar-lochar+1), end=' ')
for ch in range(lochar, hichar+1):
if chars.get(ch):
print '%s,' % chars[ch],
print('%s,' % chars[ch], end=' ')
else:
print '0,',
print "};"
print('0,', end=' ')
print("};")

View File

@ -99,20 +99,20 @@ def tohexv(v):
for arr in [output]:
if args.asmhex:
print '\thex ' + string.join(map(tohex,arr),'')
print('\thex ' + string.join(list(map(tohex,arr)),''))
if args.asmdb:
for i in range(0,len(output),height):
print '.DB', string.join(map(tohex2,arr[i:i+height]),','), ';%d'%(i/height+lochar)
print('.DB', string.join(list(map(tohex2,arr[i:i+height])),','), ';%d'%(i/height+lochar))
if args.carray:
print "static char FONT[%d][%d] = {" % (hichar-lochar+1, height)
print("static char FONT[%d][%d] = {" % (hichar-lochar+1, height))
for i in range(0,len(output),height):
print '{', string.join(map(tohex2,arr[i:i+height]),','), '},',
print
print "};"
print('{', string.join(list(map(tohex2,arr[i:i+height])),','), '},', end=' ')
print()
print("};")
if args.flatcarray:
print "static char FONT[%d] = {" % ((hichar-lochar+1) * height)
print string.join(map(tohex2,arr),',')
print "}";
print("static char FONT[%d] = {" % ((hichar-lochar+1) * height))
print(string.join(list(map(tohex2,arr)),','))
print("}");
if args.verilog:
j = 0
for i in range(0,len(output),height):
@ -121,5 +121,5 @@ for arr in [output]:
#print "rom["+str(j)+"] = 32'h" + string.join(map(tohex,arr[i+height/2:i+height]),'') + ";"
#j += 1
#print "rom["+str(j)+"] = 64'h" + string.join(map(tohex,arr[i:i+height]),'') + ";"
print string.join(map(tohexv,arr[i:i+height]),',') + ", //%d" % (i/height)
print(string.join(list(map(tohexv,arr[i:i+height])),',') + ", //%d" % (i/height))
j += 1

View File

@ -27,11 +27,11 @@ with open(sys.argv[1],'rb') as f:
width,height = map(int, dims.split())
wbytes = (width+7)/8
data = f.read()
print "{%d,%d," % (wbytes,height),
print("{%d,%d," % (wbytes,height), end='')
for i in range(0,len(data)):
if i>0:
sys.stdout.write(",")
ofs = i+wbytes-(i%wbytes)*2-1
sys.stdout.write( "0x%02x" % ord(data[ofs]) )
print "}"
print("}")

View File

@ -19,21 +19,21 @@ with open(sys.argv[1],'rb') as f:
assert data[3] == 8
# palette
print "byte palette[16] = {",
print("byte palette[16] = {", end='')
for i in range(0,16):
r = data[16+i*3]
g = data[17+i*3]
b = data[18+i*3]
entry = (r>>5) | ((g>>2)&0x38) | (b&0xc0)
print '%s,' % (tohex2(entry)),
print "}"
print('%s,' % (tohex2(entry)), end='')
print("}")
# image data
width = (data[9] << 8) + data[8] + 1
height = (data[11] << 8) + data[10] + 1
rowlen = (data[0x43] << 8) + data[0x42]
print "const byte sprite[] = {"
print "%d,%d," % ((width+1)/2,height)
print("const byte sprite[] = {")
print("%d,%d," % ((width+1)/2,height))
for y in range(0,height):
ofs = 0x80 + y*rowlen
output = []
@ -41,6 +41,6 @@ with open(sys.argv[1],'rb') as f:
b = (tocolor(data[ofs]) << 4) + tocolor(data[ofs+1])
output.append(b)
ofs += 2
print string.join(map(tohex2,output),',') + ','
print "}"
print(','.join(map(tohex2,output)) + ',')
print("}")

View File

@ -11,6 +11,6 @@ n = 4
m = 7
for i in range(0,n*4):
print '%d,' % int(round(math.sin(i*math.pi/2/n)*m)),
print('%d,' % int(round(math.sin(i*math.pi/2/n)*m)), end='')
if i % 16 == 15:
print
print('')

View File

@ -14,7 +14,7 @@ def textToColor(name,opacity):
return None
color = int(name[1:],16) << 8
if opacity:
print 'opacity',opacity
print(('opacity',opacity))
return color
svg_file = sys.argv[1]
@ -24,7 +24,7 @@ groups = doc.getElementsByTagName('g')
for grp in groups:
groupID = grp.getAttribute('id')
paths = grp.getElementsByTagName('path')
print groupID,paths
print((groupID,paths))
if len(paths):
for path in paths:
shape = {}
@ -36,7 +36,7 @@ for grp in groups:
nvarr = s.split(':', 2)
if len(nvarr) == 2:
styleAttrs[nvarr[0]] = nvarr[1]
print path,d,styleAttrs
print((path,d,styleAttrs))
shape['pts'] = []
shape['currentColor'] = textToColor(styleAttrs.get("stroke"), styleAttrs.get("stroke-opacity"))
shape['currentWidth'] = textToFloat(styleAttrs.get("strokeWidth"))
@ -47,7 +47,7 @@ for grp in groups:
i0 = 0
start = True
for cmd in cmds:
print cmd
print(cmd)
ch = cmd[0]
if ch == 'm' or ch == 'l':
start = True
@ -58,7 +58,7 @@ for grp in groups:
skip = True
else:
xy = cmd.split(',')
print xy
print(xy)
x += float(xy[0])
y -= float(xy[1])
if start or hidden: