|
| 1 | +-- SPDX-FileCopyrightText: 2025 Google LLC |
| 2 | +-- |
| 3 | +-- SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +module Bittide.Instances.Tests.AddressableBytesWb where |
| 6 | + |
| 7 | +import Clash.Prelude |
| 8 | + |
| 9 | +-- Local |
| 10 | +import Bittide.Cpus.Riscv32imc (vexRiscv0) |
| 11 | +import Bittide.DoubleBufferedRam ( |
| 12 | + ContentType (Vec), |
| 13 | + wbStorage, |
| 14 | + ) |
| 15 | +import Bittide.Instances.Domains (Basic50) |
| 16 | +import Bittide.ProcessingElement ( |
| 17 | + PeConfig (..), |
| 18 | + processingElement, |
| 19 | + ) |
| 20 | +import Bittide.ProcessingElement.Util ( |
| 21 | + vecFromElfData, |
| 22 | + vecFromElfInstr, |
| 23 | + ) |
| 24 | +import Bittide.SharedTypes (withBittideByteOrder) |
| 25 | +import Bittide.Wishbone (uartBytes, uartInterfaceWb) |
| 26 | +import Project.FilePath ( |
| 27 | + CargoBuildType (Release), |
| 28 | + findParentContaining, |
| 29 | + firmwareBinariesDir, |
| 30 | + ) |
| 31 | + |
| 32 | +import Clash.Class.BitPackC (ByteOrder (BigEndian)) |
| 33 | +import Data.Char (chr) |
| 34 | +import Data.Maybe (catMaybes) |
| 35 | +import Protocols |
| 36 | +import Protocols.Idle (idleSource) |
| 37 | +import Protocols.MemoryMap (MemoryMap, Mm, getMMAny) |
| 38 | +import System.Environment (lookupEnv) |
| 39 | +import System.FilePath ((</>)) |
| 40 | +import System.IO.Unsafe (unsafePerformIO) |
| 41 | +import Test.Tasty.HUnit (HasCallStack) |
| 42 | +import VexRiscv (DumpVcd (DumpVcd, NoDumpVcd)) |
| 43 | + |
| 44 | +sim :: IO () |
| 45 | +sim = do |
| 46 | + -- Print the UART output to terminal for debugging |
| 47 | + putStrLn "=== UART Output ===" |
| 48 | + putStrLn simResult |
| 49 | + putStrLn "==================" |
| 50 | + |
| 51 | +simResult :: String |
| 52 | +simResult = chr . fromIntegral <$> catMaybes uartStream |
| 53 | + where |
| 54 | + uartStream = sampleC def{timeoutAfter = 1000000} dut0 |
| 55 | + |
| 56 | + dut0 :: Circuit () (Df Basic50 (BitVector 8)) |
| 57 | + dut0 = Circuit $ ((),) . snd . toSignals dut . ((),) . snd |
| 58 | + |
| 59 | +{- | An instance connecting a VexRiscv to a UART and an addressable buffer device. |
| 60 | +Uses the Rust test binary. |
| 61 | +-} |
| 62 | +dut :: Circuit (ToConstBwd Mm) (Df Basic50 (BitVector 8)) |
| 63 | +dut = dutWithBinary "addressable_bytes_wb_test" |
| 64 | + |
| 65 | +-- | Parameterized DUT that loads a specific firmware binary. |
| 66 | +dutWithBinary :: |
| 67 | + (HasCallStack) => |
| 68 | + String -> |
| 69 | + Circuit (ToConstBwd Mm) (Df Basic50 (BitVector 8)) |
| 70 | +dutWithBinary binaryName = |
| 71 | + withBittideByteOrder |
| 72 | + $ withClockResetEnable clockGen (resetGenN d2) enableGen |
| 73 | + $ circuit |
| 74 | + $ \mm -> do |
| 75 | + (uartRx, jtag) <- idleSource |
| 76 | + [uartBus, addressableBus] <- processingElement dumpVcd (peConfig binaryName) -< (mm, jtag) |
| 77 | + (uartTx, _uartStatus) <- uartInterfaceWb d2 d2 uartBytes -< (uartBus, uartRx) |
| 78 | + wbStorage "AddressableBuffer" d8 Nothing -< addressableBus |
| 79 | + idC -< uartTx |
| 80 | + where |
| 81 | + dumpVcd = |
| 82 | + unsafePerformIO $ do |
| 83 | + mVal <- lookupEnv "ADDRESSABLE_BYTES_WB_DUMP_VCD" |
| 84 | + case mVal of |
| 85 | + Just s -> pure (DumpVcd s) |
| 86 | + _ -> pure NoDumpVcd |
| 87 | + |
| 88 | + peConfig binary = unsafePerformIO $ do |
| 89 | + root <- findParentContaining "cabal.project" |
| 90 | + let elfPath = root </> firmwareBinariesDir "riscv32imc" Release </> binary |
| 91 | + pure |
| 92 | + PeConfig |
| 93 | + { cpu = vexRiscv0 |
| 94 | + , depthI = SNat @IMemWords |
| 95 | + , depthD = SNat @DMemWords |
| 96 | + , initI = |
| 97 | + Just |
| 98 | + $ Vec |
| 99 | + $ unsafePerformIO |
| 100 | + $ vecFromElfInstr BigEndian elfPath |
| 101 | + , initD = |
| 102 | + Just |
| 103 | + $ Vec |
| 104 | + $ unsafePerformIO |
| 105 | + $ vecFromElfData BigEndian elfPath |
| 106 | + , iBusTimeout = d0 -- No timeouts on the instruction bus |
| 107 | + , dBusTimeout = d0 -- No timeouts on the data bus |
| 108 | + , includeIlaWb = False |
| 109 | + } |
| 110 | +{-# OPAQUE dutWithBinary #-} |
| 111 | + |
| 112 | +memoryMap :: MemoryMap |
| 113 | +memoryMap = getMMAny dut0 |
| 114 | + where |
| 115 | + dut0 :: Circuit (ToConstBwd Mm, Df System ()) () |
| 116 | + dut0 = circuit $ \(mm, _df) -> do |
| 117 | + _uart <- dut -< mm |
| 118 | + idC |
| 119 | + |
| 120 | +type IMemWords = DivRU (256 * 1024) 4 |
| 121 | +type DMemWords = DivRU (256 * 1024) 4 |
0 commit comments