OpenCores
URL https://opencores.org/ocsvn/opencpu32/opencpu32/trunk

Subversion Repositories opencpu32

[/] [opencpu32/] [trunk/] [hdl/] [opencpu32/] [pkgOpenCPU32.vhd] - Blame information for rev 38

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 leonardoar
--! @file
2
--! @brief 2:1 CPU global Definitions
3 14 leonardoar
 
4
--! @mainpage
5
--! <H1>Main document of the OpenCPU32 project</H1>\n
6 38 leonardoar
--! <H2>Features</H2>
7
--! 32 Bits \n
8
--! RISC \n\n
9
--! Interesting links \n
10
--! http://www.ohwr.org/projects \n
11
--! http://opencores.org/ \n
12 5 leonardoar
 
13
--! Use standard library
14 24 leonardoar
library ieee;
15
use ieee.STD_LOGIC_1164.all;
16
use ieee.std_logic_unsigned.all;
17
use ieee.std_logic_arith.all;
18 5 leonardoar
 
19
package pkgOpenCPU32 is
20
 
21
--! Declare constants, enums, functions used by the design
22 8 leonardoar
constant nBits          : integer := 32;
23 24 leonardoar
constant instructionSize : integer := nBits;
24 8 leonardoar
 
25 14 leonardoar
--! Number of general registers (r0..r15)
26
constant numGenRegs : integer := 16;
27
 
28 20 leonardoar
type aluOps is (alu_pass, alu_passB, alu_sum, alu_sub, alu_inc, alu_dec, alu_mul, alu_or, alu_and,
29 17 leonardoar
        alu_xor, alu_not, alu_shfLt, alu_shfRt, alu_roLt, alu_roRt);
30 14 leonardoar
type typeEnDis is (enable, disable);
31 20 leonardoar
type generalRegisters is (r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15);
32 27 leonardoar
type dpMuxInputs is (fromMemory, fromImediate, fromRegFileA, fromRegFileB, fromAlu);
33
type dpMuxAluIn is (fromMemory, fromImediate, fromRegFileA);
34 24 leonardoar
type controlUnitStates is (initial, fetch, decode, execute, executing);
35 33 leonardoar
type executionStates is (initInstructionExecution, waitToExecute, writeRegister, releaseWriteRead, readRegisterA, readRegisterB, releaseRead);
36 28 leonardoar
 
37
--! Flags positions
38
constant flag_sign         : integer := 2;
39
constant flag_zero         : integer := 1;
40
constant flag_carry        : integer := 0;
41 14 leonardoar
 
42
function reg2Num (a: generalRegisters) return integer;
43
function Num2reg (a: integer) return generalRegisters;
44 27 leonardoar
function muxPos( a: dpMuxInputs) return std_logic_vector;
45
function muxRegPos(a: dpMuxAluIn) return std_logic_vector;
46
function opcode2AluOp (opcode : std_logic_vector(5 downto 0)) return aluOps;
47 24 leonardoar
 
48
-- Opcodes
49 26 leonardoar
subtype opcodes is std_logic_vector(5 downto 0); -- 6 Bits (64 instructions max)
50 24 leonardoar
 
51
-- Each instruction will take 32 bits
52
-- Tutorial on using records.. (http://vhdlguru.blogspot.com.br/2010/02/arrays-and-records-in-vhdl.html)
53
type instructionType is record
54
        opcode : std_logic_vector(5 downto 0);
55
        reg1   : std_logic_vector(3 downto 0);
56
        reg2   : std_logic_vector(3 downto 0);
57
        imm    : std_logic_vector(15 downto 0); -- Max imediate value (16 bits)
58
end record;
59
 
60
 
61
-- Data movement
62
constant mov_reg  : opcodes := conv_std_logic_vector(0,6);        -- Move data between registers
63
constant mov_val  : opcodes := conv_std_logic_vector(1,6);   -- Move data from imediate value to a register
64
constant stom_reg : opcodes := conv_std_logic_vector(2,6);   -- Store a value in memory coming from a register
65
constant stom_val : opcodes := conv_std_logic_vector(3,6);   -- Store a value in memory coming from imediate
66
constant ld_reg   : opcodes := conv_std_logic_vector(4,6);   -- Load a value from memory into a register
67
constant ld_val   : opcodes := conv_std_logic_vector(5,6);   -- Load a value from memoru into another address in memory
68
 
69
-- Jump instructions
70
constant jmp_val  : opcodes := conv_std_logic_vector(6,6);       -- Jump (PC <= Val)
71
constant jmpr_val : opcodes := conv_std_logic_vector(7,6);   -- Jump relative (PC <= PC + Val)
72
constant jz_val   : opcodes := conv_std_logic_vector(8,6);   -- Jump if zero
73
constant jzr_val  : opcodes := conv_std_logic_vector(9,6);   -- Jump if zero relative
74
constant jnz_val  : opcodes := conv_std_logic_vector(10,6);  -- Jump if not zero
75
constant jnzr_val : opcodes := conv_std_logic_vector(11,6);  -- Jump if not zero relative
76
constant call_reg : opcodes := conv_std_logic_vector(12,6);  -- Jump to address (Save return value on the stack
77
constant ret_reg  : opcodes := conv_std_logic_vector(13,6);  -- Pop return value from the stack and jump to it
78
 
79
-- Logical instructions
80
constant and_reg  : opcodes := conv_std_logic_vector(14,6);  -- And between to registers
81
constant and_val  : opcodes := conv_std_logic_vector(15,6);  -- And between register and imediate
82
constant or_reg   : opcodes := conv_std_logic_vector(16,6);  -- Or between to registers
83
constant or_val   : opcodes := conv_std_logic_vector(17,6);  -- Or between register and imediate
84
constant xor_reg  : opcodes := conv_std_logic_vector(18,6);  -- Xor between to registers
85
constant xor_val  : opcodes := conv_std_logic_vector(19,6);  -- Xor between register and imediate
86
constant not_reg  : opcodes := conv_std_logic_vector(20,6);  -- Not on register
87
constant shl_reg  : opcodes := conv_std_logic_vector(21,6);  -- Shift left register (one shift)
88
constant shr_reg  : opcodes := conv_std_logic_vector(22,6);  -- Shift right register (one shift)
89
constant rol_reg  : opcodes := conv_std_logic_vector(23,6);  -- Rotate left register (one rotation)
90
constant ror_reg  : opcodes := conv_std_logic_vector(24,6);  -- Rotate right register (one rotation)
91
constant sbit_reg : opcodes := conv_std_logic_vector(25,6);  -- Set bit pointed by register
92
constant cbit_reg : opcodes := conv_std_logic_vector(26,6);  -- Clear bit pointed by register
93
 
94
-- Math operations instructions (unsigned)
95
constant add_reg  : opcodes := conv_std_logic_vector(27,6);  -- Add to registers
96
constant add_val  : opcodes := conv_std_logic_vector(28,6);  -- Add register and a imediate value
97
constant sub_reg  : opcodes := conv_std_logic_vector(29,6);  -- Subtract to registers
98
constant sub_val  : opcodes := conv_std_logic_vector(30,6);  -- Subtract register and a imediate value
99
constant inc_reg  : opcodes := conv_std_logic_vector(31,6);  -- Increment register
100
constant dec_reg  : opcodes := conv_std_logic_vector(32,6);  -- Decrement register
101
 
102
-- Control opcodes
103 29 leonardoar
constant nop      : opcodes := conv_std_logic_vector(33,6);  -- Nop...
104
constant halt     : opcodes := conv_std_logic_vector(34,6);  -- Halt processor
105 24 leonardoar
 
106 5 leonardoar
end pkgOpenCPU32;
107
 
108
--! Define functions or procedures
109 14 leonardoar
package body pkgOpenCPU32 is
110
 
111 20 leonardoar
function muxPos( a: dpMuxInputs) return std_logic_vector is
112
variable valRet : std_logic_vector(2 downto 0);
113
begin
114
        case a is
115
                when fromMemory => valRet := "000";
116
                when fromImediate => valRet := "001";
117
                when fromRegFileA => valRet := "010";
118
                when fromRegFileB => valRet := "011";
119
                when fromAlu => valRet := "100";
120
        end case;
121
        return valRet;
122
end muxPos;
123
 
124 27 leonardoar
function muxRegPos(a: dpMuxAluIn) return std_logic_vector is
125
variable valRet : std_logic_vector(1 downto 0);
126
begin
127
        case a is
128
                when fromMemory => valRet := "00";
129
                when fromImediate => valRet := "01";
130
                when fromRegFileA => valRet := "10";
131
        end case;
132
        return valRet;
133
end muxRegPos;
134
 
135 14 leonardoar
function reg2Num (a: generalRegisters) return integer is
136 27 leonardoar
variable valRet : integer;
137
begin
138
 case a is
139
        when r0 => valRet := 0;
140
        when r1 => valRet := 1;
141
        when r2 => valRet := 2;
142
        when r3 => valRet := 3;
143
        when r4 => valRet := 4;
144
        when r5 => valRet := 5;
145
        when r6 => valRet := 6;
146
        when r7 => valRet := 7;
147
        when r8 => valRet := 8;
148
        when r9 => valRet := 9;
149
        when r10 => valRet := 10;
150
        when r11 => valRet := 11;
151
        when r12 => valRet := 12;
152
        when r13 => valRet := 13;
153
        when r14 => valRet := 14;
154
        when r15 => valRet := 15;
155
 end case;
156
 return valRet;
157
end reg2Num;
158 14 leonardoar
 
159
function Num2reg (a: integer) return generalRegisters is
160 27 leonardoar
variable valRet : generalRegisters;
161
begin
162
 case a is
163
        when 0 => valRet := r0;
164
        when 1 => valRet := r1;
165
        when 2 => valRet := r2;
166
        when 3 => valRet := r3;
167
        when 4 => valRet := r4;
168
        when 5 => valRet := r5;
169
        when 6 => valRet := r6;
170
        when 7 => valRet := r7;
171
        when 8 => valRet := r8;
172
        when 9 => valRet := r9;
173
        when 10 => valRet := r10;
174
        when 11 => valRet := r11;
175
        when 12 => valRet := r12;
176
        when 13 => valRet := r13;
177
        when 14 => valRet := r14;
178
        when 15 => valRet := r15;
179
        when others => valRet := r0;
180
 end case;
181
 return valRet;
182
end Num2reg;
183
 
184
function opcode2AluOp (opcode : std_logic_vector(5 downto 0)) return aluOps is
185
variable valRet : aluOps;
186
begin
187
        case opcode is
188
                when add_reg | add_val => valRet := alu_sum;
189
                when sub_reg | sub_val => valRet := alu_sub;
190
                when inc_reg => valRet := alu_inc;
191
                when dec_reg => valRet := alu_dec;
192
                when others => valRet := alu_pass;
193
        end case;
194
        return valRet;
195
end opcode2AluOp;
196 5 leonardoar
 
197
end pkgOpenCPU32;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.