giocomm.c 6.3 KB
#include <ultratypes.h>
#include <libaudio.h> 
#include <stdio.h>
#include <stdlib.h>
#include <bstring.h>
#include <unistd.h>
#include <fcntl.h>
#include <u64gio.h>
#include "midiApp.h"
#include "midiDmon.h"
#include "midiGlobals.h"

#ifdef SYSEX_IMPL
#include <sysex.h>
#include <synthcommon.h>
#endif

extern int      gGIOfd,hardware;



void SetValue(u32 offset,int valType,u32 value)
{
    u64_write_arg_t writeHdr;
    SysExReq        SEXReq;

    SEXReq.pcktType = SYSEX_TYPE;
    SEXReq.reqType = valType;
    SEXReq.offset = offset;
    SEXReq.value = value;
    
    writeHdr.buffer = &SEXReq;
    writeHdr.ramrom_addr = RAMROM_APP_READ_ADDR;
    writeHdr.nbytes = sizeof(SysExReq);
    writeHdr.value = HOST_APP_CMD_READY;

    if(hardware)
        ioctl(gGIOfd, U64_SAFE_WRITE, &writeHdr);
}

u32 GetValue(u32 offset,int valType)
{
    u64_read_arg_t  readHdr;
    u64_write_arg_t writeHdr;
    SysExReq        SEXReq;
    int             numBytes;
    u32             buf,readBuf;    
    
    SEXReq.pcktType = SYSEX_TYPE;
    SEXReq.reqType = valType;
    SEXReq.offset = offset;

    writeHdr.buffer = &SEXReq;
    writeHdr.ramrom_addr = RAMROM_APP_READ_ADDR;
    writeHdr.nbytes = sizeof(SysExReq);
    writeHdr.value = HOST_APP_CMD_READY;
    
    if(hardware)
    {
        ioctl(gGIOfd, U64_SAFE_WRITE, &writeHdr);

        readHdr.buffer = &readBuf;
        readHdr.ramrom_addr = RAMROM_APP_WRITE_ADDR;
        readHdr.nbytes =  4;
        readHdr.value = HOST_APP_CMD_READY; /* GAME_APP_DATA_READY; */

        /* block on read, waiting for a return ack */
        numBytes = read(gGIOfd, &buf, 1);
        /* read the data into the buffer */
        ioctl(gGIOfd, U64_SAFE_READ,&readHdr); 
        return(readBuf);
    }
    return 0;
}

void WriteBlock(u32 destOffset,char *data,u32 size)
{
    u64_write_arg_t writeHdr;
    SysExReq        SEXReq;
    u32             sending,sent = 0;
    

    SEXReq.pcktType = SYSEX_TYPE;
    SEXReq.reqType = WRITE_BLOCK;
    SEXReq.offset = destOffset;
    SEXReq.value = size;
    
    writeHdr.buffer = &SEXReq;
    writeHdr.ramrom_addr = RAMROM_APP_READ_ADDR;
    writeHdr.nbytes =  sizeof(SysExReq);
    writeHdr.value = HOST_APP_CMD_READY;

    if(hardware)
    {
        ioctl(gGIOfd, U64_SAFE_WRITE, &writeHdr);
        
        while(size)
        {
            sending = MIN(size,RAMROM_BUF_SIZE);
            writeHdr.buffer = &data[sent];
            writeHdr.nbytes = sending;

            ioctl(gGIOfd,  U64_SAFE_WRITE, &writeHdr);
            sent+=sending;
            size-=sending;
        }
    }
}

void WriteRamRom(u32 destAddr,char *data,u32 size)
{
    u64_write_arg_t writeHdr;
    SysExReq        SEXReq;
    
    SEXReq.pcktType = SYSEX_TYPE;
    SEXReq.reqType = WRITE_RAMROM;
    
    writeHdr.buffer = &SEXReq;
    writeHdr.ramrom_addr = RAMROM_APP_READ_ADDR;
    writeHdr.nbytes =  sizeof(SysExReq);
    writeHdr.value = HOST_APP_CMD_READY;

    if(hardware)
    {
        ioctl(gGIOfd, U64_SAFE_WRITE, &writeHdr);
        
        writeHdr.buffer = data;
        writeHdr.nbytes = size;
        writeHdr.ramrom_addr = destAddr;

        ioctl(gGIOfd,  U64_SAFE_WRITE, &writeHdr);
    }
}

u32 WriteMirror(void)
{
    int             i,c,numPtrs;
    char            *tmpMirror;
    ALBankFile      *bnkfPtr;
    ALBank          *bankPtr;
    ALInstrument    *instPtr;
    ALSound         *sndPtr;
    ALWaveTable     *wvTblPtr;
    ALADPCMWaveInfo *wvInfoPtr;


    tmpMirror = malloc(gMirrorCur);
    if(!tmpMirror)
    {
        fprintf(stderr,"%s: Unable to allocate memory for temp mirror\n",gAppName);
        return(MEMORY_NOT_OK);
    }

    bcopy(gMirror,tmpMirror,gMirrorCur);

    for(i=0;i<gObjCt;i++)
    {
        switch(gSymObj[i].SFObj.objectClass)
        {
            case AL_BNKFILE_CLASS:
                bnkfPtr = (ALBankFile*)&tmpMirror[gSymObj[i].SFObj.bankFileOffset];
                numPtrs = (int)bnkfPtr->bankCount;
                for(c=0;c<numPtrs;c++)
                    bnkfPtr->bankArray[c] = (ALBank*)((u32)bnkfPtr->bankArray[c] + BANK_RDRAM_ADDR);
                break;
            case AL_BANK_CLASS:
                bankPtr = (ALBank*)&tmpMirror[gSymObj[i].SFObj.bankFileOffset];
                numPtrs = (int)bankPtr->instCount;
                for(c=0;c<numPtrs;c++)
                    bankPtr->instArray[c] = (ALInstrument*)((u32)bankPtr->instArray[c] + BANK_RDRAM_ADDR);
                bankPtr->percussion = (ALInstrument*)((u32)bankPtr->percussion + BANK_RDRAM_ADDR);
                break;
            case AL_INST_CLASS:
                instPtr = (ALInstrument*)&tmpMirror[gSymObj[i].SFObj.bankFileOffset];
                numPtrs = instPtr->soundCount;
                for(c=0;c<numPtrs;c++)
                    instPtr->soundArray[c] = (ALSound*)((u32)instPtr->soundArray[c] + BANK_RDRAM_ADDR);
                break;
            case AL_SOUND_CLASS:
                sndPtr = (ALSound*)&tmpMirror[gSymObj[i].SFObj.bankFileOffset];
                sndPtr->envelope = (ALEnvelope*)((u32)sndPtr->envelope + BANK_RDRAM_ADDR);
                sndPtr->wavetable = (ALWaveTable*)((u32)sndPtr->wavetable + BANK_RDRAM_ADDR);
                sndPtr->keyMap = (ALKeyMap*)((u32)sndPtr->keyMap + BANK_RDRAM_ADDR);
                break;
            case AL_WAVETBL_CLASS:
                wvTblPtr = (ALWaveTable*)&tmpMirror[gSymObj[i].SFObj.bankFileOffset];
                wvTblPtr->base = (char*)((u32)wvTblPtr->base + SAMPLES_RAMROM_ADDR);
                wvInfoPtr = (ALADPCMWaveInfo*)&wvTblPtr->waveInfo.adpcmWave;
                wvInfoPtr->book = (ALADPCMBook*)((u32)wvInfoPtr->book + BANK_RDRAM_ADDR);
                if(wvInfoPtr->loop)
                    wvInfoPtr->loop = (ALADPCMloop*)((u32)wvInfoPtr->loop + BANK_RDRAM_ADDR);
                break;
        }
    }

    WriteBlock(BANK_RDRAM_ADDR,tmpMirror,gMirrorCur);
    free(tmpMirror);
    return(MEMORY_OK);
}

void SendSetBank(u32 offset)
{
    u64_write_arg_t writeHdr;
    SysExReq        SEXReq;
    int             i;
    
    SEXReq.pcktType = SYSEX_TYPE;
    SEXReq.reqType = RESET_BANK;
    SEXReq.offset = offset;
    SEXReq.value = 0;
    
    writeHdr.buffer = &SEXReq;
    writeHdr.ramrom_addr = RAMROM_APP_READ_ADDR;
    writeHdr.nbytes = sizeof(SysExReq);
    writeHdr.value = HOST_APP_CMD_READY;

    if(hardware)
        ioctl(gGIOfd, U64_SAFE_WRITE, &writeHdr);

    for(i=0;i<MAX_SEQ_CHANNELS;i++)
        gCurChanInst[i] = 0;
}