|
7 | 7 | import docplex.cp.modeler as mod |
8 | 8 |
|
9 | 9 | from adijif.clocks.clock import clock |
| 10 | +from adijif.draw import Layout, Node |
10 | 11 | from adijif.solvers import CpoSolveResult |
11 | 12 |
|
12 | 13 |
|
@@ -198,8 +199,103 @@ def get_config(self, solution: CpoSolveResult = None) -> Dict: |
198 | 199 | for i in range(0, 10): |
199 | 200 | config["q" + str(i)] = self._get_val(self.config["q" + str(i)]) |
200 | 201 |
|
| 202 | + self._saved_solution = config |
| 203 | + |
201 | 204 | return config |
202 | 205 |
|
| 206 | + def draw(self, lo: Layout = None) -> str: |
| 207 | + """Draw clock tree diagram for AD9545. |
| 208 | +
|
| 209 | + Args: |
| 210 | + lo: Layout for drawing |
| 211 | +
|
| 212 | + Returns: |
| 213 | + str: SVG diagram string |
| 214 | +
|
| 215 | + Raises: |
| 216 | + Exception: If no solution is saved |
| 217 | + """ |
| 218 | + if not self._saved_solution: |
| 219 | + raise Exception("No solution to draw. Must call solve first.") |
| 220 | + |
| 221 | + config = self._saved_solution |
| 222 | + system_draw = lo is not None |
| 223 | + if not system_draw: |
| 224 | + lo = Layout("AD9545 Example") |
| 225 | + else: |
| 226 | + assert isinstance(lo, Layout), "lo must be a Layout object" |
| 227 | + |
| 228 | + ic_node = Node("AD9545") |
| 229 | + lo.add_node(ic_node) |
| 230 | + |
| 231 | + # Add active input references with R dividers |
| 232 | + for i in range(4): |
| 233 | + if self.input_refs[i] != 0: |
| 234 | + r_val = config[f"r{i}"] |
| 235 | + r_node = Node(f"R{i}", ntype="divider") |
| 236 | + r_node.value = str(r_val) |
| 237 | + ic_node.add_child(r_node) |
| 238 | + |
| 239 | + if not system_draw: |
| 240 | + ref_node = Node(f"REF{i}", ntype="input") |
| 241 | + lo.add_node(ref_node) |
| 242 | + lo.add_connection( |
| 243 | + { |
| 244 | + "from": ref_node, |
| 245 | + "to": r_node, |
| 246 | + "rate": self.input_refs[i], |
| 247 | + } |
| 248 | + ) |
| 249 | + |
| 250 | + # Add PLLs |
| 251 | + for i in range(2): |
| 252 | + if self.PLL_used[i]: |
| 253 | + pll_rate = config[f"PLL{i}"].get("rate_hz", 0) |
| 254 | + pll_node = Node(f"PLL{i}", ntype="voltage-controlled-oscillator") |
| 255 | + pll_node.shape = "circle" |
| 256 | + ic_node.add_child(pll_node) |
| 257 | + |
| 258 | + # Connect active input R dividers to PLL |
| 259 | + for j in range(4): |
| 260 | + if self.input_refs[j] != 0: |
| 261 | + r_node = ic_node.get_child(f"R{j}") |
| 262 | + pll_in_rate = self.input_refs[j] / config[f"r{j}"] |
| 263 | + ic_node.add_connection( |
| 264 | + {"from": r_node, "to": pll_node, "rate": pll_in_rate} |
| 265 | + ) |
| 266 | + |
| 267 | + # Add output Q dividers |
| 268 | + out_dividers = Node("Output Dividers", ntype="shell") |
| 269 | + ic_node.add_child(out_dividers) |
| 270 | + |
| 271 | + for i in range(10): |
| 272 | + if self.out_freqs[i] != 0: |
| 273 | + q_val = config[f"q{i}"] |
| 274 | + q_node = Node(f"Q{i}", ntype="divider") |
| 275 | + q_node.value = str(q_val) |
| 276 | + out_dividers.add_child(q_node) |
| 277 | + |
| 278 | + # Determine which PLL feeds this output |
| 279 | + pll_idx = 0 if i <= 5 else 1 |
| 280 | + if self.PLL_used[pll_idx]: |
| 281 | + pll_node = ic_node.get_child(f"PLL{pll_idx}") |
| 282 | + pll_rate = config[f"PLL{pll_idx}"].get("rate_hz", 0) |
| 283 | + ic_node.add_connection( |
| 284 | + {"from": pll_node, "to": q_node, "rate": pll_rate} |
| 285 | + ) |
| 286 | + |
| 287 | + out_node = Node(f"OUT{i}", ntype="out_clock_connected") |
| 288 | + lo.add_node(out_node) |
| 289 | + lo.add_connection( |
| 290 | + { |
| 291 | + "from": q_node, |
| 292 | + "to": out_node, |
| 293 | + "rate": self.out_freqs[i], |
| 294 | + } |
| 295 | + ) |
| 296 | + |
| 297 | + return lo.draw() |
| 298 | + |
203 | 299 | def _setup_solver_constraints( |
204 | 300 | self, input_refs: List[int], out_freqs: List[int] |
205 | 301 | ) -> None: |
|
0 commit comments