Author: Michael Bruck <mbruck@digenius.de>
[openocd.git] / src / jtag / jtag.c
index f5428aab344a8be2a3c46725e9b3a24176ab33fc..4b2e85c1d5b51749ba59a1416fd0e81002efa931 100644 (file)
@@ -531,6 +531,20 @@ void cmd_queue_free(void)
        cmd_queue_pages = NULL;
 }
 
+/**
+ * Copy a scan_field_t for insertion into the queue.
+ *
+ * This allocates a new copy of out_value using cmd_queue_alloc.
+ */
+static void cmd_queue_scan_field_clone(scan_field_t * dst, const scan_field_t * src)
+{
+       dst->tap                = src->tap;
+       dst->num_bits   = src->num_bits;
+       dst->out_value  = buf_cpy(src->out_value, cmd_queue_alloc(CEIL(src->num_bits, 8)), src->num_bits);
+       dst->in_value   = src->in_value;
+}
+
+
 static void jtag_prelude1(void)
 {
        if (jtag_trst == 1)
@@ -604,9 +618,7 @@ void jtag_add_ir_scan(int in_num_fields, scan_field_t *in_fields, tap_state_t st
  */
 int MINIDRIVER(interface_jtag_add_ir_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
 {
-       int nth_tap;
-
-       int num_taps = jtag_NumEnabledTaps();
+       size_t num_taps = jtag_NumEnabledTaps();
 
        jtag_command_t * cmd            = cmd_queue_alloc(sizeof(jtag_command_t));
        scan_command_t * scan           = cmd_queue_alloc(sizeof(scan_command_t));
@@ -622,47 +634,54 @@ int MINIDRIVER(interface_jtag_add_ir_scan)(int in_num_fields, const scan_field_t
        scan->fields                    = out_fields;
        scan->end_state                 = state;
 
-       nth_tap = -1;
 
-       for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
-       {
-               int found = 0;
+       scan_field_t * field = out_fields;      /* keep track where we insert data */
 
-               nth_tap++;
+       /* loop over all enabled TAPs */
 
-               assert(nth_tap < num_taps);
+       for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
+       {
+               /* search the input field list for fields for the current TAP */
 
-               size_t scan_size                                = tap->ir_length;
-               scan->fields[nth_tap].tap               = tap;
-               scan->fields[nth_tap].num_bits  = scan_size;
-               scan->fields[nth_tap].in_value  = NULL; /* do not collect input for tap's in bypass */
+               bool found = false;
 
-               /* search the list */
                for (int j = 0; j < in_num_fields; j++)
                {
-                       if (tap == in_fields[j].tap)
-                       {
-                               found = 1;
-                               scan->fields[nth_tap].in_value  = in_fields[j].in_value;
-                               scan->fields[nth_tap].out_value = buf_cpy(in_fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
+                       if (tap != in_fields[j].tap)
+                               continue;
 
-                               tap->bypass = 0;
-                               break;
-                       }
+                       /* if TAP is listed in input fields, copy the value */
+
+                       found = true;
+
+                       tap->bypass = 0;
+
+                       assert(in_fields[j].num_bits == tap->ir_length); /* input fields must have the same length as the TAP's IR */
+
+                       cmd_queue_scan_field_clone(field, in_fields + j);
+
+                       break;
                }
 
                if (!found)
                {
-                       /* if a tap isn't listed, set it to BYPASS */
-                       scan->fields[nth_tap].out_value = buf_set_ones(cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
+                       /* if a TAP isn't listed in input fields, set it to BYPASS */
+
                        tap->bypass = 1;
+
+                       field->tap                      = tap;
+                       field->num_bits         = tap->ir_length;
+                       field->out_value        = buf_set_ones(cmd_queue_alloc(CEIL(tap->ir_length, 8)), tap->ir_length);
+                       field->in_value         = NULL; /* do not collect input for tap's in bypass */
                }
 
                /* update device information */
-               buf_cpy(scan->fields[nth_tap].out_value, tap->cur_instr, scan_size);
+               buf_cpy(field->out_value, tap->cur_instr, tap->ir_length);
+
+               field++;
        }
 
-       assert(nth_tap == (num_taps - 1));
+       assert(field == out_fields + num_taps); /* paranoia: jtag_NumEnabledTaps() and jtag_NextEnabledTap() not in sync */
 
        return ERROR_OK;
 }
@@ -707,14 +726,7 @@ int MINIDRIVER(interface_jtag_add_plain_ir_scan)(int in_num_fields, const scan_f
        scan->end_state                 = state;
 
        for (int i = 0; i < in_num_fields; i++)
-       {
-               int num_bits = in_fields[i].num_bits;
-               int num_bytes = CEIL(in_fields[i].num_bits, 8);
-               scan->fields[i].tap = in_fields[i].tap;
-               scan->fields[i].num_bits = num_bits;
-               scan->fields[i].out_value = buf_cpy(in_fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
-               scan->fields[i].in_value = in_fields[i].in_value;
-       }
+               cmd_queue_scan_field_clone(out_fields + i, in_fields + i);
 
        return ERROR_OK;
 }
@@ -817,10 +829,6 @@ void jtag_add_dr_scan(int in_num_fields, const scan_field_t *in_fields, tap_stat
  */
 int MINIDRIVER(interface_jtag_add_dr_scan)(int in_num_fields, const scan_field_t *in_fields, tap_state_t state)
 {
-       int j;
-       int nth_tap;
-       int field_count = 0;
-
        /* count devices in bypass */
 
        size_t bypass_devices = 0;
@@ -845,58 +853,46 @@ int MINIDRIVER(interface_jtag_add_dr_scan)(int in_num_fields, const scan_field_t
        scan->fields                    = out_fields;
        scan->end_state                 = state;
 
-       nth_tap = -1;
+
+       scan_field_t * field = out_fields;      /* keep track where we insert data */
+
+       /* loop over all enabled TAPs */
 
        for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
        {
-               nth_tap++;
-
-               int found = 0;
-               scan->fields[field_count].tap = tap;
+               /* if TAP is not bypassed insert matching input fields */
 
-               for (j = 0; j < in_num_fields; j++)
+               if (!tap->bypass)
                {
-                       if (tap == in_fields[j].tap)
-                       {
-                               found = 1;
-                               size_t scan_size = in_fields[j].num_bits;
-                               scan->fields[field_count].num_bits      = scan_size;
-                               scan->fields[field_count].out_value     = buf_cpy(in_fields[j].out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
-                               scan->fields[field_count].in_value      = in_fields[j].in_value;
-                               field_count++;
-                       }
-               }
-               if (!found)
-               {
-#ifdef _DEBUG_JTAG_IO_
-                       /* if a device isn't listed, the BYPASS register should be selected */
-                       if (! tap->bypass)
+                       scan_field_t * start_field = field;     /* keep initial position for assert() */
+
+                       for (int j = 0; j < in_num_fields; j++)
                        {
-                               LOG_ERROR("BUG: no scan data for a device not in BYPASS");
-                               exit(-1);
+                               if (tap != in_fields[j].tap)
+                                       continue;
+
+                               cmd_queue_scan_field_clone(field, in_fields + j);
+
+                               field++;
                        }
-#endif
-                       /* program the scan field to 1 bit length, and ignore it's value */
-                       scan->fields[field_count].num_bits              = 1;
-                       scan->fields[field_count].out_value             = NULL;
-                       scan->fields[field_count].in_value              = NULL;
-                       field_count++;
+
+                       assert(field > start_field);    /* must have at least one input field per not bypassed TAP */
                }
+               
+               /* if a TAP is bypassed, generated a dummy bit*/
                else
                {
-#ifdef _DEBUG_JTAG_IO_
-                       /* if a device is listed, the BYPASS register must not be selected */
-                       if (tap->bypass)
-                       {
-                               LOG_ERROR("BUG: scan data for a device in BYPASS");
-                               exit(-1);
-                       }
-#endif
+                       field->tap                      = tap;
+                       field->num_bits         = 1;
+                       field->out_value        = NULL;
+                       field->in_value         = NULL;
+
+                       field++;
                }
        }
 
-       /* field_count represents the true number of fields setup*/
-       scan->num_fields = field_count;
+       assert(field == out_fields + scan->num_fields); /* no superfluous input fields permitted */
+
        return ERROR_OK;
 }
 
@@ -922,9 +918,6 @@ void MINIDRIVER(interface_jtag_add_dr_out)(jtag_tap_t *target_tap,
                const u32 *value,
                tap_state_t end_state)
 {
-       int nth_tap;
-       int field_count = 0;
-
        /* count devices in bypass */
 
        size_t bypass_devices = 0;
@@ -950,50 +943,52 @@ void MINIDRIVER(interface_jtag_add_dr_out)(jtag_tap_t *target_tap,
        scan->fields                    = out_fields;
        scan->end_state                 = end_state;
 
-       nth_tap = -1;
+
+       bool target_tap_match   = false;
+
+       scan_field_t * field = out_fields;      /* keep track where we insert data */
+
+       /* loop over all enabled TAPs */
 
        for (jtag_tap_t * tap = jtag_NextEnabledTap(NULL); tap != NULL; tap = jtag_NextEnabledTap(tap))
        {
-               nth_tap++;
-               scan->fields[field_count].tap = tap;
+               /* if TAP is not bypassed insert matching input fields */
 
-               if (tap == target_tap)
+               if (!tap->bypass)
                {
-#ifdef _DEBUG_JTAG_IO_
-                       /* if a device is listed, the BYPASS register must not be selected */
-                       if (tap->bypass)
-                       {
-                               LOG_ERROR("BUG: scan data for a device in BYPASS");
-                               exit(-1);
-                       }
-#endif
+                       assert(tap == target_tap); /* target_tap must match the one not bypassed TAP */
+
+                       target_tap_match = true;
+
                        for (int j = 0; j < in_num_fields; j++)
                        {
                                u8 out_value[4];
                                size_t scan_size = num_bits[j];
                                buf_set_u32(out_value, 0, scan_size, value[j]);
-                               scan->fields[field_count].num_bits              = scan_size;
-                               scan->fields[field_count].out_value             = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
-                               scan->fields[field_count].in_value              = NULL;
-                               field_count++;
+
+                               field->tap                      = tap;
+                               field->num_bits         = scan_size;
+                               field->out_value        = buf_cpy(out_value, cmd_queue_alloc(CEIL(scan_size, 8)), scan_size);
+                               field->in_value         = NULL;
+
+                               field++;
                        }
-               } else
+               }
+
+               /* if a TAP is bypassed, generated a dummy bit*/
+               else
                {
-#ifdef _DEBUG_JTAG_IO_
-                       /* if a device isn't listed, the BYPASS register should be selected */
-                       if (! tap->bypass)
-                       {
-                               LOG_ERROR("BUG: no scan data for a device not in BYPASS");
-                               exit(-1);
-                       }
-#endif
-                       /* program the scan field to 1 bit length, and ignore it's value */
-                       scan->fields[field_count].num_bits                      = 1;
-                       scan->fields[field_count].out_value                     = NULL;
-                       scan->fields[field_count].in_value                      = NULL;
-                       field_count++;
+
+                       field->tap                              = tap;
+                       field->num_bits                 = 1;
+                       field->out_value                = NULL;
+                       field->in_value                 = NULL;
+
+                       field++;
                }
        }
+
+       assert(target_tap_match);       /* target_tap should be enabled and not bypassed */
 }
 
 
@@ -1036,14 +1031,7 @@ int MINIDRIVER(interface_jtag_add_plain_dr_scan)(int in_num_fields, const scan_f
        scan->end_state                 = state;
 
        for (int i = 0; i < in_num_fields; i++)
-       {
-               int num_bits = in_fields[i].num_bits;
-               int num_bytes = CEIL(in_fields[i].num_bits, 8);
-               scan->fields[i].tap = in_fields[i].tap;
-               scan->fields[i].num_bits = num_bits;
-               scan->fields[i].out_value = buf_cpy(in_fields[i].out_value, cmd_queue_alloc(num_bytes), num_bits);
-               scan->fields[i].in_value = in_fields[i].in_value;
-       }
+               cmd_queue_scan_field_clone(out_fields + i, in_fields + i);
 
        return ERROR_OK;
 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)