@@ -1203,12 +1203,13 @@ static int scratch_reserve(struct target *target,
12031203 if (info -> dataaccess == 1 ) {
12041204 /* Sign extend dataaddr. */
12051205 scratch -> hart_address = info -> dataaddr ;
1206- if (info -> dataaddr & (1 <<11 ))
1207- scratch -> hart_address |= 0xfffffffffffff000ULL ;
1206+ if (info -> dataaddr & BIT (DM_HARTINFO_DATAADDR_LENGTH - 1 ))
1207+ scratch -> hart_address |=
1208+ GENMASK_ULL (riscv_xlen (target ) - 1 , DM_HARTINFO_DATAADDR_LENGTH );
12081209 /* Align. */
1209- scratch -> hart_address = (scratch -> hart_address + alignment - 1 ) & ~( alignment - 1 );
1210+ scratch -> hart_address = ALIGN_UP (scratch -> hart_address , alignment );
12101211
1211- if ((size_bytes + scratch -> hart_address - info -> dataaddr + 3 ) / 4 > =
1212+ if (DIV_ROUND_UP (size_bytes + scratch -> hart_address - info -> dataaddr , 4 ) < =
12121213 info -> datasize ) {
12131214 scratch -> memory_space = SPACE_DM_DATA ;
12141215 scratch -> debug_address = (scratch -> hart_address - info -> dataaddr ) / 4 ;
@@ -1222,10 +1223,9 @@ static int scratch_reserve(struct target *target,
12221223
12231224 /* Allow for ebreak at the end of the program. */
12241225 unsigned int program_size = (program -> instruction_count + 1 ) * 4 ;
1225- scratch -> hart_address = (info -> progbuf_address + program_size + alignment - 1 ) &
1226- ~(alignment - 1 );
1226+ scratch -> hart_address = ALIGN_UP (info -> progbuf_address + program_size , alignment );
12271227 if ((info -> progbuf_writable == YNM_YES ) &&
1228- ((size_bytes + scratch -> hart_address - info -> progbuf_address + 3 ) / 4 > =
1228+ (DIV_ROUND_UP (size_bytes + scratch -> hart_address - info -> progbuf_address , 4 ) < =
12291229 info -> progbufsize )) {
12301230 scratch -> memory_space = SPACE_DMI_PROGBUF ;
12311231 scratch -> debug_address = (scratch -> hart_address - info -> progbuf_address ) / 4 ;
@@ -1235,8 +1235,7 @@ static int scratch_reserve(struct target *target,
12351235 /* Option 3: User-configured memory area as scratch RAM */
12361236 if (target_alloc_working_area (target , size_bytes + alignment - 1 ,
12371237 & scratch -> area ) == ERROR_OK ) {
1238- scratch -> hart_address = (scratch -> area -> address + alignment - 1 ) &
1239- ~(alignment - 1 );
1238+ scratch -> hart_address = ALIGN_UP (scratch -> area -> address , alignment );
12401239 scratch -> memory_space = SPACE_DMI_RAM ;
12411240 scratch -> debug_address = scratch -> hart_address ;
12421241 return ERROR_OK ;
0 commit comments