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 211

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
--            :   seconds, and tenths of a second. Module is doubled buffered
28
--            :   to ensure time consistency during accesses. Also provides
29
--            :   a programmable periodic interrupt timer, as well as a uSec
30
--            :    tick for external use.
31 191 jshamlet
--
32 168 jshamlet
-- Register Map:
33
-- Offset  Bitfield Description                        Read/Write
34
--   0x0   AAAAAAAA Periodic Interval Timer in uS      (RW)
35 189 jshamlet
--   0x1   -AAAAAAA Tenths  (0x00 - 0x63)              (RW)
36
--   0x2   --AAAAAA Seconds (0x00 - 0x3B)              (RW)
37
--   0x3   --AAAAAA Minutes (0x00 - 0x3B)              (RW)
38
--   0x4   ---AAAAA Hours   (0x00 - 0x17)              (RW)
39 168 jshamlet
--   0x5   -----AAA Day of Week (0x00 - 0x06)          (RW)
40
--   0x6   -------- Update RTC regs from Shadow Regs   (WO)
41
--   0x7   A------- Update Shadow Regs from RTC regs   (RW)
42
--                  A = Update is Busy
43
 
44
library ieee;
45
use ieee.std_logic_1164.all;
46
  use ieee.std_logic_unsigned.all;
47
  use ieee.std_logic_arith.all;
48
  use ieee.std_logic_misc.all;
49
 
50
library work;
51
  use work.open8_pkg.all;
52
 
53
entity o8_rtc is
54
generic(
55 210 jshamlet
  Sys_Freq                   : real;
56
  Reset_Level                : std_logic;
57
  Address                    : ADDRESS_TYPE
58 168 jshamlet
);
59
port(
60 210 jshamlet
  Clock                      : in  std_logic;
61
  Reset                      : in  std_logic;
62
  uSec_Tick                  : out std_logic;
63 168 jshamlet
  --
64 210 jshamlet
  Bus_Address                : in  ADDRESS_TYPE;
65
  Wr_Enable                  : in  std_logic;
66
  Wr_Data                    : in  DATA_TYPE;
67
  Rd_Enable                  : in  std_logic;
68
  Rd_Data                    : out DATA_TYPE;
69 168 jshamlet
  --
70 210 jshamlet
  Interrupt_PIT              : out std_logic;
71
  Interrupt_RTC              : out std_logic
72 168 jshamlet
);
73
end entity;
74
 
75
architecture behave of o8_rtc is
76
 
77 210 jshamlet
  constant User_Addr         : std_logic_vector(15 downto 3)
78
                               := Address(15 downto 3);
79
  alias  Comp_Addr           is Bus_Address(15 downto 3);
80
  signal Addr_Match          : std_logic;
81 168 jshamlet
 
82 210 jshamlet
  alias  Reg_Addr            is Bus_Address(2 downto 0);
83
  signal Reg_Addr_q          : std_logic_vector(2 downto 0);
84 168 jshamlet
 
85 210 jshamlet
  signal Wr_En               : std_logic;
86
  signal Wr_Data_q           : DATA_TYPE;
87
  signal Rd_En               : std_logic;
88 168 jshamlet
 
89 210 jshamlet
  constant DLY_1USEC_VAL     : integer := integer(Sys_Freq / 1000000.0);
90
  constant DLY_1USEC_WDT     : integer := ceil_log2(DLY_1USEC_VAL - 1);
91
  constant DLY_1USEC         : std_logic_vector :=
92
                     conv_std_logic_vector( DLY_1USEC_VAL - 1, DLY_1USEC_WDT);
93 168 jshamlet
 
94 210 jshamlet
  signal uSec_Cntr           : std_logic_vector( DLY_1USEC_WDT - 1 downto 0 )
95
                               := (others => '0');
96
  signal uSec_Tick_i         : std_logic;
97 168 jshamlet
 
98
  type PIT_TYPE is record
99
    timer_cnt           : DATA_TYPE;
100
    timer_ro            : std_logic;
101
  end record;
102
 
103
  signal pit            : PIT_TYPE;
104
 
105
  type RTC_TYPE is record
106
    frac                : std_logic_vector(15 downto 0);
107
    frac_ro             : std_logic;
108
 
109
    tens_l              : std_logic_vector(3 downto 0);
110
    tens_l_ro           : std_logic;
111
 
112
    tens_u              : std_logic_vector(3 downto 0);
113
    tens_u_ro           : std_logic;
114
 
115
    secs_l              : std_logic_vector(3 downto 0);
116
    secs_l_ro           : std_logic;
117
 
118
    secs_u              : std_logic_vector(3 downto 0);
119
    secs_u_ro           : std_logic;
120
 
121
    mins_l              : std_logic_vector(3 downto 0);
122
    mins_l_ro           : std_logic;
123
 
124
    mins_u              : std_logic_vector(3 downto 0);
125
    mins_u_ro           : std_logic;
126
 
127
    hours_l             : std_logic_vector(3 downto 0);
128
    hours_l_ro          : std_logic;
129
 
130
    hours_u             : std_logic_vector(3 downto 0);
131
    hours_u_ro          : std_logic;
132
 
133
    dow                 : std_logic_vector(2 downto 0);
134
  end record;
135
 
136
  constant DECISEC      : std_logic_vector(15 downto 0) :=
137
                           conv_std_logic_vector(10000,16);
138
 
139
  signal rtc            : RTC_TYPE;
140
 
141
  signal interval       : DATA_TYPE;
142 210 jshamlet
  signal update_interval: std_logic;
143 168 jshamlet
 
144
  signal shd_tens       : DATA_TYPE;
145
  signal shd_secs       : DATA_TYPE;
146
  signal shd_mins       : DATA_TYPE;
147
  signal shd_hours      : DATA_TYPE;
148
  signal shd_dow        : DATA_TYPE;
149
 
150
  signal update_rtc     : std_logic;
151
  signal update_shd     : std_logic;
152
  signal update_ctmr    : std_logic_vector(3 downto 0);
153
 
154
begin
155
 
156 210 jshamlet
  uSec_Tick                  <= uSec_Tick_i;
157
  Addr_Match                 <= '1' when Comp_Addr = User_Addr else '0';
158 168 jshamlet
 
159 210 jshamlet
  Interrupt_PIT              <= pit.timer_ro;
160
  Interrupt_RTC              <= rtc.frac_ro;
161 168 jshamlet
 
162
  io_reg: process( Clock, Reset )
163
  begin
164
    if( Reset = Reset_Level )then
165 210 jshamlet
      uSec_Cntr              <= (others => '0');
166
      uSec_Tick_i            <= '0';
167 168 jshamlet
 
168 210 jshamlet
      pit.timer_cnt          <= x"00";
169
      pit.timer_ro           <= '0';
170 168 jshamlet
 
171 210 jshamlet
      rtc.frac               <= DECISEC;
172
      rtc.frac_ro            <= '0';
173 168 jshamlet
 
174 210 jshamlet
      rtc.tens_l             <= (others => '0');
175
      rtc.tens_l_ro          <= '0';
176 168 jshamlet
 
177 210 jshamlet
      rtc.tens_u             <= (others => '0');
178
      rtc.tens_u_ro          <= '0';
179 168 jshamlet
 
180 210 jshamlet
      rtc.secs_l             <= (others => '0');
181
      rtc.secs_l_ro          <= '0';
182 168 jshamlet
 
183 210 jshamlet
      rtc.secs_u             <= (others => '0');
184
      rtc.secs_u_ro          <= '0';
185 168 jshamlet
 
186 210 jshamlet
      rtc.mins_l             <= (others => '0');
187
      rtc.mins_l_ro          <= '0';
188 168 jshamlet
 
189 210 jshamlet
      rtc.mins_u             <= (others => '0');
190
      rtc.mins_u_ro          <= '0';
191 168 jshamlet
 
192 210 jshamlet
      rtc.hours_l            <= (others => '0');
193
      rtc.hours_l_ro         <= '0';
194 168 jshamlet
 
195 210 jshamlet
      rtc.hours_u            <= (others => '0');
196
      rtc.hours_u_ro         <= '0';
197 168 jshamlet
 
198 210 jshamlet
      rtc.dow                <= (others => '0');
199 168 jshamlet
 
200 210 jshamlet
      shd_tens               <= (others => '0');
201
      shd_secs               <= (others => '0');
202
      shd_mins               <= (others => '0');
203
      shd_hours              <= (others => '0');
204
      shd_dow                <= (others => '0');
205 168 jshamlet
 
206 210 jshamlet
      update_rtc             <= '0';
207
      update_shd             <= '0';
208
      update_ctmr            <= (others => '0');
209 168 jshamlet
 
210 210 jshamlet
      interval               <= x"00";
211
      update_interval        <= '0';
212 168 jshamlet
 
213 210 jshamlet
      Wr_Data_q              <= (others => '0');
214
      Reg_Addr_q             <= (others => '0');
215
      Wr_En                  <= '0';
216
      Rd_En                  <= '0';
217
      Rd_Data                <= OPEN8_NULLBUS;
218 168 jshamlet
 
219
    elsif( rising_edge( Clock ) )then
220
 
221 210 jshamlet
      uSec_Cntr              <= uSec_Cntr - 1;
222
      uSec_Tick_i            <= '0';
223 168 jshamlet
      if( uSec_Cntr = 0 )then
224 210 jshamlet
        uSec_Cntr            <= DLY_1USEC;
225
        uSec_Tick_i          <= '1';
226 168 jshamlet
      end if;
227
 
228
      -- Periodic Interval Timer
229 210 jshamlet
      pit.timer_cnt          <= pit.timer_cnt - uSec_Tick_i;
230
      pit.timer_ro           <= '0';
231
      if( update_interval = '1' )then
232 211 jshamlet
        pit.timer_cnt        <= interval;
233 210 jshamlet
      elsif( or_reduce(pit.timer_cnt) = '0' )then
234
        pit.timer_cnt        <= interval;
235
        pit.timer_ro         <= or_reduce(interval);
236 168 jshamlet
      end if;
237
 
238
      -- Fractional decisecond counter - cycles every 10k microseconds
239 210 jshamlet
      rtc.frac               <= rtc.frac - uSec_Tick_i;
240
      rtc.frac_ro            <= '0';
241 168 jshamlet
      if( or_reduce(rtc.frac) = '0' or update_rtc = '1' )then
242 210 jshamlet
        rtc.frac             <= DECISEC;
243
        rtc.frac_ro          <= not update_rtc;
244 168 jshamlet
      end if;
245
 
246
      -- Decisecond counter (lower)
247 210 jshamlet
      rtc.tens_l             <= rtc.tens_l + rtc.frac_ro;
248
      rtc.tens_l_ro          <= '0';
249 168 jshamlet
      if( update_rtc = '1' )then
250 210 jshamlet
        rtc.tens_l           <= shd_tens(3 downto 0);
251 168 jshamlet
      elsif( rtc.tens_l > x"9")then
252 210 jshamlet
        rtc.tens_l           <= (others => '0');
253
        rtc.tens_l_ro        <= '1';
254 168 jshamlet
      end if;
255
 
256
      -- Decisecond counter (upper)
257 210 jshamlet
      rtc.tens_u             <= rtc.tens_u + rtc.tens_l_ro;
258
      rtc.tens_u_ro          <= '0';
259 168 jshamlet
      if( update_rtc = '1' )then
260 210 jshamlet
        rtc.tens_u           <= shd_tens(7 downto 4);
261 168 jshamlet
      elsif( rtc.tens_u > x"9")then
262 210 jshamlet
        rtc.tens_u           <= (others => '0');
263
        rtc.tens_u_ro        <= '1';
264 168 jshamlet
      end if;
265
 
266
      -- Second counter (lower)
267 210 jshamlet
      rtc.secs_l             <= rtc.secs_l + rtc.tens_u_ro;
268
      rtc.secs_l_ro          <= '0';
269 168 jshamlet
      if( update_rtc = '1' )then
270 210 jshamlet
        rtc.secs_l           <= shd_secs(3 downto 0);
271 168 jshamlet
      elsif( rtc.secs_l > x"9")then
272 210 jshamlet
        rtc.secs_l           <= (others => '0');
273
        rtc.secs_l_ro        <= '1';
274 168 jshamlet
      end if;
275
 
276
      -- Second counter (upper)
277 210 jshamlet
      rtc.secs_u             <= rtc.secs_u + rtc.secs_l_ro;
278
      rtc.secs_u_ro          <= '0';
279 168 jshamlet
      if( update_rtc = '1' )then
280 210 jshamlet
        rtc.secs_u           <= shd_secs(7 downto 4);
281 168 jshamlet
      elsif( rtc.secs_u > x"5")then
282 210 jshamlet
        rtc.secs_u           <= (others => '0');
283
        rtc.secs_u_ro        <= '1';
284 168 jshamlet
      end if;
285
 
286
      -- Minutes counter (lower)
287 210 jshamlet
      rtc.mins_l             <= rtc.mins_l + rtc.secs_u_ro;
288
      rtc.mins_l_ro          <= '0';
289 168 jshamlet
      if( update_rtc = '1' )then
290 210 jshamlet
        rtc.mins_l           <= shd_mins(3 downto 0);
291 168 jshamlet
      elsif( rtc.mins_l > x"9")then
292 210 jshamlet
        rtc.mins_l           <= (others => '0');
293
        rtc.mins_l_ro        <= '1';
294 168 jshamlet
      end if;
295
 
296
      -- Minutes counter (upper)
297 210 jshamlet
      rtc.mins_u             <= rtc.mins_u + rtc.mins_l_ro;
298
      rtc.mins_u_ro          <= '0';
299 168 jshamlet
      if( update_rtc = '1' )then
300 210 jshamlet
        rtc.mins_u           <= shd_mins(7 downto 4);
301 168 jshamlet
      elsif( rtc.mins_u > x"5")then
302 210 jshamlet
        rtc.mins_u           <= (others => '0');
303
        rtc.mins_u_ro        <= '1';
304 168 jshamlet
      end if;
305
 
306
      -- Hour counter (lower)
307 210 jshamlet
      rtc.hours_l            <= rtc.hours_l + rtc.mins_u_ro;
308
      rtc.hours_l_ro         <= '0';
309 168 jshamlet
      if( update_rtc = '1' )then
310 210 jshamlet
        rtc.hours_l          <= shd_hours(3 downto 0);
311 168 jshamlet
      elsif( rtc.hours_l > x"9")then
312 210 jshamlet
        rtc.hours_l          <= (others => '0');
313
        rtc.hours_l_ro       <= '1';
314 168 jshamlet
      end if;
315
 
316
      -- Hour counter (upper)
317 210 jshamlet
      rtc.hours_u            <= rtc.hours_u + rtc.hours_l_ro;
318 168 jshamlet
      if( update_rtc = '1' )then
319 210 jshamlet
        rtc.hours_u          <= shd_hours(7 downto 4);
320 168 jshamlet
      end if;
321
 
322 210 jshamlet
      rtc.hours_u_ro         <= '0';
323 168 jshamlet
      if( rtc.hours_u >= x"2" and rtc.hours_l > x"3" )then
324 210 jshamlet
        rtc.hours_l          <= (others => '0');
325
        rtc.hours_u          <= (others => '0');
326
        rtc.hours_u_ro       <= '1';
327 168 jshamlet
      end if;
328
 
329
      -- Day of Week counter
330 210 jshamlet
      rtc.dow                <= rtc.dow + rtc.hours_u_ro;
331 168 jshamlet
      if( update_rtc = '1' )then
332 210 jshamlet
        rtc.dow              <= shd_dow(2 downto 0);
333 168 jshamlet
      elsif( rtc.dow = x"07")then
334 210 jshamlet
        rtc.dow              <= (others => '0');
335 168 jshamlet
      end if;
336
 
337
      -- Copy the RTC registers to the shadow registers when the coherency
338
      --  timer is zero (RTC registers are static)
339
      if( update_shd = '1' and or_reduce(update_ctmr) = '0' )then
340 210 jshamlet
        shd_tens             <= rtc.tens_u & rtc.tens_l;
341
        shd_secs             <= rtc.secs_u & rtc.secs_l;
342
        shd_mins             <= rtc.mins_u & rtc.mins_l;
343
        shd_hours            <= rtc.hours_u & rtc.hours_l;
344
        shd_dow              <= "00000" & rtc.dow;
345
        update_shd           <= '0';
346 168 jshamlet
      end if;
347
 
348 210 jshamlet
      update_interval        <= '0';
349 209 jshamlet
 
350 210 jshamlet
      Reg_Addr_q             <= Reg_Addr;
351
      Wr_Data_q              <= Wr_Data;
352 168 jshamlet
 
353 210 jshamlet
      Wr_En                  <= Addr_Match and Wr_Enable;
354
      update_rtc             <= '0';
355 168 jshamlet
      if( Wr_En = '1' )then
356
        case( Reg_Addr_q )is
357
          when "000" =>
358 211 jshamlet
            interval         <= Wr_Data_q;
359 210 jshamlet
            update_interval  <= '1';
360 168 jshamlet
 
361
          when "001" =>
362 210 jshamlet
            shd_tens         <= Wr_Data_q;
363 168 jshamlet
 
364
          when "010" =>
365 210 jshamlet
            shd_secs         <= Wr_Data_q;
366 168 jshamlet
 
367
          when "011" =>
368 210 jshamlet
            shd_mins         <= Wr_Data_q;
369 168 jshamlet
 
370
          when "100" =>
371 210 jshamlet
            shd_hours        <= Wr_Data_q;
372 168 jshamlet
 
373
          when "101" =>
374 210 jshamlet
            shd_dow          <= Wr_Data_q;
375 168 jshamlet
 
376
          when "110" =>
377 210 jshamlet
            update_rtc       <= '1';
378 168 jshamlet
 
379
          when "111" =>
380
            update_shd  <= '1';
381
 
382
          when others => null;
383
        end case;
384
      end if;
385
 
386
      -- Coherency timer - ensures that the shadow registers are updated with
387
      --  valid time data by delaying updates until the rtc registers have
388
      --  finished cascading.
389 210 jshamlet
      update_ctmr            <= update_ctmr - or_reduce(update_ctmr);
390 168 jshamlet
      if( rtc.frac_ro = '1' )then
391 210 jshamlet
        update_ctmr          <= (others => '1');
392 168 jshamlet
      end if;
393
 
394 210 jshamlet
      Rd_Data                <= OPEN8_NULLBUS;
395
      Rd_En                  <= Addr_Match and Rd_Enable;
396 168 jshamlet
      if( Rd_En = '1' )then
397
        case( Reg_Addr_q )is
398
          when "000" =>
399 210 jshamlet
            Rd_Data          <= interval;
400 168 jshamlet
          when "001" =>
401 210 jshamlet
            Rd_Data          <= shd_tens;
402 168 jshamlet
          when "010" =>
403 210 jshamlet
            Rd_Data          <= shd_secs;
404 168 jshamlet
          when "011" =>
405 210 jshamlet
            Rd_Data          <= shd_mins;
406 168 jshamlet
          when "100" =>
407 210 jshamlet
            Rd_Data          <= shd_hours;
408 168 jshamlet
          when "101" =>
409 210 jshamlet
            Rd_Data          <= shd_dow;
410 168 jshamlet
          when "110" =>
411
            null;
412
          when "111" =>
413 210 jshamlet
            Rd_Data          <= update_shd & "0000000";
414 168 jshamlet
          when others => null;
415
        end case;
416
      end if;
417
 
418
    end if;
419
  end process;
420
 
421
end architecture;

powered by: WebSVN 2.1.0

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