-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modules/zstd/cocotb: Add cocotb testing utilities
- XLSStruct for easier handling and serializing/deserializing XLS structs - XLSChannel that serves as a dummy receiving channel - XLSMonitor that monitors transactions on an XLS channel - XLSDriver that can send data on an XLS channel - LatencyScoreboard that can measure latency between corresponding transactions on input and output buses - File-backed AXI memory python model Internal-tag: [#64075] Signed-off-by: Krzysztof Obłonczek <[email protected]>
- Loading branch information
1 parent
013deef
commit bd6b3ea
Showing
8 changed files
with
579 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# Copyright 2024 The XLS Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
load("@xls_pip_deps//:requirements.bzl", "requirement") | ||
|
||
package( | ||
default_applicable_licenses = ["//:license"], | ||
default_visibility = ["//xls:xls_users"], | ||
licenses = ["notice"], | ||
) | ||
|
||
py_library( | ||
name = "channel", | ||
srcs = ["channel.py"], | ||
deps = [ | ||
":xlsstruct", | ||
requirement("cocotb"), | ||
requirement("cocotb_bus"), | ||
] | ||
) | ||
|
||
py_library( | ||
name = "memory", | ||
srcs = ["memory.py"], | ||
deps = [ | ||
requirement("cocotbext-axi"), | ||
] | ||
) | ||
|
||
py_library( | ||
name = "scoreboard", | ||
srcs = ["scoreboard.py"], | ||
deps = [ | ||
":channel", | ||
":xlsstruct", | ||
requirement("cocotb"), | ||
] | ||
) | ||
|
||
py_library( | ||
name = "utils", | ||
srcs = ["utils.py"], | ||
deps = [ | ||
requirement("cocotb"), | ||
"//xls/common:runfiles", | ||
] | ||
) | ||
|
||
py_library( | ||
name = "xlsstruct", | ||
srcs = ["xlsstruct.py"], | ||
deps = [ | ||
requirement("cocotb"), | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Copyright 2024 The XLS Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from typing import Any, Sequence, Type, Union | ||
|
||
import cocotb | ||
from cocotb.handle import SimHandleBase | ||
from cocotb.triggers import RisingEdge | ||
from cocotb_bus.bus import Bus | ||
from cocotb_bus.drivers import BusDriver | ||
from cocotb_bus.monitors import BusMonitor | ||
|
||
from xls.modules.zstd.cocotb.xlsstruct import XLSStruct | ||
|
||
Transaction = Union[XLSStruct, Sequence[XLSStruct]] | ||
|
||
XLS_CHANNEL_SIGNALS = ["data", "rdy", "vld"] | ||
XLS_CHANNEL_OPTIONAL_SIGNALS = [] | ||
|
||
|
||
class XLSChannel(Bus): | ||
_signals = XLS_CHANNEL_SIGNALS | ||
_optional_signals = XLS_CHANNEL_OPTIONAL_SIGNALS | ||
|
||
def __init__(self, entity, name, clk, *, start_now=False, **kwargs: Any): | ||
super().__init__(entity, name, self._signals, self._optional_signals, **kwargs) | ||
self.clk = clk | ||
if start_now: | ||
self.start_recv_loop() | ||
|
||
@cocotb.coroutine | ||
async def recv_channel(self): | ||
"""Cocotb coroutine that acts as a proc receiving data from a channel""" | ||
self.rdy.setimmediatevalue(1) | ||
while True: | ||
await RisingEdge(self.clk) | ||
|
||
def start_recv_loop(self): | ||
cocotb.start_soon(self.recv_channel()) | ||
|
||
|
||
class XLSChannelDriver(BusDriver): | ||
_signals = XLS_CHANNEL_SIGNALS | ||
_optional_signals = XLS_CHANNEL_OPTIONAL_SIGNALS | ||
|
||
def __init__(self, entity: SimHandleBase, name: str, clock: SimHandleBase, **kwargs: Any): | ||
BusDriver.__init__(self, entity, name, clock, **kwargs) | ||
|
||
self.bus.data.setimmediatevalue(0) | ||
self.bus.vld.setimmediatevalue(0) | ||
|
||
async def _driver_send(self, transaction: Transaction, sync: bool = True, **kwargs: Any) -> None: | ||
if sync: | ||
await RisingEdge(self.clock) | ||
|
||
data_to_send = (transaction if isinstance(transaction, Sequence) else [transaction]) | ||
|
||
for word in data_to_send: | ||
self.bus.vld.value = 1 | ||
self.bus.data.value = word.binaryvalue | ||
|
||
while True: | ||
await RisingEdge(self.clock) | ||
if self.bus.rdy.value: | ||
break | ||
|
||
self.bus.vld.value = 0 | ||
|
||
|
||
class XLSChannelMonitor(BusMonitor): | ||
_signals = XLS_CHANNEL_SIGNALS | ||
_optional_signals = XLS_CHANNEL_OPTIONAL_SIGNALS | ||
|
||
def __init__(self, entity: SimHandleBase, name: str, clock: SimHandleBase, struct: Type[XLSStruct], **kwargs: Any): | ||
BusMonitor.__init__(self, entity, name, clock, **kwargs) | ||
self.struct = struct | ||
|
||
@cocotb.coroutine | ||
async def _monitor_recv(self) -> None: | ||
while True: | ||
await RisingEdge(self.clock) | ||
if self.bus.rdy.value and self.bus.vld.value: | ||
vec = self.struct.from_int(self.bus.data.value.integer) | ||
self._recv(vec) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright 2024 The XLS Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
|
||
from cocotbext.axi.axi_ram import AxiRam, AxiRamRead, AxiRamWrite | ||
from cocotbext.axi.sparse_memory import SparseMemory | ||
|
||
|
||
def init_axi_mem(path: os.PathLike, **kwargs): | ||
with open(path, "rb") as f: | ||
sparse_mem = SparseMemory(size=kwargs["size"]) | ||
sparse_mem.write(0x0, f.read()) | ||
kwargs["mem"] = sparse_mem | ||
|
||
|
||
class AxiRamReadFromFile(AxiRamRead): | ||
def __init__(self, *args, path: os.PathLike, **kwargs): | ||
init_axi_mem(path, **kwargs) | ||
super().__init__(*args, **kwargs) | ||
|
||
|
||
class AxiRamFromFile(AxiRam): | ||
def __init__(self, *args, path: os.PathLike, **kwargs): | ||
init_axi_mem(path, **kwargs) | ||
super().__init__(*args, **kwargs) | ||
|
||
|
||
class AxiRamWriteFromFile(AxiRamWrite): | ||
def __init__(self, *args, path: os.PathLike, **kwargs): | ||
init_axi_mem(path, **kwargs) | ||
super().__init__(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Copyright 2024 The XLS Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from dataclasses import dataclass | ||
from queue import Queue | ||
|
||
from cocotb.clock import Clock | ||
from cocotb.log import SimLog | ||
from cocotb.utils import get_sim_time | ||
|
||
from xls.modules.zstd.cocotb.channel import XLSChannelMonitor | ||
from xls.modules.zstd.cocotb.xlsstruct import XLSStruct | ||
|
||
|
||
@dataclass | ||
class LatencyQueueItem: | ||
transaction: XLSStruct | ||
timestamp: int | ||
|
||
|
||
class LatencyScoreboard: | ||
def __init__(self, dut, clock: Clock, req_monitor: XLSChannelMonitor, resp_monitor: XLSChannelMonitor): | ||
self.dut = dut | ||
self.log = SimLog(f"zstd.cocotb.scoreboard.{self.dut._name}") | ||
self.clock = clock | ||
self.req_monitor = req_monitor | ||
self.resp_monitor = resp_monitor | ||
self.pending_req = Queue() | ||
self.results = [] | ||
|
||
self.req_monitor.add_callback(self._req_callback) | ||
self.resp_monitor.add_callback(self._resp_callback) | ||
|
||
def _current_cycle(self): | ||
return get_sim_time(units='step') / self.clock.period | ||
|
||
def _req_callback(self, transaction: XLSStruct): | ||
self.pending_req.put(LatencyQueueItem(transaction, self._current_cycle())) | ||
|
||
def _resp_callback(self, transaction: XLSStruct): | ||
latency_item = self.pending_req.get() | ||
self.results.append(self._current_cycle() - latency_item.timestamp) | ||
|
||
def average_latency(self): | ||
return sum(self.results)/len(self.results) | ||
|
||
def report_result(self): | ||
if not self.pending_req.empty(): | ||
self.log.warning(f"There are unfulfilled requests from channel {self.req_monitor.name}") | ||
while not self.pending_req.empty(): | ||
self.log.warning(f"Unfulfilled request: {self.pending_req.get()}") | ||
if len(self.results) > 0: | ||
self.log.info(f"Latency report - 1st latency: {self.results[0]}") | ||
if len(self.results) > 1: | ||
self.log.info(f"Latency report - 2nd latency: {self.results[1]}") | ||
if len(self.results) > 2: | ||
avg = sum(self.results[2:])/len(self.results[2:]) | ||
self.log.info(f"Latency report - rest of the latencies (average): {avg}") |
Oops, something went wrong.