📄 fat.lst
字号:
1023
1024
1025
1026 /*F**************************************************************************
1027 * NAME: fat_update_buf_fat
1028 *----------------------------------------------------------------------------
1029 * PARAMS:
1030 *
1031 * return:
1032 *----------------------------------------------------------------------------
1033 * PURPOSE:
1034 * This function check if a fat sector have to be writen.
1035 *----------------------------------------------------------------------------
1036 * EXAMPLE:
1037 *----------------------------------------------------------------------------
1038 * NOTE:
1039 *----------------------------------------------------------------------------
1040 * REQUIREMENTS:
1041 * fat_root_entry must be updated.
1042 *****************************************************************************/
1043 void fat_update_buf_fat(Uint16 cluster_old, Uint16 cluster, bit end)
1044 {
1045 1 bit fat_12_parity;
1046 1 Byte sector_number;
C51 COMPILER V7.50 FAT 06/03/2006 10:32:54 PAGE 18
1047 1 #define i cluster_old
1048 1
1049 1 fat_12_parity = ((Byte*)&cluster_old)[1] & 0x01;
1050 1 sector_number = cluster_old * 3 / 1024;
1051 1 i = (cluster_old * 3 / 2) & 0x1FF;
1052 1
1053 1 if (end == TRUE)
1054 1 cluster = 0xFFF;
1055 1 if (fat_12_parity == 0)
1056 1 {
1057 2 fat_buf_sector[i++] = ((Byte*)&cluster)[1];
1058 2 if (((Byte*)&i)[0] == 0x02)
1059 2 {
1060 3 fat_update_fat_sector(sector_number);
1061 3 sector_number++;
1062 3 fat_load_sector(fat_ptr_fats + sector_number);
1063 3 ((Byte*)&i)[0] = 0x00;
1064 3 }
1065 2 fat_buf_sector[i] &= 0xF0;
1066 2 fat_buf_sector[i] |= ((Byte*)&cluster)[0] & 0x0F;
1067 2 }
1068 1 else
1069 1 {
1070 2 fat_buf_sector[i] &= 0x0F;
1071 2 fat_buf_sector[i] |= (((Byte*)&cluster)[1] & 0x0F) << 4;
1072 2 i++;
1073 2 if (((Byte*)&i)[0] == 0x02)
1074 2 {
1075 3 fat_update_fat_sector(sector_number);
1076 3 sector_number++;
1077 3 fat_load_sector(fat_ptr_fats + sector_number);
1078 3 ((Byte*)&i)[0] = 0x00;
1079 3 }
1080 2 fat_buf_sector[i++] = (cluster & 0x0FF0) >> 4;
1081 2 if (((Byte*)&i)[0] == 0x02)
1082 2 {
1083 3 fat_update_fat_sector(sector_number);
1084 3 sector_number++;
1085 3 fat_load_sector(fat_ptr_fats + sector_number);
1086 3 ((Byte*)&i)[0] = 0x00;
1087 3 }
1088 2 }
1089 1 if (end == TRUE)
1090 1 fat_update_fat_sector(sector_number);
1091 1 #undef i
1092 1
1093 1 }
1094 /*F**************************************************************************
1095 * NAME: fat_update_entry_fat
1096 *----------------------------------------------------------------------------
1097 * PARAMS:
1098 *
1099 * return:
1100 *----------------------------------------------------------------------------
1101 * PURPOSE:
1102 * Update root entry and FAT after a writing file session (create or re-write)
1103 *----------------------------------------------------------------------------
1104 * EXAMPLE:
1105 *----------------------------------------------------------------------------
1106 * NOTE:
1107 *----------------------------------------------------------------------------
1108 * REQUIREMENTS:
C51 COMPILER V7.50 FAT 06/03/2006 10:32:54 PAGE 19
1109 * fat_root_entry must be updated.
1110 *****************************************************************************/
1111 void fat_update_entry_fat (void)
1112 {
1113 1 Byte index;
1114 1 Byte chain_index;
1115 1 Byte sector_number;
1116 1 Uint16 i;
1117 1 Uint16 cluster;
1118 1 Uint16 nb_cluster;
1119 1
1120 1 /*********************/
1121 1 /* Update root entry */
1122 1 /*********************/
1123 1 fat_load_sector(fat_ptr_rdir + (fat_root_entry >> 4));
1124 1 i = (fat_root_entry % 16) * 32 ; /* Position of entry in the sector */
1125 1 /* Update file size */
1126 1 if (fat_cache.current.size.l <= fat_file_size.l)
1127 1 fat_cache.current.size.l = fat_file_size.l;
1128 1 fat_buf_sector[i + 28] = fat_cache.current.size.b[3];
1129 1 fat_buf_sector[i + 29] = fat_cache.current.size.b[2];
1130 1 fat_buf_sector[i + 30] = fat_cache.current.size.b[1];
1131 1 fat_buf_sector[i + 31] = fat_cache.current.size.b[0];
1132 1
1133 1 ext[0] = fat_buf_sector[i + 8];
1134 1 ext[1] = fat_buf_sector[i + 9];
1135 1 ext[2] = fat_buf_sector[i + 10];
1136 1
1137 1
1138 1 Hard_write_open(fat_ptr_rdir + (fat_root_entry >> 4));
1139 1 for (i= 0; i< SECTOR_SIZE; i++)
1140 1 Hard_write_byte(fat_buf_sector[i]);
1141 1 Hard_write_close();
1142 1
1143 1
1144 1 /********************/
1145 1 /* Update fat 1 & 2 */
1146 1 /********************/
1147 1 /* Calculate file size cluster */
1148 1 nb_cluster = (fat_cache.current.size.l / SECTOR_SIZE) / fat_cluster_size;
1149 1 if ((fat_cache.current.size.l % (fat_cluster_size * SECTOR_SIZE)))
1150 1 {
1151 2 nb_cluster++;
1152 2 }
1153 1 nb_cluster--;
1154 1 index = 0;
1155 1
1156 1 /********************/
1157 1 /* FAT16 management */
1158 1 /********************/
1159 1 if (fat_is_fat16)
1160 1 {
1161 2 /* init the starting cluster value */
1162 2 cluster = fclusters[0].cluster + 2;
1163 2 /* Start at first chain cluster */
1164 2 sector_number = cluster / 256;
1165 2 /* Bufferize fat sector */
1166 2 fat_load_sector(fat_ptr_fats + sector_number);
1167 2 /* i -> word fat sector position */
1168 2 i = (cluster * 2) & 0x1FF;
1169 2 chain_index = 1;
1170 2
C51 COMPILER V7.50 FAT 06/03/2006 10:32:54 PAGE 20
1171 2 while (nb_cluster != 0)
1172 2 {
1173 3 /* Determinate the value of the next cluster */
1174 3 if (fclusters[index].number == chain_index)
1175 3 {
1176 4 /* increase index */
1177 4 index++;
1178 4 cluster = fclusters[index].cluster + 2;
1179 4 fat_buf_sector[i++] = ((Byte*)&cluster)[1];
1180 4 fat_buf_sector[i] = ((Byte*)&cluster)[0];
1181 4 chain_index = 1;
1182 4 if ( (cluster / 256) != sector_number)
1183 4 { /* Fat change sector */
1184 5 fat_update_fat_sector(sector_number);
1185 5 sector_number = (Uint16)(cluster / 256);
1186 5 fat_load_sector(fat_ptr_fats + sector_number);
1187 5 }
1188 4 i = (cluster * 2) & 0x1FF;
1189 4
1190 4 }
1191 3 else
1192 3 {
1193 4 cluster++;
1194 4 fat_buf_sector[i++] = ((Byte*)&cluster)[1];
1195 4 fat_buf_sector[i++] = ((Byte*)&cluster)[0];
1196 4 chain_index++;
1197 4 if (((Byte*)&i)[0] == 0x02)
1198 4 {
1199 5 fat_update_fat_sector(sector_number);
1200 5 sector_number++;
1201 5 fat_load_sector(fat_ptr_fats + sector_number);
1202 5 ((Byte*)&i)[0] = 0x00;
1203 5 }
1204 4 }
1205 3 nb_cluster--;
1206 3 }
1207 2 /* End of file indicate by 0xFFFF */
1208 2 fat_buf_sector[i++] = 0xFF;
1209 2 fat_buf_sector[i] = 0xFF;
1210 2 fat_update_fat_sector(sector_number);
1211 2 }
1212 1 /********************/
1213 1 /* FAT12 management */
1214 1 /********************/
1215 1 else
1216 1 {
1217 2 cluster = fclusters[index].cluster + 2;
1218 2 sector_number = cluster * 3 / 1024;
1219 2 /* Bufferize fat sector */
1220 2 fat_load_sector(fat_ptr_fats + sector_number);
1221 2 i = cluster;
1222 2 chain_index = 1;
1223 2 while (nb_cluster != 0)
1224 2 {
1225 3 /* Determinate the value of the next cluster */
1226 3 if (fclusters[index].number == chain_index)
1227 3 {
1228 4 /* increase index */
1229 4 index++;
1230 4 fat_update_buf_fat(cluster, fclusters[index].cluster + 2, 0);
1231 4 cluster = fclusters[index].cluster + 2;
1232 4 chain_index = 1;
C51 COMPILER V7.50 FAT 06/03/2006 10:32:54 PAGE 21
1233 4 i = cluster * 3 / 1024;
1234 4 if ( i != sector_number)
1235 4 { /* Fat change sector */
1236 5 fat_update_fat_sector(sector_number);
1237 5 sector_number = i;
1238 5 fat_load_sector(fat_ptr_fats + sector_number);
1239 5 }
1240 4 }
1241 3 else
1242 3 {
1243 4 cluster++;
1244 4 fat_update_buf_fat(cluster - 1, cluster, 0);
1245 4 chain_index++;
1246 4 }
1247 3 nb_cluster--;
1248 3 }
1249 2 fat_update_buf_fat(cluster, cluster, 1);
1250 2 }
1251 1
1252 1 /* Reconstruct list file */
1253 1 i = fat_dir_list_index;
1254 1
1255 1 fat_dir_current_sect = fat_ptr_rdir;
1256 1 fat_dclust_byte_count = 0;
1257 1 dir_is_root = TRUE;
1258 1
1259 1 fat_get_dir_file_list(fat_check_ext()); /* create list of entries */
1260 1 fat_dir_list_index = i;
1261 1 for (i = 0; i <= fat_dir_list_index; i++)
1262 1 fat_dseek(fat_dir_entry_list[i] * DIR_SIZE);
1263 1
1264 1 fat_get_dir_entry(&fat_cache.current); /* update current file info */
1265 1 }
1266
1267
1268 /*F**************************************************************************
1269 * NAME: fat_fopen
1270 *----------------------------------------------------------------------------
1271 * PARAMS:
1272 * mode: READ: open file for read
1273 * WRITE: open file for write
1274 *
1275 * return:
1
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -