nlgplay for mbed

Dependencies:   SDFileSystemEx mbed

Committer:
bkc_mbed
Date:
Wed Sep 17 23:29:07 2014 +0000
Revision:
11:68032f2a11b9
Parent:
10:b8d41133e499
added com mode.(untested)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bkc_mbed8:88c89fd324bd 1//
bkc_mbed8:88c89fd324bd 2// NLGPLAY : NLG file player for mbed
bkc_mbed8:88c89fd324bd 3//
bkc_mbed10:b8d41133e499 4// example to write :
bkc_mbed10:b8d41133e499 5// ./lpc21isp -bin file.bin /dev/cu.usbserial-??? 115200 12000
bkc_mbed10:b8d41133e499 6//
bkc_mbed8:88c89fd324bd 7
bkc_mbed0:2053640461b5 8#include "mbed.h"
bkc_mbed0:2053640461b5 9#include "SDFileSystem.h"
bkc_mbed0:2053640461b5 10#include "lcd.h"
bkc_mbed0:2053640461b5 11
bkc_mbed11:68032f2a11b9 12#define USE_NBV3
bkc_mbed8:88c89fd324bd 13#define USE_FASTIO
bkc_mbed7:7e183b33c3f9 14
bkc_mbed10:b8d41133e499 15#define NBCTRL_VER "Ver 1.13"
bkc_mbed10:b8d41133e499 16
bkc_mbed10:b8d41133e499 17#pragma O3
bkc_mbed10:b8d41133e499 18#pragma Otime
bkc_mbed10:b8d41133e499 19
bkc_mbed6:e48296dca6bb 20
bkc_mbed6:e48296dca6bb 21// (pinname, mode)
bkc_mbed5:27f193e21222 22DigitalIn sw_play(dp24, PullUp);
bkc_mbed5:27f193e21222 23DigitalIn sw_next(dp10, PullUp);
bkc_mbed6:e48296dca6bb 24DigitalIn sw_prev(dp9, PullUp);
bkc_mbed0:2053640461b5 25
bkc_mbed6:e48296dca6bb 26// LED
bkc_mbed10:b8d41133e499 27DigitalOut led1(LED1, 0);
bkc_mbed10:b8d41133e499 28DigitalOut led2(LED2, 0);
bkc_mbed0:2053640461b5 29
bkc_mbed6:e48296dca6bb 30// SPI: MOSI, MISO, SCLK, CS
bkc_mbed0:2053640461b5 31SDFileSystem sd(dp2, dp1, dp6, dp25, "sd");
bkc_mbed0:2053640461b5 32
bkc_mbed0:2053640461b5 33//
bkc_mbed0:2053640461b5 34// nlg_emb.c
bkc_mbed0:2053640461b5 35//
bkc_mbed0:2053640461b5 36
bkc_mbed0:2053640461b5 37#define CMD_PSG 0x00
bkc_mbed0:2053640461b5 38#define CMD_OPM 0x01
bkc_mbed0:2053640461b5 39#define CMD_OPM2 0x02
bkc_mbed0:2053640461b5 40#define CMD_IRQ 0x80
bkc_mbed0:2053640461b5 41
bkc_mbed0:2053640461b5 42#define CMD_CTC0 0x81
bkc_mbed0:2053640461b5 43#define CMD_CTC3 0x82
bkc_mbed0:2053640461b5 44
bkc_mbed0:2053640461b5 45#define NLG_VER (110)
bkc_mbed0:2053640461b5 46#define NLG_BASECLK (4000000)
bkc_mbed0:2053640461b5 47
bkc_mbed0:2053640461b5 48FILE *nlg_file;
bkc_mbed0:2053640461b5 49
bkc_mbed3:aed228fa3ae4 50char nlg_title[65];
bkc_mbed0:2053640461b5 51int nlg_baseclk;
bkc_mbed0:2053640461b5 52int nlg_tick;
bkc_mbed0:2053640461b5 53int nlg_length;
bkc_mbed0:2053640461b5 54int nlg_loop_ptr;
bkc_mbed0:2053640461b5 55int nlg_version;
bkc_mbed0:2053640461b5 56
bkc_mbed0:2053640461b5 57int nlg_ctc0;
bkc_mbed0:2053640461b5 58int nlg_ctc3;
bkc_mbed0:2053640461b5 59
bkc_mbed8:88c89fd324bd 60#define _WAIT for(int wcnt=0; wcnt < 2; wcnt++)
bkc_mbed8:88c89fd324bd 61
bkc_mbed8:88c89fd324bd 62#ifndef USE_FASTIO
bkc_mbed8:88c89fd324bd 63// 通常IO
bkc_mbed8:88c89fd324bd 64
bkc_mbed9:834e9eed0d72 65#define RCK io04 // pin4 , P0_11
bkc_mbed9:834e9eed0d72 66#define SCK io03 // pin17, P1_8
bkc_mbed9:834e9eed0d72 67#define DBS io01 // pin13, P1_4
bkc_mbed9:834e9eed0d72 68#define CTS io02 // pin26, P0_3
bkc_mbed0:2053640461b5 69
bkc_mbed7:7e183b33c3f9 70#define IO_A0 io05
bkc_mbed7:7e183b33c3f9 71#define IO_WR io06
bkc_mbed7:7e183b33c3f9 72
bkc_mbed8:88c89fd324bd 73// (pinname, initial value)
bkc_mbed8:88c89fd324bd 74DigitalOut io01(dp13, 0); // P1_4
bkc_mbed8:88c89fd324bd 75DigitalOut io02(dp26, 0); // P0_3
bkc_mbed8:88c89fd324bd 76DigitalOut io03(dp17, 0); // P1_8
bkc_mbed8:88c89fd324bd 77DigitalOut io04(dp4, 0); // P0_11
bkc_mbed7:7e183b33c3f9 78
bkc_mbed8:88c89fd324bd 79DigitalOut io05(dp18, 0); // P1_9
bkc_mbed9:834e9eed0d72 80DigitalOut io06(dp11, 0); // P1_2
bkc_mbed8:88c89fd324bd 81
bkc_mbed8:88c89fd324bd 82void ioInit()
bkc_mbed8:88c89fd324bd 83{
bkc_mbed8:88c89fd324bd 84}
bkc_mbed2:f0b7779b03c7 85
bkc_mbed0:2053640461b5 86// 16bit output
bkc_mbed8:88c89fd324bd 87void ioShiftOut(unsigned int data)
bkc_mbed0:2053640461b5 88{
bkc_mbed0:2053640461b5 89 int i;
bkc_mbed0:2053640461b5 90
bkc_mbed0:2053640461b5 91 for(i = 0; i < 8; i++)
bkc_mbed0:2053640461b5 92 {
bkc_mbed0:2053640461b5 93 /* 2ビット分のデータをそれぞれ出力 */
bkc_mbed0:2053640461b5 94 if (data & 0x80)
bkc_mbed0:2053640461b5 95 CTS = 1;
bkc_mbed0:2053640461b5 96 else
bkc_mbed0:2053640461b5 97 CTS = 0;
bkc_mbed0:2053640461b5 98
bkc_mbed0:2053640461b5 99 if (data & 0x8000)
bkc_mbed0:2053640461b5 100 DBS = 1;
bkc_mbed0:2053640461b5 101 else
bkc_mbed0:2053640461b5 102 DBS = 0;
bkc_mbed0:2053640461b5 103
bkc_mbed0:2053640461b5 104 data <<= 1;
bkc_mbed0:2053640461b5 105
bkc_mbed0:2053640461b5 106 SCK = 1;
bkc_mbed3:aed228fa3ae4 107 // _WAIT;
bkc_mbed0:2053640461b5 108 SCK = 0;
bkc_mbed3:aed228fa3ae4 109 // _WAIT;
bkc_mbed0:2053640461b5 110 }
bkc_mbed0:2053640461b5 111
bkc_mbed0:2053640461b5 112 RCK = 1;
bkc_mbed3:aed228fa3ae4 113 // _WAIT;
bkc_mbed0:2053640461b5 114 RCK = 0;
bkc_mbed3:aed228fa3ae4 115 // _WAIT;
bkc_mbed2:f0b7779b03c7 116}
bkc_mbed8:88c89fd324bd 117#else
bkc_mbed8:88c89fd324bd 118
bkc_mbed8:88c89fd324bd 119// 高速IO
bkc_mbed8:88c89fd324bd 120
bkc_mbed8:88c89fd324bd 121#define RCK IO_04
bkc_mbed8:88c89fd324bd 122#define SCK IO_03
bkc_mbed8:88c89fd324bd 123#define DBS IO_01
bkc_mbed8:88c89fd324bd 124#define CTS IO_02
bkc_mbed8:88c89fd324bd 125
bkc_mbed8:88c89fd324bd 126
bkc_mbed8:88c89fd324bd 127#define IO_A0 io05
bkc_mbed8:88c89fd324bd 128#define IO_WR io06
bkc_mbed8:88c89fd324bd 129
bkc_mbed9:834e9eed0d72 130DigitalOut io01(dp13, 0); // P1_4
bkc_mbed9:834e9eed0d72 131DigitalOut io02(dp26, 0); // P0_3
bkc_mbed9:834e9eed0d72 132DigitalOut io03(dp17, 0); // P1_8
bkc_mbed9:834e9eed0d72 133DigitalOut io04(dp4, 0); // P0_11
bkc_mbed9:834e9eed0d72 134
bkc_mbed8:88c89fd324bd 135DigitalOut io05(dp18, 0); // P1_9
bkc_mbed9:834e9eed0d72 136DigitalOut io06(dp11, 0); // P1_2
bkc_mbed8:88c89fd324bd 137
bkc_mbed8:88c89fd324bd 138
bkc_mbed9:834e9eed0d72 139#define IO_01 (1<<4) // DBS P1_4
bkc_mbed9:834e9eed0d72 140#define IO_02 (1<<3) // CTS P0_3
bkc_mbed9:834e9eed0d72 141#define IO_03 (1<<8) // SCK P1_8
bkc_mbed9:834e9eed0d72 142#define IO_04 (1<<11) // RCK P0_11
bkc_mbed8:88c89fd324bd 143
bkc_mbed8:88c89fd324bd 144
bkc_mbed8:88c89fd324bd 145void ioInit()
bkc_mbed8:88c89fd324bd 146{
bkc_mbed8:88c89fd324bd 147 // 出力
bkc_mbed8:88c89fd324bd 148 LPC_GPIO0->DIR |= (IO_02 | IO_04);
bkc_mbed8:88c89fd324bd 149 LPC_GPIO1->DIR |= (IO_01 | IO_03);
bkc_mbed8:88c89fd324bd 150}
bkc_mbed8:88c89fd324bd 151
bkc_mbed8:88c89fd324bd 152// 16bit output
bkc_mbed8:88c89fd324bd 153void ioShiftOut(unsigned int data)
bkc_mbed8:88c89fd324bd 154{
bkc_mbed8:88c89fd324bd 155 int i;
bkc_mbed8:88c89fd324bd 156
bkc_mbed8:88c89fd324bd 157 for(i = 0; i < 8; i++)
bkc_mbed8:88c89fd324bd 158 {
bkc_mbed8:88c89fd324bd 159 /* 2ビット分のデータをそれぞれ出力 */
bkc_mbed8:88c89fd324bd 160 if (data & 0x80)
bkc_mbed8:88c89fd324bd 161 LPC_GPIO0->DATA |= CTS;
bkc_mbed8:88c89fd324bd 162 else
bkc_mbed8:88c89fd324bd 163 LPC_GPIO0->DATA &= ~CTS;
bkc_mbed8:88c89fd324bd 164
bkc_mbed8:88c89fd324bd 165 if (data & 0x8000)
bkc_mbed8:88c89fd324bd 166 LPC_GPIO1->DATA |= DBS;
bkc_mbed8:88c89fd324bd 167 else
bkc_mbed9:834e9eed0d72 168 LPC_GPIO1->DATA &= ~DBS;
bkc_mbed8:88c89fd324bd 169
bkc_mbed8:88c89fd324bd 170 data <<= 1;
bkc_mbed8:88c89fd324bd 171
bkc_mbed8:88c89fd324bd 172 LPC_GPIO1->DATA |= SCK;
bkc_mbed9:834e9eed0d72 173 LPC_GPIO1->DATA &= ~SCK;
bkc_mbed8:88c89fd324bd 174 }
bkc_mbed8:88c89fd324bd 175
bkc_mbed8:88c89fd324bd 176 LPC_GPIO0->DATA |= RCK;
bkc_mbed8:88c89fd324bd 177 LPC_GPIO0->DATA &= ~RCK;
bkc_mbed8:88c89fd324bd 178
bkc_mbed8:88c89fd324bd 179}
bkc_mbed8:88c89fd324bd 180
bkc_mbed8:88c89fd324bd 181#endif
bkc_mbed8:88c89fd324bd 182
bkc_mbed2:f0b7779b03c7 183
bkc_mbed0:2053640461b5 184/* 制御信号定義 */
bkc_mbed0:2053640461b5 185#define CS_PSG (1 << 1)
bkc_mbed0:2053640461b5 186#define CS_FM1 (1 << 2)
bkc_mbed0:2053640461b5 187#define CS_FM2 (1 << 3)
bkc_mbed0:2053640461b5 188#define A0 (1 << 4)
bkc_mbed0:2053640461b5 189#define WR (1 << 5)
bkc_mbed0:2053640461b5 190#define ICL (1 << 6)
bkc_mbed0:2053640461b5 191
bkc_mbed0:2053640461b5 192/* アクティブローの制御信号 */
bkc_mbed0:2053640461b5 193#define ACTLOW (CS_PSG | CS_FM1 | CS_FM2 | WR | ICL)
bkc_mbed0:2053640461b5 194
bkc_mbed7:7e183b33c3f9 195#ifdef USE_NBV3
bkc_mbed10:b8d41133e499 196/* NBV3 16bit出力 */
bkc_mbed10:b8d41133e499 197void regOutBase(int addr, int data, int select)
bkc_mbed7:7e183b33c3f9 198{
bkc_mbed7:7e183b33c3f9 199 /* アドレスを出力 */
bkc_mbed7:7e183b33c3f9 200 /* A0をローにして待つ */
bkc_mbed7:7e183b33c3f9 201 IO_A0 = 0;
bkc_mbed7:7e183b33c3f9 202 ioShiftOut((addr << 8) | (ACTLOW & ~(select | WR)));
bkc_mbed7:7e183b33c3f9 203 ioShiftOut((addr << 8) | (ACTLOW));
bkc_mbed7:7e183b33c3f9 204
bkc_mbed7:7e183b33c3f9 205
bkc_mbed7:7e183b33c3f9 206 /* チップ側の処理を待つ */
bkc_mbed7:7e183b33c3f9 207
bkc_mbed7:7e183b33c3f9 208 /* データを出力 */
bkc_mbed7:7e183b33c3f9 209 /* A0をハイにして待つ */
bkc_mbed7:7e183b33c3f9 210 IO_A0 = 1;
bkc_mbed7:7e183b33c3f9 211 ioShiftOut((data << 8) | (ACTLOW & ~(select | WR)));
bkc_mbed7:7e183b33c3f9 212 ioShiftOut((data << 8) | (ACTLOW));
bkc_mbed0:2053640461b5 213
bkc_mbed7:7e183b33c3f9 214 /* 処理を待つ */
bkc_mbed7:7e183b33c3f9 215}
bkc_mbed7:7e183b33c3f9 216#else
bkc_mbed7:7e183b33c3f9 217/* NBV2互換出力 */
bkc_mbed0:2053640461b5 218void regOutBase(int addr, int data,int select)
bkc_mbed0:2053640461b5 219{
bkc_mbed0:2053640461b5 220 /* アドレスを出力 */
bkc_mbed0:2053640461b5 221 /* A0をローにして待つ */
bkc_mbed0:2053640461b5 222 ioShiftOut((addr << 8) | (ACTLOW));
bkc_mbed0:2053640461b5 223 ioShiftOut((addr << 8) | (ACTLOW & ~(select | WR)));
bkc_mbed0:2053640461b5 224 ioShiftOut((addr << 8) | (ACTLOW));
bkc_mbed0:2053640461b5 225
bkc_mbed7:7e183b33c3f9 226 /* チップ処理待ち */
bkc_mbed0:2053640461b5 227
bkc_mbed0:2053640461b5 228 /* データを出力 */
bkc_mbed0:2053640461b5 229 /* A0をハイにして待つ */
bkc_mbed0:2053640461b5 230 ioShiftOut((data << 8) | A0 | (ACTLOW));
bkc_mbed0:2053640461b5 231 ioShiftOut((data << 8) | A0 | (ACTLOW & ~(select | WR)));
bkc_mbed0:2053640461b5 232 ioShiftOut((data << 8) | A0 | (ACTLOW));
bkc_mbed0:2053640461b5 233
bkc_mbed7:7e183b33c3f9 234 /* チップ処理待ち */
bkc_mbed0:2053640461b5 235}
bkc_mbed7:7e183b33c3f9 236#endif
bkc_mbed0:2053640461b5 237
bkc_mbed0:2053640461b5 238/* PSG出力 */
bkc_mbed0:2053640461b5 239void regPSGOut(int addr, int data)
bkc_mbed0:2053640461b5 240{
bkc_mbed0:2053640461b5 241 regOutBase(addr, data, CS_PSG);
bkc_mbed0:2053640461b5 242}
bkc_mbed0:2053640461b5 243
bkc_mbed0:2053640461b5 244/* FM2出力 */
bkc_mbed0:2053640461b5 245void regFM2Out(int addr, int data)
bkc_mbed0:2053640461b5 246{
bkc_mbed0:2053640461b5 247 regOutBase(addr, data, CS_FM2);
bkc_mbed0:2053640461b5 248}
bkc_mbed0:2053640461b5 249
bkc_mbed0:2053640461b5 250/* FM音源にデータを出力 */
bkc_mbed0:2053640461b5 251void regFMOut(int addr,int data)
bkc_mbed0:2053640461b5 252{
bkc_mbed0:2053640461b5 253 regOutBase(addr, data, CS_FM1);
bkc_mbed0:2053640461b5 254}
bkc_mbed0:2053640461b5 255
bkc_mbed0:2053640461b5 256/* ボード消音 */
bkc_mbed0:2053640461b5 257void boardMute(void)
bkc_mbed0:2053640461b5 258{
bkc_mbed0:2053640461b5 259 int i;
bkc_mbed0:2053640461b5 260
bkc_mbed0:2053640461b5 261 /* PSG初期化 */
bkc_mbed0:2053640461b5 262 regPSGOut(0x00,0);
bkc_mbed0:2053640461b5 263 regPSGOut(0x01,0);
bkc_mbed0:2053640461b5 264
bkc_mbed3:aed228fa3ae4 265 regPSGOut(0x06, 0x00);
bkc_mbed3:aed228fa3ae4 266 regPSGOut(0x07, 0x3f); // ALL OFF
bkc_mbed3:aed228fa3ae4 267 regPSGOut(0x08, 0x00); // CH.A 0
bkc_mbed3:aed228fa3ae4 268 regPSGOut(0x09, 0x00); // CH.B 0
bkc_mbed3:aed228fa3ae4 269 regPSGOut(0x0a, 0x00); // CH.C 0
bkc_mbed0:2053640461b5 270
bkc_mbed7:7e183b33c3f9 271 /* MUTE(disable) */
bkc_mbed0:2053640461b5 272 for(i = 0x20; i < 0x28; i++)
bkc_mbed3:aed228fa3ae4 273 {
bkc_mbed0:2053640461b5 274 regFMOut(i, 0x00);
bkc_mbed3:aed228fa3ae4 275 regFM2Out(i, 0x00);
bkc_mbed3:aed228fa3ae4 276 }
bkc_mbed0:2053640461b5 277
bkc_mbed7:7e183b33c3f9 278 // KEYOFF
bkc_mbed3:aed228fa3ae4 279 for(i = 0x00; i < 0x08; i++)
bkc_mbed3:aed228fa3ae4 280 {
bkc_mbed3:aed228fa3ae4 281 regFMOut(0x08, i & 0x07);
bkc_mbed3:aed228fa3ae4 282 regFM2Out(0x08, i & 0x07);
bkc_mbed3:aed228fa3ae4 283 }
bkc_mbed7:7e183b33c3f9 284
bkc_mbed7:7e183b33c3f9 285 // FORCE RELEASE
bkc_mbed7:7e183b33c3f9 286 for(i= 0x00; i < 0x20; i++)
bkc_mbed7:7e183b33c3f9 287 {
bkc_mbed7:7e183b33c3f9 288 regFMOut(0xE0 + i, 0x0f);
bkc_mbed7:7e183b33c3f9 289 regFM2Out(0xE0 + i, 0x0f);
bkc_mbed7:7e183b33c3f9 290 }
bkc_mbed0:2053640461b5 291}
bkc_mbed0:2053640461b5 292
bkc_mbed0:2053640461b5 293
bkc_mbed0:2053640461b5 294/* ボード初期化 */
bkc_mbed0:2053640461b5 295void boardInit(void)
bkc_mbed0:2053640461b5 296{
bkc_mbed1:ec416e6d5739 297 wait_ms(20);
bkc_mbed0:2053640461b5 298 /* ICLのみをLOW(アクティブ)にする */
bkc_mbed0:2053640461b5 299 ioShiftOut(ACTLOW & ~(ICL));
bkc_mbed6:e48296dca6bb 300 wait_ms(150);
bkc_mbed0:2053640461b5 301
bkc_mbed0:2053640461b5 302 /* 元に戻す */
bkc_mbed0:2053640461b5 303 ioShiftOut(ACTLOW);
bkc_mbed1:ec416e6d5739 304 wait_ms(10);
bkc_mbed0:2053640461b5 305}
bkc_mbed0:2053640461b5 306
bkc_mbed0:2053640461b5 307typedef unsigned char byte;
bkc_mbed0:2053640461b5 308typedef unsigned short word;
bkc_mbed0:2053640461b5 309typedef unsigned long dword;
bkc_mbed0:2053640461b5 310
bkc_mbed0:2053640461b5 311
bkc_mbed0:2053640461b5 312// 変数読み出し(WORD)
bkc_mbed0:2053640461b5 313word ReadWORD(byte *p)
bkc_mbed0:2053640461b5 314{
bkc_mbed0:2053640461b5 315 return
bkc_mbed0:2053640461b5 316 ((word)p[0]) |
bkc_mbed0:2053640461b5 317 ((word)p[1])<<8;
bkc_mbed0:2053640461b5 318}
bkc_mbed0:2053640461b5 319
bkc_mbed0:2053640461b5 320// 変数読み出し(DWORD)
bkc_mbed0:2053640461b5 321dword ReadDWORD(byte *p)
bkc_mbed0:2053640461b5 322{
bkc_mbed0:2053640461b5 323 return
bkc_mbed0:2053640461b5 324 ((dword)p[0]) |
bkc_mbed0:2053640461b5 325 ((dword)p[1])<<8 |
bkc_mbed0:2053640461b5 326 ((dword)p[2])<<16 |
bkc_mbed0:2053640461b5 327 ((dword)p[3])<<24;
bkc_mbed0:2053640461b5 328}
bkc_mbed0:2053640461b5 329
bkc_mbed0:2053640461b5 330// NLGファイルを開く
bkc_mbed0:2053640461b5 331int OpenNLG(const char *file)
bkc_mbed0:2053640461b5 332{
bkc_mbed3:aed228fa3ae4 333 byte nlg_hdr[0x60];
bkc_mbed1:ec416e6d5739 334
bkc_mbed0:2053640461b5 335 nlg_file = fopen(file, "rb");
bkc_mbed0:2053640461b5 336
bkc_mbed0:2053640461b5 337 if (!nlg_file)
bkc_mbed0:2053640461b5 338 {
bkc_mbed0:2053640461b5 339 printf("File open error:%s\n", file);
bkc_mbed0:2053640461b5 340 return -1;
bkc_mbed0:2053640461b5 341 }
bkc_mbed0:2053640461b5 342
bkc_mbed0:2053640461b5 343 fread(nlg_hdr, 0x60, 1, nlg_file);
bkc_mbed0:2053640461b5 344
bkc_mbed6:e48296dca6bb 345 if (memcmp(nlg_hdr, "NLG1", 4) != 0)
bkc_mbed0:2053640461b5 346 {
bkc_mbed0:2053640461b5 347 printf("Unknown format!\n");
bkc_mbed0:2053640461b5 348 fclose(nlg_file);
bkc_mbed0:2053640461b5 349 return -1;
bkc_mbed0:2053640461b5 350 }
bkc_mbed0:2053640461b5 351
bkc_mbed0:2053640461b5 352 nlg_version = ReadWORD(nlg_hdr + 4);
bkc_mbed0:2053640461b5 353
bkc_mbed0:2053640461b5 354 memcpy(nlg_title, nlg_hdr + 8, 64);
bkc_mbed0:2053640461b5 355 nlg_title[64]=0;
bkc_mbed0:2053640461b5 356
bkc_mbed0:2053640461b5 357 nlg_baseclk = ReadDWORD(nlg_hdr + 72);
bkc_mbed0:2053640461b5 358 nlg_tick = ReadDWORD(nlg_hdr + 76);
bkc_mbed0:2053640461b5 359
bkc_mbed0:2053640461b5 360 nlg_length = ReadDWORD(nlg_hdr + 88);
bkc_mbed0:2053640461b5 361 nlg_loop_ptr = (long)ReadDWORD(nlg_hdr + 92);
bkc_mbed0:2053640461b5 362
bkc_mbed0:2053640461b5 363 fseek(nlg_file, 0x60, SEEK_SET);
bkc_mbed0:2053640461b5 364
bkc_mbed0:2053640461b5 365 nlg_ctc0 = nlg_ctc3 = 0;
bkc_mbed0:2053640461b5 366
bkc_mbed0:2053640461b5 367 return 0;
bkc_mbed0:2053640461b5 368}
bkc_mbed0:2053640461b5 369
bkc_mbed2:f0b7779b03c7 370#if 0
bkc_mbed2:f0b7779b03c7 371
bkc_mbed8:88c89fd324bd 372
bkc_mbed8:88c89fd324bd 373// 変数書き出し(WORD)
bkc_mbed8:88c89fd324bd 374inline void WriteWORD(byte *p,word val)
bkc_mbed8:88c89fd324bd 375{
bkc_mbed8:88c89fd324bd 376 p[0] = (val & 0xff);
bkc_mbed8:88c89fd324bd 377 p[1] = ((val>>8) & 0xff);
bkc_mbed8:88c89fd324bd 378}
bkc_mbed8:88c89fd324bd 379
bkc_mbed8:88c89fd324bd 380// 変数書き出し(DWORD)
bkc_mbed8:88c89fd324bd 381inline void WriteDWORD(byte *p,dword val)
bkc_mbed8:88c89fd324bd 382{
bkc_mbed8:88c89fd324bd 383 p[0] = (val & 0xff);
bkc_mbed8:88c89fd324bd 384 p[1] = ((val>>8) & 0xff);
bkc_mbed8:88c89fd324bd 385 p[2] = ((val>>16) & 0xff);
bkc_mbed8:88c89fd324bd 386 p[3] = ((val>>24) & 0xff);
bkc_mbed8:88c89fd324bd 387}
bkc_mbed8:88c89fd324bd 388
bkc_mbed0:2053640461b5 389// 書き込み用NLGファイルを開く
bkc_mbed0:2053640461b5 390int CreateNLG(const char *file)
bkc_mbed0:2053640461b5 391{
bkc_mbed0:2053640461b5 392 byte hdr[0x80];
bkc_mbed0:2053640461b5 393
bkc_mbed0:2053640461b5 394 nlg_file = fopen(file, "wb");
bkc_mbed0:2053640461b5 395
bkc_mbed0:2053640461b5 396 if (!nlg_file)
bkc_mbed0:2053640461b5 397 {
bkc_mbed0:2053640461b5 398 printf("File open error:%s\n", file);
bkc_mbed0:2053640461b5 399 return -1;
bkc_mbed0:2053640461b5 400 }
bkc_mbed0:2053640461b5 401
bkc_mbed0:2053640461b5 402 memset(hdr, 0, 0x80);
bkc_mbed0:2053640461b5 403 memcpy(hdr,"NLG1",4);
bkc_mbed0:2053640461b5 404
bkc_mbed0:2053640461b5 405 WriteWORD(hdr + 4, NLG_VER); // Version
bkc_mbed0:2053640461b5 406 WriteDWORD(hdr + 72, NLG_BASECLK); // BaseCLK
bkc_mbed0:2053640461b5 407 WriteDWORD(hdr + 76, 0); // Tick
bkc_mbed0:2053640461b5 408 WriteDWORD(hdr + 88, 0); // Length
bkc_mbed0:2053640461b5 409 WriteDWORD(hdr + 92, 0); // Loop Pointer
bkc_mbed0:2053640461b5 410
bkc_mbed0:2053640461b5 411 fwrite(hdr, 0x60, 1, nlg_file);
bkc_mbed0:2053640461b5 412
bkc_mbed0:2053640461b5 413 nlg_ctc0 = nlg_ctc3 = 0;
bkc_mbed0:2053640461b5 414
bkc_mbed0:2053640461b5 415 return 0;
bkc_mbed0:2053640461b5 416}
bkc_mbed0:2053640461b5 417
bkc_mbed0:2053640461b5 418
bkc_mbed0:2053640461b5 419// コマンドの書き込み
bkc_mbed0:2053640461b5 420void WriteNLG_CMD(int cmd)
bkc_mbed0:2053640461b5 421{
bkc_mbed0:2053640461b5 422 if (!nlg_file)
bkc_mbed0:2053640461b5 423 return;
bkc_mbed0:2053640461b5 424
bkc_mbed0:2053640461b5 425 fputc(cmd, nlg_file);
bkc_mbed0:2053640461b5 426}
bkc_mbed0:2053640461b5 427
bkc_mbed0:2053640461b5 428// CTC定数の書き込み
bkc_mbed0:2053640461b5 429void WriteNLG_CTC(int cmd,int ctc)
bkc_mbed0:2053640461b5 430{
bkc_mbed0:2053640461b5 431 if (!nlg_file)
bkc_mbed0:2053640461b5 432 return;
bkc_mbed0:2053640461b5 433
bkc_mbed0:2053640461b5 434 fputc(cmd, nlg_file);
bkc_mbed0:2053640461b5 435 fputc(ctc, nlg_file);
bkc_mbed0:2053640461b5 436}
bkc_mbed0:2053640461b5 437
bkc_mbed0:2053640461b5 438// データの書き込み
bkc_mbed0:2053640461b5 439void WriteNLG_Data(int cmd,int addr,int data)
bkc_mbed0:2053640461b5 440{
bkc_mbed0:2053640461b5 441 if (!nlg_file)
bkc_mbed0:2053640461b5 442 return;
bkc_mbed0:2053640461b5 443
bkc_mbed0:2053640461b5 444 fputc(cmd, nlg_file);
bkc_mbed0:2053640461b5 445 fputc(addr, nlg_file);
bkc_mbed0:2053640461b5 446 fputc(data, nlg_file);
bkc_mbed0:2053640461b5 447}
bkc_mbed0:2053640461b5 448
bkc_mbed2:f0b7779b03c7 449#endif
bkc_mbed2:f0b7779b03c7 450
bkc_mbed2:f0b7779b03c7 451// ファイルを閉じる
bkc_mbed2:f0b7779b03c7 452void CloseNLG(void)
bkc_mbed2:f0b7779b03c7 453{
bkc_mbed2:f0b7779b03c7 454 if (!nlg_file)
bkc_mbed2:f0b7779b03c7 455 return;
bkc_mbed2:f0b7779b03c7 456
bkc_mbed2:f0b7779b03c7 457 fclose(nlg_file);
bkc_mbed8:88c89fd324bd 458
bkc_mbed2:f0b7779b03c7 459#if defined(__MICROLIB) && defined(__ARMCC_VERSION) // with microlib and ARM compiler
bkc_mbed2:f0b7779b03c7 460 free(nlg_file);
bkc_mbed2:f0b7779b03c7 461#endif
bkc_mbed2:f0b7779b03c7 462
bkc_mbed2:f0b7779b03c7 463 nlg_file = NULL;
bkc_mbed2:f0b7779b03c7 464}
bkc_mbed2:f0b7779b03c7 465
bkc_mbed0:2053640461b5 466// データの読み出し
bkc_mbed8:88c89fd324bd 467inline int ReadNLG(void)
bkc_mbed0:2053640461b5 468{
bkc_mbed0:2053640461b5 469 return fgetc(nlg_file);
bkc_mbed0:2053640461b5 470}
bkc_mbed0:2053640461b5 471
bkc_mbed0:2053640461b5 472// ファイルポインタの位置を取得
bkc_mbed8:88c89fd324bd 473inline long TellNLG(void)
bkc_mbed0:2053640461b5 474{
bkc_mbed0:2053640461b5 475 return ftell(nlg_file);
bkc_mbed0:2053640461b5 476}
bkc_mbed0:2053640461b5 477
bkc_mbed0:2053640461b5 478// ファイルポインタの位置を設定
bkc_mbed8:88c89fd324bd 479inline void SeekNLG(long pos)
bkc_mbed0:2053640461b5 480{
bkc_mbed0:2053640461b5 481 fseek(nlg_file, pos, SEEK_SET);
bkc_mbed0:2053640461b5 482}
bkc_mbed0:2053640461b5 483
bkc_mbed0:2053640461b5 484// タイトルの取得
bkc_mbed8:88c89fd324bd 485inline char *GetTitleNLG(void)
bkc_mbed0:2053640461b5 486{
bkc_mbed0:2053640461b5 487 return nlg_title;
bkc_mbed0:2053640461b5 488}
bkc_mbed0:2053640461b5 489
bkc_mbed0:2053640461b5 490// ティックの取得
bkc_mbed8:88c89fd324bd 491inline int GetTickNLG(void)
bkc_mbed0:2053640461b5 492{
bkc_mbed0:2053640461b5 493 return nlg_tick;
bkc_mbed0:2053640461b5 494}
bkc_mbed0:2053640461b5 495
bkc_mbed3:aed228fa3ae4 496// Set Tick
bkc_mbed3:aed228fa3ae4 497inline void SetTick(void)
bkc_mbed3:aed228fa3ae4 498{
bkc_mbed3:aed228fa3ae4 499 nlg_tick = ((nlg_ctc0 * 256) * nlg_ctc3);
bkc_mbed3:aed228fa3ae4 500 // printf("nlg_tick=%d\n", nlg_tick);
bkc_mbed3:aed228fa3ae4 501}
bkc_mbed3:aed228fa3ae4 502
bkc_mbed0:2053640461b5 503// CTC0値の設定
bkc_mbed0:2053640461b5 504void SetCTC0_NLG(int value)
bkc_mbed0:2053640461b5 505{
bkc_mbed0:2053640461b5 506 nlg_ctc0 = value;
bkc_mbed3:aed228fa3ae4 507 SetTick();
bkc_mbed0:2053640461b5 508}
bkc_mbed0:2053640461b5 509
bkc_mbed0:2053640461b5 510// CTC3値の設定
bkc_mbed0:2053640461b5 511void SetCTC3_NLG(int value)
bkc_mbed0:2053640461b5 512{
bkc_mbed0:2053640461b5 513 nlg_ctc3 = value;
bkc_mbed3:aed228fa3ae4 514 SetTick();
bkc_mbed0:2053640461b5 515}
bkc_mbed0:2053640461b5 516
bkc_mbed0:2053640461b5 517// 曲の長さを得る
bkc_mbed0:2053640461b5 518int GetLengthNLG(void)
bkc_mbed0:2053640461b5 519{
bkc_mbed0:2053640461b5 520 return nlg_length;
bkc_mbed0:2053640461b5 521}
bkc_mbed0:2053640461b5 522
bkc_mbed0:2053640461b5 523// ループポインタを得る
bkc_mbed0:2053640461b5 524int GetLoopPtrNLG(void)
bkc_mbed0:2053640461b5 525{
bkc_mbed0:2053640461b5 526 return nlg_loop_ptr;
bkc_mbed0:2053640461b5 527}
bkc_mbed0:2053640461b5 528
bkc_mbed0:2053640461b5 529// ベースクロックを得る
bkc_mbed0:2053640461b5 530int GetBaseClkNLG(void)
bkc_mbed0:2053640461b5 531{
bkc_mbed0:2053640461b5 532 return nlg_baseclk;
bkc_mbed0:2053640461b5 533}
bkc_mbed0:2053640461b5 534
bkc_mbed0:2053640461b5 535#define CMD_PSG 0x00
bkc_mbed0:2053640461b5 536#define CMD_OPM 0x01
bkc_mbed0:2053640461b5 537#define CMD_OPM2 0x02
bkc_mbed0:2053640461b5 538#define CMD_IRQ 0x80
bkc_mbed0:2053640461b5 539
bkc_mbed0:2053640461b5 540#define CMD_CTC0 0x81
bkc_mbed0:2053640461b5 541#define CMD_CTC3 0x82
bkc_mbed0:2053640461b5 542
bkc_mbed0:2053640461b5 543
bkc_mbed0:2053640461b5 544/* 単位を扱いやすいように */
bkc_mbed0:2053640461b5 545typedef unsigned char byte;
bkc_mbed0:2053640461b5 546typedef unsigned short word;
bkc_mbed0:2053640461b5 547typedef unsigned long dword;
bkc_mbed0:2053640461b5 548
bkc_mbed0:2053640461b5 549/* NLGを処理するための構造体 */
bkc_mbed0:2053640461b5 550typedef struct
bkc_mbed0:2053640461b5 551{
bkc_mbed0:2053640461b5 552 int base_clk;
bkc_mbed0:2053640461b5 553 int clk_per_sample;
bkc_mbed0:2053640461b5 554
bkc_mbed0:2053640461b5 555 int freeze;
bkc_mbed0:2053640461b5 556 int total_samples;
bkc_mbed0:2053640461b5 557
bkc_mbed0:2053640461b5 558 int total_sec;
bkc_mbed0:2053640461b5 559
bkc_mbed0:2053640461b5 560 int current_samples;
bkc_mbed0:2053640461b5 561 int tick;
bkc_mbed0:2053640461b5 562 int tick_sec;
bkc_mbed0:2053640461b5 563
bkc_mbed0:2053640461b5 564 int irq_count;
bkc_mbed0:2053640461b5 565 int irq_loop;
bkc_mbed0:2053640461b5 566 long loop_address;
bkc_mbed2:f0b7779b03c7 567
bkc_mbed1:ec416e6d5739 568 int irq_counter;
bkc_mbed1:ec416e6d5739 569 int irq_counter2;
bkc_mbed1:ec416e6d5739 570
bkc_mbed0:2053640461b5 571} NLG_R;
bkc_mbed0:2053640461b5 572
bkc_mbed0:2053640461b5 573
bkc_mbed0:2053640461b5 574/* 初期化 */
bkc_mbed0:2053640461b5 575int initNLG(NLG_R *np, const char *nlg_name)
bkc_mbed0:2053640461b5 576{
bkc_mbed0:2053640461b5 577 if (OpenNLG(nlg_name) < 0)
bkc_mbed0:2053640461b5 578 return -1;
bkc_mbed0:2053640461b5 579
bkc_mbed0:2053640461b5 580 np->base_clk = GetBaseClkNLG();
bkc_mbed0:2053640461b5 581
bkc_mbed0:2053640461b5 582 /* printf("Title:%s %d %d %dsec %d\n",
bkc_mbed0:2053640461b5 583 GetTitleNLG(),
bkc_mbed0:2053640461b5 584 GetBaseClkNLG(),
bkc_mbed0:2053640461b5 585 GetTickNLG(),
bkc_mbed0:2053640461b5 586 GetLengthNLG(),
bkc_mbed0:2053640461b5 587 GetLoopPtrNLG());
bkc_mbed0:2053640461b5 588 */
bkc_mbed0:2053640461b5 589
bkc_mbed0:2053640461b5 590 np->total_samples = 0;
bkc_mbed0:2053640461b5 591 np->current_samples = 0;
bkc_mbed0:2053640461b5 592
bkc_mbed0:2053640461b5 593 np->total_sec = 0;
bkc_mbed0:2053640461b5 594
bkc_mbed0:2053640461b5 595 np->tick = 0;
bkc_mbed0:2053640461b5 596 np->tick_sec = 0;
bkc_mbed0:2053640461b5 597
bkc_mbed0:2053640461b5 598 np->freeze = 0;
bkc_mbed0:2053640461b5 599
bkc_mbed0:2053640461b5 600 np->irq_count = 0;
bkc_mbed0:2053640461b5 601 np->irq_loop = GetLoopPtrNLG();
bkc_mbed0:2053640461b5 602 np->loop_address = -1;
bkc_mbed2:f0b7779b03c7 603
bkc_mbed1:ec416e6d5739 604 np->irq_counter = 0;
bkc_mbed1:ec416e6d5739 605 np->irq_counter2 = 0;
bkc_mbed0:2053640461b5 606
bkc_mbed0:2053640461b5 607 return 0;
bkc_mbed0:2053640461b5 608}
bkc_mbed0:2053640461b5 609
bkc_mbed0:2053640461b5 610/* PSGへの出力 */
bkc_mbed0:2053640461b5 611void WritePSG(int addr,int value)
bkc_mbed0:2053640461b5 612{
bkc_mbed0:2053640461b5 613 regPSGOut(addr, value);
bkc_mbed0:2053640461b5 614}
bkc_mbed0:2053640461b5 615
bkc_mbed0:2053640461b5 616/* FM1への出力 */
bkc_mbed0:2053640461b5 617void WriteOPM(int addr,int value)
bkc_mbed0:2053640461b5 618{
bkc_mbed0:2053640461b5 619 regFMOut(addr, value);
bkc_mbed0:2053640461b5 620}
bkc_mbed0:2053640461b5 621
bkc_mbed0:2053640461b5 622/* FM2への出力 */
bkc_mbed0:2053640461b5 623void WriteOPM2(int addr,int value)
bkc_mbed0:2053640461b5 624{
bkc_mbed0:2053640461b5 625 regFM2Out(addr, value);
bkc_mbed0:2053640461b5 626}
bkc_mbed0:2053640461b5 627
bkc_mbed6:e48296dca6bb 628// stop flag
bkc_mbed6:e48296dca6bb 629bool g_stop = false;
bkc_mbed6:e48296dca6bb 630bool g_prev = false;
bkc_mbed0:2053640461b5 631
bkc_mbed0:2053640461b5 632void DispNLG(NLG_R *np)
bkc_mbed0:2053640461b5 633{
bkc_mbed1:ec416e6d5739 634 // printf("Time %02d:%02d\r",np->total_sec / 60, np->total_sec % 60);
bkc_mbed1:ec416e6d5739 635 // fflush(stdout);
bkc_mbed0:2053640461b5 636
bkc_mbed0:2053640461b5 637 char buf[16];
bkc_mbed2:f0b7779b03c7 638 sprintf(buf, "%02d:%02d",
bkc_mbed2:f0b7779b03c7 639 np->total_sec / 60,
bkc_mbed2:f0b7779b03c7 640 np->total_sec % 60);
bkc_mbed0:2053640461b5 641
bkc_mbed2:f0b7779b03c7 642 lcd_setCursor(3,1);
bkc_mbed2:f0b7779b03c7 643 lcd_printStr(buf);
bkc_mbed0:2053640461b5 644}
bkc_mbed0:2053640461b5 645
bkc_mbed6:e48296dca6bb 646// wait until release buttons
bkc_mbed6:e48296dca6bb 647void waitButtonRelease(void)
bkc_mbed6:e48296dca6bb 648{
bkc_mbed6:e48296dca6bb 649 while(!sw_next || !sw_play || !sw_prev);
bkc_mbed6:e48296dca6bb 650}
bkc_mbed6:e48296dca6bb 651
bkc_mbed6:e48296dca6bb 652// wait until press buttons
bkc_mbed6:e48296dca6bb 653void waitButtonPress(void)
bkc_mbed6:e48296dca6bb 654{
bkc_mbed6:e48296dca6bb 655 while(sw_next && sw_play && sw_prev);
bkc_mbed6:e48296dca6bb 656}
bkc_mbed6:e48296dca6bb 657
bkc_mbed0:2053640461b5 658/* NLGの再生 */
bkc_mbed0:2053640461b5 659int PlayNLG(NLG_R *np, int sec)
bkc_mbed0:2053640461b5 660{
bkc_mbed0:2053640461b5 661 int cmd;
bkc_mbed0:2053640461b5 662 int addr, data;
bkc_mbed0:2053640461b5 663 int result = 0;
bkc_mbed0:2053640461b5 664
bkc_mbed0:2053640461b5 665 int tick;
bkc_mbed0:2053640461b5 666 int total_us = 0;
bkc_mbed0:2053640461b5 667 int us_tick = np->base_clk / 1000000;
bkc_mbed0:2053640461b5 668
bkc_mbed2:f0b7779b03c7 669 printf("start play\n");
bkc_mbed0:2053640461b5 670 Timer t;
bkc_mbed0:2053640461b5 671
bkc_mbed0:2053640461b5 672 t.start();
bkc_mbed0:2053640461b5 673 total_us = 0;
bkc_mbed0:2053640461b5 674
bkc_mbed0:2053640461b5 675 // wait until release buttons
bkc_mbed6:e48296dca6bb 676 waitButtonRelease();
bkc_mbed0:2053640461b5 677
bkc_mbed0:2053640461b5 678 DispNLG(np);
bkc_mbed0:2053640461b5 679
bkc_mbed0:2053640461b5 680 while(np->total_sec < sec && !g_stop)
bkc_mbed0:2053640461b5 681 {
bkc_mbed0:2053640461b5 682 // push next
bkc_mbed0:2053640461b5 683 if (!sw_next)
bkc_mbed0:2053640461b5 684 break;
bkc_mbed6:e48296dca6bb 685
bkc_mbed6:e48296dca6bb 686 // push prev
bkc_mbed6:e48296dca6bb 687 if (!sw_prev)
bkc_mbed6:e48296dca6bb 688 {
bkc_mbed6:e48296dca6bb 689 g_prev = true;
bkc_mbed6:e48296dca6bb 690 break;
bkc_mbed6:e48296dca6bb 691 }
bkc_mbed0:2053640461b5 692
bkc_mbed0:2053640461b5 693 // push play
bkc_mbed0:2053640461b5 694 if (!sw_play)
bkc_mbed0:2053640461b5 695 {
bkc_mbed6:e48296dca6bb 696 g_stop = true;
bkc_mbed0:2053640461b5 697 break;
bkc_mbed0:2053640461b5 698 }
bkc_mbed0:2053640461b5 699
bkc_mbed0:2053640461b5 700 /* ウエイトの処理 */
bkc_mbed0:2053640461b5 701 while (np->tick >= us_tick)
bkc_mbed0:2053640461b5 702 {
bkc_mbed0:2053640461b5 703 int us = np->tick / us_tick;
bkc_mbed0:2053640461b5 704 np->tick -= (us * us_tick);
bkc_mbed0:2053640461b5 705
bkc_mbed10:b8d41133e499 706 // 次のタイミングまで待つ
bkc_mbed0:2053640461b5 707 while(t.read_us() < total_us + us);
bkc_mbed3:aed228fa3ae4 708
bkc_mbed0:2053640461b5 709 total_us += us;
bkc_mbed0:2053640461b5 710
bkc_mbed10:b8d41133e499 711 // 1秒ごとにリセットする
bkc_mbed0:2053640461b5 712 if (total_us >= 1000000)
bkc_mbed0:2053640461b5 713 {
bkc_mbed0:2053640461b5 714 total_us -= t.read_us();
bkc_mbed0:2053640461b5 715 t.reset();
bkc_mbed0:2053640461b5 716 }
bkc_mbed0:2053640461b5 717 // DELAY_NEXT(us);
bkc_mbed0:2053640461b5 718
bkc_mbed0:2053640461b5 719 // printf("delay : %dus\n",us);
bkc_mbed0:2053640461b5 720 }
bkc_mbed0:2053640461b5 721
bkc_mbed0:2053640461b5 722 /* コマンドの読み出し */
bkc_mbed0:2053640461b5 723 cmd = ReadNLG();
bkc_mbed0:2053640461b5 724 if (cmd == EOF)
bkc_mbed0:2053640461b5 725 {
bkc_mbed0:2053640461b5 726 if (np->loop_address >= 0)
bkc_mbed0:2053640461b5 727 {
bkc_mbed0:2053640461b5 728 SeekNLG(np->loop_address);
bkc_mbed0:2053640461b5 729 cmd = ReadNLG();
bkc_mbed0:2053640461b5 730 }
bkc_mbed0:2053640461b5 731 else
bkc_mbed0:2053640461b5 732 {
bkc_mbed0:2053640461b5 733 result = EOF;
bkc_mbed0:2053640461b5 734 break;
bkc_mbed0:2053640461b5 735 }
bkc_mbed0:2053640461b5 736 }
bkc_mbed0:2053640461b5 737
bkc_mbed0:2053640461b5 738 /* コマンドの処理 */
bkc_mbed0:2053640461b5 739 switch (cmd)
bkc_mbed0:2053640461b5 740 {
bkc_mbed0:2053640461b5 741 case CMD_PSG:
bkc_mbed0:2053640461b5 742 addr = ReadNLG();
bkc_mbed0:2053640461b5 743 data = ReadNLG();
bkc_mbed0:2053640461b5 744 WritePSG(addr, data);
bkc_mbed0:2053640461b5 745 break;
bkc_mbed0:2053640461b5 746 case CMD_OPM:
bkc_mbed0:2053640461b5 747 addr = ReadNLG();
bkc_mbed0:2053640461b5 748 data = ReadNLG();
bkc_mbed0:2053640461b5 749 WriteOPM(addr, data);
bkc_mbed0:2053640461b5 750 break;
bkc_mbed0:2053640461b5 751 case CMD_OPM2:
bkc_mbed0:2053640461b5 752 addr = ReadNLG();
bkc_mbed0:2053640461b5 753 data = ReadNLG();
bkc_mbed0:2053640461b5 754 WriteOPM2(addr, data);
bkc_mbed0:2053640461b5 755 break;
bkc_mbed0:2053640461b5 756 case CMD_IRQ:
bkc_mbed0:2053640461b5 757 tick = GetTickNLG();
bkc_mbed0:2053640461b5 758 np->tick_sec += tick;
bkc_mbed0:2053640461b5 759 np->tick += tick;
bkc_mbed0:2053640461b5 760 np->irq_count++;
bkc_mbed0:2053640461b5 761 if (np->irq_count == np->irq_loop)
bkc_mbed0:2053640461b5 762 {
bkc_mbed0:2053640461b5 763 /* ループ位置の設定 */
bkc_mbed0:2053640461b5 764 np->loop_address = TellNLG();
bkc_mbed0:2053640461b5 765 np->loop_address -= 1;
bkc_mbed0:2053640461b5 766 }
bkc_mbed1:ec416e6d5739 767 np->irq_counter++;
bkc_mbed1:ec416e6d5739 768 if (np->irq_counter >= 48)
bkc_mbed1:ec416e6d5739 769 {
bkc_mbed1:ec416e6d5739 770 np->irq_counter = 0;
bkc_mbed1:ec416e6d5739 771 led1 = !led1;
bkc_mbed1:ec416e6d5739 772
bkc_mbed1:ec416e6d5739 773 np->irq_counter2++;
bkc_mbed1:ec416e6d5739 774 if (np->irq_counter2 >= 4)
bkc_mbed1:ec416e6d5739 775 {
bkc_mbed1:ec416e6d5739 776 np->irq_counter2 = 0;
bkc_mbed1:ec416e6d5739 777 led2 = !led2;
bkc_mbed1:ec416e6d5739 778 }
bkc_mbed1:ec416e6d5739 779 }
bkc_mbed0:2053640461b5 780 break;
bkc_mbed0:2053640461b5 781 case CMD_CTC0:
bkc_mbed0:2053640461b5 782 SetCTC0_NLG(ReadNLG());
bkc_mbed0:2053640461b5 783 break;
bkc_mbed0:2053640461b5 784 case CMD_CTC3:
bkc_mbed0:2053640461b5 785 SetCTC3_NLG(ReadNLG());
bkc_mbed0:2053640461b5 786 break;
bkc_mbed0:2053640461b5 787
bkc_mbed0:2053640461b5 788 }
bkc_mbed0:2053640461b5 789 /* 秒情報の表示 */
bkc_mbed0:2053640461b5 790 while (np->tick_sec >= np->base_clk)
bkc_mbed0:2053640461b5 791 {
bkc_mbed0:2053640461b5 792 np->tick_sec -= np->base_clk;
bkc_mbed0:2053640461b5 793 np->total_sec++;
bkc_mbed0:2053640461b5 794
bkc_mbed0:2053640461b5 795 DispNLG(np);
bkc_mbed0:2053640461b5 796 }
bkc_mbed0:2053640461b5 797 }
bkc_mbed0:2053640461b5 798
bkc_mbed0:2053640461b5 799 return result;
bkc_mbed0:2053640461b5 800}
bkc_mbed0:2053640461b5 801
bkc_mbed0:2053640461b5 802NLG_R n;
bkc_mbed0:2053640461b5 803
bkc_mbed2:f0b7779b03c7 804int nlg_play(const char *nlg_file)
bkc_mbed0:2053640461b5 805{
bkc_mbed1:ec416e6d5739 806 printf("nlg_play:%s\n",nlg_file);
bkc_mbed0:2053640461b5 807
bkc_mbed0:2053640461b5 808 /* NLGの初期化 */
bkc_mbed0:2053640461b5 809 if (initNLG(&n, nlg_file) < 0)
bkc_mbed0:2053640461b5 810 {
bkc_mbed7:7e183b33c3f9 811 lcd_printStrY(1, " ERROR!!");
bkc_mbed0:2053640461b5 812 printf("Failed to init.\n");
bkc_mbed0:2053640461b5 813 return -1;
bkc_mbed0:2053640461b5 814 }
bkc_mbed2:f0b7779b03c7 815
bkc_mbed2:f0b7779b03c7 816 printf("Play..\n");
bkc_mbed1:ec416e6d5739 817
bkc_mbed0:2053640461b5 818 /* 再生する */
bkc_mbed0:2053640461b5 819 PlayNLG(&n, 360);
bkc_mbed0:2053640461b5 820
bkc_mbed6:e48296dca6bb 821 /* mute */
bkc_mbed0:2053640461b5 822 boardMute();
bkc_mbed0:2053640461b5 823
bkc_mbed0:2053640461b5 824 /* NLGファイルを閉じる */
bkc_mbed0:2053640461b5 825 CloseNLG();
bkc_mbed0:2053640461b5 826
bkc_mbed0:2053640461b5 827 return 0;
bkc_mbed0:2053640461b5 828}
bkc_mbed0:2053640461b5 829
bkc_mbed10:b8d41133e499 830void error_sdcard(void)
bkc_mbed10:b8d41133e499 831{
bkc_mbed10:b8d41133e499 832 lcd_printStr2("SD CARD", "ERROR!");
bkc_mbed10:b8d41133e499 833
bkc_mbed10:b8d41133e499 834 while(1);
bkc_mbed10:b8d41133e499 835}
bkc_mbed10:b8d41133e499 836
bkc_mbed6:e48296dca6bb 837int get_nlg_file(char *dest, int index)
bkc_mbed0:2053640461b5 838{
bkc_mbed0:2053640461b5 839 char *cwd = "/sd/";
bkc_mbed0:2053640461b5 840 int count = -1;
bkc_mbed0:2053640461b5 841 DIR *dir = opendir(cwd);
bkc_mbed0:2053640461b5 842
bkc_mbed0:2053640461b5 843 dest[0] = 0;
bkc_mbed0:2053640461b5 844
bkc_mbed0:2053640461b5 845 if (dir == NULL)
bkc_mbed0:2053640461b5 846 {
bkc_mbed0:2053640461b5 847 return -1;
bkc_mbed0:2053640461b5 848 }
bkc_mbed0:2053640461b5 849 struct dirent *dent;
bkc_mbed0:2053640461b5 850
bkc_mbed0:2053640461b5 851 while(1)
bkc_mbed0:2053640461b5 852 {
bkc_mbed0:2053640461b5 853 dent = readdir(dir);
bkc_mbed0:2053640461b5 854 if (!dent)
bkc_mbed0:2053640461b5 855 break;
bkc_mbed0:2053640461b5 856
bkc_mbed0:2053640461b5 857 // resource or hidden file
bkc_mbed0:2053640461b5 858 if (dent->d_name[0] == '.')
bkc_mbed0:2053640461b5 859 continue;
bkc_mbed0:2053640461b5 860
bkc_mbed0:2053640461b5 861 char *ext = strrchr(dent->d_name, '.');
bkc_mbed0:2053640461b5 862 if (!ext)
bkc_mbed0:2053640461b5 863 continue;
bkc_mbed0:2053640461b5 864
bkc_mbed0:2053640461b5 865 if (strcasecmp(ext, ".nlg") != 0)
bkc_mbed0:2053640461b5 866 continue;
bkc_mbed0:2053640461b5 867
bkc_mbed0:2053640461b5 868 if (count < 0)
bkc_mbed0:2053640461b5 869 count = 0;
bkc_mbed0:2053640461b5 870
bkc_mbed0:2053640461b5 871 count++;
bkc_mbed0:2053640461b5 872
bkc_mbed0:2053640461b5 873 if (index < 0)
bkc_mbed0:2053640461b5 874 continue;
bkc_mbed0:2053640461b5 875
bkc_mbed0:2053640461b5 876 if (count <= index)
bkc_mbed0:2053640461b5 877 continue;
bkc_mbed0:2053640461b5 878
bkc_mbed0:2053640461b5 879 strcpy(dest, dent->d_name);
bkc_mbed0:2053640461b5 880 break;
bkc_mbed0:2053640461b5 881 }
bkc_mbed0:2053640461b5 882 closedir(dir);
bkc_mbed0:2053640461b5 883 return count;
bkc_mbed0:2053640461b5 884}
bkc_mbed0:2053640461b5 885
bkc_mbed10:b8d41133e499 886//
bkc_mbed10:b8d41133e499 887// COM mode
bkc_mbed10:b8d41133e499 888//
bkc_mbed10:b8d41133e499 889void loop_for_com()
bkc_mbed10:b8d41133e499 890{
bkc_mbed10:b8d41133e499 891 Serial pc(USBTX, USBRX);
bkc_mbed10:b8d41133e499 892
bkc_mbed10:b8d41133e499 893 lcd_printStrY(1,"COM MODE");
bkc_mbed10:b8d41133e499 894
bkc_mbed10:b8d41133e499 895 pc.printf("com_mode\n");
bkc_mbed10:b8d41133e499 896
bkc_mbed10:b8d41133e499 897 while(1)
bkc_mbed10:b8d41133e499 898 {
bkc_mbed10:b8d41133e499 899 unsigned char sw, adr, val;
bkc_mbed10:b8d41133e499 900
bkc_mbed10:b8d41133e499 901 sw = pc.getc();
bkc_mbed10:b8d41133e499 902 if (sw == 0x00)
bkc_mbed10:b8d41133e499 903 continue;
bkc_mbed10:b8d41133e499 904
bkc_mbed10:b8d41133e499 905 adr = pc.getc();
bkc_mbed10:b8d41133e499 906 val = pc.getc();
bkc_mbed10:b8d41133e499 907 switch(sw)
bkc_mbed10:b8d41133e499 908 {
bkc_mbed10:b8d41133e499 909 case 1:
bkc_mbed10:b8d41133e499 910 regFMOut(adr, val);
bkc_mbed10:b8d41133e499 911 break;
bkc_mbed10:b8d41133e499 912 case 2:
bkc_mbed10:b8d41133e499 913 regFM2Out(adr, val);
bkc_mbed10:b8d41133e499 914 break;
bkc_mbed10:b8d41133e499 915 case 3:
bkc_mbed10:b8d41133e499 916 regPSGOut(adr, val);
bkc_mbed10:b8d41133e499 917 break;
bkc_mbed10:b8d41133e499 918 case 0x0f:
bkc_mbed10:b8d41133e499 919 pc.baud(9600 * val);
bkc_mbed10:b8d41133e499 920 break;
bkc_mbed10:b8d41133e499 921 }
bkc_mbed10:b8d41133e499 922
bkc_mbed10:b8d41133e499 923 // pc.printf("sw = %02x, adr = %02x, val = %02x\n", sw, adr, val);
bkc_mbed10:b8d41133e499 924 }
bkc_mbed10:b8d41133e499 925}
bkc_mbed6:e48296dca6bb 926
bkc_mbed6:e48296dca6bb 927//
bkc_mbed6:e48296dca6bb 928// title
bkc_mbed6:e48296dca6bb 929//
bkc_mbed6:e48296dca6bb 930void putTitle()
bkc_mbed6:e48296dca6bb 931{
bkc_mbed10:b8d41133e499 932 lcd_printStr2("NBCTRL", NBCTRL_VER);
bkc_mbed7:7e183b33c3f9 933 wait(1.5);
bkc_mbed6:e48296dca6bb 934}
bkc_mbed6:e48296dca6bb 935
bkc_mbed10:b8d41133e499 936// 情報の表示
bkc_mbed10:b8d41133e499 937void show_info(int files)
bkc_mbed10:b8d41133e499 938{
bkc_mbed10:b8d41133e499 939 char buf[16];
bkc_mbed10:b8d41133e499 940
bkc_mbed10:b8d41133e499 941 Timer t;
bkc_mbed10:b8d41133e499 942 int result_us = 0;
bkc_mbed10:b8d41133e499 943 t.reset();
bkc_mbed10:b8d41133e499 944 t.start();
bkc_mbed10:b8d41133e499 945
bkc_mbed10:b8d41133e499 946 // actual timing
bkc_mbed10:b8d41133e499 947 regFMOut(0x20, 0x00);
bkc_mbed10:b8d41133e499 948
bkc_mbed10:b8d41133e499 949 result_us = t.read_us();
bkc_mbed10:b8d41133e499 950 t.stop();
bkc_mbed10:b8d41133e499 951
bkc_mbed10:b8d41133e499 952 printf("result_us=%dus\n", result_us);
bkc_mbed10:b8d41133e499 953 sprintf(buf, "R:%dus", result_us);
bkc_mbed10:b8d41133e499 954 lcd_printStrY(1, buf);
bkc_mbed10:b8d41133e499 955 wait(3);
bkc_mbed10:b8d41133e499 956
bkc_mbed10:b8d41133e499 957 sprintf(buf, "%8s", __DATE__);
bkc_mbed10:b8d41133e499 958 lcd_printStrYscr(1, buf);
bkc_mbed10:b8d41133e499 959 wait(3);
bkc_mbed10:b8d41133e499 960
bkc_mbed10:b8d41133e499 961 if (files < 0)
bkc_mbed10:b8d41133e499 962 lcd_printStrY(1, "NO FILES");
bkc_mbed10:b8d41133e499 963 else
bkc_mbed10:b8d41133e499 964 {
bkc_mbed10:b8d41133e499 965 char buf[16];
bkc_mbed10:b8d41133e499 966 sprintf(buf, "%3dfiles", files);
bkc_mbed10:b8d41133e499 967 lcd_printStrY(1, buf);
bkc_mbed10:b8d41133e499 968 }
bkc_mbed10:b8d41133e499 969 wait(3);
bkc_mbed10:b8d41133e499 970 lcd_cls();
bkc_mbed10:b8d41133e499 971}
bkc_mbed10:b8d41133e499 972
bkc_mbed6:e48296dca6bb 973//
bkc_mbed6:e48296dca6bb 974// main
bkc_mbed6:e48296dca6bb 975//
bkc_mbed0:2053640461b5 976int main()
bkc_mbed0:2053640461b5 977{
bkc_mbed10:b8d41133e499 978 bool info_mode = false;
bkc_mbed10:b8d41133e499 979 bool com_mode = false;
bkc_mbed10:b8d41133e499 980
bkc_mbed10:b8d41133e499 981 int files = 0;
bkc_mbed10:b8d41133e499 982
bkc_mbed0:2053640461b5 983 char file[32];
bkc_mbed3:aed228fa3ae4 984 char path[48];
bkc_mbed0:2053640461b5 985
bkc_mbed8:88c89fd324bd 986 ioInit();
bkc_mbed8:88c89fd324bd 987
bkc_mbed6:e48296dca6bb 988 wait_ms(20);
bkc_mbed6:e48296dca6bb 989
bkc_mbed6:e48296dca6bb 990 // reset SHIFT REGISTER
bkc_mbed7:7e183b33c3f9 991 ioShiftOut((0xFF << 8) | (ACTLOW));
bkc_mbed4:4dc67a366727 992
bkc_mbed10:b8d41133e499 993 // モード
bkc_mbed10:b8d41133e499 994 if (!sw_next)
bkc_mbed10:b8d41133e499 995 info_mode = true;
bkc_mbed10:b8d41133e499 996 if (!sw_prev)
bkc_mbed10:b8d41133e499 997 com_mode = true;
bkc_mbed10:b8d41133e499 998
bkc_mbed10:b8d41133e499 999 // 初期化とタイトル表示
bkc_mbed0:2053640461b5 1000 lcd_init();
bkc_mbed10:b8d41133e499 1001 putTitle();
bkc_mbed6:e48296dca6bb 1002
bkc_mbed10:b8d41133e499 1003 // ボードの初期化
bkc_mbed10:b8d41133e499 1004 boardInit();
bkc_mbed5:27f193e21222 1005
bkc_mbed10:b8d41133e499 1006 // 通信モードに移行
bkc_mbed10:b8d41133e499 1007 if (com_mode)
bkc_mbed10:b8d41133e499 1008 loop_for_com();
bkc_mbed10:b8d41133e499 1009
bkc_mbed0:2053640461b5 1010 files = get_nlg_file(file, -1);
bkc_mbed0:2053640461b5 1011
bkc_mbed10:b8d41133e499 1012 // エラー表示
bkc_mbed10:b8d41133e499 1013 if (files < 0)
bkc_mbed10:b8d41133e499 1014 error_sdcard();
bkc_mbed10:b8d41133e499 1015
bkc_mbed10:b8d41133e499 1016 // 情報モード
bkc_mbed10:b8d41133e499 1017 if (info_mode)
bkc_mbed10:b8d41133e499 1018 show_info(files);
bkc_mbed3:aed228fa3ae4 1019
bkc_mbed1:ec416e6d5739 1020
bkc_mbed7:7e183b33c3f9 1021 bool repeat_flag = false;
bkc_mbed0:2053640461b5 1022 int idx = 0;
bkc_mbed0:2053640461b5 1023 while(files > 0)
bkc_mbed0:2053640461b5 1024 {
bkc_mbed2:f0b7779b03c7 1025 char buf[16];
bkc_mbed2:f0b7779b03c7 1026
bkc_mbed0:2053640461b5 1027 get_nlg_file(file, idx);
bkc_mbed0:2053640461b5 1028
bkc_mbed0:2053640461b5 1029 sprintf(path, "/sd/%s", file);
bkc_mbed0:2053640461b5 1030 printf("path=%s\n",path);
bkc_mbed0:2053640461b5 1031
bkc_mbed2:f0b7779b03c7 1032 lcd_cls();
bkc_mbed1:ec416e6d5739 1033 lcd_printStrY(0, file);
bkc_mbed0:2053640461b5 1034
bkc_mbed3:aed228fa3ae4 1035 sprintf(buf, "%2d ", idx + 1);
bkc_mbed2:f0b7779b03c7 1036 lcd_printStrY(1, buf);
bkc_mbed2:f0b7779b03c7 1037
bkc_mbed2:f0b7779b03c7 1038 nlg_play(path);
bkc_mbed0:2053640461b5 1039
bkc_mbed2:f0b7779b03c7 1040 // wait for chattering
bkc_mbed1:ec416e6d5739 1041 wait(0.25);
bkc_mbed6:e48296dca6bb 1042
bkc_mbed6:e48296dca6bb 1043 // wait release
bkc_mbed6:e48296dca6bb 1044 waitButtonRelease();
bkc_mbed6:e48296dca6bb 1045
bkc_mbed6:e48296dca6bb 1046 // if play button is pressed
bkc_mbed0:2053640461b5 1047 if (g_stop)
bkc_mbed0:2053640461b5 1048 {
bkc_mbed6:e48296dca6bb 1049 g_stop = false;
bkc_mbed6:e48296dca6bb 1050 g_prev = false;
bkc_mbed6:e48296dca6bb 1051
bkc_mbed2:f0b7779b03c7 1052 lcd_printStrY(1, " STOP ");
bkc_mbed1:ec416e6d5739 1053
bkc_mbed6:e48296dca6bb 1054 // wait any button is pressed
bkc_mbed6:e48296dca6bb 1055 waitButtonPress();
bkc_mbed1:ec416e6d5739 1056
bkc_mbed7:7e183b33c3f9 1057 //
bkc_mbed7:7e183b33c3f9 1058 if (!sw_prev) {
bkc_mbed2:f0b7779b03c7 1059 lcd_printStrY(1, " PREV ");
bkc_mbed6:e48296dca6bb 1060 g_prev = true;
bkc_mbed2:f0b7779b03c7 1061 }
bkc_mbed7:7e183b33c3f9 1062 if (!sw_play)
bkc_mbed7:7e183b33c3f9 1063 {
bkc_mbed7:7e183b33c3f9 1064 lcd_printStrY(1, " PLAY ");
bkc_mbed7:7e183b33c3f9 1065 repeat_flag = true;
bkc_mbed7:7e183b33c3f9 1066 }
bkc_mbed7:7e183b33c3f9 1067 if (!sw_next)
bkc_mbed2:f0b7779b03c7 1068 {
bkc_mbed2:f0b7779b03c7 1069 lcd_printStrY(1, " NEXT ");
bkc_mbed7:7e183b33c3f9 1070 repeat_flag = false;
bkc_mbed2:f0b7779b03c7 1071 }
bkc_mbed1:ec416e6d5739 1072
bkc_mbed1:ec416e6d5739 1073 // wait release
bkc_mbed6:e48296dca6bb 1074 waitButtonRelease();
bkc_mbed1:ec416e6d5739 1075
bkc_mbed1:ec416e6d5739 1076 // reset board
bkc_mbed1:ec416e6d5739 1077 boardInit();
bkc_mbed0:2053640461b5 1078 }
bkc_mbed6:e48296dca6bb 1079
bkc_mbed6:e48296dca6bb 1080 // play prev or next song
bkc_mbed6:e48296dca6bb 1081 if (g_prev)
bkc_mbed1:ec416e6d5739 1082 {
bkc_mbed6:e48296dca6bb 1083 g_prev = false;
bkc_mbed6:e48296dca6bb 1084
bkc_mbed1:ec416e6d5739 1085 if (idx - 1 >= 0)
bkc_mbed1:ec416e6d5739 1086 idx--;
bkc_mbed1:ec416e6d5739 1087 else
bkc_mbed1:ec416e6d5739 1088 idx = files - 1;
bkc_mbed7:7e183b33c3f9 1089
bkc_mbed7:7e183b33c3f9 1090 continue;
bkc_mbed1:ec416e6d5739 1091 }
bkc_mbed7:7e183b33c3f9 1092
bkc_mbed7:7e183b33c3f9 1093 if (!repeat_flag)
bkc_mbed1:ec416e6d5739 1094 {
bkc_mbed1:ec416e6d5739 1095 if (idx + 1 < files)
bkc_mbed1:ec416e6d5739 1096 idx++;
bkc_mbed1:ec416e6d5739 1097 else
bkc_mbed1:ec416e6d5739 1098 idx = 0;
bkc_mbed1:ec416e6d5739 1099 }
bkc_mbed7:7e183b33c3f9 1100 repeat_flag = false;
bkc_mbed0:2053640461b5 1101 }
bkc_mbed0:2053640461b5 1102}