1 |
3 |
jamey.hick |
|
2 |
83 |
jamey.hick |
// The MIT License
|
3 |
|
|
|
4 |
|
|
// Copyright (c) 2006-2007 Massachusetts Institute of Technology
|
5 |
|
|
|
6 |
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 |
|
|
// of this software and associated documentation files (the "Software"), to deal
|
8 |
|
|
// in the Software without restriction, including without limitation the rights
|
9 |
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 |
|
|
// copies of the Software, and to permit persons to whom the Software is
|
11 |
|
|
// furnished to do so, subject to the following conditions:
|
12 |
|
|
|
13 |
|
|
// The above copyright notice and this permission notice shall be included in
|
14 |
|
|
// all copies or substantial portions of the Software.
|
15 |
|
|
|
16 |
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 |
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19 |
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 |
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 |
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 |
|
|
// THE SOFTWARE.
|
23 |
|
|
|
24 |
|
|
|
25 |
3 |
jamey.hick |
package mkMemClient;
|
26 |
|
|
|
27 |
|
|
|
28 |
|
|
import MemControllerTypes::*;
|
29 |
|
|
import IMemClient::*;
|
30 |
|
|
import IMemClientBackend::*;
|
31 |
|
|
import FIFOF::*;
|
32 |
|
|
import FIFO::*;
|
33 |
|
|
import mkSatCounter::*;
|
34 |
|
|
import FIFO_2::*;
|
35 |
|
|
|
36 |
|
|
`define FIFO_SIZE 2
|
37 |
|
|
|
38 |
|
|
typedef enum {
|
39 |
|
|
NORMAL,
|
40 |
|
|
WRITE_AFTER_READ
|
41 |
|
|
}
|
42 |
|
|
MemClientState
|
43 |
|
|
deriving(Bits, Eq);
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
module mkMemClient#(Integer client_num) (IMemClientBackend#(addr_type, data_type))
|
47 |
|
|
provisos
|
48 |
|
|
(Bits#(addr_type, addr_p),
|
49 |
|
|
Bits#(data_type, data_p),
|
50 |
|
|
Bits#(MemReq#(addr_type, data_type), mem_req_p)
|
51 |
|
|
);
|
52 |
|
|
|
53 |
|
|
Reg#(PRIORITY_LEVEL) priority_level <- mkReg(0);
|
54 |
|
|
FIFOF#(MemReq#(addr_type, data_type)) req_fifo <- mkFIFOF();
|
55 |
|
|
FIFOF#(Maybe#(MemReq#(addr_type, data_type))) read_fifo <- mkFIFOF();
|
56 |
|
|
FIFOF#(Maybe#(MemReq#(addr_type, data_type))) write_fifo <- mkFIFOF();
|
57 |
|
|
RWire#(MemReq#(addr_type, data_type)) read_enqueue <- mkRWire();
|
58 |
|
|
RWire#(MemReq#(addr_type, data_type)) write_enqueue <- mkRWire();
|
59 |
|
|
FIFO#(data_type) load_responses <- mkSizedFIFO(`FIFO_SIZE);
|
60 |
|
|
SatCounter#(TAdd#(1, TLog#(`FIFO_SIZE))) counter <- mkSatCounter(0);
|
61 |
|
|
Reg#(Bool) read_has_priority <- mkReg(True);
|
62 |
|
|
Reg#(Bool) write_has_priority <- mkReg(False);
|
63 |
|
|
Reg#(MemClientState) state <- mkReg(NORMAL);
|
64 |
|
|
|
65 |
|
|
rule process_input;
|
66 |
|
|
Maybe#(MemReq#(addr_type, data_type)) r_enq = read_enqueue.wget;
|
67 |
|
|
Maybe#(MemReq#(addr_type, data_type)) w_enq = write_enqueue.wget;
|
68 |
|
|
if(r_enq matches tagged Valid .r)
|
69 |
|
|
begin
|
70 |
|
|
read_fifo.enq(r_enq);
|
71 |
|
|
write_fifo.enq(w_enq);
|
72 |
|
|
end
|
73 |
|
|
else if(w_enq matches tagged Valid .w)
|
74 |
|
|
begin
|
75 |
|
|
read_fifo.enq(r_enq);
|
76 |
|
|
write_fifo.enq(w_enq);
|
77 |
|
|
end
|
78 |
|
|
endrule
|
79 |
|
|
|
80 |
|
|
rule process_queues_normal ((state == NORMAL) && (counter.value() < `FIFO_SIZE));
|
81 |
|
|
if(read_fifo.first() matches tagged Valid .r )
|
82 |
|
|
begin
|
83 |
|
|
if(write_fifo.first() matches tagged Valid .w )
|
84 |
|
|
begin
|
85 |
|
|
req_fifo.enq(r);
|
86 |
|
|
counter.up();
|
87 |
|
|
state <= WRITE_AFTER_READ;
|
88 |
|
|
end
|
89 |
|
|
else
|
90 |
|
|
begin
|
91 |
|
|
req_fifo.enq(r);
|
92 |
|
|
counter.up();
|
93 |
|
|
read_fifo.deq();
|
94 |
|
|
write_fifo.deq();
|
95 |
|
|
end
|
96 |
|
|
end
|
97 |
|
|
else
|
98 |
|
|
begin
|
99 |
|
|
if(write_fifo.first() matches tagged Valid .w )
|
100 |
|
|
begin
|
101 |
|
|
req_fifo.enq(w);
|
102 |
|
|
read_fifo.deq();
|
103 |
|
|
write_fifo.deq();
|
104 |
|
|
end
|
105 |
|
|
end
|
106 |
|
|
endrule
|
107 |
|
|
|
108 |
|
|
rule process_queues_war (state == WRITE_AFTER_READ);
|
109 |
|
|
if(write_fifo.first() matches tagged Valid .w )
|
110 |
|
|
begin
|
111 |
|
|
req_fifo.enq(w);
|
112 |
|
|
end
|
113 |
|
|
else
|
114 |
|
|
begin
|
115 |
|
|
$display("MemClient error, attempting to enqueue invalid write instruction");
|
116 |
|
|
end
|
117 |
|
|
read_fifo.deq();
|
118 |
|
|
write_fifo.deq();
|
119 |
|
|
state <= NORMAL;
|
120 |
|
|
endrule
|
121 |
|
|
|
122 |
|
|
interface IMemClient client_interface;
|
123 |
|
|
// comment about read_fifo/write_fifo....
|
124 |
|
|
method Action read_req(addr_type addr_in) if(read_fifo.notFull() && write_fifo.notFull());
|
125 |
|
|
//$display("MemClient%dReadReq: received read_req, addr:%x", fromInteger(client_num), addr_in);
|
126 |
|
|
read_enqueue.wset(tagged LoadReq{addr:addr_in});
|
127 |
|
|
endmethod
|
128 |
|
|
|
129 |
|
|
method ActionValue#(data_type) read_resp();
|
130 |
|
|
let resp = load_responses.first();
|
131 |
|
|
//$display("MemClient%dReadResp: delivered read_resp, data:%x", fromInteger(client_num), resp);
|
132 |
|
|
load_responses.deq();
|
133 |
|
|
if(counter.value() != 0)
|
134 |
|
|
begin
|
135 |
|
|
counter.down();
|
136 |
|
|
end
|
137 |
|
|
return resp;
|
138 |
|
|
endmethod
|
139 |
|
|
|
140 |
|
|
method Action write(addr_type addr_in, data_type data_in) if(read_fifo.notFull() && write_fifo.notFull());
|
141 |
|
|
//$display("MemClient%d: received write_req, addr:%x, data:%x", client_num , addr_in, data_in);
|
142 |
|
|
write_enqueue.wset(tagged StoreReq{addr:addr_in, data:data_in});
|
143 |
|
|
endmethod
|
144 |
|
|
|
145 |
|
|
method Action set_priority(PRIORITY_LEVEL prio);
|
146 |
|
|
priority_level <= prio;
|
147 |
|
|
endmethod
|
148 |
|
|
endinterface
|
149 |
|
|
|
150 |
|
|
interface request_fifo = req_fifo; // Does this compile?
|
151 |
|
|
|
152 |
|
|
method PRIORITY_LEVEL get_priority();
|
153 |
|
|
return priority_level;
|
154 |
|
|
endmethod
|
155 |
|
|
|
156 |
|
|
method Action enqueue_response(data_type data_in);
|
157 |
|
|
load_responses.enq(data_in);
|
158 |
|
|
endmethod
|
159 |
|
|
endmodule
|
160 |
|
|
|
161 |
|
|
endpackage
|