Jo Engine  2020.06.22
Jo Sega Saturn Engine
tools.h
Go to the documentation of this file.
1 /*
2 ** Jo Sega Saturn Engine
3 ** Copyright (c) 2012-2020, 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 
47 static __jo_force_inline short jo_swap_endian_short(short value)
48 {
49  return ((((value) >> 8) & 0xff) | (((value) & 0xff) << 8));
50 }
51 
56 static __jo_force_inline unsigned short jo_swap_endian_ushort(unsigned short value)
57 {
58  return ((((value) >> 8) & 0xff) | (((value) & 0xff) << 8));
59 }
60 
65 static __jo_force_inline unsigned int jo_swap_endian_uint(unsigned int value)
66 {
67  return ((((value) & 0xff000000) >> 24) | (((value) & 0x00ff0000) >> 8) | (((value) & 0x0000ff00) << 8) | (((value) & 0x000000ff) << 24));
68 }
69 
75 {
76  return ((((value) & 0xff000000) >> 24) | (((value) & 0x00ff0000) >> 8) | (((value) & 0x0000ff00) << 8) | (((value) & 0x000000ff) << 24));
77 }
78 
85 int sprintf(char* str, const char* format, ...);
86 
87 #ifdef JO_DEBUG
88 
89 #define jo_printf_debug(X, Y, fmt, args...) \
90  jo_printf(X, Y, "%s(%s)#%d : " fmt , \
91  __FILE__, __FUNCTION__, __LINE__, ## args)
92 
93 #endif
94 
99 
100 #ifdef JO_COMPILE_WITH_PRINTF_SUPPORT
101 
102 #if !JO_COMPILE_USING_SGL
103 extern unsigned char __jo_printf_current_palette_index;
104 #endif
105 
111 void jo_print(int x, int y, char * str);
112 
118 # define jo_printf(X, Y, ...) do {sprintf(__jo_sprintf_buf, __VA_ARGS__); jo_print(X, Y, __jo_sprintf_buf); } while(0)
119 
126 # 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)
127 
131 static __jo_force_inline void jo_set_printf_color_index(const unsigned char index)
132 {
133 #if JO_COMPILE_USING_SGL
134  slCurColor(index);
135 #else
136  __jo_printf_current_palette_index = index;
137 #endif
138 }
139 
143 void jo_clear_screen(void);
144 
149 void jo_clear_screen_line(const int y);
150 
151 #endif // JO_COMPILE_WITH_PRINTF_SUPPORT
152 
159 static __jo_force_inline void jo_dma_copy(void *src, void *dest, unsigned int size)
160 {
161 #if JO_COMPILE_USING_SGL
162  slDMACopy(src, dest, size);
163 #else
164  slDMACopy(src, dest, size); // TODO
165 #endif
166 }
167 
170 # define JO_NULL ((void *)0)
171 
174 # define JO_UNUSED_ARG(ARG) ((void)ARG)
175 
180 static __jo_force_inline bool jo_tools_is_whitespace(const char c)
181 {
182  return c == '\n' || c == '\r' || c == '\t' || c == ' ';
183 }
184 
189 static __jo_force_inline int jo_4_char_hash(const char * const str)
190 {
191  return (int)str[0] | (((int)str[1]) << 8) | (((int)str[2]) << 16) | (((int)str[3]) << 24);
192 }
193 
196 typedef struct
197 {
198  unsigned char day;
199  unsigned char month;
200  unsigned short year;
201  unsigned char hour;
202  unsigned char week;
203  unsigned char minute;
204  unsigned char second;
205 } jo_datetime;
206 
209 typedef enum
210 {
211  English = 0,
212  Deutsch = 1,
213  French = 2,
214  Espanol = 3,
215  Italiano = 4,
216  Japanese = 5
218 
221 typedef enum
222 {
225  Right
227 
232 
237 
242 int jo_tools_atoi(const char * restrict str);
243 
248 int jo_strlen(const char * restrict str);
249 
255 int jo_strcmp(const char * restrict p1, const char * restrict p2);
256 
262 static __jo_force_inline bool jo_string_equals(const char * restrict p1, const char * restrict p2)
263 {
264  return (jo_strcmp(p1, p2) == 0);
265 }
266 
272 bool jo_endwith(const char * restrict str, const char * restrict end);
273 
280 void jo_memset(const void * const restrict ptr, const int value, unsigned int num);
281 
284 static __jo_force_inline void jo_yield(void)
285 {
286  asm("nop");
287 }
288 
292 static __jo_force_inline void jo_spin_wait(int iterations)
293 {
294  while (iterations--) jo_yield();
295 }
296 
305 void jo_map_to_vram(unsigned short * restrict data, unsigned short * restrict vram_addr,
306  unsigned short suuj, unsigned short suui, unsigned short palnum, unsigned int mapoff);
307 
313 static __jo_force_inline void jo_cell_to_vram(unsigned char * restrict data, unsigned char * restrict vram_addr, unsigned int size)
314 {
315  while (size-- > 0) *(vram_addr++) = *(data++);
316 }
317 
318 #endif /* !__JO_TOOLS_H__ */
319 
320 /*
321 ** END OF FILE
322 */
jo_datetime::hour
unsigned char hour
Definition: tools.h:201
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:199
jo_datetime
DateTime struct.
Definition: tools.h:197
French
@ French
Definition: tools.h:213
Right
@ Right
Definition: tools.h:225
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:56
__jo_force_inline
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition: conf.h:154
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:262
jo_swap_endian_short
static __jo_force_inline short jo_swap_endian_short(short value)
Swap Short endian.
Definition: tools.h:47
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:222
__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:180
jo_swap_endian_int
static __jo_force_inline int jo_swap_endian_int(int value)
Swap int endian.
Definition: tools.h:74
English
@ English
Definition: tools.h:211
jo_swap_endian_uint
static __jo_force_inline unsigned int jo_swap_endian_uint(unsigned int value)
Swap unsigned int endian.
Definition: tools.h:65
JO_PRINTF_BUF_SIZE
#define JO_PRINTF_BUF_SIZE
Max character in available in jo_printf()
Definition: conf.h:147
jo_datetime::week
unsigned char week
Definition: tools.h:202
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:313
jo_datetime::year
unsigned short year
Definition: tools.h:200
Italiano
@ Italiano
Definition: tools.h:215
jo_language
jo_language
Language type.
Definition: tools.h:210
Japanese
@ Japanese
Definition: tools.h:216
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:204
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:292
jo_dma_copy
static __jo_force_inline void jo_dma_copy(void *src, void *dest, unsigned int size)
DMA copy.
Definition: tools.h:159
jo_yield
static __jo_force_inline void jo_yield(void)
Little pause possible.
Definition: tools.h:284
jo_datetime::minute
unsigned char minute
Definition: tools.h:203
None
@ None
Definition: tools.h:223
jo_datetime::day
unsigned char day
Definition: tools.h:198
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:189
Espanol
@ Espanol
Definition: tools.h:214
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:224
Deutsch
@ Deutsch
Definition: tools.h:212