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