summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/libndsfpc/src/nds/timers.inc
blob: ae0a13403ca0c1c2f5a6dd2b952959a892443349 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
{$ifdef NDS_INTERFACE}

const
  TIMER0_CR   : pcuint16 = pointer($04000102);
  TIMER1_CR   : pcuint16 = pointer($04000106);
  TIMER2_CR   : pcuint16 = pointer($0400010A);
  TIMER3_CR   : pcuint16 = pointer($0400010E);
function TIMER_CR(n: cint): pcuint16; inline;

const
  TIMER0_DATA    : pcuint16 = pointer($04000100);
  TIMER1_DATA    : pcuint16 = pointer($04000104);
  TIMER2_DATA    : pcuint16 = pointer($04000108);
  TIMER3_DATA    : pcuint16 = pointer($0400010C);
function TIMER_DATA(n: cuint): pcuint16; inline;

const
  BUS_CLOCK = 33513982;

const
  TIMER_ENABLE    = (1 shl 7);
  TIMER_IRQ_REQ   = (1 shl 6);
  TIMER_CASCADE   = (1 shl 2);


type
  ClockDivider = cint;
const
  ClockDivider_1   : ClockDivider = 0;
  ClockDivider_64  : ClockDivider = 1;
  ClockDivider_256 : ClockDivider = 2;
  ClockDivider_1024: ClockDivider = 3;

const
  TIMER_DIV_1     = (0);
  TIMER_DIV_64    = (1);
  TIMER_DIV_256   = (2);
  TIMER_DIV_1024  = (3);

function TIMER_FREQ(n: cint): cint; inline;
function TIMER_FREQ_64(n: cint): cint; inline;
function TIMER_FREQ_256(n: cint): cint; inline;
function TIMER_FREQ_1024(n: cint): cint; inline; 


procedure timerStart(channel: cint; divider: ClockDivider; ticks: cuint16; callback: fp); cdecl; external; 
function timerElapsed(channel: cint): cuint16; cdecl; external;

function timerTick(timer: cuint): cuint16; inline; 
function timerPause(timer: cuint): cuint16; cdecl; external;
procedure timerUnpause(timer: cuint); inline;

function timerStop(channel: cint): cuint16; cdecl; external;
procedure cpuStartTiming(timer: cuint32); cdecl; external;
function cpuGetTiming(): cuint32; cdecl; external;
function cpuEndTiming(): cuint32; cdecl; external;

function timerTicks2usec(ticks: cuint32): cuint32; inline;
function timerTicks2msec(ticks: cuint32): cuint32; inline;

function timerFreqToTicks_1(freq: cint): cuint16; inline;
function timerFreqToTicks_64(freq: cint): cuint16; inline;
function timerFreqToTicks_256(freq: cint): cuint16; inline;
function timerFreqToTicks_1024(freq: cint): cuint16; inline;

{$endif NDS_INTERFACE}



{$ifdef NDS_IMPLEMENTATION}

function TIMER_FREQ(n: cint): cint; inline;
begin
  TIMER_FREQ := cint(-BUS_CLOCK div (n));
end;
	
function TIMER_FREQ_64(n: cint): cint; inline;
begin
  TIMER_FREQ_64 := cint(-(BUS_CLOCK shr 6) div (n));
end;
	
function TIMER_FREQ_256(n: cint): cint; inline;
begin
  TIMER_FREQ_256 := cint(-(BUS_CLOCK shr 8) div (n));
end;

function TIMER_FREQ_1024(n: cint): cint; inline; 
begin
  TIMER_FREQ_1024 := cint(-(BUS_CLOCK shr 10) div (n));
end;

function TIMER_DATA(n: cuint): pcuint16; inline;
begin
  TIMER_DATA := pcuint16($04000100 + (n shl 2));
end;

function TIMER_CR(n: cint): pcuint16; inline;
begin
  TIMER_CR := pcuint16($04000102 + (n shl 2));
end;

function timerTick(timer: cuint): cuint16; inline; 
begin
	timerTick := TIMER_DATA(timer)^;
end;

procedure timerUnpause(timer: cuint); inline;
begin
	TIMER_CR(timer)^ := TIMER_CR(timer)^ or TIMER_ENABLE;
end;

function timerFreqToTicks_1(freq: cint): cuint16; inline;
begin
  result := -BUS_CLOCK div freq;
end;

function timerFreqToTicks_64(freq: cint): cuint16; inline;
begin
  result := (-BUS_CLOCK shr 6) div freq;
end;

function timerFreqToTicks_256(freq: cint): cuint16; inline;
begin
  result := (-BUS_CLOCK shr 8) div freq;
end;

function timerFreqToTicks_1024(freq: cint): cuint16; inline;
begin
  result := (-BUS_CLOCK shr 10) div freq;
end;

function timerTicks2usec(ticks: cuint32): cuint32; inline;
begin
  timerTicks2usec := (cuint64(ticks) * 1000000) div BUS_CLOCK;
end;

function timerTicks2msec(ticks: cuint32): cuint32; inline;
begin
  timerTicks2msec := (cuint64(ticks)*1000) div BUS_CLOCK;
end;

{$endif NDS_IMPLEMENTATION}