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