copyleft: gplv3 added and set repo to public
[calu.git] / cpu / src / alu_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 use IEEE.std_logic_1164.all;
24 use IEEE.numeric_std.all;
25
26 use work.alu_pkg.all;
27
28
29 architecture behaviour of alu is
30         component exec_op is
31         port(
32                 --System inputs
33                 
34                 clk : in std_logic;
35                 reset : in std_logic;
36                 --operation inputs
37                 left_operand : in gp_register_t;
38                 right_operand : in gp_register_t;
39                 op_detail  : in op_opt_t;
40                 alu_state  : in alu_result_rec;
41                 alu_result : out alu_result_rec
42         );                      
43         end component exec_op;
44         
45         signal add_result, and_result, or_result, xor_result, shift_result : alu_result_rec;
46         signal left_o, right_o : gp_register_t;
47         
48 begin
49
50         add_inst : entity work.exec_op(add_op)
51         port map(clk,reset,left_o, right_o, op_detail, alu_state, add_result);
52         
53         and_inst : entity work.exec_op(and_op)
54         port map(clk,reset,left_o, right_o, op_detail, alu_state, and_result);
55
56         or_inst : entity work.exec_op(or_op)
57         port map(clk,reset,left_o, right_o, op_detail, alu_state, or_result);
58
59         xor_inst : entity work.exec_op(xor_op)
60         port map(clk,reset,left_o, right_o, op_detail, alu_state, xor_result);
61         
62         shift_inst : entity work.exec_op(shift_op)
63         port map(clk,reset,left_o, right_o, op_detail, alu_state, shift_result);
64
65 calc: process(left_operand, right_operand,displacement, cond, op_group, op_detail ,alu_state,and_result,add_result,or_result,xor_result,shift_result, prog_cnt,brpr, pval, pval_nxt)
66         variable result_v : alu_result_rec;
67         variable res_prod : std_logic;
68         variable cond_met : std_logic;
69         variable mem_en : std_logic;
70    variable mem_op, hword_op, byte_op : std_logic;
71         variable alu_jump : std_logic;
72         variable nop     : std_logic;
73         
74         variable pinc_v, pwr_en_v : std_logic;
75         
76         variable prog_cnt_nxt : std_logic_vector(prog_cnt'range);
77 begin
78         result_v := alu_state;
79         
80         res_prod := '1';
81         mem_en := '0';
82     mem_op := '0';
83          hword_op := '0';
84          byte_op := '0';
85         alu_jump := '0';
86   
87         left_o <= left_operand;
88         right_o <= right_operand;
89
90         addr <= add_result.result;
91         data <= right_operand;
92         
93         pinc_v := '0';
94         pwr_en_v := '0';
95         
96         paddr <= (others =>'0');
97         
98         result_v.result := add_result.result;
99         if (op_detail(DIRECT_JUMP_OPT) = '0') then
100                 prog_cnt_nxt := std_logic_vector(unsigned(prog_cnt)+1);
101         else
102                 prog_cnt_nxt := prog_cnt;
103         end if;
104         case cond is
105         when COND_NZERO =>
106                 cond_met := not(alu_state.status.zero);
107         when COND_ZERO =>
108                 cond_met := alu_state.status.zero;
109         when COND_NOFLO =>
110                 cond_met := not(alu_state.status.oflo);
111         when COND_OFLO =>
112                 cond_met := alu_state.status.oflo;
113         when COND_NCARRY =>
114                 cond_met := not(alu_state.status.carry);
115         when COND_CARRY =>
116                 cond_met := alu_state.status.carry;
117         when COND_NSIGN =>
118                 cond_met := not(alu_state.status.sign);
119         when COND_SIGN =>
120                 cond_met := alu_state.status.sign;
121         when COND_ABOVE =>
122                 cond_met := not(alu_state.status.carry) and not(alu_state.status.zero);
123         when COND_BEQ =>
124                 cond_met := alu_state.status.carry or alu_state.status.zero;
125         when COND_GEQ =>
126                 cond_met := not(alu_state.status.sign xor alu_state.status.oflo);
127         when COND_LT =>
128                 cond_met := alu_state.status.sign xor alu_state.status.oflo;
129         when COND_GT =>
130                 cond_met := not(alu_state.status.zero) and not(alu_state.status.sign xor alu_state.status.oflo);
131         when COND_LEQ =>
132                 cond_met := alu_state.status.zero or (alu_state.status.sign xor alu_state.status.oflo);
133         when COND_ALWAYS =>
134                 cond_met := '1';
135         when COND_NEVER =>
136                 cond_met := '0';
137         when others => null;
138         end case;
139         
140         nop := (alu_state.alu_jump xnor alu_state.brpr);
141         cond_met := cond_met and nop;
142
143         case op_group is
144         when ADDSUB_OP =>
145                 result_v := add_result;
146         when AND_OP =>
147                 result_v := and_result;
148         when OR_OP =>
149                 result_v := or_result;
150         when XOR_OP =>
151                 result_v := xor_result;
152         when SHIFT_OP =>
153                 result_v := shift_result;
154    when LDST_OP =>
155                 res_prod := '0';
156                 mem_op := '1';
157                 --right_o <= displacement;
158                 addr <= std_logic_vector(unsigned(left_operand)+unsigned(displacement));
159                 if op_detail(IMM_OPT) = '1' then
160                                          
161                                                                 result_v.result := right_operand;
162                                          
163                                                                 if (op_detail(LDI_REPLACE_OPT) = '0') then
164                                                                         result_v.result := left_operand;
165                                                                         if (op_detail(LOW_HIGH_OPT) = '1') then
166                                                                                 result_v.result(31 downto 16) := right_operand(31 downto 16);
167                                                                         else
168                                                                                 result_v.result(15 downto 0) := right_operand(15 downto 0);
169                                                                         end if;
170                                                                 end if;
171
172                         res_prod := '1';
173                         mem_op := '0';
174                 end if;
175                 if op_detail(ST_OPT) = '1' then
176                         mem_en := '1';
177                 end if;
178                                          
179                                          hword_op := op_detail(HWORD_OPT);
180                                          byte_op := op_detail(BYTE_OPT);
181                                          
182         when JMP_OP =>
183                 if op_detail(JMP_REG_OPT) = '0' then
184                         left_o <= prog_cnt;
185                 end if;
186                 alu_jump := '1';
187         when JMP_ST_OP => 
188                 left_o <= prog_cnt;
189                 mem_en := '1';
190                 alu_jump := '1';
191                 mem_op := '1';
192                 pinc_v := '1';
193                 pwr_en_v := '1';
194                 paddr <= (others =>'0');
195                 
196                 addr <= pval;
197                 data <= prog_cnt_nxt;
198                 if op_detail(RET_OPT) = '1' then
199                         addr <= pval_nxt;
200                         mem_en := '0';
201                         pinc_v := '0';
202                         res_prod := '0';
203                 end if;
204         when STACK_OP =>
205                 mem_op := '1';
206                 pwr_en_v := op_detail(PWREN_OPT);
207                 if op_detail(PUSH_OPT) = '1' then
208                         mem_en := '1';
209                         pinc_v := '1';
210                         res_prod := '0';
211                         addr <= pval;
212                         data <= left_operand;
213                 else
214                         addr <= pval_nxt;
215                 end if;
216                 
217         end case;
218         
219
220         result_v.status.zero := '0';
221         if result_v.result = REG_ZERO then
222                 result_v.status.zero := '1';
223         end if;
224         
225         result_v.status.sign := result_v.result(gp_register_t'high);
226
227         if (op_detail(NO_PSW_OPT) = '1') or (cond_met = '0') then
228                 result_v.status := alu_state.status;
229         end if;
230         
231         result_v.reg_op := not(op_detail(NO_DST_OPT)) and res_prod and cond_met;
232         result_v.mem_en := mem_en and cond_met;
233     result_v.mem_op := mem_op and cond_met;
234         result_v.alu_jump := alu_jump and cond_met;
235         result_v.brpr := brpr and nop;
236         
237         result_v.hw_op := hword_op and cond_met;
238         result_v.byte_op := byte_op and cond_met;
239         
240         pwr_en_v := pwr_en_v and cond_met;
241         
242         if (result_v.alu_jump = '0') and (brpr = '1') then
243                 result_v.result := (others => '0');
244                 result_v.result(prog_cnt'range) := prog_cnt_nxt;
245                 --result_v.reg_op := '1';
246         end if;
247
248         -- if result_v.mem_op = '0' then --- do this if selecting enable for extension modules is too slow.
249                 -- addr <= (others => '0');
250         -- end if;
251         alu_result <= result_v;
252         pinc <= pinc_v;
253         pwr_en <= pwr_en_v;
254         
255 end process calc; 
256
257 end architecture behaviour;
258