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