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

import java.io.IOException;
import java.net.InetSocketAddress;
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;
	public ServerSocketChannel channel;
    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;
        parseArgs();
        try
		{
			channel = ServerSocketChannel.open();
			try
			{
                System.out.println("Listening on port " + portNumber);
				channel.socket().bind(new InetSocketAddress(portNumber));
				for (;;)
				{
					try
					{
						simulator=simFactory.create();
				        simulator.suspend();
                        moreParse();
						run();
					} catch (CPUException e)
					{
						e.printStackTrace();
					} 
				}
			} finally
			{
				channel.close();
			}
		} catch (IOException e1)
		{
			e1.printStackTrace();
		}
	
	}

    private void run() 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();
            }
        }
            
    }
}
OpenPOWER on IntegriCloud