bugfix: sp operation first approach.
[calu.git] / cpu / src / alu_b.vhd
1 library IEEE;\r
2 use IEEE.std_logic_1164.all;\r
3 use IEEE.numeric_std.all;\r
4 \r
5 use work.alu_pkg.all;\r
6 \r
7 \r
8 architecture behaviour of alu is\r
9         component exec_op is\r
10         port(\r
11                 --System inputs\r
12                 \r
13                 clk : in std_logic;\r
14                 reset : in std_logic;\r
15                 --operation inputs\r
16                 left_operand : in gp_register_t;\r
17                 right_operand : in gp_register_t;\r
18                 op_detail  : in op_opt_t;\r
19                 alu_state  : in alu_result_rec;\r
20                 alu_result : out alu_result_rec\r
21         );                      \r
22         end component exec_op;\r
23         \r
24         signal add_result, and_result, or_result, xor_result, shift_result : alu_result_rec;\r
25         signal left_o, right_o : gp_register_t;\r
26         \r
27 begin\r
28 \r
29         add_inst : entity work.exec_op(add_op)\r
30         port map(clk,reset,left_o, right_o, op_detail, alu_state, add_result);\r
31         \r
32         and_inst : entity work.exec_op(and_op)\r
33         port map(clk,reset,left_o, right_o, op_detail, alu_state, and_result);\r
34 \r
35         or_inst : entity work.exec_op(or_op)\r
36         port map(clk,reset,left_o, right_o, op_detail, alu_state, or_result);\r
37 \r
38         xor_inst : entity work.exec_op(xor_op)\r
39         port map(clk,reset,left_o, right_o, op_detail, alu_state, xor_result);\r
40         \r
41         shift_inst : entity work.exec_op(shift_op)\r
42         port map(clk,reset,left_o, right_o, op_detail, alu_state, shift_result);\r
43 \r
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)\r
45         variable result_v : alu_result_rec;\r
46         variable res_prod : std_logic;\r
47         variable cond_met : std_logic;\r
48         variable mem_en : std_logic;\r
49         variable mem_op : std_logic;\r
50         variable alu_jump : std_logic;\r
51         variable nop     : std_logic;\r
52 begin\r
53         result_v := alu_state;\r
54         \r
55         res_prod := '1';\r
56         mem_en := '0';\r
57     mem_op := '0';\r
58         alu_jump := '0';\r
59   \r
60         left_o <= left_operand;\r
61         right_o <= right_operand;\r
62 \r
63         addr <= add_result.result;\r
64         data <= right_operand;\r
65         \r
66         pinc <= '0';\r
67         pwr_en <= '0';\r
68         paddr <= (others =>'0');\r
69         \r
70         result_v.result := add_result.result;\r
71 \r
72         case cond is\r
73         when COND_NZERO =>\r
74                 cond_met := not(alu_state.status.zero);\r
75         when COND_ZERO =>\r
76                 cond_met := alu_state.status.zero;\r
77         when COND_NOFLO =>\r
78                 cond_met := not(alu_state.status.oflo);\r
79         when COND_OFLO =>\r
80                 cond_met := alu_state.status.oflo;\r
81         when COND_NCARRY =>\r
82                 cond_met := not(alu_state.status.carry);\r
83         when COND_CARRY =>\r
84                 cond_met := alu_state.status.carry;\r
85         when COND_NSIGN =>\r
86                 cond_met := not(alu_state.status.sign);\r
87         when COND_SIGN =>\r
88                 cond_met := alu_state.status.sign;\r
89         when COND_ABOVE =>\r
90                 cond_met := not(alu_state.status.carry) and not(alu_state.status.zero);\r
91         when COND_BEQ =>\r
92                 cond_met := alu_state.status.carry or alu_state.status.zero;\r
93         when COND_GEQ =>\r
94                 cond_met := not(alu_state.status.sign xor alu_state.status.oflo);\r
95         when COND_LT =>\r
96                 cond_met := alu_state.status.sign xor alu_state.status.oflo;\r
97         when COND_GT =>\r
98                 cond_met := not(alu_state.status.zero) and not(alu_state.status.sign xor alu_state.status.oflo);\r
99         when COND_LEQ =>\r
100                 cond_met := alu_state.status.zero or (alu_state.status.sign xor alu_state.status.oflo);\r
101         when COND_ALWAYS =>\r
102                 cond_met := '1';\r
103         when COND_NEVER =>\r
104                 cond_met := '0';\r
105         when others => null;\r
106         end case;\r
107         \r
108         nop := (alu_state.alu_jump xnor alu_state.brpr);\r
109         cond_met := cond_met and nop;\r
110 \r
111         case op_group is\r
112         when ADDSUB_OP =>\r
113                 result_v := add_result;\r
114         when AND_OP =>\r
115                 result_v := and_result;\r
116         when OR_OP =>\r
117                 result_v := or_result;\r
118         when XOR_OP =>\r
119                 result_v := xor_result;\r
120         when SHIFT_OP =>\r
121                 result_v := shift_result;\r
122         when LDST_OP =>\r
123                 res_prod := '0';\r
124                 mem_op := '1';\r
125                 if op_detail(IMM_OPT) = '1' then\r
126                         result_v.result := right_operand;\r
127                         res_prod := '1';\r
128                         mem_op := '0';\r
129                 end if;\r
130                 if op_detail(ST_OPT) = '1' then\r
131                         right_o <= displacement;\r
132                         mem_en := '1';\r
133                 end if;\r
134         when JMP_OP =>\r
135                 if op_detail(JMP_REG_OPT) = '0' then\r
136                         left_o <= prog_cnt;\r
137                 end if;\r
138                 alu_jump := '1';\r
139         when JMP_ST_OP => null;\r
140                 \r
141         end case;\r
142         \r
143 \r
144         result_v.status.zero := '0';\r
145         if result_v.result = REG_ZERO then\r
146                 result_v.status.zero := '1';\r
147         end if;\r
148         \r
149         result_v.status.sign := result_v.result(gp_register_t'high);\r
150 \r
151         if (op_detail(NO_PSW_OPT) = '1') or (cond_met = '0') then\r
152                 result_v.status := alu_state.status;\r
153         end if;\r
154         \r
155         result_v.reg_op := not(op_detail(NO_DST_OPT)) and res_prod and cond_met;\r
156         result_v.mem_en := mem_en and cond_met;\r
157     result_v.mem_op := mem_op and cond_met;\r
158         result_v.alu_jump := alu_jump and cond_met;\r
159         result_v.brpr := brpr and nop;\r
160         \r
161         if (result_v.alu_jump = '0') and (brpr = '1') then\r
162                 result_v.result := (others => '0');\r
163                 result_v.result(prog_cnt'range) := std_logic_vector(unsigned(prog_cnt)+1);\r
164                 --result_v.reg_op := '1';\r
165         end if;\r
166 \r
167         alu_result <= result_v;\r
168         \r
169 end process calc; \r
170 \r
171 end architecture behaviour;\r
172 \r