1010#include "riscv.h"
1111#include "field_helpers.h"
1212
13+ // TODO: DTM_DMI_MAX_ADDRESS_LENGTH should be reduced to 32 (per the debug spec)
1314#define DTM_DMI_MAX_ADDRESS_LENGTH ((1<<DTM_DTMCS_ABITS_LENGTH)-1)
1415#define DMI_SCAN_MAX_BIT_LENGTH (DTM_DMI_MAX_ADDRESS_LENGTH + DTM_DMI_DATA_LENGTH + DTM_DMI_OP_LENGTH)
16+
1517#define DMI_SCAN_BUF_SIZE (DIV_ROUND_UP(DMI_SCAN_MAX_BIT_LENGTH, 8))
1618
1719/* Reserve extra room in the batch (needed for the last NOP operation) */
@@ -127,11 +129,10 @@ static void add_idle_before_batch(const struct riscv_batch *batch, size_t start_
127129 const unsigned int idle_change = new_delay - batch -> last_scan_delay ;
128130 LOG_TARGET_DEBUG (batch -> target , "Adding %u idle cycles before the batch." ,
129131 idle_change );
130- assert (idle_change <= INT_MAX );
131132 jtag_add_runtest (idle_change , TAP_IDLE );
132133}
133134
134- static int get_delay (const struct riscv_batch * batch , size_t scan_idx ,
135+ static unsigned int get_delay (const struct riscv_batch * batch , size_t scan_idx ,
135136 const struct riscv_scan_delays * delays , bool resets_delays ,
136137 size_t reset_delays_after )
137138{
@@ -142,7 +143,6 @@ static int get_delay(const struct riscv_batch *batch, size_t scan_idx,
142143 const enum riscv_scan_delay_class delay_class =
143144 batch -> delay_classes [scan_idx ];
144145 const unsigned int delay = riscv_scan_get_delay (delays , delay_class );
145- assert (delay <= INT_MAX );
146146 return delays_were_reset ? 0 : delay ;
147147}
148148
@@ -199,9 +199,10 @@ static void log_batch(const struct riscv_batch *batch, size_t start_idx,
199199 return ;
200200
201201 const unsigned int scan_bits = batch -> fields -> num_bits ;
202- assert (scan_bits == (unsigned int )riscv_get_dmi_scan_length (batch -> target ));
203- const unsigned int abits = scan_bits - DTM_DMI_OP_LENGTH
204- - DTM_DMI_DATA_LENGTH ;
202+ assert (scan_bits == riscv_get_dmi_scan_length (batch -> target ));
203+ assert (scan_bits <= DMI_SCAN_MAX_BIT_LENGTH );
204+ const unsigned int abits = (unsigned int )(scan_bits - DTM_DMI_OP_LENGTH
205+ - DTM_DMI_DATA_LENGTH );
205206
206207 /* Determine the "op" and "address" of the scan that preceded the first
207208 * executed scan.
@@ -211,7 +212,7 @@ static void log_batch(const struct riscv_batch *batch, size_t start_idx,
211212 * would be a more robust solution.
212213 */
213214 bool last_scan_was_read = false;
214- uint32_t last_scan_address = -1 /* to silence maybe-uninitialized */ ;
215+ uint32_t last_scan_address = ( uint32_t )( -1 ) /* to silence maybe-uninitialized */ ;
215216 if (start_idx > 0 ) {
216217 const struct scan_field * const field = & batch -> fields [start_idx - 1 ];
217218 assert (field -> out_value );
@@ -224,7 +225,7 @@ static void log_batch(const struct riscv_batch *batch, size_t start_idx,
224225 /* Decode and log every executed scan */
225226 for (size_t i = start_idx ; i < batch -> used_scans ; ++ i ) {
226227 static const char * const op_string [] = {"-" , "r" , "w" , "?" };
227- const int delay = get_delay (batch , i , delays , resets_delays ,
228+ const unsigned int delay = get_delay (batch , i , delays , resets_delays ,
228229 reset_delays_after );
229230 const struct scan_field * const field = & batch -> fields [i ];
230231
@@ -247,15 +248,15 @@ static void log_batch(const struct riscv_batch *batch, size_t start_idx,
247248 DTM_DMI_ADDRESS_OFFSET , abits );
248249
249250 LOG_DEBUG ("%db %s %08" PRIx32 " @%02" PRIx32
250- " -> %s %08" PRIx32 " @%02" PRIx32 "; %di " ,
251+ " -> %s %08" PRIx32 " @%02" PRIx32 "; %ui " ,
251252 field -> num_bits , op_string [out_op ], out_data , out_address ,
252253 status_string [in_op ], in_data , in_address , delay );
253254
254255 if (last_scan_was_read && in_op == DTM_DMI_OP_SUCCESS )
255256 log_dmi_decoded (batch , /*write*/ false,
256257 last_scan_address , in_data );
257258 } else {
258- LOG_DEBUG ("%db %s %08" PRIx32 " @%02" PRIx32 " -> ?; %di " ,
259+ LOG_DEBUG ("%db %s %08" PRIx32 " @%02" PRIx32 " -> ?; %ui " ,
259260 field -> num_bits , op_string [out_op ], out_data , out_address ,
260261 delay );
261262 }
@@ -321,12 +322,16 @@ int riscv_batch_run_from(struct riscv_batch *batch, size_t start_idx,
321322 return ERROR_OK ;
322323}
323324
324- void riscv_batch_add_dmi_write (struct riscv_batch * batch , uint64_t address , uint32_t data ,
325+ void riscv_batch_add_dmi_write (struct riscv_batch * batch , uint32_t address , uint32_t data ,
325326 bool read_back , enum riscv_scan_delay_class delay_class )
326327{
328+ // TODO: Check that the bit width of "address" is no more than dtmcs.abits,
329+ // otherwise return an error (during batch creation or when the batch is executed).
330+
327331 assert (batch -> used_scans < batch -> allocated_scans );
328332 struct scan_field * field = batch -> fields + batch -> used_scans ;
329333 field -> num_bits = riscv_get_dmi_scan_length (batch -> target );
334+ assert (field -> num_bits <= DMI_SCAN_MAX_BIT_LENGTH );
330335 field -> out_value = (void * )(batch -> data_out + batch -> used_scans * DMI_SCAN_BUF_SIZE );
331336 riscv_fill_dmi_write (batch -> target , (char * )field -> out_value , address , data );
332337 if (read_back ) {
@@ -340,12 +345,16 @@ void riscv_batch_add_dmi_write(struct riscv_batch *batch, uint64_t address, uint
340345 batch -> used_scans ++ ;
341346}
342347
343- size_t riscv_batch_add_dmi_read (struct riscv_batch * batch , uint64_t address ,
348+ size_t riscv_batch_add_dmi_read (struct riscv_batch * batch , uint32_t address ,
344349 enum riscv_scan_delay_class delay_class )
345350{
351+ // TODO: Check that the bit width of "address" is no more than dtmcs.abits,
352+ // otherwise return an error (during batch creation or when the batch is executed).
353+
346354 assert (batch -> used_scans < batch -> allocated_scans );
347355 struct scan_field * field = batch -> fields + batch -> used_scans ;
348356 field -> num_bits = riscv_get_dmi_scan_length (batch -> target );
357+ assert (field -> num_bits <= DMI_SCAN_MAX_BIT_LENGTH );
349358 field -> out_value = (void * )(batch -> data_out + batch -> used_scans * DMI_SCAN_BUF_SIZE );
350359 field -> in_value = (void * )(batch -> data_in + batch -> used_scans * DMI_SCAN_BUF_SIZE );
351360 riscv_fill_dmi_read (batch -> target , (char * )field -> out_value , address );
@@ -383,6 +392,7 @@ void riscv_batch_add_nop(struct riscv_batch *batch)
383392 assert (batch -> used_scans < batch -> allocated_scans );
384393 struct scan_field * field = batch -> fields + batch -> used_scans ;
385394 field -> num_bits = riscv_get_dmi_scan_length (batch -> target );
395+ assert (field -> num_bits <= DMI_SCAN_MAX_BIT_LENGTH );
386396 field -> out_value = (void * )(batch -> data_out + batch -> used_scans * DMI_SCAN_BUF_SIZE );
387397 field -> in_value = (void * )(batch -> data_in + batch -> used_scans * DMI_SCAN_BUF_SIZE );
388398 riscv_fill_dm_nop (batch -> target , (char * )field -> out_value );
0 commit comments