var Clock = xdc.useModule('ti.sysbios.knl.Clock');
var BIOS = xdc.useModule('ti.sysbios.BIOS');
var HeapMem = xdc.useModule('ti.sysbios.heaps.HeapMem');
var Cache = xdc.useModule('ti.sysbios.family.arm.a15.Cache');
var Task = xdc.useModule('ti.sysbios.knl.Task');
var Memory = xdc.useModule('xdc.runtime.Memory');
var Defaults = xdc.useModule('xdc.runtime.Defaults');
var System = xdc.useModule('xdc.runtime.System');
var SysStd    = xdc.useModule("xdc.runtime.SysStd");
var Hwi = xdc.useModule('ti.sysbios.hal.Hwi');
var MMU = xdc.useModule('ti.sysbios.family.arm.a15.Mmu');
var Timestamp = xdc.useModule('xdc.runtime.Timestamp');
Clock.timerId = -1;
Clock.tickSource = Clock.TickSource_TIMER;
Clock.swiPriority = 15;
var task0Params = new Task.Params();
task0Params.instance.name = "task0";
task0Params.stackSize = 65536;
Program.global.task0 = Task.create("&taskMain", task0Params);
Task.defaultStackSize = 16384;
Task.idleTaskStackSize = 16384;
var heapMem0Params = new HeapMem.Params();
heapMem0Params.instance.name = "heapMem0";
heapMem0Params.size = 70000;
heapMem0Params.sectionName = "DataHeap";
Program.global.heapMem0 = HeapMem.create(heapMem0Params);
Memory.defaultHeapInstance = Program.global.heapMem0;
Cache.enableCache = true;
Program.argSize = 512;
Program.heap = 1000;

// Set the proxy for System module. This enables print statements
System.SupportProxy = SysStd;


/**********************************************************************\
|                  Interrupt and MMU Configuration                     |
\**********************************************************************/

  // Enable the MMU (Required for L1/L2 data caching)
  MMU.enableMMU = true;

  // descriptor attribute structure
  var attrs = new MMU.DescriptorAttrs();
  MMU.initDescAttrsMeta(attrs);
  attrs.type = MMU.DescriptorType_BLOCK;    // BLOCK descriptor
  attrs.shareable = 3;                      // inner-sharerable
  attrs.attrIndx = 2;                       // MAIR0 Byte2 describes
                                            // memory attributes for
                                            // this level2 entry

  // write memory region attribute in mairRegAttr[2] i.e. MAIR0 Reg Byte2
  MMU.setMAIRMeta(2, 0xFF);              // Mark mem regions as cacheable

  // Set the descriptor for each entry in the address range
  for (var i=0x40000000; i < 0x40600000; i = i + 0x00200000)
  {
      // Each 'BLOCK' descriptor entry spans a 2MB address range
      MMU.setSecondLevelDescMeta(i, i, attrs);
  }    

  // Set the descriptor for each entry in the address range
  for (var i=0x80000000; i < 0xA0000000; i = i + 0x00200000)
  {
      // Each 'BLOCK' descriptor entry spans a 2MB address range
      MMU.setSecondLevelDescMeta(i, i, attrs);
  }

  // descriptor attribute structure
  var attrs = new MMU.DescriptorAttrs();
  MMU.initDescAttrsMeta(attrs);
  attrs.type = MMU.DescriptorType_BLOCK;  // BLOCK descriptor
  attrs.noExecute = true;                 // not executable
  attrs.accPerm = 0;                      // read/write at PL1
  attrs.attrIndx = 1;                     // MAIR0 Byte1 describes
                                          // memory attributes for
                                          // each BLOCK MMU entry

  // write memory region attribute in mairRegAttr[1] i.e. MAIR0 Reg Byte1
  MMU.setMAIRMeta(1, 0x04);               // Mark mem regions as device memory

  // Configure the corresponding MMU page descriptor accordingly
  for (var i=0x40600000; i < 0x60000000; i = i + 0x00200000)
  {
    // Each 'BLOCK' descriptor entry spans a 2MB address range
    MMU.setSecondLevelDescMeta(i, i, attrs);
  }
  
  // Move all sections to DDR 
Program.sectMap[".text"] = "DDR3_CA15";
Program.sectMap[".vecs"] = "DDR3_CA15";

BIOS.libType = BIOS.LibType_Custom;
BIOS.includeXdcRuntime = true;

Program.linkTemplate = "gnu/targets/arm/linkcmd_bm_v7a.xdt";
BIOS.customCCOpts = "-Wunused -Wunknown-pragmas -ffunction-sections -fdata-sections -mcpu=cortex-a15 -mfpu=neon -mfloat-abi=hard -mabi=aapcs -g  -O3 -IC:/ti/bios_6_40_01_15/packages/gnu/targets/arm//libs/install-native/$(GCCTARG)/include -Dfar= -D__DYNAMIC_REENT__";
