16c1099ace925b32aefa2dda682b4e6398200557
[calu.git] / cpu / src / alu_pkg.vhd
1 library IEEE;
2
3 use IEEE.std_logic_1164.all;
4 use IEEE.numeric_std.all;
5
6 use work.common_pkg.all;
7 --use work.core_extension.all;
8
9
10 package alu_pkg is
11         
12         type status_rec is record
13                 zero : std_logic;
14                 oflo : std_logic;
15                 sign : std_logic;
16                 carry : std_logic;
17         end record;
18         
19         subtype status_t is byte_t;
20         --type alu_interal_rec is record
21         --      
22         --end record alu_internal_rec;
23         
24         type alu_result_rec is record
25                 result : gp_register_t;
26                 result_addr : gp_addr_t;
27                 
28                 status : status_rec;
29                 --stackpointer : gp_register_t;
30
31                 alu_jump : std_logic;
32                 brpr : std_logic;
33                 reg_op : std_logic;
34                 mem_op  : std_logic;
35                 
36                 --new_val : std_logic;
37                 mem_en : std_logic;
38                 
39                 hw_op   : std_logic;
40                 byte_op : std_logic;
41                 sign_xt : std_logic;
42                 
43         end record alu_result_rec;
44         
45         constant SHIFT_WIDTH : integer := 4; --log2c(gp_register_t'length);
46         
47         constant COND_ZERO : condition_t := "0001";
48         constant COND_NZERO : condition_t := "0000";
49         constant COND_NOFLO : condition_t := "0010";
50         constant COND_OFLO : condition_t := "0011";
51         constant COND_NCARRY : condition_t := "0100";
52         constant COND_CARRY : condition_t := "0101";
53         constant COND_NSIGN : condition_t := "0110";
54         constant COND_SIGN : condition_t := "0111";
55         
56         constant COND_ABOVE : condition_t := "1000";
57         constant COND_BEQ: condition_t := "1001";
58         constant COND_GEQ : condition_t := "1010";
59         constant COND_LT : condition_t := "1011";
60         constant COND_GT : condition_t := "1100";
61         
62         constant COND_LEQ : condition_t := "1101";
63         constant COND_ALWAYS : condition_t := "1110";
64         constant COND_NEVER : condition_t := "1111";
65         
66         function add_oflo(l_neg, r_neg, res_neg : std_logic) return std_logic;
67         -- function addsub_op(left_operand, right_operand : gp_register_t; sub, addc : std_logic; alu_result : alu_result_rec) return alu_result_rec;
68         
69         -- function and_op(left_operand, right_operand : gp_register_t; alu_result : alu_result_rec) return alu_result_rec;
70         -- function or_op(left_operand, right_operand : gp_register_t; alu_result : alu_result_rec) return alu_result_rec;
71         -- function xor_op(left_operand, right_operand : gp_register_t; alu_result : alu_result_rec) return alu_result_rec;
72         
73         -- function shift_op(left_operand, right_operand : gp_register_t; arith,sleft,carry : std_logic ;alu_result : alu_result_rec) return alu_result_rec;
74         
75         component alu is
76         --some modules won't need all inputs
77         port(
78         --System inputs
79         
80                         clk : in std_logic;
81                         reset : in std_logic;
82         --operation inputs
83                         cond : in condition_t;
84                         op_group : in op_info_t;
85                         left_operand : in gp_register_t;
86                         right_operand : in gp_register_t;
87                         displacement : in gp_register_t;
88                         op_detail : in op_opt_t;
89                         alu_state  : in alu_result_rec;
90                         alu_result : out alu_result_rec;
91                         addr : out word_t; --memaddr
92                         data : out gp_register_t --mem data --ureg
93                 );
94         end component alu;
95         
96 end package alu_pkg;
97
98 package body alu_pkg is
99
100         function add_oflo(l_neg, r_neg , res_neg: std_logic) return std_logic is
101         begin
102                 return (l_neg AND r_neg AND not(res_neg)) OR 
103                                 (not(l_neg) AND not(r_neg) AND res_neg);
104         end function add_oflo;
105         
106         -- function addsub_op(left_operand, right_operand : gp_register_t; sub, addc : std_logic; alu_result : alu_result_rec) return alu_result_rec is
107                 -- variable alu_result_out : alu_result_rec;
108                 -- variable complement          : gp_register_t;
109                 -- variable carry_res           : unsigned(gp_register_t'length downto 0);
110                 -- variable tmp_right_operand : unsigned(gp_register_t'length downto 0);
111                 -- variable oflo1, oflo2, l_neg, r_neg : std_logic;
112                 -- variable addcarry            : unsigned(carry_res'range);
113         -- begin
114                 -- alu_result_out := alu_result;
115                 
116                 -- addcarry := (others =>'0');
117                 -- addcarry(0) := unsigned(alu_result.status.carry and addc);
118                 
119                 -- complement := inc(not(right_operand));
120                 -- l_neg := left_operand(gp_register_t'high);
121                 
122                 -- carry_res := unsigned('0' & left_operand)+addcarry;
123                 -- oflo1 := add_oflo(l_neg,'0',std_logic_vector(carry_res)(gp_register_t'high));
124                 
125                 -- if sub = '1' then
126                         -- tmp_right_operand := unsigned('0' & complement);
127                 -- else
128                         -- tmp_right_operand := unsigned('0' & right_operand);
129                 -- end if;
130                 
131                 -- l_neg := std_logic_vector(carry_res)(gp_register_t'high);
132                 -- r_neg := std_logic_vector(tmp_right_operand)(gp_register_t'high);
133                 
134                 -- carry_res := carry_res + tmp_right_operand;
135                 -- oflo2 := add_oflo(l_neg,r_neg,std_logic_vector(carry_res)(gp_register_t'high));
136                 
137
138                 -- alu_result_out.result := std_logic_vector(carry_res)(gp_register_t'range);
139                 -- alu_result_out.status.carry := std_logic_vector(carry_res)(carry_res'high);
140                 
141                 
142                 -- alu_result_out.status.carry := oflo1 or oflo2;
143                 
144                 -- --sign will be set globally.
145                 -- --zero will be set globally.
146                 
147                 -- return alu_result_out;
148         -- end function addsub_op;
149         
150         -- function and_op(left_operand, right_operand : gp_register_t; alu_result : alu_result_rec) return alu_result_rec is
151                 -- variable alu_result_out : alu_result_rec;
152         -- begin
153                 -- alu_result_out := alu_result;
154                 -- alu_result_out.result := left_operand and right_operand;
155         -- end function and_op;
156         
157         -- function or_op(left_operand, right_operand : gp_register_t; alu_result : alu_result_rec) return alu_result_rec is
158                 -- variable alu_result_out : alu_result_rec;
159         -- begin
160                 -- alu_result_out := alu_result;
161                 -- alu_result_out.result := left_operand or right_operand;
162         -- end function or_op;
163         
164         -- function xor_op(left_operand, right_operand : gp_register_t; alu_result : alu_result_rec) return alu_result_rec is
165                 -- variable alu_result_out : alu_result_rec;
166         -- begin
167                 -- alu_result_out := alu_result;
168                 -- alu_result_out.result := left_operand xor right_operand;
169         -- end function xor_op;
170         
171         -- function shift_op(left_operand, right_operand : gp_register_t; arith,rs,carry : std_logic ;alu_result : alu_result_rec) return alu_result_rec is
172                 -- variable alu_result_out : alu_result_rec;
173                 -- variable tmp_shift : bit_vector(gp_register_t'length+1 downto 0);
174                 -- variable tmp_sb : std_logic;
175         -- begin
176                 -- alu_result_out := alu_result;
177                 
178                 -- if rs = '1' then
179                         -- tmp_sb := (carry and alu_result.status.carry and not(arith)) or (arith and left_operand(gp_register_t'high));
180                         -- tmp_shift := bit_vector(tmp_sb & left_operand & alu_result.status.carry);
181                         -- tmp_shift := tmp_shift sra to_integer(unsigned(right_operand)(SHIFT_WIDTH-1 downto 0));
182                         
183                         -- alu_result_out.status.carry := std_logic_vector(tmp_shift)(0);
184                 -- else
185                         -- tmp_sb := (carry and alu_result.status.carry and not(arith));
186                         -- tmp_shift :=  bit_vector(alu_result.status.carry & left_operand & tmp_sb);
187                         -- tmp_shift :=  tmp_shift sla to_integer(unsigned(right_operand)(SHIFT_WIDTH-1 downto 0));
188                         
189                         -- alu_result_out.status.carry := std_logic_vector(tmp_shift)(tmp_shift'high);
190                 -- end if;
191                 
192                 -- alu_result_out.result := std_logic_vector(tmp_shift)(gp_register_t'length downto 1);
193                 
194         -- end function shift_op;
195
196 end package body alu_pkg;