--- Frodo4/Src/1541d64.cpp 2004/01/11 14:03:29 1.5 +++ Frodo4/Src/1541d64.cpp 2010/04/21 19:52:53 1.11 @@ -1,7 +1,7 @@ /* * 1541d64.cpp - 1541 emulation in disk image files (.d64/.x64/zipcode) * - * Frodo (C) 1994-1997,2002-2003 Christian Bauer + * Frodo Copyright (C) Christian Bauer * zipcode decoding routines (C) 1993-1997 Marko Mäkelä, Paul David Doherty * * This program is free software; you can redistribute it and/or modify @@ -34,6 +34,9 @@ #include "C64.h" #include "main.h" +#define DEBUG 0 +#include "debug.h" + // Channel modes (IRC users listen up :-) enum { @@ -106,14 +109,22 @@ const int accum_num_sectors[41] = { // Prototypes static bool match(const uint8 *p, int p_len, const uint8 *n); +static FILE *open_image_file(const char *path, bool write_mode); +static bool parse_image_file(FILE *f, image_file_desc &desc); /* * Constructor: Prepare emulation, open image file */ -D64Drive::D64Drive(IEC *iec, const char *filepath) : Drive(iec), the_file(NULL), bam(ram + 0x700), bam_dirty(false) +ImageDrive::ImageDrive(IEC *iec, const char *filepath) : Drive(iec), the_file(NULL), bam(ram + 0x700), bam_dirty(false) { + desc.type = TYPE_D64; + desc.header_size = 0; + desc.num_tracks = 35; + desc.id1 = desc.id2 = 0; + desc.has_error_info = false; + for (int i=0; i<18; i++) { ch[i].mode = CHMOD_FREE; ch[i].buf = NULL; @@ -132,7 +143,7 @@ D64Drive::D64Drive(IEC *iec, const char * Destructor */ -D64Drive::~D64Drive() +ImageDrive::~ImageDrive() { close_image(); } @@ -142,7 +153,7 @@ D64Drive::~D64Drive() * Close the image file */ -void D64Drive::close_image(void) +void ImageDrive::close_image(void) { if (the_file) { close_all_channels(); @@ -160,7 +171,7 @@ void D64Drive::close_image(void) * Open the image file */ -bool D64Drive::change_image(const char *path) +bool ImageDrive::change_image(const char *path) { // Close old image file close_image(); @@ -194,8 +205,10 @@ bool D64Drive::change_image(const char * * Open channel */ -uint8 D64Drive::Open(int channel, const uint8 *name, int name_len) +uint8 ImageDrive::Open(int channel, const uint8 *name, int name_len) { + D(bug("ImageDrive::Open channel %d, file %s\n", channel, name)); + set_error(ERR_OK); // Channel 15: execute file name as command @@ -226,7 +239,7 @@ uint8 D64Drive::Open(int channel, const * Open file */ -uint8 D64Drive::open_file(int channel, const uint8 *name, int name_len) +uint8 ImageDrive::open_file(int channel, const uint8 *name, int name_len) { uint8 plain_name[NAMEBUF_LENGTH]; int plain_name_len; @@ -237,6 +250,8 @@ uint8 D64Drive::open_file(int channel, c if (plain_name_len > 16) plain_name_len = 16; + D(bug(" plain name %s, type %d, mode %d\n", plain_name, type, mode)); + // Channel 0 is READ, channel 1 is WRITE if (channel == 0 || channel == 1) { mode = channel ? FMODE_WRITE : FMODE_READ; @@ -269,6 +284,7 @@ uint8 D64Drive::open_file(int channel, c if (find_first_file(plain_name, plain_name_len, dir_track, dir_sector, entry)) { // File exists + D(bug(" file exists, dir track %d, sector %d, entry %d\n", dir_track, dir_sector, entry)); ch[channel].dir_track = dir_track; ch[channel].dir_sector = dir_sector; ch[channel].entry = entry; @@ -334,6 +350,8 @@ uint8 D64Drive::open_file(int channel, c } else { // File doesn't exist + D(bug(" file not found\n")); + // Set file type to SEQ if not specified in file name if (type == FTYPE_DEL) type = FTYPE_SEQ; @@ -354,8 +372,10 @@ uint8 D64Drive::open_file(int channel, c * Open channel for reading from file given track/sector of first block */ -uint8 D64Drive::open_file_ts(int channel, int track, int sector) +uint8 ImageDrive::open_file_ts(int channel, int track, int sector) { + D(bug("open_file_ts track %d, sector %d\n", track, sector)); + // Allocate buffer and set channel mode int buf = alloc_buffer(-1); if (buf == -1) { @@ -379,8 +399,10 @@ uint8 D64Drive::open_file_ts(int channel * Create file and open channel for writing to file */ -uint8 D64Drive::create_file(int channel, const uint8 *name, int name_len, int type, bool overwrite) +uint8 ImageDrive::create_file(int channel, const uint8 *name, int name_len, int type, bool overwrite) { + D(bug("create_file %s, type %d\n", name, type)); + // Allocate buffer int buf = alloc_buffer(-1); if (buf == -1) { @@ -407,6 +429,7 @@ uint8 D64Drive::create_file(int channel, return ST_OK; } ch[channel].num_blocks = 1; + D(bug(" first data block on track %d, sector %d\n", ch[channel].track, ch[channel].sector)); // Write directory entry memset(de, 0, SIZEOF_DE); @@ -439,7 +462,7 @@ const char type_char_1[] = "DSPUREER"; const char type_char_2[] = "EERSELQG"; const char type_char_3[] = "LQGRL???"; -uint8 D64Drive::open_directory(const uint8 *pattern, int pattern_len) +uint8 ImageDrive::open_directory(const uint8 *pattern, int pattern_len) { // Special treatment for "$0" if (pattern[0] == '0' && pattern_len == 1) { @@ -594,7 +617,7 @@ uint8 D64Drive::open_directory(const uin * Open channel for direct buffer access */ -uint8 D64Drive::open_direct(int channel, const uint8 *name) +uint8 ImageDrive::open_direct(int channel, const uint8 *name) { int buf = -1; @@ -627,8 +650,10 @@ uint8 D64Drive::open_direct(int channel, * Close channel */ -uint8 D64Drive::Close(int channel) +uint8 ImageDrive::Close(int channel) { + D(bug("ImageDrive::Close channel %d\n", channel)); + switch (ch[channel].mode) { case CHMOD_FREE: break; @@ -655,6 +680,7 @@ uint8 D64Drive::Close(int channel) // Write last data block ch[channel].buf[0] = 0; ch[channel].buf[1] = ch[channel].buf_len - 1; + D(bug(" writing last data block\n")); if (!write_sector(ch[channel].track, ch[channel].sector, ch[channel].buf)) goto free; @@ -672,6 +698,7 @@ uint8 D64Drive::Close(int channel) de[DE_OVR_TRACK] = de[DE_OVR_SECTOR] = 0; } write_sector(ch[channel].dir_track, ch[channel].dir_sector, dir); + D(bug(" directory entry updated\n")); } free: free_buffer(ch[channel].buf_num); ch[channel].buf = NULL; @@ -693,7 +720,7 @@ free: free_buffer(ch[channel].buf_num); * Close all channels */ -void D64Drive::close_all_channels() +void ImageDrive::close_all_channels() { for (int i=0; i<15; i++) Close(i); @@ -708,8 +735,10 @@ void D64Drive::close_all_channels() * Read from channel */ -uint8 D64Drive::Read(int channel, uint8 &byte) +uint8 ImageDrive::Read(int channel, uint8 &byte) { +// D(bug("ImageDrive::Read channel %d\n", channel)); + switch (ch[channel].mode) { case CHMOD_FREE: if (current_error == ERR_OK) @@ -735,6 +764,7 @@ uint8 D64Drive::Read(int channel, uint8 // Read next block if necessary if (ch[channel].buf_len == 0 && ch[channel].buf[0]) { + D(bug(" reading next data block track %d, sector %d\n", ch[channel].buf[0], ch[channel].buf[1])); if (!read_sector(ch[channel].buf[0], ch[channel].buf[1], ch[channel].buf)) return ST_READ_TIMEOUT; ch[channel].buf_ptr = ch[channel].buf + 2; @@ -773,8 +803,10 @@ uint8 D64Drive::Read(int channel, uint8 * Write byte to channel */ -uint8 D64Drive::Write(int channel, uint8 byte, bool eoi) +uint8 ImageDrive::Write(int channel, uint8 byte, bool eoi) { +// D(bug("ImageDrive::Write channel %d, byte %02x, eoi %d\n", channel, byte, eoi)); + switch (ch[channel].mode) { case CHMOD_FREE: if (current_error == ERR_OK) @@ -814,6 +846,7 @@ uint8 D64Drive::Write(int channel, uint8 if (!alloc_next_block(track, sector, DATA_INTERLEAVE)) return ST_TIMEOUT; ch[channel].num_blocks++; + D(bug("next data block on track %d, sector %d\n", track, sector)); // Write buffer with link to new block ch[channel].buf[0] = track; @@ -847,7 +880,7 @@ uint8 D64Drive::Write(int channel, uint8 * Reset drive */ -void D64Drive::Reset(void) +void ImageDrive::Reset(void) { close_all_channels(); @@ -874,7 +907,7 @@ void D64Drive::Reset(void) * <- Allocated buffer number or -1 */ -int D64Drive::alloc_buffer(int want) +int ImageDrive::alloc_buffer(int want) { if (want == -1) { for (want=3; want>=0; want--) @@ -900,7 +933,7 @@ int D64Drive::alloc_buffer(int want) * Free floppy buffer */ -void D64Drive::free_buffer(int buf) +void ImageDrive::free_buffer(int buf) { buf_free[buf] = true; } @@ -929,7 +962,7 @@ static bool match(const uint8 *p, int p_ return *n == 0xa0 || c == 16; } -bool D64Drive::find_file(const uint8 *pattern, int pattern_len, int &dir_track, int &dir_sector, int &entry, bool cont) +bool ImageDrive::find_file(const uint8 *pattern, int pattern_len, int &dir_track, int &dir_sector, int &entry, bool cont) { // Counter to prevent cyclic directories from resulting in an infinite loop int num_dir_blocks = 0; @@ -967,12 +1000,12 @@ bool D64Drive::find_file(const uint8 *pa return false; } -bool D64Drive::find_first_file(const uint8 *pattern, int pattern_len, int &dir_track, int &dir_sector, int &entry) +bool ImageDrive::find_first_file(const uint8 *pattern, int pattern_len, int &dir_track, int &dir_sector, int &entry) { return find_file(pattern, pattern_len, dir_track, dir_sector, entry, false); } -bool D64Drive::find_next_file(const uint8 *pattern, int pattern_len, int &dir_track, int &dir_sector, int &entry) +bool ImageDrive::find_next_file(const uint8 *pattern, int pattern_len, int &dir_track, int &dir_sector, int &entry) { return find_file(pattern, pattern_len, dir_track, dir_sector, entry, true); } @@ -984,7 +1017,7 @@ bool D64Drive::find_next_file(const uint * The track/sector and entry numbers are returned */ -bool D64Drive::alloc_dir_entry(int &track, int §or, int &entry) +bool ImageDrive::alloc_dir_entry(int &track, int §or, int &entry) { // First look for free entry in existing directory blocks dir[DIR_NEXT_TRACK] = DIR_TRACK; @@ -995,8 +1028,10 @@ bool D64Drive::alloc_dir_entry(int &trac uint8 *de = dir + DIR_ENTRIES; for (entry=0; entry<8; entry++, de+=SIZEOF_DE) { - if (de[DE_TYPE] == 0) + if (de[DE_TYPE] == 0) { + D(bug(" allocated entry %d in dir track %d, sector %d\n", entry, track, sector)); return true; + } } } @@ -1004,6 +1039,7 @@ bool D64Drive::alloc_dir_entry(int &trac int last_track = track, last_sector = sector; if (!alloc_next_block(track, sector, DIR_INTERLEAVE)) return false; + D(bug(" new directory block track %d, sector %d\n", track, sector)); // Write link to new block to last block dir[DIR_NEXT_TRACK] = track; @@ -1022,7 +1058,7 @@ bool D64Drive::alloc_dir_entry(int &trac * Test if block is free in BAM (track/sector are not checked for validity) */ -bool D64Drive::is_block_free(int track, int sector) +bool ImageDrive::is_block_free(int track, int sector) { uint8 *p = bam + BAM_BITMAP + (track - 1) * 4; int byte = sector / 8 + 1; @@ -1035,7 +1071,7 @@ bool D64Drive::is_block_free(int track, * Get number of free blocks on a track */ -int D64Drive::num_free_blocks(int track) +int ImageDrive::num_free_blocks(int track) { return bam[BAM_BITMAP + (track - 1) * 4]; } @@ -1061,7 +1097,7 @@ static void clear_bam(uint8 *bam) * Allocate block in BAM, returns error code */ -int D64Drive::alloc_block(int track, int sector) +int ImageDrive::alloc_block(int track, int sector) { if (track < 1 || track > 35 || sector < 0 || sector >= num_sectors[track]) return ERR_ILLEGALTS; @@ -1074,6 +1110,7 @@ int D64Drive::alloc_block(int track, int if (p[byte] & (1 << bit)) { // Yes, allocate and decrement free block count + D(bug("allocating block at track %d, sector %d\n", track, sector)); p[byte] &= ~(1 << bit); p[0]--; bam_dirty = true; @@ -1088,7 +1125,7 @@ int D64Drive::alloc_block(int track, int * Free block in BAM, returns error code */ -int D64Drive::free_block(int track, int sector) +int ImageDrive::free_block(int track, int sector) { if (track < 1 || track > 35 || sector < 0 || sector >= num_sectors[track]) return ERR_ILLEGALTS; @@ -1101,6 +1138,7 @@ int D64Drive::free_block(int track, int if (!(p[byte] & (1 << bit))) { // Yes, free and increment free block count + D(bug("freeing block at track %d, sector %d\n", track, sector)); p[byte] |= (1 << bit); p[0]++; bam_dirty = true; @@ -1113,7 +1151,7 @@ int D64Drive::free_block(int track, int * Allocate chain of data blocks in BAM */ -bool D64Drive::alloc_block_chain(int track, int sector) +bool ImageDrive::alloc_block_chain(int track, int sector) { uint8 buf[256]; while (alloc_block(track, sector) == ERR_OK) { @@ -1130,7 +1168,7 @@ bool D64Drive::alloc_block_chain(int tra * Free chain of data blocks in BAM */ -bool D64Drive::free_block_chain(int track, int sector) +bool ImageDrive::free_block_chain(int track, int sector) { uint8 buf[256]; while (free_block(track, sector) == ERR_OK) { @@ -1150,7 +1188,7 @@ bool D64Drive::free_block_chain(int trac * begin */ -bool D64Drive::alloc_next_block(int &track, int §or, int interleave) +bool ImageDrive::alloc_next_block(int &track, int §or, int interleave) { // Find track with free blocks bool side_changed = false; @@ -1262,7 +1300,7 @@ const int conv_job_error[16] = { }; // Read sector, return error code -int read_sector(FILE *f, const image_file_desc &desc, int track, int sector, uint8 *buffer) +static int read_sector(FILE *f, const image_file_desc &desc, int track, int sector, uint8 *buffer) { // Convert track/sector to byte offset in file long offset = offset_from_ts(desc, track, sector); @@ -1282,7 +1320,7 @@ int read_sector(FILE *f, const image_fil } // Write sector, return error code -int write_sector(FILE *f, const image_file_desc &desc, int track, int sector, uint8 *buffer) +static int write_sector(FILE *f, const image_file_desc &desc, int track, int sector, uint8 *buffer) { // Convert track/sector to byte offset in file long offset = offset_from_ts(desc, track, sector); @@ -1300,7 +1338,7 @@ int write_sector(FILE *f, const image_fi } // Read sector and set error message, returns false on error -bool D64Drive::read_sector(int track, int sector, uint8 *buffer) +bool ImageDrive::read_sector(int track, int sector, uint8 *buffer) { int error = ::read_sector(the_file, desc, track, sector, buffer); if (error) @@ -1309,7 +1347,7 @@ bool D64Drive::read_sector(int track, in } // Write sector and set error message, returns false on error -bool D64Drive::write_sector(int track, int sector, uint8 *buffer) +bool ImageDrive::write_sector(int track, int sector, uint8 *buffer) { int error = ::write_sector(the_file, desc, track, sector, buffer); if (error) @@ -1318,7 +1356,7 @@ bool D64Drive::write_sector(int track, i } // Write error info back to image file -void write_back_error_info(FILE *f, const image_file_desc &desc) +static void write_back_error_info(FILE *f, const image_file_desc &desc) { if (desc.type == TYPE_D64 && desc.has_error_info) { int num_sectors = desc.num_tracks == 40 ? NUM_SECTORS_40 : NUM_SECTORS_35; @@ -1328,7 +1366,7 @@ void write_back_error_info(FILE *f, cons } // Format disk image -bool format_image(FILE *f, image_file_desc &desc, bool lowlevel, uint8 id1, uint8 id2, const uint8 *disk_name, int disk_name_len) +static bool format_image(FILE *f, image_file_desc &desc, bool lowlevel, uint8 id1, uint8 id2, const uint8 *disk_name, int disk_name_len) { uint8 p[256]; @@ -1392,7 +1430,7 @@ bool format_image(FILE *f, image_file_de */ // BLOCK-READ:channel,0,track,sector -void D64Drive::block_read_cmd(int channel, int track, int sector, bool user_cmd) +void ImageDrive::block_read_cmd(int channel, int track, int sector, bool user_cmd) { if (channel >= 16 || ch[channel].mode != CHMOD_DIRECT) { set_error(ERR_NOCHANNEL); @@ -1410,7 +1448,7 @@ void D64Drive::block_read_cmd(int channe } // BLOCK-WRITE:channel,0,track,sector -void D64Drive::block_write_cmd(int channel, int track, int sector, bool user_cmd) +void ImageDrive::block_write_cmd(int channel, int track, int sector, bool user_cmd) { if (write_protected) { set_error(ERR_WRITEPROTECT); @@ -1431,7 +1469,7 @@ void D64Drive::block_write_cmd(int chann } // BLOCK-ALLOCATE:0,track,sector -void D64Drive::block_allocate_cmd(int track, int sector) +void ImageDrive::block_allocate_cmd(int track, int sector) { int err = alloc_block(track, sector); if (err) { @@ -1459,7 +1497,7 @@ void D64Drive::block_allocate_cmd(int tr } // BLOCK-FREE:0,track,sector -void D64Drive::block_free_cmd(int track, int sector) +void ImageDrive::block_free_cmd(int track, int sector) { int err = free_block(track, sector); if (err) @@ -1467,7 +1505,7 @@ void D64Drive::block_free_cmd(int track, } // BUFFER-POINTER:channel,pos -void D64Drive::buffer_pointer_cmd(int channel, int pos) +void ImageDrive::buffer_pointer_cmd(int channel, int pos) { if (channel >= 16 || ch[channel].mode != CHMOD_DIRECT) { set_error(ERR_NOCHANNEL); @@ -1478,7 +1516,7 @@ void D64Drive::buffer_pointer_cmd(int ch } // M-R[] -void D64Drive::mem_read_cmd(uint16 adr, uint8 len) +void ImageDrive::mem_read_cmd(uint16 adr, uint8 len) { error_len = len; if (adr >= 0x300 && adr < 0x1000) { @@ -1495,7 +1533,7 @@ void D64Drive::mem_read_cmd(uint16 adr, } // M-W -void D64Drive::mem_write_cmd(uint16 adr, uint8 len, uint8 *p) +void ImageDrive::mem_write_cmd(uint16 adr, uint8 len, uint8 *p) { while (len) { if (adr >= 0x300 && adr < 0x1000) { @@ -1512,7 +1550,7 @@ void D64Drive::mem_write_cmd(uint16 adr, // COPY:new=file1,file2,... // ^ ^ // new_file old_files -void D64Drive::copy_cmd(const uint8 *new_file, int new_file_len, const uint8 *old_files, int old_files_len) +void ImageDrive::copy_cmd(const uint8 *new_file, int new_file_len, const uint8 *old_files, int old_files_len) { // Check if destination file is already present int dir_track, dir_sector, entry; @@ -1575,7 +1613,7 @@ void D64Drive::copy_cmd(const uint8 *new // RENAME:new=old // ^ ^ // new_file old_file -void D64Drive::rename_cmd(const uint8 *new_file, int new_file_len, const uint8 *old_file, int old_file_len) +void ImageDrive::rename_cmd(const uint8 *new_file, int new_file_len, const uint8 *old_file, int old_file_len) { // Check if destination file is already present int dir_track, dir_sector, entry; @@ -1606,7 +1644,7 @@ void D64Drive::rename_cmd(const uint8 *n // SCRATCH:file1,file2,... // ^ // files -void D64Drive::scratch_cmd(const uint8 *files, int files_len) +void ImageDrive::scratch_cmd(const uint8 *files, int files_len) { // Check for write-protection if (write_protected) { @@ -1654,7 +1692,7 @@ void D64Drive::scratch_cmd(const uint8 * } // INITIALIZE -void D64Drive::initialize_cmd(void) +void ImageDrive::initialize_cmd(void) { // Close all channels and re-read BAM close_all_channels(); @@ -1668,7 +1706,7 @@ void D64Drive::initialize_cmd(void) // NEW:name,id // ^ ^ // name comma (or NULL) -void D64Drive::new_cmd(const uint8 *name, int name_len, const uint8 *comma) +void ImageDrive::new_cmd(const uint8 *name, int name_len, const uint8 *comma) { // Check for write-protection if (write_protected) { @@ -1705,7 +1743,7 @@ void D64Drive::new_cmd(const uint8 *name } // VALIDATE -void D64Drive::validate_cmd(void) +void ImageDrive::validate_cmd(void) { // Backup of old BAM in case something goes amiss uint8 old_bam[256]; @@ -1925,7 +1963,7 @@ error: * Open disk image file, return file handle */ -FILE *open_image_file(const char *path, bool write_mode) +static FILE *open_image_file(const char *path, bool write_mode) { #if 0 if (is_zipcode_file(path)) { @@ -2009,7 +2047,7 @@ static bool parse_x64_file(FILE *f, imag return true; } -bool parse_image_file(FILE *f, image_file_desc &desc) +static bool parse_image_file(FILE *f, image_file_desc &desc) { // Read header uint8 header[64]; @@ -2031,6 +2069,76 @@ bool parse_image_file(FILE *f, image_fil } +/* + * Read directory of disk image file into (empty) c64_dir_entry vector, + * returns false on error + */ + +bool ReadImageDirectory(const char *path, vector &vec) +{ + bool result = false; + + // Open file + FILE *f = open_image_file(path, false); + if (f) { + int num_dir_blocks = 0; + + // Determine file type and fill in image_file_desc structure + image_file_desc desc; + if (!parse_image_file(f, desc)) + goto done; + + // Scan all directory blocks + uint8 dir[256]; + dir[DIR_NEXT_TRACK] = DIR_TRACK; + dir[DIR_NEXT_SECTOR] = 1; + + while (dir[DIR_NEXT_TRACK] && num_dir_blocks < num_sectors[DIR_TRACK]) { + if (read_sector(f, desc, dir[DIR_NEXT_TRACK], dir[DIR_NEXT_SECTOR], dir) != ERR_OK) + break; + num_dir_blocks++; + + // Scan all 8 entries of a block + uint8 *de = dir + DIR_ENTRIES; + for (int j=0; j<8; j++, de+=SIZEOF_DE) { + + // Skip empty entries + if (de[DE_TYPE] == 0) + continue; + + // Convert file name (strip everything after and including the first trailing space) + uint8 name_buf[17]; + memcpy(name_buf, de + DE_NAME, 16); + name_buf[16] = 0; + uint8 *p = (uint8 *)memchr(name_buf, 0xa0, 16); + if (p) + *p = 0; + + // Convert file type + int type = de[DE_TYPE] & 7; + if (type > 4) + type = FTYPE_UNKNOWN; + + // Read start address + uint8 sa_lo = 0, sa_hi = 0; + uint8 buf[256]; + if (read_sector(f, desc, de[DE_TRACK], de[DE_SECTOR], buf) == ERR_OK) { + sa_lo = buf[2]; + sa_hi = buf[3]; + } + + // Add entry + vec.push_back(c64_dir_entry(name_buf, type, !(de[DE_TYPE] & 0x80), de[DE_TYPE] & 0x40, ((de[DE_NUM_BLOCKS_H] << 8) + de[DE_NUM_BLOCKS_L]) * 254, 0, sa_lo, sa_hi)); + } + } + + result = true; +done: fclose(f); + } + return result; +} + + /* * Create new blank disk image file, returns false on error */