build/lattice/diamond/clock_constraints: review and improve similarities with the others build backends.

This commit is contained in:
Florent Kermarrec 2020-05-11 10:50:25 +02:00
parent ebcf67c10f
commit 1e610600f6
1 changed files with 21 additions and 27 deletions

View File

@ -6,6 +6,7 @@
import os import os
import re import re
import sys import sys
import math
import subprocess import subprocess
import shutil import shutil
@ -41,28 +42,26 @@ def _format_lpf(signame, pin, others, resname):
return "\n".join(lpf) return "\n".join(lpf)
def _build_lpf(named_sc, named_pc, cc, build_name): def _build_lpf(named_sc, named_pc, clocks, vns, build_name):
lpf = [] lpf = []
lpf.append("BLOCK RESETPATHS;") lpf.append("BLOCK RESETPATHS;")
lpf.append("BLOCK ASYNCPATHS;") lpf.append("BLOCK ASYNCPATHS;")
clk_ports = set()
for sig, pins, others, resname in named_sc: for sig, pins, others, resname in named_sc:
if len(pins) > 1: if len(pins) > 1:
for i, p in enumerate(pins): for i, p in enumerate(pins):
lpf.append(_format_lpf(sig + "[" + str(i) + "]", p, others, resname)) lpf.append(_format_lpf(sig + "[" + str(i) + "]", p, others, resname))
else: else:
lpf.append(_format_lpf(sig, pins[0], others, resname)) lpf.append(_format_lpf(sig, pins[0], others, resname))
if sig in cc.keys():
clk_ports.add(sig)
if named_pc: if named_pc:
lpf.append("\n".join(named_pc)) lpf.append("\n".join(named_pc))
# NOTE: The lpf is only used post-synthesis. Currently Synplify is fed no constraints, # Note: .lpf is only used post-synthesis, Synplify constraints clocks by default to 200MHz.
# so defaults to inferring clocks and trying to hit 200MHz on all of them. for clk, period in clocks.items():
# NOTE: For additional options (PAR_ADJ, etc), use add_platform_command clk_name = vns.get_name(clk)
for clk, freq in cc.items(): lpf.append("FREQUENCY {} \"{}\" {} MHz;".format(
sig_type = "PORT" if clk in clk_ports else "NET" "PORT" if clk_name in [name for name, _, _, _ in named_sc] else "NET",
lpf.append("FREQUENCY {} \"{}\" {} MHz;".format(sig_type, clk, freq)) clk_name,
str(1e3/period)))
tools.write_to_file(build_name + ".lpf", "\n".join(lpf)) tools.write_to_file(build_name + ".lpf", "\n".join(lpf))
@ -104,7 +103,7 @@ def _build_tcl(device, sources, vincpaths, build_name):
if _produces_jedec(device): if _produces_jedec(device):
tcl.append("prj_run Export -impl impl -task Jedecgen") tcl.append("prj_run Export -impl impl -task Jedecgen")
# Cleanly close the project # Close project
tcl.append("prj_project close") tcl.append("prj_project close")
tools.write_to_file(build_name + ".tcl", "\n".join(tcl)) tools.write_to_file(build_name + ".tcl", "\n".join(tcl))
@ -148,7 +147,6 @@ def _run_script(script):
if subprocess.call(shell + [script]) != 0: if subprocess.call(shell + [script]) != 0:
raise OSError("Subprocess failed") raise OSError("Subprocess failed")
# This operates the same way tmcheck does, but tmcheck isn't usable without gui
def _check_timing(build_name): def _check_timing(build_name):
lines = open("impl/{}_impl.par".format(build_name), "r").readlines() lines = open("impl/{}_impl.par".format(build_name), "r").readlines()
runs = [None, None] runs = [None, None]
@ -167,12 +165,12 @@ def _check_timing(build_name):
limit = 1e-8 limit = 1e-8
setup = m.group(2) setup = m.group(2)
hold = m.group(4) hold = m.group(4)
# if there were no freq constraints in lpf, ratings will be dashed. # If there were no freq constraints in lpf, ratings will be dashed.
# results will likely be terribly unreliable, so bail # results will likely be terribly unreliable, so bail
assert not setup == hold == "-", "No timing constraints were provided" assert not setup == hold == "-", "No timing constraints were provided"
setup, hold = map(float, (setup, hold)) setup, hold = map(float, (setup, hold))
if setup > limit and hold > limit: if setup > limit and hold > limit:
# at least one run met timing # At least one run met timing
# XXX is this necessarily the run from which outputs will be used? # XXX is this necessarily the run from which outputs will be used?
return return
raise Exception("Failed to meet timing") raise Exception("Failed to meet timing")
@ -196,7 +194,7 @@ class LatticeDiamondToolchain:
special_overrides = common.lattice_ecp5_special_overrides special_overrides = common.lattice_ecp5_special_overrides
def __init__(self): def __init__(self):
self.period_constraints = [] self.clocks = {}
self.false_paths = set() # FIXME: use it self.false_paths = set() # FIXME: use it
def build(self, platform, fragment, def build(self, platform, fragment,
@ -223,15 +221,8 @@ class LatticeDiamondToolchain:
v_output.write(v_file) v_output.write(v_file)
platform.add_source(v_file) platform.add_source(v_file)
cc = {}
for (clk, freq) in self.period_constraints:
clk_name = v_output.ns.get_name(clk)
if clk_name in cc and cc[clk_name] != freq:
raise ConstraintError("Differing period constraints on {}".format(clk_name))
cc[clk_name] = freq
# Generate design constraints file (.lpf) # Generate design constraints file (.lpf)
_build_lpf(named_sc, named_pc, cc, build_name) _build_lpf(named_sc, named_pc, self.clocks, v_output.ns, build_name)
# Generate design script file (.tcl) # Generate design script file (.tcl)
_build_tcl(platform.device, platform.sources, platform.verilog_include_paths, build_name) _build_tcl(platform.device, platform.sources, platform.verilog_include_paths, build_name)
@ -250,10 +241,13 @@ class LatticeDiamondToolchain:
return v_output.ns return v_output.ns
def add_period_constraint(self, platform, clk, period): def add_period_constraint(self, platform, clk, period):
# TODO: handle differential clk
clk.attr.add("keep") clk.attr.add("keep")
freq = str(float(1/period)*1000) period = math.floor(period*1e3)/1e3 # round to lowest picosecond
self.period_constraints.append((clk, freq)) if clk in self.clocks:
if period != self.clocks[clk]:
raise ValueError("Clock already constrained to {:.2f}ns, new constraint to {:.2f}ns"
.format(self.clocks[clk], period))
self.clocks[clk] = period
def add_false_path_constraint(self, platform, from_, to): def add_false_path_constraint(self, platform, from_, to):
from_.attr.add("keep") from_.attr.add("keep")