-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpex.cpp
177 lines (144 loc) · 3.79 KB
/
pex.cpp
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
#include <iostream>
#include <cstdio>
#include <numeric>
#include <SDL2/SDL_net.h>
#include <list>
#include <thread>
#include <mutex>
#include <chrono>
#include "types.hpp"
#include "cpu.hpp"
#include "gdb.hpp"
#include "pex.hpp"
#include "state.hpp"
#include "sdl.hpp"
#include "net.hpp"
#include "gpio.hpp"
#include "prof.hpp"
u32 samplingFrequency = 48 * 1024 * 1024 >> 4;
static TCPsocket server, listener;
extern bool hasQuit;
static std::thread worker;
u64 lastTick;
s64 tti;
class Client {
TCPsocket socket;
std::thread thread;
public:
Client(TCPsocket socket) : socket(socket), thread(&Client::run, this) {
}
virtual ~Client(){
thread.join();
if(socket)
SDLNet_TCP_Close(socket);
}
void run(){
std::string buffer;
char command = 0;
bool profInit = false;
while( !hasQuit ){
char ch;
int len = SDLNet_TCP_Recv( socket, &ch, 1 );
if( !len ){
if( listener == socket )
listener = 0;
SDLNet_TCP_Close(socket);
socket = 0;
return;
}
switch( command ){
case 0:
command = ch;
continue;
case 'l':
command = 0;
listener = socket;
continue;
case 'g':
command = 0;
GDB::interrupt();
continue;
case 'p':
command = 0;
if( profInit )
continue;
profInit = true;
PROF::dumpOnExit = false;
PROF::init(false);
continue;
case 'P':
command = 0;
SDLNet_TCP_Send( socket, (char*)PROF::hits, sizeof(MMU::flash)<<1 );
continue;
case 'f':
if(ch==';'){
try{
samplingFrequency = std::stoi(buffer);
}catch(std::exception ex){
std::cout << "Invalid Frequency:" << buffer << std::endl;
}
command = 0;
buffer.clear();
}else{
buffer += ch;
}
break;
}
}
}
};
std::list<Client> clients;
static void sample(){
if( !listener ) return;
u32 out[] = { GPIO::POUT0, GPIO::POUT1, GPIO::POUT2 };
SDLNet_TCP_Send( listener, (char*)out, 4*3 );
}
static void run(){
while( !hasQuit ){
TCPsocket clientSocket = SDLNet_TCP_Accept(server);
if( !clientSocket ){
std::this_thread::sleep_for( std::chrono::milliseconds(100) );
continue;
}
clients.emplace_back(clientSocket);
}
}
namespace PEX {
bool init( u32 port ){
NetManager::hold();
atexit([](){
hasQuit = true;
worker.join();
if( server )
SDLNet_TCP_Close(server);
clients.clear();
NetManager::release();
});
IPaddress ip;
if( SDLNet_ResolveHost( &ip, nullptr, port ) == -1 ){
std::printf("SDLNet_ResolveHost: %s\n", SDLNet_GetError());
return false;
}
server = SDLNet_TCP_Open(&ip);
if (!server) {
std::printf("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
return false;
}
worker = std::thread(run);
return true;
}
u32 update(){
u32 delta = CPU::cpuTotalTicks - lastTick;
lastTick = CPU::cpuTotalTicks;
if( !listener )
return ~0;
tti -= delta;
if( tti < 0 ){
sample();
tti += samplingFrequency;
if( tti < 0 )
tti = samplingFrequency;
}
return tti;
}
}