1010#include "riscv.h"
1111#include "field_helpers.h"
1212
13+ #define DTM_DMI_MIN_ADDRESS_LENGTH 7
14+ // TODO: DTM_DMI_MAX_ADDRESS_LENGTH should be reduced to 32 (per the debug spec)
1315#define DTM_DMI_MAX_ADDRESS_LENGTH ((1<<DTM_DTMCS_ABITS_LENGTH)-1)
16+
17+ #define DMI_SCAN_MIN_BIT_LENGTH (DTM_DMI_MIN_ADDRESS_LENGTH + DTM_DMI_DATA_LENGTH + DTM_DMI_OP_LENGTH)
1418#define DMI_SCAN_MAX_BIT_LENGTH (DTM_DMI_MAX_ADDRESS_LENGTH + DTM_DMI_DATA_LENGTH + DTM_DMI_OP_LENGTH)
19+
1520#define DMI_SCAN_BUF_SIZE (DIV_ROUND_UP(DMI_SCAN_MAX_BIT_LENGTH, 8))
1621
1722/* Reserve extra room in the batch (needed for the last NOP operation) */
@@ -127,11 +132,10 @@ static void add_idle_before_batch(const struct riscv_batch *batch, size_t start_
127132 const unsigned int idle_change = new_delay - batch -> last_scan_delay ;
128133 LOG_TARGET_DEBUG (batch -> target , "Adding %u idle cycles before the batch." ,
129134 idle_change );
130- assert (idle_change <= INT_MAX );
131135 jtag_add_runtest (idle_change , TAP_IDLE );
132136}
133137
134- static int get_delay (const struct riscv_batch * batch , size_t scan_idx ,
138+ static unsigned int get_delay (const struct riscv_batch * batch , size_t scan_idx ,
135139 const struct riscv_scan_delays * delays , bool resets_delays ,
136140 size_t reset_delays_after )
137141{
@@ -142,7 +146,6 @@ static int get_delay(const struct riscv_batch *batch, size_t scan_idx,
142146 const enum riscv_scan_delay_class delay_class =
143147 batch -> delay_classes [scan_idx ];
144148 const unsigned int delay = riscv_scan_get_delay (delays , delay_class );
145- assert (delay <= INT_MAX );
146149 return delays_were_reset ? 0 : delay ;
147150}
148151
@@ -199,9 +202,11 @@ static void log_batch(const struct riscv_batch *batch, size_t start_idx,
199202 return ;
200203
201204 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 ;
205+ assert (scan_bits == riscv_get_dmi_scan_length (batch -> target ));
206+ assert (scan_bits >= DMI_SCAN_MIN_BIT_LENGTH );
207+ assert (scan_bits <= DMI_SCAN_MAX_BIT_LENGTH );
208+ const unsigned int abits = (unsigned int )(scan_bits - DTM_DMI_OP_LENGTH
209+ - DTM_DMI_DATA_LENGTH );
205210
206211 /* Determine the "op" and "address" of the scan that preceded the first
207212 * executed scan.
@@ -211,7 +216,7 @@ static void log_batch(const struct riscv_batch *batch, size_t start_idx,
211216 * would be a more robust solution.
212217 */
213218 bool last_scan_was_read = false;
214- uint32_t last_scan_address = -1 /* to silence maybe-uninitialized */ ;
219+ uint32_t last_scan_address = ( uint32_t )( -1 ) /* to silence maybe-uninitialized */ ;
215220 if (start_idx > 0 ) {
216221 const struct scan_field * const field = & batch -> fields [start_idx - 1 ];
217222 assert (field -> out_value );
@@ -224,7 +229,7 @@ static void log_batch(const struct riscv_batch *batch, size_t start_idx,
224229 /* Decode and log every executed scan */
225230 for (size_t i = start_idx ; i < batch -> used_scans ; ++ i ) {
226231 static const char * const op_string [] = {"-" , "r" , "w" , "?" };
227- const int delay = get_delay (batch , i , delays , resets_delays ,
232+ const unsigned int delay = get_delay (batch , i , delays , resets_delays ,
228233 reset_delays_after );
229234 const struct scan_field * const field = & batch -> fields [i ];
230235
@@ -247,15 +252,15 @@ static void log_batch(const struct riscv_batch *batch, size_t start_idx,
247252 DTM_DMI_ADDRESS_OFFSET , abits );
248253
249254 LOG_DEBUG ("%db %s %08" PRIx32 " @%02" PRIx32
250- " -> %s %08" PRIx32 " @%02" PRIx32 "; %di " ,
255+ " -> %s %08" PRIx32 " @%02" PRIx32 "; %ui " ,
251256 field -> num_bits , op_string [out_op ], out_data , out_address ,
252257 status_string [in_op ], in_data , in_address , delay );
253258
254259 if (last_scan_was_read && in_op == DTM_DMI_OP_SUCCESS )
255260 log_dmi_decoded (batch , /*write*/ false,
256261 last_scan_address , in_data );
257262 } else {
258- LOG_DEBUG ("%db %s %08" PRIx32 " @%02" PRIx32 " -> ?; %di " ,
263+ LOG_DEBUG ("%db %s %08" PRIx32 " @%02" PRIx32 " -> ?; %ui " ,
259264 field -> num_bits , op_string [out_op ], out_data , out_address ,
260265 delay );
261266 }
@@ -321,12 +326,14 @@ int riscv_batch_run_from(struct riscv_batch *batch, size_t start_idx,
321326 return ERROR_OK ;
322327}
323328
324- void riscv_batch_add_dmi_write (struct riscv_batch * batch , uint64_t address , uint32_t data ,
329+ void riscv_batch_add_dmi_write (struct riscv_batch * batch , uint32_t address , uint32_t data ,
325330 bool read_back , enum riscv_scan_delay_class delay_class )
326331{
327332 assert (batch -> used_scans < batch -> allocated_scans );
328333 struct scan_field * field = batch -> fields + batch -> used_scans ;
329334 field -> num_bits = riscv_get_dmi_scan_length (batch -> target );
335+ assert (field -> num_bits >= DMI_SCAN_MIN_BIT_LENGTH );
336+ assert (field -> num_bits <= DMI_SCAN_MAX_BIT_LENGTH );
330337 field -> out_value = (void * )(batch -> data_out + batch -> used_scans * DMI_SCAN_BUF_SIZE );
331338 riscv_fill_dmi_write (batch -> target , (char * )field -> out_value , address , data );
332339 if (read_back ) {
@@ -340,12 +347,14 @@ void riscv_batch_add_dmi_write(struct riscv_batch *batch, uint64_t address, uint
340347 batch -> used_scans ++ ;
341348}
342349
343- size_t riscv_batch_add_dmi_read (struct riscv_batch * batch , uint64_t address ,
350+ size_t riscv_batch_add_dmi_read (struct riscv_batch * batch , uint32_t address ,
344351 enum riscv_scan_delay_class delay_class )
345352{
346353 assert (batch -> used_scans < batch -> allocated_scans );
347354 struct scan_field * field = batch -> fields + batch -> used_scans ;
348355 field -> num_bits = riscv_get_dmi_scan_length (batch -> target );
356+ assert (field -> num_bits >= DMI_SCAN_MIN_BIT_LENGTH );
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,8 @@ 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_MIN_BIT_LENGTH );
396+ assert (field -> num_bits <= DMI_SCAN_MAX_BIT_LENGTH );
386397 field -> out_value = (void * )(batch -> data_out + batch -> used_scans * DMI_SCAN_BUF_SIZE );
387398 field -> in_value = (void * )(batch -> data_in + batch -> used_scans * DMI_SCAN_BUF_SIZE );
388399 riscv_fill_dm_nop (batch -> target , (char * )field -> out_value );
0 commit comments