Debian Codegraph @

src/hardware/timer.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: timer.cpp,v 1.49 2009-04-10 09:53:04 c2woody Exp $ */
20
21 #include <math.h>
22 #include "dosbox.h"
23 #include "inout.h"
24 #include "pic.h"
25 #include "mem.h"
26 #include "mixer.h"
27 #include "timer.h"
28 #include "setup.h"
29
30 static INLINE void BIN2BCD(Bit16u& val) {
31 Bit16u temp=val%10 + (((val/10)%10)<<4)+ (((val/100)%10)<<8) + (((val/1000)%10)<<12);
32 val=temp;
33 }
34
35 static INLINE void BCD2BIN(Bit16u& val) {
36 Bit16u temp= (val&0x0f) +((val>>4)&0x0f) *10 +((val>>8)&0x0f) *100 +((val>>12)&0x0f) *1000;
37 val=temp;
38 }
39
40 struct PIT_Block {
41 Bitu cntr;
42 float delay;
43 double start;
44
45 Bit16u read_latch;
46 Bit16u write_latch;
47
48 Bit8u mode;
49 Bit8u latch_mode;
50 Bit8u read_state;
51 Bit8u write_state;
52
53 bool bcd;
54 bool go_read_latch;
55 bool new_mode;
56 bool counterstatus_set;
57 bool counting;
58 bool update_count;
59 };
60
61 static PIT_Block pit[3];
62 static bool gate2;
63
64 static Bit8u latched_timerstatus;
65 // the timer status can not be overwritten until it is read or the timer was
66 // reprogrammed.
67 static bool latched_timerstatus_locked;
68
69 static void PIT0_Event(Bitu /*val*/) {
70 PIC_ActivateIRQ(0);
71 if (pit[0].mode != 0) {
72 pit[0].start += pit[0].delay;
73
74 if (GCC_UNLIKELY(pit[0].update_count)) {
75 pit[0].delay=(1000.0f/((float)PIT_TICK_RATE/(float)pit[0].cntr));
76 pit[0].update_count=false;
77 }
78 PIC_AddEvent(PIT0_Event,pit[0].delay);
79 }
80 }
81
82 static bool counter_output(Bitu counter) {
83 PIT_Block * p=&pit[counter];
84 double index=PIC_FullIndex()-p->start;
85 switch (p->mode) {
86 case 0:
87 if (p->new_mode) return false;
88 if (index>p->delay) return true;
89 else return false;
90 break;
91 case 2:
92 if (p->new_mode) return true;
93 index=fmod(index,(double)p->delay);
94 return index>0;
95 case 3:
96 if (p->new_mode) return true;
97 index=fmod(index,(double)p->delay);
98 return index*2<p->delay;
99 case 4:
100 //Only low on terminal count
101 // if(fmod(index,(double)p->delay) == 0) return false; //Maybe take one rate tick in consideration
102 //Easiest solution is to report always high (Space marines uses this mode)
103 return true;
104 default:
105 LOG(LOG_PIT,LOG_ERROR)("Illegal Mode %d for reading output",p->mode);
106 return true;
107 }
108 }
109 static void status_latch(Bitu counter) {
110 // the timer status can not be overwritten until it is read or the timer was
111 // reprogrammed.
112 if(!latched_timerstatus_locked) {
113 PIT_Block * p=&pit[counter];
114 latched_timerstatus=0;
115 // Timer Status Word
116 // 0: BCD
117 // 1-3: Timer mode
118 // 4-5: read/load mode
119 // 6: "NULL" - this is 0 if "the counter value is in the counter" ;)
120 // should rarely be 1 (i.e. on exotic modes)
121 // 7: OUT - the logic level on the Timer output pin
122 if(p->bcd)latched_timerstatus|=0x1;
123 latched_timerstatus|=((p->mode&7)<<1);
124 if((p->read_state==0)||(p->read_state==3)) latched_timerstatus|=0x30;
125 else if(p->read_state==1) latched_timerstatus|=0x10;
126 else if(p->read_state==2) latched_timerstatus|=0x20;
127 if(counter_output(counter)) latched_timerstatus|=0x80;
128 if(p->new_mode) latched_timerstatus|=0x40;
129 // The first thing that is being read from this counter now is the
130 // counter status.
131 p->counterstatus_set=true;
132 latched_timerstatus_locked=true;
133 }
134 }
135 static void counter_latch(Bitu counter) {
136 /* Fill the read_latch of the selected counter with current count */
137 PIT_Block * p=&pit[counter];
138 p->go_read_latch=false;
139
140 //If gate2 is disabled don't update the read_latch
141 if(counter == 2 && !gate2 && p->mode !=1) return;
142
143 double index=PIC_FullIndex()-p->start;
144 switch (p->mode) {
145 case 4: /* Software Triggered Strobe */
146 case 0: /* Interrupt on Terminal Count */
147 /* Counter keeps on counting after passing terminal count */
148 if (index>p->delay) {
149 index-=p->delay;
150 if(p->bcd) {
151 index = fmod(index,(1000.0/PIT_TICK_RATE)*10000.0);
152 p->read_latch = (Bit16u)(9999-index*(PIT_TICK_RATE/1000.0));
153 } else {
154 index = fmod(index,(1000.0/PIT_TICK_RATE)*(double)0x10000);
155 p->read_latch = (Bit16u)(0xffff-index*(PIT_TICK_RATE/1000.0));
156 }
157 } else {
158 p->read_latch=(Bit16u)(p->cntr-index*(PIT_TICK_RATE/1000.0));
159 }
160 break;
161 case 1: // countdown
162 if(p->counting) {
163 if (index>p->delay) { // has timed out
164 p->read_latch = 0xffff; //unconfirmed
165 } else {
166 p->read_latch=(Bit16u)(p->cntr-index*(PIT_TICK_RATE/1000.0));
167 }
168 }
169 break;
170 case 2: /* Rate Generator */
171 index=fmod(index,(double)p->delay);
172 p->read_latch=(Bit16u)(p->cntr - (index/p->delay)*p->cntr);
173 break;
174 case 3: /* Square Wave Rate Generator */
175 index=fmod(index,(double)p->delay);
176 index*=2;
177 if (index>p->delay) index-=p->delay;
178 p->read_latch=(Bit16u)(p->cntr - (index/p->delay)*p->cntr);
179 // In mode 3 it never returns odd numbers LSB (if odd number is written 1 will be
180 // subtracted on first clock and then always 2)
181 // fixes "Corncob 3D"
182 p->read_latch&=0xfffe;
183 break;
184 default:
185 LOG(LOG_PIT,LOG_ERROR)("Illegal Mode %d for reading counter %d",p->mode,counter);
186 p->read_latch=0xffff;
187 break;
188 }
189 }
190
191
192 static void write_latch(Bitu port,Bitu val,Bitu /*iolen*/) {
193 //LOG(LOG_PIT,LOG_ERROR)("port %X write:%X state:%X",port,val,pit[port-0x40].write_state);
194 Bitu counter=port-0x40;
195 PIT_Block * p=&pit[counter];
196 if(p->bcd == true) BIN2BCD(p->write_latch);
197
198 switch (p->write_state) {
199 case 0:
200 p->write_latch = p->write_latch | ((val & 0xff) << 8);
201 p->write_state = 3;
202 break;
203 case 3:
204 p->write_latch = val & 0xff;
205 p->write_state = 0;
206 break;
207 case 1:
208 p->write_latch = val & 0xff;
209 break;
210 case 2:
211 p->write_latch = (val & 0xff) << 8;
212 break;
213 }
214 if (p->bcd==true) BCD2BIN(p->write_latch);
215 if (p->write_state != 0) {
216 if (p->write_latch == 0) {
217 if (p->bcd == false) p->cntr = 0x10000;
218 else p->cntr=9999;
219 } else p->cntr = p->write_latch;
220
221 if ((!p->new_mode) && (p->mode == 2) && (counter == 0)) {
222 // In mode 2 writing another value has no direct effect on the count
223 // until the old one has run out. This might apply to other modes too.
224 // This is not fixed for PIT2 yet!!
225 p->update_count=true;
226 return;
227 }
228 p->start=PIC_FullIndex();
229 p->delay=(1000.0f/((float)PIT_TICK_RATE/(float)p->cntr));
230
231 switch (counter) {
232 case 0x00: /* Timer hooked to IRQ 0 */
233 if (p->new_mode || p->mode == 0 ) {
234 if(p->mode==0) PIC_RemoveEvents(PIT0_Event); // DoWhackaDo demo
235 PIC_AddEvent(PIT0_Event,p->delay);
236 } else LOG(LOG_PIT,LOG_NORMAL)("PIT 0 Timer set without new control word");
237 LOG(LOG_PIT,LOG_NORMAL)("PIT 0 Timer at %.4f Hz mode %d",1000.0/p->delay,p->mode);
238 break;
239 case 0x02: /* Timer hooked to PC-Speaker */
240 // LOG(LOG_PIT,"PIT 2 Timer at %.3g Hz mode %d",PIT_TICK_RATE/(double)p->cntr,p->mode);
241 PCSPEAKER_SetCounter(p->cntr,p->mode);
242 break;
243 default:
244 LOG(LOG_PIT,LOG_ERROR)("PIT:Illegal timer selected for writing");
245 }
246 p->new_mode=false;
247 }
248 }
249
250 static Bitu read_latch(Bitu port,Bitu /*iolen*/) {
251 //LOG(LOG_PIT,LOG_ERROR)("port read %X",port);
252 Bit32u counter=port-0x40;
253 Bit8u ret=0;
254 if(GCC_UNLIKELY(pit[counter].counterstatus_set)){
255 pit[counter].counterstatus_set = false;
256 latched_timerstatus_locked = false;
257 ret = latched_timerstatus;
258 } else {
259 if (pit[counter].go_read_latch == true)
260 counter_latch(counter);
261
262 if( pit[counter].bcd == true) BIN2BCD(pit[counter].read_latch);
263
264 switch (pit[counter].read_state) {
265 case 0: /* read MSB & return to state 3 */
266 ret=(pit[counter].read_latch >> 8) & 0xff;
267 pit[counter].read_state = 3;
268 pit[counter].go_read_latch = true;
269 break;
270 case 3: /* read LSB followed by MSB */
271 ret = pit[counter].read_latch & 0xff;
272 pit[counter].read_state = 0;
273 break;
274 case 1: /* read LSB */
275 ret = pit[counter].read_latch & 0xff;
276 pit[counter].go_read_latch = true;
277 break;
278 case 2: /* read MSB */
279 ret = (pit[counter].read_latch >> 8) & 0xff;
280 pit[counter].go_read_latch = true;
281 break;
282 default:
283 E_Exit("Timer.cpp: error in readlatch");
284 break;
285 }
286 if( pit[counter].bcd == true) BCD2BIN(pit[counter].read_latch);
287 }
288 return ret;
289 }
290
291 static void write_p43(Bitu /*port*/,Bitu val,Bitu /*iolen*/) {
292 //LOG(LOG_PIT,LOG_ERROR)("port 43 %X",val);
293 Bitu latch=(val >> 6) & 0x03;
294 switch (latch) {
295 case 0:
296 case 1:
297 case 2:
298 if ((val & 0x30) == 0) {
299 /* Counter latch command */
300 counter_latch(latch);
301 } else {
302 pit[latch].bcd = (val&1)>0;
303 if (val & 1) {
304 if(pit[latch].cntr>=9999) pit[latch].cntr=9999;
305 }
306
307 // Timer is being reprogrammed, unlock the status
308 if(pit[latch].counterstatus_set) {
309 pit[latch].counterstatus_set=false;
310 latched_timerstatus_locked=false;
311 }
312 pit[latch].update_count = false;
313 pit[latch].counting = false;
314 pit[latch].read_state = (val >> 4) & 0x03;
315 pit[latch].write_state = (val >> 4) & 0x03;
316 Bit8u mode = (val >> 1) & 0x07;
317 if (mode > 5)
318 mode -= 4; //6,7 become 2 and 3
319
320 /* Don't set it directly so counter_output uses the old mode */
321 /* That's theory. It breaks panic. So set it here again */
322 if(!pit[latch].mode) pit[latch].mode = mode;
323
324 /* If the line goes from low to up => generate irq.
325 * ( BUT needs to stay up until acknowlegded by the cpu!!! therefore: )
326 * If the line goes to low => disable irq.
327 * Mode 0 starts with a low line. (so always disable irq)
328 * Mode 2,3 start with a high line.
329 * counter_output tells if the current counter is high or low
330 * So actually a mode 2 timer enables and disables irq al the time. (not handled) */
331
332 if (latch == 0) {
333 PIC_RemoveEvents(PIT0_Event);
334 if (!counter_output(0) && mode) {
335 PIC_ActivateIRQ(0);
336 //Don't raise instantaniously. (Origamo)
337 if(CPU_Cycles < 25) CPU_Cycles = 25;
338 }
339 if(!mode)
340 PIC_DeActivateIRQ(0);
341 }
342 pit[latch].new_mode = true;
343 pit[latch].mode = mode; //Set the correct mode (here)
344 }
345 break;
346 case 3:
347 if ((val & 0x20)==0) { /* Latch multiple pit counters */
348 if (val & 0x02) counter_latch(0);
349 if (val & 0x04) counter_latch(1);
350 if (val & 0x08) counter_latch(2);
351 }
352 // status and values can be latched simultaneously
353 if ((val & 0x10)==0) { /* Latch status words */
354 // but only 1 status can be latched simultaneously
355 if (val & 0x02) status_latch(0);
356 else if (val & 0x04) status_latch(1);
357 else if (val & 0x08) status_latch(2);
358 }
359 break;
360 }
361 }
362
363 void TIMER_SetGate2(bool in) {
364 //No changes if gate doesn't change
365 if(gate2 == in) return;
366 Bit8u & mode=pit[2].mode;
367 switch(mode) {
368 case 0:
369 if(in) pit[2].start = PIC_FullIndex();
370 else {
371 //Fill readlatch and store it.
372 counter_latch(2);
373 pit[2].cntr = pit[2].read_latch;
374 }
375 break;
376 case 1:
377 // gate 1 on: reload counter; off: nothing
378 if(in) {
379 pit[2].counting = true;
380 pit[2].start = PIC_FullIndex();
381 }
382 break;
383 case 2:
384 case 3:
385 //If gate is enabled restart counting. If disable store the current read_latch
386 if(in) pit[2].start = PIC_FullIndex();
387 else counter_latch(2);
388 break;
389 case 4:
390 case 5:
391 LOG(LOG_MISC,LOG_WARN)("unsupported gate 2 mode %x",mode);
392 break;
393 }
394 gate2 = in; //Set it here so the counter_latch above works
395 }
396
397 class TIMER:public Module_base{
398 private:
399 IO_ReadHandleObject ReadHandler[4];
400 IO_WriteHandleObject WriteHandler[4];
401 public:
402 TIMER(Section* configuration):Module_base(configuration){
403 WriteHandler[0].Install(0x40,write_latch,IO_MB);
404 // WriteHandler[1].Install(0x41,write_latch,IO_MB);
405 WriteHandler[2].Install(0x42,write_latch,IO_MB);
406 WriteHandler[3].Install(0x43,write_p43,IO_MB);
407 ReadHandler[0].Install(0x40,read_latch,IO_MB);
408 ReadHandler[1].Install(0x41,read_latch,IO_MB);
409 ReadHandler[2].Install(0x42,read_latch,IO_MB);
410 /* Setup Timer 0 */
411 pit[0].cntr=0x10000;
412 pit[0].write_state = 3;
413 pit[0].read_state = 3;
414 pit[0].read_latch=0;
415 pit[0].write_latch=0;
416 pit[0].mode=3;
417 pit[0].bcd = false;
418 pit[0].go_read_latch = true;
419 pit[0].counterstatus_set = false;
420 pit[0].update_count = false;
421
422 pit[1].bcd = false;
423 pit[1].write_state = 1;
424 pit[1].read_state = 1;
425 pit[1].go_read_latch = true;
426 pit[1].cntr = 18;
427 pit[1].mode = 2;
428 pit[1].write_state = 3;
429 pit[1].counterstatus_set = false;
430
431 pit[2].read_latch=1320; /* MadTv1 */
432 pit[2].write_state = 3; /* Chuck Yeager */
433 pit[2].read_state = 3;
434 pit[2].mode=3;
435 pit[2].bcd=false;
436 pit[2].cntr=1320;
437 pit[2].go_read_latch=true;
438 pit[2].counterstatus_set = false;
439 pit[2].counting = false;
440
441 pit[0].delay=(1000.0f/((float)PIT_TICK_RATE/(float)pit[0].cntr));
442 pit[1].delay=(1000.0f/((float)PIT_TICK_RATE/(float)pit[1].cntr));
443 pit[2].delay=(1000.0f/((float)PIT_TICK_RATE/(float)pit[2].cntr));
444
445 latched_timerstatus_locked=false;
446 gate2 = false;
447 PIC_AddEvent(PIT0_Event,pit[0].delay);
448 }
449 ~TIMER(){
450 PIC_RemoveEvents(PIT0_Event);
451 }
452 };
453 static TIMER* test;
454
455 void TIMER_Destroy(Section*){
456 delete test;
457 }
458 void TIMER_Init(Section* sec) {
459 test = new TIMER(sec);
460 sec->AddDestroyFunction(&TIMER_Destroy);
461 }