|
| 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 |
0 commit comments