Skip to content

Commit 74ddd94

Browse files
committed
core: Update code to LiteXModule and current coding style.
1 parent 976656b commit 74ddd94

File tree

1 file changed

+57
-38
lines changed

1 file changed

+57
-38
lines changed

litescope/core.py

+57-38
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,47 @@
11
#
22
# This file is part of LiteScope.
33
#
4-
# Copyright (c) 2016-2019 Florent Kermarrec <[email protected]>
4+
# Copyright (c) 2016-2024 Florent Kermarrec <[email protected]>
55
# Copyright (c) 2018 bunnie <[email protected]>
66
# Copyright (c) 2016 Tim 'mithro' Ansell <[email protected]>
77
# SPDX-License-Identifier: BSD-2-Clause
88

99
from migen import *
1010
from migen.genlib.cdc import MultiReg, PulseSynchronizer
1111

12+
from litex.gen import *
1213
from litex.gen.genlib.misc import WaitTimer
14+
1315
from litex.build.tools import write_to_file
1416

1517
from litex.soc.interconnect.csr import *
16-
from litex.soc.cores.gpio import GPIOInOut
18+
19+
from litex.soc.cores.gpio import GPIOInOut
1720
from litex.soc.interconnect import stream
1821

1922
# LiteScope IO -------------------------------------------------------------------------------------
2023

21-
class LiteScopeIO(Module, AutoCSR):
24+
class LiteScopeIO(LiteXModule):
2225
def __init__(self, data_width):
2326
self.data_width = data_width
2427
self.input = Signal(data_width)
2528
self.output = Signal(data_width)
2629

2730
# # #
2831

29-
self.submodules.gpio = GPIOInOut(self.input, self.output)
32+
self.gpio = GPIOInOut(self.input, self.output)
3033

3134
def get_csrs(self):
3235
return self.gpio.get_csrs()
3336

34-
# LiteScope Analyzer -------------------------------------------------------------------------------
37+
# LiteScope Analyzer Constants/Layouts -------------------------------------------------------------
3538

3639
def core_layout(data_width):
3740
return [("data", data_width), ("hit", 1)]
3841

42+
# LiteScope Analyzer Trigger -----------------------------------------------------------------------
3943

40-
class _Trigger(Module, AutoCSR):
44+
class _Trigger(LiteXModule):
4145
def __init__(self, data_width, depth=16):
4246
self.sink = sink = stream.Endpoint(core_layout(data_width))
4347
self.source = source = stream.Endpoint(core_layout(data_width))
@@ -52,17 +56,17 @@ def __init__(self, data_width, depth=16):
5256

5357
# # #
5458

55-
# Control re-synchronization
59+
# Control re-synchronization.
5660
enable = Signal()
5761
enable_d = Signal()
5862
self.specials += MultiReg(self.enable.storage, enable, "scope")
5963
self.sync.scope += enable_d.eq(enable)
6064

61-
# Status re-synchronization
65+
# Status re-synchronization.
6266
done = Signal()
6367
self.specials += MultiReg(done, self.done.status)
6468

65-
# Memory and configuration
69+
# Memory and configuration.
6670
mem = stream.AsyncFIFO([("mask", data_width), ("value", data_width)], depth)
6771
mem = ClockDomainsRenamer({"write": "sys", "read": "scope"})(mem)
6872
self.submodules += mem
@@ -73,7 +77,7 @@ def __init__(self, data_width, depth=16):
7377
self.mem_full.status.eq(~mem.sink.ready)
7478
]
7579

76-
# Hit and memory read/flush
80+
# Hit and memory read/flush.
7781
hit = Signal()
7882
flush = WaitTimer(2*depth)
7983
flush = ClockDomainsRenamer("scope")(flush)
@@ -84,15 +88,17 @@ def __init__(self, data_width, depth=16):
8488
mem.source.ready.eq((enable & hit) | ~flush.done),
8589
]
8690

87-
# Output
91+
# Output.
8892
self.comb += [
8993
sink.connect(source),
90-
# Done when all triggers have been consumed
94+
# Done when all triggers have been consumed.
9195
done.eq(~mem.source.valid),
9296
source.hit.eq(done)
9397
]
9498

95-
class _SubSampler(Module, AutoCSR):
99+
# LiteScope Analyzer SubSampler --------------------------------------------------------------------
100+
101+
class _SubSampler(LiteXModule):
96102
def __init__(self, data_width):
97103
self.sink = sink = stream.Endpoint(core_layout(data_width))
98104
self.source = source = stream.Endpoint(core_layout(data_width))
@@ -121,8 +127,9 @@ def __init__(self, data_width):
121127
source.valid.eq(sink.valid & done)
122128
]
123129

130+
# LiteScope Analyzer Mux ---------------------------------------------------------------------------
124131

125-
class _Mux(Module, AutoCSR):
132+
class _Mux(LiteXModule):
126133
def __init__(self, data_width, n):
127134
self.sinks = sinks = [stream.Endpoint(core_layout(data_width)) for i in range(n)]
128135
self.source = source = stream.Endpoint(core_layout(data_width))
@@ -139,8 +146,9 @@ def __init__(self, data_width, n):
139146
cases[i] = sinks[i].connect(source)
140147
self.comb += Case(value, cases)
141148

149+
# LiteScope Analyzer Storage -----------------------------------------------------------------------
142150

143-
class _Storage(Module, AutoCSR):
151+
class _Storage(LiteXModule):
144152
def __init__(self, data_width, depth):
145153
self.sink = sink = stream.Endpoint(core_layout(data_width))
146154

@@ -156,7 +164,7 @@ def __init__(self, data_width, depth):
156164

157165
# # #
158166

159-
# Control re-synchronization
167+
# Control re-synchronization.
160168
enable = Signal()
161169
enable_d = Signal()
162170
self.specials += MultiReg(self.enable.storage, enable, "scope")
@@ -167,28 +175,27 @@ def __init__(self, data_width, depth):
167175
self.specials += MultiReg(self.length.storage, length, "scope")
168176
self.specials += MultiReg(self.offset.storage, offset, "scope")
169177

170-
# Status re-synchronization
178+
# Status re-synchronization.
171179
done = Signal()
172180
level = Signal().like(self.mem_level.status)
173181
self.specials += MultiReg(done, self.done.status)
174182
self.specials += MultiReg(level, self.mem_level.status)
175183

176-
# Memory
184+
# Memory.
177185
mem = stream.SyncFIFO([("data", data_width)], depth, buffered=True)
178186
mem = ClockDomainsRenamer("scope")(mem)
179187
cdc = stream.AsyncFIFO([("data", data_width)], 4)
180-
cdc = ClockDomainsRenamer(
181-
{"write": "scope", "read": "sys"})(cdc)
188+
cdc = ClockDomainsRenamer({"write": "scope", "read": "sys"})(cdc)
182189
self.submodules += mem, cdc
183190

184191
self.comb += level.eq(mem.level)
185192

186-
# Flush
193+
# Flush.
187194
mem_flush = WaitTimer(depth)
188195
mem_flush = ClockDomainsRenamer("scope")(mem_flush)
189196
self.submodules += mem_flush
190197

191-
# FSM
198+
# FSM.
192199
fsm = FSM(reset_state="IDLE")
193200
fsm = ClockDomainsRenamer("scope")(fsm)
194201
self.submodules += fsm
@@ -222,7 +229,7 @@ def __init__(self, data_width, depth):
222229
)
223230
)
224231

225-
# Memory read
232+
# Memory read.
226233
read_source = stream.Endpoint([("data", data_width)])
227234
if data_width > read_width:
228235
pad_bits = - data_width % read_width
@@ -238,9 +245,16 @@ def __init__(self, data_width, depth):
238245
self.mem_data.status.eq(read_source.data)
239246
]
240247

248+
# LiteScope Analyzer -------------------------------------------------------------------------------
241249

242-
class LiteScopeAnalyzer(Module, AutoCSR):
243-
def __init__(self, groups, depth, samplerate=1e12, clock_domain="sys", trigger_depth=16, register=False, csr_csv="analyzer.csv"):
250+
class LiteScopeAnalyzer(LiteXModule):
251+
def __init__(self, groups, depth,
252+
samplerate = 1e12,
253+
clock_domain = "sys",
254+
trigger_depth = 16,
255+
register = False,
256+
csr_csv = "analyzer.csv",
257+
):
244258
self.groups = groups = self.format_groups(groups)
245259
self.depth = depth
246260
self.samplerate = int(samplerate)
@@ -251,12 +265,13 @@ def __init__(self, groups, depth, samplerate=1e12, clock_domain="sys", trigger_d
251265

252266
# # #
253267

254-
# Create scope clock domain
255-
self.clock_domains.cd_scope = ClockDomain()
268+
# Create scope clock domain.
269+
self.cd_scope = ClockDomain()
256270
self.comb += self.cd_scope.clk.eq(ClockSignal(clock_domain))
257271

258-
# Mux
259-
self.submodules.mux = _Mux(data_width, len(groups))
272+
# Mux.
273+
# ----
274+
self.mux = _Mux(data_width, len(groups))
260275
sd = getattr(self.sync, clock_domain)
261276
for i, signals in groups.items():
262277
s = Cat(signals)
@@ -269,19 +284,23 @@ def __init__(self, groups, depth, samplerate=1e12, clock_domain="sys", trigger_d
269284
self.mux.sinks[i].data.eq(s)
270285
]
271286

272-
# Frontend
273-
self.submodules.trigger = _Trigger(data_width, depth=trigger_depth)
274-
self.submodules.subsampler = _SubSampler(data_width)
287+
# Frontend.
288+
# ---------
289+
self.trigger = _Trigger(data_width, depth=trigger_depth)
290+
self.subsampler = _SubSampler(data_width)
275291

276-
# Storage
277-
self.submodules.storage = _Storage(data_width, depth)
292+
# Storage.
293+
# --------
294+
self.storage = _Storage(data_width, depth)
278295

279-
# Pipeline
280-
self.submodules.pipeline = stream.Pipeline(
281-
self.mux.source,
296+
# Pipeline: Mux -> Trigger -> Subsampler -> Storage.
297+
# --------------------------------------------------
298+
self.pipeline = stream.Pipeline(
299+
self.mux,
282300
self.trigger,
283301
self.subsampler,
284-
self.storage.sink)
302+
self.storage,
303+
)
285304

286305
def format_groups(self, groups):
287306
if not isinstance(groups, dict):

0 commit comments

Comments
 (0)