Add support for text macros with arguments.
[iverilog.git] / vpip / vpi_callback.c
blobe6645153a0968aa9f2fda9e8c415bde42460cae8
1 /*
2 * Copyright (c) 1999 Stephen Williams (steve@icarus.com)
4 * This source code is free software; you can redistribute it
5 * and/or modify it in source code form under the terms of the GNU
6 * General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 #ifdef HAVE_CVS_IDENT
20 #ident "$Id: vpi_callback.c,v 1.2 2002/08/12 01:35:05 steve Exp $"
21 #endif
23 # include "vpi_priv.h"
24 # include <stdlib.h>
25 # include <assert.h>
27 static struct __vpirt vpip_callback_rt = {
28 vpiCallback,
38 * This function is called by the scheduler to execute an event of
39 * some sort. The parameter is a vpiHandle of the callback that is to
40 * be executed.
42 static void vpip_call_callback(void*cp)
44 unsigned long now;
45 struct __vpiCallback*rfp = (struct __vpiCallback*)cp;
46 assert(rfp->base.vpi_type->type_code == vpiCallback);
48 switch (rfp->cb_data.time->type) {
49 case vpiSuppressTime:
50 case vpiScaledRealTime: /* XXXX not supported */
51 break;
53 case vpiSimTime:
54 now = ((struct __vpiTimeVar*)vpip_sim_time())->time;
55 rfp->cb_data.time->low = now;
56 rfp->cb_data.time->high = 0;
57 break;
60 rfp->cb_data.cb_rtn(&rfp->cb_data);
61 free(rfp);
65 * This function is called by the product when it changes the value of
66 * a signal. It arranges for all the value chance callbacks to be
67 * called.
69 void vpip_run_value_changes(struct __vpiSignal*sig)
71 struct __vpiCallback*cur;
73 while (sig->mfirst) {
74 cur = sig->mfirst;
75 sig->mfirst = cur->next;
76 if (sig->mfirst == 0)
77 sig->mlast = 0;
78 cur->next = 0;
79 cur->ev = vpip_sim_insert_event(0, cur, vpip_call_callback, 0);
84 * Handle read-only synch events. This causes the callback to be
85 * scheduled for a moment at the end of the time period. This method
86 * handles scheduling with itme delays.
88 static void go_readonly_synch(struct __vpiCallback*rfp)
90 unsigned long tim;
91 assert(rfp->cb_data.time);
92 assert(rfp->cb_data.time->type == vpiSimTime);
93 assert(rfp->cb_data.time->high == 0);
94 tim = rfp->cb_data.time->low;
95 rfp->ev = vpip_sim_insert_event(tim, rfp, vpip_call_callback, 1);
99 * To schedule a value change, attach the callback to the signal to me
100 * monitored. I'll be inserted as an event later.
102 static void go_value_change(struct __vpiCallback*rfp)
104 struct __vpiSignal*sig = (struct __vpiSignal*)rfp->cb_data.obj;
105 assert((sig->base.vpi_type->type_code == vpiReg)
106 || (sig->base.vpi_type->type_code == vpiNet));
109 /* If there are no monitor events, start the list. */
110 if (sig->mfirst == 0) {
111 rfp->sig = sig;
112 rfp->next = 0;
113 sig->mfirst = rfp;
114 sig->mlast = rfp;
115 return;
118 /* Put me at the end of the list. Remember that the monitor
119 points to the *last* item in the list. */
120 rfp->sig = sig;
121 rfp->next = 0;
122 sig->mlast->next = rfp;
126 * Register callbacks. This supports a variety of callback reasons,
127 * mostly by dispatching them to a type specific handler.
129 vpiHandle vpi_register_cb(p_cb_data data)
131 struct __vpiCallback*rfp = calloc(1, sizeof(struct __vpiCallback));
132 rfp->base.vpi_type = &vpip_callback_rt;
133 rfp->cb_data = *data;
135 switch (data->reason) {
136 case cbReadOnlySynch:
137 go_readonly_synch(rfp);
138 break;
140 case cbValueChange:
141 go_value_change(rfp);
142 break;
144 default:
145 assert(0);
148 return &(rfp->base);
152 int vpi_remove_cb(vpiHandle ref)
154 struct __vpiCallback*rfp = (struct __vpiCallback*)ref;
155 assert(ref->vpi_type->type_code == vpiCallback);
157 if (rfp->ev) {
159 /* callbacks attached to events are easy. */
160 vpip_sim_cancel_event(rfp->ev);
162 } else if (rfp->sig) {
164 /* callbacks to signals need to be removed from the
165 signal's list of monitor callbacks. */
166 struct __vpiSignal*sig = rfp->sig;
168 if (sig->mfirst == rfp) {
169 sig->mfirst = rfp->next;
170 if (sig->mfirst == 0)
171 sig->mlast = 0;
172 rfp->next = 0;
173 rfp->sig = 0;
174 } else {
175 struct __vpiCallback*cur = sig->mfirst;
176 while (cur->next != rfp) {
177 assert(cur->next);
178 cur = cur->next;
180 cur->next = rfp->next;
181 if (cur->next == 0)
182 sig->mlast = cur;
184 } else {
185 assert(0);
188 free(rfp);
189 return 0;
193 * $Log: vpi_callback.c,v $
194 * Revision 1.2 2002/08/12 01:35:05 steve
195 * conditional ident string using autoconfig.
197 * Revision 1.1 2001/03/14 19:27:44 steve
198 * Rearrange VPI support libraries.
200 * Revision 1.8 2000/08/20 17:49:05 steve
201 * Clean up warnings and portability issues.
203 * Revision 1.7 2000/03/31 07:08:39 steve
204 * allow cancelling of cbValueChange events.
206 * Revision 1.6 2000/02/23 02:56:56 steve
207 * Macintosh compilers do not support ident.
209 * Revision 1.5 1999/12/15 04:01:14 steve
210 * Add the VPI implementation of $readmemh.
212 * Revision 1.4 1999/11/07 20:33:30 steve
213 * Add VCD output and related system tasks.
215 * Revision 1.3 1999/10/29 03:37:22 steve
216 * Support vpiValueChance callbacks.
218 * Revision 1.2 1999/10/28 04:47:57 steve
219 * Support delay in constSync callback.
221 * Revision 1.1 1999/10/28 00:47:25 steve
222 * Rewrite vvm VPI support to make objects more
223 * persistent, rewrite the simulation scheduler
224 * in C (to interface with VPI) and add VPI support
225 * for callbacks.