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

Subversion Repositories opencpu32

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

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

powered by: WebSVN 2.1.0

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