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 1779

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
#include "usb_functions.h"
69
#include "vpi_functions.h"
70
#include "or_debug_proxy.h"
71
 
72
// Defines of endpoint numbers
73
#define ENDPOINT_TARGET_NONE 0
74
#define ENDPOINT_TARGET_USB 1
75
#define ENDPOINT_TARGET_VPI 2
76
static int endpoint_target; // Either VPI interface via sockets, or the USB device
77
 
78
#define GDB_PROTOCOL_JTAG  1
79
#define GDB_PROTOCOL_RSP   2
80
#define GDB_PROTOCOL_NONE  3
81
 
82
int err; // Global error value
83
 
84
/* Currently selected scan chain - just to prevent unnecessary transfers. */
85
int current_chain = -1;
86
 
87
/* The chain that should be currently selected. */
88
int dbg_chain = -1;
89
 
90
int main(int argc,  char *argv[]) {
91
 
92
  char *s;
93
  int gdb_protocol = GDB_PROTOCOL_NONE;
94
  endpoint_target = ENDPOINT_TARGET_NONE;
95
  int inp_arg = 1;
96
 
97
  // init our global error number
98
  err = DBG_ERR_OK;
99
 
100
  // Parse input options
101
  if (argc < 3)
102
    {
103
      print_usage();
104
      exit(1);
105
    }
106
 
107
  err = DBG_ERR_OK;
108
  srand(getpid());
109
 
110
  // Parse through the input, check what we've been given
111
 
112
  while ( argv[inp_arg] != NULL )
113
    {
114
      if(strcmp(argv[inp_arg], "-j") == 0)
115
        {
116
          gdb_protocol = GDB_PROTOCOL_JTAG;
117
          endpoint_target = ENDPOINT_TARGET_USB;
118
        }
119
      else if(strcmp(argv[inp_arg], "-r") == 0)
120
        {
121
          gdb_protocol = GDB_PROTOCOL_RSP;
122
          endpoint_target = ENDPOINT_TARGET_USB;
123
        }
124
      else if(strcmp(argv[inp_arg], "-v") == 0)
125
        {
126
          gdb_protocol = GDB_PROTOCOL_RSP;
127
          endpoint_target = ENDPOINT_TARGET_VPI;
128
        }
129
      else
130
        {
131
          serverPort = strtol(argv[2],&s,10);
132
        }
133
 
134
      inp_arg++;
135
    }
136
 
137
  if(endpoint_target == ENDPOINT_TARGET_NONE || gdb_protocol == GDB_PROTOCOL_NONE || serverPort > 65535 || *s != '\0')
138
    {
139
      print_usage();
140
      exit(1);
141
    }
142
 
143
#ifdef CYGWIN_COMPILE
144
  // Load the FTCJTAG DLL function pointers
145
  if (getFTDIJTAGFunctions() < 0){
146
    exit(-1);
147
  }
148
#endif
149
 
150
#ifndef CYGWIN_COMPILE
151
  // Install a signal handler to exit gracefully
152
  // when we receive a sigint
153
  signal(SIGINT, catch_sigint);
154
#endif
155
 
156
  /* Initialise connection to our OR1k system */
157
  current_chain = -1;
158
  /* USB Endpoint */
159
  if (endpoint_target == ENDPOINT_TARGET_USB)
160
    {
161
      printf("\nConnecting to OR1k via USB debug cable\n\n");
162
      if ((err = usb_dbg_reset())) goto JtagIfError;
163
      dbg_test(); // Perform some tests
164
    }
165
  /* RTL simulation endpoint */
166
  else if (endpoint_target == ENDPOINT_TARGET_VPI){
167
    printf("\nConnecting to OR1k RTL simulation\n\n");
168
    // Connect to the (hopefully) already running RTL simulation server running via VPI
169
    vpi_fd = vpi_connect();
170
 
171
    if ((err = vpi_dbg_reset())) goto JtagIfError;
172
    vpi_dbg_test(); // Perform some tests
173
  }
174
 
175
  /* We have a connection to the target system.  Now establish server connection. */
176
  if(gdb_protocol == GDB_PROTOCOL_JTAG)
177
  { // Connect to JTAG server
178
    if((server_fd = GetServerSocket("or1ksim","tcp", serverPort))) {
179
      // printf("JTAG Proxy server started on port %d\n", serverPort);
180
      printf("Remote JTAG proxy server started on port %d\n", serverPort);
181
      printf("Note: The OpenRISC remote JTAG protocol is now DEPRECATED. Please use GDB version 6.8 or later.\n");
182
      printf("Press CTRL+c to exit.\n");
183
    } else {
184
      // fprintf(stderr,"Cannot start JTAG Proxy server on port %d\n", serverPort);
185
      fprintf(stderr,"Cannot start Proxy server on port %d\n", serverPort);
186
      exit(-1);
187
    }
188
 
189
    /* Do endless loop of checking and handle GDB requests.  Ctrl-c exits.  */
190
    // HandleServerSocket(true);
191
    HandleServerSocket();
192
    return 0;
193
  }
194
  else if(gdb_protocol == GDB_PROTOCOL_RSP)
195
  { // Connect to RSP server
196
    /* RSP always starts stalled as though we have just reset the processor. */
197
    // rsp_exception (EXCEPT_TRAP);
198
    handle_rsp ();
199
    // if((server_fd = GetServerSocket("or1ksim","tcp", serverPort))) {
200
  }else {
201
    fprintf(stderr,"Cannot start RSP Proxy server on port %d\n", serverPort);
202
    exit(-1);
203
  }
204
 
205
 
206
JtagIfError:
207
  fprintf(stderr,"Connection via USB debug cable failed (err = %d).\nPlease ensure the device is attached and correctly installed\n\n", err);
208
  exit(-1);
209
  return 0;
210
}
211
 
212
 
213
 
214
int dbg_reset()
215
{
216
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_reset();
217
  else if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_reset();
218
  else return DBG_ERR_INVALID_ENDPOINT;
219
}
220
 
221
void dbg_test() {
222
  if (endpoint_target == ENDPOINT_TARGET_USB) usb_dbg_test();
223
  else if (endpoint_target == ENDPOINT_TARGET_VPI) vpi_dbg_test();
224
}
225
 
226
/* Set TAP instruction register */
227
int dbg_set_tap_ir(uint32_t ir) {
228
  if (endpoint_target == ENDPOINT_TARGET_USB) usb_set_tap_ir(ir);
229
  else return DBG_ERR_INVALID_ENDPOINT;
230
  return 0;
231
}
232
 
233
/* Sets scan chain.  */
234
int dbg_set_chain(uint32_t chain) {
235
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_set_chain(chain);
236
  else if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_set_chain(chain);
237
  else return DBG_ERR_INVALID_ENDPOINT;
238
}
239
 
240
/* sends out a command with 32bit address and 16bit length, if len >= 0 */
241
int dbg_command(uint32_t type, uint32_t adr, uint32_t len) {
242
  // This is never called by any of the VPI functions, so only USB endpoint
243
 if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_command(type,adr,len);
244
 else return DBG_ERR_INVALID_ENDPOINT;
245
}
246
 
247
/* writes a ctrl reg */
248
int dbg_ctrl(uint32_t reset, uint32_t stall) {
249
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_ctrl(reset, stall);
250
  else if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_ctrl(reset, stall);
251
  else return DBG_ERR_INVALID_ENDPOINT;
252
}
253
 
254
/* reads control register */
255
int dbg_ctrl_read(uint32_t *reset, uint32_t *stall) {
256
    if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_ctrl_read(reset, stall);
257
    else if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_ctrl_read(reset, stall);
258
    else return DBG_ERR_INVALID_ENDPOINT;
259
}
260
 
261
/* issues a burst read/write */
262
int dbg_go(unsigned char *data, uint16_t len, uint32_t read) {
263
  // Only USB endpouint32_t option here
264
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_go(data, len, read);
265
  else return DBG_ERR_INVALID_ENDPOINT;
266
}
267
 
268
/* read a word from wishbone */
269
int dbg_wb_read32(uint32_t adr, uint32_t *data) {
270
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_read32(adr, data);
271
  else if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_read32(adr, data);
272
  else return DBG_ERR_INVALID_ENDPOINT;
273
}
274
 
275
 
276
/* write a word to wishbone */
277
int dbg_wb_write32(uint32_t adr, uint32_t data) {
278
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_write32( adr, data);
279
  else if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_write32( adr, data);
280
  else return DBG_ERR_INVALID_ENDPOINT;
281
}
282
 
283
/* read a block from wishbone */
284
int dbg_wb_read_block32(uint32_t adr, uint32_t *data, uint32_t len) {
285
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_read_block32( adr, data, len);
286
  else if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_read_block32( adr, data, len);
287
  else return DBG_ERR_INVALID_ENDPOINT;
288
}
289
 
290
 
291
/* write a block to wishbone */
292
int dbg_wb_write_block32(uint32_t adr, uint32_t *data, uint32_t len) {
293
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_wb_write_block32( adr, data, len);
294
  else if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_wb_write_block32( adr, data, len);
295
  else return DBG_ERR_INVALID_ENDPOINT;
296
}
297
 
298
/* read a register from cpu */
299
int dbg_cpu0_read(uint32_t adr, uint32_t *data) {
300
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_read( adr, data);
301
  else if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_read( adr, data);
302
  else return DBG_ERR_INVALID_ENDPOINT;
303
}
304
 
305
/* write a cpu register */
306
int dbg_cpu0_write(uint32_t adr, uint32_t data) {
307
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_write( adr, data);
308
  else if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_write( adr, data);
309
  else return DBG_ERR_INVALID_ENDPOINT;
310
}
311
 
312
/* write a cpu module register */
313
int dbg_cpu0_write_ctrl(uint32_t adr, unsigned char data) {
314
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_write_ctrl( adr, data);
315
  else if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_write_ctrl( adr, data);
316
  else return DBG_ERR_INVALID_ENDPOINT;
317
}
318
 
319
 
320
/* read a register from cpu module */
321
int dbg_cpu0_read_ctrl(uint32_t adr, unsigned char *data) {
322
  if (endpoint_target == ENDPOINT_TARGET_USB) return usb_dbg_cpu0_read_ctrl( adr, data);
323
  else if (endpoint_target == ENDPOINT_TARGET_VPI) return vpi_dbg_cpu0_read_ctrl( adr, data);
324
  else return DBG_ERR_INVALID_ENDPOINT;
325
}
326
 
327
 
328
void test_sdram(void) {
329
        return;
330
}
331
 
332
// Close down gracefully when we receive any kill signals
333
void catch_sigint(int sig_num)
334
{
335
  // Close down any potentially open sockets and USB handles
336
  if (vpi_fd) close(vpi_fd);
337
  if (server_fd) close(server_fd);
338
  usb_close_device_handle();
339
  gdb_close();
340
  printf("\nInterrupt signal received. Closing down connections and exiting\n\n");
341
  exit(0);
342
}
343
 
344
void print_usage()
345
{
346
  printf("Invalid or insufficient arguments\n");
347
  printf("\n");
348
  printf("OpenRISC GDB proxy server usage: or_debug_proxy -server_type port\n");
349
  printf("\n");
350
  printf("server_type:\n");
351
  printf("\t-r Start a server using RSP, connection to hadware target via\n\t   USB\n");
352
  printf("\t-j Start a server using legacy OR remote JTAG protocol, to\n\t   hardware target via USB\n");
353
  printf("\t-v Start a server using RSP, connection to RTL sim. VPI server\n\t   target via sockets\n");
354
  printf("\n");
355
  printf("port:\n");
356
  printf("\tAny free port within the usable range of 0 - 65535\n");
357
  printf("\n");
358
  printf("Example:\n");
359
  printf("\tStart a GDB server on port 5555, using RSP, connecting to\n\thardware target via USB\n");
360
  printf("\tor_debug_proxy -r 5555\n");
361
  printf("\n");
362
  fflush (stdout);
363
}

powered by: WebSVN 2.1.0

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