summaryrefslogtreecommitdiff
path: root/interpreter/signals.c
blob: 32a61c61b7c035cf85058301df2a14617d775b93 (plain)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* $Id: signals.c 5075 2010-11-19 11:23:59Z potyra $
 *
 * Signal/Driver handling related functions.
 *
 * Copyright (C) 2008-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#include "signals.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "glue-main.h" /* for time_virt */
#include "glue-log.h"
#include "kernel.h"

/** transaction element of a driver */
struct drv_trans {
	/** simulation time at which the transaction is to happen */
	universal_integer sim_time;
	/** value of the driver at given time. */
	union fauhdli_value val;
};

/** ordering function for transactions of a driver in ascending simulation 
 *  time order.
 */
static int
__attribute__((__pure__))
drv_trans_compare(const void *_t1, const void *_t2)
{
	const struct drv_trans *t1 = (const struct drv_trans *)_t1;
	const struct drv_trans *t2 = (const struct drv_trans *)_t2;

	if (t1->sim_time < t2->sim_time) {
		return -1;
	}

	if (t1->sim_time == t2->sim_time) {
		return 0;
	}

	return 1;
}

static void
driver_connect_non_foreign(
	struct driver *driver,
	struct signal *_signal,
	const struct glue_vhdl_cb *callbacks
)
{
	assert(driver != NULL);
	assert(_signal != NULL);

	slset_add(_signal->connected_drivers, driver, callbacks->malloc);
	driver->connected_signal = _signal;
}

bool
driver_connect(
	struct driver *driver, 
	struct signal *_signal,
	struct fauhdli *instance
)
{
	if (_signal->is_foreign) {
		/* foreign out drivers are NOT connected
		 * to the VHDL signal! these directly forward
		 * the value to the foreign signal via glue_vhdl
		 */
		assert(instance->callbacks.connect_out != NULL);
		instance->callbacks.connect_out(
					instance->glue_vhdl, 
					_signal->foreign_id,
					_signal->value,
					driver);
		driver->foreign_out = true;
		driver->foreign_id = _signal->foreign_id;
		return true;
	}

	driver_connect_non_foreign(driver, _signal, &instance->callbacks);
	return false;
}

static void
driver_disconnect(struct driver *driver, const struct glue_vhdl_cb *callbacks)
{
	if (driver->connected_signal == NULL) {
		/* not connected */
		return;
	}

	assert(driver->connected_signal->connected_drivers != NULL);
	slset_remove(
		driver->connected_signal->connected_drivers, 
		driver,
		callbacks->free);
	driver->connected_signal = NULL;
}

struct driver *
driver_create(union fauhdli_value init, const struct glue_vhdl_cb *callbacks)
{
	struct driver *ret;
	ret = callbacks->malloc(sizeof(struct driver));
	assert(ret != NULL);
	ret->driving_value = init;
	ret->connected_signal = NULL;
	ret->active = false;
	ret->transactions = 
		slset_create(drv_trans_compare, callbacks->malloc);
	ret->foreign_out = false;
	ret->foreign_id = 0;
	return ret;
}

void
driver_destroy(struct driver *driver, const struct glue_vhdl_cb *callbacks)
{
	driver_disconnect(driver, callbacks);
	slset_destroy_data(driver->transactions, callbacks->free);
	callbacks->free(driver);
}

void
driver_update(
	struct driver *driver,
	union fauhdli_value val,
	universal_integer sim_time,
	const struct glue_vhdl_cb *callbacks
)
{
	struct drv_trans *t;

	t = callbacks->malloc(sizeof(struct drv_trans));
	assert(t != NULL);

	t->sim_time = sim_time;
	t->val = val;

	slset_truncate_at(driver->transactions, t, true);
	slset_add(driver->transactions, t, callbacks->malloc);
}


/** Update driving_value from the transactions at a given simulation
 *  time.
 *  @param driver driver to look up current transaction
 *  @param sim_time current simulation time.
 *  @return true, if the driver is now active, false if not
 *          (FIXME: actually active should be set correctly instead!)
 */
static bool 
update_driving_value(
	struct driver *driver, 
	universal_integer sim_time,
	const struct glue_vhdl_cb *callbacks
)
{
	struct drv_trans *t;

	if (driver->transactions->first == NULL) {
		return false;
	}

	t = (struct drv_trans *)driver->transactions->first->data;

	if (sim_time < t->sim_time) {
		/* entry in the future */
		return false;
	}
	/* entry now or in the past (FAUmachine's CPU isn't keeping
	 * exact track of the time, so registered event callbacks may
	 * come at a later point in simulation time than these
	 * have been scheduled -- however only if the schedule updated 
	 * *while* the CPU is running.
	 */

	driver->active = true;
	driver->driving_value = t->val;
	slset_remove(driver->transactions, t, callbacks->free);
	callbacks->free(t);
	return true;
}

static void
driver_propagate(
	struct driver *driver, 
	universal_integer sim_time,
	const struct glue_vhdl_cb *callbacks
)
{
	bool active;
	/* FIXME reset active somewhere */

	active = update_driving_value(driver, sim_time, callbacks);
	if (! active) {
		return;
	}

	if (memcmp(&driver->connected_signal->value, &driver->driving_value,
		sizeof(union fauhdli_value)) != 0) {

		driver->connected_signal->event = true;
		driver->connected_signal->value = driver->driving_value;
	}

	assert(! driver->foreign_out);
}

void
driver_forward_foreign(
	struct fauhdli *instance,
	struct driver *driver,
	universal_integer sim_time
)
{
	bool active;

	assert(driver->foreign_out);
	active = update_driving_value(
					driver, 
					sim_time, 
					&instance->callbacks);
	if (! active) {
		return;
	}

	assert(driver->connected_signal == NULL);
	assert(instance->callbacks.drv_set != NULL);
	instance->callbacks.drv_set(instance->glue_vhdl,
					driver->foreign_id,
					driver->driving_value,
					driver);
}

static void
signal_drivers_propagate_unresolved(
	const struct glue_vhdl_cb *callbacks,
	struct signal *sig, 
	universal_integer sim_time
)
{
	static bool error_reported = false;
	unsigned int n_drivers = 0;
	struct slset_entry *i;

	for (i = sig->connected_drivers->first; i != NULL; i = i->next) {
		struct driver *d = (struct driver *)i->data;
		
		assert(! d->foreign_out);
		driver_propagate(d, sim_time, callbacks);
		n_drivers++;
	}

	if ((1 < n_drivers) && (! error_reported)) {
		error_reported = true;

		callbacks->log(FAUHDLI_LOG_CRITICAL, "fauhdli", __func__,
			"There is more than one driver for an unresoved "
			"signal.\n");
	}

}

void
signal_drivers_propagate(
	struct fauhdli *instance, 
	struct signal *sig, 
	universal_integer sim_time
)
{
	struct slset_entry *i;
	union fauhdli_value drv_args[10];
	union fauhdli_value ret;
	size_t num_drvs = 0;
	bool active = false;

	if (sig->resolver == NULL) {
		signal_drivers_propagate_unresolved(
				&instance->callbacks, sig, sim_time);
	}

	/* resolved signal */

	/* collect driving values */
	for (i = sig->connected_drivers->first; i != NULL; i = i->next) {
		struct driver *d = (struct driver *)i->data;
		
		assert(! d->foreign_out);
		active |= update_driving_value(d, 
						sim_time, 
						&instance->callbacks);

		assert(num_drvs < (sizeof(drv_args) / sizeof(drv_args[0])));
		drv_args[num_drvs] = d->driving_value;
		num_drvs++;
	}

	/* FIXME does that adhere to lrm? */
	if (! active) {
		return;
	}

	/* let the kernel call the resolution function */
	fauhdli_kernel_resolve(instance, 
				sig->resolver, 
				&ret, 
				drv_args, 
				num_drvs);

	if (memcmp(&sig->value, &ret, sizeof(union fauhdli_value)) != 0) {
		sig->event = true;
		sig->value = ret;
	}
}

universal_integer
driver_get_next_event(const struct driver *drv)
{
	const struct slset_entry *i;
	const struct drv_trans *t;

	if (slset_empty(drv->transactions)) {
		return INT64_MAX;
	}

	i = drv->transactions->first;
	t = (const struct drv_trans *)i->data;

	return t->sim_time;
}

struct signal *
signal_create(
	union fauhdli_value init,
	const char *foreign,
	struct fauhdli *instance,
	const char *name,
	const struct code_container *resolver
)
{
	struct signal *ret;

	ret = instance->callbacks.malloc(sizeof(struct signal));
	assert(ret != NULL);
	ret->value = init;
	ret->connected_drivers = 
		slset_create(NULL, instance->callbacks.malloc);
	ret->event = false;
	ret->resolver = resolver;

	if (foreign == NULL) {
		ret->is_foreign = false;
		ret->foreign_id = 0;
		return ret;
	}

	/* foreign signal */
	ret->is_foreign = true;
	assert(instance->callbacks.signal_create != NULL);
	ret->foreign_id = 
		instance->callbacks.signal_create(
						instance->glue_vhdl,
						foreign,
						name);
	return ret;
}

void
signal_destroy(struct signal *_signal, const struct glue_vhdl_cb *callbacks)
{
	if (_signal->connected_drivers != NULL) {
		slset_destroy(_signal->connected_drivers, callbacks->free);
	}
	callbacks->free(_signal);
}

void
signal_connect_foreign_in_driver(
	struct signal *sig,
	struct fauhdli *instance,
	struct slset *driver_set
)
{
	struct driver *drv;
	union fauhdli_value init;

	/* FIXME obtain foreign value */
	init.univ_int = 0;

	drv = driver_create(init, &instance->callbacks);
	driver_connect_non_foreign(drv, sig, &instance->callbacks);
	sig->value = init;
	
	assert(instance->callbacks.connect_in != NULL);
	instance->callbacks.connect_in(
				instance->glue_vhdl,
				sig->foreign_id, 
				drv);
	slset_add(driver_set, drv, instance->callbacks.malloc);
}

unsigned int
fauhdli_get_sig_id(const void *_sig)
{
	const struct signal *sig = (const struct signal*)_sig;

	assert(sig != NULL);
	assert(sig->is_foreign);

	return sig->foreign_id;
}

unsigned int
fauhdli_get_sig_id_driver(const void *_drv)
{
	const struct driver *drv = (const struct driver *)_drv;

	assert(drv != NULL);
	assert(drv->foreign_out == true);
	return drv->foreign_id;
}