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 519

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 497 julius
void catch_sigint(int sig_num); // First param must be "int"
65 39 julius
#endif
66
 
67
#include "gdb.h"
68
 
69
#ifdef USB_ENDPOINT_ENABLED
70
#include "usb_functions.h"
71
#endif
72
 
73
#include "or_debug_proxy.h"
74
 
75
// Defines of endpoint numbers
76
#define ENDPOINT_TARGET_NONE 0
77
#define ENDPOINT_TARGET_USB 1
78 376 julius
#define ENDPOINT_TARGET_OTHER 2
79 39 julius
 
80 497 julius
static int endpoint_target;     // Value to hold targeted endpoint
81 39 julius
 
82
#define GDB_PROTOCOL_JTAG  1
83
#define GDB_PROTOCOL_RSP   2
84
#define GDB_PROTOCOL_NONE  3
85
 
86 497 julius
int err;                        // Global error value
87 39 julius
 
88
/* Currently selected scan chain - just to prevent unnecessary transfers. */
89
int current_chain = -1;
90
 
91
/* The chain that should be currently selected. */
92
int dbg_chain = -1;
93
 
94 497 julius
int main(int argc, char *argv[])
95
{
96 39 julius
 
97 497 julius
        char *s;
98
        int gdb_protocol = GDB_PROTOCOL_NONE;
99
        endpoint_target = ENDPOINT_TARGET_NONE;
100
        int inp_arg = 1;
101 39 julius
 
102 497 julius
        // Check we were compiled with at least one endpoint enabled
103 39 julius
#ifndef USB_ENDPOINT_ENABLED
104 497 julius
        printf
105
            ("No endpoints enabled.\nRecompile the proxy with at least one endpoint enabled\n");
106
        exit(0);
107 39 julius
#endif
108
 
109 497 julius
        // init our global error number
110
        err = DBG_ERR_OK;
111 39 julius
 
112 497 julius
        // Parse input options
113
        if (argc < 3) {
114
                print_usage();
115
                exit(1);
116
        }
117 39 julius
 
118 497 julius
        err = DBG_ERR_OK;
119
        srand(getpid());
120 39 julius
 
121 497 julius
        // Parse through the input, check what we've been given
122 39 julius
 
123 497 julius
        while (argv[inp_arg] != NULL) {
124
                if (strcmp(argv[inp_arg], "-r") == 0) {
125
                        gdb_protocol = GDB_PROTOCOL_RSP;
126
                        endpoint_target = ENDPOINT_TARGET_USB;
127
                } else if (strcmp(argv[inp_arg], "-o") == 0) {
128
                        gdb_protocol = GDB_PROTOCOL_RSP;
129
                        endpoint_target = ENDPOINT_TARGET_OTHER;
130
                } else if (strcmp(argv[inp_arg], "-k") == 0) {
131
                        kernel_debug = 1;
132
                } else {
133
                        serverPort = strtol(argv[2], &s, 10);
134
                }
135
 
136
                inp_arg++;
137 39 julius
        }
138 497 julius
 
139
        if (endpoint_target == ENDPOINT_TARGET_NONE ||
140
            gdb_protocol == GDB_PROTOCOL_NONE ||
141
            serverPort > 65535 || *s != '\0') {
142
                print_usage();
143
                exit(1);
144 39 julius
        }
145 497 julius
#ifdef CYGWIN_COMPILE
146
        // Load the FTCJTAG DLL function pointers
147
        if (getFTDIJTAGFunctions() < 0) {
148
                exit(-1);
149 39 julius
        }
150
#endif
151
 
152
#ifndef CYGWIN_COMPILE
153 497 julius
        // Install a signal handler to exit gracefully
154
        // when we receive a sigint
155
        signal(SIGINT, catch_sigint);
156 39 julius
#endif
157 497 julius
 
158
        /* Initialise connection to our OR1k system */
159
        current_chain = -1;
160 39 julius
#ifdef USB_ENDPOINT_ENABLED
161 497 julius
        /* USB Endpoint */
162
        if (endpoint_target == ENDPOINT_TARGET_USB) {
163
                printf("\nConnecting to OR1k via USB debug cable\n\n");
164
                if ((err = usb_dbg_reset()))
165
                        goto JtagIfError;
166
                dbg_test();     // Perform some tests
167
        }
168 39 julius
#endif
169
 
170 497 julius
        /* We have a connection to the target system.  Now establish server
171
           connection. */
172
        if (gdb_protocol == GDB_PROTOCOL_RSP) { // Connect to RSP server
173
                /* RSP always starts stalled as though we have just reset the
174
                   processor. */
175
                // rsp_exception (EXCEPT_TRAP);
176
                handle_rsp();
177
        } else {
178
                fprintf(stderr, "Cannot start RSP Proxy server on port %d\n",
179
                        serverPort);
180
                exit(-1);
181
        }
182 39 julius
 
183
JtagIfError:
184 497 julius
        fprintf(stderr,
185
                "Connection via USB debug cable failed (err = %d).\nPlease ensure the device is attached and correctly installed\n\n",
186
                err);
187
        exit(-1);
188
        return 0;
189 39 julius
}
190
 
191
int dbg_reset()
192
{
193
#ifdef USB_ENDPOINT_ENABLED
194 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
195
                return usb_dbg_reset();
196 39 julius
#endif
197 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
198 39 julius
}
199
 
200 497 julius
void dbg_test()
201
{
202 39 julius
#ifdef USB_ENDPOINT_ENABLED
203 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
204
                usb_dbg_test();
205 39 julius
#endif
206
}
207
 
208
/* Set TAP instruction register */
209 497 julius
int dbg_set_tap_ir(uint32_t ir)
210
{
211 39 julius
#ifdef USB_ENDPOINT_ENABLED
212 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
213
                usb_set_tap_ir(ir);
214 39 julius
#endif
215 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
216 39 julius
}
217
 
218
/* Sets scan chain.  */
219 497 julius
int dbg_set_chain(uint32_t chain)
220
{
221 39 julius
#ifdef USB_ENDPOINT_ENABLED
222 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
223
                return usb_dbg_set_chain(chain);
224 39 julius
#endif
225 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
226
}
227 39 julius
 
228
/* sends out a command with 32bit address and 16bit length, if len >= 0 */
229 497 julius
int dbg_command(uint32_t type, uint32_t adr, uint32_t len)
230
{
231
        // This is never called by any of the VPI functions, so only USB 
232
        // endpoint
233 39 julius
#ifdef USB_ENDPOINT_ENABLED
234 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
235
                return usb_dbg_command(type, adr, len);
236 39 julius
#endif
237 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
238 39 julius
}
239
 
240
/* writes a ctrl reg */
241 497 julius
int dbg_ctrl(uint32_t reset, uint32_t stall)
242
{
243 39 julius
#ifdef USB_ENDPOINT_ENABLED
244 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
245
                return usb_dbg_ctrl(reset, stall);
246 39 julius
#endif
247 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
248 39 julius
}
249
 
250
/* reads control register */
251 497 julius
int dbg_ctrl_read(uint32_t * reset, uint32_t * stall)
252
{
253 39 julius
#ifdef USB_ENDPOINT_ENABLED
254 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
255
                return usb_dbg_ctrl_read(reset, stall);
256 39 julius
#endif
257 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
258 39 julius
}
259
 
260
/* issues a burst read/write */
261 497 julius
int dbg_go(unsigned char *data, uint16_t len, uint32_t read)
262
{
263
        // Only USB endpouint32_t option here
264 39 julius
#ifdef USB_ENDPOINT_ENABLED
265 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
266
                return usb_dbg_go(data, len, read);
267 39 julius
#endif
268 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
269 39 julius
}
270
 
271 94 julius
/* read a byte from wishbone */
272 497 julius
int dbg_wb_read8(uint32_t adr, uint8_t * data)
273
{
274 94 julius
#ifdef USB_ENDPOINT_ENABLED
275 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
276
                return usb_dbg_wb_read8(adr, data);
277 94 julius
#endif
278 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
279 94 julius
}
280
 
281 39 julius
/* read a word from wishbone */
282 497 julius
int dbg_wb_read32(uint32_t adr, uint32_t * data)
283
{
284 39 julius
#ifdef USB_ENDPOINT_ENABLED
285 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
286
                return usb_dbg_wb_read32(adr, data);
287 39 julius
#endif
288 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
289 39 julius
}
290
 
291 46 julius
/* write a word to wishbone */
292 497 julius
int dbg_wb_write8(uint32_t adr, uint8_t data)
293
{
294 46 julius
#ifdef USB_ENDPOINT_ENABLED
295 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
296
                return usb_dbg_wb_write8(adr, data);
297 46 julius
#endif
298 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
299 46 julius
}
300 39 julius
 
301
/* write a word to wishbone */
302 497 julius
int dbg_wb_write32(uint32_t adr, uint32_t data)
303
{
304 39 julius
#ifdef USB_ENDPOINT_ENABLED
305 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
306
                return usb_dbg_wb_write32(adr, data);
307 39 julius
#endif
308 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
309 39 julius
}
310
 
311
/* read a block from wishbone */
312 497 julius
int dbg_wb_read_block32(uint32_t adr, uint32_t * data, uint32_t len)
313
{
314 39 julius
#ifdef USB_ENDPOINT_ENABLED
315 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
316
                return usb_dbg_wb_read_block32(adr, data, len);
317 39 julius
#endif
318 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
319 39 julius
}
320
 
321
/* write a block to wishbone */
322 497 julius
int dbg_wb_write_block32(uint32_t adr, uint32_t * data, uint32_t len)
323
{
324 39 julius
#ifdef USB_ENDPOINT_ENABLED
325 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
326
                return usb_dbg_wb_write_block32(adr, data, len);
327 39 julius
#endif
328 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
329 39 julius
}
330
 
331
/* read a register from cpu */
332 497 julius
int dbg_cpu0_read(uint32_t adr, uint32_t * data, uint32_t length)
333
{
334 39 julius
#ifdef USB_ENDPOINT_ENABLED
335 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
336
                return usb_dbg_cpu0_read(adr, data, length);
337 39 julius
#endif
338 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
339 39 julius
}
340
 
341
/* write a cpu register */
342 497 julius
int dbg_cpu0_write(uint32_t adr, uint32_t * data, uint32_t length)
343
{
344 39 julius
#ifdef USB_ENDPOINT_ENABLED
345 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
346
                return usb_dbg_cpu0_write(adr, data, length);
347 39 julius
#endif
348 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
349 39 julius
}
350
 
351
/* write a cpu module register */
352 497 julius
int dbg_cpu0_write_ctrl(uint32_t adr, unsigned char data)
353
{
354 39 julius
#ifdef USB_ENDPOINT_ENABLED
355 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
356
                return usb_dbg_cpu0_write_ctrl(adr, data);
357 39 julius
#endif
358 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
359 39 julius
}
360
 
361
/* read a register from cpu module */
362 497 julius
int dbg_cpu0_read_ctrl(uint32_t adr, unsigned char *data)
363
{
364 39 julius
#ifdef USB_ENDPOINT_ENABLED
365 497 julius
        if (endpoint_target == ENDPOINT_TARGET_USB)
366
                return usb_dbg_cpu0_read_ctrl(adr, data);
367 39 julius
#endif
368 497 julius
        return DBG_ERR_INVALID_ENDPOINT;
369 39 julius
}
370
 
371 497 julius
void test_sdram(void)
372
{
373 39 julius
        return;
374
}
375
 
376
// Close down gracefully when we receive any kill signals
377
void catch_sigint(int sig_num)
378
{
379 497 julius
        // Close down any potentially open sockets and USB handles
380
        if (server_fd)
381
                close(server_fd);
382
        gdb_close();
383 39 julius
#ifdef USB_ENDPOINT_ENABLED
384 497 julius
        usb_close_device_handle();
385 39 julius
#endif
386 497 julius
        printf
387
            ("\nInterrupt signal received. Closing down connections and exiting\n\n");
388
        exit(0);
389 39 julius
}
390
 
391
void print_usage()
392
{
393 497 julius
        printf("\n");
394
        printf("Usage: or_debug_proxy <SERVERTYPE> <PORT> <OPTIONS>\n");
395
        printf("OpenRISC GDB Proxy Server\n");
396
        printf("\n");
397
        printf("\tServer Type:\n");
398 39 julius
#ifdef USB_ENDPOINT_ENABLED
399 497 julius
        printf
400
            ("\t-r\tStart a server using RSP, connection to hadware target via\n\t\tUSB\n");
401
        printf
402
            ("\t-j\tStart a server using legacy OR remote JTAG protocol, to\n\t\thardware target via USB (DEPRECATED)\n");
403
        printf("\n");
404 39 julius
#endif
405 497 julius
        printf("\tPort:\n");
406
        printf("\t\tAny free port within the usable range of 0 - 65535\n");
407
        printf("\n");
408
        printf("\tOptions:\n");
409
        printf
410
            ("\t-k\tAccesses to 0xC0000000 map to 0x0. Useful for kernel debugging.\n");
411
        printf("\n");
412
        printf("\tExample:\n");
413 39 julius
#ifdef USB_ENDPOINT_ENABLED
414 497 julius
        printf
415
            ("\t\tStart a GDB server on port 50001, using RSP, connecting to\n\t\thardware target via USB:\n\n");
416
        printf("\t\t./or_debug_proxy -r 50001\n");
417
        printf("\n");
418 39 julius
#endif
419 497 julius
        fflush(stdout);
420 39 julius
}

powered by: WebSVN 2.1.0

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