1 |
2 |
alfik |
/*
|
2 |
|
|
* Copyright (c) 2014, Aleksander Osman
|
3 |
|
|
* All rights reserved.
|
4 |
|
|
*
|
5 |
|
|
* Redistribution and use in source and binary forms, with or without
|
6 |
|
|
* modification, are permitted provided that the following conditions are met:
|
7 |
|
|
*
|
8 |
|
|
* * Redistributions of source code must retain the above copyright notice, this
|
9 |
|
|
* list of conditions and the following disclaimer.
|
10 |
|
|
*
|
11 |
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
12 |
|
|
* this list of conditions and the following disclaimer in the documentation
|
13 |
|
|
* and/or other materials provided with the distribution.
|
14 |
|
|
*
|
15 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
16 |
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
17 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
18 |
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
19 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
20 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
21 |
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
22 |
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
23 |
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
24 |
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
25 |
|
|
*/
|
26 |
|
|
|
27 |
|
|
`include "defines.v"
|
28 |
|
|
|
29 |
|
|
module decode(
|
30 |
|
|
input clk,
|
31 |
|
|
input rst_n,
|
32 |
|
|
|
33 |
|
|
input dec_reset,
|
34 |
|
|
|
35 |
|
|
//global input
|
36 |
|
|
input [63:0] cs_cache,
|
37 |
|
|
|
38 |
|
|
input protected_mode,
|
39 |
|
|
|
40 |
|
|
//eip
|
41 |
|
|
input pr_reset,
|
42 |
|
|
input [31:0] prefetch_eip,
|
43 |
|
|
output reg [31:0] eip,
|
44 |
|
|
|
45 |
|
|
//fetch interface
|
46 |
|
|
input [3:0] fetch_valid,
|
47 |
|
|
input [63:0] fetch,
|
48 |
|
|
input fetch_limit,
|
49 |
|
|
input fetch_page_fault,
|
50 |
|
|
|
51 |
|
|
output [3:0] dec_acceptable,
|
52 |
|
|
|
53 |
|
|
//exceptions
|
54 |
|
|
output reg dec_gp_fault,
|
55 |
|
|
output reg dec_ud_fault,
|
56 |
|
|
output reg dec_pf_fault,
|
57 |
|
|
|
58 |
|
|
//pipeline
|
59 |
|
|
input micro_busy,
|
60 |
|
|
output dec_ready,
|
61 |
|
|
|
62 |
|
|
output [95:0] decoder,
|
63 |
|
|
output [31:0] dec_eip,
|
64 |
|
|
output dec_operand_32bit,
|
65 |
|
|
output dec_address_32bit,
|
66 |
|
|
output [1:0] dec_prefix_group_1_rep,
|
67 |
|
|
output dec_prefix_group_1_lock,
|
68 |
|
|
output [2:0] dec_prefix_group_2_seg,
|
69 |
|
|
output dec_prefix_2byte,
|
70 |
|
|
output [3:0] dec_consumed,
|
71 |
|
|
output [2:0] dec_modregrm_len,
|
72 |
|
|
output dec_is_8bit,
|
73 |
|
|
output [6:0] dec_cmd,
|
74 |
|
|
output [3:0] dec_cmdex,
|
75 |
|
|
output dec_is_complex
|
76 |
|
|
);
|
77 |
|
|
|
78 |
|
|
//------------------------------------------------------------------------------
|
79 |
|
|
|
80 |
|
|
wire enable;
|
81 |
|
|
wire instr_prefix;
|
82 |
|
|
wire instr_finished;
|
83 |
|
|
wire stop;
|
84 |
|
|
|
85 |
|
|
wire [3:0] consume_count;
|
86 |
|
|
|
87 |
|
|
wire gp_fault;
|
88 |
|
|
wire pf_fault;
|
89 |
|
|
|
90 |
|
|
wire is_prefix;
|
91 |
|
|
wire [3:0] decoder_count;
|
92 |
|
|
|
93 |
|
|
wire consume_one;
|
94 |
|
|
wire consume_one_one;
|
95 |
|
|
wire consume_one_two;
|
96 |
|
|
wire consume_one_three;
|
97 |
|
|
wire consume_call_jmp_imm;
|
98 |
|
|
wire consume_modregrm_one;
|
99 |
|
|
wire consume_one_imm;
|
100 |
|
|
wire consume_modregrm_imm;
|
101 |
|
|
wire consume_mem_offset;
|
102 |
|
|
|
103 |
|
|
//------------------------------------------------------------------------------
|
104 |
|
|
|
105 |
|
|
reg gp_fault_last;
|
106 |
|
|
reg pf_fault_last;
|
107 |
|
|
|
108 |
|
|
//------------------------------------------------------------------------------
|
109 |
|
|
|
110 |
|
|
wire dec_ready_one;
|
111 |
|
|
wire dec_ready_one_one;
|
112 |
|
|
wire dec_ready_one_two;
|
113 |
|
|
wire dec_ready_one_three;
|
114 |
|
|
wire dec_ready_2byte_one;
|
115 |
|
|
wire dec_ready_modregrm_one;
|
116 |
|
|
wire dec_ready_2byte_modregrm;
|
117 |
|
|
wire dec_ready_call_jmp_imm;
|
118 |
|
|
wire dec_ready_one_imm;
|
119 |
|
|
wire dec_ready_2byte_imm;
|
120 |
|
|
wire dec_ready_mem_offset;
|
121 |
|
|
wire dec_ready_modregrm_imm;
|
122 |
|
|
wire dec_ready_2byte_modregrm_imm;
|
123 |
|
|
|
124 |
|
|
wire [3:0] consume_count_local;
|
125 |
|
|
|
126 |
|
|
wire dec_is_modregrm;
|
127 |
|
|
|
128 |
|
|
decode_ready decode_ready_inst(
|
129 |
|
|
|
130 |
|
|
.enable (enable), //input
|
131 |
|
|
.is_prefix (is_prefix), //input
|
132 |
|
|
.decoder_count (decoder_count), //input [3:0]
|
133 |
|
|
|
134 |
|
|
.decoder (decoder), //input [95:0]
|
135 |
|
|
|
136 |
|
|
.dec_operand_32bit (dec_operand_32bit), //input
|
137 |
|
|
.dec_address_32bit (dec_address_32bit), //input
|
138 |
|
|
|
139 |
|
|
.dec_prefix_2byte (dec_prefix_2byte), //input
|
140 |
|
|
.dec_modregrm_len (dec_modregrm_len), //input [2:0]
|
141 |
|
|
|
142 |
|
|
|
143 |
|
|
.dec_ready_one (dec_ready_one), //output
|
144 |
|
|
.dec_ready_one_one (dec_ready_one_one), //output
|
145 |
|
|
.dec_ready_one_two (dec_ready_one_two), //output
|
146 |
|
|
.dec_ready_one_three (dec_ready_one_three), //output
|
147 |
|
|
.dec_ready_2byte_one (dec_ready_2byte_one), //output
|
148 |
|
|
.dec_ready_modregrm_one (dec_ready_modregrm_one), //output
|
149 |
|
|
.dec_ready_2byte_modregrm (dec_ready_2byte_modregrm), //output
|
150 |
|
|
.dec_ready_call_jmp_imm (dec_ready_call_jmp_imm), //output
|
151 |
|
|
.dec_ready_one_imm (dec_ready_one_imm), //output
|
152 |
|
|
.dec_ready_2byte_imm (dec_ready_2byte_imm), //output
|
153 |
|
|
.dec_ready_mem_offset (dec_ready_mem_offset), //output
|
154 |
|
|
.dec_ready_modregrm_imm (dec_ready_modregrm_imm), //output
|
155 |
|
|
.dec_ready_2byte_modregrm_imm (dec_ready_2byte_modregrm_imm), //output
|
156 |
|
|
|
157 |
|
|
.consume_one (consume_one), //input
|
158 |
|
|
.consume_one_one (consume_one_one), //input
|
159 |
|
|
.consume_one_two (consume_one_two), //input
|
160 |
|
|
.consume_one_three (consume_one_three), //input
|
161 |
|
|
.consume_call_jmp_imm (consume_call_jmp_imm), //input
|
162 |
|
|
.consume_modregrm_one (consume_modregrm_one), //input
|
163 |
|
|
.consume_one_imm (consume_one_imm), //input
|
164 |
|
|
.consume_modregrm_imm (consume_modregrm_imm), //input
|
165 |
|
|
.consume_mem_offset (consume_mem_offset), //input
|
166 |
|
|
|
167 |
|
|
.consume_count_local (consume_count_local), //output [3:0]
|
168 |
|
|
.dec_is_modregrm (dec_is_modregrm) //output
|
169 |
|
|
);
|
170 |
|
|
|
171 |
|
|
//------------------------------------------------------------------------------
|
172 |
|
|
|
173 |
|
|
wire [3:0] prefix_count;
|
174 |
|
|
wire prefix_group_1_lock;
|
175 |
|
|
|
176 |
|
|
decode_prefix decode_prefix_inst(
|
177 |
|
|
.clk (clk),
|
178 |
|
|
.rst_n (rst_n),
|
179 |
|
|
|
180 |
|
|
.cs_cache (cs_cache), //input [63:0]
|
181 |
|
|
.dec_is_modregrm (dec_is_modregrm), //input
|
182 |
|
|
.decoder (decoder), //input [95:0]
|
183 |
|
|
|
184 |
|
|
.instr_prefix (instr_prefix), //input
|
185 |
|
|
.instr_finished (instr_finished), //input
|
186 |
|
|
|
187 |
|
|
.dec_operand_32bit (dec_operand_32bit), //output
|
188 |
|
|
.dec_address_32bit (dec_address_32bit), //output
|
189 |
|
|
|
190 |
|
|
.dec_prefix_group_1_rep (dec_prefix_group_1_rep), //output [1:0]
|
191 |
|
|
.dec_prefix_group_1_lock (dec_prefix_group_1_lock), //output
|
192 |
|
|
.dec_prefix_group_2_seg (dec_prefix_group_2_seg), //output [2:0],
|
193 |
|
|
|
194 |
|
|
.dec_prefix_2byte (dec_prefix_2byte), //output
|
195 |
|
|
.dec_modregrm_len (dec_modregrm_len), //output [2:0]
|
196 |
|
|
|
197 |
|
|
.prefix_count (prefix_count), // output [3:0]
|
198 |
|
|
.is_prefix (is_prefix), // output
|
199 |
|
|
.prefix_group_1_lock (prefix_group_1_lock) // output
|
200 |
|
|
);
|
201 |
|
|
|
202 |
|
|
//------------------------------------------------------------------------------
|
203 |
|
|
|
204 |
|
|
decode_regs decode_regs_inst(
|
205 |
|
|
.clk (clk),
|
206 |
|
|
.rst_n (rst_n),
|
207 |
|
|
|
208 |
|
|
.dec_reset (dec_reset), //input
|
209 |
|
|
|
210 |
|
|
.fetch_valid (fetch_valid), //input [3:0]
|
211 |
|
|
.fetch (fetch), //input [63:0]
|
212 |
|
|
|
213 |
|
|
.prefix_count (prefix_count), //input [3:0]
|
214 |
|
|
.consume_count (consume_count), //input [3:0]
|
215 |
|
|
|
216 |
|
|
.dec_acceptable (dec_acceptable), //output [3:0]
|
217 |
|
|
|
218 |
|
|
.decoder (decoder), //output [95:0]
|
219 |
|
|
.decoder_count (decoder_count) //output [3:0]
|
220 |
|
|
);
|
221 |
|
|
|
222 |
|
|
//------------------------------------------------------------------------------
|
223 |
|
|
|
224 |
|
|
wire dec_exception_ud;
|
225 |
|
|
|
226 |
|
|
decode_commands decode_commands_inst(
|
227 |
|
|
.protected_mode (protected_mode), //input
|
228 |
|
|
|
229 |
|
|
.dec_ready_one (dec_ready_one), //input
|
230 |
|
|
.dec_ready_one_one (dec_ready_one_one), //input
|
231 |
|
|
.dec_ready_one_two (dec_ready_one_two), //input
|
232 |
|
|
.dec_ready_one_three (dec_ready_one_three), //input
|
233 |
|
|
.dec_ready_2byte_one (dec_ready_2byte_one), //input
|
234 |
|
|
.dec_ready_modregrm_one (dec_ready_modregrm_one), //input
|
235 |
|
|
.dec_ready_2byte_modregrm (dec_ready_2byte_modregrm), //input
|
236 |
|
|
.dec_ready_call_jmp_imm (dec_ready_call_jmp_imm), //input
|
237 |
|
|
.dec_ready_one_imm (dec_ready_one_imm), //input
|
238 |
|
|
.dec_ready_2byte_imm (dec_ready_2byte_imm), //input
|
239 |
|
|
.dec_ready_mem_offset (dec_ready_mem_offset), //input
|
240 |
|
|
.dec_ready_modregrm_imm (dec_ready_modregrm_imm), //input
|
241 |
|
|
.dec_ready_2byte_modregrm_imm (dec_ready_2byte_modregrm_imm), //input
|
242 |
|
|
|
243 |
|
|
.decoder (decoder), //input [95:0]
|
244 |
|
|
.prefix_group_1_lock (prefix_group_1_lock), //input
|
245 |
|
|
.dec_prefix_group_1_rep (dec_prefix_group_1_rep), //input [1:0]
|
246 |
|
|
.dec_prefix_2byte (dec_prefix_2byte), //input
|
247 |
|
|
|
248 |
|
|
.consume_one (consume_one), //output
|
249 |
|
|
.consume_one_one (consume_one_one), //output
|
250 |
|
|
.consume_one_two (consume_one_two), //output
|
251 |
|
|
.consume_one_three (consume_one_three), //output
|
252 |
|
|
.consume_call_jmp_imm (consume_call_jmp_imm), //output
|
253 |
|
|
.consume_modregrm_one (consume_modregrm_one), //output
|
254 |
|
|
.consume_one_imm (consume_one_imm), //output
|
255 |
|
|
.consume_modregrm_imm (consume_modregrm_imm), //output
|
256 |
|
|
.consume_mem_offset (consume_mem_offset), //output
|
257 |
|
|
|
258 |
|
|
.dec_exception_ud (dec_exception_ud), //output
|
259 |
|
|
|
260 |
|
|
.dec_is_8bit (dec_is_8bit), //output
|
261 |
|
|
.dec_cmd (dec_cmd), //output [6:0]
|
262 |
|
|
.dec_cmdex (dec_cmdex), //output [3:0]
|
263 |
|
|
.dec_is_complex (dec_is_complex) //output
|
264 |
|
|
);
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
//------------------------------------------------------------------------------
|
268 |
|
|
|
269 |
|
|
//------------------------------------------------------------------------------
|
270 |
|
|
|
271 |
|
|
//------------------------------------------------------------------------------
|
272 |
|
|
|
273 |
|
|
//input: micro_busy, dec_reset
|
274 |
|
|
|
275 |
|
|
assign enable = ~(stop);
|
276 |
|
|
|
277 |
|
|
assign instr_prefix = enable && ~(dec_prefix_2byte) && is_prefix && decoder_count > 4'd0;
|
278 |
|
|
|
279 |
|
|
assign dec_ready = ~(dec_reset) && enable && ~(instr_prefix) && consume_count_local > 4'd0 && ~(micro_busy);
|
280 |
|
|
|
281 |
|
|
assign instr_finished = dec_ready || dec_reset;
|
282 |
|
|
|
283 |
|
|
assign stop = dec_ud_fault || dec_gp_fault || dec_pf_fault;
|
284 |
|
|
|
285 |
|
|
assign consume_count =
|
286 |
|
|
(instr_prefix)? 4'd1 :
|
287 |
|
|
(dec_reset)? 4'd0 :
|
288 |
|
|
(micro_busy)? 4'd0 :
|
289 |
|
|
consume_count_local;
|
290 |
|
|
|
291 |
|
|
assign dec_consumed = (dec_ready)? consume_count_local + prefix_count : 4'd0;
|
292 |
|
|
|
293 |
|
|
|
294 |
|
|
//------------------------------------------------------------------------------
|
295 |
|
|
|
296 |
|
|
|
297 |
|
|
//-------------------------- GP
|
298 |
|
|
|
299 |
|
|
assign gp_fault = enable && ~(instr_prefix) && consume_count_local == 4'd0 && (
|
300 |
|
|
( fetch_valid == 4'd0 && fetch_limit ) || // external limit reached
|
301 |
|
|
( dec_acceptable == 4'd0 ) // instruction length limit reached
|
302 |
|
|
);
|
303 |
|
|
|
304 |
|
|
always @(posedge clk or negedge rst_n) begin
|
305 |
|
|
if(rst_n == 1'b0) gp_fault_last <= 1'b0;
|
306 |
|
|
else gp_fault_last <= gp_fault;
|
307 |
|
|
end
|
308 |
|
|
|
309 |
|
|
always @(posedge clk or negedge rst_n) begin
|
310 |
|
|
if(rst_n == 1'b0) dec_gp_fault <= `FALSE;
|
311 |
|
|
else if(dec_reset) dec_gp_fault <= `FALSE;
|
312 |
|
|
else if(gp_fault && gp_fault_last) dec_gp_fault <= `TRUE;
|
313 |
|
|
end
|
314 |
|
|
|
315 |
|
|
//-------------------------- UD
|
316 |
|
|
|
317 |
|
|
|
318 |
|
|
always @(posedge clk or negedge rst_n) begin
|
319 |
|
|
if(rst_n == 1'b0) dec_ud_fault <= `FALSE;
|
320 |
|
|
else if(dec_reset) dec_ud_fault <= `FALSE;
|
321 |
|
|
else if(dec_exception_ud) dec_ud_fault <= `TRUE;
|
322 |
|
|
end
|
323 |
|
|
|
324 |
|
|
//-------------------------- PF
|
325 |
|
|
|
326 |
|
|
assign pf_fault = enable && ~(instr_prefix) && consume_count_local == 4'd0 && (
|
327 |
|
|
( fetch_valid == 4'd0 && fetch_page_fault )
|
328 |
|
|
);
|
329 |
|
|
|
330 |
|
|
always @(posedge clk or negedge rst_n) begin
|
331 |
|
|
if(rst_n == 1'b0) pf_fault_last <= 1'b0;
|
332 |
|
|
else pf_fault_last <= pf_fault;
|
333 |
|
|
end
|
334 |
|
|
|
335 |
|
|
always @(posedge clk or negedge rst_n) begin
|
336 |
|
|
if(rst_n == 1'b0) dec_pf_fault <= `FALSE;
|
337 |
|
|
else if(dec_reset) dec_pf_fault <= `FALSE;
|
338 |
|
|
else if(pf_fault && pf_fault_last) dec_pf_fault <= `TRUE;
|
339 |
|
|
end
|
340 |
|
|
|
341 |
|
|
//------------------------------------------------------------------------------ eip
|
342 |
|
|
|
343 |
|
|
assign dec_eip = eip + { 28'd0, dec_consumed };
|
344 |
|
|
|
345 |
|
|
always @(posedge clk or negedge rst_n) begin
|
346 |
|
|
if(rst_n == 1'b0) eip <= `STARTUP_EIP;
|
347 |
|
|
else if(pr_reset) eip <= prefetch_eip;
|
348 |
|
|
else if(dec_ready) eip <= dec_eip;
|
349 |
|
|
end
|
350 |
|
|
|
351 |
|
|
//------------------------------------------------------------------------------
|
352 |
|
|
|
353 |
|
|
endmodule
|