www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/04/07/18:35:10

From: "Luiz Fernando" <luisoft AT myway DOT com DOT br>
To: <djgpp AT delorie DOT com>
Subject: timer functions
Date: Wed, 7 Apr 1999 19:31:33 -0300
Message-ID: <01be8146$64145320$LocalHost@aguia>
MIME-Version: 1.0
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 4.71.1712.3
X-MimeOLE: Produced By Microsoft MimeOLE V4.71.1712.3
Reply-To: djgpp AT delorie DOT com

This is a multi-part message in MIME format.

------=_NextPart_000_000F_01BE812D.3EC71B20
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hi everybody,

    i have copied some timer functions for a game that im programming... but
it doesnt work well...
    i need some timer functions that works (i dont want allegro ones... i'd
like some simple functions...) for djgpp

    it must have some functions like :
    -timer_init
    -delay (not djgpp ones...)
    - timer_quit

    does anybody here have something to help.
    i've put the file that i've taken in atach... in the game loop it works
for a fwe seconds than it crashes... (the game loop is correct because i
have tested a lot).

thanks

Luiz Fernando
luisoft AT myway DOT com DOT br


------=_NextPart_000_000F_01BE812D.3EC71B20
Content-Type: application/octet-stream;
	name="Gccint8.c"
Content-Transfer-Encoding: quoted-printable
Content-Disposition: attachment;
	filename="Gccint8.c"

#include <dos.h>
#include <dpmi.h>
#include <pc.h>
#include "gccint8.h"

#define IRQ0 0x8
#define PIT0 0x40
#define PIT1 0x41
#define PIT2 0x42
#define PITMODE 0x43
#define PITCONST 1193180L
#define PIT0DEF 18.2067597
#define KBCTRL 0x61
#define NEW8H 1

static float    tick_per_ms =3D 0.0182068;
static float    ms_per_tick =3D 54.9246551;
static float    freq8h =3D 18.2067597;
static unsigned char flag8h =3D 0;
static _go32_dpmi_seginfo rm_old_handler,
                rm_new_handler,
                pm_old_handler,
                pm_new_handler;
static _go32_dpmi_registers r,
                r1;

volatile int    counter_8h;
volatile int    counter_reset;
volatile unsigned long int ticks_8h;

void            init8h(unsigned int Hz)
{
    unsigned int    pit0_set,
                    pit0_value;

    if (flag8h !=3D NEW8H) {
        disable();

        _go32_dpmi_get_protected_mode_interrupt_vector(8, =
&pm_old_handler);
        pm_new_handler.pm_offset =3D (int) pm_new8h;
        pm_new_handler.pm_selector =3D _go32_my_cs();
        _go32_dpmi_chain_protected_mode_interrupt_vector(8, =
&pm_new_handler);

        _go32_dpmi_get_real_mode_interrupt_vector(8, &rm_old_handler);
        rm_new_handler.pm_offset =3D (int) rm_new8h;
        _go32_dpmi_allocate_real_mode_callback_iret(&rm_new_handler, =
&r1);
        _go32_dpmi_set_real_mode_interrupt_vector(8, &rm_new_handler);

        outportb(PITMODE, 0x36);
        pit0_value =3D PITCONST / Hz;
        pit0_set =3D (pit0_value & 0x00ff);
        outportb(PIT0, pit0_set);
        pit0_set =3D (pit0_value >> 8);
        outportb(PIT0, pit0_set);

        enable();

        flag8h =3D NEW8H;
        freq8h =3D Hz;
        counter_8h =3D 0;
        counter_reset =3D freq8h / PIT0DEF;
        tick_per_ms =3D freq8h / 1000;
        ms_per_tick =3D 1000 / freq8h;
    }
}

void            quit8h(void)
{
    unsigned int    pit0_set,
                    pit0_value;
    unsigned long   tick;
    char           *cmostime;

    if (flag8h =3D=3D NEW8H) {

        disable();

        outportb(PITMODE, 0x36);
        outportb(PIT0, 0x00);
        outportb(PIT0, 0x00);

        _go32_dpmi_set_real_mode_interrupt_vector(8, &rm_old_handler);
        _go32_dpmi_set_protected_mode_interrupt_vector(8, =
&pm_old_handler);
        _go32_dpmi_free_real_mode_callback(&rm_new_handler);

        enable();

        cmostime =3D get_cmostime();
        tick =3D PIT0DEF *
            (
             (((float) *cmostime) * 3600) +
             (((float) *(cmostime + 1)) * 60) +
             (((float) *(cmostime + 2)))
            );
        biostime(1, tick);

        flag8h =3D 0;
        freq8h =3D PIT0DEF;
        counter_reset =3D freq8h / PIT0DEF;
        tick_per_ms =3D freq8h / 1000;
        ms_per_tick =3D 1000 / freq8h;
    }
}

void            rm_new8h(void)
{
    disable();
    ticks_8h++;
    counter_8h++;

    if (counter_8h =3D=3D counter_reset) {
        counter_8h =3D 0;
        memset(&r, 0, sizeof(r));
        r.x.cs =3D rm_old_handler.rm_segment;
        r.x.ip =3D rm_old_handler.rm_offset;
        r.x.ss =3D r.x.sp =3D 0;
        enable();
        _go32_dpmi_simulate_fcall_iret(&r);
    } else {
        outportb(0x20, 0x20);
    }
}

void            pm_new8h(void)
{
    disable();
    ticks_8h++;
    counter_8h++;

    if (counter_8h =3D=3D counter_reset) {
        counter_8h =3D 0;
        enable();
    } else {
        outportb(0x20, 0x20);
    }
}

unsigned long   time8h(unsigned long start, unsigned long stop)
{
    unsigned long   duration,
                    millisec;

    if (stop < start)
        return 0;
    else {
        duration =3D stop - start;
        millisec =3D duration * ms_per_tick;
        return millisec;
    }
}

void            delay8h(unsigned int delayms)
{
    unsigned long int delaybegin =3D 0;
    unsigned long int delayend =3D 0;
    unsigned int    delaytick;

    delaytick =3D delayms * tick_per_ms;

    if (flag8h =3D=3D NEW8H)
        delaybegin =3D ticks_8h;
    else
        delaybegin =3D biostime(0, 0);

    do {
        if (flag8h =3D=3D NEW8H)
            delayend =3D ticks_8h;
        else
            delayend =3D biostime(0, 0);
    } while ((delayend - delaybegin) < delaytick);
}

void            sound8h(int freq, int duration)
{
    int             byte;
    unsigned int    freq1;

    freq1 =3D PITCONST / freq;
    outportb(PITMODE, 0xb6);
    byte =3D (freq1 & 0xff);
    outportb(PIT2, byte);
    byte =3D (freq1 >> 8);
    outportb(PIT2, byte);
    byte =3D inportb(KBCTRL);
    outportb(KBCTRL, (byte | 3));

    delay8h(duration);
    outportb(KBCTRL, (byte & 0xfc));
}

char           *get_cmostime(void)
{
    char           *buff;
    static char     buffer[6];
    char            ch;

    buff =3D buffer;
    memset(&r, 0, sizeof(r));
    r.h.ah =3D 0x02;
    _go32_dpmi_simulate_int(0x1a, &r);

    ch =3D r.h.ch;
    buffer[0] =3D (char) ((int) (ch & 0x0f) + (int) ((ch >> 4) & 0x0f) * =
10);
    ch =3D r.h.cl;
    buffer[1] =3D (char) ((int) (ch & 0x0f) + (int) ((ch >> 4) & 0x0f) * =
10);
    ch =3D r.h.dh;
    buffer[2] =3D (char) ((int) (ch & 0x0f) + (int) ((ch >> 4) & 0x0f) * =
10);
    buffer[3] =3D r.h.dl;
    buffer[4] =3D (char) (r.x.flags & 0x0001);
    buffer[5] =3D 0x00;

    return (buff);
}

------=_NextPart_000_000F_01BE812D.3EC71B20--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019