File: amiga.c

package info (click to toggle)
libmikmod 3.3.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,188 kB
  • sloc: ansic: 36,299; sh: 5,013; makefile: 548; javascript: 1
file content (78 lines) | stat: -rw-r--r-- 2,255 bytes parent folder | download | duplicates (5)
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
/* amiga-specific stuff for libmikmod example programs
 *
 * This example is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRENTY; without event the implied warrenty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */

#if defined(__amigaos4__)
#define __USE_INLINE__
#define __USE_OLD_TIMEVAL__
#endif

#include <proto/exec.h>
#include <proto/dos.h>
#include <proto/timer.h>

#include <stdio.h>
#include <stdlib.h>

struct timerequest *timerio;
struct MsgPort     *timerport;
#if defined(__MORPHOS__) || defined(__VBCC__)
struct Library *TimerBase;
#else
struct Device  *TimerBase;
#endif

static void amiga_atexit (void) {
    if (TimerBase) {
        WaitIO((struct IORequest *) timerio);
        CloseDevice((struct IORequest *) timerio);
        DeleteIORequest((struct IORequest *) timerio);
        DeleteMsgPort(timerport);
        TimerBase = NULL;
    }
}

void amiga_sysinit (void) {
    if ((timerport = CreateMsgPort())) {
        if ((timerio = (struct timerequest *)CreateIORequest(timerport, sizeof(struct timerequest)))) {
            if (OpenDevice((STRPTR) TIMERNAME, UNIT_MICROHZ, (struct IORequest *) timerio, 0) == 0) {
#if defined(__MORPHOS__) || defined(__VBCC__)
                TimerBase = (struct Library *)timerio->tr_node.io_Device;
#else
                TimerBase = timerio->tr_node.io_Device;
#endif
            }
            else {
                DeleteIORequest((struct IORequest *)timerio);
                DeleteMsgPort(timerport);
            }
        }
        else {
            DeleteMsgPort(timerport);
        }
    }
    if (!TimerBase) {
        fprintf(stderr, "Can't open timer.device\n");
        exit (-1);
    }

    /* 1us wait, for timer cleanup success */
    timerio->tr_node.io_Command = TR_ADDREQUEST;
    timerio->tr_time.tv_secs = 0;
    timerio->tr_time.tv_micro = 1;
    SendIO((struct IORequest *) timerio);
    WaitIO((struct IORequest *) timerio);

    atexit (amiga_atexit);
}

void amiga_usleep(unsigned long timeout) {
    timerio->tr_node.io_Command = TR_ADDREQUEST;
    timerio->tr_time.tv_secs = timeout / 1000000;
    timerio->tr_time.tv_micro = timeout % 1000000;
    SendIO((struct IORequest *) timerio);
    WaitIO((struct IORequest *) timerio);
}