Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: KL46Z-lpc81isp lpcterm2
RamDisk.cpp
00001 #include "RamDisk.h" 00002 00003 #if (DEBUG2 > 3) 00004 #define RAMDISK_DBG(...) do{fprintf(stderr,"[%s@%d] ",__PRETTY_FUNCTION__,__LINE__);fprintf(stderr,__VA_ARGS__);fprintf(stderr,"\r\n");} while(0); 00005 #else 00006 #define RAMDISK_DBG(...) 00007 #endif 00008 00009 RamDisk::RamDisk(int sectors):_sectors(sectors) 00010 { 00011 format(); 00012 } 00013 00014 /* virtual */ uint32_t RamDisk::storage_sectors() 00015 { 00016 report_sectors_count++; 00017 return _sectors; 00018 } 00019 00020 /* virtual */ uint32_t RamDisk::storage_size() 00021 { 00022 report_size_count++; 00023 return _sectors * 512; 00024 } 00025 00026 /* virtual */ int RamDisk::storage_read(uint8_t * data, uint32_t block) 00027 { 00028 RAMDISK_DBG("R block=%d", block); 00029 report_read_count++; 00030 memset(data, 0x00, 512); 00031 if (_sector_image.count(block) > 0) { 00032 uint8_t* buf = _sector_image[block]; 00033 int size = buf[0]|(buf[1]<<8); 00034 size &= 0x7fff; // remove ROM area bit 00035 memcpy(data, buf + 2, size); 00036 } 00037 return 0; 00038 } 00039 00040 static int block_size(const uint8_t* data) 00041 { 00042 for(int i = 512-1; i >= 0; i--) { 00043 if (data[i] != 0x00) { 00044 return i+1; 00045 } 00046 } 00047 return 0; 00048 } 00049 00050 /* virtual */ int RamDisk::storage_write(const uint8_t * data, uint32_t block) 00051 { 00052 report_write_count++; 00053 int size = block_size(data); 00054 if (_sector_image.count(block) > 0) { 00055 uint8_t* buf = _sector_image[block]; 00056 if (!(buf[1] & 0x80)) { // ROM area 00057 free(buf); 00058 } 00059 buf = (uint8_t*)malloc(size + 2); 00060 if (buf == NULL) { 00061 RAMDISK_DBG("DISK FULL"); 00062 exit(1); 00063 return 1; 00064 } 00065 _sector_image[block] = buf; 00066 buf[0] = size; 00067 buf[1] = size>>8; 00068 memcpy(buf + 2, data, size); 00069 RAMDISK_DBG("W block=%d size=%d", (int)block, size); 00070 } else if (size > 0) { 00071 uint8_t* buf = (uint8_t*)malloc(size + 2); 00072 if (buf == NULL) { 00073 RAMDISK_DBG("DISK FULL"); 00074 exit(1); 00075 return 1; 00076 } 00077 _sector_image[block] = (uint8_t*)buf; 00078 buf[0] = size; 00079 buf[1] = size>>8; 00080 00081 memcpy(buf + 2, data, size); 00082 RAMDISK_DBG("W block=%d size=%d NEW %d", (int)block, size, _sector_image.size()); 00083 } else { 00084 RAMDISK_DBG("W block=%d BLANK", (int)block); 00085 } 00086 return 0; 00087 } 00088 00089 void RamDisk::dump(int mode) 00090 { 00091 printf("static const uint8_t block_image[] = { // %d blocks\n", _sector_image.size()); 00092 for(int i = 0; i < _sector_image.size(); i++) { 00093 int block = _sector_image.getKey(i); 00094 uint8_t* buf = _sector_image[block]; 00095 int size = buf[0]|(buf[1]<<8); 00096 size &= 0x7fff; 00097 printf("0x%02x,0x%02x,0x%02x|0x80, // block=%d, size=%d, ROM area\n", 00098 block, buf[0], buf[1], block, size); 00099 if (mode == 1) { 00100 for(int j = 0; j < size; j++) { 00101 printf("0x%02x,", buf[j+2]); 00102 if ((j % 16) == 15 || j == size-1) { 00103 printf("\n"); 00104 } 00105 } 00106 } 00107 } 00108 printf("0xff};\n"); 00109 } 00110
Generated on Tue Jul 12 2022 19:39:32 by
1.7.2