-
Notifications
You must be signed in to change notification settings - Fork 18
/
FED.hs
306 lines (259 loc) · 9.59 KB
/
FED.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
-- Stuff for the FED / LHC work
{-# LANGUAGE ScopedTypeVariables, BangPatterns, RecursiveDo, UndecidableInstances, FlexibleContexts, KindSignatures, RankNTypes, GADTs, RecordWildCards, FlexibleInstances #-}
module FED where
import Language.KansasLava
-- import Language.KansasLava.Protocols
-- import Language.KansasLava.Utils
-- import Language.KansasLava.Fabric (observeFabric)
-- import Control.Monad.Fix
-- import Control.Monad.IO.Class
-- import Control.Monad
-- import System.IO
-- import Data.Monoid
-- import Data.Maybe
-- import Data.Word
-- import Data.Sized.Fin
-- import Data.Sized.Unsigned
-- import Control.Concurrent.STM
-- import Control.Concurrent
-- import System.Environment
-- import Control.Concurrent.MVar
-- import System.IO.Unsafe
-- import Control.Monad.Trans.Class
-- import System.Random
-- import Control.Monad.Trans.Trace
--------------------------------------------------------------------------
data SendDatum a
= SendUnknown
| SendNoDatum
| SendDatum a
sendDatum :: (Rep a)
=> IO (X Bool)
-> (X a -> IO ())
-> (X Bool -> IO ())
-> SendDatum a
-> IO Bool
sendDatum ready dat en cmd =
case cmd of
SendUnknown -> do
_ <- ready -- ignore the ready signal
dat unknownX -- send unknown
en unknownX -- send unknown enable
return True
SendNoDatum -> do
_ <- ready -- ignore the ready signal
dat unknownX -- send unknown
en (pureX False) -- send *no* enable
return True
SendDatum a -> do
r <- ready -- ignore the ready signal
case unX r of
Nothing -> error "ready signal unknown in sendDatum"
Just False -> do
dat unknownX -- send unknown
en (pureX False) -- send *no* enable
return False
Just True -> do
dat (pureX a) -- send value
en (pureX True) -- send an enable
return True
writeDatum :: (Rep a)
=> (X a -> IO ())
-> (X Bool -> IO ())
-> SendDatum a
-> IO ()
writeDatum dat en cmd = do
_ <- sendDatum (return $ pureX True) dat en cmd
return ()
data RecvDatum
= RecvUnknown
| RecvNoDatum
| RecvDatum
recvDatum :: (Rep a)
=> (X Bool -> IO ())
-> IO (X a)
-> IO (X Bool)
-> RecvDatum
-> IO (Maybe a)
recvDatum ready dat en cmd =
case cmd of
RecvUnknown -> do
ready unknownX
_ <- dat
_ <- en
return Nothing
RecvNoDatum -> do
ready (pureX False)
_ <- dat
_ <- en
return Nothing
RecvDatum -> do
ready (pureX True)
d <- dat
e <- en
return $ case unX e of
Nothing -> error "enabled undefined in recvDatum"
Just True -> case unX d of
Nothing -> error "enabled set, undefined value in recvDatum"
Just a -> Just a
Just False -> Nothing
readDatum :: (Rep a)
=> IO (X a)
-> IO (X Bool)
-> RecvDatum
-> IO (Maybe a)
readDatum = recvDatum (const $ return ())
-- All this is now inside dut-check.
{-
--------------------------------------------------------------------------
-- The Monad
data FifoM :: ((* -> *) -> *) -> * -> * where
FifoM :: (Env f -> IO a) -> FifoM f a
instance Monad (FifoM f) where
return a = FifoM $ \ env -> return a
(FifoM m) >>= k = FifoM $ \ env -> do
r <- m env
case k r of
FifoM m -> m env
instance MonadIO (FifoM f) where
liftIO m = FifoM $ \ _ -> m
runFifoM :: String -> (f Reply -> IO (f Ret)) -> FifoM f () -> IO ()
runFifoM seed action prog = do
cmd_var <- newEmptyMVar
let std0 :: StdGen = read seed
var <- newMVar std0
env <- return $ Env
{ env_rand = do
std <- takeMVar var
let (n,std') = random std
putMVar var std'
return n
, env_randR = \ (a,b) -> do
std <- takeMVar var
let (n,std') = randomR (a,b) std
putMVar var std'
return n
, the_cmds = cmd_var
}
forkIO $ case prog of
FifoM m -> do m env
dut_interp action cmd_var
-- if n `mod` == 0 then do { putChar '.' ; hFlush stdout } else return ()
dut_interp :: (f Reply -> IO (f Ret)) -> MVar (StepCmd f) -> IO ()
dut_interp callout cmd_var = loop 0 []
where
loop n checked = do
if n `mod` 10000 == 0 then do { putChar '.' ; hFlush stdout } else return ()
-- get step commands
(props,opt_cmd) <- takeStepCmd cmd_var
case opt_cmd of
Nothing -> return ()
-- do step
Just cmd -> do ret <- callout cmd
props' <- sequence [ mkProp prop | prop <- props ]
loop2 n ret (props' ++ checked)
loop2 n ret checked = do
answers <- sequence [ f ret | f <- checked ]
let result = Prelude.and answers -- better all be true
if result then do loop (n+1) checked
else do -- failed! Give error messsage
print ("failed at #",n)
return ()
mkProp :: Prop f -> IO (f Ret -> IO Bool)
mkProp (Prop nm f) = do
in_var <- newEmptyMVar
out_var <- newEmptyMVar
forkIO $ do
let in_loop = do
state <- takeMVar in_var
states <- unsafeInterleaveIO in_loop
return $ state : states
-- You need the ! pattern here to make sure
-- the unsafeInterleaveIO is consumed by
-- *this* thread.
loop ((!o):os) = do
-- print o
putMVar out_var o
loop os
loop [] = return ()
ins <- in_loop
loop (f ins)
return $ \ state -> do
putMVar in_var state
takeMVar out_var
--------------------------------------------------------------------------
data Reply a = Reply (a -> IO ())
data Ret a = Ret a
deriving Show
--------------------------------------------------------------------------
-- A prop is something that should always be true for a specific test.
data Prop f = Prop String ([f Ret] -> [Bool])
data StepCmd f
= StepDone -- 1 cycle, no more commands
| StepCmd (f Reply) -- 1 cycle
| RegProp (Prop f) -- no cycles
takeStepCmd :: MVar (StepCmd f) -> IO ([Prop f],Maybe (f Reply))
takeStepCmd var = loop []
where
loop regs = do
-- print "take step"
v <- takeMVar var
-- print "taken step"
case v of
StepDone -> return (regs,Nothing)
StepCmd cmd -> return (regs,Just cmd)
RegProp prop -> loop (regs ++ [prop])
data Env f = Env
{ env_rand :: forall r . (Random r) => IO r -- a random number generator
, env_randR :: forall r . (Random r) => (r,r) -> IO r
, the_cmds :: MVar (StepCmd f) -- where to send the commands
}
parFifoM :: (Monoid (f Reply)) => [ FifoM f () ] -> FifoM f ()
parFifoM ms = FifoM $ \ env -> do
vs <- sequence
[ do v <- newEmptyMVar
forkIO $ do f (env { the_cmds = v })
forever $ putMVar v StepDone
return v
| FifoM f <- ms
]
-- returns when all the commands
let loop = do
(regs,vals) <- liftM unzip $ sequence [ takeStepCmd v | v <- vs ]
case (concat regs,mconcat vals) of
([],Nothing) -> return ()
(props,opt_cmd) -> do
sequence_ [ putMVar (the_cmds env) (RegProp prop)
| prop <- props
]
putMVar (the_cmds env) $ case opt_cmd of
Nothing -> StepDone
Just cmd -> StepCmd cmd
loop
-- call loop until all the sub-threads "die" (start issuing Nothing)
loop
h i m = do print ("starting",i)
r <- m
print ("done",i)
return r
---------------------------------------------------------
-- Monad Commands
wait :: Monoid (f Reply) => Int -> FifoM f ()
wait 0 = return ()
wait n = do
FifoM $ \ env -> putMVar (the_cmds env) (StepCmd $ mempty)
wait (n - 1)
-- You need to use the reply argument (otherwise the takeMVar v hangs)
putCmd :: Monoid (f Reply) => (Reply b -> f Reply) -> FifoM f b
putCmd cmd = FifoM $ \ env -> do
v <- newEmptyMVar
putMVar (the_cmds env) (StepCmd $ cmd (Reply $ putMVar v))
takeMVar v
rand :: (Random r) => FifoM f r
rand = FifoM $ \ env -> env_rand env
randR :: (Random r) => (r,r) -> FifoM f r
randR (a,b) = FifoM $ \ env -> env_randR env (a,b)
property :: Prop f -> FifoM f ()
property prop = FifoM $ \ env -> do
putMVar (the_cmds env) (RegProp prop)
-}