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

Subversion Repositories opencpu32

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

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

powered by: WebSVN 2.1.0

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