summaryrefslogtreecommitdiffstats
path: root/zpu/sw/simulator/com/zylin/zpu/simulator/SimApp.java
blob: 3f6e1a9e5eadd1ebe0fae692cfbf5cdbf3564082 (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
package com.zylin.zpu.simulator;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.ServerSocketChannel;

import com.zylin.zpu.simulator.exceptions.CPUException;
import com.zylin.zpu.simulator.gdb.GDBServer;

public class SimApp
{
	private static Simulator simulator;
    private String[] args;
    private int portNumber;
	private SimFactory simFactory;

    public SimApp(SimFactory factory)
	{
    	simFactory=factory;
	}

	public void parseArgs()
    {
        portNumber = 4444;
        if (args.length>=1)
        {
            portNumber=Integer.parseInt(args[0]);
        }
    }

    private void moreParse()
    {
        if (args.length>=2)
        {
            simulator.setTraceFile(args[1]);
        }
    }

	void run(String[] args)
	{
        this.args=args;
		createSimulator();
        parseArgs();
        moreParse();
        runSimAndGDB();
	}
	Object launched=new Object();
	private boolean doneLaunching;
	private boolean manyGDBSessions;
	public ServerSocket serverSocket;
	public void runSimAndGDB()
	{
        try
		{
			serverSocket = new ServerSocket(portNumber);
			try
			{
				serverSocket.setReuseAddress(true);
                System.out.println("Listening on port " + portNumber);
                setLaunchedFlag();
                do 
                {
					try
					{
						runGDBServer();
					} catch (CPUException e)
					{
						e.printStackTrace();
					}
                } while (manyGDBSessions);
			} finally
			{
				serverSocket.close();
			}
		} catch (IOException e1)
		{
			e1.printStackTrace();
		} finally
		{
            setLaunchedFlag();
		}
	
	}

	private void setLaunchedFlag()
	{
		synchronized(launched)
		{
			doneLaunching=true;
			launched.notify();
		}
	}

	public void createSimulator()
	{
		simulator=simFactory.create();
        simulator.suspend();
	}

    private void runGDBServer() throws CPUException
    {
        final GDBServer gdbServer=new GDBServer(simulator, this);
        simulator.setSyscall(gdbServer);
        Thread thread = new Thread(new Runnable()
                {
                    public void run()
                    {
                        try
                        {
                            gdbServer.gdbServer();
                        } 
                        catch (Throwable e)
                        {
                            e.printStackTrace();
                        }
                        simulator.shutdown();
                    }
                });
        thread.start();
        try
        {
            simulator.run();
        } 
        finally
        {
            try
            {
                thread.join();
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
            
    }

	public Simulator getSimulator()
	{
		return simulator;
	}

	public void setPort(int i)
	{
		portNumber=i;
	}

	/** synchronous launch of GDB server */
	public void launchGDBServer()
	{
		Thread t=new Thread(new Runnable()
		{

			public void run()
			{
				runSimAndGDB();
			}
		});
		t.start();
		synchronized (launched)
		{
			while (!doneLaunching)
			{
				try
				{
					launched.wait(2000);
				} catch (InterruptedException e)
				{
					e.printStackTrace();
				}
			}
		}
		
		
	}
}
OpenPOWER on IntegriCloud