1+ import logging
2+
3+ from enum import Enum
14from dataclasses import dataclass
2- from typing import Set , List
5+ from typing import Set , List , TypeVar , Dict
36
47from amaranth import Shape
58from amaranth .lib import wiring , io , meta
69from amaranth .lib .wiring import In , Out
710from pydantic import BaseModel , ConfigDict
811from pprint import pformat
912
13+ from .. import ChipFlowError
1014from .silicon import *
1115from .sim import *
1216
17+ logger = logging .getLogger (__name__ )
18+
1319def chipflow_schema_uri (name : str , version :int ) -> str :
1420 return f"https://api.chipflow.com/schemas/{ version } /{ name } "
1521
@@ -66,10 +72,10 @@ def origin(self): # type: ignore
6672 return self .model
6773
6874 def as_json (self ): # type: ignore
69- print (f"PinAnnotations: { pformat (self .model .model_dump_json ())} " )
7075 return self .model .model_dump ()
7176
72- PIN_ANNOTATION_SCHEMA = chipflow_schema_uri ("pin-annotation" ,0 )
77+ class Schemas (Enum ):
78+ PIN_ANNOTATION = chipflow_schema_uri ("pin-annotation" ,0 )
7379
7480class PinSignature (wiring .Signature ):
7581 def __init__ (self , direction , width = 1 , init = None ):
@@ -95,10 +101,6 @@ def __init__(self, direction, width=1, init=None):
95101 def annotations (self , * args ):
96102 return wiring .Signature .annotations (self , * args ) + (PinAnnotation (direction = self ._direction , width = self ._width ),)
97103
98- def create (self , * args , ** kwargs ):
99- print (f"Calling PinSignature.create({ args } ,{ kwargs } )" )
100- return super ().create (* args , ** kwargs )
101-
102104 def __repr__ (self ):
103105 return f"PinSignature({ self ._direction } , { self ._width } )"
104106
@@ -111,9 +113,9 @@ def InputPinSignature(width):
111113def BidirPinSignature (width ):
112114 return PinSignature (io .Direction .Bidir )
113115
114- def group_consecutive_items (lst ) :
116+ def group_consecutive_items (lst : List [ int ]) -> Dict [ int , List [ int ]] :
115117 if not lst :
116- return []
118+ return {}
117119
118120 grouped = []
119121 current_group = [lst [0 ]]
@@ -131,8 +133,7 @@ def group_consecutive_items(lst):
131133 d .setdefault (len (g ), []).append (g )
132134 return d
133135
134-
135- def find_contiguous_sequence (lst , n ):
136+ def find_contiguous_sequence (lst : List [int ], total : int ) -> List [int ]:
136137 """Find the next sequence of n consecutive numbers in a sorted list
137138
138139 Args:
@@ -143,22 +144,22 @@ def find_contiguous_sequence(lst, n):
143144 A slice indexing the first sequence of n consecutive numbers found within the given list
144145 if unable to find a consecutive list, allocate as contigously as possible
145146 """
146- if not lst or len (lst ) < n :
147- return None
147+ if not lst or len (lst ) < total :
148+ raise ChipFlowError ( "Invalid request to find_contiguous_argument" )
148149
149150 grouped = group_consecutive_items (lst )
150151
151- print (f"grouped list: { pformat (grouped )} " )
152152 ret = []
153+ n = total
153154 for k in sorted (grouped .keys (), reverse = True ):
154155 for g in grouped [k ]:
155- print ( f" { g } { k } { n } " )
156+ assert ( n + len ( ret ) == total )
156157 if k >= n :
157- ret += g [0 :min (n - 1 , k - 1 )]
158+ ret += g [0 :min (n , k )]
158159 return ret
159160 else :
160161 n = n - k
161- ret += g [0 :min ( n - 1 , k - 1 ) ]
162+ ret += g [0 :k ]
162163
163164 return ret
164165
@@ -173,8 +174,10 @@ def pins(self) -> Set[str]:
173174
174175 def allocate (self , available : Set [str ], width :int ) -> List [str ]:
175176 avail_n = sorted ([int (i ) for i in available ])
176- print (f"PGAPackageDef.allocate { avail_n } , { width } " )
177+ logger . debug (f"PGAPackageDef.allocate { width } from { len ( avail_n ) } remaining " )
177178 ret = find_contiguous_sequence (avail_n , width )
179+ logger .debug (f"PGAPackageDef.returned { ret } " )
180+ assert (len (ret )== width )
178181 return [str (i ) for i in ret ]
179182
180183PACKAGE_DEFINITIONS = {
0 commit comments