Jo Engine 2026.11.1
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
118int jo_sprintf(char * const str, const char * const format, ...);
119
126int jo_vsprintf(char * const str, const char * format, va_list args);
127
128#ifdef JO_DEBUG
129
130#define jo_printf_debug(X, Y, fmt, args...) \
131 jo_printf(X, Y, "%s(%s)#%d : " fmt , \
132 __FILE__, __FUNCTION__, __LINE__, ## args)
133
134#endif
135
140
141#ifdef JO_COMPILE_WITH_PRINTF_SUPPORT
142
143
149void jo_print(int x, int y, char * str);
150
156# define jo_printf(X, Y, ...) do {jo_sprintf(__jo_sprintf_buf, __VA_ARGS__); jo_print(X, Y, __jo_sprintf_buf); } while(0)
157
164# 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)
165
167extern unsigned short __jo_vdp2_printf_palette;
168
172static __jo_force_inline void jo_set_printf_color_index(const unsigned char index)
173{
174 __jo_vdp2_printf_palette = (unsigned short)((index & 7) << 12);
175}
176
180void jo_clear_screen(void);
181
186void jo_clear_screen_line(const int y);
187
188#endif // JO_COMPILE_WITH_PRINTF_SUPPORT
189
196void jo_memcpy(void * const restrict dst, const void * const restrict src, unsigned int size);
197
207void jo_dma_copy_async(const void * const src, void * const dest, const unsigned int size);
208
212void jo_dma_wait(void);
213
221void jo_dma_copy(const void * const src, void * const dest, const unsigned int size);
222
228void jo_dma_fill32(void * const dest, const unsigned int value, const unsigned int size);
229
235void __jo_dma_read_port32(const volatile void * const port, void * const dest, const unsigned int size);
236
239# define JO_NULL ((void *)0)
240
243# define JO_UNUSED_ARG(ARG) ((void)ARG)
244
250{
251 return c == '\n' || c == '\r' || c == '\t' || c == ' ';
252}
253
258static __jo_force_inline int jo_4_char_hash(const char * const str)
259{
260 return (int)str[0] | (((int)str[1]) << 8) | (((int)str[2]) << 16) | (((int)str[3]) << 24);
261}
262
265typedef struct
266{
267 unsigned char day;
268 unsigned char month;
269 unsigned short year;
270 unsigned char hour;
271 unsigned char week;
272 unsigned char minute;
273 unsigned char second;
275
278typedef enum
279{
287
296
301
306
311int jo_tools_atoi(const char * restrict str);
312
317int jo_strlen(const char * restrict str);
318
324int jo_strcmp(const char * restrict p1, const char * restrict p2);
325
331static __jo_force_inline bool jo_string_equals(const char * restrict p1, const char * restrict p2)
332{
333 return (jo_strcmp(p1, p2) == 0);
334}
335
341bool jo_endwith(const char * restrict str, const char * restrict end);
342
349void jo_memset(const void * const restrict ptr, const int value, unsigned int num);
350
357int jo_memcmp(const void * const p1, const void * const p2, unsigned int num);
358
363static __jo_force_inline unsigned int jo_lz_size(const unsigned char * const src)
364{
365 return (((unsigned int)src[0] << 24) | ((unsigned int)src[1] << 16) | ((unsigned int)src[2] << 8) | src[3]);
366}
367
376unsigned int jo_lz_decompress(const unsigned char * const src, unsigned char * const dst);
377
381{
382 asm("nop");
383}
384
388static __jo_force_inline void jo_spin_wait(int iterations)
389{
390 while (iterations--) jo_yield();
391}
392
401void jo_map_to_vram(unsigned short * restrict data, unsigned short * restrict vram_addr,
402 unsigned short suuj, unsigned short suui, unsigned short palnum, unsigned int mapoff);
403
409static __jo_force_inline void jo_cell_to_vram(unsigned char * restrict data, unsigned char * restrict vram_addr, unsigned int size)
410{
411 while (size-- > 0) *(vram_addr++) = *(data++);
412}
413
414#endif /* !__JO_TOOLS_H__ */
415
416/*
417** END OF FILE
418*/
#define JO_PRINTF_BUF_SIZE
Max character in available in jo_printf().
Definition conf.h:157
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:249
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:258
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:409
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 - and 0,...
unsigned char hour
Definition tools.h:270
unsigned char minute
Definition tools.h:272
static __jo_force_inline void jo_yield(void)
Little pause possible.
Definition tools.h:380
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:267
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:363
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_dma_read_port32(const volatile void *const port, void *const dest, const unsigned int size)
Read a 32 bits I/O port into memory with the DMA controller of the current CPU (Internal usage).
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:269
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:279
@ Japanese
Definition tools.h:285
@ English
Definition tools.h:280
@ Italiano
Definition tools.h:284
@ Deutsch
Definition tools.h:281
@ Espanol
Definition tools.h:283
@ French
Definition tools.h:282
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:331
unsigned char week
Definition tools.h:271
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:388
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:291
@ Left
Definition tools.h:293
@ None
Definition tools.h:292
@ Right
Definition tools.h:294
unsigned char month
Definition tools.h:268
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:273
DateTime struct.
Definition tools.h:266
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition types.h:39