4f6990148e2ba524e847b8435b519cfd8c50f9d7
[calu.git] / cpu / src / decode_stage_b.vhd
1 --   `Deep Thought', a softcore CPU implemented on a FPGA
2 --
3 --  Copyright (C) 2010 Markus Hofstaetter <markus.manrow@gmx.at>
4 --  Copyright (C) 2010 Martin Perner <e0725782@student.tuwien.ac.at>
5 --  Copyright (C) 2010 Stefan Rebernig <stefan.rebernig@gmail.com>
6 --  Copyright (C) 2010 Manfred Schwarz <e0725898@student.tuwien.ac.at>
7 --  Copyright (C) 2010 Bernhard Urban <lewurm@gmail.com>
8 --
9 --  This program is free software: you can redistribute it and/or modify
10 --  it under the terms of the GNU General Public License as published by
11 --  the Free Software Foundation, either version 3 of the License, or
12 --  (at your option) any later version.
13 --
14 --  This program is distributed in the hope that it will be useful,
15 --  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 --  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 --  GNU General Public License for more details.
18 --
19 --  You should have received a copy of the GNU General Public License
20 --  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22 library IEEE;
23
24 use IEEE.std_logic_1164.all;
25 use IEEE.numeric_std.all;
26
27 use work.mem_pkg.all;
28 use work.core_pkg.all;
29 use work.common_pkg.all;
30
31
32 architecture behav of decode_stage is
33
34 signal instr_spl : instruction_rec;
35
36 signal rtw_rec, rtw_rec_nxt : read_through_write_rec;
37 signal reg1_mem_data, reg2_mem_data, reg1_rd_data, reg2_rd_data : gp_register_t;
38 signal dec_op_inst, dec_op_inst_nxt : dec_op;
39
40
41 begin
42
43         -- register file
44         register_ram : r2_w_ram
45                 generic map (
46                         REG_ADDR_WIDTH,
47                         WORD_WIDTH
48                 )
49                 
50                 port map (
51                         clk,
52                         reg_w_addr,
53                         instr_spl.reg_src1_addr,
54                         instr_spl.reg_src2_addr,
55                         reg_we,
56                         reg_wr_data,
57                         reg1_mem_data,
58                         reg2_mem_data
59                 );
60
61
62         decoder_inst : decoder
63
64                 port map (
65                         instruction, 
66                         instr_spl
67                 );
68
69 -- sync process for read through write registers
70 syn: process(clk, reset)
71
72 begin
73
74         if (reset = RESET_VALUE) then
75                 rtw_rec.rtw_reg <= (others => '0');
76                 rtw_rec.rtw_reg1 <= '0';
77                 rtw_rec.rtw_reg2 <= '0';
78                 rtw_rec.immediate <= (others => '0');
79                 rtw_rec.imm_set <= '0';
80
81                 dec_op_inst.condition <= (others => '1');
82                 dec_op_inst.op_detail <= (others => '0');
83                 dec_op_inst.op_group <= ADDSUB_OP;
84                 dec_op_inst.brpr <= '0'; --branch_prediction_bit;
85                 dec_op_inst.src1 <= (others => '0');
86                 dec_op_inst.src2 <= (others => '0');
87                 dec_op_inst.saddr1 <= (others => '0');
88                 dec_op_inst.saddr2 <= (others => '0');
89                 dec_op_inst.daddr <= (others => '0');
90                 dec_op_inst.displacement <= (others => '0');
91                 dec_op_inst.prog_cnt <= (others => '0');
92
93         elsif rising_edge(clk) then
94                 rtw_rec <= rtw_rec_nxt;
95                 dec_op_inst <= dec_op_inst_nxt;
96         end if;
97         
98 end process; 
99
100 --      type dec_op is record
101 --              condition : condition_t;
102 --              op_group : op_info_t;
103 --              op_detail : op_opt_t;
104 --              brpr : std_logic;
105 --              
106 --              src1 : gp_register_t;
107 --              src2 : gp_register_t;
108 --              
109 --              saddr1 : gp_addr_t;
110 --              saddr2 : gp_addr_t;
111 --              
112 --              daddr   : gp_addr_t;
113 --              
114 --      end record;
115
116 -- output logic incl. bypassing reg-file
117 output_next_stage: process(dec_op_inst, reg1_rd_data, reg2_rd_data, nop)
118
119 begin
120
121         to_next_stage <= dec_op_inst;
122         to_next_stage.src1 <= reg1_rd_data;
123         to_next_stage.src2 <= reg2_rd_data;
124
125         if (nop = '1') then
126                 to_next_stage.condition <= "1111";
127         end if;
128
129 end process;
130
131
132 -- fills output register
133 to_next: process(instr_spl, prog_cnt)
134
135 begin
136         dec_op_inst_nxt.condition <= instr_spl.predicates;
137         dec_op_inst_nxt.op_detail <= instr_spl.op_detail;
138         dec_op_inst_nxt.brpr <= instr_spl.bp; --branch_prediction_bit;
139         dec_op_inst_nxt.src1 <= (others => '0');
140         dec_op_inst_nxt.src2 <= (others => '0');
141         dec_op_inst_nxt.saddr1 <= instr_spl.reg_src1_addr;
142         dec_op_inst_nxt.saddr2 <= instr_spl.reg_src2_addr;
143         dec_op_inst_nxt.daddr <= instr_spl.reg_dest_addr; --(others => '0');
144         dec_op_inst_nxt.op_group <= instr_spl.op_group;
145         dec_op_inst_nxt.displacement <= instr_spl.displacement;
146         dec_op_inst_nxt.prog_cnt <= prog_cnt;
147
148 end process;
149
150 -- async process: decides between memory and read-through-write buffer on output
151 output: process(rtw_rec, rtw_rec_nxt, reg1_mem_data, reg2_mem_data)
152
153 begin
154         if ((rtw_rec.rtw_reg1) = '1') then
155                 reg1_rd_data <= rtw_rec.rtw_reg;
156         else
157                 reg1_rd_data <= reg1_mem_data;
158         end if;
159
160         if ((rtw_rec.rtw_reg2) = '1') then
161                 reg2_rd_data <= rtw_rec.rtw_reg;
162         else
163                 reg2_rd_data <= reg2_mem_data;
164         end if;
165
166         if (rtw_rec.imm_set = '1') then
167                 reg2_rd_data <= rtw_rec.immediate;
168         end if;
169
170 end process;
171
172
173 -- async process: checks forward condition
174 forward: process(instr_spl, reg_w_addr, reg_wr_data, reg_we)
175
176 begin
177
178         rtw_rec_nxt.rtw_reg <= reg_wr_data;
179         rtw_rec_nxt.rtw_reg1 <= '0';
180         rtw_rec_nxt.rtw_reg2 <= '0';
181         rtw_rec_nxt.immediate <= (others => '0');
182         rtw_rec_nxt.imm_set <= '0';
183 --- ???? wieso
184         rtw_rec_nxt.reg1_addr <= instr_spl.reg_src1_addr;
185         rtw_rec_nxt.reg2_addr <= instr_spl.reg_src2_addr;
186
187         if (instr_spl.op_detail(IMM_OPT) = '1') then -- or instr_spl.op_group = LDST_OP
188                 rtw_rec_nxt.immediate <= instr_spl.immediate;
189                 rtw_rec_nxt.imm_set <= '1';
190         end if;
191
192         if (reg_w_addr = instr_spl.reg_src1_addr) then
193                 rtw_rec_nxt.rtw_reg1 <= ('1' and reg_we);
194         end if;
195
196         if (reg_w_addr = instr_spl.reg_src2_addr) then
197                 rtw_rec_nxt.rtw_reg2 <= ('1' and reg_we);
198         end if;
199
200 end process;
201
202
203 -- async process: calculates branch prediction
204 br_pred: process(instr_spl, prog_cnt, reset)
205
206 begin
207
208         branch_prediction_res <= (others => '0');
209         branch_prediction_bit <= '0';
210
211         if ((instr_spl.opcode = "10110" or instr_spl.opcode = "10111") and instr_spl.bp = '1') then
212                 if instr_spl.int = '0' then             
213                         branch_prediction_res <= std_logic_vector(unsigned(instr_spl.immediate) + unsigned(prog_cnt));  --both 32 bit
214                 else 
215                         branch_prediction_res <= instr_spl.immediate;
216                 end if;
217                 branch_prediction_bit <= '1';
218         end if;
219
220         if reset = RESET_VALUE then
221                 branch_prediction_bit <= '0';
222         end if;
223
224 end process;
225
226 end behav;
227