-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold-dcache.txt
207 lines (174 loc) · 5.31 KB
/
old-dcache.txt
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
/*
Module : Data Cache
Author : Isuru Nawinne, Kisaru Liyanage
Date : 25/05/2020
Description :
This file presents a skeleton implementation of the cache controller using a Finite State Machine model.
*/
`timescale 1ns/100ps
module dcache (
clock,reset,read,write,address,writedata,readdata,busywait,
mem_read,mem_write,mem_address,mem_writedata,mem_readdata,mem_busywait
);
input read,write,clock,reset;
input [7:0] address,writedata;
output reg [7:0] readdata;
output reg busywait;
output reg mem_read, mem_write;
output reg [5:0] mem_address;
output reg [31:0] mem_writedata;
input [31:0] mem_readdata;
input mem_busywait;
//Cache Memory
reg [31:0] cache_data[7:0]; // 8 blocks, each with 4 bytes
reg [2:0] cache_tag[7:0]; // Tag for each block
reg cache_valid[7:0]; // Valid bit for each block
reg cache_dirty[7:0]; // Dirty bit for each block
//Splitting the address in to tag,index,offset
wire [2:0] index = address[4:2];
wire [2:0] tag = address[7:5];
wire [1:0] offset = address[1:0];
// Internal signals
wire [31:0] block_data;
wire [2:0] block_tag;
wire valid;
wire dirty;
reg hit;
/*
Combinational part for indexing, tag comparison for hit deciding, etc.*/
assign block_tag = cache_tag[index];
assign block_data = cache_data[index];
assign valid = cache_valid[index];
assign dirty = cache_dirty[index];
//assign hit = (valid && (block_tag == tag));
always @(*) begin
hit = #0.9 (valid && (block_tag == tag));
end
always @(*) begin
#1
case (offset)
2'b00: readdata = cache_data[index][7:0];
2'b01: readdata = cache_data[index][15:8];
2'b10: readdata = cache_data[index][23:16];
2'b11: readdata = cache_data[index][31:24];
endcase
end
always @(*)
begin
if(hit)begin
if (read || write) begin
busywait = 0;
end
end else if (read || write) begin
busywait = 1;
end else begin
busywait = 0;
end
end
//write hit
always @(posedge clock)
begin
if(hit) begin
if (write) begin
#1
case (offset)
2'b00: cache_data[index][7:0] = writedata;
2'b01: cache_data[index][15:8] = writedata;
2'b10: cache_data[index][23:16] = writedata;
2'b11: cache_data[index][31:24] = writedata;
endcase
cache_dirty[index] = 1;
end
end
end
/* Cache Controller FSM Start */
parameter IDLE = 3'b000,MEM_READ = 3'b001, MEM_WRITE = 3'b010;
reg [2:0] state, next_state;
// combinational next state logic
always @(*)
begin
case (state)
IDLE:begin
if((read || write) && !dirty && !hit)
next_state = MEM_READ;
else if ((read || write) && dirty && !hit)
next_state = MEM_WRITE;
else
next_state = IDLE;
end
MEM_READ:begin
if (!mem_busywait)
next_state = IDLE;
else
next_state = MEM_READ;
end
MEM_WRITE:begin
if (!mem_busywait)
next_state = MEM_READ;
else
next_state = MEM_WRITE;
end
default: next_state = IDLE;
endcase
end
// combinational output logic
always @(*)
begin
case(state)
IDLE:
begin
mem_read = 0;
mem_write = 0;
mem_address = 8'dx;
mem_writedata = 8'dx;
busywait = 0;
end
MEM_READ:
begin
mem_read = 1;
mem_write = 0;
mem_address = {tag, index};
mem_writedata = 32'dx;
busywait = 1;
#1 if(mem_busywait==0) begin
cache_data[index] = mem_readdata ;
cache_valid[index] = 1 ;
cache_tag[index] = tag ;
end
end
MEM_WRITE:
begin
mem_read = 0;
mem_write = 1;
mem_address = {block_tag, index};
mem_writedata = cache_data[index];
busywait = 1; // Cache is waiting for memory write
if(mem_busywait==0) begin
cache_dirty[index] = 0;
end
end
endcase
end
// sequential logic for state transitioning
integer i;
always @(posedge clock or posedge reset)
begin
if(reset) begin
state <= IDLE;
for(i = 0 ; i<8 ;i = i+1) begin
cache_dirty[i] <= 0 ;
cache_valid[i] <= 0 ;
end
end
else begin
state <= next_state;
end
end
/* Cache Controller FSM End */
initial
begin
$dumpfile("cpu_wavedata.vcd");
for(i=0;i<8;i++)
$dumpvars(1,cache_data[i]);
end
endmodule