]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/tumbl.git/blob - hw/core_ctrl.vhd
When branching, do not execute the instruction behind the branch
[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, 2012: interrupt handling corrected to let
12 --                                 a pending branch be taken first
13 --                                 (with thanks to Matthis Meier, TU Dortmund,
14 --                                  for detecting this errror).
15 --  Remarks:
16 --
17 --------------------------------------------------------------------------------
18
19 LIBRARY IEEE;
20
21 USE IEEE.std_logic_1164.all;
22 USE WORK.mbl_Pkg.all;
23
24
25 --------------------------------------------------------------------------------
26 ENTITY core_ctrl IS
27 --------------------------------------------------------------------------------
28     PORT (
29         clk_i           :  IN STD_LOGIC;
30         rst_i           :  IN STD_LOGIC;
31         halt_i          :  IN STD_LOGIC;
32         int_i           :  IN STD_LOGIC;
33         trace_i         :  IN STD_LOGIC;
34         trace_kick_i    :  IN STD_LOGIC;
35         core_clk_en_o   : OUT STD_LOGIC;
36         -- specific fetch i/o
37         imem_addr_o     : OUT STD_LOGIC_VECTOR (31 DOWNTO 0);
38         imem_clken_o    : OUT STD_LOGIC;
39         pc_ctrl_o       : OUT STD_LOGIC;
40         -- fetch to decode pipeline registers
41         IF2ID_REG_i     :  IN IF2ID_Type;
42         IF2ID_REG_o     : OUT IF2ID_Type;
43         -- decode to exeq pipeline registers
44         ID2EX_REG_i     :  IN ID2EX_Type;
45         ID2EX_REG_o     : OUT ID2EX_Type;
46         -- GPRF control
47         gprf_clken_o    : OUT STD_LOGIC;
48         -- exeq to fetch feedback registers
49         EX2IF_REG_i     :  IN EX2IF_Type;
50         EX2IF_REG_o     : OUT EX2IF_Type;
51         -- exeq to mem pipeline registers
52         EX2MEM_REG_i    :  IN EX2MEM_Type;
53         EX2MEM_REG_o    : OUT EX2MEM_Type;
54         -- mem pipeline register
55         MEM_REG_i       :  IN MEM_REG_Type;
56         MEM_REG_o       : OUT MEM_REG_Type;
57         -- decode control i/o
58         ID2CTRL_i       :  IN ID2CTRL_Type;
59         INT_CTRL_o      : OUT INT_CTRL_Type;
60         -- exeq control i/o
61         EX_WRB_i        :  IN WRB_Type;
62         EX_WRB_o        : OUT WRB_Type;
63         -- data hazard i/o
64         HAZARD_WRB_i    :  IN HAZARD_WRB_Type;
65         HAZARD_WRB_o    : OUT HAZARD_WRB_Type;
66         -- for handling the 'IMM' instruction
67         IMM_LOCK_i      :  IN IMM_LOCK_Type;
68         IMM_LOCK_o      : OUT IMM_LOCK_Type;
69         -- for handling the Machine Status Register
70         MSR_i           :  IN MSR_Type;
71         MSR_o           : OUT MSR_Type;
72         -- miscellaneous
73         MEM2CTRL_i      :  IN MEM2CTRL_Type;
74         done_o          : OUT STD_LOGIC
75         );
76 END ENTITY core_ctrl;
77
78 --------------------------------------------------------------------------------
79 ARCHITECTURE rtl OF core_ctrl IS
80 --------------------------------------------------------------------------------
81
82     SIGNAL rst_r          : STD_LOGIC;
83     SIGNAL reset_s        : STD_LOGIC;
84     SIGNAL core_clk_en_s  : STD_LOGIC;
85
86     SIGNAL ID2EX_REG_r    : ID2EX_Type;
87     SIGNAL EX2IF_REG_r    : EX2IF_Type;
88     SIGNAL delayBit_r     : STD_LOGIC;
89     SIGNAL delayBit_2r    : STD_LOGIC;
90     SIGNAL IMM_LOCK_r     : IMM_LOCK_Type;
91     SIGNAL HAZARD_WRB_r   : HAZARD_WRB_Type;
92
93     SIGNAL clken_s        : STD_LOGIC;
94     SIGNAL clken_pipe_s   : STD_LOGIC;
95     SIGNAL flush_ID2EX_s  : STD_LOGIC;
96     SIGNAL flush_ID2EX_r  : STD_LOGIC;
97     SIGNAL flush_EX2MEM_s : STD_LOGIC;
98
99     SIGNAL setup_int_r    : STD_LOGIC;
100     SIGNAL int_busy_r     : STD_LOGIC;
101
102
103 BEGIN
104
105     -- static connections
106     reset_s        <= rst_i OR rst_r;
107     pc_ctrl_o      <= NOT rst_r;
108     imem_addr_o    <= IF2ID_REG_i.program_counter;
109     -- Tracing
110     -- Reset_s is 1 when rst_i is one and then gets deactivated
111     core_clk_en_s <= reset_s OR (NOT trace_i) OR trace_kick_i;
112     core_clk_en_o <= core_clk_en_s;
113     -- clock/wait control lines
114     clken_s        <= MEM2CTRL_i.clken OR rst_i;
115     clken_pipe_s   <= clken_s AND (NOT HAZARD_WRB_i.hazard);
116     imem_clken_o   <= clken_pipe_s;
117     gprf_clken_o   <= clken_s;
118     -- signals for clearing the ID2EX and EX2MEM registers during branches
119     flush_ID2EX_s  <= EX2IF_REG_r.take_branch;
120     flush_EX2MEM_s <= (flush_ID2EX_s AND (NOT delayBit_2r)) OR HAZARD_WRB_i.hazard;
121     -- outputs that need to be readable too, so needing shadowing signals
122     ID2EX_REG_o    <= ID2EX_REG_r;
123     EX2IF_REG_o    <= EX2IF_REG_r;
124     IMM_LOCK_o     <= IMM_LOCK_r;
125     HAZARD_WRB_o   <= HAZARD_WRB_r;
126     --
127     INT_CTRL_o.setup_int   <= setup_int_r;
128     INT_CTRL_o.rti_target  <= ID2EX_REG_r.program_counter;
129     INT_CTRL_o.int_busy    <= int_busy_r;
130
131 regd_proc:
132     PROCESS ( clk_i, rst_i, halt_i, core_clk_en_s,
133               -- complete sensitivity list for synthesizer
134               reset_s, MEM2CTRL_i, clken_pipe_s, IF2ID_REG_i,
135               flush_ID2EX_s, flush_EX2MEM_s, HAZARD_WRB_i,
136               MEM_REG_i, ID2CTRL_i, int_i, MSR_i,
137               int_busy_r, delayBit_r, IMM_LOCK_i, ID2EX_REG_i, ID2EX_REG_r,
138               EX2IF_REG_i, EX_WRB_i, EX2MEM_REG_i )
139
140         -- some local procedures
141         PROCEDURE lp_rst_IF2ID_REG IS
142         BEGIN
143             IF2ID_REG_o.program_counter <= (OTHERS => '0');
144         END PROCEDURE;
145
146         PROCEDURE lp_rst_ID2EX_REG IS
147         BEGIN
148             -- reset and handle ID2EX_REG_r.program_counter separately,
149             -- since it will be needed during interrupt setup
150             ID2EX_REG_r.rdix_rA          <= (OTHERS => '0');
151             ID2EX_REG_r.rdix_rB          <= (OTHERS => '0');
152             ID2EX_REG_r.curr_rD          <= (OTHERS => '0');
153             ID2EX_REG_r.alu_Action       <= A_NOP;
154             ID2EX_REG_r.alu_Op1          <= ALU_IN_ZERO;
155             ID2EX_REG_r.alu_Op2          <= ALU_IN_IMM;
156             ID2EX_REG_r.alu_Cin          <= CIN_ZERO;
157             ID2EX_REG_r.IMM16            <= (OTHERS => '0');
158             ID2EX_REG_r.IMM_Lock         <= '0';
159             ID2EX_REG_r.msr_Action       <= KEEP_CARRY;
160             ID2EX_REG_r.branch_Action    <= NO_BR;
161             ID2EX_REG_r.mem_Action       <= NO_MEM;
162             ID2EX_REG_r.transfer_Size    <= WORD;
163             ID2EX_REG_r.wrb_Action       <= NO_WRB;
164         END PROCEDURE;
165
166         PROCEDURE lp_rst_EX2IF_REG IS
167         BEGIN
168             EX2IF_REG_r.take_branch   <= '0';
169             EX2IF_REG_r.branch_target <= (OTHERS => '0');
170         END PROCEDURE;
171
172         PROCEDURE lp_rst_EX2MEM_REG IS
173         BEGIN
174             EX2MEM_REG_o.mem_Action  <= NO_MEM;
175             EX2MEM_REG_o.wrb_Action  <= NO_WRB;
176             EX2MEM_REG_o.exeq_result <= (OTHERS => '0');
177             EX2MEM_REG_o.data_rD     <= (OTHERS => '0');
178             EX2MEM_REG_o.byte_Enable <= (OTHERS => '0');
179             EX2MEM_REG_o.wrix_rD     <= (OTHERS => '0');
180         END PROCEDURE;
181
182         PROCEDURE lp_rst_IMM_LOCK IS
183         BEGIN
184             IMM_LOCK_r.locked   <= '0';
185             IMM_LOCK_r.IMM_hi16 <= (OTHERS => '0');
186         END PROCEDURE;
187
188         PROCEDURE lp_rst_MSR IS
189         BEGIN
190             MSR_o.IE  <= '0';
191             MSR_o.C   <= '0';
192         END PROCEDURE;
193
194         PROCEDURE lp_rst_EX_WRB IS
195         BEGIN
196             EX_WRB_o.wrb_Action <= NO_WRB;
197             EX_WRB_o.wrix_rD    <= (OTHERS => '0');
198             EX_WRB_o.data_rD    <= (OTHERS => '0');
199         END PROCEDURE;
200
201         PROCEDURE lp_rst_HAZARD_WRB IS
202         BEGIN
203             HAZARD_WRB_r.hazard  <= '0';
204             HAZARD_WRB_r.save_rX <= NO_SAVE;
205             HAZARD_WRB_r.data_rX <= (OTHERS => '0');
206             HAZARD_WRB_r.data_rD <= (OTHERS => '0');
207         END PROCEDURE;
208
209         PROCEDURE lp_rst_MEM_REG IS
210         BEGIN
211             MEM_REG_o.wrb_Action  <= NO_WRB;
212             MEM_REG_o.exeq_result <= (OTHERS => '0');
213             MEM_REG_o.byte_Enable <= (OTHERS => '0');
214             MEM_REG_o.wrix_rD     <= (OTHERS => '0');
215         END PROCEDURE;
216
217     BEGIN
218
219         IF (RISING_EDGE (clk_i) AND (MEM2CTRL_i.clken = '1')) AND halt_i = '0' AND
220                                             core_clk_en_s = '1' THEN
221             rst_r <= rst_i;
222             IF (reset_s = '1') THEN     -- synchronous reset ...
223                 lp_rst_IF2ID_REG;       -- ... so lasts at least one clock_cycle
224                 lp_rst_MSR;
225                 lp_rst_HAZARD_WRB;
226                 lp_rst_MEM_REG;
227                 delayBit_r    <= '0';
228                 flush_ID2EX_r <= '0';
229                 setup_int_r   <= '0';
230                 int_busy_r    <= '0';
231                 done_o        <= '0';
232                 ID2EX_REG_r.program_counter <= (OTHERS => '0');
233             ELSE
234                 IF (clken_pipe_s = '1') THEN
235                     IF2ID_REG_o <= IF2ID_REG_i;
236                 END IF;
237                 flush_ID2EX_r <= flush_ID2EX_s;
238                 HAZARD_WRB_r  <= HAZARD_WRB_i;
239                 MEM_REG_o     <= MEM_REG_i;
240                 int_busy_r    <= ID2CTRL_i.int_busy;
241             END IF;
242             -- decode-to-exeq unit registers
243             IF ((reset_s = '1') OR (flush_ID2EX_s = '1')) THEN
244                 lp_rst_ID2EX_REG;
245                 delayBit_r <= '0';
246             -- check for the need and possibility to handle active interrupt requests
247             ELSIF (((int_i = '1') OR (MEM2CTRL_i.int = '1')) AND (MSR_i.IE = '1') AND
248                         (ID2CTRL_i.int_busy = '0') AND (int_busy_r = '0') AND
249                 -- pending branch should be taken before interrupt can be executed
250                 -- dectected by Matthis Meier, TU Dortmund (Sept 2012)
251                             (EX2IF_REG_i.take_branch = '0') AND
252                                 (delayBit_r = '0') AND
253                                     (IMM_LOCK_i.locked = '0') AND
254                                         (HAZARD_WRB_i.hazard = '0')) THEN
255                 setup_int_r <= '1';
256                 ID2EX_REG_r.program_counter <= ID2EX_REG_i.program_counter;
257                 lp_rst_ID2EX_REG;
258             ELSIF (clken_pipe_s = '1') THEN
259                 setup_int_r <= '0';
260                 if (EX2IF_REG_i.take_branch = '1') THEN
261                     lp_rst_ID2EX_REG;
262                 ELSE
263                     ID2EX_REG_r <= ID2EX_REG_i;
264                 END IF;
265                 delayBit_r  <= ID2CTRL_i.delayBit;
266             END IF;
267             -- exeq-to-mem unit registers
268             IF ((reset_s = '1') OR (flush_EX2MEM_s = '1')) THEN
269                 lp_rst_EX2IF_REG;
270                 lp_rst_EX2MEM_REG;
271                 lp_rst_EX_WRB;
272                 lp_rst_IMM_LOCK;
273                 delayBit_2r <= '0';
274             ELSE
275                 IF (clken_pipe_s = '1') THEN
276                     EX2IF_REG_r <= EX2IF_REG_i;
277                     delayBit_2r <= delayBit_r;
278                     EX_WRB_o    <= EX_WRB_i;
279                 END IF;
280                 IF (clken_s = '1') THEN
281                     -- next test to prevent a flush from disrupting
282                     -- the write-back pipeline
283                     IF (flush_ID2EX_r = '0') THEN
284                         EX2MEM_REG_o <= EX2MEM_REG_i;
285                     END IF;
286                     IMM_LOCK_r <= IMM_LOCK_i;
287                     MSR_o      <= MSR_i;
288                 END IF;
289             END IF;
290             -- check on End-Of-Program viz. "bri 0x00"
291             -- use delayBit to distinguish between "bri" and "rtsd/rtid"
292             IF ((ID2EX_REG_r.branch_Action = BR) AND
293                     (ID2EX_REG_r.alu_Op2 = ALU_IN_IMM) AND
294                         (ID2EX_REG_r.IMM16 = C_16_ZEROS) AND
295                             (delayBit_r = '0') AND (flush_EX2MEM_s = '0')) THEN
296                 done_o <= '1';
297             END IF;
298         END IF;     -- rising edge clk_i ...
299     END PROCESS regd_proc;
300
301 END ARCHITECTURE rtl;