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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [VHDL/] [o8_rtc.vhd] - Blame information for rev 223

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 191 jshamlet
-- Copyright (c)2020 Jeremy Seth Henry
2
-- All rights reserved.
3
--
4
-- Redistribution and use in source and binary forms, with or without
5
-- modification, are permitted provided that the following conditions are met:
6
--     * Redistributions of source code must retain the above copyright
7
--       notice, this list of conditions and the following disclaimer.
8
--     * Redistributions in binary form must reproduce the above copyright
9
--       notice, this list of conditions and the following disclaimer in the
10
--       documentation and/or other materials provided with the distribution,
11
--       where applicable (as part of a user interface, debugging port, etc.)
12
--
13
-- THIS SOFTWARE IS PROVIDED BY JEREMY SETH HENRY ``AS IS'' AND ANY
14
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15
-- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16
-- DISCLAIMED. IN NO EVENT SHALL JEREMY SETH HENRY BE LIABLE FOR ANY
17
-- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19
-- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20
-- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 194 jshamlet
-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22
-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 191 jshamlet
--
24
-- VHDL Units :  o8_rtc
25 168 jshamlet
-- Description:  Provides automatically updated registers that maintain the
26
--            :   time of day. Keeps track of the day of week, hours, minutes
27 213 jshamlet
--            :   seconds, and tenths of a second in packed BCD format.
28
--            :  Module is doubled buffered to ensure time consistency during
29
--            :   accesses.
30
--            :  Also provides an 8-bit programmable periodic interrupt timer
31
--            :   with 1uS resolution, a 10uS fixed interrupt, as well as a
32
--            :   1 uSec tick (1 clock wide) for external use.
33 191 jshamlet
--
34 168 jshamlet
-- Register Map:
35
-- Offset  Bitfield Description                        Read/Write
36
--   0x0   AAAAAAAA Periodic Interval Timer in uS      (RW)
37 213 jshamlet
--   0x1   BBBBAAAA Tenths  (0x00 - 0x99)              (RW)
38
--   0x2   -BBBAAAA Seconds (0x00 - 0x59)              (RW)
39
--   0x3   -BBBAAAA Minutes (0x00 - 0x59)              (RW)
40
--   0x4   --BBAAAA Hours   (0x00 - 0x23)              (RW)
41 168 jshamlet
--   0x5   -----AAA Day of Week (0x00 - 0x06)          (RW)
42
--   0x6   -------- Update RTC regs from Shadow Regs   (WO)
43
--   0x7   A------- Update Shadow Regs from RTC regs   (RW)
44
--                  A = Update is Busy
45
 
46
library ieee;
47
use ieee.std_logic_1164.all;
48
  use ieee.std_logic_unsigned.all;
49
  use ieee.std_logic_arith.all;
50
  use ieee.std_logic_misc.all;
51
 
52
library work;
53
  use work.open8_pkg.all;
54
 
55
entity o8_rtc is
56
generic(
57 210 jshamlet
  Sys_Freq                   : real;
58
  Reset_Level                : std_logic;
59
  Address                    : ADDRESS_TYPE
60 168 jshamlet
);
61
port(
62 210 jshamlet
  Clock                      : in  std_logic;
63
  Reset                      : in  std_logic;
64
  uSec_Tick                  : out std_logic;
65 168 jshamlet
  --
66 223 jshamlet
  Open8_Bus                  : in  OPEN8_BUS_TYPE;
67 210 jshamlet
  Rd_Data                    : out DATA_TYPE;
68 168 jshamlet
  --
69 210 jshamlet
  Interrupt_PIT              : out std_logic;
70
  Interrupt_RTC              : out std_logic
71 168 jshamlet
);
72
end entity;
73
 
74
architecture behave of o8_rtc is
75
 
76 210 jshamlet
  constant User_Addr         : std_logic_vector(15 downto 3)
77
                               := Address(15 downto 3);
78 223 jshamlet
  alias  Comp_Addr           is Open8_Bus.Address(15 downto 3);
79 210 jshamlet
  signal Addr_Match          : std_logic;
80 168 jshamlet
 
81 223 jshamlet
  alias  Reg_Addr            is Open8_Bus.Address(2 downto 0);
82 210 jshamlet
  signal Reg_Addr_q          : std_logic_vector(2 downto 0);
83 168 jshamlet
 
84 210 jshamlet
  signal Wr_En               : std_logic;
85
  signal Wr_Data_q           : DATA_TYPE;
86
  signal Rd_En               : std_logic;
87 168 jshamlet
 
88 210 jshamlet
  constant DLY_1USEC_VAL     : integer := integer(Sys_Freq / 1000000.0);
89
  constant DLY_1USEC_WDT     : integer := ceil_log2(DLY_1USEC_VAL - 1);
90
  constant DLY_1USEC         : std_logic_vector :=
91
                     conv_std_logic_vector( DLY_1USEC_VAL - 1, DLY_1USEC_WDT);
92 168 jshamlet
 
93 210 jshamlet
  signal uSec_Cntr           : std_logic_vector( DLY_1USEC_WDT - 1 downto 0 )
94
                               := (others => '0');
95
  signal uSec_Tick_i         : std_logic;
96 168 jshamlet
 
97
  type PIT_TYPE is record
98 217 jshamlet
    timer_cnt                : DATA_TYPE;
99
    timer_ro                 : std_logic;
100 168 jshamlet
  end record;
101
 
102 217 jshamlet
  signal pit                 : PIT_TYPE;
103 168 jshamlet
 
104
  type RTC_TYPE is record
105 217 jshamlet
    frac                     : std_logic_vector(15 downto 0);
106
    frac_ro                  : std_logic;
107 168 jshamlet
 
108 217 jshamlet
    tens_l                   : std_logic_vector(3 downto 0);
109
    tens_l_ro                : std_logic;
110 168 jshamlet
 
111 217 jshamlet
    tens_u                   : std_logic_vector(3 downto 0);
112
    tens_u_ro                : std_logic;
113 168 jshamlet
 
114 217 jshamlet
    secs_l                   : std_logic_vector(3 downto 0);
115
    secs_l_ro                : std_logic;
116 168 jshamlet
 
117 217 jshamlet
    secs_u                   : std_logic_vector(3 downto 0);
118
    secs_u_ro                : std_logic;
119 168 jshamlet
 
120 217 jshamlet
    mins_l                   : std_logic_vector(3 downto 0);
121
    mins_l_ro                : std_logic;
122 168 jshamlet
 
123 217 jshamlet
    mins_u                   : std_logic_vector(3 downto 0);
124
    mins_u_ro                : std_logic;
125 168 jshamlet
 
126 217 jshamlet
    hours_l                  : std_logic_vector(3 downto 0);
127
    hours_l_ro               : std_logic;
128 168 jshamlet
 
129 217 jshamlet
    hours_u                  : std_logic_vector(3 downto 0);
130
    hours_u_ro               : std_logic;
131 168 jshamlet
 
132 217 jshamlet
    dow                      : std_logic_vector(2 downto 0);
133 168 jshamlet
  end record;
134
 
135 217 jshamlet
  constant DECISEC           : std_logic_vector(15 downto 0) :=
136
                                conv_std_logic_vector(10000,16);
137 168 jshamlet
 
138 217 jshamlet
  signal rtc                 : RTC_TYPE;
139 168 jshamlet
 
140 217 jshamlet
  signal interval            : DATA_TYPE;
141
  signal update_interval     : std_logic;
142 168 jshamlet
 
143 217 jshamlet
  signal shd_tens            : DATA_TYPE;
144
  signal shd_secs            : DATA_TYPE;
145
  signal shd_mins            : DATA_TYPE;
146
  signal shd_hours           : DATA_TYPE;
147
  signal shd_dow             : DATA_TYPE;
148 168 jshamlet
 
149 217 jshamlet
  signal update_rtc          : std_logic;
150
  signal update_shd          : std_logic;
151
  signal update_ctmr         : std_logic_vector(3 downto 0);
152 168 jshamlet
 
153
begin
154
 
155 210 jshamlet
  uSec_Tick                  <= uSec_Tick_i;
156
  Addr_Match                 <= '1' when Comp_Addr = User_Addr else '0';
157 168 jshamlet
 
158 210 jshamlet
  Interrupt_PIT              <= pit.timer_ro;
159
  Interrupt_RTC              <= rtc.frac_ro;
160 168 jshamlet
 
161
  io_reg: process( Clock, Reset )
162
  begin
163
    if( Reset = Reset_Level )then
164 210 jshamlet
      uSec_Cntr              <= (others => '0');
165
      uSec_Tick_i            <= '0';
166 168 jshamlet
 
167 210 jshamlet
      pit.timer_cnt          <= x"00";
168
      pit.timer_ro           <= '0';
169 168 jshamlet
 
170 210 jshamlet
      rtc.frac               <= DECISEC;
171
      rtc.frac_ro            <= '0';
172 168 jshamlet
 
173 210 jshamlet
      rtc.tens_l             <= (others => '0');
174
      rtc.tens_l_ro          <= '0';
175 168 jshamlet
 
176 210 jshamlet
      rtc.tens_u             <= (others => '0');
177
      rtc.tens_u_ro          <= '0';
178 168 jshamlet
 
179 210 jshamlet
      rtc.secs_l             <= (others => '0');
180
      rtc.secs_l_ro          <= '0';
181 168 jshamlet
 
182 210 jshamlet
      rtc.secs_u             <= (others => '0');
183
      rtc.secs_u_ro          <= '0';
184 168 jshamlet
 
185 210 jshamlet
      rtc.mins_l             <= (others => '0');
186
      rtc.mins_l_ro          <= '0';
187 168 jshamlet
 
188 210 jshamlet
      rtc.mins_u             <= (others => '0');
189
      rtc.mins_u_ro          <= '0';
190 168 jshamlet
 
191 210 jshamlet
      rtc.hours_l            <= (others => '0');
192
      rtc.hours_l_ro         <= '0';
193 168 jshamlet
 
194 210 jshamlet
      rtc.hours_u            <= (others => '0');
195
      rtc.hours_u_ro         <= '0';
196 168 jshamlet
 
197 210 jshamlet
      rtc.dow                <= (others => '0');
198 168 jshamlet
 
199 210 jshamlet
      shd_tens               <= (others => '0');
200
      shd_secs               <= (others => '0');
201
      shd_mins               <= (others => '0');
202
      shd_hours              <= (others => '0');
203
      shd_dow                <= (others => '0');
204 168 jshamlet
 
205 210 jshamlet
      update_rtc             <= '0';
206
      update_shd             <= '0';
207
      update_ctmr            <= (others => '0');
208 168 jshamlet
 
209 210 jshamlet
      interval               <= x"00";
210
      update_interval        <= '0';
211 168 jshamlet
 
212 210 jshamlet
      Wr_Data_q              <= (others => '0');
213
      Reg_Addr_q             <= (others => '0');
214
      Wr_En                  <= '0';
215
      Rd_En                  <= '0';
216
      Rd_Data                <= OPEN8_NULLBUS;
217 168 jshamlet
 
218
    elsif( rising_edge( Clock ) )then
219
 
220 210 jshamlet
      uSec_Cntr              <= uSec_Cntr - 1;
221
      uSec_Tick_i            <= '0';
222 168 jshamlet
      if( uSec_Cntr = 0 )then
223 210 jshamlet
        uSec_Cntr            <= DLY_1USEC;
224
        uSec_Tick_i          <= '1';
225 168 jshamlet
      end if;
226
 
227
      -- Periodic Interval Timer
228 210 jshamlet
      pit.timer_cnt          <= pit.timer_cnt - uSec_Tick_i;
229
      pit.timer_ro           <= '0';
230
      if( update_interval = '1' )then
231 211 jshamlet
        pit.timer_cnt        <= interval;
232 210 jshamlet
      elsif( or_reduce(pit.timer_cnt) = '0' )then
233
        pit.timer_cnt        <= interval;
234
        pit.timer_ro         <= or_reduce(interval);
235 168 jshamlet
      end if;
236
 
237
      -- Fractional decisecond counter - cycles every 10k microseconds
238 210 jshamlet
      rtc.frac               <= rtc.frac - uSec_Tick_i;
239
      rtc.frac_ro            <= '0';
240 168 jshamlet
      if( or_reduce(rtc.frac) = '0' or update_rtc = '1' )then
241 210 jshamlet
        rtc.frac             <= DECISEC;
242
        rtc.frac_ro          <= not update_rtc;
243 168 jshamlet
      end if;
244
 
245
      -- Decisecond counter (lower)
246 210 jshamlet
      rtc.tens_l             <= rtc.tens_l + rtc.frac_ro;
247
      rtc.tens_l_ro          <= '0';
248 168 jshamlet
      if( update_rtc = '1' )then
249 210 jshamlet
        rtc.tens_l           <= shd_tens(3 downto 0);
250 168 jshamlet
      elsif( rtc.tens_l > x"9")then
251 210 jshamlet
        rtc.tens_l           <= (others => '0');
252
        rtc.tens_l_ro        <= '1';
253 168 jshamlet
      end if;
254
 
255
      -- Decisecond counter (upper)
256 210 jshamlet
      rtc.tens_u             <= rtc.tens_u + rtc.tens_l_ro;
257
      rtc.tens_u_ro          <= '0';
258 168 jshamlet
      if( update_rtc = '1' )then
259 210 jshamlet
        rtc.tens_u           <= shd_tens(7 downto 4);
260 168 jshamlet
      elsif( rtc.tens_u > x"9")then
261 210 jshamlet
        rtc.tens_u           <= (others => '0');
262
        rtc.tens_u_ro        <= '1';
263 168 jshamlet
      end if;
264
 
265
      -- Second counter (lower)
266 210 jshamlet
      rtc.secs_l             <= rtc.secs_l + rtc.tens_u_ro;
267
      rtc.secs_l_ro          <= '0';
268 168 jshamlet
      if( update_rtc = '1' )then
269 210 jshamlet
        rtc.secs_l           <= shd_secs(3 downto 0);
270 168 jshamlet
      elsif( rtc.secs_l > x"9")then
271 210 jshamlet
        rtc.secs_l           <= (others => '0');
272
        rtc.secs_l_ro        <= '1';
273 168 jshamlet
      end if;
274
 
275
      -- Second counter (upper)
276 210 jshamlet
      rtc.secs_u             <= rtc.secs_u + rtc.secs_l_ro;
277
      rtc.secs_u_ro          <= '0';
278 168 jshamlet
      if( update_rtc = '1' )then
279 210 jshamlet
        rtc.secs_u           <= shd_secs(7 downto 4);
280 168 jshamlet
      elsif( rtc.secs_u > x"5")then
281 210 jshamlet
        rtc.secs_u           <= (others => '0');
282
        rtc.secs_u_ro        <= '1';
283 168 jshamlet
      end if;
284
 
285
      -- Minutes counter (lower)
286 210 jshamlet
      rtc.mins_l             <= rtc.mins_l + rtc.secs_u_ro;
287
      rtc.mins_l_ro          <= '0';
288 168 jshamlet
      if( update_rtc = '1' )then
289 210 jshamlet
        rtc.mins_l           <= shd_mins(3 downto 0);
290 168 jshamlet
      elsif( rtc.mins_l > x"9")then
291 210 jshamlet
        rtc.mins_l           <= (others => '0');
292
        rtc.mins_l_ro        <= '1';
293 168 jshamlet
      end if;
294
 
295
      -- Minutes counter (upper)
296 210 jshamlet
      rtc.mins_u             <= rtc.mins_u + rtc.mins_l_ro;
297
      rtc.mins_u_ro          <= '0';
298 168 jshamlet
      if( update_rtc = '1' )then
299 210 jshamlet
        rtc.mins_u           <= shd_mins(7 downto 4);
300 168 jshamlet
      elsif( rtc.mins_u > x"5")then
301 210 jshamlet
        rtc.mins_u           <= (others => '0');
302
        rtc.mins_u_ro        <= '1';
303 168 jshamlet
      end if;
304
 
305
      -- Hour counter (lower)
306 210 jshamlet
      rtc.hours_l            <= rtc.hours_l + rtc.mins_u_ro;
307
      rtc.hours_l_ro         <= '0';
308 168 jshamlet
      if( update_rtc = '1' )then
309 210 jshamlet
        rtc.hours_l          <= shd_hours(3 downto 0);
310 168 jshamlet
      elsif( rtc.hours_l > x"9")then
311 210 jshamlet
        rtc.hours_l          <= (others => '0');
312
        rtc.hours_l_ro       <= '1';
313 168 jshamlet
      end if;
314
 
315
      -- Hour counter (upper)
316 210 jshamlet
      rtc.hours_u            <= rtc.hours_u + rtc.hours_l_ro;
317 168 jshamlet
      if( update_rtc = '1' )then
318 210 jshamlet
        rtc.hours_u          <= shd_hours(7 downto 4);
319 168 jshamlet
      end if;
320
 
321 210 jshamlet
      rtc.hours_u_ro         <= '0';
322 168 jshamlet
      if( rtc.hours_u >= x"2" and rtc.hours_l > x"3" )then
323 210 jshamlet
        rtc.hours_l          <= (others => '0');
324
        rtc.hours_u          <= (others => '0');
325
        rtc.hours_u_ro       <= '1';
326 168 jshamlet
      end if;
327
 
328
      -- Day of Week counter
329 210 jshamlet
      rtc.dow                <= rtc.dow + rtc.hours_u_ro;
330 168 jshamlet
      if( update_rtc = '1' )then
331 210 jshamlet
        rtc.dow              <= shd_dow(2 downto 0);
332 168 jshamlet
      elsif( rtc.dow = x"07")then
333 210 jshamlet
        rtc.dow              <= (others => '0');
334 168 jshamlet
      end if;
335
 
336
      -- Copy the RTC registers to the shadow registers when the coherency
337
      --  timer is zero (RTC registers are static)
338
      if( update_shd = '1' and or_reduce(update_ctmr) = '0' )then
339 210 jshamlet
        shd_tens             <= rtc.tens_u & rtc.tens_l;
340
        shd_secs             <= rtc.secs_u & rtc.secs_l;
341
        shd_mins             <= rtc.mins_u & rtc.mins_l;
342
        shd_hours            <= rtc.hours_u & rtc.hours_l;
343
        shd_dow              <= "00000" & rtc.dow;
344
        update_shd           <= '0';
345 168 jshamlet
      end if;
346
 
347 210 jshamlet
      update_interval        <= '0';
348 209 jshamlet
 
349 210 jshamlet
      Reg_Addr_q             <= Reg_Addr;
350 223 jshamlet
      Wr_Data_q              <= Open8_Bus.Wr_Data;
351 168 jshamlet
 
352 223 jshamlet
      Wr_En                  <= Addr_Match and Open8_Bus.Wr_En;
353 210 jshamlet
      update_rtc             <= '0';
354 168 jshamlet
      if( Wr_En = '1' )then
355
        case( Reg_Addr_q )is
356
          when "000" =>
357 211 jshamlet
            interval         <= Wr_Data_q;
358 210 jshamlet
            update_interval  <= '1';
359 168 jshamlet
 
360
          when "001" =>
361 210 jshamlet
            shd_tens         <= Wr_Data_q;
362 168 jshamlet
 
363
          when "010" =>
364 210 jshamlet
            shd_secs         <= Wr_Data_q;
365 168 jshamlet
 
366
          when "011" =>
367 210 jshamlet
            shd_mins         <= Wr_Data_q;
368 168 jshamlet
 
369
          when "100" =>
370 210 jshamlet
            shd_hours        <= Wr_Data_q;
371 168 jshamlet
 
372
          when "101" =>
373 210 jshamlet
            shd_dow          <= Wr_Data_q;
374 168 jshamlet
 
375
          when "110" =>
376 210 jshamlet
            update_rtc       <= '1';
377 168 jshamlet
 
378
          when "111" =>
379
            update_shd  <= '1';
380
 
381
          when others => null;
382
        end case;
383
      end if;
384
 
385
      -- Coherency timer - ensures that the shadow registers are updated with
386
      --  valid time data by delaying updates until the rtc registers have
387
      --  finished cascading.
388 210 jshamlet
      update_ctmr            <= update_ctmr - or_reduce(update_ctmr);
389 168 jshamlet
      if( rtc.frac_ro = '1' )then
390 210 jshamlet
        update_ctmr          <= (others => '1');
391 168 jshamlet
      end if;
392
 
393 210 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
394 223 jshamlet
      Rd_En                  <= Addr_Match and Open8_Bus.Rd_En;
395 168 jshamlet
      if( Rd_En = '1' )then
396
        case( Reg_Addr_q )is
397
          when "000" =>
398 210 jshamlet
            Rd_Data          <= interval;
399 168 jshamlet
          when "001" =>
400 210 jshamlet
            Rd_Data          <= shd_tens;
401 168 jshamlet
          when "010" =>
402 210 jshamlet
            Rd_Data          <= shd_secs;
403 168 jshamlet
          when "011" =>
404 210 jshamlet
            Rd_Data          <= shd_mins;
405 168 jshamlet
          when "100" =>
406 210 jshamlet
            Rd_Data          <= shd_hours;
407 168 jshamlet
          when "101" =>
408 210 jshamlet
            Rd_Data          <= shd_dow;
409 168 jshamlet
          when "110" =>
410
            null;
411
          when "111" =>
412 210 jshamlet
            Rd_Data          <= update_shd & "0000000";
413 168 jshamlet
          when others => null;
414
        end case;
415
      end if;
416
 
417
    end if;
418
  end process;
419
 
420
end architecture;

powered by: WebSVN 2.1.0

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