-
Notifications
You must be signed in to change notification settings - Fork 2
/
bin2mif.py
140 lines (130 loc) · 3.62 KB
/
bin2mif.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
# bin2mif.py - Convert PDP-8 binary file to MIF file
# Read in the binary file creates by macro8x assembler
# DEC bin file format
# http://www.bitsavers.org/www.computer.museum.uq.edu.au/pdf/DEC-08-LBAA-D%20Binary%20Loader.pdf
# Write out the Altera formatted Memory Initialization (.MIF) file to the command prompt window
# Redirect with >
# Run from windows with python ../bin2mif.py easy.bin > easy.mif
# Run from Linux with python3 ../bin2mif.py easy.bin > easy.mif
import sys
def readBinaryFile(inFileName):
"""
"""
# inBinList = []
with open(inFileName, "rb") as f:
inBinList = f.read()
# print('input file len', len(inBinList))
# print(inBinList)
return inBinList
def fillOutArray():
"""
"""
oArray = []
for count in range(4096):
oArray.append('0000')
# print('oArray',oArray)
return oArray
def getAddrVal(val1, val2):
"""
"""
nval1 = int(val1) & 0x3f;
nval2 = int(val2) & 0x3f;
val = (nval1 << 6) | nval2;
return val
def getDataVal(val1, val2):
"""
"""
nval1 = int(val1) & 0x3f;
nval2 = int(val2) & 0x3f;
val = (nval1 << 6) | nval2;
txt = '{0:o}'
return txt.format(val)
# print('argv',sys.argv)
# print('count',len(sys.argv))
if len(sys.argv) == 1:
assert False,'Usage: python ..\\bin2mif.py echo.bin'
inFileName = sys.argv[1]
print('Input file:',inFileName)
if inFileName.upper()[-4:] != '.BIN':
assert False,'file name should end in .bin'
outFileName = inFileName[:-4] + '.mif'
print('Output file:',outFileName)
binList = readBinaryFile(inFileName)
if binList[0] != 0x80:
assert False,'Expected 0x80 leader at the start of the file'
if binList[-1] != 0x80:
assert False,'Expected 0x80 leader at the end of the file'
outArray = fillOutArray()
# Ignore header
pairOffset = 0
addressOffset = 0
while pairOffset < len(binList):
if binList[pairOffset] & 0x80:
pairOffset += 1
elif binList[pairOffset] & 0x40:
addressOffset = getAddrVal(binList[pairOffset],binList[pairOffset+1])
pairOffset += 2
else:
outVal = getDataVal(binList[pairOffset],binList[pairOffset+1])
if addressOffset > 4095:
errStr = 'Output address error, address = ' + str(addressOffset) + ' probably the checksum?'
print(errStr)
else:
outArray[addressOffset] = outVal
# print('wrote',outVal,'to address',addressOffset)
addressOffset += 1
pairOffset += 2
outList = []
outStr = '-- File: ' + inFileName[0:-4] + '.mif'
outList.append(outStr)
outList.append('-- Generated by bin2mif.py')
outList.append('-- ')
outStr = 'DEPTH = '+ str(len(outArray)) + ';'
outList.append(outStr)
outStr = ''
outList.append('WIDTH = 12;')
outList.append('ADDRESS_RADIX = OCTAL;')
outList.append('DATA_RADIX = OCTAL;')
outList.append('CONTENT BEGIN')
lineCount = 0
addrCount = 0
for cell in outArray:
if lineCount == 0:
# if len(str(addrCount)) == 4:
# outVal = str(addrCount) + ':'
# elif len(str(addrCount)) == 3:
# outVal = '0' + str(addrCount) + ':'
# elif len(str(addrCount)) == 2:
# outVal = '00' + str(addrCount) + ':'
# elif len(str(addrCount)) == 1:
# outVal = '000' + str(addrCount) + ':'
# outStr += outVal
outStr += str((addrCount >> 9) & 7)
outStr += str((addrCount >> 6) & 7)
outStr += str((addrCount >> 3) & 7)
outStr += str(addrCount & 7)
outStr += ':'
lineCount += 1
addrCount += 1
newCell = ''
if len(cell) == 4:
newCell = cell
elif len(cell) == 3:
newCell = '0' + cell
elif len(cell) == 2:
newCell = '00' + cell
elif len(cell) == 1:
newCell = '000' + cell
outStr += ' ' + newCell
if lineCount == 8:
lineCount = 0
outStr += ';'
outList.append(outStr)
outStr = ''
outList.append('END;')
# for line in outList:
# print(line)
F = open(outFileName, 'w')
for row in outList:
F.writelines(row+'\n')
F.close()