forked from zeroasiccorp/switchboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·78 lines (56 loc) · 2.01 KB
/
test.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
#!/usr/bin/env python3
# Simple example illustrating the Switchboard Python binding
# Copyright (c) 2023 Zero ASIC Corporation
# This code is licensed under Apache License 2.0 (see LICENSE for details)
import sys
import numpy as np
from argparse import ArgumentParser
from switchboard import PySbPacket, PySbTx, PySbRx, SbDut
def main(fast=False, tool='verilator'):
# build the simulator
dut = SbDut(tool=tool, default_main=True)
dut.input('testbench.sv')
dut.build(fast=fast)
# create queues
tx = PySbTx('to_rtl.q', fresh=True)
rx = PySbRx('from_rtl.q', fresh=True)
# start chip simulation
chip = dut.simulate()
# form packet to be sent into the simulation. note that the arguments
# to the constructor are all optional, and can all be specified later
txp = PySbPacket(
destination=123456789,
flags=1,
data=np.arange(32, dtype=np.uint8)
)
# send the packet
tx.send(txp) # note: blocking by default, can disable with blocking=False
print("*** TX packet ***")
print(txp)
print()
# receive packet
rxp = rx.recv() # note: blocking by default, can disable with blocking=False
rxp.data = rxp.data[:32]
print("*** RX packet ***")
print(rxp)
print()
# check that the received data
success = np.array_equal(rxp.data, txp.data + 1)
# stop simulation
tx.send(PySbPacket(data=np.array([0xff] * 32, dtype=np.uint8)))
chip.wait()
# declare test as having passed for regression testing purposes
if success:
print("PASS!")
sys.exit(0)
else:
print("FAIL")
sys.exit(1)
if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--fast', action='store_true', help='Do not build'
' the simulator binary if it has already been built.')
parser.add_argument('--tool', default='verilator', choices=['icarus', 'verilator'],
help='Name of the simulator to use.')
args = parser.parse_args()
main(fast=args.fast, tool=args.tool)