Skip to content

Commit

Permalink
Merge pull request #2558 from clash-lang/clash-ffi-update
Browse files Browse the repository at this point in the history
Ditch `ContT` from `clash-ffi`
  • Loading branch information
kleinreact authored Nov 25, 2023
2 parents 63b90ce + d9f0a45 commit b242d02
Show file tree
Hide file tree
Showing 26 changed files with 787 additions and 953 deletions.
7 changes: 4 additions & 3 deletions clash-ffi/clash-ffi.cabal
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cabal-version: 2.2

name: clash-ffi
version: 1.5.0
version: 1.7.0
synopsis: Interact with Simulators from Clash
description: Interact with Simulators from Clash
bug-reports: https://github.com/clash-lang/clash-compiler/issues
Expand Down Expand Up @@ -41,14 +41,15 @@ library
DeriveGeneric
DerivingStrategies
GeneralizedNewtypeDeriving
LambdaCase
RecordWildCards
TupleSections
build-depends:
derive-storable >= 0.3 && < 0.4,
derive-storable-plugin >= 0.2 && < 0.3,
mtl >= 2.2 && < 2.4,
hs-source-dirs: src
c-sources: cbits/entry_vpi.c
exposed-modules:
Clash.FFI.Monad
Clash.FFI.View
Clash.FFI.VPI.Callback
Clash.FFI.VPI.Callback.Reason
Expand Down
30 changes: 14 additions & 16 deletions clash-ffi/example/Simulate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import Data.Bits (complement)
import Data.List (intercalate, zip5)
import Control.Exception (SomeException, try)
import Control.Monad (void)
import Control.Monad.IO.Class (liftIO)
import Foreign.C.String (newCString)
import Foreign.Marshal.Alloc (free)

Expand All @@ -20,7 +19,6 @@ import Clash.Prelude
, low, high, pack, unpack, resize
)

import Clash.FFI.Monad
import Clash.FFI.VPI.Info
import Clash.FFI.VPI.IO
import Clash.FFI.VPI.Callback
Expand Down Expand Up @@ -49,7 +47,7 @@ foreign export ccall "clash_ffi_main"
ffiMain :: IO ()

ffiMain :: IO ()
ffiMain = runSimAction $ do
ffiMain = do
--------------------------
-- print simulator info --
--------------------------
Expand Down Expand Up @@ -122,7 +120,7 @@ ffiMain = runSimAction $ do
4 -> "<=>" -- mixed input-output
_ -> "x" -- no direction

assignInputs :: (?state :: State) => SimAction ()
assignInputs :: (?state :: State) => IO ()
assignInputs = do
SimTime time <- receiveTime Sim $ Just top

Expand Down Expand Up @@ -163,7 +161,7 @@ assignInputs = do
sendValue port (BitVectorVal SNat $ pack v) $ InertialDelay $ SimTime 0
return $ Just v

readOutputs :: (?state :: State) => SimAction ()
readOutputs :: (?state :: State) => IO ()
readOutputs = do
SimTime time <- receiveTime Sim $ Just top
receiveValue VectorFmt dataOut >>= \case
Expand All @@ -174,14 +172,14 @@ readOutputs = do
}
_ -> return ()

if (steps > 0) then do
if steps > 0 then do
let ?state = ?state { steps = steps - 1 }
nextCB ReadWriteSynch 1 assignInputs
else do
putStrLn ""
putStrLn "[ Simulation done ]"

liftIO $ void $ try @SomeException $ runSimAction
void $ try @SomeException
$ controlSimulator $ Finish NoDiagnostics

where
Expand Down Expand Up @@ -226,31 +224,31 @@ updates = Updates 0 Nothing Nothing Nothing Nothing Nothing
nextCB ::
(Maybe Object -> Time -> CallbackReason) ->
Int64 ->
SimAction () ->
SimAction ()
IO () ->
IO ()
nextCB reason time action =
void $ registerCallback
CallbackInfo
{ cbReason = reason Nothing (SimTime time)
, cbRoutine = const (runSimAction action >> return 0)
, cbRoutine = const (action >> return 0)
, cbIndex = 0
, cbData = B.empty
}

getByName ::
(Coercible a Object, Show a, Typeable a, Coercible Object b) =>
Maybe a -> B.ByteString -> SimCont o b
Maybe a -> B.ByteString -> IO b
getByName m name = do
ref <- liftIO $ newCString $ B.unpack name
ref <- newCString $ B.unpack name
obj <- getChild ref m
liftIO $ free ref
free ref
return obj

putStr :: String -> SimAction ()
putStr :: String -> IO ()
putStr = simPutStr . B.pack

putStrLn :: String -> SimAction ()
putStrLn :: String -> IO ()
putStrLn = simPutStrLn . B.pack

print :: Show a => a -> SimAction ()
print :: Show a => a -> IO ()
print = simPutStrLn . B.pack . show
172 changes: 0 additions & 172 deletions clash-ffi/src/Clash/FFI/Monad.hs

This file was deleted.

Loading

0 comments on commit b242d02

Please sign in to comment.