Skip to content

Commit 8076d23

Browse files
committed
[sw/example] update example programs
1 parent 1c4312a commit 8076d23

File tree

4 files changed

+8
-89
lines changed

4 files changed

+8
-89
lines changed

sw/example/demo_dual_core/main.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ volatile uint8_t __attribute__ ((aligned (16))) core1_stack[2048]; // stack memo
3030
* Main function for core 0 (primary core).
3131
*
3232
* @attention This program requires the dual-core configuration, the CLINT, UART0
33-
* and the Zalrsc ISa extension.
33+
* and the Zaamo ISA extension.
3434
*
3535
* @return Irrelevant (but can be inspected by the debugger).
3636
**************************************************************************/
@@ -57,8 +57,8 @@ int main(void) {
5757
neorv32_uart0_printf("[ERROR] CLINT module not available!\n");
5858
return -1;
5959
}
60-
if ((neorv32_cpu_csr_read(CSR_MXISA) & (1<<CSR_MXISA_ZALRSC)) == 0) { // atomic lr/sc operations available?
61-
neorv32_uart0_printf("[ERROR] 'Zalrsc' ISA extension not available!\n");
60+
if ((neorv32_cpu_csr_read(CSR_MXISA) & (1<<CSR_MXISA_ZAAMO)) == 0) { // atomic memory operations available?
61+
neorv32_uart0_printf("[ERROR] 'Zaamo' ISA extension not available!\n");
6262
return -1;
6363
}
6464
#ifndef __riscv_atomic
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
make USER_FLAGS+="-DUART0_SIM_MODE -DUART1_SIM_MODE" clean_all asm install sim

sw/example/demo_dual_core/spinlock.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file spinlock.c
3-
* @brief Single simple spin-lock based on atomic lr/sc operations.
3+
* @brief Single simple spin-lock based on atomic memory operations.
44
*/
55
#include <neorv32.h>
66

@@ -18,7 +18,7 @@ static volatile uint32_t __spin_locked = 0;
1818
**************************************************************************/
1919
void spin_lock(void) {
2020

21-
while (neorv32_cpu_amoswapw((uint32_t)&__spin_locked, 1) != 0);
21+
while(__sync_lock_test_and_set(&__spin_locked, -1)); // -> amoswap.w
2222
}
2323

2424

@@ -27,5 +27,6 @@ void spin_lock(void) {
2727
**************************************************************************/
2828
void spin_unlock(void) {
2929

30-
neorv32_cpu_amoswapw((uint32_t)&__spin_locked, 0);
30+
//__sync_lock_release(&__spin_locked); // uses fence that is not required here
31+
__sync_lock_test_and_set(&__spin_locked, 0); // -> amoswap.w
3132
}

sw/example/processor_check/main.c

-83
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ volatile uint32_t constr_test = 0; // for constructor test
8585

8686
volatile uint32_t dma_src; // dma source & destination data
8787
volatile uint32_t store_access_addr[2]; // variable to test store accesses
88-
volatile uint32_t amo_var; // variable for testing atomic memory accesses
8988
volatile uint32_t __attribute__((aligned(4))) pmp_access[2]; // variable to test pmp
9089
volatile uint32_t trap_cnt; // number of triggered traps
9190
volatile uint32_t pmp_num_regions; // number of implemented pmp regions
@@ -1976,88 +1975,6 @@ int main() {
19761975
}
19771976

19781977

1979-
// ----------------------------------------------------------
1980-
// Test atomic lr/sc memory access - failing access
1981-
// ----------------------------------------------------------
1982-
#if defined __riscv_atomic
1983-
neorv32_cpu_csr_write(CSR_MCAUSE, mcause_never_c);
1984-
PRINT_STANDARD("[%i] AMO LR/SC (", cnt_test);
1985-
PRINT_STANDARD("failing) ");
1986-
1987-
if (neorv32_cpu_csr_read(CSR_MXISA) & (1 << CSR_MXISA_ZALRSC)) {
1988-
cnt_test++;
1989-
1990-
// [NOTE] LR/SC operations bypass the data cache so we need to flush/reload
1991-
// it before/after making "normal" load/store operations
1992-
1993-
amo_var = 0x00cafe00; // initialize
1994-
asm volatile ("fence"); // flush/reload d-cache
1995-
1996-
tmp_a = neorv32_cpu_amolr((uint32_t)&amo_var);
1997-
amo_var = 0x10cafe00; // break reservation
1998-
asm volatile ("fence"); // flush/reload d-cache
1999-
tmp_b = neorv32_cpu_amosc((uint32_t)&amo_var, 0xaaaaaaaa);
2000-
tmp_b = (tmp_b << 1) | neorv32_cpu_amosc((uint32_t)&amo_var, 0xcccccccc); // another SC: must fail
2001-
tmp_b = (tmp_b << 1) | neorv32_cpu_amosc((uint32_t)ADDR_UNREACHABLE, 0); // another SC: must fail; no bus exception!
2002-
asm volatile ("fence"); // flush/reload d-cache
2003-
2004-
if ((tmp_a == 0x00cafe00) && // correct LR.W result
2005-
(amo_var == 0x10cafe00) && // atomic variable NOT updates by SC.W
2006-
(tmp_b == 0x00000007) && // SC.W[2] failed, SC.W[1] failed, SC.W[0] failed
2007-
(neorv32_cpu_csr_read(CSR_MCAUSE) == mcause_never_c)) { // no exception
2008-
test_ok();
2009-
}
2010-
else {
2011-
test_fail();
2012-
}
2013-
}
2014-
else {
2015-
PRINT_STANDARD("[n.a.]\n");
2016-
}
2017-
#endif
2018-
2019-
2020-
// ----------------------------------------------------------
2021-
// Test atomic lr/sc memory access - succeeding access
2022-
// ----------------------------------------------------------
2023-
#if defined __riscv_atomic
2024-
neorv32_cpu_csr_write(CSR_MCAUSE, mcause_never_c);
2025-
PRINT_STANDARD("[%i] AMO LR/SC (", cnt_test);
2026-
PRINT_STANDARD("succeed) ");
2027-
2028-
if (neorv32_cpu_csr_read(CSR_MXISA) & (1 << CSR_MXISA_ZALRSC)) {
2029-
cnt_test++;
2030-
2031-
// [NOTE] LR/SC operations bypass the data cache so we need to flush/reload
2032-
// it before/after making "normal" load/store operations
2033-
2034-
amo_var = 0x00abba00; // initialize
2035-
asm volatile ("fence"); // flush/reload d-cache
2036-
2037-
tmp_a = neorv32_cpu_amolr((uint32_t)&amo_var);
2038-
asm volatile ("fence"); // flush/reload d-cache
2039-
neorv32_cpu_load_unsigned_word((uint32_t)&amo_var); // dummy read, must not alter reservation set state
2040-
tmp_b = neorv32_cpu_amosc((uint32_t)&amo_var, 0xcccccccc);
2041-
tmp_b = (tmp_b << 1) | neorv32_cpu_amosc((uint32_t)&amo_var, 0xcccccccc); // another SC: must fail
2042-
tmp_b = (tmp_b << 1) | neorv32_cpu_amosc((uint32_t)ADDR_UNREACHABLE, 0); // another SC: must fail; no bus exception!
2043-
asm volatile ("fence"); // flush/reload d-cache
2044-
2045-
if ((tmp_a == 0x00abba00) && // correct LR.W result
2046-
(amo_var == 0xcccccccc) && // atomic variable WAS updates by SC.W
2047-
(tmp_b == 0x00000003) && // SC.W[2] succeeded, SC.W[1] failed, SC.W[0] failed
2048-
(neorv32_cpu_csr_read(CSR_MCAUSE) == mcause_never_c)) { // no exception
2049-
test_ok();
2050-
}
2051-
else {
2052-
test_fail();
2053-
}
2054-
}
2055-
else {
2056-
PRINT_STANDARD("[n.a.]\n");
2057-
}
2058-
#endif
2059-
2060-
20611978
// ----------------------------------------------------------
20621979
// Test physical memory protection
20631980
// ----------------------------------------------------------

0 commit comments

Comments
 (0)