summaryrefslogtreecommitdiff
path: root/rtl/core/fetch/fetch.sv
blob: dc9790931b34011574610819d2944e17d83608ed (plain)
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
`include "core/uarch.sv"

module core_fetch
#(parameter PREFETCH_ORDER=2)
(
	input  logic clk,
	             rst_n,
	             stall,
	             branch,
	             fetched,
	             wr_pc,
	             prefetch_flush,
	input  ptr   branch_target,
	input  word  wr_current,
	             fetch_data,

	output logic fetch,
	             flush,
	output word  insn,
	output ptr   insn_pc,
	             addr
);

	ptr next_pc, head, hold_addr, target;
	logic fetched_valid, discard;

	assign flush = branch || prefetch_flush;
	assign target = wr_pc ? wr_current[31:2] : branch_target; //TODO: alignment exception
	assign fetched_valid = fetched && !discard;

	core_prefetch #(.ORDER(PREFETCH_ORDER)) prefetch
	(
		.fetched(fetched_valid),
		.*
	);

	always_comb begin
		if(branch)
			head = target;
		else if(prefetch_flush)
			head = next_pc;
		else
			head = {30{1'bx}};

		if(flush)
			addr = head;
		else if(fetch && fetched_valid)
			addr = hold_addr + 1;
		else
			addr = hold_addr;
	end

	always_ff @(posedge clk or negedge rst_n)
		if(!rst_n) begin
			discard <= 0;
			hold_addr <= 0;
		end else begin
			discard <= discard ? !fetched : flush && fetch;
			hold_addr <= addr;
		end

endmodule