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