-
Notifications
You must be signed in to change notification settings - Fork 1
/
writeback.v
138 lines (105 loc) · 2.41 KB
/
writeback.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
`ifndef WRITEBACK_V
`define WRITEBACK_V
`include "header.v"
// writes data back to the regfile, memory, and PC
module writeback(
input clk,
input reset,
// exec-wb comms
input i_input_valid,
input [7:0] i_opcode,
input [`DATA_WIDTH-1:0] i_val,
input [3:0] i_dest_reg,
input [`ADDRESS_WIDTH-1:0] i_dest_addr,
output o_fetching,
// wb-fetch comms
output o_pc_valid,
output [`ADDRESS_WIDTH-1:0] o_pc,
output o_ready
);
// WB state machine macros
`define ST_RESET 4'h0
`define ST_IDLE 4'h1
`define ST_NEW_INST 4'h2
`define ST_MEM_WRITE 4'h3
`define ST_REG_WRITE 4'h4
`define ST_PC_WRITE 4'h5
`define ST_END_INST 4'hF
// WB type macros
`define WB_PC 2'h0
`define WB_MEM 2'h1
`define WB_REG 2'h2
// status registers
reg ready;
reg fetching;
reg state_idle;
reg [1:0] wb_type;
// FSM registers
reg [3:0] reg_status = `ST_RESET;
reg [3:0] nxt_status = `ST_IDLE;
// info about current instruction
reg [7:0] opcode;
reg [7:0] value;
reg [3:0] dest_reg;
reg [3:0] dest_addr;
reg [`ADDRESS_WIDTH-1:0] out_pc;
reg pc_valid;
assign o_pc_valid = pc_valid;
assign o_ready = ready;
assign o_fetching = fetching;
// synchronous reset
always @(posedge clk && reset) begin
reg_status <= `ST_RESET;
end
// synchronous retrieve instruction
always @(posedge clk && i_input_valid) begin
if(state_idle) begin
reg_status = `ST_NEW_INST;
fetching = 1;
end
else
fetching = 0;
end
// execute the instructions
always @(posedge clk) begin
//$display("STATE = %d",reg_status);
case (reg_status)
`ST_RESET: begin
reg_status = `ST_IDLE;
pc_valid = 0;
fetching = 1;
out_pc = 0;
pc_valid = 0;
end
`ST_IDLE: begin
state_idle = 1;
if (pc_valid == 0 && opcode == 0) begin
pc_valid = 1;
out_pc = dest_addr;
end
end
`ST_NEW_INST: begin
$display("WRITEBACK");
state_idle = 0;
opcode = i_opcode;
value = i_val;
dest_reg = i_dest_reg;
dest_addr = i_dest_addr;
if(dest_addr != 0)
reg_status = `ST_MEM_WRITE;
else
reg_status = `ST_REG_WRITE;
end
`ST_MEM_WRITE: begin
wb_type = `WB_MEM;
end
`ST_REG_WRITE: begin
wb_type = `WB_REG;
end
`ST_PC_WRITE: begin
wb_type = `WB_PC;
end
endcase
end
endmodule
`endif