OpenCores
URL https://opencores.org/ocsvn/mips32/mips32/trunk

Subversion Repositories mips32

[/] [mips32/] [trunk/] [Classic-MIPS/] [source/] [src/] [mem.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jjf
`timescale 1ns / 1ps
2
//////////////////////////////////////////////////////////////////////////////////
3
// Company: 
4
// Engineer: 
5
// 
6
// Create Date: 2017/01/11 16:52:30
7
// Design Name: 
8
// Module Name: mem
9
// Project Name: 
10
// Target Devices: 
11
// Tool Versions: 
12
// Description: 
13
// 
14
// Dependencies: 
15
// 
16
// Revision:
17
// Revision 0.01 - File Created
18
// Additional Comments:
19
// 
20
//////////////////////////////////////////////////////////////////////////////////
21
 
22
`include "macros.v"
23
module mem(
24
input wire                      clk,
25
input wire                      rst,
26
input wire [5:0]                ctr_m,
27
input wire [1:0]                ctr_wb,
28
input wire [31:0]               ALU_out,
29
input wire [31:0]               reg2_data,
30
input wire [4:0]                write_reg,
31
output reg [31:0]               reg_mem_out,
32
output reg [31:0]               reg_ALU_out,
33
output reg [4:0]                reg_write_reg,
34
output reg [1:0]                reg_ctr_wb,
35
output wire                     isFlush,
36
output wire                     isCacheStall,
37
input wire [31:0]               pc,
38
input wire signed [31:0]        pc_branch,
39
input wire signed [31:0]        pc_jump,
40
output wire                     real_token,
41
output wire                     jump_token,
42
input wire [1:0]                bht_token,
43
/* the bht interface */
44
output wire [9:0]               bht_write_addr,
45
output wire                     bht_we,
46
output wire [33:0]              bht_din,
47
/* user ram interface */
48
input wire [12:0]                         user_addr,
49
input wire                      user_we,
50
input wire [31:0]               user_din,
51
output wire [31:0]                    user_dout
52
    );
53
 
54
    wire [1:0]                  branch_flag;
55
    wire                        memread_flag;
56
    wire                        memwrite_flag;
57
    reg signed [31:0]           reg_pc;
58
    wire [31:0]                 mem_out;
59
    wire [1:0]                  next_token;
60
 
61
    /* the MEM operation related signals */
62
    assign memread_flag = ctr_m[1];
63
    assign memwrite_flag = ctr_m[0];
64
    assign branch_flag = ctr_m[3:2];
65
    assign jump_token = |ctr_m[5:4];
66
 
67
    /* check the real branch and jump situation */
68
    assign real_token =  ((branch_flag == `BRANCH_OP_BEQ) && (ALU_out == 32'h0)) ||
69
          ( (branch_flag == `BRANCH_OP_BNE) && (ALU_out != 32'h0) );
70
 
71
    /* set the flush signal when the real_token != bht_token */
72
    assign isFlush = ISFLUSH( real_token, jump_token, bht_token, pc, pc_branch, pc_jump );
73
 
74
    function ISFLUSH;
75
    input           real_token;
76
    input           jump_token;
77
    input [1:0]     bht_token;
78
    input [31:0]    pc;
79
    input [31:0]    pc_branch;
80
    input [31:0]    pc_jump;
81
        if( (real_token | jump_token) != bht_token[1] )
82
            ISFLUSH = 1;
83
        /* check whether the branch_pc is equal to the predict pc, it not equal, Flush and JUMP to the branch_pc */
84
        else if( (real_token == 1'b1) && (bht_token[1] == 1'b1) && ( pc != pc_branch))
85
            ISFLUSH = 1;
86
        /* check the jump */
87
        else if( (jump_token == 1'b1) && (bht_token[1] == 1'b1) && ( pc != pc_jump))
88
            ISFLUSH = 1;
89
        else
90
            ISFLUSH = 0;
91
    endfunction
92
 
93
    /* set the bht interface */
94
    assign bht_write_addr = reg_pc[11:2];
95
    assign bht_we = (branch_flag == `BRANCH_OP_BEQ) | (branch_flag == `BRANCH_OP_BNE) | jump_token;
96
    assign next_token = NEXT_TOKEN(bht_token, (real_token | jump_token) );
97
    assign bht_din = real_token ? {next_token, pc_branch} : {next_token, pc_jump};
98
 
99
`ifdef __BHT_DEBUG__
100
    /* debug the predict accuracy */
101
    reg [31:0]     total_cnt;
102
    reg [31:0]     right_cnt;
103
    always @(posedge clk)
104
    begin
105
        if( rst )
106
        begin
107
            total_cnt <= 0;
108
            right_cnt <= 0;
109
        end
110
        else
111
        begin
112
            if( bht_we )
113
            begin
114
                total_cnt <= total_cnt + 1'd1;
115
                if( isFlush )
116
                begin
117
                    $display("%x predict wrong(%d|%d)", bht_write_addr, right_cnt, total_cnt);
118
                end
119
                else
120
                begin
121
                    right_cnt <= right_cnt + 1'b1;
122
                    $display("%x predict right(%d|%d)", bht_write_addr, right_cnt, total_cnt);
123
                end
124
            end
125
        end
126
    end
127
`endif
128
 
129
 
130
    /* calculate the next state */
131
    function [1:0] NEXT_TOKEN;
132
    input [1:0] bht_token;
133
    input       all_real_token;
134
    case( bht_token )
135
    2'b00:  begin
136
                if( all_real_token )
137
                    NEXT_TOKEN = 2'b01;
138
                else
139
                    NEXT_TOKEN = 2'b00;
140
            end
141
    2'b01:  begin
142
                if( all_real_token )
143
                    NEXT_TOKEN = 2'b10;
144
                else
145
                    NEXT_TOKEN = 2'b00;
146
            end
147
    2'b10:  begin
148
                if( all_real_token )
149
                    NEXT_TOKEN = 2'b11;
150
                else
151
                    NEXT_TOKEN = 2'b01;
152
            end
153
    2'b11:  begin
154
                if( all_real_token )
155
                    NEXT_TOKEN = 2'b11;
156
                else
157
                    NEXT_TOKEN = 2'b10;
158
            end
159
    endcase
160
    endfunction
161
 
162
    //Instantiate of the SimpleCache
163
    SimpleCache SimpleCache_i(
164
        .clk(               clk             ),
165
        .rst(               rst             ),
166
        .CPU_read_en(       memread_flag    ),
167
        .CPU_read_dout(     mem_out     ),
168
        .CPU_write_en(      memwrite_flag   ),
169
        .CPU_write_din(     reg2_data       ),
170
        .CPU_addr(          ALU_out         ),
171
        .isCacheStall(       isCacheStall     ),
172
        .mem_b_we(          user_we         ),
173
        .mem_b_addr(        user_addr       ),
174
        .mem_b_din(         user_din        ),
175
        .mem_b_dout(        user_dout       )
176
    );
177
 
178
    /* register data to the next stage */
179
    always @(posedge clk)
180
    begin
181
        if( rst )
182
        begin
183
            reg_ALU_out <= 0;
184
            reg_write_reg <= 0;
185
            reg_ctr_wb <= 0;
186
            reg_mem_out <= 0;
187
            reg_pc <= 0;
188
        end
189
        else
190
        begin
191
            if( ~isCacheStall )
192
            begin
193
                if( ctr_m[5:4] == `JUMP_JAL)
194
                    reg_ALU_out <= reg_pc + 3'd4;
195
                else
196
                    reg_ALU_out <= ALU_out;
197
                reg_write_reg <= write_reg;
198
                reg_ctr_wb <= ctr_wb;
199
                reg_mem_out <= mem_out;
200
                reg_pc <= pc;
201
            end
202
        end
203
    end
204
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.