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