Jo Engine 2026.11.2
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
168void jo_print(int x, int y, char * str);
169
175# 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)
176
183# 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)
184
186extern unsigned short __jo_vdp2_printf_palette;
187
191static __jo_force_inline void jo_set_printf_color_index(const unsigned char index)
192{
193 __jo_vdp2_printf_palette = (unsigned short)((index & 7) << 12);
194}
195
199void jo_clear_screen(void);
200
205void jo_clear_screen_line(const int y);
206
207#endif // JO_COMPILE_WITH_PRINTF_SUPPORT
208
215void jo_memcpy(void * const restrict dst, const void * const restrict src, unsigned int size);
216
226void jo_dma_copy_async(const void * const src, void * const dest, const unsigned int size);
227
231void jo_dma_wait(void);
232
240void jo_dma_copy(const void * const src, void * const dest, const unsigned int size);
241
247void jo_dma_fill32(void * const dest, const unsigned int value, const unsigned int size);
248
251# define JO_NULL ((void *)0)
252
255# define JO_UNUSED_ARG(ARG) ((void)ARG)
256
262{
263 return c == '\n' || c == '\r' || c == '\t' || c == ' ';
264}
265
270static __jo_force_inline int jo_4_char_hash(const char * const str)
271{
272 return (int)str[0] | (((int)str[1]) << 8) | (((int)str[2]) << 16) | (((int)str[3]) << 24);
273}
274
277typedef struct
278{
279 unsigned char day;
280 unsigned char month;
281 unsigned short year;
282 unsigned char hour;
283 unsigned char week;
284 unsigned char minute;
285 unsigned char second;
287
290typedef enum
291{
299
308
313
318
323int jo_tools_atoi(const char * restrict str);
324
329int jo_strlen(const char * restrict str);
330
336int jo_strcmp(const char * restrict p1, const char * restrict p2);
337
343static __jo_force_inline bool jo_string_equals(const char * restrict p1, const char * restrict p2)
344{
345 return (jo_strcmp(p1, p2) == 0);
346}
347
353bool jo_endwith(const char * restrict str, const char * restrict end);
354
361void jo_memset(const void * const restrict ptr, const int value, unsigned int num);
362
369int jo_memcmp(const void * const p1, const void * const p2, unsigned int num);
370
375static __jo_force_inline unsigned int jo_lz_size(const unsigned char * const src)
376{
377 return (((unsigned int)src[0] << 24) | ((unsigned int)src[1] << 16) | ((unsigned int)src[2] << 8) | src[3]);
378}
379
388unsigned int jo_lz_decompress(const unsigned char * const src, unsigned char * const dst);
389
393{
394 asm("nop");
395}
396
400static __jo_force_inline void jo_spin_wait(int iterations)
401{
402 while (iterations--) jo_yield();
403}
404
413void jo_map_to_vram(unsigned short * restrict data, unsigned short * restrict vram_addr,
414 unsigned short suuj, unsigned short suui, unsigned short palnum, unsigned int mapoff);
415
421static __jo_force_inline void jo_cell_to_vram(unsigned char * restrict data, unsigned char * restrict vram_addr, unsigned int size)
422{
423 while (size-- > 0) *(vram_addr++) = *(data++);
424}
425
426#endif /* !__JO_TOOLS_H__ */
427
428/*
429** END OF FILE
430*/
#define JO_PRINTF_BUF_SIZE
Max character in available in jo_printf().
Definition conf.h:149
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:261
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:270
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:421
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:282
unsigned char minute
Definition tools.h:284
static __jo_force_inline void jo_yield(void)
Little pause possible.
Definition tools.h:392
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:279
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:375
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:281
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:291
@ Japanese
Definition tools.h:297
@ English
Definition tools.h:292
@ Italiano
Definition tools.h:296
@ Deutsch
Definition tools.h:293
@ Espanol
Definition tools.h:295
@ French
Definition tools.h:294
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:343
unsigned char week
Definition tools.h:283
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:400
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:303
@ Left
Definition tools.h:305
@ None
Definition tools.h:304
@ Right
Definition tools.h:306
unsigned char month
Definition tools.h:280
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:285
DateTime struct.
Definition tools.h:278
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition types.h:39