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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [sw/] [dev/] [rtcsim.c] - Blame information for rev 15

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

Line No. Rev Author Line
1 15 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    rtcsim.c
4
//
5
// Project:     CMod S6 System on a Chip, ZipCPU demonstration project
6
//
7
// Purpose:     Since we lost the real-time clock on board due to space
8
//              constraints, this unit attempts to replace that functionality
9
//      in software.
10
//
11
// Creator:     Dan Gisselquist, Ph.D.
12
//              Gisselquist Technology, LLC
13
//
14
////////////////////////////////////////////////////////////////////////////////
15
//
16
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
17
//
18
// This program is free software (firmware): you can redistribute it and/or
19
// modify it under the terms of  the GNU General Public License as published
20
// by the Free Software Foundation, either version 3 of the License, or (at
21
// your option) any later version.
22
//
23
// This program is distributed in the hope that it will be useful, but WITHOUT
24
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
25
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
26
// for more details.
27
//
28
// You should have received a copy of the GNU General Public License along
29
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
30
// target there if the PDF file isn't present.)  If not, see
31
// <http://www.gnu.org/licenses/> for a copy.
32
//
33
// License:     GPL, v3, as defined and found on www.gnu.org,
34
//              http://www.gnu.org/licenses/gpl.html
35
//
36
//
37
////////////////////////////////////////////////////////////////////////////////
38
//
39
//
40
#include "rtcsim.h"
41
 
42
unsigned        rtcclock, rtcalarm, rtcdate;
43
#ifdef  ZIPOS
44
SEMAPHORE       SEMDATE, SEMCLOCK;
45
#endif
46
 
47
unsigned        rtcnext(unsigned now) {
48
        now = now+1;
49
        if ((now & 0x0f)>=0x0a) {
50
                now += 0x06; // Seconds
51
                if ((now & 0x0f0)> 0x050) {
52
                        now += 0x0a0; // Increment minutes
53
                        if ((now & 0x0f00)>= 0x0a00) {
54
                                now += 0x0600;
55
                                if ((now & 0x0f000)> 0x05000) {
56
                                        now += 0x0a000; // Increment hours
57
                                        if ((now & 0x0f0000)>= 0x0a0000)
58
                                                now += 0x060000;
59
                                        if (now >= 0x0240000)
60
                                                now = 0;
61
                                }
62
                        }
63
                }
64
        }
65
 
66
        return now;
67
}
68
 
69
static const int days_per_month[] = {
70
        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
71
};
72
 
73
unsigned        rtcdatenext(unsigned today) {
74
        int dy, mo;
75
 
76
        dy = (today&0x0f)+((today>>4)&0x03)*10;
77
        mo = ((today>>8)&0x0f)+(today&0x0100)?10:0;
78
        dy = dy+1;
79
        if ((mo>12)||(dy > days_per_month[mo-1])) {
80
                if (mo == 2) {
81
                        // If its a leap year
82
                        //      If it is divisible by four
83
                        //              But not a century year
84
                        //                      unless it's a fourth century yr
85
                        int     tst;
86
                        if ((today&0x0ff0000) == 0) {
87
                                tst = (today >> 24) & 0x03f;
88
                        } else {
89
                                tst = (today >> 20) & 0x0ff;
90
                        } tst &= 0x013;
91
                        if ((tst==0)||(tst==0x012)) {
92
                                // Divisible by four
93
                                if (dy >= 29)
94
                                        dy = 1;
95
                        } else dy = 1;
96
                } else dy = 1;
97
        } if (dy == 1) {
98
                // Set to the first day of the month
99
                today &= (~0x0ff);
100
                today |= 1;
101
                mo++;
102
                if (mo > 12) {
103
                        // Set to the first month of the year
104
                        today &= ~(0x0ff00);
105
                        today |= 0x0100;
106
                        // Increment the year
107
                        today += 0x010000;
108
                        if ((today & 0x0f0000)>=0x0a0000) {
109
                                today += 0x060000;
110
                                if ((today& 0x0f00000)>= 0x0a00000) {
111
                                        today += 0x0600000;
112
                                        if ((today & 0x0f000000)>= 0x0a000000) {
113
                                                today += 0x06000000;
114
                                                if ((today & 0x0f0000000)>=0x0a0000000)
115
                                                        today += 0x060000000;
116
                                        }
117
                                }
118
                        }
119
                }
120
        } else {
121
                today += 1;
122
                if ((today & 0x0f)>=0x0a)
123
                        today += 0x06;
124
        }
125
 
126
        return today;
127
}
128
 
129
#ifdef  ZIPOS
130
void rtctask(void) {
131
        rtcdate = 0x20160430;
132
        rtctime = 0;
133
        while(1) {
134
                unsigned event = wait(SWINT_RTCPPS);
135
                if (event&INT_TIMA) {
136
                        unsigned v, nextevent = SWINT_PPS;
137
                        semget(SEMCLOCK);
138
                        rtctime = v = rtcnext(rtctime);
139
                        semput(SEMCLOCK);
140
                        if (v == rtcalarm)
141
                                nextevent |= SWINT_ALARM;
142
                        if (v == 0) {
143
                                semget(SEMDATE);
144
                                rtcdate = rtcdatenext(rtcdate);
145
                                semput(SEMDATE);
146
 
147
                                nextevent |= SWINT_PPD;
148
                        } swthrow(nextevent);
149
                }
150
        }
151
}
152
#endif
153
 

powered by: WebSVN 2.1.0

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