vj-sim 0.3.0

Netlist, scheduler and process model of the vj SystemVerilog simulator.
Documentation
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! The declaration side of the model: what the SystemVerilog source says,
//! before any instance exists (IEEE 1800-2023 19.3, 19.5).
//!
//! A [`CoverpointDecl`] is produced once per `covergroup` and instantiated once
//! per `new`, with that instance's constructor arguments (see
//! [`super::coverpoint::Coverpoint::new`]).

use super::value::{Expr, TypeInfo};

/// `bins`, `ignore_bins` or `illegal_bins` (19.5, 19.5.5, 19.5.6).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BinKind {
    Normal,
    Ignore,
    Illegal,
}

/// The bracket after the bin name.
#[derive(Clone, Debug, PartialEq)]
pub enum ArrayKind {
    /// `bins b`: one bin.
    None,
    /// `bins b[]`: one bin per value (per interval for reals).
    Open,
    /// `bins b[N]`: `N` bins, `N` evaluated at construction.
    Fixed(Expr),
}

/// One end of a `[lo:hi]` range; `$` is the type minimum/maximum.
#[derive(Clone, Debug, PartialEq)]
pub enum Bound {
    Expr(Expr),
    Dollar,
}

/// One element of a `covergroup_range_list` (Syntax 19-2).
#[derive(Clone, Debug, PartialEq)]
pub enum RangeItem {
    /// A single value (or, for a set expression's elements, one element).
    Single(Expr),
    /// `[lo:hi]`, `[$:hi]`, `[lo:$]`.
    Range(Bound, Bound),
    /// `[center +/- tol]` (`relative = false`) or `[center +%- tol]`
    /// (`relative = true`), 11.4.13.
    Tolerance {
        center: Expr,
        tol: Expr,
        relative: bool,
    },
}

/// `repeat_range`: `n` or `lo : hi` (Syntax 19-3).
#[derive(Clone, Debug, PartialEq)]
pub struct RepeatRange {
    pub lo: Expr,
    pub hi: Option<Expr>,
}

impl RepeatRange {
    pub fn exactly(n: i128) -> RepeatRange {
        RepeatRange {
            lo: Expr::int(n),
            hi: None,
        }
    }

    pub fn between(lo: i128, hi: i128) -> RepeatRange {
        RepeatRange {
            lo: Expr::int(lo),
            hi: Some(Expr::int(hi)),
        }
    }
}

/// The repetition suffix of a `trans_item`.
#[derive(Clone, Debug, PartialEq)]
pub enum TransRep {
    Once,
    /// `[* repeat_range ]`
    Consecutive(RepeatRange),
    /// `[-> repeat_range ]`
    Goto(RepeatRange),
    /// `[= repeat_range ]`
    NonConsecutive(RepeatRange),
}

/// A `trans_range_list`: a value range list with its repetition.
#[derive(Clone, Debug, PartialEq)]
pub struct TransItem {
    pub values: Vec<RangeItem>,
    pub rep: TransRep,
}

impl TransItem {
    pub fn new(values: Vec<RangeItem>) -> TransItem {
        TransItem {
            values,
            rep: TransRep::Once,
        }
    }

    pub fn rep(mut self, rep: TransRep) -> TransItem {
        self.rep = rep;
        self
    }
}

/// A `trans_set`: items joined by `=>`.
pub type TransSet = Vec<TransItem>;

/// What a bin matches.
#[derive(Clone, Debug, PartialEq)]
pub enum BinBody {
    /// `( trans_set ) { , ( trans_set ) }` (19.5.2).
    Trans(Vec<TransSet>),
    /// `default sequence` (19.5.2).
    DefaultSeq,
    /// `{ range list } [with (expr)]`. A `with` kernel receives the
    /// constructor arguments followed by `item`. The coverpoint-name form
    /// `cp with (expr)` is `[$:$]` plus the clause.
    Ranges {
        items: Vec<RangeItem>,
        with: Option<Expr>,
    },
    /// A `set_covergroup_expression`: evaluates to a [`super::value::Val::List`].
    Set(Expr),
    /// `default` (19.5): the values of no other bin.
    Default,
}

#[derive(Clone, Debug, PartialEq)]
pub struct BinDecl {
    pub name: String,
    pub kind: BinKind,
    pub array: ArrayKind,
    pub wildcard: bool,
    /// Per-bin `iff` (sample-time kernel).
    pub iff: Option<Expr>,
    pub body: BinBody,
}

impl BinDecl {
    /// A plain `bins <name> = { items }`.
    pub fn ranges(name: &str, items: Vec<RangeItem>) -> BinDecl {
        BinDecl {
            name: name.to_string(),
            kind: BinKind::Normal,
            array: ArrayKind::None,
            wildcard: false,
            iff: None,
            body: BinBody::Ranges { items, with: None },
        }
    }

    pub fn kind(mut self, kind: BinKind) -> BinDecl {
        self.kind = kind;
        self
    }

    pub fn array(mut self, array: ArrayKind) -> BinDecl {
        self.array = array;
        self
    }

    pub fn wildcard(mut self) -> BinDecl {
        self.wildcard = true;
        self
    }

    pub fn iff(mut self, iff: Expr) -> BinDecl {
        self.iff = Some(iff);
        self
    }

    pub fn with(mut self, with: Expr) -> BinDecl {
        if let BinBody::Ranges { with: w, .. } = &mut self.body {
            *w = Some(with);
        }
        self
    }

    /// A transition bin `bins <name> = (set) , (set) ...`.
    pub fn trans(name: &str, sets: Vec<TransSet>) -> BinDecl {
        BinDecl {
            name: name.to_string(),
            kind: BinKind::Normal,
            array: ArrayKind::None,
            wildcard: false,
            iff: None,
            body: BinBody::Trans(sets),
        }
    }

    /// `bins <name> = default sequence`.
    pub fn default_seq(name: &str) -> BinDecl {
        BinDecl {
            name: name.to_string(),
            kind: BinKind::Normal,
            array: ArrayKind::None,
            wildcard: false,
            iff: None,
            body: BinBody::DefaultSeq,
        }
    }

    pub fn default_bin(name: &str) -> BinDecl {
        BinDecl {
            name: name.to_string(),
            kind: BinKind::Normal,
            array: ArrayKind::None,
            wildcard: false,
            iff: None,
            body: BinBody::Default,
        }
    }
}

/// The `matches` clause of a `with` select or a set select (19.6.1.2).
#[derive(Clone, Debug, PartialEq)]
pub enum Matches {
    /// `matches n`: at least `n` satisfying value tuples (a positive integer,
    /// evaluated at construction). Omitted means `Count(1)`.
    Count(Expr),
    /// `matches $`: every value tuple must satisfy.
    All,
}

impl Matches {
    /// The default policy of 19.6.1.2: a single value tuple selects.
    pub fn one() -> Matches {
        Matches::Count(Expr::int(1))
    }
}

/// A `select_expression` (Syntax 19-4).
#[derive(Clone, Debug, PartialEq)]
pub enum SelectExpr {
    /// `binsof(cp)` / `binsof(cp.bin)`, optionally `intersect { ranges }`.
    /// `bin` names one bin, or a whole `b[]` / `b[N]` array by its base name.
    Binsof {
        cp: String,
        bin: Option<String>,
        intersect: Option<Vec<RangeItem>>,
    },
    Not(Box<SelectExpr>),
    And(Box<SelectExpr>, Box<SelectExpr>),
    Or(Box<SelectExpr>, Box<SelectExpr>),
    /// `sel with (expr) [matches ...]`. The kernel receives the constructor
    /// arguments followed by one value per cross item, in item order.
    With {
        sel: Box<SelectExpr>,
        expr: Expr,
        matches: Matches,
    },
    /// The enclosing cross's own identifier: every bin tuple.
    All,
    /// `cross_set_expression [matches ...]`: evaluates to a
    /// [`super::value::Val::List`] of tuples (each a `Val::List` with one value
    /// per cross item), 19.6.1.4.
    Set {
        expr: Expr,
        matches: Matches,
    },
}

/// A `bins`, `ignore_bins` or `illegal_bins` selection of a cross (19.6.1,
/// 19.6.2, 19.6.3).
#[derive(Clone, Debug, PartialEq)]
pub struct CrossBinDecl {
    pub name: String,
    pub kind: BinKind,
    pub select: SelectExpr,
    /// Per-bin `iff` (sample-time kernel).
    pub iff: Option<Expr>,
}

impl CrossBinDecl {
    pub fn new(name: &str, kind: BinKind, select: SelectExpr) -> CrossBinDecl {
        CrossBinDecl {
            name: name.to_string(),
            kind,
            select,
            iff: None,
        }
    }

    pub fn iff(mut self, iff: Expr) -> CrossBinDecl {
        self.iff = Some(iff);
        self
    }
}

/// A `cross` (19.6).
#[derive(Clone, Debug, PartialEq)]
pub struct CrossDecl {
    pub name: String,
    /// The crossed coverpoints, by name, in declaration order. A variable
    /// operand is represented by the implicit coverpoint the frontend adds.
    pub items: Vec<String>,
    /// Cross-level `iff` (sample-time kernel).
    pub iff: Option<Expr>,
    pub bins: Vec<CrossBinDecl>,
    pub options: Vec<(String, Expr)>,
    pub type_options: Vec<(String, Expr)>,
}

impl CrossDecl {
    pub fn new(name: &str, items: &[&str]) -> CrossDecl {
        CrossDecl {
            name: name.to_string(),
            items: items.iter().map(|s| s.to_string()).collect(),
            iff: None,
            bins: Vec::new(),
            options: Vec::new(),
            type_options: Vec::new(),
        }
    }

    pub fn bin(mut self, bin: CrossBinDecl) -> CrossDecl {
        self.bins.push(bin);
        self
    }
}

/// A `coverpoint` (explicit, or the implicit one of a cross operand).
#[derive(Clone, Debug, PartialEq)]
pub struct CoverpointDecl {
    pub name: String,
    pub ty: TypeInfo,
    /// Sample-time kernel yielding the coverpoint value.
    pub expr: Expr,
    /// Coverpoint-level `iff` (sample-time kernel).
    pub iff: Option<Expr>,
    pub bins: Vec<BinDecl>,
    /// For an enum-typed coverpoint: every enumerator `(name, value)` in
    /// declaration order (auto bins are named after them, 19.5.3).
    pub enum_values: Option<Vec<(String, i128)>>,
    /// `option.<name> = <expr>` assignments inside the coverpoint, in order.
    pub options: Vec<(String, Expr)>,
    /// `type_option.<name> = <expr>` assignments inside the coverpoint.
    pub type_options: Vec<(String, Expr)>,
}

/// A `covergroup` declaration (19.3): its options and its coverage items.
#[derive(Clone, Debug, PartialEq)]
pub struct CovGroupDecl {
    pub name: String,
    /// `option.<name> = <expr>` assignments of the covergroup, in order.
    /// Evaluated per instance with the constructor arguments (19.7).
    pub options: Vec<(String, Expr)>,
    /// `type_option.<name> = <expr>` assignments; constant expressions
    /// (19.7.1), evaluated once when the type is defined.
    pub type_options: Vec<(String, Expr)>,
    pub coverpoints: Vec<CoverpointDecl>,
    pub crosses: Vec<CrossDecl>,
}

impl CovGroupDecl {
    pub fn new(name: &str) -> CovGroupDecl {
        CovGroupDecl {
            name: name.to_string(),
            options: Vec::new(),
            type_options: Vec::new(),
            coverpoints: Vec::new(),
            crosses: Vec::new(),
        }
    }

    pub fn coverpoint(mut self, cp: CoverpointDecl) -> CovGroupDecl {
        self.coverpoints.push(cp);
        self
    }

    pub fn cross(mut self, x: CrossDecl) -> CovGroupDecl {
        self.crosses.push(x);
        self
    }

    pub fn option(mut self, name: &str, v: Expr) -> CovGroupDecl {
        self.options.push((name.to_string(), v));
        self
    }

    pub fn type_option(mut self, name: &str, v: Expr) -> CovGroupDecl {
        self.type_options.push((name.to_string(), v));
        self
    }
}

impl CoverpointDecl {
    pub fn new(name: &str, ty: TypeInfo, expr: Expr) -> CoverpointDecl {
        CoverpointDecl {
            name: name.to_string(),
            ty,
            expr,
            iff: None,
            bins: Vec::new(),
            enum_values: None,
            options: Vec::new(),
            type_options: Vec::new(),
        }
    }

    pub fn bin(mut self, bin: BinDecl) -> CoverpointDecl {
        self.bins.push(bin);
        self
    }
}