Rev 2940 Rev 2941
1 // MLAB Xilinx Virtual Cable Network Server 1 // MLAB Xilinx Virtual Cable Network Server
2 // ---------------------------------------- 2 // ----------------------------------------
3 // 3 //
4 // (c) miho 2012 http://www.mlab.cz/PermaLink/XVC_SOFTWARE 4 // (c) miho 2012, 2013 http://www.mlab.cz/PermaLink/XVC_SOFTWARE
5 // 5 //
6 // This program if free. 6 // This program if free.
7 // 7 //
8 // 8 //
9 // History: 9 // History:
10 // 10 //
11 // 1.00 2012_09 Proof of concept (no configuration, not for public release) 11 // 1.00 2012_09 Proof of concept (no configuration, not for public release)
12 // 1.01 2012_09 Added parameter for device selection 12 // 1.01 2012_09 Added parameter for device selection
13 // 1.02 2012_12 Error handling and debugged 13 // 1.02 2012_12 Error handling and debugged
14 // 1.03 2012_12 Release version ready to publish 14 // 1.03 2012_12 Release version ready to publish
15 // 1.04 2013_04 Socket Bind Error with explanation (multiple instance of XVC Server) 15 // 1.04 2013_04 Socket Bind Error with explanation (multiple instance of XVC Server)
16 // 1.05 2013-04 Test FTDI cable during wait for Accept (to stop the server immediately when cable is disconnected) 16 // 1.05 2013-04 Test FTDI cable during wait for Accept (to stop the server immediately when cable is disconnected)
17 // 1.06 2013-04 Added support for Linux (thanks to Martin Poviser) 17 // 1.06 2013-04 Added support for Linux (thanks to Martin Poviser)
18 // 18 //
19 // 19 //
20 // Purpose: 20 // Purpose:
21 // 21 //
22 // XILINX development software (ISE, WebPack) supports several types of JTAG programming 22 // XILINX development software (ISE, WebPack) supports several types of JTAG programming
23 // cables. Among them there is one particularly interesting. It is Xilinx Virtual Cable 23 // cables. Among them there is one particularly interesting. It is Xilinx Virtual Cable
24 // which uses (documented) XVC network protocol to send JTAG commands across TCP/IP network. 24 // which uses (documented) XVC network protocol to send JTAG commands across TCP/IP network.
25 // So it is possible to realize own hardware/software and have it directly supported by 25 // So it is possible to realize own hardware/software and have it directly supported by
26 // XILINX development software (both IMPACT and ChipScope). 26 // XILINX development software (both IMPACT and ChipScope).
27 // 27 //
28 // This program listens TCP data send by XILINX ISE IMAPACT (or ChipScope) and sends it 28 // This program listens TCP data send by XILINX ISE IMAPACT (or ChipScope) and sends it
29 // to the JTAG device (typically FPGA) connected to FTDI USB Chip. You can use ordinary 29 // to the JTAG device (typically FPGA) connected to FTDI USB Chip. You can use ordinary
30 // USB/RS232 translator based on FT232R chip or you can use our own module from 30 // USB/RS232 translator based on FT232R chip or you can use our own module from
31 // http://www.mlab.cz/PermaLink/XVC_FT220X 31 // http://www.mlab.cz/PermaLink/XVC_FT220X
32 // 32 //
33 // Target device JTAG port is connected to pins on FTDI USB chip. Program writes to standard 33 // Target device JTAG port is connected to pins on FTDI USB chip. Program writes to standard
34 // output Which pins are used. Program writes what to set in ISE to enable XVC plugin. 34 // output Which pins are used. Program writes what to set in ISE to enable XVC plugin.
35 // 35 //
36 // 36 //
37 // Environment: 37 // Environment:
38 // 38 //
39 // This is Win32 Console Application and run in WinXP / Win7 / Win8 both 32 and 64 bit. 39 // This is Win32 Console Application and run in WinXP / Win7 / Win8 both 32 and 64 bit.
40 // 40 //
41 // Program needs to listen to the network so it is necessary to allow doing so. In Windows 41 // Program needs to listen to the network so it is necessary to allow doing so. In Windows
42 // firewall configuration enable networking for the exe file. 42 // firewall configuration enable networking for the exe file.
43 // WinXP: run as Administrator c:\WINDOWS\System32\firewall.cpl and add the exe file 43 // WinXP: run as Administrator c:\WINDOWS\System32\firewall.cpl and add the exe file
44 // Win7: the system asks directly to do so 44 // Win7: the system asks directly to do so
45 // 45 //
46 // 46 //
47 // Technology: 47 // Technology:
48 // 48 //
49 // The program uses Windows WINSOCK2 library for network communication 49 // The program uses Windows WINSOCK2 library for network communication
50 // and FTDI ftd2xx library for communication with FTDI USB chip. 50 // and FTDI ftd2xx library for communication with FTDI USB chip.
51 // It can be staticly linked to minimize dependencies on libraries. 51 // It can be staticly linked to minimize dependencies on libraries.
52 // Program requires FTDI drivers installed. 52 // Program requires FTDI drivers installed.
53 // Because of the usage of standard libraries you don't need to solve how to sign drivers. 53 // Because of the usage of standard libraries you don't need to solve how to sign drivers.
54 // 54 //
55 // The program was debug with FT232R and FT220X device. 55 // The program was debug with FT232R and FT220X device.
56 // It should work with any similar FTDI USB chip. 56 // It should work with any similar FTDI USB chip.
57 // 57 //
58 // XVC protocol is documented (you have to ask XILINX support to gain access). 58 // XVC protocol is documented (you have to ask XILINX support to gain access).
59 // The program is inspired by the work http://debugmo.de/2012/02/xvcd-the-xilinx-virtual-cable-daemon/ 59 // The program is inspired by the work http://debugmo.de/2012/02/xvcd-the-xilinx-virtual-cable-daemon/
60 // Ask Google about Xilinx Virtual Cable. 60 // Ask Google about Xilinx Virtual Cable.
61 // 61 //
62 // 62 //
63 // Compilation for Windows: 63 // Compilation for Windows:
64 // 64 //
65 // MS Visual C++ 2010 Express (free, registration required) 65 // MS Visual C++ 2010 Express (free, registration required)
66 // Create new empty project for Win32 Console Application and name project mlab_xvcd (to build mlab_xvcd.exe) 66 // Create new empty project for Win32 Console Application and name project mlab_xvcd (to build mlab_xvcd.exe)
67 // Header Files / Add / Existing Items - all .h files 67 // Header Files / Add / Existing Items - all .h files
68 // Source Files / Add / Existing Items - all .cpp files 68 // Source Files / Add / Existing Items - all .cpp files
69 // Library Files / Add / Existing Items - all .lib .h files from lib_win32 directory 69 // Library Files / Add / Existing Items - all .lib .h files from lib_win32 directory
70 // Select Release version (no debug info) 70 // Select Release version (no debug info)
71 // Set static linkage Project Properties / Configuration Release / Configuration Properties 71 // Set static linkage Project Properties / Configuration Release / Configuration Properties
72 // / Code Generation / Runtime Library = Multithreaded (/MT) 72 // / Code Generation / Runtime Library = Multithreaded (/MT)
73 // 73 //
74 // Compilation for Linux: 74 // Compilation for Linux:
75 // 75 //
76 // On Ubuntu 12.04LTS just run the .sh file 76 // On Ubuntu 12.04LTS just run the .sh file
77 // 77 //
78 // Problems: 78 // Problems:
79 // 79 //
80 // Programming of SPI FLASH configuration memory connected to FPGA does not work. No idea why. 80 // Programming of SPI FLASH configuration memory connected to FPGA does not work. No idea why.
81 // It does not work for internal FLASH of Spartan XC3SxxAN either. 81 // It does not work for internal FLASH of Spartan XC3SxxAN either.
82 // 82 //
83 // 83 //
84 // Possible improvements: 84 // Possible improvements:
85 // 85 //
86 // Linux version (Winsock library differs). 86 // Linux version (not fully functional yet).
87 // External definition of JTAG pins. 87 // External definition of JTAG pins.
88 // Enable Socket Number (to be able to run multiple XVC Servers), now it is constant XVC_TCP_PORT (should be only a default) 88 // Enable Socket Number (to be able to run multiple XVC Servers), now it is constant XVC_TCP_PORT (should be only a default)
89   89  
90   90  
91 // Library Definitions 91 // Library Definitions
92 // ------------------- 92 // -------------------
93   93  
94 #undef UNICODE 94 #undef UNICODE
95 #define WIN32_LEAN_AND_MEAN 95 #define WIN32_LEAN_AND_MEAN
96   96  
97 #include "mlab_xvcd.h" // Program Configuration 97 #include "mlab_xvcd.h" // Program Configuration
98 #include <stdlib.h> // Standard Library (exit, atoi, ...) 98 #include <stdlib.h> // Standard Library (exit, atoi, ...)
99 #include <stdio.h> // Standard IO (printf, ...) 99 #include <stdio.h> // Standard IO (printf, ...)
100 #include <signal.h> // CTRL+C handling 100 #include <signal.h> // CTRL+C handling
101   101  
102 #ifdef WIN32 102 #ifdef WIN32
103   103  
104 #include <windows.h> // Windows Console Application 104 #include <windows.h> // Windows Console Application
105 #include <winsock2.h> // Windows WinSock2 105 #include <winsock2.h> // Windows WinSock2
106 #include <ws2tcpip.h> // Windows WinSock2 106 #include <ws2tcpip.h> // Windows WinSock2
107   107  
108 // Link with library 108 // Link with library
109 #pragma comment (lib, "Ws2_32.lib") 109 #pragma comment (lib, "Ws2_32.lib")
110 //#pragma comment (lib, "../lib_win32/ftd2xx.lib") // Add this file to Resources 110 //#pragma comment (lib, "../lib_win32/ftd2xx.lib") // Add this file to Resources
111   111  
112 #else // not WIN32 112 #else // not WIN32
113   113  
114 #include "lib_linux_i386/WinTypes.h" 114 #include "lib_linux_i386/WinTypes.h"
115 #include <sys/types.h> 115 #include <sys/types.h>
116 #include <sys/socket.h> 116 #include <sys/socket.h>
117 #include <fcntl.h> 117 #include <fcntl.h>
118 #include <errno.h> 118 #include <errno.h>
119 #include <unistd.h> 119 #include <unistd.h>
120 #include <netdb.h> 120 #include <netdb.h>
121   121  
122 #endif 122 #endif
123   123  
124 #define XVC_RX_BUFLEN (XVC_JTAG_LEN/8*2+20) // Length of receive buffer in bytes (command+length+TMSbuffer+TDIbuffer) 124 #define XVC_RX_BUFLEN (XVC_JTAG_LEN/8*2+20) // Length of receive buffer in bytes (command+length+TMSbuffer+TDIbuffer)
125 #define XVC_TX_BUFLEN (XVC_JTAG_LEN/8) // Length of transmit buffer in bytes (TDObuffer) 125 #define XVC_TX_BUFLEN (XVC_JTAG_LEN/8) // Length of transmit buffer in bytes (TDObuffer)
126   126  
127 #ifdef WIN32 127 #ifdef WIN32
128   128  
129 typedef int socklen_t; 129 typedef int socklen_t;
130   130  
131 #else //not WIN32 131 #else //not WIN32
132   132  
133 typedef int SOCKET; 133 typedef int SOCKET;
134   134  
135 #define SOCKET_ERROR -1 135 #define SOCKET_ERROR -1
136 #define INVALID_SOCKET -1 136 #define INVALID_SOCKET -1
137   137  
138 void closesocket(int socket) 138 void closesocket(int socket)
139 { 139 {
140 close(socket); 140 close(socket);
141 } 141 }
142   142  
143 void WSACleanup() 143 void WSACleanup()
144 { 144 {
145 } 145 }
146   146  
147 int WSAGetLastError() 147 int WSAGetLastError()
148 { 148 {
149 return errno; 149 return errno;
150 } 150 }
151   151  
152 #endif 152 #endif
153   153  
154 // JTAG state machine 154 // JTAG state machine
155 // ------------------ 155 // ------------------
156   156  
157 // JTAG States 157 // JTAG States
158 enum 158 enum
159 { 159 {
160 test_logic_reset, run_test_idle, // Starts from 0 160 test_logic_reset, run_test_idle, // Starts from 0
161   161  
162 select_dr_scan, capture_dr, shift_dr, 162 select_dr_scan, capture_dr, shift_dr,
163 exit1_dr, pause_dr, exit2_dr, update_dr, 163 exit1_dr, pause_dr, exit2_dr, update_dr,
164   164  
165 select_ir_scan, capture_ir, shift_ir, 165 select_ir_scan, capture_ir, shift_ir,
166 exit1_ir, pause_ir, exit2_ir, update_ir, 166 exit1_ir, pause_ir, exit2_ir, update_ir,
167   167  
168 num_states 168 num_states
169 }; 169 };
170   170  
171   171  
172 // JTAG State Machine transfer Function 172 // JTAG State Machine transfer Function
173 static int jtagStep(int state, int tms) 173 static int jtagStep(int state, int tms)
174 { 174 {
175 static const int next_state[num_states][2] = 175 static const int next_state[num_states][2] =
176 { 176 {
177 /* JTAG State -->> New State */ 177 /* JTAG State -->> New State */
178 /* -------------------------------------------------------------*/ 178 /* -------------------------------------------------------------*/
179 /* | TMS=0 | TMS=1 */ 179 /* | TMS=0 | TMS=1 */
180 /* -------------------------------------------------------------*/ 180 /* -------------------------------------------------------------*/
181 /* [test_logic_reset] -> */ { run_test_idle, test_logic_reset }, 181 /* [test_logic_reset] -> */ { run_test_idle, test_logic_reset },
182 /* [run_test_idle] -> */ { run_test_idle, select_dr_scan }, 182 /* [run_test_idle] -> */ { run_test_idle, select_dr_scan },
183 /* [select_dr_scan] -> */ { capture_dr, select_ir_scan }, 183 /* [select_dr_scan] -> */ { capture_dr, select_ir_scan },
184 /* [capture_dr] -> */ { shift_dr, exit1_dr }, 184 /* [capture_dr] -> */ { shift_dr, exit1_dr },
185 /* [shift_dr] -> */ { shift_dr, exit1_dr }, 185 /* [shift_dr] -> */ { shift_dr, exit1_dr },
186 /* [exit1_dr] -> */ { pause_dr, update_dr }, 186 /* [exit1_dr] -> */ { pause_dr, update_dr },
187 /* [pause_dr] -> */ { pause_dr, exit2_dr }, 187 /* [pause_dr] -> */ { pause_dr, exit2_dr },
188 /* [exit2_dr] -> */ { shift_dr, update_dr }, 188 /* [exit2_dr] -> */ { shift_dr, update_dr },
189 /* [update_dr] -> */ { run_test_idle, select_dr_scan }, 189 /* [update_dr] -> */ { run_test_idle, select_dr_scan },
190 /* [select_ir_scan] -> */ { capture_ir, test_logic_reset }, 190 /* [select_ir_scan] -> */ { capture_ir, test_logic_reset },
191 /* [capture_ir] -> */ { shift_ir, exit1_ir }, 191 /* [capture_ir] -> */ { shift_ir, exit1_ir },
192 /* [shift_ir] -> */ { shift_ir, exit1_ir }, 192 /* [shift_ir] -> */ { shift_ir, exit1_ir },
193 /* [exit1_ir] -> */ { pause_ir, update_ir }, 193 /* [exit1_ir] -> */ { pause_ir, update_ir },
194 /* [pause_ir] -> */ { pause_ir, exit2_ir }, 194 /* [pause_ir] -> */ { pause_ir, exit2_ir },
195 /* [exit2_ir] -> */ { shift_ir, update_ir }, 195 /* [exit2_ir] -> */ { shift_ir, update_ir },
196 /* [update_ir] -> */ { run_test_idle, select_dr_scan } 196 /* [update_ir] -> */ { run_test_idle, select_dr_scan }
197 }; 197 };
198   198  
199 return next_state[state][tms]; 199 return next_state[state][tms];
200 } 200 }
201   201  
202   202  
203 int handleData(SOCKET ClientSocket) 203 int handleData(SOCKET ClientSocket)
204 { 204 {
205   205  
206 bool seen_tlr = false; 206 bool seen_tlr = false;
207 bool jtagError = false; 207 bool jtagError = false;
208   208  
209 static int jtag_state; 209 static int jtag_state;
210   210  
211 do 211 do
212 { 212 {
213 int iResult; 213 int iResult;
214   214  
215 // Read Command 215 // Read Command
216 char command[16]; 216 char command[16];
217 unsigned int commandLen = 0; 217 unsigned int commandLen = 0;
218   218  
219 // Read String terminated by ':' 219 // Read String terminated by ':'
220 do 220 do
221 { 221 {
222 iResult = recv(ClientSocket, command+commandLen, 1, 0); 222 iResult = recv(ClientSocket, command+commandLen, 1, 0);
223 if (iResult==0) 223 if (iResult==0)
224 { 224 {
225 printf("\n Connection Closed\n\n"); 225 printf("\n Connection Closed\n\n");
226 return -1; 226 return -1;
227 } 227 }
228 else if (iResult==1) 228 else if (iResult==1)
229 { 229 {
230 commandLen++; 230 commandLen++;
231 } 231 }
232 else 232 else
233 { 233 {
234 fprintf(stderr, "Error Reading Command\n"); 234 fprintf(stderr, "Error Reading Command\n");
235 return -2; 235 return -2;
236 } 236 }
237 } 237 }
238 while (command[commandLen-1]!=':' && commandLen<sizeof(command)-1 ); 238 while (command[commandLen-1]!=':' && commandLen<sizeof(command)-1 );
239 command[commandLen] = char(0); 239 command[commandLen] = char(0);
240   240  
241 if (0==strncmp(command, "shift:", sizeof(command))) 241 if (0==strncmp(command, "shift:", sizeof(command)))
242 { 242 {
243 243
244 } 244 }
245 else 245 else
246 { 246 {
247 fprintf(stderr, "Invalid Command '%s'\n", command); 247 fprintf(stderr, "Invalid Command '%s'\n", command);
248 return -2; 248 return -2;
249 } 249 }
250   250  
251 // Read Length (in bits, 32bit integer) 251 // Read Length (in bits, 32bit integer)
252 int len; 252 int len;
253   253  
254 iResult = recv(ClientSocket, (char *)&len, 4, 0); // pøepsat pøenositelnì 254 iResult = recv(ClientSocket, (char *)&len, 4, 0); // pøepsat pøenositelnì
255 if (iResult==0) 255 if (iResult==0)
256 { 256 {
257 printf("\n Connection Closed\n\n"); 257 printf("\n Connection Closed\n\n");
258 return -1; 258 return -1;
259 } 259 }
260 if (iResult != 4) 260 if (iResult != 4)
261 { 261 {
262 fprintf(stderr, "Reading Length Failed\n"); 262 fprintf(stderr, "Reading Length Failed\n");
263 return -2; 263 return -2;
264 } 264 }
265   265  
266 char buffer[2048]; 266 char buffer[2048];
267   267  
268 // Read Data (data string for TMS and TDI) 268 // Read Data (data string for TMS and TDI)
269 unsigned int nr_bytes = (len + 7) / 8; 269 unsigned int nr_bytes = (len + 7) / 8;
270 if (nr_bytes * 2 > sizeof(buffer)) 270 if (nr_bytes * 2 > sizeof(buffer))
271 { 271 {
272 fprintf(stderr, "Buffer Size Exceeded\n"); 272 fprintf(stderr, "Buffer Size Exceeded\n");
273 return -2; 273 return -2;
274 } 274 }
275   275  
276 unsigned int iReceivedBytes=0; 276 unsigned int iReceivedBytes=0;
277 while (iReceivedBytes<nr_bytes * 2) 277 while (iReceivedBytes<nr_bytes * 2)
278 { 278 {
279 iResult = recv(ClientSocket, buffer+iReceivedBytes, nr_bytes * 2 - iReceivedBytes, 0); 279 iResult = recv(ClientSocket, buffer+iReceivedBytes, nr_bytes * 2 - iReceivedBytes, 0);
280 if (iResult==0) 280 if (iResult==0)
281 { 281 {
282 printf("\n Connection Closed\n\n"); 282 printf("\n Connection Closed\n\n");
283 return -1; 283 return -1;
284 } 284 }
285 if (iResult<=0) 285 if (iResult<=0)
286 { 286 {
287 fprintf(stderr, "Reading Data Failed %d %d\n", iResult, nr_bytes * 2); 287 fprintf(stderr, "Reading Data Failed %d %d\n", iResult, nr_bytes * 2);
288 return -2; 288 return -2;
289 } 289 }
290 iReceivedBytes += iResult; 290 iReceivedBytes += iResult;
291 } 291 }
292   292  
293 char result[1024]; 293 char result[1024];
294 memset(result, 0, nr_bytes); 294 memset(result, 0, nr_bytes);
295   295  
296 // Deal with JTAG 296 // Deal with JTAG
297   297  
298 // Only allow exiting if the state is rti and the IR 298 // Only allow exiting if the state is rti and the IR
299 // has the default value (IDCODE) by going through test_logic_reset. 299 // has the default value (IDCODE) by going through test_logic_reset.
300 // As soon as going through capture_dr or capture_ir no exit is 300 // As soon as going through capture_dr or capture_ir no exit is
301 // allowed as this will change DR/IR. 301 // allowed as this will change DR/IR.
302 seen_tlr = (seen_tlr || jtag_state == test_logic_reset) && (jtag_state != capture_dr) && (jtag_state != capture_ir); 302 seen_tlr = (seen_tlr || jtag_state == test_logic_reset) && (jtag_state != capture_dr) && (jtag_state != capture_ir);
303 303
304 // Due to a weird bug(??) xilinx impacts goes through another "capture_ir"/"capture_dr" cycle after 304 // Due to a weird bug(??) xilinx impacts goes through another "capture_ir"/"capture_dr" cycle after
305 // reading IR/DR which unfortunately sets IR to the read-out IR value. 305 // reading IR/DR which unfortunately sets IR to the read-out IR value.
306 // Just ignore these transactions. 306 // Just ignore these transactions.
307 if ((jtag_state == exit1_ir && len == 5 && buffer[0] == 0x17) || (jtag_state == exit1_dr && len == 4 && buffer[0] == 0x0b)) 307 if ((jtag_state == exit1_ir && len == 5 && buffer[0] == 0x17) || (jtag_state == exit1_dr && len == 4 && buffer[0] == 0x0b))
308 { 308 {
309 // printf("Ignoring Bogus jtag State movement at jtag_state %d\n", jtag_state); 309 // printf("Ignoring Bogus jtag State movement at jtag_state %d\n", jtag_state);
310 } 310 }
311 else 311 else
312 { 312 {
313 for (int i = 0; i < len; ++i) 313 for (int i = 0; i < len; ++i)
314 { 314 {
315 // 315 //
316 // Do the actual cycle. 316 // Do the actual cycle.
317 // 317 //
318 int tms = !!(buffer[i/8] & (1<<(i&7))); 318 int tms = !!(buffer[i/8] & (1<<(i&7)));
319 // 319 //
320 // Track the state. 320 // Track the state.
321 // 321 //
322 jtag_state = jtagStep(jtag_state, tms); 322 jtag_state = jtagStep(jtag_state, tms);
323 } 323 }
324 if (jtagScan((unsigned char *) buffer, (unsigned char *) buffer + nr_bytes, (unsigned char *) result, len) < 0) 324 if (jtagScan((unsigned char *) buffer, (unsigned char *) buffer + nr_bytes, (unsigned char *) result, len) < 0)
325 { 325 {
326 //fprintf(stderr, "jtagScan failed\n"); 326 //fprintf(stderr, "jtagScan failed\n");
327 // Can't stop now, have to sent (any) answer not to hung the IMPACT 327 // Can't stop now, have to sent (any) answer not to hung the IMPACT
328 jtagError = true; 328 jtagError = true;
329 } 329 }
330 } 330 }
331   331  
332 // Send the Ansver 332 // Send the Ansver
333 iResult = send(ClientSocket, result, nr_bytes, 0 ); 333 iResult = send(ClientSocket, result, nr_bytes, 0 );
334 if (iResult == SOCKET_ERROR) 334 if (iResult == SOCKET_ERROR)
335 { 335 {
336 printf("Send Failed with Error: %d\n", WSAGetLastError()); 336 printf("Send Failed with Error: %d\n", WSAGetLastError());
337 closesocket(ClientSocket); 337 closesocket(ClientSocket);
338 WSACleanup(); 338 WSACleanup();
339 return -2; 339 return -2;
340 } 340 }
341 // printf("Bytes Sent: %d\n", iSendResult); 341 // printf("Bytes Sent: %d\n", iSendResult);
342 // printf("jtag state %d\n", jtag_state); 342 // printf("jtag state %d\n", jtag_state);
343 } 343 }
344 while (!(seen_tlr && jtag_state == run_test_idle)); 344 while (!(seen_tlr && jtag_state == run_test_idle));
345   345  
346 return jtagError ? -2 : 0; 346 return jtagError ? -2 : 0;
347 } 347 }
348   348  
349   349  
350 // Stop Handler - switch JTAG port off and stop program 350 // Stop Handler - switch JTAG port off and stop program
351 void stopHandler(int) 351 void stopHandler(int)
352 { 352 {
353 jtagClosePort(); 353 jtagClosePort();
354 exit(1); 354 exit(1);
355 } 355 }
356   356  
357   357  
358 // Print help and stop program with error 358 // Print help and stop program with error
359 void Help(char *progName) 359 void Help(char *progName)
360 { 360 {
361 fprintf(stderr, "Bad Parameters\n"); 361 fprintf(stderr, "Bad Parameters\n");
362 fprintf(stderr, "\n"); 362 fprintf(stderr, "\n");
363 fprintf(stderr, "Usage: %s [arg]\n", progName); 363 fprintf(stderr, "Usage: %s [arg]\n", progName);
364 fprintf(stderr, "\n"); 364 fprintf(stderr, "\n");
365 fprintf(stderr, " Where [arg] is one of: \n"); 365 fprintf(stderr, " Where [arg] is one of: \n");
366 fprintf(stderr, " -d Description Fing FTDI device by Description\n"); 366 fprintf(stderr, " -d Description Fing FTDI device by Description\n");
367 fprintf(stderr, " -l Location Fing FTDI device by Loaction\n"); 367 fprintf(stderr, " -l Location Fing FTDI device by Loaction\n");
368 fprintf(stderr, " -s Serial_number Fing FTDI device by it's SN\n"); 368 fprintf(stderr, " -s Serial_number Fing FTDI device by it's SN\n");
369 fprintf(stderr, " -n Number Use N-th FTDI device\n"); 369 fprintf(stderr, " -n Number Use N-th FTDI device\n");
370 fprintf(stderr, " The first FTDI device is used if no argument\n"); 370 fprintf(stderr, " The first FTDI device is used if no argument\n");
371 exit(2); 371 exit(2);
372 } 372 }
373   373  
374   374  
375 int main(int argc, char *argv[]) 375 int main(int argc, char *argv[])
376 { 376 {
377 // Variables 377 // Variables
378 bool verbose = true; 378 bool verbose = true;
379   379  
380 // Program Info 380 // Program Info
381 printf("\n"); 381 printf("\n");
382 printf("Xilinx Virtual Cable Network Server\n"); 382 printf("Xilinx Virtual Cable Network Server\n");
383 printf("===================================\n"); 383 printf("===================================\n");
384 printf("(c) miho 2012 v " VERSION "\n\n"); 384 printf("(c) miho " YEAR " v " VERSION "\n\n");
385   385  
386 // Get program name 386 // Get program name
387 char *cp; 387 char *cp;
388 char *progName; 388 char *progName;
389 cp = argv[0]; 389 cp = argv[0];
390 progName=cp; 390 progName=cp;
391 while (cp[0]!='\0') 391 while (cp[0]!='\0')
392 { 392 {
393 if (cp[0]=='/' || cp[0]=='\\') 393 if (cp[0]=='/' || cp[0]=='\\')
394 progName=cp+1; 394 progName=cp+1;
395 cp++; 395 cp++;
396 } 396 }
397   397  
398 // Process command line params 398 // Process command line params
399 char *findDeviceByStr = 0; // String parameter 399 char *findDeviceByStr = 0; // String parameter
400 int findDeviceBy = 0; // What does the string means 400 int findDeviceBy = 0; // What does the string means
401   401  
402 if (argc>1) 402 if (argc>1)
403 { 403 {
404 if (argc==3) 404 if (argc==3)
405 { 405 {
406 findDeviceByStr = argv[2]; 406 findDeviceByStr = argv[2];
407 if (strcmp(argv[1], "-d")==0) 407 if (strcmp(argv[1], "-d")==0)
408 { 408 {
409 findDeviceBy = OPEN_BY_DESCRIPTION; 409 findDeviceBy = OPEN_BY_DESCRIPTION;
410 } 410 }
411 else if (strcmp(argv[1], "-l")==0) 411 else if (strcmp(argv[1], "-l")==0)
412 { 412 {
413 findDeviceBy = OPEN_BY_LOCATION; 413 findDeviceBy = OPEN_BY_LOCATION;
414 } 414 }
415 else if (strcmp(argv[1], "-s")==0) 415 else if (strcmp(argv[1], "-s")==0)
416 { 416 {
417 findDeviceBy = OPEN_BY_SERIAL_NUMBER; 417 findDeviceBy = OPEN_BY_SERIAL_NUMBER;
418 } 418 }
419 else if (strcmp(argv[1], "-n")==0) 419 else if (strcmp(argv[1], "-n")==0)
420 { 420 {
421 findDeviceBy = 0; 421 findDeviceBy = 0;
422 } 422 }
423 else 423 else
424 { 424 {
425 Help(progName); 425 Help(progName);
426 } 426 }
427 } 427 }
428 else 428 else
429 { 429 {
430 Help(progName); 430 Help(progName);
431 } 431 }
432 } 432 }
433 else 433 else
434 { 434 {
435 // Empty String - find device by number and number is empty 435 // Empty String - find device by number and number is empty
436 findDeviceBy = 0; 436 findDeviceBy = 0;
437 findDeviceByStr = (char *)""; 437 findDeviceByStr = (char *)"";
438 } 438 }
439   439  
440 // Find, Init and Open FTDI USB Chip 440 // Find, Init and Open FTDI USB Chip
441 if (jtagOpenPort(findDeviceBy, findDeviceByStr)<0) { 441 if (jtagOpenPort(findDeviceBy, findDeviceByStr)<0) {
442 // No Device Found 442 // No Device Found
443 fprintf(stderr, "ERROR: No Device Found\n"); 443 fprintf(stderr, "ERROR: No Device Found\n");
444 return -1; 444 return -1;
445 } 445 }
446   446  
447 // Signal Handler (for CRTL+C) 447 // Signal Handler (for CRTL+C)
448 signal(SIGINT, &stopHandler); 448 signal(SIGINT, &stopHandler);
449   449  
450 printf("Starting Network Server\n"); 450 printf("Starting Network Server\n");
451 int iResult; 451 int iResult;
452   452  
453 SOCKET ListenSocket = INVALID_SOCKET; 453 SOCKET ListenSocket = INVALID_SOCKET;
454 SOCKET ClientSocket = INVALID_SOCKET; 454 SOCKET ClientSocket = INVALID_SOCKET;
455   455  
456 #ifdef WIN32 456 #ifdef WIN32
457 // Initialize Winsock 457 // Initialize Winsock
458 WSADATA wsaData; 458 WSADATA wsaData;
459 iResult = WSAStartup(MAKEWORD(2,2), &wsaData); 459 iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
460 if (iResult != 0) 460 if (iResult != 0)
461 { 461 {
462 fprintf(stderr, "WSAStartup failed with error: %d\n", iResult); 462 fprintf(stderr, "WSAStartup failed with error: %d\n", iResult);
463 jtagClosePort(); 463 jtagClosePort();
464 return -2; 464 return -2;
465 } 465 }
466 #endif 466 #endif
467   467  
468 // Display HostName and Address 468 // Display HostName and Address
469 char sMyName[255]; 469 char sMyName[255];
470 gethostname(sMyName, sizeof(sMyName)); 470 gethostname(sMyName, sizeof(sMyName));
471 printf(" Host Name %s\n", sMyName); 471 printf(" Host Name %s\n", sMyName);
472 hostent * pHostInfo; 472 hostent * pHostInfo;
473 pHostInfo = gethostbyname(sMyName); 473 pHostInfo = gethostbyname(sMyName);
474 printf(" Network Name %s\n", pHostInfo->h_name); 474 printf(" Network Name %s\n", pHostInfo->h_name);
475 if (pHostInfo->h_length>0 && pHostInfo->h_length<=16) 475 if (pHostInfo->h_length>0 && pHostInfo->h_length<=16)
476 { 476 {
477 printf(" Host Address "); 477 printf(" Host Address ");
478 for (int i=0; i<pHostInfo->h_length-1; i++) 478 for (int i=0; i<pHostInfo->h_length-1; i++)
479 { 479 {
480 printf("%d.", (unsigned char)pHostInfo->h_addr_list[0][i]); 480 printf("%d.", (unsigned char)pHostInfo->h_addr_list[0][i]);
481 } 481 }
482 printf("%d\n", (unsigned char)pHostInfo->h_addr_list[0][pHostInfo->h_length-1]); 482 printf("%d\n", (unsigned char)pHostInfo->h_addr_list[0][pHostInfo->h_length-1]);
483 } 483 }
484   484  
485 // Create Protocol Structure 485 // Create Protocol Structure
486 struct addrinfo hints; 486 struct addrinfo hints;
487 memset(&hints, 0, sizeof(hints)); 487 memset(&hints, 0, sizeof(hints));
488 hints.ai_family = AF_INET; // IP6 488 hints.ai_family = AF_INET; // IP6
489 hints.ai_socktype = SOCK_STREAM; // Reliable two-way connection 489 hints.ai_socktype = SOCK_STREAM; // Reliable two-way connection
490 hints.ai_protocol = IPPROTO_TCP; // Protocol TCP 490 hints.ai_protocol = IPPROTO_TCP; // Protocol TCP
491 hints.ai_flags = AI_PASSIVE; 491 hints.ai_flags = AI_PASSIVE;
492   492  
493 // Resolve the server address and port (allocate structure "result") 493 // Resolve the server address and port (allocate structure "result")
494 struct addrinfo *result = NULL; 494 struct addrinfo *result = NULL;
495 iResult = getaddrinfo(NULL, XVC_TCP_PORT, &hints, &result); 495 iResult = getaddrinfo(NULL, XVC_TCP_PORT, &hints, &result);
496 if ( iResult != 0 ) 496 if ( iResult != 0 )
497 { 497 {
498 fprintf(stderr, "getaddrinfo failed with error: %d\n", iResult); 498 fprintf(stderr, "getaddrinfo failed with error: %d\n", iResult);
499 WSACleanup(); 499 WSACleanup();
500 jtagClosePort(); 500 jtagClosePort();
501 return -2; 501 return -2;
502 } 502 }
503   503  
504 // Create a SOCKET 504 // Create a SOCKET
505 ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol); 505 ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
506 if (ListenSocket == INVALID_SOCKET) 506 if (ListenSocket == INVALID_SOCKET)
507 { 507 {
508 fprintf(stderr, "socket failed with error: %d\n", WSAGetLastError()); 508 fprintf(stderr, "socket failed with error: %d\n", WSAGetLastError());
509 freeaddrinfo(result); 509 freeaddrinfo(result);
510 WSACleanup(); 510 WSACleanup();
511 jtagClosePort(); 511 jtagClosePort();
512 return -2; 512 return -2;
513 } 513 }
514   514  
515 // Bind the SOCKED (assign the address) 515 // Bind the SOCKED (assign the address)
516 iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen); 516 iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
517 if (iResult == SOCKET_ERROR) 517 if (iResult == SOCKET_ERROR)
518 { 518 {
519 int LastError=WSAGetLastError(); 519 int LastError=WSAGetLastError();
520 fprintf(stderr, "Bind failed with error: %d\n", LastError); 520 fprintf(stderr, "Bind failed with error: %d\n", LastError);
521 #ifdef WIN32 521 #ifdef WIN32
522 if (LastError==WSAEADDRINUSE) 522 if (LastError==WSAEADDRINUSE)
523 #else 523 #else
524 if (LastError==EADDRINUSE) 524 if (LastError==EADDRINUSE)
525 #endif 525 #endif
526 fprintf(stderr, "Trying to start second instance of XVC Server?\n"); 526 fprintf(stderr, "Trying to start second instance of XVC Server?\n");
527 freeaddrinfo(result); 527 freeaddrinfo(result);
528 closesocket(ListenSocket); 528 closesocket(ListenSocket);
529 WSACleanup(); 529 WSACleanup();
530 jtagClosePort(); 530 jtagClosePort();
531 return -2; 531 return -2;
532 } 532 }
533   533  
534 if (verbose) 534 if (verbose)
535 { 535 {
536 printf(" Bound Socket %s\n", XVC_TCP_PORT); 536 printf(" Bound Socket %s\n", XVC_TCP_PORT);
537 } 537 }
538   538  
539 // Help for user 539 // Help for user
540 printf(" Set in IMPACT xilinx_xvc host=%s:%s disableversioncheck=true\n", sMyName, XVC_TCP_PORT); 540 printf(" Set in IMPACT xilinx_xvc host=%s:%s disableversioncheck=true\n", sMyName, XVC_TCP_PORT);
541   541  
542 freeaddrinfo(result); 542 freeaddrinfo(result);
543   543  
544 // Listen SOCKET 544 // Listen SOCKET
545 iResult = listen(ListenSocket, SOMAXCONN); 545 iResult = listen(ListenSocket, SOMAXCONN);
546 if (iResult == SOCKET_ERROR) 546 if (iResult == SOCKET_ERROR)
547 { 547 {
548 fprintf(stderr, "listen failed with error: %d\n", WSAGetLastError()); 548 fprintf(stderr, "listen failed with error: %d\n", WSAGetLastError());
549 closesocket(ListenSocket); 549 closesocket(ListenSocket);
550 WSACleanup(); 550 WSACleanup();
551 jtagClosePort(); 551 jtagClosePort();
552 return -2; 552 return -2;
553 } 553 }
554   554  
555 printf("\n"); 555 printf("\n");
556   556  
557 do 557 do
558 { 558 {
559 printf(" Listen\n"); 559 printf(" Listen\n");
560 jtagSetLED(true); 560 jtagSetLED(true);
561   561  
562 // Set ListenSocket to non-blocking mode 562 // Set ListenSocket to non-blocking mode
563 // We need during waiting for Accept to detect FTDI disconnect 563 // We need during waiting for Accept to detect FTDI disconnect
564   564  
565 #ifdef WIN32 565 #ifdef WIN32
566 u_long iMode = 1; 566 u_long iMode = 1;
567 iResult = ioctlsocket(ListenSocket, FIONBIO, &iMode); 567 iResult = ioctlsocket(ListenSocket, FIONBIO, &iMode);
568 if (iResult != NO_ERROR) 568 if (iResult != NO_ERROR)
569 { 569 {
570 fprintf(stderr, "ioctlsocket failed with error: %ld\n", iResult); 570 fprintf(stderr, "ioctlsocket failed with error: %ld\n", iResult);
571 WSACleanup(); 571 WSACleanup();
572 jtagClosePort(); 572 jtagClosePort();
573 return -2; 573 return -2;
574 } 574 }
575 #else 575 #else
576 iResult = fcntl(ListenSocket, F_GETFL, 0); 576 iResult = fcntl(ListenSocket, F_GETFL, 0);
577 if (iResult < 0 || fcntl(ListenSocket, F_SETFL, iResult | O_NONBLOCK) < 0) 577 if (iResult < 0 || fcntl(ListenSocket, F_SETFL, iResult | O_NONBLOCK) < 0)
578 { 578 {
579 fprintf(stderr, "fcntl failed with error: %d\n", errno); 579 fprintf(stderr, "fcntl failed with error: %d\n", errno);
580 jtagClosePort(); 580 jtagClosePort();
581 return -2; 581 return -2;
582 } 582 }
583 #endif 583 #endif
584   584  
585 // Accept a client SOCKET (wait for Accept) 585 // Accept a client SOCKET (wait for Accept)
586 sockaddr ClientSocetAddr; 586 sockaddr ClientSocetAddr;
587 socklen_t ClientSocetAddrLen = sizeof(sockaddr); 587 socklen_t ClientSocetAddrLen = sizeof(sockaddr);
588 do 588 do
589 { 589 {
590 // Try Accept (non-blocking) 590 // Try Accept (non-blocking)
591 ClientSocket = accept(ListenSocket, &ClientSocetAddr, &ClientSocetAddrLen); 591 ClientSocket = accept(ListenSocket, &ClientSocetAddr, &ClientSocetAddrLen);
592 if (ClientSocket == INVALID_SOCKET) 592 if (ClientSocket == INVALID_SOCKET)
593 { 593 {
594 // Accept Error 594 // Accept Error
595 #ifdef WIN32 595 #ifdef WIN32
596 if (WSAGetLastError() != WSAEWOULDBLOCK) 596 if (WSAGetLastError() != WSAEWOULDBLOCK)
597 #else 597 #else
598 if (WSAGetLastError() != EAGAIN && WSAGetLastError() != EWOULDBLOCK) 598 if (WSAGetLastError() != EAGAIN && WSAGetLastError() != EWOULDBLOCK)
599 #endif 599 #endif
600 { 600 {
601 fprintf(stderr, "accept failed with error: %d\n", WSAGetLastError()); 601 fprintf(stderr, "accept failed with error: %d\n", WSAGetLastError());
602 closesocket(ListenSocket); 602 closesocket(ListenSocket);
603 WSACleanup(); 603 WSACleanup();
604 jtagClosePort(); 604 jtagClosePort();
605 return -2; 605 return -2;
606 } 606 }
607 // Not yet Accepted 607 // Not yet Accepted
608 { 608 {
609 // Check FTDI 609 // Check FTDI
610 if (!CheckCable()) 610 if (!CheckCable())
611 { 611 {
612 fprintf(stderr, "XVC Cable unexpectedly disconnected\n"); 612 fprintf(stderr, "XVC Cable unexpectedly disconnected\n");
613 closesocket(ListenSocket); 613 closesocket(ListenSocket);
614 WSACleanup(); 614 WSACleanup();
615 jtagClosePort(); 615 jtagClosePort();
616 return -2; 616 return -2;
617 } 617 }
618 // Sleep some time (do not eat CPU time for nothong) 618 // Sleep some time (do not eat CPU time for nothong)
619 #ifdef WIN32 619 #ifdef WIN32
620 Sleep(100); //ms 620 Sleep(100); //ms
621 #else 621 #else
622 usleep(100000); //us 622 usleep(100000); //us
623 #endif 623 #endif
624 } 624 }
625 } 625 }
626 } 626 }
627 while (ClientSocket == INVALID_SOCKET); 627 while (ClientSocket == INVALID_SOCKET);
628   628  
629 // Set (Accepted) Socket to blocking mode 629 // Set (Accepted) Socket to blocking mode
630   630  
631 #ifdef WIN32 631 #ifdef WIN32
632 iMode = 0; 632 iMode = 0;
633 iResult = ioctlsocket(ClientSocket, FIONBIO, &iMode); 633 iResult = ioctlsocket(ClientSocket, FIONBIO, &iMode);
634 if (iResult != NO_ERROR) 634 if (iResult != NO_ERROR)
635 { 635 {
636 fprintf(stderr, "ioctlsocket failed with error: %ld\n", iResult); 636 fprintf(stderr, "ioctlsocket failed with error: %ld\n", iResult);
637 WSACleanup(); 637 WSACleanup();
638 jtagClosePort(); 638 jtagClosePort();
639 return -2; 639 return -2;
640 } 640 }
641 #else 641 #else
642 iResult = fcntl(ListenSocket, F_GETFL, 0); 642 iResult = fcntl(ListenSocket, F_GETFL, 0);
643 if (iResult < 0 || fcntl(ListenSocket, F_SETFL, iResult & ~O_NONBLOCK) < 0) 643 if (iResult < 0 || fcntl(ListenSocket, F_SETFL, iResult & ~O_NONBLOCK) < 0)
644 { 644 {
645 fprintf(stderr, "fcntl failed with error: %d\n", errno); 645 fprintf(stderr, "fcntl failed with error: %d\n", errno);
646 jtagClosePort(); 646 jtagClosePort();
647 return -2; 647 return -2;
648 } 648 }
649 #endif 649 #endif
650   650  
651 // Print Accepted + Address 651 // Print Accepted + Address
652 printf(" Accepted "); 652 printf(" Accepted ");
653 jtagSetLED(false); 653 jtagSetLED(false);
654 for (int i=2; i<2+4-1; i++) 654 for (int i=2; i<2+4-1; i++)
655 { 655 {
656 printf("%d.", (unsigned char)ClientSocetAddr.sa_data[i]); 656 printf("%d.", (unsigned char)ClientSocetAddr.sa_data[i]);
657 } 657 }
658 printf("%d:%d\n", (unsigned char)ClientSocetAddr.sa_data[2+4-1], (unsigned char)ClientSocetAddr.sa_data[0]*256+(unsigned char)ClientSocetAddr.sa_data[1]); 658 printf("%d:%d\n", (unsigned char)ClientSocetAddr.sa_data[2+4-1], (unsigned char)ClientSocetAddr.sa_data[0]*256+(unsigned char)ClientSocetAddr.sa_data[1]);
659   659  
660 // Process Data until the peer shuts down the connection 660 // Process Data until the peer shuts down the connection
661 int Cnt = 0; 661 int Cnt = 0;
662 printf(" Handle Data "); 662 printf(" Handle Data ");
663 do 663 do
664 { 664 {
665 iResult = handleData(ClientSocket); 665 iResult = handleData(ClientSocket);
666 if (iResult>=0) 666 if (iResult>=0)
667 { 667 {
668 printf("."); 668 printf(".");
669 fflush(stdout); 669 fflush(stdout);
670 Cnt++; 670 Cnt++;
671 if (Cnt>40) 671 if (Cnt>40)
672 { 672 {
673 Cnt = 0; 673 Cnt = 0;
674 printf("\n "); 674 printf("\n ");
675 } 675 }
676 } 676 }
677 } 677 }
678 while (iResult >= 0); 678 while (iResult >= 0);
679   679  
680 // Connection Closed by peer 680 // Connection Closed by peer
681 if (iResult==-1) 681 if (iResult==-1)
682 { 682 {
683 // JTAG port 683 // JTAG port
684 jtagSetIdle(); 684 jtagSetIdle();
685 } 685 }
686   686  
687 // Error - shutdown the connection 687 // Error - shutdown the connection
688 if (iResult==-2) 688 if (iResult==-2)
689 { 689 {
690 fprintf(stderr, " Disconnect\n"); 690 fprintf(stderr, " Disconnect\n");
691 #ifdef WIN32 691 #ifdef WIN32
692 iResult = shutdown(ClientSocket, SD_SEND); 692 iResult = shutdown(ClientSocket, SD_SEND);
693 #else 693 #else
694 iResult = shutdown(ClientSocket, SHUT_WR); 694 iResult = shutdown(ClientSocket, SHUT_WR);
695 #endif 695 #endif
696 if (iResult == SOCKET_ERROR) 696 if (iResult == SOCKET_ERROR)
697 { 697 {
698 fprintf(stderr, "shutdown failed with error: %d\n", WSAGetLastError()); 698 fprintf(stderr, "shutdown failed with error: %d\n", WSAGetLastError());
699 } 699 }
700 iResult=-2; // Error 700 iResult=-2; // Error
701 } 701 }
702   702  
703 // cleanup 703 // cleanup
704 closesocket(ClientSocket); 704 closesocket(ClientSocket);
705   705  
706 } 706 }
707 // If not Error Listen Again 707 // If not Error Listen Again
708 while (iResult!=-2); 708 while (iResult!=-2);
709   709  
710 // cleanup 710 // cleanup
711 closesocket(ListenSocket); 711 closesocket(ListenSocket);
712 WSACleanup(); 712 WSACleanup();
713 jtagClosePort(); 713 jtagClosePort();
714   714  
715 return 1; 715 return 1;
716 } 716 }