view CircuitNetlistCreator.c @ 27:fb4970a0c337

fix up comment in main.c
author kshalle
date Wed, 25 Feb 2015 15:40:06 -0800
parents b924d86f829e
children
line source
1 /*
2 * Copyright 2011 OpenSourceResearchInstitute.org
3 * Licensed under GNU General Public License version 2
4 *
5 * Author: seanhalle@yahoo.com
6 *
7 */
8 #include "HWSim__PingPong__HWDef/HWSim__PingPong__HWDef.h"
10 /* is an expr resolves to an actual commPath struct instance
11 */
12 #define setCommPathValuesTo( commPath, fromElIdx, outPort, toElIdx, inPort)\
13 do{\
14 commPath->idxOfFromElem = fromElIdx; \
15 commPath->idxOfFromOutPort = outPort; \
16 commPath->idxOfToElem = toElIdx; \
17 commPath->idxOfToInPort = inPort; \
18 }while(0); //macro magic for namespace
22 HWSimActivityType* createPingPongActivityType ();
24 HWSimElem* createAPingPongElem (HWSimNetlist *netlist);
26 /*This file constructs the netlist for the Hello World circuit
27 *
28 *It has two elements, each with one input port and one output port, and
29 * a single activity-type.
30 *
31 *The elements are cross-coupled, so output port of one connects to input
32 * port of the other. The input port has a single trigger, which fires
33 * the one activity-type.
34 *
35 *The activity does nothing, except send a NULL message on the output port.
36 *The activity-sim-time and communication-sim-time are both constants.
37 *
38 *Note that elements are generic. They are specialized by declaring
39 * inports and outports, and by registering triggers that fire particular
40 * activity-types.
41 */
42 HWSimNetlist *createPingPongNetlist()
43 { HWSimNetlist *netlist;
44 HWSimCommPath **commPaths;
45 HWSimElem **elems;
46 int32 numElems;
47 int32 numCommPaths;
49 netlist = malloc( sizeof(HWSimNetlist) );
50 numElems= 2;
51 elems = malloc( numElems * sizeof(HWSimElem *) );
52 netlist->numElems = numElems;
53 netlist->elems = elems;
54 netlist->numActivityTypes = 1;
55 netlist->activityTypes = malloc(netlist->numActivityTypes*sizeof(HWSimActivityType*));
57 netlist->activityTypes[PING_PONG_ACTIVITY] = createPingPongActivityType();
59 elems[0] = createAPingPongElem( netlist ); //use activity types from netlist
60 elems[1] = createAPingPongElem( netlist );
62 //make reset trigger an action on one of the elements
63 elems[1]->inPorts[-1].triggeredActivityType =
64 netlist->activityTypes[PING_PONG_ACTIVITY];
66 /*OutPorts and InPorts may have many commPaths attached.
67 * but an inPort only
68 * has one kind of activity that all incoming communications trigger. That
69 * activity can be zero time and then switch on the type of message then
70 * end with a continuation, where the continuation activity is chosen by the
71 * switch.
72 *So, a commPath only connects an out port to an in port
73 *The format is: sending elem-index, out-port, dest elem-index, in-port
74 */
75 numCommPaths = 2;
76 commPaths = malloc( numCommPaths * sizeof(HWSimCommPath *) );
77 netlist->numCommPaths= numCommPaths;
78 netlist->commPaths= commPaths;
79 //elem 0, out-port 0 to elem 1, in-port 0
80 commPaths[0]= malloc(sizeof(HWSimCommPath));
81 setCommPathValuesTo(commPaths[0],0,0,1,0);
82 commPaths[0]->hasFixedTiming = TRUE;
83 commPaths[0]->fixedFlightTime = 10; //all time is stated in (integer) units
85 //elem 1, out-port 0 to elem 0, in-port 0
86 commPaths[1]= malloc(sizeof(HWSimCommPath));
87 setCommPathValuesTo(commPaths[1], 1,0,0,0);
88 commPaths[1]->hasFixedTiming = TRUE;
89 commPaths[1]->fixedFlightTime = 10; //all time is stated in (integer) units
91 //TODO: decide how to do bidirectional commPaths, like connection to a bus
92 // thinking make it two separate commPaths with guard between in-port and out-port
94 return netlist;
95 }
98 /*
99 */
100 void
101 freePingPongNetlist (HWSimNetlist *netlist)
102 { int i;
104 for( i = 0; i < netlist->numCommPaths; i++ )
105 { free(netlist->commPaths[i]);
106 }
107 free(netlist->commPaths);
108 for( i= 0; i < netlist->numElems; i++ )
109 { HWSim_ext__free_inPortsArray( netlist->elems[i]->inPorts );
110 free(netlist->elems[i]->outPorts);
111 free(netlist->elems[i]);
112 }
114 free(netlist->activityTypes);
115 free(netlist->elems);
116 free(netlist);
117 }
119 /*This creates a simulation element. A simulation element is of the type
120 * HWSimElem. The HWSimElem data structure contains information about
121 * the input ports and output ports of the element. In particular, when
122 * a communication arrives on an input port, that triggers activity inside
123 * the element. It is here that the function is registerd, which implements
124 * the behavior of that activity.
125 *Triggered activities is the heart of HWSim. It is inside these functions
126 * that application defined behavior takes place.
127 */
128 HWSimElem *
129 createAPingPongElem( HWSimNetlist *netlist )
130 { HWSimElem *elem;
131 elem = malloc( sizeof(HWSimElem) );
132 elem->numInPorts = 1;
133 elem->numOutPorts = 1;
134 elem->inPorts = HWSim_ext__make_inPortsArray( elem->numInPorts );
135 elem->inPorts[-1].triggeredActivityType = IDLE_SPAN; //reset port
136 //point the trigger at an activity that is already in the netlist.
137 // The function below creates one of such activities.
138 //The netlist creation function first creates the activities, then
139 // it calls this function, which grabs activities from the netlist
140 elem->inPorts[0].triggeredActivityType = netlist->activityTypes[PING_PONG_ACTIVITY];
141 return elem;
142 }
144 /*Define a type of activity that is possible. The netlist creator
145 * first calls all of this kind of function, and fills the netlist
146 * with these data structures that implement activities.
147 *Then, the creator calls functions that create individual elements.
148 * Those element-creation functions grab the activity structures out
149 * of the netlist.
150 */
151 HWSimActivityType *
152 createPingPongActivityType( )
153 { HWSimActivityType *pingPongActivityType;
154 pingPongActivityType = malloc( sizeof(HWSimActivityType) );
156 //For execution speed, some special cases exist, such as activities
157 // that have no behavior, or ones that have a fixed time to complete
158 pingPongActivityType->hasBehavior = TRUE;
159 pingPongActivityType->hasTiming = TRUE;
160 pingPongActivityType->timingIsFixed = TRUE;
161 pingPongActivityType->fixedTime = 10;
162 pingPongActivityType->behaviorFn = &pingPongElem_PingActivity_behavior;
163 return pingPongActivityType;
164 }