1 -- `Deep Thought', a softcore CPU implemented on a FPGA
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>
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.
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.
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/>.
23 use IEEE.std_logic_1164.all;
24 use IEEE.numeric_std.all;
29 architecture behaviour of alu is
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
43 end component exec_op;
45 signal add_result, and_result, or_result, xor_result, shift_result : alu_result_rec;
46 signal left_o, right_o : gp_register_t;
50 add_inst : entity work.exec_op(add_op)
51 port map(clk,reset,left_o, right_o, op_detail, alu_state, add_result);
53 and_inst : entity work.exec_op(and_op)
54 port map(clk,reset,left_o, right_o, op_detail, alu_state, and_result);
56 or_inst : entity work.exec_op(or_op)
57 port map(clk,reset,left_o, right_o, op_detail, alu_state, or_result);
59 xor_inst : entity work.exec_op(xor_op)
60 port map(clk,reset,left_o, right_o, op_detail, alu_state, xor_result);
62 shift_inst : entity work.exec_op(shift_op)
63 port map(clk,reset,left_o, right_o, op_detail, alu_state, shift_result);
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;
74 variable pinc_v, pwr_en_v : std_logic;
76 variable prog_cnt_nxt : std_logic_vector(prog_cnt'range);
78 result_v := alu_state;
87 left_o <= left_operand;
88 right_o <= right_operand;
90 addr <= add_result.result;
91 data <= right_operand;
96 paddr <= (others =>'0');
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);
102 prog_cnt_nxt := prog_cnt;
106 cond_met := not(alu_state.status.zero);
108 cond_met := alu_state.status.zero;
110 cond_met := not(alu_state.status.oflo);
112 cond_met := alu_state.status.oflo;
114 cond_met := not(alu_state.status.carry);
116 cond_met := alu_state.status.carry;
118 cond_met := not(alu_state.status.sign);
120 cond_met := alu_state.status.sign;
122 cond_met := not(alu_state.status.carry) and not(alu_state.status.zero);
124 cond_met := alu_state.status.carry or alu_state.status.zero;
126 cond_met := not(alu_state.status.sign xor alu_state.status.oflo);
128 cond_met := alu_state.status.sign xor alu_state.status.oflo;
130 cond_met := not(alu_state.status.zero) and not(alu_state.status.sign xor alu_state.status.oflo);
132 cond_met := alu_state.status.zero or (alu_state.status.sign xor alu_state.status.oflo);
140 nop := (alu_state.alu_jump xnor alu_state.brpr);
141 cond_met := cond_met and nop;
145 result_v := add_result;
147 result_v := and_result;
149 result_v := or_result;
151 result_v := xor_result;
153 result_v := shift_result;
157 --right_o <= displacement;
158 addr <= std_logic_vector(unsigned(left_operand)+unsigned(displacement));
159 if op_detail(IMM_OPT) = '1' then
161 result_v.result := right_operand;
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);
168 result_v.result(15 downto 0) := right_operand(15 downto 0);
175 if op_detail(ST_OPT) = '1' then
179 hword_op := op_detail(HWORD_OPT);
180 byte_op := op_detail(BYTE_OPT);
183 if op_detail(JMP_REG_OPT) = '0' then
194 paddr <= (others =>'0');
197 data <= prog_cnt_nxt;
198 if op_detail(RET_OPT) = '1' then
206 pwr_en_v := op_detail(PWREN_OPT);
207 if op_detail(PUSH_OPT) = '1' then
212 data <= left_operand;
220 result_v.status.zero := '0';
221 if result_v.result = REG_ZERO then
222 result_v.status.zero := '1';
225 result_v.status.sign := result_v.result(gp_register_t'high);
227 if (op_detail(NO_PSW_OPT) = '1') or (cond_met = '0') then
228 result_v.status := alu_state.status;
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;
237 result_v.hw_op := hword_op and cond_met;
238 result_v.byte_op := byte_op and cond_met;
240 pwr_en_v := pwr_en_v and cond_met;
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';
248 -- if result_v.mem_op = '0' then --- do this if selecting enable for extension modules is too slow.
249 -- addr <= (others => '0');
251 alu_result <= result_v;
257 end architecture behaviour;