2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
2002-02-09 01:27:51 +03:00
|
|
|
// $Id: pit_wrap.cc,v 1.17 2002-02-08 22:27:51 yakovlev Exp $
|
2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2002-01-29 20:20:12 +03:00
|
|
|
// Copyright (C) 2002 MandrakeSoft S.A.
|
2001-06-11 07:09:40 +04:00
|
|
|
//
|
|
|
|
// MandrakeSoft S.A.
|
|
|
|
// 43, rue d'Aboukir
|
|
|
|
// 75002 Paris - France
|
|
|
|
// http://www.linux-mandrake.com/
|
|
|
|
// http://www.mandrakesoft.com/
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "bochs.h"
|
2001-07-02 00:49:46 +04:00
|
|
|
|
2001-08-16 00:23:58 +04:00
|
|
|
#if BX_USE_NEW_PIT
|
2001-07-02 00:49:46 +04:00
|
|
|
|
2001-06-11 07:09:40 +04:00
|
|
|
#include "pit_wrap.h"
|
2001-07-02 00:49:46 +04:00
|
|
|
#define LOG_THIS bx_pit.
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2001-07-02 00:49:46 +04:00
|
|
|
bx_pit_c bx_pit;
|
2001-06-11 07:09:40 +04:00
|
|
|
#if BX_USE_PIT_SMF
|
2001-07-02 00:49:46 +04:00
|
|
|
#define this (&bx_pit)
|
2001-06-11 07:09:40 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef OUT
|
|
|
|
# undef OUT
|
|
|
|
#endif
|
|
|
|
|
2002-02-09 01:27:51 +03:00
|
|
|
#define DEBUG_REALTIME_WITH_PRINTF 0
|
|
|
|
|
2002-02-08 05:57:27 +03:00
|
|
|
#define TIME_DIVIDER (1)
|
2002-02-08 00:22:55 +03:00
|
|
|
#define TIME_MULTIPLIER (1)
|
|
|
|
#define TIME_HEADSTART (1)
|
|
|
|
//USEC_ALPHA is multiplier for the past.
|
|
|
|
//USEC_ALPHA_B is 1-USEC_ALPHA, or multiplier for the present.
|
2002-02-09 01:27:51 +03:00
|
|
|
#define USEC_ALPHA ((double)(.8))
|
|
|
|
#define USEC_ALPHA_B ((double)(((double)1)-USEC_ALPHA))
|
2002-02-08 08:18:14 +03:00
|
|
|
#define MIN_MULT (0.9)
|
|
|
|
#define MIN_MULT_FLOOR (0.75)
|
|
|
|
#define MAX_MULT (1.25)
|
|
|
|
#define MAX_MULT_CEILING (1.5)
|
2002-02-08 00:22:55 +03:00
|
|
|
|
2001-10-11 17:01:27 +04:00
|
|
|
//How many timer ticks per usecond.
|
2002-01-31 20:18:38 +03:00
|
|
|
//1.193181MHz Clock
|
2001-10-11 17:01:27 +04:00
|
|
|
//1193/1000 Ticks Per usecond.
|
2002-02-08 00:22:55 +03:00
|
|
|
#define TICKS_PER_SECOND (1193181)
|
|
|
|
#define USEC_PER_SECOND (1000000)
|
2001-10-11 17:01:27 +04:00
|
|
|
#define TIME_MULT 1.193
|
2002-02-08 00:22:55 +03:00
|
|
|
#if BX_USE_REALTIME_PIT
|
|
|
|
# define TICKS_TO_USEC(a) ( ((a)*BX_PIT_THIS s.usec_per_second)/BX_PIT_THIS s.ticks_per_second )
|
|
|
|
# define USEC_TO_TICKS(a) ( ((a)*BX_PIT_THIS s.ticks_per_second)/BX_PIT_THIS s.usec_per_second )
|
|
|
|
#else
|
|
|
|
# define TICKS_TO_USEC(a) ( ((a)*USEC_PER_SECOND)/TICKS_PER_SECOND )
|
|
|
|
# define USEC_TO_TICKS(a) ( ((a)*TICKS_PER_SECOND)/USEC_PER_SECOND )
|
|
|
|
#endif
|
2001-10-11 17:01:27 +04:00
|
|
|
#define MAX(a,b) ( ((a)>(b))?(a):(b) )
|
|
|
|
|
2001-07-02 00:49:46 +04:00
|
|
|
bx_pit_c::bx_pit_c( void )
|
2001-06-11 07:09:40 +04:00
|
|
|
{
|
2001-06-27 23:16:01 +04:00
|
|
|
put("PIT");
|
2001-06-11 07:09:40 +04:00
|
|
|
settype(PITLOG);
|
|
|
|
s.speaker_data_on=0;
|
|
|
|
|
|
|
|
/* 8254 PIT (Programmable Interval Timer) */
|
|
|
|
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.timer_handle[1] = BX_NULL_TIMER_HANDLE;
|
|
|
|
BX_PIT_THIS s.timer_handle[2] = BX_NULL_TIMER_HANDLE;
|
2001-08-18 07:28:23 +04:00
|
|
|
BX_PIT_THIS s.timer_handle[0] = BX_NULL_TIMER_HANDLE;
|
2001-06-11 07:09:40 +04:00
|
|
|
}
|
|
|
|
|
2001-07-02 00:49:46 +04:00
|
|
|
bx_pit_c::~bx_pit_c( void )
|
2001-06-11 07:09:40 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2001-07-02 00:49:46 +04:00
|
|
|
bx_pit_c::init( bx_devices_c *d )
|
2001-06-11 07:09:40 +04:00
|
|
|
{
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS devices = d;
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS devices->register_irq(0, "8254 PIT");
|
|
|
|
BX_PIT_THIS devices->register_io_read_handler(this, read_handler, 0x0040, "8254 PIT");
|
|
|
|
BX_PIT_THIS devices->register_io_read_handler(this, read_handler, 0x0041, "8254 PIT");
|
|
|
|
BX_PIT_THIS devices->register_io_read_handler(this, read_handler, 0x0042, "8254 PIT");
|
|
|
|
BX_PIT_THIS devices->register_io_read_handler(this, read_handler, 0x0043, "8254 PIT");
|
|
|
|
BX_PIT_THIS devices->register_io_read_handler(this, read_handler, 0x0061, "8254 PIT");
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS devices->register_io_write_handler(this, write_handler, 0x0040, "8254 PIT");
|
|
|
|
BX_PIT_THIS devices->register_io_write_handler(this, write_handler, 0x0041, "8254 PIT");
|
|
|
|
BX_PIT_THIS devices->register_io_write_handler(this, write_handler, 0x0042, "8254 PIT");
|
|
|
|
BX_PIT_THIS devices->register_io_write_handler(this, write_handler, 0x0043, "8254 PIT");
|
|
|
|
BX_PIT_THIS devices->register_io_write_handler(this, write_handler, 0x0061, "8254 PIT");
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2001-08-18 07:28:23 +04:00
|
|
|
BX_DEBUG(("pit: starting init"));
|
|
|
|
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.speaker_data_on = 0;
|
|
|
|
BX_PIT_THIS s.refresh_clock_div2 = 0;
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.timer.init();
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2001-08-18 07:28:23 +04:00
|
|
|
BX_PIT_THIS s.timer_handle[0] = bx_pc_system.register_timer(this, timer_handler, (unsigned) 100 , 1, 1);
|
|
|
|
BX_DEBUG(("pit: RESETting timer."));
|
|
|
|
bx_pc_system.deactivate_timer(BX_PIT_THIS s.timer_handle[0]);
|
|
|
|
BX_DEBUG(("deactivated timer."));
|
|
|
|
if(BX_PIT_THIS s.timer.get_next_event_time()) {
|
|
|
|
bx_pc_system.activate_timer(BX_PIT_THIS s.timer_handle[0],
|
2001-10-11 17:01:27 +04:00
|
|
|
MAX(1,TICKS_TO_USEC(BX_PIT_THIS s.timer.get_next_event_time())),
|
2001-08-18 07:28:23 +04:00
|
|
|
0);
|
|
|
|
BX_DEBUG(("activated timer."));
|
|
|
|
}
|
|
|
|
BX_PIT_THIS s.last_next_event_time = BX_PIT_THIS s.timer.get_next_event_time();
|
|
|
|
BX_PIT_THIS s.last_usec=bx_pc_system.time_usec();
|
|
|
|
|
2002-01-31 20:18:38 +03:00
|
|
|
BX_PIT_THIS s.total_ticks=0;
|
|
|
|
|
2002-02-08 00:22:55 +03:00
|
|
|
#if BX_USE_REALTIME_PIT
|
|
|
|
BX_PIT_THIS s.usec_per_second=USEC_PER_SECOND;
|
|
|
|
BX_PIT_THIS s.ticks_per_second=TICKS_PER_SECOND;
|
|
|
|
BX_PIT_THIS s.total_sec=0;
|
|
|
|
BX_PIT_THIS s.last_time=(time(NULL)*TIME_MULTIPLIER/TIME_DIVIDER)+TIME_HEADSTART;
|
|
|
|
#else
|
|
|
|
BX_PIT_THIS s.total_usec=0;
|
|
|
|
#endif
|
|
|
|
|
2001-08-18 07:28:23 +04:00
|
|
|
BX_DEBUG(("pit: finished init"));
|
|
|
|
|
|
|
|
BX_DEBUG(("s.last_usec=%d",BX_PIT_THIS s.last_usec));
|
|
|
|
BX_DEBUG(("s.timer_id=%d",BX_PIT_THIS s.timer_handle[0]));
|
|
|
|
BX_DEBUG(("s.timer.get_next_event_time=%d",BX_PIT_THIS s.timer.get_next_event_time()));
|
|
|
|
BX_DEBUG(("s.last_next_event_time=%d",BX_PIT_THIS s.last_next_event_time));
|
|
|
|
|
2001-06-11 07:09:40 +04:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-18 07:28:23 +04:00
|
|
|
void
|
|
|
|
bx_pit_c::timer_handler(void *this_ptr) {
|
|
|
|
bx_pit_c * class_ptr = (bx_pit_c *) this_ptr;
|
|
|
|
|
|
|
|
class_ptr->handle_timer();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
bx_pit_c::handle_timer() {
|
|
|
|
Bit64u time_passed = bx_pc_system.time_usec()-BX_PIT_THIS s.last_usec;
|
|
|
|
Bit32u time_passed32 = time_passed;
|
|
|
|
|
|
|
|
BX_DEBUG(("pit: entering timer handler"));
|
|
|
|
|
2002-01-31 20:18:38 +03:00
|
|
|
if(time_passed32) {
|
|
|
|
periodic(time_passed32);
|
2001-08-18 07:28:23 +04:00
|
|
|
}
|
|
|
|
BX_PIT_THIS s.last_usec=BX_PIT_THIS s.last_usec + time_passed;
|
|
|
|
if(time_passed ||
|
|
|
|
(BX_PIT_THIS s.last_next_event_time
|
|
|
|
!= BX_PIT_THIS s.timer.get_next_event_time())
|
|
|
|
) {
|
|
|
|
BX_DEBUG(("pit: RESETting timer."));
|
|
|
|
bx_pc_system.deactivate_timer(BX_PIT_THIS s.timer_handle[0]);
|
|
|
|
BX_DEBUG(("deactivated timer."));
|
|
|
|
if(BX_PIT_THIS s.timer.get_next_event_time()) {
|
|
|
|
bx_pc_system.activate_timer(BX_PIT_THIS s.timer_handle[0],
|
2001-10-11 17:01:27 +04:00
|
|
|
MAX(1,TICKS_TO_USEC(BX_PIT_THIS s.timer.get_next_event_time())),
|
2001-08-18 07:28:23 +04:00
|
|
|
0);
|
|
|
|
BX_DEBUG(("activated timer."));
|
|
|
|
}
|
|
|
|
BX_PIT_THIS s.last_next_event_time = BX_PIT_THIS s.timer.get_next_event_time();
|
|
|
|
}
|
|
|
|
BX_DEBUG(("s.last_usec=%d",BX_PIT_THIS s.last_usec));
|
|
|
|
BX_DEBUG(("s.timer_id=%d",BX_PIT_THIS s.timer_handle[0]));
|
|
|
|
BX_DEBUG(("s.timer.get_next_event_time=%x",BX_PIT_THIS s.timer.get_next_event_time()));
|
|
|
|
BX_DEBUG(("s.last_next_event_time=%d",BX_PIT_THIS s.last_next_event_time));
|
|
|
|
}
|
|
|
|
|
2001-06-11 07:09:40 +04:00
|
|
|
|
|
|
|
// static IO port read callback handler
|
|
|
|
// redirects to non-static class handler to avoid virtual functions
|
|
|
|
|
|
|
|
Bit32u
|
2001-07-02 00:49:46 +04:00
|
|
|
bx_pit_c::read_handler(void *this_ptr, Bit32u address, unsigned io_len)
|
2001-06-11 07:09:40 +04:00
|
|
|
{
|
|
|
|
#if !BX_USE_PIT_SMF
|
2001-07-02 00:49:46 +04:00
|
|
|
bx_pit_c *class_ptr = (bx_pit_c *) this_ptr;
|
2001-06-11 07:09:40 +04:00
|
|
|
|
|
|
|
return( class_ptr->read(address, io_len) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Bit32u
|
2001-07-02 00:49:46 +04:00
|
|
|
bx_pit_c::read( Bit32u address, unsigned int io_len )
|
2001-06-11 07:09:40 +04:00
|
|
|
{
|
|
|
|
#else
|
|
|
|
UNUSED(this_ptr);
|
|
|
|
#endif // !BX_USE_PIT_SMF
|
2001-08-18 07:28:23 +04:00
|
|
|
BX_DEBUG(("pit: entering read handler"));
|
|
|
|
|
|
|
|
handle_timer();
|
|
|
|
|
2001-06-11 07:09:40 +04:00
|
|
|
if (io_len > 1)
|
|
|
|
BX_PANIC(("pit: io read from port %04x, len=%u", (unsigned) address,
|
|
|
|
(unsigned) io_len));
|
|
|
|
|
|
|
|
if (bx_dbg.pit)
|
|
|
|
BX_INFO(("pit: io read from port %04x", (unsigned) address));
|
|
|
|
|
|
|
|
switch (address) {
|
|
|
|
|
|
|
|
case 0x40: /* timer 0 - system ticks */
|
2001-07-02 00:49:46 +04:00
|
|
|
return(BX_PIT_THIS s.timer.read(0));
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
case 0x41: /* timer 1 read */
|
2001-07-02 00:49:46 +04:00
|
|
|
return(BX_PIT_THIS s.timer.read(1));
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
case 0x42: /* timer 2 read */
|
2001-07-02 00:49:46 +04:00
|
|
|
return(BX_PIT_THIS s.timer.read(2));
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
case 0x43: /* timer 1 read */
|
2001-07-02 00:49:46 +04:00
|
|
|
return(BX_PIT_THIS s.timer.read(3));
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x61:
|
|
|
|
/* AT, port 61h */
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.refresh_clock_div2 = !BX_PIT_THIS s.refresh_clock_div2;
|
|
|
|
return( (BX_PIT_THIS s.timer.read_OUT(2)<<5) |
|
|
|
|
(BX_PIT_THIS s.refresh_clock_div2<<4) |
|
|
|
|
(BX_PIT_THIS s.speaker_data_on<<1) |
|
2001-08-30 06:02:10 +04:00
|
|
|
(BX_PIT_THIS s.timer.read_GATE(2)?1:0) );
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
BX_PANIC(("pit: unsupported io read from port %04x", address));
|
2001-08-18 07:28:23 +04:00
|
|
|
}
|
2001-06-11 07:09:40 +04:00
|
|
|
return(0); /* keep compiler happy */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// static IO port write callback handler
|
|
|
|
// redirects to non-static class handler to avoid virtual functions
|
|
|
|
|
|
|
|
void
|
2001-07-02 00:49:46 +04:00
|
|
|
bx_pit_c::write_handler(void *this_ptr, Bit32u address, Bit32u dvalue, unsigned io_len)
|
2001-06-11 07:09:40 +04:00
|
|
|
{
|
|
|
|
#if !BX_USE_PIT_SMF
|
2001-07-02 00:49:46 +04:00
|
|
|
bx_pit_c *class_ptr = (bx_pit_c *) this_ptr;
|
2001-06-11 07:09:40 +04:00
|
|
|
|
|
|
|
class_ptr->write(address, dvalue, io_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-07-02 00:49:46 +04:00
|
|
|
bx_pit_c::write( Bit32u address, Bit32u dvalue,
|
2001-06-11 07:09:40 +04:00
|
|
|
unsigned int io_len )
|
|
|
|
{
|
|
|
|
#else
|
|
|
|
UNUSED(this_ptr);
|
|
|
|
#endif // !BX_USE_PIT_SMF
|
|
|
|
Bit8u value;
|
2001-08-18 07:28:23 +04:00
|
|
|
Bit64u time_passed = bx_pc_system.time_usec()-BX_PIT_THIS s.last_usec;
|
|
|
|
Bit32u time_passed32 = time_passed;
|
|
|
|
|
|
|
|
BX_DEBUG(("pit: entering write handler"));
|
|
|
|
|
2002-01-31 20:18:38 +03:00
|
|
|
if(time_passed32) {
|
|
|
|
periodic(time_passed32);
|
2001-08-18 07:28:23 +04:00
|
|
|
}
|
|
|
|
BX_PIT_THIS s.last_usec=BX_PIT_THIS s.last_usec + time_passed;
|
2001-06-11 07:09:40 +04:00
|
|
|
|
|
|
|
value = (Bit8u ) dvalue;
|
|
|
|
|
|
|
|
if (io_len > 1)
|
|
|
|
BX_PANIC(("pit: io write to port %04x, len=%u", (unsigned) address,
|
|
|
|
(unsigned) io_len));
|
|
|
|
|
|
|
|
if (bx_dbg.pit)
|
|
|
|
BX_INFO(("pit: write to port %04x = %02x",
|
|
|
|
(unsigned) address, (unsigned) value));
|
|
|
|
|
|
|
|
switch (address) {
|
|
|
|
case 0x40: /* timer 0: write count register */
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.timer.write(0,value);
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x41: /* timer 1: write count register */
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.timer.write( 1,value );
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x42: /* timer 2: write count register */
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.timer.write( 2,value );
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x43: /* timer 0-2 mode control */
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.timer.write( 3,value );
|
2001-08-18 07:28:23 +04:00
|
|
|
break;
|
2001-06-11 07:09:40 +04:00
|
|
|
|
|
|
|
case 0x61:
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.speaker_data_on = (value >> 1) & 0x01;
|
2001-06-11 07:09:40 +04:00
|
|
|
/*??? only on AT+ */
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.timer.set_GATE(2, value & 0x01);
|
2001-06-11 07:09:40 +04:00
|
|
|
#if BX_CPU_LEVEL < 2
|
|
|
|
/* ??? XT: */
|
|
|
|
bx_kbd_port61h_write(value);
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
BX_PANIC(("pit: unsupported io write to port %04x = %02x",
|
|
|
|
(unsigned) address, (unsigned) value));
|
2001-08-18 07:28:23 +04:00
|
|
|
}
|
|
|
|
|
2002-01-31 20:18:38 +03:00
|
|
|
if ((BX_PIT_THIS s.timer.read_OUT(0))==1) {
|
|
|
|
bx_pic.raise_irq(0);
|
|
|
|
} else {
|
|
|
|
bx_pic.lower_irq(0);
|
|
|
|
}
|
|
|
|
|
2001-08-18 07:28:23 +04:00
|
|
|
if(time_passed ||
|
|
|
|
(BX_PIT_THIS s.last_next_event_time
|
|
|
|
!= BX_PIT_THIS s.timer.get_next_event_time())
|
|
|
|
) {
|
|
|
|
BX_DEBUG(("pit: RESETting timer."));
|
|
|
|
bx_pc_system.deactivate_timer(BX_PIT_THIS s.timer_handle[0]);
|
|
|
|
BX_DEBUG(("deactivated timer."));
|
|
|
|
if(BX_PIT_THIS s.timer.get_next_event_time()) {
|
|
|
|
bx_pc_system.activate_timer(BX_PIT_THIS s.timer_handle[0],
|
2001-10-11 17:01:27 +04:00
|
|
|
MAX(1,TICKS_TO_USEC(BX_PIT_THIS s.timer.get_next_event_time())),
|
2001-08-18 07:28:23 +04:00
|
|
|
0);
|
|
|
|
BX_DEBUG(("activated timer."));
|
2001-06-11 07:09:40 +04:00
|
|
|
}
|
2001-08-18 07:28:23 +04:00
|
|
|
BX_PIT_THIS s.last_next_event_time = BX_PIT_THIS s.timer.get_next_event_time();
|
|
|
|
}
|
|
|
|
BX_DEBUG(("s.last_usec=%d",BX_PIT_THIS s.last_usec));
|
|
|
|
BX_DEBUG(("s.timer_id=%d",BX_PIT_THIS s.timer_handle[0]));
|
|
|
|
BX_DEBUG(("s.timer.get_next_event_time=%x",BX_PIT_THIS s.timer.get_next_event_time()));
|
|
|
|
BX_DEBUG(("s.last_next_event_time=%d",BX_PIT_THIS s.last_next_event_time));
|
|
|
|
|
2001-06-11 07:09:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2001-07-02 00:49:46 +04:00
|
|
|
bx_pit_c::SaveState( class state_file *fd )
|
2001-06-11 07:09:40 +04:00
|
|
|
{
|
|
|
|
fd->write_check ("8254 start");
|
2001-07-02 00:49:46 +04:00
|
|
|
fd->write (&BX_PIT_THIS s, sizeof (BX_PIT_THIS s));
|
2001-06-11 07:09:40 +04:00
|
|
|
fd->write_check ("8254 end");
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
2001-07-02 00:49:46 +04:00
|
|
|
bx_pit_c::LoadState( class state_file *fd )
|
2001-06-11 07:09:40 +04:00
|
|
|
{
|
|
|
|
fd->read_check ("8254 start");
|
2001-07-02 00:49:46 +04:00
|
|
|
fd->read (&BX_PIT_THIS s, sizeof (BX_PIT_THIS s));
|
2001-06-11 07:09:40 +04:00
|
|
|
fd->read_check ("8254 end");
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
void
|
|
|
|
bx_kbd_port61h_write(Bit8u value)
|
|
|
|
{
|
|
|
|
// PcError("KBD_PORT61H_WRITE(): not implemented yet");
|
|
|
|
UNUSED( value );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
Boolean
|
2001-07-02 00:49:46 +04:00
|
|
|
bx_pit_c::periodic( Bit32u usec_delta )
|
2001-06-11 07:09:40 +04:00
|
|
|
{
|
2001-07-02 00:49:46 +04:00
|
|
|
Boolean prev_timer0_out = BX_PIT_THIS s.timer.read_OUT(0);
|
2001-06-11 07:09:40 +04:00
|
|
|
Boolean want_interrupt = 0;
|
2002-01-31 20:18:38 +03:00
|
|
|
Bit32u ticks_delta = 0;
|
|
|
|
|
2002-02-08 00:22:55 +03:00
|
|
|
#if BX_USE_REALTIME_PIT
|
2002-02-09 01:27:51 +03:00
|
|
|
if(BX_PIT_THIS s.total_ticks < (MAX_MULT_CEILING*TICKS_PER_SECOND + (TICKS_PER_SECOND * BX_PIT_THIS s.total_sec))) {
|
2002-02-08 08:18:14 +03:00
|
|
|
ticks_delta=(Bit32u)(USEC_TO_TICKS(usec_delta));
|
|
|
|
BX_PIT_THIS s.total_ticks += ticks_delta;
|
|
|
|
}
|
2002-02-08 00:22:55 +03:00
|
|
|
second_update_data();
|
|
|
|
#else
|
2002-01-31 20:18:38 +03:00
|
|
|
BX_PIT_THIS s.total_usec += usec_delta;
|
|
|
|
ticks_delta=(Bit32u)((USEC_TO_TICKS((Bit64u)(BX_PIT_THIS s.total_usec)))-BX_PIT_THIS s.total_ticks);
|
|
|
|
BX_PIT_THIS s.total_ticks += ticks_delta;
|
|
|
|
|
|
|
|
while ((BX_PIT_THIS s.total_ticks >= 1193181) && (BX_PIT_THIS s.total_usec >= 1000000)) {
|
|
|
|
BX_PIT_THIS s.total_ticks -= 1193181;
|
|
|
|
BX_PIT_THIS s.total_usec -= 1000000;
|
|
|
|
}
|
2002-02-08 00:22:55 +03:00
|
|
|
#endif
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2001-10-11 17:01:27 +04:00
|
|
|
while(ticks_delta>0) {
|
2001-07-02 00:49:46 +04:00
|
|
|
Bit32u maxchange=BX_PIT_THIS s.timer.get_next_event_time();
|
2001-06-28 02:25:24 +04:00
|
|
|
Bit32u timedelta=maxchange;
|
2001-10-11 17:01:27 +04:00
|
|
|
if((maxchange==0) || (maxchange>ticks_delta)) {
|
|
|
|
timedelta=ticks_delta;
|
2001-06-28 02:25:24 +04:00
|
|
|
}
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.timer.clock_all(timedelta);
|
2002-01-31 20:18:38 +03:00
|
|
|
if ( (prev_timer0_out==0) ) {
|
2002-01-31 20:26:26 +03:00
|
|
|
if ((BX_PIT_THIS s.timer.read_OUT(0))==1) {
|
2002-01-31 20:18:38 +03:00
|
|
|
bx_pic.raise_irq(0);
|
2002-01-31 20:26:26 +03:00
|
|
|
prev_timer0_out=1;
|
|
|
|
}
|
2002-01-31 20:18:38 +03:00
|
|
|
} else {
|
2002-01-31 20:26:26 +03:00
|
|
|
if ((BX_PIT_THIS s.timer.read_OUT(0))==0) {
|
2002-01-31 20:18:38 +03:00
|
|
|
bx_pic.lower_irq(0);
|
2002-01-31 20:26:26 +03:00
|
|
|
prev_timer0_out=0;
|
|
|
|
}
|
2001-06-11 07:09:40 +04:00
|
|
|
}
|
2001-07-02 00:49:46 +04:00
|
|
|
prev_timer0_out=BX_PIT_THIS s.timer.read_OUT(0);
|
2001-10-11 17:01:27 +04:00
|
|
|
ticks_delta-=timedelta;
|
2001-06-11 07:09:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return(want_interrupt);
|
|
|
|
}
|
2001-07-02 00:49:46 +04:00
|
|
|
|
2002-02-08 00:22:55 +03:00
|
|
|
|
|
|
|
#if BX_USE_REALTIME_PIT
|
|
|
|
void
|
|
|
|
bx_pit_c::second_update_data(void) {
|
|
|
|
Bit64u timediff=(time(NULL)*TIME_MULTIPLIER/TIME_DIVIDER)-BX_PIT_THIS s.last_time;
|
|
|
|
BX_PIT_THIS s.last_time += timediff;
|
|
|
|
if(timediff) {
|
|
|
|
Bit64s tickstemp;
|
|
|
|
|
|
|
|
BX_PIT_THIS s.total_sec += timediff;
|
|
|
|
|
2002-02-09 01:27:51 +03:00
|
|
|
#if DEBUG_REALTIME_WITH_PRINTF
|
|
|
|
printf("timediff: %lld, total_sec: %lld, total_ticks: %lld\n",timediff, BX_PIT_THIS s.total_sec, BX_PIT_THIS s.total_ticks);
|
|
|
|
#endif
|
2002-02-08 00:22:55 +03:00
|
|
|
|
|
|
|
tickstemp =
|
|
|
|
(((BX_PIT_THIS s.total_sec)*TICKS_PER_SECOND)-BX_PIT_THIS s.total_ticks)
|
|
|
|
+ TICKS_PER_SECOND;
|
|
|
|
|
|
|
|
// while((BX_PIT_THIS s.total_sec >= 0) && (BX_PIT_THIS s.total_ticks >= TICKS_PER_SECOND)) {
|
|
|
|
// BX_PIT_THIS s.total_sec -= 1;
|
|
|
|
// BX_PIT_THIS s.total_ticks -= TICKS_PER_SECOND;
|
|
|
|
// }
|
|
|
|
|
2002-02-08 08:18:14 +03:00
|
|
|
if(tickstemp > (TICKS_PER_SECOND*MAX_MULT)) {
|
2002-02-09 01:27:51 +03:00
|
|
|
#if DEBUG_REALTIME_WITH_PRINTF
|
|
|
|
printf("Running WAY too slow. tps:%lld\n",tickstemp);
|
|
|
|
#endif
|
2002-02-08 08:18:14 +03:00
|
|
|
tickstemp = TICKS_PER_SECOND*MAX_MULT_CEILING;
|
2002-02-09 01:27:51 +03:00
|
|
|
#if DEBUG_REALTIME_WITH_PRINTF
|
|
|
|
printf("..................... tps:%lld\n",tickstemp);
|
|
|
|
#endif
|
2002-02-08 08:18:14 +03:00
|
|
|
} else if(tickstemp < (TICKS_PER_SECOND*MIN_MULT)) {
|
2002-02-09 01:27:51 +03:00
|
|
|
#if DEBUG_REALTIME_WITH_PRINTF
|
|
|
|
printf("Running WAY too fast. tps:%lld\n",tickstemp);
|
|
|
|
#endif
|
2002-02-08 08:18:14 +03:00
|
|
|
tickstemp = TICKS_PER_SECOND*MIN_MULT_FLOOR;
|
2002-02-09 01:27:51 +03:00
|
|
|
#if DEBUG_REALTIME_WITH_PRINTF
|
|
|
|
printf("..................... tps:%lld\n",tickstemp);
|
|
|
|
#endif
|
2002-02-08 00:22:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BX_PIT_THIS s.ticks_per_second = tickstemp;
|
|
|
|
|
|
|
|
BX_PIT_THIS s.usec_per_second = ((bx_pc_system.time_usec()-BX_PIT_THIS s.last_sec_usec)/timediff)*USEC_ALPHA_B + USEC_ALPHA*BX_PIT_THIS s.usec_per_second;
|
|
|
|
BX_PIT_THIS s.last_sec_usec = bx_pc_system.time_usec();
|
2002-02-09 01:27:51 +03:00
|
|
|
#if DEBUG_REALTIME_WITH_PRINTF
|
|
|
|
printf("Parms: ticks_per_second=%lld, usec_per_second=%lld\n",BX_PIT_THIS s.ticks_per_second, BX_PIT_THIS s.usec_per_second);
|
|
|
|
#endif
|
2002-02-08 00:22:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // #if BX_USE_REALTIME_PIT
|
2001-08-16 00:23:58 +04:00
|
|
|
#endif // #if BX_USE_NEW_PIT
|