Previous 199869 Revisions Next

r33404 Sunday 16th November, 2014 at 04:01:08 UTC by hap
made plaparse less strict about whitespace and ordering, and added .phase keyword
[src/lib/util]plaparse.c

trunk/src/lib/util/plaparse.c
r241915r241916
44
55    plaparse.h
66
7    Parser for Berkeley standard PLA files into raw fusemaps.
7    Simple parser for Berkeley standard PLA files into raw fusemaps.
8    It supports no more than one output matrix, and is limited to
9    keywords: i, o, p, phase, e
810
911***************************************************************************/
1012
r241915r241916
3133
3234struct parse_info
3335{
34   UINT32  inputs;
35   UINT32  outputs;
36   UINT32  terms;
36   UINT32  inputs;     /* number of input columns */
37   UINT32  outputs;    /* number of output columns */
38   UINT32  terms;      /* number of terms */
39   UINT32  xorval[JED_MAX_FUSES/64];   /* output polarity */
40   UINT32  xorptr;
3741};
3842
3943
r241915r241916
5761    character stream
5862-------------------------------------------------*/
5963
60static UINT32 suck_number(const UINT8 **psrc)
64static UINT32 suck_number(const UINT8 **cursrc, const UINT8 *srcend)
6165{
62   const UINT8 *src = *psrc;
6366   UINT32 value = 0;
67   (*cursrc)++;
68   
69   // find first digit
70   while (*cursrc < srcend && !iscrlf(**cursrc) && !isdigit(**cursrc))
71      (*cursrc)++;
6472
6573   // loop over and accumulate digits
66   while (isdigit(*src))
74   while (isdigit(**cursrc))
6775   {
68      value = value * 10 + *src - '0';
69      src++;
76      value = value * 10 + (**cursrc) - '0';
77      (*cursrc)++;
7078   }
7179
72   // return a pointer to the string afterwards
73   *psrc = src;
7480   return value;
7581}
7682
r241915r241916
8187***************************************************************************/
8288
8389/*-------------------------------------------------
84    process_field - process a single field
90    process_terms - process input/output matrix
8591-------------------------------------------------*/
8692
87static void process_field(jed_data *data, const UINT8 *cursrc, const UINT8 *srcend, parse_info *pinfo)
93static bool process_terms(jed_data *data, const UINT8 **cursrc, const UINT8 *srcend, parse_info *pinfo)
8894{
89   cursrc++;
95   UINT32 curinput = 0;
96   UINT32 curoutput = 0;
97   bool outputs = false;
9098
91   // switch off of the field type
92   switch (*cursrc)
99   while (*cursrc < srcend && **cursrc != '.' && **cursrc != '#')
93100   {
94      // number of inputs
95      case 'i':
96         cursrc += 2;
97         pinfo->inputs = suck_number(&cursrc);
98         if (LOG_PARSE) printf("Inputs: %u\n", pinfo->inputs);
99         break;
100
101      // number of outputs
102      case 'o':
103         cursrc += 2;
104         pinfo->outputs = suck_number(&cursrc);
105         if (LOG_PARSE) printf("Outputs: %u\n", pinfo->outputs);
106         break;
107
108      // number of product terms
109      case 'p':
101      switch (**cursrc)
110102      {
111         cursrc += 2;
112         pinfo->terms = suck_number(&cursrc);
113         if (LOG_PARSE) printf("Terms: %u\n", pinfo->terms);
103         case '-':
104            if (!outputs)
105            {
106               curinput++;
107               jed_set_fuse(data, data->numfuses++, 1);
108               jed_set_fuse(data, data->numfuses++, 1);
114109
115         UINT32 curfuse = 0;
116         bool outputs = false;
110               if (LOG_PARSE) printf("11");
111            }
112            break;
117113
118         cursrc++;
119         while (cursrc < srcend && *cursrc != '.')
120         {
121            switch (*cursrc)
114         case '~':
115            if (!outputs)
122116            {
123            case '-':
124               if (!outputs)
125               {
126                  jed_set_fuse(data, curfuse++, 1);
127                  jed_set_fuse(data, curfuse++, 1);
117               curinput++;
118               // this product term is inhibited
119               jed_set_fuse(data, data->numfuses++, 0);
120               jed_set_fuse(data, data->numfuses++, 0);
128121
129                  if (LOG_PARSE) printf("11");
130               }
131               break;
122               if (LOG_PARSE) printf("00");
123            }
124            break;
132125
133            case '1':
134               if (outputs)
135               {
136                  jed_set_fuse(data, curfuse++, 0);
126         case '1':
127            if (outputs)
128            {
129               curoutput++;
130               jed_set_fuse(data, data->numfuses++, 0);
137131
138                  if (LOG_PARSE) printf("0");
139               }
140               else
141               {
142                  jed_set_fuse(data, curfuse++, 1);
143                  jed_set_fuse(data, curfuse++, 0);
132               if (LOG_PARSE) printf("0");
133            }
134            else
135            {
136               curinput++;
137               jed_set_fuse(data, data->numfuses++, 1);
138               jed_set_fuse(data, data->numfuses++, 0);
144139
145                  if (LOG_PARSE) printf("10");
146               }
147               break;
140               if (LOG_PARSE) printf("10");
141            }
142            break;
148143
149            case '0':
150               if (outputs)
151               {
152                  jed_set_fuse(data, curfuse++, 1);
144         case '0':
145            if (outputs)
146            {
147               curoutput++;
148               jed_set_fuse(data, data->numfuses++, 1);
153149
154                  if (LOG_PARSE) printf("1");
155               }
156               else
157               {
158                  jed_set_fuse(data, curfuse++, 0);
159                  jed_set_fuse(data, curfuse++, 1);
150               if (LOG_PARSE) printf("1");
151            }
152            else
153            {
154               curinput++;
155               jed_set_fuse(data, data->numfuses++, 0);
156               jed_set_fuse(data, data->numfuses++, 1);
160157
161                  if (LOG_PARSE) printf("01");
162               }
163               break;
158               if (LOG_PARSE) printf("01");
159            }
160            break;
164161
165            case ' ':
162         case ' ': case '\t':
163            if (curinput > 0)
164            {
166165               outputs = true;
167166               if (LOG_PARSE) printf(" ");
168               break;
169167            }
168            break;
169     
170         default:
171            break;
172      }
170173
171            if (iscrlf(*cursrc) && outputs)
174      if (iscrlf(**cursrc) && outputs)
175      {
176         outputs = false;
177         if (LOG_PARSE) printf("\n");
178         
179         if (curinput != pinfo->inputs || curoutput != pinfo->outputs)
180            return false;
181
182         curinput = 0;
183         curoutput = 0;
184      }
185
186      (*cursrc)++;
187   }
188   
189   return true;
190}
191
192
193
194/*-------------------------------------------------
195    process_field - process a single field
196-------------------------------------------------*/
197
198static bool process_field(jed_data *data, const UINT8 **cursrc, const UINT8 *srcend, parse_info *pinfo)
199{
200   (*cursrc)++;
201
202   switch (**cursrc)
203   {
204      // number of inputs
205      case 'i':
206         pinfo->inputs = suck_number(cursrc, srcend);
207         if (pinfo->inputs == 0 || pinfo->inputs >= (JED_MAX_FUSES/2))
208            return false;
209
210         if (LOG_PARSE) printf("Inputs: %u\n", pinfo->inputs);
211         break;
212
213      // number of outputs
214      case 'o':
215         pinfo->outputs = suck_number(cursrc, srcend);
216         if (pinfo->outputs == 0 || pinfo->outputs >= (JED_MAX_FUSES/2))
217            return false;
218
219         if (LOG_PARSE) printf("Outputs: %u\n", pinfo->outputs);
220         break;
221
222      case 'p':
223         // output polarity (optional)
224         if ((*cursrc)[1] == 'h' && (*cursrc)[2] == 'a' && (*cursrc)[3] == 's' && (*cursrc)[4] == 'e')
225         {
226            if (LOG_PARSE) printf("Phase...\n");
227            while (*cursrc < srcend && !iscrlf(**cursrc) && pinfo->xorptr < (JED_MAX_FUSES/2))
172228            {
173               outputs = false;
174               if (LOG_PARSE) printf("\n");
229               if (**cursrc == '0' || **cursrc == '1')
230               {
231                  // 0 is negative
232                  if (**cursrc == '0')
233                     pinfo->xorval[pinfo->xorptr/32] |= 1 << (pinfo->xorptr & 31);
234                  pinfo->xorptr++;
235               }
236               
237               (*cursrc)++;
175238            }
239         }
240         
241         // or number of product terms (optional)
242         else
243         {
244            pinfo->terms = suck_number(cursrc, srcend);
245            if (pinfo->terms == 0 || pinfo->terms >= (JED_MAX_FUSES/2))
246               return false;
176247
177            cursrc++;
248            if (LOG_PARSE) printf("Terms: %u\n", pinfo->terms);
178249         }
179
180         data->numfuses = curfuse;
181250         break;
182      }
183251
184      // end of file
252      // end of file (optional)
185253      case 'e':
186254         if (LOG_PARSE) printf("End of file\n");
187255         break;
256     
257      default:
258         return false;
188259   }
189
190   cursrc++;
260   
261   return true;
191262}
192263
193264
r241915r241916
201272{
202273   const UINT8 *cursrc = (const UINT8 *)data;
203274   const UINT8 *srcend = cursrc + length;
204   const UINT8 *scan;
275   
205276   parse_info pinfo;
277   memset(&pinfo, 0, sizeof(pinfo));
206278
207279   result->numfuses = 0;
208   memset(result->fusemap, 0x00, sizeof(result->fusemap));
280   memset(result->fusemap, 0, sizeof(result->fusemap));
209281
210282   while (cursrc < srcend)
211283   {
212      if (*cursrc == '#')
284      switch (*cursrc)
213285      {
214         cursrc++;
215         while (cursrc < srcend && !iscrlf(*cursrc))
286         // comment line
287         case '#':
288            while (cursrc < srcend && !iscrlf(*cursrc))
289               cursrc++;
290            break;
291
292         // keyword
293         case '.':
294            if (!process_field(result, &cursrc, srcend, &pinfo))
295               return JEDERR_INVALID_DATA;
296            break;
297         
298         // terms
299         case '0': case '1': case '-': case '~':
300            if (!process_terms(result, &cursrc, srcend, &pinfo))
301               return JEDERR_INVALID_DATA;
302            break;
303         
304         default:
216305            cursrc++;
306            break;
217307      }
218      else if (*cursrc == '.')
308   }
309   
310   // write output polarity
311   if (pinfo.xorptr > 0)
312   {
313      if (LOG_PARSE) printf("Polarity: ");
314     
315      for (int i = 0; i < pinfo.outputs; i++)
219316      {
220         scan = cursrc;
221         while (scan < srcend && !iscrlf(*scan))
222            scan++;
223         if (scan >= srcend)
224            return JEDERR_INVALID_DATA;
225
226         process_field(result, cursrc, srcend, &pinfo);
227
228         cursrc = scan + 1;
317         int bit = pinfo.xorval[i/32] >> (i & 31) & 1;
318         jed_set_fuse(result, result->numfuses++, bit);
319         if (LOG_PARSE) printf("%d", bit);
229320      }
230
231      cursrc++;
321      if (LOG_PARSE) printf("\n");
232322   }
233323
234324   return JEDERR_NONE;


Previous 199869 Revisions Next


© 1997-2024 The MAME Team