OSDN Git Service

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