Jo Engine  2024.04.28
Jo Sega Saturn Engine
tools.h
Go to the documentation of this file.
1 /*
2 ** Jo Sega Saturn Engine
3 ** Copyright (c) 2012-2024, Johannes Fetz (johannesfetz@gmail.com)
4 ** All rights reserved.
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are met:
8 ** * Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** * Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** * Neither the name of the Johannes Fetz nor the
14 ** names of its contributors may be used to endorse or promote products
15 ** derived from this software without specific prior written permission.
16 **
17 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 ** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
21 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
35 #ifndef __JO_TOOLS_H__
36 # define __JO_TOOLS_H__
37 
38 /*
39 ** INCLUDES
40 */
41 #include "math.h"
42 
44 #define JO_FIXED_TV_WIDTH JO_INT_TO_FIXED(JO_TV_WIDTH)
45 
46 #define JO_FIXED_TV_HEIGHT JO_INT_TO_FIXED(JO_TV_HEIGHT)
47 
52 static __jo_force_inline short jo_swap_endian_short(short value)
53 {
54  return ((((value) >> 8) & 0xff) | (((value) & 0xff) << 8));
55 }
56 
61 static __jo_force_inline unsigned short jo_swap_endian_ushort(unsigned short value)
62 {
63  return ((((value) >> 8) & 0xff) | (((value) & 0xff) << 8));
64 }
65 
70 static __jo_force_inline unsigned int jo_swap_endian_uint(unsigned int value)
71 {
72  return ((((value) & 0xff000000) >> 24) | (((value) & 0x00ff0000) >> 8) | (((value) & 0x0000ff00) << 8) | (((value) & 0x000000ff) << 24));
73 }
74 
80 {
81  return ((((value) & 0xff000000) >> 24) | (((value) & 0x00ff0000) >> 8) | (((value) & 0x0000ff00) << 8) | (((value) & 0x000000ff) << 24));
82 }
83 
90 int sprintf(char* str, const char* format, ...);
91 
92 #ifdef JO_DEBUG
93 
94 #define jo_printf_debug(X, Y, fmt, args...) \
95  jo_printf(X, Y, "%s(%s)#%d : " fmt , \
96  __FILE__, __FUNCTION__, __LINE__, ## args)
97 
98 #endif
99 
104 
105 #ifdef JO_COMPILE_WITH_PRINTF_SUPPORT
106 
107 #if !JO_COMPILE_USING_SGL
108 extern unsigned char __jo_printf_current_palette_index;
109 #endif
110 
116 void jo_print(int x, int y, char * str);
117 
123 # define jo_printf(X, Y, ...) do {sprintf(__jo_sprintf_buf, __VA_ARGS__); jo_print(X, Y, __jo_sprintf_buf); } while(0)
124 
131 # define jo_printf_with_color(X, Y, COLOR_INDEX, ...) do { jo_set_printf_color_index(COLOR_INDEX); jo_printf(X, Y, __VA_ARGS__); } while(0)
132 
136 static __jo_force_inline void jo_set_printf_color_index(const unsigned char index)
137 {
138 #if JO_COMPILE_USING_SGL
139  slCurColor(index);
140 #else
141  __jo_printf_current_palette_index = index;
142 #endif
143 }
144 
148 void jo_clear_screen(void);
149 
154 void jo_clear_screen_line(const int y);
155 
156 #endif // JO_COMPILE_WITH_PRINTF_SUPPORT
157 
164 static __jo_force_inline void jo_dma_copy(void *src, void *dest, unsigned int size)
165 {
166 #if JO_COMPILE_USING_SGL
167  slDMACopy(src, dest, size);
168 #else
169  slDMACopy(src, dest, size); // TODO
170 #endif
171 }
172 
175 # define JO_NULL ((void *)0)
176 
179 # define JO_UNUSED_ARG(ARG) ((void)ARG)
180 
185 static __jo_force_inline bool jo_tools_is_whitespace(const char c)
186 {
187  return c == '\n' || c == '\r' || c == '\t' || c == ' ';
188 }
189 
194 static __jo_force_inline int jo_4_char_hash(const char * const str)
195 {
196  return (int)str[0] | (((int)str[1]) << 8) | (((int)str[2]) << 16) | (((int)str[3]) << 24);
197 }
198 
201 typedef struct
202 {
203  unsigned char day;
204  unsigned char month;
205  unsigned short year;
206  unsigned char hour;
207  unsigned char week;
208  unsigned char minute;
209  unsigned char second;
210 } jo_datetime;
211 
214 typedef enum
215 {
216  English = 0,
217  Deutsch = 1,
218  French = 2,
219  Espanol = 3,
220  Italiano = 4,
221  Japanese = 5
223 
226 typedef enum
227 {
230  Right
232 
237 
242 
247 int jo_tools_atoi(const char * restrict str);
248 
253 int jo_strlen(const char * restrict str);
254 
260 int jo_strcmp(const char * restrict p1, const char * restrict p2);
261 
267 static __jo_force_inline bool jo_string_equals(const char * restrict p1, const char * restrict p2)
268 {
269  return (jo_strcmp(p1, p2) == 0);
270 }
271 
277 bool jo_endwith(const char * restrict str, const char * restrict end);
278 
285 void jo_memset(const void * const restrict ptr, const int value, unsigned int num);
286 
289 static __jo_force_inline void jo_yield(void)
290 {
291  asm("nop");
292 }
293 
297 static __jo_force_inline void jo_spin_wait(int iterations)
298 {
299  while (iterations--) jo_yield();
300 }
301 
310 void jo_map_to_vram(unsigned short * restrict data, unsigned short * restrict vram_addr,
311  unsigned short suuj, unsigned short suui, unsigned short palnum, unsigned int mapoff);
312 
318 static __jo_force_inline void jo_cell_to_vram(unsigned char * restrict data, unsigned char * restrict vram_addr, unsigned int size)
319 {
320  while (size-- > 0) *(vram_addr++) = *(data++);
321 }
322 
323 #endif /* !__JO_TOOLS_H__ */
324 
325 /*
326 ** END OF FILE
327 */
jo_datetime::hour
unsigned char hour
Definition: tools.h:206
jo_map_to_vram
void jo_map_to_vram(unsigned short *restrict data, unsigned short *restrict vram_addr, unsigned short suuj, unsigned short suui, unsigned short palnum, unsigned int mapoff)
Copies the image map data to VRAM.
jo_getdate
void jo_getdate(jo_datetime *now)
get current date and time
jo_datetime::month
unsigned char month
Definition: tools.h:204
jo_datetime
DateTime struct.
Definition: tools.h:202
French
@ French
Definition: tools.h:218
Right
@ Right
Definition: tools.h:230
jo_strlen
int jo_strlen(const char *restrict str)
strlen implementation
jo_swap_endian_ushort
static __jo_force_inline unsigned short jo_swap_endian_ushort(unsigned short value)
Swap Unsigned short endian.
Definition: tools.h:61
sprintf
int sprintf(char *str, const char *format,...)
sprintf prototypes
jo_string_equals
static __jo_force_inline bool jo_string_equals(const char *restrict p1, const char *restrict p2)
Determine if two string equals.
Definition: tools.h:267
jo_swap_endian_short
static __jo_force_inline short jo_swap_endian_short(short value)
Swap Short endian.
Definition: tools.h:52
jo_tools_atoi
int jo_tools_atoi(const char *restrict str)
atoi implementation
math.h
Jo Engine Math tools.
jo_horizontal_move
jo_horizontal_move
Horizontal move.
Definition: tools.h:227
__jo_sprintf_buf
char __jo_sprintf_buf[JO_PRINTF_BUF_SIZE]
Internal sprintf buffer.
jo_tools_is_whitespace
static __jo_force_inline bool jo_tools_is_whitespace(const char c)
Check if the character is a whitespace.
Definition: tools.h:185
jo_swap_endian_int
static __jo_force_inline int jo_swap_endian_int(int value)
Swap int endian.
Definition: tools.h:79
English
@ English
Definition: tools.h:216
jo_swap_endian_uint
static __jo_force_inline unsigned int jo_swap_endian_uint(unsigned int value)
Swap unsigned int endian.
Definition: tools.h:70
JO_PRINTF_BUF_SIZE
#define JO_PRINTF_BUF_SIZE
Max character in available in jo_printf()
Definition: conf.h:165
__jo_force_inline
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition: types.h:39
jo_datetime::week
unsigned char week
Definition: tools.h:207
jo_get_current_language
jo_language jo_get_current_language(void)
Get Sega saturn current language.
jo_cell_to_vram
static __jo_force_inline void jo_cell_to_vram(unsigned char *restrict data, unsigned char *restrict vram_addr, unsigned int size)
Copies the image cel data to VRAM.
Definition: tools.h:318
jo_datetime::year
unsigned short year
Definition: tools.h:205
Italiano
@ Italiano
Definition: tools.h:220
jo_language
jo_language
Language type.
Definition: tools.h:215
Japanese
@ Japanese
Definition: tools.h:221
jo_endwith
bool jo_endwith(const char *restrict str, const char *restrict end)
determine if str end with a specific string
jo_strcmp
int jo_strcmp(const char *restrict p1, const char *restrict p2)
strcmp implementation
jo_datetime::second
unsigned char second
Definition: tools.h:209
jo_spin_wait
static __jo_force_inline void jo_spin_wait(int iterations)
Causes "a thread" to wait the number of times defined by the iterations parameter.
Definition: tools.h:297
jo_dma_copy
static __jo_force_inline void jo_dma_copy(void *src, void *dest, unsigned int size)
DMA copy.
Definition: tools.h:164
jo_yield
static __jo_force_inline void jo_yield(void)
Little pause possible.
Definition: tools.h:289
jo_datetime::minute
unsigned char minute
Definition: tools.h:208
None
@ None
Definition: tools.h:228
jo_datetime::day
unsigned char day
Definition: tools.h:203
jo_4_char_hash
static __jo_force_inline int jo_4_char_hash(const char *const str)
Create an hash code based on the first four character of the string.
Definition: tools.h:194
Espanol
@ Espanol
Definition: tools.h:219
jo_memset
void jo_memset(const void *const restrict ptr, const int value, unsigned int num)
Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as...
Left
@ Left
Definition: tools.h:229
Deutsch
@ Deutsch
Definition: tools.h:217