OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [or_debug_proxy/] [src/] [or_debug_proxy.c] - Blame information for rev 1781

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1779 julius
/*$$HEADER*/
2
/******************************************************************************/
3
/*                                                                            */
4
/*                    H E A D E R   I N F O R M A T I O N                     */
5
/*                                                                            */
6
/******************************************************************************/
7
 
8
// Project Name                   : OpenRISC Debug Proxy
9
// File Name                      : or_debug_proxy.c
10
// Prepared By                    : jb
11
// Project Start                  : 2008-10-01
12
 
13
/*$$COPYRIGHT NOTICE*/
14
/******************************************************************************/
15
/*                                                                            */
16
/*                      C O P Y R I G H T   N O T I C E                       */
17
/*                                                                            */
18
/******************************************************************************/
19
/*
20
  This library is free software; you can redistribute it and/or
21
  modify it under the terms of the GNU Lesser General Public
22
  License as published by the Free Software Foundation;
23
  version 2.1 of the License, a copy of which is available from
24
  http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
25
 
26
  This library is distributed in the hope that it will be useful,
27
  but WITHOUT ANY WARRANTY; without even the implied warranty of
28
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29
  Lesser General Public License for more details.
30
 
31
  You should have received a copy of the GNU Lesser General Public
32
  License along with this library; if not, write to the Free Software
33
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
34
*/
35
 
36
/*$$DESCRIPTION*/
37
/******************************************************************************/
38
/*                                                                            */
39
/*                           D E S C R I P T I O N                            */
40
/*                                                                            */
41
/******************************************************************************/
42
//
43
// The entry point for the OpenRISC debug proxy console application. Is 
44
// compilable under both Linux/Unix systems and Cygwin Windows.
45
//
46
 
47
#include <assert.h>
48
#include <stdio.h>
49
#include <ctype.h>
50
#include <string.h>
51
#include <stdlib.h>
52
#include <unistd.h>
53
#include <stdarg.h>
54
#include <sys/stat.h>
55
#include <sys/types.h>
56
 
57
// Windows includes
58
#ifdef CYGWIN_COMPILE
59
#include <windows.h>
60
#include "win_FTCJTAG.h"
61
#include "win_FTCJTAG_ptrs.h"
62
#else
63
#include <signal.h>
64
void catch_sigint(int sig_num); // First param must be "int"
65
#endif
66
 
67
#include "gdb.h"
68 1780 julius
 
69
#ifdef USB_ENDPOINT_ENABLED
70 1779 julius
#include "usb_functions.h"
71 1780 julius
#endif
72
 
73
#ifdef VPI_ENDPOINT_ENABLED
74 1779 julius
#include "vpi_functions.h"
75 1780 julius
#endif
76
 
77 1779 julius
#include "or_debug_proxy.h"
78
 
79
// Defines of endpoint numbers
80
#define ENDPOINT_TARGET_NONE 0
81
#define ENDPOINT_TARGET_USB 1
82
#define ENDPOINT_TARGET_VPI 2
83 1780 julius
 
84 1779 julius
static int endpoint_target; // Either VPI interface via sockets, or the USB device
85
 
86
#define GDB_PROTOCOL_JTAG  1
87
#define GDB_PROTOCOL_RSP   2
88
#define GDB_PROTOCOL_NONE  3
89
 
90
int err; // Global error value
91
 
92
/* Currently selected scan chain - just to prevent unnecessary transfers. */
93
int current_chain = -1;
94
 
95
/* The chain that should be currently selected. */
96
int dbg_chain = -1;
97
 
98
int main(int argc,  char *argv[]) {
99
 
100
  char *s;
101
  int gdb_protocol = GDB_PROTOCOL_NONE;
102
  endpoint_target = ENDPOINT_TARGET_NONE;
103
  int inp_arg = 1;
104
 
105 1780 julius
  // Check we were compiled with at least one endpoint enabled
106
#ifndef USB_ENDPOINT_ENABLED
107
#ifndef VPI_ENDPOINT_ENABLED
108
  printf("No endpoints enabled.\nRecompile the proxy with at least one endpoint enabled\n");
109
  exit(0);
110
#endif
111
#endif
112
 
113 1779 julius
  // init our global error number
114
  err = DBG_ERR_OK;
115
 
116
  // Parse input options
117
  if (argc < 3)
118
    {
119
      print_usage();
120
      exit(1);
121
    }
122
 
123
  err = DBG_ERR_OK;
124
  srand(getpid());
125
 
126
  // Parse through the input, check what we've been given
127
 
128
  while ( argv[inp_arg] != NULL )
129
    {
130
      if(strcmp(argv[inp_arg], "-j") == 0)
131
        {
132
          gdb_protocol = GDB_PROTOCOL_JTAG;
133
          endpoint_target = ENDPOINT_TARGET_USB;
134
        }
135
      else if(strcmp(argv[inp_arg], "-r") == 0)
136
        {
137
          gdb_protocol = GDB_PROTOCOL_RSP;
138
          endpoint_target = ENDPOINT_TARGET_USB;
139
        }
140
      else if(strcmp(argv[inp_arg], "-v") == 0)
141
        {
142
          gdb_protocol = GDB_PROTOCOL_RSP;
143
          endpoint_target = ENDPOINT_TARGET_VPI;
144
        }
145
      else
146
        {
147
          serverPort = strtol(argv[2],&s,10);
148
        }
149
 
150
      inp_arg++;
151
    }
152
 
153
  if(endpoint_target == ENDPOINT_TARGET_NONE || gdb_protocol == GDB_PROTOCOL_NONE || serverPort > 65535 || *s != '\0')
154
    {
155
      print_usage();
156
      exit(1);
157
    }
158
 
159
#ifdef CYGWIN_COMPILE
160
  // Load the FTCJTAG DLL function pointers
161
  if (getFTDIJTAGFunctions() < 0){
162
    exit(-1);
163
  }
164
#endif
165
 
166
#ifndef CYGWIN_COMPILE
167
  // Install a signal handler to exit gracefully
168
  // when we receive a sigint
169
  signal(SIGINT, catch_sigint);
170
#endif
171
 
172
  /* Initialise connection to our OR1k system */
173
  current_chain = -1;
174 1780 julius
#ifdef USB_ENDPOINT_ENABLED
175 1779 julius
  /* USB Endpoint */
176
  if (endpoint_target == ENDPOINT_TARGET_USB)
177
    {
178
      printf("\nConnecting to OR1k via USB debug cable\n\n");
179
      if ((err = usb_dbg_reset())) goto JtagIfError;
180
      dbg_test(); // Perform some tests
181
    }
182 1780 julius
#endif
183
#ifdef VPI_ENDPOINT_ENABLED
184 1779 julius
  /* RTL simulation endpoint */
185 1780 julius
  if (endpoint_target == ENDPOINT_TARGET_VPI){
186 1779 julius
    printf("\nConnecting to OR1k RTL simulation\n\n");
187
    // Connect to the (hopefully) already running RTL simulation server running via VPI
188
    vpi_fd = vpi_connect();
189
 
190
    if ((err = vpi_dbg_reset())) goto JtagIfError;
191
    vpi_dbg_test(); // Perform some tests
192
  }
193 1780 julius
#endif
194 1779 julius
 
195
  /* We have a connection to the target system.  Now establish server connection. */
196
  if(gdb_protocol == GDB_PROTOCOL_JTAG)
197
  { // Connect to JTAG server
198
    if((server_fd = GetServerSocket("or1ksim","tcp", serverPort))) {
199
      // printf("JTAG Proxy server started on port %d\n", serverPort);
200
      printf("Remote JTAG proxy server started on port %d\n", serverPort);
201
      printf("Note: The OpenRISC remote JTAG protocol is now DEPRECATED. Please use GDB version 6.8 or later.\n");
202
      printf("Press CTRL+c to exit.\n");
203
    } else {
204
      // fprintf(stderr,"Cannot start JTAG Proxy server on port %d\n", serverPort);
205
      fprintf(stderr,"Cannot start Proxy server on port %d\n", serverPort);
206
      exit(-1);
207
    }
208
 
209
    /* Do endless loop of checking and handle GDB requests.  Ctrl-c exits.  */
210
    // HandleServerSocket(true);
211
    HandleServerSocket();
212
    return 0;
213
  }
214
  else if(gdb_protocol == GDB_PROTOCOL_RSP)
215
  { // Connect to RSP server
216
    /* RSP always starts stalled as though we have just reset the processor. */
217
    // rsp_exception (EXCEPT_TRAP);
218
    handle_rsp ();
219
    // if((server_fd = GetServerSocket("or1ksim","tcp", serverPort))) {
220
  }else {
221
    fprintf(stderr,"Cannot start RSP Proxy server on port %d\n", serverPort);
222
    exit(-1);
223
  }
224
 
225
 
226
JtagIfError:
227
  fprintf(stderr,"Connection via USB debug cable failed (err = %d).\nPlease ensure the device is attached and correctly installed\n\n", err);
228
  exit(-1);
229
  return 0;
230
}
231
 
232
 
233
 
234
int dbg_reset()
235
{
236 1780 julius
#ifdef USB_ENDPOINT_ENABLED
237 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_reset();
238 1780 julius
#endif
239
#ifdef VPI_ENDPOINT_ENABLED
240
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_reset();
241
#endif
242
  return DBG_ERR_INVALID_ENDPOINT;
243 1779 julius
}
244
 
245
void dbg_test() {
246 1780 julius
#ifdef USB_ENDPOINT_ENABLED
247 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) usb_dbg_test();
248 1780 julius
#endif
249
#ifdef VPI_ENDPOINT_ENABLED
250
  if (endpoint_target == ENDPOINT_TARGET_VPI) vpi_dbg_test();
251
#endif
252 1779 julius
}
253
 
254
/* Set TAP instruction register */
255
int dbg_set_tap_ir(uint32_t ir) {
256 1780 julius
#ifdef USB_ENDPOINT_ENABLED
257 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) usb_set_tap_ir(ir);
258 1780 julius
#endif
259
  return DBG_ERR_INVALID_ENDPOINT;
260 1779 julius
}
261
 
262
/* Sets scan chain.  */
263
int dbg_set_chain(uint32_t chain) {
264 1780 julius
#ifdef USB_ENDPOINT_ENABLED
265 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_set_chain(chain);
266 1780 julius
#endif
267
#ifdef VPI_ENDPOINT_ENABLED
268
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_set_chain(chain);
269
#endif
270
  return DBG_ERR_INVALID_ENDPOINT;
271 1779 julius
}
272
 
273
/* sends out a command with 32bit address and 16bit length, if len >= 0 */
274
int dbg_command(uint32_t type, uint32_t adr, uint32_t len) {
275
  // This is never called by any of the VPI functions, so only USB endpoint
276 1780 julius
#ifdef USB_ENDPOINT_ENABLED
277 1779 julius
 if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_command(type,adr,len);
278 1780 julius
#endif
279
 return DBG_ERR_INVALID_ENDPOINT;
280 1779 julius
}
281
 
282
/* writes a ctrl reg */
283
int dbg_ctrl(uint32_t reset, uint32_t stall) {
284 1780 julius
#ifdef USB_ENDPOINT_ENABLED
285 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_ctrl(reset, stall);
286 1780 julius
#endif
287
#ifdef VPI_ENDPOINT_ENABLED
288
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_ctrl(reset, stall);
289
#endif
290
  return DBG_ERR_INVALID_ENDPOINT;
291 1779 julius
}
292
 
293
/* reads control register */
294
int dbg_ctrl_read(uint32_t *reset, uint32_t *stall) {
295 1780 julius
#ifdef USB_ENDPOINT_ENABLED
296 1779 julius
    if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_ctrl_read(reset, stall);
297 1780 julius
#endif
298
#ifdef VPI_ENDPOINT_ENABLED
299
    if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_ctrl_read(reset, stall);
300
#endif
301
    return DBG_ERR_INVALID_ENDPOINT;
302 1779 julius
}
303
 
304
/* issues a burst read/write */
305
int dbg_go(unsigned char *data, uint16_t len, uint32_t read) {
306
  // Only USB endpouint32_t option here
307 1780 julius
#ifdef USB_ENDPOINT_ENABLED
308 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_go(data, len, read);
309 1780 julius
#endif
310
  return DBG_ERR_INVALID_ENDPOINT;
311 1779 julius
}
312
 
313
/* read a word from wishbone */
314
int dbg_wb_read32(uint32_t adr, uint32_t *data) {
315 1780 julius
#ifdef USB_ENDPOINT_ENABLED
316 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_read32(adr, data);
317 1780 julius
#endif
318
#ifdef VPI_ENDPOINT_ENABLED
319
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_read32(adr, data);
320
#endif
321
  return DBG_ERR_INVALID_ENDPOINT;
322 1779 julius
}
323
 
324
 
325
/* write a word to wishbone */
326
int dbg_wb_write32(uint32_t adr, uint32_t data) {
327 1780 julius
#ifdef USB_ENDPOINT_ENABLED
328 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_write32( adr, data);
329 1780 julius
#endif
330
#ifdef VPI_ENDPOINT_ENABLED
331
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_write32( adr, data);
332
#endif
333
  return DBG_ERR_INVALID_ENDPOINT;
334 1779 julius
}
335
 
336
/* read a block from wishbone */
337
int dbg_wb_read_block32(uint32_t adr, uint32_t *data, uint32_t len) {
338 1780 julius
#ifdef USB_ENDPOINT_ENABLED
339 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_read_block32( adr, data, len);
340 1780 julius
#endif
341
#ifdef VPI_ENDPOINT_ENABLED
342
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_read_block32( adr, data, len);
343
#endif
344
  return DBG_ERR_INVALID_ENDPOINT;
345 1779 julius
}
346
 
347
 
348
/* write a block to wishbone */
349
int dbg_wb_write_block32(uint32_t adr, uint32_t *data, uint32_t len) {
350 1780 julius
#ifdef USB_ENDPOINT_ENABLED
351 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_write_block32( adr, data, len);
352 1780 julius
#endif
353
#ifdef VPI_ENDPOINT_ENABLED
354
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_write_block32( adr, data, len);
355
#endif
356
  return DBG_ERR_INVALID_ENDPOINT;
357 1779 julius
}
358
 
359
/* read a register from cpu */
360
int dbg_cpu0_read(uint32_t adr, uint32_t *data) {
361 1780 julius
#ifdef USB_ENDPOINT_ENABLED
362 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_read( adr, data);
363 1780 julius
#endif
364
#ifdef VPI_ENDPOINT_ENABLED
365
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_read( adr, data);
366
#endif
367
  return DBG_ERR_INVALID_ENDPOINT;
368 1779 julius
}
369
 
370
/* write a cpu register */
371
int dbg_cpu0_write(uint32_t adr, uint32_t data) {
372 1780 julius
#ifdef USB_ENDPOINT_ENABLED
373 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_write( adr, data);
374 1780 julius
#endif
375
#ifdef VPI_ENDPOINT_ENABLED
376
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_write( adr, data);
377
#endif
378
  return DBG_ERR_INVALID_ENDPOINT;
379 1779 julius
}
380
 
381
/* write a cpu module register */
382
int dbg_cpu0_write_ctrl(uint32_t adr, unsigned char data) {
383 1780 julius
#ifdef USB_ENDPOINT_ENABLED
384 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_write_ctrl( adr, data);
385 1780 julius
#endif
386
#ifdef VPI_ENDPOINT_ENABLED
387
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_write_ctrl( adr, data);
388
#endif
389
  return DBG_ERR_INVALID_ENDPOINT;
390 1779 julius
}
391
 
392
 
393
/* read a register from cpu module */
394
int dbg_cpu0_read_ctrl(uint32_t adr, unsigned char *data) {
395 1780 julius
#ifdef USB_ENDPOINT_ENABLED
396 1779 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_read_ctrl( adr, data);
397 1780 julius
#endif
398
#ifdef VPI_ENDPOINT_ENABLED
399
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_read_ctrl( adr, data);
400
#endif
401
  return DBG_ERR_INVALID_ENDPOINT;
402 1779 julius
}
403
 
404
 
405
void test_sdram(void) {
406
        return;
407
}
408
 
409
// Close down gracefully when we receive any kill signals
410
void catch_sigint(int sig_num)
411
{
412
  // Close down any potentially open sockets and USB handles
413 1780 julius
#ifdef VPI_ENDPOINT_ENABLED
414 1779 julius
  if (vpi_fd) close(vpi_fd);
415 1780 julius
#endif
416 1779 julius
  if (server_fd) close(server_fd);
417 1780 julius
  gdb_close();
418
#ifdef USB_ENDPOINT_ENABLED
419 1779 julius
  usb_close_device_handle();
420 1780 julius
#endif
421 1779 julius
  printf("\nInterrupt signal received. Closing down connections and exiting\n\n");
422
  exit(0);
423
}
424
 
425
void print_usage()
426
{
427
  printf("Invalid or insufficient arguments\n");
428
  printf("\n");
429
  printf("OpenRISC GDB proxy server usage: or_debug_proxy -server_type port\n");
430
  printf("\n");
431
  printf("server_type:\n");
432 1780 julius
#ifdef USB_ENDPOINT_ENABLED
433 1779 julius
  printf("\t-r Start a server using RSP, connection to hadware target via\n\t   USB\n");
434
  printf("\t-j Start a server using legacy OR remote JTAG protocol, to\n\t   hardware target via USB\n");
435 1780 julius
#endif
436
#ifdef VPI_ENDPOINT_ENABLED
437 1779 julius
  printf("\t-v Start a server using RSP, connection to RTL sim. VPI server\n\t   target via sockets\n");
438 1780 julius
#endif
439 1779 julius
  printf("\n");
440
  printf("port:\n");
441
  printf("\tAny free port within the usable range of 0 - 65535\n");
442
  printf("\n");
443
  printf("Example:\n");
444 1780 julius
#ifdef USB_ENDPOINT_ENABLED
445 1779 julius
  printf("\tStart a GDB server on port 5555, using RSP, connecting to\n\thardware target via USB\n");
446
  printf("\tor_debug_proxy -r 5555\n");
447
  printf("\n");
448 1780 julius
#endif
449
#ifdef VPI_ENDPOINT_ENABLED
450
  printf("\tStart a GDB server on port 5555, using RSP, connecting to\n\trtl target via VPI\n");
451
  printf("\tor_debug_proxy -v 5555\n");
452
  printf("\n");
453
#endif
454 1779 julius
  fflush (stdout);
455
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.