1 %option prefix="readmem"
2 %option nounput
3 %option noinput
4
5 %{
6 /*
7 * Copyright (c) 1999-2017,2019 Stephen Williams (steve@icarus.com)
8 *
9 * This source code is free software; you can redistribute it
10 * and/or modify it in source code form under the terms of the GNU
11 * General Public License as published by the Free Software
12 * Foundation; either version 2 of the License, or (at your option)
13 * any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 */
24
25 # include "sys_readmem_lex.h"
26 # include <string.h>
27 static void make_addr(void);
28 static void make_hex_value(void);
29 static void make_bin_value(void);
30
31 static int save_state;
32
33 char *readmem_error_token = 0;
34
35 %}
36
37 %x BIN
38 %x HEX
39 %x CCOMMENT
40
41 %option noyywrap
42 %%
43
44 <HEX,BIN>"//".* { ; }
45 <HEX,BIN>[ \t\f\n\r] { ; }
46
47 <HEX,BIN>@[0-9a-fA-F]+ { make_addr(); return MEM_ADDRESS; }
48 <HEX>[0-9a-fA-FxXzZ_]+ { make_hex_value(); return MEM_WORD; }
49 <BIN>[01xXzZ_]+ { make_bin_value(); return MEM_WORD; }
50
51 <HEX,BIN>"/*" { save_state = YY_START; BEGIN(CCOMMENT); }
52 <CCOMMENT>[^*]* { ; }
53 <CCOMMENT>"*" { ; }
54 <CCOMMENT>"*"+"/" { BEGIN(save_state); }
55
56 /* Catch any invalid tokens and flagged them as an error. */
57 <HEX,BIN>. { readmem_error_token = yytext; return MEM_ERROR; }
58
59 %%
60 /* The call_handle is the handle for the call to the $readmem()
61 system task. This is used for adding line numbers to warnings and
62 error messages. */
63 static vpiHandle call_handle = 0;
64 static int too_many_digits_warning = 0;
65
66 static unsigned word_width = 0;
67 static struct t_vpi_vecval*vecval = 0;
68
make_addr(void)69 static void make_addr(void)
70 {
71 sscanf(yytext+1, "%x", (unsigned int*)&vecval->aval);
72 }
73
make_hex_value(void)74 static void make_hex_value(void)
75 {
76 char*beg = yytext;
77 char*end = beg + strlen(beg);
78 struct t_vpi_vecval*cur;
79 int idx;
80 int width = 0, word_max = word_width;
81
82 for (idx = 0, cur = vecval ; idx < word_max ; idx += 32, cur += 1) {
83 cur->aval = 0;
84 cur->bval = 0;
85 }
86
87 cur = vecval;
88 while ((width < word_max) && (end > beg)) {
89 int aval = 0;
90 int bval = 0;
91
92 end -= 1;
93 if (*end == '_') continue;
94 switch (*end) {
95 case '0':
96 case '1':
97 case '2':
98 case '3':
99 case '4':
100 case '5':
101 case '6':
102 case '7':
103 case '8':
104 case '9':
105 aval = *end - '0';
106 break;
107 case 'a':
108 case 'b':
109 case 'c':
110 case 'd':
111 case 'e':
112 case 'f':
113 aval = *end - 'a' + 10;
114 break;
115 case 'A':
116 case 'B':
117 case 'C':
118 case 'D':
119 case 'E':
120 case 'F':
121 aval = *end - 'A' + 10;
122 break;
123 case 'x':
124 case 'X':
125 aval = 15;
126 bval = 15;
127 break;
128 case 'z':
129 case 'Z':
130 bval = 15;
131 break;
132 }
133
134 cur->aval |= aval << width;
135 cur->bval |= bval << width;
136 width += 4;
137 if (width == 32) {
138 cur += 1;
139 width = 0;
140 word_max -= 32;
141 }
142 }
143
144 /* If there are more text digits then needed to fill the
145 memory word, count those digits and print a warning
146 message. Print that warning only once per call to
147 $readmemh() so that the user isn't flooded. */
148 int count_extra_digits = 0;
149 while (end > beg) {
150 end -= 1;
151 if (*end == '_') continue;
152 count_extra_digits += 1;
153 }
154
155 if (count_extra_digits && too_many_digits_warning==0) {
156 vpi_printf("WARNING: %s:%d: Excess hex digits (%d of '%s') while reading %d-bit words.\n",
157 vpi_get_str(vpiFile, call_handle),
158 vpi_get(vpiLineNo, call_handle),
159 count_extra_digits, beg,
160 word_width);
161 too_many_digits_warning += 1;
162 }
163 }
164
make_bin_value(void)165 static void make_bin_value(void)
166 {
167 char*beg = yytext;
168 char*end = beg + strlen(beg);
169 struct t_vpi_vecval*cur;
170 int idx;
171 int width = 0, word_max = word_width;
172
173 for (idx = 0, cur = vecval ; idx < word_max ; idx += 32, cur += 1) {
174 cur->aval = 0;
175 cur->bval = 0;
176 }
177
178 cur = vecval;
179 while ((width < word_max) && (end > beg)) {
180 int aval = 0;
181 int bval = 0;
182
183 end -= 1;
184 if (*end == '_') continue;
185 switch (*end) {
186 case '0':
187 case '1':
188 aval = *end - '0';
189 break;
190 case 'x':
191 case 'X':
192 aval = 1;
193 bval = 1;
194 break;
195 case 'z':
196 case 'Z':
197 bval = 1;
198 break;
199 }
200
201 cur->aval |= aval << width;
202 cur->bval |= bval << width;
203 width += 1;
204 if (width == 32) {
205 cur += 1;
206 width = 0;
207 word_max -= 32;
208 }
209 }
210
211 /* If there are more text digits then needed to fill the
212 memory word, count those digits and print a warning
213 message. Print that warning only once per call to
214 $readmem() so that the user isn't flooded. */
215 int count_extra_digits = 0;
216 while (end > beg) {
217 end -= 1;
218 if (*end == '_') continue;
219 count_extra_digits += 1;
220 }
221
222 if (count_extra_digits && too_many_digits_warning==0) {
223 vpi_printf("WARNING: %s:%d: Excess binary digits (%d of '%s') while reading %d-bit words.\n",
224 vpi_get_str(vpiFile, call_handle),
225 vpi_get(vpiLineNo, call_handle),
226 count_extra_digits, beg,
227 word_width);
228 too_many_digits_warning += 1;
229 }
230 }
231
sys_readmem_start_file(vpiHandle callh,FILE * in,int bin_flag,unsigned width,struct t_vpi_vecval * vv)232 void sys_readmem_start_file(vpiHandle callh, FILE*in, int bin_flag,
233 unsigned width, struct t_vpi_vecval *vv)
234 {
235 call_handle = callh;
236 too_many_digits_warning = 0;
237 yyrestart(in);
238 BEGIN(bin_flag? BIN : HEX);
239 word_width = width;
240 vecval = vv;
241 }
242
243 /*
244 * Modern version of flex (>=2.5.9) can clean up the scanner data.
245 */
destroy_readmem_lexor(void)246 void destroy_readmem_lexor(void)
247 {
248 # ifdef FLEX_SCANNER
249 # if YY_FLEX_MAJOR_VERSION >= 2 && YY_FLEX_MINOR_VERSION >= 5
250 # if YY_FLEX_MINOR_VERSION > 5 || defined(YY_FLEX_SUBMINOR_VERSION) && YY_FLEX_SUBMINOR_VERSION >= 9
251 yylex_destroy();
252 # endif
253 # endif
254 # endif
255 }
256