OSDN Git Service

[General][VM][FM7][CMake] Build at least eFM77AV40EX.
[csp-qt/common_source_project-fm7.git] / source / src / common.h
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.08.18 -
6
7         [ common header ]
8 */
9
10 #ifndef _COMMON_H_
11 #define _COMMON_H_
12
13 #ifdef _WIN32
14         #ifdef _MSC_VER
15                 // Microsoft Visual C++
16                 #if _MSC_VER == 1200
17                         // variable scope of 'for' loop for Microsoft Visual C++ 6.0
18                         #define for if(0);else for
19                 #endif
20                 #if _MSC_VER >= 1200
21                         // Microsoft Visual C++ 6.0 or later
22                         #define SUPPORT_TCHAR_TYPE
23                 #endif
24                 #if _MSC_VER >= 1400
25                         // Microsoft Visual C++ 8.0 (2005) or later
26                         #define SUPPORT_SECURE_FUNCTIONS
27                         #pragma warning( disable : 4819 )
28                         //#pragma warning( disable : 4995 )
29                         #pragma warning( disable : 4996 )
30                 #endif
31                 #if _MSC_VER >= 1800
32                         // Microsoft Visual C++ 12.0 (2013) or later
33                         #define SUPPORT_CPLUSPLUS_11
34                 #endif
35         #else
36                 // Win32, but not Microsoft Visual C++
37                 #define SUPPORT_TCHAR_TYPE
38 //              #define SUPPORT_SECURE_FUNCTIONS
39         #endif
40 #endif
41 #ifndef SUPPORT_CPLUSPLUS_11
42         #if defined(__cplusplus) && (__cplusplus > 199711L)
43                 #define SUPPORT_CPLUSPLUS_11
44         #endif
45 #endif
46 #ifndef SUPPORT_TCHAR_TYPE
47         // secure functions need tchar type
48         #undef SUPPORT_SECURE_FUNCTIONS
49 #endif
50
51 #ifdef SUPPORT_TCHAR_TYPE
52         #include <tchar.h>
53 #endif
54 #ifdef SUPPORT_CPLUSPLUS_11
55         #include <stdint.h>
56 #endif
57
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <errno.h>
62
63 #if defined(__GNUC__)
64         #if defined(Q_OS_CYGWIN) 
65                 #define CSP_OS_GCC_CYGWIN
66                 #define CSP_OS_WINDOWS
67         #elif defined(Q_OS_WIN) || defined(__WIN32) || defined(__WIN64)
68                 #define CSP_OS_GCC_WINDOWS
69                 #define CSP_OS_WINDOWS
70         #else
71                 #define CSP_OS_GCC_GENERIC
72                 #define CSP_OS_GENERIC
73         #endif
74         #if defined(__clang__)
75                 #define __CSP_COMPILER_CLANG
76         #else
77                 #define __CSP_COMPILER_GCC
78         #endif
79         #include <stdarg.h>
80 //#include "common_gcc.h"
81 #endif
82 #if defined(_USE_QT)
83         #if defined(_USE_QT5)
84                 #include <QtEndian>
85         #endif
86         #include <sys/param.h>
87         #ifndef _MAX_PATH
88                 #define _MAX_PATH 2048
89         #endif
90 #endif
91
92 #ifdef _WIN32
93 #include <windows.h>
94 #include <windowsx.h>
95 #include <mmsystem.h>
96 #include <process.h>
97 #endif
98
99 #ifdef _USE_QT
100 #ifdef _USE_QT5
101 #include <QString>
102 #include <QFile>
103 #include <QtEndian>
104 #else
105 #include <QtCore/QString>
106 #include <QtCore/QFile>
107 #endif
108 #endif
109
110 // endian
111 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
112         #if defined(__BYTE_ORDER) && (defined(__LITTLE_ENDIAN) || defined(__BIG_ENDIAN))
113                 #if __BYTE_ORDER == __LITTLE_ENDIAN
114                         #define __LITTLE_ENDIAN__
115                 #elif __BYTE_ORDER == __BIG_ENDIAN
116                         #define __BIG_ENDIAN__
117                 #endif
118         #elif defined(SDL_BYTEORDER) && (defined(SDL_LIL_ENDIAN) || defined(SDL_BIG_ENDIAN))
119                 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
120                         #define __LITTLE_ENDIAN__
121                 #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
122                         #define __BIG_ENDIAN__
123                 #endif
124         #elif defined(WORDS_LITTLEENDIAN)
125                 #define __LITTLE_ENDIAN__
126         #elif defined(WORDS_BIGENDIAN)
127                 #define __BIG_ENDIAN__
128         #endif
129 #endif
130 #if !defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
131         // may be Microsoft Visual C++
132         #define __LITTLE_ENDIAN__
133 #endif
134
135 // type definition
136 #ifndef SUPPORT_TCHAR_TYPE
137         #ifndef _TCHAR
138                 typedef char _TCHAR;
139         #endif
140 #endif
141
142 #if defined(__GNUC__)
143         #include <stdint.h>
144         #if !defined(SUPPORT_CPLUSPLUS_11)
145                 #define SUPPORT_CPLUSPLUS_11
146         #endif
147 #endif
148
149 #if !defined(SUPPORT_CPLUSPLUS_11)
150         #ifndef int8_t
151                 typedef signed char int8_t;
152         #endif
153         #ifndef int16_t
154                 typedef signed short int16_t;
155         #endif
156         #ifndef int32_t
157                 typedef signed int int32_t;
158         #endif
159         #ifndef int64_t
160                 typedef signed long long int64_t;
161         #endif
162         #ifndef uint8_t
163                 typedef unsigned char uint8_t;
164         #endif
165         #ifndef uint16_t
166                 typedef unsigned short uint16_t;
167         #endif
168         #ifndef uint32_t
169                 typedef unsigned int uint32_t;
170         #endif
171         #ifndef uint64_t
172                 typedef unsigned long long uint64_t;
173         #endif
174 #endif
175
176 #ifndef _WIN32
177         #ifndef LPTSTR
178                 typedef _TCHAR* LPTSTR;
179         #endif
180         #ifndef LPCTSTR
181                 typedef const _TCHAR* LPCTSTR;
182         #endif
183         #ifndef BOOL
184                 typedef int BOOL;
185         #endif
186         #ifndef TRUE
187                 #define TRUE 1
188         #endif
189         #ifndef FALSE
190                 #define FALSE 0
191         #endif
192         #ifndef BYTE
193                 typedef uint8_t BYTE;
194         #endif
195         #ifndef WORD
196                 typedef uint16_t WORD;
197         #endif
198         #ifndef DWORD
199                 typedef uint32_t DWORD;
200         #endif
201         #ifndef QWORD
202                 typedef uint64_t QWORD;
203         #endif
204         #ifndef INT8
205                 typedef int8_t INT8;
206         #endif
207         #ifndef INT16
208                 typedef int16_t INT16;
209         #endif
210         #ifndef INT32
211                 typedef int32_t INT32;
212         #endif
213         #ifndef INT64
214                 typedef int64_t INT64;
215         #endif
216         #ifndef INT
217                 typedef int INT;
218         #endif
219         #ifndef UINT8
220                 typedef uint8_t UINT8;
221         #endif
222         #ifndef UINT16
223                 typedef uint16_t UINT16;
224         #endif
225         #ifndef UINT32
226                 typedef uint32_t UINT32;
227         #endif
228         #ifndef UINT64
229                 typedef uint64_t UINT64;
230         #endif
231         #ifndef UINT
232                 typedef unsigned int UINT;
233         #endif
234 #endif
235
236 typedef union {
237         struct {
238 #ifdef __BIG_ENDIAN__
239                 uint8_t h3, h2, h, l;
240 #else
241                 uint8_t l, h, h2, h3;
242 #endif
243         } b;
244         struct {
245 #ifdef __BIG_ENDIAN__
246                 int8_t h3, h2, h, l;
247 #else
248                 int8_t l, h, h2, h3;
249 #endif
250         } sb;
251         struct {
252 #ifdef __BIG_ENDIAN__
253                 uint16_t h, l;
254 #else
255                 uint16_t l, h;
256 #endif
257         } w;
258         struct {
259 #ifdef __BIG_ENDIAN__
260                 int16_t h, l;
261 #else
262                 int16_t l, h;
263 #endif
264         } sw;
265         uint32_t d;
266         int32_t sd;
267         inline void read_2bytes_le_from(uint8_t *t)
268         {
269                 b.l = t[0]; b.h = t[1]; b.h2 = b.h3 = 0;
270         }
271         inline void write_2bytes_le_to(uint8_t *t)
272         {
273                 t[0] = b.l; t[1] = b.h;
274         }
275         inline void read_2bytes_be_from(uint8_t *t)
276         {
277                 b.h3 = b.h2 = 0; b.h = t[0]; b.l = t[1];
278         }
279         inline void write_2bytes_be_to(uint8_t *t)
280         {
281                 t[0] = b.h; t[1] = b.l;
282         }
283         inline void read_4bytes_le_from(uint8_t *t)
284         {
285                 b.l = t[0]; b.h = t[1]; b.h2 = t[2]; b.h3 = t[3];
286         }
287         inline void write_4bytes_le_to(uint8_t *t)
288         {
289                 t[0] = b.l; t[1] = b.h; t[2] = b.h2; t[3] = b.h3;
290         }
291         inline void read_4bytes_be_from(uint8_t *t)
292         {
293                 b.h3 = t[0]; b.h2 = t[1]; b.h = t[2]; b.l = t[3];
294         }
295         inline void write_4bytes_be_to(uint8_t *t)
296         {
297                 t[0] = b.h3; t[1] = b.h2; t[2] = b.h; t[3] = b.l;
298         }
299 } pair_t;
300
301 uint32_t EndianToLittle_DWORD(uint32_t x);
302 uint16_t EndianToLittle_WORD(uint16_t x);
303
304 // max/min
305 #ifndef _MSC_VER
306         #undef max
307         #undef min
308         int max(int a, int b);
309         unsigned int max(unsigned int a, unsigned int b);
310         int min(int a, int b);
311         unsigned int min(unsigned int a, unsigned int b);
312 #endif
313
314 // string
315 #if defined(__GNUC__) || defined(__CYGWIN__) || defined(Q_OS_CYGWIN)
316         #define stricmp(a,b) strcasecmp(a,b)
317         #define strnicmp(a,b,n) strncasecmp(a,b,n)
318 #endif
319
320 #ifndef SUPPORT_TCHAR_TYPE
321         #ifndef _tfopen
322                 #define _tfopen fopen
323         #endif
324         #ifndef _tcscmp
325                 #define _tcscmp strcmp
326         #endif
327         #ifndef _tcscpy
328                 #define _tcscpy strcpy
329         #endif
330         #ifndef _tcsicmp
331                 #define _tcsicmp stricmp
332         #endif
333         #ifndef _tcslen
334                 #define _tcslen strlen
335         #endif
336         #ifndef _tcsncat
337                 #define _tcsncat strncat
338         #endif
339         #ifndef _tcsncpy
340                 #define _tcsncpy strncpy
341         #endif
342         #ifndef _tcsncicmp
343                 #define _tcsncicmp strnicmp
344         #endif
345         #ifndef _tcschr
346                 #define _tcschr strchr
347         #endif
348         #ifndef _tcsrchr
349                 #define _tcsrchr strrchr
350         #endif
351         #ifndef _tcsstr
352                 #define _tcsstr strstr
353         #endif
354         #ifndef _tcstok
355                 #define _tcstok strtok
356         #endif
357         #ifndef _tstoi
358                 #define _tstoi atoi
359         #endif
360         #ifndef _tcstol
361                 #define _tcstol strtol
362         #endif
363         #ifndef _tcstoul
364                 #define _tcstoul strtoul
365         #endif
366         #ifndef _stprintf
367                 #define _stprintf sprintf
368         #endif
369         #ifndef _vstprintf
370                 #define _vstprintf vsprintf
371         #endif
372         #ifndef _taccess
373                 #define _taccess access
374         #endif
375         #ifndef _tremove
376                 #define _tremove remove
377         #endif
378         #ifndef _trename
379                 #define _trename rename
380         #endif
381         #define __T(x) x
382         #define _T(x) __T(x)
383         #define _TEXT(x) __T(x)
384 #endif
385
386 #ifndef SUPPORT_SECURE_FUNCTIONS
387         #ifndef errno_t
388                 typedef int errno_t;
389         #endif
390 //      errno_t my_tfopen_s(FILE** pFile, const _TCHAR *filename, const _TCHAR *mode);
391         errno_t my_strcpy_s(char *strDestination, size_t numberOfElements, const char *strSource);
392         errno_t my_tcscpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource);
393         errno_t my_strncpy_s(char *strDestination, size_t numberOfElements, const char *strSource, size_t count);
394         errno_t my_tcsncpy_s(_TCHAR *strDestination, size_t numberOfElements, const _TCHAR *strSource, size_t count);
395         char *my_strtok_s(char *strToken, const char *strDelimit, char **context);
396         _TCHAR *my_tcstok_s(_TCHAR *strToken, const char *strDelimit, _TCHAR **context);
397         #define my_fprintf_s fprintf
398         int my_sprintf_s(char *buffer, size_t sizeOfBuffer, const char *format, ...);
399         int my_stprintf_s(_TCHAR *buffer, size_t sizeOfBuffer, const _TCHAR *format, ...);
400         int my_vsprintf_s(char *buffer, size_t numberOfElements, const char *format, va_list argptr);
401         int my_vstprintf_s(_TCHAR *buffer, size_t numberOfElements, const _TCHAR *format, va_list argptr);
402 #else
403 //      #define my_tfopen_s _tfopen_s
404         #define my_strcpy_s strcpy_s
405         #define my_tcscpy_s _tcscpy_s
406         #define my_strncpy_s strncpy_s
407         #define my_tcsncpy_s _tcsncpy_s
408         #define my_strtok_s strtok_s
409         #define my_tcstok_s _tcstok_s
410         #define my_fprintf_s fprintf_s
411         #define my_sprintf_s sprintf_s
412         #define my_stprintf_s _stprintf_s
413         #define my_vsprintf_s vsprintf_s
414         #define my_vstprintf_s _vstprintf_s
415 #endif
416
417 // win32 api
418 #ifndef _WIN32
419         BOOL MyWritePrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString, LPCTSTR lpFileName);
420         DWORD MyGetPrivateProfileString(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, LPCTSTR lpFileName);
421         UINT MyGetPrivateProfileInt(LPCTSTR lpAppName, LPCTSTR lpKeyName, INT nDefault, LPCTSTR lpFileName);
422         // used only in winmain and win32 osd class
423 //      #define ZeroMemory(p,s) memset(p,0x00,s)
424 //      #define CopyMemory(t,f,s) memcpy(t,f,s)
425 #else
426         #define MyWritePrivateProfileString WritePrivateProfileString
427         #define MyGetPrivateProfileString GetPrivateProfileString
428         #define MyGetPrivateProfileInt GetPrivateProfileInt
429 #endif
430
431 // rgb color
432 #if !defined(_RGB555) && !defined(_RGB565) && !defined(_RGB888)
433         #define _RGB888
434 #endif
435
436 #if defined(_RGB555) || defined(_RGB565)
437         typedef uint16_t scrntype_t;
438         scrntype_t RGB_COLOR(uint32_t r, uint32_t g, uint32_t b);
439         scrntype_t RGBA_COLOR(uint32_t r, uint32_t g, uint32_t b, uint32_t a);
440         uint8_t R_OF_COLOR(scrntype_t c);
441         uint8_t G_OF_COLOR(scrntype_t c);
442         uint8_t B_OF_COLOR(scrntype_t c);
443         uint8_t A_OF_COLOR(scrntype_t c);
444 #elif defined(_RGB888)
445         typedef uint32_t scrntype_t;
446         #define RGB_COLOR(r, g, b)      (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0))
447         #define RGBA_COLOR(r, g, b, a)  (((uint32_t)(r) << 16) | ((uint32_t)(g) << 8) | ((uint32_t)(b) << 0) | ((uint32_t)(a) << 24))
448         #define R_OF_COLOR(c)           (((c) >> 16) & 0xff)
449         #define G_OF_COLOR(c)           (((c) >>  8) & 0xff)
450         #define B_OF_COLOR(c)           (((c)      ) & 0xff)
451         #define A_OF_COLOR(c)           (((c) >> 24) & 0xff)
452 #endif
453
454 // wav file header
455 #pragma pack(1)
456 typedef struct {
457         char id[4];
458         uint32_t size;
459 } wav_chunk_t;
460 #pragma pack()
461
462 #pragma pack(1)
463 typedef struct {
464         wav_chunk_t riff_chunk;
465         char wave[4];
466         wav_chunk_t fmt_chunk;
467         uint16_t format_id;
468         uint16_t channels;
469         uint32_t sample_rate;
470         uint32_t data_speed;
471         uint16_t block_size;
472         uint16_t sample_bits;
473 } wav_header_t;
474 #pragma pack()
475
476 // file path
477 const _TCHAR *get_application_path();
478 const _TCHAR *create_local_path(const _TCHAR *format, ...);
479 void create_local_path(_TCHAR *file_path, int length, const _TCHAR *format, ...);
480 const _TCHAR *create_date_file_path(const _TCHAR *extension);
481 void create_date_file_path(_TCHAR *file_path, int length, const _TCHAR *extension);
482 bool check_file_extension(const _TCHAR *file_path, const _TCHAR *ext);
483 const _TCHAR *get_file_path_without_extensiton(const _TCHAR *file_path);
484 void get_long_full_path_name(const _TCHAR* src, _TCHAR* dst, size_t dst_len);
485 const _TCHAR* get_parent_dir(const _TCHAR* file);
486
487 // misc
488 const _TCHAR *create_string(const _TCHAR* format, ...);
489 uint32_t get_crc32(uint8_t data[], int size);
490 uint16_t jis_to_sjis(uint16_t jis);
491
492 int decibel_to_volume(int decibel);
493 int32_t apply_volume(int32_t sample, int volume);
494
495 #define array_length(array) (sizeof(array) / sizeof(array[0]))
496
497 #define FROM_BCD(v)     (((v) & 0x0f) + (((v) >> 4) & 0x0f) * 10)
498 #define TO_BCD(v)       ((int)(((v) % 100) / 10) << 4) | ((v) % 10)
499 #define TO_BCD_LO(v)    ((v) % 10)
500 #define TO_BCD_HI(v)    (int)(((v) % 100) / 10)
501
502 #define LEAP_YEAR(y)    (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))
503
504 typedef struct cur_time_s {
505         int year, month, day, day_of_week, hour, minute, second;
506         bool initialized;
507         cur_time_s()
508         {
509                 initialized = false;
510         }
511         void increment();
512         void update_year();
513         void update_day_of_week();
514         void save_state(void *f);
515         bool load_state(void *f);
516 } cur_time_t;
517
518 void get_host_time(cur_time_t* cur_time);
519
520 // LOG COMMAND
521 #define EMU_LOG_CPU1        0x00000001
522 #define EMU_LOG_CPU2        0x00000002
523 #define EMU_LOG_CPU3        0x00000004
524 #define EMU_LOG_CPU4        0x00000008
525 #define EMU_LOG_FLOPPY      0x00000010
526 #define EMU_LOG_CMT         0x00000020
527 #define EMU_LOG_QD          0x00000040
528 #define EMU_LOG_CART        0x00000080
529 #define EMU_LOG_BINARY      0x00000100
530 #define EMU_LOG_LASERDISC   0x00000200
531 #define EMU_LOG_DISPLAY     0x00001000
532 #define EMU_LOG_SOUND       0x00002000
533 #define EMU_LOG_KEYBOARD    0x00004000
534 #define EMU_LOG_IO          0x00008000
535 #define EMU_LOG_MEMORY      0x00010000
536 #define EMU_LOG_USR1        0x00020000
537 #define EMU_LOG_USR2        0x00040000
538 #define EMU_LOG_USR3        0x00080000
539 #define EMU_LOG_USR4        0x00100000
540 #define EMU_LOG_USR5        0x00200000
541 #define EMU_LOG_USR6        0x00400000
542 #define EMU_LOG_USR7        0x00800000
543 #define EMU_LOG_USR8        0x01000000
544 #define EMU_LOG_USR9        0x02000000
545 #define EMU_LOG_USR10       0x04000000
546 #define EMU_LOG_GUI         0x08000000
547 #define EMU_LOG_DEBUG       0x10000000
548 #define EMU_LOG_INFO        0x20000000
549 #define EMU_LOG_WARNING     0x40000000
550 #define EMU_LOG_GENERAL     0x80000000
551
552 #endif
553