KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
arch.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 arch/dreamcast/include/arch.h
4 Copyright (C) 2001 Megan Potter
5 Copyright (C) 2013, 2020 Lawrence Sebald
6
7*/
8
9/** \file arch/arch.h
10 \brief Dreamcast architecture specific options.
11 \ingroup arch
12
13 This file has various architecture specific options defined in it.
14
15 \author Megan Potter
16*/
17
18#ifndef __ARCH_ARCH_H
19#define __ARCH_ARCH_H
20
21#include <kos/cdefs.h>
22__BEGIN_DECLS
23
24#include <stdbool.h>
25#include <stdint.h>
26
27#include <kos/elf.h>
28
29/** \defgroup arch Architecture
30 \brief Dreamcast Architecture-Specific Options and high-level API
31 \ingroup system
32 @{
33*/
34
35/** \brief Top of memory available, depending on memory size. */
36#if defined(__KOS_GCC_32MB__) || __KOS_GCC_PATCHLEVEL__ >= 2025062800
37extern uint32_t _arch_mem_top;
38#else
39#pragma message "Outdated toolchain: not patched for 32MB support, limiting "\
40 "KOS to 16MB-only behavior to retain maximum compatibility. Please "\
41 "update your toolchain."
42#define _arch_mem_top ((uint32_t) 0x8d000000)
43#endif
44
45/** \brief Start and End address for .text portion of program. */
46extern char _executable_start;
47extern char _etext;
48
49#define PAGESIZE 4096 /**< \brief Page size (for MMU) */
50#define PAGESIZE_BITS 12 /**< \brief Bits for page size */
51#define PAGEMASK (PAGESIZE - 1) /**< \brief Mask for page offset */
52
53/** \brief Page count "variable".
54
55 The number of pages is static, so we can optimize this quite a bit. */
56#define page_count ((_arch_mem_top - page_phys_base) / PAGESIZE)
57
58/** \brief Base address of available physical pages. */
59#define page_phys_base 0x8c010000
60
61#ifndef THD_SCHED_HZ
62/** \brief Scheduler interrupt frequency
63
64 Timer interrupt frequency for the KOS thread scheduler.
65
66 \note
67 This value is what KOS uses initially upon startup, but it can be
68 reconfigured at run-time.
69
70 \sa thd_get_hz(), thd_set_hz()
71*/
72#define THD_SCHED_HZ 100
73#endif
74
75/** Legacy symbol for scheduler frequency.
76 * \deprecated
77 * \sa THD_SCHED_HZ
78 */
79static const
80unsigned HZ __depr("Please use the new THD_SCHED_HZ macro.") = THD_SCHED_HZ;
81
82/** \brief Global symbol prefix in ELF files. */
83#define ELF_SYM_PREFIX "_"
84
85/** \brief Length of global symbol prefix in ELF files. */
86#define ELF_SYM_PREFIX_LEN 1
87
88/** \brief Standard name for this arch. */
89#define ARCH_NAME "Dreamcast"
90
91/** \brief ELF class for this architecture. */
92#define ARCH_ELFCLASS ELFCLASS32
93
94/** \brief ELF data encoding for this architecture. */
95#define ARCH_ELFDATA ELFDATA2LSB
96
97/** \brief ELF machine type code for this architecture. */
98#define ARCH_CODE EM_SH
99
100/** \brief Panic function.
101
102 This function will cause a kernel panic, printing the specified message.
103
104 \param str The error message to print.
105 \note This function will never return!
106*/
107void arch_panic(const char *str) __noreturn;
108
109/** \brief Kernel C-level entry point.
110 \note This function will never return!
111*/
113
114/** @} */
115
116/** \defgroup arch_retpaths Exit Paths
117 \brief Potential exit paths from the kernel on
118 arch_exit()
119 \ingroup arch
120 @{
121*/
122#define ARCH_EXIT_RETURN 1 /**< \brief Return to loader */
123#define ARCH_EXIT_MENU 2 /**< \brief Return to system menu */
124#define ARCH_EXIT_REBOOT 3 /**< \brief Reboot the machine */
125/** @} */
126
127/** \brief Set the exit path.
128 \ingroup arch
129
130 The default, if you don't call this, is ARCH_EXIT_RETURN.
131
132 \param path What arch_exit() should do.
133 \see arch_retpaths
134*/
135void arch_set_exit_path(int path);
136
137/** \brief Generic kernel "exit" point.
138 \ingroup arch
139 \note This function will never return!
140*/
142
143/** \brief Kernel "return" point.
144 \ingroup arch
145 \note This function will never return!
146*/
147void arch_return(int ret_code) __noreturn;
148
149/** \brief Kernel "abort" point.
150 \ingroup arch
151 \note This function will never return!
152*/
154
155/** \brief Kernel "reboot" call.
156 \ingroup arch
157 \note This function will never return!
158*/
160
161/** \brief Kernel "exit to menu" call.
162 \ingroup arch
163 \note This function will never return!
164*/
166
167/** \defgroup hw_memsizes Memory Capacity
168 \brief Console memory sizes
169 \ingroup arch
170
171 These are the various memory sizes, in bytes, that can be returned by the
172 HW_MEMSIZE macro.
173
174 @{
175*/
176#define HW_MEM_16 16777216 /**< \brief 16M retail Dreamcast */
177#define HW_MEM_32 33554432 /**< \brief 32M NAOMI/modded Dreamcast */
178/** @} */
179
180/** \brief Determine how much memory is installed in current machine.
181 \ingroup arch
182
183 \return The total size of system memory in bytes.
184*/
185#define HW_MEMSIZE (_arch_mem_top - 0x8c000000)
186
187/** \brief Use this macro to easily determine if system has 32MB of RAM.
188 \ingroup arch
189
190 \return Non-zero if console has 32MB of RAM, zero otherwise
191*/
192#define DBL_MEM (_arch_mem_top - 0x8d000000)
193
194/* Bring in the init flags for compatibility with old code that expects them
195 here. */
196#include <kos/init.h>
197
198/* Dreamcast-specific arch init things */
199/** \brief Initialize bare-bones hardware systems.
200 \ingroup arch
201
202 This will be done automatically for you on start by the default arch_main(),
203 so you shouldn't have to deal with this yourself.
204
205 \retval 0 On success (no error conditions defined).
206*/
208
209/** \brief Initialize some peripheral systems.
210 \ingroup arch
211
212 This will be done automatically for you on start by the default arch_main(),
213 so you shouldn't have to deal with this yourself.
214
215 \retval 0 On success (no error conditions defined).
216*/
218
219/** \brief Shut down hardware that was initted.
220 \ingroup arch
221
222 This function will shut down anything initted with hardware_sys_init() and
223 hardware_periph_init(). This will be done for you automatically by the
224 various exit points, so you shouldn't have to do this yourself.
225*/
227
228/** \defgroup hw_consoles Console Types
229 \brief Byte values returned by hardware_sys_mode()
230 \ingroup arch
231
232 These are the various console types that can be returned by the
233 hardware_sys_mode() function.
234
235 @{
236*/
237#define HW_TYPE_RETAIL 0x0 /**< \brief A retail Dreamcast. */
238#define HW_TYPE_SET5 0x9 /**< \brief A Set5.xx devkit. */
239#define HW_TYPE_NAOMI 0xa /**< \brief A NAOMI arcade. */
240/** @} */
241
242/** \defgroup hw_regions Region Codes
243 \brief Values returned by hardware_sys_mode();
244 \ingroup arch
245
246 These are the various region codes that can be returned by the
247 hardware_sys_mode() function.
248
249 \note
250 A retail Dreamcast will always return 0 for the region code.
251 You must read the region of a retail device from the flashrom.
252
253 \see fr_region
254 \see flashrom_get_region()
255
256 @{
257*/
258#define HW_REGION_UNKNOWN 0x0 /**< \brief Unknown region. */
259#define HW_REGION_ASIA 0x1 /**< \brief Japan/Asia (NTSC) */
260#define HW_REGION_US 0x4 /**< \brief North America */
261#define HW_REGION_EUROPE 0xC /**< \brief Europe (PAL) */
262/** @} */
263
264/** \brief Retrieve the system mode of the console in use.
265 \ingroup arch
266
267 This function retrieves the system mode register of the console that is in
268 use. This register details the actual system type in use (and in some system
269 types the region of the device).
270
271 \param region On return, the region code (one of the
272 \ref hw_regions) of the device if the console type
273 allows reading it through the system mode register
274 -- otherwise, you must retrieve the region from the
275 flashrom.
276 \return The console type (one of the \ref hw_consoles).
277
278 \note Do not use before hardware_sys_init() has been called.
279*/
280int hardware_sys_mode(int *region);
281
282/** \brief Dreamcast specific sleep mode function.
283 \ingroup arch
284*/
285static inline void arch_sleep(void) {
286 __asm__ __volatile__("sleep\n");
287}
288
289/** \brief Returns true if the passed address is likely to be valid. Doesn't
290 have to be exact, just a sort of general idea.
291 \ingroup arch
292
293 \return Whether the address is valid or not for normal
294 memory access.
295*/
296static inline bool arch_valid_address(uintptr_t ptr) {
297 return ptr >= 0x8c010000 && ptr < _arch_mem_top;
298}
299
300/** \brief Returns true if the passed address is in the text section of your
301 program.
302 \ingroup arch
303
304 \return Whether the address is valid or not for text
305 memory access.
306*/
307static inline bool arch_valid_text_address(uintptr_t ptr) {
308 return ptr >= (uintptr_t)&_executable_start && ptr < (uintptr_t)&_etext;
309}
310
311/* The following functions are moved out of this header and are only provided for
312 compatibility reasons. Including any of them via this header is deprecated.
313*/
314
315/* Moved to <kos/banner.h> */
316const char * __pure2 kos_get_banner(void);
317const char * __pure2 kos_get_license(void);
318const char *__pure2 kos_get_authors(void);
319
320__END_DECLS
321
322#endif /* __ARCH_ARCH_H */
const char *__pure2 kos_get_banner(void)
const char *__pure2 kos_get_authors(void)
const char *__pure2 kos_get_license(void)
Various common macros used throughout the codebase.
ELF binary loading support.
void arch_return(int ret_code) __noreturn
Kernel "return" point.
void hardware_shutdown(void)
Shut down hardware that was initted.
void arch_reboot(void) __noreturn
Kernel "reboot" call.
static bool arch_valid_text_address(uintptr_t ptr)
Returns true if the passed address is in the text section of your program.
Definition arch.h:307
char _executable_start
Start and End address for .text portion of program.
static void arch_sleep(void)
Dreamcast specific sleep mode function.
Definition arch.h:285
void arch_main(void) __noreturn
Kernel C-level entry point.
int hardware_sys_init(void)
Initialize bare-bones hardware systems.
#define _arch_mem_top
Top of memory available, depending on memory size.
Definition arch.h:42
int hardware_periph_init(void)
Initialize some peripheral systems.
static const unsigned HZ
Legacy symbol for scheduler frequency.
Definition arch.h:80
void arch_set_exit_path(int path)
Set the exit path.
void arch_exit(void) __noreturn
Generic kernel "exit" point.
void arch_abort(void) __noreturn
Kernel "abort" point.
void arch_menu(void) __noreturn
Kernel "exit to menu" call.
void arch_panic(const char *str) __noreturn
Panic function.
char _etext
#define THD_SCHED_HZ
Scheduler interrupt frequency.
Definition arch.h:72
static bool arch_valid_address(uintptr_t ptr)
Returns true if the passed address is likely to be valid.
Definition arch.h:296
int hardware_sys_mode(int *region)
Retrieve the system mode of the console in use.
#define __noreturn
Identify a function that will never return.
Definition cdefs.h:49
Initialization-related flags and macros.