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

Subversion Repositories opencpu32

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

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 47 leonardoar
                          MuxRegDp : out dpMuxAluIn;                                                                            --! 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 46 leonardoar
                                        -- ld r5,20 (Load into r5 register the content of the memory at address 20)
150 31 leonardoar
                                        when ld_val =>
151
                                                MemoryDataRdAddr <= "0000000000" & operand_imm;
152 46 leonardoar
                                                MemoryDataReadEn <= '1';
153
                                                if cyclesExecute = 0 then
154
                                                        MemoryDataReadEn <= '0';
155
                                                end if;
156 31 leonardoar
 
157 44 leonardoar
                                        -- STORE r1,10 (Store the value 10 on memory address pointed by r1)
158
                                        when stom_val =>
159
                                                -- And put the imediate value ...                                                       
160
                                                        MemoryDataOut <= "0000000000" & operand_imm;
161
                                                        if cyclesExecute = 1 then
162
                                                                -- After the register data is avaible in DataDp we put it's address and                                                         
163
                                                                accDp := DataDp;
164
                                                                MemoryDataWrAddr <= accDp;
165
                                                        elsif cyclesExecute = 0 then
166
                                                                -- strobe in to enter the data
167
                                                                MemoryDataWriteEn <= '1';
168
                                                        end if;
169 31 leonardoar
 
170 29 leonardoar
                                        when others =>
171
                                                null;
172
                                end case;
173
 
174 26 leonardoar
                                if cyclesExecute = 0 then
175
                                        -- Finish the instruction execution get next
176 25 leonardoar
                                        nextCpuState <= fetch;
177 26 leonardoar
                                else
178 44 leonardoar
                                        nextCpuState <= executing;
179 26 leonardoar
                                end if;
180
 
181
                        -- Just wait a cycle and back again to execute state which verify if still need to wait some cycles
182
                        when executing =>
183
                                cyclesExecute := cyclesExecute - 1;
184 44 leonardoar
                                nextCpuState <= execute;
185 26 leonardoar
 
186 24 leonardoar
                        when others =>
187
                                null;
188
                end case;
189 25 leonardoar
        end process;
190
 
191 31 leonardoar
        -- Process that handles the execution of each instruction (Excluding the call,jump,load,store instructions)
192 26 leonardoar
        process (currentExState)
193
        --variable operando1_reg : std_logic_vector(generalRegisters'range);
194 27 leonardoar
        variable opcodeIR     : std_logic_vector(5 downto 0);
195
        variable operand_reg1 : std_logic_vector(3 downto 0);
196
        variable operand_reg2 : std_logic_vector(3 downto 0);
197
        variable operand_imm  : std_logic_vector(21 downto 0);
198 25 leonardoar
        begin
199 27 leonardoar
                -- Parse the common operands
200
                opcodeIR := IR((IR'HIGH) downto (IR'HIGH - 5));                                 -- 6 Bits opcode (Max 64 instructions)
201
                operand_reg1 := IR((IR'HIGH - 6) downto (IR'HIGH - 9));         -- 4 bits register operand1 (Max 16 registers)
202
                operand_reg2 := IR((IR'HIGH - 10) downto (IR'HIGH - 13));   -- 4 bits register operand2 (Max 16 registers
203
                operand_imm  := IR((IR'HIGH - 10) downto (IR'LOW));                     -- 22 bits imediate value (Max value 4194304)
204
 
205
                -- Select the instruction and init it's execution
206 26 leonardoar
                case currentExState is
207 27 leonardoar
                        when initInstructionExecution =>
208 33 leonardoar
                                nextExState <= waitToExecute;
209
 
210
                        when waitToExecute =>
211
                                if ( (currentCpuState /= execute) and (currentCpuState /= executing) ) then
212 44 leonardoar
                                        nextExState <= initInstructionExecution;
213 33 leonardoar
                                else
214
                                        case opcodeIR is
215 27 leonardoar
                                        -- MOV r2,r1 (See the testDatapath to see how to drive the datapath for this function)
216
                                        when mov_reg =>
217 42 leonardoar
                                                MuxDp <= fromRegFileB;
218 27 leonardoar
                                                DpRegFileReadAddrB <= Num2reg(conv_integer(UNSIGNED(operand_reg2)));
219
                                                DpRegFileWriteAddr <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));
220
                                                DpRegFileReadEnB <= '1';
221
                                                nextExState <= writeRegister;
222
 
223 31 leonardoar
                                        -- LOAD r1,10 (Load into r1, the value in the main memory located at address 10)
224
                                        when ld_val =>
225 42 leonardoar
                                                MuxDp <= fromMemory;
226 31 leonardoar
                                                DpRegFileWriteAddr <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));
227
                                                -- The part that interface with the memory is located on the first process
228
                                                nextExState <= writeRegister;
229
 
230 44 leonardoar
                                        -- STORE r1,10 (Store the value 10 on the main memory pointed by r1)
231 31 leonardoar
                                        when stom_val =>
232 42 leonardoar
                                        MuxDp <= fromRegFileB;
233 31 leonardoar
                                        DpRegFileReadAddrB <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));
234 44 leonardoar
                                        DpRegFileReadEnB <= '1';
235 31 leonardoar
                                        -- The part that interface with the memory is located on the first process
236 44 leonardoar
                                        nextExState <= readRegisterB;
237 31 leonardoar
 
238 27 leonardoar
                                        -- ADD r2,r0 (See the testDatapath to see how to drive the datapath for this function)
239
                                        when add_reg | sub_reg | and_reg | or_reg | xor_reg =>
240 42 leonardoar
                                                MuxDp <= fromAlu;
241 47 leonardoar
                                                MuxRegDp <= fromRegFileA;
242 27 leonardoar
                                                DpRegFileReadAddrA <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));    -- Read first operand
243
                                                DpRegFileReadAddrB <= Num2reg(conv_integer(UNSIGNED(operand_reg2))); -- Read second operand
244
                                                DpRegFileReadEnA <= '1';
245
                                                DpRegFileReadEnB <= '1';
246
                                                DpRegFileWriteAddr <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));    -- Point to write in first operand (pointing to register)                                       
247
                                                DpAluOp <= opcode2AluOp(opcodeIR);      -- Select the alu operation from the operand
248
                                                nextExState <= writeRegister;
249
 
250
                                        -- MOV r0,10d (See the testDatapath to see how to drive the datapath for this function)
251
                                        when mov_val =>
252 42 leonardoar
                                                MuxDp <= fromImediate;
253 27 leonardoar
                                                DpRegFileWriteAddr <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));
254
                                                ImmDp <= "0000000000" & operand_imm;    -- & is used to concatenate signals
255
                                                nextExState <= writeRegister;
256
 
257
                                        -- ADD r3,2 (r2 <= r2+2) (See the testDatapath to see how to drive the datapath for this function)
258
                                        when add_val | sub_val | and_val | or_val | xor_val =>
259 42 leonardoar
                                                MuxDp <= fromAlu;
260 47 leonardoar
                                                MuxRegDp <= fromImediate;
261 27 leonardoar
                                                DpRegFileWriteAddr <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));
262
                                                DpRegFileReadAddrB <= Num2reg(conv_integer(UNSIGNED(operand_reg1)));    -- Read first operand
263
                                                DpRegFileReadEnB <= '1';
264
                                                ImmDp <= "0000000000" & operand_imm;    -- & is used to concatenate signals                                             
265
                                                DpAluOp <= opcode2AluOp(opcodeIR);      -- Select the alu operation from the operand
266
                                                nextExState <= writeRegister;
267
 
268 26 leonardoar
                                        when others =>
269
                                                null;
270 33 leonardoar
                                        end case;
271
                                end if;
272 27 leonardoar
 
273
                        -- Write something on the register files
274
                        when writeRegister =>
275
                                DpRegFileWriteEn <= '1';
276
                                nextExState <= releaseWriteRead;
277
 
278 31 leonardoar
                        when readRegisterB =>
279
                                DpRegFileReadEnB <= '1';
280
                                outEnDp <= enable;
281
                                nextExState <= releaseWriteRead;
282
 
283
                        when readRegisterA =>
284
                                DpRegFileReadEnA <= '1';
285
                                outEnDp <= enable;
286
                                nextExState <= releaseWriteRead;
287
 
288 27 leonardoar
                        -- Release lines (Reset Datapath lines to something that does nothing...)
289
                        when releaseWriteRead =>
290
                                DpRegFileReadEnB <= '0';
291
                                DpRegFileReadEnA <= '0';
292 31 leonardoar
                                DpRegFileWriteEn <= '0';
293 33 leonardoar
                                outEnDp <= disable;
294
                                -- Come back to waiting state
295 34 leonardoar
                                nextExState <= waitToExecute;
296 27 leonardoar
 
297 26 leonardoar
                        when others =>
298
                                null;
299
                end case;
300 24 leonardoar
        end process;
301 22 leonardoar
 
302
end Behavioral;
303
 

powered by: WebSVN 2.1.0

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