checkpoint
[slipway.git] / src / edu / berkeley / slipway / gui / GuiCell.java
1 package edu.berkeley.slipway.gui;
2
3 import com.atmel.fpslic.*;
4 import static com.atmel.fpslic.FpslicConstants.*;
5
6 public class GuiCell {
7     
8     public static final int BORDER_COLOR      = 0x00BBBBBB;
9     public static final int BODY_COLOR        = 0x00555555;
10     public static final int GLOBAL_WIRE_COLOR = 0x00008000;
11     public static final int LOCAL_WIRE_COLOR  = 0x000FF000;
12     public static final int XGATE_COLOR       = 0x00800000;
13     public static final int YGATE_COLOR       = 0x00000080;
14     double MAIN_AREA = 0.75;
15
16     private final Fpslic.Cell fpslicCell;
17     private GuiGate xgate = null;
18     private GuiGate ygate = null;
19
20     public GuiCell(Fpslic.Cell fpslicCell) {
21         this.fpslicCell = fpslicCell;
22     }
23
24     /**
25      *  The graphics context argument must already be translated such
26      *  that the space allocated to this cell is from (-1,-1) to (1,1)
27      */
28     public void draw(G g, R r, boolean selected) {
29         if (selected) {
30             g.color(0x00555555);
31             r.fill(g);
32         }
33         drawGlobalRouting(g, r);
34         if (fpslicCell.relevant()) {
35             xgate = fpslicCell.xlut_relevant() ? new GuiGate() : null;
36             ygate = fpslicCell.ylut_relevant() ? new GuiGate() : null;
37             drawBody(g, r);
38         }
39     }
40
41     private void drawGlobalRouting(G g, R r) {
42         g.color(GLOBAL_WIRE_COLOR);
43         for(int i=1; i<6; i++) {
44             g.line(r.minx() + 2*i, r.miny(),
45                    r.minx() + 2*i, r.maxy());
46             g.line(r.minx(),       r.miny() + 2*i,
47                    r.maxx(),       r.miny() + 2*i);
48         }
49     }
50
51     private void drawBody(G g, R r) {
52         if (xgate == null && ygate == null) return;
53
54         int N = 7;
55         int TSIZE   = 10;
56         double TSQR = TSIZE / Math.sqrt(2.0);
57
58         R body = r;
59         g.color(BODY_COLOR);
60         R xgater = null;
61         R ygater = null;
62         R gateArea = body.plus(12+N, 12+N, -(4+N), -(4+N));
63         if      (xgate==null) ygater = gateArea;
64         else if (ygate==null) xgater = gateArea;
65         else {
66             double factor = gateArea.width()/2;
67             xgater = gateArea.plus(0, 0, -factor, -factor);
68             ygater = gateArea.plus(factor, 0, 0, -factor);
69         }
70
71         R xring = gateArea.plus(-4, -4, 4, 4);
72         R yring = gateArea.plus(-6, -6, 6, 6);
73         if (xgate != null) {
74             xgate.rotation(fpslicCell.yi());
75             xgate.gateArea = gateArea;
76             xgate.r = xgater;
77         }
78         if (ygate != null) {
79             ygate.rotation(fpslicCell.yi());
80             ygate.gateArea = gateArea;
81             ygate.r = ygater;
82         }
83
84         g.color(XGATE_COLOR);
85         P p = r.corner(fpslicCell.xi());
86         if (p!=null) {
87             if (ygate != null) {
88                 g.route(p, xring, ygate.getInput(1));
89                 g.line(ygate.getInput(1), ygate.getInputDest(1));
90             }
91             if (xgate != null) {
92                 g.route(p, xring, xgate.getInput(0));
93                 g.line(xgate.getInput(0), xgate.getInputDest(0));
94             }
95         }
96
97         p = r.corner(fpslicCell.yi());
98         g.color(YGATE_COLOR);
99         if (p!=null) {
100             if (ygate != null) {
101                 g.route(p, yring, ygate.getInput(0));
102                 g.line(ygate.getInput(0), ygate.getInputDest(0));
103             }
104             if (xgate != null) {
105                 g.route(p, yring, xgate.getInput(1));
106                 g.line(xgate.getInput(1), xgate.getInputDest(1));
107             }
108         }
109
110         if (xgater != null) {
111             /*
112             if (fpslicCell.zi() != NONE) {
113                 g.color(LOCAL_WIRE_COLOR);
114                 int layer = fpslicCell.zi() - L0;
115                 P p2 = new P(r.minx()+2*(layer+1), r.miny()+2*(layer+1));
116                 R r2 = new R(r.minx()+2*(layer+1), r.miny()+2*(layer+1),
117                              r.maxx()-2*(layer+1), r.maxy()-2*(layer+1));
118                 g.route(p2, r2, xgate.getInput(3));
119             }
120             */
121             if (fpslicCell.wi() != NONE) {
122                 g.color(LOCAL_WIRE_COLOR);
123                 int layer = fpslicCell.wi() - L0;
124                 P p2 = new P(r.minx()+2*(layer+1), r.miny()+2*(layer+1));
125                 R r2 = new R(r.minx()+2*(layer+1), r.miny()+2*(layer+1),
126                              r.maxx()-2*(layer+1), r.maxy()-2*(layer+1));
127                 g.route(p2, r2, xgate.getInput(2));
128                 g.line(xgate.getInput(2), xgate.getInputDest(2));
129             }
130             xgate.draw(g, XGATE_COLOR);
131         }
132         if (ygater != null) {
133             /*
134             if (fpslicCell.zi() != NONE) {
135                 g.color(LOCAL_WIRE_COLOR);
136                 int layer = fpslicCell.zi() - L0;
137                 P p2 = new P(r.minx()+2*(layer+1), r.miny()+2*(layer+1));
138                 R r2 = new R(r.minx()+2*(layer+1), r.miny()+2*(layer+1),
139                              r.maxx()-2*(layer+1), r.maxy()-2*(layer+1));
140                 g.route(p2, r2, ygate.getInput(3));
141             }
142             */
143             if (fpslicCell.wi() != NONE) {
144                 g.color(LOCAL_WIRE_COLOR);
145                 int layer = fpslicCell.wi() - L0;
146                 P p2 = new P(r.minx()+2*(layer+1), r.miny()+2*(layer+1));
147                 R r2 = new R(r.minx()+2*(layer+1), r.miny()+2*(layer+1),
148                              r.maxx()-2*(layer+1), r.maxy()-2*(layer+1));
149                 g.route(p2, r2, ygate.getInput(2));
150                 g.line(ygate.getInput(2), ygate.getInputDest(2));
151             }
152             ygate.draw(g, YGATE_COLOR);
153         }
154     }
155 }