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