| 1 |
-- | DFLSystem.hs |
|---|
| 2 |
-- |
|---|
| 3 |
-- A whole sorting DFL system assembler and executor with auxiliary circuit. |
|---|
| 4 |
-- |
|---|
| 5 |
-- Copytight (C) 2007, 2008 Serguey Zefirov |
|---|
| 6 |
-- |
|---|
| 7 |
-- This program is free software; you can redistribute it and/or modify |
|---|
| 8 |
-- it under the terms of the GNU General Public License as published by |
|---|
| 9 |
-- the Free Software Foundation; either version 3, or (at your option) |
|---|
| 10 |
-- any later version. |
|---|
| 11 |
-- See file COPYING or visit http://www.gnu.org/licenses for details. |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
module DFLSystem( |
|---|
| 15 |
dflSystemRunner |
|---|
| 16 |
) where |
|---|
| 17 |
|
|---|
| 18 |
import System.IO |
|---|
| 19 |
|
|---|
| 20 |
import qualified Que as Q |
|---|
| 21 |
import Info |
|---|
| 22 |
import S |
|---|
| 23 |
import Presorter |
|---|
| 24 |
import Config |
|---|
| 25 |
|
|---|
| 26 |
------------------------------------------------------------------------------- |
|---|
| 27 |
-- Infinite FIFO with two inputs. |
|---|
| 28 |
-- |
|---|
| 29 |
-- This is the only part of sorting DFL system which does not limit the size |
|---|
| 30 |
-- of data contained within. |
|---|
| 31 |
|
|---|
| 32 |
fifoF outWidth (maxql,que) ((token1,token2),cts) = |
|---|
| 33 |
#ifdef DEBUG |
|---|
| 34 |
((max maxql queLen,nextQue),(out,debinfo)) |
|---|
| 35 |
#else |
|---|
| 36 |
((max maxql queLen,nextQue),out) |
|---|
| 37 |
#endif |
|---|
| 38 |
where |
|---|
| 39 |
addQ xs = Q.appends xs |
|---|
| 40 |
(out,que0) |
|---|
| 41 |
| cts = Q.take outWidth que |
|---|
| 42 |
| otherwise = ([],que) |
|---|
| 43 |
que1 = addQ token1 que0 |
|---|
| 44 |
que2 = addQ token2 que1 |
|---|
| 45 |
queLen = Q.size que |
|---|
| 46 |
nextQue = que2 |
|---|
| 47 |
noinfo = "" |
|---|
| 48 |
debinfo = "FIFO" |
|---|
| 49 |
!# ("input tokens",(token1,token2)) |
|---|
| 50 |
## ("cts",cts) |
|---|
| 51 |
## ("out token",out) |
|---|
| 52 |
-- ## ("start que",que) |
|---|
| 53 |
-- ## ("end que",nextQue) |
|---|
| 54 |
## ("que length",queLen) |
|---|
| 55 |
#! ("maxql",maxql) |
|---|
| 56 |
|
|---|
| 57 |
fifo outWidth tokens1 tokens2 cts = |
|---|
| 58 |
#ifdef DEBUG |
|---|
| 59 |
(out,info) |
|---|
| 60 |
#else |
|---|
| 61 |
out |
|---|
| 62 |
#endif |
|---|
| 63 |
where |
|---|
| 64 |
#ifdef DEBUG |
|---|
| 65 |
(out,info) = unzipS $ |
|---|
| 66 |
loopS (0,Q.empty) (fifoF outWidth) $ zipS (zipS tokens1 tokens2) cts |
|---|
| 67 |
#else |
|---|
| 68 |
out = loopS (0,Q.empty) (fifoF outWidth) $ zipS (zipS tokens1 tokens2) cts |
|---|
| 69 |
#endif |
|---|
| 70 |
|
|---|
| 71 |
------------------------------------------------------------------------------- |
|---|
| 72 |
-- DFL system compiled into single block. |
|---|
| 73 |
-- Parametrized by processor used. |
|---|
| 74 |
|
|---|
| 75 |
dflSystem procWidth proc inTokens = |
|---|
| 76 |
#ifdef DEBUG |
|---|
| 77 |
(hostTokens,info) |
|---|
| 78 |
#else |
|---|
| 79 |
(hostTokens) |
|---|
| 80 |
#endif |
|---|
| 81 |
where |
|---|
| 82 |
hostTokens = mapS onlyHost procOutTokens |
|---|
| 83 |
procTokens = mapS onlyProc procOutTokens |
|---|
| 84 |
onlyHost' (t) |
|---|
| 85 |
| isNotToHost t = [] |
|---|
| 86 |
| otherwise = [t] |
|---|
| 87 |
onlyProc' (t) |
|---|
| 88 |
| isNotToHost t = [t] |
|---|
| 89 |
| otherwise = [] |
|---|
| 90 |
onlyHost ts = concatMap onlyHost' ts |
|---|
| 91 |
onlyProc ts = concatMap onlyProc' ts |
|---|
| 92 |
#ifdef DEBUG |
|---|
| 93 |
(procInTokens,fifoInfo) = fifo procWidth inTokens procTokens procCTS |
|---|
| 94 |
(procOutTokens,procCTS,procInfo) = proc procInTokens (constS True) |
|---|
| 95 |
info = combineInfos [ |
|---|
| 96 |
fifoInfo |
|---|
| 97 |
,procInfo |
|---|
| 98 |
] |
|---|
| 99 |
#else |
|---|
| 100 |
(procInTokens) = fifo procWidth inTokens procTokens procCTS |
|---|
| 101 |
(procOutTokens,procCTS,procInfo) = proc procInTokens (constS True) |
|---|
| 102 |
#endif |
|---|
| 103 |
|
|---|
| 104 |
------------------------------------------------------------------------------- |
|---|
| 105 |
-- Runner of the DFL system. |
|---|
| 106 |
|
|---|
| 107 |
dflSystemRunner config proc (matcher,tokens,hostTokensCount) = |
|---|
| 108 |
run 0 maxWait hostTokensCount (toList hostTokens) (toList info) |
|---|
| 109 |
where |
|---|
| 110 |
debFlag = cfgDebug config |
|---|
| 111 |
debShow |
|---|
| 112 |
| debFlag = putStrLn |
|---|
| 113 |
| otherwise = const $ return () |
|---|
| 114 |
maxWait = cfgMaxWait config |
|---|
| 115 |
parametrizedProc = proc matcher |
|---|
| 116 |
procWidth = getHierProcWidth config |
|---|
| 117 |
takeBy n [] = [] |
|---|
| 118 |
takeBy n xs = let (h,t) = splitAt n xs in h:takeBy n t |
|---|
| 119 |
fromHost = fromListS [] $ takeBy procWidth tokens |
|---|
| 120 |
#ifdef DEBUG |
|---|
| 121 |
(hostTokens,info) = dflSystem procWidth parametrizedProc fromHost |
|---|
| 122 |
#else |
|---|
| 123 |
hostTokens = dflSystem procWidth parametrizedProc fromHost |
|---|
| 124 |
info = constS "" |
|---|
| 125 |
#endif |
|---|
| 126 |
run tc _ 0 _ _ = putStrLn $ "Total "++show tc++" ticks." |
|---|
| 127 |
run tc 0 _ _ _ = putStrLn "Timeout!" |
|---|
| 128 |
run tc wait tokCount (t:ts) (i:is) = do |
|---|
| 129 |
putStrLn $ "Tick "++show tc++"(remains "++show tokCount++")"++": "++show t |
|---|
| 130 |
debShow i |
|---|
| 131 |
-- hFlush stdout |
|---|
| 132 |
run (tc+1) wait' tokCount' ts is |
|---|
| 133 |
where |
|---|
| 134 |
wait' = wait - 1 |
|---|
| 135 |
tokCount' = case t of |
|---|
| 136 |
[] -> tokCount |
|---|
| 137 |
_ -> tokCount-length t |
|---|