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