source: vis_dev/vis-2.1/examples/eisenberg/eisenberg.v @ 11

Last change on this file since 11 was 11, checked in by cecile, 13 years ago

Add vis

File size: 4.5 KB
Line 
1/* the interleaving is emulated in the system process by permit variables */
2
3`define N 2
4
5/* to emulate program counter */
6typedef enum { L1, L2, L3, L4, L5, L6, L7, L8, 
7               L9, L10, L11, L12, L13, L14, L15, L16 } loc;
8
9/* state of how hard a process want to enter critical section */
10typedef enum { idle, want_in, in_cs } flag_type;
11
12module system (clk);
13input clk;
14
15/* flag? and turn are shared memory */
16flag_type reg flag0, flag1;
17reg turn;
18
19flag_type wire out_flag0, out_flag1;
20wire out_turn0, out_turn1;
21
22wire perm0, perm1;
23reg st_perm0, st_perm1; 
24// st_pterm? just show which process is chosen
25
26/* the current processes */
27process p0(clk, perm0, flag0, flag1, out_flag0, turn, out_turn0, 0);
28process p1(clk, perm1, flag0, flag1, out_flag1, turn, out_turn1, 1);
29
30/* the processes are chosen to be executed by CPU nondeterministically */
31assign perm0 = $NDset(@(posedge clk), 0, 1);
32assign perm1 = ~perm0;
33
34/* only OS/CPU can update system memory */
35initial begin flag0=idle; flag1=idle; turn=0; end
36initial st_perm0 = $ND(0,1);
37initial st_perm1 = $ND(0,1);
38always @(posedge clk) begin
39    st_perm0 = perm0; st_perm1 = perm1;
40    if (perm0==1)     begin flag0 = out_flag0; turn = out_turn0; end
41    else if(perm1==1) begin flag1 = out_flag1; turn = out_turn1; end
42end
43
44endmodule
45
46
47/* individual process */
48module process (clk, perm, flag0, flag1, out_flag, turn, out_turn, i);
49input clk;
50input perm;
51input flag0, flag1;
52output out_flag;
53input turn;
54output out_turn;
55input i;
56
57flag_type wire flag0, flag1;
58flag_type wire out_flag;
59 
60// pc and j are local to each process
61loc reg pc;
62reg j;
63
64flag_type wire flagj, flagturn;
65wire nond_exit;
66
67assign flagj = (j==0) ?       flag0 : flag1,
68       flagturn = (turn==0) ? flag0 : flag1;
69
70/* if the process decide to change system wide data */
71assign out_flag = (pc==L1) ? want_in : 
72                  (pc==L7) ? in_cs : 
73                  (pc==L16)? idle :
74                  (i==0) ? flag0 : flag1;
75assign out_turn = (pc==L11) ? i : (pc==L15) ? j : turn;
76assign nond_exit = $NDset(@(posedge clk), 0, 1);
77
78initial begin j=0; pc=L1; end
79
80// ----- the program is executed forever -----
81always @(posedge clk) begin                                           
82    if (perm==1) begin
83        case (pc)
84// repeat   flag[i]=want_in;
85            L1: begin pc=L2; end                                       
86//     j=turn;
87            L2: begin j=turn; pc=L3; end                               
88//     while (j != i)
89            L3: begin if (j!=i) pc=L4; else pc=L7; end                 
90//         do if (flag[j] != idle)
91            L4: begin if (flagj != idle) pc=L5; else pc=L6; end       
92//                then j = turn;
93            L5: begin j=turn; pc=L3; end                               
94//                else j = j+1 mod `N;
95            L6: begin if (j==`N-1) j=0; else j=j+1; pc=L3; end         
96//     flag[i] = in_cs;
97            L7: begin pc=L8; end                                       
98//     j=0;
99            L8: begin j=0; pc=L9; end                                 
100//     while ((j<`N) && (j==i || flag[j] != in_cs)) do j=j+1;
101            L9: begin                                                 
102                if ((j<`N) && ((j==i) || (flagj!=in_cs)))       
103                    begin j=j+1; pc=L9; end
104                else pc=L10;
105                end
106// until (j>=`N) && ((turn==i) || flag[turn] == idle);
107            L10: begin                                                 
108                 if ((j>=`N) && ((turn==i) || (flagturn==idle)))
109                     pc=L11;
110                 else 
111                     pc=L1;
112                 end
113// turn = i;
114            L11: begin pc=L12; end                                     
115// critical section
116           
117// ----- CRITICAL SECTION -----
118            L12: begin if (nond_exit==1) pc=L13; else pc=L12; end     
119// j = turn+1 mod `N;
120            L13: begin if (turn==`N-1) j=0; else j=turn+1; pc=L14; end 
121// while (flag[j] == idle) do j=j+1 mod `N;
122            L14: begin if (flagj==idle)                               
123                           begin
124                           if (j==`N-1) j=0;
125                           else j=j+1;
126                           pc=L14;
127                           end
128                       else pc=L15;
129                 end
130// turn = j;
131            L15: begin pc=L16; end                                     
132// flag[i] = idle;
133            L16: begin if (nond_exit==1) pc=L1; else pc=L16; end       
134                                                                       
135// ----- REMAINDER SECTION -----
136            default: ;
137        endcase
138    end
139end
140
141endmodule
Note: See TracBrowser for help on using the repository browser.