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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [testsuite/] [test-code/] [lib-jtag/] [lib-jtag-full.c] - Blame information for rev 220

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 97 jeremybenn
/* lib-jtag-full.c. Comprehensive test of Or1ksim library JTAG interface.
2
 
3
   Copyright (C) 1999-2006 OpenCores
4
   Copyright (C) 2010 Embecosm Limited
5
 
6
   Contributors various OpenCores participants
7
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8
 
9
   This file is part of OpenRISC 1000 Architectural Simulator.
10
 
11
   This program is free software; you can redistribute it and/or modify it
12
   under the terms of the GNU General Public License as published by the Free
13
   Software Foundation; either version 3 of the License, or (at your option)
14
   any later version.
15
 
16
   This program is distributed in the hope that it will be useful, but WITHOUT
17
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19
   more details.
20
 
21
   You should have received a copy of the GNU General Public License along
22 104 jeremybenn
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23 97 jeremybenn
 
24
/* ----------------------------------------------------------------------------
25
   This code is commented throughout for use with Doxygen.
26
   --------------------------------------------------------------------------*/
27
 
28
#include <errno.h>
29
#include <stddef.h>
30
#include <stdlib.h>
31
#include <stdio.h>
32
#include <string.h>
33
 
34
#include "or1ksim.h"
35
 
36
 
37
/* --------------------------------------------------------------------------*/
38
/*!Compute a IEEE 802.3 CRC-32.
39
 
40
   Print an error message if we get a duff argument, but we really should
41
   not.
42
 
43
   @param[in] value     The value to shift into the CRC
44
   @param[in] num_bits  The number of bits in the value.
45
   @param[in] crc_in    The existing CRC
46
 
47
   @return  The computed CRC.                                                */
48
/* --------------------------------------------------------------------------*/
49 104 jeremybenn
static unsigned long int
50 97 jeremybenn
crc32 (unsigned long long int  value,
51
       int                     num_bits,
52
       unsigned long int       crc_in)
53
{
54
  if ((1 > num_bits) || (num_bits > 64))
55
    {
56
      printf ("ERROR: Max 64 bits of CRC can be computed. Ignored\n");
57
      return  crc_in;
58
    }
59
 
60
  static const unsigned long int  CRC32_POLY = 0x04c11db7;
61
  int                             i;
62
 
63 104 jeremybenn
  /* Compute the CRC, MS bit first */
64 97 jeremybenn
  for (i = num_bits - 1; i >= 0; i--)
65
    {
66
      unsigned long int  d;
67
      unsigned long int  t;
68
 
69
      d = (1 == ((value >> i) & 1))   ? 0xfffffff : 0x0000000;
70
      t = (1 == ((crc_in >> 31) & 1)) ? 0xfffffff : 0x0000000;
71
 
72
      crc_in <<= 1;
73
      crc_in  ^= (d ^ t) & CRC32_POLY;
74
    }
75
 
76
  return  crc_in;
77
 
78
}       /* crc32 () */
79
 
80
 
81
/* --------------------------------------------------------------------------*/
82
/*!Reverse a value's bits
83
 
84
   @param[in] val  The value to reverse (up to 64 bits).
85
   @param[in] len  The number of bits to reverse.
86
 
87
   @return  The reversed value                                               */
88
/* --------------------------------------------------------------------------*/
89
static unsigned long long
90
reverse_bits (unsigned long long  val,
91
              int                 len)
92
{
93
  if ((1 > len) || (len > 64))
94
    {
95
      printf ("ERROR: Cannot reverse %d bits. Returning zero\n", len);
96
      return  0;
97
    }
98
 
99
  /* Reverse the string */
100
  val = (((val & 0xaaaaaaaaaaaaaaaaULL) >>  1) |
101
         ((val & 0x5555555555555555ULL) <<  1));
102
  val = (((val & 0xccccccccccccccccULL) >>  2) |
103
         ((val & 0x3333333333333333ULL) <<  2));
104
  val = (((val & 0xf0f0f0f0f0f0f0f0ULL) >>  4) |
105
         ((val & 0x0f0f0f0f0f0f0f0fULL) <<  4));
106
  val = (((val & 0xff00ff00ff00ff00ULL) >>  8) |
107
         ((val & 0x00ff00ff00ff00ffULL) <<  8));
108
  val = (((val & 0xffff0000ffff0000ULL) >> 16) |
109
         ((val & 0x0000ffff0000ffffULL) << 16));
110
 
111
  return  ((val >> 32) | (val << 32)) >> (64 - len);
112
 
113
}       /* reverse_bits () */
114
 
115
 
116
/* --------------------------------------------------------------------------*/
117
/*!Dump a JTAG register
118
 
119
   Prefix with the supplied string and add a newline afterwards.
120
 
121
   @param[in] prefix     Prefix string to print out
122
   @param[in] jreg       The JTAG register
123
   @param[in] num_bytes  The number of bytes in the register                 */
124
/* --------------------------------------------------------------------------*/
125
static void
126
dump_jreg (const char    *prefix,
127
           unsigned char *jreg,
128
           int            num_bytes)
129
{
130
  int  i;
131
 
132
  printf ("%s:  0x", prefix);
133
 
134
  /* Dump each byte in turn */
135
  for (i = num_bytes - 1; i >=0; i--)
136
    {
137
      printf ("%02x", jreg[i]);
138
    }
139
 
140
  printf ("\n");
141
 
142
}       /* dump_jreg () */
143
 
144
 
145
/* --------------------------------------------------------------------------*/
146
/*!Process a JTAG instruction register
147
 
148
   Usage:
149
 
150
     INSTRUCTION <value>
151
 
152
   The single argument is a single hex digit, specifying the instruction
153
   value.
154
 
155
   Like all the JTAG instructions, it must be reversed, so it is shifted MS
156
   bit first.
157
 
158
   @param[in] next_jreg  Offset into argv of the next JTAG register hex
159
                         string.
160
   @param[in] argc       argc from the main program (for checking next_jreg).
161
   @param[in] argv       argv from the main program.
162
 
163
   @return  1 (TRUE) on success, 0 (FALSE) on failure.                       */
164
/* --------------------------------------------------------------------------*/
165
static int
166
process_instruction (int   next_jreg,
167
                     int   argc,
168
                     char *argv[])
169
{
170
  printf ("Shifting instruction.\n");
171
 
172
  /* Do we have the arg? */
173
  if (next_jreg >= argc)
174
    {
175
      printf ("ERROR: no instruction register value found.\n");
176
      return  0;
177
    }
178
 
179
  /* Is the argument in range? */
180
  unsigned long int  ival = strtoul (argv[next_jreg], NULL, 16);
181
 
182
  if (ival > 0xf)
183
    {
184
      printf ("ERROR: instruction value 0x%lx too large\n", ival);
185
      return  0;
186
    }
187
 
188
  /* Reverse the bits of the value */
189
  ival = reverse_bits (ival, 4);
190
 
191
  /* Allocate space and populate the register */
192
  unsigned char *jreg = malloc (1);
193
 
194
  if (NULL == jreg)
195
    {
196
      printf ("ERROR: malloc for instruction register failed.\n");
197
      return  0;
198
    }
199
 
200
  jreg[0] = ival;
201
 
202
  dump_jreg ("  shifting in", jreg, 1);
203
 
204 98 jeremybenn
  double  t = or1ksim_jtag_shift_ir (jreg, 4);
205 97 jeremybenn
 
206
  dump_jreg ("  shifted out", jreg, 1);
207
  printf ("  time taken:   %.12fs\n", t);
208
 
209
  free (jreg);
210
  return  1;                    /* Completed successfully */
211
 
212
}       /* process_instruction () */
213
 
214
 
215
/* --------------------------------------------------------------------------*/
216
/*!Process a JTAG SELECT_MODULE debug data register
217
 
218
   Usage:
219
 
220
     SELECT_MODULE <value>
221
 
222
   The one argument is a single hex digit, specifying the module value.
223
 
224
   Like all the JTAG fields, it must be reversed, so it is shifted MS
225
   bit first. It also requires a 32-bit CRC.
226
 
227
   On return we get a status register and CRC.
228
 
229
   @param[in] next_jreg  Offset into argv of the next JTAG register hex
230
                         string.
231
   @param[in] argc       argc from the main program (for checking next_jreg).
232
   @param[in] argv       argv from the main program.
233
 
234
   @return  1 (TRUE) on success, 0 (FALSE) on failure.                       */
235
/* --------------------------------------------------------------------------*/
236
static int
237
process_select_module (int   next_jreg,
238
                       int   argc,
239
                       char *argv[])
240
{
241
  printf ("Selecting module.\n");
242
 
243
  /* Do we have the arg? */
244
  if (next_jreg >= argc)
245
    {
246
      printf ("ERROR: no module specified.\n");
247
      return  0;
248
    }
249
 
250
  /* Is the argument in range? */
251
  unsigned long int  module_id = strtoul (argv[next_jreg], NULL, 16);
252
 
253
  if (module_id > 0xf)
254
    {
255
      printf ("ERROR: module value 0x%lx too large\n", module_id);
256
      return  0;
257
    }
258
 
259
  /* Compute the CRC */
260
  unsigned long int  crc_in;
261
 
262
  crc_in = crc32 (1,         1, 0xffffffff);
263
  crc_in = crc32 (module_id, 4, crc_in);
264
 
265
  /* Reverse the fields */
266
  module_id = reverse_bits (module_id, 4);
267
  crc_in    = reverse_bits (crc_in, 32);
268
 
269
  /* Allocate space and initialize the register
270
     - 1 indicator bit
271
     - 4 module bits in
272
     - 32 bit CRC in
273
     - 4 bits status out
274
     - 32 bits CRC out
275
 
276
     Total 73 bits = 10 bytes */
277
  int            num_bytes = 10;
278
  unsigned char *jreg      = malloc (num_bytes);
279
 
280
  if (NULL == jreg)
281
    {
282
      printf ("ERROR: malloc for SELECT_MODULE register failed.\n");
283
      return  0;
284
    }
285
 
286
  memset (jreg, 0, num_bytes);
287
 
288
  jreg[0]  = 0x01;
289
  jreg[0] |= module_id << 1;
290
  jreg[0] |= crc_in << 5;
291
  jreg[1]  = crc_in >> 3;
292
  jreg[2]  = crc_in >> 11;
293
  jreg[3]  = crc_in >> 19;
294
  jreg[4]  = crc_in >> 27;
295
 
296
  /* Note what we are shifting in and shift it. */
297
  dump_jreg ("  shifting in", jreg, num_bytes);
298 98 jeremybenn
  double  t = or1ksim_jtag_shift_dr (jreg, 32 + 4 + 32 + 4 + 1);
299 97 jeremybenn
 
300
  /* Diagnose what we are shifting out. */
301
  dump_jreg ("  shifted out", jreg, num_bytes);
302
 
303
  /* Break out fields */
304
  unsigned char      status;
305
  unsigned long int  crc_out;
306
 
307
  status = ((jreg[4] >> 5) | (jreg[5] << 3)) & 0xf ;
308
 
309
  crc_out = ((unsigned long int) jreg[5] >>  1) |
310
            ((unsigned long int) jreg[6] <<  7) |
311
            ((unsigned long int) jreg[7] << 15) |
312
            ((unsigned long int) jreg[8] << 23) |
313
            ((unsigned long int) jreg[9] << 31);
314
 
315
  /* Reverse the fields */
316
  status  = reverse_bits (status,  4);
317
  crc_out = reverse_bits (crc_out, 32);
318
 
319
  /* Compute our own CRC */
320
  unsigned long int  crc_computed = crc32 (status, 4, 0xffffffff);
321
 
322
  /* Log the results */
323
  printf ("  status:       0x%01x\n", status);
324
 
325
  if (crc_out != crc_computed)
326
    {
327
      printf ("  CRC mismatch\n");
328
      printf ("    CRC out:      0x%08lx\n", crc_out);
329
      printf ("    CRC computed: 0x%08lx\n", crc_computed);
330
    }
331
 
332
  printf ("  time taken:   %.12fs\n", t);
333
 
334
  free (jreg);
335
  return  1;                    /* Completed successfully */
336
 
337
}       /* process_select_module () */
338
 
339
 
340
/* --------------------------------------------------------------------------*/
341
/*!Process a JTAG WRITE_COMMAND debug data register
342
 
343
   Usage:
344
 
345
     WRITE_COMMAND <access_type> <address> <length>
346
 
347
   The argumens are all hex values:
348
   - access_type  Access type - 4 bits
349
   - address      32-bit address
350
   - length       number of bytes to transer up to 2^16.
351
 
352
   Like all the JTAG fields these must be reversed, so they are shifted MS bit
353
   first. They also require a 32-bit CRC.
354
 
355
   On return we get a status register and CRC.
356
 
357
   @param[in] next_jreg  Offset into argv of the next JTAG register hex
358
                         string.
359
   @param[in] argc       argc from the main program (for checking next_jreg).
360
   @param[in] argv       argv from the main program.
361
 
362
   @return  1 (TRUE) on success, 0 (FALSE) on failure.                       */
363
/* --------------------------------------------------------------------------*/
364
static int
365
process_write_command (int   next_jreg,
366
                       int   argc,
367
                       char *argv[])
368
{
369
  printf ("Processing WRITE_COMMAND.\n");
370
 
371
  /* Do we have the args */
372
  if (next_jreg + 3 > argc)
373
    {
374
      printf ("WRITE_COMMAND usage: WRITE_COMMAND <access_type> <address> "
375
              "<length>\n");
376
      return  0;
377
    }
378
 
379
  /* Are the arguments in range? Remember the length we actually put in has 1
380
     subtracted. */
381
  unsigned long int  cmd         = 2;   /* WRITE_COMMAND */
382
 
383
  unsigned long int  access_type = strtoul (argv[next_jreg    ], NULL, 16);
384
  unsigned long int  addr        = strtoul (argv[next_jreg + 1], NULL, 16);
385
  unsigned long int  len         = strtoul (argv[next_jreg + 2], NULL, 16) - 1;
386
 
387
  if (access_type > 0xf)
388
    {
389
      printf ("ERROR: WRITE_COMMAND access type 0x%lx too large\n",
390
              access_type);
391
      return  0;
392
    }
393
 
394
  if (addr > 0xffffffff)
395
    {
396
      printf ("ERROR: WRITE_COMMAND address 0x%lx too large\n", addr);
397
      return  0;
398
    }
399
 
400 98 jeremybenn
  if ((len + 1) < 0x1)
401 97 jeremybenn
    {
402 98 jeremybenn
      printf ("ERROR: WRITE_COMMAND length 0x%lx too small\n", len + 1);
403 97 jeremybenn
      return  0;
404
    }
405 98 jeremybenn
  else if ((len + 1) > 0x10000)
406
    {
407
      printf ("ERROR: WRITE_COMMAND length 0x%lx too large\n", len + 1);
408
      return  0;
409
    }
410 97 jeremybenn
 
411
  /* Compute the CRC */
412
  unsigned long int  crc_in;
413
 
414
  crc_in = crc32 (0,            1, 0xffffffff);
415
  crc_in = crc32 (cmd,          4, crc_in);
416
  crc_in = crc32 (access_type,  4, crc_in);
417
  crc_in = crc32 (addr,        32, crc_in);
418
  crc_in = crc32 (len,         16, crc_in);
419
 
420
  /* Reverse the fields */
421
  cmd         = reverse_bits (cmd,          4);
422
  access_type = reverse_bits (access_type,  4);
423
  addr        = reverse_bits (addr,        32);
424
  len         = reverse_bits (len,         16);
425
  crc_in      = reverse_bits (crc_in,      32);
426
 
427
  /* Allocate space and initialize the register
428
     -  1 indicator bit
429
     -  4 bits command in
430
     -  4 bits access type in
431
     - 32 bits address in
432
     - 16 bits length in
433
     - 32 bits CRC in
434
     -  4 bits status out
435
     - 32 bits CRC out
436
 
437
     Total 125 bits = 16 bytes */
438
  int            num_bytes = 16;
439
  unsigned char *jreg      = malloc (num_bytes);
440
 
441
  if (NULL == jreg)
442
    {
443
      printf ("ERROR: malloc for WRITE_COMMAND register failed.\n");
444
      return  0;
445
    }
446
 
447
  memset (jreg, 0, num_bytes);
448
 
449
  jreg[ 0]  = 0x0;
450
 
451
  jreg[ 0] |= cmd         << 1;
452
 
453
  jreg[ 0] |= access_type << 5;
454
  jreg[ 1]  = access_type >> 3;
455
 
456
  jreg[ 1] |= addr        <<  1;
457
  jreg[ 2]  = addr        >>  7;
458
  jreg[ 3]  = addr        >> 15;
459
  jreg[ 4]  = addr        >> 23;
460
  jreg[ 5]  = addr        >> 31;
461
 
462
  jreg[ 5] |= len         <<  1;
463
  jreg[ 6]  = len         >>  7;
464
  jreg[ 7]  = len         >> 15;
465
 
466
  jreg[ 7] |= crc_in      <<  1;
467
  jreg[ 8]  = crc_in      >>  7;
468
  jreg[ 9]  = crc_in      >> 15;
469
  jreg[10]  = crc_in      >> 23;
470
  jreg[11]  = crc_in      >> 31;
471
 
472
  /* Note what we are shifting in and shift it. */
473
  dump_jreg ("  shifting in", jreg, num_bytes);
474 98 jeremybenn
  double  t = or1ksim_jtag_shift_dr (jreg, 32 + 4 + 32 + 16 + 32 + 4 + 4 + 1);
475 97 jeremybenn
 
476
  /* Diagnose what we are shifting out. */
477
  dump_jreg ("  shifted out", jreg, num_bytes);
478
 
479
  /* Break out fields */
480
  unsigned char      status;
481
  unsigned long int  crc_out;
482
 
483
  status = (jreg[11] >> 1) & 0xf ;
484
 
485
  crc_out = ((unsigned long int) jreg[11] >>  5) |
486
            ((unsigned long int) jreg[12] <<  3) |
487
            ((unsigned long int) jreg[13] << 11) |
488
            ((unsigned long int) jreg[14] << 19) |
489
            ((unsigned long int) jreg[15] << 27);
490
 
491
  /* Reverse the fields */
492
  status  = reverse_bits (status,  4);
493
  crc_out = reverse_bits (crc_out, 32);
494
 
495
  /* Compute our own CRC */
496
  unsigned long int  crc_computed = crc32 (status, 4, 0xffffffff);
497
 
498
  /* Log the results */
499
  printf ("  status:       0x%01x\n", status);
500
 
501
  if (crc_out != crc_computed)
502
    {
503
      printf ("  CRC mismatch\n");
504
      printf ("    CRC out:      0x%08lx\n", crc_out);
505
      printf ("    CRC computed: 0x%08lx\n", crc_computed);
506
    }
507
 
508
  printf ("  time taken:   %.12fs\n", t);
509
 
510
  free (jreg);
511
  return  1;                    /* Completed successfully */
512
 
513
}       /* process_write_command () */
514
 
515
 
516
/* --------------------------------------------------------------------------*/
517
/*!Process a JTAG READ_COMMAND debug data register
518
 
519
   Usage:
520
 
521
     READ_COMMAND
522
 
523
   There are no arguments. It is used to read back the values used in a prior
524
   WRITE_COMMAND.
525
 
526
   On return we get the access type, address, length, status register and CRC.
527
 
528
   @param[in] next_jreg  Offset into argv of the next JTAG register hex
529
                         string.
530
   @param[in] argc       argc from the main program (for checking next_jreg).
531
   @param[in] argv       argv from the main program.
532
 
533
   @return  1 (TRUE) on success, 0 (FALSE) on failure.                       */
534
/* --------------------------------------------------------------------------*/
535
static int
536
process_read_command (int   next_jreg,
537
                       int   argc,
538
                       char *argv[])
539
{
540
  printf ("Processing READ_COMMAND.\n");
541
 
542
  /* The only value on input is the READ_COMMAND command */
543
  unsigned long int  cmd         = 1;   /* READ_COMMAND */
544
 
545
  /* Compute the CRC */
546
  unsigned long int  crc_in;
547
 
548
  crc_in = crc32 (0,            1, 0xffffffff);
549
  crc_in = crc32 (cmd,          4, crc_in);
550
 
551
  /* Reverse the fields */
552
  cmd    = reverse_bits (cmd,     4);
553
  crc_in = reverse_bits (crc_in, 32);
554
 
555
  /* Allocate space and initialize the register
556
     -  1 indicator bit
557
     -  4 bits command in
558
     - 32 bits CRC in
559
     -  4 bits access type out
560
     - 32 bits address out
561
     - 16 bits length out
562
     -  4 bits status out
563
     - 32 bits CRC out
564
 
565
     Total 125 bits = 16 bytes */
566
  int            num_bytes = 16;
567
  unsigned char *jreg      = malloc (num_bytes);
568
 
569
  if (NULL == jreg)
570
    {
571
      printf ("ERROR: malloc for READ_COMMAND register failed.\n");
572
      return  0;
573
    }
574
 
575
  memset (jreg, 0, num_bytes);
576
 
577
  jreg[ 0]  = 0x0;
578
 
579
  jreg[0] |= cmd    << 1;
580
 
581
  jreg[0] |= crc_in <<  5;
582
  jreg[1]  = crc_in >>  3;
583
  jreg[2]  = crc_in >> 11;
584
  jreg[3]  = crc_in >> 19;
585
  jreg[4]  = crc_in >> 27;
586
 
587
  /* Note what we are shifting in and shift it. */
588
  dump_jreg ("  shifting in", jreg, num_bytes);
589 98 jeremybenn
  double  t = or1ksim_jtag_shift_dr (jreg, 32 + 4 + 16 + 32 + 4 + 32 + 4 + 1);
590 97 jeremybenn
 
591
  /* Diagnose what we are shifting out. */
592
  dump_jreg ("  shifted out", jreg, num_bytes);
593
 
594
  /* Break out fields */
595
  unsigned char      access_type;
596
  unsigned long int  addr;
597
  unsigned long int  len;
598
  unsigned char      status;
599
  unsigned long int  crc_out;
600
 
601
  access_type = ((jreg[4] >> 5) | (jreg[5] << 3)) & 0xf ;
602
  addr        = ((unsigned long int) jreg[ 5] >>  1) |
603
                ((unsigned long int) jreg[ 6] <<  7) |
604
                ((unsigned long int) jreg[ 7] << 15) |
605
                ((unsigned long int) jreg[ 8] << 23) |
606
                ((unsigned long int) jreg[ 9] << 31);
607
 
608
  len         = ((unsigned long int)  jreg[ 9]        >>  1) |
609
                ((unsigned long int)  jreg[10]        <<  7) |
610
                ((unsigned long int) (jreg[11] & 0x1) << 15);
611
 
612
  status      = (jreg[11] >> 1) & 0xf ;
613
 
614
  crc_out     = ((unsigned long int) jreg[11] >>  5) |
615
                ((unsigned long int) jreg[12] <<  3) |
616
                ((unsigned long int) jreg[13] << 11) |
617
                ((unsigned long int) jreg[14] << 19) |
618
                ((unsigned long int) jreg[15] << 27);
619
 
620
  /* Reverse the fields */
621
 
622
  access_type = reverse_bits (access_type,  4);
623
  addr        = reverse_bits (addr,        32);
624
  len         = reverse_bits (len,         16);
625
  status      = reverse_bits (status,       4);
626
  crc_out     = reverse_bits (crc_out,     32);
627
 
628
  /* Compute our own CRC */
629
  unsigned long int  crc_computed;
630
 
631
  crc_computed = crc32 (access_type,  4, 0xffffffff);
632
  crc_computed = crc32 (addr,        32, crc_computed);
633
  crc_computed = crc32 (len,         16, crc_computed);
634
  crc_computed = crc32 (status,       4, crc_computed);
635
 
636
  /* Log the results. Remember the length is 1 greater than the value
637
     returned. */
638 98 jeremybenn
  printf ("  access_type:  0x%x\n",    access_type);
639 97 jeremybenn
  printf ("  address:      0x%lx\n",   addr);
640
  printf ("  length:       0x%lx\n",   len + 1);
641
  printf ("  status:       0x%x\n",    status);
642
 
643
  if (crc_out != crc_computed)
644
    {
645
      printf ("  CRC mismatch\n");
646
      printf ("    CRC out:      0x%08lx\n", crc_out);
647
      printf ("    CRC computed: 0x%08lx\n", crc_computed);
648
    }
649
 
650
  printf ("  time taken:   %.12fs\n", t);
651
 
652
  free (jreg);
653
  return  1;                    /* Completed successfully */
654
 
655
}       /* process_read_command () */
656
 
657
 
658
/* --------------------------------------------------------------------------*/
659
/*!Process a JTAG GO_COMMAND_WRITE debug data register
660
 
661
   Usage:
662
 
663
     GO_COMMAND_WRITE <data>
664
 
665 98 jeremybenn
   The one argument is a string of bytes to be written, LS byte first.
666 97 jeremybenn
 
667
   Like all the JTAG fields, each data byte must be reversed, so it is shifted
668
   MS bit first. It also requires a 32-bit CRC.
669
 
670
   On return we get a status register and CRC.
671
 
672
   @param[in] next_jreg  Offset into argv of the next JTAG register hex
673
                         string.
674
   @param[in] argc       argc from the main program (for checking next_jreg).
675
   @param[in] argv       argv from the main program.
676
 
677
   @return  1 (TRUE) on success, 0 (FALSE) on failure.                       */
678
/* --------------------------------------------------------------------------*/
679
static int
680
process_go_command_write (int   next_jreg,
681
                          int   argc,
682
                          char *argv[])
683
{
684
  printf ("Processing GO_COMMAND_WRITE.\n");
685
 
686
  /* Do we have the arg */
687
  if (next_jreg >= argc)
688
    {
689
      printf ("GO_COMMAND_WRITE usage: GO_COMMAND_WRITE <data>.\n");
690
      return  0;
691
    }
692
 
693
  /* Break out the fields, including the data string into a vector of bytes. */
694
  unsigned long int  cmd        = 0;     /* GO_COMMAND */
695
 
696
  char              *data_str   = argv[next_jreg];
697
  int                data_len   = strlen (data_str);
698 98 jeremybenn
  int                data_bytes = data_len / 2;
699 97 jeremybenn
  unsigned char     *data       = malloc (data_bytes);
700
 
701
  if (NULL == data)
702
    {
703
      printf ("ERROR: data malloc for GO_COMMAND_WRITE register failed.\n");
704
      return  0;
705
    }
706
 
707 98 jeremybenn
  if (1 == (data_len % 2))
708
    {
709
      printf ("Warning: GO_COMMAND_WRITE odd char ignored\n");
710
    }
711
 
712 97 jeremybenn
  int  i;
713
 
714
  for (i = 0; i < data_bytes; i++)
715
    {
716 98 jeremybenn
      int            ch_off_ms = i * 2;
717
      int            ch_off_ls = i * 2 + 1;
718 97 jeremybenn
 
719
      /* Get each nybble in turn, remembering that we may not have a MS nybble
720
         if the data string has an odd number of chars. */
721
      data[i] = 0;
722
 
723 98 jeremybenn
      int  j;
724
 
725 97 jeremybenn
      for (j = ch_off_ms; j <= ch_off_ls; j++)
726
        {
727
          char  c       = data_str[j];
728
          int   dig_val = (('0' <= c) && (c <= '9')) ? c - '0' :
729
                          (('a' <= c) && (c <= 'f')) ? c - 'a' + 10 :
730
                          (('A' <= c) && (c <= 'F')) ? c - 'A' + 10 : -1;
731
 
732
          if (dig_val < 0)
733
            {
734
              printf ("ERROR: Non-hex digit in data: %c\n", c);
735
              free (data);
736
              return  0;
737
            }
738
 
739
          data[i] = (data[i] << 4) | dig_val;
740
        }
741
    }
742
 
743
  /* Are the arguments in range? Remember the length we actually put in has 1
744
     subtracted. */
745
 
746
  /* Compute the CRC */
747
  unsigned long int  crc_in;
748
 
749
  crc_in = crc32 (0,   1, 0xffffffff);
750
  crc_in = crc32 (cmd, 4, crc_in);
751
 
752
  for (i = 0; i < data_bytes; i++)
753
    {
754
      crc_in = crc32 (data[i], 8, crc_in);
755
    }
756
 
757
  /* Reverse the fields */
758
  cmd = reverse_bits (cmd, 4);
759
 
760
  for (i = 0; i < data_bytes; i++)
761
    {
762
      data[i] = reverse_bits (data[i], 8);
763
    }
764
 
765
  crc_in = reverse_bits (crc_in,  32);
766
 
767
  /* Allocate space and initialize the register
768
     -              1 indicator bit
769
     -              4 bits command in
770
     - data_bytes * 8 bits access type in
771
     -             32 bits CRC in
772
     -              4 bits status out
773
     -             32 bits CRC out
774
 
775
     Total 73 + data_bytes * 8 bits = 10 + data_bytes bytes */
776
  int            num_bytes = 10 + data_bytes;
777
  unsigned char *jreg      = malloc (num_bytes);
778
 
779
  if (NULL == jreg)
780
    {
781
      printf ("ERROR: jreg malloc for GO_COMMAND_WRITE register failed.\n");
782
      free (data);
783
      return  0;
784
    }
785
 
786
  memset (jreg, 0, num_bytes);
787
 
788
  jreg[ 0]  = 0x0;
789
  jreg[ 0] |= cmd << 1;
790
 
791
  for (i = 0; i < data_bytes; i++)
792
    {
793
      jreg[i]     |= data[i] << 5;
794
      jreg[i + 1]  = data[i] >> 3;
795
    }
796
 
797
  jreg[data_bytes    ] |= crc_in <<  5;
798
  jreg[data_bytes + 1]  = crc_in >>  3;
799
  jreg[data_bytes + 2]  = crc_in >> 11;
800
  jreg[data_bytes + 3]  = crc_in >> 19;
801
  jreg[data_bytes + 4]  = crc_in >> 27;
802
 
803
  /* Note what we are shifting in and shift it. */
804
  dump_jreg ("  shifting in", jreg, num_bytes);
805 98 jeremybenn
  double  t = or1ksim_jtag_shift_dr (jreg,
806
                                     32 + 4 + 32 + data_bytes * 8 + 4 + 1);
807 97 jeremybenn
 
808
  /* Diagnose what we are shifting out. */
809
  dump_jreg ("  shifted out", jreg, num_bytes);
810
 
811
  /* Break out fields */
812
  unsigned char      status;
813
  unsigned long int  crc_out;
814
 
815
  status = ((jreg[data_bytes + 4] >> 5) | (jreg[data_bytes + 5] << 3)) & 0xf ;
816
 
817
  crc_out = ((unsigned long int) jreg[data_bytes + 5] >>  1) |
818
            ((unsigned long int) jreg[data_bytes + 6] <<  7) |
819
            ((unsigned long int) jreg[data_bytes + 7] << 15) |
820
            ((unsigned long int) jreg[data_bytes + 8] << 23) |
821
            ((unsigned long int) jreg[data_bytes + 9] << 31);
822
 
823
  /* Reverse the fields */
824
  status  = reverse_bits (status,   4);
825
  crc_out = reverse_bits (crc_out, 32);
826
 
827
  /* Compute our own CRC */
828
  unsigned long int  crc_computed = crc32 (status, 4, 0xffffffff);
829
 
830
  /* Log the results */
831
  printf ("  status:       0x%01x\n", status);
832
 
833
  if (crc_out != crc_computed)
834
    {
835
      printf ("  CRC mismatch\n");
836
      printf ("    CRC out:      0x%08lx\n", crc_out);
837
      printf ("    CRC computed: 0x%08lx\n", crc_computed);
838
    }
839
 
840
  printf ("  time taken:   %.12fs\n", t);
841
 
842
  free (data);
843
  free (jreg);
844
  return  1;                    /* Completed successfully */
845
 
846
}       /* process_go_command_write () */
847
 
848
 
849
/* --------------------------------------------------------------------------*/
850
/*!Process a JTAG GO_COMMAND_READ debug data register
851
 
852
   Usage:
853
 
854
     GO_COMMAND_READ <length>
855
 
856
   The one argument is a length in hex, specifying the number of bytes to be
857
   read.
858
 
859
   On return we get a status register and CRC.
860
 
861
   Like all JTAG fields, the CRC shifted in, the data read back, the status
862
   and CRC shifted out, must be reversed, since they are shifted in MS bit
863
   first and out LS bit first.
864
 
865
   @param[in] next_jreg  Offset into argv of the next JTAG register hex
866
                         string.
867
   @param[in] argc       argc from the main program (for checking next_jreg).
868
   @param[in] argv       argv from the main program.
869
 
870
   @return  1 (TRUE) on success, 0 (FALSE) on failure.                       */
871
/* --------------------------------------------------------------------------*/
872
static int
873
process_go_command_read (int   next_jreg,
874
                         int   argc,
875
                         char *argv[])
876
{
877
  printf ("Processing GO_COMMAND_READ.\n");
878
 
879
  /* Do we have the args */
880
  if (next_jreg >= argc)
881
    {
882
      printf ("GO_COMMAND_READ usage: GO_COMMAND_READ <length>\n");
883
      return  0;
884
    }
885
 
886
  /* Is the argument in range? Remember the length we actually put in has 1
887
     subtracted, so although it is a 16-bit field, it can be up to 2^16. */
888
  unsigned long int  cmd        = 0;     /* GO_COMMAND */
889
  unsigned long int  data_bytes = strtoul (argv[next_jreg], NULL, 16);
890
 
891 98 jeremybenn
  if (data_bytes < 0)
892 97 jeremybenn
    {
893 98 jeremybenn
      printf ("ERROR: GO_COMMAND_READ length 0x%lx too small\n", data_bytes);
894
      return  0;
895
    }
896
  else if (data_bytes > 0x10000)
897
    {
898 97 jeremybenn
      printf ("ERROR: GO_COMMAND_READ length 0x%lx too large\n", data_bytes);
899
      return  0;
900
    }
901
 
902
  /* Compute the CRC */
903
  unsigned long int  crc_in;
904
 
905
  crc_in = crc32 (0,   1, 0xffffffff);
906
  crc_in = crc32 (cmd, 4, crc_in);
907
 
908
  /* Reverse the fields */
909
  cmd    = reverse_bits (cmd, 4);
910
  crc_in = reverse_bits (crc_in,  32);
911
 
912
  /* Allocate space and initialize the register
913
     -              1 indicator bit
914
     -              4 bits command in
915
     -             32 bits CRC in
916
     - data_bytes * 8 bits access type out
917
     -              4 bits status out
918
     -             32 bits CRC out
919
 
920
     Total 73 + data_bytes * 8 bits = 10 + data_bytes bytes */
921
  int            num_bytes = 10 + data_bytes;
922
  unsigned char *jreg      = malloc (num_bytes);
923
 
924
  if (NULL == jreg)
925
    {
926
      printf ("ERROR: malloc forGO_COMMAND_READ register failed.\n");
927
      return  0;
928
    }
929
 
930
  memset (jreg, 0, num_bytes);
931
 
932
  jreg[0]  = 0x0;
933
  jreg[0] |= cmd << 1;
934
 
935
  jreg[0] |= crc_in <<  5;
936
  jreg[1]  = crc_in >>  3;
937
  jreg[2]  = crc_in >> 11;
938
  jreg[3]  = crc_in >> 19;
939
  jreg[4]  = crc_in >> 27;
940
 
941
  /* Note what we are shifting in and shift it. */
942
  dump_jreg ("  shifting in", jreg, num_bytes);
943 98 jeremybenn
  double  t = or1ksim_jtag_shift_dr (jreg,
944
                                     32 + 4 + data_bytes * 8 + 32 + 4 + 1);
945 97 jeremybenn
 
946
  /* Diagnose what we are shifting out. */
947
  dump_jreg ("  shifted out", jreg, num_bytes);
948
 
949
  /* Break out fields */
950
  unsigned char     *data = malloc (data_bytes);
951
  unsigned char      status;
952
  unsigned long int  crc_out;
953
 
954
  if (NULL == data)
955
    {
956 98 jeremybenn
      printf ("ERROR: data malloc for GO_COMMAND_READ register failed.\n");
957 97 jeremybenn
      free (jreg);
958
      return  0;
959
    }
960
 
961
  int  i;
962
 
963
  for (i = 0; i < data_bytes; i++)
964
    {
965
      data[i] = ((jreg[i + 4] >> 5) | (jreg[i + 5] << 3)) & 0xff;
966
    }
967
 
968
  status = ((jreg[data_bytes + 4] >> 5) | (jreg[data_bytes + 5] << 3)) & 0xf ;
969
 
970
  crc_out = ((unsigned long int) jreg[data_bytes + 5] >>  1) |
971
            ((unsigned long int) jreg[data_bytes + 6] <<  7) |
972
            ((unsigned long int) jreg[data_bytes + 7] << 15) |
973
            ((unsigned long int) jreg[data_bytes + 8] << 23) |
974
            ((unsigned long int) jreg[data_bytes + 9] << 31);
975
 
976
  /* Reverse the fields */
977
  for (i = 0; i < data_bytes; i++)
978
    {
979
      data[i] = reverse_bits (data[i], 8);
980
    }
981
 
982
  status  = reverse_bits (status,   4);
983
  crc_out = reverse_bits (crc_out, 32);
984
 
985
  /* Compute our own CRC */
986
  unsigned long int  crc_computed = 0xffffffff;
987
 
988
  for (i = 0; i < data_bytes; i++)
989
    {
990
      crc_computed = crc32 (data[i], 8, crc_computed);
991
    }
992
 
993
  crc_computed = crc32 (status, 4, crc_computed);
994
 
995 98 jeremybenn
  /* Log the results, remembering these are bytes, so endianness is not a
996
     factor here. Since the OR1K is big endian, the lowest numbered byte will
997
     be the least significant, and the first printed */
998
  printf ("  data:         ");
999 97 jeremybenn
 
1000 98 jeremybenn
  for (i = 0; i < data_bytes; i++)
1001 97 jeremybenn
    {
1002
      printf ("%02x", data[i]);
1003
    }
1004
 
1005
  printf ("\n");
1006
  printf ("  status:       0x%01x\n", status);
1007
 
1008
  if (crc_out != crc_computed)
1009
    {
1010
      printf ("  CRC mismatch\n");
1011
      printf ("    CRC out:      0x%08lx\n", crc_out);
1012
      printf ("    CRC computed: 0x%08lx\n", crc_computed);
1013
    }
1014
 
1015
  printf ("  time taken:   %.12fs\n", t);
1016
 
1017
  free (data);
1018
  free (jreg);
1019
  return  1;                    /* Completed successfully */
1020
 
1021
}       /* process_go_command_read () */
1022
 
1023
 
1024
/* --------------------------------------------------------------------------*/
1025
/*!Process a JTAG WRITE_CONTROL debug data register
1026
 
1027
   Usage:
1028
 
1029
     WRITE_CONTROL <reset> <stall>
1030
 
1031
   The arguments should be either zero or one.
1032
 
1033
   The arguments are used to construct the 52-bit CPU control register. Like
1034
   all JTAG fields, it must be reversed, so it is shifted MS bit first. It
1035
   also requires a 32-bit CRC.
1036
 
1037
   On return we get a status register and CRC.
1038
 
1039
   @param[in] next_jreg  Offset into argv of the next JTAG register hex
1040
                         string.
1041
   @param[in] argc       argc from the main program (for checking next_jreg).
1042
   @param[in] argv       argv from the main program.
1043
 
1044
   @return  1 (TRUE) on success, 0 (FALSE) on failure.                       */
1045
/* --------------------------------------------------------------------------*/
1046
static int
1047
process_write_control (int   next_jreg,
1048
                       int   argc,
1049
                       char *argv[])
1050
{
1051
  printf ("Processing WRITE_CONTROL.\n");
1052
 
1053
  /* Do we have the args */
1054
  if (next_jreg + 2 > argc)
1055
    {
1056
      printf ("WRITE_CONTROL usage: WRITE_CONTROL <reset> <status>\n");
1057
      return  0;
1058
    }
1059
 
1060
  /* Are the arguments in range? */
1061
  unsigned long int  cmd   = 4;         /* WRITE_CONTROL */
1062
 
1063
  unsigned long int  reset = strtoul (argv[next_jreg    ], NULL, 16);
1064
  unsigned long int  stall = strtoul (argv[next_jreg + 1], NULL, 16);
1065
 
1066
  if (reset > 0x1)
1067
    {
1068
      printf ("ERROR: invalid WRITE_CONTROL reset value 0x%lx.\n", reset);
1069
      return  0;
1070
    }
1071
 
1072
  if (stall > 0x1)
1073
    {
1074
      printf ("ERROR: invalid WRITE_CONTROL stall value 0x%lx.\n", stall);
1075
      return  0;
1076
    }
1077
 
1078
  /* Construct the control register */
1079
  unsigned long long int  creg = ((unsigned long long int) reset << 51) |
1080
                                 ((unsigned long long int) stall << 50);
1081
 
1082
  /* Compute the CRC */
1083
  unsigned long int  crc_in;
1084
 
1085
  crc_in = crc32 (0,     1, 0xffffffff);
1086
  crc_in = crc32 (cmd,   4, crc_in);
1087
  crc_in = crc32 (creg, 52, crc_in);
1088
 
1089
  /* Reverse the fields */
1090
  cmd    = reverse_bits (cmd,     4);
1091
  creg   = reverse_bits (creg,   52);
1092
  crc_in = reverse_bits (crc_in, 32);
1093
 
1094
  /* Allocate space and initialize the register
1095
     -  1 indicator bit
1096
     -  4 bits command in
1097
     - 52 bits control register
1098
     - 32 bits CRC in
1099
     -  4 bits status out
1100
     - 32 bits CRC out
1101
 
1102
     Total 125 bits = 16 bytes */
1103
  int            num_bytes = 16;
1104
  unsigned char *jreg      = malloc (num_bytes);
1105
 
1106
  if (NULL == jreg)
1107
    {
1108
      printf ("ERROR: malloc for WRITE_CONTROL register failed.\n");
1109
      return  0;
1110
    }
1111
 
1112
  memset (jreg, 0, num_bytes);
1113
 
1114
  jreg[ 0]  = 0x0;
1115
 
1116
  jreg[ 0] |= cmd    <<  1;
1117
 
1118
  jreg[ 0] |= creg   <<  5;
1119
  jreg[ 1]  = creg   >>  3;
1120
  jreg[ 2]  = creg   >> 11;
1121
  jreg[ 3]  = creg   >> 19;
1122
  jreg[ 4]  = creg   >> 27;
1123
  jreg[ 5]  = creg   >> 35;
1124
  jreg[ 6]  = creg   >> 43;
1125
  jreg[ 7]  = creg   >> 51;
1126
 
1127
  jreg[ 7] |= crc_in      <<  1;
1128
  jreg[ 8]  = crc_in      >>  7;
1129
  jreg[ 9]  = crc_in      >> 15;
1130
  jreg[10]  = crc_in      >> 23;
1131
  jreg[11]  = crc_in      >> 31;
1132
 
1133
  /* Note what we are shifting in and shift it. */
1134
  dump_jreg ("  shifting in", jreg, num_bytes);
1135 98 jeremybenn
  double  t = or1ksim_jtag_shift_dr (jreg, 32 + 4 + 32 + 52 + 4 + 1);
1136 97 jeremybenn
 
1137
  /* Diagnose what we are shifting out. */
1138
  dump_jreg ("  shifted out", jreg, num_bytes);
1139
 
1140
  /* Break out fields */
1141
  unsigned char      status;
1142
  unsigned long int  crc_out;
1143
 
1144
  status  = (jreg[11] >> 1) & 0xf ;
1145
 
1146
  crc_out = ((unsigned long int) jreg[11] >>  5) |
1147
            ((unsigned long int) jreg[12] <<  3) |
1148
            ((unsigned long int) jreg[13] << 11) |
1149
            ((unsigned long int) jreg[14] << 19) |
1150
            ((unsigned long int) jreg[15] << 27);
1151
 
1152
  /* Reverse the fields */
1153
  status  = reverse_bits (status,  4);
1154
  crc_out = reverse_bits (crc_out, 32);
1155
 
1156
  /* Compute our own CRC */
1157
  unsigned long int  crc_computed = crc32 (status, 4, 0xffffffff);
1158
 
1159
  /* Log the results */
1160
  printf ("  status:       0x%01x\n", status);
1161
 
1162
  if (crc_out != crc_computed)
1163
    {
1164
      printf ("  CRC mismatch\n");
1165
      printf ("    CRC out:      0x%08lx\n", crc_out);
1166
      printf ("    CRC computed: 0x%08lx\n", crc_computed);
1167
    }
1168
 
1169
  printf ("  time taken:   %.12fs\n", t);
1170
 
1171
  free (jreg);
1172
  return  1;                    /* Completed successfully */
1173
 
1174
}       /* process_write_control () */
1175
 
1176
 
1177
/* --------------------------------------------------------------------------*/
1178
/*!Process a JTAG READ_CONTROL debug data register
1179
 
1180
   Usage:
1181
 
1182
     READ_CONTROL
1183
 
1184
   There are no arguments. It requires a 32-bit CRC.
1185
 
1186
   On return we get the control register, status and CRC.
1187
 
1188
   Like all the JTAG fields, they must be reversed, as resutl is shifted out
1189
   LS bit first.
1190
 
1191
   @param[in] next_jreg  Offset into argv of the next JTAG register hex
1192
                         string.
1193
   @param[in] argc       argc from the main program (for checking next_jreg).
1194
   @param[in] argv       argv from the main program.
1195
 
1196
   @return  1 (TRUE) on success, 0 (FALSE) on failure.                       */
1197
/* --------------------------------------------------------------------------*/
1198
static int
1199
process_read_control (int   next_jreg,
1200
                       int   argc,
1201
                       char *argv[])
1202
{
1203
  printf ("Processing READ_CONTROL.\n");
1204
 
1205
  /* Only input field is cmd. */
1206
  unsigned long int  cmd   = 3;         /* READ_CONTROL */
1207
 
1208
  /* Compute the CRC */
1209
  unsigned long int  crc_in;
1210
 
1211
  crc_in = crc32 (0,     1, 0xffffffff);
1212
  crc_in = crc32 (cmd,   4, crc_in);
1213
 
1214
  /* Reverse the fields */
1215
  cmd    = reverse_bits (cmd,     4);
1216
  crc_in = reverse_bits (crc_in, 32);
1217
 
1218
  /* Allocate space and initialize the register
1219
     -  1 indicator bit
1220
     -  4 bits command in
1221
     - 32 bits CRC in
1222
     - 52 bits control register out
1223
     -  4 bits status out
1224
     - 32 bits CRC out
1225
 
1226
     Total 125 bits = 16 bytes */
1227
  int            num_bytes = 16;
1228
  unsigned char *jreg      = malloc (num_bytes);
1229
 
1230
  if (NULL == jreg)
1231
    {
1232
      printf ("ERROR: malloc for READ_CONTROL register failed.\n");
1233
      return  0;
1234
    }
1235
 
1236
  memset (jreg, 0, num_bytes);
1237
 
1238
  jreg[0]  = 0x0;
1239
 
1240
  jreg[0] |= cmd    <<  1;
1241
 
1242
  jreg[0] |= crc_in <<  5;
1243
  jreg[1]  = crc_in >>  3;
1244
  jreg[2]  = crc_in >> 11;
1245
  jreg[3]  = crc_in >> 19;
1246
  jreg[4]  = crc_in >> 27;
1247
 
1248
  /* Note what we are shifting in and shift it. */
1249
  dump_jreg ("  shifting in", jreg, num_bytes);
1250 98 jeremybenn
  double  t = or1ksim_jtag_shift_dr (jreg, 32 + 4 + 52 + 32 + 4 + 1);
1251 97 jeremybenn
 
1252
  /* Diagnose what we are shifting out. */
1253
  dump_jreg ("  shifted out", jreg, num_bytes);
1254
 
1255
  /* Break out fields */
1256
  unsigned long long int  creg;
1257
  unsigned char           status;
1258
  unsigned long int       crc_out;
1259
 
1260
  creg    = ((unsigned long long int)  jreg[ 4]        >>  5) |
1261
            ((unsigned long long int)  jreg[ 5]        <<  3) |
1262
            ((unsigned long long int)  jreg[ 6]        << 11) |
1263
            ((unsigned long long int)  jreg[ 7]        << 19) |
1264
            ((unsigned long long int)  jreg[ 8]        << 27) |
1265
            ((unsigned long long int)  jreg[ 9]        << 35) |
1266
            ((unsigned long long int)  jreg[10]        << 43) |
1267
            ((unsigned long long int) (jreg[11] & 0x1) << 51);
1268
 
1269
  status  = (jreg[11] >> 1) & 0xf ;
1270
 
1271
  crc_out = ((unsigned long int) jreg[11] >>  5) |
1272
            ((unsigned long int) jreg[12] <<  3) |
1273
            ((unsigned long int) jreg[13] << 11) |
1274
            ((unsigned long int) jreg[14] << 19) |
1275
            ((unsigned long int) jreg[15] << 27);
1276
 
1277
  /* Reverse the fields */
1278
  creg    = reverse_bits (creg,    52);
1279
  status  = reverse_bits (status,   4);
1280
  crc_out = reverse_bits (crc_out, 32);
1281
 
1282
  /* Compute our own CRC */
1283
  unsigned long int  crc_computed;
1284
 
1285
  crc_computed = crc32 (creg,   52, 0xffffffff);
1286
  crc_computed = crc32 (status,  4, crc_computed);
1287
 
1288
  const char *reset = (1 == ((creg >> 51) & 1)) ? "enabled" : "disabled";
1289
  const char *stall = (1 == ((creg >> 50) & 1)) ? "stalled" : "unstalled";
1290
 
1291
  /* Log the results */
1292
  printf ("  reset:        %s\n", reset);
1293
  printf ("  stall:        %s\n", stall);
1294
  printf ("  status:       0x%01x\n", status);
1295
 
1296
  if (crc_out != crc_computed)
1297
    {
1298
      printf ("  CRC mismatch\n");
1299
      printf ("    CRC out:      0x%08lx\n", crc_out);
1300
      printf ("    CRC computed: 0x%08lx\n", crc_computed);
1301
    }
1302
 
1303
  printf ("  time taken:   %.12fs\n", t);
1304
 
1305
  free (jreg);
1306
  return  1;                    /* Completed successfully */
1307
 
1308
}       /* process_read_control () */
1309
 
1310
 
1311
/* --------------------------------------------------------------------------*/
1312
/*!Main program
1313
 
1314
   Build an or1ksim program using the library which loads a program and config
1315
   from the command line and then drives JTAG.
1316
 
1317
   lib-jtag-full <config-file> <image> <jregtype> [<args>]
1318
                 [<jregtype> [<args>]] ...
1319
 
1320
   - config-file  An Or1ksim configuration file.
1321
   - image        A OpenRISC binary image to load into Or1ksim
1322
   - jregtype     One of RESET, INSTRUCTION, SELECT_MODULE, WRITE_COMMAND,
1323
                  READ_COMMAND, GO_COMMAND_WRITE, GO_COMMAND_READ,
1324
                  WRITE_CONTROL or READ_CONTROL.
1325
   - args         Arguments required by the jregtype. RESET, READ_COMMAND and
1326
                  READ_CONTROL require none.
1327
 
1328
   The target program is run in bursts of 1ms execution, and the type of
1329
   return (OK, hit breakpoint) noted. Between each burst of execution, the
1330
   JTAG interface is reset (for RESET) or the next register is submitted to
1331
   the corresponding Or1ksim JTAG interface and the resulting register noted.
1332
 
1333
   @param[in] argc  Number of elements in argv
1334
   @param[in] argv  Vector of program name and arguments
1335
 
1336
   @return  Return code for the program, zero on success.                    */
1337
/* --------------------------------------------------------------------------*/
1338
int
1339
main (int   argc,
1340
      char *argv[])
1341
{
1342 98 jeremybenn
  const double  QUANTUM = 5.0e-3;       /* Time in sec for each step. */
1343
 
1344 97 jeremybenn
  /* Check we have minimum number of args. */
1345
  if (argc < 4)
1346
    {
1347
      printf ("usage: lib-jtag <config-file> <image> <jregtype> [<args>] "
1348
              "[<jregtype> [<args>]] ...\n");
1349
      return  1;
1350
    }
1351
 
1352 220 jeremybenn
  /* Dummy argv array to pass arguments to or1ksim_init. Varies depending on
1353
     whether an image file is specified. */
1354
  int   dummy_argc;
1355
  char *dummy_argv[5];
1356
 
1357
  dummy_argv[0] = "libsim";
1358
  dummy_argv[1] = "-q";
1359
  dummy_argv[2] = "-f";
1360
  dummy_argv[3] = argv[1];
1361
  dummy_argv[4] = argv[2];
1362
 
1363
  dummy_argc = 5;
1364
 
1365 97 jeremybenn
  /* Initialize the program. Put the initialization message afterwards, or it
1366
     will get swamped by the Or1ksim header. */
1367 220 jeremybenn
  if (0 == or1ksim_init (dummy_argc, dummy_argv, NULL, NULL, NULL))
1368 97 jeremybenn
    {
1369
      printf ("Initalization succeeded.\n");
1370
    }
1371
  else
1372
    {
1373
      printf ("Initalization failed.\n");
1374
      return  1;
1375
    }
1376
 
1377 98 jeremybenn
  /* Run repeatedly for 10 milliseconds until we have processed all JTAG
1378 97 jeremybenn
     registers */
1379
  int  next_jreg = 3;                   /* Offset to next JTAG register */
1380
 
1381
  do
1382
    {
1383 98 jeremybenn
      switch (or1ksim_run (QUANTUM))
1384 97 jeremybenn
        {
1385
        case OR1KSIM_RC_OK:
1386
          printf ("Execution step completed OK.\n");
1387
          break;
1388
 
1389
        case OR1KSIM_RC_BRKPT:
1390
          printf ("Execution step completed with breakpoint.\n");
1391
          break;
1392
 
1393
        default:
1394
          printf ("ERROR: run failed.\n");
1395
          return  1;
1396
        }
1397
 
1398
      /* Process the next register appropriately, skipping any args after
1399
         processing. */
1400
      char *jregtype = argv[next_jreg++];
1401
 
1402
      if (0 == strcasecmp ("RESET", jregtype))
1403
        {
1404
          printf ("Resetting JTAG.\n");
1405
          or1ksim_jtag_reset ();
1406
        }
1407
      else if (0 == strcasecmp ("INSTRUCTION", jregtype))
1408
        {
1409
          if (process_instruction (next_jreg, argc, argv))
1410
            {
1411
              next_jreg++;              /* succeeded */
1412
            }
1413
          else
1414
            {
1415
              return  1;                /* failed */
1416
            }
1417
        }
1418
      else if (0 == strcasecmp ("SELECT_MODULE", jregtype))
1419
        {
1420
          if (process_select_module (next_jreg, argc, argv))
1421
            {
1422
              next_jreg++;              /* succeeded */
1423
            }
1424
          else
1425
            {
1426
              return  1;                /* failed */
1427
            }
1428
        }
1429
      else if (0 == strcasecmp ("WRITE_COMMAND", jregtype))
1430
        {
1431
          if (process_write_command (next_jreg, argc, argv))
1432
            {
1433
              next_jreg += 3;           /* succeeded */
1434
            }
1435
          else
1436
            {
1437
              return  1;                /* failed */
1438
            }
1439
        }
1440
      else if (0 == strcasecmp ("READ_COMMAND", jregtype))
1441
        {
1442
          if (process_read_command (next_jreg, argc, argv))
1443
            {
1444
                                        /* succeeded (no args) */
1445
            }
1446
          else
1447
            {
1448
              return  1;                /* failed */
1449
            }
1450
        }
1451
      else if (0 == strcasecmp ("GO_COMMAND_WRITE", jregtype))
1452
        {
1453
          if (process_go_command_write (next_jreg, argc, argv))
1454
            {
1455
              next_jreg++;              /* succeeded */
1456
            }
1457
          else
1458
            {
1459
              return  1;                /* failed */
1460
            }
1461
        }
1462
      else if (0 == strcasecmp ("GO_COMMAND_READ", jregtype))
1463
        {
1464
          if (process_go_command_read (next_jreg, argc, argv))
1465
            {
1466
              next_jreg++;              /* succeeded */
1467
            }
1468
          else
1469
            {
1470
              return  1;                /* failed */
1471
            }
1472
        }
1473
      else if (0 == strcasecmp ("WRITE_CONTROL", jregtype))
1474
        {
1475
          if (process_write_control (next_jreg, argc, argv))
1476
            {
1477
              next_jreg += 2;           /* succeeded */
1478
            }
1479
          else
1480
            {
1481
              return  1;                /* failed */
1482
            }
1483
        }
1484
      else if (0 == strcasecmp ("READ_CONTROL", jregtype))
1485
        {
1486
          if (process_read_control (next_jreg, argc, argv))
1487
            {
1488
                                        /* succeeded (no args) */
1489
            }
1490
          else
1491
            {
1492
              return  1;                /* failed */
1493
            }
1494
        }
1495
      else
1496
        {
1497
          printf ("ERROR: Unrecognized JTAG register '%s'.\n", jregtype);
1498
          return  1;
1499
        }
1500
    }
1501
  while (next_jreg < argc);
1502
 
1503
  /* A little longer to allow response to last upcall to be handled. */
1504 98 jeremybenn
  switch (or1ksim_run (QUANTUM))
1505 97 jeremybenn
    {
1506
    case OR1KSIM_RC_OK:
1507
      printf ("Execution step completed OK.\n");
1508
      break;
1509
 
1510
    case OR1KSIM_RC_BRKPT:
1511
      printf ("Execution step completed with breakpoint.\n");
1512
      break;
1513
 
1514
    default:
1515
      printf ("ERROR: run failed.\n");
1516
      return  1;
1517
    }
1518
 
1519
  printf ("Test completed successfully.\n");
1520
  return  0;
1521
 
1522
}       /* main () */

powered by: WebSVN 2.1.0

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