Skip to content

Commit bd3d696

Browse files
committed
simulation: Add cocotb python library for XLS
The library contains XLSChannel, XLSChannelDriver and XLSChannelMonitor classes. * XLSChannel - provides a mechanism for wrapping all signals related to XLS channels into one object. * XLSChannelDriver - may be used to send data to XLS channel * XLSChannelMonitor - may be used to monitor transaction taking place in XLS Channel Internal-tag: [#46586] Signed-off-by: Robert Winkler <[email protected]>
1 parent cf68778 commit bd3d696

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

xls/simulation/cocotb/BUILD

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright 2023 The XLS Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("@xls_pip_deps//:requirements.bzl", "requirement")
16+
17+
package(
18+
default_visibility = ["//xls:xls_public"],
19+
licenses = ["notice"], # Apache 2.0
20+
)
21+
22+
py_library(
23+
name = "cocotb_xls",
24+
srcs = ["cocotb_xls.py"],
25+
imports = ["."],
26+
deps = [
27+
requirement("cocotb"),
28+
requirement("cocotb_bus"),
29+
],
30+
)
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Copyright 2023 The XLS Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from typing import Any, Sequence, Union
16+
17+
import cocotb
18+
from cocotb.binary import BinaryValue
19+
from cocotb.handle import SimHandleBase
20+
from cocotb.triggers import RisingEdge
21+
from cocotb_bus.bus import Bus
22+
from cocotb_bus.drivers import BusDriver
23+
from cocotb_bus.monitors import BusMonitor
24+
25+
Transaction = Union[BinaryValue, Sequence[BinaryValue]]
26+
27+
XLS_CHANNEL_SIGNALS = ["data", "rdy", "vld"]
28+
XLS_CHANNEL_OPTIONAL_SIGNALS = []
29+
30+
31+
class XLSChannel(Bus):
32+
_signals = XLS_CHANNEL_SIGNALS
33+
_optional_signals = XLS_CHANNEL_OPTIONAL_SIGNALS
34+
35+
def __init__(self, entity, name, **kwargs: Any):
36+
super().__init__(entity, name, self._signals, self._optional_signals, **kwargs)
37+
38+
39+
class XLSChannelDriver(BusDriver):
40+
_signals = XLS_CHANNEL_SIGNALS
41+
_optional_signals = XLS_CHANNEL_OPTIONAL_SIGNALS
42+
43+
def __init__(self, entity: SimHandleBase, name: str, clock: SimHandleBase, **kwargs: Any):
44+
BusDriver.__init__(self, entity, name, clock, **kwargs)
45+
46+
self.bus.data.setimmediatevalue(0)
47+
self.bus.vld.setimmediatevalue(0)
48+
49+
async def _driver_send(self, transaction: Transaction, sync: bool = True, **kwargs: Any) -> None:
50+
if sync:
51+
await RisingEdge(self.clock)
52+
53+
data_to_send = (transaction if isinstance(transaction, Sequence) else [transaction])
54+
55+
for word in data_to_send:
56+
self.bus.vld.value = 1
57+
self.bus.data.value = word
58+
59+
while True:
60+
await RisingEdge(self.clock)
61+
if self.bus.rdy.value:
62+
break
63+
64+
self.bus.vld.value = 0
65+
66+
67+
class XLSChannelMonitor(BusMonitor):
68+
_signals = XLS_CHANNEL_SIGNALS
69+
_optional_signals = XLS_CHANNEL_OPTIONAL_SIGNALS
70+
71+
def __init__(self, entity: SimHandleBase, name: str, clock: SimHandleBase, **kwargs: Any):
72+
BusMonitor.__init__(self, entity, name, clock, **kwargs)
73+
74+
@cocotb.coroutine
75+
async def _monitor_recv(self) -> None:
76+
while True:
77+
await RisingEdge(self.clock)
78+
if self.bus.rdy.value:
79+
vec = self.bus.data.value
80+
self._recv(vec)

0 commit comments

Comments
 (0)