2020-11-17 22:44:13 -06:00
|
|
|
#####################################################################
|
2020-11-19 18:14:50 -06:00
|
|
|
# Python script generate Verilog codes for the Caravel wrapper
|
|
|
|
# which interface the FPGA fabric and other SoC components
|
2020-11-17 22:44:13 -06:00
|
|
|
# This script will
|
2020-11-19 18:14:50 -06:00
|
|
|
# - generate the Verilog codes to connect FPGA inputs to Wishbone and Logic analyzer
|
|
|
|
# - generate the Verilog codes to connect FPGA outputs to Wishbone and Logic analyzer
|
2020-11-17 22:44:13 -06:00
|
|
|
#####################################################################
|
|
|
|
|
|
|
|
import os
|
2020-11-19 00:15:26 -06:00
|
|
|
from os.path import dirname, abspath, isfile
|
2020-11-17 22:44:13 -06:00
|
|
|
import shutil
|
|
|
|
import re
|
|
|
|
import argparse
|
|
|
|
import logging
|
2020-11-29 11:35:10 -06:00
|
|
|
import json
|
2020-11-17 22:44:13 -06:00
|
|
|
|
|
|
|
#####################################################################
|
2020-11-19 00:15:26 -06:00
|
|
|
# Initialize logger
|
2020-11-17 22:44:13 -06:00
|
|
|
#####################################################################
|
2020-11-19 00:15:26 -06:00
|
|
|
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.DEBUG)
|
2020-11-17 22:44:13 -06:00
|
|
|
|
|
|
|
#####################################################################
|
|
|
|
# Parse the options
|
|
|
|
# - OpenFPGA root path is a manadatory option
|
|
|
|
#####################################################################
|
2020-11-19 00:15:26 -06:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description='Generator for technology-mapped wrapper')
|
|
|
|
parser.add_argument('--template_netlist', default='caravel_fpga_wrapper_hd_template.v',
|
|
|
|
help='Specify template verilog netlist')
|
2020-11-29 11:35:10 -06:00
|
|
|
parser.add_argument('--pin_assignment_file', required=True,
|
|
|
|
help='Specify the json file constaining pin assignment information')
|
2020-11-19 00:15:26 -06:00
|
|
|
parser.add_argument('--output_verilog', default='caravel_fpga_wrapper_hd.v',
|
|
|
|
help='Specify output verilog file path')
|
|
|
|
args = parser.parse_args()
|
2020-11-17 22:44:13 -06:00
|
|
|
|
2020-11-18 21:44:54 -06:00
|
|
|
#####################################################################
|
2020-11-29 11:35:10 -06:00
|
|
|
# Check options:
|
|
|
|
# - Input json file must be valid
|
|
|
|
# Otherwise, error out
|
2020-11-18 21:44:54 -06:00
|
|
|
#####################################################################
|
2020-11-29 11:35:10 -06:00
|
|
|
if not isfile(args.pin_assignment_file):
|
|
|
|
logging.error("Invalid pin assignment file: " + args.pin_assignment_file + "\nFile does not exist!\n")
|
|
|
|
exit(1)
|
2020-11-19 00:15:26 -06:00
|
|
|
|
2020-11-29 11:35:10 -06:00
|
|
|
#####################################################################
|
|
|
|
# Parse the json file
|
|
|
|
#####################################################################
|
|
|
|
json_file = open(args.pin_assignment_file, "r")
|
|
|
|
pin_data = json.load(json_file)
|
2020-11-18 21:44:54 -06:00
|
|
|
|
|
|
|
#####################################################################
|
2020-11-29 11:35:10 -06:00
|
|
|
# A function to parse pin range from json data
|
|
|
|
# JSON pin range format is LSB:MSB
|
|
|
|
# Return pin range format is [LSB, MSB] as a list
|
2020-11-18 21:44:54 -06:00
|
|
|
#####################################################################
|
2020-11-29 11:35:10 -06:00
|
|
|
def parse_json_pin_range(json_range) :
|
2020-11-29 12:57:34 -06:00
|
|
|
pin_range_str = json_range.split(':')
|
|
|
|
assert(2 == len(pin_range_str))
|
2020-11-29 13:31:32 -06:00
|
|
|
# If the range is in decend order, we will decrease the MSB by 1
|
|
|
|
if (int(pin_range_str[0]) > int(pin_range_str[1])) :
|
|
|
|
return range(int(pin_range_str[0]), int(pin_range_str[1]) - 1, -1)
|
|
|
|
# If the range is in acend order, we will increase the MSB by 1
|
2020-11-29 13:01:15 -06:00
|
|
|
return range(int(pin_range_str[0]), int(pin_range_str[1]) + 1)
|
2020-11-18 21:44:54 -06:00
|
|
|
|
2020-11-17 22:44:13 -06:00
|
|
|
#####################################################################
|
|
|
|
# Generate wrapper lines
|
|
|
|
#####################################################################
|
2020-11-19 00:15:26 -06:00
|
|
|
netlist_lines = []
|
2020-11-18 12:29:37 -06:00
|
|
|
|
2020-11-29 11:35:10 -06:00
|
|
|
# Walk through the array containing the pin information
|
|
|
|
for pin_info in pin_data['pins']:
|
|
|
|
# Deposit a tab to respect the HDL coding indent
|
2020-11-29 12:57:34 -06:00
|
|
|
curr_line = ""
|
2020-11-29 11:35:10 -06:00
|
|
|
# TODO: Check codes that ensure the pin index should match
|
2020-11-29 12:57:34 -06:00
|
|
|
assert(0 < len(pin_info['caravel_pin_type']))
|
|
|
|
assert(0 < len(pin_info['caravel_pin_index']))
|
2020-11-29 11:35:10 -06:00
|
|
|
#
|
|
|
|
# Branch on the types of connnections:
|
|
|
|
# - FPGA I/O to Caravel GPIO
|
2020-11-29 12:57:34 -06:00
|
|
|
if (("io" == pin_info['fpga_pin_type']) \
|
|
|
|
and (1 == len(pin_info['caravel_pin_type'])) \
|
|
|
|
and ("gpio" == pin_info['caravel_pin_type'][0])):
|
2020-11-29 11:35:10 -06:00
|
|
|
# Should have only 1 port in caravel
|
|
|
|
assert(1 == len(pin_info['caravel_pin_type']))
|
2020-11-29 12:57:34 -06:00
|
|
|
assert(1 == len(pin_info['caravel_pin_index']))
|
2020-11-29 11:35:10 -06:00
|
|
|
# Get pin range
|
|
|
|
fpga_io_pin_range = parse_json_pin_range(pin_info['fpga_pin_index'])
|
2020-11-29 12:57:34 -06:00
|
|
|
caravel_io_pin_range = parse_json_pin_range(pin_info['caravel_pin_index'][0])
|
|
|
|
assert(len(list(fpga_io_pin_range)) == len(list(caravel_io_pin_range)))
|
|
|
|
for indices in zip(list(fpga_io_pin_range), list(caravel_io_pin_range)) :
|
|
|
|
# Connect all the input, output and direction port
|
|
|
|
# FPGA input <- Caravel input
|
2020-11-29 13:31:32 -06:00
|
|
|
curr_line = "assign " + pin_data['fpga_gpio_input_name'] + "[" + str(indices[0]) + "] = " \
|
|
|
|
+ pin_data['caravel_gpio_input_name'] + "[" + str(indices[1]) + "];";
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-29 12:57:34 -06:00
|
|
|
# FPGA output -> Caravel output
|
2020-11-29 13:31:32 -06:00
|
|
|
curr_line = "assign " + pin_data['caravel_gpio_output_name'] + "[" + str(indices[1]) + "] = " \
|
|
|
|
+ pin_data['fpga_gpio_output_name'] + "[" + str(indices[0]) + "];";
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-29 12:57:34 -06:00
|
|
|
# FPGA direction -> Caravel direction
|
2020-11-29 13:31:32 -06:00
|
|
|
curr_line = "assign " + pin_data['caravel_gpio_direction_name'] + "[" + str(indices[1]) + "] = " \
|
|
|
|
+ pin_data['fpga_gpio_direction_name'] + "[" + str(indices[0]) + "];";
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-19 18:14:50 -06:00
|
|
|
|
2020-11-29 12:57:34 -06:00
|
|
|
# - FPGA control input ports to Caravel GPIO
|
|
|
|
if (("io" != pin_info['fpga_pin_type']) \
|
|
|
|
and (1 == len(pin_info['caravel_pin_type'])) \
|
|
|
|
and ("input" == pin_info['caravel_pin_type'][0])):
|
|
|
|
# Should have only 1 port in caravel
|
|
|
|
assert(1 == len(pin_info['caravel_pin_type']))
|
|
|
|
assert(1 == len(pin_info['caravel_pin_index']))
|
|
|
|
# Get pin range
|
|
|
|
fpga_io_pin_range = parse_json_pin_range(pin_info['fpga_pin_index'])
|
|
|
|
caravel_io_pin_range = parse_json_pin_range(pin_info['caravel_pin_index'][0])
|
|
|
|
assert(len(list(fpga_io_pin_range)) == len(list(caravel_io_pin_range)))
|
|
|
|
for indices in zip(list(fpga_io_pin_range), list(caravel_io_pin_range)) :
|
|
|
|
# Connect the FPGA input port to the Caravel input
|
2020-11-29 13:31:32 -06:00
|
|
|
curr_line = "assign " + pin_info['fpga_pin_type'] + "[" + str(indices[0]) + "] = " \
|
|
|
|
+ pin_data['caravel_gpio_input_name'] + "[" + str(indices[1]) + "];";
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-29 12:57:34 -06:00
|
|
|
# Tie Caravel output port to logic '0'
|
2020-11-29 13:31:32 -06:00
|
|
|
curr_line = "assign " + pin_data['caravel_gpio_output_name'] + "[" + str(indices[1]) + "] = 1'b0;"
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-29 12:57:34 -06:00
|
|
|
# Tie Caravel direction port to logic '1'
|
2020-11-29 13:39:56 -06:00
|
|
|
curr_line = "assign " + pin_data['caravel_gpio_direction_name'] + "[" + str(indices[1]) + "] = 1'b1;"
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-29 12:57:34 -06:00
|
|
|
|
|
|
|
# - FPGA control output ports to Caravel GPIO
|
|
|
|
if (("io" != pin_info['fpga_pin_type']) \
|
|
|
|
and (1 == len(pin_info['caravel_pin_type'])) \
|
|
|
|
and ("output" == pin_info['caravel_pin_type'][0])):
|
|
|
|
# Should have only 1 port in caravel
|
|
|
|
assert(1 == len(pin_info['caravel_pin_type']))
|
|
|
|
assert(1 == len(pin_info['caravel_pin_index']))
|
|
|
|
# Get pin range
|
|
|
|
fpga_io_pin_range = parse_json_pin_range(pin_info['fpga_pin_index'])
|
|
|
|
caravel_io_pin_range = parse_json_pin_range(pin_info['caravel_pin_index'][0])
|
|
|
|
assert(len(list(fpga_io_pin_range)) == len(list(caravel_io_pin_range)))
|
|
|
|
for indices in zip(list(fpga_io_pin_range), list(caravel_io_pin_range)) :
|
|
|
|
# Bypass the Caravel input
|
|
|
|
# Connect Caravel output port to FPGA control output
|
2020-11-29 13:31:32 -06:00
|
|
|
curr_line = "assign " + pin_data['caravel_gpio_output_name'] + "[" + str(indices[1]) + "] = " \
|
|
|
|
+ pin_info['fpga_pin_type'] + "[" + str(indices[0]) + "];";
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-29 12:57:34 -06:00
|
|
|
# Tie Caravel direction port to logic '0'
|
2020-11-29 13:39:56 -06:00
|
|
|
curr_line = "assign " + pin_data['caravel_gpio_direction_name'] + "[" + str(indices[1]) + "] = 1'b0;"
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-29 12:57:34 -06:00
|
|
|
|
|
|
|
# - FPGA I/O ports to Caravel logic analyzer I/O only
|
|
|
|
if (("io" == pin_info['fpga_pin_type']) \
|
|
|
|
and (1 == len(pin_info['caravel_pin_type'])) \
|
|
|
|
and ("logic_analyzer_io" == pin_info['caravel_pin_type'][0])):
|
|
|
|
# Should have only 1 port in caravel
|
|
|
|
assert(1 == len(pin_info['caravel_pin_type']))
|
|
|
|
assert(1 == len(pin_info['caravel_pin_index']))
|
|
|
|
# Get pin range
|
|
|
|
fpga_io_pin_range = parse_json_pin_range(pin_info['fpga_pin_index'])
|
|
|
|
caravel_io_pin_range = parse_json_pin_range(pin_info['caravel_pin_index'][0])
|
|
|
|
assert(len(list(fpga_io_pin_range)) == len(list(caravel_io_pin_range)))
|
|
|
|
for indices in zip(list(fpga_io_pin_range), list(caravel_io_pin_range)) :
|
|
|
|
##############################################################
|
|
|
|
# SOC INPUT will be directly driven by logic analyzer
|
|
|
|
# since this I/O is going to interface logic analyzer input only
|
2020-11-29 13:31:32 -06:00
|
|
|
curr_line = "assign " + pin_data['fpga_gpio_input_name'] + "[" + str(indices[0]) + "] = " \
|
|
|
|
+ pin_data['caravel_logic_analyzer_input_name'] + "[" + str(indices[1]) + "]" + ";"
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-29 12:57:34 -06:00
|
|
|
##############################################################
|
|
|
|
# SOC OUTPUT will directly drive logic analyzer
|
|
|
|
# since this I/O is going to interface logic analyzer output only
|
2020-11-29 13:31:32 -06:00
|
|
|
curr_line = "assign " + pin_data['caravel_logic_analyzer_output_name'] + "[" + str(indices[1]) + "]" \
|
|
|
|
+ " = " + pin_data['fpga_gpio_output_name'] + "[" + str(indices[0]) + "];"
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-29 12:57:34 -06:00
|
|
|
|
|
|
|
# - FPGA I/O ports to Caravel logic analyzer I/O and Wishbone interface
|
|
|
|
if (("io" == pin_info['fpga_pin_type']) \
|
|
|
|
and (2 == len(pin_info['caravel_pin_type'])) \
|
|
|
|
and ("logic_analyzer_io" == pin_info['caravel_pin_type'][0]) \
|
|
|
|
and (pin_info['caravel_pin_type'][1].startswith("wishbone"))):
|
|
|
|
# Should have only 2 port in caravel
|
|
|
|
assert(2 == len(pin_info['caravel_pin_type']))
|
|
|
|
assert(2 == len(pin_info['caravel_pin_index']))
|
|
|
|
# Get pin range
|
|
|
|
fpga_io_pin_range = parse_json_pin_range(pin_info['fpga_pin_index'])
|
|
|
|
la_io_pin_range = parse_json_pin_range(pin_info['caravel_pin_index'][0])
|
|
|
|
wb_io_pin_range = parse_json_pin_range(pin_info['caravel_pin_index'][1])
|
|
|
|
assert(len(list(fpga_io_pin_range)) == len(list(la_io_pin_range)))
|
|
|
|
assert(len(list(fpga_io_pin_range)) == len(list(wb_io_pin_range)))
|
|
|
|
|
|
|
|
# If this is an input pin of wishbone interface, whose postfix is '_i', we use MUX
|
|
|
|
# otherwise, this is an output pin, we just wire the input to logic analyzer
|
|
|
|
if (pin_info['caravel_pin_type'][1].endswith("_input")):
|
2020-11-29 13:31:32 -06:00
|
|
|
for indices in zip(list(fpga_io_pin_range), list(la_io_pin_range), list(wb_io_pin_range)) :
|
2020-11-29 12:57:34 -06:00
|
|
|
##############################################################
|
|
|
|
# SOC INPUT will be directly driven by either
|
|
|
|
# - the Wishbone input
|
|
|
|
# or
|
|
|
|
# - the logic analyzer input
|
|
|
|
# through a multiplexer controlled by the signal 'wb_la_switch
|
2020-11-29 13:31:32 -06:00
|
|
|
curr_line = "sky130_fd_sc_hd__mux2_1 FPGA2SOC_IN_" + str(indices[0]) + "_MUX (" \
|
2020-11-29 12:57:34 -06:00
|
|
|
+ ".S(" + pin_data['mode_switch_pin_name'] + "), " \
|
2020-11-29 13:31:32 -06:00
|
|
|
+ ".A1(" + pin_data['caravel_' + pin_info['caravel_pin_type'][1] + '_name'] + "[" + str(indices[2]) + "]), " \
|
2020-11-29 13:47:22 -06:00
|
|
|
+ ".A0(" + pin_data['caravel_logic_analyzer_input_name'] + "[" + str(indices[1]) + "]), " \
|
2020-11-29 13:31:32 -06:00
|
|
|
+ ".X(" + pin_data['fpga_gpio_input_name'] + "[" + str(indices[0]) + "])" \
|
2020-11-29 12:57:34 -06:00
|
|
|
+ ");"
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-29 12:57:34 -06:00
|
|
|
##############################################################
|
|
|
|
# SOC OUTPUT will drive an output of logic analyzer
|
|
|
|
# since this I/O is going to interface a Wishbone input only
|
2020-11-29 13:31:32 -06:00
|
|
|
curr_line = "assign " + pin_data['caravel_logic_analyzer_output_name'] + "[" + str(indices[1]) + "]" \
|
|
|
|
+ " = " + pin_data['fpga_gpio_output_name'] + "[" + str(indices[0]) + "];"
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-29 12:57:34 -06:00
|
|
|
elif (pin_info['caravel_pin_type'][1].endswith("_output")):
|
2020-11-29 13:31:32 -06:00
|
|
|
for indices in zip(list(fpga_io_pin_range), list(la_io_pin_range), list(wb_io_pin_range)) :
|
2020-11-19 18:14:50 -06:00
|
|
|
##############################################################
|
|
|
|
# SOC INPUT will be directly driven by logic analyzer
|
2020-11-29 12:57:34 -06:00
|
|
|
# since this I/O is going to interface a Wishbone output only
|
2020-11-29 13:31:32 -06:00
|
|
|
curr_line = "assign " + pin_data['fpga_gpio_input_name'] + "[" + str(indices[0]) + "] = " \
|
|
|
|
+ pin_data['caravel_logic_analyzer_input_name'] + "[" + str(indices[1]) + "];"
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-29 12:57:34 -06:00
|
|
|
##############################################################
|
|
|
|
# SOC OUTPUT will drive the Wishbone output through a tri-state buffer
|
|
|
|
# As the buffer is enabled by logic '0', we use the inverted 'wb_la_switch'
|
2020-11-29 13:31:32 -06:00
|
|
|
curr_line = "sky130_fd_sc_hd__ebufn_4 FPGA2SOC_OUT_" + str(indices[0]) + "_DEMUX_WB (" \
|
2020-11-29 12:57:34 -06:00
|
|
|
+ ".TE_B(" + pin_data['inverted_mode_switch_pin_name'] + "), " \
|
2020-11-29 13:31:32 -06:00
|
|
|
+ ".A(" + pin_data['fpga_gpio_output_name'] + "[" + str(indices[0]) + "]), " \
|
|
|
|
+ ".Z(" + pin_data['caravel_' + pin_info['caravel_pin_type'][1] + '_name'] + "[" + str(indices[2]) + "])" \
|
2020-11-29 12:57:34 -06:00
|
|
|
+ ");"
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-19 18:14:50 -06:00
|
|
|
##############################################################
|
2020-11-29 12:57:34 -06:00
|
|
|
# SOC OUTPUT will also drive the Logic Analyzer output through a tri-state buffer
|
|
|
|
# As the buffer is enabled by logic '0', we use the 'wb_la_switch'
|
2020-11-29 13:31:32 -06:00
|
|
|
curr_line = "sky130_fd_sc_hd__ebufn_4 FPGA2SOC_OUT_" + str(indices[0]) + "_DEMUX_LA (" \
|
2020-11-29 12:57:34 -06:00
|
|
|
+ ".TE_B(" + pin_data['mode_switch_pin_name'] + "), " \
|
2020-11-29 13:31:32 -06:00
|
|
|
+ ".A(" + pin_data['fpga_gpio_output_name'] + "[" + str(indices[0]) + "]), " \
|
|
|
|
+ ".Z(" + pin_data['caravel_logic_analyzer_output_name'] + "[" + str(indices[1]) + "])" \
|
2020-11-29 12:57:34 -06:00
|
|
|
+ ");"
|
2020-11-29 13:35:24 -06:00
|
|
|
netlist_lines.append(" " + curr_line + "\n")
|
2020-11-17 22:44:13 -06:00
|
|
|
|
2020-11-19 00:15:26 -06:00
|
|
|
if isfile(args.output_verilog):
|
|
|
|
os.remove(args.output_verilog)
|
2020-11-17 22:44:13 -06:00
|
|
|
|
2020-11-19 00:15:26 -06:00
|
|
|
with open(args.template_netlist, "r") as wp:
|
|
|
|
template_netlist = wp.readlines()
|
|
|
|
for line_num, eachline in enumerate(template_netlist):
|
|
|
|
if "Autogenerate code start" in eachline:
|
|
|
|
logging.info(f"Inserting on line {line_num}")
|
|
|
|
template_netlist[line_num+1:line_num+1] = netlist_lines
|
|
|
|
logging.info("Outputting HDL codes to " +
|
|
|
|
str(args.output_verilog) + " ...")
|
|
|
|
vlog_file = open(args.output_verilog, "w")
|
|
|
|
vlog_file.write("".join(template_netlist))
|
|
|
|
vlog_file.close()
|
|
|
|
break
|