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