|
1 | 1 | # SPDX-License-Identifier: BSD-2-Clause |
2 | | -import re |
3 | | -from typing import Dict, Optional, Literal, Any, List |
| 2 | +from typing import Dict, Optional, Any, List |
4 | 3 |
|
5 | | -from pydantic import BaseModel, model_validator, ValidationInfo, field_validator |
| 4 | +from pydantic import BaseModel |
6 | 5 |
|
7 | | -from .platforms.utils import Process |
| 6 | +from .platforms._internal import PACKAGE_DEFINITIONS, Process, Voltage |
8 | 7 |
|
9 | | -class PadConfig(BaseModel): |
10 | | - """Configuration for a pad in chipflow.toml.""" |
11 | | - type: Literal["io", "i", "o", "oe", "clock", "reset", "power", "ground"] |
12 | | - loc: str |
13 | 8 |
|
14 | | - @model_validator(mode="after") |
15 | | - def validate_loc_format(self): |
16 | | - """Validate that the location is in the correct format.""" |
17 | | - if not re.match(r"^[NSWE]?[0-9]+$", self.loc): |
18 | | - raise ValueError(f"Invalid location format: {self.loc}, expected format: [NSWE]?[0-9]+") |
19 | | - return self |
| 9 | +def known_package(package: str): |
| 10 | + if package not in PACKAGE_DEFINITIONS.keys(): |
| 11 | + raise ValueError(f"{package} is not a valid package type. Valid package types are {PACKAGE_DEFINITIONS.keys()}") |
20 | 12 |
|
21 | | - @classmethod |
22 | | - def validate_pad_dict(cls, v: dict, info: ValidationInfo): |
23 | | - """Custom validation for pad dicts from TOML that may not have all fields.""" |
24 | | - if isinstance(v, dict): |
25 | | - # Handle legacy format - if 'type' is missing but should be inferred from context |
26 | | - if 'loc' in v and 'type' not in v: |
27 | | - if info.field_name == 'power': |
28 | | - v['type'] = 'power' |
29 | | - |
30 | | - # Map legacy 'clk' type to 'clock' to match our enum |
31 | | - if 'type' in v and v['type'] == 'clk': |
32 | | - v['type'] = 'clock' |
33 | | - |
34 | | - return v |
35 | | - return v |
36 | | - |
37 | | - |
38 | | -Voltage = float |
39 | 13 |
|
40 | 14 | class SiliconConfig(BaseModel): |
41 | 15 | """Configuration for silicon in chipflow.toml.""" |
42 | 16 | process: 'Process' |
43 | | - package: Literal["caravel", "cf20", "pga144"] |
| 17 | + package: str |
44 | 18 | power: Dict[str, Voltage] = {} |
45 | 19 | debug: Optional[Dict[str, bool]] = None |
46 | 20 | # This is still kept around to allow forcing pad locations. |
47 | | - pads: Optional[Dict[str, PadConfig]] = {} |
48 | | - |
49 | | - @field_validator('pads', 'power', mode='before') |
50 | | - @classmethod |
51 | | - def validate_pad_dicts(cls, v, info: ValidationInfo): |
52 | | - """Pre-process pad dictionaries to handle legacy format.""" |
53 | | - if isinstance(v, dict): |
54 | | - result = {} |
55 | | - for key, pad_dict in v.items(): |
56 | | - # Apply the pad validator with context about which field we're in |
57 | | - validated_pad = PadConfig.validate_pad_dict(pad_dict, info) |
58 | | - result[key] = validated_pad |
59 | | - return result |
60 | | - return v |
61 | 21 |
|
62 | 22 |
|
63 | 23 | class ChipFlowConfig(BaseModel): |
|
0 commit comments