OpenCores
URL https://opencores.org/ocsvn/ion/ion/trunk

Subversion Repositories ion

[/] [ion/] [trunk/] [src/] [bin2hdl.py] - Diff between revs 56 and 77

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 56 Rev 77
Line 6... Line 6...
import getopt
import getopt
import math
import math
 
 
 
 
def usage():
def usage():
 
    print ""
    print "usage:"
    print "usage:"
    print "python bin2hdl.py [arguments]\n"
    print "python bin2hdl.py [arguments]\n"
    print "Inserts data in VHDL template\n"
    print "Inserts data in VHDL template\n"
    print "ALL of the following arguments should be given, in any order:"
    print "ALL of the following arguments should be given, in any order:"
    print "{c|code} <filename>        Code binary image file name"
    print "{c|code} <filename>        Code binary image file name"
    print "{v|vhdl} <filename>        VHDL template"
    print "{v|vhdl} <filename>        VHDL template"
    print "{a|architecture} <name>    Name of target VHDL architecture"
    print "{a|architecture} <name>    Name of target VHDL architecture"
    print "{e|entity} <name>          Name of target VHDL entity"
    print "{e|entity} <name>          Name of target VHDL entity"
    print "{o|output} <filename>      Target VHDL file name"
    print "{o|output} <filename>      Target VHDL file name"
    print "code_size <number>         Size of code memory in words (decimal)"
    print "code_size <number>         Size of bram memory in words (decimal)"
    print "data_size <number>         Size of data memory in words (decimal)"
    print "data_size <number>         Size of data memory in words (decimal)"
 
    print "flash_size <number>        Size of flash memory in words (decimal)"
 
    print "(note the flash and xram info are used in simulation only)"
    print ""
    print ""
    print "Additionally, any of these arguments can be given:"
    print "Additionally, any of these arguments can be given:"
    print "{s|sim_len} <number>       Length of simulation in clock cycles"
    print "{s|sim_len} <number>       Length of simulation in clock cycles"
    print "{d|data} <filename>        Data binary image file name or 'empty'"
    print "{d|data} <filename>        Data binary image file name or 'empty'"
    print "{h|help}                   Display some help text and exit"
    print "{h|help}                   Display some help text and exit"
Line 43... Line 46...
    print "@code_addr_size@      : ceil(Log2(@code_table_size@))"
    print "@code_addr_size@      : ceil(Log2(@code_table_size@))"
    print "@data_table_size@     : Size of data RAM block, in words"
    print "@data_table_size@     : Size of data RAM block, in words"
    print "@data_addr_size@      : ceil(Log2(@data_table_size@))"
    print "@data_addr_size@      : ceil(Log2(@data_table_size@))"
 
 
 
 
 
def build_vhdl_flash_table(flash, table_size, indent_size):
 
    # Build vhdl table for flash data
 
 
 
    # fill up empty table space with zeros
 
    if len(flash) < table_size*4:
 
        flash = flash + '\0'*4*(table_size-len(flash)/4)
 
 
 
    num_words = len(flash)/4
 
    remaining = num_words;
 
    col = 0
 
    vhdl_flash_string = "\n" + " "*indent_size
 
    for w in range(num_words):
 
        b0 = ord(flash[w*4+0]);
 
        b1 = ord(flash[w*4+1]);
 
        b2 = ord(flash[w*4+2]);
 
        b3 = ord(flash[w*4+3]);
 
 
 
        if remaining > 1:
 
            item = "X\"%02X%02X%02X%02X\"," % (b0, b1, b2, b3)
 
        else:
 
            item = "X\"%02X%02X%02X%02X\"" % (b0, b1, b2, b3)
 
 
 
        remaining = remaining - 1
 
        col = col + 1
 
        if col == 4:
 
           col = 0
 
           item = item + "\n" + " "*indent_size
 
 
 
        vhdl_flash_string = vhdl_flash_string + item
 
 
 
    return vhdl_flash_string
 
 
 
 
 
 
def build_vhdl_tables(code,table_size, indent_size):
def build_vhdl_tables(code,table_size, indent_size):
    # Build the four byte column tables. [0] is LSB, [3] is MSB
    # Build the four byte column tables. [0] is LSB, [3] is MSB
 
    # Useful only for BRAM and SRAM tables
    tables = [[0 for i in range(table_size)] for i in range(4)]
    tables = [[0 for i in range(table_size)] for i in range(4)]
 
 
    # Separate binary data into byte columns
    # Separate binary data into byte columns
    # (here's where data endianess matters, we're assuming big endian)
    # (here's where data endianess matters, we're assuming big endian)
    byte = 0    # byte 0 is LSB, 3 is MSB
    byte = 0    # byte 0 is LSB, 3 is MSB
Line 134... Line 172...
 
 
 
 
    return vhdl_data_strings
    return vhdl_data_strings
 
 
def main(argv):
def main(argv):
    code_filename = ""          # file with code sections (text+reginfo+rodata)
    code_filename = ""          # file with bram contents ('code')
    data_filename = ""          # file with data sections (data+bss)
    data_filename = ""          # file with xram contents ('data')
 
    flash_filename = ""         # file with flash contents ('flash')
    vhdl_filename = ""          # name of vhdl template file
    vhdl_filename = ""          # name of vhdl template file
    entity_name = "mips_tb"     # name of vhdl entity to be generated
    entity_name = "mips_tb"     # name of vhdl entity to be generated
    arch_name = "testbench"     # name of vhdl architecture to be generated
    arch_name = "testbench"     # name of vhdl architecture to be generated
    target_filename = "tb.vhdl" # name of target vhdl file
    target_filename = "tb.vhdl" # name of target vhdl file
    indent = 4                  # indentation for table data, in spaces
    indent = 4                  # indentation for table data, in spaces
    code_table_size = -1        # size of VHDL table
    code_table_size = -1        # size of VHDL table
    data_table_size = -1        # size of VHDL table
    data_table_size = -1        # size of VHDL table
 
    flash_table_size = 32;      # default size of flash table in 32-bit words
 
    flash = ['\0']*4*flash_table_size # default simulated flash
    bin_words = 0               # size of binary file in 32-bit words 
    bin_words = 0               # size of binary file in 32-bit words 
    simulation_length = 22000   # length of logic simulation in clock cycles
    simulation_length = 22000   # length of logic simulation in clock cycles
 
 
    #
    #
 
 
    try:
    try:
        opts, args = getopt.getopt(argv, "hc:d:v:a:e:o:i:s:",
        opts, args = getopt.getopt(argv, "hc:d:v:a:e:o:i:s:f:",
        ["help", "code=", "data=", "vhdl=", "architecture=",
        ["help", "code=", "data=", "vhdl=", "architecture=",
         "entity=", "output=", "indent=", "sim_len=",
         "entity=", "output=", "indent=", "sim_len=", "flash=",
         "code_size=", "data_size="])
         "code_size=", "data_size=", "flash_size="])
    except getopt.GetoptError:
    except getopt.GetoptError, err:
 
        print ""
 
        print err
        usage()
        usage()
        sys.exit(2)
        sys.exit(2)
 
 
    # Parse coommand line parameters
    # Parse coommand line parameters
    for opt, arg in opts:
    for opt, arg in opts:
Line 171... Line 214...
            target_filename = arg
            target_filename = arg
        elif opt in ("-c", "--code"):
        elif opt in ("-c", "--code"):
            code_filename = arg
            code_filename = arg
        elif opt in ("-d", "--data"):
        elif opt in ("-d", "--data"):
            data_filename = arg
            data_filename = arg
 
        elif opt in ("-f", "--flash"):
 
            flash_filename = arg
        elif opt in ("-a", "--architecture"):
        elif opt in ("-a", "--architecture"):
            arch_name = arg
            arch_name = arg
        elif opt in ("-e", "--entity"):
        elif opt in ("-e", "--entity"):
            entity_name = arg
            entity_name = arg
        elif opt in ("-i", "--indent"):
        elif opt in ("-i", "--indent"):
Line 183... Line 228...
            simulation_length = int(arg)
            simulation_length = int(arg)
        elif opt == "--code_size":
        elif opt == "--code_size":
            code_table_size = int(arg)
            code_table_size = int(arg)
        elif opt == "--data_size":
        elif opt == "--data_size":
            data_table_size = int(arg)
            data_table_size = int(arg)
 
        elif opt == "--flash_size":
 
            flash_table_size = int(arg)
 
 
    # See if all mandatory options are there
    # See if all mandatory options are there
    if code_filename=="" or vhdl_filename=="" or \
    if code_filename=="" or vhdl_filename=="" or \
       code_table_size < 0 or data_table_size<0:
       code_table_size < 0 or data_table_size<0:
        print "Some mandatory parameter is missing\n"
        print "Some mandatory parameter is missing\n"
Line 211... Line 258...
                data = fin.read()
                data = fin.read()
                fin.close()
                fin.close()
            except IOError:
            except IOError:
                print "Binary File %s not found" % data_filename
                print "Binary File %s not found" % data_filename
 
 
 
    if flash_filename != "":
 
        if flash_filename == "empty":
 
            flash = [0]*flash_table_size
 
        else:
 
            try:
 
                fin = open(flash_filename, "rb")
 
                flash = fin.read()
 
                fin.close()
 
            except IOError:
 
                print "Binary File %s not found" % flash_filename
 
 
    #print "Read " + str(len(code)) + " bytes."
    #print "Read " + str(len(code)) + " bytes."
 
 
    # Make sure the code and data will fit in the tables
    # Make sure the code and data will fit in the tables
    bin_words = len(code) / 4
    bin_words = len(code) / 4
    if bin_words > code_table_size:
    if bin_words > code_table_size:
Line 228... Line 286...
        if bin_words > data_table_size:
        if bin_words > data_table_size:
            print "Data does not fit table: " + str(bin_words) + " words,",
            print "Data does not fit table: " + str(bin_words) + " words,",
            print str(data_table_size) + " table entries"
            print str(data_table_size) + " table entries"
            sys.exit(1)
            sys.exit(1)
 
 
 
    if flash_filename != "":
 
        bin_words = len(flash) / 4
 
        if bin_words > flash_table_size:
 
            print "Flash data does not fit table: " + str(bin_words) + " words,",
 
            print str(flash_table_size) + " table entries"
 
            sys.exit(1)
 
 
 
 
    # Build the VHDL strings for each slice of both code and data tables
    # Build the VHDL strings for each slice of both code and data tables
    vhdl_code_strings = build_vhdl_tables(code, code_table_size, indent)
    vhdl_code_strings = build_vhdl_tables(code, code_table_size, indent)
    if data_filename != "":
    if data_filename != "":
        vhdl_data_strings = build_vhdl_tables(data, data_table_size, indent)
        vhdl_data_strings = build_vhdl_tables(data, data_table_size, indent)
    else:
    else:
        # In case we didn't get a data binary, we want the vhdl compilation 
        # In case we didn't get a data binary, we want the vhdl compilation 
        # to fail when @data@ tags are used, just to catch the error
        # to fail when @data@ tags are used, just to catch the error
        vhdl_data_strings = ["error: missing data binary file"]*6
        vhdl_data_strings = ["error: missing data binary file"]*6
 
 
 
    vhdl_flash_string = build_vhdl_flash_table(flash, flash_table_size, indent)
 
 
    # Now start scanning the VHDL template, inserting data where needed
    # Now start scanning the VHDL template, inserting data where needed
 
 
    # Read template file...
    # Read template file...
    fin = open(vhdl_filename, "r")
    fin = open(vhdl_filename, "r")
    vhdl_lines = fin.readlines()
    vhdl_lines = fin.readlines()
Line 252... Line 319...
                "@code31@", "@code20@",
                "@code31@", "@code20@",
                "@code-32bit@",
                "@code-32bit@",
                "@data0@","@data1@","@data2@","@data3@",
                "@data0@","@data1@","@data2@","@data3@",
                "@data31@", "@data20@",
                "@data31@", "@data20@",
                "@data-32bit@",
                "@data-32bit@",
 
                "@flash@",
                "@entity_name@","@arch_name@",
                "@entity_name@","@arch_name@",
                "@sim_len@",
                "@sim_len@",
                "@xram_size@",
                "@xram_size@",
                "@code_table_size@","@code_addr_size@",
                "@code_table_size@","@code_addr_size@",
                "@data_table_size@","@data_addr_size@"];
                "@data_table_size@","@data_addr_size@",
 
                "@prom_size@"];
    replacement = vhdl_code_strings + vhdl_data_strings + \
    replacement = vhdl_code_strings + vhdl_data_strings + \
                 [entity_name, arch_name,
                 [vhdl_flash_string,
 
                  entity_name, arch_name,
                  str(simulation_length),
                  str(simulation_length),
                  str(data_table_size),
                  str(data_table_size),
                  str(code_table_size),
                  str(code_table_size),
                  str(int(math.floor(math.log(code_table_size,2)))),
                  str(int(math.floor(math.log(code_table_size,2)))),
                  str(data_table_size),
                  str(data_table_size),
                  str(int(math.floor(math.log(data_table_size,2))))]
                  str(int(math.floor(math.log(data_table_size,2)))),
 
                  str(flash_table_size)]
 
 
    # Now traverse the template lines replacing any keywords with the proper 
    # Now traverse the template lines replacing any keywords with the proper 
    # vhdl stuff we just built above.
    # vhdl stuff we just built above.
    output = ""
    output = ""
    for vhdl_line in vhdl_lines:
    for vhdl_line in vhdl_lines:

powered by: WebSVN 2.1.0

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