Jo Engine 2026.11.6
Jo Sega Saturn Engine
Loading...
Searching...
No Matches
tools.h
Go to the documentation of this file.
1/*
2** Jo Sega Saturn Engine
3** Copyright (c) 2012-2026, 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*/
34
35#ifndef __JO_TOOLS_H__
36# define __JO_TOOLS_H__
37
38/*
39** INCLUDES
40*/
41#include "math.h"
42#include <stdarg.h>
43
45#define JO_FIXED_TV_WIDTH JO_INT_TO_FIXED(JO_TV_WIDTH)
47#define JO_FIXED_TV_HEIGHT JO_INT_TO_FIXED(JO_TV_HEIGHT)
48
53static __jo_force_inline short jo_swap_endian_short(short value)
54{
55 return ((((value) >> 8) & 0xff) | (((value) & 0xff) << 8));
56}
57
62static __jo_force_inline unsigned short jo_swap_endian_ushort(unsigned short value)
63{
64 return ((((value) >> 8) & 0xff) | (((value) & 0xff) << 8));
65}
66
71static __jo_force_inline unsigned int jo_swap_endian_uint(unsigned int value)
72{
73 return ((((value) & 0xff000000) >> 24) | (((value) & 0x00ff0000) >> 8) | (((value) & 0x0000ff00) << 8) | (((value) & 0x000000ff) << 24));
74}
75
81{
82 return ((((value) & 0xff000000) >> 24) | (((value) & 0x00ff0000) >> 8) | (((value) & 0x0000ff00) << 8) | (((value) & 0x000000ff) << 24));
83}
84
89static __jo_force_inline unsigned short jo_read_big_endian_ushort(const unsigned char * const p)
90{
91 return ((unsigned short)((p[0] << 8) | p[1]));
92}
93
98static __jo_force_inline unsigned int jo_read_big_endian_uint(const unsigned char * const p)
99{
100 return (((unsigned int)p[0] << 24) | ((unsigned int)p[1] << 16) | ((unsigned int)p[2] << 8) | p[3]);
101}
102
109int sprintf(char* str, const char* format, ...);
110
119int jo_sprintf(char * const str, const char * const format, ...);
120
127int jo_vsprintf(char * const str, const char * format, va_list args);
128
136int jo_snprintf(char * const str, const unsigned int size, const char * const format, ...);
137
145int jo_vsnprintf(char * const str, const unsigned int size, const char * format, va_list args);
146
147#ifdef JO_DEBUG
148
149#define jo_printf_debug(X, Y, fmt, args...) \
150 jo_printf(X, Y, "%s(%s)#%d : " fmt , \
151 __FILE__, __FUNCTION__, __LINE__, ## args)
152
153#endif
154
159
160#ifdef JO_COMPILE_WITH_PRINTF_SUPPORT
161
162
170void jo_print(int x, int y, char * str);
171
177# define jo_printf(X, Y, ...) do {jo_snprintf(__jo_sprintf_buf, JO_PRINTF_BUF_SIZE, __VA_ARGS__); jo_print(X, Y, __jo_sprintf_buf); } while(0)
178
185# 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)
186
188extern unsigned short __jo_vdp2_printf_palette;
189
193static __jo_force_inline void jo_set_printf_color_index(const unsigned char index)
194{
195 __jo_vdp2_printf_palette = (unsigned short)((index & 7) << 12);
196}
197
204void jo_set_printf_outline(const bool enabled, const jo_color color);
205
209void jo_clear_screen(void);
210
215void jo_clear_screen_line(const int y);
216
217#endif // JO_COMPILE_WITH_PRINTF_SUPPORT
218
225void jo_memcpy(void * const restrict dst, const void * const restrict src, unsigned int size);
226
236void jo_dma_copy_async(const void * const src, void * const dest, const unsigned int size);
237
241void jo_dma_wait(void);
242
250void jo_dma_copy(const void * const src, void * const dest, const unsigned int size);
251
257void jo_dma_fill32(void * const dest, const unsigned int value, const unsigned int size);
258
261# define JO_NULL ((void *)0)
262
265# define JO_UNUSED_ARG(ARG) ((void)ARG)
266
272{
273 return c == '\n' || c == '\r' || c == '\t' || c == ' ';
274}
275
281static __jo_force_inline int jo_4_char_hash(const char * const str)
282{
283 const unsigned char * const s = (const unsigned char *)str;
284 unsigned int hash;
285
286 // (unsigned: a character over 127 does not fill the bytes above it)
287 hash = s[0];
288 if (s[0] != 0)
289 {
290 hash |= (unsigned int)s[1] << 8;
291 if (s[1] != 0)
292 {
293 hash |= (unsigned int)s[2] << 16;
294 if (s[2] != 0)
295 hash |= (unsigned int)s[3] << 24;
296 }
297 }
298 return ((int)hash);
299}
300
303typedef struct
304{
305 unsigned char day;
306 unsigned char month;
307 unsigned short year;
308 unsigned char hour;
309 unsigned char week;
310 unsigned char minute;
311 unsigned char second;
313
316typedef enum
317{
325
334
339
344
349int jo_tools_atoi(const char * restrict str);
350
355int jo_strlen(const char * restrict str);
356
362int jo_strcmp(const char * restrict p1, const char * restrict p2);
363
369static __jo_force_inline bool jo_string_equals(const char * restrict p1, const char * restrict p2)
370{
371 return (jo_strcmp(p1, p2) == 0);
372}
373
379bool jo_endwith(const char * restrict str, const char * restrict end);
380
387void jo_memset(const void * const restrict ptr, const int value, unsigned int num);
388
395int jo_memcmp(const void * const p1, const void * const p2, unsigned int num);
396
401static __jo_force_inline unsigned int jo_lz_size(const unsigned char * const src)
402{
403 return (((unsigned int)src[0] << 24) | ((unsigned int)src[1] << 16) | ((unsigned int)src[2] << 8) | src[3]);
404}
405
414unsigned int jo_lz_decompress(const unsigned char * const src, unsigned char * const dst);
415
419{
420 asm("nop");
421}
422
426static __jo_force_inline void jo_spin_wait(int iterations)
427{
428 while (iterations--) jo_yield();
429}
430
439void jo_map_to_vram(unsigned short * restrict data, unsigned short * restrict vram_addr,
440 unsigned short suuj, unsigned short suui, unsigned short palnum, unsigned int mapoff);
441
447static __jo_force_inline void jo_cell_to_vram(unsigned char * restrict data, unsigned char * restrict vram_addr, unsigned int size)
448{
449 while (size-- > 0) *(vram_addr++) = *(data++);
450}
451
452#endif /* !__JO_TOOLS_H__ */
453
454/*
455** END OF FILE
456*/
#define JO_PRINTF_BUF_SIZE
Max character in available in jo_printf().
Definition conf.h:146
void * data
Definition entity.h:19
Jo Engine Math tools.
static __jo_force_inline bool jo_tools_is_whitespace(const char c)
Check if the character is a whitespace.
Definition tools.h:271
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:281
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:447
int jo_sprintf(char *const str, const char *const format,...)
A small sprintf of the engine (jo_printf()...): d i u x X c s %%, the flags - 0 + and space,...
unsigned char hour
Definition tools.h:308
unsigned char minute
Definition tools.h:310
static __jo_force_inline void jo_yield(void)
Little pause possible.
Definition tools.h:418
unsigned int jo_lz_decompress(const unsigned char *const src, unsigned char *const dst)
Decompress data compressed by Compiler/COMMON/jo_lz.py (LZSS: a byte of 8 flags, 1 a byte as it is,...
int sprintf(char *str, const char *format,...)
sprintf prototypes
static __jo_force_inline unsigned int jo_read_big_endian_uint(const unsigned char *const p)
Read a big endian unsigned int (32 bits) from any address (the bytes of a file, aligned or not).
Definition tools.h:98
static __jo_force_inline int jo_swap_endian_int(int value)
Swap int endian.
Definition tools.h:80
void jo_dma_copy_async(const void *const src, void *const dest, const unsigned int size)
Start a copy with the DMA controller of the current CPU (the function doesn't wait for the end of the...
jo_language jo_get_current_language(void)
Get Sega saturn current language.
bool jo_endwith(const char *restrict str, const char *restrict end)
determine if str end with a specific string
unsigned char day
Definition tools.h:305
int jo_strlen(const char *restrict str)
strlen implementation
static __jo_force_inline unsigned int jo_lz_size(const unsigned char *const src)
Size of the data compressed by Compiler/COMMON/jo_lz.py (the 4 bytes at its start,...
Definition tools.h:401
static __jo_force_inline unsigned short jo_swap_endian_ushort(unsigned short value)
Swap Unsigned short endian.
Definition tools.h:62
static __jo_force_inline unsigned short jo_read_big_endian_ushort(const unsigned char *const p)
Read a big endian unsigned short (16 bits) from any address (the bytes of a file, aligned or not).
Definition tools.h:89
void jo_getdate(jo_datetime *now)
get current date and time
static __jo_force_inline short jo_swap_endian_short(short value)
Swap Short endian.
Definition tools.h:53
unsigned short year
Definition tools.h:307
static __jo_force_inline unsigned int jo_swap_endian_uint(unsigned int value)
Swap unsigned int endian.
Definition tools.h:71
void jo_dma_wait(void)
Wait for the end of the copy started by jo_dma_copy_async() on the current CPU.
void jo_dma_fill32(void *const dest, const unsigned int value, const unsigned int size)
Fill memory with a 32 bits value using the DMA controller of the current CPU (waits for the end).
int jo_memcmp(const void *const p1, const void *const p2, unsigned int num)
memcmp implementation
jo_language
Language type.
Definition tools.h:317
@ Japanese
Definition tools.h:323
@ English
Definition tools.h:318
@ Italiano
Definition tools.h:322
@ Deutsch
Definition tools.h:319
@ Espanol
Definition tools.h:321
@ French
Definition tools.h:320
int jo_snprintf(char *const str, const unsigned int size, const char *const format,...)
jo_sprintf() with the size of the string: size - 1 characters at most, then the null character
char __jo_sprintf_buf[JO_PRINTF_BUF_SIZE]
Internal sprintf buffer.
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...
static __jo_force_inline bool jo_string_equals(const char *restrict p1, const char *restrict p2)
Determine if two string equals.
Definition tools.h:369
unsigned char week
Definition tools.h:309
int jo_strcmp(const char *restrict p1, const char *restrict p2)
strcmp implementation
void jo_memcpy(void *const restrict dst, const void *const restrict src, unsigned int size)
Copies size bytes from the object pointed to by src to the object pointed to by dst.
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:426
int jo_vsnprintf(char *const str, const unsigned int size, const char *format, va_list args)
jo_snprintf() with a list of arguments
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_vsprintf(char *const str, const char *format, va_list args)
jo_sprintf() with a list of arguments
jo_horizontal_move
Horizontal move.
Definition tools.h:329
@ Left
Definition tools.h:331
@ None
Definition tools.h:330
@ Right
Definition tools.h:332
unsigned char month
Definition tools.h:306
int jo_tools_atoi(const char *restrict str)
atoi implementation
void jo_dma_copy(const void *const src, void *const dest, const unsigned int size)
Copy with the DMA controller of the current CPU and wait for the end of the copy.
unsigned char second
Definition tools.h:311
DateTime struct.
Definition tools.h:304
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition types.h:39
unsigned short jo_color
15 bits color type
Definition types.h:53