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

Subversion Repositories or1k

[/] [or1k/] [branches/] [mp3_stable/] [jtag/] [jp1.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 407 simons
/* jp1-linux.c -- JTAG protocol via parallel port for linux
2
   Copyright (C) 2001 Marko Mlinar, markom@opencores.org
3
   Code for TCP/IP copied from gdb, by Chris Ziomkowski
4
 
5
This file is part of OpenRISC 1000 Architectural Simulator.
6
 
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
 
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
 
21
/* Establishes jtag proxy server and communicates with parallel
22
 port directly.  Requires root access. */
23
 
24
#include <stdio.h>
25
#include <ctype.h>
26
#include <string.h>
27
#include <stdlib.h>
28
#include <unistd.h>
29
#include <stdarg.h>
30
 
31
/* Dirty way to include inb and outb from, but they say it is
32
   a standard one.  */
33
#include <asm/io.h>
34
#include <asm/system.h>
35
 
36
#define GDB_IN "../sim/rtl_sim/run/gdb_in.dat"
37
#define GDB_OUT "../sim/rtl_sim/run/gdb_out.dat"
38
 
39
/* Libraries for JTAG proxy server.  */
40
#include <sys/stat.h>
41
#include <sys/types.h>
42
#include <sys/socket.h>
43
#include <netinet/in.h>
44
#include <sys/select.h>
45
#include <sys/poll.h>
46
#include <fcntl.h>
47
#include <netdb.h>
48
#include <netinet/tcp.h>
49
#include <inttypes.h>
50
#include <errno.h>
51
 
52
#include "gdb.h" /* partially copied from gdb/config/or1k */
53
 
54
#ifdef DEBUG
55
#define debug printf
56
#define debug2 printf
57
#define flush_debug() fflush(stdout)
58
#else
59
#define debug
60
#define debug2 printf
61
#define flush_debug()
62
#endif
63
 
64
#define LPT_BASE (base)
65
#define LPT_READ (LPT_BASE+1)
66
#define LPT_WRITE LPT_BASE
67
#if RTL_SIM
68
#define TCLK_BIT (0x01) /* D0, pin #2 */
69
#define TRST_BIT (0x02) /* D1, pin #3 */
70
#define TDI_BIT  (0x04) /* D2, pin #4 */
71
#define TMS_BIT  (0x08) /* D0, pin #5 */
72
#define TDO_BIT  (0x20) /* PE, pin #12 */
73
#define TMS      (0x02)
74
#define TDI      (0x01)
75
#else
76
#define TCLK_BIT (0x04) /* D2 pin 4 */
77
#define TRST_BIT (0x08) /* D3 pin 5 */
78
#define TDI_BIT  (0x10) /* D4 pin 6 */
79
#define TMS_BIT  (0x20) /* D5 pin 7 */
80
#define TDO_BIT  (0x20) /* S5 pin 12*/
81
#define TMS      (0x02)
82
#define TDI      (0x01)
83
#endif
84
#ifdef RTL_SIM
85
# define JTAG_WAIT() usleep(1000)
86
# define NUM_RETRIES (16)
87
# define JTAG_RETRY_WAIT() usleep (1000)
88
#else
89
# define JTAG_WAIT() {      \
90
  int i;        \
91
  volatile int j;   \
92
  for(i = 0; i < 1000; i++) \
93
    j = i;      \
94
  }
95
# define NUM_RETRIES (16)
96
# define JTAG_RETRY_WAIT() usleep (1000)
97
#endif
98
 
99
/* Selects crc trailer size in bits. Currently supported: 8 */
100
#define CRC_SIZE (8)
101
 
102
/* Scan chain size in bits.  */
103
#define SC_SIZE (4)
104
 
105
#ifndef ULONGEST
106
#define ULONGEST unsigned long
107
#endif
108
 
109
typedef enum {
110
  false = 0,
111
  true = 1,
112
} Boolean;
113
 
114
static int base = 0x378; /* FIXME: We should detect the address. */
115
int err = 0;
116
int set_pc = 0;
117
int set_step = 0;
118
 
119
unsigned int serverIP = 0;
120
unsigned int serverPort = 0;
121
unsigned int server_fd = 0;
122
unsigned int gdb_fd = 0;
123
void HandleServerSocket(Boolean);
124
void JTAGRequest(void);
125
void GDBRequest(void);
126
void ProtocolClean(int,int32_t);
127
static int gdb_read(void*,int);
128
static int gdb_write(void*,int);
129
static void jtag_set_chain (int);
130
 
131
/* Scan chain info. */
132
/* *INDENT-OFF* */
133
static int chain_addr_size[] = { 0,  32, 0,  0,  5,  32, 32};
134
static int chain_data_size[] = { 0,  32, 0,  32, 32, 32, 32};
135
static int chain_is_valid[]  = { 0,  1,  0,  1,  1,  1,   1};
136
static int chain_has_crc[]   = { 0,  1,  0,  1,  1,  1,   1};
137
static int chain_has_rw[]    = { 0,  1,  0,  0,  1,  1,   1};
138
/* *INDENT-OFF* */
139
 
140
/* Currently selected scan chain - just to prevent unnecessary
141
   transfers. */
142
static int current_chain;
143
 
144
/* Designates whether we are in SELECT_DR state, otherwise in
145
   RUN TEST/IDLE */
146
static int select_dr = 0;
147
 
148
/* Crc of current read or written data.  */
149
static int crc_r, crc_w = 0;
150
 
151
/* Address of previous read */
152
static unsigned long prev_regno = 0;
153
 
154
/* Generates new crc, sending in new bit input_bit */
155
 
156
static int
157
crc_calc (int crc, int input_bit)
158
{
159
  int c;
160
  int new_crc;
161
  int d;
162
 
163
#if (CRC_SIZE == 8)
164
  d = input_bit&1;
165
  c = crc;
166
 
167
  /* Move queue left.  */
168
  new_crc = crc << 1;
169
 
170
  /* Mask upper five bits.  */
171
  new_crc &= 0xF8;
172
 
173
  /* Set lower three bits */
174
  new_crc |= (d ^ ((c >> 7)&1));
175
  new_crc |= (d ^ ((c >> 0)&1) ^ ((c >> 7)&1)) << 1;
176
  new_crc |= (d ^ ((c >> 1)&1) ^ ((c >> 7)&1)) << 2;
177
  return new_crc;
178
#else
179
  return 0;
180
#endif
181
}
182
 
183
/* Send a byte to parallel port. */
184
 
185
inline static void
186
jp1_out (unsigned int value) {
187
 
188
#ifdef RTL_SIM
189
  time_t time;
190
  struct stat s;
191
  char buf[1000];
192
  FILE *fout;
193
  unsigned num_read;
194
  int r;
195
  fout = fopen (GDB_IN, "wt+");
196
  fprintf (fout, "F\n");
197
  fclose (fout);
198
  fout = fopen (GDB_OUT, "wt+");
199
  fprintf (fout, "%02X\n", value);
200
  fclose (fout);
201
error:
202
  fout = fopen (GDB_OUT, "rt");
203
  r = fscanf(fout,"%x", &num_read);
204
  fclose (fout);
205
  if (r == 0 || num_read != (0x10 | value))
206
    goto error;
207
#else
208
  outb(value, LPT_WRITE);
209
#endif /* RTL_SIM */
210
  if  (!(value & 1))
211
    debug("[%x%c]", (value & TDI_BIT) != 0, (value & TMS_BIT)?'^':'_');
212
  flush_debug();
213
}
214
 
215
/* Receive a byte from parallel port.  */
216
 
217
inline static unsigned char
218
jp1_in () {
219
  int data;
220
#ifndef RTL_SIM
221
  data = inb (LPT_READ);
222
  data = (data & TDO_BIT) == TDO_BIT;
223
#else
224
  FILE *fin = 0;
225
  char ch;
226
  time_t time;
227
  struct stat s;
228
  while (1) {
229
    fin = fopen (GDB_IN, "rt");
230
    if (fin == 0)
231
      continue;
232
    ch = fgetc (fin);
233
    if (ch != '0' && ch != '1') {
234
      fclose (fin);
235
      continue;
236
    } else break;
237
  }
238
  fclose (fin);
239
  data = ch == '1';
240
#endif /* !RTL_SIM */
241
  debug(" R%01X ", data);
242
  flush_debug();
243
  return data;
244
}
245
 
246
/* Writes TCLK=0, TRST=1, TMS=bit1, TDI=bit0
247
   and    TCLK=1, TRST=1, TMS=bit1, TDI=bit0 */
248
 
249
static inline void
250
jp1_write_JTAG (packet)
251
  unsigned char packet;
252
{
253
  unsigned char data = TRST_BIT;
254
  if (packet & 1)
255
    data |= TDI_BIT;
256
  if (packet & 2)
257
    data |= TMS_BIT;
258
 
259
  jp1_out (data);
260
  JTAG_WAIT();
261
  crc_w = crc_calc (crc_w, packet&1);
262
 
263
  /* rise clock */
264
  jp1_out (data | TCLK_BIT);
265
  JTAG_WAIT();
266
}
267
 
268
/* Reads TDI.  */
269
 
270
static inline int
271
jp1_read_JTAG ()
272
{
273
  int data;
274
  data = jp1_in ();
275
  crc_r = crc_calc (crc_r, data);
276
  return data;
277
}
278
 
279
/* Writes bitstream.  LS bit first.  */
280
 
281
static inline void
282
jp1_write_stream (stream, len, set_last_bit)
283
  ULONGEST stream;
284
  int len;
285
  int set_last_bit;
286
{
287
  int i;
288
  if (len <= 0) return;
289
  debug("\nwrite(");
290
  for (i = 0; i < len - 1; i++)
291
    jp1_write_JTAG ((stream >> i) & 1);
292
 
293
  if (set_last_bit)
294
    jp1_write_JTAG ((stream >> (len - 1))& 1 | TMS);
295
  else
296
    jp1_write_JTAG ((stream >> (len - 1))& 1);
297
  debug(")\n");
298
}
299
 
300
/* Gets bitstream.  LS bit first.  */
301
 
302
inline static ULONGEST
303
jp1_read_stream (stream, len, set_last_bit)
304
  unsigned long stream;
305
  int len;
306
  int set_last_bit;
307
{
308
  int i;
309
  ULONGEST data;
310
  debug("\nread(");
311
  if (len <= 0) return;
312
  data = 0;
313
  for (i = 0; i < len - 1; i++)
314
    {
315
      jp1_write_JTAG (stream & 1);   /* LSB first */
316
      stream >>= 1;
317
      data |= jp1_read_JTAG () << i; /* LSB first */
318
    }
319
 
320
  if (set_last_bit)
321
    jp1_write_JTAG (stream & 1 | TMS);
322
  else
323
    jp1_write_JTAG (stream & 1);
324
  data |= jp1_read_JTAG () << (len - 1);
325
  debug(")\n");
326
  return data;
327
}
328
 
329
/* Goes into SELECT_IR state. Should be called before every control write.  */
330
 
331
inline static void
332
jp1_prepare_control ()
333
{
334
  if (!select_dr)
335
    jp1_write_JTAG (TMS); /* SELECT_DR SCAN */
336
  jp1_write_JTAG (TMS);   /* SELECT_IR SCAN */
337
  select_dr = 0;
338
}
339
 
340
/* Resets JTAG.
341
   Writes TRST=0
342
   and    TRST=1 */
343
 
344
static void
345
jp1_reset_JTAG ()
346
{
347
  int i;
348
  debug2 ("\nreset(");
349
  jp1_out (0);
350
  JTAG_RETRY_WAIT();
351
  jp1_out (TRST_BIT);
352
  JTAG_RETRY_WAIT();
353
  jp1_write_JTAG (0);
354
  debug2(")\n");
355
  select_dr = 0;
356
}
357
 
358
/* Sets register/memory regno to data.  */
359
 
360
/* CZ 08/06/01: I am not sure how error checking is intended to
361
   be implemented here. It appears that no indication is returned
362
   to the caller as you have in standard unix system calls. Therefore,
363
   I guess the only way to use these functions when you want to know
364
   the exact position of the error is to manually clear err, call the
365
   function, and then manually check err. I have also made some changes
366
   where necessary because no value was returned at all int jtag_read_reg.
367
*/
368
 
369
static void
370
jtag_write_reg_support (regno, data)
371
  int regno;
372
  ULONGEST data;
373
{
374
  int crc_read, crc_write, crc_ok, retry;
375
  int result;
376
  int tmp;
377
 
378
  debug("\n");
379
  debug2("write_reg %i(%08x) <- %08x \n", regno, regno, data);
380
  if (!select_dr)
381
    jp1_write_JTAG (TMS); /* SELECT_DR SCAN */
382
  select_dr = 1;
383
 
384
      /* If we don't have rw bit, we assume chain
385
   is read only. */
386
  if (!chain_has_rw[current_chain])
387
    error ("Internal: Chain not writable.");
388
 
389
  for (retry = 0; retry < NUM_RETRIES; retry++) {
390
    jp1_write_JTAG (0); /* CAPTURE_DR */
391
    jp1_write_JTAG (0); /* SHIFT_DR */
392
    crc_w = 0;
393
 
394
    /* write addr */
395
    jp1_write_stream (regno, chain_addr_size[current_chain], 0);
396
 
397
    /* write (R/W=1) - we tested that previously. */
398
    jp1_write_JTAG (TDI);
399
 
400
    if (chain_has_crc[current_chain])
401
      {
402
        /* write data */
403
        jp1_write_stream (data, chain_data_size[current_chain], 0);
404
        crc_write = crc_w;
405
 
406
        /* write CRC, EXIT1_DR */
407
        crc_read = jp1_read_stream (crc_write, CRC_SIZE + 1, 1) >> 1;
408
      }
409
    else
410
      {
411
        /* write data */
412
        jp1_write_stream (data, chain_data_size[current_chain], 1);
413
      }
414
    jp1_write_JTAG (TMS); /* UPDATE_DR */
415
    jp1_write_JTAG (TMS); /* SELECT_DR */
416
 
417
    /* Did JTAG receive packet correctly? */
418
    if (chain_has_crc[current_chain])
419
      crc_ok = crc_read == crc_write;
420
 
421
    if (chain_has_crc[current_chain])
422
      {
423
        if (crc_ok)
424
          return;
425
 
426
        debug2(", crc failed. read %08x, generated %08x\n", crc_read, crc_write);
427
        jp1_reset_JTAG();
428
        jp1_write_JTAG (TMS); /* SELECT_DR SCAN */
429
        select_dr = 1;
430
        tmp = current_chain;
431
        current_chain = -1;
432
        jtag_set_chain(tmp);
433
      }
434
    else
435
      return;
436
  }
437
  printf ("Invalid CRC\n");
438
  err = ERR_CRC;
439
}
440
 
441
/* Reads register/memory from regno.
442
   Reading is a bit strange. Data is not available
443
   at the time we pass an address, but in successive
444
   read instead. Call jtag_read_reg twice to get correct
445
   data. */
446
 
447
static ULONGEST
448
jtag_read_reg (regno)
449
  unsigned int regno;
450
{
451
  ULONGEST data;
452
  int crc_read, crc_write, crc_actual_read,  retry, crc_ok;
453
  int result;
454
  int tmp;
455
 
456
  debug("\n");
457
  debug2("read_reg %i(%08x)", regno, regno);
458
  debug (" \n ");
459
  if (!select_dr)
460
    jp1_write_JTAG (TMS); /* SELECT_DR SCAN */
461
  select_dr = 1;
462
 
463
  for (retry = 0; retry < NUM_RETRIES; retry++) {
464
    jp1_write_JTAG (0); /* CAPTURE_DR */
465
    jp1_write_JTAG (0); /* SHIFT_DR */
466
    crc_w = 0;
467
 
468
    /* write addr */
469
    jp1_write_stream (regno, chain_addr_size[current_chain], 0);
470
 
471
    /* read (R/W=0) */
472
    if (chain_has_rw[current_chain])
473
      jp1_write_JTAG (0);
474
    if (chain_has_crc[current_chain])
475
      {
476
        crc_r = 0;
477
 
478
        /* data = 0 */
479
        data = jp1_read_stream (0, chain_data_size[current_chain], 0);
480
        crc_write = crc_w;
481
        crc_actual_read = crc_r;
482
 
483
        /* Send my crc, EXIT1_DR */
484
        crc_read = jp1_read_stream (crc_write, CRC_SIZE, 1);
485
      } else {
486
        /* data = 0 */
487
        data = jp1_read_stream (0, chain_data_size[current_chain], 1);
488
      }
489
    jp1_write_JTAG (TMS); /* UPDATE_DR */
490
    jp1_write_JTAG (TMS); /* SELECT_DR */
491
 
492
    /* Did JTAG receive packet correctly? */
493
    if (chain_has_crc[current_chain])
494
      crc_ok = jp1_read_JTAG ();
495
 
496
    if (chain_has_crc[current_chain])
497
      {
498
        if ((crc_read == crc_actual_read) && (crc_ok)) {
499
          debug2(" , read_reg %i(%08x) = %08x\n", regno, regno, data);
500
          prev_regno = regno;
501
          return data;
502
        }
503
        debug2(", crc failed. read %08x, generated %08x\n", crc_read, crc_actual_read);
504
        jp1_reset_JTAG();
505
        jp1_write_JTAG (TMS); /* SELECT_DR SCAN */
506
        select_dr = 1;
507
        tmp = current_chain;
508
        current_chain = -1;
509
        jtag_set_chain(tmp);
510
        jtag_read_reg (prev_regno);
511
        if (err) return -1;
512
      }
513
    else {
514
      debug2(" , read_reg %i(%08x) = %08x\n", regno, regno, data);
515
      prev_regno = regno;
516
      return data;
517
    }
518
  }
519
  printf ("Invalid CRC\n");
520
  err = ERR_CRC;
521
  return -1;
522
}
523
 
524
/* Sets scan chain.  */
525
 
526
static void
527
jtag_set_chain (chain)
528
  int chain;
529
{
530
  int crc_read, crc_write, crc_ok, retry;
531
  int result;
532
 
533
  debug("\n");
534
  debug2("set_chain %i\n", chain);
535
  if (current_chain != chain) {
536
    if (!chain_is_valid[chain])
537
      error ("Chain not valid.");
538
 
539
    current_chain = chain;
540
    jp1_prepare_control ();
541
 
542
    while (1) {
543
      jp1_write_JTAG (0); /* CAPTURE_IR */
544
      jp1_write_JTAG (0); /* SHIFT_IR */
545
 
546
      /* write data, EXIT1_IR */
547
      jp1_write_stream (JI_CHAIN_SELECT, JI_SIZE, 1);
548
 
549
      jp1_write_JTAG (TMS); /* UPDATE_IR */
550
      jp1_write_JTAG (TMS); /* SELECT_DR */
551
 
552
      jp1_write_JTAG (0); /* CAPTURE_DR */
553
      jp1_write_JTAG (0); /* SHIFT_DR */
554
 
555
      if (chain_has_crc[current_chain])
556
        {
557
          crc_w = 0;
558
          /* write data */
559
          jp1_write_stream (chain, SC_SIZE, 0);
560
 
561
          crc_write = crc_w;
562
          /* write CRC, EXIT1_DR */
563
          crc_read = jp1_read_stream (crc_write, CRC_SIZE + 1, 1) >> 1;
564
        } else {
565
          /* write data, EXIT1_DR */
566
          jp1_write_stream (chain, SC_SIZE, 1);
567
        }
568
      jp1_write_JTAG (TMS); /* UPDATE_DR */
569
      jp1_write_JTAG (TMS); /* SELECT_DR */
570
 
571
      /* Did JTAG receive packet correctly? */
572
      if (chain_has_crc[current_chain])
573
        crc_ok = crc_read == crc_write;
574
 
575
      if (chain_has_crc[current_chain])
576
        {
577
          if (!crc_ok)
578
            {
579
              debug2(", crc failed.\n");
580
              jp1_reset_JTAG();
581
              jp1_prepare_control ();
582
              continue;
583
            }
584
        }
585
 
586
      jp1_write_JTAG (TMS); /* SELECT_IR */
587
      jp1_write_JTAG (0); /* CAPTURE_IR */
588
      jp1_write_JTAG (0); /* SHIFT_IR */
589
      crc_w = 0;
590
 
591
      /* write data, EXIT1_IR */
592
      jp1_write_stream (JI_DEBUG, JI_SIZE, 1);
593
 
594
      jp1_write_JTAG (TMS); /* UPDATE_IR */
595
      jp1_write_JTAG (TMS); /* SELECT_DR */
596
      select_dr = 1;
597
 
598
      return;
599
    }
600
    printf ("Invalid CRC\n");
601
    err = ERR_CRC;
602
  } else
603
    debug2 ("Already set.\n");
604
}
605
 
606
/* Sets register/memory regno to data.  */
607
 
608
static void
609
jtag_write_reg (regno, data)
610
  int regno;
611
  ULONGEST data;
612
{
613
  /* Set PC */
614
  if (current_chain == SC_RISC_DEBUG && regno == 0x10)
615
    data = data - 4;
616
 
617
  jtag_write_reg_support (regno, data);
618
}
619
 
620
/* Stalls the CPU.  */
621
 
622
static void
623
or1k_stall ()
624
{
625
  int val;
626
  jtag_set_chain (SC_REGISTER);
627
  val = jtag_read_reg (JTAG_RISCOP);
628
  jtag_write_reg (JTAG_RISCOP, val | 1);
629
}
630
 
631
/* Unstalls the CPU.  */
632
 
633
static void
634
or1k_unstall ()
635
{
636
  unsigned int val;
637
 
638
  jtag_set_chain (SC_REGISTER);
639
  val = jtag_read_reg (JTAG_RISCOP);
640
  jtag_write_reg (JTAG_RISCOP, val & ~1);
641
}
642
 
643
/* Initialize a new connection to the or1k board, and make sure we are
644
   really connected.  */
645
 
646
static int
647
jtag_init () {
648
  int tmp, i;
649
  unsigned int npc, ppc, r1, insn, result;
650
  current_chain = -1;
651
  jp1_reset_JTAG ();
652
 
653
#if 1
654
 
655
#define RAM_BASE 0x40000000
656
  /* Stall risc */
657
  jtag_set_chain (SC_REGISTER);
658
  jtag_write_reg (4, 0x00000001);
659
 
660
  jtag_set_chain (SC_WISHBONE);
661
  jtag_write_reg (RAM_BASE + 0x00, 0x9c200000);                                /* l.addi  r1,r0,0x0       */
662
  jtag_write_reg (RAM_BASE + 0x04, 0x18400000 + (RAM_BASE >> 16));             /* l.movhi r2,0x4000       */
663
  jtag_write_reg (RAM_BASE + 0x08, 0xa8420000 + ((RAM_BASE + 0x30) & 0xffff)); /* l.ori   r2,r2,0x0000    */
664
  jtag_write_reg (RAM_BASE + 0x0c, 0x9c210001);                                /* l.addi  r1,r1,1         */
665
  jtag_write_reg (RAM_BASE + 0x10, 0x9c210001);                                /* l.addi  r1,r1,1         */
666
  jtag_write_reg (RAM_BASE + 0x14, 0xd4020800);                                /* l.sw    0(r2),r1        */
667
  jtag_write_reg (RAM_BASE + 0x18, 0x9c210001);                                /* l.addi  r1,r1,1         */
668
  jtag_write_reg (RAM_BASE + 0x1c, 0x84620000);                                /* l.lwz   r3,0(r2)        */
669
  jtag_write_reg (RAM_BASE + 0x20, 0x03fffffb);                                /* l.j     loop2           */
670
  jtag_write_reg (RAM_BASE + 0x24, 0xe0211800);                                /* l.add   r1,r1,r3        */
671
  jtag_write_reg (RAM_BASE + 0x24, 0xe0211800);                                /* l.add   r1,r1,r3        */
672
 
673
  /* Enable exceptions */
674
  jtag_set_chain (SC_RISC_DEBUG);
675
  jtag_write_reg ((0 << 11) + 17, 0x03);
676
 
677
  /* Trap causes stall */
678
  jtag_set_chain (SC_RISC_DEBUG);
679
  jtag_write_reg ((6 << 11) + 20, 0x2000);
680
 
681
  /* Set PC */
682
  jtag_set_chain (SC_RISC_DEBUG);
683
  jtag_write_reg ((0 << 11) + 16, RAM_BASE);
684
 
685
  /* Set step bit */
686
  jtag_set_chain (SC_RISC_DEBUG);
687
  jtag_write_reg ((6 << 11) + 16, 1 << 22);
688
 
689
 
690
  for (i = 0; i < 10; i++)
691
    {
692
      /* Unstall */
693
      jtag_set_chain (SC_REGISTER);
694
      jtag_write_reg (4, 0x00000000);
695
      jtag_set_chain (SC_RISC_DEBUG);
696
    }
697
 
698
  /* Read NPC */
699
  jtag_set_chain (SC_RISC_DEBUG);
700
  npc = jtag_read_reg ((0 << 11) + 16);
701
  npc = jtag_read_reg ((0 << 11) + 16);
702
 
703
  /* Read PPC */
704
  jtag_set_chain (SC_RISC_DEBUG);
705
  ppc = jtag_read_reg ((0 << 11) + 18);
706
  ppc = jtag_read_reg ((0 << 11) + 18);
707
 
708
  /* Read R1 */
709
  jtag_set_chain (SC_RISC_DEBUG);
710
  r1 = jtag_read_reg (0x401);
711
  r1 = jtag_read_reg (0x401);
712
 
713
  printf("npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
714
  result = npc + ppc + r1;
715
 
716
 
717
  /* Reset step bit */
718
  jtag_set_chain (SC_RISC_DEBUG);
719
  jtag_write_reg ((6 << 11) + 16, 0);
720
 
721
 
722
 
723
  /* Set trap insn in delay slot */
724
  jtag_set_chain (SC_WISHBONE);
725
  insn = jtag_read_reg (RAM_BASE + 0x24);
726
  insn = jtag_read_reg (RAM_BASE + 0x24);
727
  jtag_write_reg (RAM_BASE + 0x24, 0x21000001);
728
 
729
  /* Unstall */
730
  jtag_set_chain (SC_REGISTER);
731
  jtag_write_reg (4, 0x00000000);
732
  jtag_set_chain (SC_RISC_DEBUG);
733
 
734
  /* Read NPC */
735
  jtag_set_chain (SC_RISC_DEBUG);
736
  npc = jtag_read_reg ((0 << 11) + 16);
737
  npc = jtag_read_reg ((0 << 11) + 16);
738
 
739
  /* Read PPC */
740
  jtag_set_chain (SC_RISC_DEBUG);
741
  ppc = jtag_read_reg ((0 << 11) + 18);
742
  ppc = jtag_read_reg ((0 << 11) + 18);
743
 
744
  /* Read R1 */
745
  jtag_set_chain (SC_RISC_DEBUG);
746
  r1 = jtag_read_reg (0x401);
747
  r1 = jtag_read_reg (0x401);
748
 
749
  /* Set back original insn */
750
  jtag_set_chain (SC_WISHBONE);
751
  jtag_write_reg (RAM_BASE + 0x24, insn);
752
 
753
  printf("npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
754
  result = npc + ppc + r1 + result;
755
 
756
 
757
  /* Set trap insn in place of branch insn */
758
  jtag_set_chain (SC_WISHBONE);
759
  insn = jtag_read_reg (RAM_BASE + 0x20);
760
  insn = jtag_read_reg (RAM_BASE + 0x20);
761
  jtag_write_reg (RAM_BASE + 0x20, 0x21000001);
762
 
763
  /* Set PC */
764
  jtag_set_chain (SC_RISC_DEBUG);
765
  jtag_write_reg ((0 << 11) + 16, RAM_BASE + 0x0c);
766
 
767
  /* Unstall */
768
  jtag_set_chain (SC_REGISTER);
769
  jtag_write_reg (4, 0x00000000);
770
  jtag_set_chain (SC_RISC_DEBUG);
771
 
772
  /* Read NPC */
773
  jtag_set_chain (SC_RISC_DEBUG);
774
  npc = jtag_read_reg ((0 << 11) + 16);
775
  npc = jtag_read_reg ((0 << 11) + 16);
776
 
777
  /* Read PPC */
778
  jtag_set_chain (SC_RISC_DEBUG);
779
  ppc = jtag_read_reg ((0 << 11) + 18);
780
  ppc = jtag_read_reg ((0 << 11) + 18);
781
 
782
  /* Read R1 */
783
  jtag_set_chain (SC_RISC_DEBUG);
784
  r1 = jtag_read_reg (0x401);
785
  r1 = jtag_read_reg (0x401);
786
 
787
  /* Set back original insn */
788
  jtag_set_chain (SC_WISHBONE);
789
  jtag_write_reg (RAM_BASE + 0x20, insn);
790
 
791
  printf("npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
792
  result = npc + ppc + r1 + result;
793
 
794
 
795
  /* Set trap insn before branch insn */
796
  jtag_set_chain (SC_WISHBONE);
797
  insn = jtag_read_reg (RAM_BASE + 0x1c);
798
  insn = jtag_read_reg (RAM_BASE + 0x1c);
799
  jtag_write_reg (RAM_BASE + 0x1c, 0x21000001);
800
 
801
  /* Set PC */
802
  jtag_set_chain (SC_RISC_DEBUG);
803
  jtag_write_reg ((0 << 11) + 16, RAM_BASE + 0x20);
804
 
805
  /* Unstall */
806
  jtag_set_chain (SC_REGISTER);
807
  jtag_write_reg (4, 0x00000000);
808
  jtag_set_chain (SC_RISC_DEBUG);
809
 
810
  /* Read NPC */
811
  jtag_set_chain (SC_RISC_DEBUG);
812
  npc = jtag_read_reg ((0 << 11) + 16);
813
  npc = jtag_read_reg ((0 << 11) + 16);
814
 
815
  /* Read PPC */
816
  jtag_set_chain (SC_RISC_DEBUG);
817
  ppc = jtag_read_reg ((0 << 11) + 18);
818
  ppc = jtag_read_reg ((0 << 11) + 18);
819
 
820
  /* Read R1 */
821
  jtag_set_chain (SC_RISC_DEBUG);
822
  r1 = jtag_read_reg (0x401);
823
  r1 = jtag_read_reg (0x401);
824
 
825
  /* Set back original insn */
826
  jtag_set_chain (SC_WISHBONE);
827
  jtag_write_reg (RAM_BASE + 0x1c, insn);
828
 
829
  printf("npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
830
  result = npc + ppc + r1 + result;
831
 
832
 
833
  /* Set trap insn behind lsu insn */
834
  jtag_set_chain (SC_WISHBONE);
835
  insn = jtag_read_reg (RAM_BASE + 0x18);
836
  insn = jtag_read_reg (RAM_BASE + 0x18);
837
  jtag_write_reg (RAM_BASE + 0x18, 0x21000001);
838
 
839
  /* Set PC */
840
  jtag_set_chain (SC_RISC_DEBUG);
841
  jtag_write_reg ((0 << 11) + 16, RAM_BASE + 0x1c);
842
 
843
  /* Unstall */
844
  jtag_set_chain (SC_REGISTER);
845
  jtag_write_reg (4, 0x00000000);
846
  jtag_set_chain (SC_RISC_DEBUG);
847
 
848
  /* Read NPC */
849
  jtag_set_chain (SC_RISC_DEBUG);
850
  npc = jtag_read_reg ((0 << 11) + 16);
851
  npc = jtag_read_reg ((0 << 11) + 16);
852
 
853
  /* Read PPC */
854
  jtag_set_chain (SC_RISC_DEBUG);
855
  ppc = jtag_read_reg ((0 << 11) + 18);
856
  ppc = jtag_read_reg ((0 << 11) + 18);
857
 
858
  /* Read R1 */
859
  jtag_set_chain (SC_RISC_DEBUG);
860
  r1 = jtag_read_reg (0x401);
861
  r1 = jtag_read_reg (0x401);
862
 
863
  /* Set back original insn */
864
  jtag_set_chain (SC_WISHBONE);
865
  jtag_write_reg (RAM_BASE + 0x18, insn);
866
 
867
  printf("npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
868
  result = npc + ppc + r1 + result;
869
 
870
  /* Set trap insn very near previous one */
871
  jtag_set_chain (SC_WISHBONE);
872
  insn = jtag_read_reg (RAM_BASE + 0x1c);
873
  insn = jtag_read_reg (RAM_BASE + 0x1c);
874
  jtag_write_reg (RAM_BASE + 0x1c, 0x21000001);
875
 
876
  /* Set PC */
877
  jtag_set_chain (SC_RISC_DEBUG);
878
  jtag_write_reg ((0 << 11) + 16, RAM_BASE + 0x18);
879
 
880
  /* Unstall */
881
  jtag_set_chain (SC_REGISTER);
882
  jtag_write_reg (4, 0x00000000);
883
  jtag_set_chain (SC_RISC_DEBUG);
884
 
885
  /* Read NPC */
886
  jtag_set_chain (SC_RISC_DEBUG);
887
  npc = jtag_read_reg ((0 << 11) + 16);
888
  npc = jtag_read_reg ((0 << 11) + 16);
889
 
890
  /* Read PPC */
891
  jtag_set_chain (SC_RISC_DEBUG);
892
  ppc = jtag_read_reg ((0 << 11) + 18);
893
  ppc = jtag_read_reg ((0 << 11) + 18);
894
 
895
  /* Read R1 */
896
  jtag_set_chain (SC_RISC_DEBUG);
897
  r1 = jtag_read_reg (0x401);
898
  r1 = jtag_read_reg (0x401);
899
 
900
  /* Set back original insn */
901
  jtag_set_chain (SC_WISHBONE);
902
  jtag_write_reg (RAM_BASE + 0x1c, insn);
903
 
904
  printf("npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
905
  result = npc + ppc + r1 + result;
906
 
907
 
908
  /* Set trap insn to the start */
909
  jtag_set_chain (SC_WISHBONE);
910
  insn = jtag_read_reg (RAM_BASE + 0x0c);
911
  insn = jtag_read_reg (RAM_BASE + 0x0c);
912
  jtag_write_reg (RAM_BASE + 0x0c, 0x21000001);
913
 
914
  /* Set PC */
915
  jtag_set_chain (SC_RISC_DEBUG);
916
  jtag_write_reg ((0 << 11) + 16, RAM_BASE + 0x1c);
917
 
918
  /* Unstall */
919
  jtag_set_chain (SC_REGISTER);
920
  jtag_write_reg (4, 0x00000000);
921
  jtag_set_chain (SC_RISC_DEBUG);
922
 
923
  /* Read NPC */
924
  jtag_set_chain (SC_RISC_DEBUG);
925
  npc = jtag_read_reg ((0 << 11) + 16);
926
  npc = jtag_read_reg ((0 << 11) + 16);
927
 
928
  /* Read PPC */
929
  jtag_set_chain (SC_RISC_DEBUG);
930
  ppc = jtag_read_reg ((0 << 11) + 18);
931
  ppc = jtag_read_reg ((0 << 11) + 18);
932
 
933
  /* Read R1 */
934
  jtag_set_chain (SC_RISC_DEBUG);
935
  r1 = jtag_read_reg (0x401);
936
  r1 = jtag_read_reg (0x401);
937
 
938
  /* Set back original insn */
939
  jtag_set_chain (SC_WISHBONE);
940
  jtag_write_reg (RAM_BASE + 0x0c, insn);
941
 
942
  printf("npc = %.8lx ppc = %.8lx r1 = %.8lx\n", npc, ppc, r1);
943
  result = npc + ppc + r1 + result;
944
 
945
  printf("result = %.8lx\n", result + 0x5eaddc4b);
946
 
947
#endif
948
 
949
  return err;
950
}
951
 
952
main(argc, argv)
953
  int argc;
954
  char *argv[];
955
{
956
  char *redirstr;
957
  int trace_fd = 0;
958
  char *s;
959
 
960
  srand(getpid());
961
  if (argc != 2) {
962
    printf("JTAG protocol via parallel port for linux.\n");
963
    printf("Copyright (C) 2001 Marko Mlinar, markom@opencores.org\n\n");
964
    printf("Usage: %s JTAG port_number\n", argv[0]);
965
    return -1;
966
  }
967
 
968
#ifndef RTL_SIM
969
  if (ioperm(LPT_BASE, 3, 1)) {
970
    fprintf(stderr, "Couldn't get the port at %x\n", LPT_BASE);
971
    perror("Root privileges are required.\n");
972
    return -1;
973
  }
974
  printf("Connected to parallel port at %x\n", LPT_BASE);
975
#else
976
  {
977
    FILE *fin = fopen (GDB_IN, "wt+");
978
    if(fin == 0) {
979
      fprintf(stderr, "Can not open %s\n", GDB_IN);
980
      exit(1);
981
    }
982
    fclose(fin);
983
 
984
  }
985
#endif
986
#ifndef RTL_SIM
987
  /* Get rid of root privileges.  */
988
  setreuid(getuid(), getuid());
989
#endif
990
 
991
  /* Test the connection.  */
992
  if (jtag_init()) {
993
    fprintf(stderr,"Connection with jtag via parallel port failed.\n");
994
    exit(-1);
995
  }
996
 
997
  /* We have a connection.  Establish server.  */
998
  argv++; argc--;
999
  printf ("Dropping root privileges.\n");
1000
  serverPort = strtol(*(argv),&s,10);
1001
  if(*s)
1002
    return -1;
1003
  argv++; argc--;
1004
 
1005
  if(server_fd = GetServerSocket("or1ksim","tcp", serverPort)) {
1006
    printf("JTAG Proxy server started on port %d\n", serverPort);
1007
    printf("Press CTRL+c to exit.\n");
1008
  } else {
1009
    fprintf(stderr,"Cannot start JTAG Proxy server on port %d\n", serverPort);
1010
    exit(-1);
1011
  }
1012
 
1013
  /* Do endless loop of checking.  */
1014
  while(1) {
1015
    /* Handle GDB requests.  Ctrl-c exits.  */
1016
    HandleServerSocket(true);
1017
    /* Do some waiting to reduce load on CPU and should not be too long reply time also.  */
1018
    usleep(10);
1019
  }
1020
}
1021
 
1022
/************************
1023
   JTAG Server Routines
1024
 ************************/
1025
 
1026
static int tcp_level = 0;
1027
 
1028
/* Added by CZ 24/05/01 */
1029
int GetServerSocket(const char* name,const char* proto,int port)
1030
{
1031
  struct servent *service;
1032
  struct protoent *protocol;
1033
  struct sockaddr_in sa;
1034
  struct hostent *hp;
1035
  int sockfd;
1036
  char myname[256];
1037
  int flags;
1038
  char sTemp[256];
1039
 
1040
  /* First, get the protocol number of TCP */
1041
  if(!(protocol = getprotobyname(proto)))
1042
    {
1043
      sprintf(sTemp,"Unable to load protocol \"%s\"",proto);
1044
      perror(sTemp);
1045
      return 0;
1046
    }
1047
  tcp_level = protocol->p_proto; /* Save for later */
1048
 
1049
  /* If we weren't passed a non standard port, get the port
1050
     from the services directory. */
1051
  if(!port)
1052
    {
1053
      if(service = getservbyname(name,protocol->p_name))
1054
  port = ntohs(service->s_port);
1055
    }
1056
 
1057
  /* Create the socket using the TCP protocol */
1058
  if((sockfd = socket(PF_INET,SOCK_STREAM,protocol->p_proto)) < 0)
1059
    {
1060
      perror("Unable to create socket");
1061
      return 0;
1062
    }
1063
 
1064
  flags = 1;
1065
  if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(const char*)&flags,sizeof(int))
1066
 < 0)
1067
    {
1068
      sprintf(sTemp,"Can not set SO_REUSEADDR option on socket %d",sockfd);
1069
      perror(sTemp);
1070
      close(sockfd);
1071
      return 0;
1072
    }
1073
 
1074
  /* The server should also be non blocking. Get the current flags. */
1075
  if(fcntl(sockfd,F_GETFL,&flags) < 0)
1076
    {
1077
      sprintf(sTemp,"Unable to get flags for socket %d",sockfd);
1078
      perror(sTemp);
1079
      close(sockfd);
1080
      return 0;
1081
    }
1082
 
1083
  /* Set the nonblocking flag */
1084
  if(fcntl(sockfd,F_SETFL, flags | O_NONBLOCK) < 0)
1085
    {
1086
      sprintf(sTemp,"Unable to set flags for socket %d to value 0x%08x",
1087
        sockfd,flags | O_NONBLOCK);
1088
      perror(sTemp);
1089
      close(sockfd);
1090
      return 0;
1091
    }
1092
 
1093
  /* Find out what our address is */
1094
  memset(&sa,0,sizeof(struct sockaddr_in));
1095
  gethostname(myname,sizeof(myname));
1096
  if(!(hp = gethostbyname(myname)))
1097
    {
1098
      perror("Unable to read hostname");
1099
      close(sockfd);
1100
      return 0;
1101
    }
1102
 
1103
  /* Bind our socket to the appropriate address */
1104
  sa.sin_family = hp->h_addrtype;
1105
  sa.sin_port = htons(port);
1106
  if(bind(sockfd,(struct sockaddr*)&sa,sizeof(struct sockaddr_in)) < 0)
1107
    {
1108
      sprintf(sTemp,"Unable to bind socket %d to port %d",sockfd,port);
1109
      perror(sTemp);
1110
      close(sockfd);
1111
      return 0;
1112
    }
1113
  serverIP = sa.sin_addr.s_addr;
1114
  flags = sizeof(struct sockaddr_in);
1115
  if(getsockname(sockfd,(struct sockaddr*)&sa,&flags) < 0)
1116
    {
1117
      sprintf(sTemp,"Unable to get socket information for socket %d",sockfd);
1118
      perror(sTemp);
1119
      close(sockfd);
1120
      return 0;
1121
    }
1122
  serverPort = ntohs(sa.sin_port);
1123
 
1124
  /* Set the backlog to 1 connections */
1125
  if(listen(sockfd,1) < 0)
1126
    {
1127
      sprintf(sTemp,"Unable to set backlog on socket %d to %d",sockfd,1);
1128
      perror(sTemp);
1129
      close(sockfd);
1130
      return 0;
1131
    }
1132
 
1133
  return sockfd;
1134
}
1135
 
1136
void HandleServerSocket(Boolean block)
1137
{
1138
  struct pollfd fds[3];
1139
  int n = 0;
1140
  int timeout = block ? -1 : 0;
1141
  int server_index = -1;
1142
  int gdb_index = -1;
1143
  Boolean data_on_stdin = false;
1144
  int o_serv_fd = server_fd;
1145
 
1146
  if(!o_serv_fd && !gdb_fd)
1147
    return;
1148
 
1149
  if(o_serv_fd)
1150
    {
1151
      fds[n].fd = o_serv_fd;
1152
      fds[n].events = POLLIN;
1153
      fds[n++].revents = 0;
1154
    }
1155
  if(gdb_fd)
1156
    {
1157
      fds[n].fd = gdb_fd;
1158
      fds[n].events = POLLIN;
1159
      fds[n++].revents = 0;
1160
    }
1161
  if(block)
1162
    {
1163
      fds[n].fd = 0;
1164
      fds[n].events = POLLIN;
1165
      fds[n++].revents = 0;
1166
    }
1167
 
1168
  while(!data_on_stdin)
1169
    {
1170
      switch(poll(fds,n,timeout))
1171
        {
1172
          case -1:
1173
            if(errno == EINTR)
1174
              continue;
1175
            perror("poll");
1176
            server_fd = 0;
1177
            break;
1178
          case 0: /* Nothing interesting going on */
1179
            data_on_stdin = true; /* Can only get here if nonblocking */
1180
            break;
1181
          default:
1182
            /* Make sure to handle the gdb port first! */
1183
            if((fds[0].revents && (gdb_fd && !o_serv_fd) ||
1184
                fds[1].revents && (server_fd && gdb_fd)))
1185
              {
1186
                int revents = o_serv_fd ? fds[1].revents : fds[0].revents;
1187
 
1188
                if(revents & POLLIN)
1189
                   GDBRequest();
1190
                else /* Error Occurred */
1191
                  {
1192
                    fprintf(stderr,"Received flags 0x%08x on gdb socket. Shutting down.\n",revents);
1193
                    close(gdb_fd);
1194
                    gdb_fd = 0;
1195
                  }
1196
               }
1197
            if(fds[0].revents && o_serv_fd)
1198
              {
1199
                if(fds[0].revents & POLLIN)
1200
                  JTAGRequest();
1201
                else /* Error Occurred */
1202
                  {
1203
                    fprintf(stderr,"Received flags 0x%08x on server. Shutting down.\n",fds[0].revents);
1204
                    close(o_serv_fd);
1205
                    server_fd = 0;
1206
                    serverPort = 0;
1207
                    serverIP = 0;
1208
                  }
1209
              }
1210
            if(fds[2].revents || (fds[1].revents && !gdb_fd))
1211
              data_on_stdin = true;
1212
            break;
1213
        } /* End of switch statement */
1214
    } /* End of while statement */
1215
}
1216
 
1217
void JTAGRequest()
1218
{
1219
  struct sockaddr_in sa;
1220
  struct sockaddr* addr = (struct sockaddr*)&sa;
1221
  int n = sizeof(struct sockaddr_in);
1222
  int fd = accept(server_fd,addr,&n);
1223
  int on_off = 0; /* Turn off Nagel's algorithm on the socket */
1224
  int flags;
1225
  char sTemp[256];
1226
 
1227
  if(fd < 0)
1228
    {
1229
      /* This is valid, because a connection could have started,
1230
         and then terminated due to a protocol error or user
1231
         initiation before the accept could take place. */
1232
      if(errno != EWOULDBLOCK && errno != EAGAIN)
1233
        {
1234
          perror("accept");
1235
          close(server_fd);
1236
          server_fd = 0;
1237
          serverPort = 0;
1238
          serverIP = 0;
1239
        }
1240
      return;
1241
    }
1242
 
1243
  if(gdb_fd)
1244
    {
1245
      close(fd);
1246
      return;
1247
    }
1248
 
1249
  if(fcntl(fd,F_GETFL,&flags) < 0)
1250
    {
1251
      sprintf(sTemp,"Unable to get flags for gdb socket %d",fd);
1252
      perror(sTemp);
1253
      close(fd);
1254
      return;
1255
    }
1256
 
1257
  if(fcntl(fd,F_SETFL, flags | O_NONBLOCK) < 0)
1258
    {
1259
      sprintf(sTemp,"Unable to set flags for gdb socket %d to value 0x%08x",
1260
        fd,flags | O_NONBLOCK);
1261
      perror(sTemp);
1262
      close(fd);
1263
      return;
1264
    }
1265
 
1266
  if(setsockopt(fd,tcp_level,TCP_NODELAY,&on_off,sizeof(int)) < 0)
1267
    {
1268
      sprintf(sTemp,"Unable to disable Nagel's algorithm for socket %d.\nsetsockopt",fd);
1269
      perror(sTemp);
1270
      close(fd);
1271
      return;
1272
    }
1273
 
1274
  gdb_fd = fd;
1275
}
1276
 
1277
void GDBRequest()
1278
{
1279
  JTAGProxyWriteMessage msg_write;
1280
  JTAGProxyReadMessage msg_read;
1281
  JTAGProxyChainMessage msg_chain;
1282
  JTAGProxyWriteResponse resp_write;
1283
  JTAGProxyReadResponse resp_read;
1284
  JTAGProxyChainResponse resp_chain;
1285
  JTAGProxyBlockWriteMessage *msg_bwrite;
1286
  JTAGProxyBlockReadMessage msg_bread;
1287
  JTAGProxyBlockWriteResponse resp_bwrite;
1288
  JTAGProxyBlockReadResponse *resp_bread;
1289
  char *buf;
1290
  unsigned long long data;
1291
  uint32_t command,length;
1292
  int len,i;
1293
  err = 0;
1294
 
1295
  /* First, we must read the incomming command */
1296
  if(gdb_read(&command,sizeof(uint32_t)) < 0)
1297
    {
1298
      if(gdb_fd)
1299
        {
1300
          perror("gdb socket - 1");
1301
          close(gdb_fd);
1302
          gdb_fd = 0;
1303
        }
1304
      return;
1305
    }
1306
  if(gdb_read(&length,sizeof(uint32_t)) < 0)
1307
    {
1308
      if(gdb_fd)
1309
        {
1310
          perror("gdb socket - 2");
1311
          close(gdb_fd);
1312
          gdb_fd = 0;
1313
        }
1314
      return;
1315
    }
1316
  length = ntohl(length);
1317
 
1318
  /* Now, verify the protocol and implement the command */
1319
  switch(ntohl(command))
1320
    {
1321
    case JTAG_COMMAND_WRITE:
1322
      if(length != sizeof(msg_write) - 8)
1323
        {
1324
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1325
          return;
1326
        }
1327
      buf = (char*)&msg_write;
1328
      if(gdb_read(&buf[8],length) < 0)
1329
        {
1330
          if(gdb_fd)
1331
            {
1332
              perror("gdb socket - 3");
1333
              close(gdb_fd);
1334
              gdb_fd = 0;
1335
            }
1336
          return;
1337
        }
1338
      msg_write.address = ntohl(msg_write.address);
1339
      msg_write.data_H = ntohl(msg_write.data_H);
1340
      msg_write.data_L = ntohl(msg_write.data_L);
1341
      jtag_write_reg(msg_write.address,msg_write.data_L);
1342
      resp_write.status = htonl(err);
1343
      if(gdb_write(&resp_write,sizeof(resp_write)) < 0)
1344
        {
1345
          if(gdb_fd)
1346
            {
1347
              perror("gdb socket - 4");
1348
              close(gdb_fd);
1349
              gdb_fd = 0;
1350
            }
1351
          return;
1352
        }
1353
      break;
1354
    case JTAG_COMMAND_READ:
1355
      if(length != sizeof(msg_read) - 8)
1356
        {
1357
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1358
          return;
1359
        }
1360
      buf = (char*)&msg_read;
1361
      if(gdb_read(&buf[8],length) < 0)
1362
        {
1363
          if(gdb_fd)
1364
            {
1365
              perror("gdb socket - 5");
1366
              close(gdb_fd);
1367
              gdb_fd = 0;
1368
            }
1369
          return;
1370
        }
1371
      msg_read.address = ntohl(msg_read.address);
1372
      jtag_read_reg(msg_read.address); /* Data not ready at this time, repeat. */
1373
      resp_read.data_L = jtag_read_reg(msg_read.address);
1374
      resp_read.status = htonl(err);
1375
      resp_read.data_H = 0;
1376
      resp_read.data_L = htonl(resp_read.data_L);
1377
      if(gdb_write(&resp_read,sizeof(resp_read)) < 0)
1378
        {
1379
          if(gdb_fd)
1380
            {
1381
              perror("gdb socket - 6");
1382
              close(gdb_fd);
1383
              gdb_fd = 0;
1384
            }
1385
          return;
1386
        }
1387
      break;
1388
    case JTAG_COMMAND_BLOCK_WRITE:
1389
      if(length < sizeof(JTAGProxyBlockWriteMessage)-8)
1390
        {
1391
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1392
          return;
1393
        }
1394
      if(!(buf = (char*)malloc(8+length)))
1395
        {
1396
          ProtocolClean(length,JTAG_PROXY_OUT_OF_MEMORY);
1397
          return;
1398
        }
1399
      msg_bwrite = (JTAGProxyBlockWriteMessage*)buf;
1400
      if(gdb_read(&buf[8],length) < 0)
1401
        {
1402
          if(gdb_fd)
1403
            {
1404
              perror("gdb socket - 5");
1405
              close(gdb_fd);
1406
              gdb_fd = 0;
1407
            }
1408
          free(buf);
1409
          return;
1410
        }
1411
      msg_bwrite->address = ntohl(msg_bwrite->address);
1412
      msg_bwrite->nRegisters = ntohl(msg_bwrite->nRegisters);
1413
      for(i=0;i<msg_bwrite->nRegisters;i++)
1414
        {
1415
          msg_bwrite->data[i] = ntohl(msg_bwrite->data[i]);
1416
          jtag_write_reg(msg_bwrite->address + i * 4,msg_bwrite->data[i]);
1417
        }
1418
      resp_bwrite.status = htonl(err);
1419
      free(buf);
1420
      buf = msg_bwrite = NULL;
1421
      if(gdb_write(&resp_bwrite,sizeof(resp_bwrite)) < 0)
1422
        {
1423
          if(gdb_fd)
1424
            {
1425
              perror("gdb socket - 4");
1426
              close(gdb_fd);
1427
              gdb_fd = 0;
1428
            }
1429
          return;
1430
        }
1431
      break;
1432
    case JTAG_COMMAND_BLOCK_READ:
1433
      if(length != sizeof(msg_bread) - 8)
1434
        {
1435
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1436
          return;
1437
        }
1438
      buf = (char*)&msg_bread;
1439
      if(gdb_read(&buf[8],length) < 0)
1440
        {
1441
          if(gdb_fd)
1442
            {
1443
              perror("gdb socket - 5");
1444
              close(gdb_fd);
1445
              gdb_fd = 0;
1446
            }
1447
          return;
1448
        }
1449
      msg_bread.address = ntohl(msg_bread.address);
1450
      msg_bread.nRegisters = ntohl(msg_bread.nRegisters);
1451
      len = sizeof(JTAGProxyBlockReadResponse) + 4*(msg_bread.nRegisters-1);
1452
      if(!(buf = (char*)malloc(len)))
1453
        {
1454
          ProtocolClean(0,JTAG_PROXY_OUT_OF_MEMORY);
1455
          return;
1456
        }
1457
      resp_bread = (JTAGProxyBlockReadResponse*)buf;
1458
      jtag_read_reg(msg_bread.address); /* Prepare for reading. */
1459
      for(i=0;i<msg_bread.nRegisters;i++)
1460
        {
1461
          /* Read previous, address next one. */
1462
          resp_bread->data[i] = jtag_read_reg(msg_bread.address + (i + 1) * 4);
1463
          resp_bread->data[i] = htonl(resp_bread->data[i]);
1464
        }
1465
      resp_bread->status = htonl(err);
1466
      resp_bread->nRegisters = htonl(msg_bread.nRegisters);
1467
      if(gdb_write(resp_bread,len) < 0)
1468
        {
1469
          if(gdb_fd)
1470
            {
1471
              perror("gdb socket - 6");
1472
              close(gdb_fd);
1473
              gdb_fd = 0;
1474
            }
1475
          free(buf);
1476
          return;
1477
        }
1478
      free(buf);
1479
      buf = resp_bread = NULL;
1480
      break;
1481
    case JTAG_COMMAND_CHAIN:
1482
      if(length != sizeof(msg_chain) - 8)
1483
        {
1484
          ProtocolClean(length,JTAG_PROXY_PROTOCOL_ERROR);
1485
          return;
1486
        }
1487
      buf = (char*)&msg_chain;
1488
      if(gdb_read(&buf[8],sizeof(msg_chain)-8) < 0)
1489
        {
1490
          if(gdb_fd)
1491
            {
1492
              perror("gdb socket - 7");
1493
              close(gdb_fd);
1494
              gdb_fd = 0;
1495
            }
1496
          return;
1497
        }
1498
      msg_chain.chain = htonl(msg_chain.chain);
1499
      jtag_set_chain(msg_chain.chain);
1500
      resp_chain.status = htonl(err);
1501
      if(gdb_write(&resp_chain,sizeof(resp_chain)) < 0)
1502
        {
1503
          if(gdb_fd)
1504
            {
1505
              perror("gdb socket - 8");
1506
              close(gdb_fd);
1507
              gdb_fd = 0;
1508
            }
1509
          return;
1510
        }
1511
      break;
1512
    default:
1513
      perror("Unknown JTAG command.");
1514
      ProtocolClean(length,JTAG_PROXY_COMMAND_NOT_IMPLEMENTED);
1515
      break;
1516
    }
1517
}
1518
 
1519
void ProtocolClean(int length,int32_t err)
1520
{
1521
  char buf[4096];
1522
 
1523
  err = htonl(err);
1524
  if((gdb_read(buf,length) < 0) ||
1525
      (gdb_write(&err,sizeof(err)) < 0) && gdb_fd)
1526
    {
1527
      perror("gdb socket - 9");
1528
      close(gdb_fd);
1529
      gdb_fd = 0;
1530
    }
1531
}
1532
 
1533
static int gdb_write(void* buf,int len)
1534
{
1535
  int n;
1536
  char* w_buf = (char*)buf;
1537
  struct pollfd block;
1538
 
1539
  while(len)
1540
    {
1541
      if((n = write(gdb_fd,w_buf,len)) < 0)
1542
        {
1543
          switch(errno)
1544
            {
1545
            case EWOULDBLOCK: /* or EAGAIN */
1546
              /* We've been called on a descriptor marked
1547
           for nonblocking I/O. We better simulate
1548
           blocking behavior. */
1549
              block.fd = gdb_fd;
1550
              block.events = POLLOUT;
1551
              block.revents = 0;
1552
              poll(&block,1,-1);
1553
              continue;
1554
            case EINTR:
1555
              continue;
1556
            case EPIPE:
1557
              close(gdb_fd);
1558
              gdb_fd = 0;
1559
              return -1;
1560
            default:
1561
              return -1;
1562
            }
1563
        }
1564
      else
1565
        {
1566
          len -= n;
1567
          w_buf += n;
1568
        }
1569
    }
1570
  return 0;
1571
}
1572
 
1573
static int gdb_read(void* buf,int len)
1574
{
1575
  int n;
1576
  char* r_buf = (char*)buf;
1577
  struct pollfd block;
1578
 
1579
  while(len)
1580
    {
1581
      if((n = read(gdb_fd,r_buf,len)) < 0)
1582
        {
1583
          switch(errno)
1584
            {
1585
            case EWOULDBLOCK: /* or EAGAIN */
1586
              /* We've been called on a descriptor marked
1587
           for nonblocking I/O. We better simulate
1588
           blocking behavior. */
1589
              block.fd = gdb_fd;
1590
              block.events = POLLIN;
1591
              block.revents = 0;
1592
              poll(&block,1,-1);
1593
              continue;
1594
            case EINTR:
1595
              continue;
1596
            default:
1597
              return -1;
1598
            }
1599
        }
1600
      else if(n == 0)
1601
        {
1602
          close(gdb_fd);
1603
          gdb_fd = 0;
1604
          return -1;
1605
        }
1606
      else
1607
        {
1608
          len -= n;
1609
          r_buf += n;
1610
        }
1611
    }
1612
  return 0;
1613
}

powered by: WebSVN 2.1.0

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