atom: reimplement in pure assembly

This commit is contained in:
anna 2021-08-01 00:11:13 +02:00
parent 3ab358310c
commit a78a8ba59b
Signed by: fef
GPG key ID: EC22E476DC2D3D84
6 changed files with 92 additions and 44 deletions

View file

@ -7,6 +7,7 @@ target_compile_options(ardix_arch PRIVATE ${ARDIX_COMPILE_OPTIONS})
target_include_directories(ardix_arch PRIVATE ${ARDIX_INCLUDE_DIRS})
target_sources(ardix_arch PRIVATE
atom_get_put.S
atom.c
atomic.c
entry.c

View file

@ -1,52 +1,13 @@
/* See the end of this file for copyright, license, and warranty information. */
#include <ardix/atom.h>
#include <stddef.h>
void atom_init(atom_t *atom)
{
atom->count = 0;
}
int atom_get(atom_t *atom)
{
int tmp;
int newval;
atom_t atom_val;
__asm__ volatile(
"1: ldrex %0, [%3] \n" /* atom_val = *atom */
" add %1, %0, #1 \n" /* newval = atom_val.count + 1 */
" strex %2, %1, [%3] \n" /* *atom = newval */
" teq %2, #0 \n" /* store successful? */
" bne 1b \n" /* -> goto 1 if not */
" dmb " /* memory barrier */
: "=&r" (atom_val), "=&r" (newval), "=&r" (tmp)
: "r" (atom)
: "cc");
return newval;
}
int atom_put(atom_t *atom)
{
int tmp;
int newval;
atom_t atom_val;
__asm__ volatile(
"1: ldrex %0, [%3] \n" /* atom_val = *atom */
" sub %1, %0, #1 \n" /* newval = atom_val.count - 1 */
" strex %2, %1, [%3] \n" /* *atom = newval */
" teq %2, #0 \n" /* store successful? */
" bne 1b \n" /* -> goto 1 if not */
" dmb " /* memory barrier */
: "=&r" (atom_val), "=&r" (newval), "=&r" (tmp)
: "r" (atom)
: "cc");
return newval;
}
int atom_count(atom_t *atom)
{
return atom->count;

View file

@ -0,0 +1,46 @@
/* See the end of this file for copyright, license, and warranty information. */
.include "asm.S"
.text
/* int _atom_get(int *count); */
func_begin _atom_get
ldrex r1, [r0] /* int tmp = atom->count */
add r2, r1, #1 /* int newval = tmp + 1 */
strex r3, r2, [r0] /* atom->count = newval */
teq r3, #0 /* store successful? */
bne _atom_get /* -> goto _atom_get to try again if not */
dmb /* data memory barrier */
mov r0, r2 /* return newval */
bx lr
func_end _atom_get
/* int _atom_put(int *count); */
func_begin _atom_put
ldrex r1, [r0] /* int tmp = atom->count */
sub r2, r1, #1 /* int newval = tmp - 1 */
strex r3, r2, [r0] /* atom->count = newval */
teq r3, #0 /* store successful? */
bne _atom_put /* -> goto _atom_put to try again if not */
dmb /* data memory barrier */
mov r0, r2 /* return newval */
bx lr
func_end _atom_put
/*
* This file is part of Ardix.
* Copyright (c) 2021 Felix Kopp <owo@fef.moe>.
*
* Ardix is non-violent software: you may only use, redistribute,
* and/or modify it under the terms of the CNPLv6+ as found in
* the LICENSE file in the source code root directory or at
* <https://git.pixie.town/thufie/CNPL>.
*
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/

View file

@ -0,0 +1,28 @@
/* See the end of this file for copyright, license, and warranty information. */
.syntax unified
.thumb
.cpu cortex-m3
.macro func_begin name
.global \name
.type \name, "function"
\name :
.endm
.macro func_end name
.size \name, .-\name
.endm
/*
* This file is part of Ardix.
* Copyright (c) 2021 Felix Kopp <owo@fef.moe>.
*
* Ardix is non-violent software: you may only use, redistribute,
* and/or modify it under the terms of the CNPLv6+ as found in
* the LICENSE file in the source code root directory or at
* <https://git.pixie.town/thufie/CNPL>.
*
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
* permitted by applicable law. See the CNPLv6+ for details.
*/

View file

@ -16,8 +16,10 @@ set(CMAKE_RANLIB ${TOOLCHAIN_PATH}/arm-none-eabi-ranlib${CMAKE_EXECUTABLE_SUFFIX
set(CMAKE_SZE ${TOOLCHAIN_PATH}/arm-none-eabi-size${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_STRIP ${TOOLCHAIN_PATH}/arm-none-eabi-strip${CMAKE_EXECUTABLE_SUFFIX} CACHE INTERNAL "")
set(CMAKE_C_FLAGS "-Os -nodefaultlibs -nostartfiles -mcpu=cortex-m3 -mthumb -mabi=aapcs -march=armv7-m -masm-syntax-unified")
set(CMAKE_C_FLAGS_DEBUG -g)
set(CMAKE_C_FLAGS "-Os -nodefaultlibs -nostartfiles -mcpu=cortex-m3 -mabi=aapcs")
if(DEBUG)
set(CMAKE_C_FLAGS "-g ${CMAKE_C_FLAGS}")
endif()
set(CMAKE_LINKER_FLAGS "-T${CMAKE_CURRENT_LIST_DIR}/config.ld -T${CMAKE_CURRENT_LIST_DIR}/flash.ld --whole-archive")
set(ARDIX_LINKER_FLAGS

View file

@ -3,14 +3,24 @@
#pragma once
#include <ardix/types.h>
#include <toolchain.h>
#define ATOM_DEFINE(name) atom_t name = { .count = 0, }
void atom_init(atom_t *atom);
int atom_get(atom_t *atom);
extern int _atom_get(int *count);
extern int _atom_put(int *count);
int atom_put(atom_t *atom);
__always_inline int atom_get(atom_t *atom)
{
return _atom_get(&atom->count);
}
__always_inline int atom_put(atom_t *atom)
{
return _atom_get(&atom->count);
}
int atom_count(atom_t *atom);