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

Subversion Repositories jtag_stapl_player

[/] [jtag_stapl_player/] [trunk/] [jamjtag.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sukhanov
/****************************************************************************/
2
/*                                                                                                                                                      */
3
/*      Module:                 jamjtag.c                                                                                               */
4
/*                                                                                                                                                      */
5
/*                                      Copyright (C) Altera Corporation 1997                                   */
6
/*                                                                                                                                                      */
7
/*      Description:    Contains array management functions, including                  */
8
/*                                      functions for reading array initialization data in              */
9
/*                                      compressed formats.                                                                             */
10
/*                                                                                                                                                      */
11
/*      Revisions:              1.1 allow WAIT USECS without using JTAG hardware if             */
12
/*                                      JTAG hardware has not yet been initialized -- this is   */
13
/*                                      needed to create delays in VECTOR (non-JTAG) programs   */
14
/*                                                                                                                                                      */
15
/****************************************************************************/
16
 
17
/****************************************************************************/
18
/*                                                                                                                                                      */
19
/*      Actel version 1.1             May 2003                                                                  */
20
/*                                                                                                                                                      */
21
/****************************************************************************/
22
 
23
#include "jamexprt.h"
24
#include "jamdefs.h"
25
#include "jamsym.h"
26
#include "jamstack.h"
27
#include "jamutil.h"
28
#include "jamjtag.h"
29
 
30
//&RA
31
#include <stdio.h>
32
extern long verbose;
33
/*
34
*       Global variable to store the current JTAG state
35
*/
36
JAME_JTAG_STATE jam_jtag_state = JAM_ILLEGAL_JTAG_STATE;
37
 
38
/*
39
*       Store current stop-state for DR and IR scan commands
40
*/
41
JAME_JTAG_STATE jam_drstop_state = IDLE;
42
JAME_JTAG_STATE jam_irstop_state = IDLE;
43
 
44
/*
45
*       Store current padding values
46
*/
47
int jam_dr_preamble  = 0;
48
int jam_dr_postamble = 0;
49
int jam_ir_preamble  = 0;
50
int jam_ir_postamble = 0;
51
int jam_dr_length    = 0;
52
int jam_ir_length    = 0;
53
long *jam_dr_preamble_data  = NULL;
54
long *jam_dr_postamble_data = NULL;
55
long *jam_ir_preamble_data  = NULL;
56
long *jam_ir_postamble_data = NULL;
57
char *jam_dr_buffer         = NULL;
58
char *jam_ir_buffer         = NULL;
59
 
60
/*
61
*       Table of JTAG state names
62
*/
63
struct JAMS_JTAG_MAP
64
{
65
        JAME_JTAG_STATE state;
66
        char string[JAMC_MAX_JTAG_STATE_LENGTH + 1];
67
} jam_jtag_state_table[] =
68
{
69
        { RESET,     "RESET"     },
70
        { IDLE,      "IDLE"      },
71
        { DRSELECT,  "DRSELECT"  },
72
        { DRCAPTURE, "DRCAPTURE" },
73
        { DRSHIFT,   "DRSHIFT"   },
74
        { DREXIT1,   "DREXIT1"   },
75
        { DRPAUSE,   "DRPAUSE"   },
76
        { DREXIT2,   "DREXIT2"   },
77
        { DRUPDATE,  "DRUPDATE"  },
78
        { IRSELECT,  "IRSELECT"  },
79
        { IRCAPTURE, "IRCAPTURE" },
80
        { IRSHIFT,   "IRSHIFT"   },
81
        { IREXIT1,   "IREXIT1"   },
82
        { IRPAUSE,   "IRPAUSE"   },
83
        { IREXIT2,   "IREXIT2"   },
84
        { IRUPDATE,  "IRUPDATE"  }
85
};
86
 
87
#define JAMC_JTAG_STATE_COUNT \
88
  (sizeof(jam_jtag_state_table) / sizeof(jam_jtag_state_table[0]))
89
 
90
/*
91
*       This structure shows, for each JTAG state, which state is reached after
92
*       a single TCK clock cycle with TMS high or TMS low, respectively.  This
93
*       describes all possible state transitions in the JTAG state machine.
94
*/
95
struct JAMS_JTAG_MACHINE
96
{
97
        JAME_JTAG_STATE tms_high;
98
        JAME_JTAG_STATE tms_low;
99
} jam_jtag_state_transitions[] =
100
{
101
/* RESET     */ { RESET,        IDLE },
102
/* IDLE      */ { DRSELECT,     IDLE },
103
/* DRSELECT  */ { IRSELECT,     DRCAPTURE },
104
/* DRCAPTURE */ { DREXIT1,      DRSHIFT },
105
/* DRSHIFT   */ { DREXIT1,      DRSHIFT },
106
/* DREXIT1   */ { DRUPDATE,     DRPAUSE },
107
/* DRPAUSE   */ { DREXIT2,      DRPAUSE },
108
/* DREXIT2   */ { DRUPDATE,     DRSHIFT },
109
/* DRUPDATE  */ { DRSELECT,     IDLE },
110
/* IRSELECT  */ { RESET,        IRCAPTURE },
111
/* IRCAPTURE */ { IREXIT1,      IRSHIFT },
112
/* IRSHIFT   */ { IREXIT1,      IRSHIFT },
113
/* IREXIT1   */ { IRUPDATE,     IRPAUSE },
114
/* IRPAUSE   */ { IREXIT2,      IRPAUSE },
115
/* IREXIT2   */ { IRUPDATE,     IRSHIFT },
116
/* IRUPDATE  */ { DRSELECT,     IDLE }
117
};
118
 
119
/*
120
*       This table contains the TMS value to be used to take the NEXT STEP on
121
*       the path to the desired state.  The array index is the current state,
122
*       and the bit position is the desired endstate.  To find out which state
123
*       is used as the intermediate state, look up the TMS value in the
124
*       jam_jtag_state_transitions[] table.
125
*/
126
unsigned short jam_jtag_path_map[16] =
127
{
128
        0x0001, 0xFFFD, 0xFE01, 0xFFE7, 0xFFEF, 0xFF0F, 0xFFBF, 0xFF0F,
129
        0xFEFD, 0x0001, 0xF3FF, 0xF7FF, 0x87FF, 0xDFFF, 0x87FF, 0x7FFD
130
};
131
 
132
/*
133
*       Flag bits for jam_jtag_io() function
134
*/
135
#define TMS_HIGH   1
136
#define TMS_LOW    0
137
#define TDI_HIGH   1
138
#define TDI_LOW    0
139
#define READ_TDO   1
140
#define IGNORE_TDO 0
141
 
142
/****************************************************************************/
143
/*                                                                                                                                                      */
144
 
145
JAM_RETURN_TYPE jam_init_jtag(void)
146
 
147
/*                                                                                                                                                      */
148
/****************************************************************************/
149
{
150
        void **symbol_table = NULL;
151
        JAMS_STACK_RECORD *stack = NULL;
152
 
153
        /* initial JTAG state is unknown */
154
        jam_jtag_state = JAM_ILLEGAL_JTAG_STATE;
155
 
156
        /* initialize global variables to default state */
157
        jam_drstop_state = IDLE;
158
        jam_irstop_state = IDLE;
159
 
160
        /*&RA
161
        jam_dr_preamble  = 0;
162
        jam_dr_postamble = 0;
163
        jam_ir_preamble  = 0;
164
        jam_ir_postamble = 0;
165
        */
166
        jam_dr_length    = 0;
167
        jam_ir_length    = 0;
168
 
169
        if (jam_workspace != NULL)
170
        {
171
                symbol_table = (void **) jam_workspace;
172
                stack = (JAMS_STACK_RECORD *) &symbol_table[JAMC_MAX_SYMBOL_COUNT];
173
                jam_dr_preamble_data = (long *) &stack[JAMC_MAX_NESTING_DEPTH];
174
                jam_dr_postamble_data = &jam_dr_preamble_data[JAMC_MAX_JTAG_DR_PREAMBLE / 32];
175
                jam_ir_preamble_data = &jam_dr_postamble_data[JAMC_MAX_JTAG_DR_POSTAMBLE / 32];
176
                jam_ir_postamble_data = &jam_ir_preamble_data[JAMC_MAX_JTAG_IR_PREAMBLE / 32];
177
                jam_dr_buffer = (char * )&jam_ir_postamble_data[JAMC_MAX_JTAG_IR_POSTAMBLE / 32];
178
                jam_ir_buffer = &jam_dr_buffer[JAMC_MAX_JTAG_DR_LENGTH / 8];
179
        }
180
        /*&RA
181
        else
182
        {
183
                jam_dr_preamble_data  = NULL;
184
                jam_dr_postamble_data = NULL;
185
                jam_ir_preamble_data  = NULL;
186
                jam_ir_postamble_data = NULL;
187
                jam_dr_buffer         = NULL;
188
                jam_ir_buffer         = NULL;
189
        }
190
        */
191
        if(verbose&8) printf("jam_init_jtag workspace %lx chain parameters PREIR/DR, POSTIR/DR =%d/%d, %d/%d\n",
192
                (unsigned long)jam_workspace,jam_ir_preamble,jam_dr_preamble,jam_ir_postamble,jam_dr_postamble);
193
        return (JAMC_SUCCESS);
194
}
195
 
196
/****************************************************************************/
197
/*                                                                                                                                                      */
198
 
199
JAM_RETURN_TYPE jam_set_drstop_state
200
(
201
        JAME_JTAG_STATE state
202
)
203
 
204
/*                                                                                                                                                      */
205
/****************************************************************************/
206
{
207
        jam_drstop_state = state;
208
 
209
        return (JAMC_SUCCESS);
210
}
211
 
212
/****************************************************************************/
213
/*                                                                                                                                                      */
214
 
215
JAM_RETURN_TYPE jam_set_irstop_state
216
(
217
        JAME_JTAG_STATE state
218
)
219
 
220
/*                                                                                                                                                      */
221
/****************************************************************************/
222
{
223
        jam_irstop_state = state;
224
 
225
        return (JAMC_SUCCESS);
226
}
227
 
228
/****************************************************************************/
229
/*                                                                                                                                                      */
230
 
231
JAM_RETURN_TYPE jam_set_dr_preamble
232
(
233
        int count,
234
        int start_index,
235
        long *data
236
)
237
 
238
/*                                                                                                                                                      */
239
/****************************************************************************/
240
{
241
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
242
        int alloc_longs = 0;
243
        int i = 0;
244
        int bit = 0;
245
 
246
        if (count >= 0)
247
        {
248
                if (jam_workspace != NULL)
249
                {
250
                        if (count > JAMC_MAX_JTAG_DR_PREAMBLE)
251
                        {
252
                                status = JAMC_OUT_OF_MEMORY;
253
                        }
254
                        else
255
                        {
256
                                jam_dr_preamble = count;
257
                        }
258
                }
259
                else
260
                {
261
                        if (count > jam_dr_preamble)
262
                        {
263
                                alloc_longs = (count + 31) >> 5;
264
                                jam_free(jam_dr_preamble_data);
265
                                jam_dr_preamble_data = (long *) jam_malloc(
266
                                        alloc_longs * sizeof(long));
267
 
268
                                if (jam_dr_preamble_data == NULL)
269
                                {
270
                                        status = JAMC_OUT_OF_MEMORY;
271
                                }
272
                                else
273
                                {
274
                                        jam_dr_preamble = count;
275
                                }
276
                        }
277
                        else
278
                        {
279
                                jam_dr_preamble = count;
280
                        }
281
                }
282
 
283
                if (status == JAMC_SUCCESS)
284
                {
285
                        for (i = 0; i < count; ++i)
286
                        {
287
                                bit = i + start_index;
288
 
289
                                if (data == NULL)
290
                                {
291
                                        jam_dr_preamble_data[i >> 5] |= (1L << (bit & 0x1f));
292
                                }
293
                                else
294
                                {
295
                                        if (data[bit >> 5] & (1L << (bit & 0x1f)))
296
                                        {
297
                                                jam_dr_preamble_data[i >> 5] |= (1L << (bit & 0x1f));
298
                                        }
299
                                        else
300
                                        {
301
                                                jam_dr_preamble_data[i >> 5] &=
302
                                                        ~(unsigned long) (1L << (bit & 0x1f));
303
                                        }
304
                                }
305
                        }
306
                }
307
        }
308
 
309
        return (status);
310
}
311
 
312
/****************************************************************************/
313
/*                                                                                                                                                      */
314
 
315
JAM_RETURN_TYPE jam_set_ir_preamble
316
(
317
        int count,
318
        int start_index,
319
        long *data
320
)
321
 
322
/*                                                                                                                                                      */
323
/****************************************************************************/
324
{
325
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
326
        int alloc_longs = 0;
327
        int i = 0;
328
        int bit = 0;
329
 
330
        if (count >= 0)
331
        {
332
                if (jam_workspace != NULL)
333
                {
334
                        if (count > JAMC_MAX_JTAG_IR_PREAMBLE)
335
                        {
336
                                status = JAMC_OUT_OF_MEMORY;
337
                        }
338
                        else
339
                        {
340
                                jam_ir_preamble = count;
341
                        }
342
                }
343
                else
344
                {
345
                        if (count > jam_ir_preamble)
346
                        {
347
                                alloc_longs = (count + 31) >> 5;
348
                                jam_free(jam_ir_preamble_data);
349
                                jam_ir_preamble_data = (long *) jam_malloc(
350
                                        alloc_longs * sizeof(long));
351
 
352
                                if (jam_ir_preamble_data == NULL)
353
                                {
354
                                        status = JAMC_OUT_OF_MEMORY;
355
                                }
356
                                else
357
                                {
358
                                        jam_ir_preamble = count;
359
                                }
360
                        }
361
                        else
362
                        {
363
                                jam_ir_preamble = count;
364
                        }
365
                }
366
 
367
                if (status == JAMC_SUCCESS)
368
                {
369
                        for (i = 0; i < count; ++i)
370
                        {
371
                                bit = i + start_index;
372
 
373
                                if (data == NULL)
374
                                {
375
                                        jam_ir_preamble_data[i >> 5] |= (1L << (bit & 0x1f));
376
                                }
377
                                else
378
                                {
379
                                        if (data[bit >> 5] & (1L << (bit & 0x1f)))
380
                                        {
381
                                                jam_ir_preamble_data[i >> 5] |= (1L << (bit & 0x1f));
382
                                        }
383
                                        else
384
                                        {
385
                                                jam_ir_preamble_data[i >> 5] &=
386
                                                        ~(unsigned long) (1L << (bit & 0x1f));
387
                                        }
388
                                }
389
                        }
390
                }
391
        }
392
        if(verbose&8) printf("jam_ir_preamble=%d\n",jam_ir_preamble);
393
        return (status);
394
}
395
 
396
/****************************************************************************/
397
/*                                                                                                                                                      */
398
 
399
JAM_RETURN_TYPE jam_set_dr_postamble
400
(
401
        int count,
402
        int start_index,
403
        long *data
404
)
405
 
406
/*                                                                                                                                                      */
407
/****************************************************************************/
408
{
409
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
410
        int alloc_longs = 0;
411
        int i = 0;
412
        int bit = 0;
413
 
414
        if (count >= 0)
415
        {
416
                if (jam_workspace != NULL)
417
                {
418
                        if (count > JAMC_MAX_JTAG_DR_POSTAMBLE)
419
                        {
420
                                status = JAMC_OUT_OF_MEMORY;
421
                        }
422
                        else
423
                        {
424
                                jam_dr_postamble = count;
425
                        }
426
                }
427
                else
428
                {
429
                        if (count > jam_dr_postamble)
430
                        {
431
                                alloc_longs = (count + 31) >> 5;
432
                                jam_free(jam_dr_postamble_data);
433
                                jam_dr_postamble_data = (long *) jam_malloc(
434
                                        alloc_longs * sizeof(long));
435
 
436
                                if (jam_dr_postamble_data == NULL)
437
                                {
438
                                        status = JAMC_OUT_OF_MEMORY;
439
                                }
440
                                else
441
                                {
442
                                        jam_dr_postamble = count;
443
                                }
444
                        }
445
                        else
446
                        {
447
                                jam_dr_postamble = count;
448
                        }
449
                }
450
 
451
                if (status == JAMC_SUCCESS)
452
                {
453
                        for (i = 0; i < count; ++i)
454
                        {
455
                                bit = i + start_index;
456
 
457
                                if (data == NULL)
458
                                {
459
                                        jam_dr_postamble_data[i >> 5] |= (1L << (bit & 0x1f));
460
                                }
461
                                else
462
                                {
463
                                        if (data[bit >> 5] & (1L << (bit & 0x1f)))
464
                                        {
465
                                                jam_dr_postamble_data[i >> 5] |= (1L << (bit & 0x1f));
466
                                        }
467
                                        else
468
                                        {
469
                                                jam_dr_postamble_data[i >> 5] &=
470
                                                        ~(unsigned long) (1L << (bit & 0x1f));
471
                                        }
472
                                }
473
                        }
474
                }
475
        }
476
 
477
        return (status);
478
}
479
 
480
/****************************************************************************/
481
/*                                                                                                                                                      */
482
 
483
JAM_RETURN_TYPE jam_set_ir_postamble
484
(
485
        int count,
486
        int start_index,
487
        long *data
488
)
489
 
490
/*                                                                                                                                                      */
491
/****************************************************************************/
492
{
493
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
494
        int alloc_longs = 0;
495
        int i = 0;
496
        int bit = 0;
497
 
498
        if (count >= 0)
499
        {
500
                if (jam_workspace != NULL)
501
                {
502
                        if (count > JAMC_MAX_JTAG_IR_POSTAMBLE)
503
                        {
504
                                status = JAMC_OUT_OF_MEMORY;
505
                        }
506
                        else
507
                        {
508
                                jam_ir_postamble = count;
509
                        }
510
                }
511
                else
512
                {
513
                        if (count > jam_ir_postamble)
514
                        {
515
                                alloc_longs = (count + 31) >> 5;
516
                                jam_free(jam_ir_postamble_data);
517
                                jam_ir_postamble_data = (long *) jam_malloc(
518
                                        alloc_longs * sizeof(long));
519
 
520
                                if (jam_ir_postamble_data == NULL)
521
                                {
522
                                        status = JAMC_OUT_OF_MEMORY;
523
                                }
524
                                else
525
                                {
526
                                        jam_ir_postamble = count;
527
                                }
528
                        }
529
                        else
530
                        {
531
                                jam_ir_postamble = count;
532
                        }
533
                }
534
 
535
                if (status == JAMC_SUCCESS)
536
                {
537
                        for (i = 0; i < count; ++i)
538
                        {
539
                                bit = i + start_index;
540
 
541
                                if (data == NULL)
542
                                {
543
                                        jam_ir_postamble_data[i >> 5] |= (1L << (bit & 0x1f));
544
                                }
545
                                else
546
                                {
547
                                        if (data[bit >> 5] & (1L << (bit & 0x1f)))
548
                                        {
549
                                                jam_ir_postamble_data[i >> 5] |= (1L << (bit & 0x1f));
550
                                        }
551
                                        else
552
                                        {
553
                                                jam_ir_postamble_data[i >> 5] &=
554
                                                        ~(unsigned long) (1L << (bit & 0x1f));
555
                                        }
556
                                }
557
                        }
558
                }
559
        }
560
 
561
        return (status);
562
}
563
 
564
/****************************************************************************/
565
/*                                                                                                                                                      */
566
 
567
void jam_jtag_reset_idle(void)
568
 
569
/*                                                                                                                                                      */
570
/****************************************************************************/
571
{
572
        int i = 0;
573
 
574
        /*
575
        *       Go to Test Logic Reset (no matter what the starting state may be)
576
        */
577
        for (i = 0; i < 5; ++i)
578
        {
579
                jam_jtag_io(TMS_HIGH, TDI_LOW, IGNORE_TDO);
580
        }
581
 
582
        /*
583
        *       Now step to Run Test / Idle
584
        */
585
        jam_jtag_io(TMS_LOW, TDI_LOW, IGNORE_TDO);
586
 
587
        jam_jtag_state = IDLE;
588
}
589
 
590
/****************************************************************************/
591
/*                                                                                                                                                      */
592
 
593
JAM_RETURN_TYPE jam_goto_jtag_state
594
(
595
        JAME_JTAG_STATE state
596
)
597
 
598
/*                                                                                                                                                      */
599
/****************************************************************************/
600
{
601
        int tms = 0;
602
        int count = 0;
603
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
604
 
605
        if (jam_jtag_state == JAM_ILLEGAL_JTAG_STATE)
606
        {
607
                /* initialize JTAG chain to known state */
608
                jam_jtag_reset_idle();
609
        }
610
 
611
        if (jam_jtag_state == state)
612
        {
613
                /*
614
                *       We are already in the desired state.  If it is a stable state,
615
                *       loop here.  Otherwise do nothing (no clock cycles).
616
                */
617
                if ((state == IDLE) ||
618
                        (state == DRSHIFT) ||
619
                        (state == DRPAUSE) ||
620
                        (state == IRSHIFT) ||
621
                        (state == IRPAUSE))
622
                {
623
                        jam_jtag_io(TMS_LOW, TDI_LOW, IGNORE_TDO);
624
                }
625
                else if (state == RESET)
626
                {
627
                        jam_jtag_io(TMS_HIGH, TDI_LOW, IGNORE_TDO);
628
                }
629
        }
630
        else
631
        {
632
                while ((jam_jtag_state != state) && (count < 9))
633
                {
634
                        /*
635
                        *       Get TMS value to take a step toward desired state
636
                        */
637
                        tms = (jam_jtag_path_map[jam_jtag_state] & (1 << state)) ?
638
                                TMS_HIGH : TMS_LOW;
639
 
640
                        /*
641
                        *       Take a step
642
                        */
643
                        jam_jtag_io(tms, TDI_LOW, IGNORE_TDO);
644
 
645
                        if (tms)
646
                        {
647
                                jam_jtag_state =
648
                                        jam_jtag_state_transitions[jam_jtag_state].tms_high;
649
                        }
650
                        else
651
                        {
652
                                jam_jtag_state =
653
                                        jam_jtag_state_transitions[jam_jtag_state].tms_low;
654
                        }
655
 
656
                        ++count;
657
                }
658
        }
659
 
660
        if (jam_jtag_state != state)
661
        {
662
                status = JAMC_INTERNAL_ERROR;
663
        }
664
 
665
        return (status);
666
}
667
 
668
/****************************************************************************/
669
/*                                                                                                                                                      */
670
 
671
JAME_JTAG_STATE jam_get_jtag_state_from_name
672
(
673
        char *name
674
)
675
 
676
/*                                                                                                                                                      */
677
/*      Description:    Finds JTAG state code corresponding to name of state    */
678
/*                                      supplied as a string                                                                    */
679
/*                                                                                                                                                      */
680
/*      Returns:                JTAG state code, or JAM_ILLEGAL_JTAG_STATE if string    */
681
/*                                      does not match any valid state name                                             */
682
/*                                                                                                                                                      */
683
/****************************************************************************/
684
{
685
        int i = 0;
686
        JAME_JTAG_STATE jtag_state = JAM_ILLEGAL_JTAG_STATE;
687
 
688
        for (i = 0; (jtag_state == JAM_ILLEGAL_JTAG_STATE) &&
689
                (i < (int) JAMC_JTAG_STATE_COUNT); ++i)
690
        {
691
                if (jam_strcmp(name, jam_jtag_state_table[i].string) == 0)
692
                {
693
                        jtag_state = jam_jtag_state_table[i].state;
694
                }
695
        }
696
 
697
        return (jtag_state);
698
}
699
 
700
/****************************************************************************/
701
/*                                                                                                                                                      */
702
 
703
JAM_RETURN_TYPE jam_do_wait_cycles
704
(
705
        long cycles,
706
        JAME_JTAG_STATE wait_state
707
)
708
 
709
/*                                                                                                                                                      */
710
/*      Description:    Causes JTAG hardware to loop in the specified stable    */
711
/*                                      state for the specified number of TCK clock cycles.             */
712
/*                                                                                                                                                      */
713
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
714
/*                                                                                                                                                      */
715
/****************************************************************************/
716
{
717
        int tms = 0;
718
        long count = 0L;
719
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
720
 
721
        if (jam_jtag_state != wait_state)
722
        {
723
                status = jam_goto_jtag_state(wait_state);
724
        }
725
 
726
        if (status == JAMC_SUCCESS)
727
        {
728
                /*
729
                *       Set TMS high to loop in RESET state
730
                *       Set TMS low to loop in any other stable state
731
                */
732
                tms = (wait_state == RESET) ? TMS_HIGH : TMS_LOW;
733
 
734
                for (count = 0L; count < cycles; count++)
735
                {
736
                        jam_jtag_io(tms, TDI_LOW, IGNORE_TDO);
737
                }
738
        }
739
 
740
        return (status);
741
}
742
 
743
/****************************************************************************/
744
/*                                                                                                                                                      */
745
 
746
JAM_RETURN_TYPE jam_do_wait_microseconds
747
(
748
        long microseconds,
749
        JAME_JTAG_STATE wait_state
750
)
751
 
752
/*                                                                                                                                                      */
753
/*      Description:    Causes JTAG hardware to sit in the specified stable             */
754
/*                                      state for the specified duration of real time.  If              */
755
/*                                      no JTAG operations have been performed yet, then only   */
756
/*                                      a delay is performed.  This permits the WAIT USECS              */
757
/*                                      statement to be used in VECTOR programs without causing */
758
/*                                      any JTAG operations.                                                                    */
759
/*                                                                                                                                                      */
760
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
761
/*                                                                                                                                                      */
762
/****************************************************************************/
763
{
764
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
765
 
766
        if ((jam_jtag_state != JAM_ILLEGAL_JTAG_STATE) &&
767
                (jam_jtag_state != wait_state))
768
        {
769
                status = jam_goto_jtag_state(wait_state);
770
        }
771
 
772
        if (status == JAMC_SUCCESS)
773
        {
774
                /*
775
                *       Wait for specified time interval
776
                */
777
                jam_delay(microseconds);
778
        }
779
 
780
        return (status);
781
}
782
 
783
/****************************************************************************/
784
/*                                                                                                                                                      */
785
 
786
void jam_jtag_concatenate_data
787
(
788
        char *buffer,
789
        long *preamble_data,
790
        long preamble_count,
791
        long *target_data,
792
        long start_index,
793
        long target_count,
794
        long *postamble_data,
795
        long postamble_count
796
)
797
 
798
/*                                                                                                                                                      */
799
/*      Description:    Copies preamble data, target data, and postamble data   */
800
/*                                      into one buffer for IR or DR scans.  Note that buffer   */
801
/*                                      is an array of char, while other arrays are of long             */
802
/*                                                                                                                                                      */
803
/*      Returns:                nothing                                                                                                 */
804
/*                                                                                                                                                      */
805
/****************************************************************************/
806
{
807
        long i = 0L;
808
        long j = 0L;
809
        long k = 0L;
810
 
811
        for (i = 0L; i < preamble_count; ++i)
812
        {
813
                if (preamble_data[i >> 5] & (1L << (i & 0x1f)))
814
                {
815
                        buffer[i >> 3] |= (1 << (i & 7));
816
                }
817
                else
818
                {
819
                        buffer[i >> 3] &= ~(unsigned int) (1 << (i & 7));
820
                }
821
        }
822
 
823
        j = start_index;
824
        k = preamble_count + target_count;
825
        for (; i < k; ++i, ++j)
826
        {
827
                if (target_data[j >> 5] & (1L << (j & 0x1f)))
828
                {
829
                        buffer[i >> 3] |= (1 << (i & 7));
830
                }
831
                else
832
                {
833
                        buffer[i >> 3] &= ~(unsigned int) (1 << (i & 7));
834
                }
835
        }
836
 
837
        j = 0L;
838
        k = preamble_count + target_count + postamble_count;
839
        for (; i < k; ++i, ++j)
840
        {
841
                if (postamble_data[j >> 5] & (1L << (j & 0x1f)))
842
                {
843
                        buffer[i >> 3] |= (1 << (i & 7));
844
                }
845
                else
846
                {
847
                        buffer[i >> 3] &= ~(unsigned int) (1 << (i & 7));
848
                }
849
        }
850
}
851
 
852
/****************************************************************************/
853
/*                                                                                                                                                      */
854
 
855
void jam_jtag_extract_target_data
856
(
857
        char *buffer,
858
        long *target_data,
859
        long start_index,
860
        long preamble_count,
861
        long target_count
862
)
863
 
864
/*                                                                                                                                                      */
865
/*      Description:    Copies target data from scan buffer, filtering out              */
866
/*                                      preamble and postamble data.   Note that buffer is an   */
867
/*                                      array of char, while target_data is an array of long    */
868
/*                                                                                                                                                      */
869
/*      Returns:                nothing                                                                                                 */
870
/*                                                                                                                                                      */
871
/****************************************************************************/
872
{
873
        long i = 0L;
874
        long j = 0L;
875
        long k = 0L;
876
 
877
        j = preamble_count;
878
        k = start_index + target_count;
879
        for (i = start_index; i < k; ++i, ++j)
880
        {
881
                if (buffer[j >> 3] & (1 << (j & 7)))
882
                {
883
                        target_data[i >> 5] |= (1L << (i & 0x1f));
884
                }
885
                else
886
                {
887
                        target_data[i >> 5] &= ~(unsigned long) (1L << (i & 0x1f));
888
                }
889
        }
890
}
891
 
892
int jam_jtag_drscan
893
(
894
        int start_state,
895
        int count,
896
        char *tdi,
897
        char *tdo
898
)
899
{
900
        int i = 0;
901
        int tdo_bit = 0;
902
        int status = 1;
903
 
904
        /*
905
        *       First go to DRSHIFT state
906
        */
907
        switch (start_state)
908
        {
909
        case 0:                                          /* IDLE */
910
                jam_jtag_io(1, 0, 0);     /* DRSELECT */
911
                jam_jtag_io(0, 0, 0);      /* DRCAPTURE */
912
                jam_jtag_io(0, 0, 0);      /* DRSHIFT */
913
                break;
914
 
915
        case 1:                                         /* DRPAUSE */
916
                jam_jtag_io(1, 0, 0);     /* DREXIT2 */
917
                jam_jtag_io(0, 0, 0);      /* DRSHIFT */
918
                break;
919
 
920
        case 2:                                         /* IRPAUSE */
921
                jam_jtag_io(1, 0, 0);     /* IREXIT2 */
922
                jam_jtag_io(1, 0, 0);     /* IRUPDATE */
923
                jam_jtag_io(1, 0, 0);     /* DRSELECT */
924
                jam_jtag_io(0, 0, 0);      /* DRCAPTURE */
925
                jam_jtag_io(0, 0, 0);      /* DRSHIFT */
926
                break;
927
 
928
        default:
929
                status = 0;
930
        }
931
 
932
        if (status)
933
        {
934
                /* loop in the SHIFT-DR state */
935
                for (i = 0; i < count; i++)
936
                {
937
                        tdo_bit = jam_jtag_io(
938
                                (i == count - 1),
939
                                tdi[i >> 3] & (1 << (i & 7)),
940
                                (tdo != NULL));
941
 
942
                        if (tdo != NULL)
943
                        {
944
                                if (tdo_bit)
945
                                {
946
                                        tdo[i >> 3] |= (1 << (i & 7));
947
                                }
948
                                else
949
                                {
950
                                        tdo[i >> 3] &= ~(unsigned int) (1 << (i & 7));
951
                                }
952
                        }
953
                }
954
 
955
                jam_jtag_io(0, 0, 0);      /* DRPAUSE */
956
        }
957
 
958
        return (status);
959
}
960
 
961
int jam_jtag_irscan
962
(
963
        int start_state,
964
        int count,
965
        char *tdi,
966
        char *tdo
967
)
968
{
969
        int i = 0;
970
        int tdo_bit = 0;
971
        int status = 1;
972
 
973
        /*
974
        *       First go to IRSHIFT state
975
        */
976
        switch (start_state)
977
        {
978
        case 0:                                          /* IDLE */
979
                jam_jtag_io(1, 0, 0);     /* DRSELECT */
980
                jam_jtag_io(1, 0, 0);     /* IRSELECT */
981
                jam_jtag_io(0, 0, 0);      /* IRCAPTURE */
982
                jam_jtag_io(0, 0, 0);      /* IRSHIFT */
983
                break;
984
 
985
        case 1:                                         /* DRPAUSE */
986
                jam_jtag_io(1, 0, 0);     /* DREXIT2 */
987
                jam_jtag_io(1, 0, 0);     /* DRUPDATE */
988
                jam_jtag_io(1, 0, 0);     /* DRSELECT */
989
                jam_jtag_io(1, 0, 0);     /* IRSELECT */
990
                jam_jtag_io(0, 0, 0);      /* IRCAPTURE */
991
                jam_jtag_io(0, 0, 0);      /* IRSHIFT */
992
                break;
993
 
994
        case 2:                                         /* IRPAUSE */
995
                jam_jtag_io(1, 0, 0);     /* IREXIT2 */
996
                jam_jtag_io(0, 0, 0);      /* IRSHIFT */
997
                break;
998
 
999
        default:
1000
                status = 0;
1001
        }
1002
 
1003
        if (status)
1004
        {
1005
                /* loop in the SHIFT-IR state */
1006
                for (i = 0; i < count; i++)
1007
                {
1008
                        tdo_bit = jam_jtag_io(
1009
                                (i == count - 1),
1010
                                tdi[i >> 3] & (1 << (i & 7)),
1011
                                (tdo != NULL));
1012
 
1013
                        if (tdo != NULL)
1014
                        {
1015
                                if (tdo_bit)
1016
                                {
1017
                                        tdo[i >> 3] |= (1 << (i & 7));
1018
                                }
1019
                                else
1020
                                {
1021
                                        tdo[i >> 3] &= ~(unsigned int) (1 << (i & 7));
1022
                                }
1023
                        }
1024
                }
1025
 
1026
                jam_jtag_io(0, 0, 0);      /* IRPAUSE */
1027
        }
1028
 
1029
        return (status);
1030
}
1031
 
1032
/****************************************************************************/
1033
/*                                                                                                                                                      */
1034
 
1035
JAM_RETURN_TYPE jam_do_irscan
1036
(
1037
        long count,
1038
        long *data,
1039
        long start_index
1040
)
1041
 
1042
/*                                                                                                                                                      */
1043
/*      Description:    Shifts data into instruction register                                   */
1044
/*                                                                                                                                                      */
1045
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
1046
/*                                                                                                                                                      */
1047
/****************************************************************************/
1048
{
1049
        int start_code = 0;
1050
        int alloc_chars = 0;
1051
        int shift_count = (int) (jam_ir_preamble + count + jam_ir_postamble);
1052
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
1053
        JAME_JTAG_STATE start_state = JAM_ILLEGAL_JTAG_STATE;
1054
 
1055
        switch (jam_jtag_state)
1056
        {
1057
        case JAM_ILLEGAL_JTAG_STATE:
1058
        case RESET:
1059
        case IDLE:
1060
                start_code = 0;
1061
                start_state = IDLE;
1062
                break;
1063
 
1064
        case DRSELECT:
1065
        case DRCAPTURE:
1066
        case DRSHIFT:
1067
        case DREXIT1:
1068
        case DRPAUSE:
1069
        case DREXIT2:
1070
        case DRUPDATE:
1071
                start_code = 1;
1072
                start_state = DRPAUSE;
1073
                break;
1074
 
1075
        case IRSELECT:
1076
        case IRCAPTURE:
1077
        case IRSHIFT:
1078
        case IREXIT1:
1079
        case IRPAUSE:
1080
        case IREXIT2:
1081
        case IRUPDATE:
1082
                start_code = 2;
1083
                start_state = IRPAUSE;
1084
                break;
1085
 
1086
        default:
1087
                status = JAMC_INTERNAL_ERROR;
1088
                break;
1089
        }
1090
 
1091
        if (status == JAMC_SUCCESS)
1092
        {
1093
                if (jam_jtag_state != start_state)
1094
                {
1095
                        status = jam_goto_jtag_state(start_state);
1096
                }
1097
        }
1098
 
1099
        if (status == JAMC_SUCCESS)
1100
        {
1101
                if (jam_workspace != NULL)
1102
                {
1103
                        if (shift_count > JAMC_MAX_JTAG_IR_LENGTH)
1104
                        {
1105
                                status = JAMC_OUT_OF_MEMORY;
1106
                        }
1107
                }
1108
                else if (shift_count > jam_ir_length)
1109
                {
1110
                        alloc_chars = (shift_count + 7) >> 3;
1111
                        jam_free(jam_ir_buffer);
1112
                        jam_ir_buffer = (char *) jam_malloc(alloc_chars);
1113
 
1114
                        if (jam_ir_buffer == NULL)
1115
                        {
1116
                                status = JAMC_OUT_OF_MEMORY;
1117
                        }
1118
                        else
1119
                        {
1120
                                jam_ir_length = alloc_chars * 8;
1121
                        }
1122
                }
1123
        }
1124
 
1125
        if (status == JAMC_SUCCESS)
1126
        {
1127
                /*
1128
                *       Copy preamble data, IR data, and postamble data into a buffer
1129
                */
1130
                jam_jtag_concatenate_data
1131
                (
1132
                        jam_ir_buffer,
1133
                        jam_ir_preamble_data,
1134
                        jam_ir_preamble,
1135
                        data,
1136
                        start_index,
1137
                        count,
1138
                        jam_ir_postamble_data,
1139
                        jam_ir_postamble
1140
                );
1141
 
1142
                /*
1143
                *       Do the IRSCAN
1144
                */
1145
                if(verbose&8) printf("jam_do_irscan jam_ir_preamble=%d\n",jam_ir_preamble);
1146
                jam_jtag_irscan
1147
                (
1148
                        start_code,
1149
                        shift_count,
1150
                        jam_ir_buffer,
1151
                        NULL
1152
                );
1153
 
1154
                /* jam_jtag_irscan() always ends in IRPAUSE state */
1155
                jam_jtag_state = IRPAUSE;
1156
        }
1157
 
1158
        if (status == JAMC_SUCCESS)
1159
        {
1160
                if (jam_irstop_state != IRPAUSE)
1161
                {
1162
                        status = jam_goto_jtag_state(jam_irstop_state);
1163
                }
1164
        }
1165
 
1166
        return (status);
1167
}
1168
 
1169
/****************************************************************************/
1170
/*                                                                                                                                                      */
1171
 
1172
JAM_RETURN_TYPE jam_swap_ir
1173
(
1174
        long count,
1175
        long *in_data,
1176
        long in_index,
1177
        long *out_data,
1178
        long out_index
1179
)
1180
 
1181
/*                                                                                                                                                      */
1182
/*      Description:    Shifts data into instruction register, capturing output */
1183
/*                                      data                                                                                                    */
1184
/*                                                                                                                                                      */
1185
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
1186
/*                                                                                                                                                      */
1187
/****************************************************************************/
1188
{
1189
        int start_code = 0;
1190
        int alloc_chars = 0;
1191
        int shift_count = (int) (jam_ir_preamble + count + jam_ir_postamble);
1192
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
1193
        JAME_JTAG_STATE start_state = JAM_ILLEGAL_JTAG_STATE;
1194
 
1195
        switch (jam_jtag_state)
1196
        {
1197
        case JAM_ILLEGAL_JTAG_STATE:
1198
        case RESET:
1199
        case IDLE:
1200
                start_code = 0;
1201
                start_state = IDLE;
1202
                break;
1203
 
1204
        case DRSELECT:
1205
        case DRCAPTURE:
1206
        case DRSHIFT:
1207
        case DREXIT1:
1208
        case DRPAUSE:
1209
        case DREXIT2:
1210
        case DRUPDATE:
1211
                start_code = 1;
1212
                start_state = DRPAUSE;
1213
                break;
1214
 
1215
        case IRSELECT:
1216
        case IRCAPTURE:
1217
        case IRSHIFT:
1218
        case IREXIT1:
1219
        case IRPAUSE:
1220
        case IREXIT2:
1221
        case IRUPDATE:
1222
                start_code = 2;
1223
                start_state = IRPAUSE;
1224
                break;
1225
 
1226
        default:
1227
                status = JAMC_INTERNAL_ERROR;
1228
                break;
1229
        }
1230
 
1231
        if (status == JAMC_SUCCESS)
1232
        {
1233
                if (jam_jtag_state != start_state)
1234
                {
1235
                        status = jam_goto_jtag_state(start_state);
1236
                }
1237
        }
1238
 
1239
        if (status == JAMC_SUCCESS)
1240
        {
1241
                if (jam_workspace != NULL)
1242
                {
1243
                        if (shift_count > JAMC_MAX_JTAG_IR_LENGTH)
1244
                        {
1245
                                status = JAMC_OUT_OF_MEMORY;
1246
                        }
1247
                }
1248
                else if (shift_count > jam_ir_length)
1249
                {
1250
                        alloc_chars = (shift_count + 7) >> 3;
1251
                        jam_free(jam_ir_buffer);
1252
                        jam_ir_buffer = (char *) jam_malloc(alloc_chars);
1253
 
1254
                        if (jam_ir_buffer == NULL)
1255
                        {
1256
                                status = JAMC_OUT_OF_MEMORY;
1257
                        }
1258
                        else
1259
                        {
1260
                                jam_ir_length = alloc_chars * 8;
1261
                        }
1262
                }
1263
        }
1264
 
1265
        if (status == JAMC_SUCCESS)
1266
        {
1267
                /*
1268
                *       Copy preamble data, IR data, and postamble data into a buffer
1269
                */
1270
                jam_jtag_concatenate_data
1271
                (
1272
                        jam_ir_buffer,
1273
                        jam_ir_preamble_data,
1274
                        jam_ir_preamble,
1275
                        in_data,
1276
                        in_index,
1277
                        count,
1278
                        jam_ir_postamble_data,
1279
                        jam_ir_postamble
1280
                );
1281
 
1282
                /*
1283
                *       Do the IRSCAN
1284
                */
1285
        if(verbose&8) printf("jam_swap_ir jam_ir_preamble=%d\n",jam_ir_preamble);
1286
                jam_jtag_irscan
1287
                (
1288
                        start_code,
1289
                        shift_count,
1290
                        jam_ir_buffer,
1291
                        jam_ir_buffer
1292
                );
1293
 
1294
                /* jam_jtag_irscan() always ends in IRPAUSE state */
1295
                jam_jtag_state = IRPAUSE;
1296
        }
1297
 
1298
        if (status == JAMC_SUCCESS)
1299
        {
1300
                if (jam_irstop_state != IRPAUSE)
1301
                {
1302
                        status = jam_goto_jtag_state(jam_irstop_state);
1303
                }
1304
        }
1305
 
1306
        if (status == JAMC_SUCCESS)
1307
        {
1308
                /*
1309
                *       Now extract the returned data from the buffer
1310
                */
1311
                jam_jtag_extract_target_data
1312
                (
1313
                        jam_ir_buffer,
1314
                        out_data,
1315
                        out_index,
1316
                        jam_ir_preamble,
1317
                        count
1318
                );
1319
        }
1320
 
1321
        return (status);
1322
}
1323
 
1324
/****************************************************************************/
1325
/*                                                                                                                                                      */
1326
 
1327
JAM_RETURN_TYPE jam_do_drscan
1328
(
1329
        long count,
1330
        long *data,
1331
        long start_index
1332
)
1333
 
1334
/*                                                                                                                                                      */
1335
/*      Description:    Shifts data into data register (ignoring output data)   */
1336
/*                                                                                                                                                      */
1337
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
1338
/*                                                                                                                                                      */
1339
/****************************************************************************/
1340
{
1341
        int start_code = 0;
1342
        int alloc_chars = 0;
1343
        int shift_count = (int) (jam_dr_preamble + count + jam_dr_postamble);
1344
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
1345
        JAME_JTAG_STATE start_state = JAM_ILLEGAL_JTAG_STATE;
1346
 
1347
        switch (jam_jtag_state)
1348
        {
1349
        case JAM_ILLEGAL_JTAG_STATE:
1350
        case RESET:
1351
        case IDLE:
1352
                start_code = 0;
1353
                start_state = IDLE;
1354
                break;
1355
 
1356
        case DRSELECT:
1357
        case DRCAPTURE:
1358
        case DRSHIFT:
1359
        case DREXIT1:
1360
        case DRPAUSE:
1361
        case DREXIT2:
1362
        case DRUPDATE:
1363
                start_code = 1;
1364
                start_state = DRPAUSE;
1365
                break;
1366
 
1367
        case IRSELECT:
1368
        case IRCAPTURE:
1369
        case IRSHIFT:
1370
        case IREXIT1:
1371
        case IRPAUSE:
1372
        case IREXIT2:
1373
        case IRUPDATE:
1374
                start_code = 2;
1375
                start_state = IRPAUSE;
1376
                break;
1377
 
1378
        default:
1379
                status = JAMC_INTERNAL_ERROR;
1380
                break;
1381
        }
1382
 
1383
        if (status == JAMC_SUCCESS)
1384
        {
1385
                if (jam_jtag_state != start_state)
1386
                {
1387
                        status = jam_goto_jtag_state(start_state);
1388
                }
1389
        }
1390
 
1391
        if (status == JAMC_SUCCESS)
1392
        {
1393
                if (jam_workspace != NULL)
1394
                {
1395
                        if (shift_count > JAMC_MAX_JTAG_DR_LENGTH)
1396
                        {
1397
                                status = JAMC_OUT_OF_MEMORY;
1398
                        }
1399
                }
1400
                else if (shift_count > jam_dr_length)
1401
                {
1402
                        alloc_chars = (shift_count + 7) >> 3;
1403
                        jam_free(jam_dr_buffer);
1404
                        jam_dr_buffer = (char *) jam_malloc(alloc_chars);
1405
 
1406
                        if (jam_dr_buffer == NULL)
1407
                        {
1408
                                status = JAMC_OUT_OF_MEMORY;
1409
                        }
1410
                        else
1411
                        {
1412
                                jam_dr_length = alloc_chars * 8;
1413
                        }
1414
                }
1415
        }
1416
 
1417
        if (status == JAMC_SUCCESS)
1418
        {
1419
                /*
1420
                *       Copy preamble data, DR data, and postamble data into a buffer
1421
                */
1422
                jam_jtag_concatenate_data
1423
                (
1424
                        jam_dr_buffer,
1425
                        jam_dr_preamble_data,
1426
                        jam_dr_preamble,
1427
                        data,
1428
                        start_index,
1429
                        count,
1430
                        jam_dr_postamble_data,
1431
                        jam_dr_postamble
1432
                );
1433
 
1434
                /*
1435
                *       Do the DRSCAN
1436
                */
1437
                jam_jtag_drscan
1438
                (
1439
                        start_code,
1440
                        shift_count,
1441
                        jam_dr_buffer,
1442
                        NULL
1443
                );
1444
 
1445
                /* jam_jtag_drscan() always ends in DRPAUSE state */
1446
                jam_jtag_state = DRPAUSE;
1447
        }
1448
 
1449
        if (status == JAMC_SUCCESS)
1450
        {
1451
                if (jam_drstop_state != DRPAUSE)
1452
                {
1453
                        status = jam_goto_jtag_state(jam_drstop_state);
1454
                }
1455
        }
1456
 
1457
        return (status);
1458
}
1459
 
1460
/****************************************************************************/
1461
/*                                                                                                                                                      */
1462
 
1463
JAM_RETURN_TYPE jam_swap_dr
1464
(
1465
        long count,
1466
        long *in_data,
1467
        long in_index,
1468
        long *out_data,
1469
        long out_index
1470
)
1471
 
1472
/*                                                                                                                                                      */
1473
/*      Description:    Shifts data into data register, capturing output data   */
1474
/*                                                                                                                                                      */
1475
/*      Returns:                JAMC_SUCCESS for success, else appropriate error code   */
1476
/*                                                                                                                                                      */
1477
/****************************************************************************/
1478
{
1479
        int start_code = 0;
1480
        int alloc_chars = 0;
1481
        int shift_count = (int) (jam_dr_preamble + count + jam_dr_postamble);
1482
        JAM_RETURN_TYPE status = JAMC_SUCCESS;
1483
        JAME_JTAG_STATE start_state = JAM_ILLEGAL_JTAG_STATE;
1484
 
1485
        switch (jam_jtag_state)
1486
        {
1487
        case JAM_ILLEGAL_JTAG_STATE:
1488
        case RESET:
1489
        case IDLE:
1490
                start_code = 0;
1491
                start_state = IDLE;
1492
                break;
1493
 
1494
        case DRSELECT:
1495
        case DRCAPTURE:
1496
        case DRSHIFT:
1497
        case DREXIT1:
1498
        case DRPAUSE:
1499
        case DREXIT2:
1500
        case DRUPDATE:
1501
                start_code = 1;
1502
                start_state = DRPAUSE;
1503
                break;
1504
 
1505
        case IRSELECT:
1506
        case IRCAPTURE:
1507
        case IRSHIFT:
1508
        case IREXIT1:
1509
        case IRPAUSE:
1510
        case IREXIT2:
1511
        case IRUPDATE:
1512
                start_code = 2;
1513
                start_state = IRPAUSE;
1514
                break;
1515
 
1516
        default:
1517
                status = JAMC_INTERNAL_ERROR;
1518
                break;
1519
        }
1520
 
1521
        if (status == JAMC_SUCCESS)
1522
        {
1523
                if (jam_jtag_state != start_state)
1524
                {
1525
                        status = jam_goto_jtag_state(start_state);
1526
                }
1527
        }
1528
 
1529
        if (status == JAMC_SUCCESS)
1530
        {
1531
                if (jam_workspace != NULL)
1532
                {
1533
                        if (shift_count > JAMC_MAX_JTAG_DR_LENGTH)
1534
                        {
1535
                                status = JAMC_OUT_OF_MEMORY;
1536
                        }
1537
                }
1538
                else if (shift_count > jam_dr_length)
1539
                {
1540
                        alloc_chars = (shift_count + 7) >> 3;
1541
                        jam_free(jam_dr_buffer);
1542
                        jam_dr_buffer = (char *) jam_malloc(alloc_chars);
1543
 
1544
                        if (jam_dr_buffer == NULL)
1545
                        {
1546
                                status = JAMC_OUT_OF_MEMORY;
1547
                        }
1548
                        else
1549
                        {
1550
                                jam_dr_length = alloc_chars * 8;
1551
                        }
1552
                }
1553
        }
1554
 
1555
        if (status == JAMC_SUCCESS)
1556
        {
1557
                /*
1558
                *       Copy preamble data, DR data, and postamble data into a buffer
1559
                */
1560
                jam_jtag_concatenate_data
1561
                (
1562
                        jam_dr_buffer,
1563
                        jam_dr_preamble_data,
1564
                        jam_dr_preamble,
1565
                        in_data,
1566
                        in_index,
1567
                        count,
1568
                        jam_dr_postamble_data,
1569
                        jam_dr_postamble
1570
                );
1571
 
1572
                /*
1573
                *       Do the DRSCAN
1574
                */
1575
                jam_jtag_drscan
1576
                (
1577
                        start_code,
1578
                        shift_count,
1579
                        jam_dr_buffer,
1580
                        jam_dr_buffer
1581
                );
1582
 
1583
                /* jam_jtag_drscan() always ends in DRPAUSE state */
1584
                jam_jtag_state = DRPAUSE;
1585
        }
1586
 
1587
        if (status == JAMC_SUCCESS)
1588
        {
1589
                if (jam_drstop_state != DRPAUSE)
1590
                {
1591
                        status = jam_goto_jtag_state(jam_drstop_state);
1592
                }
1593
        }
1594
 
1595
        if (status == JAMC_SUCCESS)
1596
        {
1597
                /*
1598
                *       Now extract the returned data from the buffer
1599
                */
1600
                jam_jtag_extract_target_data
1601
                (
1602
                        jam_dr_buffer,
1603
                        out_data,
1604
                        out_index,
1605
                        jam_dr_preamble,
1606
                        count
1607
                );
1608
        }
1609
 
1610
        return (status);
1611
}
1612
 
1613
/****************************************************************************/
1614
/*                                                                                                                                                      */
1615
 
1616
void jam_free_jtag_padding_buffers(int reset_jtag)
1617
 
1618
/*                                                                                                                                                      */
1619
/*      Description:    Frees memory allocated for JTAG IR and DR buffers               */
1620
/*                                                                                                                                                      */
1621
/*      Returns:                nothing                                                                                                 */
1622
/*                                                                                                                                                      */
1623
/****************************************************************************/
1624
{
1625
        /*
1626
        *       If the JTAG interface was used, reset it to TLR
1627
        */
1628
        if (reset_jtag && (jam_jtag_state != JAM_ILLEGAL_JTAG_STATE))
1629
        {
1630
                jam_jtag_reset_idle();
1631
        }
1632
 
1633
        if (jam_workspace == NULL)
1634
        {
1635
                if (jam_dr_preamble_data != NULL)
1636
                {
1637
                        jam_free(jam_dr_preamble_data);
1638
                        jam_dr_preamble_data = NULL;
1639
                }
1640
 
1641
                if (jam_dr_postamble_data != NULL)
1642
                {
1643
                        jam_free(jam_dr_postamble_data);
1644
                        jam_dr_postamble_data = NULL;
1645
                }
1646
 
1647
                if (jam_dr_buffer != NULL)
1648
                {
1649
                        jam_free(jam_dr_buffer);
1650
                        jam_dr_buffer = NULL;
1651
                }
1652
 
1653
                if (jam_ir_preamble_data != NULL)
1654
                {
1655
                        jam_free(jam_ir_preamble_data);
1656
                        jam_ir_preamble_data = NULL;
1657
                }
1658
 
1659
                if (jam_ir_postamble_data != NULL)
1660
                {
1661
                        jam_free(jam_ir_postamble_data);
1662
                        jam_ir_postamble_data = NULL;
1663
                }
1664
 
1665
                if (jam_ir_buffer != NULL)
1666
                {
1667
                        jam_free(jam_ir_buffer);
1668
                        jam_ir_buffer = NULL;
1669
                }
1670
        }
1671
}

powered by: WebSVN 2.1.0

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