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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or_debug_proxy/] [src/] [or_debug_proxy.c] - Blame information for rev 151

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

Line No. Rev Author Line
1 39 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
 
69
#ifdef USB_ENDPOINT_ENABLED
70
#include "usb_functions.h"
71
#endif
72
 
73
#ifdef VPI_ENDPOINT_ENABLED
74
#include "vpi_functions.h"
75
#endif
76
 
77
#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
 
84
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
  // 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
  // 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 46 julius
      if(strcmp(argv[inp_arg], "-r") == 0)
131 39 julius
        {
132
          gdb_protocol = GDB_PROTOCOL_RSP;
133
          endpoint_target = ENDPOINT_TARGET_USB;
134
        }
135
      else if(strcmp(argv[inp_arg], "-v") == 0)
136
        {
137
          gdb_protocol = GDB_PROTOCOL_RSP;
138
          endpoint_target = ENDPOINT_TARGET_VPI;
139
        }
140
      else
141
        {
142
          serverPort = strtol(argv[2],&s,10);
143
        }
144
 
145
      inp_arg++;
146
    }
147
 
148
  if(endpoint_target == ENDPOINT_TARGET_NONE || gdb_protocol == GDB_PROTOCOL_NONE || serverPort > 65535 || *s != '\0')
149
    {
150
      print_usage();
151
      exit(1);
152
    }
153
 
154
#ifdef CYGWIN_COMPILE
155
  // Load the FTCJTAG DLL function pointers
156
  if (getFTDIJTAGFunctions() < 0){
157
    exit(-1);
158
  }
159
#endif
160
 
161
#ifndef CYGWIN_COMPILE
162
  // Install a signal handler to exit gracefully
163
  // when we receive a sigint
164
  signal(SIGINT, catch_sigint);
165
#endif
166
 
167
  /* Initialise connection to our OR1k system */
168
  current_chain = -1;
169
#ifdef USB_ENDPOINT_ENABLED
170
  /* USB Endpoint */
171
  if (endpoint_target == ENDPOINT_TARGET_USB)
172
    {
173
      printf("\nConnecting to OR1k via USB debug cable\n\n");
174
      if ((err = usb_dbg_reset())) goto JtagIfError;
175
      dbg_test(); // Perform some tests
176
    }
177
#endif
178
#ifdef VPI_ENDPOINT_ENABLED
179
  /* RTL simulation endpoint */
180
  if (endpoint_target == ENDPOINT_TARGET_VPI){
181
    printf("\nConnecting to OR1k RTL simulation\n\n");
182
    // Connect to the (hopefully) already running RTL simulation server running via VPI
183
    vpi_fd = vpi_connect();
184
 
185
    if ((err = vpi_dbg_reset())) goto JtagIfError;
186
    vpi_dbg_test(); // Perform some tests
187
  }
188
#endif
189
 
190
  /* We have a connection to the target system.  Now establish server connection. */
191 46 julius
  if(gdb_protocol == GDB_PROTOCOL_RSP)
192
    { // Connect to RSP server
193
      /* RSP always starts stalled as though we have just reset the processor. */
194
      // rsp_exception (EXCEPT_TRAP);
195
      handle_rsp ();
196 39 julius
    // if((server_fd = GetServerSocket("or1ksim","tcp", serverPort))) {
197 46 julius
    }else {
198 39 julius
    fprintf(stderr,"Cannot start RSP Proxy server on port %d\n", serverPort);
199
    exit(-1);
200
  }
201
 
202
 
203
JtagIfError:
204
  fprintf(stderr,"Connection via USB debug cable failed (err = %d).\nPlease ensure the device is attached and correctly installed\n\n", err);
205
  exit(-1);
206
  return 0;
207
}
208
 
209
 
210
int dbg_reset()
211
{
212
#ifdef USB_ENDPOINT_ENABLED
213
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_reset();
214
#endif
215
#ifdef VPI_ENDPOINT_ENABLED
216
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_reset();
217
#endif
218
  return DBG_ERR_INVALID_ENDPOINT;
219
}
220
 
221
void dbg_test() {
222
#ifdef USB_ENDPOINT_ENABLED
223
  if (endpoint_target == ENDPOINT_TARGET_USB) usb_dbg_test();
224
#endif
225
#ifdef VPI_ENDPOINT_ENABLED
226
  if (endpoint_target == ENDPOINT_TARGET_VPI) vpi_dbg_test();
227
#endif
228
}
229
 
230
/* Set TAP instruction register */
231
int dbg_set_tap_ir(uint32_t ir) {
232
#ifdef USB_ENDPOINT_ENABLED
233
  if (endpoint_target == ENDPOINT_TARGET_USB) usb_set_tap_ir(ir);
234
#endif
235
  return DBG_ERR_INVALID_ENDPOINT;
236
}
237
 
238
/* Sets scan chain.  */
239
int dbg_set_chain(uint32_t chain) {
240
#ifdef USB_ENDPOINT_ENABLED
241
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_set_chain(chain);
242
#endif
243
#ifdef VPI_ENDPOINT_ENABLED
244
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_set_chain(chain);
245
#endif
246
  return DBG_ERR_INVALID_ENDPOINT;
247
}
248
 
249
/* sends out a command with 32bit address and 16bit length, if len >= 0 */
250
int dbg_command(uint32_t type, uint32_t adr, uint32_t len) {
251
  // This is never called by any of the VPI functions, so only USB endpoint
252
#ifdef USB_ENDPOINT_ENABLED
253
 if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_command(type,adr,len);
254
#endif
255
 return DBG_ERR_INVALID_ENDPOINT;
256
}
257
 
258
/* writes a ctrl reg */
259
int dbg_ctrl(uint32_t reset, uint32_t stall) {
260
#ifdef USB_ENDPOINT_ENABLED
261
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_ctrl(reset, stall);
262
#endif
263
#ifdef VPI_ENDPOINT_ENABLED
264
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_ctrl(reset, stall);
265
#endif
266
  return DBG_ERR_INVALID_ENDPOINT;
267
}
268
 
269
/* reads control register */
270
int dbg_ctrl_read(uint32_t *reset, uint32_t *stall) {
271
#ifdef USB_ENDPOINT_ENABLED
272
    if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_ctrl_read(reset, stall);
273
#endif
274
#ifdef VPI_ENDPOINT_ENABLED
275
    if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_ctrl_read(reset, stall);
276
#endif
277
    return DBG_ERR_INVALID_ENDPOINT;
278
}
279
 
280
/* issues a burst read/write */
281
int dbg_go(unsigned char *data, uint16_t len, uint32_t read) {
282
  // Only USB endpouint32_t option here
283
#ifdef USB_ENDPOINT_ENABLED
284
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_go(data, len, read);
285
#endif
286
  return DBG_ERR_INVALID_ENDPOINT;
287
}
288
 
289 94 julius
/* read a byte from wishbone */
290
int dbg_wb_read8(uint32_t adr, uint8_t *data) {
291
#ifdef USB_ENDPOINT_ENABLED
292
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_read8(adr, data);
293
#endif
294
#ifdef VPI_ENDPOINT_ENABLED
295
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_read32(adr, data);
296
#endif
297
  return DBG_ERR_INVALID_ENDPOINT;
298
}
299
 
300
 
301 39 julius
/* read a word from wishbone */
302
int dbg_wb_read32(uint32_t adr, uint32_t *data) {
303
#ifdef USB_ENDPOINT_ENABLED
304
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_read32(adr, data);
305
#endif
306
#ifdef VPI_ENDPOINT_ENABLED
307
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_read32(adr, data);
308
#endif
309
  return DBG_ERR_INVALID_ENDPOINT;
310
}
311
 
312 46 julius
/* write a word to wishbone */
313
int dbg_wb_write8(uint32_t adr, uint8_t data) {
314
#ifdef USB_ENDPOINT_ENABLED
315
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_write8( adr, data);
316
#endif
317
  return DBG_ERR_INVALID_ENDPOINT;
318
}
319 39 julius
 
320
/* write a word to wishbone */
321
int dbg_wb_write32(uint32_t adr, uint32_t data) {
322
#ifdef USB_ENDPOINT_ENABLED
323
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_write32( adr, data);
324
#endif
325
#ifdef VPI_ENDPOINT_ENABLED
326
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_write32( adr, data);
327
#endif
328
  return DBG_ERR_INVALID_ENDPOINT;
329
}
330
 
331
/* read a block from wishbone */
332
int dbg_wb_read_block32(uint32_t adr, uint32_t *data, uint32_t len) {
333
#ifdef USB_ENDPOINT_ENABLED
334
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_read_block32( adr, data, len);
335
#endif
336
#ifdef VPI_ENDPOINT_ENABLED
337
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_read_block32( adr, data, len);
338
#endif
339
  return DBG_ERR_INVALID_ENDPOINT;
340
}
341
 
342
/* write a block to wishbone */
343
int dbg_wb_write_block32(uint32_t adr, uint32_t *data, uint32_t len) {
344
#ifdef USB_ENDPOINT_ENABLED
345
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_write_block32( adr, data, len);
346
#endif
347
#ifdef VPI_ENDPOINT_ENABLED
348
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_write_block32( adr, data, len);
349
#endif
350
  return DBG_ERR_INVALID_ENDPOINT;
351
}
352
 
353
/* read a register from cpu */
354 47 julius
int dbg_cpu0_read(uint32_t adr, uint32_t *data, uint32_t length) {
355 39 julius
#ifdef USB_ENDPOINT_ENABLED
356 47 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_read( adr, data, length);
357 39 julius
#endif
358
#ifdef VPI_ENDPOINT_ENABLED
359
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_read( adr, data);
360
#endif
361
  return DBG_ERR_INVALID_ENDPOINT;
362
}
363
 
364
/* write a cpu register */
365 47 julius
int dbg_cpu0_write(uint32_t adr, uint32_t *data, uint32_t length) {
366 39 julius
#ifdef USB_ENDPOINT_ENABLED
367 47 julius
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_write( adr, data, length);
368 39 julius
#endif
369
#ifdef VPI_ENDPOINT_ENABLED
370
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_write( adr, data);
371
#endif
372
  return DBG_ERR_INVALID_ENDPOINT;
373
}
374
 
375
/* write a cpu module register */
376
int dbg_cpu0_write_ctrl(uint32_t adr, unsigned char data) {
377
#ifdef USB_ENDPOINT_ENABLED
378
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_write_ctrl( adr, data);
379
#endif
380
#ifdef VPI_ENDPOINT_ENABLED
381
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_write_ctrl( adr, data);
382
#endif
383
  return DBG_ERR_INVALID_ENDPOINT;
384
}
385
 
386
 
387
/* read a register from cpu module */
388
int dbg_cpu0_read_ctrl(uint32_t adr, unsigned char *data) {
389
#ifdef USB_ENDPOINT_ENABLED
390
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_read_ctrl( adr, data);
391
#endif
392
#ifdef VPI_ENDPOINT_ENABLED
393
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_read_ctrl( adr, data);
394
#endif
395
  return DBG_ERR_INVALID_ENDPOINT;
396
}
397
 
398
 
399
void test_sdram(void) {
400
        return;
401
}
402
 
403
// Close down gracefully when we receive any kill signals
404
void catch_sigint(int sig_num)
405
{
406
  // Close down any potentially open sockets and USB handles
407
#ifdef VPI_ENDPOINT_ENABLED
408
  if (vpi_fd) close(vpi_fd);
409
#endif
410
  if (server_fd) close(server_fd);
411
  gdb_close();
412
#ifdef USB_ENDPOINT_ENABLED
413
  usb_close_device_handle();
414
#endif
415
  printf("\nInterrupt signal received. Closing down connections and exiting\n\n");
416
  exit(0);
417
}
418
 
419
void print_usage()
420
{
421
  printf("Invalid or insufficient arguments\n");
422
  printf("\n");
423
  printf("OpenRISC GDB proxy server usage: or_debug_proxy -server_type port\n");
424
  printf("\n");
425
  printf("server_type:\n");
426
#ifdef USB_ENDPOINT_ENABLED
427
  printf("\t-r Start a server using RSP, connection to hadware target via\n\t   USB\n");
428
  printf("\t-j Start a server using legacy OR remote JTAG protocol, to\n\t   hardware target via USB\n");
429
#endif
430
#ifdef VPI_ENDPOINT_ENABLED
431
  printf("\t-v Start a server using RSP, connection to RTL sim. VPI server\n\t   target via sockets\n");
432
#endif
433
  printf("\n");
434
  printf("port:\n");
435
  printf("\tAny free port within the usable range of 0 - 65535\n");
436
  printf("\n");
437
  printf("Example:\n");
438
#ifdef USB_ENDPOINT_ENABLED
439
  printf("\tStart a GDB server on port 5555, using RSP, connecting to\n\thardware target via USB\n");
440
  printf("\tor_debug_proxy -r 5555\n");
441
  printf("\n");
442
#endif
443
#ifdef VPI_ENDPOINT_ENABLED
444
  printf("\tStart a GDB server on port 5555, using RSP, connecting to\n\trtl target via VPI\n");
445
  printf("\tor_debug_proxy -v 5555\n");
446
  printf("\n");
447
#endif
448
  fflush (stdout);
449
}

powered by: WebSVN 2.1.0

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