celox-testbench 0.4.1

Source-independent testbench bytecode contracts for Celox
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
//! Source-independent testbench bytecode contracts.
//!
//! Frontends translate source-language operators into this vocabulary before
//! execution. No Veryl AST, parser ID, simulator backend, or runtime handle is
//! part of the bytecode representation.

use num_bigint::BigUint;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

mod format;
mod vm;
pub use format::{DisplayFormatArg, format_display_arg};
pub use vm::{CompiledExpr, TestbenchValue};

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct StateLocation<A> {
    pub address: A,
    pub byte_offset: usize,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TestbenchOperator {
    Add,
    Sub,
    Mul,
    Div,
    Rem,
    Pow,
    BitAnd,
    BitOr,
    BitXor,
    BitXnor,
    BitNand,
    BitNor,
    LogicShiftL,
    LogicShiftR,
    ArithShiftL,
    ArithShiftR,
    Eq,
    EqWildcard,
    Ne,
    NeWildcard,
    Less,
    LessEq,
    Greater,
    GreaterEq,
    LogicAnd,
    LogicOr,
    LogicNot,
    BitNot,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SourceLocation {
    pub file: String,
    pub line: u32,
    pub column: u32,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum AssertMessage<Argument> {
    Formatted {
        template: String,
        args: Vec<Argument>,
    },
    DynamicArgs(Vec<Argument>),
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ClockCount<Expression> {
    Static(u64),
    Dynamic(Expression),
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum LoopBound<Expression> {
    Static(usize),
    Dynamic {
        expr: Expression,
        width: usize,
        signed: bool,
    },
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ComponentParameterValue {
    Bits { words: Vec<u64>, width: u32 },
    String(String),
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ComponentConnection {
    pub port: String,
    pub group: Option<String>,
    pub member: Option<String>,
    pub input: bool,
    pub has_output: bool,
    pub is_clock: bool,
    pub is_reset: bool,
    pub width: u32,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TestbenchComponent {
    pub instance: String,
    pub component: String,
    pub params: Vec<(String, ComponentParameterValue)>,
    pub connections: Vec<ComponentConnection>,
    pub is_var_form: bool,
    pub source: Option<SourceLocation>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ComponentLibrary {
    pub export: String,
    pub type_name: String,
    pub path: PathBuf,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ComponentConnectionBinding<Event, Signal, Expression> {
    pub port: String,
    pub input: Option<Expression>,
    /// Direct mask source for a variable/select input connection. Runtime
    /// hosts evaluate `input` for the value and use this target to preserve
    /// the corresponding four-state mask.
    pub input_target: Option<TestbenchTarget<Signal, Expression>>,
    pub output: Option<TestbenchTarget<Signal, Expression>>,
    pub output_rtl_driven: bool,
    pub event: Option<Event>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct ComponentBinding<Event, Signal, Expression> {
    pub instance: String,
    pub connections: Vec<ComponentConnectionBinding<Event, Signal, Expression>>,
}

/// Source-independent testbench control program.
///
/// Frontends instantiate this with semantic state/event identities and
/// unbound expressions. Runtime binding instantiates the same contract with
/// backend event/signal handles and executable expressions.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum TestbenchStatement<Event, Signal, Expression, Argument, Target = Signal> {
    ClockNext {
        clock_event: Event,
        count: ClockCount<Expression>,
    },
    ResetAssert {
        reset_signal: Signal,
        reset_event: Option<Event>,
        clock_event: Event,
        duration: ClockCount<Expression>,
        assert_value: u8,
        deassert_value: u8,
    },
    Assert {
        expr: Expression,
        site_id: u32,
        continue_on_fail: bool,
        message: Option<AssertMessage<Argument>>,
        location: Option<SourceLocation>,
    },
    Display {
        message: Option<AssertMessage<Argument>>,
        newline: bool,
    },
    If {
        expr: Expression,
        then_block: Vec<Self>,
        else_block: Vec<Self>,
    },
    For {
        loop_var: Option<(Signal, usize, bool)>,
        start: LoopBound<Expression>,
        end: LoopBound<Expression>,
        inclusive: bool,
        step: usize,
        step_op: Option<TestbenchOperator>,
        reverse: bool,
        body: Vec<Self>,
    },
    Assign {
        dst: Target,
        expr: Expression,
    },
    RandomSeed {
        handle: String,
        value: Expression,
    },
    RandomGet {
        handle: String,
        width: u32,
        signed: bool,
        ret: Option<Target>,
    },
    RandomGetRange {
        handle: String,
        min: Expression,
        max: Expression,
        width: u32,
        signed: bool,
        ret: Option<Target>,
    },
    RandomGetSeed {
        handle: String,
        ret: Option<Target>,
    },
    ComponentMethod {
        instance: String,
        method: String,
        args: Vec<Argument>,
        ret: Option<Target>,
        ret_width: Option<u32>,
        ret_signed: bool,
        ret_strict: bool,
    },
    Break,
    Finish,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SemanticSignal<A> {
    pub address: A,
    pub width: usize,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TestbenchSelection<Expression> {
    pub offset: Expression,
    pub width: usize,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TestbenchTarget<Signal, Expression> {
    pub signal: Signal,
    pub selection: Option<TestbenchSelection<Expression>>,
    pub width: usize,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct SemanticArgument<A> {
    pub expr: ExprBytecode<StateLocation<A>>,
    pub width: usize,
    pub signed: bool,
    pub is_string: bool,
}

pub type SemanticStatement<A> = TestbenchStatement<
    A,
    SemanticSignal<A>,
    ExprBytecode<StateLocation<A>>,
    SemanticArgument<A>,
    TestbenchTarget<SemanticSignal<A>, ExprBytecode<StateLocation<A>>>,
>;
pub type SemanticComponentBinding<A> =
    ComponentBinding<A, SemanticSignal<A>, ExprBytecode<StateLocation<A>>>;

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct TestbenchProgram<A> {
    statements: Vec<SemanticStatement<A>>,
    random_seed: Option<u64>,
    components: Vec<TestbenchComponent>,
    component_libraries: Vec<ComponentLibrary>,
    component_file_base: Option<PathBuf>,
    component_bindings: Vec<SemanticComponentBinding<A>>,
}

impl<A> Default for TestbenchProgram<A> {
    fn default() -> Self {
        Self {
            statements: Vec::new(),
            random_seed: None,
            components: Vec::new(),
            component_libraries: Vec::new(),
            component_file_base: None,
            component_bindings: Vec::new(),
        }
    }
}

impl<A> TestbenchProgram<A> {
    pub fn new(statements: Vec<SemanticStatement<A>>) -> Self {
        Self {
            statements,
            random_seed: None,
            components: Vec::new(),
            component_libraries: Vec::new(),
            component_file_base: None,
            component_bindings: Vec::new(),
        }
    }

    pub fn with_random_seed(mut self, random_seed: u64) -> Self {
        self.random_seed = Some(random_seed);
        self
    }

    pub fn with_random_seed_option(mut self, random_seed: Option<u64>) -> Self {
        self.random_seed = random_seed;
        self
    }

    pub fn statements(&self) -> &[SemanticStatement<A>] {
        &self.statements
    }

    pub fn into_statements(self) -> Vec<SemanticStatement<A>> {
        self.statements
    }

    pub fn random_seed(&self) -> u64 {
        self.random_seed.unwrap_or_default()
    }

    pub fn configured_random_seed(&self) -> Option<u64> {
        self.random_seed
    }

    pub fn with_components(mut self, components: Vec<TestbenchComponent>) -> Self {
        self.components = components;
        self
    }

    pub fn with_component_runtime(
        mut self,
        libraries: Vec<ComponentLibrary>,
        file_base: Option<PathBuf>,
        bindings: Vec<SemanticComponentBinding<A>>,
    ) -> Self {
        self.component_libraries = libraries;
        self.component_file_base = file_base;
        self.component_bindings = bindings;
        self
    }

    pub fn components(&self) -> &[TestbenchComponent] {
        &self.components
    }

    pub fn component_libraries(&self) -> &[ComponentLibrary] {
        &self.component_libraries
    }

    pub fn component_file_base(&self) -> Option<&std::path::Path> {
        self.component_file_base.as_deref()
    }

    pub fn component_bindings(&self) -> &[SemanticComponentBinding<A>] {
        &self.component_bindings
    }

    pub fn is_empty(&self) -> bool {
        self.statements.is_empty()
    }
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum ExprOpcode<L = usize> {
    ConstU64(u64),
    ConstWide(BigUint),
    LoadU64 {
        location: L,
        byte_size: usize,
        mask: u64,
    },
    LoadWide {
        location: L,
        byte_size: usize,
        width: usize,
    },
    BinOp(TestbenchOperator),
    TypedBinOp {
        op: TestbenchOperator,
        lhs_width: usize,
        rhs_width: usize,
        result_width: usize,
        lhs_signed: bool,
        rhs_signed: bool,
    },
    TypedUnary {
        op: TestbenchOperator,
        operand_width: usize,
        result_width: usize,
    },
    Resize {
        source_width: usize,
        target_width: usize,
        signed: bool,
    },
    ConcatPart {
        part_width: usize,
        result_width: usize,
    },
    Ternary {
        then_len: usize,
        else_len: usize,
    },
    LoadIndexed {
        location: L,
        stride_bits: usize,
        base_bit_offset: usize,
        element_width: usize,
    },
    LoadBitSelect {
        location: L,
        base_byte_size: usize,
        select_width: usize,
    },
    StoreU64 {
        location: L,
        byte_size: usize,
    },
}

#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExprBytecode<L = usize> {
    ops: Vec<ExprOpcode<L>>,
}

impl<L> ExprBytecode<L> {
    pub fn new(ops: Vec<ExprOpcode<L>>) -> Self {
        Self { ops }
    }

    pub fn ops(&self) -> &[ExprOpcode<L>] {
        &self.ops
    }

    pub fn is_empty(&self) -> bool {
        self.ops.is_empty()
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct BindError<A> {
    pub address: A,
}

pub struct ExecutableArgument {
    pub expr: CompiledExpr,
    pub width: usize,
    pub signed: bool,
    pub is_string: bool,
}

pub type ExecutableAssertMessage = AssertMessage<ExecutableArgument>;
pub type ExecutableClockCount = ClockCount<CompiledExpr>;
pub type ExecutableLoopBound = LoopBound<CompiledExpr>;
pub type ExecutableStatement<Event, Signal> = TestbenchStatement<
    Event,
    Signal,
    CompiledExpr,
    ExecutableArgument,
    TestbenchTarget<Signal, CompiledExpr>,
>;
pub type ExecutableComponentBinding<Event, Signal> = ComponentBinding<Event, Signal, CompiledExpr>;

pub struct ExecutableTestbench<Event, Signal> {
    statements: Vec<ExecutableStatement<Event, Signal>>,
    random_seed: Option<u64>,
    components: Vec<TestbenchComponent>,
    component_libraries: Vec<ComponentLibrary>,
    component_file_base: Option<PathBuf>,
    component_bindings: Vec<ExecutableComponentBinding<Event, Signal>>,
}

impl<Event, Signal> ExecutableTestbench<Event, Signal> {
    pub fn new(statements: Vec<ExecutableStatement<Event, Signal>>, random_seed: u64) -> Self {
        Self::new_with_random_seed(statements, Some(random_seed))
    }

    pub fn new_with_random_seed(
        statements: Vec<ExecutableStatement<Event, Signal>>,
        random_seed: Option<u64>,
    ) -> Self {
        Self {
            statements,
            random_seed,
            components: Vec::new(),
            component_libraries: Vec::new(),
            component_file_base: None,
            component_bindings: Vec::new(),
        }
    }

    pub fn with_component_runtime(
        mut self,
        components: Vec<TestbenchComponent>,
        libraries: Vec<ComponentLibrary>,
        file_base: Option<PathBuf>,
        bindings: Vec<ExecutableComponentBinding<Event, Signal>>,
    ) -> Self {
        self.components = components;
        self.component_libraries = libraries;
        self.component_file_base = file_base;
        self.component_bindings = bindings;
        self
    }

    pub fn statements(&self) -> &[ExecutableStatement<Event, Signal>] {
        &self.statements
    }

    pub fn into_statements(self) -> Vec<ExecutableStatement<Event, Signal>> {
        self.statements
    }

    pub fn random_seed(&self) -> u64 {
        self.random_seed.unwrap_or_default()
    }

    pub fn configured_random_seed(&self) -> Option<u64> {
        self.random_seed
    }

    pub fn components(&self) -> &[TestbenchComponent] {
        &self.components
    }

    pub fn component_libraries(&self) -> &[ComponentLibrary] {
        &self.component_libraries
    }

    pub fn component_file_base(&self) -> Option<&std::path::Path> {
        self.component_file_base.as_deref()
    }

    pub fn component_bindings(&self) -> &[ExecutableComponentBinding<Event, Signal>] {
        &self.component_bindings
    }
}

impl<A> ExprBytecode<StateLocation<A>> {
    pub fn bind_with(
        self,
        mut resolve: impl FnMut(&A) -> Option<usize>,
    ) -> Result<ExprBytecode, BindError<A>> {
        let mut ops = Vec::with_capacity(self.ops.len());
        for op in self.ops {
            let bound = match op {
                ExprOpcode::ConstU64(value) => ExprOpcode::ConstU64(value),
                ExprOpcode::ConstWide(value) => ExprOpcode::ConstWide(value),
                ExprOpcode::LoadU64 {
                    location,
                    byte_size,
                    mask,
                } => ExprOpcode::LoadU64 {
                    location: bind_location(location, &mut resolve)?,
                    byte_size,
                    mask,
                },
                ExprOpcode::LoadWide {
                    location,
                    byte_size,
                    width,
                } => ExprOpcode::LoadWide {
                    location: bind_location(location, &mut resolve)?,
                    byte_size,
                    width,
                },
                ExprOpcode::BinOp(op) => ExprOpcode::BinOp(op),
                ExprOpcode::TypedBinOp {
                    op,
                    lhs_width,
                    rhs_width,
                    result_width,
                    lhs_signed,
                    rhs_signed,
                } => ExprOpcode::TypedBinOp {
                    op,
                    lhs_width,
                    rhs_width,
                    result_width,
                    lhs_signed,
                    rhs_signed,
                },
                ExprOpcode::TypedUnary {
                    op,
                    operand_width,
                    result_width,
                } => ExprOpcode::TypedUnary {
                    op,
                    operand_width,
                    result_width,
                },
                ExprOpcode::Resize {
                    source_width,
                    target_width,
                    signed,
                } => ExprOpcode::Resize {
                    source_width,
                    target_width,
                    signed,
                },
                ExprOpcode::ConcatPart {
                    part_width,
                    result_width,
                } => ExprOpcode::ConcatPart {
                    part_width,
                    result_width,
                },
                ExprOpcode::Ternary { then_len, else_len } => {
                    ExprOpcode::Ternary { then_len, else_len }
                }
                ExprOpcode::LoadIndexed {
                    location,
                    stride_bits,
                    base_bit_offset,
                    element_width,
                } => ExprOpcode::LoadIndexed {
                    location: bind_location(location, &mut resolve)?,
                    stride_bits,
                    base_bit_offset,
                    element_width,
                },
                ExprOpcode::LoadBitSelect {
                    location,
                    base_byte_size,
                    select_width,
                } => ExprOpcode::LoadBitSelect {
                    location: bind_location(location, &mut resolve)?,
                    base_byte_size,
                    select_width,
                },
                ExprOpcode::StoreU64 {
                    location,
                    byte_size,
                } => ExprOpcode::StoreU64 {
                    location: bind_location(location, &mut resolve)?,
                    byte_size,
                },
            };
            ops.push(bound);
        }
        Ok(ExprBytecode::new(ops))
    }
}

fn bind_location<A>(
    location: StateLocation<A>,
    resolve: &mut impl FnMut(&A) -> Option<usize>,
) -> Result<usize, BindError<A>> {
    let Some(base) = resolve(&location.address) else {
        return Err(BindError {
            address: location.address,
        });
    };
    Ok(base + location.byte_offset)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn bytecode_uses_source_independent_operators() {
        let code: ExprBytecode = ExprBytecode::new(vec![
            ExprOpcode::ConstU64(1),
            ExprOpcode::ConstU64(2),
            ExprOpcode::TypedBinOp {
                op: TestbenchOperator::Add,
                lhs_width: 8,
                rhs_width: 8,
                result_width: 8,
                lhs_signed: false,
                rhs_signed: false,
            },
        ]);
        assert_eq!(code.ops().len(), 3);
        assert!(!code.is_empty());
    }

    #[test]
    fn semantic_state_locations_bind_after_layout() {
        let code = ExprBytecode::new(vec![ExprOpcode::LoadU64 {
            location: StateLocation {
                address: 7u32,
                byte_offset: 3,
            },
            byte_size: 2,
            mask: 0xffff,
        }]);

        let bound = code
            .bind_with(|address| (*address == 7).then_some(100))
            .unwrap();
        assert_eq!(
            bound.ops(),
            &[ExprOpcode::LoadU64 {
                location: 103,
                byte_size: 2,
                mask: 0xffff,
            }]
        );
    }

    #[test]
    fn binding_reports_an_unmapped_semantic_address() {
        let code = ExprBytecode::new(vec![ExprOpcode::StoreU64 {
            location: StateLocation {
                address: 9u32,
                byte_offset: 0,
            },
            byte_size: 1,
        }]);

        assert_eq!(code.bind_with(|_| None), Err(BindError { address: 9 }));
    }

    #[test]
    fn control_program_is_independent_of_runtime_handles() {
        type SemanticStatement = TestbenchStatement<u32, u32, ExprBytecode<StateLocation<u32>>, ()>;

        let statement = SemanticStatement::If {
            expr: ExprBytecode::new(vec![ExprOpcode::LoadU64 {
                location: StateLocation {
                    address: 1,
                    byte_offset: 0,
                },
                byte_size: 1,
                mask: 1,
            }]),
            then_block: vec![SemanticStatement::ClockNext {
                clock_event: 2,
                count: ClockCount::Static(1),
            }],
            else_block: vec![SemanticStatement::Finish],
        };

        assert!(matches!(statement, SemanticStatement::If { .. }));
    }
}