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