1+ # SPDX-License-Identifier: BSD-2-Clause
2+ import itertools
3+ import logging
4+ import pytest #noqa
5+
6+ from pprint import pformat
7+
8+ from amaranth .lib import io
9+
10+ from chipflow_lib .platforms .utils import PinSignature , OutputPinSignature , InputPinSignature , BidirPinSignature , _PinAnnotation , _PinAnnotationModel
11+ from chipflow_lib .platforms .utils import PinList , _group_consecutive_items ,_find_contiguous_sequence , _Side
12+
13+
14+ logger = logging .getLogger (__name__ )
15+
16+
17+ def gen_quad_pins (width , height ) -> PinList :
18+ return sorted (
19+ [e for e in itertools .product ((_Side .N , _Side .S ), range (width ))] +
20+ [e for e in itertools .product ((_Side .W , _Side .E ), range (height ))]
21+ )
22+
23+
24+ def test_group_consecutive_items_null ():
25+ ordering = gen_quad_pins (50 ,60 )
26+ pins = ordering .copy ()
27+ groups = _group_consecutive_items (pins ,pins )
28+ assert len (groups .keys ()) == 1
29+ assert len (ordering ) in groups .keys ()
30+
31+ def test_group_consecutive_items_nonconsecutive ():
32+ ordering = gen_quad_pins (50 ,60 )
33+ pins = ordering [0 :6 ] + ordering [7 :70 ] + ordering [71 :180 ] + ordering [181 :]
34+ logger .debug (f"{ ordering } { pins } " )
35+ groups = _group_consecutive_items (ordering ,pins )
36+ logger .debug (f"\n { pformat (groups )} " )
37+ assert len (ordering ) == 50 * 2 + 60 * 2
38+ assert len (groups .keys ()) == 4
39+ assert sum (groups .keys ()) == len (ordering ) - 3
40+ assert 6 in groups .keys ()
41+ assert 70 - 7 in groups .keys ()
42+ assert 180 - 71 in groups .keys ()
43+ assert len (ordering ) - 181 in groups .keys ()
44+
45+ def test_find_contiguous_sequence ():
46+ ordering = gen_quad_pins (50 ,60 )
47+ pins = ordering [0 :6 ] + ordering [7 :70 ] + ordering [71 :180 ] + ordering [181 :]
48+ seq = _find_contiguous_sequence (ordering , pins , 120 )
49+ logger .debug (f"\n { pformat (seq )} " )
50+ logger .debug (f"{ ordering [71 :180 ] + ordering [181 :191 ]} " )
51+ assert len (seq ) == 120
52+ assert seq == ordering [71 :180 ] + ordering [181 :192 ]
53+
54+
55+ def test_pin_signature ():
56+ sig_bidir = PinSignature (io .Direction .Bidir , width = 8 )
57+ assert isinstance (sig_bidir , PinSignature )
58+ assert sig_bidir ._direction == io .Direction .Bidir
59+ assert sig_bidir ._width == 8
60+ assert "o" in sig_bidir .members
61+ assert "oe" in sig_bidir .members
62+ assert "i" in sig_bidir .members
63+
64+ sig_output = OutputPinSignature (width = 4 )
65+ assert isinstance (sig_output , PinSignature )
66+ assert sig_output ._direction == io .Direction .Output
67+ assert sig_output ._width == 4
68+ assert "o" in sig_output .members
69+ assert "oe" not in sig_output .members
70+ assert "i" not in sig_output .members
71+
72+ sig_input = InputPinSignature (width = 2 )
73+ assert isinstance (sig_input , PinSignature )
74+ assert sig_input ._direction == io .Direction .Input
75+ assert sig_input ._width == 2
76+ assert "o" not in sig_input .members
77+ assert "oe" not in sig_output .members
78+ assert "i" in sig_input .members
79+
80+ sig_bidir_fn = BidirPinSignature (width = 1 )
81+ assert isinstance (sig_bidir_fn , PinSignature )
82+ assert sig_bidir_fn ._direction == io .Direction .Bidir
83+ assert sig_bidir_fn ._width == 1
84+ assert "o" in sig_bidir_fn .members
85+ assert "oe" in sig_bidir_fn .members
86+ assert "i" in sig_bidir_fn .members
87+
88+ def test_pin_annotation_model ():
89+ model = _PinAnnotationModel (direction = io .Direction .Output , width = 32 )
90+ assert model .direction == "o"
91+ assert model .width == 32
92+
93+ def test_pin_annotation ():
94+ annotation = _PinAnnotation (direction = io .Direction .Input , width = 16 )
95+ assert isinstance (annotation , _PinAnnotation )
96+ assert annotation .model .direction == "i"
97+ assert annotation .model .width == 16
98+
99+ def test_pin_annotation_as_json ():
100+ annotation = _PinAnnotation (direction = io .Direction .Bidir , width = 8 )
101+ json_output = annotation .as_json ()
102+ print (f"json_output: { json_output } " ) # Debug print using print()
103+ assert isinstance (json_output , dict )
104+ assert json_output ["direction" ] == "io"
105+ assert json_output ["width" ] == 8
0 commit comments