Emulator for the Sharp CE-140F Diskette Driver
Dependencies: mbed SDFileSystem
This is an attempt (for the time being, it isn't complete yet!) of emulating the Sharp CE-140F disk drive with a Nucleo board, attached to a Sharp Pocket Computer though its platform-proprietary 11-pin interface.
commands.cpp@8:70f13d97eb44, 21 months ago (annotated)
- Committer:
- ffxx68
- Date:
- Tue Jul 19 14:03:08 2022 +0000
- Revision:
- 8:70f13d97eb44
- Parent:
- 7:0243d36a655a
minor change
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ffxx68 | 7:0243d36a655a | 1 | #include "commands.h" |
ffxx68 | 7:0243d36a655a | 2 | #include "SDFileSystem.h" |
ffxx68 | 7:0243d36a655a | 3 | |
ffxx68 | 7:0243d36a655a | 4 | volatile char inDataBuf[BUF_SIZE]; |
ffxx68 | 7:0243d36a655a | 5 | volatile char outDataBuf[BUF_SIZE]; |
ffxx68 | 7:0243d36a655a | 6 | volatile uint16_t inBufPosition; |
ffxx68 | 7:0243d36a655a | 7 | volatile uint16_t outBufPosition; |
ffxx68 | 7:0243d36a655a | 8 | volatile uint8_t checksum = 0; |
ffxx68 | 7:0243d36a655a | 9 | |
ffxx68 | 7:0243d36a655a | 10 | extern void debug_log(const char *fmt, ...); |
ffxx68 | 7:0243d36a655a | 11 | extern RawSerial pc; |
ffxx68 | 7:0243d36a655a | 12 | |
ffxx68 | 7:0243d36a655a | 13 | int fileCount; |
ffxx68 | 7:0243d36a655a | 14 | |
ffxx68 | 7:0243d36a655a | 15 | // SD Card |
ffxx68 | 7:0243d36a655a | 16 | SDFileSystem sd(PB_5, PB_4, PB_3, PA_10, "sd"); // mosi, miso, sclk, cs |
ffxx68 | 7:0243d36a655a | 17 | |
ffxx68 | 7:0243d36a655a | 18 | |
ffxx68 | 7:0243d36a655a | 19 | char CheckSum(char b) { |
ffxx68 | 7:0243d36a655a | 20 | checksum = (checksum + b) & 0xff; |
ffxx68 | 7:0243d36a655a | 21 | return b; |
ffxx68 | 7:0243d36a655a | 22 | } |
ffxx68 | 7:0243d36a655a | 23 | |
ffxx68 | 7:0243d36a655a | 24 | void outDataAppend(char b) { |
ffxx68 | 7:0243d36a655a | 25 | // NB in case we're going to implement a circular output buffer... |
ffxx68 | 7:0243d36a655a | 26 | // I expect functions appending data to be faster than the |
ffxx68 | 7:0243d36a655a | 27 | // consumer (outNibbleSend, increasing outDataPointer every about 10 ms). |
ffxx68 | 7:0243d36a655a | 28 | // We should wait here, when outBufPosition reaches outDataPointer, |
ffxx68 | 7:0243d36a655a | 29 | // for outDataPointer to increase again |
ffxx68 | 7:0243d36a655a | 30 | outDataBuf[ outBufPosition++ ] = b; |
ffxx68 | 7:0243d36a655a | 31 | } |
ffxx68 | 7:0243d36a655a | 32 | |
ffxx68 | 7:0243d36a655a | 33 | void sendString(char* s) { |
ffxx68 | 7:0243d36a655a | 34 | for (int i=0;i<strlen(s);i++) { |
ffxx68 | 7:0243d36a655a | 35 | outDataAppend(CheckSum(s[i])); |
ffxx68 | 7:0243d36a655a | 36 | } |
ffxx68 | 7:0243d36a655a | 37 | } |
ffxx68 | 7:0243d36a655a | 38 | |
ffxx68 | 7:0243d36a655a | 39 | /* |
ffxx68 | 7:0243d36a655a | 40 | QString cleanFileName(QString s) { |
ffxx68 | 7:0243d36a655a | 41 | QString r = "X:"; |
ffxx68 | 7:0243d36a655a | 42 | r = r + s.left(s.indexOf(".")).leftJustified(8,' ',true) + s.mid(s.indexOf(".")).rightJustified(4,' ',true); |
ffxx68 | 7:0243d36a655a | 43 | return r; |
ffxx68 | 7:0243d36a655a | 44 | } |
ffxx68 | 7:0243d36a655a | 45 | */ |
ffxx68 | 7:0243d36a655a | 46 | |
ffxx68 | 7:0243d36a655a | 47 | |
ffxx68 | 7:0243d36a655a | 48 | void process_FILES_LIST(int cmd) { |
ffxx68 | 7:0243d36a655a | 49 | // QString fname; |
ffxx68 | 7:0243d36a655a | 50 | |
ffxx68 | 7:0243d36a655a | 51 | pc.putc('l'); |
ffxx68 | 7:0243d36a655a | 52 | pc.putc(0x30+cmd); |
ffxx68 | 7:0243d36a655a | 53 | debug_log ("FILES_LIST %d\n", cmd); |
ffxx68 | 7:0243d36a655a | 54 | outDataAppend(0x00); |
ffxx68 | 7:0243d36a655a | 55 | checksum=0; |
ffxx68 | 7:0243d36a655a | 56 | |
ffxx68 | 7:0243d36a655a | 57 | switch (cmd) { |
ffxx68 | 7:0243d36a655a | 58 | case 0: |
ffxx68 | 7:0243d36a655a | 59 | fileCount++; |
ffxx68 | 7:0243d36a655a | 60 | //fname = fileList.at(fileCount); |
ffxx68 | 7:0243d36a655a | 61 | //debug_log ("*"+cleanFileName(fname)+"*\n"); |
ffxx68 | 7:0243d36a655a | 62 | // Send (cleaned-up) filenames |
ffxx68 | 7:0243d36a655a | 63 | //sendString(cleanFileName(fname)); |
ffxx68 | 7:0243d36a655a | 64 | sendString("X:TEST .BAS "); |
ffxx68 | 7:0243d36a655a | 65 | sendString(" "); |
ffxx68 | 7:0243d36a655a | 66 | // Send CheckSum |
ffxx68 | 7:0243d36a655a | 67 | outDataAppend(checksum); |
ffxx68 | 7:0243d36a655a | 68 | break; |
ffxx68 | 7:0243d36a655a | 69 | |
ffxx68 | 7:0243d36a655a | 70 | case 1: |
ffxx68 | 7:0243d36a655a | 71 | fileCount--; |
ffxx68 | 7:0243d36a655a | 72 | //fname = fileList.at(fileCount); |
ffxx68 | 7:0243d36a655a | 73 | //debug_log ("*"+cleanFileName(fname)+"*\n"); |
ffxx68 | 7:0243d36a655a | 74 | // Send filenames |
ffxx68 | 7:0243d36a655a | 75 | //sendString(cleanFileName(fname)); |
ffxx68 | 7:0243d36a655a | 76 | sendString("X:TEST .BAS "); |
ffxx68 | 7:0243d36a655a | 77 | sendString(" "); |
ffxx68 | 7:0243d36a655a | 78 | // Send CheckSum |
ffxx68 | 7:0243d36a655a | 79 | outDataAppend(checksum); |
ffxx68 | 7:0243d36a655a | 80 | break; |
ffxx68 | 7:0243d36a655a | 81 | } |
ffxx68 | 7:0243d36a655a | 82 | } |
ffxx68 | 7:0243d36a655a | 83 | |
ffxx68 | 7:0243d36a655a | 84 | |
ffxx68 | 7:0243d36a655a | 85 | void process_FILES(void) { |
ffxx68 | 7:0243d36a655a | 86 | |
ffxx68 | 7:0243d36a655a | 87 | struct dirent* ent; |
ffxx68 | 7:0243d36a655a | 88 | DIR *dir; |
ffxx68 | 7:0243d36a655a | 89 | char n_files = 0x00; |
ffxx68 | 7:0243d36a655a | 90 | char *ext_pos; |
ffxx68 | 7:0243d36a655a | 91 | |
ffxx68 | 7:0243d36a655a | 92 | debug_log ( "FILES\n" ); |
ffxx68 | 7:0243d36a655a | 93 | pc.putc('f'); |
ffxx68 | 7:0243d36a655a | 94 | outDataAppend(CheckSum(0x00)); |
ffxx68 | 7:0243d36a655a | 95 | // Send nb files, from specified dir (with wildcards) |
ffxx68 | 7:0243d36a655a | 96 | /* |
ffxx68 | 7:0243d36a655a | 97 | QString s =""; |
ffxx68 | 7:0243d36a655a | 98 | for (int i =3;i< 15;i++) { |
ffxx68 | 7:0243d36a655a | 99 | s.append(QChar(data.at(i))); |
ffxx68 | 7:0243d36a655a | 100 | } |
ffxx68 | 7:0243d36a655a | 101 | // s="*.BAS"; |
ffxx68 | 7:0243d36a655a | 102 | fileList = directory.entryList( QStringList() << s.replace(" ",""),QDir::Files); |
ffxx68 | 7:0243d36a655a | 103 | outDataAppend(CheckSum(fileList.size())); |
ffxx68 | 7:0243d36a655a | 104 | fileCount = -1; |
ffxx68 | 7:0243d36a655a | 105 | */ |
ffxx68 | 7:0243d36a655a | 106 | if ((dir = opendir ("/sd/")) != NULL) { // ignore files other than BASIC |
ffxx68 | 7:0243d36a655a | 107 | // print all the files and directories within directory |
ffxx68 | 7:0243d36a655a | 108 | while ((ent = readdir (dir)) != NULL |
ffxx68 | 7:0243d36a655a | 109 | && n_files < 0xFF ) { // max 255 files |
ffxx68 | 7:0243d36a655a | 110 | debug_log("<%s>\n", ent->d_name); |
ffxx68 | 7:0243d36a655a | 111 | // ignore files other than BASIC (for the timebeing...) |
ffxx68 | 7:0243d36a655a | 112 | if ( (ext_pos = strstr (ent->d_name, ".BAS")) != 0 ) { |
ffxx68 | 7:0243d36a655a | 113 | n_files++; |
ffxx68 | 7:0243d36a655a | 114 | } |
ffxx68 | 7:0243d36a655a | 115 | } |
ffxx68 | 7:0243d36a655a | 116 | closedir (dir); |
ffxx68 | 7:0243d36a655a | 117 | fileCount = -1; |
ffxx68 | 7:0243d36a655a | 118 | debug_log("%d files\n", n_files); |
ffxx68 | 7:0243d36a655a | 119 | outDataAppend(CheckSum(n_files)); |
ffxx68 | 7:0243d36a655a | 120 | outDataAppend(checksum); |
ffxx68 | 7:0243d36a655a | 121 | |
ffxx68 | 8:70f13d97eb44 | 122 | // process_FILES_LIST ( 0x00 ); // append first file name by default ? |
ffxx68 | 7:0243d36a655a | 123 | |
ffxx68 | 7:0243d36a655a | 124 | } else { |
ffxx68 | 7:0243d36a655a | 125 | // could not open directory |
ffxx68 | 7:0243d36a655a | 126 | pc.putc('x'); |
ffxx68 | 7:0243d36a655a | 127 | pc.printf("Could not open SD directory\n"); |
ffxx68 | 7:0243d36a655a | 128 | } |
ffxx68 | 7:0243d36a655a | 129 | } |
ffxx68 | 7:0243d36a655a | 130 | |
ffxx68 | 7:0243d36a655a | 131 | |
ffxx68 | 7:0243d36a655a | 132 | void ProcessCommand ( void ) { |
ffxx68 | 7:0243d36a655a | 133 | |
ffxx68 | 7:0243d36a655a | 134 | checksum=0; |
ffxx68 | 7:0243d36a655a | 135 | outBufPosition = 0; |
ffxx68 | 7:0243d36a655a | 136 | |
ffxx68 | 7:0243d36a655a | 137 | switch (inDataBuf[0]) { |
ffxx68 | 7:0243d36a655a | 138 | |
ffxx68 | 7:0243d36a655a | 139 | case 0x05: process_FILES();break; |
ffxx68 | 7:0243d36a655a | 140 | case 0x06: process_FILES_LIST(0);break; |
ffxx68 | 7:0243d36a655a | 141 | case 0x07: process_FILES_LIST(1);break; |
ffxx68 | 7:0243d36a655a | 142 | |
ffxx68 | 7:0243d36a655a | 143 | case 0x1D: |
ffxx68 | 7:0243d36a655a | 144 | // case 0x1D: process_DSKF(); |
ffxx68 | 7:0243d36a655a | 145 | debug_log ( "DSKF\n" ); |
ffxx68 | 7:0243d36a655a | 146 | outDataAppend(CheckSum(0x00)); |
ffxx68 | 7:0243d36a655a | 147 | outDataAppend(CheckSum(0x02)); // number of byte |
ffxx68 | 7:0243d36a655a | 148 | outDataAppend(CheckSum(0x50)); // number of 256Bytes free sectors |
ffxx68 | 7:0243d36a655a | 149 | outDataAppend(CheckSum(0x00)); |
ffxx68 | 7:0243d36a655a | 150 | outDataAppend(0x52); // don't know yet. Perhaps a checksum ? |
ffxx68 | 7:0243d36a655a | 151 | break; |
ffxx68 | 7:0243d36a655a | 152 | |
ffxx68 | 7:0243d36a655a | 153 | default: |
ffxx68 | 7:0243d36a655a | 154 | debug_log ( "Unsupported command (yet...)" ); |
ffxx68 | 7:0243d36a655a | 155 | break; |
ffxx68 | 7:0243d36a655a | 156 | } |
ffxx68 | 7:0243d36a655a | 157 | |
ffxx68 | 7:0243d36a655a | 158 | } |