-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathAmiga_player.c
executable file
·422 lines (326 loc) · 9.18 KB
/
Amiga_player.c
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/* Copyright 2000 Kjetil S. Matheussen
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/**************** OVERVIEW ***************************************
Handles timing from the AmigaOS. Uses realtime.library for this
purpose, which seems to work very fine. Unfortunately, the
documentation for realtime.library is not the best, so it might
be better ways to do things. But I guess its good enough.
And yes, realtime.library uses CIA for timing (I think..).
*****************************************************************/
#include <dos.h>
#include <dos/dos.h>
#include <proto/exec.h>
#include <clib/alib_protos.h>
#include <proto/realtime.h>
#include <proto/dos.h>
#include <proto/graphics.h>
#include <hardware/cia.h>
#include <hardware/custom.h>
#include "nsmtracker.h"
#include "../config/config.h"
#include "Amiga_player_proc.h"
#include "../common/player_proc.h"
#include "../common/playerclass.h"
#define DONT_USE_UAE_TIMER 1
__far extern struct CIA ciaa;
__far extern struct Custom custom;
STime playertime;
struct Player *player=NULL;
struct Conductor *conductor=NULL;
struct Task *amigatimertask=NULL;
extern struct Task *mytask;
ULONG waitforamigatimersig= -1;
ULONG startamigatimersig=-1;
ULONG pausesig=-1;
ULONG pausedplayingsig=-1;
ULONG startplaysig=-1;
extern LONG ptask2mtasksig;
extern void (*Ptask2MtaskCallBack)(void);
bool waitforamigatimersigokey=true;
extern struct Library *RealTimeBase;
extern char *screenname;
char *playertaskname;
// Try to call PlayerTask 1200 times a second.
#define PLAYERTASKFREQ 1200
struct myHook{
struct Hook hook;
struct Task *task;
ULONG signal;
};
struct myHook *myhook; /* Has to be global because of the GC. */
#ifdef NOPLAYER
void Amiga_endplayer(void){}
void Amiga_initplayer2(void){}
bool Amiga_initplayer1(void){return true;}
void StartPlayer(void){}
void PausePlayer(void){}
void StopPausePlayer(void){}
#else
STime GetCurrentSTime(void){
return (STime)(conductor->cdt_ClockTime*(PFREQ/1200)); // 1200 is a constant from realtime.libary
}
#if DONT_USE_UAE_TIMER
STime lasttime=0;
#endif
extern PlayerClass *pc;
#if DONT_USE_UAE_TIMER
#else
static ULONG (*calltrap3)(ULONG,ULONG) = (ULONG (*)())0xF0FF60;
static ULONG camd_GetTime(ULONG freq)
{
return (*calltrap3) (93,freq);
}
#endif
void __saveds Amiga_timertask(void){
#if DONT_USE_UAE_TIMER
STime newtime;
#endif
ULONG mysignal;
ULONG tempsigbit;
ULONG temp2sigbit;
ULONG temp3sigbit;
startamigatimersig=AllocSignal(-1);
tempsigbit=waitforamigatimersig=AllocSignal(-1);
temp2sigbit=pausesig=AllocSignal(-1);
temp3sigbit=startplaysig=AllocSignal(-1);
if(-1==waitforamigatimersig || -1==startamigatimersig || -1==pausesig || -1==startplaysig){
waitforamigatimersigokey=false;
waitforamigatimersig=0;
return;
}
waitforamigatimersig=1L<<waitforamigatimersig;
pausesig=1L<<pausesig;
startplaysig=1L<<startplaysig;
Wait(1L<<startamigatimersig);
FreeSignal(waitforamigatimersig);
#if DONT_USE_UAE_TIMER
#else
camd_GetTime(PFREQ);
#endif
for(;;){
mysignal=Wait(waitforamigatimersig | SIGBREAKF_CTRL_C | pausesig | startplaysig);
if(mysignal & pausesig){
Pdebug("paused player\n");
Signal(mytask,1L<<pausedplayingsig);
Wait(pausesig);
Pdebug("stopped paused player\n");
continue;
}
if(mysignal & startplaysig){
#if DONT_USE_UAE_TIMER
lasttime=0;
#endif
SetConductorState(player,CONDSTATE_RUNNING,0);
}
#if DONT_USE_UAE_TIMER
newtime=conductor->cdt_ClockTime*(PFREQ/1200); // 1200 is a constant from realtime.libary
PlayerTask(newtime-lasttime);
lasttime=newtime;
#else
PlayerTask(camd_GetTime(PFREQ)/6000);
#endif
if(mysignal & SIGBREAKF_CTRL_C) break;
}
FreeSignal(temp3sigbit);
FreeSignal(tempsigbit);
FreeSignal(temp2sigbit);
}
#if 0
#include <dos/dostags.h>
#include <proto/dos.h>
#define POLL_FREQUENCY 1024 // times a second.
#include <exec/memory.h>
static bool running=true;
static bool putproc_running=true;
static void __saveds PutProc(void){
struct timerequest *TimerIO;
struct MsgPort *TimerMP;
TimerMP=CreateMsgPort();
TimerIO=(struct timerequest *)AllocMem(sizeof(struct timerequest),MEMF_ANY|MEMF_CLEAR|MEMF_PUBLIC);
TimerIO->tr_node.io_Message.mn_Node.ln_Type=NT_MESSAGE;
TimerIO->tr_node.io_Message.mn_ReplyPort=TimerMP;
TimerIO->tr_node.io_Message.mn_Length=sizeof(struct timerequest);
OpenDevice(
TIMERNAME,UNIT_ECLOCK,(struct IORequest *)TimerIO,0L
);
while(running==true){
TimerIO->tr_node.io_Command=TR_ADDREQUEST;
TimerIO->tr_time.tv_secs=0;
TimerIO->tr_time.tv_micro=1000000/POLL_FREQUENCY;
DoIO((struct IORequest *)TimerIO);
Signal(amigatimertask,waitforamigatimersig);
}
CloseDevice((struct IORequest *)TimerIO);
FreeMem(TimerIO,sizeof(struct timerequest));
DeleteMsgPort(TimerMP);
putproc_running=false;
}
#endif
ULONG __asm __saveds timercallback(
register __a0 struct myHook *myhook
){
static STime checkmousebuttons=0;
static bool isboth=false;
checkmousebuttons++;
if(checkmousebuttons==1200){
if( !(ciaa.ciapra&(1<<6)) && !(custom.potinp&(1<<10)) ){
if(isboth==true){
pc->isplaying=false;
isboth=false;
}else{
isboth=true;
}
}else{
isboth=false;
}
checkmousebuttons=0;
}
Signal(myhook->task,myhook->signal);
return 0;
}
void Amiga_endtimertask(void){
struct Task *task;
if(amigatimertask!=NULL){
Signal(amigatimertask,SIGBREAKF_CTRL_C);
}
Delay(10);
for(;;){
Forbid();
task=FindTask(playertaskname);
Permit();
if(task==NULL) break;
Delay(50);
}
if(pausedplayingsig!=-1) FreeSignal(pausedplayingsig);
}
void Amiga_endplayer(void){
#if 0
running=false;
while(putproc_running==true){
Delay(1);
}
#endif
if(player!=NULL){
SetConductorState(player,CONDSTATE_STOPPED,0);
DeletePlayer(player);
}
Amiga_endtimertask();
}
void Amiga_initplayer2(void){
debug("init1\n");
Signal(amigatimertask,1L<<startamigatimersig);
debug("init2, SetConductorState: %x, player:%x\n",RealTimeBase,player);
SetConductorState(player,CONDSTATE_RUNNING,0);
debug("init3\n");
}
extern char *screenname;
bool __saveds Amiga_initplayer1(void){
LONG playererrorcode;
APTR lockhandle;
BOOL ready;
debug("pre2\n");
playertaskname=malloc(500);
sprintf(playertaskname,"Player task for %s\n",screenname);
pausedplayingsig=AllocSignal(-1);
if(pausedplayingsig==-1){
fprintf(stderr,"Could not create pausedplayingsig\n");
return false;
}
myhook=talloc(sizeof(struct myHook));
debug("pre3, CreateTask: %x\n",CreateTask);
amigatimertask=CreateTask(playertaskname,PLAYERPRI,Amiga_timertask,20000L);
debug("pre4\n");
if(amigatimertask==NULL){
fprintf(stderr,"Could not create timer-task.\n");
return false;
}
debug("pre5\n");
myhook->hook.h_Entry=(ULONG(*)())&timercallback;
myhook->task=amigatimertask;
debug("pre6\n");
while(waitforamigatimersig==-1) Delay(20);
debug("pre7\n");
if( ! waitforamigatimersigokey){
fprintf(stderr,"Could not allocate signal(s) in the timer-task.\n");
return false;
}
debug("pre8\n");
myhook->signal=waitforamigatimersig;
debug("pre9\n");
lockhandle=LockRealTime(RT_CONDUCTORS);
player=CreatePlayer(
PLAYER_Name,screenname,
PLAYER_Conductor, screenname,
PLAYER_Hook,&myhook->hook,
PLAYER_Priority, PLAYERPRI*2,
PLAYER_ErrorCode, &playererrorcode,
TAG_DONE
);
if(lockhandle!=NULL) UnlockRealTime(lockhandle);
debug("pre10\n");
if(player==NULL){
fprintf(stderr,"Cant create Player from realtime.library:\n");
switch(playererrorcode){
case RTE_NOMEMORY:
fprintf(stderr,"/* memory allocation failed */\n");
break;
case RTE_NOCONDUCTOR:
fprintf(stderr,"/* player needs a conductor */\n");
break;
case RTE_NOTIMER:
fprintf(stderr,"/* timer (CIA) allocation failed */\n");
break;
case RTE_PLAYING:
fprintf(stderr,"/* can't shuttle while playing */\n");
break;
}
Amiga_endtimertask();
return false;
}
conductor=player->pl_Source;
GetPlayerAttrs(player,PLAYER_Ready,&ready,TAG_DONE);
if(ready==TRUE){
printf("player ready\n");
}else{
printf("player not ready\n");
}
#if 0
CreateNewProcTags(
NP_Entry,PutProc,
NP_Name,"camd_254uae_putproc",
NP_Priority,36,
TAG_END
);
#endif
return true;
}
void StartPlayer(void){
Signal(amigatimertask,startplaysig);
}
void PausePlayer(void){
// printf("starting to pause player\n");
ULONG signal;
Signal(amigatimertask,pausesig);
for(;;){
signal=Wait(1L<<pausedplayingsig | ptask2mtasksig);
if(signal & 1L<<pausedplayingsig) break;
if(signal & ptask2mtasksig){
(*Ptask2MtaskCallBack)();
}
}
// printf("finished to pause player\n");
}
void StopPausePlayer(void){
Signal(amigatimertask,pausesig);
}
#endif