annotate trau-decode/parse-main.c @ 35:0979407719f0

doc/TFO-xform/HRv1: article written
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 02 Sep 2024 07:32:09 +0000
parents 4d1732e4a143
children d7674c80426c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This program reads a 64 kbit/s timeslot recording file, examines one
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * of the four 16 kbit/s subslots (selected), looks for GSM 08.60 TRAU
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * frames, and dumps whatever it finds.
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <stdint.h>
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdlib.h>
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <string.h>
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <strings.h>
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 static unsigned file_offset;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 static enum {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 HUNT_FOR_0,
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 HUNT_FOUND_0,
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 GOT_8_ZEROS,
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 SYNCED
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 } state;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 static unsigned hunt_for_0_count, frame_pos_count;
2
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
22 static uint8_t in_frame_2bit[160], frame_bits[320], d_bits[260];
0
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 static void
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 unpack_dibits()
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 int i, inb;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 uint8_t *op;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 op = frame_bits;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 for (i = 0; i < 160; i++) {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 inb = in_frame_2bit[i];
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (inb & 2)
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 *op++ = 1;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 else
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 *op++ = 0;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 if (inb & 1)
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 *op++ = 1;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 else
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 *op++ = 0;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 static unsigned
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 bits_to_num(bits, nbits)
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 uint8_t *bits;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 unsigned nbits;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 unsigned accum;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 unsigned n;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 accum = 0;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 for (n = 0; n < nbits; n++) {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 accum <<= 1;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 if (*bits)
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 accum |= 1;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 bits++;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 return accum;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 static void
2
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
63 collect_d_bits()
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
64 {
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
65 bcopy(frame_bits + 33, d_bits, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
66 bcopy(frame_bits + 49, d_bits + 15, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
67 bcopy(frame_bits + 65, d_bits + 30, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
68 bcopy(frame_bits + 81, d_bits + 45, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
69 bcopy(frame_bits + 97, d_bits + 60, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
70 bcopy(frame_bits + 113, d_bits + 75, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
71 bcopy(frame_bits + 129, d_bits + 90, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
72 bcopy(frame_bits + 145, d_bits + 105, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
73 bcopy(frame_bits + 161, d_bits + 120, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
74 bcopy(frame_bits + 177, d_bits + 135, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
75 bcopy(frame_bits + 193, d_bits + 150, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
76 bcopy(frame_bits + 209, d_bits + 165, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
77 bcopy(frame_bits + 225, d_bits + 180, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
78 bcopy(frame_bits + 241, d_bits + 195, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
79 bcopy(frame_bits + 257, d_bits + 210, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
80 bcopy(frame_bits + 273, d_bits + 225, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
81 bcopy(frame_bits + 289, d_bits + 240, 15);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
82 bcopy(frame_bits + 305, d_bits + 255, 5);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
83 }
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
84
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
85 static void
0
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 process_frame()
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 unsigned c1_5, c6_11;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89
10
4d1732e4a143 trau-parse: frame offsets were reported off by one byte
Mychaela Falconia <falcon@freecalypso.org>
parents: 5
diff changeset
90 printf("Frame at 0x%x:\n", file_offset - 159);
0
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 printf(" C1-C5: %u%u%u%u%u", frame_bits[17], frame_bits[18],
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 frame_bits[19], frame_bits[20], frame_bits[21]);
1
b0dcd48a1c8a trau-parse: initial bugfixes
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
93 c1_5 = bits_to_num(frame_bits + 17, 5);
0
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 switch (c1_5) {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 case 0x02:
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 fputs(" (FR UL)", stdout);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 break;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 case 0x1C:
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 fputs(" (FR DL)", stdout);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 break;
1
b0dcd48a1c8a trau-parse: initial bugfixes
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
101 case 0x1A:
0
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 fputs(" (EFR)", stdout);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 break;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 case 0x10:
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 fputs(" (idle UL)", stdout);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 break;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 case 0x0E:
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 fputs(" (idle DL)", stdout);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 break;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 putchar('\n');
1
b0dcd48a1c8a trau-parse: initial bugfixes
Mychaela Falconia <falcon@freecalypso.org>
parents: 0
diff changeset
112 c6_11 = bits_to_num(frame_bits + 22, 6);
0
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 printf(" C6-C11: %u\n", c6_11);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 printf(" C12=%u C13=%u C14=%u C15=%u\n", frame_bits[28],
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 frame_bits[29], frame_bits[30], frame_bits[31]);
2
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
116 switch (c1_5) {
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
117 case 0x02:
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
118 case 0x1C:
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
119 collect_d_bits();
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
120 print_fr_frame(d_bits);
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
121 break;
4
d9c095357c32 trau-parse: check and report EFR CRC
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
122 case 0x1A:
d9c095357c32 trau-parse: check and report EFR CRC
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
123 collect_d_bits();
d9c095357c32 trau-parse: check and report EFR CRC
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
124 check_efr_crc(d_bits);
5
bf5c9fb431b8 trau-parse: implement EFR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 4
diff changeset
125 print_efr_frame(d_bits);
4
d9c095357c32 trau-parse: check and report EFR CRC
Mychaela Falconia <falcon@freecalypso.org>
parents: 2
diff changeset
126 break;
2
b2ef2c80fef1 trau-parse: add FR decoding
Mychaela Falconia <falcon@freecalypso.org>
parents: 1
diff changeset
127 }
0
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 printf(" C16=%u C17=%u C18=%u C19=%u C20=%u C21=%u\n",
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 frame_bits[310], frame_bits[311], frame_bits[312],
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 frame_bits[313], frame_bits[314], frame_bits[315]);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 printf(" T1=%u T2=%u T3=%u T4=%u\n", frame_bits[316], frame_bits[317],
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 frame_bits[318], frame_bits[319]);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 static int
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 check_sync_zeros()
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 int i;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 for (i = 0; i < 16; i++) {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 if (frame_bits[i])
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 return 0;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 return 1;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 static int
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 check_sync_ones()
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 int i;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152 for (i = 1; i < 20; i++) {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 if (!frame_bits[i*16])
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154 return 0;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156 return 1;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
157 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
158
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 static void
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 check_sync()
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161 {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 if (check_sync_zeros() && check_sync_ones())
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163 return;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 printf("Bad frame sync, returning to hunt state\n");
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 state = HUNT_FOR_0;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
167
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 static void
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169 byte_input(inb)
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 switch (state) {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 case HUNT_FOR_0:
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
173 if (inb != 0)
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 return;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 state = HUNT_FOUND_0;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 hunt_for_0_count = 1;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 return;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 case HUNT_FOUND_0:
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 if (inb != 0) {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 state = HUNT_FOR_0;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 return;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 hunt_for_0_count++;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 if (hunt_for_0_count >= 8)
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185 state = GOT_8_ZEROS;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 return;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
187 case GOT_8_ZEROS:
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 if (inb & 2) {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 printf("Found frame sync at file offset 0x%x\n",
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 file_offset - 8);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 bzero(in_frame_2bit, 8);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192 in_frame_2bit[8] = inb;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193 frame_pos_count = 9;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194 state = SYNCED;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195 return;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
196 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
197 if (inb != 0)
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
198 state = HUNT_FOR_0;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
199 return;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
200 case SYNCED:
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
201 in_frame_2bit[frame_pos_count++] = inb;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
202 if (frame_pos_count < 160)
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
203 return;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
204 frame_pos_count = 0;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
205 unpack_dibits();
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
206 process_frame();
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
207 check_sync();
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
208 return;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
209 default:
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
210 fprintf(stderr, "BUG: bad sync state\n");
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211 abort();
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
212 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
213 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
214
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
215 main(argc, argv)
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
216 char **argv;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
217 {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
218 FILE *inf;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
219 int subslot, right_shift;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
220 int inb;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
221
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
222 if (argc != 3) {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
223 fprintf(stderr, "usage: %s binfile subslot\n", argv[0]);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
224 exit(1);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
225 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
226 inf = fopen(argv[1], "r");
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
227 if (!inf) {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
228 perror(argv[1]);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
229 exit(1);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
230 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
231 subslot = atoi(argv[2]);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
232 if (subslot < 0 || subslot > 3) {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
233 fprintf(stderr, "error: invalid subslot argument\n");
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
234 exit(1);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
235 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
236 right_shift = (3 - subslot) * 2;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
237 state = HUNT_FOR_0;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
238 for (file_offset = 0; ; file_offset++) {
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
239 inb = getc(inf);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
240 if (inb < 0)
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
241 break;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
242 inb >>= right_shift;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
243 inb &= 3;
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
244 byte_input(inb);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
245 }
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
246 exit(0);
131e0f1972bb beginning of trau-parse program
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
247 }