cleanup init code
[megadrive.git] / mx / linux / sms2mx.c
CommitLineData
262a16f4 1#include <stdio.h>
2#include <string.h>
3
43c53abb 4#define IN_SIZE 0x8000
5#define OUT_SIZE 0x400000
6static unsigned char buff[OUT_SIZE], buff_in[IN_SIZE];
262a16f4 7
8int main(int argc, char *argv[])
9{
10 FILE *fi, *fo;
11 int size, bsize;
12 int i, o;
13
14 if (argc != 3) {
15 fprintf(stderr, "usage:\n%s <sms ROM> <mx img>\n", argv[0]);
16 return 1;
17 }
18
19 fi = fopen(argv[1], "rb");
20 if (fi == NULL) {
21 fprintf(stderr, "can't open: %s\n", argv[1]);
22 return 1;
23 }
24
25 fo = fopen(argv[2], "wb");
26 if (fo == NULL) {
27 fprintf(stderr, "can't open: %s\n", argv[2]);
28 return 1;
29 }
30
31 fseek(fi, 0, SEEK_END);
32 size = ftell(fi);
33 fseek(fi, 0, SEEK_SET);
34
35 if (size > IN_SIZE) {
36 fprintf(stderr, "ROMs > 32k won't work\n");
37 size = IN_SIZE;
38 }
39
40 if (fread(buff_in, 1, size, fi) != size)
41 fprintf(stderr, "failed to read %s\n", argv[1]);
42
43 memset(buff, 0, sizeof(buff));
44
45 for (bsize = 1; bsize < size; bsize <<= 1)
46 ;
47
43c53abb 48 for (i = o = 0; o < sizeof(buff); i = (i + 1) & (bsize - 1), o += 2)
49 buff[o+1] = buff_in[i];
262a16f4 50
43c53abb 51 if (fwrite(buff, 1, OUT_SIZE, fo) != OUT_SIZE)
262a16f4 52 fprintf(stderr, "failed to write to %s\n", argv[2]);
53 fclose(fi);
54 fclose(fo);
55
56 return 0;
57}
58