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

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

Add vis

File size: 5.3 KB
Line 
1module CACHE_CTRLER (
2  clk,
3  read_req, write_req, data, address,      // input: requests from processor
4  acknowledge,                             // output: to processor
5  write_back_req, inval, blocknum,         // input: requests from directory
6  blk_ok, blk_data,                        // input: answers from directory
7  back_data, cache_req,blk_add,           // output: to directory
8  );   
9             
10input clk;
11input read_req, write_req, data;           // input: requests from processor
12input [`address_size:0] address;           // input: requests from processor
13input [`address_size:0] blocknum;          // input: requests from directory
14input  write_back_req, inval;              // input: requests from directory
15input blk_ok, blk_data;                    // input: answers from directory
16output acknowledge;                        // output: to processor
17output back_data;                          // output: to directory
18output cache_req;                          // output: to directory
19output [`address_size:0] blk_add;          // output: to directory
20
21Cache_reqstatus reg  cache_req;         
22wire  back_data;
23wire  acknowledge;
24reg blk_add;
25
26
27//  Registers local to the cache controler
28Block_status reg block_state;
29Cache_status reg cache_state;
30reg [`address_size:0] block_add;           // memory address of the block
31reg block_val;                             // value of the block
32
33initial begin
34  cache_state = Ready;
35  block_state = INVALID;
36  block_add = 0;
37  block_val = 0;
38  blk_add = 0;
39  cache_req = noop;
40end
41
42assign back_data =(cache_req==ok)?block_val:0;
43assign acknowledge = ((cache_state == Rgrant)||(cache_state == Wgrant))?1:0;
44
45always @(posedge clk) begin
46
47
48case ( cache_state )
49   Ready: begin            //ready to service a directory request
50      if ((inval)&&(block_add ==blocknum))         // block invalidation request
51         begin
52            block_state = INVALID ;
53            cache_req = ok;
54            cache_state = Ready;
55         end
56      else if  (write_back_req)
57         begin
58            block_state = SHARED ;
59            cache_req = ok;
60            cache_state = Ready;
61         end
62      else if  (read_req)
63         begin
64            if ((block_add != address) || (block_state == INVALID)) 
65            begin                         // read miss
66               cache_req = blk_rreq;             // ask to read block from memory
67               blk_add = address;
68               cache_state = Rwait;
69               block_state = INVALID;          //invalidates if replacement
70            end
71            else
72             begin                       // read hit
73               cache_state = Rgrant;
74               cache_req = noop;
75             end
76          end
77      else  if  (write_req)
78         begin
79            if ((block_add != address) || (block_state != EXCLUSIVE)) 
80            begin                        // write miss
81               cache_req = blk_excl;             // ask exclusive block from memory
82               blk_add = address;
83               cache_state = Wwait;
84               block_state = INVALID;          //invalidates if replacement
85            end
86            else
87             begin                       // write hit
88               cache_state = Wgrant;
89               cache_req = noop;
90             end
91          end
92      else 
93        begin
94         cache_req = noop;
95         blk_add = 0;
96        end 
97      end
98
99      Rgrant: begin               // read acknowledge to processor
100         if ((inval)&&(block_add == blocknum)) 
101          begin
102            block_state = INVALID;
103            cache_req = ok;
104            cache_state = Ready;
105          end             
106         else
107         begin
108         cache_state = Ready;
109         end
110      end
111
112      Wgrant: begin
113         if ((inval)&&(block_add == blocknum)) 
114          begin
115            block_state = INVALID;
116            cache_req = ok;
117            cache_state = Ready;
118          end             
119         else
120         begin
121         block_val = data;
122         cache_state = Ready;
123         end 
124      end
125
126      Rwait: begin
127         if ((inval)&&(block_add == blocknum)) 
128          begin
129            block_state = INVALID;
130            cache_req = ok;
131            cache_state = Ready;
132          end             
133         else if (write_back_req)
134          begin
135               cache_state = Ready;
136               //cache_req = ok;
137          end
138         else if ( blk_ok )
139         begin
140            block_val = blk_data;
141            block_add = blk_add;
142            block_state = SHARED;
143            cache_req = noop;
144            cache_state = Rgrant;
145         end
146         else cache_state = Rwait;
147      end
148
149      Wwait: begin
150         if ((inval)&&(block_add == blocknum)) 
151          begin
152            block_state = INVALID;
153            cache_req = ok;
154            cache_state = Ready;
155          end             
156         else  if (write_back_req)
157          begin
158               cache_state = Ready;
159               //cache_req = ok;               
160         end
161         else if ( blk_ok )
162         begin
163            block_val = blk_data;
164            block_add = blk_add;
165            block_state = EXCLUSIVE;
166            cache_req = noop;
167            cache_state = Wgrant;
168         end
169         else cache_state = Wwait;
170      end
171     
172      default: cache_req = noop;
173   endcase;
174end // end of always statement describing cache controller automaton
175endmodule
Note: See TracBrowser for help on using the repository browser.