#!/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.
|
#
|
#
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
# more details.
|
# more details.
|
#-------------------------------------------------------------------------------
|
#-------------------------------------------------------------------------------
|
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.
|
ctl_prefix = "\n"+" "*19
|
ctl_prefix = "\n"+" "*19
|
|
|
# Read in the content of the macro substitution file
|
# Read in the content of the macro substitution file
|
macros = []
|
macros = []
|
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 = []
|
|
|
# Returns a substitution string given the section name (key) and the macro token
|
# Returns a substitution string given the section name (key) and the macro token
|
# This is done by simply traversing macro substitution list of lines, finding a
|
# This is done by simply traversing macro substitution list of lines, finding a
|
# section that starts with a :key and copying the substitution lines verbatim.
|
# section that starts with a :key and copying the substitution lines verbatim.
|
def getSubst(key, token):
|
def getSubst(key, token):
|
subst = []
|
subst = []
|
multiline = False
|
multiline = False
|
validset = False
|
validset = False
|
if key=="Comments": # Special case: ignore "Comments" column!
|
if key=="Comments": # Special case: ignore "Comments" column!
|
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
|
elif validset and lx[0]==token:
|
elif validset and lx[0]==token:
|
if len(lx)==1:
|
if len(lx)==1:
|
return ""
|
return ""
|
if lx[1]=='\\': # Multi-line macro state starts with '\' character
|
if lx[1]=='\\': # Multi-line macro state starts with '\' character
|
multiline = True
|
multiline = True
|
continue
|
continue
|
lx.pop(0)
|
lx.pop(0)
|
s = " ".join(lx)
|
s = " ".join(lx)
|
return ' ' + s.strip()
|
return ' ' + s.strip()
|
err = "{0} not in {1}".format(token, key)
|
err = "{0} not in {1}".format(token, key)
|
if err not in errors:
|
if err not in errors:
|
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
|
tkeys = {} # Spreadsheet table column keys
|
tkeys = {} # Spreadsheet table column keys
|
tokens = content.pop(0).split('\t')
|
tokens = content.pop(0).split('\t')
|
for col in range(len(tokens)):
|
for col in range(len(tokens)):
|
if len(tokens[col])==0:
|
if len(tokens[col])==0:
|
continue
|
continue
|
tkeys[col] = tokens[col]
|
tkeys[col] = tokens[col]
|
|
|
# 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
|
s = col_clean[0]
|
s = col_clean[0]
|
tag = s.find(":")
|
tag = s.find(":")
|
condition = s[4:tag]
|
condition = s[4:tag]
|
imatrix.append("if ({0}) begin".format(condition.strip()))
|
imatrix.append("if ({0}) begin".format(condition.strip()))
|
if debug and len(s[tag:])>1: # Print only in debug and there is something to print
|
if debug and len(s[tag:])>1: # Print only in debug and there is something to print
|
imatrix.append(" $display(\"{0}\");".format(s[4:]))
|
imatrix.append(" $display(\"{0}\");".format(s[4:]))
|
|
|
# We recognize 2 kinds of timing statements based on the starting characters:
|
# We recognize 2 kinds of timing statements based on the starting characters:
|
# "#0".. common timings using M and T cycles (M being optional)
|
# "#0".. common timings using M and T cycles (M being optional)
|
# "#always" timing that does not depend on M and T cycles (ex. ALU operations)
|
# "#always" timing that does not depend on M and T cycles (ex. ALU operations)
|
if col_clean[0].startswith('#0') or col_clean[0].startswith('#always'):
|
if col_clean[0].startswith('#0') or col_clean[0].startswith('#always'):
|
# 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
|