Zend - The PHP Company




Files and Directories

Add Code


C64 disk reader (D64 files)  

Type: class
Added by: tonypeters
Entered: 02/04/2001
Last modified: 04/12/2000
Rating: - (fewer than 3 votes)
Views: 6688
Class to read Commodore 64 .D64 files. This will produce a listing of the contents of the disk in an array with all details (see example). This was mainly produced as a test of PHP's file handling functions and class implementation (which I think it handles quite well!)


<?php
    
// Class to view .D64 files (Commodore 64 1541 disk files)
    // usage: var = new d64[filename]
    
class D64 {
    
        
// filename of file uploaded
        
var $filename;
        
// directory contents of .d64
        // format: $directory[directory item][information]
        // contents: filename, filetype, filesize
        
var $directory = array();
        
// complete contents of .d64
        // format: $disk[block][track]
        
var $disk = array();
        
        
// function to get individual sectors
        
function getsectors($fp){
            return 
fread($fp,256);
        }
        
        
// function to get tracks for individual blocks
        
function gettracks($fp,$block){
            
$tracks = array();
            if (
$block 18) {
                
$notracks 21;
            } elseif (
$block 25) {
                
$notracks 19;
            } elseif (
$block 31) {
                
$notracks 18;
            } else {
                
$notracks 17;
            }
            for(
$i=0;$i<$notracks;$i++){
                
$tracks[] = $this->getsectors($fp);
            }
            return 
$tracks;
        }
        
        
// function to get disk contents
        
function getdisk(){
            
$fp fopen($this->filename,'r');
            for(
$i=1;$i<35;$i++){
                
$this->disk[] = $this->gettracks($fp,$i);
            }
        }
        
        
// function to get individual directory items, and pass back it's information
        
function gedirentry($entry,$offset){
            
$info['filetype'] = substr($entry,($offset 2),16);
            switch (
ord($info['filetype'])) {
                case 
128:
                    
$info['filetype'] = 'DEL';
                    break;
                case 
129:
                    
$info['filetype'] = 'SEQ';
                    break;
                case 
130:
                    
$info['filetype'] = 'PRG';
                    break;
                case 
131:
                    
$info['filetype'] = 'USR';
                    break;
                case 
132:
                    
$info['filetype'] = 'REL';
                    break;
                default:
                    
$info['filetype'] = 'UNK';
            }
            
$info['filename'] = substr($entry,($offset 5),16);
            if(
$info['filename'] == chr(00).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00).chr(00)) {
                
$info['filename'] = false;
            }
            
$sizelo substr($entry,($offset 30),1);
            
$sizehi substr($entry,($offset 31),1);
            
$size = (ord($sizelo) + ord($sizehi) * 256);
            
$info['filesize'] = ($size 254);
            return 
$info;
        }
        
        
// function to get the directory contents of the disk
        
function getdirectory($track){
            
$currtrack 1;
            for(
$i=1;$i<19;$i++){
                for(
$y=0;$y<8;$y++) {
                    
$offset = ($y 32);
                    
$this->directory[] = $this->gedirentry($track[$i],$offset);
                }
                
$currtrack substr($track[$i],0,2);
                if (
$currtrack=='00') break;
            }
        }
        
        
// function to load the .d64 file. run on initialisation of class
        
function D64($filenametoload){
            
$this->filename $filenametoload;
            
$this->getdisk();
            
$this->getdirectory($this->disk[17]);
        }
    }    
    

?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories