|
| 1 | +# amaranth: UnusedElaboratable=no |
| 2 | +# SPDX-License-Identifier: BSD-2-Clause |
| 3 | + |
| 4 | +import os |
| 5 | +import unittest |
| 6 | +from unittest import mock |
| 7 | + |
| 8 | +import tomli |
| 9 | +from amaranth import Module, Signal, ClockDomain, ClockSignal, ResetSignal |
| 10 | +from amaranth.lib import io, wiring |
| 11 | +from amaranth.lib.wiring import Component, In |
| 12 | + |
| 13 | +from chipflow_lib import ChipFlowError |
| 14 | +from chipflow_lib.platforms.silicon import ( |
| 15 | + make_hashable, Heartbeat, IOBuffer, FFBuffer, SiliconPlatform, |
| 16 | + SiliconPlatformPort, HeartbeatSignature |
| 17 | +) |
| 18 | +from chipflow_lib.platforms.utils import Port |
| 19 | + |
| 20 | + |
| 21 | +class TestMakeHashable(unittest.TestCase): |
| 22 | + def test_make_hashable(self): |
| 23 | + """Test the make_hashable decorator""" |
| 24 | + # Create a simple class |
| 25 | + class TestClass: |
| 26 | + def __init__(self, value): |
| 27 | + self.value = value |
| 28 | + |
| 29 | + # Apply the decorator |
| 30 | + HashableTestClass = make_hashable(TestClass) |
| 31 | + |
| 32 | + # Create two instances with the same value |
| 33 | + obj1 = HashableTestClass(42) |
| 34 | + obj2 = HashableTestClass(42) |
| 35 | + |
| 36 | + # Test that they hash to different values (based on id) |
| 37 | + self.assertNotEqual(hash(obj1), hash(obj2)) |
| 38 | + |
| 39 | + # Test that they are not equal (based on id) |
| 40 | + self.assertNotEqual(obj1, obj2) |
| 41 | + |
| 42 | + # Test that an object is equal to itself |
| 43 | + self.assertEqual(obj1, obj1) |
| 44 | + |
| 45 | + |
| 46 | +class TestHeartbeat(unittest.TestCase): |
| 47 | + def test_heartbeat_init(self): |
| 48 | + """Test Heartbeat initialization""" |
| 49 | + # Create a mock port |
| 50 | + mock_port = mock.MagicMock() |
| 51 | + |
| 52 | + # Create heartbeat component |
| 53 | + heartbeat = Heartbeat(mock_port) |
| 54 | + |
| 55 | + # Check initialization |
| 56 | + self.assertEqual(heartbeat.clock_domain, "sync") |
| 57 | + self.assertEqual(heartbeat.counter_size, 23) |
| 58 | + self.assertEqual(heartbeat.name, "heartbeat") |
| 59 | + self.assertEqual(heartbeat.ports, mock_port) |
| 60 | + |
| 61 | + # Check signature |
| 62 | + self.assertEqual(heartbeat.signature, HeartbeatSignature) |
| 63 | + |
| 64 | + @mock.patch('chipflow_lib.platforms.silicon.io.Buffer') |
| 65 | + def test_heartbeat_elaborate(self, mock_buffer): |
| 66 | + """Test Heartbeat elaboration""" |
| 67 | + # Create mocks |
| 68 | + mock_port = mock.MagicMock() |
| 69 | + mock_platform = mock.MagicMock() |
| 70 | + mock_buffer_instance = mock.MagicMock() |
| 71 | + mock_buffer.return_value = mock_buffer_instance |
| 72 | + |
| 73 | + # Create heartbeat component |
| 74 | + heartbeat = Heartbeat(mock_port) |
| 75 | + |
| 76 | + # Call elaborate |
| 77 | + result = heartbeat.elaborate(mock_platform) |
| 78 | + |
| 79 | + # Verify the module has clock domain logic |
| 80 | + self.assertIsInstance(result, Module) |
| 81 | + |
| 82 | + # Check that the buffer was created |
| 83 | + mock_buffer.assert_called_with("o", mock_port.heartbeat) |
| 84 | + |
| 85 | + |
| 86 | +@mock.patch('chipflow_lib.platforms.silicon.IOBuffer.elaborate') |
| 87 | +class TestIOBuffer(unittest.TestCase): |
| 88 | + def test_io_buffer_elaborate_mocked(self, mock_elaborate): |
| 89 | + """Test IOBuffer class by mocking the elaborate method""" |
| 90 | + # Create a mock SiliconPlatformPort |
| 91 | + mock_port = mock.MagicMock(spec=SiliconPlatformPort) |
| 92 | + mock_port.direction = io.Direction.Input |
| 93 | + mock_port.invert = False |
| 94 | + |
| 95 | + # Setup mock elaborate to return a Module |
| 96 | + mock_elaborate.return_value = Module() |
| 97 | + |
| 98 | + # Create buffer |
| 99 | + buffer = IOBuffer("i", mock_port) |
| 100 | + |
| 101 | + # Call elaborate |
| 102 | + result = buffer.elaborate(mock.MagicMock()) |
| 103 | + |
| 104 | + # Check mock was called |
| 105 | + mock_elaborate.assert_called_once() |
| 106 | + |
| 107 | + # Check result is what was returned by mock |
| 108 | + self.assertIsInstance(result, Module) |
| 109 | + |
| 110 | + |
| 111 | +@mock.patch('chipflow_lib.platforms.silicon.FFBuffer.elaborate') |
| 112 | +class TestFFBuffer(unittest.TestCase): |
| 113 | + def test_ff_buffer_elaborate_mocked(self, mock_elaborate): |
| 114 | + """Test FFBuffer class by mocking the elaborate method""" |
| 115 | + # Create a mock SiliconPlatformPort |
| 116 | + mock_port = mock.MagicMock(spec=SiliconPlatformPort) |
| 117 | + mock_port.direction = io.Direction.Input |
| 118 | + |
| 119 | + # Setup mock elaborate to return a Module |
| 120 | + mock_elaborate.return_value = Module() |
| 121 | + |
| 122 | + # Create buffer |
| 123 | + buffer = FFBuffer("i", mock_port) |
| 124 | + |
| 125 | + # Call elaborate |
| 126 | + result = buffer.elaborate(mock.MagicMock()) |
| 127 | + |
| 128 | + # Check mock was called |
| 129 | + mock_elaborate.assert_called_once() |
| 130 | + |
| 131 | + # Check result is what was returned by mock |
| 132 | + self.assertIsInstance(result, Module) |
| 133 | + |
| 134 | + def test_ff_buffer_with_domains(self, mock_elaborate): |
| 135 | + """Test FFBuffer with custom domains""" |
| 136 | + # Create a mock SiliconPlatformPort |
| 137 | + mock_port = mock.MagicMock(spec=SiliconPlatformPort) |
| 138 | + mock_port.direction = io.Direction.Bidir |
| 139 | + |
| 140 | + # Setup mock elaborate to return a Module |
| 141 | + mock_elaborate.return_value = Module() |
| 142 | + |
| 143 | + # Create buffer with custom domains |
| 144 | + buffer = FFBuffer("io", mock_port, i_domain="i_domain", o_domain="o_domain") |
| 145 | + |
| 146 | + # Check domains were set |
| 147 | + self.assertEqual(buffer.i_domain, "i_domain") |
| 148 | + self.assertEqual(buffer.o_domain, "o_domain") |
| 149 | + |
| 150 | + |
| 151 | +class TestSiliconPlatformMethods(unittest.TestCase): |
| 152 | + def setUp(self): |
| 153 | + os.environ["CHIPFLOW_ROOT"] = os.path.dirname(os.path.dirname(__file__)) |
| 154 | + current_dir = os.path.dirname(__file__) |
| 155 | + customer_config = f"{current_dir}/fixtures/mock.toml" |
| 156 | + with open(customer_config, "rb") as f: |
| 157 | + self.config = tomli.load(f) |
| 158 | + |
| 159 | + @mock.patch('chipflow_lib.platforms.silicon.io.Buffer') |
| 160 | + @mock.patch('chipflow_lib.platforms.silicon.FFSynchronizer') |
| 161 | + @mock.patch('chipflow_lib.platforms.silicon.SiliconPlatformPort') |
| 162 | + @mock.patch('chipflow_lib.platforms.silicon.load_pinlock') |
| 163 | + def test_instantiate_ports(self, mock_load_pinlock, mock_silicon_platform_port, |
| 164 | + mock_ff_synchronizer, mock_buffer): |
| 165 | + """Test instantiate_ports method with fully mocked objects""" |
| 166 | + # Import here to avoid issues during test collection |
| 167 | + from chipflow_lib.platforms.silicon import SiliconPlatform |
| 168 | + |
| 169 | + # Create mock SiliconPlatformPort instances |
| 170 | + mock_port1 = mock.MagicMock() |
| 171 | + mock_port1.direction = io.Direction.Input |
| 172 | + mock_port2 = mock.MagicMock() |
| 173 | + mock_port2.direction = io.Direction.Input |
| 174 | + mock_port3 = mock.MagicMock() |
| 175 | + mock_port3.direction = io.Direction.Input |
| 176 | + |
| 177 | + # Setup SiliconPlatformPort constructor to return different mocks |
| 178 | + mock_silicon_platform_port.side_effect = [mock_port1, mock_port2, mock_port3] |
| 179 | + |
| 180 | + # Create buffer mocks |
| 181 | + mock_buffer_ret = mock.MagicMock() |
| 182 | + mock_buffer_ret.i = Signal() |
| 183 | + mock_buffer.return_value = mock_buffer_ret |
| 184 | + |
| 185 | + # Create mock pinlock |
| 186 | + mock_pinlock = mock.MagicMock() |
| 187 | + mock_load_pinlock.return_value = mock_pinlock |
| 188 | + |
| 189 | + # Setup port_map |
| 190 | + mock_component_port = mock.MagicMock() |
| 191 | + mock_component_port.port_name = "test_port" |
| 192 | + mock_pinlock.port_map = { |
| 193 | + "comp1": { |
| 194 | + "iface1": { |
| 195 | + "port1": mock_component_port |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + # Setup clocks and resets |
| 201 | + mock_clock_port = mock.MagicMock() |
| 202 | + mock_clock_port.port_name = "sys_clk" |
| 203 | + mock_reset_port = mock.MagicMock() |
| 204 | + mock_reset_port.port_name = "sys_rst_n" |
| 205 | + mock_pinlock.package.clocks = {"sys_clk": mock_clock_port} |
| 206 | + mock_pinlock.package.resets = {"sys_rst_n": mock_reset_port} |
| 207 | + |
| 208 | + # Create platform |
| 209 | + platform = SiliconPlatform(self.config) |
| 210 | + |
| 211 | + # Create module |
| 212 | + m = Module() |
| 213 | + |
| 214 | + # Call instantiate_ports |
| 215 | + platform.instantiate_ports(m) |
| 216 | + |
| 217 | + # Check that SiliconPlatformPort was called 3 times (once per port) |
| 218 | + self.assertEqual(mock_silicon_platform_port.call_count, 3) |
| 219 | + |
| 220 | + # Check that ports were added to the platform |
| 221 | + self.assertEqual(len(platform._ports), 3) |
| 222 | + self.assertIn("test_port", platform._ports) |
| 223 | + self.assertIn("sys_clk", platform._ports) |
| 224 | + self.assertIn("sys_rst_n", platform._ports) |
| 225 | + |
| 226 | + # Check that pinlock was set |
| 227 | + self.assertEqual(platform.pinlock, mock_pinlock) |
| 228 | + |
| 229 | + @mock.patch('chipflow_lib.platforms.silicon.load_pinlock') |
| 230 | + def test_instantiate_ports_missing_clock(self, mock_load_pinlock): |
| 231 | + """Test instantiate_ports method with missing clock""" |
| 232 | + # Import here to avoid issues during test collection |
| 233 | + from chipflow_lib.platforms.silicon import SiliconPlatform |
| 234 | + |
| 235 | + # Create mocks |
| 236 | + mock_pinlock = mock.MagicMock() |
| 237 | + mock_load_pinlock.return_value = mock_pinlock |
| 238 | + |
| 239 | + # Setup port_map |
| 240 | + mock_pinlock.port_map = {} |
| 241 | + |
| 242 | + # Setup clocks and resets - empty |
| 243 | + mock_pinlock.package.clocks = {} |
| 244 | + mock_pinlock.package.resets = {"sys_rst_n": mock.MagicMock()} |
| 245 | + |
| 246 | + # Create platform |
| 247 | + platform = SiliconPlatform(self.config) |
| 248 | + |
| 249 | + # Create module |
| 250 | + m = Module() |
| 251 | + |
| 252 | + # Call instantiate_ports - should raise ChipFlowError |
| 253 | + with self.assertRaises(ChipFlowError): |
| 254 | + platform.instantiate_ports(m) |
| 255 | + |
| 256 | + def test_get_io_buffer(self): |
| 257 | + """Test get_io_buffer method""" |
| 258 | + # Import here to avoid issues during test collection |
| 259 | + from chipflow_lib.platforms.silicon import SiliconPlatform |
| 260 | + |
| 261 | + # Create platform |
| 262 | + platform = SiliconPlatform(self.config) |
| 263 | + |
| 264 | + # Create a SiliconPlatformPort |
| 265 | + port_obj = Port(type="bidir", pins=["1", "2"], port_name="test_bidir", |
| 266 | + direction="io", options={"all_have_oe": False}) |
| 267 | + silicon_port = SiliconPlatformPort("comp", "test_bidir", port_obj) |
| 268 | + |
| 269 | + # Create different buffer types |
| 270 | + io_buffer = io.Buffer("io", silicon_port) |
| 271 | + ff_buffer = io.FFBuffer("io", silicon_port, i_domain="sync", o_domain="sync") |
| 272 | + |
| 273 | + # Test with io.Buffer |
| 274 | + result_io = platform.get_io_buffer(io_buffer) |
| 275 | + self.assertIsInstance(result_io, IOBuffer) |
| 276 | + |
| 277 | + # Test with io.FFBuffer |
| 278 | + result_ff = platform.get_io_buffer(ff_buffer) |
| 279 | + self.assertIsInstance(result_ff, FFBuffer) |
| 280 | + |
| 281 | + # Test with unsupported buffer type |
| 282 | + unsupported_buffer = mock.MagicMock() |
| 283 | + with self.assertRaises(TypeError): |
| 284 | + platform.get_io_buffer(unsupported_buffer) |
| 285 | + |
| 286 | + |
0 commit comments