Files
TeslaRel410/CODE/RP/MUNGA_L4/L4TIME.CPP
T
CydandClaude Fable 5 fdd9ac9d97 Initial import: Tesla Release 4.10 (Tesla:BattleTech & Tesla:Red Planet)
Archival snapshot of the Virtual World Entertainment Tesla cockpit
software, 1994-1996: MUNGA engine and L4 pod layer source (Borland
C++ 5.0), BT/RP game code, and game content (models, audio, maps,
gauges, Division renderer data). Includes third-party libraries:
Division dVS/DPL graphics, HMI SOS audio, WATTCP networking.

Files are preserved byte-for-byte (.gitattributes disables all
line-ending conversion). README.md documents the layout, target
hardware, and toolchain.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-02 13:21:58 -05:00

154 lines
3.7 KiB
C++

#include <mungal4.hpp>
#pragma hdrstop
#if !defined(L4TIME_HPP)
# include <l4time.hpp>
#endif
#include <bios.h>
#include <sos.h>
//#############################################################################
//########################### System Clock ##############################
//#############################################################################
SystemClock
SystemClock::timer;
Scalar
SystemClock::ticksPerSecond;
volatile long fast_time=0L;
WORD timer_handler_handle=0xFFFF;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
Timer_Handler()
{
++fast_time;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
long
SystemClock::GetRTC()
{
if (timer_handler_handle == 0xFFFF)
{
//
//----------------------------------------------------------------------
// Read the BIOS time. If it is less than the value it was last time we
// read it, then we have crossed midnight. All the clock values start
// over again, so we will subtract 24 hours worth of ticks from when we
// started the application, allowing the application to simply see the
// clock continue to get bigger, regardless of the boundary condition
//----------------------------------------------------------------------
//
static long last_time=0L;
long new_time = biostime(0, 0L);
if (new_time < last_time)
{
fast_time -= 1573040L;
}
last_time = new_time;
return new_time - fast_time;
}
else
{
return fast_time;
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
SystemClock::SystemClock()
{
//
//-------------------------------
// Initialize the timer variables
//-------------------------------
//
pauseStart = 0L;
pauseTime = 0L;
//
//------------------------------
// Initialize the platform timer
//------------------------------
//
fast_time = 0L;
char *time_options = getenv(TIMER_ENV);
if (time_options && !strcmp(time_options,USE_SOS_TIMER))
{
#if DEBUG_LEVEL>0
WORD err = sosTIMERInitSystem(_TIMER_DOS_RATE, _SOS_DEBUG_NORMAL);
Verify(err == _ERR_NO_ERROR);
err =
sosTIMERRegisterEvent(28, Timer_Handler, &timer_handler_handle);
Verify(err == _ERR_NO_ERROR);
#else
sosTIMERInitSystem(_TIMER_DOS_RATE, _SOS_DEBUG_NORMAL);
sosTIMERRegisterEvent(28, Timer_Handler, &timer_handler_handle);
#endif
ticksPerSecond = 28.0f;
}
else
{
ticksPerSecond = 1193180.0f / 65536.0f;
fast_time = biostime(0, 0L);
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
SystemClock::Shutdown()
{
if (timer_handler_handle != 0xFFFF)
{
#if DEBUG_LEVEL>0
WORD err = sosTIMERRemoveEvent(timer_handler_handle);
Verify(err == _ERR_NO_ERROR);
err = sosTIMERUnInitSystem(0);
Verify(err == _ERR_NO_ERROR);
#else
sosTIMERRemoveEvent(timer_handler_handle);
sosTIMERUnInitSystem(0);
#endif
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
#define TIMER_CONTROL 0x43
#define GET_TIMER0 0xC2
#define TIMER0_PORT 0x40
#define OUTPUT_BIT 0x80
#undef inportb
#undef outportb
float
Get_Frame_Percent_Used()
{
outportb(TIMER_CONTROL, GET_TIMER0);
int half = inportb(TIMER0_PORT) & OUTPUT_BIT;
int subtick = ((int)inportb(TIMER0_PORT)) >> 1;
subtick += (((int)inportb(TIMER0_PORT)) << 7);
if (timer_handler_handle == 0xFFFF)
{
subtick += half << 8;
return (65535 - subtick) / 65536.0f;
}
else
{
if (half)
{
subtick += _wTIMERValue >> 1;
}
return (_wTIMERValue - 1 - subtick) / (float)_wTIMERValue;
}
}