59ff7a7e9566866c7ee85753a3fcf15b76060aac
[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         subtype byte_t is std_logic_vector(BYTE_WIDTH-1 downto 0);
24         subtype hword_t is std_logic_vector(HWORD_WIDTH-1 downto 0);
25         subtype word_t  is std_logic_vector(WORD_WIDTH-1 downto 0);
26         
27         subtype instruction_word_t is std_logic_vector(WORD_WIDTH-1 downto 0);
28         subtype instruction_addr_t is std_logic_vector(INSTR_ADDR_WIDTH-1 downto 0);
29         
30         subtype gp_register_t is word_t;
31         subtype gp_addr_t       is unsigned(REG_ADDR_WIDTH-1 downto 0);
32         subtype data_ram_word_t is std_logic_vector(WORD_WIDTH-1 downto 0);
33         subtype data_ram_addr_t is std_logic_vector(DATA_ADDR_WIDTH-1 downto 0);
34
35         subtype opcode_t is std_logic_vector(OPCODE_WIDTH-1 downto 0);
36         
37         function inc(value : in std_logic_vector; constant by : in integer := 1) return std_logic_vector;
38         function log2c(constant value : in integer range 0 to integer'high) return integer;
39 end package common_pkg;
40
41 package body common_pkg;
42
43         function inc(value : in std_logic_vector; constant by : in integer := 1) return std_logic_vector is
44         begin
45                 return std_logic_vector(UNSIGNED(value)+by);
46         end function inc;
47         
48         function log2c(constant value : in integer range 0 to integer'high) return integer is
49                 variable ret_value : integer;
50                 variable cur_value : integer;
51         begin
52                 ret_value := 0;
53                 cur_value := 1;
54                 
55                 while cur_value < value loop
56                         ret_value := ret_value + 1;
57                         cur_value := cur_value * 2;
58                 end loop;
59                 return ret_value;
60         end function log2c;
61         
62 end package body common_pkg;