updates that were lying around but never got checked in; includes reorg of gui
[slipway.git] / src / edu / berkeley / slipway / demos / Demo2.java
1 package edu.berkeley.slipway.demos;
2
3 import edu.berkeley.slipway.*;
4 import edu.berkeley.slipway.util.*;
5 import static java.awt.event.KeyEvent.*;
6 import com.atmel.fpslic.*;
7 import static com.atmel.fpslic.FpslicConstants.*;
8 import edu.berkeley.slipway.gui.*;
9 import java.awt.*;
10 import java.awt.event.*;
11 import java.awt.color.*;
12 import org.ibex.util.*;
13 import java.io.*;
14 import java.util.*;
15 import gnu.io.*;
16
17 public class Demo2 implements KeyListener {
18
19     public static void main(String[] s) throws Exception {
20         new Demo2().go(); 
21     }
22     public SlipwayBoard slipway;
23     public Demo2() throws Exception {
24         slipway = new SlipwayBoard();
25     }
26     public void go() throws Exception {
27         long begin = System.currentTimeMillis();
28         long end = System.currentTimeMillis();
29         Log.info(Demo.class, "finished in " + ((end-begin)/1000) + "s");
30         Thread.sleep(1000);
31
32
33         Log.info(Demo.class, "issuing command");
34
35         FpslicDevice device = slipway.getFpslicDevice();
36         FpslicDevice.Cell root = device.cell(5,5);
37
38         root.ylut(LUT_SELF);
39         root.yi(NORTH);
40         root.xi(NW);
41         root.wi(L0);
42         root.zi(L2);
43
44         root = root.north();
45         root.ylut(LUT_SELF);
46         root.yi(WEST);
47         root.out(1, true);
48         root.h(1, true);
49         root.wi(L1);
50         root.zi(L3);
51
52         root = root.west();
53         root.xi(SE);
54         root.ylut(LUT_SELF);
55         root.yi(SOUTH);
56         root.wi(L2);
57         root.zi(L4);
58
59         root = root.south();
60         root.ylut(LUT_SELF);
61         root.yi(EAST);
62         root.wi(L3);
63         root.zi(L0);
64         //root = root.n();
65
66         device.iob_bot(12, false).enableOutput(NW);
67         FpslicDevice.Cell c = device.cell(12, 0);
68         c.xo(c.east());
69         while(c.east() != null && c.east().east() != null) {
70             c.yo(c.east());
71             c = c.east();
72         }
73         device.flush();
74
75         FpslicDevice.Cell div = device.cell(19, 21);
76         while(div != null)
77             div = ExperimentUtils.divider(div);
78         device.flush();
79
80         int MAX=17;
81         for(int x=2; x<MAX+1; x++) {
82             c = device.cell(x, 20);
83             FpslicDevice.Cell bridge = x==2 ? c.sw()    : c.nw();
84             FpslicDevice.Cell pred   = x==MAX ? c.south() : c.east();
85             FpslicDevice.Cell next   = x==2 ? c.south() : c.west();
86             muller(c, pred, bridge, next);
87
88             c = c.south();
89             bridge = x==MAX ? c.ne()    : c.se();
90             pred   = x==2 ? c.north() : c.west();
91             next   = x==MAX ? c.north() : c.east();
92             muller(c, pred, bridge, next);
93         }
94         //device.cell(MAX+0,20).yi(WEST);
95         //device.cell(MAX+0,20).ylut(LUT_SELF);
96         //device.cell(MAX+1,20).yi(WEST);
97         //device.cell(MAX+1,20).ylut(LUT_SELF);
98         device.cell(MAX+2,20).yi(WEST);
99         device.cell(MAX+2,20).ylut(LUT_SELF);
100         device.cell(MAX+2,20).xlut(LUT_OTHER);
101         device.cell(18,20).ylut(LUT_SELF);
102         device.flush();
103         go2();
104     }
105
106     public void go2() throws Exception {
107         FpslicDevice device = slipway.getFpslicDevice();
108         setupScanCell();
109         device.flush();
110
111         vis = new Gui3(device, slipway);
112         vis.addKeyListener(this);
113         Frame fr = new Frame();
114         fr.setLayout(new BorderLayout());
115         fr.add(vis, BorderLayout.CENTER);
116         fr.pack();
117         fr.setSize(900, 900);
118         vis.repaint();
119         fr.repaint();
120         fr.show();
121         //synchronized(Demo.class) { Demo.class.wait(); }
122         while(true) {
123             try { Thread.sleep(500); } catch (Exception e) { }
124             synchronized(vis) {
125                 scan();
126             }
127         }
128     }
129     Gui3 vis;
130     public void muller(FpslicDevice.Cell c, FpslicDevice.Cell pred, FpslicDevice.Cell bridge, FpslicDevice.Cell next) {
131         FpslicDevice device = slipway.getFpslicDevice();
132         bridge.yi(next);
133         bridge.xlut(LUT_OTHER);
134
135         c.yi(pred);
136         c.xi(bridge);
137         c.b(false);
138         c.f(false);
139         c.c(YLUT);
140         c.t(TMUX_FB);
141         c.ylut((LUT_SELF & ~LUT_OTHER) |
142                (LUT_Z    & ~LUT_OTHER) |
143                (LUT_Z    &   LUT_SELF));
144     }
145
146     public void setupScanCell() {
147         FpslicDevice device = slipway.getFpslicDevice();
148         FpslicDevice fpslic = (FpslicDevice)device;
149         fpslic.cell(23,15).h(3, true);
150         fpslic.cell(23,15).yi(L3);
151         fpslic.cell(23,15).ylut(0xAA);
152         fpslic.iob_right(15, true).enableOutput(WEST);
153
154         fpslic.cell(23,0).ylut(0x00);
155         fpslic.iob_right(0, true).enableOutput(WEST);
156         fpslic.flush();
157     }
158
159     public void keyTyped(KeyEvent k) { }
160     public void keyReleased(KeyEvent k) { }
161     public void keyPressed(KeyEvent k) {
162         switch(k.getKeyCode()) {
163             case VK_SPACE:
164                 scan();
165                 break;
166         }
167     }
168     public void scan() {
169         for(int x=0; x<4; x++)
170             for(int y=0; y<4; y++)
171                 scan(vis.ca[x][y]);
172         for(int x=0; x<4; x++)
173             for(int y=0; y<4; y++)
174                 scan(vis.ca[x][y]);
175     }
176     public void scan(final GuiCell c) {
177         try {
178             final FpslicDevice.Cell cell = c.fpslicCell;
179             scan(slipway, cell, YLUT, true);
180             int x = cell.col;
181             int y = cell.row;
182             slipway.readFpgaData(new BCB(c));
183             scan(slipway, cell, YLUT, false);
184         } catch (IOException e) {
185             throw new RuntimeException(e);
186         }
187     }
188
189     public static void scan(SlipwayBoard slipway, FpslicDevice.Cell cell, int source, boolean setup) {
190         FpslicDevice dev = slipway.getFpslicDevice();
191         if (setup) {
192             //if (source != NONE) cell.c(source);
193             if (cell.b()) cell.b(false);
194             if (cell.f()) cell.f(false);
195         }
196         if (cell.out(L3)!=setup) cell.out(L3, setup);
197         if (cell.vx(L3)!=setup) cell.v(L3, setup);
198
199         FpslicDevice.SectorWire sw = cell.vwire(L3);
200         //System.out.println("wire is: " + sw);
201
202         if (sw.row > (12 & ~0x3) && sw.north()!=null && sw.north().drives(sw))
203             sw.north().drives(sw, false);
204         while(sw.row > (12 & ~0x3) && sw.south() != null) {
205             //System.out.println(sw + " -> " + sw.south());
206             if (sw.drives(sw.south())!=setup) sw.drives(sw.south(), setup);
207             sw = sw.south();
208         }
209         if (sw.row < (12 & ~0x3) && sw.south() != null && sw.south().drives(sw))
210             sw.north().drives(sw, false);
211         while(sw.row < (12 & ~0x3) && sw.north() != null) {
212             //System.out.println(sw + " -> " + sw.north());
213             if (sw.drives(sw.north())!=setup) sw.drives(sw.north(), setup);
214             sw = sw.north();
215         }
216
217         //cell = dev.cell(19, 15);
218         cell = dev.cell(cell.col, 15);
219         /*
220         System.out.println("cell is " + cell);
221         cell.xlut(0xff);
222         cell.ylut(0xff);
223         cell.b(false);
224         cell.f(false);
225         cell.c(XLUT);
226         cell.out(L3, true);
227         cell.oe(NONE);
228         */
229         if (cell.hx(L3) != setup) cell.h(L3, setup);
230         if (cell.vx(L3) != setup) cell.v(L3, setup);
231         sw = cell.hwire(L3);
232
233         if (sw.west()!=null && sw.west().drives(sw)) { sw.west().drives(sw, false); }
234         while(sw.east() != null) {
235             //System.out.println(sw + " -> " + sw.east());
236             if (sw.drives(sw.east())!=setup) sw.drives(sw.east(), setup);
237             sw = sw.east();
238         }
239
240     }
241
242
243     private class BCB extends SlipwayBoard.ByteCallback {
244         GuiCell c;
245         public BCB(GuiCell c) {
246             this.c = c;
247         }
248         public void call(byte b) throws Exception {
249             boolean on = (b & 0x80) != 0;
250             c.val = on;
251             vis.repaint();
252         }
253     }
254 }