-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcpu_fetch_stage.v
104 lines (93 loc) · 3.78 KB
/
cpu_fetch_stage.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
/*
* Copyright 2018 Jacob Lifshay
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
`timescale 1ns / 1ps
`include "riscv.vh"
`include "cpu.vh"
module cpu_fetch_stage(
input clk,
input reset,
output [31:2] memory_interface_fetch_address,
input [31:0] memory_interface_fetch_data,
input memory_interface_fetch_valid,
input `fetch_action fetch_action,
input [31:0] target_pc,
output reg [31:0] output_pc,
output [31:0] output_instruction,
output reg `fetch_output_state output_state
);
parameter reset_vector = 32'hXXXXXXXX;
parameter mtvec = 32'hXXXXXXXX;
reg [31:0] fetch_pc = reset_vector;
always @(posedge clk or posedge reset) output_pc <= reset ? reset_vector : ((fetch_action == `fetch_action_wait) ? output_pc : fetch_pc);
assign memory_interface_fetch_address = fetch_pc[31:2];
initial output_pc <= reset_vector;
initial output_state <= `fetch_output_state_empty;
reg [31:0] delayed_instruction = 0;
reg delayed_instruction_valid = 0;
always @(posedge clk or posedge reset) delayed_instruction <= reset ? 0 : output_instruction;
assign output_instruction = delayed_instruction_valid ? delayed_instruction : memory_interface_fetch_data;
always @(posedge clk or posedge reset) begin
if(reset)
delayed_instruction_valid <= 0;
else
delayed_instruction_valid <= fetch_action == `fetch_action_wait;
end
always @(posedge clk or posedge reset) begin
if(reset) begin
fetch_pc <= reset_vector;
output_state <= `fetch_output_state_empty;
end
else begin
case(fetch_action)
`fetch_action_default,
`fetch_action_ack_trap: begin
if(memory_interface_fetch_valid) begin
fetch_pc <= fetch_pc + 4;
output_state <= `fetch_output_state_valid;
end
else begin
fetch_pc <= mtvec;
output_state <= `fetch_output_state_trap;
end
end
`fetch_action_fence: begin
fetch_pc <= output_pc + 4;
output_state <= `fetch_output_state_empty;
end
`fetch_action_jump: begin
fetch_pc <= target_pc;
output_state <= `fetch_output_state_empty;
end
`fetch_action_error_trap,
`fetch_action_noerror_trap: begin
fetch_pc <= mtvec;
output_state <= `fetch_output_state_empty;
end
`fetch_action_wait: begin
fetch_pc <= fetch_pc;
output_state <= `fetch_output_state_valid;
end
endcase
end
end
endmodule