Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/psx/octoshock/endian.cpp
2 views
1
/* Mednafen - Multi-system Emulator
2
*
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License as published by
5
* the Free Software Foundation; either version 2 of the License, or
6
* (at your option) any later version.
7
*
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU General Public License for more details.
12
*
13
* You should have received a copy of the GNU General Public License
14
* along with this program; if not, write to the Free Software
15
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
*/
17
18
#include <stdio.h>
19
#include "octoshock.h"
20
#include "endian.h"
21
22
void Endian_A16_Swap(void *src, uint32 nelements)
23
{
24
uint32 i;
25
uint8 *nsrc = (uint8 *)src;
26
27
for(i = 0; i < nelements; i++)
28
{
29
uint8 tmp = nsrc[i * 2];
30
31
nsrc[i * 2] = nsrc[i * 2 + 1];
32
nsrc[i * 2 + 1] = tmp;
33
}
34
}
35
36
void Endian_A32_Swap(void *src, uint32 nelements)
37
{
38
uint32 i;
39
uint8 *nsrc = (uint8 *)src;
40
41
for(i = 0; i < nelements; i++)
42
{
43
uint8 tmp1 = nsrc[i * 4];
44
uint8 tmp2 = nsrc[i * 4 + 1];
45
46
nsrc[i * 4] = nsrc[i * 4 + 3];
47
nsrc[i * 4 + 1] = nsrc[i * 4 + 2];
48
49
nsrc[i * 4 + 2] = tmp2;
50
nsrc[i * 4 + 3] = tmp1;
51
}
52
}
53
54
void Endian_A64_Swap(void *src, uint32 nelements)
55
{
56
uint32 i;
57
uint8 *nsrc = (uint8 *)src;
58
59
for(i = 0; i < nelements; i++)
60
{
61
uint8 *base = &nsrc[i * 8];
62
63
for(int z = 0; z < 4; z++)
64
{
65
uint8 tmp = base[z];
66
67
base[z] = base[7 - z];
68
base[7 - z] = tmp;
69
}
70
}
71
}
72
73
void Endian_A16_NE_LE(void *src, uint32 nelements)
74
{
75
#ifdef MSB_FIRST
76
Endian_A16_Swap(src, nelements);
77
#endif
78
}
79
80
void Endian_A32_NE_LE(void *src, uint32 nelements)
81
{
82
#ifdef MSB_FIRST
83
Endian_A32_Swap(src, nelements);
84
#endif
85
}
86
87
void Endian_A64_NE_LE(void *src, uint32 nelements)
88
{
89
#ifdef MSB_FIRST
90
Endian_A64_Swap(src, nelements);
91
#endif
92
}
93
94
//
95
//
96
//
97
void Endian_A16_NE_BE(void *src, uint32 nelements)
98
{
99
#ifdef LSB_FIRST
100
Endian_A16_Swap(src, nelements);
101
#endif
102
}
103
104
void Endian_A32_NE_BE(void *src, uint32 nelements)
105
{
106
#ifdef LSB_FIRST
107
Endian_A32_Swap(src, nelements);
108
#endif
109
}
110
111
void Endian_A64_NE_BE(void *src, uint32 nelements)
112
{
113
#ifdef LSB_FIRST
114
Endian_A64_Swap(src, nelements);
115
#endif
116
}
117
118
static void FlipByteOrder(uint8 *src, uint32 count)
119
{
120
uint8 *start=src;
121
uint8 *end=src+count-1;
122
123
if((count&1) || !count) return; /* This shouldn't happen. */
124
125
count >>= 1;
126
127
while(count--)
128
{
129
uint8 tmp;
130
131
tmp=*end;
132
*end=*start;
133
*start=tmp;
134
end--;
135
start++;
136
}
137
}
138
139
void Endian_V_NE_LE(void *src, uint32 bytesize)
140
{
141
#ifdef MSB_FIRST
142
FlipByteOrder((uint8 *)src, bytesize);
143
#endif
144
}
145
146
void Endian_V_NE_BE(void *src, uint32 bytesize)
147
{
148
#ifdef LSB_FIRST
149
FlipByteOrder((uint8 *)src, bytesize);
150
#endif
151
}
152
153