Skip to content

Commit 1142ef2

Browse files
hanwHan Wang
authored andcommitted
tinytpu: bscwave testbench for VPU at 1 sublane x 4 lanes
Adds tests/bscwave/vpu-tb/, a Haskell-driven bsc testbench for mkVPU. VPU is polymorphic in (sublanes, lanes); the wrapper pins it to 1x4 — smallest config that satisfies the even-lane proviso for PAIR_ROTATE. The wrapper is the only BSV; everything else (stimulus, expected values, waveform) lives in Main.hs. First op covered is VPU_ADD: dispatches src1=[1,2,3,4] + src2=[10,20,30,40], reads resultReg, asserts [11,22,33,44]. Uses bscwave's formatPort + lanesFormat to render the 128-bit packed Vector#(1, Vector#(4, Int#(32))) ports as "[a,b,c,d]" decimal in the terminal waveform. build.sh wires bsc with -p $TINYTPU/src:+ so FpReducer/TranscUnit resolve, uses -bdir/-simdir to keep .bo/.ba droppings local, and RTS -K256M to survive VPU's per-op case-block elaboration. Requires bscwave checked out at ~/p/bscwave (sibling path; cabal.project points to ../../../../bscwave).
1 parent d9df40d commit 1142ef2

6 files changed

Lines changed: 185 additions & 0 deletions

File tree

‎tests/bscwave/vpu-tb/.gitignore‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# bsc and link artifacts
2+
src/*.ba
3+
src/*.bo
4+
src/*.cxx
5+
src/*.h
6+
src/*.so
7+
src/*.so.so
8+
src/libsim.so
9+
libsim.so
10+
11+
# bscwave-gen-ports output (regenerated by build.sh)
12+
app/MkVPU_S1L4.hs
13+
14+
# cabal
15+
dist-newstyle/

‎tests/bscwave/vpu-tb/app/Main.hs‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE TypeApplications #-}
3+
module Main where
4+
5+
import Data.Bits (shiftR, shiftL, (.&.))
6+
import System.Exit (exitFailure)
7+
8+
import Bscwave.Interface
9+
import Bscwave.Sim
10+
import qualified Bscwave.Waveform as Waveform
11+
import qualified Bscwave.Render as Render
12+
import qualified MkVPU_S1L4 as V
13+
14+
-- VpuOp encoding follows the source order of the enum declaration in
15+
-- src/VPU.bsv (bsc derives Bits in that order). VPU_ADD is first, so 0.
16+
vpuAdd :: Bit 7
17+
vpuAdd = 0
18+
19+
-- bsc's pack for Vector#(n, T) places element 0 in the high bits. For
20+
-- Vector#(1, Vector#(4, Int#(32))) we get a 128-bit value where lane 0
21+
-- occupies bits[127:96] and lane 3 occupies bits[31:0].
22+
packLanes :: [Integer] -> Bit 128
23+
packLanes xs = bit @128 $ foldr (\(i, v) acc -> acc + (mask32 v `shiftL` (96 - 32*i))) 0 (zip [0..] xs)
24+
where
25+
mask32 v = v .&. 0xffffffff
26+
27+
unpackLanes :: Bit 128 -> [Integer]
28+
unpackLanes b =
29+
[ signed32 ((unBit b `shiftR` (96 - 32*i)) .&. 0xffffffff) | i <- [0 .. 3 :: Int] ]
30+
where
31+
signed32 v = if v >= 0x80000000 then v - 0x100000000 else v
32+
33+
-- Render a single 32-bit lane as signed decimal.
34+
laneI32 :: Integer -> String
35+
laneI32 v
36+
| v >= 0x80000000 = show (v - 0x100000000)
37+
| otherwise = show v
38+
39+
testbench :: IO (Waveform.Waveform, Integer)
40+
testbench = do
41+
sim <- create @V.Inputs @V.Outputs V.modelName
42+
let i = inputs sim
43+
o = outputs sim
44+
45+
-- Attach per-lane decimal formatters BEFORE Waveform.create, since the
46+
-- waveform snapshots the formatter at capture time.
47+
formatPort (V.execute_src1 i) (lanesFormat 4 32 laneI32)
48+
formatPort (V.execute_src2 i) (lanesFormat 4 32 laneI32)
49+
formatPort (V.resultreg o) (lanesFormat 4 32 laneI32)
50+
-- The 7-bit op encoding is more readable as decimal than hex here.
51+
formatPort (V.execute_op i) decimalFormat
52+
53+
(waves, sim') <- Waveform.create sim
54+
let step en op s1 s2 = do
55+
writePort (V.en_execute (inputs sim')) en
56+
writePort (V.execute_op (inputs sim')) op
57+
writePort (V.execute_src1 (inputs sim')) (packLanes s1)
58+
writePort (V.execute_src2 (inputs sim')) (packLanes s2)
59+
simStep sim'
60+
61+
-- Dispatch VPU_ADD([1,2,3,4], [10,20,30,40]).
62+
step 1 vpuAdd [1, 2, 3, 4] [10, 20, 30, 40]
63+
-- Two idle cycles let resultReg settle and give the wave room to render.
64+
step 0 0 [0,0,0,0] [0,0,0,0]
65+
step 0 0 [0,0,0,0] [0,0,0,0]
66+
67+
out <- readPort (V.resultreg o)
68+
pure (waves, unBit out)
69+
70+
main :: IO ()
71+
main = do
72+
(waves, got) <- testbench
73+
Render.print waves
74+
let lanes = unpackLanes (bit @128 got)
75+
expected = [11, 22, 33, 44]
76+
putStrLn $ "expected: " <> show expected
77+
putStrLn $ "got: " <> show lanes
78+
if lanes == expected
79+
then putStrLn "PASS"
80+
else putStrLn "FAIL" >> exitFailure

‎tests/bscwave/vpu-tb/build.sh‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env bash
2+
# Builds libsim.so for the VPU bscwave testbench.
3+
#
4+
# 1. bsc compiles VPUWrapper.bsv (with -p so it picks up tinytpu/src/*.bsv
5+
# and the BSV stdlib) into the mkVPU_S1L4 model + sim.so.so.
6+
# 2. g++ links the bscwave wrapper against the bsc-generated objects.
7+
# 3. bscwave-gen-ports parses the emitted mkVPU_S1L4.cxx to produce a
8+
# typed Inputs/Outputs Haskell record.
9+
10+
set -euo pipefail
11+
12+
HERE="$(cd "$(dirname "$0")" && pwd)"
13+
TINYTPU="$(cd "$HERE/../../.." && pwd)"
14+
BSCWAVE="$(cd "$HERE/../../../../bscwave" && pwd)"
15+
16+
if [ -z "${BLUESPECDIR:-}" ]; then
17+
BLUESPECDIR="$(dirname "$(dirname "$(command -v bsc)")")/lib"
18+
fi
19+
BSC_INC="$BLUESPECDIR/Bluesim"
20+
21+
WRAPPER="$BSCWAVE/csrc/bsim_wrapper.cxx"
22+
23+
cd "$HERE/src"
24+
25+
# 1. bsc compile + link to Bluesim runtime.
26+
# -p searches tinytpu/src/ for VPU.bsv and its FpReducer/TranscUnit deps,
27+
# then the standard Bluespec library paths.
28+
# +RTS -K256M matches the parent Makefile — VPU elaboration recurses
29+
# deeply through the per-op case block and blows the default stack.
30+
# -bdir and -simdir keep .bo/.ba/.o droppings here in src/ rather than
31+
# polluting tinytpu/src/ next to the discovered .bsv dependencies.
32+
BSC="bsc +RTS -K256M -RTS"
33+
$BSC -sim -p "$TINYTPU/src:+" -bdir . -simdir . -g mkVPU_S1L4 -u VPUWrapper.bsv
34+
$BSC -sim -p "$TINYTPU/src:+" -bdir . -simdir . -e mkVPU_S1L4 -o sim.so mkVPU_S1L4.ba
35+
36+
# 2. Build libsim.so = bscwave wrapper + model objects.
37+
g++ -shared -fPIC \
38+
-I"$BSC_INC" \
39+
"$WRAPPER" \
40+
model_mkVPU_S1L4.o mkVPU_S1L4.o \
41+
-L. -l:sim.so.so -ldl \
42+
-Wl,-rpath,'$ORIGIN' \
43+
-o libsim.so
44+
45+
cd "$HERE"
46+
ln -sf src/libsim.so libsim.so
47+
48+
# 3. Generate the typed port record from the bsc-emitted .cxx so the
49+
# Haskell testbench can reference inputs/outputs by name with widths
50+
# checked at compile time.
51+
(cd "$BSCWAVE" && cabal --project-dir=. run -v0 bscwave-gen-ports -- \
52+
"$HERE/src/mkVPU_S1L4.cxx" \
53+
-o "$HERE/app/MkVPU_S1L4.hs")
54+
55+
echo
56+
echo "Built. Now run: LD_LIBRARY_PATH=./src cabal run testbench"

‎tests/bscwave/vpu-tb/cabal.project‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
packages: .
2+
../../../../bscwave
3+
4+
program-options
5+
ghc-options: -optl-Wl,--copy-dt-needed-entries -optl-Wl,-rpath-link,./src
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Concrete instantiation of mkVPU for bscwave testing.
2+
//
3+
// mkVPU is polymorphic in (sublanes, lanes); bscwave-gen-ports needs a
4+
// non-polymorphic top so it can emit fixed-width Inputs/Outputs records.
5+
// 1x4 is the smallest config that still satisfies the even-lane proviso
6+
// required by VPU_PAIR_ROTATE / VPU_IPAIR_ROTATE.
7+
package VPUWrapper;
8+
9+
import VPU :: *;
10+
11+
(* synthesize *)
12+
module mkVPU_S1L4(VPU_IFC#(1, 4));
13+
let _vpu <- mkVPU;
14+
return _vpu;
15+
endmodule
16+
17+
endpackage

‎tests/bscwave/vpu-tb/vpu-tb.cabal‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cabal-version: 3.0
2+
name: vpu-tb
3+
version: 0.1.0.0
4+
5+
executable testbench
6+
main-is: Main.hs
7+
other-modules: MkVPU_S1L4
8+
hs-source-dirs: app
9+
extra-libraries: sim
10+
extra-lib-dirs: ., src
11+
build-depends: base, bscwave
12+
default-language: Haskell2010

0 commit comments

Comments
 (0)