2 Skelton for retropc emulator
4 Author : Takeda.Toshiya
13 // move shared codes to DLL???
15 // #define USE_SHARED_DLL
22 // use zlib to decompress gzip file???
24 #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__
280 #ifdef __BIG_ENDIAN__
286 uint16_t u16; // ToDo: Remove
287 int16_t s16; // ToDo: Remove
291 inline void read_2bytes_le_from(uint8_t *t)
293 b.l = t[0]; b.h = t[1];
295 inline void write_2bytes_le_to(uint8_t *t)
297 t[0] = b.l; t[1] = b.h;
299 inline void read_2bytes_be_from(uint8_t *t)
301 b.h = t[0]; b.l = t[1];
303 inline void write_2bytes_be_to(uint8_t *t)
305 t[0] = b.h; t[1] = b.l;
308 inline void set_2bytes_be_from(uint16_t n)
317 b.l = bigv.b.l; b.h = bigv.b.h;
319 inline void set_2bytes_le_from(uint16_t n)
328 b.l = littlev.b.l; b.h = littlev.b.h;
330 inline uint16_t get_2bytes_be_to()
338 bigv.b.l = b.l; bigv.b.h = b.h;
341 inline uint16_t get_2bytes_le_to()
349 littlev.b.l = b.l; littlev.b.h = b.h;
357 #ifdef __BIG_ENDIAN__
358 uint8_t h3, h2, h, l;
360 uint8_t l, h, h2, h3;
364 #ifdef __BIG_ENDIAN__
371 #ifdef __BIG_ENDIAN__
378 #ifdef __BIG_ENDIAN__
385 #ifdef __BIG_ENDIAN__
393 float f; // single float
395 inline void read_2bytes_le_from(uint8_t *t)
397 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
399 inline void write_2bytes_le_to(uint8_t *t)
401 t[0] = b.l; t[1] = b.h;
403 inline void read_2bytes_be_from(uint8_t *t)
405 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
407 inline void write_2bytes_be_to(uint8_t *t)
409 t[0] = b.h; t[1] = b.l;
411 inline void read_4bytes_le_from(uint8_t *t)
413 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
415 inline void write_4bytes_le_to(uint8_t *t)
417 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
419 inline void read_4bytes_be_from(uint8_t *t)
421 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
423 inline void write_4bytes_be_to(uint8_t *t)
425 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
428 inline void set_2bytes_be_from(uint16_t n)
437 b.l = bigv.b.l; b.h = bigv.b.h;
440 inline void set_2bytes_le_from(uint16_t n)
449 b.l = littlev.b.l; b.h = littlev.b.h;
452 inline uint16_t get_2bytes_be_to()
460 bigv.b.l = b.l; bigv.b.h = b.h;
463 inline uint16_t get_2bytes_le_to()
471 littlev.b.l = b.l; littlev.b.h = b.h;
475 inline void set_4bytes_be_from(uint32_t n)
480 uint8_t h3, h2, h, l;
484 b.l = bigv.b.l; b.h = bigv.b.h; b.h2 = bigv.b.h2; b.h3 = bigv.b.h3;
486 inline void set_4bytes_le_from(uint32_t n)
491 uint8_t l, h, h2, h3;
495 b.l = littlev.b.l; b.h = littlev.b.h; b.h2 = littlev.b.h2; b.h3 = littlev.b.h3;
497 inline uint32_t get_4bytes_be_to()
502 uint8_t h3, h2, h, l;
505 bigv.b.l = b.l; bigv.b.h = b.h; bigv.b.h2 = b.h2; bigv.b.h3 = b.h3;
508 inline uint32_t get_4bytes_le_to()
513 uint8_t l, h, h2, h3;
516 littlev.b.l = b.l; littlev.b.h = b.h; littlev.b.h2 = b.h2; littlev.b.h3 = b.h3;
524 #ifdef __BIG_ENDIAN__
525 uint8_t h7, h6, h5, h4, h3, h2, h, l;
527 uint8_t l, h, h2, h3, h4, h5, h6, h7;
531 #ifdef __BIG_ENDIAN__
532 int8_t h7, h6, h5, h4, h3, h2, h, l;
534 int8_t l, h, h2, h3, h4, h5, h6, h7;
538 #ifdef __BIG_ENDIAN__
539 uint16_t h3, h2, h, l;
541 uint16_t l, h, h2, h3;
545 #ifdef __BIG_ENDIAN__
546 int16_t h3, h2, h, l;
548 int16_t l, h, h2, h3;
552 #ifdef __BIG_ENDIAN__
553 pair16_t h3, h2, h, l;
555 pair16_t l, h, h2, h3;
559 #ifdef __BIG_ENDIAN__
566 #ifdef __BIG_ENDIAN__
573 #ifdef __BIG_ENDIAN__
580 #ifdef __BIG_ENDIAN__
588 double df; // double float
589 inline void read_2bytes_le_from(uint8_t *t)
591 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
592 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
594 inline void write_2bytes_le_to(uint8_t *t)
596 t[0] = b.l; t[1] = b.h;
598 inline void read_2bytes_be_from(uint8_t *t)
600 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
601 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
603 inline void write_2bytes_be_to(uint8_t *t)
605 t[0] = b.h; t[1] = b.l;
607 inline void read_4bytes_le_from(uint8_t *t)
609 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
610 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
612 inline void write_4bytes_le_to(uint8_t *t)
614 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
616 inline void read_4bytes_be_from(uint8_t *t)
618 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
619 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
621 inline void write_4bytes_be_to(uint8_t *t)
623 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
626 inline void read_8bytes_le_from(uint8_t *t)
628 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
629 b.h4 = t[4]; b.h5 = t[5]; b.h6 = t[6]; b.h7 = t[7];
631 inline void write_8bytes_le_to(uint8_t *t)
633 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
634 t[4] = b.h4; t[5] = b.h5; t[6] = b.h6; t[7] = b.h7;
636 inline void read_8bytes_be_from(uint8_t *t)
638 b.h7 = t[0]; b.h6 = t[1]; b.h5 = t[2]; b.h4 = t[3];
639 b.h3 = t[4]; b.h2 = t[5]; b.h = t[6]; b.l = t[7];
641 inline void write_8bytes_be_to(uint8_t *t)
643 t[0] = b.h7; t[1] = b.h6; t[2] = b.h5; t[3] = b.h4;
644 t[4] = b.h3; t[5] = b.h2; t[6] = b.h; t[7] = b.l;
647 inline void set_2bytes_be_from(uint16_t n)
656 b.l = bigv.b.l; b.h = bigv.b.h;
658 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
660 inline void set_2bytes_le_from(uint16_t n)
669 b.l = littlev.b.l; b.h = littlev.b.h;
671 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
673 inline uint16_t get_2bytes_be_to()
681 bigv.b.l = b.l; bigv.b.h = b.h;
684 inline uint16_t get_2bytes_le_to()
692 littlev.b.l = b.l; littlev.b.h = b.h;
696 inline void set_4bytes_be_from(uint32_t n)
701 uint8_t h3, h2, h, l;
705 b.l = bigv.b.l; b.h = bigv.b.h; b.h2 = bigv.b.h2; b.h3 = bigv.b.h3;
706 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
708 inline void set_4bytes_le_from(uint32_t n)
713 uint8_t l, h, h2, h3;
717 b.l = littlev.b.l; b.h = littlev.b.h; b.h2 = littlev.b.h2; b.h3 = littlev.b.h3;
718 b.h4 = 0; b.h5 = 0; b.h6 = 0; b.h7 = 0;
720 inline uint32_t get_4bytes_be_to()
725 uint8_t h3, h2, h, l;
728 bigv.b.l = b.l; bigv.b.h = b.h; bigv.b.h2 = b.h2; bigv.b.h3 = b.h3;
731 inline uint32_t get_4bytes_le_to()
736 uint8_t l, h, h2, h3;
739 littlev.b.l = b.l; littlev.b.h = b.h; littlev.b.h2 = b.h2; littlev.b.h3 = b.h3;
743 inline void set_8bytes_be_from(uint64_t n)
748 uint8_t h7, h6, h5, h4, h3, h2, h, l;
752 b.l = bigv.b.l; b.h = bigv.b.h; b.h2 = bigv.b.h2; b.h3 = bigv.b.h3;
753 b.h4 = bigv.b.h4; b.h5 = bigv.b.h5; b.h6 = bigv.b.h6; b.h7 = bigv.b.h7;
755 inline void set_8bytes_le_from(uint64_t n)
760 uint8_t l, h, h2, h3, h4, h5, h6, h7;
764 b.l = littlev.b.l; b.h = littlev.b.h; b.h2 = littlev.b.h2; b.h3 = littlev.b.h3;
765 b.h4 = littlev.b.h4; b.h5 = littlev.b.h5; b.h6 = littlev.b.h6; b.h7 = littlev.b.h7;
767 inline uint64_t get_8bytes_be_to()
772 uint8_t h7, h6, h5, h4, h3, h2, h, l;
775 bigv.b.l = b.l; bigv.b.h = b.h; bigv.b.h2 = b.h2; bigv.b.h3 = b.h3;
776 bigv.b.h4 = b.h4; bigv.b.h5 = b.h5; bigv.b.h6 = b.h6; bigv.b.h7 = b.h7;
779 inline uint64_t get_8bytes_le_to()
784 uint8_t l, h, h2, h3, h4, h5, h6, h7;
787 littlev.b.l = b.l; littlev.b.h = b.h; littlev.b.h2 = b.h2; littlev.b.h3 = b.h3;
788 littlev.b.h4 = b.h4; littlev.b.h5 = b.h5; littlev.b.h6 = b.h6; littlev.b.h7 = b.h7;
794 uint32_t DLL_PREFIX EndianToLittle_DWORD(uint32_t x);
795 uint16_t DLL_PREFIX EndianToLittle_WORD(uint16_t x);
796 uint32_t DLL_PREFIX EndianFromLittle_DWORD(uint32_t x);
797 uint16_t DLL_PREFIX EndianFromLittle_WORD(uint16_t x);
799 uint32_t DLL_PREFIX EndianToBig_DWORD(uint32_t x);
800 uint16_t DLL_PREFIX EndianToBig_WORD(uint16_t x);
801 uint32_t DLL_PREFIX EndianFromBig_DWORD(uint32_t x);
802 uint16_t DLL_PREFIX EndianFromBig_WORD(uint16_t x);
807 int DLL_PREFIX max(int a, int b);
808 unsigned int DLL_PREFIX max(int a, unsigned int b);
809 unsigned int DLL_PREFIX max(unsigned int a, int b);
810 unsigned int DLL_PREFIX max(unsigned int a, unsigned int b);
811 int DLL_PREFIX min(int a, int b);
812 int DLL_PREFIX min(unsigned int a, int b);
813 int DLL_PREFIX min(int a, unsigned int b);
814 unsigned int DLL_PREFIX min(unsigned int a, unsigned int b);
818 #if defined(__GNUC__) || defined(__CYGWIN__) || defined(Q_OS_CYGWIN)
819 #define stricmp(a,b) strcasecmp(a,b)
820 #define strnicmp(a,b,n) strncasecmp(a,b,n)
823 #ifndef SUPPORT_TCHAR_TYPE
825 #define _fgetts fgets
828 #define _ftprintf printf
831 #define _tfopen fopen
834 #define _tcscmp strcmp
837 #define _tcscpy strcpy
840 #define _tcsicmp stricmp
843 #define _tcslen strlen
846 #define _tcscat strcat
849 #define _tcsncat strncat
852 #define _tcsncpy strncpy
855 #define _tcsncicmp strnicmp
858 #define _tcschr strchr
861 #define _tcsrchr strrchr
864 #define _tcsstr strstr
867 #define _tcstok strtok
873 #define _tcstol strtol
876 #define _tcstoul strtoul
879 #define _stprintf sprintf
882 #define _vstprintf vsprintf
885 #define _taccess access
888 #define _tremove remove
891 #define _trename rename
895 #define _TEXT(x) __T(x)
898 #ifndef SUPPORT_SECURE_FUNCTIONS
902 // errno_t DLL_PREFIX my_tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode);
903 errno_t DLL_PREFIX my_tcscat_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
904 errno_t DLL_PREFIX my_strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
905 errno_t DLL_PREFIX my_tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
906 errno_t DLL_PREFIX my_strncpy_s(char *strDestination, size_t numberOfElements, const char *strSource, size_t count);
907 errno_t DLL_PREFIX my_tcsncpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource, size_t count);
908 char * DLL_PREFIX my_strtok_s(char *strToken, const char *strDelimit, char **context);
909 _TCHAR *DLL_PREFIX my_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
910 #define my_fprintf_s fprintf
911 #define my_ftprintf_s fprintf
912 int DLL_PREFIX my_sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
913 int DLL_PREFIX my_swprintf_s(wchar_t *buffer, size_t sizeOfBuffer, const wchar_t *format, ...);
914 int DLL_PREFIX my_stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
915 int DLL_PREFIX my_vsprintf_s(char *buffer, size_t numberOfElements, const char *format, va_list argptr);
916 int DLL_PREFIX my_vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
918 // #define my_tfopen_s _tfopen_s
919 #define my_tcscat_s _tcscat_s
920 #define my_strcpy_s strcpy_s
921 #define my_tcscpy_s _tcscpy_s
922 #define my_strncpy_s strncpy_s
923 #define my_tcsncpy_s _tcsncpy_s
924 #define my_strtok_s strtok_s
925 #define my_tcstok_s _tcstok_s
926 #define my_fprintf_s fprintf_s
927 #define my_ftprintf_s _ftprintf_s
928 #define my_sprintf_s sprintf_s
929 #define my_swprintf_s swprintf_s
930 #define my_stprintf_s _stprintf_s
931 #define my_vsprintf_s vsprintf_s
932 #define my_vstprintf_s _vstprintf_s
937 void *DLL_PREFIX my_memcpy(void *dst, void *src, size_t len);
939 #define my_memcpy memcpy
943 #if defined(__clang__)
944 #define __DECL_VECTORIZED_LOOP _Pragma("clang loop vectorize(enable) distribute(enable)")
945 #elif defined(__GNUC__)
946 #define __DECL_VECTORIZED_LOOP _Pragma("GCC ivdep")
948 #define __DECL_VECTORIZED_LOOP
951 // C99 math functions
953 #define my_isfinite _finite
954 #define my_log2(v) (log((double)(v)) / log(2.0))
957 #define my_isfinite std::isfinite
963 BOOL MyWritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName);
964 DWORD MyGetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName);
965 UINT MyGetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName);
966 // used only in winmain and win32 osd class
967 // #define ZeroMemory(p,s) memset(p,0x00,s)
968 // #define CopyMemory(t,f,s) memcpy(t,f,s)
970 #define MyWritePrivateProfileString WritePrivateProfileString
971 #define MyGetPrivateProfileString GetPrivateProfileString
972 #define MyGetPrivateProfileInt GetPrivateProfileInt
976 #if !defined(_RGB555) && !defined(_RGB565) && !defined(_RGB888)
980 #if defined(_RGB555) || defined(_RGB565)
981 typedef uint16_t scrntype_t;
982 scrntype_t DLL_PREFIX RGB_COLOR(uint32_t r, uint32_t g, uint32_t b);
983 scrntype_t DLL_PREFIX RGBA_COLOR(uint32_t r, uint32_t g, uint32_t b, uint32_t a);
984 uint8_t DLL_PREFIX R_OF_COLOR(scrntype_t c);
985 uint8_t DLL_PREFIX G_OF_COLOR(scrntype_t c);
986 uint8_t DLL_PREFIX B_OF_COLOR(scrntype_t c);
987 uint8_t DLL_PREFIX A_OF_COLOR(scrntype_t c);
988 #elif defined(_RGB888)
989 typedef uint32_t scrntype_t;
990 #if defined(__LITTLE_ENDIAN__)
991 #define RGB_COLOR(r, g, b) (((uint32_t)(b) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(r) << 0) | (0xff << 24))
992 #define RGBA_COLOR(r, g, b, a) (((uint32_t)(b) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(r) << 0) | ((uint32_t)(a) << 24))
993 #define R_OF_COLOR(c) (((c) ) & 0xff)
994 #define G_OF_COLOR(c) (((c) >> 8) & 0xff)
995 #define B_OF_COLOR(c) (((c) >> 16) & 0xff)
996 #define A_OF_COLOR(c) (((c) >> 24) & 0xff)
998 #define RGB_COLOR(r, g, b) (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0) | (0xff << 24))
999 #define RGBA_COLOR(r, g, b, a) (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0) | ((uint32_t)(a) << 24))
1000 #define R_OF_COLOR(c) (((c) >> 16) & 0xff)
1001 #define G_OF_COLOR(c) (((c) >> 8) & 0xff)
1002 #define B_OF_COLOR(c) (((c) ) & 0xff)
1003 #define A_OF_COLOR(c) (((c) >> 24) & 0xff)
1008 // Below routines aims to render common routine.
1011 #define __DECL_ALIGNED(foo) __declspec(align(foo))
1012 #ifndef __builtin_assume_aligned
1013 #define __builtin_assume_aligned(foo, a) foo
1015 #elif defined(__GNUC__)
1016 #define __DECL_ALIGNED(foo) __attribute__((aligned(foo)))
1019 #define __builtin_assume_aligned(foo, a) foo
1020 #define __DECL_ALIGNED(foo)
1024 #if defined(_RGB555) || defined(_RGBA565)
1025 typedef __DECL_ALIGNED(16) union {
1029 typedef __DECL_ALIGNED(16) union {
1034 typedef __DECL_ALIGNED(32) union {
1038 typedef __DECL_ALIGNED(32) union {
1044 typedef __DECL_ALIGNED(16) union {
1049 typedef __DECL_ALIGNED(16) union {
1054 typedef __DECL_ALIGNED(16) union {
1059 typedef __DECL_ALIGNED(16) struct {
1060 uint16_vec8_t plane_table[256];
1061 } _bit_trans_table_t;
1063 typedef __DECL_ALIGNED(sizeof(scrntype_vec8_t)) struct {
1064 scrntype_vec8_t plane_table[256];
1065 } _bit_trans_table_scrn_t;
1068 scrntype_t* palette; // Must be 2^planes entries. If NULL, assume RGB.
1069 _bit_trans_table_t* bit_trans_table[16]; // Must be exist >= planes. Must be aligned with sizeof(uint16_vec8_t).
1070 int xzoom; // 1 - 4?
1074 uint32_t baseaddress[16];
1075 uint32_t voffset[16];
1076 uint32_t addrmask; // For global increment.
1077 uint32_t addrmask2; // For local increment.
1079 uint32_t render_width;
1080 } _render_command_data_t;
1083 inline scrntype_vec8_t ConvertByteToMonochromePackedPixel(uint8_t src, _bit_trans_table_t *tbl,scrntype_t on_val, scrntype_t off_val)
1085 __DECL_ALIGNED(16) uint16_vec8_t tmpd;
1086 __DECL_ALIGNED(32) scrntype_vec8_t tmpdd;
1087 _bit_trans_table_t* vt = (_bit_trans_table_t*)__builtin_assume_aligned(tbl, sizeof(uint16_vec8_t));
1089 tmpd.v = vt->plane_table[src].v;
1090 __DECL_VECTORIZED_LOOP
1091 for(int i = 0; i < 8; i++) {
1092 tmpdd.w[i] = (tmpd.w[i] == 0) ? off_val: on_val;
1097 // Note: Pls. read Note(s) of common.cpp -- 20181105 K.Ohta.
1098 // Tables for below functions must be aligned by 16 (_bit_trans_table_t) or 32(_bit_trans_table_scrn_t).
1099 void DLL_PREFIX ConvertByteToPackedPixelByColorTable(uint8_t *src, scrntype_t* dst, int bytes, _bit_trans_table_t *tbl, scrntype_t *on_color_table, scrntype_t* off_color_table);
1100 void DLL_PREFIX ConvertByteToPackedPixelByColorTable2(uint8_t *src, scrntype_t* dst, int bytes, _bit_trans_table_scrn_t *tbl, scrntype_t *on_color_table, scrntype_t* off_color_table);
1101 void DLL_PREFIX ConvertByteToSparceUint16(uint8_t *src, uint16_t* dst, int bytes, _bit_trans_table_t *tbl, uint16_t mask);
1102 void DLL_PREFIX ConvertByteToSparceUint8(uint8_t *src, uint16_t* dst, int bytes, _bit_trans_table_t *tbl, uint16_t mask);
1104 // Table must be (ON_VAL_COLOR : OFF_VAL_COLOR)[256].
1105 inline scrntype_vec8_t ConvertByteToPackedPixel_PixelTbl(uint8_t src, _bit_trans_table_scrn_t *tbl)
1107 __DECL_ALIGNED(32) scrntype_vec8_t tmpdd;
1108 _bit_trans_table_scrn_t* vt = (_bit_trans_table_scrn_t*)__builtin_assume_aligned(tbl, sizeof(uint16_vec8_t));
1110 tmpdd.v = vt->plane_table[src].v;
1114 // Table must be (ON_VAL_COLOR : OFF_VAL_COLOR)[256].
1115 inline scrntype_vec16_t ConvertByteToDoublePackedPixel_PixelTbl(uint8_t src, _bit_trans_table_scrn_t *tbl)
1117 __DECL_ALIGNED(32) scrntype_vec16_t tmpdd;
1118 __DECL_ALIGNED(32) scrntype_vec8_t tmpd;
1119 _bit_trans_table_scrn_t* vt = (_bit_trans_table_scrn_t*)__builtin_assume_aligned(tbl, sizeof(uint16_vec8_t));
1120 tmpd.v = vt->plane_table[src].v;
1122 __DECL_VECTORIZED_LOOP
1123 for(int i = 0; i < 16; i += 2) {
1124 tmpdd.w[i] = tmpd.w[j];
1125 tmpdd.w[i + 1] = tmpd.w[j];
1131 // Table must be initialize ON_COLOR : OFF_COLOR
1132 inline void ConvertByteToDoubleMonochromeUint8(uint8_t src, uint8_t* dst, _bit_trans_table_t* tbl)
1134 __DECL_ALIGNED(16) uint16_vec8_t tmpd;
1135 uint16_vec8_t* vt = (uint16_vec8_t*)__builtin_assume_aligned(&(tbl->plane_table[0]), sizeof(uint16_vec8_t));
1137 __DECL_ALIGNED(16) uint8_t d[16];
1140 __DECL_VECTORIZED_LOOP
1141 for(int i = 0; i < 16; i += 2) {
1142 d[i] = (uint8_t)(tmpd.w[j]);
1143 d[i + 1] = (uint8_t)(tmpd.w[j]);
1146 __DECL_VECTORIZED_LOOP
1147 for(int i = 0; i < 16; i++) {
1152 inline void ConvertByteToMonochromeUint8(uint8_t src, uint8_t* dst, _bit_trans_table_t* tbl)
1154 __DECL_ALIGNED(16) uint16_vec8_t tmpd;
1155 uint16_vec8_t* vt = (uint16_vec8_t*)__builtin_assume_aligned(&(tbl->plane_table[0]), sizeof(uint16_vec8_t));
1158 __DECL_VECTORIZED_LOOP
1159 for(int i = 0; i < 8; i++) {
1160 dst[i] = (uint8_t)(tmpd.w[i]);
1164 inline void ConvertRGBTo8ColorsUint8(uint8_t r, uint8_t g, uint8_t b, uint8_t* dst, _bit_trans_table_t* rtbl, _bit_trans_table_t* gtbl, _bit_trans_table_t* btbl, int shift)
1166 __DECL_ALIGNED(16) uint16_vec8_t tmpd;
1167 uint16_vec8_t* rvt = (uint16_vec8_t*)__builtin_assume_aligned(&(rtbl->plane_table[0]), sizeof(uint16_vec8_t));
1168 uint16_vec8_t* gvt = (uint16_vec8_t*)__builtin_assume_aligned(&(gtbl->plane_table[0]), sizeof(uint16_vec8_t));
1169 uint16_vec8_t* bvt = (uint16_vec8_t*)__builtin_assume_aligned(&(btbl->plane_table[0]), sizeof(uint16_vec8_t));
1172 tmpd.v = tmpd.v | gvt[g].v;
1173 tmpd.v = tmpd.v | bvt[b].v;
1174 tmpd.v = tmpd.v >> shift;
1175 __DECL_VECTORIZED_LOOP
1176 for(int i = 0; i < 8; i++) {
1177 dst[i] = (uint8_t)(tmpd.w[i]);
1181 inline void ConvertRGBTo8ColorsUint8_Zoom2Left(uint8_t r, uint8_t g, uint8_t b, uint8_t* dst, _bit_trans_table_t* rtbl, _bit_trans_table_t* gtbl, _bit_trans_table_t* btbl, int shift)
1183 __DECL_ALIGNED(16) uint16_vec8_t tmpd;
1184 uint16_vec8_t* rvt = (uint16_vec8_t*)__builtin_assume_aligned(&(rtbl->plane_table[0]), sizeof(uint16_vec8_t));
1185 uint16_vec8_t* gvt = (uint16_vec8_t*)__builtin_assume_aligned(&(gtbl->plane_table[0]), sizeof(uint16_vec8_t));
1186 uint16_vec8_t* bvt = (uint16_vec8_t*)__builtin_assume_aligned(&(btbl->plane_table[0]), sizeof(uint16_vec8_t));
1189 tmpd.v = tmpd.v | gvt[g].v;
1190 tmpd.v = tmpd.v | bvt[b].v;
1191 tmpd.v = tmpd.v >> shift;
1192 __DECL_VECTORIZED_LOOP
1193 for(int i = 0, j = 0; i < 8; i += 2, j++) {
1194 dst[i] = (uint8_t)(tmpd.w[j]);
1195 dst[i + 1] = (uint8_t)(tmpd.w[j]);
1199 inline void ConvertRGBTo8ColorsUint8_Zoom2Right(uint8_t r, uint8_t g, uint8_t b, uint8_t* dst, _bit_trans_table_t* rtbl, _bit_trans_table_t* gtbl, _bit_trans_table_t* btbl, int shift)
1201 __DECL_ALIGNED(16) uint16_vec8_t tmpd;
1202 uint16_vec8_t* rvt = (uint16_vec8_t*)__builtin_assume_aligned(&(rtbl->plane_table[0]), sizeof(uint16_vec8_t));
1203 uint16_vec8_t* gvt = (uint16_vec8_t*)__builtin_assume_aligned(&(gtbl->plane_table[0]), sizeof(uint16_vec8_t));
1204 uint16_vec8_t* bvt = (uint16_vec8_t*)__builtin_assume_aligned(&(btbl->plane_table[0]), sizeof(uint16_vec8_t));
1207 tmpd.v = tmpd.v | gvt[g].v;
1208 tmpd.v = tmpd.v | bvt[b].v;
1209 tmpd.v = tmpd.v >> shift;
1210 __DECL_VECTORIZED_LOOP
1211 for(int i = 0, j = 4; i < 8; i += 2, j++) {
1212 dst[i] = (uint8_t)(tmpd.w[j]);
1213 dst[i + 1] = (uint8_t)(tmpd.w[j]);
1217 inline void ConvertRGBTo8ColorsUint8_Zoom2Double(uint8_t r, uint8_t g, uint8_t b, uint8_t* dst, _bit_trans_table_t* rtbl, _bit_trans_table_t* gtbl, _bit_trans_table_t* btbl, int shift)
1219 __DECL_ALIGNED(16) uint16_vec8_t tmpd;
1220 uint16_vec8_t* rvt = (uint16_vec8_t*)__builtin_assume_aligned(&(rtbl->plane_table[0]), sizeof(uint16_vec8_t));
1221 uint16_vec8_t* gvt = (uint16_vec8_t*)__builtin_assume_aligned(&(gtbl->plane_table[0]), sizeof(uint16_vec8_t));
1222 uint16_vec8_t* bvt = (uint16_vec8_t*)__builtin_assume_aligned(&(btbl->plane_table[0]), sizeof(uint16_vec8_t));
1225 tmpd.v = tmpd.v | gvt[g].v;
1226 tmpd.v = tmpd.v | bvt[b].v;
1227 tmpd.v = tmpd.v >> shift;
1228 __DECL_VECTORIZED_LOOP
1229 for(int i = 0, j = 0; i < 16; i += 2, j++) {
1230 dst[i] = (uint8_t)(tmpd.w[j]);
1231 dst[i + 1] = (uint8_t)(tmpd.w[j]);
1235 inline void ConvertByteToMonochromeUint8Cond_Zoom2(uint8_t src, uint8_t* dst, _bit_trans_table_t* tbl, uint8_t on_color, uint8_t off_color)
1237 __DECL_ALIGNED(16) uint16_vec8_t tmpd;
1238 uint16_vec8_t* vt = (uint16_vec8_t*)__builtin_assume_aligned(&(tbl->plane_table[0]), sizeof(uint16_vec8_t));
1240 __DECL_ALIGNED(16) uint8_t d[16];
1243 __DECL_VECTORIZED_LOOP
1244 for(int i = 0; i < 16; i += 2) {
1245 d[i] = (tmpd.w[j] == 0) ? off_color : on_color;
1246 d[i + 1] = (tmpd.w[j] == 0) ? off_color : on_color;
1249 __DECL_VECTORIZED_LOOP
1250 for(int i = 0; i < 16; i++) {
1255 inline void ConvertByteToMonochromeUint8Cond(uint8_t src, uint8_t* dst, _bit_trans_table_t* tbl, uint8_t on_color, uint8_t off_color)
1257 __DECL_ALIGNED(16) uint16_vec8_t tmpd;
1258 uint16_vec8_t* vt = (uint16_vec8_t*)__builtin_assume_aligned(&(tbl->plane_table[0]), sizeof(uint16_vec8_t));
1261 __DECL_VECTORIZED_LOOP
1262 for(int i = 0; i < 8; i++) {
1263 dst[i] = (tmpd.w[i] == 0) ? off_color : on_color;
1267 void DLL_PREFIX PrepareBitTransTableUint16(_bit_trans_table_t *tbl, uint16_t on_val, uint16_t off_val);
1268 void DLL_PREFIX PrepareBitTransTableScrnType(_bit_trans_table_scrn_t *tbl, scrntype_t on_val, scrntype_t off_val);
1269 void DLL_PREFIX PrepareReverseBitTransTableUint16(_bit_trans_table_t *tbl, uint16_t on_val, uint16_t off_val);
1270 void DLL_PREFIX PrepareReverseBitTransTableScrnType(_bit_trans_table_scrn_t *tbl, scrntype_t on_val, scrntype_t off_val);
1272 void DLL_PREFIX Render8Colors_Line(_render_command_data_t *src, scrntype_t *dst, scrntype_t *dst2, bool scan_line);
1274 void DLL_PREFIX Render16Colors_Line(_render_command_data_t *src, scrntype_t *dst, scrntype_t *dst2, bool scan_line);
1275 void DLL_PREFIX Render2NColors_Line(_render_command_data_t *src, scrntype_t *dst, scrntype_t* dst2, bool scan_line, int planes);
1277 void DLL_PREFIX Convert8ColorsToByte_Line(_render_command_data_t *src, uint8_t *dst);
1278 void DLL_PREFIX Convert2NColorsToByte_Line(_render_command_data_t *src, uint8_t *dst, int planes);
1279 void DLL_PREFIX Convert2NColorsToByte_LineZoom2(_render_command_data_t *src, uint8_t *dst, int planes);
1281 inline uint64_t ExchangeEndianU64(uint64_t __in)
1287 __o.b.h5 = __i.b.h2;
1288 __o.b.h4 = __i.b.h3;
1289 __o.b.h3 = __i.b.h4;
1290 __o.b.h2 = __i.b.h5;
1296 inline int64_t ExchangeEndianS64(uint64_t __in)
1302 __o.b.h5 = __i.b.h2;
1303 __o.b.h4 = __i.b.h3;
1304 __o.b.h3 = __i.b.h4;
1305 __o.b.h2 = __i.b.h5;
1310 inline uint32_t ExchangeEndianU32(uint32_t __in)
1321 inline int32_t ExchangeEndianS32(uint32_t __in)
1332 inline uint16_t ExchangeEndianU16(uint16_t __in)
1341 inline int16_t ExchangeEndianS16(uint16_t __in)
1360 wav_chunk_t riff_chunk;
1362 wav_chunk_t fmt_chunk;
1365 uint32_t sample_rate;
1366 uint32_t data_speed;
1367 uint16_t block_size;
1368 uint16_t sample_bits;
1372 // See http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html.
1375 wav_chunk_t riff_chunk;
1377 wav_chunk_t fmt_chunk;
1380 uint32_t sample_rate;
1381 uint32_t data_speed;
1382 uint16_t block_size;
1383 uint16_t sample_bits;
1384 uint16_t cbsize; // Extension size.Normaly set to 0.
1385 wav_chunk_t fact_chunk; // "fact", 4.
1386 } wav_header_float_t;
1389 // Use this before writing wav_data.
1390 bool DLL_PREFIX write_dummy_wav_header(void *__fio);
1391 // Use this after writng wav_data.
1392 bool DLL_PREFIX set_wav_header(wav_header_t *header, wav_chunk_t *first_chunk, uint16_t channels, uint32_t rate,
1393 uint16_t bits, size_t file_length);
1394 bool DLL_PREFIX load_wav_to_stereo(void *__fio, int16_t **left_buf, int16_t **right_buf, uint32_t *rate, int *got_samples);
1395 bool DLL_PREFIX load_wav_to_monoral(void *__fio, int16_t **buffer, uint32_t *rate, int *got_samples);
1398 const _TCHAR *DLL_PREFIX get_application_path();
1399 const _TCHAR *DLL_PREFIX get_initial_current_path();
1400 const _TCHAR *DLL_PREFIX create_local_path(const _TCHAR *format, ...);
1401 void DLL_PREFIX create_local_path(_TCHAR *file_path, int length, const _TCHAR *format, ...);
1402 const _TCHAR *DLL_PREFIX create_date_file_path(const _TCHAR *extension);
1403 bool DLL_PREFIX is_absolute_path(const _TCHAR *file_path);
1404 void DLL_PREFIX create_date_file_path(_TCHAR *file_path, int length, const _TCHAR *extension);
1405 const _TCHAR *DLL_PREFIX create_date_file_name(const _TCHAR *extension);
1406 void DLL_PREFIX create_date_file_name(_TCHAR *file_path, int length, const _TCHAR *extension);
1407 bool DLL_PREFIX check_file_extension(const _TCHAR *file_path, const _TCHAR *ext);
1408 const _TCHAR *DLL_PREFIX get_file_path_without_extensiton(const _TCHAR *file_path);
1409 void DLL_PREFIX get_long_full_path_name(const _TCHAR* src, _TCHAR* dst, size_t dst_len);
1410 const _TCHAR* DLL_PREFIX get_parent_dir(const _TCHAR* file);
1413 const _TCHAR *DLL_PREFIX create_string(const _TCHAR* format, ...);
1414 const wchar_t *DLL_PREFIX char_to_wchar(const char *cs);
1415 const char *DLL_PREFIX wchar_to_char(const wchar_t *ws);
1416 const _TCHAR *DLL_PREFIX char_to_tchar(const char *cs);
1417 const char *DLL_PREFIX tchar_to_char(const _TCHAR *ts);
1418 const _TCHAR *DLL_PREFIX wchar_to_tchar(const wchar_t *ws);
1419 const wchar_t *DLL_PREFIX tchar_to_wchar(const _TCHAR *ts);
1423 void DLL_PREFIX common_initialize();
1425 int32_t DLL_PREFIX muldiv_s32(int32_t nNumber, int32_t nNumerator, int32_t nDenominator);
1426 uint32_t DLL_PREFIX muldiv_u32(uint32_t nNumber, uint32_t nNumerator, uint32_t nDenominator);
1428 uint32_t DLL_PREFIX get_crc32(uint8_t data[], int size);
1429 uint32_t DLL_PREFIX calc_crc32(uint32_t seed, uint8_t data[], int size);
1430 uint16_t DLL_PREFIX jis_to_sjis(uint16_t jis);
1432 int DLL_PREFIX decibel_to_volume(int decibel);
1433 int32_t DLL_PREFIX apply_volume(int32_t sample, int volume);
1435 #define array_length(array) (sizeof(array) / sizeof(array[0]))
1437 #define FROM_BCD(v) (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
1438 #define TO_BCD(v) ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
1439 #define TO_BCD_LO(v) ((v) % 10)
1440 #define TO_BCD_HI(v) (int)(((v) % 100) / 10)
1443 #define LEAP_YEAR(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
1445 #define dll_cur_time_t DLL_PREFIX_I struct cur_time_s
1447 typedef DLL_PREFIX struct cur_time_s {
1448 int year, month, day, day_of_week, hour, minute, second;
1452 initialized = false;
1456 void update_day_of_week();
1457 bool process_state(void *f, bool loading);
1460 void DLL_PREFIX get_host_time(cur_time_t* cur_time);
1461 const _TCHAR DLL_PREFIX *get_lib_common_version();
1464 typedef struct symbol_s {
1467 struct symbol_s *next_symbol;
1470 const _TCHAR* DLL_PREFIX get_symbol(symbol_t *first_symbol, uint32_t addr);
1471 const _TCHAR* DLL_PREFIX get_value_or_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);
1472 const _TCHAR* DLL_PREFIX get_value_and_symbol(symbol_t *first_symbol, const _TCHAR *format, uint32_t addr);