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
/*
 * 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 <sys/u64gio.h>
#include <libaudio.h>
#include <ramrom.h>
#include <ultrahost.h> 
#include "GioMgr.h"
#include "Assets.h"
#include "Mgrs.h"

/* created by romcvt in rom.c */
extern int    gRomImageSize;
extern char   gRomImage[];


GioMgr::GioMgr()
{
    char  nstr[64];
    
    u64fd = 0;
    sprintf(nstr,"/tmp/AudioEditor.%d.key\n",getuid());
    gioarena = usinit(nstr);
    if(gioarena == NULL)
    {
	perror("Failure creating semaphore arena");
	giosema = 0;
    }
    else
    {
	giosema = usnewsema(gioarena,1);
	if(giosema == NULL)
	    perror("Failure creating semaphore");
    }
}

GioMgr::~GioMgr()
{
    if(u64fd != -1 && u64fd != 0)
    {
	if(close(u64fd) == 0)
	    printf("u64 closed successfully\n");
	else
	    perror("Failure closing u64Device\n");
    }

    if(giosema && gioarena)
	usfreesema(giosema,gioarena);
}

int GioMgr::LoadCode(void)
{
    unsigned int    romOffset = 0;      /* rom will internally offset game to 0x800 */
    u64_write_arg_t arg;

    uspsema(giosema);

    if ((u64fd = open(DEV_U64, O_RDWR | O_NOCTTY)) < 0)
    {
        perror("Unable to open /dev/u64");
	usvsema(giosema);
        return(-1);
    } 

    /* Assert RESET  */
    if ( ioctl(u64fd, U64_RESET, 1) < 0 )
    {
        perror("Error starting U64_RESET ioctl");
	usvsema(giosema);
        return(-1);
    } 

    /* Write the contents of the ROM file into ramrom first.   */
    if (gRomImageSize > RAMROM_SIZE)
    {
        fprintf(stderr, "Codesegment too big! %d bytes\n", gRomImageSize);
	usvsema(giosema);
        return(-1);
    }
    /* Tell the kernel to copyin our data into ramrom.   */
    arg.buffer = (void *)gRomImage;
    arg.ramrom_addr = romOffset;
    arg.nbytes = gRomImageSize;
    arg.value = 0xFFFFFFFF; /* Inhibits acknowledge cycle with game */

    if ( ioctl(u64fd, U64_WRITE, &arg) < 0 )
    {
        perror("Error after U64_WRITE ioctl");
	usvsema(giosema);
        return(-1);
    } 

    /* Release RESET; game will subsequently boot itself. */
    if ( ioctl(u64fd, U64_RESET, 0) < 0 )
    {
        perror("Error after U64_RESET ioctl");
	usvsema(giosema);
        return(-1);
    } 

    close(u64fd);
    usvsema(giosema);
    return (0);
}

int GioMgr::InitAudio(AudioConfig *audCnfg, u8 **bmemStart, u8 **bmemEnd, u8 **smemStart)
{
    CommPckt   commPckt;
    s32        *paramBuf;
    u32        paramSize = 0;
    u32        configSize;
    u8         *configAddr;
    u8         *paramAddr;
    u8         *codeEnd;
    u8         *bootRomEnd;
    u8         *rdramEnd;
    u8         *bankStart;
    int        numSections;
    AEReverb   *reverbAsset;
    int        heapSize;

    uspsema(giosema);
    if ((u64fd = uhOpenGame("/dev/u64")) == -1) 
    {
        perror("Unable to open /dev/u64");
	*bmemStart = (u8*)0;      /* set to some workable defaults */
	*bmemEnd = (u8*)0x1000000;
	*smemStart = (u8*)0;
	usvsema(giosema);
        return(-1);
    }

    _ReadValue((int*)&codeEnd);
    _ReadValue((int*)&bootRomEnd);
    _ReadValue((int*)&rdramEnd);

    rdramEnd += 0x80000000;  /* convert to virtual address */
    rdramEnd = (u8*)CACHE_ALIGN_DOWN((int)rdramEnd);
    codeEnd = (u8*)CACHE_ALIGN_DOWN((int)codeEnd);

    /* init the config, write it to rdram,  and send InitAudio message */
    if(audCnfg->synConfig.fxType == AL_FX_CUSTOM)
    {
	reverbAsset = theAssetMgr->GetReverb();
	if(!reverbAsset)
	{
	    printf("Custom reverb has no reverb asset!\n");
	    audCnfg->synConfig.fxType = AL_FX_NONE;
	}
	else
	{
	    paramBuf = reverbAsset->GetParamBuf();
	    if(!paramBuf)
	    {
		printf("Custom reverb has no params!\n");
		audCnfg->synConfig.fxType = AL_FX_NONE;
	    }
	    else
	    {
		numSections = paramBuf[0]; // get the number of reverb sections
		paramSize = sizeof(s32) * ((numSections*PARAMS_PER_SECTION) + 2);
		paramSize = CACHE_ALIGN_UP(paramSize);
		paramAddr = rdramEnd - paramSize;
		audCnfg->synConfig.params = (s32*)paramAddr;
		if(_loadblock(paramAddr, (u8*)paramBuf, paramSize) == -1)
		{
		    fprintf(stderr,"Failure loading FX params\n");
		    usvsema(giosema);
		    return(-1);
		}
	    }
	}
    }

    configSize = CACHE_ALIGN_UP(sizeof(AudioConfig));
    configAddr = rdramEnd - (configSize + paramSize);
    
    audCnfg->audioHeap = (char*)codeEnd;
    audCnfg->heapSize = (u32)(configAddr - codeEnd);

    if(_loadblock(configAddr, (u8*)audCnfg, sizeof(AudioConfig)))
    {
        fprintf(stderr,"Failure loading audio configuration block\n");
	usvsema(giosema);
        return(-1);
    }

    commPckt.pcktType = SYSEX_TYPE;
    commPckt.d.sysr.reqType = INIT_AUDIO;
    commPckt.d.sysr.offset = (u32)configAddr;

    if(uhWriteGame(u64fd,&commPckt,sizeof(CommPckt)) == -1)
    {
        perror("Error sending _StartInit sysex request info");
	usvsema(giosema);
        return(-1);
    }
    
    _ReadValue((int*)&bankStart);

    bankStart = (u8*)CACHE_ALIGN_UP((int)bankStart);
    *bmemStart = bankStart;
    *bmemEnd = rdramEnd;
    *smemStart = bootRomEnd;

    printf("%d bytes used for audio heap\n", bankStart - codeEnd);
    usvsema(giosema);

    return(0);
}

int GioMgr::SetBank(char *bnkfaddr, int bankNum)
{
    CommPckt    commPckt;
    u32   dbg;

    if(u64fd != -1)
    {
        commPckt.pcktType = SYSEX_TYPE;
        commPckt.d.sysr.reqType = SET_BANK;
        commPckt.d.sysr.offset = (u32)bnkfaddr;
        commPckt.d.sysr.value = bankNum;

	uspsema(giosema);
        if(uhWriteGame(u64fd,&commPckt,sizeof(CommPckt)) == -1)
        {
            perror("Error sending SetBank sysex request info");
	    usvsema(giosema);
            return(-1);
        }

	usvsema(giosema);
    }
    return 0;
}

int GioMgr::StartAudio(void)
{
    CommPckt        commPckt;

    if(u64fd != -1)
    {
        commPckt.pcktType = SYSEX_TYPE;
        commPckt.d.sysr.reqType = START_SEQUENCE;

	uspsema(giosema);
        if(uhWriteGame(u64fd,&commPckt,sizeof(CommPckt)) == -1)
        {
            perror("Error sending StartSequence sysex request info");
	    usvsema(giosema);
            return(-1);
        }
	usvsema(giosema);
    }
    return 0;
}

int GioMgr::LoadMidi(CommPckt *commPckt)
{
    int dbg;
    if(u64fd != -1)
    {
	uspsema(giosema);
        if(uhWriteGame(u64fd,commPckt,sizeof(CommPckt)) == -1)
        {
            perror("Error sending midi block");
	    usvsema(giosema);
            return (-1);
        }
	usvsema(giosema);

    }
    return 0;
}

int GioMgr::LoadBlock(u8 *destAddr, u8 *data, u32 size)
{
    CommPckt        commPckt;

    if(u64fd != -1)
    {
        commPckt.pcktType = SYSEX_TYPE;
        commPckt.d.sysr.reqType = WRITE_BLOCK;
        commPckt.d.sysr.offset = (u32)destAddr;
        commPckt.d.sysr.value = size;
    
	uspsema(giosema);
        if(uhWriteGame(u64fd,&commPckt,sizeof(CommPckt)) == -1)
        {
            perror("Error sending sysex request info");
	    usvsema(giosema);
            return (-1);
        }
        
        if(uhWriteGame(u64fd,data,size) == -1)
        {
            perror("Error sending sysex data");
	    usvsema(giosema);
            return (-1);
        }
	usvsema(giosema);
    }
    return 0;
}

int GioMgr::LoadSamples(u8 *addr,char *data, int numBytes)
{
    CommPckt        commPckt;

    if(u64fd != -1)
    {
        commPckt.pcktType = SYSEX_TYPE;
        commPckt.d.sysr.reqType = WRITE_RAMROM;

	uspsema(giosema);
        if(uhWriteGame(u64fd,&commPckt,sizeof(CommPckt)) == -1)
        {
            perror("Error sending LoadRamRom sysex request info");
	    usvsema(giosema);
            return(-1);
        }
        
        if(uhWriteRamrom(u64fd,addr,data,numBytes) == -1)
        {
            perror("Error reported with uhWriteRamrom");
	    usvsema(giosema);
            return(-1);
        }
	usvsema(giosema);
    }
    return 0;
}

int GioMgr::LoadInstVol(u8 *addr, int vol)
{
    CommPckt        commPckt;
    
    if(u64fd != -1)
    {
        commPckt.pcktType = SYSEX_TYPE;
        commPckt.d.sysr.reqType = LOAD_INST_VOL;
        commPckt.d.sysr.offset = (u32)addr;
        commPckt.d.sysr.value = vol;
	
	uspsema(giosema);
        if(uhWriteGame(u64fd,&commPckt,sizeof(CommPckt)) == -1)
        {
            perror("Error sending LoadInstVol sysex request info");
	    usvsema(giosema);
            return(-1);
        }
	usvsema(giosema);
    }
    return 0;
}

int GioMgr::LoadInstPan(u8 *addr, int pan)
{
    CommPckt        commPckt;
    
    if(u64fd != -1)
    {
        commPckt.pcktType = SYSEX_TYPE;
        commPckt.d.sysr.reqType = LOAD_INST_PAN;
        commPckt.d.sysr.offset = (u32)addr;
        commPckt.d.sysr.value = pan;
	
	uspsema(giosema);
        if(uhWriteGame(u64fd,&commPckt,sizeof(CommPckt)) == -1)
        {
            perror("Error sending LoadInstPan sysex request info");
	    usvsema(giosema);
            return(-1);
        }
	usvsema(giosema);
    }
    return 0;
}

int GioMgr::LoadInstPriority(u8 *addr, int priority)
{
    CommPckt        commPckt;
    
    if(u64fd != -1)
    {
        commPckt.pcktType = SYSEX_TYPE;
        commPckt.d.sysr.reqType = LOAD_INST_PRIORITY;
        commPckt.d.sysr.offset = (u32)addr;
        commPckt.d.sysr.value = priority;
	
	uspsema(giosema);
        if(uhWriteGame(u64fd,&commPckt,sizeof(CommPckt)) == -1)
        {
            perror("Error sending LoadInstPriority sysex request info");
	    usvsema(giosema);
            return(-1);
        }
	usvsema(giosema);
    }
    return 0;
}

int GioMgr::LoadInstBendRange(u8 *addr, int bendRange)
{
    CommPckt        commPckt;
    
    if(u64fd != -1)
    {
        commPckt.pcktType = SYSEX_TYPE;
        commPckt.d.sysr.reqType = LOAD_INST_BENDRANGE;
        commPckt.d.sysr.offset = (u32)addr;
        commPckt.d.sysr.value = bendRange;
	
	uspsema(giosema);
        if(uhWriteGame(u64fd,&commPckt,sizeof(CommPckt)) == -1)
        {
            perror("Error sending LoadInstBendRange sysex request info");
	    usvsema(giosema);
            return(-1);
        }
	usvsema(giosema);
    }
    return 0;
}

int GioMgr::LoadFXParam(int paramID, int paramVal)
{
    CommPckt        commPckt;
    
    if(u64fd != -1)
    {
        commPckt.pcktType = SYSEX_TYPE;
        commPckt.d.sysr.reqType = SET_FX_PARAM;
        commPckt.d.sysr.offset = paramID;
        commPckt.d.sysr.value = paramVal;
	
	uspsema(giosema);
        if(uhWriteGame(u64fd,&commPckt,sizeof(CommPckt)) == -1)
        {
            perror("Error sending LoadFXParam sysex request info");
	    usvsema(giosema);
            return(-1);
        }
	usvsema(giosema);
    }
    return 0;
}

/*****************************************
 *
 *  CAUTION!! Thread should have control of
 *  semaphore before calling these routines!
 *
 *****************************************/
int GioMgr::_ReadValue(int *valPtr)
{
    if(u64fd != -1)
    {
        if(uhReadGame(u64fd, valPtr, 4) == -1)
        {
            perror("Unable to read value");
            return(-1);
        }
	
    }
    return 0;
}


int GioMgr::_loadblock(u8 *destAddr, u8 *data, u32 size)
{
    CommPckt        commPckt;
    int             tmp;

    if(u64fd != -1)
    {
        commPckt.pcktType = SYSEX_TYPE;
        commPckt.d.sysr.reqType = WRITE_BLOCK;
        commPckt.d.sysr.offset = (u32)destAddr;
        commPckt.d.sysr.value = size;
    
        if(uhWriteGame(u64fd,&commPckt,sizeof(CommPckt)) == -1)
        {
            perror("Error sending sysex request info");
            return (-1);
        }
        if(uhWriteGame(u64fd,data,size) == -1)
        {
            perror("Error sending sysex data");
            return (-1);
        }
    }
    return 0;
}