# File lib/file_systems/Ti99Fms.rb, line 34
def self.files(file_system_image)
  sector_0=file_system_image.get_sector(0,0)
  file_system_image.volume_name=sector_0[0,10].unpack("A*")[0]
  files=FileContainer.new
  file_descriptor_index_sector=file_system_image.get_sector(0,1)
  127.times do |file_descriptor_index|    
    dir_sector_no=(file_descriptor_index_sector[file_descriptor_index*2]<<8)+(file_descriptor_index_sector[(file_descriptor_index*2)+1])
#    puts "#{file_descriptor_index} %02X" % dir_sector_no
    break if dir_sector_no==0
    dir_sector=file_system_image.get_sector(0,dir_sector_no)
    filename=dir_sector[0,10].unpack("A*")[0]
    if dir_sector[0]>=0x20 && dir_sector[0x0a]==0 && dir_sector[0x0b]==0 then
      file_status_flag=dir_sector[0x0c]
      record_length=dir_sector[0x11]      
      records_per_sector=dir_sector[0x0d]
      valid_bytes_in_sector=records_per_sector*record_length
      valid_bytes_in_sector=0x100 if valid_bytes_in_sector==0
#      puts "FILE #{filename} RECORD LENGTH %02X NUM RECORDS %02X TOTAL BYTES %02X"  % [record_length,records_per_sector,valid_bytes_in_sector]
      
#      puts "FILE STATUS: %02X" % file_status_flag
      data_chain_pointer=0x1c
      
      #DATA CHAIN POINTER BLOCKS - Each data chain pointer block is comprised of three bytes which comprise a cluster.
      #The three bytes are broken down into 2 three nybble blocks. The first indicates the first actual sector in
      #the cluster. THe second indicates the highest logical record offset in the cluster of contiguous sectors.
      #The following diagram shows how each three byte cluster relates to the address of the first sector and the 
      #highest logical sector offset in the cluster.
      #BYTE 1   N2:N1
      #BYTE 2   M1:N3
      #BYTE 3   M3:M2
      #
      #First Sector - N3:N2:N1
      #Highest Sector Offset M2:M3:M1      
      last_sector_offset=-1
      file_contents=""
      
      while data_chain_pointer<0xFD  do
        byte_1=dir_sector[data_chain_pointer]
        byte_2=dir_sector[data_chain_pointer+1]
        byte_3=dir_sector[data_chain_pointer+2]
        n3=byte_2%0x10
        m1=byte_2>>4
        first_sector=(n3<<8)+byte_1
        highest_sector_offset=(byte_3<<4)+m1
        sectors_in_this_block=highest_sector_offset-last_sector_offset
        last_sector_offset=highest_sector_offset

#        puts "%2x:%2x:%2x FIRST SECTOR %03X OFFSET %03X" % [byte_1,byte_2,byte_3,first_sector,highest_sector_offset]
#        puts "#{filename} FS #{first_sector} COUNT #{sectors_in_this_block}"
        break if first_sector==0 || first_sector>file_system_image.image_format.sectors_per_track*file_system_image.track_count

        sectors_in_this_block.times {|i| file_contents<<file_system_image.get_sector(0,i+first_sector)}  
        data_chain_pointer+=3
            
      end
      file_type=file_status_flag
      
      files<<NativeFileType.best_fit(file_system_image,filename,file_contents,file_type)
    end
  end
  files
end