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. Also, any
14 functions that start with arch_ are in here.
15
16 \author Megan Potter
17*/
18
19#ifndef __ARCH_ARCH_H
20#define __ARCH_ARCH_H
21
22#include <kos/cdefs.h>
23__BEGIN_DECLS
24
25#include <stdbool.h>
26
27#include <arch/types.h>
28
29/** \defgroup arch Architecture
30 \brief Dreamcast Architecture-Specific Options and high-level API
31 \ingroup system
32 @{
33*/
34
35/** \brief Returns the embedded application name.
36 \ingroup arch
37
38 This returns the app name string embedded at build time via the
39 KOS_APP_NAME Makefile variable. If no embedded name is present,
40 it falls back to "prog.elf".
41
42 \return A pointer to a null-terminated string
43 representing the application name.
44*/
45const char *get_app_name(void);
46
47/** \brief Top of memory available, depending on memory size. */
48#ifdef __KOS_GCC_32MB__
50#else
51#pragma message "Outdated toolchain: not patched for 32MB support, limiting "\
52 "KOS to 16MB-only behavior to retain maximum compatibility. Please "\
53 "update your toolchain."
54#define _arch_mem_top ((uint32) 0x8d000000)
55#endif
56
57/** \brief Start and End address for .text portion of program. */
58extern char _executable_start;
59extern char _etext;
60
61#define PAGESIZE 4096 /**< \brief Page size (for MMU) */
62#define PAGESIZE_BITS 12 /**< \brief Bits for page size */
63#define PAGEMASK (PAGESIZE - 1) /**< \brief Mask for page offset */
64
65/** \brief Page count "variable".
66
67 The number of pages is static, so we can optimize this quite a bit. */
68#define page_count ((_arch_mem_top - page_phys_base) / PAGESIZE)
69
70/** \brief Base address of available physical pages. */
71#define page_phys_base 0x8c010000
72
73#ifndef THD_SCHED_HZ
74/** \brief Scheduler interrupt frequency
75
76 Timer interrupt frequency for the KOS thread scheduler.
77
78 \note
79 This value is what KOS uses initially upon startup, but it can be
80 reconfigured at run-time.
81
82 \sa thd_get_hz(), thd_set_hz()
83*/
84#define THD_SCHED_HZ 100
85#endif
86
87/** Legacy symbol for scheduler frequency.
88 * \deprecated
89 * \sa THD_SCHED_HZ
90 */
91static const
92unsigned HZ __depr("Please use the new THD_SCHED_HZ macro.") = THD_SCHED_HZ;
93
94#ifndef THD_STACK_SIZE
95/** \brief Default thread stack size. */
96#define THD_STACK_SIZE 32768
97#endif
98
99#ifndef THD_KERNEL_STACK_SIZE
100/** \brief Main/kernel thread's stack size. */
101#define THD_KERNEL_STACK_SIZE (64 * 1024)
102#endif
103
104/** \brief Default video mode. */
105#define DEFAULT_VID_MODE DM_640x480
106
107/** \brief Default pixel mode for video. */
108#define DEFAULT_PIXEL_MODE PM_RGB565
109
110/** \brief Default serial bitrate. */
111#define DEFAULT_SERIAL_BAUD 115200
112
113/** \brief Default serial FIFO behavior. */
114#define DEFAULT_SERIAL_FIFO 1
115
116/** \brief Global symbol prefix in ELF files. */
117#define ELF_SYM_PREFIX "_"
118
119/** \brief Length of global symbol prefix in ELF files. */
120#define ELF_SYM_PREFIX_LEN 1
121
122/** \brief Panic function.
123
124 This function will cause a kernel panic, printing the specified message.
125
126 \param str The error message to print.
127 \note This function will never return!
128*/
129void arch_panic(const char *str) __noreturn;
130
131/** \brief Kernel C-level entry point.
132 \note This function will never return!
133*/
135
136/** @} */
137
138/** \defgroup arch_retpaths Exit Paths
139 \brief Potential exit paths from the kernel on
140 arch_exit()
141 \ingroup arch
142 @{
143*/
144#define ARCH_EXIT_RETURN 1 /**< \brief Return to loader */
145#define ARCH_EXIT_MENU 2 /**< \brief Return to system menu */
146#define ARCH_EXIT_REBOOT 3 /**< \brief Reboot the machine */
147/** @} */
148
149/** \brief Set the exit path.
150 \ingroup arch
151
152 The default, if you don't call this, is ARCH_EXIT_RETURN.
153
154 \param path What arch_exit() should do.
155 \see arch_retpaths
156*/
157void arch_set_exit_path(int path);
158
159/** \brief Generic kernel "exit" point.
160 \ingroup arch
161 \note This function will never return!
162*/
164
165/** \brief Kernel "return" point.
166 \ingroup arch
167 \note This function will never return!
168*/
169void arch_return(int ret_code) __noreturn;
170
171/** \brief Kernel "abort" point.
172 \ingroup arch
173 \note This function will never return!
174*/
176
177/** \brief Kernel "reboot" call.
178 \ingroup arch
179 \note This function will never return!
180*/
182
183/** \brief Kernel "exit to menu" call.
184 \ingroup arch
185 \note This function will never return!
186*/
188
189/** \defgroup hw_memsizes Memory Capacity
190 \brief Console memory sizes
191 \ingroup arch
192
193 These are the various memory sizes, in bytes, that can be returned by the
194 HW_MEMSIZE macro.
195
196 @{
197*/
198#define HW_MEM_16 16777216 /**< \brief 16M retail Dreamcast */
199#define HW_MEM_32 33554432 /**< \brief 32M NAOMI/modded Dreamcast */
200/** @} */
201
202/** \brief Determine how much memory is installed in current machine.
203 \ingroup arch
204
205 \return The total size of system memory in bytes.
206*/
207#define HW_MEMSIZE (_arch_mem_top - 0x8c000000)
208
209/** \brief Use this macro to easily determine if system has 32MB of RAM.
210 \ingroup arch
211
212 \return Non-zero if console has 32MB of RAM, zero otherwise
213*/
214#define DBL_MEM (_arch_mem_top - 0x8d000000)
215
216/* These are in mm.c */
217/** \brief Initialize the memory management system.
218 \ingroup arch
219
220 \retval 0 On success (no error conditions defined).
221*/
222int mm_init(void);
223
224/** \brief Request more core memory from the system.
225 \ingroup arch
226
227 \param increment The number of bytes requested.
228 \return A pointer to the memory.
229 \note This function will panic if no memory is available.
230*/
231void * mm_sbrk(unsigned long increment);
232
233/* Bring in the init flags for compatibility with old code that expects them
234 here. */
235#include <kos/init.h>
236
237/* Dreamcast-specific arch init things */
238/** \brief Jump back to the bootloader.
239 \ingroup arch
240
241 You generally shouldn't use this function, but rather use arch_exit() or
242 exit() instead.
243
244 \note This function will never return!
245*/
246void arch_real_exit(int ret_code) __noreturn;
247
248/** \brief Initialize bare-bones hardware systems.
249 \ingroup arch
250
251 This will be done automatically for you on start by the default arch_main(),
252 so you shouldn't have to deal with this yourself.
253
254 \retval 0 On success (no error conditions defined).
255*/
257
258/** \brief Initialize some peripheral systems.
259 \ingroup arch
260
261 This will be done automatically for you on start by the default arch_main(),
262 so you shouldn't have to deal with this yourself.
263
264 \retval 0 On success (no error conditions defined).
265*/
267
268/** \brief Shut down hardware that was initted.
269 \ingroup arch
270
271 This function will shut down anything initted with hardware_sys_init() and
272 hardware_periph_init(). This will be done for you automatically by the
273 various exit points, so you shouldn't have to do this yourself.
274*/
276
277/** \defgroup hw_consoles Console Types
278 \brief Byte values returned by hardware_sys_mode()
279 \ingroup arch
280
281 These are the various console types that can be returned by the
282 hardware_sys_mode() function.
283
284 @{
285*/
286#define HW_TYPE_RETAIL 0x0 /**< \brief A retail Dreamcast. */
287#define HW_TYPE_SET5 0x9 /**< \brief A Set5.xx devkit. */
288/** @} */
289
290/** \defgroup hw_regions Region Codes
291 \brief Values returned by hardware_sys_mode();
292 \ingroup arch
293
294 These are the various region codes that can be returned by the
295 hardware_sys_mode() function.
296
297 \note
298 A retail Dreamcast will always return 0 for the region code.
299 You must read the region of a retail device from the flashrom.
300
301 \see fr_region
302 \see flashrom_get_region()
303
304 @{
305*/
306#define HW_REGION_UNKNOWN 0x0 /**< \brief Unknown region. */
307#define HW_REGION_ASIA 0x1 /**< \brief Japan/Asia (NTSC) */
308#define HW_REGION_US 0x4 /**< \brief North America */
309#define HW_REGION_EUROPE 0xC /**< \brief Europe (PAL) */
310/** @} */
311
312/** \brief Retrieve the system mode of the console in use.
313 \ingroup arch
314
315 This function retrieves the system mode register of the console that is in
316 use. This register details the actual system type in use (and in some system
317 types the region of the device).
318
319 \param region On return, the region code (one of the
320 \ref hw_regions) of the device if the console type
321 allows reading it through the system mode register
322 -- otherwise, you must retrieve the region from the
323 flashrom.
324 \return The console type (one of the \ref hw_consoles).
325*/
326int hardware_sys_mode(int *region);
327
328/* These three aught to be in their own header file at some point, but for now,
329 they'll stay here. */
330
331/** \brief Retrieve the banner printed at program initialization.
332 \ingroup attribution
333
334 This function retrieves the banner string that is printed at initialization
335 time by the kernel. This contains the version of KOS in use and basic
336 information about the environment in which it was compiled.
337
338 \return A pointer to the banner string.
339*/
340const char *kos_get_banner(void);
341
342/** \brief Retrieve the license information for the compiled copy of KOS.
343 \ingroup attribution
344
345 This function retrieves a string containing the license terms that the
346 version of KOS in use is distributed under. This can be used to easily add
347 information to your program to be displayed at runtime.
348
349 \return A pointer to the license terms.
350*/
351const char *kos_get_license(void);
352
353/** \brief Retrieve a list of authors and the dates of their contributions.
354 \ingroup attribution
355
356 This function retrieves the copyright information for the version of KOS in
357 use. This function can be used to add such information to the credits of
358 programs using KOS to give the appropriate credit to those that have worked
359 on KOS.
360
361 \remark
362 Remember, you do need to give credit where credit is due, and this is an
363 easy way to do so. ;-)
364
365 \return A pointer to the authors' copyright information.
366*/
367const char *kos_get_authors(void);
368
369/** \brief Dreamcast specific sleep mode function.
370 \ingroup arch
371*/
372static inline void arch_sleep(void) {
373 __asm__ __volatile__("sleep\n");
374}
375
376/** \brief DC specific "function" to get the return address from the current
377 function.
378 \ingroup arch
379
380 \return The return address of the current function.
381*/
382static inline uintptr_t arch_get_ret_addr(void) {
383 uintptr_t pr;
384
385 __asm__ __volatile__("sts pr,%0\n" : "=r"(pr));
386
387 return pr;
388}
389
390/* Please note that all of the following frame pointer macros are ONLY
391 valid if you have compiled your code WITHOUT -fomit-frame-pointer. These
392 are mainly useful for getting a stack trace from an error. */
393
394/** \brief DC specific "function" to get the frame pointer from the current
395 function.
396 \ingroup arch
397
398 \return The frame pointer from the current function.
399 \note This only works if you don't disable frame pointers.
400*/
401static inline uintptr_t arch_get_fptr(void) {
402 register uintptr_t fp __asm__("r14");
403
404 return fp;
405}
406
407/** \brief Pass in a frame pointer value to get the return address for the
408 given frame.
409 \ingroup arch
410
411 \param fptr The frame pointer to look at.
412 \return The return address of the pointer.
413*/
414static inline uintptr_t arch_fptr_ret_addr(uintptr_t fptr) {
415 return *(uintptr_t *)fptr;
416}
417
418/** \brief Pass in a frame pointer value to get the previous frame pointer for
419 the given frame.
420 \ingroup arch
421
422 \param fptr The frame pointer to look at.
423 \return The previous frame pointer.
424*/
425static inline uintptr_t arch_fptr_next(uintptr_t fptr) {
426 return arch_fptr_ret_addr(fptr + 4);
427}
428
429/** \brief Returns true if the passed address is likely to be valid. Doesn't
430 have to be exact, just a sort of general idea.
431 \ingroup arch
432
433 \return Whether the address is valid or not for normal
434 memory access.
435*/
436static inline bool arch_valid_address(uintptr_t ptr) {
437 return ptr >= 0x8c010000 && ptr < _arch_mem_top;
438}
439
440/** \brief Returns true if the passed address is in the text section of your
441 program.
442 \ingroup arch
443
444 \return Whether the address is valid or not for text
445 memory access.
446*/
447static inline bool arch_valid_text_address(uintptr_t ptr) {
448 return ptr >= (uintptr_t)&_executable_start && ptr < (uintptr_t)&_etext;
449}
450
451__END_DECLS
452
453#endif /* __ARCH_ARCH_H */
Various common macros used throughout the codebase.
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.
void arch_real_exit(int ret_code) __noreturn
Jump back to the bootloader.
const char * get_app_name(void)
Returns the embedded application name.
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:447
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:372
int mm_init(void)
Initialize the memory management system.
static uintptr_t arch_get_fptr(void)
DC specific "function" to get the frame pointer from the current function.
Definition arch.h:401
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:54
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.
static uintptr_t arch_fptr_ret_addr(uintptr_t fptr)
Pass in a frame pointer value to get the return address for the given frame.
Definition arch.h:414
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.
static uintptr_t arch_get_ret_addr(void)
DC specific "function" to get the return address from the current function.
Definition arch.h:382
void arch_panic(const char *str) __noreturn
Panic function.
static uintptr_t arch_fptr_next(uintptr_t fptr)
Pass in a frame pointer value to get the previous frame pointer for the given frame.
Definition arch.h:425
char _etext
#define THD_SCHED_HZ
Scheduler interrupt frequency.
Definition arch.h:84
static bool arch_valid_address(uintptr_t ptr)
Returns true if the passed address is likely to be valid.
Definition arch.h:436
int hardware_sys_mode(int *region)
Retrieve the system mode of the console in use.
const char * kos_get_license(void)
Retrieve the license information for the compiled copy of KOS.
const char * kos_get_banner(void)
Retrieve the banner printed at program initialization.
const char * kos_get_authors(void)
Retrieve a list of authors and the dates of their contributions.
#define __noreturn
Identify a function that will never return.
Definition cdefs.h:47
unsigned long uint32
32-bit unsigned integer
Definition types.h:33
Initialization-related flags and macros.
Common integer types.