{-- Copyright (c) 2009, Mary Sheeran All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Chalmers University of Technology nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY Mary Sheeran ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Mary Sheeran BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. --} module DrawPP12 where import Data.List ( partition , transpose , sort , sortBy , groupBy ) import System.Cmd ( system ) ------------------------------------------------------------------------- Nets data Net = Net { fans :: [(Int,[Int])] , wire :: Int , phase :: Int } type WDels = [(Int,Int)] netsz :: Int -> [Net] netsz n = [Net [] w 0 | w <- [1..n]] nets :: WDels -> [Net] nets wds = [Net [] w d | (w,d) <- wds ] instance Eq Net where n == m = wire n == wire m && phase n == phase m instance Ord Net where n <= m = phase n <= phase m instance Show Net where show n = show (wire n) ++ "/" ++ show (fans n) netFan [i] = [i] netFan (i:is) = (j:js) where ph = maximum $ map phase (i:is) j = Net { fans = (ph, map wire (i:is)):fans i , wire = wire i , phase = ph + 1 } js = [Net { fans = fans k , wire = wire k , phase = ph + 1 } | k <- is ] ----------------------------------------------------------------------- -- drawing draw :: Int -> Int -> Int -> [Net] -> IO () draw i pi s = putStr . unlines . (xfigdrawing i pi s) drawFile :: Int -> Int -> Int -> String -> [Net] -> IO () drawFile i pi s name = (writeFile name) . unlines . (xfigdrawing i pi s) -- producing xfig picture xfigdrawing :: Int -> Int -> Int -> [Net] -> [String] xfigdrawing i pi s ns = ["#FIG 3.2" ,"Landscape" ,"Center" ,"Inches" ,"A4" ,"100.00" ,"Single" ,"-2" ,"1200 2"] ++ nphases i pi 0 s cmps ++ nwires i maxw maxl s ++ [drawtext (s*2*i,s*(maxl+4*i)) (s*2*i*maxw) statline] where cmps = concat (map getdots (allfansN ns)) getdots (p,(i:is)) = [(p,(i,k)) | k <- is] -- a dot is associated with the phase -- at which the fanout takes place nphases i pi ph s [] = [] nphases i pi ph s cmps = concat (map ((drawdots i pi ph s).reverse) (fixup p ( [ (x,y) | (_,(x,y)) <- this]))) ++ nphases i pi (ph+1) s later where (this,later) = partition (\(ph',_) -> ph' == ph) cmps p (x1,_) (x2,_) = x1==x2 nwires i w l s = [drawline' (0,s*(2*i*x)) (s*l,s*(2*i*x)) | x <- [1..w]] drawdots i pi ph s [] = [] drawdots i pi ph s (a:as) = map (drawcomp i ph s) (a:as) fixup p [] = [] -- probably could have used some prelude function! fixup p [a] = [[a]] fixup p (a:b:cs) | (p a b) = (a:fs):gss | otherwise = [a]:fs:gss where (fs:gss) = fixup p (b:cs) drawcomp i ph s (x,y) = drawline' (s*o1,s*2*i*x) (s*(o1+2*i),s*(2*i*y)) ++ "\n" ++ drawcircle' (s*(o1+2*i),s*(2*i*y)) (s*pi) where o1 = 2*i*ph drawline (x1,y1) (x2,y2) = "2 1 0 1 0 7 50 0 -1 0.000 0 0 -1 0 0 2\n "++ show x1 ++ " " ++ show y1 ++ " " ++ show x2 ++ " " ++ show y2 drawline' (x1,y1) (x2,y2) = drawline (y1,x1) (y2,x2) drawcircle (x,y) r = "1 4 0 1 0 0 50 0 20 0.000 1 0.0000" ++ " " ++ show x ++ " " ++ show y ++ " " ++ show r ++ " " ++ show r ++ " " ++ show (x-r) ++ " " ++ show y ++ " " ++ show (x+r) ++ " " ++ show y drawWhiteCircle (x,y) r = "1 4 0 1 7 7 50 0 20 0.000 1 0.0000" ++ " " ++ show x ++ " " ++ show y ++ " " ++ show r ++ " " ++ show r ++ " " ++ show (x-r) ++ " " ++ show y ++ " " ++ show (x+r) ++ " " ++ show y -- in drawWhiteCircle, I changed the line colour of the circle -- from black to white in order to make it invisible in the diagrams -- as I decided to follow the convention of not showing white circles -- This involved changing "1 4 0 1 0 7" to "1 4 0 1 7 7". -- I believe the resulting diagrams to be more readable. drawcircle' (x,y) r = drawcircle (y,x) r drawWhiteCircle' (x,y) r = drawWhiteCircle (y,x) r drawhoriz x l = "\\put(0," ++ show x ++ "){\\line(1,0){" ++ show l ++ "}}" drawtext (x,y) l s = "4 0 0 50 0 0 24 0.0000 4 600" ++ " " ++ show l ++ " " ++ show x ++ " " ++ show y ++ " " ++ s ++ "\\001" drawtext' (x,y) = drawtext (y,x) maxw = maximum [ max x y | (_,(x,y)) <- cmps] maxph = maximum [ ph | (ph,_) <- cmps ] + 1 maxl = 2*i*maxph order (x,y) | x <= y = (x,y) | otherwise = (y,x) statline = show maxw ++ " lines, " ++ show maxph ++ " stage" ++ (if maxph > 1 then "s" else "") ++ ", " ++ show (sizeN ns) ++ " operators, " ++ show (maxfoN ns) ++ " maximum fanout." drawF = drawFile 20 3 10 getNets f wds = f netFan (nets wds) getNets0 f n = f netFan (netsz n) allfansN ns = concat (map fans ns) nop (_,ws) = length ws-1 fanout (_,ws) = length ws spanf (_,ws) = last ws - head ws square x = x*x cube x = x*x*x sizeN = sum . map nop . allfansN maxfoN = maximum . map fanout . allfansN sumfoN = sum . map fanout . allfansN sumspanN = sum . map spanf . allfansN sumspansqN = sum . map (square . spanf) . allfansN sumfosqN = sum . map (square . fanout) . allfansN sumfocubedN = sum . map (cube . fanout) . allfansN drawPP s f n = drawF (s++show n++".fig") (getNets0 f n) displayPP s f n = drawPP s f n >> system ("xfig " ++ (s++show n++".fig")) ----------------------------------------------------------------------- -- the end.