File: Byteswapping.h

package info (click to toggle)
pose 3.0a3-3
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 15,500 kB
  • ctags: 20,548
  • sloc: ansic: 72,579; cpp: 50,198; perl: 1,336; python: 1,242; sh: 363; makefile: 290
file content (229 lines) | stat: -rw-r--r-- 6,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */

#ifndef _BYTESWAPPING_H_
#define _BYTESWAPPING_H_

#if !defined (BYTESWAP)
#error "Must define BYTESWAP"
#endif

#include "EmulatorTypes.h"		// SysPktRPC2Type


/*
**	--------------------------------------------------------------------------------
**		Byte swapping
**	--------------------------------------------------------------------------------
*/

#define BYTE_SWAP_16(n)	((((unsigned short) n) >> 8) |	\
						 (((unsigned short) n) << 8))

#define BYTE_SWAP_32(n)	( (((unsigned long) n) << 24) |					\
						 ((((unsigned long) n) << 8) & 0x00FF0000) |	\
						 ((((unsigned long) n) >> 8) & 0x0000FF00) |	\
						  (((unsigned long) n) >> 24))


inline void Byteswap(bool&)
{
}

inline void Byteswap(char&)
{
}

inline void Byteswap(signed char&)
{
}

inline void Byteswap(unsigned char&)
{
}

inline void Byteswap(short& v)
{
	v = (short) BYTE_SWAP_16(v);
}

inline void Byteswap(unsigned short& v)
{
	v = (unsigned short) BYTE_SWAP_16(v);
}

inline void Byteswap(int& v)
{
	if (sizeof(int) == 4)
		v = (int) BYTE_SWAP_32(v);

	else if (sizeof(int) == 2)
		v = (int) BYTE_SWAP_16(v);
}

inline void Byteswap(unsigned int& v)
{
	if (sizeof(unsigned int) == 4)
		v = (unsigned int) BYTE_SWAP_32(v);

	else if (sizeof(unsigned int) == 2)
		v = (unsigned int) BYTE_SWAP_16(v);
}

inline void Byteswap(long& v)
{
	v = (long) BYTE_SWAP_32(v);
}

inline void Byteswap(unsigned long& v)
{
	v = (unsigned long) BYTE_SWAP_32(v);
}

template <class T>
inline void Byteswap(T*& v)
{
	v = (T*) BYTE_SWAP_32(v);
}

inline void Byteswap(WinHandle& v)
{
	v = (WinHandle) BYTE_SWAP_32(v);
}

inline void Byteswap(SysKernelInfoSelector& v)
{
	if (sizeof(SysKernelInfoSelector) == 4)
		v = (SysKernelInfoSelector) BYTE_SWAP_32(v);

	else if (sizeof(SysKernelInfoSelector) == 2)
		v = (SysKernelInfoSelector) BYTE_SWAP_16(v);
}

inline void Byteswap(enum events& v)
{
	if (sizeof(enum events) == 4)
		v = (enum events) BYTE_SWAP_32(v);

	else if (sizeof(enum events) == 2)
		v = (enum events) BYTE_SWAP_16(v);
}

void Byteswap (BreakpointType& p);
void Byteswap (CardHeaderType& p);
void Byteswap (DatabaseDirType& p);
void Byteswap (DatabaseHdrType& p);
void Byteswap (DmAccessType& p);
void Byteswap (DmOpenInfoType& p);
void Byteswap (DmSearchStateType& p);
void Byteswap (EventType& event);
void Byteswap (FieldAttrType& p);
void Byteswap (HwrDBallEZType&);
void Byteswap (HwrDBallType&);
void Byteswap (M68KRegsType& p);
void Byteswap (Mem1ChunkHeaderType& p);
void Byteswap (Mem1HeapHeaderType& p);
void Byteswap (Mem1MstrPtrTableType& p);
void Byteswap (Mem2HeapHeaderType& p);
void Byteswap (MemChunkHeaderType& p);
void Byteswap (MemHeapHeaderType& p);
void Byteswap (MemMstrPtrTableType& p);
void Byteswap (PenBtnInfoType& p);
void Byteswap (PointType& p);
void Byteswap (RecordListType& p);
void Byteswap (RecordEntryType& p);
void Byteswap (RectangleType& p);
void Byteswap (RsrcEntryType& p);
void Byteswap (SlkPktHeaderType& p);
void Byteswap (SlkPktFooterType& p);
void Byteswap (SysAppInfoType& p);
void Byteswap (SysKernelInfoType& p);
void Byteswap (SysNVParamsType& p);
void Byteswap (SysPktBodyType& p);
void Byteswap (SysPktChecksumType& p);			// --- response is same type
void Byteswap (SysPktCommCmdType& p);
void Byteswap (SysPktCommRspType& p);
void Byteswap (SysPktContinueCmdType& p);		// --- no response
void Byteswap (SysPktDbgBreakToggleCmdType& p);
void Byteswap (SysPktDbgBreakToggleRspType& p);
void Byteswap (sysPktExecFlashType& p);			// --- no response
void Byteswap (SysPktFindCmdType& p);
void Byteswap (SysPktFindRspType& p);
void Byteswap (SysPktFlashWriteType& p);		// --- response is same type
void Byteswap (SysPktGetBreakpointsCmdType& p);
void Byteswap (SysPktGetBreakpointsRspType& p);
void Byteswap (SysPktGetTrapBreaksCmdType& p);
void Byteswap (SysPktGetTrapBreaksRspType& p);
void Byteswap (SysPktGetTrapConditionsCmdType& p);
void Byteswap (SysPktGetTrapConditionsRspType& p);
void Byteswap (SysPktGremlinsCmdType&);			// --- no response
void Byteswap (SysPktReadMemCmdType& p);
void Byteswap (SysPktReadMemRspType& p);
void Byteswap (SysPktReadRegsCmdType& p);
void Byteswap (SysPktReadRegsRspType& p);
void Byteswap (SysPktRemoteMsgCmdType& p);		// --- no response
void Byteswap (SysPktRPCType& p);				// --- response is same type
void Byteswap (SysPktRPC2Type& p);				// --- response is same type
void Byteswap (SysPktRtnNameCmdType& p);
void Byteswap (SysPktRtnNameRspType& p);
void Byteswap (SysPktSetBreakpointsCmdType& p);
void Byteswap (SysPktSetBreakpointsRspType& p);
void Byteswap (SysPktSetTrapBreaksCmdType& p);
void Byteswap (SysPktSetTrapBreaksRspType& p);
void Byteswap (SysPktSetTrapConditionsCmdType& p);
void Byteswap (SysPktSetTrapConditionsRspType& p);
void Byteswap (SysPktStateCmdType& p);
void Byteswap (SysPktStateRspType& p);
void Byteswap (SysPktWriteRegsCmdType& p);
void Byteswap (SysPktWriteRegsRspType& p);
void Byteswap (SysPktWriteMemCmdType& p);
void Byteswap (SysPktWriteMemRspType& p);

void Byteswap (regstruct& r);
void Byteswap (struct flag_struct& r);

void Byteswap (SED1375RegsType& p);
void Byteswap (HwrJerryPLDType& p);

#if BYTESWAP

	template <class T>
	inline void Canonical(T& v)
	{
		Byteswap (v);
	}

#else

	template <class T>
	inline void Canonical(T&)
	{
	}

#endif


#if WORDSWAP_MEMORY

	inline void ByteswapWords (void* start, unsigned long length)
	{
		for (unsigned long ii = 0; ii < length / 2; ++ii)
		{
			unsigned short*	p = ((unsigned short*) start) + ii;
			unsigned char*	bp = (unsigned char*) p;
			*p = (((unsigned short) *bp) << 8) | (unsigned short) (*(bp + 1));
		}
	}

#else

	inline void ByteswapWords (void*, unsigned long)
	{
	}

#endif	// WORDSWAP_MEMORY

#endif /* _BYTESWAPPING_H_ */