scummvm random work
[patches.git] / f.patch
1 Index: target_bfin_new.c
2 ===================================================================
3 --- target_bfin_new.c (revision 2656)
4 +++ target_bfin_new.c (working copy)
5 @@ -21,6 +21,7 @@
6 #include <config.h>
7 #endif /* HAVE_CONFIG_H */
8
9 +#include <errno.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 @@ -30,8 +31,11 @@
14 #include <time.h>
15 #include <stdarg.h>
16 #include <getopt.h>
17 +#include <pthread.h>
18 +#include <fcntl.h>
19
20 #include "gdbproxy.h"
21 +#include "rpmisc.h"
22
23 #include "part.h"
24 #include "chain.h"
25 @@ -4672,6 +4676,177 @@ bfin_out_treg (char *in, unsigned int re
26 return bfin_out_treg_value (in, reg_no, value);
27 }
28 \f
29 +#define DR_OUT (cpu->chain->parts->parts[cpu->chain->active_part]->active_instruction->data_register->out)
30 +#define DR_IN (cpu->chain->parts->parts[cpu->chain->active_part]->active_instruction->data_register->in)
31 +
32 +static unsigned int btc_port = 2001;
33 +static int btc_listen_sock = -1;
34 +
35 +static pthread_t btc_thread;
36 +
37 +static char *btc_read_buff = NULL;
38 +static size_t btc_read_len = 0;
39 +static inline void btc_read_maybe_queue(void)
40 +{
41 + if (DR_OUT->data[40 - 1 - 7] == 0)
42 + return;
43 +
44 + size_t i;
45 + uint32_t data = 0;
46 + for (i = 0; i < 32; ++i)
47 + data |= DR_OUT->data[i] << (32 - 1 - i);
48 +
49 + printf("got from Blackfin: 0x%08x\n", data);
50 +
51 + btc_read_buff = realloc(btc_read_buff, btc_read_len + 4);
52 + memcpy(btc_read_buff + btc_read_len, &data, 4);
53 + btc_read_len += 4;
54 +}
55 +
56 +static inline int jtag_strdup_parse_line(char *buf)
57 +{
58 + int ret;
59 + buf = strdup(buf);
60 + ret = jtag_parse_line(cpu->chain, buf);
61 + free(buf);
62 + return ret;
63 +}
64 +
65 +static size_t btc_write(size_t length, void *data)
66 +{
67 + size_t i, j, b;
68 + char *buf = data;
69 + char dr_cmd[] = "dr 0000000000000000000000000000000000000000"; /* need 40bits */
70 +
71 + /* in full duplex, we need to look at 7th bit
72 + * to see if Blackfin is sending to us
73 + */
74 + for (i = 0; i < length; i += 4) {
75 + dr_cmd[3 + 6] = '0';
76 + jtag_strdup_parse_line(dr_cmd);
77 + while (1) {
78 + jtag_strdup_parse_line("shift dr");
79 + //jtag_strdup_parse_line("dr");
80 + //printf("{ in:%s\n out:%s}\n", register_get_string(DR_IN), register_get_string(DR_OUT));
81 +
82 + btc_read_maybe_queue();
83 +
84 + if (DR_OUT->data[40 - 1 - 6] == 0)
85 + break;
86 + }
87 +
88 + dr_cmd[3 + 6] = '1';
89 + for (j = 0; j < 4; ++j) {
90 + //printf("byte: %c (0x%02x): 0y", buf[i+j], buf[i+j]);
91 + for (b = 0; b < 8; ++b) {
92 + char bit = ((buf[i+j] >> b) & 0x1) + '0';
93 + dr_cmd[3 + 8 + 8*j + b] = bit;
94 + //printf("%c", bit);
95 + }
96 + //printf("\n");
97 + }
98 + //printf("dr_cmd: %s\n", dr_cmd);
99 + jtag_strdup_parse_line(dr_cmd);
100 + jtag_strdup_parse_line("shift dr");
101 + btc_read_maybe_queue();
102 + }
103 +
104 + return 0;
105 +}
106 +
107 +static void *btc_thread_main(void *arg)
108 +{
109 + int btc_sock;
110 + ssize_t io_ret;
111 + char buf[1024];
112 +
113 + {
114 + int fd = open("/dev/null", O_WRONLY);
115 + dup2(fd, STDOUT_FILENO);
116 + close(fd);
117 + }
118 +
119 + while (1) {
120 + btc_sock = sock_accept(btc_listen_sock);
121 + fcntl(btc_sock, F_SETFL, fcntl(btc_sock, F_GETFL) | O_NONBLOCK);
122 +// dup2(btc_sock, STDOUT_FILENO);
123 +
124 + jtag_strdup_parse_line("instruction EMUDAT_40_SCAN");
125 + jtag_strdup_parse_line("shift ir");
126 +
127 + while (1) {
128 +#if 0
129 + memset(buf, 'a', sizeof(buf));
130 + io_ret = read(btc_sock, buf, sizeof(buf)-1);
131 + if (io_ret <= 0)
132 + break;
133 + while (io_ret > 0)
134 + if (buf[io_ret-1] == '\n' || buf[io_ret-1] == '\r')
135 + --io_ret;
136 + else
137 + break;
138 + if (io_ret <= 0)
139 + continue;
140 + buf[io_ret] = '\0';
141 + printf("SOCKET: '%s'\n", buf);
142 +
143 + if (buf[0] == '!')
144 + printf("jtag_parse_line: %i\n", jtag_parse_line(cpu->chain, buf+1));
145 + else
146 + btc_write(io_ret, buf);
147 +#else
148 + char byte;
149 + io_ret = read(btc_sock, &byte, 1);
150 + if (io_ret == 0 || (io_ret == -1 && errno != EAGAIN))
151 + break;
152 +
153 + if (io_ret > 0)
154 + btc_write(1, &byte);
155 + else {
156 + jtag_strdup_parse_line("dr 0000000000000000000000000000000000000000");
157 + jtag_strdup_parse_line("shift dr");
158 + btc_read_maybe_queue();
159 + }
160 +
161 + if (btc_read_len) {
162 + write(btc_sock, btc_read_buff, 1);
163 + btc_read_len = 0;
164 + }
165 +#endif
166 + }
167 +
168 + close(btc_sock);
169 + }
170 +
171 + return NULL;
172 +}
173 +
174 +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
175 +static void bfin_btc_init(void)
176 +{
177 + size_t i;
178 + char *btc_command_init[] = {
179 + /* declare a 40bit version of emudat */
180 + "register EMUDAT_40 40",
181 + "instruction EMUDAT_40_SCAN 10100 EMUDAT_40",
182 +
183 + /* set emudat to 40bits */
184 + "instruction DBGCTL_SCAN",
185 + "shift ir",
186 + "shift dr",
187 + "dr 0000000100000000",
188 + "shift dr"
189 + };
190 +
191 + btc_listen_sock = listen_sock_open(&btc_port);
192 + if (btc_listen_sock == -1)
193 + return;
194 +
195 + pthread_create(&btc_thread, NULL, btc_thread_main, NULL);
196 + for (i = 0; i < ARRAY_SIZE(btc_command_init); ++i)
197 + jtag_strdup_parse_line(btc_command_init[i]);
198 +}
199 +\f
200
201 /* Target method */
202 static void
203 @@ -5208,6 +5383,8 @@ bfin_open (int argc,
204 cpu->cores[0].name = "Core";
205 }
206
207 + bfin_btc_init();
208 +
209 return RP_VAL_TARGETRET_OK;
210 }
211
212 Index: rpmisc.c
213 ===================================================================
214 --- rpmisc.c (revision 2656)
215 +++ rpmisc.c (working copy)
216 @@ -98,13 +98,14 @@
217
218
219 /* Sockets */
220 -#if defined(WIN32)
221 +#if !defined(WIN32)
222 +# undef INVALID_SOCKET
223 +# define INVALID_SOCKET -1
224 +# undef SOCKET_ERROR
225 +# define SOCKET_ERROR -1
226 +#endif
227 static int dbg_sock = INVALID_SOCKET;
228 static int dbg_listen_sock = INVALID_SOCKET;
229 -#else
230 -static int dbg_sock = -1;
231 -static int dbg_listen_sock = -1;
232 -#endif
233
234 /* Log functions */
235 static void rp_log_local(int level, const char *fmt, ...);
236 @@ -140,29 +141,21 @@ void dbg_sock_cleanup(void)
237 }
238
239 /* Open listen socket in a mode expected by gdb */
240 -int dbg_listen_sock_open(unsigned int *port)
241 +int listen_sock_open(unsigned int *port)
242 {
243 struct sockaddr_in sa;
244 int tmp;
245 int ret;
246 unsigned int p;
247 + int sock;
248
249 assert(port == NULL || *port < 0x10000);
250
251 -#if defined(WIN32)
252 - assert(dbg_sock == INVALID_SOCKET && dbg_listen_sock == INVALID_SOCKET);
253 -
254 - if ((dbg_listen_sock = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
255 - return FALSE;
256 -#else
257 - assert(dbg_sock == -1 && dbg_listen_sock == -1);
258 -
259 - if ((dbg_listen_sock = socket(PF_INET, SOCK_STREAM, 0)) < 0)
260 - return FALSE;
261 -#endif
262 + if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
263 + return INVALID_SOCKET;
264
265 tmp = 1;
266 - setsockopt(dbg_listen_sock,
267 + setsockopt(sock,
268 SOL_SOCKET,
269 SO_REUSEADDR,
270 (char *) &tmp,
271 @@ -178,13 +171,8 @@ int dbg_listen_sock_open(unsigned int *p
272 else
273 sa.sin_port = htons((unsigned short int) *port);
274
275 -#if defined(WIN32)
276 - if ((ret = bind(dbg_listen_sock, (struct sockaddr *) &sa, sizeof (sa))) == SOCKET_ERROR)
277 - return FALSE;
278 -#else
279 - if ((ret = bind(dbg_listen_sock, (struct sockaddr *) &sa, sizeof (sa))) != 0)
280 - return FALSE;
281 -#endif
282 + if ((ret = bind(sock, (struct sockaddr *) &sa, sizeof (sa))) == SOCKET_ERROR)
283 + return INVALID_SOCKET;
284 }
285 else
286 {
287 @@ -194,79 +182,68 @@ int dbg_listen_sock_open(unsigned int *p
288 {
289 sa.sin_port = htons(p);
290
291 -#if defined(WIN32)
292 - if ((ret = bind(dbg_listen_sock, (struct sockaddr *) &sa, sizeof (sa))) != SOCKET_ERROR)
293 + if ((ret = bind(sock, (struct sockaddr *) &sa, sizeof (sa))) != SOCKET_ERROR)
294 break;
295 -#else
296 - if ((ret = bind(dbg_listen_sock, (struct sockaddr *) &sa, sizeof (sa))) == 0)
297 - break;
298 -#endif
299 }
300
301 if (p == 0x10000)
302 {
303 /* No sockets available */
304 - return FALSE;
305 + return INVALID_SOCKET;
306 }
307
308 *port = p;
309 }
310
311 -#if defined(WIN32)
312 - assert(dbg_listen_sock != INVALID_SOCKET);
313 + assert(sock != INVALID_SOCKET);
314
315 - if ((ret = listen(dbg_listen_sock, 1)) == SOCKET_ERROR)
316 - return FALSE;
317 -#else
318 - assert(dbg_listen_sock >= 0);
319 + if ((ret = listen(sock, 1)) == SOCKET_ERROR)
320 + return INVALID_SOCKET;
321
322 - if ((ret = listen(dbg_listen_sock, 1)) != 0)
323 - return FALSE;
324 -#endif
325 + return sock;
326 +}
327 +int dbg_listen_sock_open(unsigned int *port)
328 +{
329 + assert(dbg_sock == INVALID_SOCKET && dbg_listen_sock == INVALID_SOCKET);
330
331 - return TRUE;
332 + dbg_listen_sock = listen_sock_open(port);
333 +
334 + return (dbg_listen_sock == INVALID_SOCKET ? FALSE : TRUE);
335 }
336
337 /* Accept incoming connection and set mode expected by gdb */
338 -int dbg_sock_accept(void)
339 +int sock_accept(int listen_sock)
340 {
341 socklen_t tmp;
342 struct sockaddr_in sa;
343 struct protoent *pe;
344 -
345 -#if defined(WIN32)
346 - assert(dbg_sock == INVALID_SOCKET);
347 - assert(dbg_listen_sock != INVALID_SOCKET);
348 + int sock;
349
350 tmp = sizeof(sa);
351 - if ((dbg_sock = accept(dbg_listen_sock, (struct sockaddr *) &sa, &tmp))
352 + if ((sock = accept(listen_sock, (struct sockaddr *) &sa, &tmp))
353 == INVALID_SOCKET)
354 {
355 - return FALSE;
356 + return INVALID_SOCKET;
357 }
358
359 if ((pe = getprotobyname("tcp")) == NULL)
360 - return FALSE;
361 -#else
362 - assert(dbg_sock < 0);
363 - assert(dbg_listen_sock >= 0);
364 -
365 - tmp = sizeof(sa);
366 - if ((dbg_sock = accept(dbg_listen_sock, (struct sockaddr *) &sa, &tmp)) < 0)
367 - return FALSE;
368 -
369 - if ((pe = getprotobyname("tcp")) == NULL)
370 - return FALSE;
371 -#endif
372 + return INVALID_SOCKET;
373
374 tmp = 1;
375 - setsockopt(dbg_sock,
376 + setsockopt(sock,
377 pe->p_proto,
378 TCP_NODELAY,
379 (char *) &tmp,
380 sizeof(tmp));
381
382 - return TRUE;
383 + return sock;
384 +}
385 +int dbg_sock_accept(void)
386 +{
387 + assert(dbg_sock == INVALID_SOCKET);
388 + assert(dbg_listen_sock != INVALID_SOCKET);
389 + dbg_sock = sock_accept(dbg_listen_sock);
390 + return (dbg_sock == INVALID_SOCKET ? FALSE : TRUE);
391 }
392
393 /* Close connection to debugger side */
394 Index: rpmisc.h
395 ===================================================================
396 --- rpmisc.h (revision 2656)
397 +++ rpmisc.h (working copy)
398 @@ -39,6 +39,9 @@ int dbg_sock_accept(void);
399 int dbg_sock_readchar(int ms);
400 int dbg_listen_sock_open(unsigned int *port);
401
402 +int listen_sock_open(unsigned int *port);
403 +int sock_accept(int listen_sock);
404 +
405 /* Return values for readchar: either character
406 code or one of the following*/
407 #define RP_VAL_MISCREADCHARRET_TMOUT (-2)