1 |
27 |
ultra_embe |
//-----------------------------------------------------------------
|
2 |
|
|
// AltOR32
|
3 |
|
|
// Alternative Lightweight OpenRisc
|
4 |
|
|
// V2.0
|
5 |
|
|
// Ultra-Embedded.com
|
6 |
|
|
// Copyright 2011 - 2013
|
7 |
|
|
//
|
8 |
|
|
// Email: admin@ultra-embedded.com
|
9 |
|
|
//
|
10 |
|
|
// License: LGPL
|
11 |
|
|
//-----------------------------------------------------------------
|
12 |
|
|
//
|
13 |
|
|
// Copyright (C) 2011 - 2013 Ultra-Embedded.com
|
14 |
|
|
//
|
15 |
|
|
// This source file may be used and distributed without
|
16 |
|
|
// restriction provided that this copyright statement is not
|
17 |
|
|
// removed from the file and that any derivative work contains
|
18 |
|
|
// the original copyright notice and the associated disclaimer.
|
19 |
|
|
//
|
20 |
|
|
// This source file is free software; you can redistribute it
|
21 |
|
|
// and/or modify it under the terms of the GNU Lesser General
|
22 |
|
|
// Public License as published by the Free Software Foundation;
|
23 |
|
|
// either version 2.1 of the License, or (at your option) any
|
24 |
|
|
// later version.
|
25 |
|
|
//
|
26 |
|
|
// This source is distributed in the hope that it will be
|
27 |
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied
|
28 |
|
|
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
29 |
|
|
// PURPOSE. See the GNU Lesser General Public License for more
|
30 |
|
|
// details.
|
31 |
|
|
//
|
32 |
|
|
// You should have received a copy of the GNU Lesser General
|
33 |
|
|
// Public License along with this source; if not, write to the
|
34 |
|
|
// Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
35 |
|
|
// Boston, MA 02111-1307 USA
|
36 |
|
|
//-----------------------------------------------------------------
|
37 |
|
|
|
38 |
|
|
//`define CONF_FETCH_DEBUG
|
39 |
|
|
|
40 |
|
|
//-----------------------------------------------------------------
|
41 |
|
|
// Includes
|
42 |
|
|
//-----------------------------------------------------------------
|
43 |
|
|
`include "altor32_defs.v"
|
44 |
|
|
|
45 |
|
|
//-----------------------------------------------------------------
|
46 |
|
|
// Module - Instruction Fetch
|
47 |
|
|
//-----------------------------------------------------------------
|
48 |
|
|
module altor32_fetch
|
49 |
|
|
(
|
50 |
|
|
// General
|
51 |
|
|
input clk_i /*verilator public*/,
|
52 |
|
|
input rst_i /*verilator public*/,
|
53 |
|
|
|
54 |
|
|
// Instruction Fetch
|
55 |
|
|
output fetch_o /*verilator public*/,
|
56 |
|
|
output [31:0] pc_o /*verilator public*/,
|
57 |
|
|
input [31:0] data_i /*verilator public*/,
|
58 |
|
|
input data_valid_i/*verilator public*/,
|
59 |
|
|
|
60 |
|
|
// Branch target
|
61 |
|
|
input branch_i /*verilator public*/,
|
62 |
|
|
input [31:0] branch_pc_i /*verilator public*/,
|
63 |
|
|
input stall_i /*verilator public*/,
|
64 |
|
|
|
65 |
|
|
// Decoded opcode
|
66 |
|
|
output [31:0] opcode_o /*verilator public*/,
|
67 |
|
|
output [31:0] opcode_pc_o /*verilator public*/,
|
68 |
|
|
output opcode_valid_o /*verilator public*/,
|
69 |
|
|
|
70 |
|
|
// Decoded register details
|
71 |
|
|
output [4:0] ra_o /*verilator public*/,
|
72 |
|
|
output [4:0] rb_o /*verilator public*/,
|
73 |
|
|
output [4:0] rd_o /*verilator public*/
|
74 |
|
|
);
|
75 |
|
|
|
76 |
|
|
//-----------------------------------------------------------------
|
77 |
|
|
// Params
|
78 |
|
|
//-----------------------------------------------------------------
|
79 |
|
|
parameter BOOT_VECTOR = 32'h00000000;
|
80 |
|
|
parameter CACHE_LINE_SIZE_WIDTH = 5; /* 5-bits -> 32 entries */
|
81 |
|
|
|
82 |
|
|
//-----------------------------------------------------------------
|
83 |
|
|
// Registers
|
84 |
|
|
//-----------------------------------------------------------------
|
85 |
|
|
reg [31:0] r_pc;
|
86 |
|
|
reg r_rd;
|
87 |
|
|
reg [31:0] r_last_opcode;
|
88 |
|
|
reg [31:0] r_last_pc;
|
89 |
|
|
reg r_last_valid;
|
90 |
|
|
reg [31:0] d_pc;
|
91 |
|
|
|
92 |
|
|
//-------------------------------------------------------------------
|
93 |
|
|
// Next PC state machine
|
94 |
|
|
//-------------------------------------------------------------------
|
95 |
|
|
reg [31:0] v_pc;
|
96 |
|
|
|
97 |
|
|
always @ (posedge clk_i or posedge rst_i)
|
98 |
|
|
begin
|
99 |
|
|
if (rst_i)
|
100 |
|
|
begin
|
101 |
|
|
r_pc <= BOOT_VECTOR + `VECTOR_RESET;
|
102 |
|
|
d_pc <= BOOT_VECTOR + `VECTOR_RESET;
|
103 |
|
|
r_rd <= 1'b1;
|
104 |
|
|
end
|
105 |
|
|
else
|
106 |
|
|
begin
|
107 |
|
|
r_rd <= 1'b0;
|
108 |
|
|
d_pc <= pc_o;
|
109 |
|
|
|
110 |
|
|
// Branch - Next PC = branch target + 4
|
111 |
|
|
if (branch_i)
|
112 |
|
|
begin
|
113 |
|
|
r_pc <= branch_pc_i + 4;
|
114 |
|
|
end
|
115 |
|
|
// Stall - rollback to previous PC + 4
|
116 |
|
|
else if (stall_i)
|
117 |
|
|
begin
|
118 |
|
|
r_pc <= d_pc + 4;
|
119 |
|
|
end
|
120 |
|
|
// Normal sequential execution (and instruction is ready)
|
121 |
|
|
else if (data_valid_i)
|
122 |
|
|
begin
|
123 |
|
|
v_pc = r_pc + 4;
|
124 |
|
|
|
125 |
|
|
// New cache line?
|
126 |
|
|
if (v_pc[CACHE_LINE_SIZE_WIDTH-1:0] == {CACHE_LINE_SIZE_WIDTH{1'b0}})
|
127 |
|
|
begin
|
128 |
|
|
// Start fetch of next line
|
129 |
|
|
r_rd <= 1'b1;
|
130 |
|
|
end
|
131 |
|
|
|
132 |
|
|
r_pc <= v_pc;
|
133 |
|
|
end
|
134 |
|
|
end
|
135 |
|
|
end
|
136 |
|
|
|
137 |
|
|
//-------------------------------------------------------------------
|
138 |
|
|
// Pipeline storage of last PC/opcode passed to exec stage
|
139 |
|
|
//-------------------------------------------------------------------
|
140 |
|
|
always @ (posedge clk_i or posedge rst_i)
|
141 |
|
|
begin
|
142 |
|
|
if (rst_i)
|
143 |
|
|
begin
|
144 |
|
|
r_last_opcode <= 32'b0;
|
145 |
|
|
r_last_pc <= 32'b0;
|
146 |
|
|
r_last_valid <= 1'b0;
|
147 |
|
|
|
148 |
|
|
end
|
149 |
|
|
else
|
150 |
|
|
begin
|
151 |
|
|
// Record last valid instruction passed to exec stage
|
152 |
|
|
if (!stall_i | !data_valid_i)
|
153 |
|
|
begin
|
154 |
|
|
r_last_pc <= opcode_pc_o;
|
155 |
|
|
r_last_opcode <= opcode_o;
|
156 |
|
|
r_last_valid <= opcode_valid_o;
|
157 |
|
|
end
|
158 |
|
|
end
|
159 |
|
|
end
|
160 |
|
|
|
161 |
|
|
//-------------------------------------------------------------------
|
162 |
|
|
// Assignments
|
163 |
|
|
//-------------------------------------------------------------------
|
164 |
|
|
|
165 |
|
|
// Instruction Fetch
|
166 |
|
|
assign pc_o = stall_i ? d_pc : (branch_i ? branch_pc_i : (~data_valid_i ? d_pc : r_pc));
|
167 |
|
|
assign fetch_o = branch_i ? 1'b1 : r_rd;
|
168 |
|
|
|
169 |
|
|
// Opcode output
|
170 |
|
|
assign opcode_valid_o = stall_i ? r_last_valid : (data_valid_i & !branch_i);
|
171 |
|
|
assign opcode_o = stall_i ? r_last_opcode : (opcode_valid_o ? data_i : 32'b0);
|
172 |
|
|
assign opcode_pc_o = stall_i ? r_last_pc : (opcode_valid_o ? d_pc : 32'b0);
|
173 |
|
|
|
174 |
|
|
// If simulation, RA = 03 if NOP instruction
|
175 |
|
|
`ifdef SIMULATION
|
176 |
|
|
wire [7:0] v_fetch_inst = {2'b00, opcode_o[31:26]};
|
177 |
|
|
wire v_is_nop = (v_fetch_inst == `INST_OR32_NOP);
|
178 |
|
|
assign ra_o = v_is_nop ? 5'd3 : opcode_o[20:16];
|
179 |
|
|
`else
|
180 |
|
|
assign ra_o = opcode_o[20:16];
|
181 |
|
|
`endif
|
182 |
|
|
|
183 |
|
|
assign rb_o = opcode_o[15:11];
|
184 |
|
|
assign rd_o = opcode_o[25:21];
|
185 |
|
|
|
186 |
|
|
endmodule
|