KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
cache.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 arch/dreamcast/include/dc/cache.h
4 Copyright (C) 2025 Paul Cercueil
5 Copyright (C) 2025 Matt Slevinsky
6 Copyright (C) 2025 TapamN
7*/
8
9/** \file dc/cache.h
10 \brief Cache management functionality.
11 \ingroup system_cache
12
13 This file contains definitions and low-level routines to manipulate the
14 instruction and data caches.
15
16 \author Paul Cercueil
17 \author Matt Slevinsky
18 \author TapamN
19*/
20
21#ifndef __DC_CACHE_H
22#define __DC_CACHE_H
23
24#include <kos/cdefs.h>
25__BEGIN_DECLS
26
27#include <kos/regfield.h>
28#include <stdbool.h>
29#include <stdint.h>
30
31#define CCR_OCE BIT(0) /** < OC enable */
32#define CCR_WT BIT(1) /** < Write-through enable (for P0/U0/P3 in non-MMU mode) */
33#define CCR_CB BIT(2) /** < Copy-back enable for P1 area */
34#define CCR_OCI BIT(3) /** < OC invalidate */
35#define CCR_ORA BIT(5) /** < OC RAM enable */
36#define CCR_OIX BIT(7) /** < OC INDEX enable */
37#define CCR_ICE BIT(8) /** < IC enable (in non-MMU mode) */
38#define CCR_ICI BIT(11) /** < IC invalidate */
39#define CCR_IIX BIT(15) /** < IC INDEX enable */
40
41/** \brief Default cache settings */
42#define CCR_DEFAULT (CCR_ICI | CCR_ICE | CCR_OCI | CCR_CB | CCR_OCE)
43
44/** \brief Write CCR register.
45
46 This function is used internally to write the cache control register.
47
48 \param mask Mask of the bits to update
49 \param value Value to replace the masked bits with
50*/
51void cache_write_ccr(uint32_t mask, uint32_t value);
52
53/** \brief Enable or disable OCINDEX mode.
54
55 This function can be used to enable or disable OCINDEX mode.
56
57 In OCINDEX mode, the bits 25 and 12-5 are used as the data cache line index.
58 In non-OCINDEX mode, the bits 13-5 are used as the data cache line index.
59
60 Using OCINDEX, data memory-mapped to an address with bit 25 set
61 (e.g. 0x2000000) will always stay in cache (unless its cache line is
62 reused by another address with bit 25 set), as the RAM size on supported
63 SH4 platforms is lower than (1 << 25), meaning that for common RAM
64 addresses, bit 25 is always cleared.
65
66 \param enable Whether or not to enable OCINDEX mode
67*/
68static inline void dcache_toggle_ocindex(bool enable) {
69 cache_write_ccr(CCR_OIX, enable ? CCR_OIX : 0);
70}
71
72/** \brief Enable or disable OCRAM mode.
73
74 This function can be used to enable or disable OCRAM mode.
75
76 In OCRAM mode, half the cache can be used as a scratchpad.
77
78 In OCRAM mode, the bits 13 and 11-5 are used as the cache line index.
79 In non-OCRAM mode, the bits 13-5 are used as the cache line index.
80
81 Using OCRAM mode, the cache lines 128 to 255 and 384 to 511 are guaranteed
82 to be unused, and can be accessed directly as if it was RAM. The 8 KiB
83 scratchpad can then be accessed at address 0x7c001000, or preferably
84 through any variable tagged with the __ocram tag.
85
86 \param enable Whether or not to enable OCINDEX mode
87*/
88static inline void dcache_toggle_ocram(bool enable) {
89 cache_write_ccr(CCR_ORA, enable ? CCR_ORA : 0);
90}
91
92/** \brief Enable or disable ICINDEX mode.
93
94 This function can be used to enable or disable ICINDEX mode.
95
96 In ICINDEX mode, the bits 25 and 11-5 are used as the instruction cache line
97 index. In non-ICINDEX mode, the bits 12-5 are used as the instruction cache
98 line index.
99
100 Using ICINDEX, code memory-mapped to an address with bit 25 set
101 (e.g. 0x2000000) will always stay in cache (unless its cache line is
102 reused by another address with bit 25 set), as the RAM size on supported
103 SH4 platforms is lower than (1 << 25), meaning that for common RAM
104 addresses, bit 25 is always cleared.
105
106 \param enable Whether or not to enable ICINDEX mode
107*/
108static inline void icache_toggle_icindex(bool enable) {
109 cache_write_ccr(CCR_IIX, (enable ? CCR_IIX : 0) | CCR_ICI);
110}
111
112/** \brief Mark a variable as residing in OCRAM
113
114 This can be used to place a variable in OCRAM space. Before any access
115 (read or write) can be made, OCRAM needs to be enabled.
116 Also note that after enabling OCRAM the variable will contain garbage, so
117 the code must not rely on the content of the variable, and must not be
118 statically initialized (even to zero).
119
120 Example:
121 __ocram static uint32_t my_array[16];
122*/
123#define __ocram __attribute__((section(".ocram")))
124
125__END_DECLS
126
127#endif /* __DC_CACHE_H */
Various common macros used throughout the codebase.
static void dcache_toggle_ocindex(bool enable)
Enable or disable OCINDEX mode.
Definition cache.h:68
static void dcache_toggle_ocram(bool enable)
Enable or disable OCRAM mode.
Definition cache.h:88
#define CCR_IIX
Definition cache.h:39
void cache_write_ccr(uint32_t mask, uint32_t value)
Write CCR register.
static void icache_toggle_icindex(bool enable)
Enable or disable ICINDEX mode.
Definition cache.h:108
#define CCR_ICI
Definition cache.h:38
#define CCR_OIX
Definition cache.h:36
#define CCR_ORA
Definition cache.h:35
Macros to help dealing with register fields.