1 |
#include <stdio.h> |
2 |
#include <stdlib.h> |
3 |
|
4 |
int sync_ok=0; |
5 |
int offset; |
6 |
FILE *in, *out; |
7 |
|
8 |
void synchronize(void); |
9 |
int getbyte(void); |
10 |
|
11 |
int main(int argc,char **argv) |
12 |
{ |
13 |
printf(".4k8 -> .tap 1.0\n"); |
14 |
if (argc!=3) { printf("Usage: %s file.4k8 file.tap\n",argv[0]); exit(1);} |
15 |
in=fopen(argv[1],"rb"); out=fopen(argv[2],"wb"); |
16 |
if (in==NULL || out==NULL) { printf("Unable to open file\n"); exit(1);} |
17 |
|
18 |
synchronize(); |
19 |
sync_ok=1; |
20 |
while (!feof(in)) putc(getbyte(),out); |
21 |
fclose(in); fclose(out); |
22 |
exit(0); |
23 |
} |
24 |
|
25 |
|
26 |
int getsample() |
27 |
{ |
28 |
static int shifter, shiftcount=0; |
29 |
if (shiftcount==0) { |
30 |
shiftcount=8; |
31 |
if (feof(in)) shifter=0x55; |
32 |
else shifter=getc(in); |
33 |
} |
34 |
shiftcount--; |
35 |
shifter<<=1; |
36 |
return (shifter>>8) & 1; |
37 |
} |
38 |
|
39 |
int getbit() |
40 |
{ |
41 |
int length=0; |
42 |
do length++; while (getsample()==1); |
43 |
do length++; while (getsample()==0); |
44 |
return length<3 ? 1 : 0; |
45 |
} |
46 |
|
47 |
int getbyte(void) |
48 |
{ |
49 |
int decaleur=0,byte=0,i,bit,sum=0; |
50 |
getbit(); |
51 |
while(getbit()==1); |
52 |
for(i=0;i<8;i++) decaleur=(decaleur<<1)|getbit(); |
53 |
for(i=0;i<8;i++) { |
54 |
bit=decaleur&1; |
55 |
decaleur=decaleur>>1; |
56 |
byte=(byte<<1)|bit; |
57 |
sum+=bit; |
58 |
} |
59 |
if ((sum&1)==getbit() && sync_ok) printf("parity error at offset $%x\n",offset); |
60 |
return byte; |
61 |
} |
62 |
|
63 |
void synchronize(void) |
64 |
{ |
65 |
int decaleur=0,val; |
66 |
printf("Searching synchro...\n"); |
67 |
while(1) { |
68 |
while((decaleur&0xff) != 0x68) |
69 |
decaleur=(decaleur<<1)|getbit(); |
70 |
if (getbyte()!=0x16) continue; |
71 |
if (getbyte()!=0x16) continue; |
72 |
if (getbyte()!=0x16) continue; |
73 |
do { |
74 |
val=getbyte(); |
75 |
} while (val==0x16); |
76 |
if (val==0x24) break; |
77 |
} |
78 |
printf("Synchro found, byte coding...\n"); |
79 |
putc(0x16,out); putc(0x16,out); putc(0x16,out); putc(0x24,out); |
80 |
} |
81 |
|