-
Notifications
You must be signed in to change notification settings - Fork 4
/
parser.y
219 lines (186 loc) · 4.64 KB
/
parser.y
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* ArduLogic - Low Speed Logic Analyzer using the Arduino Hardware
*
* Copyright (C) 2011 Clifford Wolf <clifford@clifford.at>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
%{
#include "ardulogic.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <list>
extern int yylex(void);
extern int yyget_lineno(void);
void check_decode(int v) {
if (decode != 0 && decode != v) {
fprintf(stderr, "Config error in line %d: Conflicting decode and/or trigger statements\n", yyget_lineno());
exit(1);
}
}
void yyerror (char const *s) {
fprintf(stderr, "Parser error in line %d: %s\n", yyget_lineno(), s);
exit(1);
}
%}
%union {
int num;
char *str;
}
%token <num> TOK_PIN
%token <num> TOK_FREQ
%token <str> TOK_STRING
%token TOK_TRIGGER TOK_POSEDGE TOK_NEGEDGE
%token TOK_DECODE TOK_SPI TOK_I2C TOK_JTAG
%token TOK_CAPTURE TOK_PULLUP TOK_LABEL TOK_EOL
%token TOK_MSB TOK_LSB
%type <num> edge neg msb_notlsb
%%
config:
/* empty */ |
config TOK_EOL |
config stmt TOK_EOL;
stmt:
stmt_trigger | stmt_capture | stmt_pullup | stmt_decode | stmt_label;
stmt_trigger:
TOK_TRIGGER edge TOK_PIN {
check_decode(DECODE_TRIGGER);
pins[$3] |= $2;
decode = DECODE_TRIGGER;
} |
TOK_TRIGGER TOK_FREQ {
check_decode(0);
trigger_freq = $2;
decode = DECODE_FREQ;
};
stmt_capture:
TOK_CAPTURE capture_list;
capture_list:
capture_list TOK_PIN {
pins[$2] |= PIN_CAPTURE;
} |
TOK_PIN {
pins[$1] |= PIN_CAPTURE;
};
stmt_pullup:
TOK_PULLUP pullup_list;
pullup_list:
pullup_list TOK_PIN {
pins[$2] |= PIN_PULLUP;
} |
TOK_PIN {
pins[$1] |= PIN_PULLUP;
};
stmt_decode:
TOK_DECODE TOK_SPI edge msb_notlsb TOK_PIN neg TOK_PIN TOK_PIN TOK_PIN {
check_decode(0);
decode_config[CFG_SPI_MSB] = $4;
decode_config[CFG_SPI_CSNEG] = $6;
decode_config[CFG_SPI_CS] = $7;
pins[$5] |= $3; // SCK
pins[$7] |= PIN_CAPTURE | PIN_TRIGGER_POSEDGE | PIN_TRIGGER_NEGEDGE; // CS
pins[$8] |= PIN_CAPTURE; // MOSI
pins[$9] |= PIN_CAPTURE; // MISO
pin_names[$5] = "SCK";
pin_names[$7] = "CS";
pin_names[$8] = "MOSI";
pin_names[$9] = "MISO";
decode = DECODE_SPI;
} |
TOK_DECODE TOK_I2C TOK_PIN TOK_PIN {
check_decode(0);
decode_config[CFG_I2C_SCL] = $3;
decode_config[CFG_I2C_SDA] = $4;
pins[$3] |= PIN_CAPTURE | PIN_TRIGGER_POSEDGE | PIN_TRIGGER_NEGEDGE; // SCL
pins[$4] |= PIN_CAPTURE | PIN_TRIGGER_POSEDGE | PIN_TRIGGER_NEGEDGE; // SDA
pin_names[$3] = "SCL";
pin_names[$4] = "SDA";
decode = DECODE_I2C;
} |
TOK_DECODE TOK_JTAG TOK_PIN TOK_PIN TOK_PIN TOK_PIN {
check_decode(0);
decode_config[CFG_JTAG_TMS] = $4;
decode_config[CFG_JTAG_TDI] = $5;
decode_config[CFG_JTAG_TDO] = $6;
pins[$3] |= PIN_TRIGGER_POSEDGE; // TCK
pins[$4] |= PIN_CAPTURE; // TMS
pins[$5] |= PIN_CAPTURE; // TDI
pins[$6] |= PIN_CAPTURE; // TDO
pin_names[$3] = "TCK";
pin_names[$4] = "TMS";
pin_names[$5] = "TDI";
pin_names[$6] = "TDO";
decode = DECODE_JTAG;
};
stmt_label:
TOK_LABEL TOK_PIN TOK_STRING {
pin_names[$2] = $3;
};
edge:
TOK_POSEDGE {
$$ = PIN_TRIGGER_POSEDGE;
} |
TOK_NEGEDGE {
$$ = PIN_TRIGGER_NEGEDGE;
};
neg:
'!' {
$$ = 1;
} |
/* empty */ {
$$ = 0;
};
msb_notlsb:
TOK_MSB {
$$ = 1;
} |
TOK_LSB {
$$ = 0;
};
%%
extern FILE *yyin;
void config(const char *file)
{
decode = 0;
memset(decode_config, 0, sizeof(decode_config));
memset(pins, 0, sizeof(pins));
yyin = fopen(file, "r");
if (yyin == NULL) {
fprintf(stderr, "Can't open config file `%s': %s\n", file, strerror(errno));
exit(1);
}
yyparse();
fclose(yyin);
printf("Capture configuration:");
for (int i = 0; i < TOTAL_PIN_NUM; i++) {
if (pins[i] == 0)
continue;
printf(" %s:", pin_names[i]);
if ((pins[i] & PIN_CAPTURE) != 0)
printf("c");
if ((pins[i] & PIN_TRIGGER_POSEDGE) != 0)
printf("p");
if ((pins[i] & PIN_TRIGGER_NEGEDGE) != 0)
printf("n");
}
printf("\n");
printf("Decode configuration: %d", decode);
for (int i = 0; i < CFG_WORDS; i++)
printf(" %d", decode_config[i]);
printf("\n");
}