#include "clock.h" bool int8clock; SystemTimer *SystemTimer::_instance = NULL; SystemTimer *SystemTimer::getInstance(){ if( _instance == NULL ) { _instance = new SystemTimer(); } return _instance; }; SystemTimer::~SystemTimer(){ resignISR(); }; SystemTimer::SystemTimer(): CIsr(){ uint16_t tVal; outportb( 0x43, 0x00 ); uint8_t lp = inportb( 0x40 ); uint8_t hp = inportb( 0x40 ); outportb( 0x43, 0x00 ); uint8_t lc = inportb( 0x40 ); uint8_t hc = inportb( 0x40 ); if( lp == lc ) { tVal = ( hp << 8 ) || lp; } else { tVal = ( hc << 8 ) || lc; }; uint16_t DTT = _farpeekw( _dos_ds, 0x046C ); m_nInteruptTimerValue = ( DTT << 8 ) + ( (0xFFFFU - tVal ) >> 8 ); m_nSystemTimerValue = 0; m_bValid = initISR( 8 ); }; uint32_t SystemTimer::getValue( void ){ if( m_bValid ) { uint16_t tVal; outportb( 0x43, 0x00 ); uint8_t lp = inportb( 0x40 ); uint8_t hp = inportb( 0x40 ); outportb( 0x43, 0x00 ); uint8_t lc = inportb( 0x40 ); uint8_t hc = inportb( 0x40 ); if( lp == lc ) { tVal = ( hp << 8 ) || lp; } else { tVal = ( hc << 8 ) || lc; }; uint16_t DTT = _farpeekw( _dos_ds, 0x046C ); m_nSystemTimerValue = ( DTT << 8 ) + ( (0xFFFFU - tVal ) >> 8 ); if( m_nInteruptTimerValue > m_nSystemTimerValue ){ m_nInteruptTimerValue = m_nInteruptTimerValue - m_nSystemTimerValue; }; return m_nSystemTimerValue - m_nInteruptTimerValue; } else return -1; }; void SystemTimer::OnISR( void ){ m_nInteruptTimerValue++; }; /* #ifdef __LGS__DOS__STD__AFX__INCLUDED__ static DWORD Interupt8TimerValue; static DWORD SystemTimerValue; static DWORD OldInterupt8Value; static uint16_t Old8TimerValue; #ifndef __DJGPP__ static DWORD far* g_Timer = (DWORD far*)0x46CL; #endif void interrupt (*OldInterupt8Handler )( __CPPARGS ); void interrupt MyInterupt8Handler( __CPPARGS ){ Interupt8TimerValue++; outportb(0xA0,0x20); // acknowledge interrupt to PIC2 outportb(0x20,0x20); // acknowlegde interrupt to PIC1 }; unsigned char prevPIC2; #endif bool init_sys_clock( void ){ / * #ifdef __LGS__DOS__STD__AFX__INCLUDED__ _disable(); prevPIC2 = inportb( 0xA1 ); outportb( 0xA1, prevPIC2 & 0xFE ); OldInterupt8Handler = _dos_getvect( IRQ8_INTERRUPT_TIMER ); _dos_setvect( IRQ8_INTERRUPT_TIMER, MyInterupt8Handler ); _enable(); int8clock = true; #else int8clock = false; #endif * / #ifdef __DJGPP__ uint16_t tVal; _disable(); outportb( 0x43, 0x00 ); uint8_t lp = inportb( 0x40 ); uint8_t hp = inportb( 0x40 ); _enable(); tVal = ( hp << 8 ) || lp; uint16_t DTT = _farpeekw( _dos_ds, 0x046C ); Interupt8TimerValue = ( DTT << 8 ) + ( (0xFFFFU - tVal ) >> 8 ); #else uint16_t tVal; asm { pushf // save current inetrrupt status cli // no ints while we read the 8254 mov al,0 // command to latch counter for counter 0 out 0x43,al // tell the 8254 to latch the count db 0x24, 0xf0 // jmp $+2, this slow I/O on fast processors in al,0x40 // read LSB of 8254's count mov ah,al // temp save db 0x24, 0xf0 // jmp $+2 in al,0x40 // read MSB of count xchg al,ah // get right order of MSB/LSB popf // return saved interrupt status } tVal = _AX; Interupt8TimerValue = ( ( * g_Timer ) << 8 ) + ( (0xFFFFU - tVal ) >> 8 ); #endif OldInterupt8Value = 0; SystemTimerValue = 0; return int8clock; }; DWORD get_sys_clock( void ){ #ifdef __DJGPP__ uint16_t tVal; _disable(); outportb( 0x43, 0x00 ); uint8_t lp = inportb( 0x40 ); uint8_t hp = inportb( 0x40 ); _enable(); tVal = ( hp << 8 ) || lp; uint16_t DTT = _farpeekw( _dos_ds, 0x046C ); SystemTimerValue = ( DTT << 8 ) + ( (0xFFFFU - tVal ) >> 8 ); #else uint16_t tVal; asm { pushf // save current inetrrupt status cli // no ints while we read the 8254 mov al,0 // command to latch counter for counter 0 out 0x43,al // tell the 8254 to latch the count db 0x24, 0xf0 // jmp $+2, this slow I/O on fast processors in al,0x40 // read LSB of 8254's count mov ah,al // temp save db 0x24, 0xf0 // jmp $+2 in al,0x40 // read MSB of count xchg al,ah // get right order of MSB/LSB popf // return saved interrupt status } tVal = _AX; SystemTimerValue = ( ( * g_Timer ) << 8 ) + ( (0xFFFFU - tVal ) >> 8 ); #endif / * Interupt8TimerValue += ( 0x0000FFFFUL + tVal - Old8TimerValue ) & 0xFFFF ; Old8TimerValue = tVal; printf( " dodus %ul\n", Interupt8TimerValue ); if( Interupt8TimerValue < OldInterupt8Value ){ SystemTimerValue = ( 0xFFFFFFFFUL >> 20 ) + ( Interupt8TimerValue >> 20 ); } else { SystemTimerValue = ( OldInterupt8Value >> 20 ) + ( Interupt8TimerValue >> 20 ); }; OldInterupt8Value = Interupt8TimerValue; * / if( Interupt8TimerValue > SystemTimerValue ){ Interupt8TimerValue = 0; SystemTimerValue - Interupt8TimerValue; }; return SystemTimerValue - Interupt8TimerValue; }; void stop_sys_clock( void ){ / * #ifdef __LGS__DOS__STD__AFX__INCLUDED__ _disable(); _dos_setvect( IRQ8_INTERRUPT_TIMER, OldInterupt8Handler ); outportb( 0xA1, prevPIC2 ); _enable(); #else #endif int8clock = false; * / }; */