vparse.c
4.53 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <string.h>
#include <strings.h>
char *InFileName=NULL, *OutputBaseName=NULL;
char OutputFileName[80];
FILE *InFile, *OutputFile;
char InData[80] = "foo";
int StartingFile = 0;
int FieldsPerFile = 1;
int CurrentLine = 0;
char *ReadOk = "foo";
unsigned int vbus_data, vbus_sync;
void usage(void)
{
fprintf(stderr, "usage: vparse -i foo.tab -o bar.tab [-s n] [-f n] \n");
fprintf(stderr, " outputs will be f fields per file \n");
fprintf(stderr, " in files named: bar.tab{s,s+1,s+2,...} \n");
exit(1);
}
void GetData(void)
{
ReadOk = fgets(InData, 80, InFile);
CurrentLine++;
while (InData[0] == '#')
{
ReadOk = fgets(InData, 80, InFile);
CurrentLine++;
}
sscanf(InData, "%x %d", &vbus_data, &vbus_sync);
}
void PrintHeaderInfo(void)
{
fprintf(OutputFile, "# \n");
fprintf(OutputFile, "# Created by vparse.c from input %s \n", OutputFileName);
fprintf(OutputFile, "# \n");
fprintf(OutputFile, "vclk @C 1(8) 0(8) \n");
fprintf(OutputFile, "vbus_data[6:0] @I @E 2 \n");
fprintf(OutputFile, "vbus_sync @I @E 2 \n");
fprintf(OutputFile, "\n");
fprintf(OutputFile, "0x00 0 \n");
fprintf(OutputFile, "0x0f 0 \n");
fprintf(OutputFile, "0x0f 0 \n");
}
void ReadAndProcessInFile(void)
{
int CurrentFile = StartingFile;
int ValidData = 0;
int FieldBegin = 0;
int VSyncFinished;
int FieldNumber;
if ((InFile = fopen(InFileName, "r")) == NULL)
{
fprintf(stderr, "Error, could not open input file %s \n", InFileName);
exit(1);
}
while (!ValidData)
{
GetData();
if (vbus_data == 0x0f)
{
unsigned int Next_data[3];
unsigned int Next_sync[3];
int i;
for (i=0; i<3; i++)
{
GetData();
Next_data[i] = vbus_data;
Next_sync[i] = vbus_sync;
}
if ((Next_data[0] == 0x00) && (Next_sync[0] == 1) &&
(Next_data[1] == 0x00) && (Next_sync[1] == 1) &&
(Next_data[2] == 0x00) && (Next_sync[2] == 1))
{
ValidData = 1;
/* fprintf(stderr, "Finished valid data sequence on line %d \n", CurrentLine); */
}
}
}
ValidData = 0;
while (!feof(InFile))
{
/* Loop until get vertical sync */
while (!FieldBegin && !feof(InFile))
{
GetData();
if ((!vbus_sync) && !(vbus_data & (1 << 3)))
FieldBegin = 1;
}
if (!feof(InFile))
{
/* fprintf(stderr, "Got vertical sync on line %d \n", CurrentLine); */
sprintf(OutputFileName, "%s%.3d", OutputBaseName, CurrentFile++);
fprintf(stderr, "Opening output file %s \n", OutputFileName);
if ((OutputFile = fopen(OutputFileName, "w")) == NULL)
{
fprintf(stderr, "Error, could not open outputfile file %s \n",
OutputFileName);
exit(1);
}
PrintHeaderInfo();
}
for (FieldNumber=0; FieldNumber < FieldsPerFile; FieldNumber++)
{
/* Loop until out of vertical sync */
FieldBegin = 0;
VSyncFinished = 0;
while (!VSyncFinished && !feof(InFile))
{
/* printf("0x%.2x %d\n", vbus_data, vbus_sync); */
fprintf(OutputFile, "0x%.2x %d\n", vbus_data, vbus_sync);
GetData();
if ((!vbus_sync) && (vbus_data & (1 << 3)))
VSyncFinished = 1;
}
/* if (!feof(InFile))
fprintf(stderr, "Finished vertical sync on line %d \n", CurrentLine); */
/* Now loop until get next vsync */
while (!FieldBegin && !feof(InFile))
{
GetData();
if ((!vbus_sync) && !(vbus_data & (1 << 3)))
FieldBegin = 1;
else
fprintf(OutputFile, "0x%.2x %d\n", vbus_data, vbus_sync);
}
if (!feof(InFile))
{
/* fprintf(stderr, "Finished field data on line %d \n", CurrentLine); */
}
}
if (!feof(InFile)) {
fclose(OutputFile);
printf("%s ", OutputFileName);
}
}
/* fprintf(stderr, "Finished file on line %d \n", CurrentLine); */
fclose(InFile);
printf("\n");
}
int main(int argc, char **argv)
{
int c;
extern char *optarg;
extern int optind;
while ((c = getopt(argc, argv, "i:o:s:f:")) != EOF)
switch (c)
{
case 'i':
InFileName = strdup(optarg);
break;
case 'o':
OutputBaseName = strdup(optarg);
break;
case 's':
StartingFile = atoi(optarg);
break;
case 'f':
FieldsPerFile = atoi(optarg);
break;
case '?':
usage();
break;
}
if (InFileName == NULL)
usage();
if (OutputBaseName == NULL)
usage();
ReadAndProcessInFile();
return(0);
}