1/*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7.syntax unified
8.cpu cortex-m0plus
9.thumb
10
11#include "pico/asm_helper.S"
12#include "pico/bootrom.h"
13
14__pre_init __aeabi_mem_init, 00001
15
16.macro mem_section name
17#if PICO_MEM_IN_RAM
18.section RAM_SECTION_NAME(\name), "ax"
19#else
20.section SECTION_NAME(\name), "ax"
21#endif
22.endm
23
24.equ MEMSET, 0
25.equ MEMCPY, 4
26.equ MEMSET4, 8
27.equ MEMCPY4, 12
28.equ MEM_FUNC_COUNT, 4
29
30# NOTE: All code sections are placed in RAM (at the expense of some veneer cost for calls from flash) because
31#       otherwise code using basic c division operators will require XIP flash access.
32
33.section .data.aeabi_mem_funcs
34.global aeabi_mem_funcs, aeabi_mem_funcs_end
35
36.align 2
37aeabi_mem_funcs:
38    .word ROM_FUNC_MEMSET
39    .word ROM_FUNC_MEMCPY
40    .word ROM_FUNC_MEMSET4
41    .word ROM_FUNC_MEMCPY44
42aeabi_mem_funcs_end:
43
44.section .text
45regular_func __aeabi_mem_init
46    ldr r0, =aeabi_mem_funcs
47    movs r1, #MEM_FUNC_COUNT
48    ldr r3, =rom_funcs_lookup
49    bx r3
50
51# lump them both together because likely both to be used, in which case doing so saves 1 word
52# and it only costs 1 word if not
53
54// Note from Run-time ABI for the ARM architecture 4.3.4:
55// If there is an attached device with efficient memory copying or clearing operations
56// (such as a DMA engine), its device supplement specifies whether it may be used in
57// implementations of these functions and what effect such use has on the device’s state.
58
59mem_section aeabi_memset_memcpy
60
61wrapper_func __aeabi_memset
62    // 2nd/3rd args are reversed
63    eors r2, r1
64    eors r1, r2
65    eors r2, r1
66    ldr r3, =aeabi_mem_funcs
67    ldr r3, [r3, #MEMSET]
68    bx r3
69
70wrapper_func __aeabi_memset4
71wrapper_func __aeabi_memset8
72    // 2nd/3rd args are reversed
73    eors r2, r1
74    eors r1, r2
75    eors r2, r1
76    ldr r3, =aeabi_mem_funcs
77    ldr r3, [r3, #MEMSET4]
78    bx r3
79
80wrapper_func __aeabi_memcpy4
81wrapper_func __aeabi_memcpy8
82    ldr r3, =aeabi_mem_funcs
83    ldr r3, [r3, #MEMCPY4]
84    bx r3
85
86mem_section memset
87
88wrapper_func memset
89    ldr r3, =aeabi_mem_funcs
90    ldr r3, [r3, #MEMSET]
91    bx r3
92
93mem_section memcpy
94wrapper_func __aeabi_memcpy
95wrapper_func memcpy
96    ldr r3, =aeabi_mem_funcs
97    ldr r3, [r3, #MEMCPY]
98    bx r3
99
100