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

Subversion Repositories rtcclock

[/] [rtcclock/] [trunk/] [rtl/] [rtclight.v] - Blame information for rev 6

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

Line No. Rev Author Line
1 6 dgisselq
///////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    rtclight.v
4
//              
5
// Project:     A Wishbone Controlled Real--time Clock Core
6
//
7
// Purpose:     Implement a real time clock, including alarm, count--down
8
//              timer, stopwatch, variable time frequency, and more.
9
//
10
//      This is a light-weight version of the RTC found in this directory.
11
//      Unlike the full RTC, this version does not support time hacks, seven
12
//      segment display outputs, or LED's.  It is an RTC for an internal core
13
//      only.  (That's how I was using it on one of my projects anyway ...)
14
//
15
//
16
// Creator:     Dan Gisselquist, Ph.D.
17
//              Gisselquist Tecnology, LLC
18
//
19
///////////////////////////////////////////////////////////////////////////
20
//
21
// Copyright (C) 2015, Gisselquist Technology, LLC
22
//
23
// This program is free software (firmware): you can redistribute it and/or
24
// modify it under the terms of  the GNU General Public License as published
25
// by the Free Software Foundation, either version 3 of the License, or (at
26
// your option) any later version.
27
//
28
// This program is distributed in the hope that it will be useful, but WITHOUT
29
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
30
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
31
// for more details.
32
//
33
// You should have received a copy of the GNU General Public License along
34
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
35
// target there if the PDF file isn't present.)  If not, see
36
// <http://www.gnu.org/licenses/> for a copy.
37
//
38
// License:     GPL, v3, as defined and found on www.gnu.org,
39
//              http://www.gnu.org/licenses/gpl.html
40
//
41
//
42
///////////////////////////////////////////////////////////////////////////
43
module  rtcclock(i_clk,
44
                // Wishbone interface
45
                i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
46
                //      o_wb_ack, o_wb_stb, o_wb_data, // no reads here
47
                // // Button inputs
48
                // i_btn,
49
                // Output registers
50
                o_data, // multiplexed based upon i_wb_addr
51
                // Output controls
52
                o_interrupt,
53
                // A once-per-day strobe on the last clock of the day
54
                o_ppd);
55
        input   i_clk;
56
        input   i_wb_cyc, i_wb_stb, i_wb_we;
57
        input   [2:0]    i_wb_addr;
58
        input   [31:0]   i_wb_data;
59
        // input                i_btn;
60
        output  reg     [31:0]   o_data;
61
        output  wire            o_interrupt, o_ppd;
62
 
63
        reg     [31:0]   clock, stopwatch, ckspeed;
64
        reg     [25:0]   timer;
65
 
66
        wire    ck_sel, tm_sel, sw_sel, sp_sel, al_sel;
67
        assign  ck_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr[2:0]==3'b000));
68
        assign  tm_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr[2:0]==3'b001));
69
        assign  sw_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr[2:0]==3'b010));
70
        assign  al_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr[2:0]==3'b011));
71
        assign  sp_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr[2:0]==3'b100));
72
 
73
        reg     [39:0]   ck_counter;
74
        reg             ck_carry;
75
        always @(posedge i_clk)
76
                { ck_carry, ck_counter } <= ck_counter + { 8'h00, ckspeed };
77
 
78
        wire            ck_pps;
79
        reg             ck_prepps, ck_ppm, ck_pph, ck_ppd;
80
        reg     [7:0]    ck_sub;
81
        initial clock = 32'h00000000;
82
        assign  ck_pps = (ck_carry)&&(ck_prepps);
83
        always @(posedge i_clk)
84
        begin
85
                if (ck_carry)
86
                        ck_sub <= ck_sub + 8'h1;
87
                ck_prepps <= (ck_sub == 8'hff);
88
 
89
                if (ck_pps)
90
                begin // advance the seconds
91
                        if (clock[3:0] >= 4'h9)
92
                                clock[3:0] <= 4'h0;
93
                        else
94
                                clock[3:0] <= clock[3:0] + 4'h1;
95
                        if (clock[7:0] >= 8'h59)
96
                                clock[7:4] <= 4'h0;
97
                        else if (clock[3:0] >= 4'h9)
98
                                clock[7:4] <= clock[7:4] + 4'h1;
99
                end
100
                ck_ppm <= (clock[7:0] == 8'h59);
101
 
102
                if ((ck_pps)&&(ck_ppm))
103
                begin // advance the minutes
104
                        if (clock[11:8] >= 4'h9)
105
                                clock[11:8] <= 4'h0;
106
                        else
107
                                clock[11:8] <= clock[11:8] + 4'h1;
108
                        if (clock[15:8] >= 8'h59)
109
                                clock[15:12] <= 4'h0;
110
                        else if (clock[11:8] >= 4'h9)
111
                                clock[15:12] <= clock[15:12] + 4'h1;
112
                end
113
                ck_pph <= (clock[15:0] == 16'h5959);
114
 
115
                if ((ck_pps)&&(ck_pph))
116
                begin // advance the hours
117
                        if (clock[21:16] >= 6'h23)
118
                        begin
119
                                clock[19:16] <= 4'h0;
120
                                clock[21:20] <= 2'h0;
121
                        end else if (clock[19:16] >= 4'h9)
122
                        begin
123
                                clock[19:16] <= 4'h0;
124
                                clock[21:20] <= clock[21:20] + 2'h1;
125
                        end else begin
126
                                clock[19:16] <= clock[19:16] + 4'h1;
127
                        end
128
                end
129
                ck_ppd <= (clock[21:0] == 22'h235959);
130
 
131
 
132
                if ((ck_sel)&&(i_wb_we))
133
                begin
134
                        if (8'hff != i_wb_data[7:0])
135
                        begin
136
                                clock[7:0] <= i_wb_data[7:0];
137
                                ck_ppm <= (i_wb_data[7:0] == 8'h59);
138
                        end
139
                        if (8'hff != i_wb_data[15:8])
140
                        begin
141
                                clock[15:8] <= i_wb_data[15:8];
142
                                ck_pph <= (i_wb_data[15:8] == 8'h59);
143
                        end
144
                        if (6'h3f != i_wb_data[21:16])
145
                                clock[21:16] <= i_wb_data[21:16];
146
                        clock[31:22] <= i_wb_data[31:22];
147
                        if (8'h00 == i_wb_data[7:0])
148
                                ck_sub <= 8'h00;
149
                end
150
        end
151
 
152
        // Clock updates take several clocks, so let's make sure we
153
        // are only looking at a valid clock value before testing it.
154
        reg     [21:0]           ck_last_clock;
155
        always @(posedge i_clk)
156
                ck_last_clock <= clock[21:0];
157
 
158
 
159
        reg     tm_pps, tm_ppm, tm_int;
160
        wire    tm_stopped, tm_running, tm_alarm;
161
        assign  tm_stopped = ~timer[24];
162
        assign  tm_running =  timer[24];
163
        assign  tm_alarm   =  timer[25];
164
        reg     [23:0]           tm_start;
165
        reg     [7:0]            tm_sub;
166
        initial tm_start = 24'h00;
167
        initial timer    = 26'h00;
168
        initial tm_int   = 1'b0;
169
        initial tm_pps   = 1'b0;
170
        always @(posedge i_clk)
171
        begin
172
                if (ck_carry)
173
                begin
174
                        tm_sub <= tm_sub + 8'h1;
175
                        tm_pps <= (tm_sub == 8'hff);
176
                end else
177
                        tm_pps <= 1'b0;
178
 
179
                if ((~tm_alarm)&&(tm_running)&&(tm_pps))
180
                begin // If we are running ...
181
                        timer[25] <= 1'b0;
182
                        if (timer[23:0] == 24'h00)
183
                                timer[25] <= 1'b1;
184
                        else if (timer[3:0] != 4'h0)
185
                                timer[3:0] <= timer[3:0]-4'h1;
186
                        else begin // last digit is a zero
187
                                timer[3:0] <= 4'h9;
188
                                if (timer[7:4] != 4'h0)
189
                                        timer[7:4] <= timer[7:4]-4'h1;
190
                                else begin // last two digits are zero
191
                                        timer[7:4] <= 4'h5;
192
                                        if (timer[11:8] != 4'h0)
193
                                                timer[11:8] <= timer[11:8]-4'h1;
194
                                        else begin // last three digits are zero
195
                                                timer[11:8] <= 4'h9;
196
                                                if (timer[15:12] != 4'h0)
197
                                                        timer[15:12] <= timer[15:12]-4'h1;
198
                                                else begin
199
                                                        timer[15:12] <= 4'h5;
200
                                                        if (timer[19:16] != 4'h0)
201
                                                                timer[19:16] <= timer[19:16]-4'h1;
202
                                                        else begin
203
                                                        //
204
                                                                timer[19:16] <= 4'h9;
205
                                                                timer[23:20] <= timer[23:20]-4'h1;
206
                                                        end
207
                                                end
208
                                        end
209
                                end
210
                        end
211
                end
212
 
213
                if((~tm_alarm)&&(tm_running))
214
                begin
215
                        timer[25] <= (timer[23:0] == 24'h00);
216
                        tm_int <= (timer[23:0] == 24'h00);
217
                end else tm_int <= 1'b0;
218
                if (tm_alarm)
219
                        timer[24] <= 1'b0;
220
 
221
                if ((tm_sel)&&(i_wb_we)&&(tm_running)) // Writes while running
222
                        // Only allowed to stop the timer, nothing more
223
                        timer[24] <= i_wb_data[24];
224
                else if ((tm_sel)&&(i_wb_we)&&(tm_stopped)) // Writes while off
225
                begin
226
                        timer[24] <= i_wb_data[24];
227
                        if ((timer[24])||(i_wb_data[24]))
228
                                timer[25] <= 1'b0;
229
                        if (i_wb_data[23:0] != 24'h0000)
230
                        begin
231
                                timer[23:0] <= i_wb_data[23:0];
232
                                tm_start <= i_wb_data[23:0];
233
                                tm_sub <= 8'h00;
234
                        end else if (timer[23:0] == 24'h00)
235
                        begin // Resetting timer to last valid timer start val
236
                                timer[23:0] <= tm_start;
237
                                tm_sub <= 8'h00;
238
                        end
239
                        // Any write clears the alarm
240
                        timer[25] <= 1'b0;
241
                end
242
        end
243
 
244
        //
245
        // Stopwatch functionality
246
        //
247
        // Setting bit '0' starts the stop watch, clearing it stops it.
248
        // Writing to the register with bit '1' high will clear the stopwatch,
249
        // and return it to zero provided that the stopwatch is stopped either
250
        // before or after the write.  Hence, writing a '2' to the device
251
        // will always stop and clear it, whereas writing a '3' to the device
252
        // will only clear it if it was already stopped.
253
        reg             sw_pps, sw_ppm, sw_pph;
254
        reg     [7:0]    sw_sub;
255
        wire    sw_running;
256
        assign  sw_running = stopwatch[0];
257
        initial stopwatch = 32'h00000;
258
        always @(posedge i_clk)
259
        begin
260
                sw_pps <= 1'b0;
261
                if (sw_running)
262
                begin
263
                        if (ck_carry)
264
                        begin
265
                                sw_sub <= sw_sub + 8'h1;
266
                                sw_pps <= (sw_sub == 8'hff);
267
                        end
268
                end
269
 
270
                stopwatch[7:1] <= sw_sub[7:1];
271
 
272
                if (sw_pps)
273
                begin // Second hand
274
                        if (stopwatch[11:8] >= 4'h9)
275
                                stopwatch[11:8] <= 4'h0;
276
                        else
277
                                stopwatch[11:8] <= stopwatch[11:8] + 4'h1;
278
 
279
                        if (stopwatch[15:8] >= 8'h59)
280
                                stopwatch[15:12] <= 4'h0;
281
                        else if (stopwatch[11:8] >= 4'h9)
282
                                stopwatch[15:12] <= stopwatch[15:12] + 4'h1;
283
                        sw_ppm <= (stopwatch[15:8] == 8'h59);
284
                end else sw_ppm <= 1'b0;
285
 
286
                if (sw_ppm)
287
                begin // Minutes
288
                        if (stopwatch[19:16] >= 4'h9)
289
                                stopwatch[19:16] <= 4'h0;
290
                        else
291
                                stopwatch[19:16] <= stopwatch[19:16]+4'h1;
292
 
293
                        if (stopwatch[23:16] >= 8'h59)
294
                                stopwatch[23:20] <= 4'h0;
295
                        else if (stopwatch[19:16] >= 4'h9)
296
                                stopwatch[23:20] <= stopwatch[23:20]+4'h1;
297
                        sw_pph <= (stopwatch[23:16] == 8'h59);
298
                end else sw_pph <= 1'b0;
299
 
300
                if (sw_pph)
301
                begin // And hours
302
                        if (stopwatch[27:24] >= 4'h9)
303
                                stopwatch[27:24] <= 4'h0;
304
                        else
305
                                stopwatch[27:24] <= stopwatch[27:24]+4'h1;
306
 
307
                        if((stopwatch[27:24] >= 4'h9)&&(stopwatch[31:28] < 4'hf))
308
                                stopwatch[31:28] <= stopwatch[27:24]+4'h1;
309
                end
310
 
311
                if ((sw_sel)&&(i_wb_we))
312
                begin
313
                        stopwatch[0] <= i_wb_data[0];
314
                        if((i_wb_data[1])&&((~stopwatch[0])||(~i_wb_data[0])))
315
                        begin
316
                                stopwatch[31:1] <= 31'h00;
317
                                sw_sub <= 8'h00;
318
                                sw_pps <= 1'b0;
319
                                sw_ppm <= 1'b0;
320
                                sw_pph <= 1'b0;
321
                        end
322
                end
323
        end
324
 
325
        //
326
        // The alarm code
327
        //
328
        // Set the alarm register to the time you wish the board to "alarm".
329
        // The "alarm" will take place once per day at that time.  At that
330
        // time, the RTC code will generate a clock interrupt, and the CPU/host
331
        // can come and see that the alarm tripped.
332
        //
333
        // 
334
        reg     [21:0]           alarm_time;
335
        reg                     al_int,         // The alarm interrupt line
336
                                al_enabled,     // Whether the alarm is enabled
337
                                al_tripped;     // Whether the alarm has tripped
338
        initial al_enabled= 1'b0;
339
        initial al_tripped= 1'b0;
340
        always @(posedge i_clk)
341
        begin
342
                if ((al_sel)&&(i_wb_we))
343
                begin
344
                        // Only adjust the alarm hours if the requested hours
345
                        // are valid.  This allows writes to the register,
346
                        // without a prior read, to leave these configuration
347
                        // bits alone.
348
                        if (i_wb_data[21:16] != 6'h3f)
349
                                alarm_time[21:16] <= i_wb_data[21:16];
350
                        // Here's the same thing for the minutes: only adjust
351
                        // the alarm minutes if the new bits are not all 1's. 
352
                        if (i_wb_data[15:8] != 8'hff)
353
                                alarm_time[15:8] <= i_wb_data[15:8];
354
                        // Here's the same thing for the seconds: only adjust
355
                        // the alarm minutes if the new bits are not all 1's. 
356
                        if (i_wb_data[7:0] != 8'hff)
357
                                alarm_time[7:0] <= i_wb_data[7:0];
358
                        al_enabled <= i_wb_data[24];
359
                        // Reset the alarm if a '1' is written to the tripped
360
                        // register, or if the alarm is disabled.
361
                        if ((i_wb_data[25])||(~i_wb_data[24]))
362
                                al_tripped <= 1'b0;
363
                end
364
 
365
                al_int <= 1'b0;
366
                if ((ck_last_clock != alarm_time)&&(clock[21:0] == alarm_time)
367
                        &&(al_enabled))
368
                begin
369
                        al_tripped <= 1'b1;
370
                        al_int <= 1'b1;
371
                end
372
        end
373
 
374
        //
375
        // The ckspeed register is equal to 2^48 divded by the number of
376
        // clock ticks you expect per second.  Adjust high for a slower
377
        // clock, lower for a faster clock.  In this fashion, a single
378
        // real time clock RTL file can handle tracking the clock in any
379
        // device.  Further, because this is only the lower 32 bits of a 
380
        // 48 bit counter per seconds, the clock jitter is kept below
381
        // 1 part in 65 thousand.
382
        //
383
        initial ckspeed = 32'd2814750; // 2af31e = 2^48 / 100e6 MHz
384
        // In the case of verilator, comment the above and uncomment the line
385
        // below.  The clock constant below is "close" to simulation time,
386
        // meaning that my verilator simulation is running about 300x slower
387
        // than board time.
388
        // initial      ckspeed = 32'd786432000;
389
        always @(posedge i_clk)
390
                if ((sp_sel)&&(i_wb_we))
391
                        ckspeed <= i_wb_data;
392
 
393
        assign  o_interrupt = tm_int || al_int;
394
 
395
        // A once-per day strobe, on the last second of the day so that the
396
        // the next clock is the first clock of the day.  This is useful for
397
        // connecting this module to a year/month/date date/calendar module.
398
        assign  o_ppd = (ck_ppd)&&(ck_pps);
399
 
400
        always @(posedge i_clk)
401
                case(i_wb_addr[2:0])
402
                3'b000: o_data <= { clock[31:22], ck_last_clock };
403
                3'b001: o_data <= { 6'h00, timer };
404
                3'b010: o_data <= stopwatch;
405
                3'b011: o_data <= { 6'h00, al_tripped, al_enabled, 2'b00, alarm_time };
406
                3'b100: o_data <= ckspeed;
407
                default: o_data <= 32'h000;
408
                endcase
409
 
410
endmodule

powered by: WebSVN 2.1.0

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