]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/tumbl.git/blob - hw/core_ctrl.vhd
9cd778d297ffe187d72bceeaf1701d086dffea82
[fpga/lx-cpu1/tumbl.git] / hw / core_ctrl.vhd
1 ---------------------------------------------------------------------------------
2 --
3 --  Entity:       core_ctrl
4 --  Filename:     core_ctrl.vhd
5 --  Description:  the control unit for the TUD MB-Lite implementation
6 --
7 --  Author:       Huib Lincklaen Arriens
8 --                Delft University of Technology
9 --                Faculty EEMCS, Department ME&CE, Circuits and Systems
10 --  Date:         December, 2010
11 --  Modified:     September, 2013: Core customization (Meloun)
12 --                September, 2012: interrupt handling corrected to let
13 --                                 a pending branch be taken first
14 --                                 (with thanks to Matthis Meier, TU Dortmund,
15 --                                  for detecting this errror).
16 --  Remarks:
17 --
18 --------------------------------------------------------------------------------
19
20 LIBRARY IEEE;
21
22 USE IEEE.std_logic_1164.all;
23 USE WORK.mbl_pkg.all;
24
25 --------------------------------------------------------------------------------
26 ENTITY core_ctrl IS
27 --------------------------------------------------------------------------------
28         GENERIC
29         (
30                 IMEM_ABITS_g         : positive := 9;
31                 COMPATIBILITY_MODE_g : BOOLEAN := FALSE
32         );
33         PORT
34         (
35                 clk_i           :  IN STD_LOGIC;
36                 rst_i           :  IN STD_LOGIC;
37                 halt_i          :  IN STD_LOGIC;
38                 int_i           :  IN STD_LOGIC;
39                 trace_i         :  IN STD_LOGIC;
40                 trace_kick_i    :  IN STD_LOGIC;
41                 core_clken_o    : OUT STD_LOGIC;
42                 -- specific fetch i/o
43                 imem_addr_o     : OUT STD_LOGIC_VECTOR ((IMEM_ABITS_g-1) DOWNTO 0);
44                 imem_clken_o    : OUT STD_LOGIC;
45                 pc_ctrl_o       : OUT STD_LOGIC;
46                 -- fetch to decode pipeline registers
47                 IF2ID_REG_i     :  IN IF2ID_Type;
48                 IF2ID_REG_o     : OUT IF2ID_Type;
49                 -- decode to exeq pipeline registers
50                 ID2EX_REG_i     :  IN ID2EX_Type;
51                 ID2EX_REG_o     : OUT ID2EX_Type;
52                 delay_bit_o     : OUT STD_LOGIC;
53                 -- GPRF control
54                 gprf_clken_o    : OUT STD_LOGIC;
55                 -- exeq to fetch feedback registers
56                 EX2IF_REG_i     :  IN EX2IF_Type;
57                 EX2IF_REG_o     : OUT EX2IF_Type;
58                 EX2CTRL_REG_i   :  IN EX2CTRL_Type;
59                 -- exeq to core (halting)
60                 exeq_halt_i     :  IN STD_LOGIC;
61                 -- exeq to mem pipeline registers
62                 EX2MEM_REG_i    :  IN EX2MEM_Type;
63                 EX2MEM_REG_o    : OUT EX2MEM_Type;
64                 -- mem pipeline register
65                 MEM_REG_i       :  IN MEM_REG_Type;
66                 MEM_REG_o       : OUT MEM_REG_Type;
67                 -- decode control i/o
68                 ID2CTRL_i       :  IN ID2CTRL_Type;
69                 INT_CTRL_o      : OUT INT_CTRL_Type;
70                 -- exeq control i/o
71                 EX_WRB_i        :  IN WRB_Type;
72                 EX_WRB_o        : OUT WRB_Type;
73                 -- data hazard i/o
74                 HAZARD_WRB_i    :  IN HAZARD_WRB_Type;
75                 HAZARD_WRB_o    : OUT HAZARD_WRB_Type;
76                 -- for handling the 'IMM' instruction
77                 IMM_LOCK_i      :  IN IMM_LOCK_Type;
78                 IMM_LOCK_o      : OUT IMM_LOCK_Type;
79                 -- for handling the Machine Status Register
80                 MSR_i           :  IN MSR_Type;
81                 MSR_o           : OUT MSR_Type;
82                 -- miscellaneous
83                 MEM2CTRL_i      :  IN MEM2CTRL_Type
84         );
85 END ENTITY core_ctrl;
86
87 --------------------------------------------------------------------------------
88 ARCHITECTURE rtl OF core_ctrl IS
89 --------------------------------------------------------------------------------
90
91         SIGNAL rst_r           : STD_LOGIC;
92         SIGNAL reset_s         : STD_LOGIC;
93         SIGNAL core_clken_s    : STD_LOGIC;
94
95         SIGNAL EX2MEM_REG_r    : EX2MEM_Type;
96         SIGNAL MEM_REG_r       : MEM_REG_Type;
97         SIGNAL ID2EX_REG_r     : ID2EX_Type;
98         SIGNAL EX2IF_REG_r     : EX2IF_Type;
99         SIGNAL IMM_LOCK_r      : IMM_LOCK_Type;
100         SIGNAL HAZARD_WRB_r    : HAZARD_WRB_Type;
101         SIGNAL flush_first_r   : STD_LOGIC;
102         SIGNAL flush_second_r  : STD_LOGIC;
103         SIGNAL flush_second_2r : STD_LOGIC;
104         SIGNAL ignore_state_r  : STD_LOGIC; -- Please note: Flushing first is considered immediate!
105         SIGNAL delayBit_r      : STD_LOGIC;
106         SIGNAL clken_s         : STD_LOGIC;
107         SIGNAL clken_pipe_s    : STD_LOGIC;
108         SIGNAL flush_ID2EX_s   : STD_LOGIC;
109         SIGNAL flush_ID2EX_r   : STD_LOGIC;
110         SIGNAL flush_EX2MEM_s  : STD_LOGIC;
111
112         SIGNAL setup_int_r     : STD_LOGIC;
113         SIGNAL int_busy_r      : STD_LOGIC;
114
115         SIGNAL wait_for_mem_s  : STD_LOGIC;
116         SIGNAL finish_wrb_mem_s: STD_LOGIC;
117
118 BEGIN
119
120         -- static connections
121         reset_s        <= rst_i OR rst_r;
122         pc_ctrl_o      <= NOT rst_r;
123         -- Addressing is 32-bit, so omit two lowest bytes from PC
124         imem_addr_o    <= IF2ID_REG_i.program_counter((IMEM_ABITS_g+1) DOWNTO 2);
125         -- Tracing
126         -- Reset_s is 1 when rst_i is one and then gets deactivated
127         core_clken_s  <= reset_s OR (((NOT trace_i) AND (NOT exeq_halt_i)) OR trace_kick_i);
128         core_clken_o  <= core_clken_s;
129         -- clock/wait control lines
130         finish_wrb_mem_s <= NOT MEM2CTRL_i.bus_wait WHEN (MEM_REG_r.wrb_Action = WRB_MEM AND MEM2CTRL_i.bus_taken = '1') ELSE '0';
131         wait_for_mem_s <= MEM2CTRL_i.bus_wait OR MEM2CTRL_i.bus_taken;
132         clken_s        <= NOT wait_for_mem_s OR rst_i;
133         clken_pipe_s   <= clken_s AND (NOT HAZARD_WRB_i.hazard);
134         imem_clken_o   <= clken_pipe_s;
135
136         gprf_clken_o   <= clken_s OR finish_wrb_mem_s;
137         -- signals for clearing the ID2EX and EX2MEM registers during branches
138         flush_ID2EX_s  <= ((EX2IF_REG_i.take_branch AND (NOT delayBit_r)) OR EX2IF_REG_r.take_branch) WHEN COMPATIBILITY_MODE_g = TRUE
139                                                                                 ELSE ((EX2IF_REG_i.take_branch AND (NOT delayBit_r)) OR EX2IF_REG_r.take_branch OR EX2CTRL_REG_i.flush_first OR
140                                                                                                 flush_first_r OR ((NOT EX2CTRL_REG_i.ignore_state) AND (NOT ignore_state_r) AND flush_second_2r));
141         flush_EX2MEM_s <= HAZARD_WRB_i.hazard;
142         -- outputs that need to be readable too, so needing shadowing signals
143         EX2MEM_REG_o   <= EX2MEM_REG_r;
144         MEM_REG_o      <= MEM_REG_r;
145         ID2EX_REG_o    <= ID2EX_REG_r;
146         delay_bit_o    <= delayBit_r;
147         EX2IF_REG_o    <= EX2IF_REG_r;
148         IMM_LOCK_o     <= IMM_LOCK_r;
149         HAZARD_WRB_o   <= HAZARD_WRB_r;
150         --
151         INT_CTRL_o.setup_int   <= setup_int_r;
152         INT_CTRL_o.rti_target  <= ID2EX_REG_r.program_counter;
153         INT_CTRL_o.int_busy    <= int_busy_r;
154
155 regd_proc:
156         PROCESS
157
158         -- some local procedures
159         PROCEDURE lp_rst_IF2ID_REG IS
160         BEGIN
161                 IF2ID_REG_o.program_counter <= (OTHERS => '0');
162         END PROCEDURE;
163
164         PROCEDURE lp_rst_ID2EX_REG IS
165         BEGIN
166                 -- reset and handle ID2EX_REG_r.program_counter separately,
167                 -- since it will be needed during interrupt setup
168                 ID2EX_REG_r.rdix_rA          <= (OTHERS => '0');
169                 ID2EX_REG_r.rdix_rB          <= (OTHERS => '0');
170                 ID2EX_REG_r.curr_rD          <= (OTHERS => '0');
171                 ID2EX_REG_r.alu_Action       <= A_NOP;
172                 ID2EX_REG_r.alu_Op1          <= ALU_IN_ZERO;
173                 ID2EX_REG_r.alu_Op2          <= ALU_IN_IMM;
174                 ID2EX_REG_r.alu_Cin          <= CIN_ZERO;
175                 ID2EX_REG_r.IMM16            <= (OTHERS => '0');
176                 ID2EX_REG_r.IMM_Lock         <= '0';
177                 ID2EX_REG_r.msr_Action       <= KEEP_CARRY;
178                 ID2EX_REG_r.branch_Action    <= NO_BR;
179                 ID2EX_REG_r.mem_Action       <= NO_MEM;
180                 ID2EX_REG_r.transfer_Size    <= WORD;
181                 ID2EX_REG_r.wrb_Action       <= NO_WRB;
182                 ID2EX_REG_r.condition        <= COND_ALL;
183                 ID2EX_REG_r.halt             <= '0';
184                 IF (COMPATIBILITY_MODE_g = FALSE) THEN
185                         ID2EX_REG_r.it_Action      <= NO_IT;
186                 ELSE
187                 END IF;
188         END PROCEDURE;
189
190         PROCEDURE lp_rst_EX2IF_REG IS
191         BEGIN
192                 EX2IF_REG_r.take_branch   <= '0';
193                 EX2IF_REG_r.branch_target <= (OTHERS => '0');
194         END PROCEDURE;
195
196         PROCEDURE lp_rst_EX2MEM_REG IS
197         BEGIN
198                 EX2MEM_REG_r.mem_Action  <= NO_MEM;
199                 EX2MEM_REG_r.wrb_Action  <= NO_WRB;
200                 EX2MEM_REG_r.exeq_result <= (OTHERS => '0');
201                 EX2MEM_REG_r.data_rD     <= (OTHERS => '0');
202                 EX2MEM_REG_r.byte_Enable <= (OTHERS => '0');
203                 EX2MEM_REG_r.wrix_rD     <= (OTHERS => '0');
204         END PROCEDURE;
205
206         PROCEDURE lp_rst_IMM_LOCK IS
207         BEGIN
208                 IMM_LOCK_r.locked   <= '0';
209                 IMM_LOCK_r.IMM_hi16 <= (OTHERS => '0');
210         END PROCEDURE;
211
212         PROCEDURE lp_rst_MSR IS
213         BEGIN
214                 MSR_o.IE  <= '0';
215                 MSR_o.C   <= '0';
216         END PROCEDURE;
217
218         PROCEDURE lp_rst_EX_WRB IS
219         BEGIN
220                 EX_WRB_o.wrb_Action <= NO_WRB;
221                 EX_WRB_o.wrix_rD    <= (OTHERS => '0');
222                 EX_WRB_o.data_rD    <= (OTHERS => '0');
223         END PROCEDURE;
224
225         PROCEDURE lp_rst_HAZARD_WRB IS
226         BEGIN
227                 HAZARD_WRB_r.hazard  <= '0';
228                 HAZARD_WRB_r.save_rX <= NO_SAVE;
229                 HAZARD_WRB_r.data_rX <= (OTHERS => '0');
230                 HAZARD_WRB_r.data_rD <= (OTHERS => '0');
231         END PROCEDURE;
232
233         PROCEDURE lp_rst_MEM_REG IS
234         BEGIN
235                 MEM_REG_r.wrb_Action  <= NO_WRB;
236                 MEM_REG_r.exeq_result <= (OTHERS => '0');
237                 MEM_REG_r.byte_Enable <= (OTHERS => '0');
238                 MEM_REG_r.wrix_rD     <= (OTHERS => '0');
239         END PROCEDURE;
240
241         BEGIN
242
243         WAIT UNTIL clk_i'event AND clk_i = '1';
244
245         IF (wait_for_mem_s = '0' AND  halt_i = '0' AND core_clken_s = '1') OR rst_i = '1' THEN
246                 rst_r <= rst_i;
247
248                 IF (reset_s = '1') THEN     -- synchronous reset ...
249                         lp_rst_IF2ID_REG;       -- ... so lasts at least one clock_cycle
250                         lp_rst_MSR;
251                         lp_rst_HAZARD_WRB;
252                         lp_rst_MEM_REG;
253                         delayBit_r        <= '0';
254
255                         IF (COMPATIBILITY_MODE_g = FALSE) THEN
256                                 flush_first_r   <= '0';
257                                 flush_second_r  <= '0';
258                                 flush_second_2r <= '0';
259                                 ignore_state_r  <= '0';
260                         END IF;
261
262                         flush_ID2EX_r <= '0';
263                         setup_int_r   <= '0';
264                         int_busy_r    <= '0';
265                         ID2EX_REG_r.program_counter <= (OTHERS => '0');
266                 ELSE
267                         IF (clken_pipe_s = '1') THEN
268                                 IF2ID_REG_o <= IF2ID_REG_i;
269                         END IF;
270                         flush_ID2EX_r <= flush_ID2EX_s;
271                         IF (COMPATIBILITY_MODE_g = FALSE) THEN
272                                 -- Flushing based on IT / ITE / ITT along with IMM locking
273                                 IF (ID2EX_REG_i.IMM_Lock = '1') THEN
274                                         IF (flush_second_2r = '0') THEN
275                                                 flush_second_2r <= flush_second_r;
276                                         END IF;
277                                         flush_first_r   <= EX2CTRL_REG_i.flush_first;
278                                         flush_second_r  <= EX2CTRL_REG_i.flush_second;
279                                         ignore_state_r  <= EX2CTRL_REG_i.ignore_state;
280                                 ELSE
281                                         flush_first_r   <= '0';
282                                         -- Directly to 2r (flushing the following instruction)
283                                         flush_second_2r <= flush_second_r OR EX2CTRL_REG_i.flush_second;
284                                         flush_second_r  <= '0';
285                                         ignore_state_r  <= '0';
286                                 END IF;
287                         END IF;
288                         HAZARD_WRB_r  <= HAZARD_WRB_i;
289                         MEM_REG_r     <= MEM_REG_i;
290                         int_busy_r    <= ID2CTRL_i.int_busy;
291                 END IF;
292                 -- decode-to-exeq unit registers
293                 IF ((reset_s = '1') OR (flush_ID2EX_s = '1')) THEN
294                         setup_int_r <= '0';
295                         lp_rst_ID2EX_REG;
296                         delayBit_r <= '0';
297                 -- check for the need and possibility to handle active interrupt requests
298                 ELSIF (((int_i = '1') OR (MEM2CTRL_i.int = '1')) AND (MSR_i.IE = '1') AND
299                                         (ID2CTRL_i.int_busy = '0') AND (int_busy_r = '0') AND
300                                         -- pending branch should be taken before interrupt can be executed
301                                         -- dectected by Matthis Meier, TU Dortmund (Sept 2012)
302                                         --
303                                         -- Same goes for pending flushing - should be taken before interrupt can be executed
304                                         (EX2IF_REG_i.take_branch = '0') AND (EX2IF_REG_r.take_branch = '0') AND ((COMPATIBILITY_MODE_g = TRUE)
305                                         OR ((flush_first_r = '0') AND (flush_second_r = '0') AND (flush_second_2r = '0'))) AND
306                                         (IMM_LOCK_i.locked = '0') AND (HAZARD_WRB_i.hazard = '0')) THEN
307                         setup_int_r <= '1';
308                         ID2EX_REG_r.program_counter <= ID2EX_REG_i.program_counter;
309                         lp_rst_ID2EX_REG;
310                 ELSIF (clken_pipe_s = '1') THEN
311                         setup_int_r <= '0';
312                         ID2EX_REG_r <= ID2EX_REG_i;
313                         delayBit_r  <= ID2CTRL_i.delayBit;
314                 END IF;
315                 -- exeq-to-mem unit registers
316                 IF ((reset_s = '1') OR (flush_EX2MEM_s = '1')) THEN
317                         lp_rst_EX2IF_REG;
318                         lp_rst_EX2MEM_REG;
319                         lp_rst_EX_WRB;
320                         lp_rst_IMM_LOCK;
321                 ELSE
322                         IF (clken_pipe_s = '1') THEN
323                                 EX2IF_REG_r   <= EX2IF_REG_i;
324                                 EX_WRB_o      <= EX_WRB_i;
325                         END IF;
326                         IF (clken_s = '1') THEN
327                                 -- next test to prevent a flush from disrupting
328                                 -- the write-back pipeline
329                                 IF (flush_ID2EX_r = '0') THEN
330                                         EX2MEM_REG_r <= EX2MEM_REG_i;
331                                 END IF;
332                                 IMM_LOCK_r <= IMM_LOCK_i;
333                                 MSR_o      <= MSR_i;
334                         END IF;
335                 END IF;
336         ELSE
337                 IF finish_wrb_mem_s = '1' THEN
338                         MEM_REG_r.wrb_Action  <= NO_WRB;
339                         MEM_REG_r.wrix_rD     <= (OTHERS => '0');
340                         MEM_REG_r.byte_Enable <= (OTHERS => '0');
341                         IF MEM2CTRL_i.need_keep = '1' THEN
342                                 EX2MEM_REG_r.data_rD <= MEM2CTRL_i.read_data;
343                         END IF;
344                 END IF;
345         END IF;     -- rising edge clk_i ...
346         END PROCESS regd_proc;
347
348 END ARCHITECTURE rtl;