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)
27 // check environemnt/language
30 // Microsoft Visual C++
32 // variable scope of 'for' loop for Microsoft Visual C++ 6.0
33 #define for if(0);else for
36 // Microsoft Visual C++ 6.0 or later
37 #define SUPPORT_TCHAR_TYPE
40 // Microsoft Visual C++ 8.0 (2005) or later
41 #define SUPPORT_SECURE_FUNCTIONS
42 #pragma warning( disable : 4819 )
43 //#pragma warning( disable : 4995 )
44 #pragma warning( disable : 4996 )
47 // Microsoft Visual C++ 12.0 (2013) or later
48 #define SUPPORT_CPLUSPLUS_11
50 #define CSP_OS_WINDOWS
52 // Win32, but not Microsoft Visual C++
53 #define SUPPORT_TCHAR_TYPE
54 // #define SUPPORT_SECURE_FUNCTIONS
58 #if defined(Q_OS_CYGWIN)
59 #define CSP_OS_GCC_CYGWIN
60 #define CSP_OS_WINDOWS
61 #elif defined(Q_OS_WIN) || defined(__WIN32) || defined(__WIN64)
62 #define CSP_OS_GCC_WINDOWS
63 #define CSP_OS_WINDOWS
65 #define DLL_PREFIX __declspec(dllexport)
66 #define DLL_PREFIX_I __declspec(dllimport)
69 #define CSP_OS_GCC_GENERIC
70 #define CSP_OS_GENERIC
74 #if defined(__clang__)
75 #define __CSP_COMPILER_CLANG
77 #define __CSP_COMPILER_GCC
79 #define SUPPORT_CPLUSPLUS_11
85 #ifndef SUPPORT_CPLUSPLUS_11
86 #if defined(__cplusplus) && (__cplusplus > 199711L)
87 #define SUPPORT_CPLUSPLUS_11
90 #ifndef SUPPORT_TCHAR_TYPE
91 // secure functions need tchar type
92 #undef SUPPORT_SECURE_FUNCTIONS
101 // include common header files
102 #ifdef SUPPORT_TCHAR_TYPE
105 #ifdef SUPPORT_CPLUSPLUS_11
112 #include <typeinfo.h>
119 // include environment/language dependent header files
122 #include <windowsx.h>
124 #include <mmsystem.h>
135 #if defined(__MINGW32__) || (__MINGW64__)
140 #include <QtCore/QString>
141 #include <QtCore/QFile>
143 #include <sys/param.h>
146 #define _MAX_PATH 2048
150 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
151 #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
152 #if __BYTE_ORDER == __LITTLE_ENDIAN
153 #define __LITTLE_ENDIAN__
154 #elif __BYTE_ORDER == __BIG_ENDIAN
155 #define __BIG_ENDIAN__
157 #elif defined(SDL_BYTEORDER) && (defined(SDL_LIL_ENDIAN) || defined(SDL_BIG_ENDIAN))
158 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
159 #define __LITTLE_ENDIAN__
160 #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
161 #define __BIG_ENDIAN__
163 #elif defined(WORDS_LITTLEENDIAN)
164 #define __LITTLE_ENDIAN__
165 #elif defined(WORDS_BIGENDIAN)
166 #define __BIG_ENDIAN__
169 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
170 // may be Microsoft Visual C++
171 #define __LITTLE_ENDIAN__
175 #ifndef SUPPORT_TCHAR_TYPE
181 #ifndef SUPPORT_CPLUSPLUS_11
183 typedef signed char int8_t;
186 typedef signed short int16_t;
189 typedef signed int int32_t;
192 typedef signed long long int64_t;
195 typedef unsigned char uint8_t;
198 typedef unsigned short uint16_t;
201 typedef unsigned int uint32_t;
204 typedef unsigned long long uint64_t;
210 typedef _TCHAR* LPTSTR;
213 typedef const _TCHAR* LPCTSTR;
225 typedef uint8_t BYTE;
228 typedef uint16_t WORD;
231 typedef uint32_t DWORD;
234 typedef uint64_t QWORD;
240 typedef int16_t INT16;
243 typedef int32_t INT32;
246 typedef int64_t INT64;
249 typedef uint8_t UINT8;
252 typedef uint16_t UINT16;
255 typedef uint32_t UINT32;
258 typedef uint64_t UINT64;
264 typedef unsigned int UINT;
270 #ifdef __BIG_ENDIAN__
271 uint8_t h3, h2, h, l;
273 uint8_t l, h, h2, h3;
277 #ifdef __BIG_ENDIAN__
284 #ifdef __BIG_ENDIAN__
291 #ifdef __BIG_ENDIAN__
299 inline void read_2bytes_le_from(uint8_t *t)
301 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
303 inline void write_2bytes_le_to(uint8_t *t)
305 t[0] = b.l; t[1] = b.h;
307 inline void read_2bytes_be_from(uint8_t *t)
309 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
311 inline void write_2bytes_be_to(uint8_t *t)
313 t[0] = b.h; t[1] = b.l;
315 inline void read_4bytes_le_from(uint8_t *t)
317 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
319 inline void write_4bytes_le_to(uint8_t *t)
321 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
323 inline void read_4bytes_be_from(uint8_t *t)
325 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
327 inline void write_4bytes_be_to(uint8_t *t)
329 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
333 uint32_t DLL_PREFIX EndianToLittle_DWORD(uint32_t x);
334 uint16_t DLL_PREFIX EndianToLittle_WORD(uint16_t x);
340 int DLL_PREFIX max(int a, int b);
341 unsigned int DLL_PREFIX max(unsigned int a, unsigned int b);
342 int DLL_PREFIX min(int a, int b);
343 unsigned int DLL_PREFIX min(unsigned int a, unsigned int b);
347 #if defined(__GNUC__) || defined(__CYGWIN__) || defined(Q_OS_CYGWIN)
348 #define stricmp(a,b) strcasecmp(a,b)
349 #define strnicmp(a,b,n) strncasecmp(a,b,n)
352 #ifndef SUPPORT_TCHAR_TYPE
354 #define _fgetts fgets
357 #define _ftprintf printf
360 #define _tfopen fopen
363 #define _tcscmp strcmp
366 #define _tcscpy strcpy
369 #define _tcsicmp stricmp
372 #define _tcslen strlen
375 #define _tcscat strcat
378 #define _tcsncat strncat
381 #define _tcsncpy strncpy
384 #define _tcsncicmp strnicmp
387 #define _tcschr strchr
390 #define _tcsrchr strrchr
393 #define _tcsstr strstr
396 #define _tcstok strtok
402 #define _tcstol strtol
405 #define _tcstoul strtoul
408 #define _stprintf sprintf
411 #define _vstprintf vsprintf
414 #define _taccess access
417 #define _tremove remove
420 #define _trename rename
424 #define _TEXT(x) __T(x)
427 #ifndef SUPPORT_SECURE_FUNCTIONS
431 // errno_t DLL_PREFIX my_tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode);
432 errno_t DLL_PREFIX my_tcscat_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
433 errno_t DLL_PREFIX my_strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
434 errno_t DLL_PREFIX my_tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
435 errno_t DLL_PREFIX my_strncpy_s(char *strDestination, size_t numberOfElements, const char *strSource, size_t count);
436 errno_t DLL_PREFIX my_tcsncpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource, size_t count);
437 char * DLL_PREFIX my_strtok_s(char *strToken, const char *strDelimit, char **context);
438 _TCHAR * DLL_PREFIX my_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
439 #define my_fprintf_s fprintf
440 #define my_ftprintf_s fprintf
441 int DLL_PREFIX my_sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
442 int DLL_PREFIX my_swprintf_s(wchar_t *buffer, size_t sizeOfBuffer, const wchar_t *format, ...);
443 int DLL_PREFIX my_stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
444 int DLL_PREFIX my_vsprintf_s(char *buffer, size_t numberOfElements, const char *format, va_list argptr);
445 int DLL_PREFIX my_vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
447 // #define my_tfopen_s _tfopen_s
448 #define my_tcscat_s _tcscat_s
449 #define my_strcpy_s strcpy_s
450 #define my_tcscpy_s _tcscpy_s
451 #define my_strncpy_s strncpy_s
452 #define my_tcsncpy_s _tcsncpy_s
453 #define my_strtok_s strtok_s
454 #define my_tcstok_s _tcstok_s
455 #define my_fprintf_s fprintf_s
456 #define my_ftprintf_s _ftprintf_s
457 #define my_sprintf_s sprintf_s
458 #define my_swprintf_s swprintf_s
459 #define my_stprintf_s _stprintf_s
460 #define my_vsprintf_s vsprintf_s
461 #define my_vstprintf_s _vstprintf_s
464 // C99 math functions
466 #define my_isfinite _finite
467 #define my_log2(v) (log((double)(v)) / log(2.0))
469 #define my_isfinite isfinite
475 BOOL MyWritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName);
476 DWORD MyGetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName);
477 UINT MyGetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName);
478 // used only in winmain and win32 osd class
479 // #define ZeroMemory(p,s) memset(p,0x00,s)
480 // #define CopyMemory(t,f,s) memcpy(t,f,s)
482 #define MyWritePrivateProfileString WritePrivateProfileString
483 #define MyGetPrivateProfileString GetPrivateProfileString
484 #define MyGetPrivateProfileInt GetPrivateProfileInt
488 #if !defined(_RGB555) && !defined(_RGB565) && !defined(_RGB888)
492 #if defined(_RGB555) || defined(_RGB565)
493 typedef uint16_t scrntype_t;
494 scrntype_t DLL_PREFIX RGB_COLOR(uint32_t r, uint32_t g, uint32_t b);
495 scrntype_t DLL_PREFIX RGBA_COLOR(uint32_t r, uint32_t g, uint32_t b, uint32_t a);
496 uint8_t DLL_PREFIX R_OF_COLOR(scrntype_t c);
497 uint8_t DLL_PREFIX G_OF_COLOR(scrntype_t c);
498 uint8_t DLL_PREFIX B_OF_COLOR(scrntype_t c);
499 uint8_t DLL_PREFIX A_OF_COLOR(scrntype_t c);
500 #elif defined(_RGB888)
501 typedef uint32_t scrntype_t;
502 #define RGB_COLOR(r, g, b) (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0))
503 #define RGBA_COLOR(r, g, b, a) (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0) | ((uint32_t)(a) << 24))
504 #define R_OF_COLOR(c) (((c) >> 16) & 0xff)
505 #define G_OF_COLOR(c) (((c) >> 8) & 0xff)
506 #define B_OF_COLOR(c) (((c) ) & 0xff)
507 #define A_OF_COLOR(c) (((c) >> 24) & 0xff)
510 * Below macros are hint for SIMD.
512 #if defined(__clang__)
513 #define __DECL_VECTORIZED_LOOP _Pragma("clang loop vectorize(enable) interleave(enable)")
514 #elif defined(__GNUC__)
515 #define __DECL_VECTORIZED_LOOP _Pragma("GCC ivdep")
517 #define __DECL_VECTORIZED_LOOP
530 wav_chunk_t riff_chunk;
532 wav_chunk_t fmt_chunk;
535 uint32_t sample_rate;
538 uint16_t sample_bits;
543 const _TCHAR *DLL_PREFIX get_application_path();
544 const _TCHAR *DLL_PREFIX create_local_path(const _TCHAR *format, ...);
545 void DLL_PREFIX create_local_path(_TCHAR *file_path, int length, const _TCHAR *format, ...);
546 const _TCHAR *DLL_PREFIX create_date_file_path(const _TCHAR *extension);
547 void DLL_PREFIX create_date_file_path(_TCHAR *file_path, int length, const _TCHAR *extension);
548 bool DLL_PREFIX check_file_extension(const _TCHAR *file_path, const _TCHAR *ext);
549 const _TCHAR *DLL_PREFIX get_file_path_without_extensiton(const _TCHAR *file_path);
550 void DLL_PREFIX get_long_full_path_name(const _TCHAR* src, _TCHAR* dst, size_t dst_len);
551 const _TCHAR* DLL_PREFIX get_parent_dir(const _TCHAR* file);
554 const _TCHAR *DLL_PREFIX create_string(const _TCHAR* format, ...);
555 const wchar_t *DLL_PREFIX char_to_wchar(const char *cs);
556 const char *DLL_PREFIX wchar_to_char(const wchar_t *ws);
557 const _TCHAR *DLL_PREFIX char_to_tchar(const char *cs);
558 const char *DLL_PREFIX tchar_to_char(const _TCHAR *ts);
559 const _TCHAR *DLL_PREFIX wchar_to_tchar(const wchar_t *ws);
560 const wchar_t *DLL_PREFIX tchar_to_wchar(const _TCHAR *ts);
562 void *DLL_PREFIX my_memcpy(void *dst, void *src, size_t len);
565 int32_t DLL_PREFIX muldiv_s32(int32_t nNumber, int32_t nNumerator, int32_t nDenominator);
566 uint32_t DLL_PREFIX muldiv_u32(uint32_t nNumber, uint32_t nNumerator, uint32_t nDenominator);
568 uint32_t DLL_PREFIX get_crc32(uint8_t data[], int size);
569 uint16_t DLL_PREFIX jis_to_sjis(uint16_t jis);
571 int DLL_PREFIX decibel_to_volume(int decibel);
572 int32_t DLL_PREFIX apply_volume(int32_t sample, int volume);
574 #define array_length(array) (sizeof(array) / sizeof(array[0]))
576 #define FROM_BCD(v) (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
577 #define TO_BCD(v) ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
578 #define TO_BCD_LO(v) ((v) % 10)
579 #define TO_BCD_HI(v) (int)(((v) % 100) / 10)
582 #define LEAP_YEAR(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
584 #define dll_cur_time_t DLL_PREFIX_I struct cur_time_s
586 typedef DLL_PREFIX struct cur_time_s {
587 int year, month, day, day_of_week, hour, minute, second;
595 void update_day_of_week();
596 void save_state(void *f);
597 bool load_state(void *f);
600 void DLL_PREFIX get_host_time(cur_time_t* cur_time);
603 typedef struct symbol_s {
606 struct symbol_s *next_symbol;
609 const _TCHAR* DLL_PREFIX get_symbol(symbol_t *first_symbol, uint32_t addr);
610 const _TCHAR* DLL_PREFIX get_value_or_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);
611 const _TCHAR* DLL_PREFIX get_value_and_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);