d111b7da6241b9698ef15d7cb092fa51364f44b3
[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         
63         constant CARRY_OPT : integer := 2;
64         constant BYTE_OPT : integer := 2;
65         constant LDI_REPLACE_OPT : integer := 2;
66
67         constant RIGHT_OPT : integer := 3;
68         constant JMP_REG_OPT : integer := 3;
69         constant ST_OPT  : integer := 3; -- store opt
70         constant RET_OPT : integer := 3;
71         
72         constant NO_PSW_OPT : integer := 4;--no sharing
73         constant NO_DST_OPT : integer := 5; --no sharing
74         
75         type op_info_t is (ADDSUB_OP,AND_OP,OR_OP, XOR_OP,SHIFT_OP, LDST_OP, JMP_OP, JMP_ST_OP, STACK_OP);
76         subtype op_opt_t is std_logic_vector(NUM_OP_OPT_WIDTH-1 downto 0);
77         
78         
79         type instruction_rec is record
80
81                 predicates : std_logic_vector(3 downto 0);
82
83                 opcode : opcode_t;
84
85                 reg_dest_addr : std_logic_vector(REG_ADDR_WIDTH-1 downto 0);
86                 reg_src1_addr : std_logic_vector(REG_ADDR_WIDTH-1 downto 0);
87                 reg_src2_addr : std_logic_vector(REG_ADDR_WIDTH-1 downto 0);
88
89                 immediate : std_logic_vector(WORD_WIDTH-1 downto 0);
90
91                 displacement : gp_register_t;
92
93                 jmptype : std_logic_vector(1 downto 0);
94
95                 high_low, fill, signext, bp: std_logic;
96
97                 op_detail : op_opt_t;
98                 op_group : op_info_t;
99
100         end record;
101
102
103         
104         type read_through_write_rec is record
105
106                 rtw_reg : gp_register_t;
107                 rtw_reg1 : std_logic;
108                 rtw_reg2 : std_logic;
109                 immediate : gp_register_t;
110                 imm_set : std_logic;
111                 reg1_addr : gp_addr_t;
112                 reg2_addr : gp_addr_t;
113
114         end record;
115
116         type dec_op is record
117                 condition : condition_t;
118                 op_group : op_info_t;
119                 op_detail : op_opt_t;
120                 brpr : std_logic;
121
122                 displacement : gp_register_t;
123                 prog_cnt     : instr_addr_t;
124                 
125                 src1 : gp_register_t;
126                 src2 : gp_register_t;
127                 
128                 saddr1 : gp_addr_t;
129                 saddr2 : gp_addr_t;
130                 
131                 daddr   : gp_addr_t;
132                 
133         end record;
134
135         type writeback_rec is record
136 --              result : in gp_register_t;      --reg  (alu result or jumpaddr)
137 --              result_addr : in gp_addr_t;     --reg
138                 address : word_t;               --ureg 
139 --              alu_jmp : in std_logic;         --reg
140 --              br_pred : in std_logic;         --reg
141 --              write_en : in std_logic;        --reg  (register file)
142                 dmem_en : std_logic;            --ureg (jump addr in mem or in address)
143                 dmem_write_en : std_logic;      --ureg
144                 hword : std_logic;              --ureg
145                 byte_s : std_logic;
146                 byte_en : byte_en_t;
147                 data : gp_register_t;
148         end record;
149         
150         type exec2wb_rec is record
151                         result : gp_register_t; --reg  (alu result or jumpaddr)
152                         result_addr : gp_addr_t;        --reg
153                         address : word_t;               --ureg 
154                         ram_data : word_t;              --ureg
155                         alu_jmp : std_logic;            --reg
156                         br_pred : std_logic;            --reg
157                         write_en : std_logic;   --reg  (register file) bei jump 1 wenn addr in result
158                         dmem_en : std_logic;            --ureg (jump addr in mem or in address)
159                         dmem_write_en : std_logic;      --ureg
160                         hword : std_logic;              --ureg
161                         byte_s : std_logic;             --ureg  
162         end record;
163         
164         function inc(value : in std_logic_vector; constant by : in integer := 1) return std_logic_vector;
165         function log2c(constant value : in integer range 0 to integer'high) return integer;
166 end package common_pkg;
167
168 package body common_pkg is
169
170         function inc(value : in std_logic_vector; constant by : in integer := 1) return std_logic_vector is
171         begin
172                 return std_logic_vector(UNSIGNED(value)+by);
173         end function inc;
174         
175         function log2c(constant value : in integer range 0 to integer'high) return integer is
176                 variable ret_value : integer;
177                 variable cur_value : integer;
178         begin
179                 ret_value := 0;
180                 cur_value := 1;
181                 
182                 while cur_value < value loop
183                         ret_value := ret_value + 1;
184                         cur_value := cur_value * 2;
185                 end loop;
186                 return ret_value;
187         end function log2c;
188         
189 end package body common_pkg;