Debian Codegraph @

tools/nds_firmware_tool/src/common.h

raw
1 /* NDS Firmware tools
2
3 Copyright 2009 DeSmuME team
4
5 This file is part of DeSmuME
6
7 DeSmuME is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 DeSmuME is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with DeSmuME; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifndef _COMMON_H__
23 #define _COMMON_H__
24
25 #if defined(_MSC_VER)
26 #pragma warning(disable: 4995)
27 #pragma warning(disable: 4996)
28 #endif
29
30 typedef unsigned char u8;
31 typedef unsigned short u16;
32 typedef unsigned int u32;
33 typedef unsigned __int64 u64;
34 typedef signed char s8;
35 typedef signed short s16;
36 typedef signed int s32;
37 typedef __int64 s64;
38
39 char __forceinline *hex2(const u32 val)
40 {
41 char buf[30] = {0};
42 sprintf(buf, "0x%02X", val);
43 return strdup(buf);
44 }
45
46 char __forceinline *hex2w(const u32 val)
47 {
48 char buf[30] = {0};
49 sprintf(buf, "%02X", val);
50 return strdup(buf);
51 }
52
53 char __forceinline *hex4(const u32 val)
54 {
55 char buf[30] = {0};
56 sprintf(buf, "0x%04X", val);
57 return strdup(buf);
58 }
59
60 char __forceinline *hex4w(const u32 val)
61 {
62 char buf[30] = {0};
63 sprintf(buf, "%04X", val);
64 return strdup(buf);
65 }
66
67 char __forceinline *hex6(const u32 val)
68 {
69 char buf[30] = {0};
70 sprintf(buf, "0x%06X", val);
71 return strdup(buf);
72 }
73
74 char __forceinline *hex6w(const u32 val)
75 {
76 char buf[30] = {0};
77 sprintf(buf, "%06X", val);
78 return strdup(buf);
79 }
80
81 char __forceinline *hex8(const u32 val)
82 {
83 char buf[30] = {0};
84 sprintf(buf, "0x%08X", val);
85 return strdup(buf);
86 }
87
88 #ifdef _WIN32
89 char __forceinline *error()
90 {
91 LPVOID lpMsgBuf = NULL;
92
93 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
94 NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 512, NULL);
95
96 return strdup((char*)lpMsgBuf);
97 }
98 #endif
99
100 u8 __forceinline read8(u8 *mem, u32 offset)
101 {
102 return (*(u8*)(mem + offset));
103 }
104
105 u16 __forceinline read16(u8 *mem, u32 offset)
106 {
107 return (*(u16*)(mem + offset));
108 }
109
110 u32 __forceinline read24(u8 *mem, u32 offset)
111 {
112 return (*(u16*)(mem + offset)) | (*(u8*)(mem + offset+2) << 16);
113 }
114
115 u32 __forceinline read32(u8 *mem, u32 offset)
116 {
117 return (*(u32*)(mem + offset));
118 }
119
120 u64 __forceinline read64(u8 *mem, u32 offset)
121 {
122 return (*(u64*)(mem + offset));
123 }
124
125 #endif