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