-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunswap.c
54 lines (42 loc) · 1.23 KB
/
unswap.c
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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
// simple program to swap the MSB and LSB of the instruction words
// if you wish to use the ROMs as a sample
int main()
{
FILE *srcptr, *dstptr;
srcptr = fopen("1771rom.bin","rb");
if (srcptr == NULL)
{
fprintf(stderr,"Error Opening ROM Binary\n");
return EXIT_FAILURE;
}
dstptr = fopen("1771rom_unswapped.bin","w");
if (srcptr == NULL)
{
fprintf(stderr,"Error Creating File\n");
return EXIT_FAILURE;
}
uint16_t rawbin[512];
int dsize, idx = 0;
int lpcnt, cidx;
unsigned char outchar;
int tmp_n, tmp_reg;
uint16_t opcode;
int16_t oplow, ophigh;
rewind(srcptr);
fread(&rawbin, sizeof(rawbin), 1, srcptr);
for(lpcnt = 512; lpcnt > 0; lpcnt--, idx = idx+1)
{
opcode = rawbin[idx];
opcode = ( (((opcode >> 8) & 0x00FF)) | ((opcode << 8) & 0xFF00) );
opcode = (opcode & 0x8000)?opcode:opcode^0xFF00+0x8000;
opcode = (opcode & 0x0080)?opcode:opcode^0x00FF+0x0080;
fwrite(&opcode, sizeof(opcode), 1, dstptr);
}
fclose(srcptr);
fclose(dstptr);
printf("Byte Swap successful!\n");
return EXIT_SUCCESS;
}