|
1 | 1 | # SPDX-License-Identifier: BSD-2-Clause
|
2 | 2 |
|
3 | 3 | import re
|
4 |
| -from typing import List, Tuple, Any |
5 |
| -from typing_extensions import Unpack, TypedDict |
| 4 | +import sys |
| 5 | + |
| 6 | +from dataclasses import dataclass |
| 7 | +from pathlib import Path |
| 8 | +from typing import ( |
| 9 | + List, Tuple, Any, Protocol, runtime_checkable, |
| 10 | + Literal, TypeVar, Generic, Annotated |
| 11 | + ) |
| 12 | + |
| 13 | +from typing_extensions import Unpack, TypedDict, NotRequired |
6 | 14 |
|
7 | 15 | from amaranth.lib import wiring
|
8 | 16 | from amaranth.lib.wiring import Out
|
| 17 | +from pydantic import PlainSerializer, WithJsonSchema, WrapValidator |
9 | 18 |
|
| 19 | +from .. import _ensure_chipflow_root |
10 | 20 | from ._utils import InputIOSignature, OutputIOSignature, BidirIOSignature, IOModelOptions, _chipflow_schema_uri
|
11 | 21 | from ._annotate import amaranth_annotate
|
12 | 22 |
|
13 | 23 | SIM_ANNOTATION_SCHEMA = str(_chipflow_schema_uri("simulatable-interface", 0))
|
14 |
| -SIM_DATA_SCHEMA = str(_chipflow_schema_uri("simulatable-data", 0)) |
| 24 | +DATA_SCHEMA = str(_chipflow_schema_uri("simulatable-data", 0)) |
| 25 | +DRIVER_MODEL_SCHEMA = str(_chipflow_schema_uri("driver-model", 0)) |
15 | 26 |
|
16 | 27 | class SimInterface(TypedDict):
|
17 | 28 | uid: str
|
18 | 29 | parameters: List[Tuple[str, Any]]
|
19 | 30 |
|
20 |
| -class SimData(TypedDict): |
21 |
| - file_name: str |
| 31 | +@runtime_checkable |
| 32 | +@dataclass |
| 33 | +class DataclassProtocol(Protocol): |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +@dataclass |
| 38 | +class SoftwareBuild: |
| 39 | + """ |
| 40 | + This holds the information needed for building software and providing the built outcome |
| 41 | + """ |
| 42 | + |
| 43 | + sources: list[Path] |
| 44 | + includes: list[Path] |
| 45 | + include_dirs: list[Path] |
22 | 46 | offset: int
|
| 47 | + filename: Path |
| 48 | + build_dir: Path |
| 49 | + type: Literal["SoftwareBuild"] = "SoftwareBuild" |
| 50 | + |
| 51 | + def __init__(self, *, sources: list[Path], includes: list[Path] = [], include_dirs = [], offset=0): |
| 52 | + self.build_dir = _ensure_chipflow_root() / 'build' / 'software' |
| 53 | + self.filename = self.build_dir / 'software.bin' |
| 54 | + self.sources= list(sources) |
| 55 | + self.includes = list(includes) |
| 56 | + self.include_dirs = list(include_dirs) |
| 57 | + self.offset = offset |
| 58 | + |
| 59 | + |
| 60 | +_T_DataClass = TypeVar('_T_DataClass', bound=DataclassProtocol) |
| 61 | +class Data(TypedDict, Generic[_T_DataClass]): |
| 62 | + data: _T_DataClass |
| 63 | + |
| 64 | + |
| 65 | +class DriverModel(TypedDict): |
| 66 | + """ |
| 67 | + Options for `DriverSignature` |
| 68 | +
|
| 69 | + Attributes: |
| 70 | + component: The `wiring.Component` that this is the signature for. |
| 71 | + regs_struct: The name of the C struct that represents the registers of this component |
| 72 | + h_files: Header files for the driver |
| 73 | + c_files: C files for the driver |
| 74 | + regs_bus: The bus of this `Component` which contains its control registers |
| 75 | + include_dirs: any extra include directories needed by the driver |
| 76 | + """ |
| 77 | + # we just extrat the info we need, don't actually serialise a `wiring.Component`... |
| 78 | + component: Annotated[ |
| 79 | + wiring.Component, |
| 80 | + PlainSerializer(lambda x: { |
| 81 | + 'name': x.__class__.__name__, |
| 82 | + 'file': sys.modules[x.__module__].__file__ |
| 83 | + }, return_type=dict), |
| 84 | + WithJsonSchema({ |
| 85 | + 'type': 'object', |
| 86 | + 'properties': { |
| 87 | + 'name': { 'type': 'string' }, |
| 88 | + 'file': { 'type': 'string' }, |
| 89 | + } |
| 90 | + }), |
| 91 | + WrapValidator(lambda v, h: v) # Don't care about it actually.. |
| 92 | + ] | dict |
| 93 | + |
| 94 | + regs_struct: str |
| 95 | + h_files: NotRequired[list[Path]] |
| 96 | + c_files: NotRequired[list[Path]] |
| 97 | + include_dirs: NotRequired[list[Path]] |
| 98 | + regs_bus: NotRequired[str] |
| 99 | + _base_path: NotRequired[Path] # gets filled by the decorator to the base directory where the Component was defined |
| 100 | + |
23 | 101 |
|
24 | 102 | _VALID_UID = re.compile('[a-zA-Z_.]').search
|
25 | 103 |
|
@@ -124,7 +202,31 @@ def __chipflow_parameters__(self):
|
124 | 202 | return [('pin_count',self._pin_count)]
|
125 | 203 |
|
126 | 204 |
|
127 |
| -def attach_simulation_data(c: wiring.Component, **kwargs: Unpack[SimData]): |
128 |
| - setattr(c.signature, '__chipflow_simulation_data__', kwargs) |
129 |
| - amaranth_annotate(SimData, SIM_DATA_SCHEMA, '__chipflow_simulation_data__', decorate_object=True)(c.signature) |
| 205 | +def attach_data(external_interface: wiring.PureInterface, component: wiring.Component, data: DataclassProtocol): |
| 206 | + data_dict: Data = {'data':data} |
| 207 | + setattr(component.signature, '__chipflow_data__', data_dict) |
| 208 | + amaranth_annotate(Data, DATA_SCHEMA, '__chipflow_data__', decorate_object=True)(component.signature) |
| 209 | + setattr(external_interface.signature, '__chipflow_data__', data_dict) |
| 210 | + amaranth_annotate(Data, DATA_SCHEMA, '__chipflow_data__', decorate_object=True)(external_interface.signature) |
| 211 | + |
| 212 | + |
| 213 | +class DriverSignature(wiring.Signature): |
| 214 | + |
| 215 | + def __init__(self, members, **kwargs: Unpack[DriverModel]): |
| 216 | + definition_file = sys.modules[kwargs['component'].__module__].__file__ |
| 217 | + assert definition_file |
| 218 | + base_path = Path(definition_file).parent.absolute() |
| 219 | + kwargs['_base_path'] = base_path |
| 220 | + if 'regs_bus' not in kwargs: |
| 221 | + kwargs['regs_bus'] = 'bus' |
| 222 | + |
| 223 | + # execute any generators here |
| 224 | + for k in ('c_files', 'h_files', 'includedirs'): |
| 225 | + if k in kwargs: |
| 226 | + kwargs[k] = list(kwargs[k]) #type: ignore |
| 227 | + |
| 228 | + self.__chipflow_driver_model__ = kwargs |
| 229 | + amaranth_annotate(DriverModel, DRIVER_MODEL_SCHEMA, '__chipflow_driver_model__', decorate_object=True)(self) |
| 230 | + super().__init__(members=members) |
| 231 | + |
130 | 232 |
|
0 commit comments