summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/util/plaparse.c
blob: f6228d2cbe922ed8b40221c63304f8fe056eb8b9 (plain) (blame)
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// license:BSD-3-Clause
// copyright-holders:Aaron Giles, hap
/***************************************************************************

    plaparse.h

    Simple parser for Berkeley standard PLA files into raw fusemaps.
    It supports no more than one output matrix, and is limited to
    keywords: i, o, p, phase, e

***************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "jedparse.h"
#include "plaparse.h"



/***************************************************************************
    DEBUGGING
***************************************************************************/

#define LOG_PARSE       0



/***************************************************************************
    TYPE DEFINITIONS
***************************************************************************/

struct parse_info
{
	UINT32  inputs;     /* number of input columns */
	UINT32  outputs;    /* number of output columns */
	UINT32  terms;      /* number of terms */
	UINT32  xorval[JED_MAX_FUSES/64];   /* output polarity */
	UINT32  xorptr;
};



/***************************************************************************
    UTILITIES
***************************************************************************/

/*-------------------------------------------------
    iscrlf - is a line feed character
-------------------------------------------------*/

static bool iscrlf(char c)
{
	return (c == 13 || c == 10);
}


/*-------------------------------------------------
    suck_number - read a decimal value from the
    character stream
-------------------------------------------------*/

static UINT32 suck_number(const UINT8 **src, const UINT8 *srcend)
{
	UINT32 value = 0;

	// find first digit
	while (*src < srcend && !iscrlf(**src) && !isdigit(**src))
		(*src)++;

	// loop over and accumulate digits
	while (*src < srcend && isdigit(**src))
	{
		value = value * 10 + (**src) - '0';
		(*src)++;
	}

	return value;
}



/***************************************************************************
    CORE IMPLEMENTATION
***************************************************************************/

/*-------------------------------------------------
    process_terms - process input/output matrix
-------------------------------------------------*/

static bool process_terms(jed_data *data, const UINT8 **src, const UINT8 *srcend, parse_info *pinfo)
{
	UINT32 curinput = 0;
	UINT32 curoutput = 0;
	bool outputs = false;

	// symbols for 0, 1, dont_care, no_meaning
	// PLA format documentation also describes them as simply 0, 1, 2, 3
	static const char symbols[] = { "01-~" };

	while (*src < srcend && **src != '.' && **src != '#')
	{
		if (!outputs)
		{
			// and-matrix
			if (strrchr(symbols, **src))
				curinput++;

			switch (**src)
			{
				case '0':
					jed_set_fuse(data, data->numfuses++, 0);
					jed_set_fuse(data, data->numfuses++, 1);

					if (LOG_PARSE) printf("01");
					break;

				case '1':
					jed_set_fuse(data, data->numfuses++, 1);
					jed_set_fuse(data, data->numfuses++, 0);

					if (LOG_PARSE) printf("10");
					break;

				// anything goes
				case '-':
					jed_set_fuse(data, data->numfuses++, 1);
					jed_set_fuse(data, data->numfuses++, 1);

					if (LOG_PARSE) printf("11");
					break;

				// this product term is inhibited
				case '~':
					jed_set_fuse(data, data->numfuses++, 0);
					jed_set_fuse(data, data->numfuses++, 0);

					if (LOG_PARSE) printf("00");
					break;

				case ' ': case '\t':
					if (curinput > 0)
					{
						outputs = true;
						if (LOG_PARSE) printf(" ");
					}
					break;

				default:
					break;
			}
		}
		else
		{
			// or-matrix
			if (strrchr(symbols, **src))
			{
				curoutput++;
				if (**src == '1')
				{
					jed_set_fuse(data, data->numfuses++, 0);
					if (LOG_PARSE) printf("0");
				}
				else
				{
					// write 1 for anything else
					jed_set_fuse(data, data->numfuses++, 1);
					if (LOG_PARSE) printf("1");
				}
			}
		}

		if (iscrlf(**src) && outputs)
		{
			outputs = false;
			if (LOG_PARSE) printf("\n");

			if (curinput != pinfo->inputs || curoutput != pinfo->outputs)
				return false;

			curinput = 0;
			curoutput = 0;
		}

		(*src)++;
	}

	return true;
}



/*-------------------------------------------------
    process_field - process a single field
-------------------------------------------------*/

static bool process_field(jed_data *data, const UINT8 **src, const UINT8 *srcend, parse_info *pinfo)
{
	// valid keywords
	static const char *const keywords[] = { "i", "o", "p", "phase", "e", "\0" };
	enum
	{
		KW_INPUTS = 0,
		KW_OUTPUTS,
		KW_TERMS,
		KW_PHASE,
		KW_END,

		KW_INVALID
	};

	// find keyword
	char dest[0x10];
	memset(dest, 0, ARRAY_LENGTH(dest));
	const UINT8 *seek = *src;
	int destptr = 0;

	while (seek < srcend && isalpha(*seek) && destptr < ARRAY_LENGTH(dest) - 1)
	{
		dest[destptr] = tolower(*seek);
		seek++;
		destptr++;
	}

	UINT8 find = 0;
	while (strlen(keywords[find]) && strcmp(dest, keywords[find]))
		find++;

	if (find == KW_INVALID)
		return false;

	(*src) += strlen(keywords[find]);

	// handle it
	switch (find)
	{
		// number of inputs
		case KW_INPUTS:
			pinfo->inputs = suck_number(src, srcend);
			if (pinfo->inputs == 0 || pinfo->inputs >= (JED_MAX_FUSES/2))
				return false;

			if (LOG_PARSE) printf("Inputs: %u\n", pinfo->inputs);
			break;

		// number of outputs
		case KW_OUTPUTS:
			pinfo->outputs = suck_number(src, srcend);
			if (pinfo->outputs == 0 || pinfo->outputs >= (JED_MAX_FUSES/2))
				return false;

			if (LOG_PARSE) printf("Outputs: %u\n", pinfo->outputs);
			break;

		// number of product terms (optional)
		case KW_TERMS:
			pinfo->terms = suck_number(src, srcend);
			if (pinfo->terms == 0 || pinfo->terms >= (JED_MAX_FUSES/2))
				return false;

			if (LOG_PARSE) printf("Terms: %u\n", pinfo->terms);
			break;

		// output polarity (optional)
		case KW_PHASE:
			if (LOG_PARSE) printf("Phase...\n");
			while (*src < srcend && !iscrlf(**src) && pinfo->xorptr < (JED_MAX_FUSES/2))
			{
				if (**src == '0' || **src == '1')
				{
					// 0 is negative
					if (**src == '0')
						pinfo->xorval[pinfo->xorptr/32] |= 1 << (pinfo->xorptr & 31);
					pinfo->xorptr++;
				}
				(*src)++;
			}
			break;

		// end of file (optional)
		case KW_END:
			if (LOG_PARSE) printf("End of file\n");
			break;
	}

	return true;
}



/*-------------------------------------------------
    pla_parse - parse a Berkeley standard PLA file
    that has been loaded raw into memory
-------------------------------------------------*/

/**
 * @fn	int pla_parse(const void *data, size_t length, jed_data *result)
 *
 * @brief	Pla parse.
 *
 * @param	data	  	The data.
 * @param	length	  	The length.
 * @param [out]	result	If non-null, the result.
 *
 * @return	An int.
 */

int pla_parse(const void *data, size_t length, jed_data *result)
{
	const UINT8 *src = (const UINT8 *)data;
	const UINT8 *srcend = src + length;

	parse_info pinfo;
	memset(&pinfo, 0, sizeof(pinfo));

	result->numfuses = 0;
	memset(result->fusemap, 0, sizeof(result->fusemap));

	while (src < srcend)
	{
		switch (*src)
		{
			// comment line
			case '#':
				while (src < srcend && !iscrlf(*src))
					src++;
				break;

			// keyword
			case '.':
				src++;
				if (!process_field(result, &src, srcend, &pinfo))
					return JEDERR_INVALID_DATA;
				break;

			// terms
			case '0': case '1': case '-': case '~':
				if (!process_terms(result, &src, srcend, &pinfo))
					return JEDERR_INVALID_DATA;
				break;

			default:
				src++;
				break;
		}
	}

	// write output polarity
	if (pinfo.xorptr > 0)
	{
		if (LOG_PARSE) printf("Polarity: ");

		for (int i = 0; i < pinfo.outputs; i++)
		{
			int bit = pinfo.xorval[i/32] >> (i & 31) & 1;
			jed_set_fuse(result, result->numfuses++, bit);
			if (LOG_PARSE) printf("%d", bit);
		}
		if (LOG_PARSE) printf("\n");
	}

	return JEDERR_NONE;
}