--- Frodo4/Src/1541d64.cpp 2004/01/11 14:03:29 1.5 +++ Frodo4/Src/1541d64.cpp 2004/01/12 14:58:26 1.7 @@ -34,6 +34,9 @@ #include "C64.h" #include "main.h" +#define DEBUG 0 +#include "debug.h" + // Channel modes (IRC users listen up :-) enum { @@ -112,7 +115,7 @@ static bool match(const uint8 *p, int p_ * 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) { for (int i=0; i<18; i++) { ch[i].mode = CHMOD_FREE; @@ -132,7 +135,7 @@ D64Drive::D64Drive(IEC *iec, const char * Destructor */ -D64Drive::~D64Drive() +ImageDrive::~ImageDrive() { close_image(); } @@ -142,7 +145,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 +163,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 +197,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 +231,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 +242,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 +276,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 +342,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 +364,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 +391,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 +421,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 +454,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 +609,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 +642,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 +672,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 +690,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 +712,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 +727,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 +756,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 +795,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 +838,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 +872,7 @@ uint8 D64Drive::Write(int channel, uint8 * Reset drive */ -void D64Drive::Reset(void) +void ImageDrive::Reset(void) { close_all_channels(); @@ -874,7 +899,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 +925,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 +954,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 +992,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 +1009,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 +1020,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 +1031,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 +1050,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 +1063,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 +1089,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 +1102,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 +1117,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 +1130,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 +1143,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 +1160,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 +1180,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; @@ -1300,7 +1330,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 +1339,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) @@ -1392,7 +1422,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 +1440,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 +1461,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 +1489,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 +1497,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 +1508,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 +1525,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 +1542,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 +1605,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 +1636,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 +1684,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 +1698,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 +1735,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];