Fixed some bugs.
[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         \r
26 begin\r
27 \r
28         add_inst : exec_op\r
29         port map(clk,reset,left_operand, right_operand, op_detail, alu_state, add_result);\r
30         \r
31         and_inst : exec_op\r
32         port map(clk,reset,left_operand, right_operand, op_detail, alu_state, and_result);\r
33         or_inst : exec_op\r
34         port map(clk,reset,left_operand, right_operand, op_detail, alu_state, or_result);\r
35         xor_inst : exec_op\r
36         port map(clk,reset,left_operand, right_operand, op_detail, alu_state, xor_result);\r
37         \r
38         shift_inst : exec_op\r
39         port map(clk,reset,left_operand, right_operand, op_detail, alu_state, shift_result);\r
40 \r
41 calc: process(cond, op_group, op_detail ,alu_state,and_result,add_result,or_result,xor_result,shift_result)\r
42         variable result_v : alu_result_rec;\r
43         variable res_prod : std_logic;\r
44         variable cond_met : std_logic;\r
45         variable mem_en : std_logic;\r
46 begin\r
47         result_v := alu_state;\r
48         \r
49         result_v.result := add_result.result;\r
50         res_prod := '1';\r
51         mem_en := '0';
52         addr <= add_result.result;\r
53         \r
54         case cond is\r
55         when COND_NZERO =>\r
56                 cond_met := not(alu_state.status.zero);\r
57         when COND_ZERO =>\r
58                 cond_met := alu_state.status.zero;\r
59         when COND_NOFLO =>\r
60                 cond_met := not(alu_state.status.oflo);\r
61         when COND_OFLO =>\r
62                 cond_met := alu_state.status.oflo;\r
63         when COND_NCARRY =>\r
64                 cond_met := not(alu_state.status.carry);\r
65         when COND_CARRY =>\r
66                 cond_met := alu_state.status.carry;\r
67         when COND_NSIGN =>\r
68                 cond_met := not(alu_state.status.sign);\r
69         when COND_SIGN =>\r
70                 cond_met := alu_state.status.sign;\r
71         when COND_ABOVE =>\r
72                 cond_met := not(alu_state.status.carry) and not(alu_state.status.zero);\r
73         when COND_BEQ =>\r
74                 cond_met := alu_state.status.carry or alu_state.status.zero;\r
75         when COND_GEQ =>\r
76                 cond_met := not(alu_state.status.sign xor alu_state.status.oflo);\r
77         when COND_LT =>\r
78                 cond_met := alu_state.status.sign xor alu_state.status.oflo;\r
79         when COND_GT =>\r
80                 cond_met := not(alu_state.status.zero) and not(alu_state.status.sign xor alu_state.status.oflo);\r
81         when COND_LEQ =>\r
82                 cond_met := alu_state.status.zero or (alu_state.status.sign xor alu_state.status.oflo);\r
83         when COND_ALWAYS =>\r
84                 cond_met := '1';\r
85         when COND_NEVER =>\r
86                 cond_met := '0';
87         when others => null;\r
88         end case;\r
89         \r
90         case op_group is\r
91         when ADDSUB_OP =>\r
92                 result_v := add_result;\r
93         when AND_OP =>\r
94                 result_v := and_result;\r
95         when OR_OP =>\r
96                 result_v := or_result;\r
97         when XOR_OP =>\r
98                 result_v := xor_result;\r
99         when SHIFT_OP =>\r
100                 result_v := shift_result;\r
101         end case;\r
102         \r
103         if result_v.result = REG_ZERO then\r
104                 result_v.status.zero := '1';\r
105         end if;\r
106         \r
107         result_v.status.sign := result_v.result(gp_register_t'high);\r
108 \r
109         if (op_detail(NO_PSW_OPT) = '1') or (cond_met = '0') then\r
110                 result_v.status := alu_state.status;\r
111         end if;\r
112         \r
113         result_v.reg_op := not(op_detail(NO_DST_OPT)) and res_prod and cond_met;\r
114         result_v.mem_en := mem_en and cond_met;
115
116         \r
117         data <= add_result.result;\r
118         alu_result <= result_v;\r
119         \r
120 end process calc; \r
121 \r
122 end architecture behaviour;\r
123 \r
124 configuration alu_cfg of alu is\r
125 \r
126         for behaviour\r
127                 for add_inst : exec_op \r
128                         use entity work.exec_op(add_op);\r
129                 end for;\r
130                 for and_inst : exec_op \r
131                         use entity work.exec_op(and_op);\r
132                 end for;\r
133                 for or_inst : exec_op\r
134                         use entity work.exec_op(or_op);\r
135                 end for;\r
136                 for xor_inst : exec_op\r
137                         use entity work.exec_op(xor_op);\r
138                 end for;\r
139                 for shift_inst : exec_op\r
140                         use entity work.exec_op(shift_op);\r
141                 end for;\r
142         end for;\r
143                 \r
144 end configuration alu_cfg;\r