changeset 803:236a184bf6f0

Merge
author Michael Pavone <pavone@retrodev.com>
date Sun, 26 Jul 2015 16:51:03 -0700
parents 6811f601008f (diff) 092524bb2e8f (current diff)
children 59e664fa2da8
files Makefile gdb_remote.c runtime.S runtime_32.S
diffstat 2 files changed, 76 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/Makefile	Sun Jul 26 16:32:34 2015 -0700
+++ b/Makefile	Sun Jul 26 16:51:03 2015 -0700
@@ -15,7 +15,7 @@
 BLASTEM:=blastem.exe
 CC:=wine gcc.exe
 CFLAGS:=-O2 -std=gnu99 -Wreturn-type -Werror=return-type -Werror=implicit-function-declaration -I"$(SDL2_PREFIX)/include/SDL2" -DGLEW_STATIC
-LDFLAGS:= $(GLEW32S_LIB) -L"$(SDL2_PREFIX)/lib" -lm -lmingw32 -lSDL2main -lSDL2 -lopengl32 -lglu32 -mwindows
+LDFLAGS:= $(GLEW32S_LIB) -L"$(SDL2_PREFIX)/lib" -lm -lmingw32 -lSDL2main -lSDL2 -lws2_32 -lopengl32 -lglu32 -mwindows
 CPU:=i686
 
 else
@@ -163,7 +163,7 @@
 
 vgmplay : vgmplay.o render_sdl.o $(CONFIGOBJS) $(AUDIOOBJS)
 	$(CC) -o vgmplay vgmplay.o render_sdl.o $(CONFIGOBJS) $(AUDIOOBJS) $(LDFLAGS)
-	
+
 test : test.o vdp.o
 	$(CC) -o test test.o vdp.o
 
--- a/gdb_remote.c	Sun Jul 26 16:32:34 2015 -0700
+++ b/gdb_remote.c	Sun Jul 26 16:51:03 2015 -0700
@@ -3,6 +3,23 @@
  This file is part of BlastEm.
  BlastEm is free software distributed under the terms of the GNU General Public License version 3 or greater. See COPYING for full license text.
 */
+#ifdef _WIN32
+#define WINVER 0x501
+#include <winsock2.h>
+#include <ws2tcpip.h>
+
+int gdb_sock;
+#define GDB_IN_FD gdb_sock
+#define GDB_OUT_FD gdb_sock
+#define GDB_READ(fd, buf, bufsize) recv(fd, buf, bufsize, 0)
+#define GDB_WRITE(fd, buf, bufsize) send(fd, buf, bufsize, 0)
+#else
+#define GDB_IN_FD STDIN_FILENO
+#define GDB_OUT_FD STDOUT_FILENO
+#define GDB_READ read
+#define GDB_WRITE write
+#endif
+
 #include "gdb_remote.h"
 #include "68kinst.h"
 #include "debug.h"
@@ -14,6 +31,8 @@
 #include <stdio.h>
 #include <string.h>
 
+
+
 #define INITIAL_BUFFER_SIZE (16*1024)
 
 #ifdef DO_DEBUG_PRINT
@@ -76,7 +95,7 @@
 
 void write_or_die(int fd, const void *buf, size_t count)
 {
-	if (write(fd, buf, count) < count) {
+	if (GDB_WRITE(fd, buf, count) < count) {
 		fatal_error("Error writing to stdout\n");
 	}
 }
@@ -84,11 +103,11 @@
 void gdb_send_command(char * command)
 {
 	char end[3];
-	write_or_die(STDOUT_FILENO, "$", 1);
-	write_or_die(STDOUT_FILENO, command, strlen(command));
+	write_or_die(GDB_OUT_FD, "$", 1);
+	write_or_die(GDB_OUT_FD, command, strlen(command));
 	end[0] = '#';
 	gdb_calc_checksum(command, end+1);
-	write_or_die(STDOUT_FILENO, end, 3);
+	write_or_die(GDB_OUT_FD, end, 3);
 	dfprintf(stderr, "Sent $%s#%c%c\n", command, end[1], end[2]);
 }
 
@@ -120,11 +139,11 @@
 	//TODO: Use generated read/write functions to support access to hardware that is not ROM or RAM
 	uint16_t * word = get_native_pointer(address & 0xFFFFFFFE, (void **)context->mem_pointers, &context->options->gen);
 	if (word) {	
-		if (address & 1) {
-			return *word;
-		}
-		return *word >> 8;
+	if (address & 1) {
+		return *word;
 	}
+	return *word >> 8;
+}
 	if (address >= 0xA00000 && address < 0xA04000) {
 		return gen->zram[address & 0x1FFF];
 	}
@@ -152,7 +171,7 @@
 		z80_ram[address & 0x1FFF] = value;
 		genesis_context * gen = context->system;
 #ifndef NO_Z80
-		z80_handle_code_write(address & 0x1FFF, gen->z80);
+		z80_handle_code_GDB_WRITE(address & 0x1FFF, gen->z80);
 #endif
 		return;
 	} else {
@@ -479,7 +498,12 @@
 	while(!cont)
 	{
 		if (!curbuf) {
-			int numread = read(STDIN_FILENO, buf, bufsize);
+			int numread = GDB_READ(GDB_IN_FD, buf, bufsize);
+			if (numread < 0) {
+				fputs("Failed to read on GDB input file descriptor\n", stderr);
+				exit(1);
+			}
+			dfprintf(stderr, "read %d bytes\n", numread);
 			curbuf = buf;
 			end = buf + numread;
 		} else if (partial) {
@@ -487,7 +511,7 @@
 				memmove(curbuf, buf, end-curbuf);
 				end -= curbuf - buf;
 			}
-			int numread = read(STDIN_FILENO, end, bufsize - (end-buf));
+			int numread = read(GDB_IN_FD, end, bufsize - (end-buf));
 			end += numread;
 			curbuf = buf;
 		}
@@ -507,7 +531,7 @@
 						//Null terminate payload
 						*curbuf = 0;
 						//send acknowledgement
-						if (write(STDOUT_FILENO, "+", 1) < 1) {
+						if (GDB_WRITE(GDB_OUT_FD, "+", 1) < 1) {
 							fatal_error("Error writing to stdout\n");
 						}
 						gdb_run_command(context, pc, start);
@@ -529,9 +553,47 @@
 	return context;
 }
 
+#ifdef _WIN32
+void gdb_cleanup(void)
+{
+	WSACleanup();
+}
+WSADATA wsa_data;
+#endif
+
 void gdb_remote_init(void)
 {
 	buf = malloc(INITIAL_BUFFER_SIZE);
 	curbuf = NULL;
 	bufsize = INITIAL_BUFFER_SIZE;
+#ifdef _WIN32
+	WSAStartup(MAKEWORD(2,2), &wsa_data);
+
+	struct addrinfo request, *result;
+	memset(&request, 0, sizeof(request));
+	request.ai_family = AF_UNSPEC;
+	request.ai_socktype = SOCK_STREAM;
+	request.ai_flags = AI_PASSIVE;
+	getaddrinfo(NULL, "1234", &request, &result);
+
+	int listen_sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
+	if (listen_sock < 0) {
+		fputs("Failed to open GDB remote debugging socket", stderr);
+		exit(1);
+	}
+	if (bind(listen_sock, result->ai_addr, result->ai_addrlen) < 0) {
+		fputs("Failed to bind GDB remote debugging socket", stderr);
+		exit(1);
+	}
+	if (listen(listen_sock, 1) < 0) {
+		fputs("Failed to listen on GDB remote debugging socket", stderr);
+		exit(1);
+	}
+	gdb_sock = accept(listen_sock, NULL, NULL);
+	if (gdb_sock < 0) {
+		fputs("accpet returned an error while listening on GDB remote debugging socket", stderr);
+		exit(1);
+	}
+	closesocket(listen_sock);
+#endif
 }