aboutsummaryrefslogtreecommitdiff
path: root/src/timer.c
blob: 870223343fd1cada8e8f18798da37ace8c670ab1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <stdint.h>
#include <go32.h>
#include <dpmi.h>

volatile uint32_t ticks = 0;
volatile uint32_t clock = 0;
volatile uint8_t clock_secs = 0;
volatile uint8_t clock_enabled = 0;
volatile uint8_t *clock_updated = NULL;
volatile void (*user_fn)(void) = NULL;

static _go32_dpmi_seginfo old_handler, new_handler;

static void timer_handler()
{
    ticks++;

    if (clock_enabled)
    {
        clock += 5494;
        if (clock > 100000)
        {
            clock -= 100000;
            if (clock_secs)
            {
                clock_secs--;
                if (clock_updated)
                    *clock_updated = 1;
            }
        }
    }

    if (user_fn)
        user_fn();
}

void timer_init()
{
    _go32_dpmi_get_protected_mode_interrupt_vector(0x1c, &old_handler);
    new_handler.pm_offset = (unsigned long)timer_handler;
    new_handler.pm_selector = _go32_my_cs();
    _go32_dpmi_chain_protected_mode_interrupt_vector(0x1c, &new_handler);
}

void timer_user_fn(void (*fn)(void))
{
    user_fn = fn;
}

void timer_free()
{
    if (_go32_dpmi_set_protected_mode_interrupt_vector(0x1c, &old_handler) == -1)
        fprintf(stderr, "Failed to free the timer :(\n");
}

void timer_start(uint8_t secs, volatile uint8_t *updated)
{
    *updated = 0;
    clock_updated = updated;
    clock_secs = secs;
    clock_enabled = 1;
}

uint8_t timer_value()
{
    *clock_updated = 0;
    return clock_secs;
}

void timer_stop()
{
    clock_enabled = 0;
}

void timer_resume()
{
    clock_enabled = 1;
}