Previous 199869 Revisions Next

r33421 Monday 17th November, 2014 at 12:56:13 UTC by hap
cleanup
[src/lib/util]plaparse.c

trunk/src/lib/util/plaparse.c
r241932r241933
6161    character stream
6262-------------------------------------------------*/
6363
64static UINT32 suck_number(const UINT8 **cursrc, const UINT8 *srcend)
64static UINT32 suck_number(const UINT8 **src, const UINT8 *srcend)
6565{
6666   UINT32 value = 0;
67   (*cursrc)++;
6867   
6968   // find first digit
70   while (*cursrc < srcend && !iscrlf(**cursrc) && !isdigit(**cursrc))
71      (*cursrc)++;
72   if (*cursrc >= srcend)
73      return 0;
69   while (*src < srcend && !iscrlf(**src) && !isdigit(**src))
70      (*src)++;
7471
7572   // loop over and accumulate digits
76   while (isdigit(**cursrc))
73   while (*src < srcend && isdigit(**src))
7774   {
78      value = value * 10 + (**cursrc) - '0';
79      (*cursrc)++;
75      value = value * 10 + (**src) - '0';
76      (*src)++;
8077   }
8178
8279   return value;
r241932r241933
9289    process_terms - process input/output matrix
9390-------------------------------------------------*/
9491
95static bool process_terms(jed_data *data, const UINT8 **cursrc, const UINT8 *srcend, parse_info *pinfo)
92static bool process_terms(jed_data *data, const UINT8 **src, const UINT8 *srcend, parse_info *pinfo)
9693{
9794   UINT32 curinput = 0;
9895   UINT32 curoutput = 0;
9996   bool outputs = false;
97   
98   // symbols for 0, 1, dont_care, no_meaning
99   // PLA format documentation also describes them as simply 0, 1, 2, 3
100   static const char symbols[] = { "01-~" };
100101
101   while (*cursrc < srcend && **cursrc != '.' && **cursrc != '#')
102   while (*src < srcend && **src != '.' && **src != '#')
102103   {
103      switch (**cursrc)
104      if (!outputs)
104105      {
105         case '-':
106            if (!outputs)
107            {
108               curinput++;
106         // and-matrix
107         if (strrchr(symbols, **src))
108            curinput++;
109         
110         switch (**src)
111         {
112            case '0':
113               jed_set_fuse(data, data->numfuses++, 0);
109114               jed_set_fuse(data, data->numfuses++, 1);
115
116               if (LOG_PARSE) printf("01");
117               break;
118
119            case '1':
110120               jed_set_fuse(data, data->numfuses++, 1);
121               jed_set_fuse(data, data->numfuses++, 0);
111122
123               if (LOG_PARSE) printf("10");
124               break;
125
126            // anything goes
127            case '-':
128               jed_set_fuse(data, data->numfuses++, 1);
129               jed_set_fuse(data, data->numfuses++, 1);
130
112131               if (LOG_PARSE) printf("11");
113            }
114            break;
132               break;
115133
116         case '~':
117            if (!outputs)
118            {
119               curinput++;
120               // this product term is inhibited
134            // this product term is inhibited
135            case '~':
121136               jed_set_fuse(data, data->numfuses++, 0);
122137               jed_set_fuse(data, data->numfuses++, 0);
123138
124139               if (LOG_PARSE) printf("00");
125            }
126            break;
140               break;
127141
128         case '1':
129            if (outputs)
142            case ' ': case '\t':
143               if (curinput > 0)
144               {
145                  outputs = true;
146                  if (LOG_PARSE) printf(" ");
147               }
148               break;
149     
150            default:
151               break;
152         }
153      }
154      else
155      {
156         // or-matrix
157         if (strrchr(symbols, **src))
158         {
159            curoutput++;
160            if (**src == '1')
130161            {
131               curoutput++;
132162               jed_set_fuse(data, data->numfuses++, 0);
133
134163               if (LOG_PARSE) printf("0");
135164            }
136165            else
137166            {
138               curinput++;
167               // write 1 for anything else
139168               jed_set_fuse(data, data->numfuses++, 1);
140               jed_set_fuse(data, data->numfuses++, 0);
141
142               if (LOG_PARSE) printf("10");
143            }
144            break;
145
146         case '0':
147            if (outputs)
148            {
149               curoutput++;
150               jed_set_fuse(data, data->numfuses++, 1);
151
152169               if (LOG_PARSE) printf("1");
153170            }
154            else
155            {
156               curinput++;
157               jed_set_fuse(data, data->numfuses++, 0);
158               jed_set_fuse(data, data->numfuses++, 1);
159
160               if (LOG_PARSE) printf("01");
161            }
162            break;
163
164         case ' ': case '\t':
165            if (curinput > 0 && !outputs)
166            {
167               outputs = true;
168               if (LOG_PARSE) printf(" ");
169            }
170            break;
171     
172         default:
173            break;
171         }
174172      }
175173
176      if (iscrlf(**cursrc) && outputs)
174      if (iscrlf(**src) && outputs)
177175      {
178176         outputs = false;
179177         if (LOG_PARSE) printf("\n");
r241932r241933
185183         curoutput = 0;
186184      }
187185
188      (*cursrc)++;
186      (*src)++;
189187   }
190188   
191189   return true;
r241932r241933
197195    process_field - process a single field
198196-------------------------------------------------*/
199197
200static bool process_field(jed_data *data, const UINT8 **cursrc, const UINT8 *srcend, parse_info *pinfo)
198static bool process_field(jed_data *data, const UINT8 **src, const UINT8 *srcend, parse_info *pinfo)
201199{
202   (*cursrc)++;
203
204   switch (**cursrc)
200   // valid keywords
201   static const char *const keywords[] = { "i", "o", "p", "phase", "e", "\0" };
202   enum
205203   {
204      KW_INPUTS = 0,
205      KW_OUTPUTS,
206      KW_TERMS,
207      KW_PHASE,
208      KW_END,
209     
210      KW_INVALID
211   };
212   
213   // find keyword
214   char dest[0x10];
215   memset(dest, 0, ARRAY_LENGTH(dest));
216   const UINT8 *seek = *src;
217   int destptr = 0;
218   
219   while (seek < srcend && isalpha(*seek) && destptr < ARRAY_LENGTH(dest) - 1)
220   {
221      dest[destptr] = tolower(*seek);
222      seek++;
223      destptr++;
224   }
225   
226   UINT8 find = 0;
227   while (strlen(keywords[find]) && strcmp(dest, keywords[find]))
228      find++;
229   
230   if (find == KW_INVALID)
231      return false;
232   
233   (*src) += strlen(keywords[find]);
234   
235   // handle it
236   switch (find)
237   {
206238      // number of inputs
207      case 'i': case 'I':
208         pinfo->inputs = suck_number(cursrc, srcend);
239      case KW_INPUTS:
240         pinfo->inputs = suck_number(src, srcend);
209241         if (pinfo->inputs == 0 || pinfo->inputs >= (JED_MAX_FUSES/2))
210242            return false;
211243
r241932r241933
213245         break;
214246
215247      // number of outputs
216      case 'o': case 'O':
217         pinfo->outputs = suck_number(cursrc, srcend);
248      case KW_OUTPUTS:
249         pinfo->outputs = suck_number(src, srcend);
218250         if (pinfo->outputs == 0 || pinfo->outputs >= (JED_MAX_FUSES/2))
219251            return false;
220252
221253         if (LOG_PARSE) printf("Outputs: %u\n", pinfo->outputs);
222254         break;
223255
224      case 'p': case 'P':
225         // output polarity (optional)
226         if (tolower((*cursrc)[1]) == 'h' && tolower((*cursrc)[2]) == 'a' && tolower((*cursrc)[3]) == 's' && tolower((*cursrc)[4]) == 'e')
256      // number of product terms (optional)
257      case KW_TERMS:
258         pinfo->terms = suck_number(src, srcend);
259         if (pinfo->terms == 0 || pinfo->terms >= (JED_MAX_FUSES/2))
260            return false;
261
262         if (LOG_PARSE) printf("Terms: %u\n", pinfo->terms);
263         break;
264     
265      // output polarity (optional)
266      case KW_PHASE:
267         if (LOG_PARSE) printf("Phase...\n");
268         while (*src < srcend && !iscrlf(**src) && pinfo->xorptr < (JED_MAX_FUSES/2))
227269         {
228            if (LOG_PARSE) printf("Phase...\n");
229            while (*cursrc < srcend && !iscrlf(**cursrc) && pinfo->xorptr < (JED_MAX_FUSES/2))
270            if (**src == '0' || **src == '1')
230271            {
231               if (**cursrc == '0' || **cursrc == '1')
232               {
233                  // 0 is negative
234                  if (**cursrc == '0')
235                     pinfo->xorval[pinfo->xorptr/32] |= 1 << (pinfo->xorptr & 31);
236                  pinfo->xorptr++;
237               }
238               
239               (*cursrc)++;
272               // 0 is negative
273               if (**src == '0')
274                  pinfo->xorval[pinfo->xorptr/32] |= 1 << (pinfo->xorptr & 31);
275               pinfo->xorptr++;
240276            }
277            (*src)++;
241278         }
242         
243         // or number of product terms (optional)
244         else
245         {
246            pinfo->terms = suck_number(cursrc, srcend);
247            if (pinfo->terms == 0 || pinfo->terms >= (JED_MAX_FUSES/2))
248               return false;
249
250            if (LOG_PARSE) printf("Terms: %u\n", pinfo->terms);
251         }
252279         break;
253280
254281      // end of file (optional)
255      case 'e': case 'E':
282      case KW_END:
256283         if (LOG_PARSE) printf("End of file\n");
257284         break;
258     
259      default:
260         return false;
261285   }
262286   
263287   return true;
r241932r241933
272296
273297int pla_parse(const void *data, size_t length, jed_data *result)
274298{
275   const UINT8 *cursrc = (const UINT8 *)data;
276   const UINT8 *srcend = cursrc + length;
299   const UINT8 *src = (const UINT8 *)data;
300   const UINT8 *srcend = src + length;
277301   
278302   parse_info pinfo;
279303   memset(&pinfo, 0, sizeof(pinfo));
r241932r241933
281305   result->numfuses = 0;
282306   memset(result->fusemap, 0, sizeof(result->fusemap));
283307
284   while (cursrc < srcend)
308   while (src < srcend)
285309   {
286      switch (*cursrc)
310      switch (*src)
287311      {
288312         // comment line
289313         case '#':
290            while (cursrc < srcend && !iscrlf(*cursrc))
291               cursrc++;
314            while (src < srcend && !iscrlf(*src))
315               src++;
292316            break;
293317
294318         // keyword
295319         case '.':
296            if (!process_field(result, &cursrc, srcend, &pinfo))
320            src++;
321            if (!process_field(result, &src, srcend, &pinfo))
297322               return JEDERR_INVALID_DATA;
298323            break;
299324         
300325         // terms
301326         case '0': case '1': case '-': case '~':
302            if (!process_terms(result, &cursrc, srcend, &pinfo))
327            if (!process_terms(result, &src, srcend, &pinfo))
303328               return JEDERR_INVALID_DATA;
304329            break;
305330         
306331         default:
307            cursrc++;
332            src++;
308333            break;
309334      }
310335   }


Previous 199869 Revisions Next


© 1997-2024 The MAME Team