Advertisement
sbaldovi

fuse.spectranet_win32_03.diff

Jun 26th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 14.29 KB | None | 0 0
  1. Index: peripherals/spectranet.c
  2. ===================================================================
  3. --- peripherals/spectranet.c    (revision 4510)
  4. +++ peripherals/spectranet.c    (working copy)
  5. @@ -410,6 +410,22 @@
  6.    return 0;
  7.  }
  8.  
  9. +libspectrum_error
  10. +spectranet_end( void )
  11. +{
  12. +  if( w5100 ) {
  13. +    nic_w5100_free( w5100 );
  14. +    w5100 = NULL;
  15. +  }
  16. +
  17. +  if( flash_rom ) {
  18. +    flash_am29f010_free( flash_rom );
  19. +    flash_rom = NULL;
  20. +  }
  21. +
  22. +  return LIBSPECTRUM_ERROR_NONE;
  23. +}
  24. +
  25.  static libspectrum_word
  26.  get_w5100_register( memory_page *page, libspectrum_word address )
  27.  {
  28. Index: peripherals/spectranet.h
  29. ===================================================================
  30. --- peripherals/spectranet.h    (revision 4510)
  31. +++ peripherals/spectranet.h    (working copy)
  32. @@ -27,6 +27,7 @@
  33.  #define FUSE_SPECTRANET_H
  34.  
  35.  int spectranet_init( void );
  36. +libspectrum_error spectranet_end( void );
  37.  void spectranet_page( int via_io );
  38.  void spectranet_unpage( void );
  39.  
  40. Index: peripherals/nic/w5100_internals.h
  41. ===================================================================
  42. --- peripherals/nic/w5100_internals.h   (revision 4510)
  43. +++ peripherals/nic/w5100_internals.h   (working copy)
  44. @@ -29,6 +29,26 @@
  45.  #ifndef FUSE_W5100_INTERNALS_H
  46.  #define FUSE_W5100_INTERNALS_H
  47.  
  48. +#ifdef WIN32
  49. +#include <signal.h>
  50. +#endif             /* #ifdef WIN32 */
  51. +
  52. +#ifdef WIN32
  53. +
  54. +#define close_socket( x ) closesocket( x )
  55. +#define socket_error WSAGetLastError()
  56. +#define POLLING_INTERVAL 100000
  57. +typedef SOCKET socket_t;
  58. +
  59. +#else
  60. +
  61. +#define INVALID_SOCKET -1
  62. +#define close_socket( x ) close( x )
  63. +#define socket_error errno
  64. +typedef int socket_t;
  65. +
  66. +#endif             /* #ifdef WIN32 */
  67. +
  68.  typedef enum w5100_socket_mode {
  69.    W5100_SOCKET_MODE_CLOSED = 0x00,
  70.    W5100_SOCKET_MODE_TCP,
  71. @@ -106,7 +126,7 @@
  72.  
  73.    /* Host properties */
  74.  
  75. -  int fd;                   /* Socket file descriptor */
  76. +  socket_t fd;              /* Socket file descriptor */
  77.    int socket_bound;         /* True once we've bound the socket to a port */
  78.    int write_pending;        /* True if we're waiting to write data on this socket */
  79.  
  80. Index: peripherals/nic/w5100_socket.c
  81. ===================================================================
  82. --- peripherals/nic/w5100_socket.c  (revision 4510)
  83. +++ peripherals/nic/w5100_socket.c  (working copy)
  84. @@ -28,15 +28,20 @@
  85.  
  86.  #include "config.h"
  87.  
  88. -#include <arpa/inet.h>
  89.  #include <errno.h>
  90. -#include <netinet/in.h>
  91.  #include <pthread.h>
  92.  #include <string.h>
  93. -#include <sys/socket.h>
  94.  #include <sys/types.h>
  95.  #include <unistd.h>
  96.  
  97. +#ifdef WIN32
  98. +#include <winsock2.h>
  99. +#else
  100. +#include <arpa/inet.h>
  101. +#include <netinet/in.h>
  102. +#include <sys/socket.h>
  103. +#endif             /* #ifdef WIN32 */
  104. +
  105.  #include "fuse.h"
  106.  #include "ui/ui.h"
  107.  #include "w5100.h"
  108. @@ -55,7 +60,7 @@
  109.  nic_w5100_socket_init( nic_w5100_socket_t *socket, int which )
  110.  {
  111.    socket->id = which;
  112. -  socket->fd = -1;
  113. +  socket->fd = INVALID_SOCKET;
  114.    socket->socket_bound = 0;
  115.    socket->ok_for_io = 0;
  116.    pthread_mutex_init( &socket->lock, NULL );
  117. @@ -113,10 +118,10 @@
  118.  void
  119.  nic_w5100_socket_free( nic_w5100_socket_t *socket )
  120.  {
  121. -  if( socket->fd != -1 ) {
  122. +  if( socket->fd != INVALID_SOCKET ) {
  123.      w5100_socket_acquire_lock( socket );
  124. -    close( socket->fd );
  125. -    socket->fd = -1;
  126. +    close_socket( socket->fd );
  127. +    socket->fd = INVALID_SOCKET;
  128.      socket->socket_bound = 0;
  129.      socket->ok_for_io = 0;
  130.      socket->write_pending = 0;
  131. @@ -171,8 +176,8 @@
  132.  
  133.      w5100_socket_acquire_lock( socket_obj );
  134.      socket_obj->fd = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
  135. -    if( socket_obj->fd == -1 ) {
  136. -      printf("w5100: failed to open UDP socket for socket %d; errno = %d : %s\n", socket_obj->id, errno, strerror(errno));
  137. +    if( socket_obj->fd == INVALID_SOCKET ) {
  138. +      printf("w5100: failed to open UDP socket for socket %d; errno = %d : %s\n", socket_obj->id, socket_error, strerror(socket_error));
  139.        w5100_socket_release_lock( socket_obj );
  140.        return;
  141.      }
  142. @@ -187,8 +192,8 @@
  143.  
  144.      w5100_socket_acquire_lock( socket_obj );
  145.      socket_obj->fd = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
  146. -    if( socket_obj->fd == -1 ) {
  147. -      printf("w5100: failed to open TCP socket for socket %d; errno = %d : %s\n", socket_obj->id, errno, strerror(errno));
  148. +    if( socket_obj->fd == INVALID_SOCKET ) {
  149. +      printf("w5100: failed to open TCP socket for socket %d; errno = %d : %s\n", socket_obj->id, socket_error, strerror(socket_error));
  150.        w5100_socket_release_lock( socket_obj );
  151.        return;
  152.      }
  153. @@ -211,8 +216,7 @@
  154.  
  155.    printf("w5100: attempting to bind socket IP %s\n", inet_ntoa(sa.sin_addr));
  156.    if( bind( socket->fd, (struct sockaddr*)&sa, sizeof(sa) ) == -1 ) {
  157. -    printf("w5100: failed to bind socket %d; errno = %d : %s\n", socket->id, errno, strerror(errno));
  158. -    w5100_socket_release_lock( socket );
  159. +    printf("w5100: failed to bind socket %d; errno = %d : %s\n", socket->id, socket_error, strerror(socket_error));
  160.      return;
  161.    }
  162.  
  163. @@ -236,7 +240,7 @@
  164.      memcpy( &sa.sin_addr.s_addr, socket->dip, 4 );
  165.  
  166.      if( connect( socket->fd, (struct sockaddr*)&sa, sizeof(sa) ) == -1 ) {
  167. -      printf("w5100: failed to connect socket %d to 0x%08x:0x%04x; errno = %d : %s\n", socket->id, ntohl(sa.sin_addr.s_addr), ntohs(sa.sin_port), errno, strerror(errno));
  168. +      printf("w5100: failed to connect socket %d to 0x%08x:0x%04x; errno = %d : %s\n", socket->id, ntohl(sa.sin_addr.s_addr), ntohs(sa.sin_port), socket_error, strerror(socket_error));
  169.        socket->ir |= 1 << 3;
  170.        socket->state = W5100_SOCKET_STATE_CLOSED;
  171.        w5100_socket_release_lock( socket );
  172. @@ -268,9 +272,9 @@
  173.  w5100_socket_close( nic_w5100_t *self, nic_w5100_socket_t *socket )
  174.  {
  175.    w5100_socket_acquire_lock( socket );
  176. -  if( socket->fd != -1 ) {
  177. -    close( socket->fd );
  178. -    socket->fd = -1;
  179. +  if( socket->fd != INVALID_SOCKET ) {
  180. +    close_socket( socket->fd );
  181. +    socket->fd = INVALID_SOCKET;
  182.      socket->socket_bound = 0;
  183.      socket->ok_for_io = 0;
  184.      socket->state = W5100_SOCKET_STATE_CLOSED;
  185. @@ -403,6 +407,7 @@
  186.        printf("w5100: reading 0x%02x from unsupported register 0x%03x\n", b, reg );
  187.        break;
  188.    }
  189. +
  190.    return b;
  191.  }
  192.  
  193. @@ -481,7 +486,7 @@
  194.  {
  195.    w5100_socket_acquire_lock( socket );
  196.  
  197. -  if( socket->fd != -1 ) {
  198. +  if( socket->fd != INVALID_SOCKET ) {
  199.      /* We can process a UDP read if we're in a UDP state and there are at least
  200.         9 bytes free in our buffer (8 byte UDP header and 1 byte of actual
  201.         data). */
  202. @@ -528,11 +533,11 @@
  203.      sa.sin_family = AF_INET;
  204.      memcpy( &sa.sin_port, socket->port, 2 );
  205.      sa.sin_addr.s_addr = htonl(INADDR_ANY);
  206. -    bytes_read = recvfrom( socket->fd, buffer + 8, bytes_free - 8, 0, (struct sockaddr*)&sa, &sa_length );
  207. +    bytes_read = recvfrom( socket->fd, (char *)buffer + 8, bytes_free - 8, 0, (struct sockaddr*)&sa, &sa_length );
  208.      printf("w5100: read 0x%03x bytes from UDP socket %d\n", (int)bytes_read, socket->id);
  209.    }
  210.    else {
  211. -    bytes_read = recv( socket->fd, buffer, bytes_free, 0 );
  212. +    bytes_read = recv( socket->fd, (char *)buffer, bytes_free, 0 );
  213.      printf("w5100: read 0x%03x bytes from TCP socket %d\n", (int)bytes_read, socket->id);
  214.    }
  215.  
  216. @@ -562,7 +567,7 @@
  217.      }
  218.    }
  219.    else {
  220. -    printf("w5100: error %d reading from socket %d : %s\n", errno, socket->id, strerror(errno));
  221. +    printf("w5100: error %d reading from socket %d : %s\n", socket_error, socket->id, strerror(socket_error));
  222.    }
  223.  }
  224.  
  225. @@ -592,7 +597,7 @@
  226.    memcpy( &sa.sin_port, socket->dport, 2 );
  227.    memcpy( &sa.sin_addr.s_addr, socket->dip, 4 );
  228.  
  229. -  bytes_sent = sendto( socket->fd, data, length, 0, (struct sockaddr*)&sa, sizeof(sa) );
  230. +  bytes_sent = sendto( socket->fd, (const char *)data, length, 0, (struct sockaddr*)&sa, sizeof(sa) );
  231.    printf("w5100: sent 0x%03x bytes of 0x%03x to UDP socket %d\n", (int)bytes_sent, length, socket->id);
  232.  
  233.    if( bytes_sent == length ) {
  234. @@ -609,7 +614,7 @@
  235.    else if( bytes_sent != -1 )
  236.      printf("w5100: didn't manage to send full datagram to UDP socket %d?\n", socket->id);
  237.    else
  238. -    printf("w5100: error %d writing to UDP socket %d : %s\n", errno, socket->id, strerror(errno));
  239. +    printf("w5100: error %d writing to UDP socket %d : %s\n", socket_error, socket->id, strerror(socket_error));
  240.  }
  241.  
  242.  static void
  243. @@ -626,7 +631,7 @@
  244.    if( offset + length > 0x800 )
  245.      length = 0x800 - offset;
  246.  
  247. -  bytes_sent = send( socket->fd, data, length, 0 );
  248. +  bytes_sent = send( socket->fd, (const char*)data, length, 0 );
  249.    printf("w5100: sent 0x%03x bytes of 0x%03x to TCP socket %d\n", (int)bytes_sent, length, socket->id);
  250.  
  251.    if( bytes_sent != -1 ) {
  252. @@ -637,7 +642,7 @@
  253.      }
  254.    }
  255.    else
  256. -    printf("w5100: error %d writing to TCP socket %d : %s\n", errno, socket->id, strerror(errno));
  257. +    printf("w5100: error %d writing to TCP socket %d : %s\n", socket_error, socket->id, strerror(socket_error));
  258.  }
  259.  
  260.  void
  261. @@ -648,7 +653,7 @@
  262.  
  263.    /* Process only if we're an open socket, and we haven't been closed and
  264.       re-opened since the select() started */
  265. -  if( socket->fd != -1 && socket->ok_for_io ) {
  266. +  if( socket->fd != INVALID_SOCKET && socket->ok_for_io ) {
  267.      if( FD_ISSET( socket->fd, &readfds ) )
  268.        w5100_socket_process_read( socket );
  269.  
  270. Index: peripherals/nic/w5100.c
  271. ===================================================================
  272. --- peripherals/nic/w5100.c (revision 4510)
  273. +++ peripherals/nic/w5100.c (working copy)
  274. @@ -29,12 +29,21 @@
  275.  #include "config.h"
  276.  
  277.  #include <errno.h>
  278. -#include <netinet/in.h>
  279.  #include <pthread.h>
  280. -#include <sys/socket.h>
  281.  #include <sys/types.h>
  282.  #include <unistd.h>
  283.  
  284. +#ifdef WIN32
  285. +
  286. +#include <winsock2.h>
  287. +
  288. +#else
  289. +
  290. +#include <netinet/in.h>
  291. +#include <sys/socket.h>
  292. +
  293. +#endif /* #ifdef WIN32 */
  294. +
  295.  #include "fuse.h"
  296.  #include "ui/ui.h"
  297.  #include "w5100.h"
  298. @@ -92,20 +101,29 @@
  299.  {
  300.    nic_w5100_t *self = arg;
  301.    int i;
  302. +  fd_set readfds, writefds;
  303. +  int max_fd;
  304.  
  305. +#ifdef WIN32
  306. +  struct timeval tv;
  307. +  fd_set *readfds_p, *writefds_p;
  308. +#endif /* ifdef WIN32 */
  309. +  
  310.    while( !self->stop_io_thread ) {
  311. -    fd_set readfds, writefds;
  312. -    int max_fd = self->pipe_read;
  313. +    max_fd = -1;
  314. +    FD_ZERO( &readfds );
  315. +    FD_ZERO( &writefds );
  316.  
  317. -    FD_ZERO( &readfds );
  318. +#ifndef WIN32
  319. +    max_fd = self->pipe_read;
  320.      FD_SET( self->pipe_read, &readfds );
  321. +#endif /* ifndef WIN32 */
  322.  
  323. -    FD_ZERO( &writefds );
  324. -
  325.      for( i = 0; i < 4; i++ )
  326.        nic_w5100_socket_add_to_sets( &self->socket[i], &readfds, &writefds,
  327.          &max_fd );
  328.  
  329. +#ifndef WIN32
  330.      printf("w5100: io thread select\n");
  331.  
  332.      select( max_fd + 1, &readfds, &writefds, NULL, NULL );
  333. @@ -117,7 +135,31 @@
  334.        printf("w5100: discarding pipe data\n");
  335.        read( self->pipe_read, &bitbucket, 1 );
  336.      }
  337. +#else
  338. +    /* Set max wait time upto 0.1 sec if not IO is completed */
  339. +    tv.tv_sec = 0;
  340. +    tv.tv_usec = POLLING_INTERVAL;
  341.  
  342. +    readfds_p = ( readfds.fd_count )? &readfds : NULL;
  343. +    writefds_p = ( writefds.fd_count )? &writefds : NULL;
  344. +
  345. +    /* Call select() only if there are pending sockets */
  346. +    if( readfds_p || writefds_p )
  347. +    {
  348. +      printf("w5100: io thread select\n");
  349. +
  350. +      if( select( max_fd + 1, readfds_p, writefds_p, NULL, &tv ) < 0 ) {
  351. +        printf( "w5100: io thread wake from select with error %d\n",
  352. +        socket_error );
  353. +      }
  354. +    }
  355. +    else {
  356. +      usleep( tv.tv_usec );
  357. +    }
  358. +
  359. +    printf("w5100: io thread wake\n");
  360. +#endif /* ifndef WIN32 */
  361. +
  362.      for( i = 0; i < 4; i++ )
  363.        nic_w5100_socket_process_io( &self->socket[i], readfds, writefds );
  364.    }
  365. @@ -128,17 +170,38 @@
  366.  void
  367.  nic_w5100_wake_io_thread( nic_w5100_t *self )
  368.  {
  369. +#ifndef WIN32
  370.    const char dummy = 0;
  371.    write( self->pipe_write, &dummy, 1 );
  372. +#endif /* ifndef WIN32 */
  373.  }
  374.  
  375.  nic_w5100_t*
  376.  nic_w5100_alloc( void )
  377.  {
  378.    int error;
  379. -  int pipefd[2];
  380.    int i;
  381.  
  382. +#ifndef WIN32
  383. +  int pipefd[2];
  384. +#else
  385. +  WORD wVersionRequested;
  386. +  WSADATA wsaData;
  387. +
  388. +  wVersionRequested = MAKEWORD( 2, 2 );
  389. +  error = WSAStartup( wVersionRequested, &wsaData );
  390. +  if( error ) {
  391. +    ui_error( UI_ERROR_ERROR, "WSAStartup failed with error: %d", error );
  392. +    fuse_abort();
  393. +  }
  394. +  printf("WSAStartup\n");
  395. +  if( LOBYTE( wsaData.wVersion ) != 2 || HIBYTE( wsaData.wVersion ) != 2 ) {
  396. +    WSACleanup();
  397. +    ui_error( UI_ERROR_ERROR, "FUSE could not find an usable Winsock DLL" );
  398. +    fuse_abort();
  399. +  }
  400. +#endif /* ifndef WIN32 */
  401. +
  402.    nic_w5100_t *self = malloc( sizeof( *self ) );
  403.    if( !self ) {
  404.      ui_error( UI_ERROR_ERROR, "%s:%d out of memory", __FILE__, __LINE__ );
  405. @@ -150,6 +213,7 @@
  406.  
  407.    w5100_reset( self );
  408.  
  409. +#ifndef WIN32
  410.    error = pipe( pipefd );
  411.    if( error ) {
  412.      ui_error( UI_ERROR_ERROR, "w5100: error %d creating pipe", error );
  413. @@ -158,6 +222,7 @@
  414.  
  415.    self->pipe_read = pipefd[0];
  416.    self->pipe_write = pipefd[1];
  417. +#endif /* ifndef WIN32 */
  418.  
  419.    self->stop_io_thread = 0;
  420.  
  421. @@ -178,11 +243,22 @@
  422.    self->stop_io_thread = 1;
  423.    nic_w5100_wake_io_thread( self );
  424.  
  425. +#ifdef WIN32
  426. +  /* wait IO thread wake up */
  427. +  usleep( 2 * POLLING_INTERVAL );
  428. +#endif /* ifdef WIN32 */
  429. +
  430.    pthread_join( self->thread, NULL );
  431.  
  432.    for( i = 0; i < 4; i++ )
  433.      nic_w5100_socket_free( &self->socket[i] );
  434. -    
  435. +
  436. +#ifdef WIN32
  437. +  printf("WSACleanup\n");
  438. +  if( WSACleanup() )
  439. +    printf( "Error in WSACleanup: %d\n", socket_error );
  440. +#endif /* ifdef WIN32 */
  441. +
  442.    free( self );
  443.  }
  444.  
  445. Index: fuse.c
  446. ===================================================================
  447. --- fuse.c  (revision 4510)
  448. +++ fuse.c  (working copy)
  449. @@ -831,6 +831,7 @@
  450.    opus_end();
  451.    plusd_end();
  452.    disciple_end();
  453. +  spectranet_end();
  454.  
  455.    machine_end();
  456.  
  457. Index: configure.in
  458. ===================================================================
  459. --- configure.in    (revision 4510)
  460. +++ configure.in    (working copy)
  461. @@ -110,7 +110,7 @@
  462.  AC_MSG_RESULT($win32)
  463.  if test "$win32" = yes; then
  464.    AC_CHECK_HEADER(windows.h,
  465. -                  LIBS="$LIBS -mwindows -lcomctl32 -lwinmm";
  466. +                  LIBS="$LIBS -mwindows -lcomctl32 -lwinmm -lpthread";
  467.                    AC_DEFINE([UI_WIN32], 1, [Defined if Win32 UI in use])
  468.                    AC_DEFINE([WINVER], 0x0400, [Minimal supported version of Windows is 95 or NT4])
  469.                    AC_DEFINE([_WIN32_IE], 0x400, [Internet Explorer is 4.0 or higher is required])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement