-
Notifications
You must be signed in to change notification settings - Fork 1
/
SystemVerilogCSP.sv
316 lines (298 loc) · 9.29 KB
/
SystemVerilogCSP.sv
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
//-------------------------------------------------------------------------------------------------
// Written by Arash Saifhashemi, [email protected]
// SystemVerilogCSP: Channel interface for modeling channel based asynchronous circuits
// USC Asynchronous CAD/VLSI Group
// University of Southern California
// http://async.usc.edu
//-------------------------------------------------------------------------------------------------
`timescale 1ns/1ps
// uncomment the following line to enable stall calculation and display
`define displayStalls 1
//uncomment the following line for automatic deadlock detection
`define detectDeadlock 1
`define defaultWatchDogTime 100ns
// understand "task send" "task receive" ======================================= zx ==========================
package SystemVerilogCSP;
typedef enum {idle, r_pend, s_pend, s12m_pend} ChannleStatus;
typedef enum {P2PhaseBD, P4PhaseBD, P1of2, P1of4} ChannelProtocol;
typedef enum {ZERO, ONE, NUTRAL} V1of2;
typedef logic [3:0] Channel1Of4 ;
endpackage : SystemVerilogCSP
//-------------------------------------------------------------------------------------------------
import SystemVerilogCSP::*;
//-------------------------------------------------------------------------------------------------
interface Channel;
parameter SHARED = 0;
parameter WIDTH = 8;
parameter ChannelProtocol hsProtocol = P2PhaseBD;
parameter NUMBER_OF_RECEIVERS = 1;
`ifdef detectDeadlock
time lastSendEvent = 0;
time lastReceiveEvent = 0;
`endif
ChannleStatus status = idle; //Stores the status of a channel
logic req=0, ack=0, e=1; //Handshaking signals
logic oReq, oAck, oE; //Shadow handshaking signals
logic hsSenderPhase=1;
logic hsReceiverPhase=1;
logic [WIDTH-1:0] data=0, data0=0, data1=0; //Actual data being communicated
logic [WIDTH-1:0] oData, oData0, oData1; //Actual data being communicated
logic _RESET = 1;
Channel1Of4 [((WIDTH+1)/2)-1:0] data_1of4;
integer receiveCounter =0;
semaphore receivers = new(0); //Mehrdad: It seems this semaphore is not in use
genvar i;
always @oReq req = oReq;
always @oAck ack = oAck;
always @oData data = oData;
always @oData0 data0 = oData0;
always @oData1 data1 = oData1;
always @oE ack=~oE;
always @ack e = ~ack;
//always @_RESET ack=~RESET;
`ifdef detectDeadlock
always begin
# (`defaultWatchDogTime);
fork
if(status==s_pend) begin
if(($time - lastSendEvent) > `defaultWatchDogTime) begin
#(lastSendEvent - ($time - 2*`defaultWatchDogTime)); // $display("###Deadlock Detected on %m @ %t",lastSendEvent);
wait(0);
end
end
if(status==r_pend) begin
if(($time - lastReceiveEvent) > `defaultWatchDogTime) begin
#(lastReceiveEvent - ($time - 2*`defaultWatchDogTime)); // $display("###Deadlock Detected on %m @ %t",lastReceiveEvent);
wait(0);
end
end
join
end
`endif
//-------------------------------------------------------------------------------------------------
function Channel1Of4 [((WIDTH+1)/2)-1:0] SingleRailToP1of4 ();
for (integer i = 0 ; i <= WIDTH-2 ; i+=2) begin
case ({data [i+1], data[i]})
2'b00: data_1of4[i/2] = 4'b0001;
2'b01: data_1of4[i/2] = 4'b0010;
2'b10: data_1of4[i/2] = 4'b0100;
2'b11: data_1of4[i/2] = 4'b1000;
endcase
end
SingleRailToP1of4 = data_1of4;
endfunction
//-------------------------------------------------------------------------------------------------
function logic [WIDTH-1:0] P1of4ToSingleRail ();
for (integer i = 0 ; i <= WIDTH-2 ; i+=2) begin
case ( data_1of4 [i/2])
4'b0001: data[i/2] = 2'b00;
4'b0010: data[i/2] = 2'b01;
4'b0100: data[i/2] = 2'b10;
4'b1000: data[i/2] = 2'b11;
endcase
end
P1of4ToSingleRail = data_1of4;
endfunction
//-------------------------------------------------------------------------------------------------
//Communication Action Tasks
//-------------------------------------------------------------------------------------------------
task Send (input logic[WIDTH-1:0] d);
`ifdef displayStalls
time start,stall;
start = $time;
`endif
`ifdef detectDeadlock
lastSendEvent = $time;
`endif
if(hsProtocol == P4PhaseBD || hsProtocol == P1of2) begin
data = d;
data0 = ~d;
data1 = d;
req = 1;
status = s_pend; //Set the status to s_pend before wait
wait (ack == 1 );
data0 = 0;
data1 = 0;
req = 0;
wait (ack == 0 );
status = idle;
end
else if (hsProtocol == P2PhaseBD) begin
data = d;
data0 = ~d;
req = hsSenderPhase;
status = s_pend; //Set the status to s_pend before wait
wait (ack == hsSenderPhase );
status = idle;
hsSenderPhase = ~hsSenderPhase;
end
`ifdef displayStalls
stall = $time - start;
// if(stall != 0) $display("### %m Stalled(%d) @ %t",stall,$time);
`endif
endtask
//-------------------------------------------------------------------------------------------------
task SplitSend (input logic[WIDTH-1:0] d, input integer part, input integer FL = 0);
case(hsProtocol)
P1of2: P4PhaseBD:
begin
case (part)
1: begin
data <= #FL d;
data0 <= #FL ~d;
data1 <= #FL d;
req = 1;
status = s_pend; //Set the status to s_pend before wait
end
2: begin
wait (ack == 1 );
end
3: begin
data0 = 0;
data1 = 0;
req = 0;
end
4: begin
wait (ack == 0 );
status = idle;
end
endcase
end //P1of2, P4PhaseBD
P2PhaseBD: begin
case (part)
1: begin
data = d; //Mehrdad: Do we need to have #FL here is well?
data0 = ~d;
req = hsSenderPhase;
status = s_pend; //Set the status to s_pend before wait
end
2: begin
wait (ack == hsSenderPhase );
status = idle;
hsSenderPhase = ~hsSenderPhase;
end
endcase
end //P2PhaseBD
endcase
endtask
//-------------------------------------------------------------------------------------------------
task Receive (output logic[WIDTH-1:0] d);
`ifdef displayStalls
time start,stall;
start = $time;
`endif
`ifdef detectDeadlock
lastReceiveEvent = $time;
`endif
if (hsProtocol==P4PhaseBD || hsProtocol == P1of2) begin
status = r_pend;
if (hsProtocol == P1of2 )
wait ( (&(data0 | data1)==1) );
else begin
wait (req == 1 );
if (SHARED)
req = 'z; // Inhibit other receivers from receiving
end
d = data1;
data = data1;
//If the last receiver:
if (receiveCounter == NUMBER_OF_RECEIVERS-1) begin
ack = 1;
if (hsProtocol == P1of2)
wait ( (|(data0 | data1)==0) );
else
wait (req == 0 );
ack = 0;
status = idle;
receiveCounter=0;
#0; //Release the control to other processes
end
else begin // If not the last receiver wait for other receivers to finish
status = s12m_pend;
receiveCounter++;
wait (receiveCounter ==0); //Wait for other Receivers to finish.
end
end //P4PhaseBD or P1of2
else if (hsProtocol == P2PhaseBD)begin
status = r_pend; //Set the status to r_pend before wait
wait (req == hsReceiverPhase );
if (SHARED)
req = 'z; // Inhibit other receivers from receiving
d = data;
//Is this the last receiver?
if (receiveCounter == NUMBER_OF_RECEIVERS-1) begin
ack = hsReceiverPhase;
status = idle;
receiveCounter=0;
#0; //Release the control to other processes
hsReceiverPhase=~hsReceiverPhase;
end
else begin //Wait for all other receivers to finish receiving
status = s12m_pend;
receiveCounter++;
wait (receiveCounter ==0 );
#0; //Release the control to other processes
end
end
`ifdef displayStalls
stall = $time - start;
// if(stall != 0) $display("### %m Stalled(%d) @ %t",stall,$time);
`endif
endtask
//-------------------------------------------------------------------------------------------------
task SplitReceive (output logic[WIDTH-1:0] d, input integer part);
case(hsProtocol)
P1of2:
P4PhaseBD:
begin
case (part)
1: begin
status = r_pend;
if (hsProtocol == P1of2 )
wait ( (&(data0 | data1)==1) );
else
wait (req == 1 );
end
2: begin
d = data1;
ack = 1;
end
3: begin
if (hsProtocol == P1of2 )
wait ( (|(data0 | data1)==0) );
else
wait (req == 0 );
end
4:begin
ack = 0;
status = idle;
end
endcase
end //P1of2 or P4PhaseBD
P2PhaseBD: begin
case (part)
1: begin
status = r_pend; //Set the status to r_pend before wait
wait (req == hsReceiverPhase );
d = data;
end
2: begin
ack = hsReceiverPhase;
status = idle;
hsReceiverPhase = ~ hsReceiverPhase;
end
endcase
end //P2PhaseBD
endcase
endtask
//-------------------------------------------------------------------------------------------------
task Peek (output logic[WIDTH-1:0] d);
wait (status != idle && status != r_pend );
d = data;
endtask
//-------------------------------------------------------------------------------------------------
//probe_wait_input: used on an input/output port. wait until other party starts communication
task Probe_wait_input () ;
wait (status != idle);
endtask
endinterface: Channel