-
Notifications
You must be signed in to change notification settings - Fork 1
/
Associative_Memory.v
executable file
·148 lines (137 loc) · 5 KB
/
Associative_Memory.v
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
`timescale 1ns / 1ps
module Associative_Memory (
input clk,
input clear,
input pred_result, // 上次的预测结�?
input wire query,
input wire store,
input wire [31:0] PC_query, // 要预测的PC
input wire [31:0] PC_update,
input wire [31:0] Next_PC, // 实际下一条PC
output reg success, // 查询结果
output reg [31:0] predict // 预测结果
);
reg [31:0] PC_Store [31:0];
reg [31:0] Branch_Addr [31:0];
reg [1:0] Predict_State [31:0];
reg [6:0] Used [31:0];
integer i;
integer update = 0;
integer has_empty = 0;
integer max_used = 0;
integer max_index = 0;
parameter
Strong_not_token = 0,
Weak_not_token = 1,
Weak_token = 2,
Strong_token = 3;
always @(posedge clear or posedge query or posedge store) begin
//always @(clk or clear) begin
if (clear) begin
for (i = 0; i < 32; i = i + 1) begin
//Branch_Addr[i] <= 0;
// Predict_State[i] <= Strong_token;
Used[i] <= 0;
end
predict <= 0;
success <= 0;
end
else begin
// predict
success <= 0;
if (query) begin
for (i = 0; i < 32; i = i + 1) begin
if (PC_query == PC_Store[i]) begin
success <= 1;
predict <= Branch_Addr[i];
// 把Used位置�?
Used[i] <= 0;
// 其他行的Used位加1
end
else begin
Used[i] <= Used[i] + 1;
end
end
end
end
end
always @(negedge clk) begin
/*if (store) begin
PC_Store[0] = PC_update;
Branch_Addr[0] = Next_PC;
end*/
if (clear) begin
for (i = 0; i < 32; i = i + 1) begin
Branch_Addr[i] <= 0;
Predict_State[i] <= Strong_token;
PC_Store[i] <= -1;
end
end
if (store) begin
for (i = 0; i < 32; i = i + 1) begin
if (PC_Store[i] == PC_update) begin
update = 1;
case (Predict_State[i])
Strong_not_token:
if(pred_result) begin
Predict_State[i] <= Strong_not_token;
end
else begin
Predict_State[i] <= Weak_not_token;
end
Weak_not_token:
if(pred_result) begin
Predict_State[i] <= Strong_not_token;
end
else begin
Predict_State[i] <= Weak_token;
Branch_Addr[i] <= Next_PC;
end
Weak_token:
if(pred_result) begin
Predict_State[i] <= Weak_not_token;
end
else begin
Predict_State[i] <= Strong_token;
Branch_Addr[i] <= Next_PC;
end
Strong_token:
if (pred_result) begin
Predict_State[i] <= Weak_token;
Branch_Addr[i] <= Next_PC;
end
else begin
Predict_State[i] <= Strong_token;
Branch_Addr[i] <= Next_PC;
end
endcase // Predict_State[i]
end
end
if(update == 0) begin
// 判断有没有空�?
for (i = 0; i < 32 && has_empty == 0; i = i + 1) begin
if(PC_Store[i] == -1) begin
has_empty = 1;
Predict_State[i] = Strong_not_token;
PC_Store[i] = PC_update;
Branch_Addr[i] = Next_PC;
end
end
if(has_empty == 0) begin
// 找Used�?大的淘汰
for (i = 0; i < 32; i = i + 1) begin
if(Used[i] > max_used) begin
max_used = Used[i];
max_index = i;
end
end
Predict_State[max_index] = Strong_not_token;
PC_Store[max_index] = PC_update;
Branch_Addr[max_index] = Next_PC;
end
end
has_empty <= 0;
update <= 0;
end
end
endmodule