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

Subversion Repositories s6soc

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

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 21 dgisselq
// SEMAPHORE    SEMDATE, SEMCLOCK;
45 15 dgisselq
#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
        return now;
66
}
67
 
68 45 dgisselq
static const char days_per_month[] = {
69 15 dgisselq
        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
70
};
71
 
72
unsigned        rtcdatenext(unsigned today) {
73
        int dy, mo;
74
 
75
        dy = (today&0x0f)+((today>>4)&0x03)*10;
76 45 dgisselq
        mo = ((today>>8)&0x0f)+((today&0x0100)?10:0);
77 15 dgisselq
        dy = dy+1;
78
        if ((mo>12)||(dy > days_per_month[mo-1])) {
79
                if (mo == 2) {
80
                        // If its a leap year
81
                        //      If it is divisible by four
82
                        //              But not a century year
83
                        //                      unless it's a fourth century yr
84
                        int     tst;
85
                        if ((today&0x0ff0000) == 0) {
86
                                tst = (today >> 24) & 0x03f;
87
                        } else {
88
                                tst = (today >> 20) & 0x0ff;
89
                        } tst &= 0x013;
90
                        if ((tst==0)||(tst==0x012)) {
91
                                // Divisible by four
92
                                if (dy >= 29)
93
                                        dy = 1;
94
                        } else dy = 1;
95
                } else dy = 1;
96
        } if (dy == 1) {
97
                // Set to the first day of the month
98
                today &= (~0x0ff);
99
                today |= 1;
100
                mo++;
101
                if (mo > 12) {
102
                        // Set to the first month of the year
103
                        today &= ~(0x0ff00);
104
                        today |= 0x0100;
105
                        // Increment the year
106
                        today += 0x010000;
107
                        if ((today & 0x0f0000)>=0x0a0000) {
108
                                today += 0x060000;
109
                                if ((today& 0x0f00000)>= 0x0a00000) {
110
                                        today += 0x0600000;
111
                                        if ((today & 0x0f000000)>= 0x0a000000) {
112
                                                today += 0x06000000;
113
                                                if ((today & 0x0f0000000)>=0x0a0000000)
114
                                                        today += 0x060000000;
115
                                        }
116
                                }
117
                        }
118
                }
119
        } else {
120
                today += 1;
121
                if ((today & 0x0f)>=0x0a)
122
                        today += 0x06;
123
        }
124
        return today;
125
}
126
 
127
#ifdef  ZIPOS
128 21 dgisselq
#include "board.h"
129
#include "../zipos/ktraps.h"
130
#include "../zipos/swint.h"
131
 
132 15 dgisselq
void rtctask(void) {
133 21 dgisselq
        // IOSPACE *sys = (IOSPACE *)IOADDR;
134 45 dgisselq
        rtcdate = 0x20170408;
135 21 dgisselq
        rtcclock = 0;
136 15 dgisselq
        while(1) {
137 21 dgisselq
                unsigned event = wait(SWINT_CLOCK,-1);
138
                if (event&SWINT_CLOCK) {
139 15 dgisselq
                        unsigned v, nextevent = SWINT_PPS;
140
                        semget(SEMCLOCK);
141 21 dgisselq
                        rtcclock = v = rtcnext(rtcclock);
142 15 dgisselq
                        semput(SEMCLOCK);
143 21 dgisselq
#ifdef  SWINT_ALARM
144 15 dgisselq
                        if (v == rtcalarm)
145
                                nextevent |= SWINT_ALARM;
146 21 dgisselq
#endif
147 15 dgisselq
                        if (v == 0) {
148
                                semget(SEMDATE);
149
                                rtcdate = rtcdatenext(rtcdate);
150
                                semput(SEMDATE);
151
 
152 21 dgisselq
#ifdef  SWINT_PPD
153 15 dgisselq
                                nextevent |= SWINT_PPD;
154 21 dgisselq
#endif
155
                        } post(nextevent);
156 15 dgisselq
                }
157
        }
158
}
159
#endif
160
 

powered by: WebSVN 2.1.0

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