added: alu jumps
[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;
25         signal left, right : 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, right, 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, right, 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, right, 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, right, 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, right, 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)\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;
49         variable mem_op : std_logic;
50         variable alu_jmp : std_logic;\r
51 begin\r
52         result_v := alu_state;\r
53         \r
54         res_prod := '1';\r
55         mem_en := '0';
56         mem_op := '0';
57         alu_jump := '0';\r
58   
59         left <= left_operand;
60         right <= right_operand;
61
62         addr <= add_result.result;
63         data <= right_operand;\r
64         
65         result_v.result := add_result.result;
66 \r
67         case cond is\r
68         when COND_NZERO =>\r
69                 cond_met := not(alu_state.status.zero);\r
70         when COND_ZERO =>\r
71                 cond_met := alu_state.status.zero;\r
72         when COND_NOFLO =>\r
73                 cond_met := not(alu_state.status.oflo);\r
74         when COND_OFLO =>\r
75                 cond_met := alu_state.status.oflo;\r
76         when COND_NCARRY =>\r
77                 cond_met := not(alu_state.status.carry);\r
78         when COND_CARRY =>\r
79                 cond_met := alu_state.status.carry;\r
80         when COND_NSIGN =>\r
81                 cond_met := not(alu_state.status.sign);\r
82         when COND_SIGN =>\r
83                 cond_met := alu_state.status.sign;\r
84         when COND_ABOVE =>\r
85                 cond_met := not(alu_state.status.carry) and not(alu_state.status.zero);\r
86         when COND_BEQ =>\r
87                 cond_met := alu_state.status.carry or alu_state.status.zero;\r
88         when COND_GEQ =>\r
89                 cond_met := not(alu_state.status.sign xor alu_state.status.oflo);\r
90         when COND_LT =>\r
91                 cond_met := alu_state.status.sign xor alu_state.status.oflo;\r
92         when COND_GT =>\r
93                 cond_met := not(alu_state.status.zero) and not(alu_state.status.sign xor alu_state.status.oflo);\r
94         when COND_LEQ =>\r
95                 cond_met := alu_state.status.zero or (alu_state.status.sign xor alu_state.status.oflo);\r
96         when COND_ALWAYS =>\r
97                 cond_met := '1';\r
98         when COND_NEVER =>\r
99                 cond_met := '0';\r
100         when others => null;\r
101         end case;\r
102         
103         cond_met := cond_met and (alu_state.alu_jmp xnor alu_state.brpr);
104 \r
105         case op_group is\r
106         when ADDSUB_OP =>\r
107                 result_v := add_result;\r
108         when AND_OP =>\r
109                 result_v := and_result;\r
110         when OR_OP =>\r
111                 result_v := or_result;\r
112         when XOR_OP =>\r
113                 result_v := xor_result;\r
114         when SHIFT_OP =>\r
115                 result_v := shift_result;
116         when LDST_OP =>
117                 res_prod := '0';
118                 mem_op := '1';
119                 if op_detail(IMM_OPT) = '1' then
120                         result_v.result := right_operand;
121                         res_prod := '1';
122                         mem_op := '0';
123                 end if;
124                 if op_detail(ST_OPT) = '1' then
125                         right <= displacement;
126                         mem_en := '1';
127                 end if;
128         when JMP_OP =>
129                 if op_detail(JMP_REG_OPT) = '0' then
130                         left <= prog_cnt;
131                 end if;
132                 result_v.alu_jmp := '1';
133         when JMP_ST_OP => null;\r
134         end case;\r
135         \r
136 \r
137         result_v.status.zero := '0';\r
138         if result_v.result = REG_ZERO then\r
139                 result_v.status.zero := '1';\r
140         end if;\r
141         \r
142         result_v.status.sign := result_v.result(gp_register_t'high);\r
143 \r
144         if (op_detail(NO_PSW_OPT) = '1') or (cond_met = '0') then\r
145                 result_v.status := alu_state.status;\r
146         end if;\r
147         \r
148         result_v.reg_op := not(op_detail(NO_DST_OPT)) and res_prod and cond_met;\r
149         result_v.mem_en := mem_en and cond_met;
150         result_v.mem_op := mem_op and cond_met;
151         result_v.alu_jmp := alu_jmp and cond_met;
152         \r
153         alu_result <= result_v;\r
154         \r
155 end process calc; \r
156 \r
157 end architecture behaviour;\r
158 \r