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
26#include <arch/types.h>
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
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) 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/* These are in mm.c */
195/** \brief Initialize the memory management system.
196 \ingroup arch
197
198 \retval 0 On success (no error conditions defined).
199*/
200int mm_init(void);
201
202/** \brief Request more core memory from the system.
203 \ingroup arch
204
205 \param increment The number of bytes requested.
206 \return A pointer to the memory.
207 \note This function will panic if no memory is available.
208*/
209void * mm_sbrk(unsigned long increment);
210
211/* Bring in the init flags for compatibility with old code that expects them
212 here. */
213#include <kos/init.h>
214
215/* Dreamcast-specific arch init things */
216/** \brief Initialize bare-bones hardware systems.
217 \ingroup arch
218
219 This will be done automatically for you on start by the default arch_main(),
220 so you shouldn't have to deal with this yourself.
221
222 \retval 0 On success (no error conditions defined).
223*/
225
226/** \brief Initialize some peripheral systems.
227 \ingroup arch
228
229 This will be done automatically for you on start by the default arch_main(),
230 so you shouldn't have to deal with this yourself.
231
232 \retval 0 On success (no error conditions defined).
233*/
235
236/** \brief Shut down hardware that was initted.
237 \ingroup arch
238
239 This function will shut down anything initted with hardware_sys_init() and
240 hardware_periph_init(). This will be done for you automatically by the
241 various exit points, so you shouldn't have to do this yourself.
242*/
244
245/** \defgroup hw_consoles Console Types
246 \brief Byte values returned by hardware_sys_mode()
247 \ingroup arch
248
249 These are the various console types that can be returned by the
250 hardware_sys_mode() function.
251
252 @{
253*/
254#define HW_TYPE_RETAIL 0x0 /**< \brief A retail Dreamcast. */
255#define HW_TYPE_SET5 0x9 /**< \brief A Set5.xx devkit. */
256#define HW_TYPE_NAOMI 0xa /**< \brief A NAOMI arcade. */
257/** @} */
258
259/** \defgroup hw_regions Region Codes
260 \brief Values returned by hardware_sys_mode();
261 \ingroup arch
262
263 These are the various region codes that can be returned by the
264 hardware_sys_mode() function.
265
266 \note
267 A retail Dreamcast will always return 0 for the region code.
268 You must read the region of a retail device from the flashrom.
269
270 \see fr_region
271 \see flashrom_get_region()
272
273 @{
274*/
275#define HW_REGION_UNKNOWN 0x0 /**< \brief Unknown region. */
276#define HW_REGION_ASIA 0x1 /**< \brief Japan/Asia (NTSC) */
277#define HW_REGION_US 0x4 /**< \brief North America */
278#define HW_REGION_EUROPE 0xC /**< \brief Europe (PAL) */
279/** @} */
280
281/** \brief Retrieve the system mode of the console in use.
282 \ingroup arch
283
284 This function retrieves the system mode register of the console that is in
285 use. This register details the actual system type in use (and in some system
286 types the region of the device).
287
288 \param region On return, the region code (one of the
289 \ref hw_regions) of the device if the console type
290 allows reading it through the system mode register
291 -- otherwise, you must retrieve the region from the
292 flashrom.
293 \return The console type (one of the \ref hw_consoles).
294
295 \note Do not use before hardware_sys_init() has been called.
296*/
297int hardware_sys_mode(int *region);
298
299/** \brief Dreamcast specific sleep mode function.
300 \ingroup arch
301*/
302static inline void arch_sleep(void) {
303 __asm__ __volatile__("sleep\n");
304}
305
306/** \brief Returns true if the passed address is likely to be valid. Doesn't
307 have to be exact, just a sort of general idea.
308 \ingroup arch
309
310 \return Whether the address is valid or not for normal
311 memory access.
312*/
313static inline bool arch_valid_address(uintptr_t ptr) {
314 return ptr >= 0x8c010000 && ptr < _arch_mem_top;
315}
316
317/** \brief Returns true if the passed address is in the text section of your
318 program.
319 \ingroup arch
320
321 \return Whether the address is valid or not for text
322 memory access.
323*/
324static inline bool arch_valid_text_address(uintptr_t ptr) {
325 return ptr >= (uintptr_t)&_executable_start && ptr < (uintptr_t)&_etext;
326}
327
328/* The following functions are moved out of this header and are only provided for
329 compatibility reasons. Including any of them via this header is deprecated.
330*/
331
332/* Moved to <kos/banner.h> */
333const char * __pure2 kos_get_banner(void);
334const char * __pure2 kos_get_license(void);
335const char *__pure2 kos_get_authors(void);
336
337__END_DECLS
338
339#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:324
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:302
int mm_init(void)
Initialize the memory management system.
void arch_main(void) __noreturn
Kernel C-level entry point.
int hardware_sys_init(void)
Initialize bare-bones hardware systems.
void * mm_sbrk(unsigned long increment)
Request more core memory from the system.
#define _arch_mem_top
Top of memory available, depending on memory size.
Definition arch.h:42
static const unsigned HZ("Please use the new THD_SCHED_HZ macro.")
Legacy symbol for scheduler frequency.
int hardware_periph_init(void)
Initialize some peripheral systems.
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:313
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
unsigned long uint32
32-bit unsigned integer
Definition types.h:33
Initialization-related flags and macros.
Common integer types.