updating changelog
[drnoksnes] / netplay.h
1 /*******************************************************************************
2   Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3  
4   (c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com) and
5                             Jerremy Koot (jkoot@snes9x.com)
6
7   (c) Copyright 2001 - 2004 John Weidman (jweidman@slip.net)
8
9   (c) Copyright 2002 - 2004 Brad Jorsch (anomie@users.sourceforge.net),
10                             funkyass (funkyass@spam.shaw.ca),
11                             Joel Yliluoma (http://iki.fi/bisqwit/)
12                             Kris Bleakley (codeviolation@hotmail.com),
13                             Matthew Kendora,
14                             Nach (n-a-c-h@users.sourceforge.net),
15                             Peter Bortas (peter@bortas.org) and
16                             zones (kasumitokoduck@yahoo.com)
17
18   C4 x86 assembler and some C emulation code
19   (c) Copyright 2000 - 2003 zsKnight (zsknight@zsnes.com),
20                             _Demo_ (_demo_@zsnes.com), and Nach
21
22   C4 C++ code
23   (c) Copyright 2003 Brad Jorsch
24
25   DSP-1 emulator code
26   (c) Copyright 1998 - 2004 Ivar (ivar@snes9x.com), _Demo_, Gary Henderson,
27                             John Weidman, neviksti (neviksti@hotmail.com),
28                             Kris Bleakley, Andreas Naive
29
30   DSP-2 emulator code
31   (c) Copyright 2003 Kris Bleakley, John Weidman, neviksti, Matthew Kendora, and
32                      Lord Nightmare (lord_nightmare@users.sourceforge.net
33
34   OBC1 emulator code
35   (c) Copyright 2001 - 2004 zsKnight, pagefault (pagefault@zsnes.com) and
36                             Kris Bleakley
37   Ported from x86 assembler to C by sanmaiwashi
38
39   SPC7110 and RTC C++ emulator code
40   (c) Copyright 2002 Matthew Kendora with research by
41                      zsKnight, John Weidman, and Dark Force
42
43   S-DD1 C emulator code
44   (c) Copyright 2003 Brad Jorsch with research by
45                      Andreas Naive and John Weidman
46  
47   S-RTC C emulator code
48   (c) Copyright 2001 John Weidman
49   
50   ST010 C++ emulator code
51   (c) Copyright 2003 Feather, Kris Bleakley, John Weidman and Matthew Kendora
52
53   Super FX x86 assembler emulator code 
54   (c) Copyright 1998 - 2003 zsKnight, _Demo_, and pagefault 
55
56   Super FX C emulator code 
57   (c) Copyright 1997 - 1999 Ivar, Gary Henderson and John Weidman
58
59
60   SH assembler code partly based on x86 assembler code
61   (c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se) 
62
63  
64   Specific ports contains the works of other authors. See headers in
65   individual files.
66  
67   Snes9x homepage: http://www.snes9x.com
68  
69   Permission to use, copy, modify and distribute Snes9x in both binary and
70   source form, for non-commercial purposes, is hereby granted without fee,
71   providing that this license information and copyright notice appear with
72   all copies and any derived work.
73  
74   This software is provided 'as-is', without any express or implied
75   warranty. In no event shall the authors be held liable for any damages
76   arising from the use of this software.
77  
78   Snes9x is freeware for PERSONAL USE only. Commercial users should
79   seek permission of the copyright holders first. Commercial use includes
80   charging money for Snes9x or software derived from Snes9x.
81  
82   The copyright holders request that bug fixes and improvements to the code
83   should be forwarded to them so everyone can benefit from the modifications
84   in future versions.
85  
86   Super NES and Super Nintendo Entertainment System are trademarks of
87   Nintendo Co., Limited and its subsidiary companies.
88 *******************************************************************************/
89
90 #ifndef _NETPLAY_H_
91 #define _NETPLAY_H_
92
93 /*
94  * Client to server joypad update
95  *
96  * magic        1
97  * sequence_no  1
98  * opcode       1
99  * joypad data  4
100  *
101  * Server to client joypad update
102  * magic        1
103  * sequence_no  1
104  * opcode       1 + num joypads (top 3 bits)
105  * joypad data  4 * n
106  */
107
108 //#define NP_DEBUG 1
109
110 #define NP_VERSION 10
111 #define NP_JOYPAD_HIST_SIZE 120
112 #define NP_DEFAULT_PORT 6096
113
114 #define NP_MAX_CLIENTS 5
115
116 #define NP_SERV_MAGIC 'S'
117 #define NP_CLNT_MAGIC 'C'
118
119 #define NP_CLNT_HELLO 0
120 #define NP_CLNT_JOYPAD 1
121 #define NP_CLNT_RESET 2
122 #define NP_CLNT_PAUSE 3
123 #define NP_CLNT_LOAD_ROM 4
124 #define NP_CLNT_ROM_IMAGE 5
125 #define NP_CLNT_FREEZE_FILE 6
126 #define NP_CLNT_SRAM_DATA 7
127 #define NP_CLNT_READY 8
128 #define NP_CLNT_LOADED_ROM 9
129 #define NP_CLNT_RECEIVED_ROM_IMAGE 10
130 #define NP_CLNT_WAITING_FOR_ROM_IMAGE 11
131
132 #define NP_SERV_HELLO 0
133 #define NP_SERV_JOYPAD 1
134 #define NP_SERV_RESET 2
135 #define NP_SERV_PAUSE 3
136 #define NP_SERV_LOAD_ROM 4
137 #define NP_SERV_ROM_IMAGE 5
138 #define NP_SERV_FREEZE_FILE 6
139 #define NP_SERV_SRAM_DATA 7
140 #define NP_SERV_READY 8
141
142 struct SNPClient
143 {
144     volatile uint8 SendSequenceNum;
145     volatile uint8 ReceiveSequenceNum;
146     volatile bool8 Connected;
147     volatile bool8 SaidHello;
148     volatile bool8 Paused;
149     volatile bool8 Ready;
150     int Socket;
151     char *ROMName;
152     char *HostName;
153     char *Who;
154 };
155
156 enum {
157     NP_SERVER_SEND_ROM_IMAGE,
158     NP_SERVER_SYNC_ALL,
159     NP_SERVER_SYNC_CLIENT,
160     NP_SERVER_SEND_FREEZE_FILE_ALL,
161     NP_SERVER_SEND_ROM_LOAD_REQUEST_ALL,
162     NP_SERVER_RESET_ALL,
163     NP_SERVER_SEND_SRAM_ALL,
164     NP_SERVER_SEND_SRAM
165 };
166
167 #define NP_MAX_TASKS 20
168
169 struct NPServerTask
170 {
171     uint32 Task;
172     void  *Data;
173 };
174
175 struct SNPServer
176 {
177     struct SNPClient Clients [NP_MAX_CLIENTS];
178     int    NumClients;
179     volatile struct NPServerTask TaskQueue [NP_MAX_TASKS];
180     volatile uint32 TaskHead;
181     volatile uint32 TaskTail;
182     int    Socket;
183     uint32 FrameTime;
184     uint32 FrameCount;
185     char   ROMName [30];
186     uint32 Joypads [5];
187     bool8  ClientPaused;
188     uint32 Paused;
189     bool8  SendROMImageOnConnect;
190     bool8  SyncByReset;
191 };
192
193 #define NP_MAX_ACTION_LEN 200
194
195 struct SNetPlay
196 {
197     volatile uint8  MySequenceNum;
198     volatile uint8  ServerSequenceNum;
199     volatile bool8  Connected;
200     volatile bool8  Abort;
201     volatile uint8  Player;
202     volatile bool8  ClientsReady [NP_MAX_CLIENTS];
203     volatile bool8  ClientsPaused [NP_MAX_CLIENTS];
204     volatile bool8  Paused;
205     volatile bool8  PendingWait4Sync;
206     volatile uint8  PercentageComplete;
207     volatile bool8  Waiting4EmulationThread;
208     volatile bool8  Answer;
209     volatile int    Socket;
210     char *ServerHostName;
211     char *ROMName;
212     int Port;
213     volatile uint32 JoypadWriteInd;
214     volatile uint32 JoypadReadInd;
215     uint32 Joypads [NP_JOYPAD_HIST_SIZE][NP_MAX_CLIENTS];
216     uint32 Frame [NP_JOYPAD_HIST_SIZE];
217     uint32 FrameCount;
218     uint32 MaxFrameSkip;
219     uint32 MaxBehindFrameCount;
220     char   ActionMsg [NP_MAX_ACTION_LEN];
221     char   ErrorMsg [NP_MAX_ACTION_LEN];
222     char   WarningMsg [NP_MAX_ACTION_LEN];
223 };
224
225 extern "C" struct SNetPlay NetPlay;
226
227 //
228 // NETPLAY_CLIENT_HELLO message format:
229 // header
230 // frame_time (4)
231 // ROMName (variable)
232
233 #define WRITE_LONG(p, v) { \
234 *((p) + 0) = (uint8) ((v) >> 24); \
235 *((p) + 1) = (uint8) ((v) >> 16); \
236 *((p) + 2) = (uint8) ((v) >> 8); \
237 *((p) + 3) = (uint8) ((v) >> 0); \
238 }
239
240 #define READ_LONG(p) \
241 ((((uint8) *((p) + 0)) << 24) | \
242  (((uint8) *((p) + 1)) << 16) | \
243  (((uint8) *((p) + 2)) <<  8) | \
244  (((uint8) *((p) + 3)) <<  0))
245
246 bool8 S9xNPConnectToServer (const char *server_name, int port,
247                             const char *rom_name);
248 bool8 S9xNPWaitForHeartBeat ();
249 uint32 S9xNPGetJoypad (int which1);
250 bool8 S9xNPSendJoypadUpdate (uint32 joypad);
251 void S9xNPDisconnect ();
252 bool8 S9xNPInitialise ();
253 bool8 S9xNPSendData (int fd, const uint8 *data, int len);
254 bool8 S9xNPGetData (int fd, uint8 *data, int len);
255
256 void S9xNPSyncClients ();
257 void S9xNPStepJoypadHistory ();
258
259 void S9xNPResetJoypadReadPos ();
260 bool8 S9xNPSendReady (uint8 op = NP_CLNT_READY);
261 bool8 S9xNPSendPause (bool8 pause);
262 void S9xNPReset ();
263 void S9xNPSetAction (const char *action, bool8 force = FALSE);
264 void S9xNPSetError (const char *error);
265 void S9xNPSetWarning (const char *warning);
266 void S9xNPDiscardHeartbeats ();
267 void S9xNPServerQueueSendingFreezeFile (const char *filename);
268 void S9xNPServerQueueSyncAll ();
269 void S9xNPServerQueueSendingROMImage ();
270 void S9xNPServerQueueSendingLoadROMRequest (const char *filename);
271
272 void S9xNPServerAddTask (uint32 task, void *data);
273
274 bool8 S9xNPStartServer (int port);
275 void S9xNPStopServer ();
276 #ifdef __WIN32kk__
277 #define S9xGetMilliTime timeGetTime
278 #else
279 uint32 S9xGetMilliTime ();
280 #endif
281 #endif
282