#include #include #define WAIT_HIGH while(digitalRead(PIN8) == LOW) #define WAIT_LOW while(digitalRead(PIN8) == HIGH) #define PIN1 8 #define PIN2 7 #define PIN3 6 #define PIN4 5 #define PIN6 4 #define PIN7 3 #define PIN8 2 /* #define PIN8 2 #define PIN6 3 #define PIN7 4 #define PIN1 5 #define PIN2 6 #define PIN3 7 #define PIN4 8 */ #define BUFFSIZE 512 int port[] = {PIN1, PIN2, PIN3, PIN4}; uint8_t head8[8] = {0x1F, 0xA6, 0xDE, 0xBA, 0xCC, 0x13, 0x7D, 0x74}; uint8_t buff[BUFFSIZE]; void setup() { pinMode(PIN6, INPUT); pinMode(PIN7, INPUT); pinMode(PIN8, INPUT); pinMode(PIN1, OUTPUT); pinMode(PIN2, OUTPUT); pinMode(PIN3, OUTPUT); pinMode(PIN4, OUTPUT); } void loop() { byte data; if(digitalRead(PIN8) == HIGH) // スタートビット待ち { data = ReadByte(); // コマンド受信 SD.begin(10); switch(data) { case 'F': filesCommand(); break; case 'L': loadCommand(); break; case 'S': saveCommand(); break; case 'R': renameCommand(); break; case 'K': killCommand(); break; case 'C': romCartridge(); } SD.end(); } } void filesCommand() { File root; int len; setOutput(); root = SD.open("/"); while (true) { File entry = root.openNextFile(); if (!entry) break; if (!entry.isDirectory()) // サブフォルダは扱わない { SendString(entry.name()); len = strlen(entry.name()); for(int i=0; i<(12-len); i++) SendByte(' '); sprintf(buff, "%6u", entry.size()); SendString(buff); SendByte(0x0d); SendByte(0x0a); } entry.close(); } SendByte(0); // 送信文字列の終端 setInput(); } void loadCommand() { uint8_t data; int p = 0; File loadfile; uint16_t fsize; uint8_t head[32]; bool error = false; while(1) { WAIT_HIGH; data = ReadByte(); buff[p++] = data; if(data == 0) break; } setOutput(); loadfile = SD.open(buff); if(loadfile) { //casファイルヘッダチェック for(int i=0; i<32; i++) head[i] = loadfile.read(); for(int i=0; i<8; i++) { if(head[i] != head8[i]) { error = true; break; } } if(!error) { for(int i=8; i<18; i++) if(head[i] != 0xd0) { error = true; break; } } if(!error) { for(int i=0; i<8; i++) if(head[i + 24] != head8[i]) { error = true; break; } } if(error) { SendByte(0); SendByte(0); loadfile.close(); setInput(); return; } // プログラムサイズ送信 for(int i=0; i<6; i++) head[i] = loadfile.read(); fsize = btow(head[3], head[2]); fsize -= btow(head[1], head[0]); ++fsize; data = (uint8_t)(fsize & 0xff); SendByte(data); // ファイルサイズの下位バイト data = (uint8_t)((fsize & 0xff00) >> 8); SendByte(data); // ファイルサイズの上位バイト SendByte(head[0]); // ロードアドレスの下位バイト SendByte(head[1]); // ロードアドレスの上位バイト SendByte(head[4]); // 実行アドレスの下位バイト SendByte(head[5]); // 実行アドレスの上位バイト // プログラム本体送信 while(1) { p = loadfile.read(buff, BUFFSIZE); if(p > 0) for(int i=0; i 0) newFile.write(buff, p); else break; } oldFile.close(); newFile.close(); if(SD.remove(oldName)) SendByte(1); else SendByte(0); setInput(); } void killCommand() { byte data; int p = 0; while(1) { WAIT_HIGH; data = ReadByte(); buff[p++] = data; if(data == 0) break; } setOutput(); if(SD.remove(buff)) SendByte(1); else SendByte(0); setInput(); } void romCartridge() { uint8_t data; int p = 0; File loadfile; uint32_t fsize; bool s16; while(1) { WAIT_HIGH; data = ReadByte(); buff[p++] = data; if(data == 0) break; } setOutput(); loadfile = SD.open(buff); if(loadfile) { // ROMファイルヘッダチェック p = loadfile.read(buff, BUFFSIZE); if((buff[0] != 0x41) || (buff[1] != 0x42)) { SendByte(0); loadfile.close(); setInput(); return; } fsize = loadfile.size(); if(fsize > 0x8000) { SendByte(0); loadfile.close(); setInput(); return; } else if(fsize > 0x4000) s16 = false; else s16 = true; data = buff[3] & 0xf0; if(data == 0x40) { if(s16) SendByte(1); else SendByte(2); } else if(data == 0x80) { if(s16) SendByte(3); else { SendByte(0); loadfile.close(); setInput(); return; } } else { SendByte(0); loadfile.close(); setInput(); return; } // 最初のBUFFSIZEバイト送信 for(int i=0; i 0) for(int i=0; i