-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrohd_connector.py
More file actions
293 lines (242 loc) · 10.3 KB
/
rohd_connector.py
File metadata and controls
293 lines (242 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"""
Copyright (C) 2022-2025 Intel Corporation
SPDX-License-Identifier: BSD-3-Clause
rohd_connector.py
Code to connect a python cocotb process to ROHD
2022 January 9
Author: Max Korbel <max.korbel@intel.com>
"""
import logging
import socket
from time import sleep
import select
import time
import cocotb
from cocotb.triggers import Timer
from cocotb.types import LogicArray
# pylint: disable=broad-exception-caught, multiple-statements
# pylint: disable=too-many-arguments, too-many-locals, too-many-branches, too-many-statements, too-many-instance-attributes, too-many-positional-arguments
class RohdConnector:
"""
A connector for communicating between the ROHD simulator and another simulator.
"""
def __init__(
self,
enable_logging=False,
listen_timeout=5,
tick_timeout=5,
clk_ratio=1000,
clk_units="ps",
dart_connect_timeout=120,
):
self.enable_logging = enable_logging
# this remains None until the socket is connected
self.sock = None
self._create_socket()
# track if listener should remain active
self.continue_listen = True
self.output_map = {}
self.time = 0 # clk_units
self.listen_timeout = listen_timeout
self.tick_timeout = tick_timeout
self.dart_connect_timeout = dart_connect_timeout
self.clk_units = clk_units
# this is the ratio of tick to time
# 1000 means time progresses (1000 x clk_units) per tick
self.clk_ratio = clk_ratio
# if true, indicates that we're in the middle of a tick!
self.mid_tick = False
if self.enable_logging:
cocotb.log.setLevel(logging.DEBUG)
self.shutdown_requested = False
# last time a message was received from the dart side, for timeout calculations
self.last_message_received_time = None
def _create_socket(self):
self.init_sock = socket.socket()
self.init_sock.bind(("", 0))
self.init_sock.listen()
self.socket_port = self.init_sock.getsockname()[1]
def connect_to_socket(self):
"""
Creates a socket on `self.socket_port` and attempts to connect to it.
"""
print(
f"ROHD COSIM SOCKET:{self.socket_port}", flush=True
) # critical for connection
if self.dart_connect_timeout:
self.init_sock.settimeout(self.dart_connect_timeout)
try:
conn, _addr = self.init_sock.accept()
conn.setblocking(False)
self.sock = conn
except socket.timeout:
self._error(
f"Socket connection timed out after {self.dart_connect_timeout} seconds!"
)
def _sock_send(self, message: str):
try:
self.sock.send((f"@{self.time}:" + message + "\n").encode())
except Exception as exception:
self._error(
f'Encountered error when sending message "{message}": {exception}'
)
def _error(self, message: str):
print(f"ERROR: {message}", flush=True)
# raise RuntimeError(message)
self.shutdown()
def shutdown(self):
"""
Attempts to gracefully shut down the ROHD simulator, the socket connection, and all
listeners within this connector.
"""
print("Shutting down cosimulation socket.")
try:
if not self.shutdown_requested:
# prevent infinite recursion from sock_send->shutdown
self.shutdown_requested = True
self._sock_send("END")
# give some time for the END to get there
sleep(1)
self.continue_listen = False
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close()
except Exception as exception:
print(
f"Encountered exception during disconnection, but ignoring: {exception}",
flush=True,
)
@cocotb.coroutine
async def listen_for_stimulus(self, name_to_signal_map):
"""
Starts the process of listening for stimulus from the ROHD simulator.
"""
if self.enable_logging:
print("Listening for stimulus...")
self.continue_listen = True
pending_message = ""
self.sock.setblocking(False)
# self.sock.settimeout(1)
while self.continue_listen:
# https://docs.python.org/3/howto/sockets.html#non-blocking-sockets
try:
timeout_s = self.listen_timeout # seconds
ready_to_read, _ready_to_write, _in_error = select.select(
[self.sock], [], [], timeout_s
)
except select.error:
if self.enable_logging:
print(
"Detected socket shutdown, ending cosim execution!", flush=True
)
self.sock.shutdown(socket.SHUT_RDWR)
self.sock.close()
self.continue_listen = False
break
if not ready_to_read and self.mid_tick:
# not necessarily an error, since maybe SV simulator called $finish
print("Timeout waiting for tick to complete in Simulator!", flush=True)
self.shutdown()
break
if (
self.last_message_received_time is not None
and self.dart_connect_timeout is not None
and (
(time.time() - self.last_message_received_time)
> self.dart_connect_timeout
)
):
self._error(
"Timeout waiting for Dart messages! Perhaps the Dart side has hung "
"or died without shutting down the connection gracefully."
)
break
if len(ready_to_read) > 0:
self.last_message_received_time = time.time()
try:
full_message = pending_message + self.sock.recv(1024).decode(
"ascii"
)
except Exception as exception:
print(
f"Encountered exception when reading from socket: {exception}"
)
print("It is possible the ROHD process has unexpectedly ended.")
self._error("Communication with ROHD process failed.")
# break
pending_message = ""
split_message = full_message.split(";")
# if we didn't get a full message, save the tail for next time
if not full_message.endswith(";"):
pending_message = split_message[-1]
split_message = split_message[0:-1]
for message in split_message:
message = message.strip()
if not message:
continue
if self.enable_logging:
print(f"Received message: {message}")
if message.startswith("TICK:"):
# Format: TICK:<newTimeInNs>
split_message = message.split(":")
new_time = int(split_message[1])
await self._tick(new_time)
elif message.startswith("DRIVE:"):
# Format: DRIVE:<signalName>:<newValueInBinary>
# Example: DRIVE:apple:01XZ1100
split_message = message.split(":")
signal_name = split_message[1]
binary_string = split_message[2]
logic_value = LogicArray(binary_string)
if signal_name not in name_to_signal_map:
self._error(
f'Signal "{signal_name}" not set up for driving! Unable to drive.'
)
try:
name_to_signal_map[signal_name].value = logic_value
except Exception as exception:
self._error(f"Failed to write {signal_name}: {exception}")
elif message == "END":
if self.enable_logging:
print("Finishing listening for stimulus")
self.shutdown()
break
else:
self._error(f"Unknown message received: {message}")
if self.enable_logging:
print("Listener loop ended.")
# shutdown the dart side so it knows things have ended gracefully
self.shutdown()
@cocotb.coroutine
async def listen_to_signal(self, signal_name, signal):
"""
Sets up a listener for `signal` named `signal_name` so that changes can be
communicated over to the ROHD simulator.
"""
self.output_map[signal_name] = signal
# at the start, send everything for initial values
while True:
if self.enable_logging:
print(f"Sending update {signal_name}={str(signal.value)}")
self._sock_send(f"UPDATE:{signal_name}={str(signal.value)}")
await cocotb.triggers.Edge(signal)
@cocotb.coroutine
async def _tick(self, new_time: int): # ns
self.mid_tick = True
if self.enable_logging:
print(f">>> current time: {self.time}, new_time: {new_time}")
if self.time >= self.clk_ratio * (new_time + 1):
self._error("Too many ticks or time incorrect!")
elif (self.time - self.time % self.clk_ratio) == new_time * self.clk_ratio:
# another tick in the same timestamp!
# https://docs.cocotb.org/en/stable/triggers.html
await Timer(1, units="step")
self.time += 1
else:
await Timer(new_time * self.clk_ratio - self.time, units=self.clk_units)
self.time = new_time * self.clk_ratio # clk_units
# this code sends all outputs every tick
# for signal_name, signal in self.output_map.items():
# if self.enable_logging: print(f'Sending update {signal_name}={str(signal.value)}')
# self.sock_send(f"UPDATE:{signal_name}={str(signal.value)}")
self._sock_send("TICK_COMPLETE")
self.mid_tick = False