   delay(100);
   AudioMemory(24);
 
-  Serial.println("Enabling codec...\n");
+  Serial.println("Sketch: Enabling codec...\n");
   codecControl.enable();
   delay(100);
+
+  Serial.println("Enabling SPI");
+  spiMem0.begin();
   
 }
 
-void loop() {  
+int calcData(int spiAddress, int loopPhase, int maskPhase)
+{
+  int data;
+
+  int phase = ((loopPhase << 1) + maskPhase) & 0x3;
+  switch(phase)
+  {
+    case 0 :
+    data = spiAddress ^ mask0;
+    break;
+    case 1:
+    data = spiAddress ^ mask1;
+    break;
+    case 2:
+    data = ~spiAddress ^ mask0;
+    break;
+    case 3:
+    data = ~spiAddress ^ mask1;
+    
+  }
+  return (data & 0xffff);
+}
+
+void loop() {
 
   //////////////////////////////////////////////////////////////////
   // Write test data to the SPI Memory 0
   //////////////////////////////////////////////////////////////////
-  for (spiAddress0=0; spiAddress0 <= SPI_MAX_ADDR; spiAddress0++) {
+  maskPhase = 0;
+  for (spiAddress0=0; spiAddress0 <= SPI_MAX_ADDR; spiAddress0=spiAddress0+2) {
     if ((spiAddress0 % 32768) == 0) {
       //Serial.print("Writing to ");
       //Serial.println(spiAddress0, HEX);
     }
 
-    //mem0Write(spiAddress0, spiData0);
-    spiMem0.write(spiAddress0, spiData0);
-    spiData0 = (spiData0-1) & 0xff;
+    spiData0 = calcData(spiAddress0, loopPhase, maskPhase);
+    spiMem0.write16(spiAddress0, spiData0);
+    maskPhase = (maskPhase+1) % 2;
   }
   Serial.println("SPI0 writing DONE!");
 

  ///////////////////////////////////////////////////////////////////
  spiErrorCount0 = 0;
  spiAddress0 = 0;
  spiData0 = 0xff;

  for (spiAddress0=0; spiAddress0 <= SPI_MAX_ADDR; spiAddress0++) {
    if ((spiAddress0 % 32768) == 0) {
      //Serial.print("Reading ");
      //Serial.print(spiAddress0, HEX);
    }

    //int data = mem0Read(spiAddress0);
    int data = spiMem0.read(spiAddress0);
    if (data != spiData0) {
      spiErrorCount0++;
      Serial.println("");
      Serial.print("ERROR MEM0: (expected) (actual):");
      Serial.print(spiData0, HEX); Serial.print(":");
      Serial.println(data, HEX);
      delay(100);
    }

    if ((spiAddress0 % 32768) == 0) {
      //Serial.print(", data = ");
      //Serial.println(data, HEX);
    }

    spiData0 = (spiData0-1) & 0xff;

    // Break out of test once the error count reaches 10
    if (spiErrorCount0 > 10) { break; }

MIDI_CREATE_DEFAULT_INSTANCE();
using namespace midi;

using namespace BAGuitar;

AudioInputI2S            i2sIn;
