| 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 "mednafen-endian.h" |
| 19 | |
| 20 | void Endian_A16_Swap(void *src, uint32_t nelements) |
| 21 | { |
| 22 | uint32_t i; |
| 23 | uint8_t *nsrc = (uint8_t *)src; |
| 24 | |
| 25 | for(i = 0; i < nelements; i++) |
| 26 | { |
| 27 | uint8_t tmp = nsrc[i * 2]; |
| 28 | |
| 29 | nsrc[i * 2] = nsrc[i * 2 + 1]; |
| 30 | nsrc[i * 2 + 1] = tmp; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void Endian_A32_Swap(void *src, uint32_t nelements) |
| 35 | { |
| 36 | uint32_t i; |
| 37 | uint8_t *nsrc = (uint8_t *)src; |
| 38 | |
| 39 | for(i = 0; i < nelements; i++) |
| 40 | { |
| 41 | uint8_t tmp1 = nsrc[i * 4]; |
| 42 | uint8_t tmp2 = nsrc[i * 4 + 1]; |
| 43 | |
| 44 | nsrc[i * 4] = nsrc[i * 4 + 3]; |
| 45 | nsrc[i * 4 + 1] = nsrc[i * 4 + 2]; |
| 46 | |
| 47 | nsrc[i * 4 + 2] = tmp2; |
| 48 | nsrc[i * 4 + 3] = tmp1; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void Endian_A64_Swap(void *src, uint32_t nelements) |
| 53 | { |
| 54 | uint32_t i; |
| 55 | uint8_t *nsrc = (uint8_t *)src; |
| 56 | |
| 57 | for(i = 0; i < nelements; i++) |
| 58 | { |
| 59 | unsigned z; |
| 60 | uint8_t *base = &nsrc[i * 8]; |
| 61 | |
| 62 | for(z = 0; z < 4; z++) |
| 63 | { |
| 64 | uint8_t tmp = base[z]; |
| 65 | |
| 66 | base[z] = base[7 - z]; |
| 67 | base[7 - z] = tmp; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void Endian_A16_NE_to_LE(void *src, uint32_t nelements) |
| 73 | { |
| 74 | #ifdef MSB_FIRST |
| 75 | Endian_A16_Swap(src, nelements); |
| 76 | #endif |
| 77 | } |
| 78 | |
| 79 | void Endian_A32_NE_to_LE(void *src, uint32_t nelements) |
| 80 | { |
| 81 | #ifdef MSB_FIRST |
| 82 | Endian_A32_Swap(src, nelements); |
| 83 | #endif |
| 84 | } |
| 85 | |
| 86 | void Endian_A64_NE_to_LE(void *src, uint32_t nelements) |
| 87 | { |
| 88 | #ifdef MSB_FIRST |
| 89 | Endian_A64_Swap(src, nelements); |
| 90 | #endif |
| 91 | } |
| 92 | |
| 93 | |
| 94 | void Endian_A16_LE_to_NE(void *src, uint32_t nelements) |
| 95 | { |
| 96 | #ifdef MSB_FIRST |
| 97 | uint32_t i; |
| 98 | uint8_t *nsrc = (uint8_t *)src; |
| 99 | |
| 100 | for(i = 0; i < nelements; i++) |
| 101 | { |
| 102 | uint8_t tmp = nsrc[i * 2]; |
| 103 | |
| 104 | nsrc[i * 2] = nsrc[i * 2 + 1]; |
| 105 | nsrc[i * 2 + 1] = tmp; |
| 106 | } |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | void Endian_A16_BE_to_NE(void *src, uint32_t nelements) |
| 111 | { |
| 112 | #ifndef MSB_FIRST |
| 113 | uint32_t i; |
| 114 | uint8_t *nsrc = (uint8_t *)src; |
| 115 | |
| 116 | for(i = 0; i < nelements; i++) |
| 117 | { |
| 118 | uint8_t tmp = nsrc[i * 2]; |
| 119 | |
| 120 | nsrc[i * 2] = nsrc[i * 2 + 1]; |
| 121 | nsrc[i * 2 + 1] = tmp; |
| 122 | } |
| 123 | #endif |
| 124 | } |
| 125 | |
| 126 | |
| 127 | void Endian_A32_LE_to_NE(void *src, uint32_t nelements) |
| 128 | { |
| 129 | #ifdef MSB_FIRST |
| 130 | uint32_t i; |
| 131 | uint8_t *nsrc = (uint8_t *)src; |
| 132 | |
| 133 | for(i = 0; i < nelements; i++) |
| 134 | { |
| 135 | uint8_t tmp1 = nsrc[i * 4]; |
| 136 | uint8_t tmp2 = nsrc[i * 4 + 1]; |
| 137 | |
| 138 | nsrc[i * 4] = nsrc[i * 4 + 3]; |
| 139 | nsrc[i * 4 + 1] = nsrc[i * 4 + 2]; |
| 140 | |
| 141 | nsrc[i * 4 + 2] = tmp2; |
| 142 | nsrc[i * 4 + 3] = tmp1; |
| 143 | } |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | void Endian_A64_LE_to_NE(void *src, uint32_t nelements) |
| 148 | { |
| 149 | #ifdef MSB_FIRST |
| 150 | uint32_t i; |
| 151 | uint8_t *nsrc = (uint8_t *)src; |
| 152 | |
| 153 | for(i = 0; i < nelements; i++) |
| 154 | { |
| 155 | unsigned z; |
| 156 | uint8_t *base = &nsrc[i * 8]; |
| 157 | |
| 158 | for(z = 0; z < 4; z++) |
| 159 | { |
| 160 | uint8_t tmp = base[z]; |
| 161 | |
| 162 | base[z] = base[7 - z]; |
| 163 | base[7 - z] = tmp; |
| 164 | } |
| 165 | } |
| 166 | #endif |
| 167 | } |
| 168 | |
| 169 | void FlipByteOrder(uint8_t *src, uint32_t count) |
| 170 | { |
| 171 | uint8_t *start=src; |
| 172 | uint8_t *end=src+count-1; |
| 173 | |
| 174 | if((count&1) || !count) return; /* This shouldn't happen. */ |
| 175 | |
| 176 | count >>= 1; |
| 177 | |
| 178 | while(count--) |
| 179 | { |
| 180 | uint8_t tmp; |
| 181 | |
| 182 | tmp=*end; |
| 183 | *end=*start; |
| 184 | *start=tmp; |
| 185 | end--; |
| 186 | start++; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | void Endian_V_LE_to_NE(void *src, uint32_t bytesize) |
| 191 | { |
| 192 | #ifdef MSB_FIRST |
| 193 | FlipByteOrder((uint8_t *)src, bytesize); |
| 194 | #endif |
| 195 | } |
| 196 | |
| 197 | void Endian_V_NE_to_LE(void *src, uint32_t bytesize) |
| 198 | { |
| 199 | #ifdef MSB_FIRST |
| 200 | FlipByteOrder((uint8_t *)src, bytesize); |
| 201 | #endif |
| 202 | } |
| 203 | |
| 204 | int write16le(uint16_t b, FILE *fp) |
| 205 | { |
| 206 | uint8_t s[2]; |
| 207 | s[0]=b; |
| 208 | s[1]=b>>8; |
| 209 | return((fwrite(s,1,2,fp)<2)?0:2); |
| 210 | } |
| 211 | |
| 212 | int write32le(uint32_t b, FILE *fp) |
| 213 | { |
| 214 | uint8_t s[4]; |
| 215 | s[0]=b; |
| 216 | s[1]=b>>8; |
| 217 | s[2]=b>>16; |
| 218 | s[3]=b>>24; |
| 219 | return((fwrite(s,1,4,fp)<4)?0:4); |
| 220 | } |
| 221 | |
| 222 | int read32le(uint32_t *Bufo, FILE *fp) |
| 223 | { |
| 224 | uint32_t buf; |
| 225 | if(fread(&buf,1,4,fp)<4) |
| 226 | return 0; |
| 227 | #ifdef MSB_FIRST |
| 228 | *(uint32_t*)Bufo=((buf&0xFF)<<24)|((buf&0xFF00)<<8)|((buf&0xFF0000)>>8)|((buf&0xFF000000)>>24); |
| 229 | #else |
| 230 | *(uint32_t*)Bufo=buf; |
| 231 | #endif |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | int read16le(char *d, FILE *fp) |
| 236 | { |
| 237 | #ifdef MSB_FIRST |
| 238 | int ret; |
| 239 | ret=fread(d+1,1,1,fp); |
| 240 | ret+=fread(d,1,1,fp); |
| 241 | return ret<2?0:2; |
| 242 | #else |
| 243 | return((fread(d,1,2,fp)<2)?0:2); |
| 244 | #endif |
| 245 | } |
| 246 |