Coverage for tsfpga/constraint.py: 95%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.7, created at 2024-11-20 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# -------------------------------------------------------------------------------------------------- 

8 

9# Standard libraries 

10from pathlib import Path 

11from typing import TYPE_CHECKING 

12 

13if TYPE_CHECKING: 

14 # Local folder libraries 

15 from .hdl_file import HdlFile 

16 

17 

18class Constraint: 

19 """ 

20 Class for handling a constraint file. 

21 

22 Can handle the regular global constraint files as well as scoped constraints. 

23 For the latter to work the constraint file name must be the same as the .vhd file name, 

24 which must be the same as the entity name. 

25 """ 

26 

27 def __init__( 

28 self, 

29 file: Path, 

30 used_in: str = "all", 

31 scoped_constraint: bool = False, 

32 processing_order: str = "normal", 

33 ) -> None: 

34 """ 

35 Arguments: 

36 file: Path to the constraint file. Typically ends in .xdc or .tcl. 

37 used_in: Optionally the constraint can be enabled only for "synth" or "impl". 

38 scoped_constraint: If enabled the constraint file will be loaded with the "-ref" 

39 argument in Vivado. An entity with the same name must exist. 

40 processing_order: Optionally the processing order can be changed to "early" or "late". 

41 """ 

42 self.file = file 

43 self.used_in = used_in 

44 self.ref = file.stem if scoped_constraint else None 

45 self.processing_order = processing_order.lower() 

46 

47 assert self.used_in in ["all", "synth", "impl"], self.used_in 

48 assert self.processing_order in ["early", "normal", "late"], self.processing_order 

49 

50 def validate_scoped_entity(self, source_files: list["HdlFile"]) -> bool: 

51 """ 

52 Make sure that a matching entity file exists in case this is a scoped constraint. 

53 The list of source files should be the synthesis files for the module that this 

54 constraint belongs to. 

55 """ 

56 if self.ref is not None: 

57 if not any([source_file.path.stem == self.ref] for source_file in source_files): 

58 raise FileNotFoundError( 

59 f"Could not find a matching entity file for scoped constraint file {self.file}" 

60 ) 

61 return True 

62 

63 def __str__(self) -> str: 

64 return str(self.file)