CHGame 0.3.0
Libraries for the CHGame handheld
Loading...
Searching...
No Matches
RamFunc.h
Go to the documentation of this file.
1/// @file RamFunc.h
2/// @brief RAMFUNC(name): run a function from SRAM.
3///
4/// The section name is CHGfx 1.3's trick: the core's link script puts
5/// *(.gnu.linkonce.r.*) first in .data (copied to SRAM at boot), ahead of
6/// .sdata/.sbss. linkonce merges sections of the same name, so every
7/// function needs a name of its own (which also lets the linker drop unused
8/// ones one at a time). The library's are "chg.<name>"; CHGfx's are
9/// "chgfx.<name>". A sketch's own, RAMFUNC(name), are "app.<name>", so a
10/// sketch's function can never merge with one of the library's of the same
11/// name (but two of the sketch's must have different names).
12///
13/// The simulator keeps noinline but not the section: Mach-O (macOS) rejects
14/// ELF section names.
15#pragma once
16
17/// @defgroup chgame_ramfunc RAM functions
18/// @ingroup lib_chgame
19/// @brief RAMFUNC(name): put a hot function in SRAM, about three times faster than flash.
20///
21/// Flash has 3 wait states and no cache: from flash a per-pixel loop costs
22/// ~3 us a pixel on this part; from SRAM about a third of that. Every
23/// RAMFUNC costs its size in RAM, so keep it for the inner loops.
24/// @code
25/// RAMFUNC(blitRow) static void blitRow(uint8_t *dst, ...) { ... }
26/// @endcode
27/// @{
28
29#if defined(__riscv) && !defined(CHSIM)
30/// @brief The library's own SRAM functions (section "chg.<name>").
31/// @param name A name unique among the library's RAM functions.
32#define CHGAME_RAMFUNC(name) __attribute__((section(".gnu.linkonce.r.chg." #name), noinline))
33/// @brief A sketch's SRAM functions (section "app.<name>"); RAMFUNC() is this.
34/// @param name A name unique within the sketch.
35#define CHGAME_APP_RAMFUNC(name) __attribute__((section(".gnu.linkonce.r.app." #name), noinline))
36#else
37#define CHGAME_RAMFUNC(name) __attribute__((noinline)) // the simulator, host tests and tools
38#define CHGAME_APP_RAMFUNC(name) __attribute__((noinline))
39#endif
40
41#ifndef RAMFUNC
42/// @brief Run this function from SRAM. Put it before the function's declaration.
43/// @param name A name unique within the sketch (any identifier; the
44/// function's own name is the usual choice).
45#define RAMFUNC(name) CHGAME_APP_RAMFUNC(name)
46#endif
47
48/// @}