Coverage for tsfpga/constraint.py: 92%
24 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-01 20:51 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-01 20:51 +0000
1# --------------------------------------------------------------------------------------------------
2# Copyright (c) Lukas Vik. All rights reserved.
3#
4# This file is part of the tsfpga project, a project platform for modern FPGA development.
5# https://tsfpga.com
6# https://github.com/tsfpga/tsfpga
7# --------------------------------------------------------------------------------------------------
9from __future__ import annotations
11from typing import TYPE_CHECKING, Literal
13if TYPE_CHECKING:
14 from pathlib import Path
16 from .hdl_file import HdlFile
19class Constraint:
20 """
21 Class for handling a constraint file.
23 Can handle the regular global constraint files as well as scoped constraints.
24 For the latter to work the constraint file name must be the same as the .vhd file name,
25 which must be the same as the entity name.
26 """
28 def __init__(
29 self,
30 file: Path,
31 used_in_synthesis: bool = True,
32 used_in_implementation: bool = True,
33 used_in: Literal["all", "synth", "impl"] | None = None,
34 scoped_constraint: bool = False,
35 processing_order: Literal["early", "normal", "late"] = "normal",
36 ) -> None:
37 """
38 Arguments:
39 file: Path to the constraint file. Typically ends in .xdc or .tcl.
40 used_in_synthesis: Optionally disable the constraint for synthesis.
41 used_in_implementation: Optionally disable the constraint for implementation.
42 used_in: Old way of controlling when the constraint is applied.
44 .. deprecated:: 13.1.3
45 Use the `used_in_synthesis` and `used_in_implementation` arguments instead.
46 scoped_constraint: If enabled the constraint file will be loaded with the "-ref"
47 argument in Vivado. An entity with the same name must exist.
48 processing_order: Optionally the processing order can be changed to "early" or "late".
49 """
50 self.file = file
51 self.ref = file.stem if scoped_constraint else None
52 self.processing_order = processing_order.lower()
54 if used_in is not None:
55 print(
56 f"DEPRECATED: {self.__class__.__name__}.__init__() argument 'used_in' is "
57 "deprecated and will be removed."
58 )
59 if (not used_in_synthesis) or (not used_in_implementation):
60 raise ValueError("Using both 'used_in_*' and deprecated 'used_in' arguments.")
62 self.used_in_synthesis = used_in in ("all", "synth")
63 self.used_in_implementation = used_in in ("all", "impl")
64 else:
65 self.used_in_synthesis = used_in_synthesis
66 self.used_in_implementation = used_in_implementation
68 def validate_scoped_entity(self, source_files: list[HdlFile]) -> bool:
69 """
70 Make sure that a matching entity file exists in case this is a scoped constraint.
71 The list of source files should be the synthesis files for the module that this
72 constraint belongs to.
73 """
74 if self.ref is not None and not any(
75 [source_file.path.stem == self.ref] for source_file in source_files
76 ):
77 raise FileNotFoundError(
78 f"Could not find a matching entity file for scoped constraint file {self.file}"
79 )
81 return True
83 def __str__(self) -> str:
84 return str(self.file)