Jo Engine  9
Jo Sega Saturn Engine
tools.h
Go to the documentation of this file.
1 /*
2 ** Jo Sega Saturn Engine
3 ** Copyright (c) 2012-2017, 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 unsigned short jo_swap_endian_ushort(unsigned short value)
48 {
49  return ((((value) >> 8) & 0xff) | (((value) & 0xff) << 8));
50 }
51 
56 static __jo_force_inline unsigned int jo_swap_endian_uint(unsigned int value)
57 {
58  return ((((value) & 0xff000000) >> 24) | (((value) & 0x00ff0000) >> 8) | (((value) & 0x0000ff00) << 8) | (((value) & 0x000000ff) << 24));
59 }
60 
66 {
67  return ((((value) & 0xff000000) >> 24) | (((value) & 0x00ff0000) >> 8) | (((value) & 0x0000ff00) << 8) | (((value) & 0x000000ff) << 24));
68 }
69 
70 
77 int sprintf(char* str, const char* format, ...);
78 
79 #ifdef JO_DEBUG
80 
81 #define jo_printf_debug(X, Y, fmt, args...) \
82  jo_printf(X, Y, "%s(%s)#%d : " fmt , \
83  __FILE__, __FUNCTION__, __LINE__, ## args)
84 
85 #endif
86 
91 #if !JO_COMPILE_USING_SGL
92 extern unsigned char __jo_printf_current_palette_index;
93 #endif
94 
95 void jo_print(int x, int y, char * str);
96 
102 # define jo_printf(X, Y, ...) do {sprintf(__jo_sprintf_buf, __VA_ARGS__); jo_print(X, Y, __jo_sprintf_buf); } while(0)
103 
110 # 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)
111 
116 static __jo_force_inline void jo_set_printf_palette_color(const unsigned char index, const unsigned short color)
117 {
118  *(((unsigned short *)JO_VDP2_CRAM) + 1 + JO_MULT_BY_256(index)) = color;
119 }
120 
124 static __jo_force_inline void jo_set_printf_color_index(const unsigned char index)
125 {
126 #if JO_COMPILE_USING_SGL
127  slCurColor(index);
128 #else
130 #endif
131 }
132 
139 static __jo_force_inline void jo_dma_copy(void *src, void *dest, unsigned int size)
140 {
141 #if JO_COMPILE_USING_SGL
142  slDMACopy(src, dest, size);
143 #else
144  slDMACopy(src, dest, size); // TODO
145 #endif
146 }
147 
150 # define JO_NULL ((void *)0)
151 
154 # define JO_UNUSED_ARG(ARG) ((void)ARG)
155 
160 static __jo_force_inline bool jo_tools_is_whitespace(const char c)
161 {
162  return c == '\n' || c == '\r' || c == '\t' || c == ' ';
163 }
164 
169 static __jo_force_inline int jo_4_char_hash(const char * const str)
170 {
171  return (int)str[0] | (((int)str[1]) << 8) | (((int)str[2]) << 16) | (((int)str[3]) << 24);
172 }
173 
176 typedef struct
177 {
178  unsigned char day;
179  unsigned char month;
180  unsigned short year;
181  unsigned char hour;
182  unsigned char week;
183  unsigned char minute;
184  unsigned char second;
185 } jo_datetime;
186 
189 typedef enum
190 {
191  English = 0,
192  Deutsch = 1,
193  French = 2,
194  Espanol = 3,
195  Italiano = 4,
197 } jo_language;
198 
201 typedef enum
202 {
207 
212 
216 void jo_clear_screen(void);
217 
221 void jo_getdate(jo_datetime *now);
222 
227 int jo_tools_atoi(const char * restrict str);
228 
233 int jo_strlen(const char * restrict str);
234 
240 bool jo_endwith(const char * restrict str, const char * restrict end);
241 
248 void jo_memset(const void * const restrict ptr, const int value, unsigned int num);
249 
252 static __jo_force_inline void jo_yield(void)
253 {
254  asm("nop");
255 }
256 
260 static __jo_force_inline void jo_spin_wait(int iterations)
261 {
262  while (iterations--) jo_yield();
263 }
264 
273 void jo_map_to_vram(unsigned short * restrict data, unsigned short * restrict vram_addr,
274  unsigned short suuj, unsigned short suui, unsigned short palnum, unsigned int mapoff);
275 
281 static __jo_force_inline void jo_palette_to_cram(unsigned short * restrict data, unsigned short * restrict cram_addr, unsigned int size)
282 {
283  while (size-- > 0) *(cram_addr++) = *(data++);
284 }
285 
291 static __jo_force_inline void jo_cell_to_vram(unsigned char * restrict data, unsigned char * restrict vram_addr, unsigned int size)
292 {
293  while (size-- > 0) *(vram_addr++) = *(data++);
294 }
295 
296 #endif /* !__JO_TOOLS_H__ */
297 
298 /*
299 ** END OF FILE
300 */
static __jo_force_inline void jo_set_printf_palette_color(const unsigned char index, const unsigned short color)
Set foreground palette color for the specific index.
Definition: tools.h:116
Definition: tools.h:192
Jo Engine Math tools.
unsigned short year
Definition: tools.h:180
unsigned char minute
Definition: tools.h:183
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.
int jo_strlen(const char *restrict str)
strlen implementation
#define JO_MULT_BY_256(X)
Multiply a variable by 256.
Definition: math.h:105
unsigned char hour
Definition: tools.h:181
Definition: tools.h:193
int sprintf(char *str, const char *format,...)
sprintf prototypes
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition: conf.h:154
jo_horizontal_move
Horizontal move.
Definition: tools.h:201
unsigned char day
Definition: tools.h:178
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:169
int jo_tools_atoi(const char *restrict str)
atoi implementation
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...
Definition: tools.h:194
void slCurColor(Uint16)
static __jo_force_inline void jo_palette_to_cram(unsigned short *restrict data, unsigned short *restrict cram_addr, unsigned int size)
Copies the image palette data to CRAM.
Definition: tools.h:281
bool jo_endwith(const char *restrict str, const char *restrict end)
determine if str end with a specific string
static __jo_force_inline void jo_set_printf_color_index(const unsigned char index)
Set printf foreground color index.
Definition: tools.h:124
jo_language
Language type.
Definition: tools.h:189
static __jo_force_inline void jo_yield(void)
Little pause possible.
Definition: tools.h:252
unsigned char week
Definition: tools.h:182
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:291
DateTime struct.
Definition: tools.h:176
void jo_getdate(jo_datetime *now)
get current date and time
Definition: tools.h:196
Definition: tools.h:191
Definition: tools.h:195
void jo_clear_screen(void)
Clear screen (usefull for printf)
static __jo_force_inline void jo_dma_copy(void *src, void *dest, unsigned int size)
DMA copy.
Definition: tools.h:139
Definition: tools.h:203
char __jo_sprintf_buf[JO_PRINTF_BUF_SIZE]
Internal sprintf buffer.
void slDMACopy(void *, void *, Uint32)
unsigned char __jo_printf_current_palette_index
void jo_print(int x, int y, char *str)
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:260
Definition: tools.h:204
#define JO_PRINTF_BUF_SIZE
Max character in available in jo_printf()
Definition: conf.h:147
unsigned char month
Definition: tools.h:179
Definition: tools.h:205
#define JO_VDP2_CRAM
VDP2 Color RAM (4 kilobytes)
Definition: sega_saturn.h:149
static __jo_force_inline unsigned int jo_swap_endian_uint(unsigned int value)
Swap unsigned int endian.
Definition: tools.h:56
static __jo_force_inline bool jo_tools_is_whitespace(const char c)
Check if the character is a whitespace.
Definition: tools.h:160
static __jo_force_inline int jo_swap_endian_int(int value)
Swap int endian.
Definition: tools.h:65
static __jo_force_inline unsigned short jo_swap_endian_ushort(unsigned short value)
Swap Unsigned short endian.
Definition: tools.h:47
unsigned char second
Definition: tools.h:184
jo_language jo_get_current_language(void)
Get Sega saturn current language.