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

Subversion Repositories a-z80

[/] [a-z80/] [trunk/] [cpu/] [control/] [genmatrix.py] - Diff between revs 6 and 8

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

Rev 6 Rev 8
Line 1... Line 1...
#!/usr/bin/env python
#!/usr/bin/env python3
#
#
# This script reads A-Z80 instruction timing data from a spreadsheet text file
# This script reads A-Z80 instruction timing data from a spreadsheet text file
 
# 'Timings.csv' (which is a TAB-delimited text file exported from 'Timings.xlsm')
# and generates a Verilog include file defining the control block execution matrix.
# and generates a Verilog include file defining the control block execution matrix.
# Token keywords in the timing spreadsheet are substituted using a list of keys
# Token keywords in the timing spreadsheet are substituted using a list of keys
# stored in the macros file. See the macro file for the format information.
# defined in 'timing_macros.i'.
#
 
# Input timing file is exported from the Excel file as a TAB-delimited text file.
 
#
#
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
#  Copyright (C) 2014  Goran Devic
#  Copyright (C) 2014,2016  Goran Devic
#
#
#  This program is free software; you can redistribute it and/or modify it
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the Free
#  under the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 2 of the License, or (at your option)
#  Software Foundation; either version 2 of the License, or (at your option)
#  any later version.
#  any later version.
Line 23... Line 22...
import string
import string
import sys
import sys
import csv
import csv
import os
import os
 
 
# Input file exported from a timing spreadsheet:
# Input file (exported from 'Timings.xlsm'):
fname = "Timings.csv"
fname = "Timings.csv"
 
 
# Input file containing macro substitution keys
# Input file containing macro substitution keys
kname = "timing_macros.i"
kname = "timing_macros.i"
 
 
# Set this to 1 if you want abbreviated matrix (no-action lines removed)
# Set this to 1 if you want abbreviated matrix (no-action lines removed)
abbr = 1
abbr = 1
 
 
 
# Set this to 0 if you want to strip all comments from the resulting file
 
comment = 1
 
 
# Set this to 1 if you want debug $display() printout on each PLA line
# Set this to 1 if you want debug $display() printout on each PLA line
debug = 0
debug = 0
 
 
# Print this string in front of every line that starts with "ctl_". This helps
# Print this string in front of every line that starts with "ctl_". This helps
# formatting the output to be more readable.
# formatting the output to be more readable.
Line 46... Line 48...
with open(kname, 'r') as f:
with open(kname, 'r') as f:
    for line in f:
    for line in f:
        if len(line.strip())>0 and line[0]!='/':
        if len(line.strip())>0 and line[0]!='/':
            # Wrap up non-starting //-style comments into /* ... */ so the
            # Wrap up non-starting //-style comments into /* ... */ so the
            # line can be concatenated while preserving comments
            # line can be concatenated while preserving comments
            if line.find("//")>0:
            i = line.find("//")
 
            if i>0:
 
                if comment==1:
                macros.append( line.rstrip().replace("//", "/*", 1) + " */" )
                macros.append( line.rstrip().replace("//", "/*", 1) + " */" )
            else:
            else:
 
                    macros.append( line.rstrip()[0:i] )
 
            else:
                macros.append(line.rstrip())
                macros.append(line.rstrip())
 
 
# List of errors / keys and macros that did not match. We stash them as we go
# List of errors / keys and macros that did not match. We stash them as we go
# and then print at the end so it is easier to find them
# and then print at the end so it is easier to find them
errors = []
errors = []
Line 68... Line 74...
        return ""
        return ""
    for l in macros:
    for l in macros:
        if multiline==True:
        if multiline==True:
            # Multiline copies lines until a char at [0] is not a space
            # Multiline copies lines until a char at [0] is not a space
            if len(l.strip())==0 or l[0]!=' ':
            if len(l.strip())==0 or l[0]!=' ':
                return '\n' + "\n".join(subst)
                return '\n' + "\n".join(subst).rstrip()
            else:
            else:
                subst.append(l)
                subst.append(l.rstrip())
        lx = l.split(' ')               # Split the string and then ignore (duplicate)
        lx = l.split(' ')               # Split the string and then ignore (duplicate)
        lx = filter(None, lx)           # spaces in the list left by the split()
        lx = list(filter(None, lx))     # spaces in the list left by the split()
        if l.startswith(":"):           # Find and recognize a matching set (key) section
        if l.startswith(":"):           # Find and recognize a matching set (key) section
            if validset:                # Error if there is a new section going from the macthing one
            if validset:                # Error if there is a new section going from the macthing one
                break                   # meaning we did not find our macro in there
                break                   # meaning we did not find our macro in there
            if l[1:]==key:
            if l[1:]==key:
                validset = True
                validset = True
Line 94... Line 100...
        errors.append(err)
        errors.append(err)
    return " --- {0} ?? {1} --- ".format(token, key)
    return " --- {0} ?? {1} --- ".format(token, key)
 
 
# Read the content of a file and using the csv reader and remove any quotes from the input fields
# Read the content of a file and using the csv reader and remove any quotes from the input fields
content = []                            # Content of the spreadsheet timing file
content = []                            # Content of the spreadsheet timing file
with open(fname, 'rb') as csvFile:
with open(fname, 'r') as csvFile:
    reader = csv.reader(csvFile, delimiter='\t', quotechar='"')
    reader = csv.reader(csvFile, delimiter='\t', quotechar='"')
    for row in reader:
    for row in reader:
        content.append('\t'.join(row))
        content.append('\t'.join(row))
 
 
# The first line is special: it contains names of sets for our macro substitutions
# The first line is special: it contains names of sets for our macro substitutions
Line 111... Line 117...
 
 
# Process each line separately (stateless processor)
# Process each line separately (stateless processor)
imatrix = []    # Verilog execution matrix code
imatrix = []    # Verilog execution matrix code
for line in content:
for line in content:
    col = line.split('\t')              # Split the string into a list of columns
    col = line.split('\t')              # Split the string into a list of columns
    col_clean = filter(None, col)       # Removed all empty fields (between the separators)
    col_clean = list(filter(None, col)) # Removed all empty fields (between the separators)
    if len(col_clean)==0:               # Ignore completely empty lines
    if len(col_clean)==0:               # Ignore completely empty lines
        continue
        continue
 
 
    if col_clean[0].startswith('//'):   # Print comment lines
    if col_clean[0].startswith('//') and comment==1:
        imatrix.append(col_clean[0])
        imatrix.append(col_clean[0])    # Optionally print comment lines
 
 
    if col_clean[0].startswith("#end"): # Print the end of a condition
    if col_clean[0].startswith("#end"): # Print the end of a condition
        imatrix.append("end\n")
        imatrix.append("end\n")
 
 
    if col_clean[0].startswith('#if'):  # Print the start of a condition
    if col_clean[0].startswith('#if'):  # Print the start of a condition
Line 138... Line 144...
        # M and T states are hard-coded in the table at the index 1 and 2
        # M and T states are hard-coded in the table at the index 1 and 2
        if col_clean[0].startswith('#0'):
        if col_clean[0].startswith('#0'):
            if col[1]=='?':     # M is optional, use '?' to skip it
            if col[1]=='?':     # M is optional, use '?' to skip it
                state = "    if (T{0}) begin ".format(col[2])
                state = "    if (T{0}) begin ".format(col[2])
            else:
            else:
                state = "    if (M{0} && T{1}) begin ".format(col[1], col[2])
                state = "    if (M{0} & T{1}) begin".format(col[1], col[2])
        else:
        else:
            state = "    begin "
            state = "    begin "
 
 
        # Loop over all other columns and perform verbatim substitution
        # Loop over all other columns and perform verbatim substitution
        action = ""
        action = ""
        for i in range(3,len(col)):
        for i in range(3,len(col)):
            # There may be multiple tokens separated by commas
            # There may be multiple tokens separated by commas
            tokList = col[i].strip().split(',')
            tokList = col[i].strip().split(',')
            tokList =  filter(None, tokList)   # Filter out empty lines
            tokList = list(filter(None, tokList)) # Filter out empty lines
            for token in tokList:
            for token in tokList:
                token = token.strip()
                token = token.strip()
                if i in tkeys and len(token)>0:
                if i in tkeys and len(token)>0:
                    macro = getSubst(tkeys[i], token)
                    macro = getSubst(tkeys[i], token)
                    if macro.strip().startswith("ctl_"):
                    if macro.strip().startswith("ctl_"):
                        action += ctl_prefix
                        action += ctl_prefix
                    action += macro
                    action += macro
                    if state.find("ERROR")>=0:
                    if state.find("ERROR")>=0:
                        print "{0} {1}".format(state, action)
                        print ("{0} {1}".format(state, action))
                        break
                        break
 
 
        # Complete and write out a line
        # Complete and write out a line
        if abbr and len(action)==0:
        if abbr and len(action)==0:
            continue
            continue
        imatrix.append("{0}{1} end".format(state, action))
        imatrix.append("{0}{1} end".format(state, action))
 
 
# Create a file containing the logic matrix code
# Create a file containing the logic matrix code
with open('exec_matrix.vh', 'w') as file:
with open('exec_matrix.vh', 'w') as file:
    file.write("// Automatically generated by genmatrix.py\n")
    if comment==1:
 
        file.write("// Automatically generated by genmatrix.py\n\n")
    # If there were errors, print them first (and output to the console)
    # If there were errors, print them first (and output to the console)
    if len(errors)>0:
    if len(errors)>0:
        for error in errors:
        for error in errors:
            print error
            print (error)
            file.write(error + "\n")
            file.write(error + "\n")
        file.write("-" * 80 + "\n")
        file.write("-" * 80 + "\n")
    for item in imatrix:
    for item in imatrix:
        file.write("{}\n".format(item))
        file.write("{}\n".format(item))
 
 
# Touch a file that includes 'exec_matrix.vh' to ensure it will recompile correctly
# Touch a file that includes 'exec_matrix.vh' to ensure it will recompile correctly
os.utime("execute.sv", None)
os.utime("execute.v", None)
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

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