Skip to content

Commit

Permalink
Refactor infiniemu cli
Browse files Browse the repository at this point in the history
  • Loading branch information
pipe01 committed Jan 1, 2025
1 parent 916bb1a commit cdb8018
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 63 deletions.
2 changes: 1 addition & 1 deletion include/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ typedef enum {
#define OP_IS_WRITE(op) ((op) < 0)
#define OP_IS_SIZE(op, size) ((op) == OP_READ_##size || (op) == OP_WRITE_##size)

#ifdef ABORT_ON_INVALID_MEM_ACCESS
#if ABORT_ON_INVALID_MEM_ACCESS
#define OP_INVALID_ACCESS abort()
#define OP_INVALID_SIZE abort()
#else
Expand Down
6 changes: 0 additions & 6 deletions src/gdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -776,9 +776,6 @@ void *gdb_run_cpu(void *userdata)
jmp_buf fault_jmp;
bool has_faulted = false;

time_use_real_time(false);
uint64_t i = 0;

if (setjmp(fault_jmp))
{
has_faulted = true;
Expand All @@ -795,9 +792,6 @@ void *gdb_run_cpu(void *userdata)
break;

pinetime_step(stub->gdb->pt);

if (i++ % 60 == 0)
time_increment_fake_microseconds(1);
}
}

Expand Down
139 changes: 83 additions & 56 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "commander.h"
#include "config.h"
#include "pinetime.h"
#include "fault.h"
#include "gdb.h"
#include "ie_time.h"
#include "pcap.h"
Expand All @@ -14,6 +15,8 @@
#include "util.h"
#include "peripherals/nrf52832/radio.h"

void run_emulation(pinetime_t *pt, cpu_t *cpu, NRF52832_t *nrf);

void commander_output(const char *msg, void *userdata)
{
fwrite(msg, 1, strlen(msg), stdout);
Expand Down Expand Up @@ -94,15 +97,6 @@ int main(int argc, char **argv)
NRF52832_t *nrf = pinetime_get_nrf52832(pt);
cpu_t *cpu = nrf52832_get_cpu(nrf);

(void)cpu;

#if ENABLE_SEGGER_RTT
rtt_t *rtt = rtt_new(cpu_mem(cpu));
bool found_rtt = false;
size_t rtt_counter = 0, rtt_read = 0;
char rtt_buffer[1024];
#endif

#if ENABLE_RUNLOG
runlog_t *runlog = NULL;

Expand Down Expand Up @@ -154,70 +148,103 @@ int main(int argc, char **argv)
}
else
{
time_use_real_time(false);
jmp_buf fault_jmp;

#if ENABLE_MEASUREMENT
uint64_t start, now;
uint64_t cycles_counter = 0;
size_t perf_counter = 0;
start = microseconds_now_real();
if (setjmp(fault_jmp))
{
fprintf(stderr, "R0: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_R0));
fprintf(stderr, "R1: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_R1));
fprintf(stderr, "R2: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_R2));
fprintf(stderr, "R3: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_R3));
fprintf(stderr, "R4: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_R4));
fprintf(stderr, "R5: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_R5));
fprintf(stderr, "R6: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_R6));
fprintf(stderr, "R7: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_R7));
fprintf(stderr, "R8: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_R8));
fprintf(stderr, "R9: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_R9));
fprintf(stderr, "R10: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_R10));
fprintf(stderr, "R11: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_R11));
fprintf(stderr, "R12: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_R12));
fprintf(stderr, "SP: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_SP));
fprintf(stderr, "LR: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_LR));
fprintf(stderr, "PC: 0x%08X\n", cpu_reg_read(cpu, ARM_REG_PC));
return -1;
}
else
{
fault_set_jmp(&fault_jmp);

run_emulation(pt, cpu, nrf);
}

fault_clear_jmp();
}

#if ENABLE_RUNLOG
if (runlog)
runlog_free(runlog);
#endif

size_t cycle_counter = 0;
return 0;
}

for (;;)
{
cycle_counter += pinetime_step(pt);
void run_emulation(pinetime_t *pt, cpu_t *cpu, NRF52832_t *nrf)
{
#if ENABLE_SEGGER_RTT
rtt_t *rtt = rtt_new(cpu_mem(cpu));
bool found_rtt = false;
size_t rtt_counter = 0, rtt_read = 0;
char rtt_buffer[1024];
#endif

if (cycle_counter >= NRF52832_HFCLK_FREQUENCY / (1000000 / 10))
{
time_increment_fake_microseconds(10);
cycle_counter = 0;
}
#if ENABLE_MEASUREMENT
uint64_t start, now;
uint64_t cycles_counter = 0;
size_t perf_counter = 0;
start = microseconds_now_real();
#endif

size_t cycle_counter = 0;

for (;;)
{
cycle_counter += pinetime_step(pt);

#if ENABLE_SEGGER_RTT
if (found_rtt || rtt_counter < 1000000)
if (found_rtt || rtt_counter < 1000000)
{
if (rtt_counter % 1000 == 0)
{
if (rtt_counter % 1000 == 0)
if (!found_rtt)
found_rtt = rtt_find_control(rtt);

rtt_read = rtt_flush_buffers(rtt, rtt_buffer, sizeof(rtt_buffer));
if (rtt_read > 0)
{
if (!found_rtt)
found_rtt = rtt_find_control(rtt);

rtt_read = rtt_flush_buffers(rtt, rtt_buffer, sizeof(rtt_buffer));
if (rtt_read > 0)
{
fwrite(rtt_buffer, 1, rtt_read, stdout);
fflush(stdout);
}
fwrite(rtt_buffer, 1, rtt_read, stdout);
fflush(stdout);
}

rtt_counter++;
}

rtt_counter++;
}
#endif

#if ENABLE_MEASUREMENT
if (++perf_counter == 10000000)
{
now = microseconds_now_real();
if (++perf_counter == 10000000)
{
now = microseconds_now_real();

uint64_t elapsed = now - start;
uint64_t elapsed_cycles = nrf52832_get_cycle_counter(nrf) - cycles_counter;
cycles_counter = nrf52832_get_cycle_counter(nrf);
uint64_t elapsed = now - start;
uint64_t elapsed_cycles = nrf52832_get_cycle_counter(nrf) - cycles_counter;
cycles_counter = nrf52832_get_cycle_counter(nrf);

start = now;
start = now;

printf("Cycles per second: %.0f, target: %d\n", (1000000.f / elapsed) * elapsed_cycles, NRF52832_HFCLK_FREQUENCY);
printf("Cycles per second: %.0f, target: %d\n", (1000000.f / elapsed) * elapsed_cycles, NRF52832_HFCLK_FREQUENCY);

perf_counter = 0;
}
#endif
perf_counter = 0;
}
}

#if ENABLE_RUNLOG
if (runlog)
runlog_free(runlog);
#endif

return 0;
}
}
}

0 comments on commit cdb8018

Please sign in to comment.