Skip to content

Commit ea50bc6

Browse files
committed
AVR: move declarations at the begining of the function.
Signed-off-by: Glenn Baker <[email protected]>
1 parent 543b7fb commit ea50bc6

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

samples/sample_avr.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ static bool test_avr(void)
5252
uc_hook trace1, trace2;
5353
bool success = false;
5454

55+
uint8_t regs[32];
56+
int reg_ids[32];
57+
void *reg_vals[32];
58+
int i;
59+
5560
printf("Emulate AVR code\n");
5661
do {
5762
// Initialize emulator in AVR mode
@@ -81,14 +86,11 @@ static bool test_avr(void)
8186
break;
8287

8388
// Initialize registers
84-
uint8_t regs[32];
8589
memset(regs, 0, sizeof(regs));
8690
regs[25] = 0; regs[24] = 1;
8791
regs[23] = 0; regs[22] = 2;
8892

89-
int reg_ids[32];
90-
void *reg_vals[32];
91-
for (unsigned i = 0; i < 4; i++) {
93+
for (i = 0; i < 4; i++) {
9294
reg_ids[i] = UC_AVR_REG_R0 + 22 + i;
9395
reg_vals[i] = &regs[22 + i];
9496
}

tests/unit/test_avr.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ static void uc_common_setup(uc_engine **uc, uc_cpu_avr cpu_model,
117117
static void test_avr_basic_alu(void)
118118
{
119119
uc_engine *uc = NULL;
120+
120121
uint8_t r[32] = {0,};
122+
uint32_t r_pc;
121123
uint16_t r_func_arg0 = 1, r_func_arg1 = 2, r_func_ret;
122124
r[24] = 1;
123125
r[22] = 2;
@@ -129,7 +131,6 @@ static void test_avr_basic_alu(void)
129131
const uint64_t code_start = ADDR[ADDR_test_func] + 8;
130132
OK(uc_emu_start(uc, code_start, code_start + 4, 0, 0));
131133

132-
uint32_t r_pc;
133134
OK(uc_reg_read(uc, UC_AVR_REG_PC, &r_pc));
134135
OK(uc_reg_read(uc, UC_AVR_REG_R25, &r[25]));
135136
OK(uc_reg_read(uc, UC_AVR_REG_R24, &r[24]));
@@ -185,28 +186,30 @@ static void test_avr_basic_mem(void)
185186
uc_hook eventmem_hook;
186187
MEM_HOOK_RESULTS eventmem_trace = {0};
187188

189+
const uint16_t DATA_BASE = ADDR[ADDR__data__];
190+
const uint16_t DATA_SIZE = ADDR[ADDR__data__+1] - DATA_BASE;
191+
const uint8_t *const DATA = &FLASH[ADDR[ADDR__data__]];
192+
uint8_t mem[DATA_SIZE];
193+
194+
uint32_t r_pc;
195+
int i;
196+
188197
uc_common_setup(&uc, 0, FLASH, FLASH_SIZE);
189198
OK(uc_hook_add(uc, &eventmem_hook, UC_HOOK_MEM_VALID,
190199
test_avr_basic_mem_cb_eventmem, &eventmem_trace, 1, 0));
191200

192201
const uint64_t code_start = ADDR[ADDR__init__];
193202
OK(uc_emu_start(uc, code_start, ADDR[ADDR__init__+1], 0, 0));
194203

195-
uint32_t r_pc;
196204
OK(uc_reg_read(uc, UC_AVR_REG_PC, &r_pc));
197205
TEST_CHECK(r_pc == ADDR[ADDR__init__+1]);
198206

199-
const uint16_t DATA_BASE = ADDR[ADDR__data__];
200-
const uint16_t DATA_SIZE = ADDR[ADDR__data__+1] - DATA_BASE;
201-
const uint8_t *const DATA = &FLASH[ADDR[ADDR__data__]];
202-
203207
// Check SRAM was correctly initialized with data from Flash program memory
204-
uint8_t mem[DATA_SIZE];
205208
OK(uc_mem_read(uc, MEM_BASE, mem, sizeof(mem)));
206209
TEST_CHECK(memcmp(mem, DATA, DATA_SIZE) == 0);
207210

208211
TEST_CHECK(eventmem_trace.count == 2*DATA_SIZE);
209-
for (unsigned i = 0; i < DATA_SIZE; i++) {
212+
for (i = 0; i < DATA_SIZE; i++) {
210213
const MEM_HOOK_RESULT *const mr = &eventmem_trace.results[2*i];
211214
TEST_CHECK(mr->type == UC_MEM_READ);
212215
TEST_CHECK(mr->address == (UC_AVR_MEM_FLASH|(DATA_BASE+i)));
@@ -227,16 +230,19 @@ static void test_avr_full_exec(void)
227230
{
228231
uc_engine *uc = NULL;
229232

233+
uint8_t r[32] = {0,};
234+
uint32_t r_pc;
235+
uint32_t r_sp;
236+
230237
uc_common_setup(&uc, 0, FLASH, FLASH_SIZE);
231238

232239
const uint64_t code_start = ADDR[ADDR__init__];
233240
OK(uc_emu_start(uc, code_start, ADDR[ADDR__init__+1], 0, 0));
234241

235-
uint32_t r_pc;
236242
OK(uc_reg_read(uc, UC_AVR_REG_PC, &r_pc));
237243
TEST_CHECK(r_pc == ADDR[ADDR__init__+1]);
238244

239-
uint32_t r_sp = MEM_BASE + MEM_SIZE - 1;
245+
r_sp = MEM_BASE + MEM_SIZE - 1;
240246
OK(uc_reg_write(uc, UC_AVR_REG_SP, &r_sp));
241247

242248
const uint64_t exits[] = {
@@ -249,7 +255,6 @@ static void test_avr_full_exec(void)
249255
const uint64_t code_main = ADDR[ADDR_main];
250256
OK(uc_emu_start(uc, code_main, 0, 0, 0));
251257

252-
uint8_t r[32] = {0,};
253258
OK(uc_reg_read(uc, UC_AVR_REG_R25, &r[25]));
254259
OK(uc_reg_read(uc, UC_AVR_REG_R24, &r[24]));
255260
TEST_CHECK(r[25] == 0 && r[24] == 0);

0 commit comments

Comments
 (0)