trunk/src/lib/util/plaparse.c
| r241915 | r241916 | |
| 4 | 4 | |
| 5 | 5 | plaparse.h |
| 6 | 6 | |
| 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 |
| 8 | 10 | |
| 9 | 11 | ***************************************************************************/ |
| 10 | 12 | |
| r241915 | r241916 | |
| 31 | 33 | |
| 32 | 34 | struct parse_info |
| 33 | 35 | { |
| 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; |
| 37 | 41 | }; |
| 38 | 42 | |
| 39 | 43 | |
| r241915 | r241916 | |
| 57 | 61 | character stream |
| 58 | 62 | -------------------------------------------------*/ |
| 59 | 63 | |
| 60 | | static UINT32 suck_number(const UINT8 **psrc) |
| 64 | static UINT32 suck_number(const UINT8 **cursrc, const UINT8 *srcend) |
| 61 | 65 | { |
| 62 | | const UINT8 *src = *psrc; |
| 63 | 66 | UINT32 value = 0; |
| 67 | (*cursrc)++; |
| 68 | |
| 69 | // find first digit |
| 70 | while (*cursrc < srcend && !iscrlf(**cursrc) && !isdigit(**cursrc)) |
| 71 | (*cursrc)++; |
| 64 | 72 | |
| 65 | 73 | // loop over and accumulate digits |
| 66 | | while (isdigit(*src)) |
| 74 | while (isdigit(**cursrc)) |
| 67 | 75 | { |
| 68 | | value = value * 10 + *src - '0'; |
| 69 | | src++; |
| 76 | value = value * 10 + (**cursrc) - '0'; |
| 77 | (*cursrc)++; |
| 70 | 78 | } |
| 71 | 79 | |
| 72 | | // return a pointer to the string afterwards |
| 73 | | *psrc = src; |
| 74 | 80 | return value; |
| 75 | 81 | } |
| 76 | 82 | |
| r241915 | r241916 | |
| 81 | 87 | ***************************************************************************/ |
| 82 | 88 | |
| 83 | 89 | /*------------------------------------------------- |
| 84 | | process_field - process a single field |
| 90 | process_terms - process input/output matrix |
| 85 | 91 | -------------------------------------------------*/ |
| 86 | 92 | |
| 87 | | static void process_field(jed_data *data, const UINT8 *cursrc, const UINT8 *srcend, parse_info *pinfo) |
| 93 | static bool process_terms(jed_data *data, const UINT8 **cursrc, const UINT8 *srcend, parse_info *pinfo) |
| 88 | 94 | { |
| 89 | | cursrc++; |
| 95 | UINT32 curinput = 0; |
| 96 | UINT32 curoutput = 0; |
| 97 | bool outputs = false; |
| 90 | 98 | |
| 91 | | // switch off of the field type |
| 92 | | switch (*cursrc) |
| 99 | while (*cursrc < srcend && **cursrc != '.' && **cursrc != '#') |
| 93 | 100 | { |
| 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) |
| 110 | 102 | { |
| 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); |
| 114 | 109 | |
| 115 | | UINT32 curfuse = 0; |
| 116 | | bool outputs = false; |
| 110 | if (LOG_PARSE) printf("11"); |
| 111 | } |
| 112 | break; |
| 117 | 113 | |
| 118 | | cursrc++; |
| 119 | | while (cursrc < srcend && *cursrc != '.') |
| 120 | | { |
| 121 | | switch (*cursrc) |
| 114 | case '~': |
| 115 | if (!outputs) |
| 122 | 116 | { |
| 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); |
| 128 | 121 | |
| 129 | | if (LOG_PARSE) printf("11"); |
| 130 | | } |
| 131 | | break; |
| 122 | if (LOG_PARSE) printf("00"); |
| 123 | } |
| 124 | break; |
| 132 | 125 | |
| 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); |
| 137 | 131 | |
| 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); |
| 144 | 139 | |
| 145 | | if (LOG_PARSE) printf("10"); |
| 146 | | } |
| 147 | | break; |
| 140 | if (LOG_PARSE) printf("10"); |
| 141 | } |
| 142 | break; |
| 148 | 143 | |
| 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); |
| 153 | 149 | |
| 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); |
| 160 | 157 | |
| 161 | | if (LOG_PARSE) printf("01"); |
| 162 | | } |
| 163 | | break; |
| 158 | if (LOG_PARSE) printf("01"); |
| 159 | } |
| 160 | break; |
| 164 | 161 | |
| 165 | | case ' ': |
| 162 | case ' ': case '\t': |
| 163 | if (curinput > 0) |
| 164 | { |
| 166 | 165 | outputs = true; |
| 167 | 166 | if (LOG_PARSE) printf(" "); |
| 168 | | break; |
| 169 | 167 | } |
| 168 | break; |
| 169 | |
| 170 | default: |
| 171 | break; |
| 172 | } |
| 170 | 173 | |
| 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 | |
| 198 | static 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)) |
| 172 | 228 | { |
| 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)++; |
| 175 | 238 | } |
| 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; |
| 176 | 247 | |
| 177 | | cursrc++; |
| 248 | if (LOG_PARSE) printf("Terms: %u\n", pinfo->terms); |
| 178 | 249 | } |
| 179 | | |
| 180 | | data->numfuses = curfuse; |
| 181 | 250 | break; |
| 182 | | } |
| 183 | 251 | |
| 184 | | // end of file |
| 252 | // end of file (optional) |
| 185 | 253 | case 'e': |
| 186 | 254 | if (LOG_PARSE) printf("End of file\n"); |
| 187 | 255 | break; |
| 256 | |
| 257 | default: |
| 258 | return false; |
| 188 | 259 | } |
| 189 | | |
| 190 | | cursrc++; |
| 260 | |
| 261 | return true; |
| 191 | 262 | } |
| 192 | 263 | |
| 193 | 264 | |
| r241915 | r241916 | |
| 201 | 272 | { |
| 202 | 273 | const UINT8 *cursrc = (const UINT8 *)data; |
| 203 | 274 | const UINT8 *srcend = cursrc + length; |
| 204 | | const UINT8 *scan; |
| 275 | |
| 205 | 276 | parse_info pinfo; |
| 277 | memset(&pinfo, 0, sizeof(pinfo)); |
| 206 | 278 | |
| 207 | 279 | result->numfuses = 0; |
| 208 | | memset(result->fusemap, 0x00, sizeof(result->fusemap)); |
| 280 | memset(result->fusemap, 0, sizeof(result->fusemap)); |
| 209 | 281 | |
| 210 | 282 | while (cursrc < srcend) |
| 211 | 283 | { |
| 212 | | if (*cursrc == '#') |
| 284 | switch (*cursrc) |
| 213 | 285 | { |
| 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: |
| 216 | 305 | cursrc++; |
| 306 | break; |
| 217 | 307 | } |
| 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++) |
| 219 | 316 | { |
| 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); |
| 229 | 320 | } |
| 230 | | |
| 231 | | cursrc++; |
| 321 | if (LOG_PARSE) printf("\n"); |
| 232 | 322 | } |
| 233 | 323 | |
| 234 | 324 | return JEDERR_NONE; |