GioMgr.c++
12.3 KB
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* Copyright 1995, Silicon Graphics, Inc.
* All Rights Reserved.
*
* This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
* the contents of this file may not be disclosed to third parties, copied or
* duplicated in any form, in whole or in part, without the prior written
* permission of Silicon Graphics, Inc.
*
* RESTRICTED RIGHTS LEGEND:
* Use, duplication or disclosure by the Government is subject to restrictions
* as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
* and Computer Software clause at DFARS 252.227-7013, and/or in similar or
* successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
* rights reserved under the Copyright Laws of the United States.
*
* $Revision: 1.1.1.1 $
*/
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <bstring.h>
#include <string.h>
#include <getopt.h>
#include <assert.h>
#include <sys/u64gio.h>
#include <PR/libaudio.h>
#include <PR/ramrom.h>
#include <ultrahost.h>
#include "n64.h"
#include "GioMgr.h"
#include "AeConfig.h"
#include "AeAsset.h"
#include "AeDebug.h"
/* created by romcvt in rom.c */
extern int gRomImageSize;
extern char gRomImage[];
extern char verbose;
GioMgr::GioMgr()
{
char nstr[64];
// invalidate variables
fU64fd = -1;
fGioarena = NULL;
fGiosema = NULL;
fCodeEnd = NULL;
fBootRomEnd = NULL;
fRdramEnd = NULL;
// obtain semaphores
sprintf(nstr,"/tmp/ie.%d.key\n",getuid());
fGioarena = usinit(nstr);
if(fGioarena == NULL)
{
perror("failure creating semaphore arena");
return;
}
fGiosema = usnewsema(fGioarena,1);
if(fGiosema == NULL)
{
perror("failure creating semaphore");
return;
}
// open the n64 device for communication
uspsema(fGiosema);
DebugPrint ("debug: n64: open\n");
if ((fU64fd = uhOpenGame(DEV_U64_DATA)) == -1)
{
if(verbose)
{
perror("debug: ie: Unable to open device");
fprintf(stderr,"debug: ie: No dev board located, will continue\n");
}
}
usvsema(fGiosema);
}
GioMgr::~GioMgr()
{
if(fU64fd != -1)
{
if(close(fU64fd) == 0)
{
if (verbose)
fprintf(stdout, "debug: n64: device closed successfully\n");
}
else
perror("failure closing device");
}
if(fGiosema && fGioarena)
usfreesema(fGiosema,fGioarena);
}
//
// This resets the n64 device and boots it with
// application code. It stores memory addresses
// of the code locations so it can know what memory
// is free to use.
//
// Note the device is not closed, but is left open
// as long as the GioMgr exists.
//
AeErr
GioMgr::LoadCode(void)
{
unsigned int romOffset = 0; /* rom will internally offset game to 0x800 */
u64_write_arg_t arg;
AeErr err = kAeNoErr;
assert (fGiosema);
if (fU64fd == -1)
return kAeN64OpenErr;
uspsema(fGiosema);
// Assert RESET
if ( ioctl(fU64fd, U64_RESET, 1) < 0 )
{
perror("error starting U64_RESET ioctl");
err = kAeN64ResetErr;
goto Abort;
}
// Write the contents of the ROM file into ramrom first
if (gRomImageSize > RAMROM_SIZE)
{
fprintf(stdout, "error: n64: codesegment too big. %d bytes\n", gRomImageSize);
err = kAeN64LowMemErr;
goto Abort;
}
// Tell the kernel to copyin our data into ramrom
// 8/12/96 -- value field removed from u64gio struct for new device kernal
// arg.value = 0xFFFFFFFF; /* Inhibits acknowledge cycle with game */
arg.buffer = (void *)gRomImage;
arg.ramrom_addr = romOffset;
arg.nbytes = gRomImageSize;
if ( ioctl(fU64fd, U64_WRITE, &arg) < 0 )
{
perror("failure during U64_WRITE ioctl");
err = kAeN64WriteErr;
goto Abort;
}
// Release RESET; game will subsequently boot itself
if ( ioctl(fU64fd, U64_RESET, 0) < 0 )
{
perror("failure during U64_RESET ioctl");
err = kAeN64ResetErr;
goto Abort;
}
// get the code and memory address locations
DebugPrint ("debug: n64: read codeEnd\n");
readValue((int*)&fCodeEnd);
DebugPrint ("debug: n64: read bootRomEnd\n");
readValue((int*)&fBootRomEnd);
DebugPrint ("debug: n64: read rdramEnd\n");
readValue((int*)&fRdramEnd);
fRdramEnd += 0x80000000; /* convert to virtual address */
fRdramEnd = (u8*)CACHE_ALIGN_DOWN((int)fRdramEnd);
fCodeEnd = (u8*)CACHE_ALIGN_DOWN((int)fCodeEnd);
Abort:
usvsema(fGiosema);
return err;
}
AeErr
GioMgr::InitAudio (AeN64Config *config,
int * pFxParams,
int nFxParams,
u8 **bmemStart,
u8 **bmemEnd,
u8 **smemStart)
{
CommPckt commPckt;
u32 configSize;
u8 * configAddr;
u8 * bankStart;
int heapSize;
int fxSize;
AeErr err = kAeNoErr;
assert (fU64fd != -1);
assert (fGiosema);
assert (fRdramEnd != NULL);
assert (fCodeEnd != NULL);
uspsema(fGiosema);
if (err = initFx (config, pFxParams, nFxParams, &fxSize))
goto Abort;
configSize = CACHE_ALIGN_UP(sizeof(TN64Config));
configAddr = fRdramEnd - (configSize + fxSize);
config->c.audioHeap = (char*)fCodeEnd;
config->c.heapSize = (u32)(configAddr - fCodeEnd);
if (err = writeBlock(configAddr, (u8*)&config->c.audioHeap, sizeof(TN64Config)))
goto Abort;
commPckt.pcktType = SYSEX_TYPE;
commPckt.d.sysr.reqType = INIT_AUDIO;
commPckt.d.sysr.offset = (u32)configAddr;
DebugPrint ("debug: n64: send INIT_AUDIO\n");
if (uhWriteGame(fU64fd,&commPckt,sizeof(CommPckt)) == -1)
{
err = kAeN64WriteErr;
goto Abort;
}
DebugPrint ("debug: n64: read bankStart\n");
readValue ((int*)&bankStart);
bankStart = (u8*)CACHE_ALIGN_UP((int)bankStart);
*bmemStart = bankStart;
*bmemEnd = fRdramEnd;
*smemStart = fBootRomEnd;
if (verbose)
fprintf(stdout, "debug: n64: %d bytes used for audio heap\n",
bankStart - fCodeEnd);
Abort:
usvsema (fGiosema);
return err;
}
AeErr
GioMgr::initFx (AeN64Config * config, int * pFxParams, int nFxParams, int * pFxSize)
{
AeErr err = kAeNoErr;
*pFxSize = 0;
if (nFxParams > 0)
{
assert (pFxParams);
assert (config->c.synConfig.fxType == AL_FX_CUSTOM);
u32 paramSize = CACHE_ALIGN_UP(sizeof(s32) * nFxParams);
u8 * paramAddr = fRdramEnd - paramSize;
config->c.synConfig.params = (s32*)paramAddr;
if (writeBlock(paramAddr, (u8*)pFxParams, paramSize) == -1)
{
fprintf(stdout,"error: n64: failure loading effects params\n");
err = kAeN64WriteErr;
}
*pFxSize = paramSize;
}
return err;
}
AeErr
GioMgr::SetBank(char *bnkfaddr, int bankNum)
{
AeErr err = kAeNoErr;
if(fU64fd != -1)
{
CommPckt commPckt;
commPckt.pcktType = SYSEX_TYPE;
commPckt.d.sysr.reqType = SET_BANK;
commPckt.d.sysr.offset = (u32)bnkfaddr;
commPckt.d.sysr.value = bankNum;
DebugPrint ("debug: n64: send SET_BANK\n");
if (err = writePacket (&commPckt))
perror("sending SetBank sysex request info");
}
return err;
}
AeErr
GioMgr::StartAudio(void)
{
AeErr err = kAeNoErr;
if(fU64fd != -1)
{
CommPckt commPckt;
commPckt.pcktType = SYSEX_TYPE;
commPckt.d.sysr.reqType = START_SEQUENCE;
DebugPrint ("debug: n64: send START_SEQUENCE\n");
if (err = writePacket (&commPckt))
perror("sending StartSequence sysex request info");
}
return err;
}
AeErr
GioMgr::StopAudio(void)
{
AeErr err = kAeNoErr;
if(fU64fd != -1)
{
CommPckt commPckt;
commPckt.pcktType = SYSEX_TYPE;
commPckt.d.sysr.reqType = STOP_SEQUENCE;
DebugPrint ("debug: n64: send STOP_SEQUENCE\n");
if (err = writePacket (&commPckt))
perror("sending StartSequence sysex request info");
}
return err;
}
AeErr
GioMgr::LoadMidi(CommPckt * pCommPckt)
{
AeErr err = kAeNoErr;
if(fU64fd != -1)
{
DebugPrint ("debug: n64: send MIDI\n");
if (err = writePacket (pCommPckt))
perror("sending midi block");
}
return err;
}
AeErr
GioMgr::LoadBlock(u8 *destAddr, u8 *data, u32 size)
{
AeErr err = kAeNoErr;
assert (destAddr != NULL);
assert (data != NULL);
if (fU64fd != -1 && size != 0)
{
CommPckt commPckt;
commPckt.pcktType = SYSEX_TYPE;
commPckt.d.sysr.reqType = WRITE_BLOCK;
commPckt.d.sysr.offset = (u32)destAddr;
commPckt.d.sysr.value = size;
DebugPrint ("debug: n64: write sysex data\n");
if (err = writePacketAndData (&commPckt, data, size))
perror("sending sysex data");
}
return err;
}
AeErr
GioMgr::LoadSamples(u8 *addr,char *data, int numBytes)
{
CommPckt commPckt;
AeErr err = kAeNoErr;
assert (fGiosema);
assert (addr != NULL);
assert (data != NULL);
if (fU64fd != -1 && numBytes != 0)
{
uspsema(fGiosema);
commPckt.pcktType = SYSEX_TYPE;
commPckt.d.sysr.reqType = WRITE_RAMROM;
DebugPrint ("debug: n64: send WRITE_RAMROM\n");
if(uhWriteGame(fU64fd,&commPckt,sizeof(CommPckt)) == -1)
{
perror("sending LoadRamRom sysex request info");
err = kAeN64WriteErr;
goto Abort;
}
DebugPrint ("debug: n64: write ramrom data\n");
if(uhWriteRamrom(fU64fd,addr,data,numBytes) == -1)
{
perror("sending data to ramrom");
err = kAeN64WriteErr;
goto Abort;
}
Abort:
usvsema(fGiosema);
}
return err;
}
AeErr
GioMgr::LoadFXParam(int paramID, int paramVal)
{
AeErr err = kAeNoErr;
if(fU64fd != -1)
{
CommPckt commPckt;
commPckt.pcktType = SYSEX_TYPE;
commPckt.d.sysr.reqType = SET_FX_PARAM;
commPckt.d.sysr.offset = paramID;
commPckt.d.sysr.value = paramVal;
if (verbose)
printf ("debug: n64: write SET_FX_PARAM:%d:%d\n", paramID, paramVal);
if (err = writePacket (&commPckt))
perror("sending LoadFXParam sysex request info");
}
return err;
}
AeErr
GioMgr::LoadFXSectionParams(int paramID, int * params)
{
AeErr err = kAeNoErr;
if(fU64fd != -1 && params)
{
CommPckt commPckt;
commPckt.pcktType = SYSEX_TYPE;
commPckt.d.sysr.reqType = SET_FX_SXN_PARAMS;
commPckt.d.sysr.offset = paramID;
commPckt.d.sysr.value = PARAMS_PER_SECTION;
if (verbose)
DebugPrint ("debug: n64: write SET_FX_SXN_PARAMS\n");
if (err = writePacketAndData (&commPckt, (u8 *)params,
PARAMS_PER_SECTION * sizeof (u32)))
perror("sending LoadFXSectionParams sysex request info");
}
return err;
}
/*****************************************
*
* CAUTION!! Thread should have control of
* semaphore before calling these routines!
*
*****************************************/
AeErr
GioMgr::readValue(int *valPtr)
{
if(fU64fd != -1)
{
if(uhReadGame(fU64fd, valPtr, 4) == -1)
{
perror("unable to read value");
return kAeN64ReadErr;
}
}
return 0;
}
AeErr
GioMgr::writeBlock(u8 *destAddr, u8 *data, u32 size)
{
CommPckt commPckt;
int tmp;
if(fU64fd != -1)
{
commPckt.pcktType = SYSEX_TYPE;
commPckt.d.sysr.reqType = WRITE_BLOCK;
commPckt.d.sysr.offset = (u32)destAddr;
commPckt.d.sysr.value = size;
DebugPrint ("debug: n64: send WRITE_BLOCK\n");
if(uhWriteGame(fU64fd,&commPckt,sizeof(CommPckt)) == -1)
{
perror("sending sysex request info");
return kAeN64WriteErr;
}
DebugPrint ("debug: n64: write sysex data\n");
if(uhWriteGame(fU64fd,data,size) == -1)
{
perror("sending sysex data");
return kAeN64WriteErr;
}
}
return 0;
}
AeErr
GioMgr::writePacket (CommPckt * pPacket)
{
AeErr err = kAeNoErr;
assert (fU64fd != -1);
assert (fGiosema);
uspsema(fGiosema);
if (uhWriteGame (fU64fd, pPacket, sizeof(CommPckt)) == -1)
err = kAeN64WriteErr;
usvsema(fGiosema);
return err;
}
AeErr
GioMgr::writePacketAndData (CommPckt * pPacket, u8 * pData, u32 nData)
{
AeErr err = kAeNoErr;
assert (fU64fd != -1);
assert (fGiosema);
uspsema(fGiosema);
if (uhWriteGame (fU64fd, pPacket, sizeof(CommPckt)) == -1)
{
err = kAeN64WriteErr;
goto Abort;
}
if (uhWriteGame (fU64fd, pData, nData) == -1)
{
err = kAeN64WriteErr;
goto Abort;
}
Abort:
usvsema(fGiosema);
return err;
}