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

Subversion Repositories t48

[/] [t48/] [tags/] [rel_1_0/] [rtl/] [vhdl/] [clock_ctrl.vhd] - Blame information for rev 292

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 arniml
-------------------------------------------------------------------------------
2
--
3
-- The Clock Control unit.
4
-- Clock States and Machine Cycles are generated here.
5
--
6 249 arniml
-- $Id: clock_ctrl.vhd,v 1.12 2006-07-14 01:04:35 arniml Exp $
7 4 arniml
--
8 162 arniml
-- Copyright (c) 2004, 2005, Arnim Laeuger (arniml@opencores.org)
9 4 arniml
--
10
-- All rights reserved
11
--
12
-- Redistribution and use in source and synthezised forms, with or without
13
-- modification, are permitted provided that the following conditions are met:
14
--
15
-- Redistributions of source code must retain the above copyright notice,
16
-- this list of conditions and the following disclaimer.
17
--
18
-- Redistributions in synthesized form must reproduce the above copyright
19
-- notice, this list of conditions and the following disclaimer in the
20
-- documentation and/or other materials provided with the distribution.
21
--
22
-- Neither the name of the author nor the names of other contributors may
23
-- be used to endorse or promote products derived from this software without
24
-- specific prior written permission.
25
--
26
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28
-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29
-- PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE
30
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
-- POSSIBILITY OF SUCH DAMAGE.
37
--
38
-- Please report bugs to the author, but before you do so, please
39
-- make sure that this is not a derivative work and that
40
-- you have the latest version of this file.
41
--
42
-- The latest version of this file can be found at:
43
--      http://www.opencores.org/cvsweb.shtml/t48/
44
--
45
-------------------------------------------------------------------------------
46
 
47
library ieee;
48
use ieee.std_logic_1164.all;
49
 
50
use work.t48_pack.all;
51
 
52 179 arniml
entity t48_clock_ctrl is
53 4 arniml
 
54
  generic (
55
    -- divide XTAL1 by 3 to derive Clock States
56
    xtal_div_3_g : integer := 1
57
  );
58
 
59
  port (
60
    clk_i          : in  std_logic;
61
    xtal_i         : in  std_logic;
62 219 arniml
    xtal_en_i      : in  boolean;
63 4 arniml
    res_i          : in  std_logic;
64
    en_clk_i       : in  boolean;
65
    xtal3_o        : out boolean;
66 162 arniml
    t0_o           : out std_logic;
67 4 arniml
    multi_cycle_i  : in  boolean;
68
    assert_psen_i  : in  boolean;
69
    assert_prog_i  : in  boolean;
70
    assert_rd_i    : in  boolean;
71
    assert_wr_i    : in  boolean;
72
    mstate_o       : out mstate_t;
73
    second_cycle_o : out boolean;
74
    ale_o          : out boolean;
75
    psen_o         : out boolean;
76
    prog_o         : out boolean;
77
    rd_o           : out boolean;
78
    wr_o           : out boolean
79
  );
80
 
81 179 arniml
end t48_clock_ctrl;
82 4 arniml
 
83
 
84
library ieee;
85 77 arniml
use ieee.numeric_std.all;
86 4 arniml
 
87 179 arniml
architecture rtl of t48_clock_ctrl is
88 4 arniml
 
89
  -- The three XTAL1 cycles.
90
  signal xtal_q  : unsigned(1 downto 0);
91
  signal xtal1_s,
92
         xtal2_s,
93
         xtal3_s : boolean;
94
  signal x1_s,
95
         x2_s,
96
         x3_s    : std_logic;
97
 
98 162 arniml
  signal t0_q    : std_logic;
99 4 arniml
 
100 162 arniml
 
101 4 arniml
  -- The five clock states.
102
  signal mstate_q  : mstate_t;
103
 
104
  signal ale_q     : boolean;
105
  signal psen_q    : boolean;
106
  signal prog_q    : boolean;
107
  signal rd_q      : boolean;
108
  signal wr_q      : boolean;
109
 
110
 
111
  -- The Machine Cycle marker.
112
  signal second_cycle_q : boolean;
113
  signal multi_cycle_q  : boolean;
114
 
115
begin
116
 
117
  -----------------------------------------------------------------------------
118
  -- Verify the generics
119
  -----------------------------------------------------------------------------
120
 
121
  -- pragma translate_off
122
 
123
  -- XTAL1 divide by 3 --------------------------------------------------------
124
  assert (xtal_div_3_g = 1) or (xtal_div_3_g = 0)
125
    report "xtal_div_3_g must be either 1 or 0!"
126
    severity failure;
127
 
128
  -- pragma translate_on
129
 
130
 
131
  -----------------------------------------------------------------------------
132
  -- Divide XTAL1 by 3 to derive Clock States.
133
  -----------------------------------------------------------------------------
134
  use_xtal_div: if xtal_div_3_g = 1 generate
135
    xtal: process (res_i, xtal_i)
136
    begin
137
      if res_i = res_active_c then
138 77 arniml
        xtal_q <= TO_UNSIGNED(0, 2);
139 162 arniml
        t0_q   <= '0';
140 4 arniml
 
141
      elsif xtal_i'event and xtal_i = clk_active_c then
142 219 arniml
        if xtal_en_i then
143
          if xtal_q < 2 then
144
            xtal_q <= xtal_q + 1;
145
          else
146
            xtal_q <= TO_UNSIGNED(0, 2);
147
          end if;
148 4 arniml
 
149 219 arniml
          if xtal3_s then
150
            t0_q <= '1';
151
          else
152
            t0_q <= '0';
153
          end if;
154
 
155 162 arniml
        end if;
156
 
157 4 arniml
      end if;
158
    end process xtal;
159
 
160
    x1_s <=   '1'
161 219 arniml
            when xtal_q = 0 and xtal_en_i else
162 4 arniml
              '0';
163
    x2_s <=   '1'
164 219 arniml
            when xtal_q = 1 and xtal_en_i else
165 4 arniml
              '0';
166
    x3_s <=   '1'
167 219 arniml
            when xtal_q = 2 and xtal_en_i else
168 4 arniml
              '0';
169 162 arniml
    t0_o <= t0_q;
170 4 arniml
 
171
  end generate;
172
 
173
  -----------------------------------------------------------------------------
174
  -- XTAL1 is used directly for Clock States.
175
  -----------------------------------------------------------------------------
176
  no_xtal_div: if xtal_div_3_g = 0 generate
177 77 arniml
    xtal_q <= TO_UNSIGNED(0, 2);
178 4 arniml
 
179 219 arniml
    x1_s <=   '1'
180
            when xtal_en_i else
181
              '0';
182
    x2_s <=   '1'
183
            when xtal_en_i else
184
              '0';
185 203 arniml
    x3_s <=   '1'
186 219 arniml
            when xtal_en_i else
187 203 arniml
              '0';
188 162 arniml
    t0_o <= xtal_i;
189 4 arniml
 
190
  end generate;
191
 
192
  -- And finally the boolean flags --------------------------------------------
193
  xtal1_s <= to_boolean(x1_s);
194
  xtal2_s <= to_boolean(x2_s);
195
  xtal3_s <= to_boolean(x3_s);
196
 
197
 
198
  -----------------------------------------------------------------------------
199
  -- Process external_signal
200
  --
201
  -- Purpose:
202
  --   Control signals ALE, PSEN, PROG and RD/WR are generated here.
203
  --
204
  external_signals: process (res_i, xtal_i)
205
  begin
206
    if res_i = res_active_c then
207
      ale_q    <= false;
208
      psen_q   <= false;
209
      prog_q   <= false;
210
      rd_q     <= false;
211
      wr_q     <= false;
212
 
213
    elsif xtal_i'event and xtal_i = clk_active_c then
214
 
215
      case mstate_q is
216
        when MSTATE5 =>
217
          -- RD, WR are set at the end of XTAL2 of first machine cycle
218
          if xtal2_s and not second_cycle_q then
219
            if assert_rd_i then
220
              rd_q <= true;
221
            end if;
222
            if assert_wr_i then
223
              wr_q <= true;
224
            end if;
225
          end if;
226
 
227
        when MSTATE1 =>
228 203 arniml
          if xtal3_s then
229 4 arniml
             psen_q   <= false;
230
           end if;
231
 
232 145 arniml
        when MSTATE2 =>
233 203 arniml
          if xtal3_s then
234 4 arniml
            -- RD, WR are removed at the end of XTAL3 of second machine cycle
235
            rd_q     <= false;
236
            wr_q     <= false;
237 249 arniml
            -- so is PROG
238
            prog_q   <= false;
239 4 arniml
          end if;
240
 
241
        when MSTATE3 =>
242 203 arniml
          -- ALE is set at the end of XTAL3 of every machine cycle
243
          if xtal3_s then
244 4 arniml
            ale_q    <= true;
245
          end if;
246
 
247
        when MSTATE4 =>
248 203 arniml
          if xtal3_s then
249 4 arniml
            -- PSEN is set at the end of XTAL3
250
            if assert_psen_i then
251
              psen_q <= true;
252
            end if;
253
 
254 20 arniml
          end if;
255 4 arniml
 
256 203 arniml
          -- PROG is set at the end of XTAL3
257
          if xtal3_s and
258
             multi_cycle_q and not second_cycle_q and assert_prog_i then
259 20 arniml
            prog_q <= true;
260 4 arniml
          end if;
261
 
262
          -- ALE is removed at the end of XTAL2 of every machine cycle
263
          if xtal2_s then
264
            ale_q    <= false;
265
          end if;
266
 
267
      when others =>
268
        -- recover when states are out of sync
269
        ale_q    <= false;
270
        psen_q   <= false;
271
        prog_q   <= false;
272
        rd_q     <= false;
273
        wr_q     <= false;
274
 
275
      end case;
276
 
277
    end if;
278
 
279
  end process external_signals;
280
  --
281
  -----------------------------------------------------------------------------
282
 
283
 
284
  -----------------------------------------------------------------------------
285
  -- Process states
286
  --
287
  -- Purpose:
288
  --   The Clock State controller.
289
  --
290
  states: process (res_i, clk_i)
291
  begin
292
    if res_i = res_active_c then
293 63 arniml
      -- Reset machine state to MSTATE3
294
      -- This allows a proper instruction fetch for the first real instruction
295
      -- after reset.
296
      -- The MSTATE3 is part of a virtual NOP that has no MSTATE1 and MSTATE2.
297
      mstate_q <= MSTATE3;
298 4 arniml
 
299
    elsif clk_i'event and clk_i = clk_active_c then
300
      if en_clk_i then
301
 
302
        case mstate_q is
303
          when MSTATE5 =>
304
            mstate_q <= MSTATE1;
305
 
306
          when MSTATE1 =>
307
            mstate_q <= MSTATE2;
308
 
309
          when MSTATE2 =>
310
            mstate_q <= MSTATE3;
311
 
312
          when MSTATE3 =>
313
            mstate_q <= MSTATE4;
314
 
315
          when MSTATE4 =>
316
            mstate_q <= MSTATE5;
317
 
318
          when others =>
319
            -- recover when states are out of sync
320
            mstate_q <= MSTATE1;
321
 
322
            -- pragma translate_off
323
            assert false
324
              report "Encoding of Clock States failed!"
325
              severity error;
326
            -- pragma translate_on
327
 
328
        end case;
329
 
330
      end if;
331
 
332
    end if;
333
 
334
  end process states;
335
  --
336
  -----------------------------------------------------------------------------
337
 
338
 
339
  -----------------------------------------------------------------------------
340
  -- Process machine_cycle
341
  --
342
  -- Purpose:
343
  --   Keep track of machine cycles.
344
  --   Basically, this means to differ between first and second cycle.
345
  --
346
  machine_cycle: process (res_i, clk_i)
347
    variable state2_v, state5_v : boolean;
348
  begin
349
    if res_i = res_active_c then
350
      multi_cycle_q  <= false;
351
      second_cycle_q <= false;
352
 
353
    elsif clk_i'event and clk_i = clk_active_c then
354
      if en_clk_i then
355
 
356
        state2_v := mstate_q = MSTATE2;
357
        state5_v := mstate_q = MSTATE5;
358
 
359
        -- multi cycle information is delivered in State 2 from the decoder
360
        if state2_v and multi_cycle_i then
361
          multi_cycle_q <= true;
362
        end if;
363
 
364
        -- mark second machine cycle
365
        if multi_cycle_q and state5_v then
366
          second_cycle_q <= true;
367
        end if;
368
 
369
        -- reset at end of second machine cycle
370
        if state5_v and
371 63 arniml
           (multi_cycle_q and second_cycle_q) then
372 4 arniml
          multi_cycle_q  <= false;
373
          second_cycle_q <= false;
374
        end if;
375
 
376
      end if;
377
 
378
    end if;
379
 
380
  end process machine_cycle;
381
  --
382
  -----------------------------------------------------------------------------
383
 
384
 
385
  -----------------------------------------------------------------------------
386
  -- Output assignments
387
  -----------------------------------------------------------------------------
388
  xtal3_o        <= xtal3_s;
389
  mstate_o       <= mstate_q;
390
  second_cycle_o <= second_cycle_q;
391
  ale_o          <= ale_q;
392
  psen_o         <= psen_q;
393
  prog_o         <= prog_q;
394
  rd_o           <= rd_q;
395
  wr_o           <= wr_q;
396
 
397
end rtl;
398
 
399
 
400
-------------------------------------------------------------------------------
401
-- File History:
402
--
403
-- $Log: not supported by cvs2svn $
404 249 arniml
-- Revision 1.11  2006/06/20 00:46:38  arniml
405
-- new input xtal_en_i gates xtal_i base clock
406
--
407 219 arniml
-- Revision 1.10  2005/11/01 21:24:21  arniml
408
-- * shift assertion of ALE and PROG to xtal3
409
-- * correct change of revision 1.8
410
--
411 203 arniml
-- Revision 1.9  2005/06/11 10:08:43  arniml
412
-- introduce prefix 't48_' for all packages, entities and configurations
413
--
414 179 arniml
-- Revision 1.8  2005/06/09 22:15:10  arniml
415
-- Use en_clk_i instead of xtal3_s for generation of external signals.
416
-- This is required when the core runs with full xtal clock instead
417
-- of xtal/3 (xtal_div_3_g = 0).
418
--
419 176 arniml
-- Revision 1.7  2005/05/04 20:12:36  arniml
420
-- Fix bug report:
421
-- "Wrong clock applied to T0"
422
-- t0_o is generated inside clock_ctrl with a separate flip-flop running
423
-- with xtal_i
424
--
425 162 arniml
-- Revision 1.6  2004/10/25 20:31:12  arniml
426
-- remove PROG and end of XTAL2, see comment for details
427
--
428 145 arniml
-- Revision 1.5  2004/10/25 19:35:41  arniml
429
-- deassert rd_q, wr_q and prog_q at end of XTAL3
430
--
431 142 arniml
-- Revision 1.4  2004/04/24 23:44:25  arniml
432
-- move from std_logic_arith to numeric_std
433
--
434 77 arniml
-- Revision 1.3  2004/04/18 18:56:23  arniml
435
-- reset machine state to MSTATE3 to allow proper instruction fetch
436
-- after reset
437
--
438 63 arniml
-- Revision 1.2  2004/03/28 12:55:06  arniml
439
-- move code for PROG out of if-branch for xtal3_s
440
--
441 20 arniml
-- Revision 1.1  2004/03/23 21:31:52  arniml
442
-- initial check-in
443 4 arniml
--
444
-------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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