Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[sw] add auxiliary/helper functions library #934

Merged
merged 3 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/datasheet/software.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ The NEORV32 project provides a set of pre-defined C libraries that allow an easy
|=======================
| C source file | C header file | Description
| - | `neorv32.h` | Main NEORV32 library file
| `neorv32_aux.c` | `neorv32_aux.h` | General auxiliary/helper function
| `neorv32_cfs.c` | `neorv32_cfs.h` | <<_custom_functions_subsystem_cfs>> HAL
| `neorv32_crc.c` | `neorv32_crc.h` | <<_cyclic_redundancy_check_crc>> HAL
| `neorv32_cpu.c` | `neorv32_cpu.h` | <<_neorv32_central_processing_unit_cpu>> HAL
Expand Down
54 changes: 18 additions & 36 deletions sw/example/atomic_test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@


// Prototypes
uint32_t xorshift32(void);
uint32_t check_result(uint32_t num, uint32_t amo_var_old, uint32_t amo_var_pre, uint32_t amo_var_new, uint32_t amo_var);
void print_report(int num_err, int num_tests);

Expand Down Expand Up @@ -92,8 +91,8 @@ int main() {
neorv32_uart0_printf("\namoswap.w:\n");
err_cnt = 0;
for (i=0; i<num_tests; i++) {
amo_var_old = xorshift32();
amo_var_update = xorshift32();
amo_var_old = neorv32_aux_xorshift32();
amo_var_update = neorv32_aux_xorshift32();

amo_var = amo_var_old;
asm volatile ("fence");
Expand All @@ -110,8 +109,8 @@ int main() {
neorv32_uart0_printf("\namoadd.w:\n");
err_cnt = 0;
for (i=0; i<num_tests; i++) {
amo_var_old = xorshift32();
amo_var_update = xorshift32();
amo_var_old = neorv32_aux_xorshift32();
amo_var_update = neorv32_aux_xorshift32();

amo_var = amo_var_old;
asm volatile ("fence");
Expand All @@ -128,8 +127,8 @@ int main() {
neorv32_uart0_printf("\namoand.w:\n");
err_cnt = 0;
for (i=0; i<num_tests; i++) {
amo_var_old = xorshift32();
amo_var_update = xorshift32();
amo_var_old = neorv32_aux_xorshift32();
amo_var_update = neorv32_aux_xorshift32();

amo_var = amo_var_old;
asm volatile ("fence");
Expand All @@ -146,8 +145,8 @@ int main() {
neorv32_uart0_printf("\namoor.w:\n");
err_cnt = 0;
for (i=0; i<num_tests; i++) {
amo_var_old = xorshift32();
amo_var_update = xorshift32();
amo_var_old = neorv32_aux_xorshift32();
amo_var_update = neorv32_aux_xorshift32();

amo_var = amo_var_old;
asm volatile ("fence");
Expand All @@ -164,8 +163,8 @@ int main() {
neorv32_uart0_printf("\namoxor.w:\n");
err_cnt = 0;
for (i=0; i<num_tests; i++) {
amo_var_old = xorshift32();
amo_var_update = xorshift32();
amo_var_old = neorv32_aux_xorshift32();
amo_var_update = neorv32_aux_xorshift32();

amo_var = amo_var_old;
asm volatile ("fence");
Expand All @@ -182,8 +181,8 @@ int main() {
neorv32_uart0_printf("\namomax.w:\n");
err_cnt = 0;
for (i=0; i<num_tests; i++) {
amo_var_old = xorshift32();
amo_var_update = xorshift32();
amo_var_old = neorv32_aux_xorshift32();
amo_var_update = neorv32_aux_xorshift32();

amo_var = amo_var_old;
asm volatile ("fence");
Expand All @@ -200,8 +199,8 @@ int main() {
neorv32_uart0_printf("\namomaxu.w:\n");
err_cnt = 0;
for (i=0; i<num_tests; i++) {
amo_var_old = xorshift32();
amo_var_update = xorshift32();
amo_var_old = neorv32_aux_xorshift32();
amo_var_update = neorv32_aux_xorshift32();

amo_var = amo_var_old;
asm volatile ("fence");
Expand All @@ -218,8 +217,8 @@ int main() {
neorv32_uart0_printf("\namomin.w:\n");
err_cnt = 0;
for (i=0; i<num_tests; i++) {
amo_var_old = xorshift32();
amo_var_update = xorshift32();
amo_var_old = neorv32_aux_xorshift32();
amo_var_update = neorv32_aux_xorshift32();

amo_var = amo_var_old;
asm volatile ("fence");
Expand All @@ -236,8 +235,8 @@ int main() {
neorv32_uart0_printf("\namominu.w:\n");
err_cnt = 0;
for (i=0; i<num_tests; i++) {
amo_var_old = xorshift32();
amo_var_update = xorshift32();
amo_var_old = neorv32_aux_xorshift32();
amo_var_update = neorv32_aux_xorshift32();

amo_var = amo_var_old;
asm volatile ("fence");
Expand All @@ -261,23 +260,6 @@ int main() {
}


/**********************************************************************//**
* Pseudo-Random Number Generator (to generate deterministic test vectors).
*
* @return Random data (32-bit).
**************************************************************************/
uint32_t xorshift32(void) {

static uint32_t x32 = 314159265;

x32 ^= x32 << 13;
x32 ^= x32 >> 17;
x32 ^= x32 << 5;

return x32;
}


/**********************************************************************//**
* Check results (reference (SW) vs actual hardware).
*
Expand Down
49 changes: 4 additions & 45 deletions sw/example/bus_explorer/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ void setup_access(void);
void write_memory(uint32_t address, uint32_t data);
void dump_memory(uint32_t address);
void hexdump(uint32_t address);
uint32_t hexstr_to_uint32(char *buffer, uint8_t length);
void aux_print_hex_byte(uint8_t byte);


Expand Down Expand Up @@ -118,7 +117,7 @@ int main() {
neorv32_uart0_printf("Insufficient arguments.\n");
}
else {
read_memory((uint32_t)hexstr_to_uint32(arg0, 8));
read_memory((uint32_t)neorv32_aux_hexstr2uint64(arg0, 8));
}
}

Expand All @@ -127,7 +126,7 @@ int main() {
neorv32_uart0_printf("Insufficient arguments.\n");
}
else {
write_memory((uint32_t)hexstr_to_uint32(arg0, 8), (uint32_t)hexstr_to_uint32(arg1, 8));
write_memory((uint32_t)neorv32_aux_hexstr2uint64(arg0, 8), (uint32_t)neorv32_aux_hexstr2uint64(arg1, 8));
}
}

Expand All @@ -136,7 +135,7 @@ int main() {
neorv32_uart0_printf("Insufficient arguments.\n");
}
else {
dump_memory((uint32_t)hexstr_to_uint32(arg0, 8));
dump_memory((uint32_t)neorv32_aux_hexstr2uint64(arg0, 8));
}
}

Expand All @@ -145,7 +144,7 @@ int main() {
neorv32_uart0_printf("Insufficient arguments.\n");
}
else {
hexdump((uint32_t)hexstr_to_uint32(arg0, 8));
hexdump((uint32_t)neorv32_aux_hexstr2uint64(arg0, 8));
}
}

Expand Down Expand Up @@ -395,46 +394,6 @@ void hexdump(uint32_t address) {
}


/**********************************************************************//**
* Helper function to convert N hex chars string into uint32_t
*
* @param[in,out] buffer Pointer to array of chars to convert into number.
* @param[in,out] length Length of the conversion string.
* @return Converted number.
**************************************************************************/
uint32_t hexstr_to_uint32(char *buffer, uint8_t length) {

uint32_t res = 0, d = 0;
char c = 0;

while (length--) {
c = *buffer++;

if (c == '\0') {
break;
}

if ((c >= '0') && (c <= '9')) {
d = (uint32_t)(c - '0');
}
else if ((c >= 'a') && (c <= 'f')) {
d = (uint32_t)((c - 'a') + 10);
}
else if ((c >= 'A') && (c <= 'F')) {
d = (uint32_t)((c - 'A') + 10);
}
else {
d = 0;
}

res <<= 4;
res |= d & 0xf;
}

return res;
}


/**********************************************************************//**
* Print HEX byte.
*
Expand Down
31 changes: 4 additions & 27 deletions sw/example/demo_cfs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@
/**@}*/


/**********************************************************************//**
* @name Prototypes
**************************************************************************/
uint32_t xorshift32(void);


/**********************************************************************//**
* Main function
*
Expand Down Expand Up @@ -76,28 +70,28 @@ int main() {
// function examples
neorv32_uart0_printf("\n--- CFS 'binary to gray' function ---\n");
for (i=0; i<TESTCASES; i++) {
tmp = xorshift32(); // get random test data
tmp = neorv32_aux_xorshift32(); // get random test data
NEORV32_CFS->REG[0] = tmp; // write to CFS memory-mapped register 0
neorv32_uart0_printf("%u: IN = 0x%x, OUT = 0x%x\n", i, tmp, NEORV32_CFS->REG[0]); // read from CFS memory-mapped register 0
}

neorv32_uart0_printf("\n--- CFS 'gray to binary' function ---\n");
for (i=0; i<TESTCASES; i++) {
tmp = xorshift32(); // get random test data
tmp = neorv32_aux_xorshift32(); // get random test data
NEORV32_CFS->REG[1] = tmp; // write to CFS memory-mapped register 1
neorv32_uart0_printf("%u: IN = 0x%x, OUT = 0x%x\n", i, tmp, NEORV32_CFS->REG[1]); // read from CFS memory-mapped register 1
}

neorv32_uart0_printf("\n--- CFS 'bit reversal' function ---\n");
for (i=0; i<TESTCASES; i++) {
tmp = xorshift32(); // get random test data
tmp = neorv32_aux_xorshift32(); // get random test data
NEORV32_CFS->REG[2] = tmp; // write to CFS memory-mapped register 2
neorv32_uart0_printf("%u: IN = 0x%x, OUT = 0x%x\n", i, tmp, NEORV32_CFS->REG[2]); // read from CFS memory-mapped register 2
}

neorv32_uart0_printf("\n--- CFS 'byte swap' function ---\n");
for (i=0; i<TESTCASES; i++) {
tmp = xorshift32(); // get random test data
tmp = neorv32_aux_xorshift32(); // get random test data
NEORV32_CFS->REG[3] = tmp; // write to CFS memory-mapped register 3
neorv32_uart0_printf("%u: IN = 0x%x, OUT = 0x%x\n", i, tmp, NEORV32_CFS->REG[3]); // read from CFS memory-mapped register 3
}
Expand All @@ -107,20 +101,3 @@ int main() {

return 0;
}


/**********************************************************************//**
* Pseudo-Random Number Generator (to generate deterministic test vectors).
*
* @return Random data (32-bit).
**************************************************************************/
uint32_t xorshift32(void) {

static uint32_t x32 = 314159265;

x32 ^= x32 << 13;
x32 ^= x32 >> 17;
x32 ^= x32 << 5;

return x32;
}
19 changes: 1 addition & 18 deletions sw/example/demo_cfu/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,23 +80,6 @@ uint32_t time_enc_sw, time_enc_hw, time_dec_sw, time_dec_hw;
/**@}*/


/**********************************************************************//**
* Pseudo-random number generator (to generate deterministic test data).
*
* @return Random data (32-bit).
**************************************************************************/
uint32_t xorshift32(void) {

static uint32_t x32 = 314159265;

x32 ^= x32 << 13;
x32 ^= x32 >> 17;
x32 ^= x32 << 5;

return x32;
}


/**********************************************************************//**
* XTEA encryption - software reference
*
Expand Down Expand Up @@ -218,7 +201,7 @@ int main() {

// generate "random" data for the plain text
for (i=0; i<DATA_NUM; i++) {
input_data[i] = xorshift32();
input_data[i] = neorv32_aux_xorshift32();
}


Expand Down
4 changes: 2 additions & 2 deletions sw/example/demo_mtime/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int main() {
date.minutes = 47; // 0..59
date.seconds = 11; // 0..59

neorv32_mtime_set_unixtime(neorv32_mtime_date2unixtime(&date));
neorv32_mtime_set_unixtime(neorv32_aux_date2unixtime(&date));
neorv32_uart0_printf("Unix timestamp: %u\n", (uint32_t)neorv32_mtime_get_unixtime());

// clear GPIO output port
Expand Down Expand Up @@ -103,7 +103,7 @@ void mtime_irq_handler(void) {

// show date in human-readable format
date_t date;
neorv32_mtime_unixtime2date(neorv32_mtime_get_unixtime(), &date);
neorv32_aux_unixtime2date(neorv32_mtime_get_unixtime(), &date);
neorv32_uart0_printf("%u.%u.%u (%s) ", date.day, date.month, date.year, weekdays[(date.weekday-1)%7]);
neorv32_uart0_printf("%u:%u:%u\n", date.hours, date.minutes, date.seconds);
}
Loading