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 219

Go to most recent revision | 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 219 arniml
-- $Id: clock_ctrl.vhd,v 1.11 2006-06-20 00:46:38 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
          if xtal2_s then
234
            -- PROG is removed at the end of XTAL2 of second machine cycle
235
            -- according to the user manual, PROG should be removed at the
236
            -- end of XTAL3 but this would raise the need to change P2 at
237
            -- XTAL1 or XTAL2 -> introduction of inter-xtal timing in
238
            -- the rest of the core.
239
            prog_q   <= false;
240
          end if;
241 203 arniml
          if xtal3_s then
242 4 arniml
            -- RD, WR are removed at the end of XTAL3 of second machine cycle
243
            rd_q     <= false;
244
            wr_q     <= false;
245
          end if;
246
 
247
        when MSTATE3 =>
248 203 arniml
          -- ALE is set at the end of XTAL3 of every machine cycle
249
          if xtal3_s then
250 4 arniml
            ale_q    <= true;
251
          end if;
252
 
253
        when MSTATE4 =>
254 203 arniml
          if xtal3_s then
255 4 arniml
            -- PSEN is set at the end of XTAL3
256
            if assert_psen_i then
257
              psen_q <= true;
258
            end if;
259
 
260 20 arniml
          end if;
261 4 arniml
 
262 203 arniml
          -- PROG is set at the end of XTAL3
263
          if xtal3_s and
264
             multi_cycle_q and not second_cycle_q and assert_prog_i then
265 20 arniml
            prog_q <= true;
266 4 arniml
          end if;
267
 
268
          -- ALE is removed at the end of XTAL2 of every machine cycle
269
          if xtal2_s then
270
            ale_q    <= false;
271
          end if;
272
 
273
      when others =>
274
        -- recover when states are out of sync
275
        ale_q    <= false;
276
        psen_q   <= false;
277
        prog_q   <= false;
278
        rd_q     <= false;
279
        wr_q     <= false;
280
 
281
      end case;
282
 
283
    end if;
284
 
285
  end process external_signals;
286
  --
287
  -----------------------------------------------------------------------------
288
 
289
 
290
  -----------------------------------------------------------------------------
291
  -- Process states
292
  --
293
  -- Purpose:
294
  --   The Clock State controller.
295
  --
296
  states: process (res_i, clk_i)
297
  begin
298
    if res_i = res_active_c then
299 63 arniml
      -- Reset machine state to MSTATE3
300
      -- This allows a proper instruction fetch for the first real instruction
301
      -- after reset.
302
      -- The MSTATE3 is part of a virtual NOP that has no MSTATE1 and MSTATE2.
303
      mstate_q <= MSTATE3;
304 4 arniml
 
305
    elsif clk_i'event and clk_i = clk_active_c then
306
      if en_clk_i then
307
 
308
        case mstate_q is
309
          when MSTATE5 =>
310
            mstate_q <= MSTATE1;
311
 
312
          when MSTATE1 =>
313
            mstate_q <= MSTATE2;
314
 
315
          when MSTATE2 =>
316
            mstate_q <= MSTATE3;
317
 
318
          when MSTATE3 =>
319
            mstate_q <= MSTATE4;
320
 
321
          when MSTATE4 =>
322
            mstate_q <= MSTATE5;
323
 
324
          when others =>
325
            -- recover when states are out of sync
326
            mstate_q <= MSTATE1;
327
 
328
            -- pragma translate_off
329
            assert false
330
              report "Encoding of Clock States failed!"
331
              severity error;
332
            -- pragma translate_on
333
 
334
        end case;
335
 
336
      end if;
337
 
338
    end if;
339
 
340
  end process states;
341
  --
342
  -----------------------------------------------------------------------------
343
 
344
 
345
  -----------------------------------------------------------------------------
346
  -- Process machine_cycle
347
  --
348
  -- Purpose:
349
  --   Keep track of machine cycles.
350
  --   Basically, this means to differ between first and second cycle.
351
  --
352
  machine_cycle: process (res_i, clk_i)
353
    variable state2_v, state5_v : boolean;
354
  begin
355
    if res_i = res_active_c then
356
      multi_cycle_q  <= false;
357
      second_cycle_q <= false;
358
 
359
    elsif clk_i'event and clk_i = clk_active_c then
360
      if en_clk_i then
361
 
362
        state2_v := mstate_q = MSTATE2;
363
        state5_v := mstate_q = MSTATE5;
364
 
365
        -- multi cycle information is delivered in State 2 from the decoder
366
        if state2_v and multi_cycle_i then
367
          multi_cycle_q <= true;
368
        end if;
369
 
370
        -- mark second machine cycle
371
        if multi_cycle_q and state5_v then
372
          second_cycle_q <= true;
373
        end if;
374
 
375
        -- reset at end of second machine cycle
376
        if state5_v and
377 63 arniml
           (multi_cycle_q and second_cycle_q) then
378 4 arniml
          multi_cycle_q  <= false;
379
          second_cycle_q <= false;
380
        end if;
381
 
382
      end if;
383
 
384
    end if;
385
 
386
  end process machine_cycle;
387
  --
388
  -----------------------------------------------------------------------------
389
 
390
 
391
  -----------------------------------------------------------------------------
392
  -- Output assignments
393
  -----------------------------------------------------------------------------
394
  xtal3_o        <= xtal3_s;
395
  mstate_o       <= mstate_q;
396
  second_cycle_o <= second_cycle_q;
397
  ale_o          <= ale_q;
398
  psen_o         <= psen_q;
399
  prog_o         <= prog_q;
400
  rd_o           <= rd_q;
401
  wr_o           <= wr_q;
402
 
403
end rtl;
404
 
405
 
406
-------------------------------------------------------------------------------
407
-- File History:
408
--
409
-- $Log: not supported by cvs2svn $
410 219 arniml
-- Revision 1.10  2005/11/01 21:24:21  arniml
411
-- * shift assertion of ALE and PROG to xtal3
412
-- * correct change of revision 1.8
413
--
414 203 arniml
-- Revision 1.9  2005/06/11 10:08:43  arniml
415
-- introduce prefix 't48_' for all packages, entities and configurations
416
--
417 179 arniml
-- Revision 1.8  2005/06/09 22:15:10  arniml
418
-- Use en_clk_i instead of xtal3_s for generation of external signals.
419
-- This is required when the core runs with full xtal clock instead
420
-- of xtal/3 (xtal_div_3_g = 0).
421
--
422 176 arniml
-- Revision 1.7  2005/05/04 20:12:36  arniml
423
-- Fix bug report:
424
-- "Wrong clock applied to T0"
425
-- t0_o is generated inside clock_ctrl with a separate flip-flop running
426
-- with xtal_i
427
--
428 162 arniml
-- Revision 1.6  2004/10/25 20:31:12  arniml
429
-- remove PROG and end of XTAL2, see comment for details
430
--
431 145 arniml
-- Revision 1.5  2004/10/25 19:35:41  arniml
432
-- deassert rd_q, wr_q and prog_q at end of XTAL3
433
--
434 142 arniml
-- Revision 1.4  2004/04/24 23:44:25  arniml
435
-- move from std_logic_arith to numeric_std
436
--
437 77 arniml
-- Revision 1.3  2004/04/18 18:56:23  arniml
438
-- reset machine state to MSTATE3 to allow proper instruction fetch
439
-- after reset
440
--
441 63 arniml
-- Revision 1.2  2004/03/28 12:55:06  arniml
442
-- move code for PROG out of if-branch for xtal3_s
443
--
444 20 arniml
-- Revision 1.1  2004/03/23 21:31:52  arniml
445
-- initial check-in
446 4 arniml
--
447
-------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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