2b711da003799afdb023292928077a6ec1b9f6b1
[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         
10         constant WORD_WIDTH   : INTEGER := 32;
11         constant HWORD_WIDTH  : INTEGER := 16;
12         constant BYTE_WIDTH   : INTEGER :=  8;
13         constant OPCODE_WIDTH : INTEGER :=  5;
14         constant DISPL_WIDTH  : INTEGER := 15;
15
16         subtype byte_t is std_logic_vector(BYTE_WIDTH-1 downto 0);
17         subtype hword_t is std_logic_vector(HWORD_WIDTH-1 downto 0);
18         subtype word_t  is std_logic_vector(WORD_WIDTH-1 downto 0);
19
20         subtype gp_register_t is word_t;
21         
22         subtype byte_en_t is std_logic_vector((gp_register_t'length/byte_t'length-1) downto 0); 
23         
24         constant REG_ZERO : gp_register_t := (others => '0');
25
26         constant INSTR_ADDR_WIDTH       : INTEGER := 32;
27         constant PHYS_INSTR_ADDR_WIDTH  : INTEGER := 11;
28         constant ROM_INSTR_ADDR_WIDTH : INTEGER := 7;
29         constant REG_ADDR_WIDTH         : INTEGER := 4;
30         constant DATA_ADDR_WIDTH        : INTEGER := 11;
31         constant PHYS_DATA_ADDR_WIDTH   : INTEGER := 32;
32         
33         constant NUM_OP_OPT_WIDTH       : INTEGER := 6;
34         constant COND_WIDTH : INTEGER := 4;
35         constant DATA_END_ADDR          : integer := ((2**DATA_ADDR_WIDTH)-1);
36
37         constant ROM_USE : std_logic := '1';
38         constant RAM_USE : std_logic := '0';
39         
40         subtype instruction_word_t is std_logic_vector(WORD_WIDTH-1 downto 0);
41         subtype instruction_addr_t is std_logic_vector(INSTR_ADDR_WIDTH-1 downto 0);
42         subtype instr_addr_t is instruction_addr_t;
43         
44         subtype gp_addr_t       is std_logic_vector(REG_ADDR_WIDTH-1 downto 0);
45         subtype data_ram_word_t is std_logic_vector(WORD_WIDTH-1 downto 0);
46         subtype data_ram_addr_t is std_logic_vector(DATA_ADDR_WIDTH-1 downto 0);
47
48         subtype opcode_t is std_logic_vector(OPCODE_WIDTH-1 downto 0);
49         subtype condition_t is std_logic_vector(COND_WIDTH-1 downto 0);
50         
51         --Opcode consits of decoded group information type and option bits
52         --currently not complete, might need option increase too.
53         --IMMEDIATE always in right_operand (src2)
54         
55         constant IMM_OPT : integer := 0; -- no sharing
56         
57         constant SUB_OPT : integer := 1;
58         constant ARITH_OPT : integer := 1;
59         constant HWORD_OPT : integer := 1;
60         constant PUSH_OPT : integer := 1;
61         constant LOW_HIGH_OPT : integer := 1;
62         constant DIRECT_JUMP_OPT : integer := 1;
63         
64         constant CARRY_OPT : integer := 2;
65         constant BYTE_OPT : integer := 2;
66         constant LDI_REPLACE_OPT : integer := 2;
67         constant PWREN_OPT : integer := 2;
68
69         constant RIGHT_OPT : integer := 3;
70         constant JMP_REG_OPT : integer := 3;
71         constant ST_OPT  : integer := 3; -- store opt
72         constant RET_OPT : integer := 3;
73         
74         constant NO_PSW_OPT : integer := 4;--no sharing
75         constant NO_DST_OPT : integer := 5; --no sharing
76         
77         type op_info_t is (ADDSUB_OP,AND_OP,OR_OP, XOR_OP,SHIFT_OP, LDST_OP, JMP_OP, JMP_ST_OP, STACK_OP);
78         subtype op_opt_t is std_logic_vector(NUM_OP_OPT_WIDTH-1 downto 0);
79
80         type interrupt_t is (IDLE, UART);       
81         
82         constant UART_INT_EN_BIT : integer := 1;
83         constant GLOBAL_INT_EN_BIT : integer := 0;
84
85         constant UART_INT_VECTOR : std_logic_vector(PHYS_INSTR_ADDR_WIDTH-1 downto 0) := "00000000001"; --integer := 1;
86
87         type instruction_rec is record
88
89                 predicates : std_logic_vector(3 downto 0);
90
91                 opcode : opcode_t;
92
93                 reg_dest_addr : std_logic_vector(REG_ADDR_WIDTH-1 downto 0);
94                 reg_src1_addr : std_logic_vector(REG_ADDR_WIDTH-1 downto 0);
95                 reg_src2_addr : std_logic_vector(REG_ADDR_WIDTH-1 downto 0);
96
97                 immediate : std_logic_vector(WORD_WIDTH-1 downto 0);
98
99                 displacement : gp_register_t;
100
101                 jmptype : std_logic_vector(1 downto 0);
102
103                 high_low, fill, signext, bp, int: std_logic;
104
105                 op_detail : op_opt_t;
106                 op_group : op_info_t;
107
108         end record;
109
110
111         
112         type read_through_write_rec is record
113
114                 rtw_reg : gp_register_t;
115                 rtw_reg1 : std_logic;
116                 rtw_reg2 : std_logic;
117                 immediate : gp_register_t;
118                 imm_set : std_logic;
119                 reg1_addr : gp_addr_t;
120                 reg2_addr : gp_addr_t;
121
122         end record;
123
124         type dec_op is record
125                 condition : condition_t;
126                 op_group : op_info_t;
127                 op_detail : op_opt_t;
128                 brpr : std_logic;
129
130                 displacement : gp_register_t;
131                 prog_cnt     : instr_addr_t;
132                 
133                 src1 : gp_register_t;
134                 src2 : gp_register_t;
135                 
136                 saddr1 : gp_addr_t;
137                 saddr2 : gp_addr_t;
138                 
139                 daddr   : gp_addr_t;
140                 
141         end record;
142
143         type writeback_rec is record
144 --              result : in gp_register_t;      --reg  (alu result or jumpaddr)
145 --              result_addr : in gp_addr_t;     --reg
146                 address : word_t;               --ureg 
147 --              alu_jmp : in std_logic;         --reg
148 --              br_pred : in std_logic;         --reg
149 --              write_en : in std_logic;        --reg  (register file)
150                 dmem_en : std_logic;            --ureg (jump addr in mem or in address)
151                 dmem_write_en : std_logic;      --ureg
152                 hword : std_logic;              --ureg
153                 byte_s : std_logic;
154                 byte_en : byte_en_t;
155                 data : gp_register_t;
156         end record;
157         
158         type exec2wb_rec is record
159                         result : gp_register_t; --reg  (alu result or jumpaddr)
160                         result_addr : gp_addr_t;        --reg
161                         address : word_t;               --ureg 
162                         ram_data : word_t;              --ureg
163                         alu_jmp : std_logic;            --reg
164                         br_pred : std_logic;            --reg
165                         write_en : std_logic;   --reg  (register file) bei jump 1 wenn addr in result
166                         dmem_en : std_logic;            --ureg (jump addr in mem or in address)
167                         dmem_write_en : std_logic;      --ureg
168                         hword : std_logic;              --ureg
169                         byte_s : std_logic;             --ureg  
170         end record;
171         
172         function inc(value : in std_logic_vector; constant by : in integer := 1) return std_logic_vector;
173         function log2c(constant value : in integer range 0 to integer'high) return integer;
174 end package common_pkg;
175
176 package body common_pkg is
177
178         function inc(value : in std_logic_vector; constant by : in integer := 1) return std_logic_vector is
179         begin
180                 return std_logic_vector(UNSIGNED(value)+by);
181         end function inc;
182         
183         function log2c(constant value : in integer range 0 to integer'high) return integer is
184                 variable ret_value : integer;
185                 variable cur_value : integer;
186         begin
187                 ret_value := 0;
188                 cur_value := 1;
189                 
190                 while cur_value < value loop
191                         ret_value := ret_value + 1;
192                         cur_value := cur_value * 2;
193                 end loop;
194                 return ret_value;
195         end function log2c;
196         
197 end package body common_pkg;