1 |
|
/* |
2 |
|
* 1541d64.cpp - 1541 emulation in disk image files (.d64/.x64/zipcode) |
3 |
|
* |
4 |
< |
* Frodo (C) 1994-1997,2002-2003 Christian Bauer |
4 |
> |
* Frodo Copyright (C) Christian Bauer |
5 |
|
* zipcode decoding routines (C) 1993-1997 Marko Mäkelä, Paul David Doherty |
6 |
|
* |
7 |
|
* This program is free software; you can redistribute it and/or modify |
34 |
|
#include "C64.h" |
35 |
|
#include "main.h" |
36 |
|
|
37 |
+ |
#define DEBUG 0 |
38 |
+ |
#include "debug.h" |
39 |
+ |
|
40 |
|
|
41 |
|
// Channel modes (IRC users listen up :-) |
42 |
|
enum { |
109 |
|
|
110 |
|
// Prototypes |
111 |
|
static bool match(const uint8 *p, int p_len, const uint8 *n); |
112 |
+ |
static FILE *open_image_file(const char *path, bool write_mode); |
113 |
+ |
static bool parse_image_file(FILE *f, image_file_desc &desc); |
114 |
|
|
115 |
|
|
116 |
|
/* |
119 |
|
|
120 |
|
ImageDrive::ImageDrive(IEC *iec, const char *filepath) : Drive(iec), the_file(NULL), bam(ram + 0x700), bam_dirty(false) |
121 |
|
{ |
122 |
+ |
desc.type = TYPE_D64; |
123 |
+ |
desc.header_size = 0; |
124 |
+ |
desc.num_tracks = 35; |
125 |
+ |
desc.id1 = desc.id2 = 0; |
126 |
+ |
desc.has_error_info = false; |
127 |
+ |
|
128 |
|
for (int i=0; i<18; i++) { |
129 |
|
ch[i].mode = CHMOD_FREE; |
130 |
|
ch[i].buf = NULL; |
207 |
|
|
208 |
|
uint8 ImageDrive::Open(int channel, const uint8 *name, int name_len) |
209 |
|
{ |
210 |
+ |
D(bug("ImageDrive::Open channel %d, file %s\n", channel, name)); |
211 |
+ |
|
212 |
|
set_error(ERR_OK); |
213 |
|
|
214 |
|
// Channel 15: execute file name as command |
250 |
|
if (plain_name_len > 16) |
251 |
|
plain_name_len = 16; |
252 |
|
|
253 |
+ |
D(bug(" plain name %s, type %d, mode %d\n", plain_name, type, mode)); |
254 |
+ |
|
255 |
|
// Channel 0 is READ, channel 1 is WRITE |
256 |
|
if (channel == 0 || channel == 1) { |
257 |
|
mode = channel ? FMODE_WRITE : FMODE_READ; |
284 |
|
if (find_first_file(plain_name, plain_name_len, dir_track, dir_sector, entry)) { |
285 |
|
|
286 |
|
// File exists |
287 |
+ |
D(bug(" file exists, dir track %d, sector %d, entry %d\n", dir_track, dir_sector, entry)); |
288 |
|
ch[channel].dir_track = dir_track; |
289 |
|
ch[channel].dir_sector = dir_sector; |
290 |
|
ch[channel].entry = entry; |
350 |
|
} else { |
351 |
|
|
352 |
|
// File doesn't exist |
353 |
+ |
D(bug(" file not found\n")); |
354 |
+ |
|
355 |
|
// Set file type to SEQ if not specified in file name |
356 |
|
if (type == FTYPE_DEL) |
357 |
|
type = FTYPE_SEQ; |
374 |
|
|
375 |
|
uint8 ImageDrive::open_file_ts(int channel, int track, int sector) |
376 |
|
{ |
377 |
+ |
D(bug("open_file_ts track %d, sector %d\n", track, sector)); |
378 |
+ |
|
379 |
|
// Allocate buffer and set channel mode |
380 |
|
int buf = alloc_buffer(-1); |
381 |
|
if (buf == -1) { |
401 |
|
|
402 |
|
uint8 ImageDrive::create_file(int channel, const uint8 *name, int name_len, int type, bool overwrite) |
403 |
|
{ |
404 |
+ |
D(bug("create_file %s, type %d\n", name, type)); |
405 |
+ |
|
406 |
|
// Allocate buffer |
407 |
|
int buf = alloc_buffer(-1); |
408 |
|
if (buf == -1) { |
429 |
|
return ST_OK; |
430 |
|
} |
431 |
|
ch[channel].num_blocks = 1; |
432 |
+ |
D(bug(" first data block on track %d, sector %d\n", ch[channel].track, ch[channel].sector)); |
433 |
|
|
434 |
|
// Write directory entry |
435 |
|
memset(de, 0, SIZEOF_DE); |
652 |
|
|
653 |
|
uint8 ImageDrive::Close(int channel) |
654 |
|
{ |
655 |
+ |
D(bug("ImageDrive::Close channel %d\n", channel)); |
656 |
+ |
|
657 |
|
switch (ch[channel].mode) { |
658 |
|
case CHMOD_FREE: |
659 |
|
break; |
680 |
|
// Write last data block |
681 |
|
ch[channel].buf[0] = 0; |
682 |
|
ch[channel].buf[1] = ch[channel].buf_len - 1; |
683 |
+ |
D(bug(" writing last data block\n")); |
684 |
|
if (!write_sector(ch[channel].track, ch[channel].sector, ch[channel].buf)) |
685 |
|
goto free; |
686 |
|
|
698 |
|
de[DE_OVR_TRACK] = de[DE_OVR_SECTOR] = 0; |
699 |
|
} |
700 |
|
write_sector(ch[channel].dir_track, ch[channel].dir_sector, dir); |
701 |
+ |
D(bug(" directory entry updated\n")); |
702 |
|
} |
703 |
|
free: free_buffer(ch[channel].buf_num); |
704 |
|
ch[channel].buf = NULL; |
737 |
|
|
738 |
|
uint8 ImageDrive::Read(int channel, uint8 &byte) |
739 |
|
{ |
740 |
+ |
// D(bug("ImageDrive::Read channel %d\n", channel)); |
741 |
+ |
|
742 |
|
switch (ch[channel].mode) { |
743 |
|
case CHMOD_FREE: |
744 |
|
if (current_error == ERR_OK) |
764 |
|
|
765 |
|
// Read next block if necessary |
766 |
|
if (ch[channel].buf_len == 0 && ch[channel].buf[0]) { |
767 |
+ |
D(bug(" reading next data block track %d, sector %d\n", ch[channel].buf[0], ch[channel].buf[1])); |
768 |
|
if (!read_sector(ch[channel].buf[0], ch[channel].buf[1], ch[channel].buf)) |
769 |
|
return ST_READ_TIMEOUT; |
770 |
|
ch[channel].buf_ptr = ch[channel].buf + 2; |
805 |
|
|
806 |
|
uint8 ImageDrive::Write(int channel, uint8 byte, bool eoi) |
807 |
|
{ |
808 |
+ |
// D(bug("ImageDrive::Write channel %d, byte %02x, eoi %d\n", channel, byte, eoi)); |
809 |
+ |
|
810 |
|
switch (ch[channel].mode) { |
811 |
|
case CHMOD_FREE: |
812 |
|
if (current_error == ERR_OK) |
846 |
|
if (!alloc_next_block(track, sector, DATA_INTERLEAVE)) |
847 |
|
return ST_TIMEOUT; |
848 |
|
ch[channel].num_blocks++; |
849 |
+ |
D(bug("next data block on track %d, sector %d\n", track, sector)); |
850 |
|
|
851 |
|
// Write buffer with link to new block |
852 |
|
ch[channel].buf[0] = track; |
1028 |
|
|
1029 |
|
uint8 *de = dir + DIR_ENTRIES; |
1030 |
|
for (entry=0; entry<8; entry++, de+=SIZEOF_DE) { |
1031 |
< |
if (de[DE_TYPE] == 0) |
1031 |
> |
if (de[DE_TYPE] == 0) { |
1032 |
> |
D(bug(" allocated entry %d in dir track %d, sector %d\n", entry, track, sector)); |
1033 |
|
return true; |
1034 |
+ |
} |
1035 |
|
} |
1036 |
|
} |
1037 |
|
|
1039 |
|
int last_track = track, last_sector = sector; |
1040 |
|
if (!alloc_next_block(track, sector, DIR_INTERLEAVE)) |
1041 |
|
return false; |
1042 |
+ |
D(bug(" new directory block track %d, sector %d\n", track, sector)); |
1043 |
|
|
1044 |
|
// Write link to new block to last block |
1045 |
|
dir[DIR_NEXT_TRACK] = track; |
1110 |
|
if (p[byte] & (1 << bit)) { |
1111 |
|
|
1112 |
|
// Yes, allocate and decrement free block count |
1113 |
+ |
D(bug("allocating block at track %d, sector %d\n", track, sector)); |
1114 |
|
p[byte] &= ~(1 << bit); |
1115 |
|
p[0]--; |
1116 |
|
bam_dirty = true; |
1138 |
|
if (!(p[byte] & (1 << bit))) { |
1139 |
|
|
1140 |
|
// Yes, free and increment free block count |
1141 |
+ |
D(bug("freeing block at track %d, sector %d\n", track, sector)); |
1142 |
|
p[byte] |= (1 << bit); |
1143 |
|
p[0]++; |
1144 |
|
bam_dirty = true; |
1300 |
|
}; |
1301 |
|
|
1302 |
|
// Read sector, return error code |
1303 |
< |
int read_sector(FILE *f, const image_file_desc &desc, int track, int sector, uint8 *buffer) |
1303 |
> |
static int read_sector(FILE *f, const image_file_desc &desc, int track, int sector, uint8 *buffer) |
1304 |
|
{ |
1305 |
|
// Convert track/sector to byte offset in file |
1306 |
|
long offset = offset_from_ts(desc, track, sector); |
1320 |
|
} |
1321 |
|
|
1322 |
|
// Write sector, return error code |
1323 |
< |
int write_sector(FILE *f, const image_file_desc &desc, int track, int sector, uint8 *buffer) |
1323 |
> |
static int write_sector(FILE *f, const image_file_desc &desc, int track, int sector, uint8 *buffer) |
1324 |
|
{ |
1325 |
|
// Convert track/sector to byte offset in file |
1326 |
|
long offset = offset_from_ts(desc, track, sector); |
1356 |
|
} |
1357 |
|
|
1358 |
|
// Write error info back to image file |
1359 |
< |
void write_back_error_info(FILE *f, const image_file_desc &desc) |
1359 |
> |
static void write_back_error_info(FILE *f, const image_file_desc &desc) |
1360 |
|
{ |
1361 |
|
if (desc.type == TYPE_D64 && desc.has_error_info) { |
1362 |
|
int num_sectors = desc.num_tracks == 40 ? NUM_SECTORS_40 : NUM_SECTORS_35; |
1366 |
|
} |
1367 |
|
|
1368 |
|
// Format disk image |
1369 |
< |
bool format_image(FILE *f, image_file_desc &desc, bool lowlevel, uint8 id1, uint8 id2, const uint8 *disk_name, int disk_name_len) |
1369 |
> |
static bool format_image(FILE *f, image_file_desc &desc, bool lowlevel, uint8 id1, uint8 id2, const uint8 *disk_name, int disk_name_len) |
1370 |
|
{ |
1371 |
|
uint8 p[256]; |
1372 |
|
|
1963 |
|
* Open disk image file, return file handle |
1964 |
|
*/ |
1965 |
|
|
1966 |
< |
FILE *open_image_file(const char *path, bool write_mode) |
1966 |
> |
static FILE *open_image_file(const char *path, bool write_mode) |
1967 |
|
{ |
1968 |
|
#if 0 |
1969 |
|
if (is_zipcode_file(path)) { |
2047 |
|
return true; |
2048 |
|
} |
2049 |
|
|
2050 |
< |
bool parse_image_file(FILE *f, image_file_desc &desc) |
2050 |
> |
static bool parse_image_file(FILE *f, image_file_desc &desc) |
2051 |
|
{ |
2052 |
|
// Read header |
2053 |
|
uint8 header[64]; |
2069 |
|
} |
2070 |
|
|
2071 |
|
|
2072 |
+ |
/* |
2073 |
+ |
* Read directory of disk image file into (empty) c64_dir_entry vector, |
2074 |
+ |
* returns false on error |
2075 |
+ |
*/ |
2076 |
+ |
|
2077 |
+ |
bool ReadImageDirectory(const char *path, vector<c64_dir_entry> &vec) |
2078 |
+ |
{ |
2079 |
+ |
bool result = false; |
2080 |
+ |
|
2081 |
+ |
// Open file |
2082 |
+ |
FILE *f = open_image_file(path, false); |
2083 |
+ |
if (f) { |
2084 |
+ |
int num_dir_blocks = 0; |
2085 |
+ |
|
2086 |
+ |
// Determine file type and fill in image_file_desc structure |
2087 |
+ |
image_file_desc desc; |
2088 |
+ |
if (!parse_image_file(f, desc)) |
2089 |
+ |
goto done; |
2090 |
+ |
|
2091 |
+ |
// Scan all directory blocks |
2092 |
+ |
uint8 dir[256]; |
2093 |
+ |
dir[DIR_NEXT_TRACK] = DIR_TRACK; |
2094 |
+ |
dir[DIR_NEXT_SECTOR] = 1; |
2095 |
+ |
|
2096 |
+ |
while (dir[DIR_NEXT_TRACK] && num_dir_blocks < num_sectors[DIR_TRACK]) { |
2097 |
+ |
if (read_sector(f, desc, dir[DIR_NEXT_TRACK], dir[DIR_NEXT_SECTOR], dir) != ERR_OK) |
2098 |
+ |
break; |
2099 |
+ |
num_dir_blocks++; |
2100 |
+ |
|
2101 |
+ |
// Scan all 8 entries of a block |
2102 |
+ |
uint8 *de = dir + DIR_ENTRIES; |
2103 |
+ |
for (int j=0; j<8; j++, de+=SIZEOF_DE) { |
2104 |
+ |
|
2105 |
+ |
// Skip empty entries |
2106 |
+ |
if (de[DE_TYPE] == 0) |
2107 |
+ |
continue; |
2108 |
+ |
|
2109 |
+ |
// Convert file name (strip everything after and including the first trailing space) |
2110 |
+ |
uint8 name_buf[17]; |
2111 |
+ |
memcpy(name_buf, de + DE_NAME, 16); |
2112 |
+ |
name_buf[16] = 0; |
2113 |
+ |
uint8 *p = (uint8 *)memchr(name_buf, 0xa0, 16); |
2114 |
+ |
if (p) |
2115 |
+ |
*p = 0; |
2116 |
+ |
|
2117 |
+ |
// Convert file type |
2118 |
+ |
int type = de[DE_TYPE] & 7; |
2119 |
+ |
if (type > 4) |
2120 |
+ |
type = FTYPE_UNKNOWN; |
2121 |
+ |
|
2122 |
+ |
// Read start address |
2123 |
+ |
uint8 sa_lo = 0, sa_hi = 0; |
2124 |
+ |
uint8 buf[256]; |
2125 |
+ |
if (read_sector(f, desc, de[DE_TRACK], de[DE_SECTOR], buf) == ERR_OK) { |
2126 |
+ |
sa_lo = buf[2]; |
2127 |
+ |
sa_hi = buf[3]; |
2128 |
+ |
} |
2129 |
+ |
|
2130 |
+ |
// Add entry |
2131 |
+ |
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)); |
2132 |
+ |
} |
2133 |
+ |
} |
2134 |
+ |
|
2135 |
+ |
result = true; |
2136 |
+ |
done: fclose(f); |
2137 |
+ |
} |
2138 |
+ |
return result; |
2139 |
+ |
} |
2140 |
+ |
|
2141 |
+ |
|
2142 |
|
/* |
2143 |
|
* Create new blank disk image file, returns false on error |
2144 |
|
*/ |