This is an automated email from Gerrit. "Jan Matyas <[email protected]>" just uploaded a new patch set to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/9742
-- gerrit commit b8af092f20521e51872b9066813284a1d8d15c47 Author: Jan Matyas <[email protected]> Date: Fri Jun 12 15:34:01 2026 +0200 target/riscv: refactor scratch_reserve() - use standardized macros The original author of this commit is Farid Khaidari, see [1]. This commit refactors the scratch_reserve() - Replacing hardcoded bit manipulation with BIT() macro - Using GENMASK_ULL() instead of hardcoded masks for sign extension - Applying ALIGN_UP() macro for address alignment calculations - Using DIV_ROUND_UP() instead of manual division with addition [1] https://github.com/riscv-collab/riscv-openocd/ commit/9d4c94e51fdaba3290a16825e48f393b9650a321 Change-Id: Ic40923ef7d9ac5ca0ffb313c9d4fc6d1457d6bbb Signed-off-by: Farid Khaydari <[email protected]> Signed-off-by: Jan Matyas <[email protected]> diff --git a/src/target/riscv/riscv-013.c b/src/target/riscv/riscv-013.c index 7ce5dfddf9..cf07de531f 100644 --- a/src/target/riscv/riscv-013.c +++ b/src/target/riscv/riscv-013.c @@ -1200,12 +1200,13 @@ static int scratch_reserve(struct target *target, if (info->dataaccess == 1) { /* Sign extend dataaddr. */ scratch->hart_address = info->dataaddr; - if (info->dataaddr & (1<<11)) - scratch->hart_address |= 0xfffffffffffff000ULL; + if (info->dataaddr & BIT(DM_HARTINFO_DATAADDR_LENGTH - 1)) + scratch->hart_address |= + GENMASK_ULL(riscv_xlen(target) - 1, DM_HARTINFO_DATAADDR_LENGTH); /* Align. */ - scratch->hart_address = (scratch->hart_address + alignment - 1) & ~(alignment - 1); + scratch->hart_address = ALIGN_UP(scratch->hart_address, alignment); - if ((size_bytes + scratch->hart_address - info->dataaddr + 3) / 4 <= + if (DIV_ROUND_UP(size_bytes + scratch->hart_address - info->dataaddr, 4) <= info->datasize) { scratch->memory_space = SPACE_DM_DATA; scratch->debug_address = (scratch->hart_address - info->dataaddr) / 4; @@ -1219,10 +1220,9 @@ static int scratch_reserve(struct target *target, /* Allow for ebreak at the end of the program. */ unsigned int program_size = (program->instruction_count + 1) * 4; - scratch->hart_address = (info->progbuf_address + program_size + alignment - 1) & - ~(alignment - 1); + scratch->hart_address = ALIGN_UP(info->progbuf_address + program_size, alignment); if ((info->progbuf_writable == YNM_YES) && - ((size_bytes + scratch->hart_address - info->progbuf_address + 3) / 4 <= + (DIV_ROUND_UP(size_bytes + scratch->hart_address - info->progbuf_address, 4) <= info->progbufsize)) { scratch->memory_space = SPACE_DMI_PROGBUF; scratch->debug_address = (scratch->hart_address - info->progbuf_address) / 4; @@ -1232,8 +1232,7 @@ static int scratch_reserve(struct target *target, /* Option 3: User-configured memory area as scratch RAM */ if (target_alloc_working_area(target, size_bytes + alignment - 1, &scratch->area) == ERROR_OK) { - scratch->hart_address = (scratch->area->address + alignment - 1) & - ~(alignment - 1); + scratch->hart_address = ALIGN_UP(scratch->area->address, alignment); scratch->memory_space = SPACE_DMI_RAM; scratch->debug_address = scratch->hart_address; return ERROR_OK; --
