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

Subversion Repositories opencpu32

[/] [opencpu32/] [trunk/] [hdl/] [opencpu32/] [ControlUnit.vhd] - Blame information for rev 44

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

Line No. Rev Author Line
1 22 leonardoar
--! @file
2
--! @brief ControlUnit http://en.wikipedia.org/wiki/Control_unit
3
 
4
--! Use standard library and import the packages (std_logic_1164,std_logic_unsigned,std_logic_arith)
5
library IEEE;
6
use ieee.std_logic_1164.all;
7
use ieee.std_logic_unsigned.all;
8
use ieee.std_logic_arith.all;
9
 
10
--! Use CPU Definitions package
11
use work.pkgOpenCPU32.all;
12
 
13
--! The control unit coordinates the input and output devices of a computer system. It fetches the code of all of the instructions \n
14
--! in the microprograms. It directs the operation of the other units by providing timing and control signals. \n
15
--! all computer resources are managed by the Control Unit.It directs the flow of data between the cpu and the other devices.\n
16
--! The outputs of the control unit control the activity of the rest of the device. A control unit can be thought of as a finite-state machine.
17
 
18
--! The purpose of datapaths is to provide routes for data to travel between functional units.
19
entity ControlUnit is
20 24 leonardoar
    generic (n : integer := nBits - 1);                                                                 --! Generic value (Used to easily change the size of the Alu on the package)
21
         Port ( reset : in  STD_LOGIC;
22 28 leonardoar
           clk : in  STD_LOGIC;                                                                                         --! Main system clock
23 30 leonardoar
           FlagsDp : in  STD_LOGIC_VECTOR (2 downto 0);                          --! Flags comming from the Datapath
24 28 leonardoar
           DataDp : in  STD_LOGIC_VECTOR (n downto 0);                           --! Data comming from the Datapath
25 31 leonardoar
                          outEnDp : out  typeEnDis;                                                                             --! Enable/Disable datapath output
26 42 leonardoar
           MuxDp : out  dpMuxInputs;                                                                            --! Select on datapath data from (Memory, Imediate, RegFileA, RegFileB, AluOut)
27 27 leonardoar
                          MuxRegDp : out STD_LOGIC_VECTOR(1 downto 0);                           --! Select Alu InputA (Memory,Imediate,RegFileA)
28 28 leonardoar
           ImmDp : out  STD_LOGIC_VECTOR (n downto 0);                           --! Imediate value passed to the Datapath
29 27 leonardoar
           DpAluOp : out  aluOps;                                                                                       --! Alu operations
30 28 leonardoar
                          DpRegFileWriteAddr : out  generalRegisters;                           --! General register address to write
31
           DpRegFileWriteEn : out  STD_LOGIC;                                                   --! Enable register write
32
           DpRegFileReadAddrA : out  generalRegisters;                          --! General register address to read
33
           DpRegFileReadAddrB : out  generalRegisters;                          --! General register address to read
34
           DpRegFileReadEnA : out  STD_LOGIC;                                                   --! Enable register read (PortA)
35
           DpRegFileReadEnB : out  STD_LOGIC;                                                   --! Enable register read (PortB)
36
           MemoryDataReadEn : out std_logic;                                                            --! Enable Main memory read
37
                          MemoryDataWriteEn: out std_logic;                                                             --! Enable Main memory write
38
                          MemoryDataInput : in  STD_LOGIC_VECTOR (n downto 0);   --! Incoming data from main memory
39 30 leonardoar
           MemoryDataRdAddr : out  STD_LOGIC_VECTOR (n downto 0);        --! Main memory Read address
40
                          MemoryDataWrAddr : out  STD_LOGIC_VECTOR (n downto 0); --! Main memory Write address
41 28 leonardoar
           MemoryDataOut : out  STD_LOGIC_VECTOR (n downto 0));  --! Data to write on main memory
42 22 leonardoar
end ControlUnit;
43
 
44
--! @brief ControlUnit http://en.wikipedia.org/wiki/Control_unit
45
--! @details The control unit receives external instructions or commands which it converts into a sequence of control signals that the control \n
46
--! unit applies to data path to implement a sequence of register-transfer level operations.
47
architecture Behavioral of ControlUnit is
48 26 leonardoar
 
49 25 leonardoar
signal currentCpuState : controlUnitStates;                                     -- CPU states
50
signal nextCpuState    : controlUnitStates;                                     -- CPU states
51 26 leonardoar
 
52
signal currentExState  : executionStates;                                               -- Execution states
53
signal nextExState     : executionStates;                                               -- Execution states
54
 
55 25 leonardoar
signal PC              : std_logic_vector(n downto 0);   -- Program Counter
56
signal IR              : std_logic_vector(n downto 0);   -- Intruction register
57
signal currInstruction : std_logic_vector(n downto 0);   -- Current Intruction
58 22 leonardoar
begin
59 24 leonardoar
 
60 26 leonardoar
        -- Next state logic (CPU, fetch, decode, execute states)
61 24 leonardoar
        process (clk, reset)
62
        begin
63
                if (reset = '1') then
64 26 leonardoar
                        currentCpuState <= initial;
65 24 leonardoar
                elsif rising_edge(clk) then
66
                        currentCpuState <= nextCpuState;
67
                end if;
68
        end process;
69
 
70 26 leonardoar
        -- Next state logic (Execution states)
71
        process (clk, currentCpuState)
72
        begin
73 33 leonardoar
                if (reset = '1') then
74 27 leonardoar
                        currentExState <= initInstructionExecution;
75 26 leonardoar
                elsif rising_edge(clk) then
76
                        currentExState <= nextExState;
77
                end if;
78
        end process;
79
 
80 29 leonardoar
        -- States Fetch, decode, execute from the processor (Also handles the execution of jump instructions)
81 24 leonardoar
        process (currentCpuState)
82 44 leonardoar
        variable cyclesExecute : integer range 0 to 20;          -- Cycles to wait while executing instruction
83 29 leonardoar
        variable opcodeIR : std_logic_vector(5 downto 0);
84
        variable operand_reg1 : std_logic_vector(3 downto 0);
85
        variable operand_imm  : std_logic_vector(21 downto 0);
86 44 leonardoar
        variable accDp : std_logic_vector(n downto 0);                   -- Value stored from DataPath
87 24 leonardoar
        begin
88 29 leonardoar
                opcodeIR := IR((IR'HIGH) downto (IR'HIGH - 5));
89
                operand_reg1 := IR((IR'HIGH - 6) downto (IR'HIGH - 9));         -- 4 bits register operand1 (Max 16 registers)
90
                operand_imm  := IR((IR'HIGH - 10) downto (IR'LOW));                     -- 22 bits imediate value (Max value 4194304)
91 24 leonardoar
                case currentCpuState is
92
                        -- Initial state left from reset ...
93
                        when initial =>
94 25 leonardoar
                                cyclesExecute := 0;
95 24 leonardoar
                                PC <= (others => '0');
96 25 leonardoar
                                IR <= (others => '0');
97 30 leonardoar
                                MemoryDataRdAddr <= (others => '0');
98 28 leonardoar
                                MemoryDataReadEn <= '0';
99 30 leonardoar
                                MemoryDataWriteEn <= '0';
100 24 leonardoar
                                nextCpuState <= fetch;
101
 
102
                        -- Fetch state (Go to memory and get a instruction)
103
                        when fetch =>
104
                                -- Increment program counter (Remember that PC will be update only on the next cycle...
105
                                PC <= PC + conv_std_logic_vector(1, nBits);
106 30 leonardoar
                                MemoryDataRdAddr <= PC; -- Warning PC is not 1 yet...
107 24 leonardoar
                                IR <= MemoryDataInput;
108 28 leonardoar
                                MemoryDataReadEn <= '1';
109 44 leonardoar
                                MemoryDataWriteEn <= '0';
110 24 leonardoar
                                nextCpuState <= decode;
111
 
112 25 leonardoar
                        -- Detect with instruction came from memory, set the number of cycles to execute...
113 24 leonardoar
                        when decode =>
114 28 leonardoar
                                MemoryDataReadEn <= '0';
115
                                MemoryDataWriteEn <= '0';
116 25 leonardoar
 
117
                                -- The high attribute points to the highes bit position
118 26 leonardoar
                                case opcodeIR is
119 44 leonardoar
                                        when mov_reg | mov_val | add_reg | add_val | sub_reg | and_reg | or_reg | xor_reg =>
120 25 leonardoar
                                                        nextCpuState <= execute;
121 34 leonardoar
                                                        cyclesExecute := 1;     -- Wait 1 cycles
122 25 leonardoar
                                                        currInstruction <= IR;
123 44 leonardoar
 
124
                                        when ld_reg | ld_val | stom_reg | stom_val =>
125
                                                        nextCpuState <= execute;
126
                                                        cyclesExecute := 2;     -- Wait 2 cycles
127
                                                        currInstruction <= IR;
128 29 leonardoar
 
129
                                        when jmp_val | jmpr_val =>
130
                                                nextCpuState <= execute;
131 34 leonardoar
                                                cyclesExecute := 0;              -- No Wait cycle
132 29 leonardoar
 
133 26 leonardoar
                                        -- Invalid instruction (Now will be ignored, but latter should raise a trap
134 29 leonardoar
                                        when others =>
135
                                                null;
136 25 leonardoar
                                end case;
137 24 leonardoar
 
138 25 leonardoar
                        -- Wait while the process that handles the execution works..
139
                        when execute =>
140 29 leonardoar
                                -- On the case of jump instructions, it's execution will be handled on this process
141
                                case opcodeIR is
142 31 leonardoar
 
143 29 leonardoar
                                        when jmp_val =>
144
                                                PC      <= "0000000000" & operand_imm;
145 31 leonardoar
 
146 29 leonardoar
                                        when jmpr_val =>
147
                                                PC      <= PC + ("0000000000" & operand_imm);
148 31 leonardoar
 
149
                                        when ld_val =>
150
                                                MemoryDataRdAddr <= "0000000000" & operand_imm;
151
                                                MemoryDataReadEn <= '1';
152
 
153 44 leonardoar
                                        -- STORE r1,10 (Store the value 10 on memory address pointed by r1)
154
                                        when stom_val =>
155
                                                -- And put the imediate value ...                                                       
156
                                                        MemoryDataOut <= "0000000000" & operand_imm;
157
                                                        if cyclesExecute = 1 then
158
                                                                -- After the register data is avaible in DataDp we put it's address and                                                         
159
                                                                accDp := DataDp;
160
                                                                MemoryDataWrAddr <= accDp;
161
                                                        elsif cyclesExecute = 0 then
162
                                                                -- strobe in to enter the data
163
                                                                MemoryDataWriteEn <= '1';
164
                                                        end if;
165 31 leonardoar
 
166 29 leonardoar
                                        when others =>
167
                                                null;
168
                                end case;
169
 
170 26 leonardoar
                                if cyclesExecute = 0 then
171
                                        -- Finish the instruction execution get next
172 25 leonardoar
                                        nextCpuState <= fetch;
173 26 leonardoar
                                else
174 44 leonardoar
                                        nextCpuState <= executing;
175 26 leonardoar
                                end if;
176
 
177
                        -- Just wait a cycle and back again to execute state which verify if still need to wait some cycles
178
                        when executing =>
179
                                cyclesExecute := cyclesExecute - 1;
180 44 leonardoar
                                nextCpuState <= execute;
181 26 leonardoar
 
182 24 leonardoar
                        when others =>
183
                                null;
184
                end case;
185 25 leonardoar
        end process;
186
 
187 31 leonardoar
        -- Process that handles the execution of each instruction (Excluding the call,jump,load,store instructions)
188 26 leonardoar
        process (currentExState)
189
        --variable operando1_reg : std_logic_vector(generalRegisters'range);
190 27 leonardoar
        variable opcodeIR     : std_logic_vector(5 downto 0);
191
        variable operand_reg1 : std_logic_vector(3 downto 0);
192
        variable operand_reg2 : std_logic_vector(3 downto 0);
193
        variable operand_imm  : std_logic_vector(21 downto 0);
194 25 leonardoar
        begin
195 27 leonardoar
                -- Parse the common operands
196
                opcodeIR := IR((IR'HIGH) downto (IR'HIGH - 5));                                 -- 6 Bits opcode (Max 64 instructions)
197
                operand_reg1 := IR((IR'HIGH - 6) downto (IR'HIGH - 9));         -- 4 bits register operand1 (Max 16 registers)
198
                operand_reg2 := IR((IR'HIGH - 10) downto (IR'HIGH - 13));   -- 4 bits register operand2 (Max 16 registers
199
                operand_imm  := IR((IR'HIGH - 10) downto (IR'LOW));                     -- 22 bits imediate value (Max value 4194304)
200
 
201
                -- Select the instruction and init it's execution
202 26 leonardoar
                case currentExState is
203 27 leonardoar
                        when initInstructionExecution =>
204 33 leonardoar
                                nextExState <= waitToExecute;
205
 
206
                        when waitToExecute =>
207
                                if ( (currentCpuState /= execute) and (currentCpuState /= executing) ) then
208 44 leonardoar
                                        nextExState <= initInstructionExecution;
209 33 leonardoar
                                else
210
                                        case opcodeIR is
211 27 leonardoar
                                        -- MOV r2,r1 (See the testDatapath to see how to drive the datapath for this function)
212
                                        when mov_reg =>
213 42 leonardoar
                                                MuxDp <= fromRegFileB;
214 27 leonardoar
                                                DpRegFileReadAddrB <= Num2reg(conv_integer(UNSIGNED(operand_reg2)));
215
                                                DpRegFileWriteAddr <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));
216
                                                DpRegFileReadEnB <= '1';
217
                                                nextExState <= writeRegister;
218
 
219 31 leonardoar
                                        -- LOAD r1,10 (Load into r1, the value in the main memory located at address 10)
220
                                        when ld_val =>
221 42 leonardoar
                                                MuxDp <= fromMemory;
222 31 leonardoar
                                                DpRegFileWriteAddr <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));
223
                                                -- The part that interface with the memory is located on the first process
224
                                                nextExState <= writeRegister;
225
 
226 44 leonardoar
                                        -- STORE r1,10 (Store the value 10 on the main memory pointed by r1)
227 31 leonardoar
                                        when stom_val =>
228 42 leonardoar
                                        MuxDp <= fromRegFileB;
229 31 leonardoar
                                        DpRegFileReadAddrB <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));
230 44 leonardoar
                                        DpRegFileReadEnB <= '1';
231 31 leonardoar
                                        -- The part that interface with the memory is located on the first process
232 44 leonardoar
                                        nextExState <= readRegisterB;
233 31 leonardoar
 
234 27 leonardoar
                                        -- ADD r2,r0 (See the testDatapath to see how to drive the datapath for this function)
235
                                        when add_reg | sub_reg | and_reg | or_reg | xor_reg =>
236 42 leonardoar
                                                MuxDp <= fromAlu;
237 27 leonardoar
                                                MuxRegDp <= muxRegPos(fromRegFileA);
238
                                                DpRegFileReadAddrA <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));    -- Read first operand
239
                                                DpRegFileReadAddrB <= Num2reg(conv_integer(UNSIGNED(operand_reg2))); -- Read second operand
240
                                                DpRegFileReadEnA <= '1';
241
                                                DpRegFileReadEnB <= '1';
242
                                                DpRegFileWriteAddr <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));    -- Point to write in first operand (pointing to register)                                       
243
                                                DpAluOp <= opcode2AluOp(opcodeIR);      -- Select the alu operation from the operand
244
                                                nextExState <= writeRegister;
245
 
246
                                        -- MOV r0,10d (See the testDatapath to see how to drive the datapath for this function)
247
                                        when mov_val =>
248 42 leonardoar
                                                MuxDp <= fromImediate;
249 27 leonardoar
                                                DpRegFileWriteAddr <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));
250
                                                ImmDp <= "0000000000" & operand_imm;    -- & is used to concatenate signals
251
                                                nextExState <= writeRegister;
252
 
253
                                        -- ADD r3,2 (r2 <= r2+2) (See the testDatapath to see how to drive the datapath for this function)
254
                                        when add_val | sub_val | and_val | or_val | xor_val =>
255 42 leonardoar
                                                MuxDp <= fromAlu;
256 27 leonardoar
                                                MuxRegDp <= muxRegPos(fromImediate);
257
                                                DpRegFileWriteAddr <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));
258
                                                DpRegFileReadAddrB <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));    -- Read first operand
259
                                                DpRegFileReadEnB <= '1';
260
                                                ImmDp <= "0000000000" & operand_imm;    -- & is used to concatenate signals                                             
261
                                                DpAluOp <= opcode2AluOp(opcodeIR);      -- Select the alu operation from the operand
262
                                                nextExState <= writeRegister;
263
 
264 26 leonardoar
                                        when others =>
265
                                                null;
266 33 leonardoar
                                        end case;
267
                                end if;
268 27 leonardoar
 
269
                        -- Write something on the register files
270
                        when writeRegister =>
271
                                DpRegFileWriteEn <= '1';
272
                                nextExState <= releaseWriteRead;
273
 
274 31 leonardoar
                        when readRegisterB =>
275
                                DpRegFileReadEnB <= '1';
276
                                outEnDp <= enable;
277
                                nextExState <= releaseWriteRead;
278
 
279
                        when readRegisterA =>
280
                                DpRegFileReadEnA <= '1';
281
                                outEnDp <= enable;
282
                                nextExState <= releaseWriteRead;
283
 
284 27 leonardoar
                        -- Release lines (Reset Datapath lines to something that does nothing...)
285
                        when releaseWriteRead =>
286
                                DpRegFileReadEnB <= '0';
287
                                DpRegFileReadEnA <= '0';
288 31 leonardoar
                                DpRegFileWriteEn <= '0';
289 33 leonardoar
                                outEnDp <= disable;
290
                                -- Come back to waiting state
291 34 leonardoar
                                nextExState <= waitToExecute;
292 27 leonardoar
 
293 26 leonardoar
                        when others =>
294
                                null;
295
                end case;
296 24 leonardoar
        end process;
297 22 leonardoar
 
298
end Behavioral;
299
 

powered by: WebSVN 2.1.0

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