OpenCores
URL https://opencores.org/ocsvn/a-z80/a-z80/trunk

Subversion Repositories a-z80

[/] [a-z80/] [trunk/] [tools/] [zmac/] [bin2coe.py] - Blame information for rev 8

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 Xilinx coe file')
6
 
7
parser.add_argument('infile', help='Input binary file')
8
parser.add_argument('outfile', help='Output coe file')
9
parser.add_argument('-d', '--depth', type=int, default=0)
10
args = parser.parse_args()
11
 
12
with open(args.infile, 'rb') as f:
13
    depth = os.path.getsize(args.infile)
14
    if args.depth > 0:
15
        if args.depth < depth:
16
            print ("ERROR: Input file is larger than the specified depth!")
17
            exit (-1)
18
        depth = args.depth
19
 
20
    with open(args.outfile, 'w') as fmif:
21
        fmif.write('; Automatically generated by bin2coe.py\n')
22
        fmif.write('; Translated from ' + args.infile + '\n')
23
        fmif.write('memory_initialization_radix=16;\n')
24
        fmif.write('memory_initialization_vector=\n')
25
 
26
        count = 0
27
        buffer = f.read()
28
        for b in buffer:
29
            if count > 0:
30
                fmif.write(',\n')
31
            fmif.write('{0:02x}'.format(b))
32
            count += 1
33
 
34
        while count < depth:
35
            if count < depth:
36
                fmif.write(',\n')
37
            fmif.write('00')
38
            count += 1
39
        fmif.write(';\n')

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.