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 209

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 189 jshamlet
  Sys_Freq              : real;
56 168 jshamlet
  Reset_Level           : std_logic;
57 189 jshamlet
  Address               : ADDRESS_TYPE
58 168 jshamlet
);
59
port(
60
  Clock                 : in  std_logic;
61
  Reset                 : in  std_logic;
62
  uSec_Tick             : out std_logic;
63
  --
64
  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
  --
70
  Interrupt_PIT         : out std_logic;
71
  Interrupt_RTC         : out std_logic
72
);
73
end entity;
74
 
75
architecture behave of o8_rtc is
76
 
77
  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
 
82
  alias  Reg_Addr       is Bus_Address(2 downto 0);
83
  signal Reg_Addr_q     : std_logic_vector(2 downto 0);
84
 
85
  signal Wr_En          : std_logic;
86
  signal Wr_Data_q      : DATA_TYPE;
87
  signal Rd_En          : std_logic;
88
 
89
  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
 
94
  signal uSec_Cntr      : std_logic_vector( DLY_1USEC_WDT - 1 downto 0 )
95
                          := (others => '0');
96
  signal uSec_Tick_i      : std_logic;
97
 
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 209 jshamlet
  signal mask_pit_int   : 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
  uSec_Tick             <= uSec_Tick_i;
157
  Addr_Match            <= '1' when Comp_Addr = User_Addr else '0';
158
 
159
  Interrupt_PIT         <= pit.timer_ro;
160
  Interrupt_RTC         <= rtc.frac_ro;
161
 
162
  io_reg: process( Clock, Reset )
163
  begin
164
    if( Reset = Reset_Level )then
165
      uSec_Cntr         <= (others => '0');
166
      uSec_Tick_i       <= '0';
167
 
168
      pit.timer_cnt     <= x"00";
169
      pit.timer_ro      <= '0';
170
 
171
      rtc.frac          <= DECISEC;
172
      rtc.frac_ro       <= '0';
173
 
174
      rtc.tens_l        <= (others => '0');
175
      rtc.tens_l_ro     <= '0';
176
 
177
      rtc.tens_u        <= (others => '0');
178
      rtc.tens_u_ro     <= '0';
179
 
180
      rtc.secs_l        <= (others => '0');
181
      rtc.secs_l_ro     <= '0';
182
 
183
      rtc.secs_u        <= (others => '0');
184
      rtc.secs_u_ro     <= '0';
185
 
186
      rtc.mins_l        <= (others => '0');
187
      rtc.mins_l_ro     <= '0';
188
 
189
      rtc.mins_u        <= (others => '0');
190
      rtc.mins_u_ro     <= '0';
191
 
192
      rtc.hours_l       <= (others => '0');
193
      rtc.hours_l_ro    <= '0';
194
 
195
      rtc.hours_u       <= (others => '0');
196
      rtc.hours_u_ro    <= '0';
197
 
198
      rtc.dow           <= (others => '0');
199
 
200
      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
 
206
      update_rtc        <= '0';
207
      update_shd        <= '0';
208
      update_ctmr       <= (others => '0');
209
 
210
      interval          <= x"00";
211 209 jshamlet
      mask_pit_int      <= '0';
212 168 jshamlet
 
213
      Wr_Data_q         <= (others => '0');
214
      Reg_Addr_q        <= (others => '0');
215
      Wr_En             <= '0';
216
      Rd_En             <= '0';
217 191 jshamlet
      Rd_Data           <= OPEN8_NULLBUS;
218 168 jshamlet
 
219
    elsif( rising_edge( Clock ) )then
220
 
221
      uSec_Cntr         <= uSec_Cntr - 1;
222
      uSec_Tick_i       <= '0';
223
      if( uSec_Cntr = 0 )then
224
        uSec_Cntr       <= DLY_1USEC;
225 190 jshamlet
        uSec_Tick_i     <= '1';
226 168 jshamlet
      end if;
227
 
228
      -- Periodic Interval Timer
229
      pit.timer_cnt     <= pit.timer_cnt - uSec_Tick_i;
230 190 jshamlet
      pit.timer_ro      <= '0';
231 168 jshamlet
      if( or_reduce(pit.timer_cnt) = '0' )then
232
        pit.timer_cnt   <= interval;
233 209 jshamlet
        pit.timer_ro    <= or_reduce(interval) and -- Only issue output on Int > 0
234
                           (not mask_pit_int);     -- and we didn't just update it
235
 
236 168 jshamlet
      end if;
237
 
238
      -- Fractional decisecond counter - cycles every 10k microseconds
239
      rtc.frac          <= rtc.frac - uSec_Tick_i;
240 190 jshamlet
      rtc.frac_ro       <= '0';
241 168 jshamlet
      if( or_reduce(rtc.frac) = '0' or update_rtc = '1' )then
242
        rtc.frac        <= DECISEC;
243
        rtc.frac_ro     <= not update_rtc;
244
      end if;
245
 
246
      -- Decisecond counter (lower)
247
      rtc.tens_l        <= rtc.tens_l + rtc.frac_ro;
248 190 jshamlet
      rtc.tens_l_ro     <= '0';
249 168 jshamlet
      if( update_rtc = '1' )then
250
        rtc.tens_l      <= shd_tens(3 downto 0);
251
      elsif( rtc.tens_l > x"9")then
252
        rtc.tens_l      <= (others => '0');
253
        rtc.tens_l_ro   <= '1';
254
      end if;
255
 
256
      -- Decisecond counter (upper)
257
      rtc.tens_u        <= rtc.tens_u + rtc.tens_l_ro;
258 190 jshamlet
      rtc.tens_u_ro     <= '0';
259 168 jshamlet
      if( update_rtc = '1' )then
260
        rtc.tens_u      <= shd_tens(7 downto 4);
261
      elsif( rtc.tens_u > x"9")then
262
        rtc.tens_u      <= (others => '0');
263
        rtc.tens_u_ro   <= '1';
264
      end if;
265
 
266
      -- Second counter (lower)
267
      rtc.secs_l        <= rtc.secs_l + rtc.tens_u_ro;
268 190 jshamlet
      rtc.secs_l_ro     <= '0';
269 168 jshamlet
      if( update_rtc = '1' )then
270
        rtc.secs_l      <= shd_secs(3 downto 0);
271
      elsif( rtc.secs_l > x"9")then
272
        rtc.secs_l      <= (others => '0');
273
        rtc.secs_l_ro   <= '1';
274
      end if;
275
 
276
      -- Second counter (upper)
277
      rtc.secs_u        <= rtc.secs_u + rtc.secs_l_ro;
278 190 jshamlet
      rtc.secs_u_ro     <= '0';
279 168 jshamlet
      if( update_rtc = '1' )then
280
        rtc.secs_u      <= shd_secs(7 downto 4);
281
      elsif( rtc.secs_u > x"5")then
282
        rtc.secs_u      <= (others => '0');
283
        rtc.secs_u_ro   <= '1';
284
      end if;
285
 
286
      -- Minutes counter (lower)
287
      rtc.mins_l        <= rtc.mins_l + rtc.secs_u_ro;
288 190 jshamlet
      rtc.mins_l_ro     <= '0';
289 168 jshamlet
      if( update_rtc = '1' )then
290
        rtc.mins_l      <= shd_mins(3 downto 0);
291
      elsif( rtc.mins_l > x"9")then
292
        rtc.mins_l      <= (others => '0');
293
        rtc.mins_l_ro   <= '1';
294
      end if;
295
 
296
      -- Minutes counter (upper)
297
      rtc.mins_u        <= rtc.mins_u + rtc.mins_l_ro;
298 190 jshamlet
      rtc.mins_u_ro     <= '0';
299 168 jshamlet
      if( update_rtc = '1' )then
300
        rtc.mins_u      <= shd_mins(7 downto 4);
301
      elsif( rtc.mins_u > x"5")then
302
        rtc.mins_u      <= (others => '0');
303
        rtc.mins_u_ro   <= '1';
304
      end if;
305
 
306
      -- Hour counter (lower)
307
      rtc.hours_l       <= rtc.hours_l + rtc.mins_u_ro;
308 190 jshamlet
      rtc.hours_l_ro    <= '0';
309 168 jshamlet
      if( update_rtc = '1' )then
310
        rtc.hours_l     <= shd_hours(3 downto 0);
311
      elsif( rtc.hours_l > x"9")then
312
        rtc.hours_l     <= (others => '0');
313
        rtc.hours_l_ro  <= '1';
314
      end if;
315
 
316
      -- Hour counter (upper)
317
      rtc.hours_u       <= rtc.hours_u + rtc.hours_l_ro;
318
      if( update_rtc = '1' )then
319
        rtc.hours_u     <= shd_hours(7 downto 4);
320
      end if;
321
 
322 190 jshamlet
      rtc.hours_u_ro    <= '0';
323 168 jshamlet
      if( rtc.hours_u >= x"2" and rtc.hours_l > x"3" )then
324
        rtc.hours_l     <= (others => '0');
325
        rtc.hours_u     <= (others => '0');
326
        rtc.hours_u_ro  <= '1';
327
      end if;
328
 
329
      -- Day of Week counter
330
      rtc.dow           <= rtc.dow + rtc.hours_u_ro;
331
      if( update_rtc = '1' )then
332
        rtc.dow        <= shd_dow(2 downto 0);
333
      elsif( rtc.dow = x"07")then
334
        rtc.dow         <= (others => '0');
335
      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
        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
      end if;
347
 
348 209 jshamlet
      mask_pit_int      <= '0';
349
 
350 168 jshamlet
      Reg_Addr_q        <= Reg_Addr;
351
      Wr_Data_q         <= Wr_Data;
352
 
353
      Wr_En             <= Addr_Match and Wr_Enable;
354
      update_rtc        <= '0';
355
      if( Wr_En = '1' )then
356
        case( Reg_Addr_q )is
357
          when "000" =>
358
            interval    <= Wr_Data_q;
359 209 jshamlet
            mask_pit_int <= '1';
360 168 jshamlet
 
361
          when "001" =>
362
            shd_tens    <= Wr_Data_q;
363
 
364
          when "010" =>
365
            shd_secs    <= Wr_Data_q;
366
 
367
          when "011" =>
368
            shd_mins    <= Wr_Data_q;
369
 
370
          when "100" =>
371
            shd_hours   <= Wr_Data_q;
372
 
373
          when "101" =>
374
            shd_dow     <= Wr_Data_q;
375
 
376
          when "110" =>
377
            update_rtc  <= '1';
378
 
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
      update_ctmr       <= update_ctmr - or_reduce(update_ctmr);
390
      if( rtc.frac_ro = '1' )then
391
        update_ctmr     <= (others => '1');
392
      end if;
393
 
394 191 jshamlet
      Rd_Data           <= OPEN8_NULLBUS;
395 168 jshamlet
      Rd_En             <= Addr_Match and Rd_Enable;
396
      if( Rd_En = '1' )then
397
        case( Reg_Addr_q )is
398
          when "000" =>
399
            Rd_Data     <= interval;
400
          when "001" =>
401
            Rd_Data     <= shd_tens;
402
          when "010" =>
403
            Rd_Data     <= shd_secs;
404
          when "011" =>
405
            Rd_Data     <= shd_mins;
406
          when "100" =>
407
            Rd_Data     <= shd_hours;
408
          when "101" =>
409
            Rd_Data     <= shd_dow;
410
          when "110" =>
411
            null;
412
          when "111" =>
413
            Rd_Data     <= update_shd & "0000000";
414
          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.