nlgplay for mbed

Dependencies:   SDFileSystemEx mbed

Revision:
3:aed228fa3ae4
Parent:
2:f0b7779b03c7
Child:
4:4dc67a366727
--- a/main.cpp	Tue May 20 09:24:55 2014 +0000
+++ b/main.cpp	Tue May 27 15:47:18 2014 +0000
@@ -33,7 +33,7 @@
 
 FILE *nlg_file;
 
-char nlg_title[80];
+char nlg_title[65];
 int  nlg_baseclk;
 int  nlg_tick;
 int  nlg_length;
@@ -56,7 +56,7 @@
     CTS = 0;
 }
 
-#define _WAIT for(int wcnt=0; wcnt < 5; wcnt++)
+#define _WAIT for(int wcnt=0; wcnt < 2; wcnt++)
 
 // 16bit output
 void ioShiftOut(unsigned int data)
@@ -79,63 +79,18 @@
         data <<= 1;
 
         SCK = 1;
-        _WAIT;
+        // _WAIT;
         SCK = 0;
-        _WAIT;
+        // _WAIT;
     }
 
     RCK = 1;
-    _WAIT;
+    // _WAIT;
     RCK = 0;
-    _WAIT;
+    // _WAIT;
 }
 
 
-// 16bit output
-void ioShiftOutTest(unsigned int data)
-{
-    int i;
-    int ta = 0;
-    int tb = 0;
-    Timer t;
-    
-    t.start();
-    
-    for(i = 0; i < 8; i++)
-    {
-        /* 2ビット分のデータをそれぞれ出力 */
-        if (data & 0x80)
-            CTS = 1;
-        else
-            CTS = 0;
-        
-        if (data & 0x8000)
-            DBS = 1;
-        else
-            DBS = 0;
-        
-        data <<= 1;
-
-        SCK = 1;
-        _WAIT;
-        SCK = 0;
-        _WAIT;
-    }
-    
-    ta = t.read_us();
-
-    RCK = 1;
-    _WAIT;
-    RCK = 0;
-    _WAIT;
-    
-    tb = t.read_us();
-    
-    t.stop();
-    
-    printf("test result ta=%d tb=%d\n", ta, tb);    
-}
-
 /* 制御信号定義 */
 #define CS_PSG (1 << 1)
 #define CS_FM1 (1 << 2)
@@ -159,8 +114,8 @@
     
     
     /* チップ側の処理を待つ */
-    if (!(select & CS_PSG))
-        wait_us(10);
+    // if (!(select & CS_PSG))
+    //    wait_us(10);
         
     
     /* データを出力 */
@@ -170,8 +125,8 @@
     ioShiftOut((data << 8) | A0 | (ACTLOW));
 
     /* wait if FM */
-    if (!(select & CS_PSG))
-        wait_us(10);
+    // if (!(select & CS_PSG))
+    //    wait_us(10);
 }
 
 /* PSG出力 */
@@ -201,18 +156,24 @@
     regPSGOut(0x00,0);
     regPSGOut(0x01,0);
     
-    regPSGOut(0x06,0x00);
-    regPSGOut(0x07,0x3f); // ALL OFF
-    regPSGOut(0x08,0x00); // CH.A 0
-    regPSGOut(0x09,0x00); // CH.B 0
-    regPSGOut(0x0a,0x00); // CH.C 0
+    regPSGOut(0x06, 0x00);
+    regPSGOut(0x07, 0x3f); // ALL OFF
+    regPSGOut(0x08, 0x00); // CH.A 0
+    regPSGOut(0x09, 0x00); // CH.B 0
+    regPSGOut(0x0a, 0x00); // CH.C 0
     
     /* FM音源初期化 */
     for(i = 0x20; i < 0x28; i++)
+    {
         regFMOut(i, 0x00);
+        regFM2Out(i, 0x00);
+    }
     
-    for(i = 0x20; i < 0x28; i++)
-        regFM2Out(i, 0x00);
+    for(i = 0x00; i < 0x08; i++)
+    {
+        regFMOut(0x08, i & 0x07);
+        regFM2Out(0x08, i & 0x07);
+    }
 }
 
 
@@ -271,7 +232,7 @@
 // NLGファイルを開く
 int OpenNLG(const char *file)
 {    
-    byte nlg_hdr[0x80];
+    byte nlg_hdr[0x60];
 
     nlg_file = fopen(file, "rb");
         
@@ -370,7 +331,6 @@
     fputc(data, nlg_file);
 }
 
-
 #endif
 
 // ファイルを閉じる
@@ -417,18 +377,25 @@
     return nlg_tick;
 }
 
+// Set Tick
+inline void SetTick(void)
+{
+    nlg_tick = ((nlg_ctc0 * 256) * nlg_ctc3);
+    // printf("nlg_tick=%d\n", nlg_tick);
+}
+
 // CTC0値の設定
 void SetCTC0_NLG(int value)
 {
     nlg_ctc0 = value;
-    nlg_tick = ((nlg_ctc0 * 256) * nlg_ctc3);
+    SetTick();
 }
 
 // CTC3値の設定
 void SetCTC3_NLG(int value)
 {
     nlg_ctc3 = value;
-    nlg_tick = ((nlg_ctc0 * 256) * nlg_ctc3);
+    SetTick();
 }
 
 // 曲の長さを得る
@@ -570,6 +537,7 @@
     int us_tick = np->base_clk / 1000000;
     
     printf("start play\n");
+    printf("us_tick=%d\n", us_tick);
     Timer t;
     
     t.start();
@@ -600,8 +568,10 @@
             np->tick -= (us * us_tick);
             
             while(t.read_us() < total_us + us);
+            
             total_us += us;
             
+            // reset timer
             if (total_us >= 1000000)
             {
                 total_us -= t.read_us();
@@ -731,8 +701,7 @@
     
     if (dir == NULL)
     {
-        lcd_setCursor(0, 1);
-        lcd_printStr("ERROR!");
+        lcd_printStr2("SD CARD","ERROR!");
         return -1;
     } 
     struct dirent *dent;
@@ -775,7 +744,7 @@
 int main() 
 {
     char file[32];
-    char path[64];
+    char path[48];
     
     led1 = 0;
     led2 = 0;
@@ -793,17 +762,30 @@
     files = get_nlg_file(file, -1);
     
     if (disp_mode) 
-    {
+    {        
         char buf[16];
+        Timer t;
+        int result_us = 0;
         
-        ioShiftOutTest(( 0x08 << 8) | (ACTLOW));
+        t.start();
+                
+        ioShiftOut(( 0x68 << 8) | (ACTLOW));
+        ioShiftOut(( 0x68 << 8) | (ACTLOW));
+        ioShiftOut(( 0x68 << 8) | (ACTLOW));
+        
+        ioShiftOut(( 0x68 << 8) | (ACTLOW));
+        ioShiftOut(( 0x68 << 8) | (ACTLOW));
+        ioShiftOut(( 0x68 << 8) | (ACTLOW));
+        
+        result_us = t.read_us();
+        t.stop();
         // ioShiftOutTest(( 0xff << 8) | (ACTLOW));
         // ioShiftOutTest(( 0xaa << 8) | (ACTLOW));
         // ioShiftOutTest(( 0x22 << 8) | (ACTLOW));
         
         
         printf("DATE:%s\nTIME:%s", __DATE__, __TIME__);
-        lcd_printStr2("NBCTRL", "Ver 1.06");
+        lcd_printStr2("NBCTRL", "Ver 1.07");
         wait(3);
         sprintf(buf, "%8s", __DATE__); 
         lcd_printStrYscr(1, buf);
@@ -811,6 +793,11 @@
         sprintf(buf, "%8s", __TIME__); 
         lcd_printStrY(1, buf);
         wait(3);
+        
+        sprintf(buf, "REG=%d", result_us); 
+        lcd_printStrY(1, buf);
+        wait(3);
+
 
         if (files < 0)
             lcd_printStrY(1, "NO FILES");
@@ -837,7 +824,7 @@
         lcd_cls();
         lcd_printStrY(0, file);
 
-        sprintf(buf, "%2d ", idx);
+        sprintf(buf, "%2d ", idx + 1);
         lcd_printStrY(1, buf);
 
         nlg_play(path);