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 46

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
 
211
int dbg_reset()
212
{
213
#ifdef USB_ENDPOINT_ENABLED
214
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_reset();
215
#endif
216
#ifdef VPI_ENDPOINT_ENABLED
217
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_reset();
218
#endif
219
  return DBG_ERR_INVALID_ENDPOINT;
220
}
221
 
222
void dbg_test() {
223
#ifdef USB_ENDPOINT_ENABLED
224
  if (endpoint_target == ENDPOINT_TARGET_USB) usb_dbg_test();
225
#endif
226
#ifdef VPI_ENDPOINT_ENABLED
227
  if (endpoint_target == ENDPOINT_TARGET_VPI) vpi_dbg_test();
228
#endif
229
}
230
 
231
/* Set TAP instruction register */
232
int dbg_set_tap_ir(uint32_t ir) {
233
#ifdef USB_ENDPOINT_ENABLED
234
  if (endpoint_target == ENDPOINT_TARGET_USB) usb_set_tap_ir(ir);
235
#endif
236
  return DBG_ERR_INVALID_ENDPOINT;
237
}
238
 
239
/* Sets scan chain.  */
240
int dbg_set_chain(uint32_t chain) {
241
#ifdef USB_ENDPOINT_ENABLED
242
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_set_chain(chain);
243
#endif
244
#ifdef VPI_ENDPOINT_ENABLED
245
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_set_chain(chain);
246
#endif
247
  return DBG_ERR_INVALID_ENDPOINT;
248
}
249
 
250
/* sends out a command with 32bit address and 16bit length, if len >= 0 */
251
int dbg_command(uint32_t type, uint32_t adr, uint32_t len) {
252
  // This is never called by any of the VPI functions, so only USB endpoint
253
#ifdef USB_ENDPOINT_ENABLED
254
 if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_command(type,adr,len);
255
#endif
256
 return DBG_ERR_INVALID_ENDPOINT;
257
}
258
 
259
/* writes a ctrl reg */
260
int dbg_ctrl(uint32_t reset, uint32_t stall) {
261
#ifdef USB_ENDPOINT_ENABLED
262
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_ctrl(reset, stall);
263
#endif
264
#ifdef VPI_ENDPOINT_ENABLED
265
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_ctrl(reset, stall);
266
#endif
267
  return DBG_ERR_INVALID_ENDPOINT;
268
}
269
 
270
/* reads control register */
271
int dbg_ctrl_read(uint32_t *reset, uint32_t *stall) {
272
#ifdef USB_ENDPOINT_ENABLED
273
    if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_ctrl_read(reset, stall);
274
#endif
275
#ifdef VPI_ENDPOINT_ENABLED
276
    if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_ctrl_read(reset, stall);
277
#endif
278
    return DBG_ERR_INVALID_ENDPOINT;
279
}
280
 
281
/* issues a burst read/write */
282
int dbg_go(unsigned char *data, uint16_t len, uint32_t read) {
283
  // Only USB endpouint32_t option here
284
#ifdef USB_ENDPOINT_ENABLED
285
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_go(data, len, read);
286
#endif
287
  return DBG_ERR_INVALID_ENDPOINT;
288
}
289
 
290
/* read a word from wishbone */
291
int dbg_wb_read32(uint32_t adr, uint32_t *data) {
292
#ifdef USB_ENDPOINT_ENABLED
293
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_read32(adr, data);
294
#endif
295
#ifdef VPI_ENDPOINT_ENABLED
296
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_read32(adr, data);
297
#endif
298
  return DBG_ERR_INVALID_ENDPOINT;
299
}
300
 
301 46 julius
/* write a word to wishbone */
302
int dbg_wb_write8(uint32_t adr, uint8_t data) {
303
#ifdef USB_ENDPOINT_ENABLED
304
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_write8( adr, data);
305
#endif
306
  return DBG_ERR_INVALID_ENDPOINT;
307
}
308 39 julius
 
309
/* write a word to wishbone */
310
int dbg_wb_write32(uint32_t adr, uint32_t data) {
311
#ifdef USB_ENDPOINT_ENABLED
312
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_write32( adr, data);
313
#endif
314
#ifdef VPI_ENDPOINT_ENABLED
315
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_write32( adr, data);
316
#endif
317
  return DBG_ERR_INVALID_ENDPOINT;
318
}
319
 
320
/* read a block from wishbone */
321
int dbg_wb_read_block32(uint32_t adr, uint32_t *data, uint32_t len) {
322
#ifdef USB_ENDPOINT_ENABLED
323
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_read_block32( adr, data, len);
324
#endif
325
#ifdef VPI_ENDPOINT_ENABLED
326
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_read_block32( adr, data, len);
327
#endif
328
  return DBG_ERR_INVALID_ENDPOINT;
329
}
330
 
331
 
332
/* write a block to wishbone */
333
int dbg_wb_write_block32(uint32_t adr, uint32_t *data, uint32_t len) {
334
#ifdef USB_ENDPOINT_ENABLED
335
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_write_block32( adr, data, len);
336
#endif
337
#ifdef VPI_ENDPOINT_ENABLED
338
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_write_block32( adr, data, len);
339
#endif
340
  return DBG_ERR_INVALID_ENDPOINT;
341
}
342
 
343
/* read a register from cpu */
344
int dbg_cpu0_read(uint32_t adr, uint32_t *data) {
345
#ifdef USB_ENDPOINT_ENABLED
346
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_read( adr, data);
347
#endif
348
#ifdef VPI_ENDPOINT_ENABLED
349
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_read( adr, data);
350
#endif
351
  return DBG_ERR_INVALID_ENDPOINT;
352
}
353
 
354
/* write a cpu register */
355
int dbg_cpu0_write(uint32_t adr, uint32_t data) {
356
#ifdef USB_ENDPOINT_ENABLED
357
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_write( adr, data);
358
#endif
359
#ifdef VPI_ENDPOINT_ENABLED
360
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_write( adr, data);
361
#endif
362
  return DBG_ERR_INVALID_ENDPOINT;
363
}
364
 
365
/* write a cpu module register */
366
int dbg_cpu0_write_ctrl(uint32_t adr, unsigned char data) {
367
#ifdef USB_ENDPOINT_ENABLED
368
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_write_ctrl( adr, data);
369
#endif
370
#ifdef VPI_ENDPOINT_ENABLED
371
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_write_ctrl( adr, data);
372
#endif
373
  return DBG_ERR_INVALID_ENDPOINT;
374
}
375
 
376
 
377
/* read a register from cpu module */
378
int dbg_cpu0_read_ctrl(uint32_t adr, unsigned char *data) {
379
#ifdef USB_ENDPOINT_ENABLED
380
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_read_ctrl( adr, data);
381
#endif
382
#ifdef VPI_ENDPOINT_ENABLED
383
  if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_read_ctrl( adr, data);
384
#endif
385
  return DBG_ERR_INVALID_ENDPOINT;
386
}
387
 
388
 
389
void test_sdram(void) {
390
        return;
391
}
392
 
393
// Close down gracefully when we receive any kill signals
394
void catch_sigint(int sig_num)
395
{
396
  // Close down any potentially open sockets and USB handles
397
#ifdef VPI_ENDPOINT_ENABLED
398
  if (vpi_fd) close(vpi_fd);
399
#endif
400
  if (server_fd) close(server_fd);
401
  gdb_close();
402
#ifdef USB_ENDPOINT_ENABLED
403
  usb_close_device_handle();
404
#endif
405
  printf("\nInterrupt signal received. Closing down connections and exiting\n\n");
406
  exit(0);
407
}
408
 
409
void print_usage()
410
{
411
  printf("Invalid or insufficient arguments\n");
412
  printf("\n");
413
  printf("OpenRISC GDB proxy server usage: or_debug_proxy -server_type port\n");
414
  printf("\n");
415
  printf("server_type:\n");
416
#ifdef USB_ENDPOINT_ENABLED
417
  printf("\t-r Start a server using RSP, connection to hadware target via\n\t   USB\n");
418
  printf("\t-j Start a server using legacy OR remote JTAG protocol, to\n\t   hardware target via USB\n");
419
#endif
420
#ifdef VPI_ENDPOINT_ENABLED
421
  printf("\t-v Start a server using RSP, connection to RTL sim. VPI server\n\t   target via sockets\n");
422
#endif
423
  printf("\n");
424
  printf("port:\n");
425
  printf("\tAny free port within the usable range of 0 - 65535\n");
426
  printf("\n");
427
  printf("Example:\n");
428
#ifdef USB_ENDPOINT_ENABLED
429
  printf("\tStart a GDB server on port 5555, using RSP, connecting to\n\thardware target via USB\n");
430
  printf("\tor_debug_proxy -r 5555\n");
431
  printf("\n");
432
#endif
433
#ifdef VPI_ENDPOINT_ENABLED
434
  printf("\tStart a GDB server on port 5555, using RSP, connecting to\n\trtl target via VPI\n");
435
  printf("\tor_debug_proxy -v 5555\n");
436
  printf("\n");
437
#endif
438
  fflush (stdout);
439
}

powered by: WebSVN 2.1.0

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