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

Subversion Repositories openrisc_me

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

Go to most recent revision | 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
   with this program.  If not, see <http:  www.gnu.org/licenses/>.  */
23
 
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
unsigned long int
50
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
  // Compute the CRC, MS bit first
64
  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
  double  t = or1ksim_jtag_shift_ir (jreg);
205
 
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
  double  t = or1ksim_jtag_shift_dr (jreg);
299
 
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
  if (len > 0xffff)
401
    {
402
      printf ("ERROR: WRITE_COMMAND length 0x%lx too large\n", len);
403
      return  0;
404
    }
405
 
406
  /* Compute the CRC */
407
  unsigned long int  crc_in;
408
 
409
  crc_in = crc32 (0,            1, 0xffffffff);
410
  crc_in = crc32 (cmd,          4, crc_in);
411
  crc_in = crc32 (access_type,  4, crc_in);
412
  crc_in = crc32 (addr,        32, crc_in);
413
  crc_in = crc32 (len,         16, crc_in);
414
 
415
  /* Reverse the fields */
416
  cmd         = reverse_bits (cmd,          4);
417
  access_type = reverse_bits (access_type,  4);
418
  addr        = reverse_bits (addr,        32);
419
  len         = reverse_bits (len,         16);
420
  crc_in      = reverse_bits (crc_in,      32);
421
 
422
  /* Allocate space and initialize the register
423
     -  1 indicator bit
424
     -  4 bits command in
425
     -  4 bits access type in
426
     - 32 bits address in
427
     - 16 bits length in
428
     - 32 bits CRC in
429
     -  4 bits status out
430
     - 32 bits CRC out
431
 
432
     Total 125 bits = 16 bytes */
433
  int            num_bytes = 16;
434
  unsigned char *jreg      = malloc (num_bytes);
435
 
436
  if (NULL == jreg)
437
    {
438
      printf ("ERROR: malloc for WRITE_COMMAND register failed.\n");
439
      return  0;
440
    }
441
 
442
  memset (jreg, 0, num_bytes);
443
 
444
  jreg[ 0]  = 0x0;
445
 
446
  jreg[ 0] |= cmd         << 1;
447
 
448
  jreg[ 0] |= access_type << 5;
449
  jreg[ 1]  = access_type >> 3;
450
 
451
  jreg[ 1] |= addr        <<  1;
452
  jreg[ 2]  = addr        >>  7;
453
  jreg[ 3]  = addr        >> 15;
454
  jreg[ 4]  = addr        >> 23;
455
  jreg[ 5]  = addr        >> 31;
456
 
457
  jreg[ 5] |= len         <<  1;
458
  jreg[ 6]  = len         >>  7;
459
  jreg[ 7]  = len         >> 15;
460
 
461
  jreg[ 7] |= crc_in      <<  1;
462
  jreg[ 8]  = crc_in      >>  7;
463
  jreg[ 9]  = crc_in      >> 15;
464
  jreg[10]  = crc_in      >> 23;
465
  jreg[11]  = crc_in      >> 31;
466
 
467
  /* Note what we are shifting in and shift it. */
468
  dump_jreg ("  shifting in", jreg, num_bytes);
469
  double  t = or1ksim_jtag_shift_dr (jreg);
470
 
471
  /* Diagnose what we are shifting out. */
472
  dump_jreg ("  shifted out", jreg, num_bytes);
473
 
474
  /* Break out fields */
475
  unsigned char      status;
476
  unsigned long int  crc_out;
477
 
478
  status = (jreg[11] >> 1) & 0xf ;
479
 
480
  crc_out = ((unsigned long int) jreg[11] >>  5) |
481
            ((unsigned long int) jreg[12] <<  3) |
482
            ((unsigned long int) jreg[13] << 11) |
483
            ((unsigned long int) jreg[14] << 19) |
484
            ((unsigned long int) jreg[15] << 27);
485
 
486
  /* Reverse the fields */
487
  status  = reverse_bits (status,  4);
488
  crc_out = reverse_bits (crc_out, 32);
489
 
490
  /* Compute our own CRC */
491
  unsigned long int  crc_computed = crc32 (status, 4, 0xffffffff);
492
 
493
  /* Log the results */
494
  printf ("  status:       0x%01x\n", status);
495
 
496
  if (crc_out != crc_computed)
497
    {
498
      printf ("  CRC mismatch\n");
499
      printf ("    CRC out:      0x%08lx\n", crc_out);
500
      printf ("    CRC computed: 0x%08lx\n", crc_computed);
501
    }
502
 
503
  printf ("  time taken:   %.12fs\n", t);
504
 
505
  free (jreg);
506
  return  1;                    /* Completed successfully */
507
 
508
}       /* process_write_command () */
509
 
510
 
511
/* --------------------------------------------------------------------------*/
512
/*!Process a JTAG READ_COMMAND debug data register
513
 
514
   Usage:
515
 
516
     READ_COMMAND
517
 
518
   There are no arguments. It is used to read back the values used in a prior
519
   WRITE_COMMAND.
520
 
521
   On return we get the access type, address, length, status register and CRC.
522
 
523
   @param[in] next_jreg  Offset into argv of the next JTAG register hex
524
                         string.
525
   @param[in] argc       argc from the main program (for checking next_jreg).
526
   @param[in] argv       argv from the main program.
527
 
528
   @return  1 (TRUE) on success, 0 (FALSE) on failure.                       */
529
/* --------------------------------------------------------------------------*/
530
static int
531
process_read_command (int   next_jreg,
532
                       int   argc,
533
                       char *argv[])
534
{
535
  printf ("Processing READ_COMMAND.\n");
536
 
537
  /* The only value on input is the READ_COMMAND command */
538
  unsigned long int  cmd         = 1;   /* READ_COMMAND */
539
 
540
  /* Compute the CRC */
541
  unsigned long int  crc_in;
542
 
543
  crc_in = crc32 (0,            1, 0xffffffff);
544
  crc_in = crc32 (cmd,          4, crc_in);
545
 
546
  /* Reverse the fields */
547
  cmd    = reverse_bits (cmd,     4);
548
  crc_in = reverse_bits (crc_in, 32);
549
 
550
  /* Allocate space and initialize the register
551
     -  1 indicator bit
552
     -  4 bits command in
553
     - 32 bits CRC in
554
     -  4 bits access type out
555
     - 32 bits address out
556
     - 16 bits length out
557
     -  4 bits status out
558
     - 32 bits CRC out
559
 
560
     Total 125 bits = 16 bytes */
561
  int            num_bytes = 16;
562
  unsigned char *jreg      = malloc (num_bytes);
563
 
564
  if (NULL == jreg)
565
    {
566
      printf ("ERROR: malloc for READ_COMMAND register failed.\n");
567
      return  0;
568
    }
569
 
570
  memset (jreg, 0, num_bytes);
571
 
572
  jreg[ 0]  = 0x0;
573
 
574
  jreg[0] |= cmd    << 1;
575
 
576
  jreg[0] |= crc_in <<  5;
577
  jreg[1]  = crc_in >>  3;
578
  jreg[2]  = crc_in >> 11;
579
  jreg[3]  = crc_in >> 19;
580
  jreg[4]  = crc_in >> 27;
581
 
582
  /* Note what we are shifting in and shift it. */
583
  dump_jreg ("  shifting in", jreg, num_bytes);
584
  double  t = or1ksim_jtag_shift_dr (jreg);
585
 
586
  /* Diagnose what we are shifting out. */
587
  dump_jreg ("  shifted out", jreg, num_bytes);
588
 
589
  /* Break out fields */
590
  unsigned char      access_type;
591
  unsigned long int  addr;
592
  unsigned long int  len;
593
  unsigned char      status;
594
  unsigned long int  crc_out;
595
 
596
  access_type = ((jreg[4] >> 5) | (jreg[5] << 3)) & 0xf ;
597
 
598
  addr        = ((unsigned long int) jreg[ 5] >>  1) |
599
                ((unsigned long int) jreg[ 6] <<  7) |
600
                ((unsigned long int) jreg[ 7] << 15) |
601
                ((unsigned long int) jreg[ 8] << 23) |
602
                ((unsigned long int) jreg[ 9] << 31);
603
 
604
  len         = ((unsigned long int)  jreg[ 9]        >>  1) |
605
                ((unsigned long int)  jreg[10]        <<  7) |
606
                ((unsigned long int) (jreg[11] & 0x1) << 15);
607
 
608
  status      = (jreg[11] >> 1) & 0xf ;
609
 
610
  crc_out     = ((unsigned long int) jreg[11] >>  5) |
611
                ((unsigned long int) jreg[12] <<  3) |
612
                ((unsigned long int) jreg[13] << 11) |
613
                ((unsigned long int) jreg[14] << 19) |
614
                ((unsigned long int) jreg[15] << 27);
615
 
616
  /* Reverse the fields */
617
 
618
  access_type = reverse_bits (access_type,  4);
619
  addr        = reverse_bits (addr,        32);
620
  len         = reverse_bits (len,         16);
621
  status      = reverse_bits (status,       4);
622
  crc_out     = reverse_bits (crc_out,     32);
623
 
624
  /* Compute our own CRC */
625
  unsigned long int  crc_computed;
626
 
627
  crc_computed = crc32 (access_type,  4, 0xffffffff);
628
  crc_computed = crc32 (addr,        32, crc_computed);
629
  crc_computed = crc32 (len,         16, crc_computed);
630
  crc_computed = crc32 (status,       4, crc_computed);
631
 
632
  /* Log the results. Remember the length is 1 greater than the value
633
     returned. */
634
  printf ("  access_type:  0x%x\n",    status);
635
  printf ("  address:      0x%lx\n",   addr);
636
  printf ("  length:       0x%lx\n",   len + 1);
637
  printf ("  status:       0x%x\n",    status);
638
 
639
  if (crc_out != crc_computed)
640
    {
641
      printf ("  CRC mismatch\n");
642
      printf ("    CRC out:      0x%08lx\n", crc_out);
643
      printf ("    CRC computed: 0x%08lx\n", crc_computed);
644
    }
645
 
646
  printf ("  time taken:   %.12fs\n", t);
647
 
648
  free (jreg);
649
  return  1;                    /* Completed successfully */
650
 
651
}       /* process_read_command () */
652
 
653
 
654
/* --------------------------------------------------------------------------*/
655
/*!Process a JTAG GO_COMMAND_WRITE debug data register
656
 
657
   Usage:
658
 
659
     GO_COMMAND_WRITE <data>
660
 
661
   The one argument is a string of bytes to be written, MS byte first.
662
 
663
   Like all the JTAG fields, each data byte must be reversed, so it is shifted
664
   MS bit first. It also requires a 32-bit CRC.
665
 
666
   On return we get a status register and CRC.
667
 
668
   @param[in] next_jreg  Offset into argv of the next JTAG register hex
669
                         string.
670
   @param[in] argc       argc from the main program (for checking next_jreg).
671
   @param[in] argv       argv from the main program.
672
 
673
   @return  1 (TRUE) on success, 0 (FALSE) on failure.                       */
674
/* --------------------------------------------------------------------------*/
675
static int
676
process_go_command_write (int   next_jreg,
677
                          int   argc,
678
                          char *argv[])
679
{
680
  printf ("Processing GO_COMMAND_WRITE.\n");
681
 
682
  /* Do we have the arg */
683
  if (next_jreg >= argc)
684
    {
685
      printf ("GO_COMMAND_WRITE usage: GO_COMMAND_WRITE <data>.\n");
686
      return  0;
687
    }
688
 
689
  /* Break out the fields, including the data string into a vector of bytes. */
690
  unsigned long int  cmd        = 0;     /* GO_COMMAND */
691
 
692
  char              *data_str   = argv[next_jreg];
693
  int                data_len   = strlen (data_str);
694
  int                data_bytes = (data_len + 1) / 2;
695
  unsigned char     *data       = malloc (data_bytes);
696
 
697
  if (NULL == data)
698
    {
699
      printf ("ERROR: data malloc for GO_COMMAND_WRITE register failed.\n");
700
      return  0;
701
    }
702
 
703
  int  i;
704
 
705
  for (i = 0; i < data_bytes; i++)
706
    {
707
      int            ch_off_ls = data_len - (i * 2) - 1;
708
      int            ch_off_ms = (0 == ch_off_ls) ? 0 : ch_off_ls - 1;
709
      int            j;
710
 
711
      /* Get each nybble in turn, remembering that we may not have a MS nybble
712
         if the data string has an odd number of chars. */
713
      data[i] = 0;
714
 
715
      for (j = ch_off_ms; j <= ch_off_ls; j++)
716
        {
717
          char  c       = data_str[j];
718
          int   dig_val = (('0' <= c) && (c <= '9')) ? c - '0' :
719
                          (('a' <= c) && (c <= 'f')) ? c - 'a' + 10 :
720
                          (('A' <= c) && (c <= 'F')) ? c - 'A' + 10 : -1;
721
 
722
          if (dig_val < 0)
723
            {
724
              printf ("ERROR: Non-hex digit in data: %c\n", c);
725
              free (data);
726
              return  0;
727
            }
728
 
729
          data[i] = (data[i] << 4) | dig_val;
730
        }
731
    }
732
 
733
  /* Are the arguments in range? Remember the length we actually put in has 1
734
     subtracted. */
735
 
736
  /* Compute the CRC */
737
  unsigned long int  crc_in;
738
 
739
  crc_in = crc32 (0,   1, 0xffffffff);
740
  crc_in = crc32 (cmd, 4, crc_in);
741
 
742
  for (i = 0; i < data_bytes; i++)
743
    {
744
      crc_in = crc32 (data[i], 8, crc_in);
745
    }
746
 
747
  /* Reverse the fields */
748
  cmd = reverse_bits (cmd, 4);
749
 
750
  for (i = 0; i < data_bytes; i++)
751
    {
752
      data[i] = reverse_bits (data[i], 8);
753
    }
754
 
755
  crc_in = reverse_bits (crc_in,  32);
756
 
757
  /* Allocate space and initialize the register
758
     -              1 indicator bit
759
     -              4 bits command in
760
     - data_bytes * 8 bits access type in
761
     -             32 bits CRC in
762
     -              4 bits status out
763
     -             32 bits CRC out
764
 
765
     Total 73 + data_bytes * 8 bits = 10 + data_bytes bytes */
766
  int            num_bytes = 10 + data_bytes;
767
  unsigned char *jreg      = malloc (num_bytes);
768
 
769
  if (NULL == jreg)
770
    {
771
      printf ("ERROR: jreg malloc for GO_COMMAND_WRITE register failed.\n");
772
      free (data);
773
      return  0;
774
    }
775
 
776
  memset (jreg, 0, num_bytes);
777
 
778
  jreg[ 0]  = 0x0;
779
  jreg[ 0] |= cmd << 1;
780
 
781
  for (i = 0; i < data_bytes; i++)
782
    {
783
      jreg[i]     |= data[i] << 5;
784
      jreg[i + 1]  = data[i] >> 3;
785
    }
786
 
787
  jreg[data_bytes    ] |= crc_in <<  5;
788
  jreg[data_bytes + 1]  = crc_in >>  3;
789
  jreg[data_bytes + 2]  = crc_in >> 11;
790
  jreg[data_bytes + 3]  = crc_in >> 19;
791
  jreg[data_bytes + 4]  = crc_in >> 27;
792
 
793
  /* Note what we are shifting in and shift it. */
794
  dump_jreg ("  shifting in", jreg, num_bytes);
795
  double  t = or1ksim_jtag_shift_dr (jreg);
796
 
797
  /* Diagnose what we are shifting out. */
798
  dump_jreg ("  shifted out", jreg, num_bytes);
799
 
800
  /* Break out fields */
801
  unsigned char      status;
802
  unsigned long int  crc_out;
803
 
804
  status = ((jreg[data_bytes + 4] >> 5) | (jreg[data_bytes + 5] << 3)) & 0xf ;
805
 
806
  crc_out = ((unsigned long int) jreg[data_bytes + 5] >>  1) |
807
            ((unsigned long int) jreg[data_bytes + 6] <<  7) |
808
            ((unsigned long int) jreg[data_bytes + 7] << 15) |
809
            ((unsigned long int) jreg[data_bytes + 8] << 23) |
810
            ((unsigned long int) jreg[data_bytes + 9] << 31);
811
 
812
  /* Reverse the fields */
813
  status  = reverse_bits (status,   4);
814
  crc_out = reverse_bits (crc_out, 32);
815
 
816
  /* Compute our own CRC */
817
  unsigned long int  crc_computed = crc32 (status, 4, 0xffffffff);
818
 
819
  /* Log the results */
820
  printf ("  status:       0x%01x\n", status);
821
 
822
  if (crc_out != crc_computed)
823
    {
824
      printf ("  CRC mismatch\n");
825
      printf ("    CRC out:      0x%08lx\n", crc_out);
826
      printf ("    CRC computed: 0x%08lx\n", crc_computed);
827
    }
828
 
829
  printf ("  time taken:   %.12fs\n", t);
830
 
831
  free (data);
832
  free (jreg);
833
  return  1;                    /* Completed successfully */
834
 
835
}       /* process_go_command_write () */
836
 
837
 
838
/* --------------------------------------------------------------------------*/
839
/*!Process a JTAG GO_COMMAND_READ debug data register
840
 
841
   Usage:
842
 
843
     GO_COMMAND_READ <length>
844
 
845
   The one argument is a length in hex, specifying the number of bytes to be
846
   read.
847
 
848
   On return we get a status register and CRC.
849
 
850
   Like all JTAG fields, the CRC shifted in, the data read back, the status
851
   and CRC shifted out, must be reversed, since they are shifted in MS bit
852
   first and out LS bit first.
853
 
854
   @param[in] next_jreg  Offset into argv of the next JTAG register hex
855
                         string.
856
   @param[in] argc       argc from the main program (for checking next_jreg).
857
   @param[in] argv       argv from the main program.
858
 
859
   @return  1 (TRUE) on success, 0 (FALSE) on failure.                       */
860
/* --------------------------------------------------------------------------*/
861
static int
862
process_go_command_read (int   next_jreg,
863
                         int   argc,
864
                         char *argv[])
865
{
866
  printf ("Processing GO_COMMAND_READ.\n");
867
 
868
  /* Do we have the args */
869
  if (next_jreg >= argc)
870
    {
871
      printf ("GO_COMMAND_READ usage: GO_COMMAND_READ <length>\n");
872
      return  0;
873
    }
874
 
875
  /* Is the argument in range? Remember the length we actually put in has 1
876
     subtracted, so although it is a 16-bit field, it can be up to 2^16. */
877
  unsigned long int  cmd        = 0;     /* GO_COMMAND */
878
  unsigned long int  data_bytes = strtoul (argv[next_jreg], NULL, 16);
879
 
880
  if (data_bytes > 0x10000)
881
    {
882
      printf ("ERROR: GO_COMMAND_READ length 0x%lx too large\n", data_bytes);
883
      return  0;
884
    }
885
 
886
  /* Compute the CRC */
887
  unsigned long int  crc_in;
888
 
889
  crc_in = crc32 (0,   1, 0xffffffff);
890
  crc_in = crc32 (cmd, 4, crc_in);
891
 
892
  /* Reverse the fields */
893
  cmd    = reverse_bits (cmd, 4);
894
  crc_in = reverse_bits (crc_in,  32);
895
 
896
  /* Allocate space and initialize the register
897
     -              1 indicator bit
898
     -              4 bits command in
899
     -             32 bits CRC in
900
     - data_bytes * 8 bits access type out
901
     -              4 bits status out
902
     -             32 bits CRC out
903
 
904
     Total 73 + data_bytes * 8 bits = 10 + data_bytes bytes */
905
  int            num_bytes = 10 + data_bytes;
906
  unsigned char *jreg      = malloc (num_bytes);
907
 
908
  if (NULL == jreg)
909
    {
910
      printf ("ERROR: malloc forGO_COMMAND_READ register failed.\n");
911
      return  0;
912
    }
913
 
914
  memset (jreg, 0, num_bytes);
915
 
916
  jreg[0]  = 0x0;
917
  jreg[0] |= cmd << 1;
918
 
919
  jreg[0] |= crc_in <<  5;
920
  jreg[1]  = crc_in >>  3;
921
  jreg[2]  = crc_in >> 11;
922
  jreg[3]  = crc_in >> 19;
923
  jreg[4]  = crc_in >> 27;
924
 
925
  /* Note what we are shifting in and shift it. */
926
  dump_jreg ("  shifting in", jreg, num_bytes);
927
  double  t = or1ksim_jtag_shift_dr (jreg);
928
 
929
  /* Diagnose what we are shifting out. */
930
  dump_jreg ("  shifted out", jreg, num_bytes);
931
 
932
  /* Break out fields */
933
  unsigned char     *data = malloc (data_bytes);
934
  unsigned char      status;
935
  unsigned long int  crc_out;
936
 
937
  if (NULL == data)
938
    {
939
      printf ("ERROR: data malloc for GO_COMMAND_WRITE register failed.\n");
940
      free (jreg);
941
      return  0;
942
    }
943
 
944
  int  i;
945
 
946
  for (i = 0; i < data_bytes; i++)
947
    {
948
      data[i] = ((jreg[i + 4] >> 5) | (jreg[i + 5] << 3)) & 0xff;
949
    }
950
 
951
  status = ((jreg[data_bytes + 4] >> 5) | (jreg[data_bytes + 5] << 3)) & 0xf ;
952
 
953
  crc_out = ((unsigned long int) jreg[data_bytes + 5] >>  1) |
954
            ((unsigned long int) jreg[data_bytes + 6] <<  7) |
955
            ((unsigned long int) jreg[data_bytes + 7] << 15) |
956
            ((unsigned long int) jreg[data_bytes + 8] << 23) |
957
            ((unsigned long int) jreg[data_bytes + 9] << 31);
958
 
959
  /* Reverse the fields */
960
  for (i = 0; i < data_bytes; i++)
961
    {
962
      data[i] = reverse_bits (data[i], 8);
963
    }
964
 
965
  status  = reverse_bits (status,   4);
966
  crc_out = reverse_bits (crc_out, 32);
967
 
968
  /* Compute our own CRC */
969
  unsigned long int  crc_computed = 0xffffffff;
970
 
971
  for (i = 0; i < data_bytes; i++)
972
    {
973
      crc_computed = crc32 (data[i], 8, crc_computed);
974
    }
975
 
976
  crc_computed = crc32 (status, 4, crc_computed);
977
 
978
  /* Log the results, ignoring a leading zero on the MS byte */
979
  printf ("  data:         0x%x", data[data_bytes - 1]);
980
 
981
  for (i = data_bytes - 2; i >= 0; i--)
982
    {
983
      printf ("%02x", data[i]);
984
    }
985
 
986
  printf ("\n");
987
  printf ("  status:       0x%01x\n", status);
988
 
989
  if (crc_out != crc_computed)
990
    {
991
      printf ("  CRC mismatch\n");
992
      printf ("    CRC out:      0x%08lx\n", crc_out);
993
      printf ("    CRC computed: 0x%08lx\n", crc_computed);
994
    }
995
 
996
  printf ("  time taken:   %.12fs\n", t);
997
 
998
  free (data);
999
  free (jreg);
1000
  return  1;                    /* Completed successfully */
1001
 
1002
}       /* process_go_command_read () */
1003
 
1004
 
1005
/* --------------------------------------------------------------------------*/
1006
/*!Process a JTAG WRITE_CONTROL debug data register
1007
 
1008
   Usage:
1009
 
1010
     WRITE_CONTROL <reset> <stall>
1011
 
1012
   The arguments should be either zero or one.
1013
 
1014
   The arguments are used to construct the 52-bit CPU control register. Like
1015
   all JTAG fields, it must be reversed, so it is shifted MS bit first. It
1016
   also requires a 32-bit CRC.
1017
 
1018
   On return we get a status register and CRC.
1019
 
1020
   @param[in] next_jreg  Offset into argv of the next JTAG register hex
1021
                         string.
1022
   @param[in] argc       argc from the main program (for checking next_jreg).
1023
   @param[in] argv       argv from the main program.
1024
 
1025
   @return  1 (TRUE) on success, 0 (FALSE) on failure.                       */
1026
/* --------------------------------------------------------------------------*/
1027
static int
1028
process_write_control (int   next_jreg,
1029
                       int   argc,
1030
                       char *argv[])
1031
{
1032
  printf ("Processing WRITE_CONTROL.\n");
1033
 
1034
  /* Do we have the args */
1035
  if (next_jreg + 2 > argc)
1036
    {
1037
      printf ("WRITE_CONTROL usage: WRITE_CONTROL <reset> <status>\n");
1038
      return  0;
1039
    }
1040
 
1041
  /* Are the arguments in range? */
1042
  unsigned long int  cmd   = 4;         /* WRITE_CONTROL */
1043
 
1044
  unsigned long int  reset = strtoul (argv[next_jreg    ], NULL, 16);
1045
  unsigned long int  stall = strtoul (argv[next_jreg + 1], NULL, 16);
1046
 
1047
  if (reset > 0x1)
1048
    {
1049
      printf ("ERROR: invalid WRITE_CONTROL reset value 0x%lx.\n", reset);
1050
      return  0;
1051
    }
1052
 
1053
  if (stall > 0x1)
1054
    {
1055
      printf ("ERROR: invalid WRITE_CONTROL stall value 0x%lx.\n", stall);
1056
      return  0;
1057
    }
1058
 
1059
  /* Construct the control register */
1060
  unsigned long long int  creg = ((unsigned long long int) reset << 51) |
1061
                                 ((unsigned long long int) stall << 50);
1062
 
1063
  /* Compute the CRC */
1064
  unsigned long int  crc_in;
1065
 
1066
  crc_in = crc32 (0,     1, 0xffffffff);
1067
  crc_in = crc32 (cmd,   4, crc_in);
1068
  crc_in = crc32 (creg, 52, crc_in);
1069
 
1070
  /* Reverse the fields */
1071
  cmd    = reverse_bits (cmd,     4);
1072
  creg   = reverse_bits (creg,   52);
1073
  crc_in = reverse_bits (crc_in, 32);
1074
 
1075
  /* Allocate space and initialize the register
1076
     -  1 indicator bit
1077
     -  4 bits command in
1078
     - 52 bits control register
1079
     - 32 bits CRC in
1080
     -  4 bits status out
1081
     - 32 bits CRC out
1082
 
1083
     Total 125 bits = 16 bytes */
1084
  int            num_bytes = 16;
1085
  unsigned char *jreg      = malloc (num_bytes);
1086
 
1087
  if (NULL == jreg)
1088
    {
1089
      printf ("ERROR: malloc for WRITE_CONTROL register failed.\n");
1090
      return  0;
1091
    }
1092
 
1093
  memset (jreg, 0, num_bytes);
1094
 
1095
  jreg[ 0]  = 0x0;
1096
 
1097
  jreg[ 0] |= cmd    <<  1;
1098
 
1099
  jreg[ 0] |= creg   <<  5;
1100
  jreg[ 1]  = creg   >>  3;
1101
  jreg[ 2]  = creg   >> 11;
1102
  jreg[ 3]  = creg   >> 19;
1103
  jreg[ 4]  = creg   >> 27;
1104
  jreg[ 5]  = creg   >> 35;
1105
  jreg[ 6]  = creg   >> 43;
1106
  jreg[ 7]  = creg   >> 51;
1107
 
1108
  jreg[ 7] |= crc_in      <<  1;
1109
  jreg[ 8]  = crc_in      >>  7;
1110
  jreg[ 9]  = crc_in      >> 15;
1111
  jreg[10]  = crc_in      >> 23;
1112
  jreg[11]  = crc_in      >> 31;
1113
 
1114
  /* Note what we are shifting in and shift it. */
1115
  dump_jreg ("  shifting in", jreg, num_bytes);
1116
  double  t = or1ksim_jtag_shift_dr (jreg);
1117
 
1118
  /* Diagnose what we are shifting out. */
1119
  dump_jreg ("  shifted out", jreg, num_bytes);
1120
 
1121
  /* Break out fields */
1122
  unsigned char      status;
1123
  unsigned long int  crc_out;
1124
 
1125
  status  = (jreg[11] >> 1) & 0xf ;
1126
 
1127
  crc_out = ((unsigned long int) jreg[11] >>  5) |
1128
            ((unsigned long int) jreg[12] <<  3) |
1129
            ((unsigned long int) jreg[13] << 11) |
1130
            ((unsigned long int) jreg[14] << 19) |
1131
            ((unsigned long int) jreg[15] << 27);
1132
 
1133
  /* Reverse the fields */
1134
  status  = reverse_bits (status,  4);
1135
  crc_out = reverse_bits (crc_out, 32);
1136
 
1137
  /* Compute our own CRC */
1138
  unsigned long int  crc_computed = crc32 (status, 4, 0xffffffff);
1139
 
1140
  /* Log the results */
1141
  printf ("  status:       0x%01x\n", status);
1142
 
1143
  if (crc_out != crc_computed)
1144
    {
1145
      printf ("  CRC mismatch\n");
1146
      printf ("    CRC out:      0x%08lx\n", crc_out);
1147
      printf ("    CRC computed: 0x%08lx\n", crc_computed);
1148
    }
1149
 
1150
  printf ("  time taken:   %.12fs\n", t);
1151
 
1152
  free (jreg);
1153
  return  1;                    /* Completed successfully */
1154
 
1155
}       /* process_write_control () */
1156
 
1157
 
1158
/* --------------------------------------------------------------------------*/
1159
/*!Process a JTAG READ_CONTROL debug data register
1160
 
1161
   Usage:
1162
 
1163
     READ_CONTROL
1164
 
1165
   There are no arguments. It requires a 32-bit CRC.
1166
 
1167
   On return we get the control register, status and CRC.
1168
 
1169
   Like all the JTAG fields, they must be reversed, as resutl is shifted out
1170
   LS bit first.
1171
 
1172
   @param[in] next_jreg  Offset into argv of the next JTAG register hex
1173
                         string.
1174
   @param[in] argc       argc from the main program (for checking next_jreg).
1175
   @param[in] argv       argv from the main program.
1176
 
1177
   @return  1 (TRUE) on success, 0 (FALSE) on failure.                       */
1178
/* --------------------------------------------------------------------------*/
1179
static int
1180
process_read_control (int   next_jreg,
1181
                       int   argc,
1182
                       char *argv[])
1183
{
1184
  printf ("Processing READ_CONTROL.\n");
1185
 
1186
  /* Only input field is cmd. */
1187
  unsigned long int  cmd   = 3;         /* READ_CONTROL */
1188
 
1189
  /* Compute the CRC */
1190
  unsigned long int  crc_in;
1191
 
1192
  crc_in = crc32 (0,     1, 0xffffffff);
1193
  crc_in = crc32 (cmd,   4, crc_in);
1194
 
1195
  /* Reverse the fields */
1196
  cmd    = reverse_bits (cmd,     4);
1197
  crc_in = reverse_bits (crc_in, 32);
1198
 
1199
  /* Allocate space and initialize the register
1200
     -  1 indicator bit
1201
     -  4 bits command in
1202
     - 32 bits CRC in
1203
     - 52 bits control register out
1204
     -  4 bits status out
1205
     - 32 bits CRC out
1206
 
1207
     Total 125 bits = 16 bytes */
1208
  int            num_bytes = 16;
1209
  unsigned char *jreg      = malloc (num_bytes);
1210
 
1211
  if (NULL == jreg)
1212
    {
1213
      printf ("ERROR: malloc for READ_CONTROL register failed.\n");
1214
      return  0;
1215
    }
1216
 
1217
  memset (jreg, 0, num_bytes);
1218
 
1219
  jreg[0]  = 0x0;
1220
 
1221
  jreg[0] |= cmd    <<  1;
1222
 
1223
  jreg[0] |= crc_in <<  5;
1224
  jreg[1]  = crc_in >>  3;
1225
  jreg[2]  = crc_in >> 11;
1226
  jreg[3]  = crc_in >> 19;
1227
  jreg[4]  = crc_in >> 27;
1228
 
1229
  /* Note what we are shifting in and shift it. */
1230
  dump_jreg ("  shifting in", jreg, num_bytes);
1231
  double  t = or1ksim_jtag_shift_dr (jreg);
1232
 
1233
  /* Diagnose what we are shifting out. */
1234
  dump_jreg ("  shifted out", jreg, num_bytes);
1235
 
1236
  /* Break out fields */
1237
  unsigned long long int  creg;
1238
  unsigned char           status;
1239
  unsigned long int       crc_out;
1240
 
1241
  creg    = ((unsigned long long int)  jreg[ 4]        >>  5) |
1242
            ((unsigned long long int)  jreg[ 5]        <<  3) |
1243
            ((unsigned long long int)  jreg[ 6]        << 11) |
1244
            ((unsigned long long int)  jreg[ 7]        << 19) |
1245
            ((unsigned long long int)  jreg[ 8]        << 27) |
1246
            ((unsigned long long int)  jreg[ 9]        << 35) |
1247
            ((unsigned long long int)  jreg[10]        << 43) |
1248
            ((unsigned long long int) (jreg[11] & 0x1) << 51);
1249
 
1250
  status  = (jreg[11] >> 1) & 0xf ;
1251
 
1252
  crc_out = ((unsigned long int) jreg[11] >>  5) |
1253
            ((unsigned long int) jreg[12] <<  3) |
1254
            ((unsigned long int) jreg[13] << 11) |
1255
            ((unsigned long int) jreg[14] << 19) |
1256
            ((unsigned long int) jreg[15] << 27);
1257
 
1258
  /* Reverse the fields */
1259
  creg    = reverse_bits (creg,    52);
1260
  status  = reverse_bits (status,   4);
1261
  crc_out = reverse_bits (crc_out, 32);
1262
 
1263
  /* Compute our own CRC */
1264
  unsigned long int  crc_computed;
1265
 
1266
  crc_computed = crc32 (creg,   52, 0xffffffff);
1267
  crc_computed = crc32 (status,  4, crc_computed);
1268
 
1269
  const char *reset = (1 == ((creg >> 51) & 1)) ? "enabled" : "disabled";
1270
  const char *stall = (1 == ((creg >> 50) & 1)) ? "stalled" : "unstalled";
1271
 
1272
  /* Log the results */
1273
  printf ("  reset:        %s\n", reset);
1274
  printf ("  stall:        %s\n", stall);
1275
  printf ("  status:       0x%01x\n", status);
1276
 
1277
  if (crc_out != crc_computed)
1278
    {
1279
      printf ("  CRC mismatch\n");
1280
      printf ("    CRC out:      0x%08lx\n", crc_out);
1281
      printf ("    CRC computed: 0x%08lx\n", crc_computed);
1282
    }
1283
 
1284
  printf ("  time taken:   %.12fs\n", t);
1285
 
1286
  free (jreg);
1287
  return  1;                    /* Completed successfully */
1288
 
1289
}       /* process_read_control () */
1290
 
1291
 
1292
/* --------------------------------------------------------------------------*/
1293
/*!Main program
1294
 
1295
   Build an or1ksim program using the library which loads a program and config
1296
   from the command line and then drives JTAG.
1297
 
1298
   lib-jtag-full <config-file> <image> <jregtype> [<args>]
1299
                 [<jregtype> [<args>]] ...
1300
 
1301
   - config-file  An Or1ksim configuration file.
1302
   - image        A OpenRISC binary image to load into Or1ksim
1303
   - jregtype     One of RESET, INSTRUCTION, SELECT_MODULE, WRITE_COMMAND,
1304
                  READ_COMMAND, GO_COMMAND_WRITE, GO_COMMAND_READ,
1305
                  WRITE_CONTROL or READ_CONTROL.
1306
   - args         Arguments required by the jregtype. RESET, READ_COMMAND and
1307
                  READ_CONTROL require none.
1308
 
1309
   The target program is run in bursts of 1ms execution, and the type of
1310
   return (OK, hit breakpoint) noted. Between each burst of execution, the
1311
   JTAG interface is reset (for RESET) or the next register is submitted to
1312
   the corresponding Or1ksim JTAG interface and the resulting register noted.
1313
 
1314
   @param[in] argc  Number of elements in argv
1315
   @param[in] argv  Vector of program name and arguments
1316
 
1317
   @return  Return code for the program, zero on success.                    */
1318
/* --------------------------------------------------------------------------*/
1319
int
1320
main (int   argc,
1321
      char *argv[])
1322
{
1323
  /* Check we have minimum number of args. */
1324
  if (argc < 4)
1325
    {
1326
      printf ("usage: lib-jtag <config-file> <image> <jregtype> [<args>] "
1327
              "[<jregtype> [<args>]] ...\n");
1328
      return  1;
1329
    }
1330
 
1331
  /* Initialize the program. Put the initialization message afterwards, or it
1332
     will get swamped by the Or1ksim header. */
1333
  if (0 == or1ksim_init (argv[1], argv[2], NULL, NULL, NULL))
1334
    {
1335
      printf ("Initalization succeeded.\n");
1336
    }
1337
  else
1338
    {
1339
      printf ("Initalization failed.\n");
1340
      return  1;
1341
    }
1342
 
1343
  /* Run repeatedly for 1 millisecond until we have processed all JTAG
1344
     registers */
1345
  int  next_jreg = 3;                   /* Offset to next JTAG register */
1346
 
1347
  do
1348
    {
1349
      switch (or1ksim_run (1.0e-3))
1350
        {
1351
        case OR1KSIM_RC_OK:
1352
          printf ("Execution step completed OK.\n");
1353
          break;
1354
 
1355
        case OR1KSIM_RC_BRKPT:
1356
          printf ("Execution step completed with breakpoint.\n");
1357
          break;
1358
 
1359
        default:
1360
          printf ("ERROR: run failed.\n");
1361
          return  1;
1362
        }
1363
 
1364
      /* Process the next register appropriately, skipping any args after
1365
         processing. */
1366
      char *jregtype = argv[next_jreg++];
1367
 
1368
      if (0 == strcasecmp ("RESET", jregtype))
1369
        {
1370
          printf ("Resetting JTAG.\n");
1371
          or1ksim_jtag_reset ();
1372
        }
1373
      else if (0 == strcasecmp ("INSTRUCTION", jregtype))
1374
        {
1375
          if (process_instruction (next_jreg, argc, argv))
1376
            {
1377
              next_jreg++;              /* succeeded */
1378
            }
1379
          else
1380
            {
1381
              return  1;                /* failed */
1382
            }
1383
        }
1384
      else if (0 == strcasecmp ("SELECT_MODULE", jregtype))
1385
        {
1386
          if (process_select_module (next_jreg, argc, argv))
1387
            {
1388
              next_jreg++;              /* succeeded */
1389
            }
1390
          else
1391
            {
1392
              return  1;                /* failed */
1393
            }
1394
        }
1395
      else if (0 == strcasecmp ("WRITE_COMMAND", jregtype))
1396
        {
1397
          if (process_write_command (next_jreg, argc, argv))
1398
            {
1399
              next_jreg += 3;           /* succeeded */
1400
            }
1401
          else
1402
            {
1403
              return  1;                /* failed */
1404
            }
1405
        }
1406
      else if (0 == strcasecmp ("READ_COMMAND", jregtype))
1407
        {
1408
          if (process_read_command (next_jreg, argc, argv))
1409
            {
1410
                                        /* succeeded (no args) */
1411
            }
1412
          else
1413
            {
1414
              return  1;                /* failed */
1415
            }
1416
        }
1417
      else if (0 == strcasecmp ("GO_COMMAND_WRITE", jregtype))
1418
        {
1419
          if (process_go_command_write (next_jreg, argc, argv))
1420
            {
1421
              next_jreg++;              /* succeeded */
1422
            }
1423
          else
1424
            {
1425
              return  1;                /* failed */
1426
            }
1427
        }
1428
      else if (0 == strcasecmp ("GO_COMMAND_READ", jregtype))
1429
        {
1430
          if (process_go_command_read (next_jreg, argc, argv))
1431
            {
1432
              next_jreg++;              /* succeeded */
1433
            }
1434
          else
1435
            {
1436
              return  1;                /* failed */
1437
            }
1438
        }
1439
      else if (0 == strcasecmp ("WRITE_CONTROL", jregtype))
1440
        {
1441
          if (process_write_control (next_jreg, argc, argv))
1442
            {
1443
              next_jreg += 2;           /* succeeded */
1444
            }
1445
          else
1446
            {
1447
              return  1;                /* failed */
1448
            }
1449
        }
1450
      else if (0 == strcasecmp ("READ_CONTROL", jregtype))
1451
        {
1452
          if (process_read_control (next_jreg, argc, argv))
1453
            {
1454
                                        /* succeeded (no args) */
1455
            }
1456
          else
1457
            {
1458
              return  1;                /* failed */
1459
            }
1460
        }
1461
      else
1462
        {
1463
          printf ("ERROR: Unrecognized JTAG register '%s'.\n", jregtype);
1464
          return  1;
1465
        }
1466
    }
1467
  while (next_jreg < argc);
1468
 
1469
  /* A little longer to allow response to last upcall to be handled. */
1470
  switch (or1ksim_run (1.0e-3))
1471
    {
1472
    case OR1KSIM_RC_OK:
1473
      printf ("Execution step completed OK.\n");
1474
      break;
1475
 
1476
    case OR1KSIM_RC_BRKPT:
1477
      printf ("Execution step completed with breakpoint.\n");
1478
      break;
1479
 
1480
    default:
1481
      printf ("ERROR: run failed.\n");
1482
      return  1;
1483
    }
1484
 
1485
  printf ("Test completed successfully.\n");
1486
  return  0;
1487
 
1488
}       /* main () */

powered by: WebSVN 2.1.0

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