2004-02-02 02:42:04 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
2011-02-25 01:05:47 +03:00
|
|
|
// $Id$
|
2001-10-03 17:10:38 +04:00
|
|
|
/////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2017-01-28 12:52:09 +03:00
|
|
|
// Copyright (C) 2001-2017 The Bochs Project
|
2001-06-11 07:09:40 +04:00
|
|
|
//
|
|
|
|
// 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
|
2009-02-08 12:05:52 +03:00
|
|
|
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2009-01-11 21:46:01 +03:00
|
|
|
// Define BX_PLUGGABLE in files that can be compiled into plugins. For
|
|
|
|
// platforms that require a special tag on exported symbols, BX_PLUGGABLE
|
|
|
|
// is used to know when we are exporting symbols and when we are importing.
|
|
|
|
#define BX_PLUGGABLE
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2004-06-19 19:20:15 +04:00
|
|
|
#include "iodev.h"
|
2012-01-15 12:50:20 +04:00
|
|
|
#include "pit.h"
|
2008-12-29 23:16:08 +03:00
|
|
|
#include "virt_timer.h"
|
2004-09-05 14:30:19 +04:00
|
|
|
#include "speaker.h"
|
2002-10-31 02:54:29 +03:00
|
|
|
|
2009-01-11 21:46:01 +03:00
|
|
|
|
|
|
|
#define LOG_THIS thePit->
|
|
|
|
|
|
|
|
bx_pit_c *thePit = NULL;
|
|
|
|
|
2017-01-28 12:52:09 +03:00
|
|
|
int CDECL libpit_LTX_plugin_init(plugin_t *plugin, plugintype_t type)
|
2009-01-11 21:46:01 +03:00
|
|
|
{
|
2011-12-25 12:52:34 +04:00
|
|
|
if (type == PLUGTYPE_CORE) {
|
|
|
|
thePit = new bx_pit_c();
|
2017-04-15 23:31:07 +03:00
|
|
|
bx_devices.pluginPitDevice = thePit;
|
2011-12-25 12:52:34 +04:00
|
|
|
BX_REGISTER_DEVICE_DEVMODEL(plugin, type, thePit, BX_PLUGIN_PIT);
|
|
|
|
return 0; // Success
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
2009-01-11 21:46:01 +03:00
|
|
|
}
|
|
|
|
|
2014-06-08 12:40:08 +04:00
|
|
|
void CDECL libpit_LTX_plugin_fini(void)
|
2009-01-11 21:46:01 +03:00
|
|
|
{
|
|
|
|
delete thePit;
|
|
|
|
}
|
|
|
|
|
2002-10-31 02:54:29 +03:00
|
|
|
//Important constant #defines:
|
|
|
|
#define USEC_PER_SECOND (1000000)
|
|
|
|
//1.193181MHz Clock
|
|
|
|
#define TICKS_PER_SECOND (1193181)
|
|
|
|
|
|
|
|
|
2002-12-05 20:43:00 +03:00
|
|
|
// define a macro to convert floating point numbers into 64-bit integers.
|
|
|
|
// In MSVC++ you can convert a 64-bit float into a 64-bit signed integer,
|
|
|
|
// but it will not convert a 64-bit float into a 64-bit unsigned integer.
|
|
|
|
// This macro works around that.
|
|
|
|
#define F2I(x) ((Bit64u)(Bit64s) (x))
|
|
|
|
#define I2F(x) ((double)(Bit64s) (x))
|
|
|
|
|
2002-09-21 03:10:55 +04:00
|
|
|
|
2002-02-08 00:22:55 +03:00
|
|
|
//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-05-21 01:53:26 +04:00
|
|
|
#define USEC_ALPHA2 ((double)(.5))
|
|
|
|
#define USEC_ALPHA2_B ((double)(((double)1)-USEC_ALPHA2))
|
2002-12-05 20:43:00 +03:00
|
|
|
#define ALPHA_LOWER(old,new) ((Bit64u)((old<new)?((USEC_ALPHA*(I2F(old)))+(USEC_ALPHA_B*(I2F(new)))):((USEC_ALPHA2*(I2F(old)))+(USEC_ALPHA2_B*(I2F(new))))))
|
2002-02-08 00:22:55 +03:00
|
|
|
|
2002-10-31 02:54:29 +03:00
|
|
|
|
|
|
|
//PIT tick to usec conversion functions:
|
|
|
|
//Direct conversions:
|
2008-02-16 01:05:43 +03:00
|
|
|
#define TICKS_TO_USEC(a) (((a)*USEC_PER_SECOND)/TICKS_PER_SECOND)
|
|
|
|
#define USEC_TO_TICKS(a) (((a)*TICKS_PER_SECOND)/USEC_PER_SECOND)
|
2002-10-31 02:54:29 +03:00
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
bx_pit_c::bx_pit_c()
|
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
|
|
|
|
|
|
|
/* 8254 PIT (Programmable Interval Timer) */
|
|
|
|
|
2009-01-11 21:46:01 +03:00
|
|
|
s.timer_handle[1] = BX_NULL_TIMER_HANDLE;
|
|
|
|
s.timer_handle[2] = BX_NULL_TIMER_HANDLE;
|
|
|
|
s.timer_handle[0] = BX_NULL_TIMER_HANDLE;
|
2001-06-11 07:09:40 +04:00
|
|
|
}
|
|
|
|
|
2012-08-19 12:16:20 +04:00
|
|
|
bx_pit_c::~bx_pit_c()
|
|
|
|
{
|
|
|
|
SIM->get_bochs_root()->remove("pit");
|
|
|
|
BX_DEBUG(("Exit"));
|
|
|
|
}
|
|
|
|
|
2009-01-11 21:46:01 +03:00
|
|
|
void bx_pit_c::init(void)
|
2001-06-11 07:09:40 +04:00
|
|
|
{
|
2014-10-19 12:54:16 +04:00
|
|
|
int clock_mode = SIM->get_param_enum(BXPN_CLOCK_SYNC)->get();
|
|
|
|
BX_PIT_THIS is_realtime = (clock_mode == BX_CLOCK_SYNC_REALTIME) ||
|
|
|
|
(clock_mode == BX_CLOCK_SYNC_BOTH);
|
|
|
|
|
2003-07-31 16:04:48 +04:00
|
|
|
DEV_register_irq(0, "8254 PIT");
|
2017-04-15 23:31:07 +03:00
|
|
|
BX_PIT_THIS s.irq_enabled = 1;
|
2003-07-31 16:04:48 +04:00
|
|
|
DEV_register_ioread_handler(this, read_handler, 0x0040, "8254 PIT", 1);
|
|
|
|
DEV_register_ioread_handler(this, read_handler, 0x0041, "8254 PIT", 1);
|
|
|
|
DEV_register_ioread_handler(this, read_handler, 0x0042, "8254 PIT", 1);
|
|
|
|
DEV_register_ioread_handler(this, read_handler, 0x0043, "8254 PIT", 1);
|
|
|
|
DEV_register_ioread_handler(this, read_handler, 0x0061, "8254 PIT", 1);
|
|
|
|
|
|
|
|
DEV_register_iowrite_handler(this, write_handler, 0x0040, "8254 PIT", 1);
|
|
|
|
DEV_register_iowrite_handler(this, write_handler, 0x0041, "8254 PIT", 1);
|
|
|
|
DEV_register_iowrite_handler(this, write_handler, 0x0042, "8254 PIT", 1);
|
|
|
|
DEV_register_iowrite_handler(this, write_handler, 0x0043, "8254 PIT", 1);
|
|
|
|
DEV_register_iowrite_handler(this, write_handler, 0x0061, "8254 PIT", 1);
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2009-04-11 11:04:52 +04:00
|
|
|
BX_DEBUG(("starting init"));
|
2001-08-18 07:28:23 +04:00
|
|
|
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.speaker_data_on = 0;
|
2012-05-03 23:45:06 +04:00
|
|
|
BX_PIT_THIS s.speaker_active = 0;
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.timer.init();
|
2006-01-08 23:39:08 +03:00
|
|
|
BX_PIT_THIS s.timer.set_OUT_handler(0, irq_handler);
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2014-10-19 12:54:16 +04:00
|
|
|
Bit64u my_time_usec = bx_virt_timer.time_usec(BX_PIT_THIS is_realtime);
|
2003-04-04 01:59:20 +04:00
|
|
|
|
2002-10-06 23:04:47 +04:00
|
|
|
if (BX_PIT_THIS s.timer_handle[0] == BX_NULL_TIMER_HANDLE) {
|
2014-10-19 12:54:16 +04:00
|
|
|
BX_PIT_THIS s.timer_handle[0] = bx_virt_timer.register_timer(this, timer_handler,
|
|
|
|
(unsigned) 100 , 1, 1, BX_PIT_THIS is_realtime, "pit");
|
|
|
|
if (BX_PIT_THIS is_realtime) {
|
|
|
|
BX_INFO(("PIT using realtime synchronisation method"));
|
|
|
|
}
|
2002-10-06 23:04:47 +04:00
|
|
|
}
|
2009-04-11 11:04:52 +04:00
|
|
|
BX_DEBUG(("RESETting timer."));
|
2003-02-18 19:54:48 +03:00
|
|
|
bx_virt_timer.deactivate_timer(BX_PIT_THIS s.timer_handle[0]);
|
2001-08-18 07:28:23 +04:00
|
|
|
BX_DEBUG(("deactivated timer."));
|
2006-09-17 23:19:15 +04:00
|
|
|
if (BX_PIT_THIS s.timer.get_next_event_time()) {
|
2008-01-27 01:24:03 +03:00
|
|
|
bx_virt_timer.activate_timer(BX_PIT_THIS s.timer_handle[0],
|
2009-04-11 11:04:52 +04:00
|
|
|
(Bit32u)BX_MAX(1,TICKS_TO_USEC(BX_PIT_THIS s.timer.get_next_event_time())),
|
|
|
|
0);
|
2001-08-18 07:28:23 +04:00
|
|
|
BX_DEBUG(("activated timer."));
|
|
|
|
}
|
|
|
|
BX_PIT_THIS s.last_next_event_time = BX_PIT_THIS s.timer.get_next_event_time();
|
2011-06-04 10:40:04 +04:00
|
|
|
BX_PIT_THIS s.last_usec = my_time_usec;
|
2001-08-18 07:28:23 +04:00
|
|
|
|
2011-06-04 10:40:04 +04:00
|
|
|
BX_PIT_THIS s.total_ticks = 0;
|
|
|
|
BX_PIT_THIS s.total_usec = 0;
|
2002-02-08 00:22:55 +03:00
|
|
|
|
2009-04-11 11:04:52 +04:00
|
|
|
BX_DEBUG(("finished init"));
|
2001-08-18 07:28:23 +04:00
|
|
|
|
2014-12-01 20:06:00 +03:00
|
|
|
BX_DEBUG(("s.last_usec=" FMT_LL "d",BX_PIT_THIS s.last_usec));
|
2001-08-18 07:28:23 +04:00
|
|
|
BX_DEBUG(("s.timer_id=%d",BX_PIT_THIS s.timer_handle[0]));
|
2011-06-04 10:40:04 +04:00
|
|
|
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));
|
2012-05-01 19:53:28 +04:00
|
|
|
|
|
|
|
#if BX_DEBUGGER
|
|
|
|
// register device for the 'info device' command (calls debug_dump())
|
|
|
|
bx_dbg_register_debug_info("pit", this);
|
|
|
|
#endif
|
2006-09-17 23:19:15 +04:00
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
void bx_pit_c::reset(unsigned type)
|
2002-08-27 23:54:46 +04:00
|
|
|
{
|
2006-01-08 23:39:08 +03:00
|
|
|
BX_PIT_THIS s.timer.reset(type);
|
2002-08-27 23:54:46 +04:00
|
|
|
}
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2006-05-27 19:54:49 +04:00
|
|
|
void bx_pit_c::register_state(void)
|
|
|
|
{
|
2012-02-12 22:43:20 +04:00
|
|
|
bx_list_c *list = new bx_list_c(SIM->get_bochs_root(), "pit", "8254 PIT State");
|
2011-06-04 10:40:04 +04:00
|
|
|
new bx_shadow_bool_c(list, "speaker_data_on", &BX_PIT_THIS s.speaker_data_on);
|
2012-05-03 23:45:06 +04:00
|
|
|
new bx_shadow_bool_c(list, "speaker_active", &BX_PIT_THIS s.speaker_active);
|
2006-05-27 19:54:49 +04:00
|
|
|
new bx_shadow_num_c(list, "last_usec", &BX_PIT_THIS s.last_usec);
|
|
|
|
new bx_shadow_num_c(list, "last_next_event_time", &BX_PIT_THIS s.last_next_event_time);
|
|
|
|
new bx_shadow_num_c(list, "total_ticks", &BX_PIT_THIS s.total_ticks);
|
|
|
|
new bx_shadow_num_c(list, "total_usec", &BX_PIT_THIS s.total_usec);
|
2017-04-15 23:31:07 +03:00
|
|
|
BXRS_PARAM_BOOL(list, irq_enabled, BX_PIT_THIS s.irq_enabled);
|
2012-02-12 22:43:20 +04:00
|
|
|
bx_list_c *counter = new bx_list_c(list, "counter");
|
2006-05-27 19:54:49 +04:00
|
|
|
BX_PIT_THIS s.timer.register_state(counter);
|
|
|
|
}
|
|
|
|
|
2014-05-10 10:50:06 +04:00
|
|
|
void bx_pit_c::after_restore_state(void)
|
|
|
|
{
|
|
|
|
if (BX_PIT_THIS s.speaker_active) {
|
|
|
|
Bit32u value32 = BX_PIT_THIS get_timer(2);
|
|
|
|
if (value32 == 0) value32 = 0x10000;
|
|
|
|
DEV_speaker_beep_on((float)(1193180.0 / value32));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
void bx_pit_c::timer_handler(void *this_ptr)
|
|
|
|
{
|
2001-08-18 07:28:23 +04:00
|
|
|
bx_pit_c * class_ptr = (bx_pit_c *) this_ptr;
|
|
|
|
class_ptr->handle_timer();
|
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
void bx_pit_c::handle_timer()
|
|
|
|
{
|
2014-10-19 12:54:16 +04:00
|
|
|
Bit64u my_time_usec = bx_virt_timer.time_usec(BX_PIT_THIS is_realtime);
|
2003-04-04 01:59:20 +04:00
|
|
|
Bit64u time_passed = my_time_usec-BX_PIT_THIS s.last_usec;
|
2003-06-07 23:16:55 +04:00
|
|
|
Bit32u time_passed32 = (Bit32u)time_passed;
|
2001-08-18 07:28:23 +04:00
|
|
|
|
2009-04-11 11:04:52 +04:00
|
|
|
BX_DEBUG(("entering timer handler"));
|
2001-08-18 07:28:23 +04:00
|
|
|
|
2002-01-31 20:18:38 +03:00
|
|
|
if(time_passed32) {
|
|
|
|
periodic(time_passed32);
|
2001-08-18 07:28:23 +04:00
|
|
|
}
|
2011-06-04 10:40:04 +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())) {
|
2009-04-11 11:04:52 +04:00
|
|
|
BX_DEBUG(("RESETting timer"));
|
2003-02-18 19:54:48 +03:00
|
|
|
bx_virt_timer.deactivate_timer(BX_PIT_THIS s.timer_handle[0]);
|
2008-02-16 01:05:43 +03:00
|
|
|
BX_DEBUG(("deactivated timer"));
|
2001-08-18 07:28:23 +04:00
|
|
|
if(BX_PIT_THIS s.timer.get_next_event_time()) {
|
2008-01-27 01:24:03 +03:00
|
|
|
bx_virt_timer.activate_timer(BX_PIT_THIS s.timer_handle[0],
|
2009-04-11 11:04:52 +04:00
|
|
|
(Bit32u)BX_MAX(1,TICKS_TO_USEC(BX_PIT_THIS s.timer.get_next_event_time())),
|
|
|
|
0);
|
2008-02-16 01:05:43 +03:00
|
|
|
BX_DEBUG(("activated timer"));
|
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();
|
|
|
|
}
|
2014-12-01 20:06:00 +03:00
|
|
|
BX_DEBUG(("s.last_usec=" FMT_LL "d", BX_PIT_THIS s.last_usec));
|
2011-06-04 10:40:04 +04:00
|
|
|
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-08-18 07:28:23 +04:00
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
// static IO port read callback handler
|
|
|
|
// redirects to non-static class handler to avoid virtual functions
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
Bit32u 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;
|
2006-03-08 00:11:20 +03:00
|
|
|
return class_ptr->read(address, io_len);
|
2001-06-11 07:09:40 +04:00
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
Bit32u bx_pit_c::read(Bit32u address, unsigned io_len)
|
2001-06-11 07:09:40 +04:00
|
|
|
{
|
|
|
|
#else
|
|
|
|
UNUSED(this_ptr);
|
|
|
|
#endif // !BX_USE_PIT_SMF
|
2012-05-03 23:45:06 +04:00
|
|
|
bx_bool refresh_clock_div2;
|
2009-04-11 11:04:52 +04:00
|
|
|
Bit8u value = 0;
|
2001-08-18 07:28:23 +04:00
|
|
|
|
|
|
|
handle_timer();
|
|
|
|
|
2001-06-11 07:09:40 +04:00
|
|
|
switch (address) {
|
|
|
|
|
|
|
|
case 0x40: /* timer 0 - system ticks */
|
2009-04-11 11:04:52 +04:00
|
|
|
value = BX_PIT_THIS s.timer.read(0);
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
case 0x41: /* timer 1 read */
|
2009-04-11 11:04:52 +04:00
|
|
|
value = BX_PIT_THIS s.timer.read(1);
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
case 0x42: /* timer 2 read */
|
2009-04-11 11:04:52 +04:00
|
|
|
value = BX_PIT_THIS s.timer.read(2);
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
case 0x43: /* timer 1 read */
|
2009-04-11 11:04:52 +04:00
|
|
|
value = BX_PIT_THIS s.timer.read(3);
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x61:
|
|
|
|
/* AT, port 61h */
|
2014-10-19 12:54:16 +04:00
|
|
|
refresh_clock_div2 = (bx_bool)((bx_virt_timer.time_usec(BX_PIT_THIS is_realtime) / 15) & 1);
|
2011-06-04 10:40:04 +04:00
|
|
|
value = (BX_PIT_THIS s.timer.read_OUT(2) << 5) |
|
2012-05-03 23:45:06 +04:00
|
|
|
(refresh_clock_div2 << 4) |
|
2011-06-04 10:40:04 +04:00
|
|
|
(BX_PIT_THIS s.speaker_data_on << 1) |
|
|
|
|
(BX_PIT_THIS s.timer.read_GATE(2) ? 1 : 0);
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-04-11 11:04:52 +04:00
|
|
|
BX_PANIC(("unsupported io read from port 0x%04x", address));
|
2001-08-18 07:28:23 +04:00
|
|
|
}
|
2009-04-11 11:04:52 +04:00
|
|
|
|
|
|
|
BX_DEBUG(("read from port 0x%04x, value = 0x%02x", address, value));
|
|
|
|
return value;
|
2001-06-11 07:09:40 +04:00
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
// static IO port write callback handler
|
|
|
|
// redirects to non-static class handler to avoid virtual functions
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
void 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);
|
|
|
|
}
|
|
|
|
|
2006-03-08 00:11:20 +03:00
|
|
|
void bx_pit_c::write(Bit32u address, Bit32u dvalue, unsigned io_len)
|
2001-06-11 07:09:40 +04:00
|
|
|
{
|
|
|
|
#else
|
|
|
|
UNUSED(this_ptr);
|
|
|
|
#endif // !BX_USE_PIT_SMF
|
|
|
|
Bit8u value;
|
2014-10-19 12:54:16 +04:00
|
|
|
Bit64u my_time_usec = bx_virt_timer.time_usec(BX_PIT_THIS is_realtime);
|
2003-04-04 01:59:20 +04:00
|
|
|
Bit64u time_passed = my_time_usec-BX_PIT_THIS s.last_usec;
|
2012-05-03 23:45:06 +04:00
|
|
|
Bit32u value32, time_passed32 = (Bit32u)time_passed;
|
|
|
|
bx_bool new_speaker_active;
|
2001-08-18 07:28:23 +04:00
|
|
|
|
2013-06-12 01:04:07 +04:00
|
|
|
if (time_passed32) {
|
2002-01-31 20:18:38 +03:00
|
|
|
periodic(time_passed32);
|
2001-08-18 07:28:23 +04:00
|
|
|
}
|
2011-06-04 10:40:04 +04:00
|
|
|
BX_PIT_THIS s.last_usec = BX_PIT_THIS s.last_usec + time_passed;
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2008-02-16 01:05:43 +03:00
|
|
|
value = (Bit8u) dvalue;
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2009-04-11 11:04:52 +04:00
|
|
|
BX_DEBUG(("write to port 0x%04x, value = 0x%02x", address, value));
|
2001-06-11 07:09:40 +04:00
|
|
|
|
|
|
|
switch (address) {
|
|
|
|
case 0x40: /* timer 0: write count register */
|
2008-02-16 01:05:43 +03: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 */
|
2008-02-16 01:05:43 +03: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 */
|
2008-02-16 01:05:43 +03:00
|
|
|
BX_PIT_THIS s.timer.write(2, value);
|
2013-06-14 23:08:51 +04:00
|
|
|
if (BX_PIT_THIS s.speaker_active && BX_PIT_THIS new_timer_count(2)) {
|
2013-06-12 01:04:07 +04:00
|
|
|
value32 = BX_PIT_THIS get_timer(2);
|
|
|
|
if (value32 == 0) value32 = 0x10000;
|
|
|
|
DEV_speaker_beep_on((float)(1193180.0 / value32));
|
|
|
|
}
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 0x43: /* timer 0-2 mode control */
|
2008-02-16 01:05:43 +03: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:
|
2012-05-03 23:45:06 +04:00
|
|
|
BX_PIT_THIS s.timer.set_GATE(2, value & 0x01);
|
2001-07-02 00:49:46 +04:00
|
|
|
BX_PIT_THIS s.speaker_data_on = (value >> 1) & 0x01;
|
2012-05-03 23:45:06 +04:00
|
|
|
new_speaker_active = ((value & 3) == 3);
|
|
|
|
if (BX_PIT_THIS s.speaker_active != new_speaker_active) {
|
|
|
|
if (new_speaker_active) {
|
|
|
|
value32 = BX_PIT_THIS get_timer(2);
|
|
|
|
if (value32 == 0) value32 = 0x10000;
|
|
|
|
DEV_speaker_beep_on((float)(1193180.0 / value32));
|
|
|
|
} else {
|
|
|
|
DEV_speaker_beep_off();
|
|
|
|
}
|
|
|
|
BX_PIT_THIS s.speaker_active = new_speaker_active;
|
2004-01-16 19:30:46 +03:00
|
|
|
}
|
2001-06-11 07:09:40 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-04-11 11:04:52 +04:00
|
|
|
BX_PANIC(("unsupported io write to port 0x%04x = 0x%02x", address, value));
|
2001-08-18 07:28:23 +04:00
|
|
|
}
|
|
|
|
|
2011-06-04 10:40:04 +04:00
|
|
|
if (time_passed || (BX_PIT_THIS s.last_next_event_time != BX_PIT_THIS s.timer.get_next_event_time())) {
|
2009-04-11 11:04:52 +04:00
|
|
|
BX_DEBUG(("RESETting timer"));
|
2003-02-18 19:54:48 +03:00
|
|
|
bx_virt_timer.deactivate_timer(BX_PIT_THIS s.timer_handle[0]);
|
2008-02-16 01:05:43 +03:00
|
|
|
BX_DEBUG(("deactivated timer"));
|
2001-08-18 07:28:23 +04:00
|
|
|
if(BX_PIT_THIS s.timer.get_next_event_time()) {
|
2008-01-27 01:24:03 +03:00
|
|
|
bx_virt_timer.activate_timer(BX_PIT_THIS s.timer_handle[0],
|
2009-04-11 11:04:52 +04:00
|
|
|
(Bit32u)BX_MAX(1,TICKS_TO_USEC(BX_PIT_THIS s.timer.get_next_event_time())),
|
|
|
|
0);
|
2008-02-16 01:05:43 +03:00
|
|
|
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();
|
|
|
|
}
|
2014-12-01 20:06:00 +03:00
|
|
|
BX_DEBUG(("s.last_usec=" FMT_LL "d", BX_PIT_THIS s.last_usec));
|
2011-06-04 10:40:04 +04:00
|
|
|
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-08-18 07:28:23 +04:00
|
|
|
|
2001-06-11 07:09:40 +04:00
|
|
|
}
|
|
|
|
|
2006-01-08 23:39:08 +03:00
|
|
|
bx_bool bx_pit_c::periodic(Bit32u usec_delta)
|
2001-06-11 07:09:40 +04:00
|
|
|
{
|
2002-01-31 20:18:38 +03:00
|
|
|
Bit32u ticks_delta = 0;
|
|
|
|
|
2003-08-19 04:10:39 +04:00
|
|
|
BX_PIT_THIS s.total_usec += usec_delta;
|
2011-06-04 10:40:04 +04:00
|
|
|
ticks_delta = (Bit32u)((USEC_TO_TICKS((Bit64u)(BX_PIT_THIS s.total_usec)))-BX_PIT_THIS s.total_ticks);
|
2003-08-19 04:10:39 +04:00
|
|
|
BX_PIT_THIS s.total_ticks += ticks_delta;
|
2002-09-21 03:10:55 +04:00
|
|
|
|
2003-08-19 04:10:39 +04:00
|
|
|
while ((BX_PIT_THIS s.total_ticks >= TICKS_PER_SECOND) && (BX_PIT_THIS s.total_usec >= USEC_PER_SECOND)) {
|
|
|
|
BX_PIT_THIS s.total_ticks -= TICKS_PER_SECOND;
|
|
|
|
BX_PIT_THIS s.total_usec -= USEC_PER_SECOND;
|
2002-01-31 20:18:38 +03:00
|
|
|
}
|
2001-06-11 07:09:40 +04:00
|
|
|
|
2001-10-11 17:01:27 +04:00
|
|
|
while(ticks_delta>0) {
|
2011-06-04 10:40:04 +04:00
|
|
|
Bit32u maxchange = BX_PIT_THIS s.timer.get_next_event_time();
|
|
|
|
Bit32u timedelta = maxchange;
|
|
|
|
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);
|
2011-06-04 10:40:04 +04:00
|
|
|
ticks_delta -= timedelta;
|
2001-06-11 07:09:40 +04:00
|
|
|
}
|
|
|
|
|
2006-01-08 23:39:08 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void bx_pit_c::irq_handler(bx_bool value)
|
|
|
|
{
|
2017-04-15 23:31:07 +03:00
|
|
|
if (BX_PIT_THIS s.irq_enabled) {
|
|
|
|
if (value == 1) {
|
|
|
|
DEV_pic_raise_irq(0);
|
|
|
|
} else {
|
|
|
|
DEV_pic_lower_irq(0);
|
|
|
|
}
|
2006-01-08 23:39:08 +03:00
|
|
|
}
|
2001-06-11 07:09:40 +04:00
|
|
|
}
|
2009-01-11 21:46:01 +03:00
|
|
|
|
2012-05-01 19:53:28 +04:00
|
|
|
Bit16u bx_pit_c::get_timer(int Timer)
|
|
|
|
{
|
2009-01-11 21:46:01 +03:00
|
|
|
return BX_PIT_THIS s.timer.get_inlatch(Timer);
|
|
|
|
}
|
2012-05-01 19:53:28 +04:00
|
|
|
|
2013-06-14 23:08:51 +04:00
|
|
|
Bit16u bx_pit_c::new_timer_count(int Timer)
|
|
|
|
{
|
|
|
|
return BX_PIT_THIS s.timer.new_count_ready(Timer);
|
|
|
|
}
|
|
|
|
|
2012-05-01 19:53:28 +04:00
|
|
|
#if BX_DEBUGGER
|
|
|
|
void bx_pit_c::debug_dump(int argc, char **argv)
|
|
|
|
{
|
|
|
|
Bit32u value;
|
|
|
|
int counter = -1;
|
|
|
|
|
|
|
|
dbg_printf("82C54 PIT\n\n");
|
|
|
|
dbg_printf("GATE #2 = %d\n", BX_PIT_THIS s.timer.read_GATE(2));
|
|
|
|
dbg_printf("Speaker = %d\n\n", BX_PIT_THIS s.speaker_data_on);
|
|
|
|
if (argc == 0) {
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
value = BX_PIT_THIS get_timer(i);
|
|
|
|
if (value == 0) value = 0x10000;
|
|
|
|
dbg_printf("counter #%d: freq=%.3f, OUT=%d\n", i, (float)(1193180.0 / value),
|
|
|
|
BX_PIT_THIS s.timer.read_OUT(i));
|
|
|
|
}
|
|
|
|
dbg_printf("\nSupported options:\n");
|
|
|
|
dbg_printf("info device 'pit' 'counter=N' - show status of counter N\n");
|
|
|
|
} else {
|
|
|
|
for (int arg = 0; arg < argc; arg++) {
|
|
|
|
if (!strncmp(argv[arg], "counter=", 8) && isdigit(argv[arg][8])) {
|
|
|
|
counter = atoi(&argv[arg][8]);
|
|
|
|
} else {
|
|
|
|
dbg_printf("\nUnknown option: '%s'\n", argv[arg]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((counter >= 0) && (counter < 3)) {
|
|
|
|
value = BX_PIT_THIS get_timer(counter);
|
|
|
|
if (value == 0) value = 0x10000;
|
|
|
|
dbg_printf("counter #%d: freq=%.3f\n", counter, (float)(1193180.0 / value));
|
|
|
|
BX_PIT_THIS s.timer.print_cnum(counter);
|
|
|
|
} else {
|
|
|
|
dbg_printf("\nInvalid PIT counter number: %d\n", counter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|