| 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_LE_to_NE(void *src, uint32_t nelements) |
| 73 | { |
| 74 | #ifdef MSB_FIRST |
| 75 | uint32_t i; |
| 76 | uint8_t *nsrc = (uint8_t *)src; |
| 77 | |
| 78 | for(i = 0; i < nelements; i++) |
| 79 | { |
| 80 | uint8_t tmp = nsrc[i * 2]; |
| 81 | |
| 82 | nsrc[i * 2] = nsrc[i * 2 + 1]; |
| 83 | nsrc[i * 2 + 1] = tmp; |
| 84 | } |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | void Endian_A16_BE_to_NE(void *src, uint32_t nelements) |
| 89 | { |
| 90 | #ifndef MSB_FIRST |
| 91 | uint32_t i; |
| 92 | uint8_t *nsrc = (uint8_t *)src; |
| 93 | |
| 94 | for(i = 0; i < nelements; i++) |
| 95 | { |
| 96 | uint8_t tmp = nsrc[i * 2]; |
| 97 | |
| 98 | nsrc[i * 2] = nsrc[i * 2 + 1]; |
| 99 | nsrc[i * 2 + 1] = tmp; |
| 100 | } |
| 101 | #endif |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void Endian_A32_LE_to_NE(void *src, uint32_t nelements) |
| 106 | { |
| 107 | #ifdef MSB_FIRST |
| 108 | uint32_t i; |
| 109 | uint8_t *nsrc = (uint8_t *)src; |
| 110 | |
| 111 | for(i = 0; i < nelements; i++) |
| 112 | { |
| 113 | uint8_t tmp1 = nsrc[i * 4]; |
| 114 | uint8_t tmp2 = nsrc[i * 4 + 1]; |
| 115 | |
| 116 | nsrc[i * 4] = nsrc[i * 4 + 3]; |
| 117 | nsrc[i * 4 + 1] = nsrc[i * 4 + 2]; |
| 118 | |
| 119 | nsrc[i * 4 + 2] = tmp2; |
| 120 | nsrc[i * 4 + 3] = tmp1; |
| 121 | } |
| 122 | #endif |
| 123 | } |
| 124 | |
| 125 | void Endian_A64_LE_to_NE(void *src, uint32_t nelements) |
| 126 | { |
| 127 | #ifdef MSB_FIRST |
| 128 | uint32_t i; |
| 129 | uint8_t *nsrc = (uint8_t *)src; |
| 130 | |
| 131 | for(i = 0; i < nelements; i++) |
| 132 | { |
| 133 | unsigned z; |
| 134 | uint8_t *base = &nsrc[i * 8]; |
| 135 | |
| 136 | for(z = 0; z < 4; z++) |
| 137 | { |
| 138 | uint8_t tmp = base[z]; |
| 139 | |
| 140 | base[z] = base[7 - z]; |
| 141 | base[7 - z] = tmp; |
| 142 | } |
| 143 | } |
| 144 | #endif |
| 145 | } |
| 146 | |
| 147 | void FlipByteOrder(uint8_t *src, uint32_t count) |
| 148 | { |
| 149 | uint8_t *start=src; |
| 150 | uint8_t *end=src+count-1; |
| 151 | |
| 152 | if((count&1) || !count) |
| 153 | return; /* This shouldn't happen. */ |
| 154 | |
| 155 | count >>= 1; |
| 156 | |
| 157 | while(count--) |
| 158 | { |
| 159 | uint8_t tmp = *end; |
| 160 | |
| 161 | *end=*start; |
| 162 | *start=tmp; |
| 163 | end--; |
| 164 | start++; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | int write16le(uint16_t b, FILE *fp) |
| 169 | { |
| 170 | uint8_t s[2]; |
| 171 | s[0]=b; |
| 172 | s[1]=b>>8; |
| 173 | return((fwrite(s,1,2,fp)<2)?0:2); |
| 174 | } |
| 175 | |
| 176 | int write32le(uint32_t b, FILE *fp) |
| 177 | { |
| 178 | uint8_t s[4]; |
| 179 | s[0]=b; |
| 180 | s[1]=b>>8; |
| 181 | s[2]=b>>16; |
| 182 | s[3]=b>>24; |
| 183 | return((fwrite(s,1,4,fp)<4)?0:4); |
| 184 | } |
| 185 | |
| 186 | int read32le(uint32_t *Bufo, FILE *fp) |
| 187 | { |
| 188 | uint32_t buf; |
| 189 | if(fread(&buf,1,4,fp)<4) |
| 190 | return 0; |
| 191 | #ifdef MSB_FIRST |
| 192 | *(uint32_t*)Bufo=((buf&0xFF)<<24)|((buf&0xFF00)<<8)|((buf&0xFF0000)>>8)|((buf&0xFF000000)>>24); |
| 193 | #else |
| 194 | *(uint32_t*)Bufo=buf; |
| 195 | #endif |
| 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | int read16le(char *d, FILE *fp) |
| 200 | { |
| 201 | #ifdef MSB_FIRST |
| 202 | int ret; |
| 203 | ret=fread(d+1,1,1,fp); |
| 204 | ret+=fread(d,1,1,fp); |
| 205 | return ret<2?0:2; |
| 206 | #else |
| 207 | return((fread(d,1,2,fp)<2)?0:2); |
| 208 | #endif |
| 209 | } |
| 210 |