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

Subversion Repositories opencpu32

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

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

powered by: WebSVN 2.1.0

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