root/hiersort/sim/HierProc.hs

Revision 1, 6.4 kB (checked in by thesz, 2 years ago)

Initial commit

Line 
1 -- | HierProc.hs
2 -- Hierarchical (combined) processor.
3 --
4 -- Copytight (C) 2007, 2008 Serguey Zefirov
5 --
6 -- This program is free software; you can redistribute it and/or modify
7 -- it under the terms of the GNU General Public License as published by
8 -- the Free Software Foundation; either version 3, or (at your option)
9 -- any later version.
10 -- See file COPYING or visit http://www.gnu.org/licenses for details.
11
12 -- Here we have a hierProcLeaf for bottom nodes with processing elements
13 -- and hierProcNode which combines two subhierarchies into one bigger.
14 --
15 -- hierProc governs them.
16 --
17 -- The main challenge here is to properly connect subparts in hierProcNode.
18
19 module HierProc(hierProc) where
20
21 import Config
22
23 import S
24 import SQ3
25 import Proc
26 import Info
27 import Matcher
28 import MatcherProc
29 import Presorter
30 import UpBuf
31 import InterfaceTypes
32
33 import Data.Bits
34
35 -------------------------------------------------------------------------------
36 -- Hierarchical processor leaf (bottom level).
37
38 hierProcLeaf config mask procIndex procTransFunc inTokens cts = (outTokens,rtr,info)
39         where
40                 -- configuration parameters.
41                 chanWidth = mkWidth "chanWidth" $ getProcessorChanWidth config
42
43                 thisTokenF' HostTok = False
44                 thisTokenF' (ModTok addr) = (addr .&. mask) == procIndex
45                 thisTokenF msg = thisTokenF' $ msgModuleAddr msg
46                 (outTokens,rtr,procInfo) =
47                         matcherProc config procTransFunc thisTokenF
48                         chanWidth mask procIndex inTokens cts
49                
50                 info = combineInfos [
51                                  constS $ infoLineWords ["Processor",show procIndex]
52                                 ,procInfo
53                         ]
54
55 -------------------------------------------------------------------------------
56 -- Hierarchical processor node (intermediate and top levels).
57
58 ourAddrF mask (low,high) msg = case msgModuleAddr msg of
59         HostTok -> False
60         ModTok a' -> let a = a' .&. mask in a >= low && a <= high
61
62 leftAddrF mask (low,high) msg = case msgModuleAddr msg of
63         HostTok -> False
64         ModTok a' -> let a = a' .&. mask in a >= low && a < mid
65         where
66                 mid = (`div` 2) $ low+high+1
67
68 hierProcNode config nodeParams mask procNRange leftProc rightProc inTokens cts =
69         (outTokens,rtr,info)
70         where
71                 -- configuration parameters.
72                 upWidth = mkWidth "upWidth" $ nodeCfgUpChanWidth nodeParams
73                 dnWidth = mkWidth "dnWidth" $ nodeCfgDownChanWidth nodeParams
74                 presorterSortWidth =
75                         -- mkWidth "presorterSortWidth" $ nodeCfgPreSortChanWidth nodeParams
76                         mkWidth "presorterSortWidth" $ nodeCfgDownChanWidth nodeParams*2
77                 presorterUpWidth =
78                         -- mkWidth "presorterUpWidth" $ nodeCfgPreUpChanWidth nodeParams
79                         mkWidth "presorterUpWidth" $ nodeCfgDownChanWidth nodeParams*2
80                 sorterUpWidth = mkWidth "sorterUpWidth" $ fromWidth presorterSortWidth - fromWidth dnWidth
81                 sorterDnWidth = dnWidth
82                 -- Instance circuits.
83                 (leftProcOutTokens,leftProcRTR,leftProcInfo) =
84                         leftProc leftProcInTokens leftProcCTS
85                 (rightProcOutTokens,rightProcRTR,rightProcInfo) =
86                         rightProc rightProcInTokens rightProcCTS
87                 (leftSorterRTR,leftSorterUpTokens,leftSorterDownTokens,leftSorterInfo) =
88                         sorter leftSorterInTokens leftSorterDownCTS leftSorterUpCTS
89                 (rightSorterRTR,rightSorterUpTokens,rightSorterDownTokens,rightSorterInfo) =
90                         sorter rightSorterInTokens rightSorterDownCTS rightSorterUpCTS
91 #ifdef  DEBUG
92                 (upTokens,upBufRTR,upBufInfo) =
93 #else
94                 upBufInfo = constS ""
95                 (upTokens,upBufRTR) =
96 #endif
97                         upstreamBuffer
98                                 (cfgUpBufMaxLen config)
99                                 upWidth
100                                 upForwardedTokens leftSorterUpTokens rightSorterUpTokens
101                                 cts
102 #ifdef  DEBUG
103                 (leftForwardedTokens,rightForwardedTokens,upForwardedTokens,presorterUpRTR,presorterDnRTR,presorterInfo) =
104 #else
105                 presorterInfo = constS ""
106                 (leftForwardedTokens,rightForwardedTokens,upForwardedTokens,presorterUpRTR,presorterDnRTR) =
107 #endif
108                         presorterInstance
109                                 presorterSortWidth
110                                 presorterUpWidth
111                                 inTokens
112                                 leftProcOutTokens rightProcOutTokens
113                                 presorterUpCTS
114                                 presorterLeftCTS
115                                 presorterRightCTS
116                                
117                 -- Sorter circuit.
118                 sorter input dncts upcts = sq3chain
119                         (nodeCfgSortSize nodeParams)
120                         (nodeCfgSortHeight nodeParams)
121                         (nodeCfgSortForceUp nodeParams)
122                         sorterUpWidth
123                         sorterDnWidth
124                         compareOrders
125                         input dncts upcts
126                 compareOrders msga msgb =
127                         compare (msgOrder msga) (msgOrder msgb)
128
129                 -- Presorter circuit.
130                 presorterInstance =
131                         presorter
132                                 (ourAddrF mask procNRange)
133                                 (leftAddrF mask procNRange)
134                                 maxlen
135                                
136                         where
137                                 maxlen = (cfgPresorterMaxLen config)
138                 -- Connect wires.
139                 leftProcInTokens = leftSorterDownTokens
140                 rightProcInTokens = rightSorterDownTokens
141                 leftSorterDownCTS = leftProcRTR
142                 rightSorterDownCTS = rightProcRTR
143                 leftProcCTS = presorterDnRTR
144                 rightProcCTS = presorterDnRTR
145                 presorterUpCTS = upBufRTR
146                 presorterLeftCTS = leftSorterRTR
147                 presorterRightCTS = rightSorterRTR
148                 leftSorterInTokens = leftForwardedTokens
149                 rightSorterInTokens = rightForwardedTokens
150                 leftSorterUpCTS = upBufRTR
151                 rightSorterUpCTS = upBufRTR
152                 outTokens = upTokens
153                 rtr = presorterUpRTR
154                 -- generate info.
155                 (low,high) = procNRange
156                 nodeHeader = constS $ infoLineWords ["Node",show low++".."++show high]
157                 procInfos = [leftProcInfo,rightProcInfo]
158                 sorterInfos = [
159                                  constS "left sorter\n"
160                                 ,leftSorterInfo
161                                 ,constS "right sorter\n"
162                                 ,rightSorterInfo
163                         ]
164                 allInfos = nodeHeader:presorterInfo:upBufInfo:sorterInfos++procInfos
165                 onlyProcInfos = nodeHeader:procInfos
166                 info = combineInfos allInfos --onlyProcInfos
167
168 -------------------------------------------------------------------------------
169 -- Run a construction of hierarchical processor.
170
171 hierProc config procTransFunc
172         | depth < 0 = error "Invalid construction depth."
173         | otherwise = construct startMask startRange params
174         where
175                 depth = length params
176                 np = shiftL (1::Int) depth
177                 startMask = np-1
178                 startRange = (0,np-1)
179                 procIndexFromRange range@(start,end)
180                         | start==end = start
181                         | otherwise = error $ "start /= end: "++show range
182                 params = getHierParams config
183                 construct mask procNRange [] inTokens cts =
184                         hierProcLeaf
185                                 config
186                                 mask
187                                 (procIndexFromRange procNRange)
188                                 procTransFunc
189                                 inTokens
190                                 cts
191                 construct mask procNRange (nodeParams:paramss) inTokens cts =
192                         hierProcNode
193                                 config
194                                 nodeParams
195                                 mask procNRange
196                                 leftProc
197                                 rightProc
198                                 inTokens
199                                 cts
200                         where
201                                 nextMask = mask .|. shiftR mask 1       -- expand one bit further
202                                 (start,end) = procNRange
203                                 mid = div (start+end+1) 2
204                                 leftRange = (start,mid-1)
205                                 rightRange = (mid,end)
206                                 leftProc =
207                                         construct
208                                                 nextMask
209                                                 leftRange
210                                                 paramss
211                                 rightProc =
212                                         construct
213                                                 nextMask
214                                                 rightRange
215                                                 paramss
Note: See TracBrowser for help on using the browser.