/*
 * Copyright (c) 2015 Texas Instruments Incorporated - http://www.ti.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *  ======== Dsp1.cfg ========
 */

/* root of the configuration object model */
var Program = xdc.useModule('xdc.cfg.Program');
var cfgArgs = Program.build.cfgArgs;
var RB = (cfgArgs.profile == "release" ? true : false);

/* application uses the following modules and packages */
xdc.useModule('xdc.runtime.Assert');
var Diags = xdc.useModule('xdc.runtime.Diags');
xdc.useModule('xdc.runtime.Error');
xdc.useModule('xdc.runtime.Log');
xdc.useModule('xdc.runtime.Registry');

xdc.useModule('ti.sysbios.knl.Task');

var BIOS = xdc.useModule('ti.sysbios.BIOS');
BIOS.libType = (RB ? BIOS.LibType_NonInstrumented : BIOS.LibType_Instrumented);
// BIOS.libType = BIOS.LibType_Custom;
// BIOS.libType = BIOS.LibType_Debug;

xdc.useModule('ti.sdo.ipc.heaps.HeapBufMP');


/*
 *  ======== IPC Configuration ========
 */
xdc.global.SR0_cacheEnable = true;
Program.global.SR0_owner = "CORE0"; /* DSP1 is owner of SR #0 */

/* register a startup first function to set SR#0 owner */
var Startup = xdc.useModule('xdc.runtime.Startup');
Startup.firstFxns.$add('&MainDspN_startupFirst');

/* configure processor names */
var MultiProc = xdc.useModule('ti.sdo.utils.MultiProc');
MultiProc.procAddrMode = MultiProc.ProcAddrMode_Cluster;

/* these must match host side configuration: MultiProcCfg_tci6638.c */

/* single device configuration */
//var procNameAry = MultiProc.getDeviceProcNames();

/* development configuration */
var procNameAry = [ "HOST", "CORE0", "CORE1", "CORE2", "CORE3", "CORE4",
        "CORE5", "CORE6", "CORE7" ];
MultiProc.numProcessors = 4608;
MultiProc.baseIdOfCluster = MultiProc.INVALIDID;

/* Note: MultiProc_self is set during VirtQueue_init based on DNUM. */
MultiProc.setConfig(null, procNameAry);
Program.global.MultiProc_clusterSize = MultiProc.numProcsInCluster;

var MultiProcSetup = xdc.useModule('ti.sdo.ipc.family.tci663x.MultiProcSetup');
MultiProcSetup.configureProcId = false;

/* ipc startup configuration */
var Ipc = xdc.useModule('ti.sdo.ipc.Ipc');
Ipc.procSync = Ipc.ProcSync_ALL;

/* shared region configuration */
var SharedRegion = xdc.useModule('ti.sdo.ipc.SharedRegion');

/* configure SharedRegion #0 (IPC) */
var SR0Mem = Program.cpu.memoryMap["SR_0"];
SharedRegion.setEntryMeta(0,
    new SharedRegion.Entry({
        name:           "SR_0",
        base:           0xc0000000,
        len:            0x50000,
//      ownerProcId:    MultiProc.getIdMeta(Program.global.SR0_owner),
        isValid:        false,
        cacheEnable:    xdc.global.SR0_cacheEnable
    })
);

/* make SR_0 non-cacheable */
var Cache = xdc.useModule('ti.sysbios.family.c66.Cache');
//Cache.setMarMeta(SR0Mem.base, SR0Mem.len, Cache.Mar_DISABLE);

/* select ipc libraries */
var Build = xdc.useModule('ti.sdo.ipc.Build');
Build.libType = (RB ? Build.LibType_NonInstrumented : Build.LibType_Debug);
Build.assertsEnabled = (RB ? false : true);
Build.logsEnabled = (RB ? false : true);

/* used to write SysMin trace buffer to memory */
var Idle = xdc.useModule('ti.sysbios.knl.Idle');
Idle.addFunc('&VirtQueue_cacheWb');

/* should be done internally */
xdc.useModule("ti.ipc.namesrv.NameServerRemoteRpmsg");

xdc.useModule('ti.ipc.family.tci6638.VirtQueue');
var Interrupt = xdc.useModule('ti.ipc.family.tci6638.Interrupt');

/* why doe we need this? */
Program.sectMap[".text:_c_int00"] = new Program.SectionSpec();
Program.sectMap[".text:_c_int00"].loadSegment = "L2SRAM";
Program.sectMap[".text:_c_int00"].loadAlign = 0x400;

/* is this not enabled by default? */
var Hwi = xdc.useModule('ti.sysbios.family.c64p.Hwi');
Hwi.enableException = true;

/*  This makes the vrings address range 0xA000_0000 to 0xA1FF_FFFF uncachable.
 *  We assume the rest is to be left cacheable.
 *
 *  Per sprugw0b.pdf
 *  0184_8280h MAR160 Memory Attribute Register 160 A000 0000h - A0FF FFFFh
 *  0184_8284h MAR161 Memory Attribute Register 161 A100 0000h - A1FF FFFFh
 */
var Cache = xdc.useModule('ti.sysbios.family.c66.Cache');
Cache.setMarMeta(0xA0000000, 0x2000000, 0); /* 32 MB, value should be enum */

/* Enable Memory Translation module that operates on the Resource Table */
var Resource = xdc.useModule('ti.ipc.remoteproc.Resource');
Resource.loadSegment = Program.platform.dataMemory;

xdc.loadPackage('ti.ipc.ipcmgr');

/* MessageQ application specific configuration */
var MessageQ = xdc.useModule('ti.sdo.ipc.MessageQ');
MessageQ.numReservedEntries = 8;
MessageQ.numHeaps = 9; /* 1 for transport, 1 for each dsp (8x) */

/* this heap is used by RPMsgTransport */
var HeapBuf = xdc.useModule('ti.sysbios.heaps.HeapBuf');
var heapBufP = new HeapBuf.Params;
heapBufP.align = 8;
heapBufP.blockSize = 512;
heapBufP.numBlocks = 64;
var msgHeap = HeapBuf.create(heapBufP);
MessageQ.registerHeapMeta(msgHeap, 0);

var VirtioSetup = xdc.useModule('ti.ipc.transports.TransportRpmsgSetup');
VirtioSetup.common$.diags_INFO = Diags.RUNTIME_OFF;

//xdc.loadPackage('ti.ipc.transports').profile = 'release';
xdc.loadPackage('ti.ipc.transports');

/* why is this not done internally? */
var NotifyDriverCirc =
        xdc.useModule('ti.sdo.ipc.notifyDrivers.NotifyDriverCirc');
var Interrupt = xdc.useModule('ti.ipc.family.tci6638.Interrupt');
NotifyDriverCirc.InterruptProxy = Interrupt;

/*  Notify brings in the ti.sdo.ipc.family.Settings module, which does
 *  lots of config magic which will need to be UNDONE later, or setup
 *  earlier, to get the necessary overrides to various IPC module proxies!
 */
var Notify = xdc.module('ti.sdo.ipc.Notify');
var Ipc = xdc.useModule('ti.sdo.ipc.Ipc');

/* Note: Must call this to override what's done in Settings.xs ! */
Notify.SetupProxy = xdc.module('ti.ipc.family.tci6638.NotifyCircSetup');


/*
 *  ======== transport ========
 */
//var TransportQMSS = xdc.useModule('transport.rtos.TransportQMSS');


/*
 *  ======== Operating System Configuration ========
 */

/* no rts heap */
Program.heap = 0;
Program.argSize = 100;  /* minimum size */
Program.stack = 0x1000;

/* create a default heap */
var HeapMem = xdc.useModule('ti.sysbios.heaps.HeapMem');
var heapMemParams = new HeapMem.Params();
heapMemParams.size = 0x8000;

var Memory = xdc.useModule('xdc.runtime.Memory');
Memory.defaultHeapInstance = HeapMem.create(heapMemParams);

/* configure System module */
var SysMin = xdc.useModule('xdc.runtime.SysMin');
SysMin.bufSize = 0x1000;
SysMin.flushAtExit = false;

var System = xdc.useModule('xdc.runtime.System');
System.SupportProxy = SysMin;


/*
 *  ======== Miscellaneous Configuration ========
 */

/* set default diags mask */
var Diags = xdc.useModule('xdc.runtime.Diags');
var Defaults = xdc.useModule('xdc.runtime.Defaults');

Defaults.common$.diags_ENTRY     = Diags.ALWAYS_OFF;
Defaults.common$.diags_EXIT      = Diags.ALWAYS_OFF;
Defaults.common$.diags_LIFECYCLE = Diags.ALWAYS_OFF;
Defaults.common$.diags_INTERNAL  = (RB ? Diags.ALWAYS_OFF : Diags.ALWAYS_ON);
Defaults.common$.diags_ASSERT    = (RB ? Diags.ALWAYS_OFF : Diags.ALWAYS_ON);
Defaults.common$.diags_STATUS    = Diags.RUNTIME_ON;
Defaults.common$.diags_USER1     = Diags.ALWAYS_OFF;
Defaults.common$.diags_USER2     = Diags.ALWAYS_OFF;
Defaults.common$.diags_USER3     = Diags.ALWAYS_OFF;
Defaults.common$.diags_USER4     = Diags.ALWAYS_OFF;
Defaults.common$.diags_USER5     = Diags.ALWAYS_OFF;
Defaults.common$.diags_USER6     = Diags.ALWAYS_OFF;
Defaults.common$.diags_INFO      = Diags.ALWAYS_OFF;
Defaults.common$.diags_ANALYSIS  = Diags.ALWAYS_OFF;

/* override diags mask for selected modules */
xdc.useModule('xdc.runtime.Main');
Diags.setMaskMeta(
    "xdc.runtime.Main",
    Diags.ENTRY | Diags.EXIT | Diags.INFO,
    Diags.RUNTIME_ON
);

var Registry = xdc.useModule('xdc.runtime.Registry');
Registry.common$.diags_ENTRY    = Diags.RUNTIME_OFF;
Registry.common$.diags_EXIT     = Diags.RUNTIME_OFF;
Registry.common$.diags_INFO     = Diags.RUNTIME_OFF;
Registry.common$.diags_ANALYSIS = Diags.RUNTIME_OFF;
Registry.common$.diags_USER1    = Diags.RUNTIME_OFF;

/* create a logger instance */
var LoggerBuf = xdc.useModule('xdc.runtime.LoggerBuf');
var loggerBufP = new LoggerBuf.Params();
loggerBufP.numEntries = 256;  /* 256 entries = 8 KB of memory */
loggerBufP.bufType = LoggerBuf.BufType_FIXED;

var appLogger = LoggerBuf.create(loggerBufP);
appLogger.instance.name = "AppLog_Core1";
Defaults.common$.logger = appLogger;
