2 Skelton for retropc emulator
4 Author : Takeda.Toshiya
13 // move shared codes to DLL???
15 // #define USE_SHARED_DLL
18 // use zlib to decompress gzip file???
20 #if defined(_MSC_VER) && (_MSC_VER >= 1500)
30 // check environemnt/language
33 // Microsoft Visual C++
35 // variable scope of 'for' loop for Microsoft Visual C++ 6.0
36 #define for if(0);else for
39 // Microsoft Visual C++ 6.0 or later
40 #define SUPPORT_TCHAR_TYPE
43 // Microsoft Visual C++ 8.0 (2005) or later
44 #define SUPPORT_SECURE_FUNCTIONS
45 #pragma warning( disable : 4819 )
46 //#pragma warning( disable : 4995 )
47 #pragma warning( disable : 4996 )
50 // Microsoft Visual C++ 12.0 (2013) or later
51 #define SUPPORT_CPLUSPLUS_11
53 #define CSP_OS_WINDOWS
55 // Win32, but not Microsoft Visual C++
56 #define SUPPORT_TCHAR_TYPE
57 // #define SUPPORT_SECURE_FUNCTIONS
61 #if defined(Q_OS_CYGWIN)
62 #define CSP_OS_GCC_CYGWIN
63 #define CSP_OS_WINDOWS
64 #elif defined(Q_OS_WIN) || defined(__WIN32) || defined(__WIN64)
65 #define CSP_OS_GCC_WINDOWS
66 #define CSP_OS_WINDOWS
68 #define DLL_PREFIX __declspec(dllexport)
69 #define DLL_PREFIX_I __declspec(dllimport)
72 #define CSP_OS_GCC_GENERIC
73 #define CSP_OS_GENERIC
77 #if defined(__clang__)
78 #define __CSP_COMPILER_CLANG
80 #define __CSP_COMPILER_GCC
82 #define SUPPORT_CPLUSPLUS_11
88 #ifndef SUPPORT_CPLUSPLUS_11
89 #if defined(__cplusplus) && (__cplusplus > 199711L)
90 #define SUPPORT_CPLUSPLUS_11
93 #ifndef SUPPORT_TCHAR_TYPE
94 // secure functions need tchar type
95 #undef SUPPORT_SECURE_FUNCTIONS
104 // include common header files
105 #ifdef SUPPORT_TCHAR_TYPE
108 #ifdef SUPPORT_CPLUSPLUS_11
115 #include <typeinfo.h>
122 // include environment/language dependent header files
125 #include <windowsx.h>
127 #include <mmsystem.h>
138 #if defined(__MINGW32__) || (__MINGW64__)
143 #include <QtCore/QString>
144 #include <QtCore/QFile>
146 #include <sys/param.h>
149 #define _MAX_PATH 2048
153 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
154 #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
155 #if __BYTE_ORDER == __LITTLE_ENDIAN
156 #define __LITTLE_ENDIAN__
157 #elif __BYTE_ORDER == __BIG_ENDIAN
158 #define __BIG_ENDIAN__
160 #elif defined(SDL_BYTEORDER) && (defined(SDL_LIL_ENDIAN) || defined(SDL_BIG_ENDIAN))
161 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
162 #define __LITTLE_ENDIAN__
163 #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
164 #define __BIG_ENDIAN__
166 #elif defined(WORDS_LITTLEENDIAN)
167 #define __LITTLE_ENDIAN__
168 #elif defined(WORDS_BIGENDIAN)
169 #define __BIG_ENDIAN__
172 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
173 // may be Microsoft Visual C++
174 #define __LITTLE_ENDIAN__
178 #ifndef SUPPORT_TCHAR_TYPE
184 #ifndef SUPPORT_CPLUSPLUS_11
186 typedef signed char int8_t;
189 typedef signed short int16_t;
192 typedef signed int int32_t;
195 typedef signed long long int64_t;
198 typedef unsigned char uint8_t;
201 typedef unsigned short uint16_t;
204 typedef unsigned int uint32_t;
207 typedef unsigned long long uint64_t;
213 typedef _TCHAR* LPTSTR;
216 typedef const _TCHAR* LPCTSTR;
228 typedef uint8_t BYTE;
231 typedef uint16_t WORD;
234 typedef uint32_t DWORD;
237 typedef uint64_t QWORD;
243 typedef int16_t INT16;
246 typedef int32_t INT32;
249 typedef int64_t INT64;
252 typedef uint8_t UINT8;
255 typedef uint16_t UINT16;
258 typedef uint32_t UINT32;
261 typedef uint64_t UINT64;
267 typedef unsigned int UINT;
273 #ifdef __BIG_ENDIAN__
274 uint8_t h3, h2, h, l;
276 uint8_t l, h, h2, h3;
280 #ifdef __BIG_ENDIAN__
287 #ifdef __BIG_ENDIAN__
294 #ifdef __BIG_ENDIAN__
302 inline void read_2bytes_le_from(uint8_t *t)
304 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
306 inline void write_2bytes_le_to(uint8_t *t)
308 t[0] = b.l; t[1] = b.h;
310 inline void read_2bytes_be_from(uint8_t *t)
312 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
314 inline void write_2bytes_be_to(uint8_t *t)
316 t[0] = b.h; t[1] = b.l;
318 inline void read_4bytes_le_from(uint8_t *t)
320 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
322 inline void write_4bytes_le_to(uint8_t *t)
324 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
326 inline void read_4bytes_be_from(uint8_t *t)
328 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
330 inline void write_4bytes_be_to(uint8_t *t)
332 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
336 uint32_t DLL_PREFIX EndianToLittle_DWORD(uint32_t x);
337 uint16_t DLL_PREFIX EndianToLittle_WORD(uint16_t x);
343 int DLL_PREFIX max(int a, int b);
344 unsigned int DLL_PREFIX max(unsigned int a, unsigned int b);
345 int DLL_PREFIX min(int a, int b);
346 unsigned int DLL_PREFIX min(unsigned int a, unsigned int b);
350 #if defined(__GNUC__) || defined(__CYGWIN__) || defined(Q_OS_CYGWIN)
351 #define stricmp(a,b) strcasecmp(a,b)
352 #define strnicmp(a,b,n) strncasecmp(a,b,n)
355 #ifndef SUPPORT_TCHAR_TYPE
357 #define _fgetts fgets
360 #define _ftprintf printf
363 #define _tfopen fopen
366 #define _tcscmp strcmp
369 #define _tcscpy strcpy
372 #define _tcsicmp stricmp
375 #define _tcslen strlen
378 #define _tcscat strcat
381 #define _tcsncat strncat
384 #define _tcsncpy strncpy
387 #define _tcsncicmp strnicmp
390 #define _tcschr strchr
393 #define _tcsrchr strrchr
396 #define _tcsstr strstr
399 #define _tcstok strtok
405 #define _tcstol strtol
408 #define _tcstoul strtoul
411 #define _stprintf sprintf
414 #define _vstprintf vsprintf
417 #define _taccess access
420 #define _tremove remove
423 #define _trename rename
427 #define _TEXT(x) __T(x)
430 #ifndef SUPPORT_SECURE_FUNCTIONS
434 // errno_t DLL_PREFIX my_tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode);
435 errno_t DLL_PREFIX my_tcscat_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
436 errno_t DLL_PREFIX my_strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
437 errno_t DLL_PREFIX my_tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
438 errno_t DLL_PREFIX my_strncpy_s(char *strDestination, size_t numberOfElements, const char *strSource, size_t count);
439 errno_t DLL_PREFIX my_tcsncpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource, size_t count);
440 char * DLL_PREFIX my_strtok_s(char *strToken, const char *strDelimit, char **context);
441 _TCHAR *DLL_PREFIX my_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
442 #define my_fprintf_s fprintf
443 #define my_ftprintf_s fprintf
444 int DLL_PREFIX my_sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
445 int DLL_PREFIX my_swprintf_s(wchar_t *buffer, size_t sizeOfBuffer, const wchar_t *format, ...);
446 int DLL_PREFIX my_stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
447 int DLL_PREFIX my_vsprintf_s(char *buffer, size_t numberOfElements, const char *format, va_list argptr);
448 int DLL_PREFIX my_vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
450 // #define my_tfopen_s _tfopen_s
451 #define my_tcscat_s _tcscat_s
452 #define my_strcpy_s strcpy_s
453 #define my_tcscpy_s _tcscpy_s
454 #define my_strncpy_s strncpy_s
455 #define my_tcsncpy_s _tcsncpy_s
456 #define my_strtok_s strtok_s
457 #define my_tcstok_s _tcstok_s
458 #define my_fprintf_s fprintf_s
459 #define my_ftprintf_s _ftprintf_s
460 #define my_sprintf_s sprintf_s
461 #define my_swprintf_s swprintf_s
462 #define my_stprintf_s _stprintf_s
463 #define my_vsprintf_s vsprintf_s
464 #define my_vstprintf_s _vstprintf_s
469 void *DLL_PREFIX my_memcpy(void *dst, void *src, size_t len);
471 #define my_memcpy memcpy
475 #if defined(__clang__)
476 #define __DECL_VECTORIZED_LOOP _Pragma("clang loop vectorize(enable) interleave(enable)")
477 #elif defined(__GNUC__)
478 #define __DECL_VECTORIZED_LOOP _Pragma("GCC ivdep")
480 #define __DECL_VECTORIZED_LOOP
483 // C99 math functions
485 #define my_isfinite _finite
486 #define my_log2(v) (log((double)(v)) / log(2.0))
488 #define my_isfinite isfinite
494 BOOL MyWritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName);
495 DWORD MyGetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName);
496 UINT MyGetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName);
497 // used only in winmain and win32 osd class
498 // #define ZeroMemory(p,s) memset(p,0x00,s)
499 // #define CopyMemory(t,f,s) memcpy(t,f,s)
501 #define MyWritePrivateProfileString WritePrivateProfileString
502 #define MyGetPrivateProfileString GetPrivateProfileString
503 #define MyGetPrivateProfileInt GetPrivateProfileInt
507 #if !defined(_RGB555) && !defined(_RGB565) && !defined(_RGB888)
511 #if defined(_RGB555) || defined(_RGB565)
512 typedef uint16_t scrntype_t;
513 scrntype_t DLL_PREFIX RGB_COLOR(uint32_t r, uint32_t g, uint32_t b);
514 scrntype_t DLL_PREFIX RGBA_COLOR(uint32_t r, uint32_t g, uint32_t b, uint32_t a);
515 uint8_t DLL_PREFIX R_OF_COLOR(scrntype_t c);
516 uint8_t DLL_PREFIX G_OF_COLOR(scrntype_t c);
517 uint8_t DLL_PREFIX B_OF_COLOR(scrntype_t c);
518 uint8_t DLL_PREFIX A_OF_COLOR(scrntype_t c);
519 #elif defined(_RGB888)
520 typedef uint32_t scrntype_t;
521 #define RGB_COLOR(r, g, b) (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0))
522 #define RGBA_COLOR(r, g, b, a) (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0) | ((uint32_t)(a) << 24))
523 #define R_OF_COLOR(c) (((c) >> 16) & 0xff)
524 #define G_OF_COLOR(c) (((c) >> 8) & 0xff)
525 #define B_OF_COLOR(c) (((c) ) & 0xff)
526 #define A_OF_COLOR(c) (((c) >> 24) & 0xff)
539 wav_chunk_t riff_chunk;
541 wav_chunk_t fmt_chunk;
544 uint32_t sample_rate;
547 uint16_t sample_bits;
552 const _TCHAR *DLL_PREFIX get_application_path();
553 const _TCHAR *DLL_PREFIX create_local_path(const _TCHAR *format, ...);
554 void DLL_PREFIX create_local_path(_TCHAR *file_path, int length, const _TCHAR *format, ...);
555 const _TCHAR *DLL_PREFIX create_date_file_path(const _TCHAR *extension);
556 bool DLL_PREFIX is_absolute_path(const _TCHAR *file_path);
557 const _TCHAR *DLL_PREFIX create_absolute_path(const _TCHAR *file_name);
558 void DLL_PREFIX create_absolute_path(_TCHAR *file_path, int length, const _TCHAR *file_name);
559 void DLL_PREFIX create_date_file_path(_TCHAR *file_path, int length, const _TCHAR *extension);
560 bool DLL_PREFIX check_file_extension(const _TCHAR *file_path, const _TCHAR *ext);
561 const _TCHAR *DLL_PREFIX get_file_path_without_extensiton(const _TCHAR *file_path);
562 void DLL_PREFIX get_long_full_path_name(const _TCHAR* src, _TCHAR* dst, size_t dst_len);
563 const _TCHAR* DLL_PREFIX get_parent_dir(const _TCHAR* file);
566 const _TCHAR *DLL_PREFIX create_string(const _TCHAR* format, ...);
567 const wchar_t *DLL_PREFIX char_to_wchar(const char *cs);
568 const char *DLL_PREFIX wchar_to_char(const wchar_t *ws);
569 const _TCHAR *DLL_PREFIX char_to_tchar(const char *cs);
570 const char *DLL_PREFIX tchar_to_char(const _TCHAR *ts);
571 const _TCHAR *DLL_PREFIX wchar_to_tchar(const wchar_t *ws);
572 const wchar_t *DLL_PREFIX tchar_to_wchar(const _TCHAR *ts);
576 int32_t DLL_PREFIX muldiv_s32(int32_t nNumber, int32_t nNumerator, int32_t nDenominator);
577 uint32_t DLL_PREFIX muldiv_u32(uint32_t nNumber, uint32_t nNumerator, uint32_t nDenominator);
579 uint32_t DLL_PREFIX get_crc32(uint8_t data[], int size);
580 uint32_t DLL_PREFIX calc_crc32(uint32_t seed, uint8_t data[], int size);
581 uint16_t DLL_PREFIX jis_to_sjis(uint16_t jis);
583 int DLL_PREFIX decibel_to_volume(int decibel);
584 int32_t DLL_PREFIX apply_volume(int32_t sample, int volume);
586 #define array_length(array) (sizeof(array) / sizeof(array[0]))
588 #define FROM_BCD(v) (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
589 #define TO_BCD(v) ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
590 #define TO_BCD_LO(v) ((v) % 10)
591 #define TO_BCD_HI(v) (int)(((v) % 100) / 10)
594 #define LEAP_YEAR(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
596 #define dll_cur_time_t DLL_PREFIX_I struct cur_time_s
598 typedef DLL_PREFIX struct cur_time_s {
599 int year, month, day, day_of_week, hour, minute, second;
607 void update_day_of_week();
608 void save_state(void *f);
609 bool load_state(void *f);
610 void save_state_helper(void *f, uint32_t *sumseed, bool *__stat);
611 bool load_state_helper(void *f, uint32_t *sumseed, bool *__stat);
614 void DLL_PREFIX get_host_time(cur_time_t* cur_time);
617 typedef struct symbol_s {
620 struct symbol_s *next_symbol;
623 const _TCHAR* DLL_PREFIX get_symbol(symbol_t *first_symbol, uint32_t addr);
624 const _TCHAR* DLL_PREFIX get_value_or_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);
625 const _TCHAR* DLL_PREFIX get_value_and_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);