summaryrefslogtreecommitdiffstats
path: root/zpu/sw/simulator/com/zylin/zpu/simulator/applet/ZPUApplet.java
blob: 731c8fcfccdc3b94cc8e0cb6b3f1978fb2acd0b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281

package com.zylin.zpu.simulator.applet;

import java.applet.Applet;
import java.awt.TextArea;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.swing.SwingUtilities;

import com.zylin.zpu.simulator.Host;
import com.zylin.zpu.simulator.Sim;
import com.zylin.zpu.simulator.Simulator;
import com.zylin.zpu.simulator.exceptions.CPUException;
import com.zylin.zpu.simulator.exceptions.UnsupportedSyscallException;

public class ZPUApplet extends Applet implements Host
{

	private static final long serialVersionUID = 1L;
	private TextArea console;
    private Simulator simulator;
	private PipedOutputStream outputPipe;
	private PipedInputStream inputPipe;

    public void init()
    {
        super.init();
        
        console=new TextArea();
        //console.setEditable(false);

        
        console.addKeyListener(new KeyListener()
                {

                    public void keyPressed(KeyEvent e)
                    {
                    	try 
                    	{
							outputPipe.write((int)e.getKeyChar());
						} catch (Throwable e1) 
						{
							e1.printStackTrace();
						}
                        
                    }

                    public void keyReleased(KeyEvent e)
                    {
                        
                    }

                    public void keyTyped(KeyEvent e)
                    {
                        
                    }
                });

        //Add the text field to the applet.
        add(console);

        //Set the layout manager so that the text field will be
        //as wide as possible.
        setLayout(new java.awt.GridLayout(1,0));

        validate();
        initSimulator();
        
    }
    
    



    public void start() 
	{
		super.start();
	}





	public void stop()
    {
        simulator.shutdown();
        super.stop();
    }

    private void initSimulator()
    {
    	final ZPUApplet me = this;
    	
        try
        {
        	outputPipe = new PipedOutputStream();
			inputPipe = new PipedInputStream(outputPipe);
			
			showStatus("Loading ZPU binary image...");

			Thread thread = new Thread(new Runnable() {
				public void run() {
					try {
						// lineReader=new LineNumberReader(new
						// InputStreamReader(inputPipe));

						simulator = new Simulator();
						simulator.setSyscall(me);

						String file = getParameter("executable");

						URL url;
						url = new URL(getCodeBase(), file);
						showStatus("Loading: " + url.toString() +"...");

						URLConnection connection = url.openConnection();
						
						simulator.loadImage(connection.getInputStream(), connection.getContentLength());

						simulator.run();
					} catch (Throwable e) {
						e.printStackTrace();
					}
				}
			});
			thread.start();
		} catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e)
        {
            throw new RuntimeException(e);
        }
    }

    public void syscall(Sim s) throws CPUException
    {
        int retval=-1;
        int syscallErrno=0;
        int id;
        id=simulator.getArg(1);
        try
        {
	        switch (id)
	        {
	        case SYS_write:
	            writeToConsole();
	            retval=simulator.getArg(4);
	            break;
	        case SYS_read:
	        	int i;
	        	for (i=0; i<simulator.getArg(4); i++)
	        	{
	        		int t=inputPipe.read();
        			simulator.writeByte(simulator.getArg(3)+i, t);
        			if ((t=='\n')||(t=='\r'))
        			{
        				/* done reading line */
        				i++;
        				break;
        			}
	        	}
	        	retval=i;
	        	break;
	        	
	        case SYS_fstat:
	        	syscallErrno=EIO;
	        	retval=-1;
	            break;
	            
	        default:
	            simulator.suspend();
	            throw new UnsupportedSyscallException();
	        }
        } 
        catch (IOException e)
        {
            retval=-1;
            syscallErrno=EIO;
        }
        simulator.cpuWriteLong(simulator.getArg(0), syscallErrno);
        simulator.cpuWriteLong(0, retval);
    }

    private void writeToConsole() throws CPUException
    {
    	String t="";
        for (int i=0; i<simulator.getArg(4); i++)
        {
        	t+=(char)simulator.readByte(simulator.getArg(3)+i);
        }
        final String t2=t;
        System.out.println(t2);
        try {
			SwingUtilities.invokeAndWait(new Runnable()
			        {
			            public void run()
			            {
			                console.append(t2);
			            }
			        });
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }




    public boolean doneContinue()
    {
        return false;
    }


    public void writeUART(final int val)
    {
        try {
			SwingUtilities.invokeAndWait(new Runnable()
			        {

			            public void run()
			            {
			                console.append(""+(char)(val));
			                repaint();
			                
			            }
			        });
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
    }





	public int readUART() throws CPUException 
	{
		try 
		{
			return inputPipe.read();
		} catch (IOException e) 
		{
			e.printStackTrace();
			throw new CPUException();
		}
	}





	public int readFIFO() 
	{
		return 0;
	}

	public void halted()
	{
		showStatus("ZPU application halted");
	}

	public void running()
	{
		showStatus("ZPU application running...");
	}
}
OpenPOWER on IntegriCloud