c4273f9ff238c6346d5de725f1b21b9e8b32ee92
[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
15     private static final int LOCAL_ROUTING_CHANNEL_WIDTH = 7;
16
17     private final Fpslic.Cell fpslicCell;
18     private GuiGate xgate = new GuiGate();
19     private GuiGate ygate = new GuiGate();
20
21     public GuiCell(Fpslic.Cell fpslicCell) {
22         this.fpslicCell = fpslicCell;
23     }
24
25     /**
26      *  The graphics context argument must already be translated such
27      *  that the space allocated to this cell is from (-1,-1) to (1,1)
28      */
29     public void draw(G g, R r, boolean selected) {
30         if (selected) {
31             g.color(0x00555555);
32             r.fill(g);
33         }
34         drawGlobalRouting(g, r);
35         if (fpslicCell.relevant()) {
36             xgate.disabled = !fpslicCell.xlut_relevant();
37             ygate.disabled = !fpslicCell.ylut_relevant();
38             drawBody(g, r);
39         }
40     }
41
42     private void drawGlobalRouting(G g, R r) {
43         g.color(GLOBAL_WIRE_COLOR);
44         for(int i=1; i<6; i++) {
45             g.line(r.minx() + 2*i, r.miny(),
46                    r.minx() + 2*i, r.maxy());
47             g.line(r.minx(),       r.miny() + 2*i,
48                    r.maxx(),       r.miny() + 2*i);
49         }
50     }
51
52     private void drawBody(G g, R r) {
53         if (xgate.disabled && ygate.disabled) return;
54
55         R gateArea =
56             xgate.gateArea = 
57             ygate.gateArea = 
58             r.plus(12+LOCAL_ROUTING_CHANNEL_WIDTH,
59                    12+LOCAL_ROUTING_CHANNEL_WIDTH,
60                    -(4+LOCAL_ROUTING_CHANNEL_WIDTH),
61                    -(4+LOCAL_ROUTING_CHANNEL_WIDTH));
62
63         double factor = gateArea.width()/2;
64         xgate.r = xgate.disabled || ygate.disabled ? gateArea : gateArea.plus(factor, 0, 0, -factor);
65         ygate.r = xgate.disabled || ygate.disabled ? gateArea : gateArea.plus(0, 0, -factor, -factor);
66
67         P xip   = r.corner(fpslicCell.xi());
68         P yip   = r.corner(fpslicCell.yi());
69         R xring = gateArea.plus(-4, -4, 4, 4);
70         R yring = gateArea.plus(-6, -6, 6, 6);
71         xgate.rotation(fpslicCell.yi());
72         ygate.rotation(fpslicCell.yi());
73         if (xip != null) xgate.route(g, xip, xring, 0, XGATE_COLOR);
74         if (yip != null) xgate.route(g, yip, yring, 2, YGATE_COLOR);
75         if (xip != null) ygate.route(g, xip, xring, 2, XGATE_COLOR);
76         if (yip != null) ygate.route(g, yip, yring, 0, YGATE_COLOR);
77
78         drawWin(g, r);
79         drawGates(g);
80     }
81
82     private void drawWin(G g, R r) {
83         int layer = fpslicCell.wi() - L0;
84         P   wip   = r.corner(SW).translate(2*(layer+1), 2*(layer+1));
85         R   wring = r.plus(2*(layer+1), 2*(layer+1), -2*(layer+1), -2*(layer+1));
86         ygate.route(g, wip, wring, 1, LOCAL_WIRE_COLOR);
87         xgate.route(g, wip, wring, 1, LOCAL_WIRE_COLOR);
88     }
89
90     private void drawGates(G g) {
91         xgate.draw(g, XGATE_COLOR);
92         ygate.draw(g, YGATE_COLOR);
93     }
94
95 }