summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/netlist/nl_parser.c
blob: 21ea62fec9ea1cf3cc8174cc539cb65574d75695 (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
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * nl_parser.c
 *
 */

#include "nl_parser.h"

//#undef NL_VERBOSE_OUT
//#define NL_VERBOSE_OUT(x) printf x

// ----------------------------------------------------------------------------------------
// A netlist parser
// ----------------------------------------------------------------------------------------

ATTR_COLD void netlist_parser::error(const char *format, ...)
{
    va_list ap;
    va_start(ap, format);

    pstring errmsg1 =pstring(format).vprintf(ap);
    va_end(ap);

    char buf[300];
    int bufp = 0;
    const char *p = m_line_ptr;
    while (*p && *p != 10)
        buf[bufp++] = *p++;
    buf[bufp] = 0;

    m_setup.netlist().error("line %d: error: %s\n\t\t%s\n", m_line, errmsg1.cstr(), buf);

    //throw error;
}


void netlist_parser::parse(const char *buf)
{
	m_px = buf;
	m_line_ptr = buf;
	m_line = 1;

	while (!eof())
	{
		pstring n;
		skipws();
		if (eof()) break;
		n = getname('(');
		NL_VERBOSE_OUT(("Parser: Device: %s\n", n.cstr()));
		if (n == "NET_ALIAS")
			net_alias();
		else if (n == "NET_C")
			net_c();
		else if (n == "NETDEV_PARAM")
			netdev_param();
		else if ((n == "NET_MODEL"))
		    net_model();
		else if (n == "NETLIST_START")
		    netdev_netlist_start();
        else if (n == "NETLIST_END")
            netdev_netlist_end();
		else
			netdev_device(n);
	}
}

void netlist_parser::netdev_netlist_start()
{
    // don't do much
    skipws();
    /*pstring dummyname = */ getname(')');
    //check_char(')');
}

void netlist_parser::netdev_netlist_end()
{
    // don't do much
    skipws();
    check_char(')');
}

void netlist_parser::net_model()
{
    // don't do much
    pstring model = getstring();
    m_setup.register_model(model);
    check_char(')');
}

void netlist_parser::net_alias()
{
	pstring alias;
	pstring out;
	skipws();
	alias = getname(',');
	skipws();
	out = getname(')');
	NL_VERBOSE_OUT(("Parser: Alias: %s %s\n", alias.cstr(), out.cstr()));
	m_setup.register_alias(alias, out);
}

void netlist_parser::net_c()
{
	pstring t1;
	pstring t2;
	skipws();
	t1 = getname(',');
	skipws();
	t2 = getname(')');
	NL_VERBOSE_OUT(("Parser: Connect: %s %s\n", t1.cstr(), t2.cstr()));
	m_setup.register_link(t1 , t2);
}

void netlist_parser::netdev_param()
{
	pstring param;
	double val;
	skipws();
	param = getname(',');
	skipws();
	val = eval_param();
	NL_VERBOSE_OUT(("Parser: Param: %s %f\n", param.cstr(), val));
	m_setup.register_param(param, val);
	check_char(')');
}

void netlist_parser::netdev_device(const pstring &dev_type)
{
	pstring devname;
	net_device_t_base_factory *f = m_setup.factory().factory_by_name(dev_type, m_setup);
	netlist_device_t *dev;
	nl_util::pstring_list termlist = f->term_param_list();
	pstring def_param = f->def_param();

	int cnt;

	skipws();
	devname = getname2(',', ')');
	dev = f->Create();
	m_setup.register_dev(dev, devname);

	NL_VERBOSE_OUT(("Parser: IC: %s\n", devname.cstr()));

	if (def_param != "")
	{
        pstring paramfq = devname + "." + def_param;
	    NL_VERBOSE_OUT(("Defparam: %s\n", def_param.cstr()));
        check_char(',');
	    skipws();
	    if (peekc() == '"')
	    {
            pstring val = getstring();
            m_setup.register_param(paramfq, val);
	    }
	    else
	    {
	        double val = eval_param();
	        m_setup.register_param(paramfq, val);
	    }
	    if (termlist.count() > 0)
	        check_char(',');
	}

	cnt = 0;
	while (getc() != ')' && cnt < termlist.count())
	{
		skipws();
		pstring output_name = getname2(',', ')');

		m_setup.register_link(devname + "." + termlist[cnt], output_name);

		skipws();
		cnt++;
	}
    if (cnt != termlist.count())
        fatalerror("netlist: input count mismatch for %s - expected %d found %d\n", devname.cstr(), termlist.count(), cnt);
}


// ----------------------------------------------------------------------------------------
// private
// ----------------------------------------------------------------------------------------

void netlist_parser::skipeol()
{
	char c = getc();
	while (c)
	{
		if (c == 10)
		{
			c = getc();
			if (c != 13)
				ungetc();
			return;
		}
		c = getc();
	}
}

void netlist_parser::skipws()
{
	while (unsigned char c = getc())
	{
		switch (c)
		{
		case ' ':
		case 9:
		case 10:
		case 13:
			break;
		case '#':
		    skipeol(); // treat preprocessor defines as comments
		    break;
		case '/':
			c = getc();
			if (c == '/')
			{
				skipeol();
			}
			else if (c == '*')
			{
				int f=0;
				while (!eof() )
				{
				    c = getc();
					if (f == 0 && c == '*')
						f=1;
					else if (f == 1 && c== '/' )
						break;
					else
						f=0;
				}
			}
			break;
		default:
			ungetc();
			return;
		}
	}
}

pstring netlist_parser::getstring()
{
    skipws();
    check_char('"');
    pstring ret = getname2_ext('"', 0, NULL);
    check_char('"');
    skipws();
    return ret;
}


pstring netlist_parser::getname(char sep)
{
	pstring ret = getname2(sep, 0);
	getc(); // undo the undo ...
	return ret;
}

pstring netlist_parser::getname2(char sep1, char sep2)
{
    static const char *allowed = "0123456789_.ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    return getname2_ext(sep1, sep2, allowed);
}

pstring netlist_parser::getname2_ext(char sep1, char sep2, const char *allowed)
{
    char buf[1024];
    char *p1 = buf;
    char c=getc();

    while ((c != sep1) && (c != sep2))
    {
        char cU = toupper(c);
        if ((allowed == NULL) || strchr(allowed, cU) != NULL)
            *p1++ = c;
        else
        {
            *p1 = 0;
            error("illegal character <%c> in name ...\n", c);
        }
        c = getc();
    }
    *p1 = 0;
    ungetc();
    return pstring(buf);
}
void netlist_parser::check_char(char ctocheck)
{
	skipws();
	char c = getc();
	if ( c == ctocheck)
	{
		return;
	}
	error("expected '%c' found '%c'\n", ctocheck, c);
}

double netlist_parser::eval_param()
{
	static const char *macs[6] = {"", "RES_K(", "RES_M(", "CAP_U(", "CAP_N(", "CAP_P("};
	static const char *allowed = "RESKMUNPAC_0123456789E(+-.";
	static double facs[6] = {1, 1e3, 1e6, 1e-6, 1e-9, 1e-12};
	int i;
	int f=0;
	bool e;
	double ret;

	pstring s = getname2_ext(')',',', allowed);

	for (i=1; i<6;i++)
		if (strncmp(s.cstr(), macs[i], strlen(macs[i])) == 0)
			f = i;
    if (f>0)
        check_char(')');
	s = s.substr(strlen(macs[f]));
	ret = s.as_double(&e);
//    if ((f>0) && e)
	if (e)
		error("Error with parameter ...\n");
	return ret * facs[f];
}

unsigned char netlist_parser::peekc()
{
    unsigned char c = getc();
    ungetc();
    return c;
}

unsigned char netlist_parser::getc()
{
    if (*m_px == 10)
    {
        m_line++;
        m_line_ptr = m_px + 1;
    }
	if (*m_px)
		return *(m_px++);
	else
		return *m_px;
}

void netlist_parser::ungetc()
{
	m_px--;
}