| 1 |
-- | Matcher.hs |
|---|
| 2 |
-- A circuit that matches tokens in its' pool and sends pairs (Pair i) |
|---|
| 3 |
-- into processor. |
|---|
| 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 |
-- The matcher also forwards tokens to other processors if needed. |
|---|
| 14 |
|
|---|
| 15 |
module Matcher(matcher) where |
|---|
| 16 |
|
|---|
| 17 |
import Data.List |
|---|
| 18 |
import Data.Maybe |
|---|
| 19 |
|
|---|
| 20 |
import Utils |
|---|
| 21 |
import InterfaceTypes |
|---|
| 22 |
|
|---|
| 23 |
import S |
|---|
| 24 |
|
|---|
| 25 |
import Info |
|---|
| 26 |
|
|---|
| 27 |
------------------------------------------------------------------------------- |
|---|
| 28 |
-- Matcher function. |
|---|
| 29 |
|
|---|
| 30 |
matcherF maxlen uplen dnWidth upWidth upPred tokens (allInTokens,(ctsu,pairsAllowed)) = (nextTokens,(((otu,otp),rtr),debInfo)) |
|---|
| 31 |
where |
|---|
| 32 |
-- We're ready to receive tokens when we're not overflown. |
|---|
| 33 |
tokensCount = length tokens |
|---|
| 34 |
rtr = tokensCount < maxlen |
|---|
| 35 |
forceUp = tokensCount >= uplen |
|---|
| 36 |
-- Tokens that go up. |
|---|
| 37 |
(tokenSentUp,otu) |
|---|
| 38 |
| ctsu = formSentUp (fromWidth upWidth) uplen upPred tokens |
|---|
| 39 |
| otherwise = (tokens,[]) |
|---|
| 40 |
-- Pairs that go to EU. |
|---|
| 41 |
(tokensPairSent,otp) = formPairs tokenSentUp |
|---|
| 42 |
-- form pairs. |
|---|
| 43 |
maxPairs = min dnWidth pairsAllowed |
|---|
| 44 |
formPairs ts = form maxPairs [] ts |
|---|
| 45 |
where |
|---|
| 46 |
form n acc ts |
|---|
| 47 |
| n <= 0 = (ts,reverse acc) |
|---|
| 48 |
| otherwise = case formPair upPred ts of |
|---|
| 49 |
(_,[]) -> (ts,acc) |
|---|
| 50 |
(ts',ps) -> form (n-length ps) (acc++ps) ts' |
|---|
| 51 |
-- Next state of sorted tokens buffer. |
|---|
| 52 |
nextTokens = sortBy cmpOrders' $ |
|---|
| 53 |
tokensPairSent++allInTokens |
|---|
| 54 |
cmpOrders a b = compare (msgOrder a) (msgOrder b) |
|---|
| 55 |
cmpOrders' a b = case (upPred a,upPred b) of |
|---|
| 56 |
(False,False) -> cmpOrders a b |
|---|
| 57 |
(True,_) -> GT |
|---|
| 58 |
(_,True) -> LT |
|---|
| 59 |
noInfo = "" |
|---|
| 60 |
debInfo = "matcher" |
|---|
| 61 |
!# ("maxlen",maxlen) |
|---|
| 62 |
-- ## ("forceUp",forceUp) |
|---|
| 63 |
## ("tokensCount",tokensCount) |
|---|
| 64 |
-- ## ("allInTokens",allInTokens) |
|---|
| 65 |
-- ## ("ctsu",ctsu) |
|---|
| 66 |
-- ## ("pairsAllowed",pairsAllowed) |
|---|
| 67 |
-- ## ("tokens",tokens) |
|---|
| 68 |
-- ## ("tokenSentUp",tokenSentUp) |
|---|
| 69 |
-- ## ("otu",otu) |
|---|
| 70 |
-- ## ("map upPred otu",map upPred otu) |
|---|
| 71 |
-- ## ("otp",otp) |
|---|
| 72 |
-- ## ("upWidth",fromWidth upWidth) |
|---|
| 73 |
-- ## ("tokens",tokens) |
|---|
| 74 |
-- ## ("nextTokens",nextTokens) |
|---|
| 75 |
-- ## ("map upPred nextTokens",map upPred nextTokens) |
|---|
| 76 |
#! ("rtr",rtr) |
|---|
| 77 |
|
|---|
| 78 |
-- Helpers. |
|---|
| 79 |
|
|---|
| 80 |
-- A function to find a pair to execute. |
|---|
| 81 |
formPair upPred tokens = tryHeadArity tokens |
|---|
| 82 |
where |
|---|
| 83 |
hostPred (_,HostTok,_,_) = True |
|---|
| 84 |
hostPred _ = False |
|---|
| 85 |
skipPred = |
|---|
| 86 |
-- hostPred |
|---|
| 87 |
upPred |
|---|
| 88 |
tryHeadArity (t@Msg {msgOrder=o0,msgArity=HdA1,msgToken=v0}:ts) |
|---|
| 89 |
| skipPred t = findPair [] tokens |
|---|
| 90 |
| otherwise = (ts,[(P1 v0,o0)]) |
|---|
| 91 |
tryHeadArity _ = findPair [] tokens |
|---|
| 92 |
findPair acc (x0:xs@(x1:xs1)) = case arity0 of |
|---|
| 93 |
A2 |
|---|
| 94 |
| orderEq order0 order1 -> |
|---|
| 95 |
(revacc++xs1,[(P2 v0 v1,order0)]) |
|---|
| 96 |
| otherwise -> findPair (x0:acc) xs |
|---|
| 97 |
A1 |
|---|
| 98 |
| skipPred x0 -> findPair (x0:acc) xs |
|---|
| 99 |
| otherwise -> (revacc++xs,[(P1 v0,order0)]) |
|---|
| 100 |
_ -> findPair (x0:acc) xs |
|---|
| 101 |
where |
|---|
| 102 |
revacc = reverse acc |
|---|
| 103 |
Msg {msgOrder=order0,msgModuleAddr=modn0,msgArity=arity0,msgToken=v0} = x0 |
|---|
| 104 |
Msg {msgOrder=order1,msgModuleAddr=modn1,msgArity=arity1,msgToken=v1} = x1 |
|---|
| 105 |
--(order1,modn1,arity1,v1) = x1 |
|---|
| 106 |
findPair acc [x0@Msg {msgOrder=order0,msgModuleAddr=modn0,msgArity=A1,msgToken=v0}] |
|---|
| 107 |
| skipPred x0 = |
|---|
| 108 |
(reverse (x0:acc),[]) |
|---|
| 109 |
| otherwise = |
|---|
| 110 |
(reverse acc,[(P1 v0,order0)]) |
|---|
| 111 |
findPair acc rest = (reverse acc++rest,[]) |
|---|
| 112 |
orderEq a b = a == b |
|---|
| 113 |
|
|---|
| 114 |
-- a function to split tokens into sent up and remaining. |
|---|
| 115 |
formSentUp upWidth uplen upPred tokens = |
|---|
| 116 |
(reverse remains,reverse sentUp) |
|---|
| 117 |
where |
|---|
| 118 |
(sentUp,remains) = form upWidth (length tokens-uplen) [] $ reverse tokens |
|---|
| 119 |
-- first case: width exhausted: |
|---|
| 120 |
form 0 _ sendAcc remains = (sendAcc,remains) |
|---|
| 121 |
-- second case: empty input list. |
|---|
| 122 |
form _ _ sendAcc [] = (sendAcc,[]) |
|---|
| 123 |
-- third case: we still have something to send up. |
|---|
| 124 |
form w diff sendAcc xs@(t:ts) |
|---|
| 125 |
| diff > 0 = form (w-1) (diff-1) (t:sendAcc) ts |
|---|
| 126 |
| upPred t = form (w-1) diff (t:sendAcc) ts |
|---|
| 127 |
| otherwise = (sendAcc,xs) |
|---|
| 128 |
|
|---|
| 129 |
------------------------------------------------------------------------------- |
|---|
| 130 |
-- Matcher circuit. |
|---|
| 131 |
|
|---|
| 132 |
matcher maxlen uplen dnWidth upWidth upPred itu itp ctsu ctsp = (otu,otp,rtr,info) |
|---|
| 133 |
where |
|---|
| 134 |
bus = loopS [] (matcherF maxlen uplen dnWidth upWidth upPred) $ |
|---|
| 135 |
zipS allTokens $ zipS ctsu ctsp |
|---|
| 136 |
allTokens = zipWithS (++) itu itp |
|---|
| 137 |
(resultsBus,info) = unzipS bus |
|---|
| 138 |
(outotp,rtr) = unzipS resultsBus |
|---|
| 139 |
(otu,otp) = unzipS outotp |
|---|