URL
https://opencores.org/ocsvn/a-z80/a-z80/trunk
[/] [a-z80/] [trunk/] [tools/] [zmac/] [bin2mif.py] - Blame information for rev 18
Go to most recent revision |
Details |
Compare with Previous |
View Log
Line No. |
Rev |
Author |
Line |
1 |
8 |
gdevic |
#!/usr/bin/env python3
|
2 |
|
|
import sys, os
|
3 |
|
|
from argparse import ArgumentParser
|
4 |
|
|
|
5 |
|
|
parser = ArgumentParser(description='Converts binary file to mif file')
|
6 |
|
|
|
7 |
|
|
parser.add_argument('infile', help='Input binary file')
|
8 |
|
|
parser.add_argument('outfile', help='Output mif file')
|
9 |
|
|
parser.add_argument('-d', '--depth', help='Total memory depth (fill with zeros)', type=int, default=0)
|
10 |
|
|
parser.add_argument('-s', '--simple', help='Simple format (data only)', action="store_true", default=False)
|
11 |
|
|
args = parser.parse_args()
|
12 |
|
|
|
13 |
|
|
with open(args.infile, 'rb') as f:
|
14 |
|
|
depth = os.path.getsize(args.infile)
|
15 |
|
|
if args.depth > 0:
|
16 |
|
|
if args.depth < depth:
|
17 |
|
|
print ("ERROR: Input file is larger than the specified depth!")
|
18 |
|
|
exit (-1)
|
19 |
|
|
depth = args.depth
|
20 |
|
|
|
21 |
|
|
with open(args.outfile, 'w') as fmif:
|
22 |
|
|
if args.simple:
|
23 |
|
|
count = 0
|
24 |
|
|
buffer = f.read()
|
25 |
|
|
for b in buffer:
|
26 |
|
|
fmif.write('{0:08b}\n'.format(b))
|
27 |
|
|
count += 1
|
28 |
|
|
while count < depth:
|
29 |
|
|
fmif.write('00000000')
|
30 |
|
|
count += 1
|
31 |
|
|
else:
|
32 |
|
|
fmif.write('-- Automatically generated by bin2mif.py\n')
|
33 |
|
|
fmif.write('-- Translated from ' + args.infile + '\n')
|
34 |
|
|
fmif.write('DEPTH = {};\n'.format(depth))
|
35 |
|
|
fmif.write('WIDTH = 8;\n')
|
36 |
|
|
fmif.write('ADDRESS_RADIX = DEC;\n')
|
37 |
|
|
fmif.write('DATA_RADIX = HEX;\n')
|
38 |
|
|
fmif.write('CONTENT\n')
|
39 |
|
|
fmif.write('BEGIN\n')
|
40 |
|
|
|
41 |
|
|
count = 0
|
42 |
|
|
buffer = f.read()
|
43 |
|
|
for b in buffer:
|
44 |
|
|
fmif.write('{0:<4}: {1:02x};\n'.format(count, b))
|
45 |
|
|
count += 1
|
46 |
|
|
if count < depth:
|
47 |
|
|
fmif.write('[{0}..{1}] : 0;\n'.format(count, depth))
|
48 |
|
|
fmif.write('END;\n')
|
© copyright 1999-2024
OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.