-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Clash FFI interface tests & bugfixes
- Loading branch information
1 parent
e0a0a42
commit cce665f
Showing
25 changed files
with
3,477 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,14 +12,9 @@ maintainer: [email protected] | |
copyright: Copyright © 2022, QBayLogic B.V. | ||
category: Hardware | ||
|
||
library | ||
common common-options | ||
default-language: Haskell2010 | ||
default-extensions: | ||
BangPatterns | ||
DeriveAnyClass | ||
DeriveGeneric | ||
DerivingStrategies | ||
GeneralizedNewtypeDeriving | ||
ScopedTypeVariables | ||
TypeApplications | ||
ghc-options: | ||
|
@@ -29,12 +24,28 @@ library | |
bytestring >= 0.10 && < 0.12, | ||
clash-prelude >= 1.2 && < 1.8, | ||
deepseq >= 1.4 && < 1.5, | ||
include-dirs: include | ||
includes: vpi_user.h | ||
cpp-options: | ||
-DVERILOG=1 | ||
-DIVERILOG=1 | ||
-DVERILOG_2001=1 | ||
-DVERILOG_2005=1 | ||
-DVPI_VECVAL=1 | ||
|
||
library | ||
import: common-options | ||
default-extensions: | ||
BangPatterns | ||
DeriveAnyClass | ||
DeriveGeneric | ||
DerivingStrategies | ||
GeneralizedNewtypeDeriving | ||
build-depends: | ||
derive-storable >= 0.3 && < 0.4, | ||
derive-storable-plugin >= 0.2 && < 0.3, | ||
mtl >= 2.2 && < 2.3, | ||
hs-source-dirs: src | ||
include-dirs: include | ||
includes: vpi_user.h | ||
c-sources: cbits/entry_vpi.c | ||
exposed-modules: | ||
Clash.FFI.Monad | ||
|
@@ -64,9 +75,34 @@ library | |
Clash.FFI.VPI.Port | ||
Clash.FFI.VPI.Port.Direction | ||
Clash.FFI.VPI.Reg | ||
cpp-options: | ||
-DVERILOG=1 | ||
-DIVERILOG=1 | ||
-DVERILOG_2001=1 | ||
-DVERILOG_2005=1 | ||
-DVPI_VECVAL=1 | ||
|
||
executable ffi-interface-tests | ||
import: common-options | ||
default-extensions: | ||
DataKinds | ||
RankNTypes | ||
LambdaCase | ||
ViewPatterns | ||
TupleSections | ||
ImplicitParams | ||
FlexibleContexts | ||
FlexibleInstances | ||
MultiParamTypeClasses | ||
ExistentialQuantification | ||
hs-source-dirs: tests | ||
main-is: Main.hs | ||
other-modules: | ||
Clash.FFI.Test | ||
Clash.FFI.Test.Instances | ||
include-dirs: tests/cbits | ||
c-sources: | ||
tests/cbits/VPI.c | ||
tests/cbits/Test.c | ||
tests/cbits/Pipe.c | ||
tests/cbits/Print.c | ||
build-depends: | ||
, clash-ffi | ||
, smallcheck | ||
, tasty | ||
, tasty-hunit | ||
, tasty-smallcheck |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ License: BSD2 (see the file LICENSE) | |
Maintainer: QBayLogic B.V. <[email protected]> | ||
-} | ||
|
||
{-# LANGUAGE CPP #-} | ||
|
||
module Clash.FFI.Monad | ||
( SimCont | ||
, SimAction | ||
|
@@ -33,6 +35,15 @@ import Foreign.Storable (Storable) | |
import qualified Foreign.Storable as FFI (peek) | ||
import GHC.Stack (HasCallStack) | ||
|
||
#if MIN_VERSION_base(4,9,0) | ||
#ifdef MIN_VERSION_GLASGOW_HASKELL | ||
#if MIN_VERSION_GLASGOW_HASKELL(8,8,4,0) | ||
#else | ||
import Control.Monad.Fail (MonadFail) | ||
#endif | ||
#endif | ||
#endif | ||
|
||
{- | ||
NOTE [continuation-based API] | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
@@ -69,17 +80,22 @@ same way, e.g. | |
-- 'runSimAction'. | ||
-- | ||
newtype SimCont o i = SimCont (ContT o IO i) | ||
deriving newtype (Applicative, Functor, Monad, MonadCont, MonadIO, MonadFail) | ||
deriving newtype | ||
( Applicative, Functor, Monad, MonadCont, MonadIO | ||
#if MIN_VERSION_base(4,9,0) | ||
, MonadFail | ||
#endif | ||
) | ||
|
||
-- | The type of an VPI "main" action run in @clash-ffi@. For the more general | ||
-- type of FFI computations, use 'SimCont'. | ||
-- | ||
type SimAction = SimCont () () | ||
type SimAction a = SimCont a a | ||
|
||
-- | Run a VPI "main" action. See 'SimAction' and 'SimCont' for more | ||
-- information. | ||
-- | ||
runSimAction :: SimAction -> IO () | ||
runSimAction :: SimAction a -> IO a | ||
runSimAction (SimCont cont) = Cont.runContT cont pure | ||
|
||
-- | Lift a continuation into a simulation FFI action. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ Maintainer: QBayLogic B.V. <[email protected]> | |
-} | ||
|
||
{-# LANGUAGE TypeFamilies #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
|
||
-- Used to improve the performance of derived instances. | ||
{-# OPTIONS_GHC -fplugin=Foreign.Storable.Generic.Plugin #-} | ||
|
@@ -51,7 +52,7 @@ data CInfo = CInfo | |
|
||
-- | Information about the simulator connected to over VPI. This includes the | ||
-- command line used to start the simulation tool. Depending on the simulator | ||
-- this may include / remove arguments recognised by the simulator (i.e. it | ||
-- this may include / remove arguments recognized by the simulator (i.e. it | ||
-- will only contain other flags like RTS flags). | ||
-- | ||
data Info = Info | ||
|
@@ -64,20 +65,20 @@ data Info = Info | |
type instance CRepr Info = CInfo | ||
|
||
instance UnsafeReceive Info where | ||
unsafeReceive cinfo = do | ||
unsafeReceive CInfo{..} = do | ||
-- When passing +RTS to some simulators, they may replace the whole | ||
-- argument with NULL, so we check for that instead of using argc. | ||
args <- unsafeReceiveArray0 FFI.nullPtr (cinfoArgv cinfo) | ||
prod <- unsafeReceive (cinfoProduct cinfo) | ||
ver <- unsafeReceive (cinfoVersion cinfo) | ||
-- argument with NULL, so we check in addition to argc. | ||
args <- unsafeReceiveArray0 (fromEnum cinfoArgc) FFI.nullPtr cinfoArgv | ||
prod <- unsafeReceive cinfoProduct | ||
ver <- unsafeReceive cinfoVersion | ||
|
||
pure (Info args prod ver) | ||
|
||
instance Receive Info where | ||
receive cinfo = do | ||
args <- receiveArray0 FFI.nullPtr (cinfoArgv cinfo) | ||
prod <- receive (cinfoProduct cinfo) | ||
ver <- receive (cinfoVersion cinfo) | ||
receive CInfo{..} = do | ||
args <- receiveArray0 (fromEnum cinfoArgc) FFI.nullPtr cinfoArgv | ||
prod <- receive cinfoProduct | ||
ver <- receive cinfoVersion | ||
|
||
pure (Info args prod ver) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.