46434bcf991648fa24da65974733342c8507c1b1
[calu.git] / cpu / src / common_pkg.vhd
1 library IEEE;
2
3 use IEEE.std_logic_1164.all;
4 use IEEE.numeric_std.all;
5
6 package common_pkg is
7
8         
9         constant WORD_WIDTH   : INTEGER := 32;
10         constant HWORD_WIDTH  : INTEGER := 16;
11         constant BYTE_WIDTH   : INTEGER :=  8;
12         constant OPCODE_WIDTH : INTEGER :=  5;
13         constant DISPL_WIDTH  : INTEGER := 15;
14         
15         constant REG_ZERO : gp_register_t := (others => '0');
16
17         constant INSTR_ADDR_WIDTH       : INTEGER := 32;
18         constant PHYS_INSTR_ADDR_WIDTH  : INTEGER := 11;
19         constant REG_ADDR_WIDTH         : INTEGER := 4;
20         constant DATA_ADDR_WIDTH        : INTEGER := 32;
21         constant PHYS_DATA_ADDR_WIDTH   : INTEGER := 32;
22         
23         constant NUM_OP_OPT_WIDTH       : INTEGER := 5;
24         constant COND_WIDTH : INTEGER := 4;
25
26         subtype byte_t is std_logic_vector(BYTE_WIDTH-1 downto 0);
27         subtype hword_t is std_logic_vector(HWORD_WIDTH-1 downto 0);
28         subtype word_t  is std_logic_vector(WORD_WIDTH-1 downto 0);
29         
30         subtype instruction_word_t is std_logic_vector(WORD_WIDTH-1 downto 0);
31         subtype instruction_addr_t is std_logic_vector(INSTR_ADDR_WIDTH-1 downto 0);
32         
33         subtype gp_register_t is word_t;
34         subtype gp_addr_t       is unsigned(REG_ADDR_WIDTH-1 downto 0);
35         subtype data_ram_word_t is std_logic_vector(WORD_WIDTH-1 downto 0);
36         subtype data_ram_addr_t is std_logic_vector(DATA_ADDR_WIDTH-1 downto 0);
37
38         subtype opcode_t is std_logic_vector(OPCODE_WIDTH-1 downto 0);
39         subtype condition_t is std_logic_vector(COND_WIDTH-1 downto 0);
40         
41         --Opcode consits of decoded group information type and option bits
42         --currently not complete, might need option increase too.
43         --IMMEDIATE always in right_operand (src2)
44         
45         constant IMM_OPT : integer := 0;
46         
47         constant SUB_OPT : integer := 1;
48         constant LOG_SHIFT : integer := 1;
49         
50         constant CARRY_OPT : integer := 2;
51         
52         constant LEFT_SHIFT : integer := 3;
53         
54         constant PSW_DISABLE : integer := 4;
55         
56         
57         
58         
59         type op_info_t is (ADDSUB_OP,AND_OP,OR_OP, XOR_OP,SHIFT_OP);
60         subtype op_opt_rec is std_logic_vector(NUM_OP_OPT_WIDTH-1 downto 0);
61         
62         type dec_op is
63                 condition : condition_t;
64                 op_group : op_info_t;
65                 op_detail : op_opt_rec;
66                 brpr : std_logic;
67                 
68                 src1 : gp_register_t;
69                 src2 : gp_register_t;
70                 
71                 saddr1 : gp_addr_t;
72                 saddr2 : gp_addr_t;
73                 
74                 daddr   : gp_addr_t
75                 
76         end record dec_op;
77         
78         function inc(value : in std_logic_vector; constant by : in integer := 1) return std_logic_vector;
79         function log2c(constant value : in integer range 0 to integer'high) return integer;
80 end package common_pkg;
81
82 package body common_pkg;
83
84         function inc(value : in std_logic_vector; constant by : in integer := 1) return std_logic_vector is
85         begin
86                 return std_logic_vector(UNSIGNED(value)+by);
87         end function inc;
88         
89         function log2c(constant value : in integer range 0 to integer'high) return integer is
90                 variable ret_value : integer;
91                 variable cur_value : integer;
92         begin
93                 ret_value := 0;
94                 cur_value := 1;
95                 
96                 while cur_value < value loop
97                         ret_value := ret_value + 1;
98                         cur_value := cur_value * 2;
99                 end loop;
100                 return ret_value;
101         end function log2c;
102         
103 end package body common_pkg;