-
Notifications
You must be signed in to change notification settings - Fork 3
/
uart_monitor.py
305 lines (285 loc) · 11.8 KB
/
uart_monitor.py
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
294
295
296
297
298
299
300
301
302
303
304
305
from uvm.macros import uvm_component_utils, uvm_fatal, uvm_info, uvm_error, uvm_warning
from uvm.comps.uvm_monitor import UVMMonitor
from uvm.tlm1.uvm_analysis_port import UVMAnalysisPort
from uvm.base.uvm_config_db import UVMConfigDb
from cocotb.triggers import (
Timer,
ClockCycles,
FallingEdge,
Event,
RisingEdge,
Combine,
First,
)
from uart_item.uart_item import uart_item
from uart_item.uart_interrupt import uart_interrupt
from uvm.base.uvm_object_globals import UVM_HIGH, UVM_LOW, UVM_MEDIUM
import cocotb
import math
from EF_UVM.ip_env.ip_agent.ip_monitor import ip_monitor
class uart_monitor(ip_monitor):
def __init__(self, name="uart_monitor", parent=None):
super().__init__(name, parent)
self.tx_received = Event("tx_received")
self.rx_received = Event("rx_received")
async def run_phase(self, phase):
sample_tx = await cocotb.start(self.sample_tx())
sample_rx = await cocotb.start(self.sample_rx())
timeout_thread = await cocotb.start(self.watch_rx_timeout())
break_line_thread = await cocotb.start(self.watch_line_break())
await self.get_clk_period()
await Combine(sample_tx, sample_rx)
async def get_clk_period(self):
await RisingEdge(self.vif.PCLK)
await RisingEdge(self.vif.PCLK)
time0 = cocotb.utils.get_sim_time(units="ns")
await RisingEdge(self.vif.PCLK)
# await ClockCycles(self.vif.PCLK, 1)
time1 = cocotb.utils.get_sim_time(units="ns")
self.clk_period = time1 - time0
uvm_info(self.tag, f"clock period = {self.clk_period}", UVM_MEDIUM)
return
async def sample_tx(self):
while True:
tr = uart_item.type_id.create("tr", self)
# wait for a char
tr.char, tr.parity, tr.word_length = await self.get_char()
tr.direction = uart_item.TX
uvm_info(
self.tag,
"sampled uart TX transaction: " + tr.convert2string(),
UVM_HIGH,
)
self.monitor_port.write(tr)
self.tx_received.set()
async def sample_rx(self):
while True:
tr = uart_item.type_id.create("tr", self)
# wait for a char
tr.char, tr.parity, tr.word_length = await self.get_char(uart_item.RX)
if tr.char == tr.parity == tr.word_length == "None":
uvm_warning(
self.tag, "ignore char sampled as the stop bit isn't detected"
)
continue
tr.direction = uart_item.RX
uvm_info(
self.tag,
"sampled uart RX transaction: " + tr.convert2string(),
UVM_MEDIUM,
)
self.monitor_port.write(tr)
self.rx_received.set()
self.check_parity(tr.char, tr.parity)
async def get_char(self, direction=uart_item.TX):
if direction == uart_item.TX:
num_cyc_bit, word_length = await self.start_of_tx()
signal = self.vif.TX
done_signal = self.vif.tx_done
else:
num_cyc_bit, word_length = await self.start_of_rx()
signal = self.vif.RX
done_signal = self.vif.rx_done
char = ""
parity = "None"
for i in range(word_length):
new_bit = await self.glitch_free_sample(signal, num_cyc_bit, 8)
char = new_bit + char
uvm_info(
self.tag, f"char[{i}] = {new_bit} length = {word_length}", UVM_HIGH
)
# get parity bit
if self.is_parity_exists():
parity = await self.glitch_free_sample(signal, num_cyc_bit, 8)
uvm_info(
self.tag, f"parity bit = {parity} length = {word_length}", UVM_HIGH
)
# stop bit
stop_bit = await self.glitch_free_sample(
signal, num_cyc_bit, 8, last_bit=not self.is_stop_bit_exists()
)
if stop_bit != "1":
uvm_warning(self.tag, f"stop bit expected but got {stop_bit}")
if direction == uart_item.RX:
self.frame_error()
return "None", "None", "None"
# await ClockCycles(self.vif.PCLK, num_cyc_bit - 2) # to even the /2 in the start of tx
# mimic stop bit
if self.is_stop_bit_exists():
stop_bit = await self.glitch_free_sample(
signal, num_cyc_bit, 8, last_bit=True
)
if stop_bit != "1":
uvm_warning(self.tag, f"stop bit expected but got {stop_bit}")
if direction == uart_item.RX:
self.frame_error()
return "None", "None", "None"
if direction == uart_item.TX:
uvm_info(
self.tag,
f"waiting for {'tx' if direction == uart_item.TX else 'rx'}_done",
UVM_MEDIUM,
)
# wait for done from the model
# check the monitor waited for the done less than num_cyc_bit_tx / 2 if not there is an issue in the protocol
wait_done_time = cocotb.utils.get_sim_time(units="ns")
while True:
await RisingEdge(
done_signal
) # for the fifo of the model to get the same timing as the fifo in rtl
await Timer(1, "ns")
if done_signal.value == 1: # to make sure it's no latch
await FallingEdge(
done_signal
) # for the fifo of the model to get the same timing as the fifo in rtl
uvm_info(
self.tag,
f"found {'tx' if direction == uart_item.TX else 'rx'}_done",
UVM_HIGH,
)
await Timer(1, "ns")
break
done_time = cocotb.utils.get_sim_time(units="ns")
uvm_info(
self.tag,
f"waited for done {(done_time - wait_done_time)/ self.clk_period} cycles num_cyc {num_cyc_bit}",
UVM_HIGH,
)
# check the monitor waited for the done less than num_cyc_bit_tx / 2 if not there is an issue in the protocol
if (done_time - wait_done_time) / self.clk_period > num_cyc_bit / 2:
uvm_error(
self.tag,
f"stop bit checker waited for the done more than {num_cyc_bit / 2} < {(done_time - wait_done_time)/ self.clk_period} cycles",
)
return int(char, 2), parity, word_length
async def start_of_tx(self):
while True:
await FallingEdge(self.vif.TX)
uvm_info(self.tag, "start of TX", UVM_HIGH)
num_cyc_bit_tx = self.get_bit_n_cyc()
word_length_tx = self.get_n_bits()
await Timer(1, units="ns")
if self.vif.TX.value == 1:
continue
await ClockCycles(self.vif.PCLK, num_cyc_bit_tx)
break
return num_cyc_bit_tx, word_length_tx
async def start_of_rx(self):
while True:
await FallingEdge(self.vif.RX)
uvm_info(self.tag, "start of RX", UVM_HIGH)
num_cyc_bit_rx = self.get_bit_n_cyc()
word_length_rx = self.get_n_bits()
await Timer(1, units="ns")
if self.vif.RX.value == 1:
continue
await ClockCycles(self.vif.PCLK, num_cyc_bit_rx)
break
return num_cyc_bit_rx, word_length_rx
def get_bit_n_cyc(self):
prescale = self.regs.read_reg_value("PR")
uvm_info(self.tag, "prescale = " + str(prescale), UVM_HIGH)
return (prescale + 1) * 8
def get_n_bits(self):
word_length = self.regs.read_reg_value("CFG") & 0b1111
uvm_info(self.tag, "Data word length = " + str(word_length), UVM_HIGH)
return word_length
def is_parity_exists(self):
parity = (self.regs.read_reg_value("CFG") >> 5) & 0b111
return parity != 0
def is_stop_bit_exists(self):
stop_bit = (self.regs.read_reg_value("CFG") >> 4) & 0x1
return stop_bit
async def watch_rx_timeout(self):
# wait for uart enable
while True:
uart_enabled = self.regs.read_reg_value("CTRL") & 1
if uart_enabled:
break
await ClockCycles(self.vif.PCLK, 1)
while True:
timeout = 1 + ((self.regs.read_reg_value("CFG") >> 8) & 0b111111)
bit_rate = self.get_bit_n_cyc() * self.clk_period
total_time_ns = int(timeout * bit_rate)
uvm_info(
self.tag,
f"timeout = {timeout} bit_rate = {bit_rate} mult = {bit_rate * timeout}",
UVM_MEDIUM,
)
time_out = Timer(total_time_ns, "ns") # time out condition
new_rx = self.rx_received.wait() # recieved new rx
await First(time_out, new_rx)
if self.rx_received.is_set():
self.rx_received.clear()
else:
# if it reaches here it means it timed out
irq = uart_interrupt.type_id.create("tr_irq", self)
irq.rx_timeout = 1
# uvm_info(self.tag, f"timed out for {timeout}", UVM_HIGH)
self.monitor_irq_port.write(irq)
async def watch_line_break(self):
while True:
await FallingEdge(self.vif.RX)
bit_num_cycles = self.get_bit_n_cyc()
await ClockCycles(self.vif.PCLK, math.floor(bit_num_cycles / 2))
for _ in range(11):
await ClockCycles(self.vif.PCLK, bit_num_cycles)
if self.vif.RX.value == 1:
break
if self.vif.RX.value == 1:
continue
irq = uart_interrupt.type_id.create("tr_irq", self)
irq.rx_break_line = 1
uvm_info(self.tag, "break line", UVM_HIGH)
self.monitor_irq_port.write(irq)
def check_parity(self, char, parity):
tr = uart_item.type_id.create("tr", self)
tr.char = char
parity_type = (self.regs.read_reg_value("CFG") >> 5) & 0x7
tr.calculate_parity(parity_type)
if tr.parity == parity:
return
else:
irq = uart_interrupt.type_id.create("tr_irq", self)
irq.rx_wrong_parity = 1
uvm_info(
self.tag,
f"wrong parity for char = {bin(char)} parity_type = {parity_type} parity = {parity} expected = {tr.parity}",
UVM_MEDIUM,
)
self.monitor_irq_port.write(irq)
def frame_error(self):
irq = uart_interrupt.type_id.create("tr_irq", self)
irq.rx_frame_error = 1
self.monitor_irq_port.write(irq)
async def glitch_free_sample(self, signal, num_cyc, sample_num, last_bit=False):
base_value = num_cyc // sample_num
lst = [base_value] * sample_num
# Distribute the remainder across the first 'remainder' elements of the list by adding 1
remainder = num_cyc % sample_num
ones = 0
zeros = 0
for i in range(remainder):
lst[i] += 1
if last_bit:
lst.pop() # in the last bit leave time to wait for tx_done for tx only to sync the fifos
# Count the number of ones and zeros
for cyc in lst:
val = signal.value.binstr
if val == "1":
ones += 1
elif val == "0":
zeros += 1
await ClockCycles(self.vif.PCLK, cyc)
uvm_info(
self.tag,
f"finish glitch_free_sample ones = {ones} zeros = {zeros} sample rate = {sample_num} num_cyc = {num_cyc} list = {lst}",
UVM_HIGH,
)
if ones > zeros:
return "1"
elif ones < zeros:
return "0"
else:
return "X"
uvm_component_utils(uart_monitor)