Skip to content

Commit 243cefd

Browse files
committed
fix up config model
1 parent ee4b82c commit 243cefd

File tree

1 file changed

+7
-47
lines changed

1 file changed

+7
-47
lines changed

chipflow_lib/config_models.py

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,23 @@
11
# 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
43

5-
from pydantic import BaseModel, model_validator, ValidationInfo, field_validator
4+
from pydantic import BaseModel
65

7-
from .platforms.utils import Process
6+
from .platforms._internal import PACKAGE_DEFINITIONS, Process, Voltage
87

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
138

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()}")
2012

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
3913

4014
class SiliconConfig(BaseModel):
4115
"""Configuration for silicon in chipflow.toml."""
4216
process: 'Process'
43-
package: Literal["caravel", "cf20", "pga144"]
17+
package: str
4418
power: Dict[str, Voltage] = {}
4519
debug: Optional[Dict[str, bool]] = None
4620
# 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
6121

6222

6323
class ChipFlowConfig(BaseModel):

0 commit comments

Comments
 (0)