Debian Codegraph @

mednafen/vb/timer.cpp

raw symbols SCIP
1 /* Mednafen - Multi-system Emulator
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 #include "vb.h"
19 #include "timer.h"
20
21 #define TC_TENABLE 0x01
22 #define TC_ZSTAT 0x02
23 #define TC_ZSTATCLR 0x04
24 #define TC_TIMZINT 0x08
25 #define TC_TCLKSEL 0x10
26
27 static uint8 TimerControl;
28 static uint16 TimerReloadValue;
29 static uint16 TimerCounter;
30 static int32 TimerDivider;
31 static v810_timestamp_t TimerLastTS;
32 static bool TimerStatus, TimerStatusShadow;
33 static bool ReloadPending;
34
35 v810_timestamp_t TIMER_Update(v810_timestamp_t timestamp)
36 {
37 int32 run_time = timestamp - TimerLastTS;
38
39 if(TimerControl & TC_TENABLE)
40 {
41 TimerDivider -= run_time;
42 while(TimerDivider <= 0)
43 {
44 if(!TimerCounter || ReloadPending)
45 {
46 TimerCounter = TimerReloadValue;
47 ReloadPending = false;
48 }
49
50 if(TimerCounter)
51 TimerCounter--;
52
53 if(!TimerCounter || TimerStatus)
54 {
55 TimerStatusShadow = TimerStatus = true;
56 }
57
58 VBIRQ_Assert(VBIRQ_SOURCE_TIMER, TimerStatusShadow && (TimerControl & TC_TIMZINT));
59 TimerDivider += (TimerControl & TC_TCLKSEL) ? 500 : 2000;
60 }
61 }
62
63 TimerLastTS = timestamp;
64
65 return((TimerControl & TC_TENABLE) ? (timestamp + TimerDivider) : VB_EVENT_NONONO);
66 }
67
68 void TIMER_ResetTS(void)
69 {
70 TimerLastTS = 0;
71 }
72
73 uint8 TIMER_Read(const v810_timestamp_t &timestamp, uint32 A)
74 {
75 //if(A <= 0x1C)
76 //printf("Read: %d, %08x\n", timestamp, A);
77 TIMER_Update(timestamp);
78
79 switch(A & 0xFF)
80 {
81 case 0x18:
82 return TimerCounter;
83 case 0x1C:
84 return TimerCounter >> 8;
85 case 0x20:
86 return TimerControl | (0xE0 | TC_ZSTATCLR) | (TimerStatus ? TC_ZSTAT : 0);
87 }
88
89 return 0;
90 }
91
92 void TIMER_Write(const v810_timestamp_t &timestamp, uint32 A, uint8 V)
93 {
94 if(A & 0x3)
95 {
96 puts("HWCtrl Bogus Write?");
97 return;
98 }
99
100 TIMER_Update(timestamp);
101
102 //if((A & 0xFF) <= 0x1C)
103 //printf("Write: %d, %08x %02x\n", timestamp, A, V);
104
105 switch(A & 0xFF)
106 {
107 case 0x18:
108 TimerReloadValue &= 0xFF00;
109 TimerReloadValue |= V;
110 ReloadPending = true;
111 break;
112
113 case 0x1C:
114 TimerReloadValue &= 0x00FF;
115 TimerReloadValue |= V << 8;
116 ReloadPending = true;
117 break;
118
119 case 0x20:
120 if(V & TC_ZSTATCLR)
121 {
122 if((TimerControl & TC_TENABLE) && TimerCounter == 0)
123 {
124 //puts("Faulty Z-Stat-Clr");
125 }
126 else
127 TimerStatus = false;
128 TimerStatusShadow = false;
129 }
130 if((V & TC_TENABLE) && !(TimerControl & TC_TENABLE))
131 {
132 //TimerCounter = TimerReloadValue;
133 TimerDivider = (V & TC_TCLKSEL) ? 500 : 2000;
134 }
135 TimerControl = V & (0x10 | 0x08 | 0x01);
136
137 if(!(TimerControl & TC_TIMZINT))
138 TimerStatus = TimerStatusShadow = false;
139
140 VBIRQ_Assert(VBIRQ_SOURCE_TIMER, TimerStatusShadow && (TimerControl & TC_TIMZINT));
141
142 if(TimerControl & TC_TENABLE)
143 VB_SetEvent(VB_EVENT_TIMER, timestamp + TimerDivider);
144 break;
145 }
146 }
147
148 void TIMER_Power(void)
149 {
150 TimerLastTS = 0;
151
152 TimerCounter = 0xFFFF;
153 TimerReloadValue = 0xFFFF;
154 TimerDivider = 2000; //2150; //2000;
155
156 TimerStatus = false;
157 TimerStatusShadow = false;
158 TimerControl = 0;
159
160 ReloadPending = false;
161
162 VBIRQ_Assert(VBIRQ_SOURCE_TIMER, false);
163 }
164
165 int TIMER_StateAction(StateMem *sm, int load, int data_only)
166 {
167 SFORMAT StateRegs[] =
168 {
169 SFVAR(TimerCounter),
170 SFVAR(TimerReloadValue),
171 SFVAR(TimerDivider),
172 SFVAR(TimerStatus),
173 SFVAR(TimerStatusShadow),
174 SFVAR(TimerControl),
175 SFVAR(ReloadPending),
176 SFEND
177 };
178
179 int ret = MDFNSS_StateAction(sm, load, data_only, StateRegs, "TIMER");
180
181 if(load)
182 {
183
184 }
185
186 return(ret);
187 }
188
189 uint32 TIMER_GetRegister(const unsigned int id, char *special, const uint32 special_len)
190 {
191 switch(id)
192 {
193 case TIMER_GSREG_TCR:
194 return TimerControl;
195 case TIMER_GSREG_DIVCOUNTER:
196 return TimerDivider;
197 case TIMER_GSREG_RELOAD_VALUE:
198 return TimerReloadValue;
199 case TIMER_GSREG_COUNTER:
200 return TimerCounter;
201 }
202
203 return 0xDEADBEEF;
204 }
205
206 void TIMER_SetRegister(const unsigned int id, const uint32 value)
207 {
208 switch(id)
209 {
210 case TIMER_GSREG_TCR:
211 TimerControl = value & (TC_TENABLE | TC_TIMZINT | TC_TCLKSEL);
212 break;
213
214 case TIMER_GSREG_DIVCOUNTER:
215 TimerDivider = value % ((TimerControl & TC_TCLKSEL) ? 500 : 2000);
216 break;
217
218 case TIMER_GSREG_RELOAD_VALUE:
219 TimerReloadValue = value;
220 break;
221
222 case TIMER_GSREG_COUNTER:
223 TimerCounter = value;
224 break;
225
226 }
227 }