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

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [or1ksim/] [or1ksim-0.5.0rc1/] [debug/] [jtag.c] - Blame information for rev 97

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 82 jeremybenn
/* jtag.c -- JTAG modeling
2
 
3
   Copyright (C) 2008 Embecosm Limited
4
 
5
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
6
 
7
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
8
 
9
   This program is free software; you can redistribute it and/or modify it
10
   under the terms of the GNU General Public License as published by the Free
11
   Software Foundation; either version 3 of the License, or (at your option)
12
   any later version.
13
 
14
   This program is distributed in the hope that it will be useful, but WITHOUT
15
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17
   more details.
18
 
19
   You should have received a copy of the GNU General Public License along
20
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
 
22
/* This program is commented throughout in a fashion suitable for processing
23
   with Doxygen. */
24
 
25
 
26
/* Autoconf and/or portability configuration */
27
#include "config.h"
28
#include "port.h"
29
 
30
/* System includes */
31
#include <stdlib.h>
32
#include <unistd.h>
33
#include <stddef.h>
34
#include <stdint.h>
35
#include <string.h>
36
 
37
/* Package includes */
38
#include "sim-config.h"
39
#include "sprs.h"
40
#include "debug-unit.h"
41
#include "spr-defs.h"
42
#include "jtag.h"
43
#include "abstract.h"
44
#include "toplevel-support.h"
45
 
46
 
47
/*---------------------------------------------------------------------------*/
48
/*!Calculate an ongoing 32-bit CRC for a value
49
 
50
   Utility function. This is for a CRC, where the value is presented in normal
51
   order (i.e its most significant bit is at the most significant positions,
52
   it has not been reversed for incorporating ina JTAG register).
53
 
54
   The function can be used for values with more than 64 bits, but will treat
55
   the supplied value as the most-significant 64 bits, with all other bits
56
   zero. In practice this is only of use when shifting in a large number of
57
   zeros.
58
 
59
   The CRC used is the IEEE 802.3 32-bit CRC using the polynomial:
60
 
61
   x^32 + x^26 + x^23 +x^22 +x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 +
62
   x^4 + x^2 + x^1 + 1
63
 
64
   In this implementation, the CRC is initialized to all ones (outside this
65
   function), to catch leading zeros.
66
 
67
   The function is designed to be used in a continuing calculation if
68
   desired. It takes a partially computed CRC and shifts in the appropriate
69
   new bits for the value supplied. For a new calculation, this value should
70
   be 0xffffffff.
71
 
72
  @param[in] value     The number whose CRC is wanted (MSB first)
73
  @param[in] num_bits  The number of bits to use from value
74
  @param[in] crc_in    The value of the CRC computed so far
75
 
76
  @return  The updated CRC value                                             */
77
/*---------------------------------------------------------------------------*/
78
static uint32_t
79
crc32 (uint64_t  value,
80
       int       num_bits,
81
       uint32_t  crc_in)
82
{
83
  static const uint32_t  CRC32_POLY = 0x04c11db7;
84
 
85
  /* Incorporate the bits, MS bit first. */
86
  int  i;
87
 
88
  for (i = num_bits - 1; i >= 0; i--)
89
    {
90
      uint32_t  d = (1 == ((value >> i) & 1))   ? 0xfffffff : 0x0000000;
91
      uint32_t  t = (1 == ((crc_in >> 31) & 1)) ? 0xfffffff : 0x0000000;
92
 
93
      crc_in <<= 1;
94
      crc_in  ^= (d ^ t) & CRC32_POLY;
95
    }
96
 
97
  return  crc_in;
98
 
99
}       /* crc32 () */
100
 
101
 
102
/*---------------------------------------------------------------------------*/
103
/*!Reverse up to 64 bits in a word
104
 
105
   Utility function. Uses the "reverse" algorithm from the University of
106
   Kentucky Aggregate Magic Algorithms.
107
 
108
   @param[in] val      The long word to reverse
109
   @param[in] numBits  Number of bits to reverse
110
 
111
   @return  The reversed word.                                               */
112
/*---------------------------------------------------------------------------*/
113
static uint64_t
114
reverse_bits (uint64_t  val,
115
              int       numBits)
116
{
117
  val = (((val & 0xaaaaaaaaaaaaaaaaULL) >>  1) |
118
         ((val & 0x5555555555555555ULL) <<  1));
119
  val = (((val & 0xccccccccccccccccULL) >>  2) |
120
         ((val & 0x3333333333333333ULL) <<  2));
121
  val = (((val & 0xf0f0f0f0f0f0f0f0ULL) >>  4) |
122
         ((val & 0x0f0f0f0f0f0f0f0fULL) <<  4));
123
  val = (((val & 0xff00ff00ff00ff00ULL) >>  8) |
124
         ((val & 0x00ff00ff00ff00ffULL) <<  8));
125
  val = (((val & 0xffff0000ffff0000ULL) >> 16) |
126
         ((val & 0x00000fff0000ffffULL) << 16));
127
  val = ((val >> 32) | (val << 32));
128
 
129
  return  val >> (64 - numBits);        /* Only the bits we want */
130
 
131
}       /* reverse_bits () */
132
 
133
 
134
/*---------------------------------------------------------------------------*/
135
/*!Reverse a byte
136
 
137
   Utility function. Uses the "reverse" algorithm from the University of
138
   Kentucky Aggregate Magic Algorithms.
139
 
140
   @param[in] byte  The byte to reverse
141
 
142
   @return  The reversed byte.                                               */
143
/*---------------------------------------------------------------------------*/
144
static uint8_t
145
reverse_byte (uint8_t  byte)
146
{
147
  byte = (((byte & 0xaa) >> 1) | ((byte & 0x55) << 1));
148
  byte = (((byte & 0xcc) >> 2) | ((byte & 0x33) << 2));
149
 
150
  return ((byte >> 4) | (byte << 4));
151
 
152
}       /* reverse_byte () */
153
 
154
 
155
/*---------------------------------------------------------------------------*/
156
/*!Construct a response register to shift out.
157
 
158
   The generic format is:
159
 
160
          +-------+--------+---------+---------+
161
          |       |        |         |         |
162
   TDI -> |  CRC  | Status |  XX..XX |  00..00 | -> TDO
163
          |       |        |         |         |
164
          +-------+--------+---------+---------+
165
              32       4    "ignored"   "zero"
166
             bits    bits      bits      bits
167
 
168
 
169
   Fields are always shifted in MS bit first, so must be reversed. The CRC is
170
   on the 4 status bits. Only the "zero" bits are zeroed. This allows for
171
   other fields to be specified in the "ignored" bits region, which are
172
   guaranteed to be left untouched.
173
 
174
   @param[out] jreg       The buffer for the constructed register
175
   @param[in]  status     The status bits
176
   @param[in]  crc_in     CRC computed so far (set to 0xffffffff if none)
177
   @param[in]  ign_bits   The number of bits to ignore
178
   @param[in]  zero_bits  The number of bits to zero
179
 
180
   @return  The register length in bits                                      */
181
/*---------------------------------------------------------------------------*/
182
static int
183
construct_response (unsigned char *jreg,
184
                    uint8_t        status,
185
                    uint32_t       crc_in,
186
                    unsigned int   ign_bits,
187
                    unsigned int   zero_bits)
188
{
189
  /* Construct the outgoing CRC */
190
  uint32_t  crc_out = crc32 (status, 4, crc_in);
191
 
192
  /* Reversed versions of fields ready for insertion */
193
  uint8_t   status_r  = reverse_bits (status, 4);
194
  uint32_t  crc_out_r = reverse_bits (crc_out, 32);
195
 
196
  /* Construct the response register */
197
  unsigned int  zero_bytes = zero_bits / 8;
198
  unsigned int  zero_off   = zero_bits % 8;
199
 
200
  /* Clear the zero bits */
201
  if (zero_bytes > 0)
202
    {
203
      memset (jreg, 0, zero_bytes);
204
    }
205
 
206
  jreg[zero_bytes] >>= zero_off;
207
  jreg[zero_bytes] <<= zero_off;
208
 
209
  /* Determine how much to skip in total */
210 97 jeremybenn
  unsigned int  skip_bytes = (ign_bits + zero_bits) / 8;
211
  unsigned int  bit_off    = (ign_bits + zero_bits) % 8;
212 82 jeremybenn
 
213
  /* Simplify by dealing separately with two cases:
214
     - the bit offset is less than or equal to 4, so the status goes in the
215
       first free byte, with some CRC.
216
     - the bit offset is greater than 4 but less than 8, so the status goes in
217
       the first and second free bytes.
218
 
219
     For completeness we deal with what should be the impossible case of
220
     bit_off > 7. */
221
  if (bit_off <= 4)
222
    {
223
      /* Note that this works even if bit_off == 4, since there will be no CRC
224
         remaining to OR into the first byte. */
225
      jreg[skip_bytes]     |=  ((status_r & 0xf) << bit_off) |
226
                              ((crc_out_r << (4 + bit_off)) & 0xff);
227
      jreg[skip_bytes + 1]  =  (crc_out_r >> ( 4 - bit_off)) & 0xff;
228
      jreg[skip_bytes + 2]  =  (crc_out_r >> (12 - bit_off)) & 0xff;
229
      jreg[skip_bytes + 3]  =  (crc_out_r >> (20 - bit_off)) & 0xff;
230
      jreg[skip_bytes + 4]  =  (crc_out_r >> (28 - bit_off)) & 0xff;
231
    }
232
  else if (bit_off < 8)
233
    {
234
      jreg[skip_bytes]     |=   status_r << bit_off;
235
      jreg[skip_bytes + 1]  =  (status_r >> (8 - bit_off)) |
236
                              ((crc_out_r << (bit_off - 4)) & 0xff);
237
      jreg[skip_bytes + 2]  =  (crc_out_r >> (12 - bit_off)) & 0xff;
238
      jreg[skip_bytes + 3]  =  (crc_out_r >> (20 - bit_off)) & 0xff;
239
      jreg[skip_bytes + 4]  =  (crc_out_r >> (28 - bit_off)) & 0xff;
240
      jreg[skip_bytes + 5]  =  (crc_out_r >> (36 - bit_off)) & 0xff;
241
    }
242
  else
243
    {
244
      fprintf (stderr, "*** ABORT ***: construct_response: impossible bit "
245
               "offset: %u\n", bit_off);
246
      abort ();
247
    }
248
 
249
  /* Result is the register length in bits */
250
  return  32 + 4 + skip_bytes;
251
 
252
}       /* construct_response () */
253
 
254
 
255
/*---------------------------------------------------------------------------*/
256
/*!Select a module for debug
257
 
258
   Process a module selection register. The format is:
259
 
260
          +---------+-------+--------+---+
261
          |         |       |        |   |
262
   TDI -> | Ignored |  CRC  | Module | 1 | -> TDO
263
          |         |       |   ID   |   |
264
          +---------+-------+--------+---+
265
              36        32       4
266
             bits      bits    bits
267
 
268
   The returned register has the format:
269
 
270
          +-------+--------+---------+
271
          |       |        |         |
272
   TDI -> |  CRC  | Status |  00..00 | -> TDO
273
          |       |        |         |
274
          +-------+--------+---------+
275
              32       4        37
276
             bits    bits      bits
277
 
278
 
279
   Fields are always shifted in MS bit first, so must be reversed. The CRC in
280
   is computed on the first 5 bits, the CRC out on the 4 status bits.
281
 
282
   @param[in,out] jreg  The register to shift in, and the register shifted
283
                        back out.
284
 
285
   @return  The number of cycles the shift took, which in turn is the number
286
            of bits in the register                                          */
287
/*---------------------------------------------------------------------------*/
288
static int
289
module_select (unsigned char *jreg)
290
{
291
  /* Break out the fields */
292
  uint8_t   mod_id = reverse_bits ((jreg[0] >> 1) & 0xf, 4);
293 97 jeremybenn
  uint32_t  crc_in = reverse_bits (((uint32_t ) (jreg[0] & 0xe0) >>  5) |
294 82 jeremybenn
                                   ((uint32_t )  jreg[1]         <<  3) |
295
                                   ((uint32_t )  jreg[2]         << 11) |
296
                                   ((uint32_t )  jreg[3]         << 19) |
297
                                   ((uint32_t ) (jreg[4] & 0x1f) << 27), 32);
298
 
299
  /* Compute the expected CRC */
300
  uint32_t  crc_computed;
301
 
302
  crc_computed = crc32 (1,      1, 0xffffffff);
303
  crc_computed = crc32 (mod_id, 4, crc_computed);
304
 
305
  /* Status flags */
306
  enum jtag_status  status = JS_OK;
307
 
308
  /* Validate the CRC */
309
  if (crc_computed != crc_in)
310
    {
311
      /* Mismatch: record the error */
312
      status |= JS_CRC_IN_ERROR;
313
    }
314
  else
315
    {
316
      /* Is it a valid module? */
317
      switch (mod_id)
318
        {
319
        case JM_WISHBONE:
320
        case JM_CPU0:
321
        case JM_CPU1:
322
          /* All valid. Record the module */
323
          runtime.debug.mod_id = mod_id;
324
 
325
          break;
326
 
327
        default:
328
          /* Bad module: record the error */
329
          status |= JS_MODULE_MISSING;
330
          break;
331
        }
332
    }
333
 
334
  /* Construct the outgoing register and return the JTAG cycles taken (the
335
     register length) */
336
  return  construct_response (jreg, status, 0xffffffff, 0, 37);
337
 
338
}       /* module_select */
339
 
340
 
341
/*---------------------------------------------------------------------------*/
342
/*!Validate WRITE_COMMAND fields for WishBone
343
 
344
   Check that a WRITE_COMMAND's fields are valid for WishBone access.
345
 
346
   - 16 and 32-bit access must be correctly aligned. If not a warning is
347
     printed and validation fails.
348
 
349
   - size must be a multiple of 2 for 16-bit access, and a multiple of 4 for
350
     32-bit access
351
 
352
   Validation is carried out when executing a GO_COMMAND, rather than when
353
   setting the WRITE_COMMAND, because only the GO_COMMAND can set the status
354
   error if there is a problem.
355
 
356
   Warning messages are printed to explain any validation problems.
357
 
358
   @todo Should multiple SPR accesses be allowed in a single access?
359
 
360
   @note The size of the data is one greater than the length specified in the
361
         original WRITE_COMMAND.
362
 
363
   @return  1 (TRUE) if validation is OK, 0 (FALSE) if validation fails.     */
364
/*---------------------------------------------------------------------------*/
365
static int
366
validate_wb_fields ()
367
{
368
  /* Determine the size of the access */
369
  uint32_t  access_bits;
370
 
371
  switch (runtime.debug.acc_type)
372
    {
373
    case JAT_WRITE8:
374
    case JAT_READ8:
375
      access_bits =  8;
376
      break;
377
 
378
    case JAT_WRITE16:
379
    case JAT_READ16:
380
      access_bits = 16;
381
      break;
382
 
383
    case JAT_WRITE32:
384
    case JAT_READ32:
385
      access_bits = 32;
386
      break;
387
 
388
    default:
389 97 jeremybenn
      fprintf (stderr, "Warning: validate_wb_fields: unknown access "
390 82 jeremybenn
               "type %u\n", runtime.debug.acc_type);
391 97 jeremybenn
      return  0;
392 82 jeremybenn
    }
393
 
394
  /* Check for validity. This works for 8-bit, although the tests will always
395
     pass. */
396
  uint32_t  access_bytes = access_bits / 8;
397
 
398
  if (0 != (runtime.debug.addr % access_bytes))
399
    {
400
      fprintf (stderr, "Warning: JTAG WishBone %d-bit access must be %d-byte "
401
               "aligned\n", access_bits, access_bytes);
402
      return  0;
403
    }
404
  else if (0 != (runtime.debug.size % access_bytes))
405
    {
406
      fprintf (stderr, "Warning: JTAG %d-bit WishBone access must be multiple "
407
               "of %d bytes in length\n", access_bits, access_bytes);
408
      return  0;
409
    }
410
  else
411
    {
412
      return  1;                        /* No problems */
413
    }
414
}       /* validate_wb_fields () */
415
 
416
 
417
/*---------------------------------------------------------------------------*/
418
/*!Read WishBone data
419
 
420
   Read memory from WishBone. The WRITE_COMMAND address is updated to reflect
421
   the completed read if successful.
422
 
423
   The result is the CRC of the data read. The status flag is supplied as a
424
   pointer, since this can also be updated if there is a problem reading the
425
   data.
426
 
427
   @note The size of the data is one greater than the length specified in the
428
         original WRITE_COMMAND.
429
 
430
   @todo For now we always read a byte a time. In the future, we ought to use
431
         16 and 32-bit accesses for greater efficiency.
432
 
433
   @todo  The algorithm for ensuring we only set the bits of interest in the
434
          register is inefficient. We should instead clear the whole area
435
          before starting.
436
 
437
   @param[out] jreg        The JTAG register buffer where data is to be
438
                           stored.
439
   @param[in]  skip_bits   Bits to skip before storing data in jreg.
440
   @param[in]  status_ptr  Pointer to the status register.
441
 
442
   @return  The CRC of the data read                                         */
443
/*---------------------------------------------------------------------------*/
444
static uint32_t
445
wishbone_read (unsigned char    *jreg,
446
               unsigned int      skip_bits,
447
               enum jtag_status *status_ptr)
448
{
449
  /* Compute the CRC as we go */
450
  uint32_t  crc_out  = 0xffffffff;
451
 
452
  /* Validate the fields for the wishbone read. If this fails we stop here,
453
     setting an error in the status_ptr. */
454
  if (!validate_wb_fields())
455
    {
456
      *status_ptr |= JS_WISHBONE_ERROR;
457
      return  crc_out;
458
    }
459
 
460
  /* Transfer each byte in turn, computing the CRC as we go */
461
  unsigned  byte_off = skip_bits / 8;
462
  unsigned  bit_off  = skip_bits % 8;
463
 
464
  uint32_t  i;                          /* Index into the data being read */
465
 
466
  for (i = 0; i < runtime.debug.size; i++)
467
    {
468
      /* Error if we can't access this byte */
469
      if (NULL == verify_memoryarea (runtime.debug.addr + i))
470
        {
471
          *status_ptr |= JS_WISHBONE_ERROR;
472
          return  crc_out;
473
        }
474
 
475
      /* Get the data with no cache or VM translation and update the CRC */
476
      unsigned char  byte = eval_direct8 (runtime.debug.addr + i, 0, 0);
477
      crc_out = crc32 (byte, 8, crc_out);
478
 
479 97 jeremybenn
      /* Reverse, then store the byte in the register, without trampling
480
         adjacent bits. Simplified version when the bit offset is zero. */
481
      byte = reverse_byte (byte);
482
 
483 82 jeremybenn
      if (0 == bit_off)
484
        {
485
          jreg[byte_off + i] = byte;
486
        }
487
      else
488
        {
489
          /* Clear the bits (only) we are setting */
490
          jreg[byte_off + i]     <<= bit_off;
491
          jreg[byte_off + i]     >>= bit_off;
492
          jreg[byte_off + i + 1] >>= 8 - bit_off;
493
          jreg[byte_off + i + 1] <<= 8 - bit_off;
494
 
495
          /* OR in the bits */
496
          jreg[byte_off + i]     |= (byte <<      bit_off)  & 0xff;
497
          jreg[byte_off + i + 1] |= (byte >> (8 - bit_off)) & 0xff;
498
        }
499
    }
500
 
501
    runtime.debug.addr += runtime.debug.size;
502
 
503
  return  crc_out;
504
 
505
}       /* wishbone_read () */
506
 
507
 
508
/*---------------------------------------------------------------------------*/
509
/*!Validate WRITE_COMMAND fields for SPR
510
 
511
   Check that a WRITE_COMMAND's fields are valid for SPR access. Only prints
512
   messages, since the protocol does not allow for any errors.
513
 
514
   - 8 and 16-bit access is only permitted for WishBone. If they are specified
515
     for SPR, they are treated as their 32 bit equivalent. A warning is
516
     printed.
517
 
518
   - size must be 1 word. If a larger value is specified that is treated as 1
519
     with a warning.
520
 
521
   - address must be less than MAX_SPRS. If a larger value is specified, it is
522
     reduced module MAX_SPRS (which is hopefully a power of 2).
523
 
524
   Where errors are found, the data is updated.
525
 
526
   Validation is carried out with the GO_COMMAND, rather than with the
527
   WRITE_COMMAND, for consistency with WishBone error checking, for which
528
   only the GO_COMMAND can set the status error if there is a problem.
529
 
530
   Warning messages are printed to explain any validation problems.
531
 
532
   @todo Should multiple SPR accesses be allowed in a single access?
533
 
534
   @note The size of the data is one greater than the length specified in the
535 97 jeremybenn
         original WRITE_COMMAND.
536
 
537
   @return  1 (TRUE) if we could validate, even if we had to modify a field
538
            with a warning. 0 (FALSE) otherwise.                             */
539 82 jeremybenn
/*---------------------------------------------------------------------------*/
540 97 jeremybenn
static int
541 82 jeremybenn
validate_spr_fields ()
542
{
543
  int  access_bits;
544
  int  is_read_p;
545
 
546
  switch (runtime.debug.acc_type)
547
    {
548 97 jeremybenn
    case JAT_WRITE8:  access_bits =  8; is_read_p = 0; break;
549
    case JAT_READ8:   access_bits =  8; is_read_p = 1; break;
550
    case JAT_WRITE16: access_bits = 16; is_read_p = 0; break;
551
    case JAT_READ16:  access_bits = 16; is_read_p = 1; break;
552
    case JAT_WRITE32: access_bits = 32; is_read_p = 0; break;
553
    case JAT_READ32:  access_bits = 32; is_read_p = 1; break;
554 82 jeremybenn
 
555
    default:
556 97 jeremybenn
      fprintf (stderr, "Warning: validate_spr_fields: unknown access "
557 82 jeremybenn
               "type %u\n", runtime.debug.acc_type);
558 97 jeremybenn
      return  0;
559 82 jeremybenn
    }
560
 
561
  /* Validate and correct if necessary access width */
562
  if (32 != access_bits)
563
    {
564
      fprintf (stderr, "Warning: JTAG %d-bit access not permitted for SPR: "
565
               "corrected\n", access_bits);
566
      runtime.debug.acc_type = is_read_p ? JAT_READ32 : JAT_WRITE32;
567
    }
568
 
569
  /* Validate and correct if necessary access size */
570
  if (1 != runtime.debug.size)
571
    {
572 97 jeremybenn
      fprintf (stderr, "Warning: JTAG SPR access must be 1 word in length: "
573 82 jeremybenn
               "corrected\n");
574
      runtime.debug.size = 1;
575
    }
576 97 jeremybenn
 
577 82 jeremybenn
  /* Validate and correct if necessary access size */
578
  if (runtime.debug.addr >= MAX_SPRS)
579
    {
580 97 jeremybenn
      fprintf (stderr, "Warning: JTAG SPR address exceeds MAX_SPRS: "
581 82 jeremybenn
               "truncated\n");
582
      runtime.debug.addr %= MAX_SPRS;
583
    }
584 97 jeremybenn
 
585
  return  0;                     /* Success */
586
 
587 82 jeremybenn
}       /* validate_spr_fields () */
588
 
589
 
590
/*---------------------------------------------------------------------------*/
591
/*!Read SPR data
592
 
593
   Read memory from WishBone. The WRITE_COMMAND address is updated to reflect
594
   the completed read if successful.
595
 
596
   The result is the CRC of the data read.
597
 
598
   Unlike with Wishbone, there is no concept of any errors possible when
599
   reading an SPR.
600
 
601
   @todo The algorithm for ensuring we only set the bits of interest in the
602
         register is inefficient. We should instead clear the whole area
603
         before starting.
604
 
605
   @note The address is treated as a word address of the SPR.
606
 
607
   @note The debug unit is documented as being explicitly Big Endian. However
608
         that seems to be a poor basis for modeling, and more to do with the
609
         debug unit only ever being used with big-endian architectures. We
610
         transfer the bytes in the endianness of the OR1K.
611
 
612
   @param[out] jreg       The JTAG register buffer where data is to be stored.
613
   @param[in]  skip_bits  Bits to skip before storing data in jreg.
614
 
615
   @return  The CRC of the data read                                         */
616
/*---------------------------------------------------------------------------*/
617
static uint32_t
618
spr_read (unsigned char *jreg,
619
          unsigned int   skip_bits)
620
{
621
  /* Compute the CRC as we go */
622
  uint32_t  crc_out  = 0xffffffff;
623
 
624 97 jeremybenn
  /* Validate the fields for the SPR read. This mostly just prints
625
     out warnings and corrects the problems. However if the access type
626
     cannot be worked out, we must use empty data. */
627
  uint32_t  spr;
628 82 jeremybenn
 
629 97 jeremybenn
  if (validate_spr_fields())
630
    {
631
      /* Transfer the SPR */
632
      spr = mfspr (runtime.debug.addr);
633
    }
634
  else
635
    {
636
      spr = 0;
637
    }
638
 
639 82 jeremybenn
  runtime.debug.addr++;
640
 
641
  /* Store the SPR in the register, without trampling adjacent
642
     bits. Simplified version when the bit offset is zero. Compute the CRC as
643
     we go. */
644
  unsigned  byte_off = skip_bits / 8;
645
  unsigned  bit_off  = skip_bits % 8;
646
 
647
  if (0 == bit_off)
648
    {
649
      /* Each byte in turn */
650
      int  i;
651
 
652
      for (i = 0; i < 4; i++)
653
        {
654
#ifdef OR32_BIG_ENDIAN
655
          uint8_t byte = (spr >> 8 * i) & 0xff;
656
#else /* !OR32_BIG_ENDIAN */
657
          uint8_t byte = (spr >> (24 - (8 * i))) & 0xff;
658
#endif /* OR32_BIG_ENDIAN */
659
 
660
          jreg[byte_off + i] =  byte;
661
          crc_out = crc32 (byte, 8, crc_out);
662
        }
663
    }
664
  else
665
    {
666
      /* Each byte in turn */
667
      int  i;
668
 
669
      for (i = 0; i < 4; i++)
670
        {
671
#ifdef OR32_BIG_ENDIAN
672
          uint8_t byte = (spr >> 8 * i) & 0xff;
673
#else /* !OR32_BIG_ENDIAN */
674
          uint8_t byte = (spr >> (24 - (8 * i))) & 0xff;
675
#endif /* OR32_BIG_ENDIAN */
676
 
677
          /* Clear the bits (only) we are setting */
678
          jreg[byte_off + i]     <<= bit_off;
679
          jreg[byte_off + i]     >>= bit_off;
680
          jreg[byte_off + i + 1] >>= 8 - bit_off;
681
          jreg[byte_off + i + 1] <<= 8 - bit_off;
682
 
683
          /* OR in the bits */
684
          jreg[byte_off + i]     |= (byte <<      bit_off)  & 0xff;
685
          jreg[byte_off + i + 1] |= (byte >> (8 - bit_off)) & 0xff;
686
 
687
          crc_out = crc32 (byte, 8, crc_out);
688
        }
689
    }
690
 
691
  return  crc_out;
692
 
693
}       /* spr_read () */
694
 
695
 
696
/*---------------------------------------------------------------------------*/
697
/*!Carry out a read from WishBone or SPR
698
 
699
   Process a GO_COMMAND register for read. The format is:
700
 
701
          +-------------+-------+---------+---+
702
          |             |       |    GO   |   |
703
   TDI -> |   Ignored   |  CRC  | COMMAND | 0 | -> TDO
704
          |             |       |  (0x0)  |   |
705
          +-------------+-------+---------+---+
706
           36 + 8 * size    32       4
707
               bits        bits    bits
708
 
709
   The returned register has the format:
710
 
711
          +-------+--------+------------+---------+
712
          |       |        |            |         |
713
   TDI -> |  CRC  | Status |    Data    |  00..00 | -> TDO
714
          |       |        |            |         |
715
          +-------+--------+------------+---------+
716
              32       4      8 * size       37
717
             bits    bits       bits        bits
718
 
719
   Fields are always shifted in MS bit first, so must be reversed. The CRC in
720
   is computed on the first 5 bits, the CRC out on the 4 + 8 * size status and
721
   data bits.
722
 
723
   @note The size of the data is one greater than the length specified in the
724
         original WRITE_COMMAND.
725
 
726
   @param[in,out] jreg  The register to shift in, and the register shifted
727
                        back out.
728
 
729
   @return  The number of cycles the shift took, which in turn is the number
730
            of bits in the register                                          */
731
/*---------------------------------------------------------------------------*/
732
static int
733
go_command_read (unsigned char *jreg)
734
{
735
  /* Break out the fields */
736
  uint32_t  crc_in   = reverse_bits (((uint32_t) (jreg[0] & 0xe0) >>  5) |
737
                                     ((uint32_t)  jreg[1]         <<  3) |
738
                                     ((uint32_t)  jreg[2]         << 11) |
739
                                     ((uint32_t)  jreg[3]         << 19) |
740
                                     ((uint32_t) (jreg[4] & 0x1f) << 27), 32);
741
 
742
  /* Compute the expected CRC */
743
  uint32_t  crc_computed;
744
 
745
  crc_computed = crc32 (0,                1, 0xffffffff);
746
  crc_computed = crc32 (JCMD_GO_COMMAND,  4, crc_computed);
747
 
748
  /* Status flags */
749
  enum jtag_status  status = JS_OK;
750
 
751
  /* CRC to go out */
752 96 jeremybenn
  uint32_t  crc_out = 0;
753 82 jeremybenn
 
754
  /* Validate the CRC */
755
  if (crc_computed == crc_in)
756
    {
757
      /* Read the data. */
758
      switch (runtime.debug.mod_id)
759
        {
760
        case JM_WISHBONE:
761
          crc_out = wishbone_read (jreg, 37, &status);
762
          break;
763
 
764
        case JM_CPU0:
765
          crc_out = spr_read (jreg, 37);
766
          break;
767
 
768
        case JM_CPU1:
769
          fprintf (stderr, "Warning: JTAG attempt to read from CPU1: Not "
770
                   "supported.\n");
771
          break;
772
 
773
        default:
774 97 jeremybenn
          fprintf (stderr, "Warning: go_command_read: invalid module 0x%lx\n",
775
                   runtime.debug.mod_id);
776
          break;
777 82 jeremybenn
        }
778
    }
779
  else
780
    {
781
      /* Mismatch: record the error */
782
      status |= JS_CRC_IN_ERROR;
783
    }
784
 
785
  /* Construct the outgoing register, skipping the data read and returning the
786
     number of JTAG cycles taken (the register length). */
787
  return  construct_response (jreg, status, crc_out, 8 * runtime.debug.size,
788
                              37);
789
 
790
}       /* go_command_read () */
791
 
792
 
793
/*---------------------------------------------------------------------------*/
794
/*!Write WishBone data
795
 
796
   Write memory to WishBone. The WRITE_COMMAND address is updated to reflect
797
   the completed write if successful.
798
 
799
   @note The size of the data is one greater than the length specified in the
800
         original WRITE_COMMAND.
801
 
802
   @todo For now we always write a byte a time. In the future, we ought to use
803
         16 and 32-bit accesses for greater efficiency.
804
 
805
   @todo  The algorithm for ensuring we only set the bits of interest in the
806
          register is inefficient. We should instead clear the whole area
807
          before starting.
808
 
809
   @param[out] jreg        The JTAG register buffer where data is to be
810
                           stored.
811
   @param[in]  skip_bits   Bits to skip before reading data from jreg.
812
   @param[in]  status_ptr  Pointer to the status register.                   */
813
/*---------------------------------------------------------------------------*/
814
static void
815
wishbone_write (unsigned char   *jreg,
816
               unsigned int      skip_bits,
817
               enum jtag_status *status_ptr)
818
{
819
  /* Validate the fields for the wishbone write. If this fails we stop here,
820
     setting an error in the status_ptr. */
821
  if (!validate_wb_fields())
822
    {
823
      *status_ptr |= JS_WISHBONE_ERROR;
824
      return;
825
    }
826
 
827
  /* Transfer each byte in turn, computing the CRC as we go */
828
  unsigned  byte_off = skip_bits / 8;
829
  unsigned  bit_off  = skip_bits % 8;
830
 
831
  uint32_t  i;                          /* Index into the data being write */
832
 
833
  for (i = 0; i < runtime.debug.size; i++)
834
    {
835
      /* Error if we can't access this byte */
836
      if (NULL == verify_memoryarea (runtime.debug.addr + i))
837
        {
838
          *status_ptr |= JS_WISHBONE_ERROR;
839
          return;
840
        }
841
 
842
      /* Extract the byte from the register. Simplified version when the bit
843
         offset is zero. */
844 97 jeremybenn
      unsigned char  byte;
845 82 jeremybenn
 
846
      if (0 == bit_off)
847
        {
848 97 jeremybenn
          byte = jreg[byte_off + i];
849 82 jeremybenn
        }
850
      else
851
        {
852 97 jeremybenn
          byte = jreg[byte_off + i]     >>      bit_off  |
853
                 jreg[byte_off + i + 1] << (8 - bit_off);
854 82 jeremybenn
        }
855
 
856
      /* Circumvent the read-only check usually done for mem accesses. */
857 97 jeremybenn
      set_program8 (runtime.debug.addr + i, reverse_byte (byte));
858 82 jeremybenn
    }
859
 
860
  runtime.debug.addr += runtime.debug.size;
861
 
862
}       /* wishbone_write () */
863
 
864
 
865
/*---------------------------------------------------------------------------*/
866
/*!Write SPR data
867
 
868
   Write memory to WishBone. The WRITE_COMMAND address is updated to reflect
869
   the completed write if successful.
870
 
871
   Unlike with Wishbone, there is no concept of any errors possible when
872
   writeing an SPR.
873
 
874
   @todo The algorithm for ensuring we only set the bits of interest in the
875
         register is inefficient. We should instead clear the whole area
876
         before starting.
877
 
878
   @note The address is treated as a word address of the SPR.
879
 
880
   @note The debug unit is documented as being explicitly Big Endian. However
881
         that seems to be a poor basis for modeling, and more to do with the
882
         debug unit only ever being used with big-endian architectures. We
883
         transfer the bytes in the endianness of the OR1K.
884
 
885
   @param[out] jreg       The JTAG register buffer where data is to be stored.
886
   @param[in]  skip_bits  Bits to skip before reading data from jreg.        */
887
/*---------------------------------------------------------------------------*/
888
static void
889
spr_write (unsigned char *jreg,
890
          unsigned int   skip_bits)
891
{
892 97 jeremybenn
  /* Validate the fields for the SPR write. This doesn't stop us most of the
893
     time, just prints out warnings and corrects the problems. However if we
894
     don't know the access type, then it will fail and we do nothing. */
895
  if (!validate_spr_fields ())
896
    {
897
      return;
898
    }
899 82 jeremybenn
 
900
  /* Construct the SPR value one byte at a time. */
901
  uint32_t  spr = 0;
902
 
903
  unsigned  byte_off = skip_bits / 8;
904
  unsigned  bit_off  = skip_bits % 8;
905
 
906
  /* Each byte in turn */
907
  int  i;
908
 
909
  for (i = 0; i < 4; i++)
910
    {
911
      uint8_t byte;
912
 
913
      /* Simplified version when the bit offset is zero */
914
      if (0 == bit_off)
915
        {
916
          byte = reverse_byte (jreg[byte_off + i]);
917
        }
918
      else
919
        {
920
          byte = reverse_byte ((jreg[byte_off + i]     >>      bit_off) |
921
                               (jreg[byte_off + i + 1] << (8 - bit_off)));
922
        }
923
 
924
#ifdef OR32_BIG_ENDIAN
925
      spr |= ((uint32_t) (byte)) << (8 * i);
926
#else /* !OR32_BIG_ENDIAN */
927
      spr |= ((uint32_t) (byte)) << (24 - (8 * i));
928
#endif /* OR32_BIG_ENDIAN */
929
    }
930
 
931
  /* Transfer the SPR */
932
  mtspr (runtime.debug.addr, spr);
933
  runtime.debug.addr++;
934
 
935
}       /* spr_write () */
936
 
937
 
938
/*---------------------------------------------------------------------------*/
939
/*!Carry out a write to WishBone or SPR
940
 
941
   Process a GO_COMMAND register for write. The format is:
942
 
943
          +-------------+-------+------------+---------+---+
944
          |             |       |            |    GO   |   |
945
   TDI -> |   Ignored   |  CRC  |    Data    | COMMAND | 0 | -> TDO
946
          |             |       |            |  (0x0)  |   |
947
          +-------------+-------+------------+---------+---+
948
                36          32     8 * size       4
949
               bits        bits      bits       bits
950
 
951
   The returned register has the format:
952
 
953
          +-------+--------+-------------+
954
          |       |        |             |
955
   TDI -> |  CRC  | Status |  00......00 | -> TDO
956
          |       |        |             |
957
          +-------+--------+-------------+
958
              32       4    8 * size + 37
959
             bits    bits        bits
960
 
961
   Fields are always shifted in MS bit first, so must be reversed. The CRC in
962
   is computed on the first 5 + 8 * size bits, the CRC out on the 4 status
963
   bits.
964
 
965
   @note The size of the data is one greater than the length specified in the
966
         original WRITE_COMMAND.
967
 
968
   @todo The rules say we look for errors in the WRITE_COMMAND spec
969
         here. However it would be better to do that at WRITE_COMMAND time and
970
         save the result for here, to avoid using duff data.
971
 
972
   @param[in,out] jreg  The register to shift in, and the register shifted
973
                        back out.
974
 
975
   @return  The number of cycles the shift took, which in turn is the number
976
            of bits in the register                                          */
977
/*---------------------------------------------------------------------------*/
978
static int
979
go_command_write (unsigned char *jreg)
980
{
981
  /* Break out the fields */
982
  uint32_t  crc_in =
983
    reverse_bits (((uint32_t) (jreg[runtime.debug.size + 0] & 0xe0) >>  5) |
984
                  ((uint32_t)  jreg[runtime.debug.size + 1]         <<  3) |
985
                  ((uint32_t)  jreg[runtime.debug.size + 2]         << 11) |
986
                  ((uint32_t)  jreg[runtime.debug.size + 3]         << 19) |
987
                  ((uint32_t) (jreg[runtime.debug.size + 4] & 0x1f) << 27),
988
                  32);
989
 
990
  /* Compute the expected CRC */
991
  uint32_t  crc_computed;
992
 
993
  crc_computed = crc32 (0,                1, 0xffffffff);
994
  crc_computed = crc32 (JCMD_GO_COMMAND,  4, crc_computed);
995
 
996
  int  i;
997
 
998
  for (i = 0; i < runtime.debug.size; i++)
999
    {
1000 97 jeremybenn
      uint8_t  byte = reverse_bits (((jreg[i] & 0xe0) >> 5) |
1001
                      ((jreg[i + 1] & 0x1f) << 3), 8);
1002 82 jeremybenn
      crc_computed = crc32 (byte, 8, crc_computed);
1003
    }
1004
 
1005
  /* Status flags */
1006
  enum jtag_status  status = JS_OK;
1007
 
1008
  /* Validate the CRC */
1009
  if (crc_computed == crc_in)
1010
    {
1011
      /* Read the data. */
1012
      switch (runtime.debug.mod_id)
1013
        {
1014
        case JM_WISHBONE:
1015
          wishbone_write (jreg, 5, &status);
1016
          break;
1017
 
1018
        case JM_CPU0:
1019
          spr_write (jreg, 5);
1020
          break;
1021
 
1022
        case JM_CPU1:
1023
          fprintf (stderr, "Warning: JTAG attempt to write to CPU1: Not "
1024
                   "supported.\n");
1025
          break;
1026
 
1027
        default:
1028 97 jeremybenn
          fprintf (stderr, "Warning: go_command_write: invalid module 0x%lx\n",
1029
                   runtime.debug.mod_id);
1030
          break;
1031 82 jeremybenn
        }
1032
    }
1033
  else
1034
    {
1035
      /* Mismatch: record the error */
1036
      status |= JS_CRC_IN_ERROR;
1037
    }
1038
 
1039
  /* Construct the outgoing register, skipping the data read and returning the
1040
     number of JTAG cycles taken (the register length). */
1041
  return  construct_response (jreg, status, 0xffffffff, 0,
1042
                              37 + 8 * runtime.debug.size);
1043
 
1044
}       /* go_command_write () */
1045
 
1046
 
1047
/*---------------------------------------------------------------------------*/
1048
/*!Invoke the action specified by a prior WRITE_COMMAND register
1049
 
1050
   Process a GO_COMMAND register.
1051
 
1052
   How this is handled depends on whether a previous WRITE_COMMAND has
1053
   selected a read access type or a write access type has been selected.
1054
 
1055
   This function breaks this out.
1056
 
1057
   @param[in,out] jreg  The register to shift in, and the register shifted
1058
                        back out.
1059
 
1060
   @return  The number of cycles the shift took, which in turn is the number
1061
            of bits in the register                                          */
1062
/*---------------------------------------------------------------------------*/
1063
static int
1064
go_command (unsigned char *jreg)
1065
{
1066
  /* Have we even had a WRITE_COMMAND? */
1067
  if (!runtime.debug.write_defined_p)
1068
    {
1069
      fprintf (stderr, "Warning: JTAG GO_COMMAND with no prior WRITE_COMMAND: "
1070
               "ignored\n");
1071
      return 4 + 1;                     /* Only the first 5 bits meaningful */
1072
    }
1073
 
1074
  /* Whether to read or write depends on the access type */
1075
  switch (runtime.debug.acc_type)
1076
    {
1077
    case JAT_WRITE8:
1078
    case JAT_WRITE16:
1079
    case JAT_WRITE32:
1080
      return  go_command_write (jreg);
1081
 
1082
    case JAT_READ8:
1083
    case JAT_READ16:
1084
    case JAT_READ32:
1085
      return  go_command_read (jreg);
1086
 
1087
    default:
1088
      fprintf (stderr, "Warning: JTAG GO_COMMAND: invalid access type: "
1089
               "ignored\n");
1090
      return 4 + 1;                     /* Only the first 5 bits meaningful */
1091
    }
1092
}       /* go_command () */
1093
 
1094
 
1095
/*---------------------------------------------------------------------------*/
1096
/*!Read a previouse WRITE_COMMAND register
1097
 
1098
   Process a READ_COMMAND register. The format is:
1099
 
1100
          +---------+-------+---------+---+
1101
          |         |       |   READ  |   |
1102
   TDI -> | Ignored |  CRC  | COMMAND | 0 | -> TDO
1103
          |         |       |  (0x1)  |   |
1104
          +---------+-------+---------+---+
1105
              88        32       4
1106
             bits      bits    bits
1107
 
1108
   The returned register has the format:
1109
 
1110
          +-------+--------+--------+---------+--------+---------+
1111
          |       |        |        |         |        |         |
1112
   TDI -> |  CRC  | Status | Length | Address | Access |  00..00 | -> TDO
1113
          |       |        |        |         |  Type  |         |
1114
          +-------+--------+--------+---------+--------+---------+
1115
              32       4       16        32        4        37
1116
             bits    bits     bits      bits     bits      bits
1117
 
1118
   Fields are always shifted in MS bit first, so must be reversed. The CRC in
1119
   is computed on the first 5 bits, the CRC out on the 56 status, length,
1120
   address and access type bits.
1121
 
1122
   @param[in,out] jreg  The register to shift in, and the register shifted
1123
                        back out.
1124
 
1125
   @return  The number of cycles the shift took, which in turn is the number
1126
            of bits in the register                                          */
1127
/*---------------------------------------------------------------------------*/
1128
static int
1129
read_command (unsigned char *jreg)
1130
{
1131
  /* Break out the fields */
1132
  uint32_t  crc_in   = reverse_bits (((uint32_t) (jreg[0] & 0xe0) >>  5) |
1133
                                     ((uint32_t)  jreg[1]         <<  3) |
1134
                                     ((uint32_t)  jreg[2]         << 11) |
1135
                                     ((uint32_t)  jreg[3]         << 19) |
1136
                                     ((uint32_t) (jreg[4] & 0x1f) << 27), 32);
1137
 
1138
  /* Compute the expected CRC */
1139
  uint32_t  crc_computed;
1140
 
1141
  crc_computed = crc32 (0,                   1, 0xffffffff);
1142
  crc_computed = crc32 (JCMD_READ_COMMAND,  4, crc_computed);
1143
 
1144
  /* CRC to go out */
1145
  uint32_t  crc_out = 0xffffffff;
1146
 
1147
  /* Status flags */
1148
  enum jtag_status  status = JS_OK;
1149
 
1150
  /* Validate the CRC */
1151
  if (crc_computed != crc_in)
1152
    {
1153
      /* Mismatch: record the error */
1154
      status |= JS_CRC_IN_ERROR;
1155
    }
1156 97 jeremybenn
 
1157
  /* If we haven't had a previous WRITE_COMMAND, then we return empty
1158
     data. There is no valid status flag we can use, but we print a rude
1159
     message. */
1160
  uint8_t   acc_type_r;
1161
  uint32_t  addr_r;
1162
  uint16_t  len_r;
1163
 
1164
  if (runtime.debug.write_defined_p)
1165 82 jeremybenn
    {
1166
      /* Compute the CRC */
1167
      crc_out = crc32 (runtime.debug.acc_type,  4, crc_out);
1168
      crc_out = crc32 (runtime.debug.addr,     32, crc_out);
1169
      crc_out = crc32 (runtime.debug.size - 1, 16, crc_out);
1170
 
1171 97 jeremybenn
      /* Reverse the bit fields */
1172
      acc_type_r = reverse_bits (runtime.debug.acc_type,  4);
1173
      addr_r     = reverse_bits (runtime.debug.addr,     32);
1174
      len_r      = reverse_bits (runtime.debug.size - 1, 16);
1175 82 jeremybenn
    }
1176
  else
1177
    {
1178 97 jeremybenn
      fprintf (stderr, "Warning: JTAG READ_COMMAND finds no data\n");
1179
 
1180
      /* Compute the CRC */
1181
      crc_out = crc32 (0,  4, crc_out);
1182
      crc_out = crc32 (0, 32, crc_out);
1183
      crc_out = crc32 (0, 16, crc_out);
1184
 
1185
      /* Empty data */
1186
      acc_type_r = 0;
1187
      addr_r     = 0;
1188
      len_r      = 0;
1189 82 jeremybenn
    }
1190
 
1191 97 jeremybenn
  /* Construct the outgoing register. Fields can only be written if they
1192
     are available */
1193
  jreg[ 4] |= (acc_type_r <<  5) & 0xf8;
1194
  jreg[ 5] |= (acc_type_r >>  3) & 0x07;
1195
  jreg[ 5] |= (addr_r     <<  1) & 0xfe;
1196
  jreg[ 6] |= (addr_r     >>  7) & 0xff;
1197
  jreg[ 7] |= (addr_r     >> 15) & 0xff;
1198
  jreg[ 8] |= (addr_r     >> 23) & 0xff;
1199
  jreg[ 9] |= (addr_r     >> 31) & 0x01;
1200
  jreg[ 9] |= (len_r      <<  1) & 0xfe;
1201
  jreg[10] |= (len_r      >>  7) & 0xff;
1202
  jreg[11] |= (len_r      >> 15) & 0x01;
1203
 
1204 82 jeremybenn
  /* Construct the final response with the status, skipping the fields we've
1205
     just (possibly) written. */
1206
  return  construct_response (jreg, status, crc_out, 52, 37);
1207
 
1208
}       /* read_command () */
1209
 
1210
 
1211
/*---------------------------------------------------------------------------*/
1212
/*!Specify details for a subsequence GO_COMMAND
1213
 
1214
   Process a WRITE_COMMAND register. The format is:
1215
 
1216
          +---------+-------+--------+---------+--------+---------+---+
1217
          |         |       |        |         |        |  WRITE  |   |
1218
   TDI -> | Ignored |  CRC  | Length | Address | Access | COMMAND | 0 | -> TDO
1219
          |         |       |        |         |  Type  |  (0x2)  |   |
1220
          +---------+-------+--------+---------+--------+---------+---+
1221
              36        32      16        32        4         4
1222
             bits      bits    bits      bits     bits      bits
1223
 
1224
   The returned register has the format:
1225
 
1226
          +-------+--------+---------+
1227
          |       |        |         |
1228
   TDI -> |  CRC  | Status |  00..00 | -> TDO
1229
          |       |        |         |
1230
          +-------+--------+---------+
1231
              32       4        89
1232
             bits    bits      bits
1233
 
1234
 
1235
   Fields are always shifted in MS bit first, so must be reversed. The CRC in
1236
   is computed on the first 57 bits, the CRC out on the 4 status bits.
1237
 
1238
   @param[in,out] jreg  The register to shift in, and the register shifted
1239
                        back out.
1240
 
1241
   @return  The number of cycles the shift took, which in turn is the number
1242
            of bits in the register                                          */
1243
/*---------------------------------------------------------------------------*/
1244
static int
1245
write_command (unsigned char *jreg)
1246
{
1247
  /* Break out the fields */
1248 97 jeremybenn
  uint8_t   acc_type = reverse_bits (((jreg[0] & 0xe0) >> 5) |
1249
                                     ((jreg[1] & 0x01) << 3) , 4);
1250 82 jeremybenn
  uint32_t  addr     = reverse_bits (((uint32_t) (jreg[ 1] & 0xfe) >>  1) |
1251
                                     ((uint32_t)  jreg[ 2]         <<  7) |
1252
                                     ((uint32_t)  jreg[ 3]         << 15) |
1253
                                     ((uint32_t)  jreg[ 4]         << 23) |
1254
                                     ((uint32_t) (jreg[ 5] & 0x01) << 31), 32);
1255
  uint16_t  len      = reverse_bits (((uint32_t) (jreg[ 5] & 0xfe) >>  1) |
1256
                                     ((uint32_t)  jreg[ 6]         <<  7) |
1257
                                     ((uint32_t) (jreg[ 7] & 0x01) << 15), 16);
1258
  uint32_t  crc_in   = reverse_bits (((uint32_t) (jreg[ 7] & 0xfe) >>  1) |
1259
                                     ((uint32_t)  jreg[ 8]         <<  7) |
1260
                                     ((uint32_t)  jreg[ 9]         << 15) |
1261
                                     ((uint32_t)  jreg[10]         << 23) |
1262
                                     ((uint32_t) (jreg[11] & 0x01) << 31), 32);
1263
 
1264
  /* Compute the expected CRC */
1265
  uint32_t  crc_computed;
1266
 
1267
  crc_computed = crc32 (0,                   1, 0xffffffff);
1268
  crc_computed = crc32 (JCMD_WRITE_COMMAND,  4, crc_computed);
1269
  crc_computed = crc32 (acc_type,            4, crc_computed);
1270
  crc_computed = crc32 (addr,               32, crc_computed);
1271
  crc_computed = crc32 (len,                16, crc_computed);
1272
 
1273
  /* Status flags */
1274
  enum jtag_status  status = JS_OK;
1275
 
1276
  /* Validate the CRC */
1277
  if (crc_computed != crc_in)
1278
    {
1279
      /* Mismatch: record the error */
1280
      status |= JS_CRC_IN_ERROR;
1281
    }
1282
  else
1283
    {
1284
      /* All OK. All other errors can only occur when the GO_COMMAND tries to
1285
         execute the write. Record the information for next GO_COMMAND */
1286
      runtime.debug.write_defined_p = 1;
1287
      runtime.debug.acc_type        = acc_type;
1288
      runtime.debug.addr            = addr;
1289
      runtime.debug.size            = (unsigned long int) len + 1UL;
1290
    }
1291
 
1292
  /* Construct the outgoing register and return the JTAG cycles taken (the
1293
     register length) */
1294
  return  construct_response (jreg, status, 0xffffffff, 0, 89);
1295
 
1296
}       /* write_command () */
1297
 
1298
 
1299
/*---------------------------------------------------------------------------*/
1300
/*!Read the control bits from a CPU.
1301
 
1302
   Process a READ_CONTROL register. The format is:
1303
 
1304
          +---------+-------+---------+---+
1305
          |         |       |  READ   |   |
1306
   TDI -> | Ignored |  CRC  | CONTROL | 0 | -> TDO
1307
          |         |       |  (0x3)  |   |
1308
          +---------+-------+---------+---+
1309
              36        32       4
1310
             bits      bits    bits
1311
 
1312
   The returned register has the format:
1313
 
1314
          +-------+--------+--------+---------+
1315
          |       |        |        |         |
1316
   TDI -> |  CRC  | Status |  Data  |  00..00 | -> TDO
1317
          |       |        |        |         |
1318
          +-------+--------+--------+---------+
1319
              32       4       52        37
1320
             bits    bits     bits      bits
1321
 
1322
   Fields are always shifted in MS bit first, so must be reversed. The CRC in
1323
   is computed on the first 57 bits, the CRC out on the 4 status bits.
1324
 
1325
   @param[in,out] jreg  The register to shift in, and the register shifted
1326
                        back out.
1327
 
1328
   @return  The number of cycles the shift took, which in turn is the number
1329
            of bits in the register                                          */
1330
/*---------------------------------------------------------------------------*/
1331
static int
1332
read_control (unsigned char *jreg)
1333
{
1334
  /* Break out the fields. */
1335
  uint32_t  crc_in = reverse_bits (((uint32_t) (jreg[0] & 0xe0) >>  5) |
1336
                                   ((uint32_t)  jreg[1]         <<  3) |
1337
                                   ((uint32_t)  jreg[2]         << 11) |
1338
                                   ((uint32_t)  jreg[3]         << 19) |
1339
                                   ((uint32_t) (jreg[4] & 0x1f) << 27), 32);
1340
 
1341
  /* Compute the expected CRC */
1342
  uint32_t  crc_computed;
1343
 
1344
  crc_computed = crc32 (0,                   1, 0xffffffff);
1345
  crc_computed = crc32 (JCMD_READ_CONTROL,  4, crc_computed);
1346
 
1347
  /* CRC to go out */
1348
  uint32_t  crc_out = 0xffffffff;
1349
 
1350
  /* Status flags */
1351
  enum jtag_status  status = JS_OK;
1352
 
1353
  /* Validate the CRC */
1354
  if (crc_computed != crc_in)
1355
    {
1356
      /* Mismatch: record the error */
1357
      status |= JS_CRC_IN_ERROR;
1358
    }
1359
  else if (JM_CPU0 == runtime.debug.mod_id)
1360
    {
1361
      /* Valid module. Only bit we can sensibly read is the stall bit. */
1362
      uint64_t  data = (uint64_t) runtime.cpu.stalled << JCB_STALL;
1363
 
1364
      /* Compute the CRC */
1365
      crc_out = crc32 (data,  52, crc_out);
1366
 
1367
      /* Construct the outgoing register. */
1368
      uint64_t  data_r = reverse_bits (data, 52);
1369
 
1370
      jreg[ 4] |= (data_r <<  5) & 0xf8;
1371
      jreg[ 5] |= (data_r >>  3) & 0x07;
1372
      jreg[ 5] |= (data_r >> 11) & 0xff;
1373
      jreg[ 5] |= (data_r >> 19) & 0xff;
1374
      jreg[ 5] |= (data_r >> 27) & 0xff;
1375
      jreg[ 5] |= (data_r >> 35) & 0xff;
1376
      jreg[ 5] |= (data_r >> 43) & 0xff;
1377
      jreg[ 5] |= (data_r >> 51) & 0x01;
1378
    }
1379
  else
1380
    {
1381
      /* Not a valid module */
1382
      fprintf (stderr, "ERROR: JTAG attempt to read control data for module "
1383
               "other than CPU0: ignored\n");
1384
    }
1385
 
1386
  /* Construct the response with the status */
1387
  return  construct_response (jreg, status, crc_out, 52, 37);
1388
 
1389
}       /* read_control () */
1390
 
1391
 
1392
/*---------------------------------------------------------------------------*/
1393
/*!Write the control bits to a CPU.
1394
 
1395
   Process a WRITE_CONTROL register. The format is:
1396
 
1397
          +---------+-------+--------+---------+---+
1398
          |         |       |        |  WRITE  |   |
1399
   TDI -> | Ignored |  CRC  |  Data  | CONTROL | 0 | -> TDO
1400
          |         |       |        |  (0x4)  |   |
1401
          +---------+-------+--------+---------+---+
1402
              36        32      52        4
1403
             bits      bits    bits     bits
1404
 
1405
   The returned register has the format:
1406
 
1407
          +-------+--------+---------+
1408
          |       |        |         |
1409
   TDI -> |  CRC  | Status |  00..00 | -> TDO
1410
          |       |        |         |
1411
          +-------+--------+---------+
1412
              32       4        89
1413
             bits    bits      bits
1414
 
1415
   Fields are always shifted in MS bit first, so must be reversed. The CRC in
1416
   is computed on the first 57 bits, the CRC out on the 4 status bits.
1417
 
1418
   @param[in,out] jreg  The register to shift in, and the register shifted
1419
                        back out.
1420
 
1421
   @return  The number of cycles the shift took, which in turn is the number
1422
            of bits in the register                                          */
1423
/*---------------------------------------------------------------------------*/
1424
static int
1425
write_control (unsigned char *jreg)
1426
{
1427
  /* Break out the fields. */
1428
  uint64_t  data   = reverse_bits (((uint64_t) (jreg[ 0] & 0xe0) >>  5) |
1429
                                   ((uint64_t)  jreg[ 1]         <<  3) |
1430
                                   ((uint64_t)  jreg[ 2]         << 11) |
1431
                                   ((uint64_t)  jreg[ 3]         << 19) |
1432
                                   ((uint64_t)  jreg[ 4]         << 27) |
1433
                                   ((uint64_t)  jreg[ 5]         << 35) |
1434
                                   ((uint64_t)  jreg[ 6]         << 43) |
1435
                                   ((uint64_t) (jreg[ 7] & 0x01) << 51), 52);
1436
  uint32_t  crc_in = reverse_bits (((uint32_t) (jreg[ 7] & 0xfe) >>  1) |
1437
                                   ((uint32_t)  jreg[ 8]         <<  7) |
1438
                                   ((uint32_t)  jreg[ 9]         << 15) |
1439
                                   ((uint32_t)  jreg[10]         << 23) |
1440
                                   ((uint32_t) (jreg[11] & 0x01) << 31), 32);
1441
 
1442
  /* Compute the expected CRC */
1443
  uint32_t  crc_computed;
1444
 
1445
  crc_computed = crc32 (0,                   1, 0xffffffff);
1446
  crc_computed = crc32 (JCMD_WRITE_CONTROL,  4, crc_computed);
1447
  crc_computed = crc32 (data,               52, crc_computed);
1448
 
1449
  /* Status flags */
1450
  enum jtag_status  status = JS_OK;
1451
 
1452
  /* Validate the CRC */
1453
  if (crc_computed != crc_in)
1454
    {
1455
      /* Mismatch: record the error */
1456
      status |= JS_CRC_IN_ERROR;
1457
    }
1458
  else if (JM_CPU0 == runtime.debug.mod_id)
1459
    {
1460
      /* Good data and valid module. Reset, stall or unstall the register as
1461
         required. If reset is requested, there is no point considering
1462
         stalling! */
1463
      int  reset_bit = (0x1 == ((data >> JCB_RESET) & 0x1));
1464
      int  stall_bit = (0x1 == ((data >> JCB_STALL) & 0x1));
1465
 
1466
      if (reset_bit)
1467
        {
1468
          sim_reset ();
1469
        }
1470
      else
1471
        {
1472
          set_stall_state (stall_bit);
1473
        }
1474
    }
1475
  else
1476
    {
1477
      /* Not a valid module */
1478
      fprintf (stderr, "ERROR: JTAG attempt to control module other than "
1479
               "CPU0: ignored\n");
1480
    }
1481
 
1482
  /* Construct the response with the status */
1483
  return  construct_response (jreg, status, 0xffffffff, 0, 89);
1484
 
1485
}       /* write_control () */
1486
 
1487
 
1488
/*---------------------------------------------------------------------------*/
1489
/*!Initialize the JTAG system
1490
 
1491
   For now just reset the JTAG interface                                     */
1492
/*---------------------------------------------------------------------------*/
1493
void
1494
jtag_init ()
1495
{
1496
  (void) jtag_reset ();
1497
 
1498
}       /* jtag_init () */
1499
 
1500
 
1501
/*---------------------------------------------------------------------------*/
1502
/*!Reset the JTAG interface
1503
 
1504
   Mark the current JTAG instruction as undefined.
1505
 
1506
   @return  The number of cycles the reset took.                             */
1507
/*---------------------------------------------------------------------------*/
1508
int
1509
jtag_reset ()
1510
{
1511
  runtime.debug.instr = JI_UNDEFINED;
1512
 
1513
  return  JTAG_RESET_CYCLES;
1514
 
1515
}       /* jtag_reset () */
1516
 
1517
 
1518
/*---------------------------------------------------------------------------*/
1519
/*!Shift a JTAG instruction register
1520
 
1521
   @note Like all the JTAG interface functions, this must not be called
1522
         re-entrantly while a call to any other function (e.g. or1kim_run ())
1523
         is in progress. It is the responsibility of the caller to ensure this
1524
         constraint is met, for example by use of a SystemC mutex.
1525
 
1526
   The register is represented as a vector of bytes, with the byte at offset
1527
   zero being shifted first, and the least significant bit in each byte being
1528
   shifted first. Where the register will not fit in an exact number of bytes,
1529
   the odd bits are in the highest numbered byte, shifted to the low end.
1530
 
1531
   The format is:
1532
 
1533
          +-------------+
1534
          |             |
1535
   TDI -> | Instruction | -> TDO
1536
          |             |
1537
          +-------------+
1538
                 4
1539
               bits
1540
 
1541
   With this debug interface, registers are shifted MS bit first, so we must
1542
   reverse the bits to get the actual value.
1543
 
1544
   We record the selected instruction. For completeness the register is parsed
1545
   and a warning given if any register other than DEBUG is shifted.
1546
 
1547
   @param[in,out] jreg  The register to shift in, and the register shifted
1548
                        back out.
1549
 
1550
   @return  The number of cycles the shift took, which in turn is the number
1551
            of bits in the register                                          */
1552
/*---------------------------------------------------------------------------*/
1553
int
1554
jtag_shift_ir (unsigned char *jreg)
1555
{
1556
  runtime.debug.instr = reverse_bits (jreg[0] & 0xf, 4);
1557
 
1558
  switch (runtime.debug.instr)
1559
    {
1560
    case JI_EXTEST:
1561
      fprintf (stderr, "Warning: JTAG EXTEST shifted\n");
1562
      break;
1563
 
1564
    case JI_SAMPLE_PRELOAD:
1565
      fprintf (stderr, "Warning: JTAG SAMPLE/PRELOAD shifted\n");
1566
      break;
1567
 
1568
    case JI_IDCODE:
1569
      fprintf (stderr, "Warning: JTAG IDCODE shifted\n");
1570
      break;
1571
 
1572
    case JI_DEBUG:
1573
      /* Do nothing for this one */
1574
      break;
1575
 
1576
    case JI_MBIST:
1577
      fprintf (stderr, "Warning: JTAG MBIST shifted\n");
1578
      break;
1579
 
1580
    case JI_BYPASS:
1581
      fprintf (stderr, "Warning: JTAG BYPASS shifted\n");
1582
      break;
1583
 
1584
    default:
1585 97 jeremybenn
      fprintf (stderr, "Warning: Unknown JTAG instruction 0x%1x shifted\n",
1586 82 jeremybenn
               runtime.debug.instr);
1587
      break;
1588
    }
1589
 
1590
  return  4;                    /* Register length */
1591
 
1592
}       /* jtag_shift_ir () */
1593
 
1594
 
1595
/*---------------------------------------------------------------------------*/
1596
/*!Shift a JTAG data register
1597
 
1598
   @note Like all the JTAG interface functions, this must not be called
1599
         re-entrantly while a call to any other function (e.g. or1kim_run ())
1600
         is in progress. It is the responsibility of the caller to ensure this
1601
         constraint is met, for example by use of a SystemC mutex.
1602
 
1603
   The register is represented as a vector of bytes, with the byte at offset
1604
   zero being shifted first, and the least significant bit in each byte being
1605
   shifted first. Where the register will not fit in an exact number of bytes,
1606
   the odd bits are in the highest numbered byte, shifted to the low end.
1607
 
1608
   This is only meaningful if the DEBUG register instruction is already
1609
   selected. If not, the data register is rejected.
1610
 
1611
   The register is parsed to determine which of the six possible register
1612
   types it could be.
1613
   - MODULE_SELECT
1614
   - WRITE_COMMNAND
1615
   - READ_COMMAND
1616
   - GO_COMMAND
1617
   - WRITE_CONTROL
1618
   - READ_CONTROL
1619
 
1620
   @note In practice READ_COMMAND is not used. However the functionality is
1621
         provided for future compatibility.
1622
 
1623
   The parsing is hierarchical. The first bit determines if we have
1624
   MODULE_SELECT, if not, the next 4 bits determine the command.
1625
 
1626
   @param[in,out] jreg  The register to shift in, and the register shifted
1627
                        back out.
1628
 
1629
   @return  The number of cycles the shift took, which in turn is the number
1630
            of bits in the register                                          */
1631
/*---------------------------------------------------------------------------*/
1632
int
1633
jtag_shift_dr (unsigned char *jreg)
1634
{
1635
  if (JI_DEBUG != runtime.debug.instr)
1636
    {
1637
      fprintf (stderr, "ERROR: Attempt to shift JTAG data register when "
1638
               "DEBUG not instruction: ignored\n");
1639
      return  0;
1640
    }
1641
 
1642
  int  module_select_p = (1 == (jreg[0] & 0x1));
1643
 
1644
  if (module_select_p)
1645
    {
1646
      return  module_select (jreg);
1647
    }
1648
  else
1649
    {
1650
      enum jtag_cmd  cmd = reverse_bits ((jreg[0] >> 1) & 0xf, 4);
1651
 
1652
      switch (cmd)
1653
        {
1654
        case JCMD_GO_COMMAND:    return  go_command (jreg);
1655
        case JCMD_READ_COMMAND:  return  read_command (jreg);
1656
        case JCMD_WRITE_COMMAND: return  write_command (jreg);
1657
        case JCMD_READ_CONTROL:  return  read_control (jreg);
1658
        case JCMD_WRITE_CONTROL: return  write_control (jreg);
1659
 
1660
        default:
1661
          /* Not a command we recognize. Decide this after we've read just
1662
             the module and command bits */
1663
          return  4 + 1;
1664
        }
1665
    }
1666
}       /* jtag_shift_dr () */

powered by: WebSVN 2.1.0

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