1 |
2 |
jamieiles |
// Copyright Jamie Iles, 2017
|
2 |
|
|
//
|
3 |
|
|
// This file is part of s80x86.
|
4 |
|
|
//
|
5 |
|
|
// s80x86 is free software: you can redistribute it and/or modify
|
6 |
|
|
// it under the terms of the GNU General Public License as published by
|
7 |
|
|
// the Free Software Foundation, either version 3 of the License, or
|
8 |
|
|
// (at your option) any later version.
|
9 |
|
|
//
|
10 |
|
|
// s80x86 is distributed in the hope that it will be useful,
|
11 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
|
|
// GNU General Public License for more details.
|
14 |
|
|
//
|
15 |
|
|
// You should have received a copy of the GNU General Public License
|
16 |
|
|
// along with s80x86. If not, see .
|
17 |
|
|
|
18 |
|
|
// Prefetcher
|
19 |
|
|
//
|
20 |
|
|
// This module is responsible for fetching bytes from the memory and pushing
|
21 |
|
|
// them into the instruction stream FIFO. The CS is stored outside of the
|
22 |
|
|
// Prefetch unit and passed in to be combined with the internal IP which is
|
23 |
|
|
// wired out. This means that IP wrapping works correctly, and can be updated
|
24 |
|
|
// under external control. The fetching can be stalled when servicing
|
25 |
|
|
// a branch, updating the IP will flush the FIFO.
|
26 |
|
|
module Prefetch(input logic clk,
|
27 |
|
|
input logic reset,
|
28 |
|
|
// Address control.
|
29 |
|
|
input logic [15:0] new_cs,
|
30 |
|
|
input logic [15:0] new_ip,
|
31 |
|
|
input logic load_new_ip,
|
32 |
|
|
// FIFO write port.
|
33 |
|
|
output logic fifo_wr_en,
|
34 |
|
|
output logic [7:0] fifo_wr_data,
|
35 |
|
|
output logic fifo_reset,
|
36 |
|
|
input logic fifo_full,
|
37 |
|
|
// Memory port.
|
38 |
|
|
output logic mem_access,
|
39 |
|
|
input logic mem_ack,
|
40 |
|
|
output logic [19:1] mem_address,
|
41 |
|
|
input logic [15:0] mem_data);
|
42 |
|
|
|
43 |
|
|
reg [15:0] next_fetch_address, fetch_address;
|
44 |
|
|
reg [15:0] next_cs, cs;
|
45 |
|
|
reg abort_cur;
|
46 |
|
|
reg [7:0] fetched_high_byte;
|
47 |
|
|
reg write_second;
|
48 |
|
|
wire should_write_second_byte = mem_ack && !abort_cur && !fetch_address[0] && !fifo_reset;
|
49 |
|
|
|
50 |
|
|
// verilator lint_off UNUSED
|
51 |
|
|
wire [15:0] next_address = mem_ack && !abort_cur ? fetch_address + 1'b1 : fetch_address;
|
52 |
|
|
// verilator lint_on UNUSED
|
53 |
|
|
|
54 |
|
|
assign mem_address = {cs, 3'b0} + {4'b0, next_address[15:1]};
|
55 |
|
|
assign mem_access = !reset && !fifo_full && !mem_ack && !write_second;
|
56 |
|
|
|
57 |
|
|
assign fifo_wr_en = !abort_cur && !load_new_ip && (mem_ack || write_second);
|
58 |
|
|
assign fifo_reset = load_new_ip | (abort_cur & mem_ack);
|
59 |
|
|
assign fifo_wr_data = mem_ack ?
|
60 |
|
|
(fetch_address[0] ? mem_data[15:8] : mem_data[7:0]) : fetched_high_byte;
|
61 |
|
|
|
62 |
|
|
always_ff @(posedge clk or posedge reset) begin
|
63 |
|
|
if (reset) begin
|
64 |
|
|
abort_cur <= 1'b0;
|
65 |
|
|
end else begin
|
66 |
|
|
if (abort_cur && mem_ack)
|
67 |
|
|
abort_cur <= 1'b0;
|
68 |
|
|
else if (mem_access && load_new_ip)
|
69 |
|
|
abort_cur <= 1'b1;
|
70 |
|
|
end
|
71 |
|
|
end
|
72 |
|
|
|
73 |
|
|
always_ff @(posedge clk or posedge reset)
|
74 |
|
|
write_second <= reset ? 1'b0 : should_write_second_byte;
|
75 |
|
|
|
76 |
|
|
always_ff @(posedge clk or posedge reset)
|
77 |
|
|
if (reset)
|
78 |
|
|
cs <= 16'hffff;
|
79 |
|
|
else if (abort_cur && mem_ack)
|
80 |
|
|
cs <= next_cs;
|
81 |
|
|
else if (load_new_ip && !mem_access)
|
82 |
|
|
cs <= new_cs;
|
83 |
|
|
|
84 |
|
|
always_ff @(posedge clk or posedge reset)
|
85 |
|
|
if (reset)
|
86 |
|
|
next_cs <= 16'hffff;
|
87 |
|
|
else if (load_new_ip)
|
88 |
|
|
next_cs <= new_cs;
|
89 |
|
|
|
90 |
|
|
always_ff @(posedge clk or posedge reset) begin
|
91 |
|
|
if (reset)
|
92 |
|
|
fetch_address <= 16'b0;
|
93 |
|
|
else if (abort_cur && mem_ack)
|
94 |
|
|
fetch_address <= next_fetch_address;
|
95 |
|
|
else if (load_new_ip && !mem_access)
|
96 |
|
|
fetch_address <= new_ip;
|
97 |
|
|
else if (!abort_cur && !load_new_ip && (mem_ack || write_second))
|
98 |
|
|
fetch_address <= fetch_address + 1'b1;
|
99 |
|
|
end
|
100 |
|
|
|
101 |
|
|
always_ff @(posedge clk or posedge reset) begin
|
102 |
|
|
if (reset)
|
103 |
|
|
next_fetch_address <= 16'b0;
|
104 |
|
|
else if (load_new_ip)
|
105 |
|
|
next_fetch_address <= new_ip;
|
106 |
|
|
end
|
107 |
|
|
|
108 |
|
|
always_ff @(posedge clk)
|
109 |
|
|
if (mem_ack)
|
110 |
|
|
fetched_high_byte <= mem_data[15:8];
|
111 |
|
|
|
112 |
|
|
endmodule
|