src/emu/imagedev/flopimg.h
| r11457 | r11458 | |
| 1 | | /********************************************************************* |
| 2 | | |
| 3 | | flopimg.h |
| 4 | | |
| 5 | | Floppy disk image abstraction code |
| 6 | | |
| 7 | | *********************************************************************/ |
| 8 | | |
| 9 | | #ifndef FLOPIMG_H |
| 10 | | #define FLOPIMG_H |
| 11 | | |
| 12 | | #include "emu.h" |
| 13 | | #include "opresolv.h" |
| 14 | | |
| 15 | | |
| 16 | | /*************************************************************************** |
| 17 | | |
| 18 | | Constants |
| 19 | | |
| 20 | | ***************************************************************************/ |
| 21 | | |
| 22 | | #define FLOPPY_FLAGS_READWRITE 0 |
| 23 | | #define FLOPPY_FLAGS_READONLY 1 |
| 24 | | |
| 25 | | |
| 26 | | |
| 27 | | /*************************************************************************** |
| 28 | | |
| 29 | | Type definitions |
| 30 | | |
| 31 | | ***************************************************************************/ |
| 32 | | |
| 33 | | typedef enum |
| 34 | | { |
| 35 | | FLOPPY_ERROR_SUCCESS, /* no error */ |
| 36 | | FLOPPY_ERROR_INTERNAL, /* fatal internal error */ |
| 37 | | FLOPPY_ERROR_UNSUPPORTED, /* this operation is unsupported */ |
| 38 | | FLOPPY_ERROR_OUTOFMEMORY, /* ran out of memory */ |
| 39 | | FLOPPY_ERROR_SEEKERROR, /* attempted to seek to nonexistant location */ |
| 40 | | FLOPPY_ERROR_INVALIDIMAGE, /* this image in invalid */ |
| 41 | | FLOPPY_ERROR_READONLY, /* attempt to write to read-only image */ |
| 42 | | FLOPPY_ERROR_NOSPACE, |
| 43 | | FLOPPY_ERROR_PARAMOUTOFRANGE, |
| 44 | | FLOPPY_ERROR_PARAMNOTSPECIFIED |
| 45 | | } |
| 46 | | floperr_t; |
| 47 | | |
| 48 | | typedef struct _floppy_image floppy_image; |
| 49 | | |
| 50 | | struct FloppyCallbacks |
| 51 | | { |
| 52 | | floperr_t (*read_sector)(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen); |
| 53 | | floperr_t (*write_sector)(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam); |
| 54 | | floperr_t (*read_indexed_sector)(floppy_image *floppy, int head, int track, int sector_index, void *buffer, size_t buflen); |
| 55 | | floperr_t (*write_indexed_sector)(floppy_image *floppy, int head, int track, int sector_index, const void *buffer, size_t buflen, int ddam); |
| 56 | | floperr_t (*read_track)(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen); |
| 57 | | floperr_t (*write_track)(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen); |
| 58 | | floperr_t (*format_track)(floppy_image *floppy, int head, int track, option_resolution *params); |
| 59 | | floperr_t (*post_format)(floppy_image *floppy, option_resolution *params); |
| 60 | | int (*get_heads_per_disk)(floppy_image *floppy); |
| 61 | | int (*get_tracks_per_disk)(floppy_image *floppy); |
| 62 | | int (*get_sectors_per_track)(floppy_image *floppy, int head, int track); |
| 63 | | UINT32 (*get_track_size)(floppy_image *floppy, int head, int track); |
| 64 | | floperr_t (*get_sector_length)(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length); |
| 65 | | floperr_t (*get_indexed_sector_info)(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags); |
| 66 | | floperr_t (*get_track_data_offset)(floppy_image *floppy, int head, int track, UINT64 *offset); |
| 67 | | }; |
| 68 | | |
| 69 | | |
| 70 | | |
| 71 | | struct FloppyFormat |
| 72 | | { |
| 73 | | const char *name; |
| 74 | | const char *extensions; |
| 75 | | const char *description; |
| 76 | | floperr_t (*identify)(floppy_image *floppy, const struct FloppyFormat *format, int *vote); |
| 77 | | floperr_t (*construct)(floppy_image *floppy, const struct FloppyFormat *format, option_resolution *params); |
| 78 | | floperr_t (*destruct)(floppy_image *floppy, const struct FloppyFormat *format); |
| 79 | | const char *param_guidelines; |
| 80 | | }; |
| 81 | | |
| 82 | | #define FLOPPY_IDENTIFY(name) floperr_t name(floppy_image *floppy, const struct FloppyFormat *format, int *vote) |
| 83 | | #define FLOPPY_CONSTRUCT(name) floperr_t name(floppy_image *floppy, const struct FloppyFormat *format, option_resolution *params) |
| 84 | | #define FLOPPY_DESTRUCT(name) floperr_t name(floppy_image *floppy, const struct FloppyFormat *format) |
| 85 | | |
| 86 | | FLOPPY_IDENTIFY(td0_dsk_identify); |
| 87 | | FLOPPY_CONSTRUCT(td0_dsk_construct); |
| 88 | | FLOPPY_DESTRUCT(td0_dsk_destruct); |
| 89 | | |
| 90 | | FLOPPY_IDENTIFY(imd_dsk_identify); |
| 91 | | FLOPPY_CONSTRUCT(imd_dsk_construct); |
| 92 | | |
| 93 | | FLOPPY_IDENTIFY(cqm_dsk_identify); |
| 94 | | FLOPPY_CONSTRUCT(cqm_dsk_construct); |
| 95 | | |
| 96 | | FLOPPY_IDENTIFY(dsk_dsk_identify); |
| 97 | | FLOPPY_CONSTRUCT(dsk_dsk_construct); |
| 98 | | |
| 99 | | FLOPPY_IDENTIFY(d88_dsk_identify); |
| 100 | | FLOPPY_CONSTRUCT(d88_dsk_construct); |
| 101 | | |
| 102 | | FLOPPY_IDENTIFY(fdi_dsk_identify); |
| 103 | | FLOPPY_CONSTRUCT(fdi_dsk_construct); |
| 104 | | |
| 105 | | #define FLOPPY_OPTIONS_NAME(name) floppyoptions_##name |
| 106 | | |
| 107 | | #define FLOPPY_OPTIONS_START(name) \ |
| 108 | | const struct FloppyFormat floppyoptions_##name[] = \ |
| 109 | | { \ |
| 110 | | |
| 111 | | #define FLOPPY_OPTIONS_END0 \ |
| 112 | | { NULL } \ |
| 113 | | }; |
| 114 | | |
| 115 | | #define FLOPPY_OPTIONS_EXTERN(name) \ |
| 116 | | extern const struct FloppyFormat floppyoptions_##name[] \ |
| 117 | | |
| 118 | | #define FLOPPY_OPTION(name, extensions_, description_, identify_, construct_, destruct_, ranges_)\ |
| 119 | | { #name, extensions_, description_, identify_, construct_, destruct_, ranges_ }, \ |
| 120 | | |
| 121 | | #define FLOPPY_OPTIONS_END \ |
| 122 | | FLOPPY_OPTION( fdi, "fdi", "Formatted Disk Image", fdi_dsk_identify, fdi_dsk_construct, NULL, NULL) \ |
| 123 | | FLOPPY_OPTION( td0, "td0", "Teledisk floppy disk image", td0_dsk_identify, td0_dsk_construct, td0_dsk_destruct, NULL) \ |
| 124 | | FLOPPY_OPTION( imd, "imd", "IMD floppy disk image", imd_dsk_identify, imd_dsk_construct, NULL, NULL) \ |
| 125 | | FLOPPY_OPTION( cqm, "cqm,dsk", "CopyQM floppy disk image", cqm_dsk_identify, cqm_dsk_construct, NULL, NULL) \ |
| 126 | | FLOPPY_OPTION( dsk, "dsk", "DSK floppy disk image", dsk_dsk_identify, dsk_dsk_construct, NULL, NULL) \ |
| 127 | | FLOPPY_OPTION( d88, "d77,d88,1dd", "D88 Floppy Disk image", d88_dsk_identify, d88_dsk_construct, NULL, NULL) \ |
| 128 | | FLOPPY_OPTIONS_END0 |
| 129 | | |
| 130 | | FLOPPY_OPTIONS_EXTERN(default); |
| 131 | | |
| 132 | | #define PARAM_END '\0' |
| 133 | | #define PARAM_HEADS 'H' |
| 134 | | #define PARAM_TRACKS 'T' |
| 135 | | #define PARAM_SECTORS 'S' |
| 136 | | #define PARAM_SECTOR_LENGTH 'L' |
| 137 | | #define PARAM_INTERLEAVE 'I' |
| 138 | | #define PARAM_FIRST_SECTOR_ID 'F' |
| 139 | | |
| 140 | | #define HEADS(range) "H" #range |
| 141 | | #define TRACKS(range) "T" #range |
| 142 | | #define SECTORS(range) "S" #range |
| 143 | | #define SECTOR_LENGTH(range) "L" #range |
| 144 | | #define INTERLEAVE(range) "I" #range |
| 145 | | #define FIRST_SECTOR_ID(range) "F" #range |
| 146 | | |
| 147 | | |
| 148 | | /*************************************************************************** |
| 149 | | |
| 150 | | Prototypes |
| 151 | | |
| 152 | | ***************************************************************************/ |
| 153 | | |
| 154 | | OPTION_GUIDE_EXTERN(floppy_option_guide); |
| 155 | | |
| 156 | | /* opening, closing and creating of floppy images */ |
| 157 | | floperr_t floppy_open(void *fp, const struct io_procs *procs, const char *extension, const struct FloppyFormat *format, int flags, floppy_image **outfloppy); |
| 158 | | floperr_t floppy_open_choices(void *fp, const struct io_procs *procs, const char *extension, const struct FloppyFormat *formats, int flags, floppy_image **outfloppy); |
| 159 | | floperr_t floppy_create(void *fp, const struct io_procs *procs, const struct FloppyFormat *format, option_resolution *parameters, floppy_image **outfloppy); |
| 160 | | void floppy_close(floppy_image *floppy); |
| 161 | | |
| 162 | | /* useful for identifying a floppy image */ |
| 163 | | floperr_t floppy_identify(void *fp, const struct io_procs *procs, const char *extension, |
| 164 | | const struct FloppyFormat *formats, int *identified_format); |
| 165 | | |
| 166 | | /* functions useful within format constructors */ |
| 167 | | void *floppy_tag(floppy_image *floppy); |
| 168 | | void *floppy_create_tag(floppy_image *floppy, size_t tagsize); |
| 169 | | struct FloppyCallbacks *floppy_callbacks(floppy_image *floppy); |
| 170 | | UINT8 floppy_get_filler(floppy_image *floppy); |
| 171 | | void floppy_set_filler(floppy_image *floppy, UINT8 filler); |
| 172 | | |
| 173 | | /* calls for accessing disk image data */ |
| 174 | | floperr_t floppy_read_sector(floppy_image *floppy, int head, int track, int sector, int offset, void *buffer, size_t buffer_len); |
| 175 | | floperr_t floppy_write_sector(floppy_image *floppy, int head, int track, int sector, int offset, const void *buffer, size_t buffer_len, int ddam); |
| 176 | | floperr_t floppy_read_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, void *buffer, size_t buffer_len); |
| 177 | | floperr_t floppy_write_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, const void *buffer, size_t buffer_len, int ddam); |
| 178 | | floperr_t floppy_read_track(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len); |
| 179 | | floperr_t floppy_write_track(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len); |
| 180 | | floperr_t floppy_read_track_data(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len); |
| 181 | | floperr_t floppy_write_track_data(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len); |
| 182 | | floperr_t floppy_format_track(floppy_image *floppy, int head, int track, option_resolution *params); |
| 183 | | int floppy_get_tracks_per_disk(floppy_image *floppy); |
| 184 | | int floppy_get_heads_per_disk(floppy_image *floppy); |
| 185 | | UINT32 floppy_get_track_size(floppy_image *floppy, int head, int track); |
| 186 | | floperr_t floppy_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length); |
| 187 | | floperr_t floppy_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags); |
| 188 | | floperr_t floppy_get_sector_count(floppy_image *floppy, int head, int track, int *sector_count); |
| 189 | | floperr_t floppy_load_track(floppy_image *floppy, int head, int track, int dirtify, void **track_data, size_t *track_length); |
| 190 | | int floppy_is_read_only(floppy_image *floppy); |
| 191 | | UINT8 floppy_random_byte(floppy_image *floppy); |
| 192 | | |
| 193 | | /* accessors for meta information about the image */ |
| 194 | | const char *floppy_format_description(floppy_image *floppy); |
| 195 | | |
| 196 | | /* calls for accessing the raw disk image */ |
| 197 | | void floppy_image_read(floppy_image *floppy, void *buffer, UINT64 offset, size_t length); |
| 198 | | void floppy_image_write(floppy_image *floppy, const void *buffer, UINT64 offset, size_t length); |
| 199 | | void floppy_image_write_filler(floppy_image *floppy, UINT8 filler, UINT64 offset, size_t length); |
| 200 | | UINT64 floppy_image_size(floppy_image *floppy); |
| 201 | | |
| 202 | | /* misc */ |
| 203 | | const char *floppy_error(floperr_t err); |
| 204 | | |
| 205 | | |
| 206 | | #endif /* FLOPIMG_H */ |
| 207 | | |
src/emu/imagedev/cassimg.c
| r11457 | r11458 | |
| 1 | | /********************************************************************* |
| 2 | | |
| 3 | | cassimg.c |
| 4 | | |
| 5 | | Cassette tape image abstraction code |
| 6 | | |
| 7 | | *********************************************************************/ |
| 8 | | |
| 9 | | |
| 10 | | #include <string.h> |
| 11 | | |
| 12 | | #include "emu.h" |
| 13 | | #include "imageutl.h" |
| 14 | | #include "pool.h" |
| 15 | | #include "cassimg.h" |
| 16 | | |
| 17 | | |
| 18 | | /* debugging parameters */ |
| 19 | | #define LOG_PUT_SAMPLES 0 |
| 20 | | #define DUMP_CASSETTES 0 |
| 21 | | |
| 22 | | #define SAMPLES_PER_BLOCK 0x40000 |
| 23 | | #define CASSETTE_FLAG_DIRTY 0x10000 |
| 24 | | |
| 25 | | |
| 26 | | CASSETTE_FORMATLIST_START(cassette_default_formats) |
| 27 | | CASSETTE_FORMATLIST_END |
| 28 | | |
| 29 | | |
| 30 | | |
| 31 | | /********************************************************************* |
| 32 | | helper code |
| 33 | | *********************************************************************/ |
| 34 | | |
| 35 | | static double map_double(double d, UINT64 low, UINT64 high, UINT64 value) |
| 36 | | { |
| 37 | | #if defined(_MSC_VER) && (_MSC_VER <= 1200) |
| 38 | | /* casting unsigned __int64 to double is not supported on VC6 or before */ |
| 39 | | return d * (INT64)(value - low) / (INT64)(high - low); |
| 40 | | #else |
| 41 | | return d * (value - low) / (high - low); |
| 42 | | #endif |
| 43 | | } |
| 44 | | |
| 45 | | |
| 46 | | |
| 47 | | static size_t waveform_bytes_per_sample(int waveform_flags) |
| 48 | | { |
| 49 | | return (size_t) (1 << ((waveform_flags & 0x06) / 2)); |
| 50 | | } |
| 51 | | |
| 52 | | |
| 53 | | |
| 54 | | /********************************************************************* |
| 55 | | extrapolation and interpolation |
| 56 | | *********************************************************************/ |
| 57 | | |
| 58 | | static INT32 extrapolate8(INT8 value) |
| 59 | | { |
| 60 | | return ((INT32) value) << 24; |
| 61 | | } |
| 62 | | |
| 63 | | static INT32 extrapolate16(INT16 value) |
| 64 | | { |
| 65 | | return ((INT32) value) << 16; |
| 66 | | } |
| 67 | | |
| 68 | | static INT8 interpolate8(INT32 value) |
| 69 | | { |
| 70 | | return (INT8) (value >> 24); |
| 71 | | } |
| 72 | | |
| 73 | | static INT16 interpolate16(INT32 value) |
| 74 | | { |
| 75 | | return (INT16) (value >> 16); |
| 76 | | } |
| 77 | | |
| 78 | | |
| 79 | | |
| 80 | | /********************************************************************* |
| 81 | | initialization and termination |
| 82 | | *********************************************************************/ |
| 83 | | |
| 84 | | static cassette_image *cassette_init(const struct CassetteFormat *format, void *file, const struct io_procs *procs, int flags) |
| 85 | | { |
| 86 | | cassette_image *cassette; |
| 87 | | |
| 88 | | cassette = (cassette_image *)malloc(sizeof(struct _cassette_image)); |
| 89 | | if (!cassette) |
| 90 | | return NULL; |
| 91 | | |
| 92 | | memset(cassette, 0, sizeof(*cassette)); |
| 93 | | cassette->format = format; |
| 94 | | cassette->io.file = file; |
| 95 | | cassette->io.procs = procs; |
| 96 | | cassette->flags = flags; |
| 97 | | cassette->pool = pool_alloc_lib(NULL); |
| 98 | | return cassette; |
| 99 | | } |
| 100 | | |
| 101 | | |
| 102 | | |
| 103 | | static void cassette_finishinit(casserr_t err, cassette_image *cassette, cassette_image **outcassette) |
| 104 | | { |
| 105 | | if (cassette && (err || !outcassette)) |
| 106 | | { |
| 107 | | cassette_close(cassette); |
| 108 | | cassette = NULL; |
| 109 | | } |
| 110 | | if (outcassette) |
| 111 | | *outcassette = cassette; |
| 112 | | } |
| 113 | | |
| 114 | | |
| 115 | | |
| 116 | | static int good_format(const struct CassetteFormat *format, const char *extension, int flags) |
| 117 | | { |
| 118 | | if (extension && !image_find_extension(format->extensions, extension)) |
| 119 | | return FALSE; |
| 120 | | if (((flags & CASSETTE_FLAG_READONLY) == 0) && !format->save) |
| 121 | | return FALSE; |
| 122 | | return TRUE; |
| 123 | | } |
| 124 | | |
| 125 | | |
| 126 | | |
| 127 | | casserr_t cassette_open_choices(void *file, const struct io_procs *procs, const char *extension, |
| 128 | | const struct CassetteFormat *const *formats, int flags, cassette_image **outcassette) |
| 129 | | { |
| 130 | | casserr_t err; |
| 131 | | cassette_image *cassette; |
| 132 | | const struct CassetteFormat *format; |
| 133 | | struct CassetteOptions opts = {0, }; |
| 134 | | int i; |
| 135 | | |
| 136 | | /* if not specified, use the dummy arguments */ |
| 137 | | if (!formats) |
| 138 | | formats = cassette_default_formats; |
| 139 | | |
| 140 | | /* create the cassette object */ |
| 141 | | cassette = cassette_init(NULL, file, procs, flags); |
| 142 | | if (!cassette) |
| 143 | | { |
| 144 | | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 145 | | goto done; |
| 146 | | } |
| 147 | | |
| 148 | | /* identify the image */ |
| 149 | | format = NULL; |
| 150 | | for (i = 0; !format && formats[i]; i++) |
| 151 | | { |
| 152 | | if (good_format(formats[i], extension, flags)) |
| 153 | | { |
| 154 | | format = formats[i]; |
| 155 | | memset(&opts, 0, sizeof(opts)); |
| 156 | | err = format->identify(cassette, &opts); |
| 157 | | if (err == CASSETTE_ERROR_INVALIDIMAGE) |
| 158 | | format = NULL; |
| 159 | | else if (err) |
| 160 | | goto done; |
| 161 | | } |
| 162 | | } |
| 163 | | |
| 164 | | /* have we found a proper format */ |
| 165 | | if (!format) |
| 166 | | { |
| 167 | | err = CASSETTE_ERROR_INVALIDIMAGE; |
| 168 | | goto done; |
| 169 | | } |
| 170 | | cassette->format = format; |
| 171 | | |
| 172 | | /* read the options */ |
| 173 | | cassette->channels = opts.channels; |
| 174 | | cassette->sample_frequency = opts.sample_frequency; |
| 175 | | |
| 176 | | /* load the image */ |
| 177 | | err = format->load(cassette); |
| 178 | | if (err) |
| 179 | | goto done; |
| 180 | | |
| 181 | | /* success */ |
| 182 | | cassette->flags &= ~CASSETTE_FLAG_DIRTY; |
| 183 | | err = CASSETTE_ERROR_SUCCESS; |
| 184 | | |
| 185 | | done: |
| 186 | | cassette_finishinit(err, cassette, outcassette); |
| 187 | | return err; |
| 188 | | } |
| 189 | | |
| 190 | | |
| 191 | | |
| 192 | | casserr_t cassette_open(void *file, const struct io_procs *procs, |
| 193 | | const struct CassetteFormat *format, int flags, cassette_image **outcassette) |
| 194 | | { |
| 195 | | const struct CassetteFormat *formats[2]; |
| 196 | | formats[0] = format; |
| 197 | | formats[1] = NULL; |
| 198 | | return cassette_open_choices(file, procs, NULL, formats, flags, outcassette); |
| 199 | | } |
| 200 | | |
| 201 | | |
| 202 | | |
| 203 | | casserr_t cassette_create(void *file, const struct io_procs *procs, const struct CassetteFormat *format, |
| 204 | | const struct CassetteOptions *opts, int flags, cassette_image **outcassette) |
| 205 | | { |
| 206 | | casserr_t err; |
| 207 | | cassette_image *cassette; |
| 208 | | static const struct CassetteOptions default_options = { 1, 16, 44100 }; |
| 209 | | |
| 210 | | /* cannot create to a read only image */ |
| 211 | | if (flags & CASSETTE_FLAG_READONLY) |
| 212 | | return CASSETTE_ERROR_INVALIDIMAGE; |
| 213 | | |
| 214 | | /* is this a good format? */ |
| 215 | | if (!good_format(format, NULL, flags)) |
| 216 | | return CASSETTE_ERROR_INVALIDIMAGE; |
| 217 | | |
| 218 | | /* normalize arguments */ |
| 219 | | if (!opts) |
| 220 | | opts = &default_options; |
| 221 | | |
| 222 | | /* create the cassette object */ |
| 223 | | cassette = cassette_init(format, file, procs, flags); |
| 224 | | if (!cassette) |
| 225 | | { |
| 226 | | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 227 | | goto done; |
| 228 | | } |
| 229 | | |
| 230 | | /* read the options */ |
| 231 | | cassette->channels = opts->channels; |
| 232 | | cassette->sample_frequency = opts->sample_frequency; |
| 233 | | |
| 234 | | err = CASSETTE_ERROR_SUCCESS; |
| 235 | | |
| 236 | | done: |
| 237 | | cassette_finishinit(err, cassette, outcassette); |
| 238 | | return err; |
| 239 | | } |
| 240 | | |
| 241 | | |
| 242 | | |
| 243 | | static casserr_t cassette_perform_save(cassette_image *cassette) |
| 244 | | { |
| 245 | | struct CassetteInfo info; |
| 246 | | cassette_get_info(cassette, &info); |
| 247 | | return cassette->format->save(cassette, &info); |
| 248 | | } |
| 249 | | |
| 250 | | |
| 251 | | |
| 252 | | casserr_t cassette_save(cassette_image *cassette) |
| 253 | | { |
| 254 | | casserr_t err; |
| 255 | | |
| 256 | | if (!cassette->format || !cassette->format->save) |
| 257 | | return CASSETTE_ERROR_UNSUPPORTED; |
| 258 | | |
| 259 | | err = cassette_perform_save(cassette); |
| 260 | | if (err) |
| 261 | | return err; |
| 262 | | |
| 263 | | cassette->flags &= ~CASSETTE_FLAG_DIRTY; |
| 264 | | return CASSETTE_ERROR_SUCCESS; |
| 265 | | } |
| 266 | | |
| 267 | | |
| 268 | | |
| 269 | | void cassette_get_info(cassette_image *cassette, struct CassetteInfo *info) |
| 270 | | { |
| 271 | | memset(info, 0, sizeof(*info)); |
| 272 | | info->channels = cassette->channels; |
| 273 | | info->sample_count = cassette->sample_count; |
| 274 | | info->sample_frequency = cassette->sample_frequency; |
| 275 | | info->bits_per_sample = (int) waveform_bytes_per_sample(cassette->flags) * 8; |
| 276 | | } |
| 277 | | |
| 278 | | |
| 279 | | |
| 280 | | void cassette_close(cassette_image *cassette) |
| 281 | | { |
| 282 | | if (cassette) |
| 283 | | { |
| 284 | | if ((cassette->flags & CASSETTE_FLAG_DIRTY) && (cassette->flags & CASSETTE_FLAG_SAVEONEXIT)) |
| 285 | | cassette_save(cassette); |
| 286 | | pool_free_lib(cassette->pool); |
| 287 | | free(cassette); |
| 288 | | } |
| 289 | | } |
| 290 | | |
| 291 | | |
| 292 | | |
| 293 | | void cassette_change(cassette_image *cassette, void *file, const struct io_procs *procs, const struct CassetteFormat *format, int flags) |
| 294 | | { |
| 295 | | if ((flags & CASSETTE_FLAG_READONLY) == 0) |
| 296 | | flags |= CASSETTE_FLAG_DIRTY; |
| 297 | | cassette->io.file = file; |
| 298 | | cassette->io.procs = procs; |
| 299 | | cassette->format = format; |
| 300 | | cassette->flags = flags; |
| 301 | | } |
| 302 | | |
| 303 | | |
| 304 | | |
| 305 | | /********************************************************************* |
| 306 | | calls for accessing the raw cassette image |
| 307 | | *********************************************************************/ |
| 308 | | |
| 309 | | void cassette_image_read(cassette_image *cassette, void *buffer, UINT64 offset, size_t length) |
| 310 | | { |
| 311 | | io_generic_read(&cassette->io, buffer, offset, length); |
| 312 | | } |
| 313 | | |
| 314 | | |
| 315 | | |
| 316 | | void cassette_image_write(cassette_image *cassette, const void *buffer, UINT64 offset, size_t length) |
| 317 | | { |
| 318 | | io_generic_write(&cassette->io, buffer, offset, length); |
| 319 | | } |
| 320 | | |
| 321 | | |
| 322 | | |
| 323 | | UINT64 cassette_image_size(cassette_image *cassette) |
| 324 | | { |
| 325 | | return io_generic_size(&cassette->io); |
| 326 | | } |
| 327 | | |
| 328 | | |
| 329 | | |
| 330 | | /********************************************************************* |
| 331 | | waveform accesses |
| 332 | | *********************************************************************/ |
| 333 | | |
| 334 | | struct manipulation_ranges |
| 335 | | { |
| 336 | | int channel_first; |
| 337 | | int channel_last; |
| 338 | | size_t sample_first; |
| 339 | | size_t sample_last; |
| 340 | | }; |
| 341 | | |
| 342 | | |
| 343 | | |
| 344 | | static size_t my_round(double d) |
| 345 | | { |
| 346 | | size_t result; |
| 347 | | d += 0.5; |
| 348 | | result = (size_t) d; |
| 349 | | return result; |
| 350 | | } |
| 351 | | |
| 352 | | |
| 353 | | |
| 354 | | static casserr_t compute_manipulation_ranges(cassette_image *cassette, int channel, |
| 355 | | double time_index, double sample_period, struct manipulation_ranges *ranges) |
| 356 | | { |
| 357 | | if (channel < 0) |
| 358 | | { |
| 359 | | ranges->channel_first = 0; |
| 360 | | ranges->channel_last = cassette->channels - 1; |
| 361 | | } |
| 362 | | else |
| 363 | | { |
| 364 | | ranges->channel_first = channel; |
| 365 | | ranges->channel_last = channel; |
| 366 | | } |
| 367 | | |
| 368 | | ranges->sample_first = my_round(time_index * cassette->sample_frequency); |
| 369 | | ranges->sample_last = my_round((time_index + sample_period) * cassette->sample_frequency); |
| 370 | | |
| 371 | | if (ranges->sample_last > ranges->sample_first) |
| 372 | | ranges->sample_last--; |
| 373 | | |
| 374 | | return CASSETTE_ERROR_SUCCESS; |
| 375 | | } |
| 376 | | |
| 377 | | |
| 378 | | |
| 379 | | static casserr_t lookup_sample(cassette_image *cassette, int channel, size_t sample, int allocate, INT32 **ptr) |
| 380 | | { |
| 381 | | size_t sample_block; |
| 382 | | size_t sample_index; |
| 383 | | size_t sample_size; |
| 384 | | size_t new_block_count; |
| 385 | | size_t new_block_sample_count; |
| 386 | | INT32 *new_block; |
| 387 | | struct sample_block *new_blocks; |
| 388 | | struct sample_block *block; |
| 389 | | |
| 390 | | *ptr = NULL; |
| 391 | | sample_block = (sample / SAMPLES_PER_BLOCK) * cassette->channels + channel; |
| 392 | | sample_index = sample % SAMPLES_PER_BLOCK; |
| 393 | | sample_size = sizeof(block->block[0]); |
| 394 | | |
| 395 | | /* is this block beyond the edge of our waveform? */ |
| 396 | | if (sample_block >= cassette->block_count) |
| 397 | | { |
| 398 | | if (!allocate) |
| 399 | | return CASSETTE_ERROR_SUCCESS; |
| 400 | | |
| 401 | | /* allocate new blocks */ |
| 402 | | new_block_count = sample_block + 1; |
| 403 | | new_blocks = (struct sample_block *)pool_realloc_lib(cassette->pool, cassette->blocks, new_block_count * sizeof(cassette->blocks[0])); |
| 404 | | if (!new_blocks) |
| 405 | | return CASSETTE_ERROR_OUTOFMEMORY; |
| 406 | | |
| 407 | | cassette->blocks = new_blocks; |
| 408 | | memset(&cassette->blocks[cassette->block_count], 0, (new_block_count - cassette->block_count) * sizeof(cassette->blocks[0])); |
| 409 | | cassette->block_count = new_block_count; |
| 410 | | } |
| 411 | | |
| 412 | | block = &cassette->blocks[sample_block]; |
| 413 | | |
| 414 | | /* is this sample access off the current block? */ |
| 415 | | if (sample_index >= block->sample_count) |
| 416 | | { |
| 417 | | if (!allocate) |
| 418 | | return CASSETTE_ERROR_SUCCESS; |
| 419 | | |
| 420 | | new_block_sample_count = SAMPLES_PER_BLOCK; |
| 421 | | |
| 422 | | new_block = (INT32*)pool_realloc_lib(cassette->pool, block->block, new_block_sample_count * sample_size); |
| 423 | | if (!new_block) |
| 424 | | return CASSETTE_ERROR_OUTOFMEMORY; |
| 425 | | |
| 426 | | block->block = new_block; |
| 427 | | memset(&block->block[block->sample_count], 0, (new_block_sample_count - block->sample_count) * sample_size); |
| 428 | | block->sample_count = new_block_sample_count; |
| 429 | | } |
| 430 | | |
| 431 | | *ptr = &block->block[sample_index]; |
| 432 | | return CASSETTE_ERROR_SUCCESS; |
| 433 | | } |
| 434 | | |
| 435 | | |
| 436 | | |
| 437 | | /********************************************************************* |
| 438 | | waveform accesses |
| 439 | | *********************************************************************/ |
| 440 | | |
| 441 | | casserr_t cassette_get_samples(cassette_image *cassette, int channel, |
| 442 | | double time_index, double sample_period, size_t sample_count, size_t sample_bytes, |
| 443 | | void *samples, int waveform_flags) |
| 444 | | { |
| 445 | | casserr_t err; |
| 446 | | struct manipulation_ranges ranges; |
| 447 | | size_t sample_index; |
| 448 | | size_t cassette_sample_index; |
| 449 | | UINT8 *dest_ptr; |
| 450 | | const INT32 *source_ptr; |
| 451 | | double d; |
| 452 | | INT16 word; |
| 453 | | INT32 dword; |
| 454 | | INT64 sum; |
| 455 | | |
| 456 | | assert(cassette); |
| 457 | | |
| 458 | | err = compute_manipulation_ranges(cassette, channel, time_index, sample_period, &ranges); |
| 459 | | if (err) |
| 460 | | return err; |
| 461 | | |
| 462 | | for (sample_index = 0; sample_index < sample_count; sample_index++) |
| 463 | | { |
| 464 | | sum = 0; |
| 465 | | |
| 466 | | for (channel = ranges.channel_first; channel <= ranges.channel_last; channel++) |
| 467 | | { |
| 468 | | /* find the sample that we are putting */ |
| 469 | | d = map_double(ranges.sample_last + 1 - ranges.sample_first, 0, sample_count, sample_index) + ranges.sample_first; |
| 470 | | cassette_sample_index = (size_t) d; |
| 471 | | err = lookup_sample(cassette, channel, cassette_sample_index, TRUE, (INT32 **) &source_ptr); |
| 472 | | if (err) |
| 473 | | return err; |
| 474 | | |
| 475 | | sum += *source_ptr; |
| 476 | | } |
| 477 | | |
| 478 | | /* average out the samples */ |
| 479 | | sum /= (ranges.channel_last + 1 - ranges.channel_first); |
| 480 | | |
| 481 | | /* and write out the result */ |
| 482 | | dest_ptr = (UINT8*)samples; |
| 483 | | dest_ptr += waveform_bytes_per_sample(waveform_flags) * sample_index; |
| 484 | | switch(waveform_bytes_per_sample(waveform_flags)) |
| 485 | | { |
| 486 | | case 1: |
| 487 | | *((INT8 *) dest_ptr) = interpolate8(sum); |
| 488 | | break; |
| 489 | | case 2: |
| 490 | | word = interpolate16(sum); |
| 491 | | if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP) |
| 492 | | word = FLIPENDIAN_INT16(word); |
| 493 | | *((INT16 *) dest_ptr) = word; |
| 494 | | break; |
| 495 | | case 4: |
| 496 | | dword = sum; |
| 497 | | if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP) |
| 498 | | dword = FLIPENDIAN_INT32(dword); |
| 499 | | *((INT32 *) dest_ptr) = dword; |
| 500 | | break; |
| 501 | | } |
| 502 | | } |
| 503 | | return CASSETTE_ERROR_SUCCESS; |
| 504 | | } |
| 505 | | |
| 506 | | |
| 507 | | |
| 508 | | casserr_t cassette_put_samples(cassette_image *cassette, int channel, |
| 509 | | double time_index, double sample_period, size_t sample_count, size_t sample_bytes, |
| 510 | | const void *samples, int waveform_flags) |
| 511 | | { |
| 512 | | casserr_t err; |
| 513 | | struct manipulation_ranges ranges; |
| 514 | | size_t sample_index; |
| 515 | | INT32 *dest_ptr; |
| 516 | | INT32 dest_value; |
| 517 | | INT16 word; |
| 518 | | INT32 dword; |
| 519 | | const UINT8 *source_ptr; |
| 520 | | double d; |
| 521 | | |
| 522 | | if (!cassette) |
| 523 | | return CASSETTE_ERROR_SUCCESS; |
| 524 | | |
| 525 | | if (sample_period == 0) |
| 526 | | return CASSETTE_ERROR_SUCCESS; |
| 527 | | |
| 528 | | err = compute_manipulation_ranges(cassette, channel, time_index, sample_period, &ranges); |
| 529 | | if (err) |
| 530 | | return err; |
| 531 | | |
| 532 | | if (cassette->sample_count < ranges.sample_last+1) |
| 533 | | cassette->sample_count = ranges.sample_last + 1; |
| 534 | | cassette->flags |= CASSETTE_FLAG_DIRTY; |
| 535 | | |
| 536 | | if (LOG_PUT_SAMPLES) |
| 537 | | { |
| 538 | | logerror("cassette_put_samples(): Putting samples TIME=[%2.6g..%2.6g] INDEX=[%i..%i]\n", |
| 539 | | time_index, time_index + sample_period, |
| 540 | | (int)ranges.sample_first, (int)ranges.sample_last); |
| 541 | | } |
| 542 | | |
| 543 | | for (sample_index = ranges.sample_first; sample_index <= ranges.sample_last; sample_index++) |
| 544 | | { |
| 545 | | /* figure out the source pointer */ |
| 546 | | d = map_double(sample_count, ranges.sample_first, ranges.sample_last + 1, sample_index); |
| 547 | | source_ptr = (const UINT8*)samples; |
| 548 | | source_ptr += ((size_t) d) * sample_bytes; |
| 549 | | |
| 550 | | /* compute the value that we are writing */ |
| 551 | | switch(waveform_bytes_per_sample(waveform_flags)) { |
| 552 | | case 1: |
| 553 | | dest_value = extrapolate8(*((INT8 *) source_ptr)); |
| 554 | | break; |
| 555 | | case 2: |
| 556 | | word = *((INT16 *) source_ptr); |
| 557 | | if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP) |
| 558 | | word = FLIPENDIAN_INT16(word); |
| 559 | | dest_value = extrapolate16(word); |
| 560 | | break; |
| 561 | | case 4: |
| 562 | | dword = *((INT32 *) source_ptr); |
| 563 | | if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP) |
| 564 | | dword = FLIPENDIAN_INT32(dword); |
| 565 | | dest_value = dword; |
| 566 | | break; |
| 567 | | default: |
| 568 | | return CASSETTE_ERROR_INTERNAL; |
| 569 | | } |
| 570 | | |
| 571 | | for (channel = ranges.channel_first; channel <= ranges.channel_last; channel++) |
| 572 | | { |
| 573 | | /* find the sample that we are putting */ |
| 574 | | err = lookup_sample(cassette, channel, sample_index, TRUE, &dest_ptr); |
| 575 | | if (err) |
| 576 | | return err; |
| 577 | | *dest_ptr = dest_value; |
| 578 | | } |
| 579 | | } |
| 580 | | return CASSETTE_ERROR_SUCCESS; |
| 581 | | } |
| 582 | | |
| 583 | | |
| 584 | | |
| 585 | | casserr_t cassette_get_sample(cassette_image *cassette, int channel, |
| 586 | | double time_index, double sample_period, INT32 *sample) |
| 587 | | { |
| 588 | | return cassette_get_samples(cassette, channel, time_index, |
| 589 | | sample_period, 1, 0, sample, CASSETTE_WAVEFORM_32BIT); |
| 590 | | } |
| 591 | | |
| 592 | | |
| 593 | | |
| 594 | | casserr_t cassette_put_sample(cassette_image *cassette, int channel, |
| 595 | | double time_index, double sample_period, INT32 sample) |
| 596 | | { |
| 597 | | return cassette_put_samples(cassette, channel, time_index, |
| 598 | | sample_period, 1, 0, &sample, CASSETTE_WAVEFORM_32BIT); |
| 599 | | } |
| 600 | | |
| 601 | | |
| 602 | | |
| 603 | | /********************************************************************* |
| 604 | | waveform accesses to/from the raw image |
| 605 | | *********************************************************************/ |
| 606 | | |
| 607 | | casserr_t cassette_read_samples(cassette_image *cassette, int channels, double time_index, |
| 608 | | double sample_period, size_t sample_count, UINT64 offset, int waveform_flags) |
| 609 | | { |
| 610 | | casserr_t err; |
| 611 | | size_t chunk_sample_count; |
| 612 | | size_t bytes_per_sample; |
| 613 | | size_t sample_bytes; |
| 614 | | size_t samples_loaded = 0; |
| 615 | | double chunk_time_index; |
| 616 | | double chunk_sample_period; |
| 617 | | int channel; |
| 618 | | UINT8 buffer[8192]; |
| 619 | | |
| 620 | | bytes_per_sample = waveform_bytes_per_sample(waveform_flags); |
| 621 | | sample_bytes = bytes_per_sample * channels; |
| 622 | | |
| 623 | | while(samples_loaded < sample_count) |
| 624 | | { |
| 625 | | chunk_sample_count = MIN(sizeof(buffer) / sample_bytes, (sample_count - samples_loaded)); |
| 626 | | chunk_sample_period = map_double(sample_period, 0, sample_count, chunk_sample_count); |
| 627 | | chunk_time_index = time_index + map_double(sample_period, 0, sample_count, samples_loaded); |
| 628 | | |
| 629 | | cassette_image_read(cassette, buffer, offset, chunk_sample_count * sample_bytes); |
| 630 | | |
| 631 | | for (channel = 0; channel < channels; channel++) |
| 632 | | { |
| 633 | | err = cassette_put_samples(cassette, channel, chunk_time_index, chunk_sample_period, |
| 634 | | chunk_sample_count, sample_bytes, &buffer[channel * bytes_per_sample], waveform_flags); |
| 635 | | if (err) |
| 636 | | return err; |
| 637 | | } |
| 638 | | |
| 639 | | offset += chunk_sample_count * sample_bytes; |
| 640 | | samples_loaded += chunk_sample_count; |
| 641 | | } |
| 642 | | return CASSETTE_ERROR_SUCCESS; |
| 643 | | } |
| 644 | | |
| 645 | | |
| 646 | | |
| 647 | | casserr_t cassette_write_samples(cassette_image *cassette, int channels, double time_index, |
| 648 | | double sample_period, size_t sample_count, UINT64 offset, int waveform_flags) |
| 649 | | { |
| 650 | | casserr_t err; |
| 651 | | size_t chunk_sample_count; |
| 652 | | size_t bytes_per_sample; |
| 653 | | size_t sample_bytes; |
| 654 | | size_t samples_saved = 0; |
| 655 | | double chunk_time_index; |
| 656 | | double chunk_sample_period; |
| 657 | | int channel; |
| 658 | | UINT8 buffer[8192]; |
| 659 | | |
| 660 | | bytes_per_sample = waveform_bytes_per_sample(waveform_flags); |
| 661 | | sample_bytes = bytes_per_sample * channels; |
| 662 | | |
| 663 | | while(samples_saved < sample_count) |
| 664 | | { |
| 665 | | chunk_sample_count = MIN(sizeof(buffer) / sample_bytes, (sample_count - samples_saved)); |
| 666 | | chunk_sample_period = map_double(sample_period, 0, sample_count, chunk_sample_count); |
| 667 | | chunk_time_index = time_index + map_double(sample_period, 0, sample_count, samples_saved); |
| 668 | | |
| 669 | | for (channel = 0; channel < channels; channel++) |
| 670 | | { |
| 671 | | err = cassette_get_samples(cassette, channel, chunk_time_index, chunk_sample_period, |
| 672 | | chunk_sample_count, sample_bytes, &buffer[channel * bytes_per_sample], waveform_flags); |
| 673 | | if (err) |
| 674 | | return err; |
| 675 | | } |
| 676 | | |
| 677 | | cassette_image_write(cassette, buffer, offset, chunk_sample_count * sample_bytes); |
| 678 | | |
| 679 | | offset += chunk_sample_count * sample_bytes; |
| 680 | | samples_saved += chunk_sample_count; |
| 681 | | } |
| 682 | | return CASSETTE_ERROR_SUCCESS; |
| 683 | | } |
| 684 | | |
| 685 | | |
| 686 | | |
| 687 | | /********************************************************************* |
| 688 | | waveform accesses to/from the raw image |
| 689 | | *********************************************************************/ |
| 690 | | |
| 691 | | static const INT8 *choose_wave(const struct CassetteModulation *modulation, size_t *wave_bytes_length) |
| 692 | | { |
| 693 | | static const INT8 square_wave[] = { -128, 127 }; |
| 694 | | static const INT8 sine_wave[] = { 0, 48, 89, 117, 127, 117, 89, 48, 0, -48, -89, -117, -127, -117, -89, -48 }; |
| 695 | | |
| 696 | | if (modulation->flags & CASSETTE_MODULATION_SINEWAVE) |
| 697 | | { |
| 698 | | *wave_bytes_length = ARRAY_LENGTH(sine_wave); |
| 699 | | return sine_wave; |
| 700 | | } |
| 701 | | else |
| 702 | | { |
| 703 | | *wave_bytes_length = ARRAY_LENGTH(square_wave); |
| 704 | | return square_wave; |
| 705 | | } |
| 706 | | } |
| 707 | | |
| 708 | | |
| 709 | | |
| 710 | | casserr_t cassette_modulation_identify(cassette_image *cassette, const struct CassetteModulation *modulation, |
| 711 | | struct CassetteOptions *opts) |
| 712 | | { |
| 713 | | size_t wave_bytes_length; |
| 714 | | choose_wave(modulation, &wave_bytes_length); |
| 715 | | opts->bits_per_sample = 8; |
| 716 | | opts->channels = 1; |
| 717 | | opts->sample_frequency = (UINT32) (MAX(modulation->zero_frequency_high, modulation->one_frequency_high) * wave_bytes_length * 2); |
| 718 | | return CASSETTE_ERROR_SUCCESS; |
| 719 | | } |
| 720 | | |
| 721 | | |
| 722 | | |
| 723 | | casserr_t cassette_put_modulated_data(cassette_image *cassette, int channel, double time_index, |
| 724 | | const void *data, size_t data_length, const struct CassetteModulation *modulation, |
| 725 | | double *time_displacement) |
| 726 | | { |
| 727 | | casserr_t err; |
| 728 | | const UINT8 *data_bytes = (const UINT8 *)data; |
| 729 | | const INT8 *wave_bytes; |
| 730 | | size_t wave_bytes_length; |
| 731 | | double total_displacement = 0.0; |
| 732 | | double pulse_period; |
| 733 | | double pulse_frequency; |
| 734 | | UINT8 b; |
| 735 | | int i; |
| 736 | | |
| 737 | | wave_bytes = choose_wave(modulation, &wave_bytes_length); |
| 738 | | |
| 739 | | while(data_length--) |
| 740 | | { |
| 741 | | b = *(data_bytes++); |
| 742 | | for (i = 0; i < 8; i++) |
| 743 | | { |
| 744 | | pulse_frequency = (b & (1 << i)) ? modulation->one_frequency_cannonical : modulation->zero_frequency_cannonical; |
| 745 | | pulse_period = 1 / pulse_frequency; |
| 746 | | err = cassette_put_samples(cassette, 0, time_index, pulse_period, wave_bytes_length, 1, wave_bytes, CASSETTE_WAVEFORM_8BIT); |
| 747 | | if (err) |
| 748 | | goto done; |
| 749 | | time_index += pulse_period; |
| 750 | | total_displacement += pulse_period; |
| 751 | | } |
| 752 | | } |
| 753 | | err = CASSETTE_ERROR_SUCCESS; |
| 754 | | |
| 755 | | done: |
| 756 | | if (time_displacement) |
| 757 | | *time_displacement = total_displacement; |
| 758 | | return err; |
| 759 | | } |
| 760 | | |
| 761 | | |
| 762 | | |
| 763 | | casserr_t cassette_put_modulated_filler(cassette_image *cassette, int channel, double time_index, |
| 764 | | UINT8 filler, size_t filler_length, const struct CassetteModulation *modulation, |
| 765 | | double *time_displacement) |
| 766 | | { |
| 767 | | casserr_t err; |
| 768 | | double delta; |
| 769 | | double total_displacement = 0.0; |
| 770 | | |
| 771 | | while(filler_length--) |
| 772 | | { |
| 773 | | err = cassette_put_modulated_data(cassette, channel, time_index, &filler, 1, modulation, &delta); |
| 774 | | if (err) |
| 775 | | return err; |
| 776 | | total_displacement += delta; |
| 777 | | time_index += delta; |
| 778 | | } |
| 779 | | |
| 780 | | if (time_displacement) |
| 781 | | *time_displacement = total_displacement; |
| 782 | | return CASSETTE_ERROR_SUCCESS; |
| 783 | | } |
| 784 | | |
| 785 | | |
| 786 | | |
| 787 | | casserr_t cassette_read_modulated_data(cassette_image *cassette, int channel, double time_index, |
| 788 | | UINT64 offset, UINT64 length, const struct CassetteModulation *modulation, |
| 789 | | double *time_displacement) |
| 790 | | { |
| 791 | | casserr_t err; |
| 792 | | UINT8 buffer_stack[1024]; |
| 793 | | UINT8 *buffer; |
| 794 | | UINT8 *alloc_buffer = NULL; |
| 795 | | double delta; |
| 796 | | double total_displacement = 0.0; |
| 797 | | size_t this_length; |
| 798 | | size_t buffer_length; |
| 799 | | |
| 800 | | if (length <= sizeof(buffer_stack)) |
| 801 | | { |
| 802 | | buffer = buffer_stack; |
| 803 | | buffer_length = sizeof(buffer_stack); |
| 804 | | } |
| 805 | | else |
| 806 | | { |
| 807 | | buffer_length = MIN(length, 100000); |
| 808 | | alloc_buffer = (UINT8*)malloc(buffer_length); |
| 809 | | if (!alloc_buffer) |
| 810 | | { |
| 811 | | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 812 | | goto done; |
| 813 | | } |
| 814 | | buffer = alloc_buffer; |
| 815 | | } |
| 816 | | |
| 817 | | while(length > 0) |
| 818 | | { |
| 819 | | this_length = (size_t) MIN(length, buffer_length); |
| 820 | | cassette_image_read(cassette, buffer, offset, this_length); |
| 821 | | |
| 822 | | err = cassette_put_modulated_data(cassette, channel, time_index, buffer, this_length, modulation, &delta); |
| 823 | | if (err) |
| 824 | | goto done; |
| 825 | | total_displacement += delta; |
| 826 | | time_index += delta; |
| 827 | | length -= this_length; |
| 828 | | } |
| 829 | | |
| 830 | | if (time_displacement) |
| 831 | | *time_displacement = total_displacement; |
| 832 | | err = CASSETTE_ERROR_SUCCESS; |
| 833 | | |
| 834 | | done: |
| 835 | | if (alloc_buffer) |
| 836 | | free(alloc_buffer); |
| 837 | | return err; |
| 838 | | } |
| 839 | | |
| 840 | | |
| 841 | | |
| 842 | | casserr_t cassette_put_modulated_data_bit(cassette_image *cassette, int channel, double time_index, |
| 843 | | UINT8 data, const struct CassetteModulation *modulation, |
| 844 | | double *time_displacement) |
| 845 | | { |
| 846 | | casserr_t err; |
| 847 | | const INT8 *wave_bytes; |
| 848 | | size_t wave_bytes_length; |
| 849 | | double total_displacement = 0.0; |
| 850 | | double pulse_period; |
| 851 | | double pulse_frequency; |
| 852 | | |
| 853 | | wave_bytes = choose_wave(modulation, &wave_bytes_length); |
| 854 | | |
| 855 | | pulse_frequency = (data) ? modulation->one_frequency_cannonical : modulation->zero_frequency_cannonical; |
| 856 | | pulse_period = 1 / pulse_frequency; |
| 857 | | err = cassette_put_samples(cassette, 0, time_index, pulse_period, wave_bytes_length, 1, wave_bytes, CASSETTE_WAVEFORM_8BIT); |
| 858 | | if (err) |
| 859 | | goto done; |
| 860 | | time_index += pulse_period; |
| 861 | | total_displacement += pulse_period; |
| 862 | | |
| 863 | | err = CASSETTE_ERROR_SUCCESS; |
| 864 | | |
| 865 | | done: |
| 866 | | if (time_displacement) |
| 867 | | *time_displacement = total_displacement; |
| 868 | | return err; |
| 869 | | } |
| 870 | | |
| 871 | | |
| 872 | | |
| 873 | | /********************************************************************* |
| 874 | | waveform accesses to/from the raw image |
| 875 | | *********************************************************************/ |
| 876 | | |
| 877 | | casserr_t cassette_legacy_identify(cassette_image *cassette, struct CassetteOptions *opts, |
| 878 | | const struct CassetteLegacyWaveFiller *legacy_args) |
| 879 | | { |
| 880 | | opts->channels = 1; |
| 881 | | opts->bits_per_sample = 16; |
| 882 | | opts->sample_frequency = legacy_args->sample_frequency; |
| 883 | | return CASSETTE_ERROR_SUCCESS; |
| 884 | | } |
| 885 | | |
| 886 | | |
| 887 | | |
| 888 | | casserr_t cassette_legacy_construct(cassette_image *cassette, |
| 889 | | const struct CassetteLegacyWaveFiller *legacy_args) |
| 890 | | { |
| 891 | | casserr_t err; |
| 892 | | int length; |
| 893 | | int sample_count; |
| 894 | | void *bytes = NULL; |
| 895 | | void *chunk = NULL; |
| 896 | | INT16 *samples = NULL; |
| 897 | | int pos = 0; |
| 898 | | UINT64 offset = 0; |
| 899 | | UINT64 size; |
| 900 | | struct CassetteLegacyWaveFiller args; |
| 901 | | |
| 902 | | /* sanity check the args */ |
| 903 | | assert(legacy_args->header_samples >= -1); |
| 904 | | assert(legacy_args->trailer_samples >= 0); |
| 905 | | assert(legacy_args->fill_wave); |
| 906 | | |
| 907 | | size = cassette_image_size(cassette); |
| 908 | | |
| 909 | | /* normalize the args */ |
| 910 | | args = *legacy_args; |
| 911 | | if (args.chunk_size == 0) |
| 912 | | args.chunk_size = 1; |
| 913 | | else if (args.chunk_size < 0) |
| 914 | | args.chunk_size = cassette_image_size(cassette); |
| 915 | | if (args.sample_frequency == 0) |
| 916 | | args.sample_frequency = 11025; |
| 917 | | |
| 918 | | /* allocate a buffer for the binary data */ |
| 919 | | chunk = malloc(args.chunk_size); |
| 920 | | if (!chunk) |
| 921 | | { |
| 922 | | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 923 | | goto done; |
| 924 | | } |
| 925 | | |
| 926 | | /* determine number of samples */ |
| 927 | | if (args.chunk_sample_calc) |
| 928 | | { |
| 929 | | if (size > 0x7FFFFFFF) |
| 930 | | { |
| 931 | | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 932 | | goto done; |
| 933 | | } |
| 934 | | |
| 935 | | bytes = malloc(size); |
| 936 | | if (!bytes) |
| 937 | | { |
| 938 | | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 939 | | goto done; |
| 940 | | } |
| 941 | | cassette_image_read(cassette, bytes, 0, size); |
| 942 | | sample_count = args.chunk_sample_calc((const UINT8*)bytes, (int) size); |
| 943 | | |
| 944 | | if (args.header_samples < 0) |
| 945 | | args.header_samples = sample_count; |
| 946 | | } |
| 947 | | else |
| 948 | | { |
| 949 | | sample_count = ((size + args.chunk_size - 1) / args.chunk_size) |
| 950 | | * args.chunk_samples; |
| 951 | | } |
| 952 | | sample_count += args.header_samples + args.trailer_samples; |
| 953 | | |
| 954 | | /* allocate a buffer for the completed samples */ |
| 955 | | samples = (INT16*)malloc(sample_count * sizeof(*samples)); |
| 956 | | if (!samples) |
| 957 | | { |
| 958 | | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 959 | | goto done; |
| 960 | | } |
| 961 | | |
| 962 | | /* if there has to be a header */ |
| 963 | | if (args.header_samples > 0) |
| 964 | | { |
| 965 | | length = args.fill_wave(samples + pos, sample_count - pos, CODE_HEADER); |
| 966 | | if (length < 0) |
| 967 | | { |
| 968 | | err = CASSETTE_ERROR_INVALIDIMAGE; |
| 969 | | goto done; |
| 970 | | } |
| 971 | | pos += length; |
| 972 | | } |
| 973 | | |
| 974 | | /* convert the file data to samples */ |
| 975 | | while((pos < sample_count) && (offset < size)) |
| 976 | | { |
| 977 | | cassette_image_read(cassette, chunk, offset, args.chunk_size); |
| 978 | | offset += args.chunk_size; |
| 979 | | |
| 980 | | length = args.fill_wave(samples + pos, sample_count - pos, (UINT8*)chunk); |
| 981 | | if (length < 0) |
| 982 | | { |
| 983 | | err = CASSETTE_ERROR_INVALIDIMAGE; |
| 984 | | goto done; |
| 985 | | } |
| 986 | | pos += length; |
| 987 | | if (length == 0) |
| 988 | | break; |
| 989 | | } |
| 990 | | |
| 991 | | /* if there has to be a trailer */ |
| 992 | | if (args.trailer_samples > 0) |
| 993 | | { |
| 994 | | length = args.fill_wave(samples + pos, sample_count - pos, CODE_TRAILER); |
| 995 | | if (length < 0) |
| 996 | | { |
| 997 | | err = CASSETTE_ERROR_INVALIDIMAGE; |
| 998 | | goto done; |
| 999 | | } |
| 1000 | | pos += length; |
| 1001 | | } |
| 1002 | | |
| 1003 | | /* specify the wave */ |
| 1004 | | err = cassette_put_samples(cassette, 0, 0.0, ((double) pos) / args.sample_frequency, |
| 1005 | | pos, 2, samples, CASSETTE_WAVEFORM_16BIT); |
| 1006 | | if (err) |
| 1007 | | goto done; |
| 1008 | | |
| 1009 | | /* success! */ |
| 1010 | | err = CASSETTE_ERROR_SUCCESS; |
| 1011 | | |
| 1012 | | #if DUMP_CASSETTES |
| 1013 | | cassette_dump(cassette, "C:\\TEMP\\CASDUMP.WAV"); |
| 1014 | | #endif |
| 1015 | | |
| 1016 | | done: |
| 1017 | | if (samples) |
| 1018 | | free(samples); |
| 1019 | | if (chunk) |
| 1020 | | free(chunk); |
| 1021 | | if (bytes) |
| 1022 | | free(bytes); |
| 1023 | | return err; |
| 1024 | | } |
| 1025 | | |
| 1026 | | |
| 1027 | | |
| 1028 | | /********************************************************************* |
| 1029 | | cassette_dump |
| 1030 | | |
| 1031 | | A debugging call to dump a casette image to a disk based wave file |
| 1032 | | *********************************************************************/ |
| 1033 | | |
| 1034 | | void cassette_dump(cassette_image *image, const char *filename) |
| 1035 | | { |
| 1036 | | FILE *f; |
| 1037 | | struct io_generic saved_io; |
| 1038 | | const struct CassetteFormat *saved_format; |
| 1039 | | |
| 1040 | | f = fopen(filename, "wb"); |
| 1041 | | if (!f) |
| 1042 | | return; |
| 1043 | | |
| 1044 | | memcpy(&saved_io, &image->io, sizeof(saved_io)); |
| 1045 | | saved_format = image->format; |
| 1046 | | |
| 1047 | | image->io.file = f; |
| 1048 | | image->io.procs = &stdio_ioprocs_noclose; |
| 1049 | | image->format = &wavfile_format; |
| 1050 | | cassette_perform_save(image); |
| 1051 | | |
| 1052 | | memcpy(&image->io, &saved_io, sizeof(saved_io)); |
| 1053 | | image->format = saved_format; |
| 1054 | | |
| 1055 | | fclose(f); |
| 1056 | | } |
| 1057 | | |
| 1058 | | |
src/emu/imagedev/flopimg.c
| r11457 | r11458 | |
| 1 | | /********************************************************************* |
| 2 | | |
| 3 | | flopimg.c |
| 4 | | |
| 5 | | Floppy disk image abstraction code |
| 6 | | |
| 7 | | *********************************************************************/ |
| 8 | | |
| 9 | | #include <stdlib.h> |
| 10 | | #include <string.h> |
| 11 | | #include <stdio.h> |
| 12 | | #include <ctype.h> |
| 13 | | #include <assert.h> |
| 14 | | #include <limits.h> |
| 15 | | |
| 16 | | #include "osdepend.h" |
| 17 | | #include "flopimg.h" |
| 18 | | #include "pool.h" |
| 19 | | #include "imageutl.h" |
| 20 | | |
| 21 | | #define TRACK_LOADED 0x01 |
| 22 | | #define TRACK_DIRTY 0x02 |
| 23 | | |
| 24 | | |
| 25 | | struct _floppy_image |
| 26 | | { |
| 27 | | struct io_generic io; |
| 28 | | |
| 29 | | const struct FloppyFormat *floppy_option; |
| 30 | | struct FloppyCallbacks format; |
| 31 | | |
| 32 | | /* loaded track stuff */ |
| 33 | | int loaded_track_head; |
| 34 | | int loaded_track_index; |
| 35 | | UINT32 loaded_track_size; |
| 36 | | void *loaded_track_data; |
| 37 | | UINT8 loaded_track_status; |
| 38 | | UINT8 flags; |
| 39 | | |
| 40 | | /* tagging system */ |
| 41 | | object_pool *tags; |
| 42 | | void *tag_data; |
| 43 | | }; |
| 44 | | |
| 45 | | |
| 46 | | |
| 47 | | struct _floppy_params |
| 48 | | { |
| 49 | | int param; |
| 50 | | int value; |
| 51 | | }; |
| 52 | | |
| 53 | | |
| 54 | | |
| 55 | | static floperr_t floppy_track_unload(floppy_image *floppy); |
| 56 | | |
| 57 | | OPTION_GUIDE_START(floppy_option_guide) |
| 58 | | OPTION_INT('H', "heads", "Heads") |
| 59 | | OPTION_INT('T', "tracks", "Tracks") |
| 60 | | OPTION_INT('S', "sectors", "Sectors") |
| 61 | | OPTION_INT('L', "sectorlength", "Sector Bytes") |
| 62 | | OPTION_INT('I', "interleave", "Interleave") |
| 63 | | OPTION_INT('F', "firstsectorid", "First Sector") |
| 64 | | OPTION_GUIDE_END |
| 65 | | |
| 66 | | |
| 67 | | static void floppy_close_internal(floppy_image *floppy, int close_file); |
| 68 | | |
| 69 | | /********************************************************************* |
| 70 | | opening, closing and creating of floppy images |
| 71 | | *********************************************************************/ |
| 72 | | |
| 73 | | /* basic floppy_image initialization common to floppy_open() and floppy_create() */ |
| 74 | | static floppy_image *floppy_init(void *fp, const struct io_procs *procs, int flags) |
| 75 | | { |
| 76 | | floppy_image *floppy; |
| 77 | | |
| 78 | | floppy = (floppy_image *)malloc(sizeof(struct _floppy_image)); |
| 79 | | if (!floppy) |
| 80 | | return NULL; |
| 81 | | |
| 82 | | memset(floppy, 0, sizeof(*floppy)); |
| 83 | | floppy->tags = pool_alloc_lib(NULL); |
| 84 | | floppy->tag_data = NULL; |
| 85 | | floppy->io.file = fp; |
| 86 | | floppy->io.procs = procs; |
| 87 | | floppy->io.filler = 0xFF; |
| 88 | | floppy->flags = (UINT8) flags; |
| 89 | | return floppy; |
| 90 | | } |
| 91 | | |
| 92 | | |
| 93 | | |
| 94 | | /* main code for identifying and maybe opening a disk image; not exposed |
| 95 | | * directly because this function is big and hideous */ |
| 96 | | static floperr_t floppy_open_internal(void *fp, const struct io_procs *procs, const char *extension, |
| 97 | | const struct FloppyFormat *floppy_options, int max_options, int flags, floppy_image **outfloppy, |
| 98 | | int *outoption) |
| 99 | | { |
| 100 | | floperr_t err; |
| 101 | | floppy_image *floppy; |
| 102 | | int best_option = -1; |
| 103 | | int best_vote = 0; |
| 104 | | int vote; |
| 105 | | size_t i; |
| 106 | | |
| 107 | | floppy = floppy_init(fp, procs, flags); |
| 108 | | if (!floppy) |
| 109 | | { |
| 110 | | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 111 | | goto done; |
| 112 | | } |
| 113 | | |
| 114 | | /* vote on the best format */ |
| 115 | | for (i = 0; (i < max_options) && floppy_options[i].construct; i++) |
| 116 | | { |
| 117 | | if (!extension || !floppy_options[i].extensions || image_find_extension(floppy_options[i].extensions, extension)) |
| 118 | | { |
| 119 | | if (floppy_options[i].identify) |
| 120 | | { |
| 121 | | vote = 0; |
| 122 | | err = floppy_options[i].identify(floppy, &floppy_options[i], &vote); |
| 123 | | if (err) |
| 124 | | goto done; |
| 125 | | } |
| 126 | | else |
| 127 | | { |
| 128 | | vote = 1; |
| 129 | | } |
| 130 | | |
| 131 | | /* is this option a better one? */ |
| 132 | | if (vote > best_vote) |
| 133 | | { |
| 134 | | best_vote = vote; |
| 135 | | best_option = i; |
| 136 | | } |
| 137 | | } |
| 138 | | } |
| 139 | | |
| 140 | | /* did we find a format? */ |
| 141 | | if (best_option == -1) |
| 142 | | { |
| 143 | | err = FLOPPY_ERROR_INVALIDIMAGE; |
| 144 | | goto done; |
| 145 | | } |
| 146 | | |
| 147 | | if (outfloppy) |
| 148 | | { |
| 149 | | /* call the format constructor */ |
| 150 | | err = floppy_options[best_option].construct(floppy, &floppy_options[best_option], NULL); |
| 151 | | if (err) |
| 152 | | goto done; |
| 153 | | |
| 154 | | floppy->floppy_option = &floppy_options[best_option]; |
| 155 | | } |
| 156 | | |
| 157 | | err = FLOPPY_ERROR_SUCCESS; |
| 158 | | |
| 159 | | done: |
| 160 | | /* if we have a floppy disk and we either errored or are not keeping it, close it */ |
| 161 | | if (floppy && (!outfloppy || err)) |
| 162 | | { |
| 163 | | floppy_close_internal(floppy, FALSE); |
| 164 | | floppy = NULL; |
| 165 | | } |
| 166 | | |
| 167 | | if (outoption) |
| 168 | | *outoption = err ? -1 : best_option; |
| 169 | | if (outfloppy) |
| 170 | | *outfloppy = floppy; |
| 171 | | return err; |
| 172 | | } |
| 173 | | |
| 174 | | |
| 175 | | |
| 176 | | floperr_t floppy_identify(void *fp, const struct io_procs *procs, const char *extension, |
| 177 | | const struct FloppyFormat *formats, int *identified_format) |
| 178 | | { |
| 179 | | return floppy_open_internal(fp, procs, extension, formats, INT_MAX, FLOPPY_FLAGS_READONLY, NULL, identified_format); |
| 180 | | } |
| 181 | | |
| 182 | | |
| 183 | | |
| 184 | | floperr_t floppy_open(void *fp, const struct io_procs *procs, const char *extension, |
| 185 | | const struct FloppyFormat *format, int flags, floppy_image **outfloppy) |
| 186 | | { |
| 187 | | return floppy_open_internal(fp, procs, extension, format, 1, flags, outfloppy, NULL); |
| 188 | | } |
| 189 | | |
| 190 | | |
| 191 | | |
| 192 | | floperr_t floppy_open_choices(void *fp, const struct io_procs *procs, const char *extension, |
| 193 | | const struct FloppyFormat *formats, int flags, floppy_image **outfloppy) |
| 194 | | { |
| 195 | | return floppy_open_internal(fp, procs, extension, formats, INT_MAX, flags, outfloppy, NULL); |
| 196 | | } |
| 197 | | |
| 198 | | |
| 199 | | |
| 200 | | static floperr_t option_to_floppy_error(optreserr_t oerr) |
| 201 | | { |
| 202 | | floperr_t err; |
| 203 | | switch(oerr) { |
| 204 | | case OPTIONRESOLUTION_ERROR_SUCCESS: |
| 205 | | err = FLOPPY_ERROR_SUCCESS; |
| 206 | | break; |
| 207 | | case OPTIONRESOLUTION_ERROR_OUTOFMEMORY: |
| 208 | | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 209 | | break; |
| 210 | | case OPTIONRESOLUTION_ERROR_PARAMOUTOFRANGE: |
| 211 | | case OPTIONRESOLUTION_ERROR_PARAMNOTSPECIFIED: |
| 212 | | case OPTIONRESOLUTION_ERROR_PARAMNOTFOUND: |
| 213 | | case OPTIONRESOLUTION_ERROR_PARAMALREADYSPECIFIED: |
| 214 | | case OPTIONRESOLUTION_ERROR_BADPARAM: |
| 215 | | case OPTIONRESOLUTION_ERROR_SYNTAX: |
| 216 | | default: |
| 217 | | err = FLOPPY_ERROR_INTERNAL; |
| 218 | | break; |
| 219 | | }; |
| 220 | | return err; |
| 221 | | } |
| 222 | | |
| 223 | | |
| 224 | | |
| 225 | | floperr_t floppy_create(void *fp, const struct io_procs *procs, const struct FloppyFormat *format, option_resolution *parameters, floppy_image **outfloppy) |
| 226 | | { |
| 227 | | floppy_image *floppy = NULL; |
| 228 | | optreserr_t oerr; |
| 229 | | floperr_t err; |
| 230 | | int heads, tracks, h, t; |
| 231 | | option_resolution *alloc_resolution = NULL; |
| 232 | | |
| 233 | | assert(format); |
| 234 | | |
| 235 | | /* create the new image */ |
| 236 | | floppy = floppy_init(fp, procs, 0); |
| 237 | | if (!floppy) |
| 238 | | { |
| 239 | | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 240 | | goto done; |
| 241 | | } |
| 242 | | |
| 243 | | /* if this format expects creation parameters and none were specified, create some */ |
| 244 | | if (!parameters && format->param_guidelines) |
| 245 | | { |
| 246 | | alloc_resolution = option_resolution_create(floppy_option_guide, format->param_guidelines); |
| 247 | | if (!alloc_resolution) |
| 248 | | { |
| 249 | | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 250 | | goto done; |
| 251 | | } |
| 252 | | parameters = alloc_resolution; |
| 253 | | } |
| 254 | | |
| 255 | | /* finish the parameters, if specified */ |
| 256 | | if (parameters) |
| 257 | | { |
| 258 | | oerr = option_resolution_finish(parameters); |
| 259 | | if (oerr) |
| 260 | | { |
| 261 | | err = option_to_floppy_error(oerr); |
| 262 | | goto done; |
| 263 | | } |
| 264 | | } |
| 265 | | |
| 266 | | /* call the format constructor */ |
| 267 | | err = format->construct(floppy, format, parameters); |
| 268 | | if (err) |
| 269 | | goto done; |
| 270 | | |
| 271 | | /* format the disk, ignoring if formatting not implemented */ |
| 272 | | if (floppy->format.format_track) |
| 273 | | { |
| 274 | | heads = floppy_get_heads_per_disk(floppy); |
| 275 | | tracks = floppy_get_tracks_per_disk(floppy); |
| 276 | | |
| 277 | | for (h = 0; h < heads; h++) |
| 278 | | { |
| 279 | | for (t = 0; t < tracks; t++) |
| 280 | | { |
| 281 | | err = floppy->format.format_track(floppy, h, t, parameters); |
| 282 | | if (err) |
| 283 | | goto done; |
| 284 | | } |
| 285 | | } |
| 286 | | } |
| 287 | | |
| 288 | | /* call the post_format function, if present */ |
| 289 | | if (floppy->format.post_format) |
| 290 | | { |
| 291 | | err = floppy->format.post_format(floppy, parameters); |
| 292 | | if (err) |
| 293 | | goto done; |
| 294 | | } |
| 295 | | |
| 296 | | floppy->floppy_option = format; |
| 297 | | err = FLOPPY_ERROR_SUCCESS; |
| 298 | | |
| 299 | | done: |
| 300 | | if (err && floppy) |
| 301 | | { |
| 302 | | floppy_close_internal(floppy, FALSE); |
| 303 | | floppy = NULL; |
| 304 | | } |
| 305 | | |
| 306 | | if (outfloppy) |
| 307 | | *outfloppy = floppy; |
| 308 | | else if (floppy) |
| 309 | | floppy_close_internal(floppy, FALSE); |
| 310 | | |
| 311 | | if (alloc_resolution) |
| 312 | | option_resolution_close(alloc_resolution); |
| 313 | | return err; |
| 314 | | } |
| 315 | | |
| 316 | | |
| 317 | | |
| 318 | | static void floppy_close_internal(floppy_image *floppy, int close_file) |
| 319 | | { |
| 320 | | if (floppy) { |
| 321 | | floppy_track_unload(floppy); |
| 322 | | |
| 323 | | if(floppy->floppy_option->destruct) |
| 324 | | floppy->floppy_option->destruct(floppy, floppy->floppy_option); |
| 325 | | if (close_file) |
| 326 | | io_generic_close(&floppy->io); |
| 327 | | if (floppy->loaded_track_data) |
| 328 | | free(floppy->loaded_track_data); |
| 329 | | pool_free_lib(floppy->tags); |
| 330 | | |
| 331 | | free(floppy); |
| 332 | | } |
| 333 | | } |
| 334 | | |
| 335 | | |
| 336 | | |
| 337 | | void floppy_close(floppy_image *floppy) |
| 338 | | { |
| 339 | | floppy_close_internal(floppy, TRUE); |
| 340 | | } |
| 341 | | |
| 342 | | |
| 343 | | |
| 344 | | /********************************************************************* |
| 345 | | functions useful in format constructors |
| 346 | | *********************************************************************/ |
| 347 | | |
| 348 | | struct FloppyCallbacks *floppy_callbacks(floppy_image *floppy) |
| 349 | | { |
| 350 | | assert(floppy); |
| 351 | | return &floppy->format; |
| 352 | | } |
| 353 | | |
| 354 | | |
| 355 | | |
| 356 | | void *floppy_tag(floppy_image *floppy) |
| 357 | | { |
| 358 | | assert(floppy); |
| 359 | | return floppy->tag_data; |
| 360 | | } |
| 361 | | |
| 362 | | |
| 363 | | |
| 364 | | void *floppy_create_tag(floppy_image *floppy, size_t tagsize) |
| 365 | | { |
| 366 | | floppy->tag_data = pool_malloc_lib(floppy->tags,tagsize); |
| 367 | | return floppy->tag_data; |
| 368 | | } |
| 369 | | |
| 370 | | |
| 371 | | |
| 372 | | UINT8 floppy_get_filler(floppy_image *floppy) |
| 373 | | { |
| 374 | | return floppy->io.filler; |
| 375 | | } |
| 376 | | |
| 377 | | |
| 378 | | |
| 379 | | void floppy_set_filler(floppy_image *floppy, UINT8 filler) |
| 380 | | { |
| 381 | | floppy->io.filler = filler; |
| 382 | | } |
| 383 | | |
| 384 | | |
| 385 | | |
| 386 | | /********************************************************************* |
| 387 | | calls for accessing the raw disk image |
| 388 | | *********************************************************************/ |
| 389 | | |
| 390 | | void floppy_image_read(floppy_image *floppy, void *buffer, UINT64 offset, size_t length) |
| 391 | | { |
| 392 | | io_generic_read(&floppy->io, buffer, offset, length); |
| 393 | | } |
| 394 | | |
| 395 | | |
| 396 | | |
| 397 | | void floppy_image_write(floppy_image *floppy, const void *buffer, UINT64 offset, size_t length) |
| 398 | | { |
| 399 | | io_generic_write(&floppy->io, buffer, offset, length); |
| 400 | | } |
| 401 | | |
| 402 | | |
| 403 | | |
| 404 | | void floppy_image_write_filler(floppy_image *floppy, UINT8 filler, UINT64 offset, size_t length) |
| 405 | | { |
| 406 | | io_generic_write_filler(&floppy->io, filler, offset, length); |
| 407 | | } |
| 408 | | |
| 409 | | |
| 410 | | |
| 411 | | UINT64 floppy_image_size(floppy_image *floppy) |
| 412 | | { |
| 413 | | return io_generic_size(&floppy->io); |
| 414 | | } |
| 415 | | |
| 416 | | |
| 417 | | |
| 418 | | /********************************************************************* |
| 419 | | calls for accessing disk image data |
| 420 | | *********************************************************************/ |
| 421 | | |
| 422 | | static floperr_t floppy_readwrite_sector(floppy_image *floppy, int head, int track, int sector, int offset, |
| 423 | | void *buffer, size_t buffer_len, int writing, int indexed, int ddam) |
| 424 | | { |
| 425 | | floperr_t err; |
| 426 | | const struct FloppyCallbacks *fmt; |
| 427 | | size_t this_buffer_len; |
| 428 | | UINT8 *alloc_buf = NULL; |
| 429 | | UINT32 sector_length; |
| 430 | | UINT8 *buffer_ptr = (UINT8 *)buffer; |
| 431 | | floperr_t (*read_sector)(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen); |
| 432 | | floperr_t (*write_sector)(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam); |
| 433 | | |
| 434 | | fmt = floppy_callbacks(floppy); |
| 435 | | |
| 436 | | /* choose proper calls for indexed vs non-indexed */ |
| 437 | | if (indexed) |
| 438 | | { |
| 439 | | read_sector = fmt->read_indexed_sector; |
| 440 | | write_sector = fmt->write_indexed_sector; |
| 441 | | if (!fmt->get_indexed_sector_info) |
| 442 | | { |
| 443 | | err = FLOPPY_ERROR_UNSUPPORTED; |
| 444 | | goto done; |
| 445 | | } |
| 446 | | } |
| 447 | | else |
| 448 | | { |
| 449 | | read_sector = fmt->read_sector; |
| 450 | | write_sector = fmt->write_sector; |
| 451 | | if (!fmt->get_sector_length) |
| 452 | | { |
| 453 | | err = FLOPPY_ERROR_UNSUPPORTED; |
| 454 | | goto done; |
| 455 | | } |
| 456 | | } |
| 457 | | |
| 458 | | /* check to make sure that the operation is supported */ |
| 459 | | if (!read_sector || (writing && !write_sector)) |
| 460 | | { |
| 461 | | err = FLOPPY_ERROR_UNSUPPORTED; |
| 462 | | goto done; |
| 463 | | } |
| 464 | | |
| 465 | | /* main loop */ |
| 466 | | while(buffer_len > 0) |
| 467 | | { |
| 468 | | /* find out the size of this sector */ |
| 469 | | if (indexed) |
| 470 | | err = fmt->get_indexed_sector_info(floppy, head, track, sector, NULL, NULL, NULL, §or_length, NULL); |
| 471 | | else |
| 472 | | err = fmt->get_sector_length(floppy, head, track, sector, §or_length); |
| 473 | | if (err) |
| 474 | | goto done; |
| 475 | | |
| 476 | | /* do we even do anything with this sector? */ |
| 477 | | if (offset < sector_length) |
| 478 | | { |
| 479 | | /* ok we will be doing something */ |
| 480 | | if ((offset > 0) || (buffer_len < sector_length)) |
| 481 | | { |
| 482 | | /* we will be doing an partial read/write; in other words we |
| 483 | | * will not be reading/writing a full sector */ |
| 484 | | if (alloc_buf) free(alloc_buf); |
| 485 | | alloc_buf = (UINT8*)malloc(sector_length); |
| 486 | | if (!alloc_buf) |
| 487 | | { |
| 488 | | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 489 | | goto done; |
| 490 | | } |
| 491 | | |
| 492 | | /* read the sector (we need to do this even when writing */ |
| 493 | | err = read_sector(floppy, head, track, sector, alloc_buf, sector_length); |
| 494 | | if (err) |
| 495 | | goto done; |
| 496 | | |
| 497 | | this_buffer_len = MIN(buffer_len, sector_length - offset); |
| 498 | | |
| 499 | | if (writing) |
| 500 | | { |
| 501 | | memcpy(alloc_buf + offset, buffer_ptr, this_buffer_len); |
| 502 | | |
| 503 | | err = write_sector(floppy, head, track, sector, alloc_buf, sector_length, ddam); |
| 504 | | if (err) |
| 505 | | goto done; |
| 506 | | } |
| 507 | | else |
| 508 | | { |
| 509 | | memcpy(buffer_ptr, alloc_buf + offset, this_buffer_len); |
| 510 | | } |
| 511 | | offset += this_buffer_len; |
| 512 | | offset %= sector_length; |
| 513 | | } |
| 514 | | else |
| 515 | | { |
| 516 | | this_buffer_len = sector_length; |
| 517 | | |
| 518 | | if (writing) |
| 519 | | err = write_sector(floppy, head, track, sector, buffer_ptr, sector_length, ddam); |
| 520 | | else |
| 521 | | err = read_sector(floppy, head, track, sector, buffer_ptr, sector_length); |
| 522 | | if (err) |
| 523 | | goto done; |
| 524 | | } |
| 525 | | } |
| 526 | | else |
| 527 | | { |
| 528 | | /* skip this sector */ |
| 529 | | offset -= sector_length; |
| 530 | | this_buffer_len = 0; |
| 531 | | } |
| 532 | | |
| 533 | | buffer_ptr += this_buffer_len; |
| 534 | | buffer_len -= this_buffer_len; |
| 535 | | sector++; |
| 536 | | } |
| 537 | | |
| 538 | | err = FLOPPY_ERROR_SUCCESS; |
| 539 | | |
| 540 | | done: |
| 541 | | if (alloc_buf) |
| 542 | | free(alloc_buf); |
| 543 | | return err; |
| 544 | | } |
| 545 | | |
| 546 | | |
| 547 | | |
| 548 | | floperr_t floppy_read_sector(floppy_image *floppy, int head, int track, int sector, int offset, void *buffer, size_t buffer_len) |
| 549 | | { |
| 550 | | return floppy_readwrite_sector(floppy, head, track, sector, offset, buffer, buffer_len, FALSE, FALSE, 0); |
| 551 | | } |
| 552 | | |
| 553 | | |
| 554 | | |
| 555 | | floperr_t floppy_write_sector(floppy_image *floppy, int head, int track, int sector, int offset, const void *buffer, size_t buffer_len, int ddam) |
| 556 | | { |
| 557 | | return floppy_readwrite_sector(floppy, head, track, sector, offset, (void *) buffer, buffer_len, TRUE, FALSE, ddam); |
| 558 | | } |
| 559 | | |
| 560 | | |
| 561 | | |
| 562 | | floperr_t floppy_read_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, void *buffer, size_t buffer_len) |
| 563 | | { |
| 564 | | return floppy_readwrite_sector(floppy, head, track, sector_index, offset, buffer, buffer_len, FALSE, TRUE, 0); |
| 565 | | } |
| 566 | | |
| 567 | | |
| 568 | | |
| 569 | | floperr_t floppy_write_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, const void *buffer, size_t buffer_len, int ddam) |
| 570 | | { |
| 571 | | return floppy_readwrite_sector(floppy, head, track, sector_index, offset, (void *) buffer, buffer_len, TRUE, TRUE, ddam); |
| 572 | | } |
| 573 | | |
| 574 | | |
| 575 | | static floperr_t floppy_get_track_data_offset(floppy_image *floppy, int head, int track, UINT64 *offset) |
| 576 | | { |
| 577 | | floperr_t err; |
| 578 | | const struct FloppyCallbacks *callbacks; |
| 579 | | |
| 580 | | *offset = 0; |
| 581 | | callbacks = floppy_callbacks(floppy); |
| 582 | | if (callbacks->get_track_data_offset) |
| 583 | | { |
| 584 | | err = callbacks->get_track_data_offset(floppy, head, track, offset); |
| 585 | | if (err) |
| 586 | | return err; |
| 587 | | } |
| 588 | | return FLOPPY_ERROR_SUCCESS; |
| 589 | | } |
| 590 | | |
| 591 | | |
| 592 | | |
| 593 | | static floperr_t floppy_read_track_offset(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buffer_len) |
| 594 | | { |
| 595 | | floperr_t err; |
| 596 | | const struct FloppyCallbacks *format; |
| 597 | | |
| 598 | | format = floppy_callbacks(floppy); |
| 599 | | |
| 600 | | if (!format->read_track) |
| 601 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 602 | | |
| 603 | | err = floppy_track_unload(floppy); |
| 604 | | if (err) |
| 605 | | return err; |
| 606 | | |
| 607 | | err = format->read_track(floppy, head, track, offset, buffer, buffer_len); |
| 608 | | if (err) |
| 609 | | return err; |
| 610 | | |
| 611 | | return FLOPPY_ERROR_SUCCESS; |
| 612 | | } |
| 613 | | |
| 614 | | |
| 615 | | |
| 616 | | floperr_t floppy_read_track(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len) |
| 617 | | { |
| 618 | | return floppy_read_track_offset(floppy, head, track, 0, buffer, buffer_len); |
| 619 | | } |
| 620 | | |
| 621 | | |
| 622 | | |
| 623 | | floperr_t floppy_read_track_data(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len) |
| 624 | | { |
| 625 | | floperr_t err; |
| 626 | | UINT64 offset; |
| 627 | | |
| 628 | | err = floppy_get_track_data_offset(floppy, head, track, &offset); |
| 629 | | if (err) |
| 630 | | return err; |
| 631 | | |
| 632 | | return floppy_read_track_offset(floppy, head, track, offset, buffer, buffer_len); |
| 633 | | } |
| 634 | | |
| 635 | | |
| 636 | | |
| 637 | | static floperr_t floppy_write_track_offset(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buffer_len) |
| 638 | | { |
| 639 | | floperr_t err; |
| 640 | | |
| 641 | | /* track writing supported? */ |
| 642 | | if (!floppy_callbacks(floppy)->write_track) |
| 643 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 644 | | |
| 645 | | /* read only? */ |
| 646 | | if (floppy->flags & FLOPPY_FLAGS_READONLY) |
| 647 | | return FLOPPY_ERROR_READONLY; |
| 648 | | |
| 649 | | err = floppy_track_unload(floppy); |
| 650 | | if (err) |
| 651 | | return err; |
| 652 | | |
| 653 | | err = floppy_callbacks(floppy)->write_track(floppy, head, track, offset, buffer, buffer_len); |
| 654 | | if (err) |
| 655 | | return err; |
| 656 | | |
| 657 | | return FLOPPY_ERROR_SUCCESS; |
| 658 | | } |
| 659 | | |
| 660 | | |
| 661 | | |
| 662 | | floperr_t floppy_write_track(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len) |
| 663 | | { |
| 664 | | return floppy_write_track_offset(floppy, head, track, 0, buffer, buffer_len); |
| 665 | | } |
| 666 | | |
| 667 | | |
| 668 | | |
| 669 | | floperr_t floppy_write_track_data(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len) |
| 670 | | { |
| 671 | | floperr_t err; |
| 672 | | UINT64 offset; |
| 673 | | |
| 674 | | err = floppy_get_track_data_offset(floppy, head, track, &offset); |
| 675 | | if (err) |
| 676 | | return err; |
| 677 | | |
| 678 | | return floppy_write_track_offset(floppy, head, track, offset, buffer, buffer_len); |
| 679 | | } |
| 680 | | |
| 681 | | |
| 682 | | |
| 683 | | floperr_t floppy_format_track(floppy_image *floppy, int head, int track, option_resolution *parameters) |
| 684 | | { |
| 685 | | floperr_t err; |
| 686 | | struct FloppyCallbacks *format; |
| 687 | | option_resolution *alloc_resolution = NULL; |
| 688 | | optreserr_t oerr; |
| 689 | | |
| 690 | | /* supported? */ |
| 691 | | format = floppy_callbacks(floppy); |
| 692 | | if (!format->format_track) |
| 693 | | { |
| 694 | | err = FLOPPY_ERROR_UNSUPPORTED; |
| 695 | | goto done; |
| 696 | | } |
| 697 | | |
| 698 | | /* create a dummy resolution; if no parameters were specified */ |
| 699 | | if (!parameters) |
| 700 | | { |
| 701 | | alloc_resolution = option_resolution_create(floppy_option_guide, floppy->floppy_option->param_guidelines); |
| 702 | | if (!alloc_resolution) |
| 703 | | { |
| 704 | | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 705 | | goto done; |
| 706 | | } |
| 707 | | parameters = alloc_resolution; |
| 708 | | } |
| 709 | | |
| 710 | | oerr = option_resolution_finish(parameters); |
| 711 | | if (oerr) |
| 712 | | { |
| 713 | | err = option_to_floppy_error(oerr); |
| 714 | | goto done; |
| 715 | | } |
| 716 | | |
| 717 | | err = format->format_track(floppy, head, track, parameters); |
| 718 | | if (err) |
| 719 | | goto done; |
| 720 | | |
| 721 | | done: |
| 722 | | if (alloc_resolution) |
| 723 | | option_resolution_close(alloc_resolution); |
| 724 | | return err; |
| 725 | | } |
| 726 | | |
| 727 | | |
| 728 | | |
| 729 | | int floppy_get_tracks_per_disk(floppy_image *floppy) |
| 730 | | { |
| 731 | | return floppy_callbacks(floppy)->get_tracks_per_disk(floppy); |
| 732 | | } |
| 733 | | |
| 734 | | |
| 735 | | |
| 736 | | int floppy_get_heads_per_disk(floppy_image *floppy) |
| 737 | | { |
| 738 | | return floppy_callbacks(floppy)->get_heads_per_disk(floppy); |
| 739 | | } |
| 740 | | |
| 741 | | |
| 742 | | |
| 743 | | UINT32 floppy_get_track_size(floppy_image *floppy, int head, int track) |
| 744 | | { |
| 745 | | const struct FloppyCallbacks *fmt; |
| 746 | | |
| 747 | | fmt = floppy_callbacks(floppy); |
| 748 | | if (!fmt->get_track_size) |
| 749 | | return 0; |
| 750 | | |
| 751 | | return fmt->get_track_size(floppy, head, track); |
| 752 | | } |
| 753 | | |
| 754 | | |
| 755 | | |
| 756 | | floperr_t floppy_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 757 | | { |
| 758 | | const struct FloppyCallbacks *fmt; |
| 759 | | |
| 760 | | fmt = floppy_callbacks(floppy); |
| 761 | | if (!fmt->get_sector_length) |
| 762 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 763 | | |
| 764 | | return fmt->get_sector_length(floppy, head, track, sector, sector_length); |
| 765 | | } |
| 766 | | |
| 767 | | |
| 768 | | |
| 769 | | floperr_t floppy_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 770 | | { |
| 771 | | const struct FloppyCallbacks *fmt; |
| 772 | | |
| 773 | | fmt = floppy_callbacks(floppy); |
| 774 | | if (!fmt->get_indexed_sector_info) |
| 775 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 776 | | |
| 777 | | return fmt->get_indexed_sector_info(floppy, head, track, sector_index, cylinder, side, sector, sector_length, flags); |
| 778 | | } |
| 779 | | |
| 780 | | |
| 781 | | |
| 782 | | floperr_t floppy_get_sector_count(floppy_image *floppy, int head, int track, int *sector_count) |
| 783 | | { |
| 784 | | floperr_t err; |
| 785 | | int sector_index = 0; |
| 786 | | |
| 787 | | do |
| 788 | | { |
| 789 | | err = floppy_get_indexed_sector_info(floppy, head, track, sector_index, NULL, NULL, NULL, NULL, NULL); |
| 790 | | if (!err) |
| 791 | | sector_index++; |
| 792 | | } |
| 793 | | while(!err); |
| 794 | | |
| 795 | | if (sector_index && (err == FLOPPY_ERROR_SEEKERROR)) |
| 796 | | err = FLOPPY_ERROR_SUCCESS; |
| 797 | | if (sector_count) |
| 798 | | *sector_count = err ? 0 : sector_index; |
| 799 | | return err; |
| 800 | | } |
| 801 | | |
| 802 | | |
| 803 | | |
| 804 | | int floppy_is_read_only(floppy_image *floppy) |
| 805 | | { |
| 806 | | return floppy->flags & FLOPPY_FLAGS_READONLY; |
| 807 | | } |
| 808 | | |
| 809 | | |
| 810 | | |
| 811 | | UINT8 floppy_random_byte(floppy_image *floppy) |
| 812 | | { |
| 813 | | /* can't use mame_rand(); this might not be in the core */ |
| 814 | | #ifdef rand |
| 815 | | #undef rand |
| 816 | | #endif |
| 817 | | return rand(); |
| 818 | | } |
| 819 | | |
| 820 | | |
| 821 | | |
| 822 | | /********************************************************************* |
| 823 | | calls for track based IO |
| 824 | | *********************************************************************/ |
| 825 | | |
| 826 | | floperr_t floppy_load_track(floppy_image *floppy, int head, int track, int dirtify, void **track_data, size_t *track_length) |
| 827 | | { |
| 828 | | floperr_t err; |
| 829 | | void *new_loaded_track_data; |
| 830 | | UINT32 track_size; |
| 831 | | |
| 832 | | /* have we already loaded this track? */ |
| 833 | | if (((floppy->loaded_track_status & TRACK_LOADED) == 0) || (head != floppy->loaded_track_head) || (track != floppy->loaded_track_index)) |
| 834 | | { |
| 835 | | err = floppy_track_unload(floppy); |
| 836 | | if (err) |
| 837 | | goto error; |
| 838 | | |
| 839 | | track_size = floppy_callbacks(floppy)->get_track_size(floppy, head, track); |
| 840 | | |
| 841 | | if (floppy->loaded_track_data) free(floppy->loaded_track_data); |
| 842 | | new_loaded_track_data = malloc(track_size); |
| 843 | | if (!new_loaded_track_data) |
| 844 | | { |
| 845 | | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 846 | | goto error; |
| 847 | | } |
| 848 | | |
| 849 | | floppy->loaded_track_data = new_loaded_track_data; |
| 850 | | floppy->loaded_track_size = track_size; |
| 851 | | floppy->loaded_track_head = head; |
| 852 | | floppy->loaded_track_index = track; |
| 853 | | |
| 854 | | err = floppy_callbacks(floppy)->read_track(floppy, floppy->loaded_track_head, floppy->loaded_track_index, 0, floppy->loaded_track_data, floppy->loaded_track_size); |
| 855 | | if (err) |
| 856 | | goto error; |
| 857 | | |
| 858 | | floppy->loaded_track_status |= TRACK_LOADED | (dirtify ? TRACK_DIRTY : 0); |
| 859 | | } |
| 860 | | else |
| 861 | | floppy->loaded_track_status |= (dirtify ? TRACK_DIRTY : 0); |
| 862 | | |
| 863 | | if (track_data) |
| 864 | | *track_data = floppy->loaded_track_data; |
| 865 | | if (track_length) |
| 866 | | *track_length = floppy->loaded_track_size; |
| 867 | | return FLOPPY_ERROR_SUCCESS; |
| 868 | | |
| 869 | | error: |
| 870 | | if (track_data) |
| 871 | | *track_data = NULL; |
| 872 | | if (track_length) |
| 873 | | *track_length = 0; |
| 874 | | return err; |
| 875 | | } |
| 876 | | |
| 877 | | |
| 878 | | |
| 879 | | static floperr_t floppy_track_unload(floppy_image *floppy) |
| 880 | | { |
| 881 | | int err; |
| 882 | | if (floppy->loaded_track_status & TRACK_DIRTY) |
| 883 | | { |
| 884 | | err = floppy_callbacks(floppy)->write_track(floppy, floppy->loaded_track_head, floppy->loaded_track_index, 0, floppy->loaded_track_data, floppy->loaded_track_size); |
| 885 | | if (err) |
| 886 | | return (floperr_t)err; |
| 887 | | } |
| 888 | | |
| 889 | | floppy->loaded_track_status &= ~(TRACK_LOADED | TRACK_DIRTY); |
| 890 | | return FLOPPY_ERROR_SUCCESS; |
| 891 | | } |
| 892 | | |
| 893 | | |
| 894 | | |
| 895 | | /********************************************************************* |
| 896 | | accessors for meta information about the image |
| 897 | | *********************************************************************/ |
| 898 | | |
| 899 | | const char *floppy_format_description(floppy_image *floppy) |
| 900 | | { |
| 901 | | return floppy->floppy_option->description; |
| 902 | | } |
| 903 | | |
| 904 | | |
| 905 | | |
| 906 | | /********************************************************************* |
| 907 | | misc calls |
| 908 | | *********************************************************************/ |
| 909 | | |
| 910 | | const char *floppy_error(floperr_t err) |
| 911 | | { |
| 912 | | static const char *const error_messages[] = |
| 913 | | { |
| 914 | | "The operation completed successfully", |
| 915 | | "Fatal internal error", |
| 916 | | "This operation is unsupported", |
| 917 | | "Out of memory", |
| 918 | | "Seek error", |
| 919 | | "Invalid image", |
| 920 | | "Attempted to write to read only image", |
| 921 | | "No space left on image", |
| 922 | | "Parameter out of range", |
| 923 | | "Required parameter not specified" |
| 924 | | }; |
| 925 | | |
| 926 | | if ((err < 0) || (err >= ARRAY_LENGTH(error_messages))) |
| 927 | | return NULL; |
| 928 | | return error_messages[err]; |
| 929 | | } |
| 930 | | |
| 931 | | |
| 932 | | FLOPPY_OPTIONS_START(default) |
| 933 | | FLOPPY_OPTIONS_END |
src/lib/formats/imageutl.c
| r0 | r11458 | |
| 1 | /*************************************************************************** |
| 2 | |
| 3 | imageutl.h |
| 4 | |
| 5 | Image related utilities |
| 6 | |
| 7 | **************************************************************************** |
| 8 | |
| 9 | Copyright Aaron Giles |
| 10 | All rights reserved. |
| 11 | |
| 12 | Redistribution and use in source and binary forms, with or without |
| 13 | modification, are permitted provided that the following conditions are |
| 14 | met: |
| 15 | |
| 16 | * Redistributions of source code must retain the above copyright |
| 17 | notice, this list of conditions and the following disclaimer. |
| 18 | * Redistributions in binary form must reproduce the above copyright |
| 19 | notice, this list of conditions and the following disclaimer in |
| 20 | the documentation and/or other materials provided with the |
| 21 | distribution. |
| 22 | * Neither the name 'MAME' nor the names of its contributors may be |
| 23 | used to endorse or promote products derived from this software |
| 24 | without specific prior written permission. |
| 25 | |
| 26 | THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR |
| 27 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 28 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 29 | DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT, |
| 30 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 31 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 32 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 33 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 34 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 35 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 36 | POSSIBILITY OF SUCH DAMAGE. |
| 37 | |
| 38 | ***************************************************************************/ |
| 39 | |
| 40 | #include <ctype.h> |
| 41 | #include "imageutl.h" |
| 42 | |
| 43 | int compute_log2(int val) |
| 44 | { |
| 45 | int count = 0; |
| 46 | |
| 47 | while(val > 1) |
| 48 | { |
| 49 | if (val & 1) |
| 50 | return -1; |
| 51 | val /= 2; |
| 52 | count++; |
| 53 | } |
| 54 | return (val == 0) ? -1 : count; |
| 55 | } |
| 56 | |
| 57 | /* |
| 58 | Compute CCITT CRC-16 using the correct bit order for floppy disks. |
| 59 | CRC code courtesy of Tim Mann. |
| 60 | */ |
| 61 | |
| 62 | /* Accelerator table to compute the CRC eight bits at a time */ |
| 63 | static const unsigned short ccitt_crc16_table[256] = |
| 64 | { |
| 65 | 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, |
| 66 | 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, |
| 67 | 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, |
| 68 | 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, |
| 69 | 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, |
| 70 | 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, |
| 71 | 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, |
| 72 | 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, |
| 73 | 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, |
| 74 | 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, |
| 75 | 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, |
| 76 | 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, |
| 77 | 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, |
| 78 | 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, |
| 79 | 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, |
| 80 | 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, |
| 81 | 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, |
| 82 | 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, |
| 83 | 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, |
| 84 | 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, |
| 85 | 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, |
| 86 | 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, |
| 87 | 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, |
| 88 | 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, |
| 89 | 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, |
| 90 | 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, |
| 91 | 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, |
| 92 | 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, |
| 93 | 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, |
| 94 | 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, |
| 95 | 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, |
| 96 | 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 |
| 97 | }; |
| 98 | |
| 99 | unsigned short ccitt_crc16(unsigned short crc, const unsigned char *buffer, size_t buffer_len) |
| 100 | { |
| 101 | size_t i; |
| 102 | for (i = 0; i < buffer_len; i++) |
| 103 | crc = (crc << 8) ^ ccitt_crc16_table[(crc >> 8) ^ buffer[i]]; |
| 104 | return crc; |
| 105 | } |
| 106 | |
| 107 | unsigned short ccitt_crc16_one( unsigned short crc, const unsigned char data ) |
| 108 | { |
| 109 | return (crc << 8) ^ ccitt_crc16_table[(crc >> 8) ^ data]; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | /*------------------------------------------------- |
| 114 | is_delim - returns whether c is a comma or \0 |
| 115 | -------------------------------------------------*/ |
| 116 | |
| 117 | static int is_delim(char c) |
| 118 | { |
| 119 | return (c == '\0') || (c == ','); |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | /*------------------------------------------------- |
| 125 | image_internal_find_extension - find an extension in |
| 126 | an extension list |
| 127 | -------------------------------------------------*/ |
| 128 | |
| 129 | static int image_internal_find_extension(const char *extension_list, const char *target_extension) |
| 130 | { |
| 131 | /* this version allows target_extension to be delimited with a comma */ |
| 132 | int pos = 0; |
| 133 | int i; |
| 134 | |
| 135 | while(extension_list[pos] != '\0') |
| 136 | { |
| 137 | /* compare a file extension */ |
| 138 | i = 0; |
| 139 | while(!is_delim(extension_list[pos + i]) |
| 140 | && !is_delim(target_extension[i]) |
| 141 | && (tolower(extension_list[pos + i]) == tolower(target_extension[i]))) |
| 142 | { |
| 143 | i++; |
| 144 | } |
| 145 | |
| 146 | /* check to see if it was found */ |
| 147 | if (is_delim(extension_list[pos + i]) && is_delim(target_extension[i])) |
| 148 | return TRUE; |
| 149 | |
| 150 | /* move to next position in the buffer */ |
| 151 | pos += i; |
| 152 | while(!is_delim(extension_list[pos])) |
| 153 | pos++; |
| 154 | while(extension_list[pos] == ',') |
| 155 | pos++; |
| 156 | } |
| 157 | |
| 158 | /* not found */ |
| 159 | return FALSE; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | /*------------------------------------------------- |
| 165 | image_find_extension - find an extension in an extension |
| 166 | list |
| 167 | -------------------------------------------------*/ |
| 168 | |
| 169 | int image_find_extension(const char *extension_list, const char *target_extension) |
| 170 | { |
| 171 | /* the internal function allows something that we do not */ |
| 172 | if (strchr(target_extension, ',')) |
| 173 | return FALSE; |
| 174 | |
| 175 | /* special case to allow ext to be in the form '.EXT' */ |
| 176 | if (*target_extension == '.') |
| 177 | target_extension++; |
| 178 | |
| 179 | /* do the actual work */ |
| 180 | return image_internal_find_extension(extension_list, target_extension); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | |
| 185 | /*------------------------------------------------- |
| 186 | image_specify_extension - merge a comma-delimited |
| 187 | list of file extensions onto an existing list |
| 188 | -------------------------------------------------*/ |
| 189 | |
| 190 | void image_specify_extension(char *buffer, size_t buffer_len, const char *extension) |
| 191 | { |
| 192 | int extension_pos = 0; |
| 193 | int len; |
| 194 | int found; |
| 195 | |
| 196 | /* determine the length of the buffer */ |
| 197 | len = strlen(buffer); |
| 198 | |
| 199 | /* be aware that extension can be NULL */ |
| 200 | if (extension != NULL) |
| 201 | { |
| 202 | while(extension[extension_pos] != '\0') |
| 203 | { |
| 204 | /* try to find the file extension */ |
| 205 | found = image_internal_find_extension(buffer, &extension[extension_pos]); |
| 206 | |
| 207 | /* append a delimiter if we have to */ |
| 208 | if (!found && (len > 0)) |
| 209 | len += snprintf(&buffer[len], buffer_len - len, ","); |
| 210 | |
| 211 | /* move to the next extension, appending the extension if not found */ |
| 212 | while(!is_delim(extension[extension_pos])) |
| 213 | { |
| 214 | if (!found) |
| 215 | len += snprintf(&buffer[len], buffer_len - len, "%c", extension[extension_pos]); |
| 216 | extension_pos++; |
| 217 | } |
| 218 | while(extension[extension_pos] == ',') |
| 219 | extension_pos++; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
src/lib/formats/tzx_cas.c
| r0 | r11458 | |
| 1 | /* |
| 2 | |
| 3 | TZX (currently spectrum only) and spectrum TAP cassette format support by Wilbert Pol |
| 4 | |
| 5 | TODO: |
| 6 | Add support for the remaining block types: |
| 7 | case 0x15: Direct Recording |
| 8 | case 0x18: CSW Recording |
| 9 | case 0x19: Generalized Data Block |
| 10 | case 0x21: Group Start |
| 11 | case 0x22: Group End |
| 12 | case 0x23: Jump To Block |
| 13 | case 0x24: Loop Start |
| 14 | case 0x25: Loop End |
| 15 | case 0x26: Call Sequence |
| 16 | case 0x27: Return From Sequence |
| 17 | case 0x28: Select Block |
| 18 | case 0x2A: Stop Tape if in 48K Mode |
| 19 | case 0x2B: Set signal level |
| 20 | case 0x5A: Merge Block |
| 21 | Add support for the deprecated block types? Only if there is some image which need them: |
| 22 | case 0x16: C64 ROM type data block |
| 23 | case 0x17: C64 turbo tape data block |
| 24 | case 0x34: Emulation info |
| 25 | case 0x40: Snapshot block |
| 26 | |
| 27 | Notes: |
| 28 | |
| 29 | TZX format specification lists |
| 30 | 8064 pulses for a header block and 3220 for a data block |
| 31 | |
| 32 | but the documentaiton on worldofspectrum lists |
| 33 | 8063 pulses for a header block and 3223 for a data block |
| 34 | |
| 35 | see http://www.worldofspectrum.org/faq/reference/48kreference.htm#TapeDataStructure |
| 36 | |
| 37 | We are currently using the numbers from the TZX specification... |
| 38 | |
| 39 | */ |
| 40 | |
| 41 | #include "tzx_cas.h" |
| 42 | |
| 43 | |
| 44 | #define TZX_WAV_FREQUENCY 44100 |
| 45 | #define WAVE_LOW -0x5a9e |
| 46 | #define WAVE_HIGH 0x5a9e |
| 47 | #define WAVE_NULL 0 |
| 48 | |
| 49 | #define SUPPORTED_VERSION_MAJOR 0x01 |
| 50 | |
| 51 | #define INITIAL_MAX_BLOCK_COUNT 256 |
| 52 | #define BLOCK_COUNT_INCREMENTS 256 |
| 53 | |
| 54 | static const UINT8 TZX_HEADER[8] = { 'Z','X','T','a','p','e','!',0x1a }; |
| 55 | |
| 56 | /* |
| 57 | Global variables |
| 58 | |
| 59 | Initialized by tzx_cas_get_wave_size, used (and cleaned up) by tzx_cas_fill_wave |
| 60 | */ |
| 61 | |
| 62 | static INT16 wave_data = 0; |
| 63 | static int block_count = 0; |
| 64 | static UINT8** blocks = NULL; |
| 65 | static float t_scale = 1; /* for scaling T-states to the 4MHz CPC */ |
| 66 | |
| 67 | static void toggle_wave_data(void) |
| 68 | { |
| 69 | if (wave_data == WAVE_LOW) |
| 70 | { |
| 71 | wave_data = WAVE_HIGH; |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | wave_data = WAVE_LOW; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static void tzx_cas_get_blocks( const UINT8 *casdata, int caslen ) |
| 80 | { |
| 81 | int pos = sizeof(TZX_HEADER) + 2; |
| 82 | int max_block_count = INITIAL_MAX_BLOCK_COUNT; |
| 83 | |
| 84 | blocks = (UINT8**)malloc(max_block_count); |
| 85 | memset(blocks,0,max_block_count); |
| 86 | block_count = 0; |
| 87 | |
| 88 | while (pos < caslen) |
| 89 | { |
| 90 | UINT32 datasize; |
| 91 | UINT8 blocktype = casdata[pos]; |
| 92 | |
| 93 | if (block_count == max_block_count) |
| 94 | { |
| 95 | void *old_blocks = blocks; |
| 96 | int old_max_block_count = max_block_count; |
| 97 | printf("realoc"); |
| 98 | max_block_count = max_block_count + BLOCK_COUNT_INCREMENTS; |
| 99 | blocks = (UINT8**)malloc(max_block_count); // SHOULD NOT BE USING auto_alloc_array() |
| 100 | memset(blocks,0,max_block_count); |
| 101 | memcpy(blocks, old_blocks, old_max_block_count * sizeof(UINT8*)); |
| 102 | if (blocks) free(old_blocks); |
| 103 | } |
| 104 | |
| 105 | blocks[block_count] = (UINT8*)&casdata[pos]; |
| 106 | |
| 107 | pos += 1; |
| 108 | |
| 109 | switch (blocktype) |
| 110 | { |
| 111 | case 0x10: |
| 112 | pos += 2; |
| 113 | datasize = casdata[pos] + (casdata[pos + 1] << 8); |
| 114 | pos += 2 + datasize; |
| 115 | break; |
| 116 | case 0x11: |
| 117 | pos += 0x0f; |
| 118 | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16); |
| 119 | pos += 3 + datasize; |
| 120 | break; |
| 121 | case 0x12: |
| 122 | pos += 4; |
| 123 | break; |
| 124 | case 0x13: |
| 125 | datasize = casdata[pos]; |
| 126 | pos += 1 + 2 * datasize; |
| 127 | break; |
| 128 | case 0x14: |
| 129 | pos += 7; |
| 130 | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16); |
| 131 | pos += 3 + datasize; |
| 132 | break; |
| 133 | case 0x15: |
| 134 | pos += 5; |
| 135 | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16); |
| 136 | pos += 3 + datasize; |
| 137 | break; |
| 138 | case 0x20: case 0x23: case 0x24: |
| 139 | pos += 2; |
| 140 | break; |
| 141 | case 0x21: case 0x30: |
| 142 | datasize = casdata[pos]; |
| 143 | pos += 1 + datasize; |
| 144 | break; |
| 145 | case 0x22: case 0x25: case 0x27: |
| 146 | break; |
| 147 | case 0x26: |
| 148 | datasize = casdata[pos] + (casdata[pos + 1] << 8); |
| 149 | pos += 2 + 2 * datasize; |
| 150 | break; |
| 151 | case 0x28: case 0x32: |
| 152 | datasize = casdata[pos] + (casdata[pos + 1] << 8); |
| 153 | pos += 2 + datasize; |
| 154 | break; |
| 155 | case 0x31: |
| 156 | pos += 1; |
| 157 | datasize = casdata[pos]; |
| 158 | pos += 1 + datasize; |
| 159 | break; |
| 160 | case 0x33: |
| 161 | datasize = casdata[pos]; |
| 162 | pos += 1 + 3 * datasize; |
| 163 | break; |
| 164 | case 0x34: |
| 165 | pos += 8; |
| 166 | break; |
| 167 | case 0x35: |
| 168 | pos += 0x10; |
| 169 | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16) + (casdata[pos + 3] << 24); |
| 170 | pos += 4 + datasize; |
| 171 | break; |
| 172 | case 0x40: |
| 173 | pos += 1; |
| 174 | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16); |
| 175 | pos += 3 + datasize; |
| 176 | break; |
| 177 | case 0x5A: |
| 178 | pos += 9; |
| 179 | break; |
| 180 | default: |
| 181 | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16) + (casdata[pos + 3] << 24); |
| 182 | pos += 4 + datasize; |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | block_count++; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | INLINE int millisec_to_samplecount( int millisec ) |
| 191 | { |
| 192 | return (int) (millisec * ((double)TZX_WAV_FREQUENCY / 1000.0)); |
| 193 | } |
| 194 | |
| 195 | INLINE int tcycles_to_samplecount( int tcycles ) |
| 196 | { |
| 197 | return (int) ((0.5 + (((double)TZX_WAV_FREQUENCY / 3500000) * (double)tcycles)) * t_scale); |
| 198 | } |
| 199 | |
| 200 | static void tzx_output_wave( INT16 **buffer, int length ) |
| 201 | { |
| 202 | if (buffer == NULL) |
| 203 | { |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | for ( ; length > 0; length--) |
| 208 | { |
| 209 | **buffer = wave_data; |
| 210 | *buffer = *buffer + 1; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | static int tzx_cas_handle_block( INT16 **buffer, const UINT8 *bytes, int pause, int data_size, int pilot, int pilot_length, int sync1, int sync2, int bit0, int bit1, int bits_in_last_byte ) |
| 215 | { |
| 216 | int pilot_samples = tcycles_to_samplecount(pilot); |
| 217 | int sync1_samples = tcycles_to_samplecount(sync1); |
| 218 | int sync2_samples = tcycles_to_samplecount(sync2); |
| 219 | int bit0_samples = tcycles_to_samplecount(bit0); |
| 220 | int bit1_samples = tcycles_to_samplecount(bit1); |
| 221 | int data_index; |
| 222 | int size = 0; |
| 223 | |
| 224 | /* Uncomment this to include into error.log a fully detailed analysis of each block */ |
| 225 | // logerror("tzx_cas_block_size: pilot_length = %d, pilot_samples = %d, sync1_samples = %d, sync2_samples = %d, bit0_samples = %d, bit1_samples = %d\n", pilot_length, pilot_samples, sync1_samples, sync2_samples, bit0_samples, bit1_samples); |
| 226 | |
| 227 | /* PILOT */ |
| 228 | for ( ; pilot_length > 0; pilot_length--) |
| 229 | { |
| 230 | tzx_output_wave(buffer, pilot_samples); |
| 231 | size += pilot_samples; |
| 232 | toggle_wave_data(); |
| 233 | } |
| 234 | /* SYNC1 */ |
| 235 | if (sync1_samples > 0) |
| 236 | { |
| 237 | tzx_output_wave(buffer, sync1_samples); |
| 238 | size += sync1_samples; |
| 239 | toggle_wave_data(); |
| 240 | } |
| 241 | /* SYNC2 */ |
| 242 | if (sync2_samples > 0) |
| 243 | { |
| 244 | tzx_output_wave(buffer, sync2_samples); |
| 245 | size += sync2_samples; |
| 246 | toggle_wave_data(); |
| 247 | } |
| 248 | /* data */ |
| 249 | for (data_index = 0; data_index < data_size; data_index++) |
| 250 | { |
| 251 | UINT8 byte = bytes[data_index]; |
| 252 | int bits_to_go = (data_index == (data_size - 1)) ? bits_in_last_byte : 8; |
| 253 | |
| 254 | for ( ; bits_to_go > 0; byte <<= 1, bits_to_go--) |
| 255 | { |
| 256 | int bit_samples = (byte & 0x80) ? bit1_samples : bit0_samples; |
| 257 | tzx_output_wave(buffer, bit_samples); |
| 258 | size += bit_samples; |
| 259 | toggle_wave_data(); |
| 260 | tzx_output_wave(buffer, bit_samples); |
| 261 | size += bit_samples; |
| 262 | toggle_wave_data(); |
| 263 | } |
| 264 | } |
| 265 | /* pause */ |
| 266 | if (pause > 0) |
| 267 | { |
| 268 | int start_pause_samples = millisec_to_samplecount(1); |
| 269 | int rest_pause_samples = millisec_to_samplecount(pause - 1); |
| 270 | |
| 271 | tzx_output_wave(buffer, start_pause_samples); |
| 272 | size += start_pause_samples; |
| 273 | wave_data = WAVE_LOW; |
| 274 | tzx_output_wave(buffer, rest_pause_samples); |
| 275 | size += rest_pause_samples; |
| 276 | } |
| 277 | return size; |
| 278 | } |
| 279 | |
| 280 | static void ascii_block_common_log( const char *block_type_string, UINT8 block_type ) |
| 281 | { |
| 282 | logerror("%s (type %02x) encountered.\n", block_type_string, block_type); |
| 283 | logerror("This block contains info on the .tzx file you are loading.\n"); |
| 284 | logerror("Please include the following info in your bug reports, if the image has issue in M.E.S.S.\n"); |
| 285 | } |
| 286 | |
| 287 | static const char *const archive_ident[] = |
| 288 | { |
| 289 | "Full title", |
| 290 | "Software house/publisher", |
| 291 | "Author(s)", |
| 292 | "Year of publication", |
| 293 | "Language", |
| 294 | "Game/utility type", |
| 295 | "Price", |
| 296 | "Protection scheme/loader", |
| 297 | "Origin", |
| 298 | }; |
| 299 | |
| 300 | static const char *const hw_info[] = |
| 301 | { |
| 302 | "Tape runs on this machine / this hardware", |
| 303 | "Tape needs this machine / this hardware", |
| 304 | "Tape runs on this machine / this hardware, but does not require its special features", |
| 305 | "Tape does not run on this machine / this hardware", |
| 306 | }; |
| 307 | |
| 308 | /* Will go through blocks and calculate number of samples needed. |
| 309 | If buffer is not NULL the sample data will also be written. */ |
| 310 | static int tzx_cas_do_work( INT16 **buffer ) |
| 311 | { |
| 312 | int current_block = 0; |
| 313 | int size = 0; |
| 314 | |
| 315 | wave_data = WAVE_LOW; |
| 316 | |
| 317 | while (current_block < block_count) |
| 318 | { |
| 319 | int pause_time; |
| 320 | int data_size, text_size, total_size, i; |
| 321 | int pilot, pilot_length, sync1, sync2; |
| 322 | int bit0, bit1, bits_in_last_byte; |
| 323 | UINT8 *cur_block = blocks[current_block]; |
| 324 | UINT8 block_type = cur_block[0]; |
| 325 | |
| 326 | /* Uncomment this to include into error.log a list of the types each block */ |
| 327 | // logerror("tzx_cas_fill_wave: block %d, block_type %02x\n", current_block, block_type); |
| 328 | |
| 329 | switch (block_type) |
| 330 | { |
| 331 | case 0x10: /* Standard Speed Data Block (.TAP block) */ |
| 332 | pause_time = cur_block[1] + (cur_block[2] << 8); |
| 333 | data_size = cur_block[3] + (cur_block[4] << 8); |
| 334 | pilot_length = (cur_block[5] == 0x00) ? 8064 : 3220; |
| 335 | size += tzx_cas_handle_block(buffer, &cur_block[5], pause_time, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8); |
| 336 | current_block++; |
| 337 | break; |
| 338 | case 0x11: /* Turbo Loading Data Block */ |
| 339 | pilot = cur_block[1] + (cur_block[2] << 8); |
| 340 | sync1 = cur_block[3] + (cur_block[4] << 8); |
| 341 | sync2 = cur_block[5] + (cur_block[6] << 8); |
| 342 | bit0 = cur_block[7] + (cur_block[8] << 8); |
| 343 | bit1 = cur_block[9] + (cur_block[10] << 8); |
| 344 | pilot_length = cur_block[11] + (cur_block[12] << 8); |
| 345 | bits_in_last_byte = cur_block[13]; |
| 346 | pause_time = cur_block[14] + (cur_block[15] << 8); |
| 347 | data_size = cur_block[16] + (cur_block[17] << 8) + (cur_block[18] << 16); |
| 348 | size += tzx_cas_handle_block(buffer, &cur_block[19], pause_time, data_size, pilot, pilot_length, sync1, sync2, bit0, bit1, bits_in_last_byte); |
| 349 | current_block++; |
| 350 | break; |
| 351 | case 0x12: /* Pure Tone */ |
| 352 | pilot = cur_block[1] + (cur_block[2] << 8); |
| 353 | pilot_length = cur_block[3] + (cur_block[4] << 8); |
| 354 | size += tzx_cas_handle_block(buffer, cur_block, 0, 0, pilot, pilot_length, 0, 0, 0, 0, 0); |
| 355 | current_block++; |
| 356 | break; |
| 357 | case 0x13: /* Sequence of Pulses of Different Lengths */ |
| 358 | for (data_size = 0; data_size < cur_block[1]; data_size++) |
| 359 | { |
| 360 | pilot = cur_block[2 + 2 * data_size] + (cur_block[3 + 2 * data_size] << 8); |
| 361 | size += tzx_cas_handle_block(buffer, cur_block, 0, 0, pilot, 1, 0, 0, 0, 0, 0); |
| 362 | } |
| 363 | current_block++; |
| 364 | break; |
| 365 | case 0x14: /* Pure Data Block */ |
| 366 | bit0 = cur_block[1] + (cur_block[2] << 8); |
| 367 | bit1 = cur_block[3] + (cur_block[4] << 8); |
| 368 | bits_in_last_byte = cur_block[5]; |
| 369 | pause_time = cur_block[6] + (cur_block[7] << 8); |
| 370 | data_size = cur_block[8] + (cur_block[9] << 8) + (cur_block[10] << 16); |
| 371 | size += tzx_cas_handle_block(buffer, &cur_block[11], pause_time, data_size, 0, 0, 0, 0, bit0, bit1, bits_in_last_byte); |
| 372 | current_block++; |
| 373 | break; |
| 374 | case 0x20: /* Pause (Silence) or 'Stop the Tape' Command */ |
| 375 | pause_time = cur_block[1] + (cur_block[2] << 8); |
| 376 | if (pause_time == 0) |
| 377 | { |
| 378 | /* pause = 0 is used to let an emulator automagically stop the tape |
| 379 | in MESS we do not do that, so we insert a 5 second pause. */ |
| 380 | pause_time = 5000; |
| 381 | } |
| 382 | size += tzx_cas_handle_block(buffer, cur_block, pause_time, 0, 0, 0, 0, 0, 0, 0, 0); |
| 383 | current_block++; |
| 384 | break; |
| 385 | case 0x16: /* C64 ROM Type Data Block */ // Deprecated in TZX 1.20 |
| 386 | case 0x17: /* C64 Turbo Tape Data Block */ // Deprecated in TZX 1.20 |
| 387 | case 0x34: /* Emulation Info */ // Deprecated in TZX 1.20 |
| 388 | case 0x40: /* Snapshot Block */ // Deprecated in TZX 1.20 |
| 389 | logerror("Deprecated block type (%02x) encountered.\n", block_type); |
| 390 | logerror("Please look for an updated .tzx file.\n"); |
| 391 | current_block++; |
| 392 | break; |
| 393 | case 0x30: /* Text Description */ |
| 394 | ascii_block_common_log("Text Description Block", block_type); |
| 395 | for (data_size = 0; data_size < cur_block[1]; data_size++) |
| 396 | logerror("%c", cur_block[2 + data_size]); |
| 397 | logerror("\n"); |
| 398 | current_block++; |
| 399 | break; |
| 400 | case 0x31: /* Message Block */ |
| 401 | ascii_block_common_log("Message Block", block_type); |
| 402 | logerror("Expected duration of the message display: %02x\n", cur_block[1]); |
| 403 | logerror("Message: \n"); |
| 404 | for (data_size = 0; data_size < cur_block[2]; data_size++) |
| 405 | { |
| 406 | logerror("%c", cur_block[3 + data_size]); |
| 407 | if (cur_block[3 + data_size] == 0x0d) |
| 408 | logerror("\n"); |
| 409 | } |
| 410 | logerror("\n"); |
| 411 | current_block++; |
| 412 | break; |
| 413 | case 0x32: /* Archive Info */ |
| 414 | ascii_block_common_log("Archive Info Block", block_type); |
| 415 | total_size = cur_block[1] + (cur_block[2] << 8); |
| 416 | text_size = 0; |
| 417 | for (data_size = 0; data_size < cur_block[3]; data_size++) // data_size = number of text blocks, in this case |
| 418 | { |
| 419 | if (cur_block[4 + text_size] < 0x09) |
| 420 | logerror("%s: \n", archive_ident[cur_block[4 + text_size]]); |
| 421 | else |
| 422 | logerror("Comment(s): \n"); |
| 423 | |
| 424 | for (i = 0; i < cur_block[4 + text_size + 1]; i++) |
| 425 | { |
| 426 | logerror("%c", cur_block[4 + text_size + 2 + i]); |
| 427 | } |
| 428 | text_size += 2 + i; |
| 429 | } |
| 430 | logerror("\n"); |
| 431 | if (text_size != total_size) |
| 432 | logerror("Malformed Archive Info Block (Text length different from the declared one).\n Please verify your tape image.\n"); |
| 433 | current_block++; |
| 434 | break; |
| 435 | case 0x33: /* Hardware Type */ |
| 436 | ascii_block_common_log("Hardware Type Block", block_type); |
| 437 | for (data_size = 0; data_size < cur_block[1]; data_size++) // data_size = number of hardware blocks, in this case |
| 438 | { |
| 439 | logerror("Hardware Type %02x - Hardware ID %02x - ", cur_block[2 + data_size * 3], cur_block[2 + data_size * 3 + 1]); |
| 440 | logerror("%s \n ", hw_info[cur_block[2 + data_size * 3 + 2]]); |
| 441 | } |
| 442 | current_block++; |
| 443 | break; |
| 444 | case 0x35: /* Custom Info Block */ |
| 445 | ascii_block_common_log("Custom Info Block", block_type); |
| 446 | for (data_size = 0; data_size < 10; data_size++) |
| 447 | { |
| 448 | logerror("%c", cur_block[1 + data_size]); |
| 449 | } |
| 450 | logerror(":\n"); |
| 451 | text_size = cur_block[11] + (cur_block[12] << 8) + (cur_block[13] << 16) + (cur_block[14] << 24); |
| 452 | for (data_size = 0; data_size < text_size; data_size++) |
| 453 | logerror("%c", cur_block[15 + data_size]); |
| 454 | logerror("\n"); |
| 455 | current_block++; |
| 456 | break; |
| 457 | case 0x5A: /* "Glue" Block */ |
| 458 | logerror("Glue Block (type %02x) encountered.\n", block_type); |
| 459 | logerror("Please use a .tzx handling utility to split the merged tape files.\n"); |
| 460 | current_block++; |
| 461 | break; |
| 462 | case 0x15: /* Direct Recording */ |
| 463 | case 0x18: /* CSW Recording */ |
| 464 | case 0x19: /* Generalized Data Block */ |
| 465 | case 0x21: /* Group Start */ |
| 466 | case 0x22: /* Group End */ |
| 467 | case 0x23: /* Jump To Block */ |
| 468 | case 0x24: /* Loop Start */ |
| 469 | case 0x25: /* Loop End */ |
| 470 | case 0x26: /* Call Sequence */ |
| 471 | case 0x27: /* Return From Sequence */ |
| 472 | case 0x28: /* Select Block */ |
| 473 | case 0x2A: /* Stop Tape if in 48K Mode */ |
| 474 | case 0x2B: /* Set signal level */ |
| 475 | default: |
| 476 | logerror("Unsupported block type (%02x) encountered.\n", block_type); |
| 477 | current_block++; |
| 478 | break; |
| 479 | } |
| 480 | } |
| 481 | return size; |
| 482 | } |
| 483 | |
| 484 | static int tzx_cas_to_wav_size( const UINT8 *casdata, int caslen ) |
| 485 | { |
| 486 | int size = 0; |
| 487 | |
| 488 | /* Header size plus major and minor version number */ |
| 489 | if (caslen < 10) |
| 490 | { |
| 491 | logerror("tzx_cas_to_wav_size: cassette image too small\n"); |
| 492 | goto cleanup; |
| 493 | } |
| 494 | |
| 495 | /* Check for correct header */ |
| 496 | if (memcmp(casdata, TZX_HEADER, sizeof(TZX_HEADER))) |
| 497 | { |
| 498 | logerror("tzx_cas_to_wav_size: cassette image has incompatible header\n"); |
| 499 | goto cleanup; |
| 500 | } |
| 501 | |
| 502 | /* Check major version number in header */ |
| 503 | if (casdata[0x08] > SUPPORTED_VERSION_MAJOR) |
| 504 | { |
| 505 | logerror("tzx_cas_to_wav_size: unsupported version\n"); |
| 506 | goto cleanup; |
| 507 | } |
| 508 | |
| 509 | tzx_cas_get_blocks(casdata, caslen); |
| 510 | |
| 511 | logerror("tzx_cas_to_wav_size: %d blocks found\n", block_count); |
| 512 | |
| 513 | if (block_count == 0) |
| 514 | { |
| 515 | logerror("tzx_cas_to_wav_size: no blocks found!\n"); |
| 516 | goto cleanup; |
| 517 | } |
| 518 | |
| 519 | size = tzx_cas_do_work(NULL); |
| 520 | |
| 521 | return size; |
| 522 | |
| 523 | cleanup: |
| 524 | return -1; |
| 525 | } |
| 526 | |
| 527 | static int tzx_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) |
| 528 | { |
| 529 | INT16 *p = buffer; |
| 530 | int size = 0; |
| 531 | t_scale = 1.0; |
| 532 | size = tzx_cas_do_work(&p); |
| 533 | return size; |
| 534 | } |
| 535 | |
| 536 | static int cdt_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) |
| 537 | { |
| 538 | INT16 *p = buffer; |
| 539 | int size = 0; |
| 540 | t_scale = (40 / 35); /* scale to 4MHz */ |
| 541 | size = tzx_cas_do_work(&p); |
| 542 | return size; |
| 543 | } |
| 544 | |
| 545 | static int tap_cas_to_wav_size( const UINT8 *casdata, int caslen ) |
| 546 | { |
| 547 | int size = 0; |
| 548 | const UINT8 *p = casdata; |
| 549 | |
| 550 | while (p < casdata + caslen) |
| 551 | { |
| 552 | int data_size = p[0] + (p[1] << 8); |
| 553 | int pilot_length = (p[2] == 0x00) ? 8064 : 3220; /* TZX specification */ |
| 554 | // int pilot_length = (p[2] == 0x00) ? 8063 : 3223; /* worldofspectrum */ |
| 555 | logerror("tap_cas_to_wav_size: Handling TAP block containing 0x%X bytes", data_size); |
| 556 | p += 2; |
| 557 | size += tzx_cas_handle_block(NULL, p, 1000, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8); |
| 558 | logerror(", total size is now: %d\n", size); |
| 559 | p += data_size; |
| 560 | } |
| 561 | return size; |
| 562 | } |
| 563 | |
| 564 | static int tap_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) |
| 565 | { |
| 566 | INT16 *p = buffer; |
| 567 | int size = 0; |
| 568 | |
| 569 | while (size < length) |
| 570 | { |
| 571 | int data_size = bytes[0] + (bytes[1] << 8); |
| 572 | int pilot_length = (bytes[2] == 0x00) ? 8064 : 3220; /* TZX specification */ |
| 573 | // int pilot_length = (bytes[2] == 0x00) ? 8063 : 3223; /* worldofspectrum */ |
| 574 | logerror("tap_cas_fill_wave: Handling TAP block containing 0x%X bytes\n", data_size); |
| 575 | bytes += 2; |
| 576 | size += tzx_cas_handle_block(&p, bytes, 1000, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8); |
| 577 | bytes += data_size; |
| 578 | } |
| 579 | return size; |
| 580 | } |
| 581 | |
| 582 | static const struct CassetteLegacyWaveFiller tzx_legacy_fill_wave = |
| 583 | { |
| 584 | tzx_cas_fill_wave, /* fill_wave */ |
| 585 | -1, /* chunk_size */ |
| 586 | 0, /* chunk_samples */ |
| 587 | tzx_cas_to_wav_size, /* chunk_sample_calc */ |
| 588 | TZX_WAV_FREQUENCY, /* sample_frequency */ |
| 589 | 0, /* header_samples */ |
| 590 | 0 /* trailer_samples */ |
| 591 | }; |
| 592 | |
| 593 | static const struct CassetteLegacyWaveFiller tap_legacy_fill_wave = |
| 594 | { |
| 595 | tap_cas_fill_wave, /* fill_wave */ |
| 596 | -1, /* chunk_size */ |
| 597 | 0, /* chunk_samples */ |
| 598 | tap_cas_to_wav_size, /* chunk_sample_calc */ |
| 599 | TZX_WAV_FREQUENCY, /* sample_frequency */ |
| 600 | 0, /* header_samples */ |
| 601 | 0 /* trailer_samples */ |
| 602 | }; |
| 603 | |
| 604 | static const struct CassetteLegacyWaveFiller cdt_legacy_fill_wave = |
| 605 | { |
| 606 | cdt_cas_fill_wave, /* fill_wave */ |
| 607 | -1, /* chunk_size */ |
| 608 | 0, /* chunk_samples */ |
| 609 | tzx_cas_to_wav_size, /* chunk_sample_calc */ |
| 610 | TZX_WAV_FREQUENCY, /* sample_frequency */ |
| 611 | 0, /* header_samples */ |
| 612 | 0 /* trailer_samples */ |
| 613 | }; |
| 614 | |
| 615 | static casserr_t tzx_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) |
| 616 | { |
| 617 | return cassette_legacy_identify(cassette, opts, &tzx_legacy_fill_wave); |
| 618 | } |
| 619 | |
| 620 | static casserr_t tap_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) |
| 621 | { |
| 622 | return cassette_legacy_identify(cassette, opts, &tap_legacy_fill_wave); |
| 623 | } |
| 624 | |
| 625 | static casserr_t cdt_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) |
| 626 | { |
| 627 | return cassette_legacy_identify(cassette, opts, &cdt_legacy_fill_wave); |
| 628 | } |
| 629 | |
| 630 | static casserr_t tzx_cassette_load( cassette_image *cassette ) |
| 631 | { |
| 632 | return cassette_legacy_construct(cassette, &tzx_legacy_fill_wave); |
| 633 | } |
| 634 | |
| 635 | static casserr_t tap_cassette_load( cassette_image *cassette ) |
| 636 | { |
| 637 | return cassette_legacy_construct(cassette, &tap_legacy_fill_wave); |
| 638 | } |
| 639 | |
| 640 | static casserr_t cdt_cassette_load( cassette_image *cassette ) |
| 641 | { |
| 642 | return cassette_legacy_construct(cassette, &cdt_legacy_fill_wave); |
| 643 | } |
| 644 | |
| 645 | static const struct CassetteFormat tzx_cassette_format = |
| 646 | { |
| 647 | "tzx", |
| 648 | tzx_cassette_identify, |
| 649 | tzx_cassette_load, |
| 650 | NULL |
| 651 | }; |
| 652 | |
| 653 | static const struct CassetteFormat tap_cassette_format = |
| 654 | { |
| 655 | "tap,blk", |
| 656 | tap_cassette_identify, |
| 657 | tap_cassette_load, |
| 658 | NULL |
| 659 | }; |
| 660 | |
| 661 | static const struct CassetteFormat cdt_cassette_format = |
| 662 | { |
| 663 | "cdt", |
| 664 | cdt_cassette_identify, |
| 665 | cdt_cassette_load, |
| 666 | NULL |
| 667 | }; |
| 668 | |
| 669 | CASSETTE_FORMATLIST_START(tzx_cassette_formats) |
| 670 | CASSETTE_FORMAT(tzx_cassette_format) |
| 671 | CASSETTE_FORMAT(tap_cassette_format) |
| 672 | CASSETTE_FORMATLIST_END |
| 673 | |
| 674 | CASSETTE_FORMATLIST_START(cdt_cassette_formats) |
| 675 | CASSETTE_FORMAT(cdt_cassette_format) |
| 676 | CASSETTE_FORMATLIST_END |
| 677 | |
src/lib/formats/coco_dsk.c
| r0 | r11458 | |
| 1 | /********************************************************************* |
| 2 | |
| 3 | formats/coco_dsk.c |
| 4 | |
| 5 | Tandy Color Computer / Dragon disk images |
| 6 | |
| 7 | *********************************************************************/ |
| 8 | |
| 9 | #include <string.h> |
| 10 | #include <assert.h> |
| 11 | #include <time.h> |
| 12 | |
| 13 | #include "osdepend.h" |
| 14 | #include "formats/coco_dsk.h" |
| 15 | #include "formats/basicdsk.h" |
| 16 | #include "imageutl.h" |
| 17 | |
| 18 | /* ----------------------------------------------------------------------- |
| 19 | * JVC (Jeff Vavasour CoCo) format |
| 20 | * |
| 21 | * Used by Jeff Vavasour's CoCo Emulators |
| 22 | * |
| 23 | * Documentation taken from Tim Linder's web site: |
| 24 | * http://home.netcom.com/~tlindner/JVC.html |
| 25 | * |
| 26 | * A. Header length |
| 27 | * The header length is determined by the file length modulo 256: |
| 28 | * headerSize = fileLength % 256; |
| 29 | * This means that the header is variable length and the minimum size |
| 30 | * is zero bytes, and the maximum size of 255 bytes. |
| 31 | * |
| 32 | * B. Header |
| 33 | * Here is a description of the header bytes: |
| 34 | * Byte Offset Description Default |
| 35 | * ----------- ----------------- ------- |
| 36 | * 0 Sectors per track 18 |
| 37 | * 1 Side count 1 |
| 38 | * 2 Sector size code 1 |
| 39 | * 3 First sector ID 1 |
| 40 | * 4 Sector attribute flag 0 |
| 41 | * |
| 42 | * If the sector attribute flag is zero then the track count is determined |
| 43 | * by the formula: |
| 44 | * |
| 45 | * (fileLength - headerSize) / (sectorsPerTrack * (128 << |
| 46 | * sectorSizeCode)) / sideCount |
| 47 | * |
| 48 | * If the sector attribute flag is non zero then the track count is |
| 49 | * determined by the more complex formula: |
| 50 | * |
| 51 | * (fileLength - headerSize) / (sectorsPerTrack * ((128 << |
| 52 | * sectorSizeCode) + 1) ) / sideCount |
| 53 | * |
| 54 | * If the length of the header is to short to contain the geometry desired, |
| 55 | * then the default values are assumed. If the header length is zero the all |
| 56 | * of the geometry is assumed. When creating disk images it is desirable to |
| 57 | * make the header length as short as possible. The header should only be |
| 58 | * used to deviate from the default values. |
| 59 | * |
| 60 | * The sector data begins immediately after the header. If the header length |
| 61 | * is zero then the sector data is at the beginning file. |
| 62 | * |
| 63 | * C. Sectors per track |
| 64 | * This is the number of sectors per track (ones based). A value of 18 |
| 65 | * means there are 18 sectors per track |
| 66 | * |
| 67 | * D. Side Count |
| 68 | * This is the number of sides in the disk image. Values of 1 or 2 are |
| 69 | * acceptable. If there are two sides then the tracks are interleaved. |
| 70 | * The first track in the image file is track zero side 1, the second |
| 71 | * track in the image file is track zero side 2. |
| 72 | * |
| 73 | * E. Sector size |
| 74 | * The is the same value that is stored in the wd179x ID field to |
| 75 | * determine sector size: |
| 76 | * |
| 77 | * 0x00 128 bytes |
| 78 | * 0x01 256 bytes |
| 79 | * 0x02 512 bytes |
| 80 | * 0x03 1024 bytes |
| 81 | * |
| 82 | * Other values are undefined. Every sector in the disk image must be the |
| 83 | * same size. |
| 84 | * |
| 85 | * F. First sector ID |
| 86 | * This determines the first sector ID for each track. Each successive |
| 87 | * sector adds one to the previous ID. If the first sector ID is 1, then |
| 88 | * the second sector has an ID of 2, and the third has an ID of 3. |
| 89 | * |
| 90 | * G. Sector Attribute Flag |
| 91 | * If this byte is non zero, then each sector contains an additional |
| 92 | * byte prepended to the sector data. If the attribute flag is zero then |
| 93 | * there are no extra bytes in front of the sector data. |
| 94 | * |
| 95 | * H. Sector attribute byte |
| 96 | * This byte is put at the begining of every sector if the header flag |
| 97 | * is turned on. The information this byte contains is the same as the |
| 98 | * status register (of the wd179x) would contain when a 'Read Sector' |
| 99 | * command was issued. The bit fields are defined as: |
| 100 | * |
| 101 | * Bit position: |
| 102 | * --------------- |
| 103 | * 7 6 5 4 3 2 1 0 |
| 104 | * | | | | | | | | |
| 105 | * | | | | | | | +--- Not used. Set to zero. |
| 106 | * | | | | | | +----- Not used. Set to zero. |
| 107 | * | | | | | +------- Not used. Set to zero. |
| 108 | * | | | | +--------- Set on CRC error. |
| 109 | * | | | +----------- Set if sector not found. |
| 110 | * | | +------------- Record type: 1 - Deleted Data Mark, 0 - Data Mark. |
| 111 | * | +--------------- Not Used. Set to zero. |
| 112 | * +----------------- Not Used. Set to zero. |
| 113 | * |
| 114 | * ----------------------------------------------------------------------- */ |
| 115 | |
| 116 | static int coco_jvc_decode_header(floppy_image *floppy, UINT64 size, |
| 117 | struct basicdsk_geometry *geometry) |
| 118 | { |
| 119 | UINT8 header[256]; |
| 120 | UINT8 sector_attribute_flag; |
| 121 | UINT16 physical_bytes_per_sector; |
| 122 | UINT32 header_size, file_size; |
| 123 | struct basicdsk_geometry dummy_geometry; |
| 124 | |
| 125 | if (geometry) |
| 126 | memset(geometry, 0, sizeof(*geometry)); |
| 127 | else |
| 128 | geometry = &dummy_geometry; |
| 129 | |
| 130 | if (size > 0xFFFFFFFF) |
| 131 | return -1; |
| 132 | file_size = (UINT32) size; |
| 133 | |
| 134 | /* read the header */ |
| 135 | header_size = (UINT32) file_size % 0x100; |
| 136 | floppy_image_read(floppy, header, 0, header_size); |
| 137 | geometry->offset = header_size; |
| 138 | |
| 139 | /* byte offset 0 - sectors per track */ |
| 140 | geometry->sectors = (header_size > 0) ? header[0] : 18; |
| 141 | if (geometry->sectors <= 0) |
| 142 | return -1; |
| 143 | |
| 144 | /* byte offset 1 - side count */ |
| 145 | geometry->heads = (header_size > 1) ? header[1] : 1; |
| 146 | if (geometry->heads <= 0) |
| 147 | return -1; |
| 148 | |
| 149 | /* byte offset 2 - sector size code */ |
| 150 | geometry->sector_length = 128 << ((header_size > 2) ? header[2] : 1); |
| 151 | if (geometry->sector_length <= 0) |
| 152 | return -1; |
| 153 | |
| 154 | /* byte offset 3 - first sector ID */ |
| 155 | geometry->first_sector_id = (header_size > 3) ? header[3] : 1; |
| 156 | |
| 157 | /* byte offset 4 - sector attribute flag */ |
| 158 | sector_attribute_flag = (header_size > 4) ? header[4] : 0; |
| 159 | if (sector_attribute_flag != 0) |
| 160 | return -1; /* we do not support sector attribute flags */ |
| 161 | |
| 162 | physical_bytes_per_sector = geometry->sector_length; |
| 163 | if (sector_attribute_flag) |
| 164 | physical_bytes_per_sector++; |
| 165 | |
| 166 | geometry->tracks = (file_size - header_size) / geometry->sectors / geometry->heads / physical_bytes_per_sector; |
| 167 | |
| 168 | /* do we have an oddball size? reject this file if not */ |
| 169 | if ((file_size - header_size) % physical_bytes_per_sector) |
| 170 | return -1; |
| 171 | |
| 172 | /* minimum of 35 tracks; support degenerate JVC files */ |
| 173 | if (geometry->tracks < 35) |
| 174 | geometry->tracks = 35; |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | |
| 181 | static FLOPPY_IDENTIFY(coco_jvc_identify) |
| 182 | { |
| 183 | UINT64 size; |
| 184 | size = floppy_image_size(floppy); |
| 185 | *vote = coco_jvc_decode_header(floppy, size, NULL) ? 0 : 100; |
| 186 | return FLOPPY_ERROR_SUCCESS; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | static FLOPPY_CONSTRUCT(coco_jvc_construct) |
| 192 | { |
| 193 | struct basicdsk_geometry geometry; |
| 194 | UINT8 header[5]; |
| 195 | size_t header_size; |
| 196 | |
| 197 | if (params) |
| 198 | { |
| 199 | /* create */ |
| 200 | memset(&geometry, 0, sizeof(geometry)); |
| 201 | geometry.heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 202 | geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 203 | geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 204 | geometry.first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID); |
| 205 | geometry.sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 206 | |
| 207 | header[0] = (UINT8) geometry.sectors; |
| 208 | header[1] = (UINT8) geometry.heads; |
| 209 | header[2] = (UINT8) compute_log2(geometry.sector_length) - 7; |
| 210 | header[3] = (UINT8) geometry.first_sector_id; |
| 211 | header[4] = 0; |
| 212 | |
| 213 | /* now that we have the header computed, figure out the header size */ |
| 214 | header_size = 0; |
| 215 | if (header[0] != 18) |
| 216 | header_size = 1; |
| 217 | if (header[1] != 1) |
| 218 | header_size = 2; |
| 219 | if (header[2] != 1) |
| 220 | header_size = 3; |
| 221 | if (header[3] != 1) |
| 222 | header_size = 4; |
| 223 | if (header[4] != 0) |
| 224 | header_size = 5; |
| 225 | |
| 226 | geometry.offset = header_size; |
| 227 | |
| 228 | floppy_image_write(floppy, header, 0, header_size); |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | /* load */ |
| 233 | if (coco_jvc_decode_header(floppy, floppy_image_size(floppy), &geometry)) |
| 234 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 235 | } |
| 236 | return basicdsk_construct(floppy, &geometry); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | /* ----------------------------------------------------------------------- |
| 241 | * OS-9 file format |
| 242 | * |
| 243 | * This file format is largely a hack because there are a large amount of |
| 244 | * disk images that do not have geometry image separate from the disk image |
| 245 | * itself. So we support OS-9 images with are simply basic disks whose |
| 246 | * geometry is determined by the disk image. |
| 247 | * |
| 248 | * OS-9 images identified by an LSN; which are simply blocks of 256 bytes |
| 249 | * |
| 250 | * LSN0 |
| 251 | * Byte size use |
| 252 | * $00 3 sectors on disk |
| 253 | * $03 1 track size in sectors |
| 254 | * $04 2 bytes in allocation bit map; typically 1bit/sector so for |
| 255 | * 35 tracks of 18 sectors each that's $4E (single sided disk) |
| 256 | * 40 tracks per side, 18 sectors each = $B4 |
| 257 | * $06 2 sectors per bit in allocation map; normally 1 |
| 258 | * $08 3 LSN of root directory; normally 2 but depends on size of |
| 259 | * $04 value |
| 260 | * $0B 2 owner's user number; normally 0 |
| 261 | * $0D 1 disk attributes; normally $FF |
| 262 | * $0E 2 psuedo random number for identification |
| 263 | * $10 1 disk format; typical is 3 |
| 264 | * %00000001 0=single side 1=double side |
| 265 | * %00000010 0=single density (non Coco) 1=double density |
| 266 | * %00000100 0=48tracks/inch 1=96tracks/inch |
| 267 | * $11 2 sectors per track; normal is $12 skip several not needed |
| 268 | * for Format |
| 269 | * $1A 5 date of creation Y:M:D:H:M |
| 270 | * $1F 32 ASCII name of disk, last letter has $80 added to it, |
| 271 | * the full 32 bytes do not need to be used. |
| 272 | * |
| 273 | * Allocation bit map, fill with zeros and set bits from low to high as |
| 274 | * sectors are used. So, for a fresh disk sectors LSN0,LSN1,LSN2, and LSN3 |
| 275 | * will be in use so the first byte will be $FF $C0 and all others |
| 276 | * in the map are $00 |
| 277 | * |
| 278 | * Root directory LSN2 |
| 279 | * Byte size use |
| 280 | * $00 1 attributes will be $BF |
| 281 | * $01 2 owners ID will be $0000 |
| 282 | * $03 5 date last modified will be creation date Y:M:D:H:M |
| 283 | * $08 1 link count; set to $02 |
| 284 | * $09 4 file size in bytes, set to $40 |
| 285 | * $0D 3 date created Y:M:D |
| 286 | * $10 3 block LSN set to current sector number+1 ie $03 in this case |
| 287 | * $13 2 size in sectors of directory block, set to $07 |
| 288 | * All other bytes in sector set to $00 |
| 289 | * LSN3 first sector of directory with names |
| 290 | * Fill sector with all $00 and then set listed bytes |
| 291 | * $00 2 value $2EAE which is .. with last byte+$80 |
| 292 | * $1F 1 value $02 LSN for start of this directory as there is |
| 293 | * none higher in tree |
| 294 | * $20 1 value $AE which is . with $80 added |
| 295 | * $3F 1 value $02 LSN for start of this directory |
| 296 | * ----------------------------------------------------------------------- */ |
| 297 | |
| 298 | static floperr_t coco_os9_readheader(floppy_image *floppy, struct basicdsk_geometry *geometry) |
| 299 | { |
| 300 | UINT8 header[0x20]; |
| 301 | int total_sectors; |
| 302 | |
| 303 | floppy_image_read(floppy, header, 0, sizeof(header)); |
| 304 | |
| 305 | total_sectors = (header[0x00] << 16) | (header[0x01] << 8) | header[0x02]; |
| 306 | |
| 307 | memset(geometry, 0, sizeof(*geometry)); |
| 308 | geometry->first_sector_id = 1; |
| 309 | geometry->sector_length = 256; |
| 310 | geometry->sectors = (header[0x11] << 8) + header[0x12]; |
| 311 | geometry->heads = (header[0x10] & 0x01) ? 2 : 1; |
| 312 | |
| 313 | if (!geometry->sectors) |
| 314 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 315 | |
| 316 | geometry->tracks = total_sectors / geometry->sectors / geometry->heads; |
| 317 | |
| 318 | if (total_sectors != geometry->tracks * geometry->sectors * geometry->heads) |
| 319 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 320 | |
| 321 | return FLOPPY_ERROR_SUCCESS; |
| 322 | } |
| 323 | |
| 324 | |
| 325 | |
| 326 | static floperr_t coco_os9_post_format(floppy_image *floppy, option_resolution *params) |
| 327 | { |
| 328 | UINT8 header[0x0400]; |
| 329 | floperr_t err; |
| 330 | time_t t; |
| 331 | struct tm *ltime; |
| 332 | int heads, tracks, sectors, total_sectors; |
| 333 | |
| 334 | heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 335 | tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 336 | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 337 | total_sectors = heads * tracks * sectors; |
| 338 | |
| 339 | /* write the initial header */ |
| 340 | time(&t); |
| 341 | ltime = localtime(&t); |
| 342 | |
| 343 | memset(&header, 0, sizeof(header)); |
| 344 | header[0x0000] = (UINT8) (total_sectors >> 16); |
| 345 | header[0x0001] = (UINT8) (total_sectors >> 8); |
| 346 | header[0x0002] = (UINT8) (total_sectors >> 0); |
| 347 | header[0x0003] = (UINT8) sectors; |
| 348 | header[0x0004] = (UINT8) (((total_sectors + 7) / 8) >> 8); |
| 349 | header[0x0005] = (UINT8) (((total_sectors + 7) / 8) >> 0); |
| 350 | header[0x0006] = 0x00; |
| 351 | header[0x0007] = 0x01; |
| 352 | header[0x0008] = 0x00; |
| 353 | header[0x0009] = 0x00; |
| 354 | header[0x000a] = 0x02; |
| 355 | header[0x000b] = 0x00; |
| 356 | header[0x000c] = 0x00; |
| 357 | header[0x000d] = 0xff; |
| 358 | header[0x000e] = floppy_random_byte(floppy); |
| 359 | header[0x000f] = floppy_random_byte(floppy); |
| 360 | header[0x0010] = (heads == 2) ? 3 : 2; |
| 361 | header[0x0011] = (UINT8) (sectors >> 8); |
| 362 | header[0x0012] = (UINT8) (sectors >> 0); |
| 363 | header[0x001A] = (UINT8) ltime->tm_year; |
| 364 | header[0x001B] = (UINT8) ltime->tm_mon + 1; |
| 365 | header[0x001C] = (UINT8) ltime->tm_mday; |
| 366 | header[0x001D] = (UINT8) ltime->tm_hour; |
| 367 | header[0x001E] = (UINT8) ltime->tm_min; |
| 368 | header[0x001F] = 0xA0; |
| 369 | header[0x0100] = 0xFF; |
| 370 | header[0x0101] = 0xC0; |
| 371 | header[0x0200] = 0xBF; |
| 372 | header[0x0201] = 0x00; |
| 373 | header[0x0202] = 0x00; |
| 374 | header[0x0203] = (UINT8) ltime->tm_year; |
| 375 | header[0x0204] = (UINT8) ltime->tm_mon + 1; |
| 376 | header[0x0205] = (UINT8) ltime->tm_mday; |
| 377 | header[0x0206] = (UINT8) ltime->tm_hour; |
| 378 | header[0x0207] = (UINT8) ltime->tm_min; |
| 379 | header[0x0208] = 0x02; |
| 380 | header[0x0209] = 0x00; |
| 381 | header[0x020A] = 0x00; |
| 382 | header[0x020B] = 0x00; |
| 383 | header[0x020C] = 0x40; |
| 384 | header[0x020D] = (UINT8) (ltime->tm_year % 100); |
| 385 | header[0x020E] = (UINT8) ltime->tm_mon; |
| 386 | header[0x020F] = (UINT8) ltime->tm_mday; |
| 387 | header[0x0210] = 0x00; |
| 388 | header[0x0211] = 0x00; |
| 389 | header[0x0212] = 0x03; |
| 390 | header[0x0213] = 0x00; |
| 391 | header[0x0214] = 0x07; |
| 392 | header[0x0300] = 0x2E; |
| 393 | header[0x0301] = 0xAE; |
| 394 | header[0x031F] = 0x02; |
| 395 | header[0x0320] = 0xAE; |
| 396 | header[0x033F] = 0x02; |
| 397 | |
| 398 | if (total_sectors % 8) |
| 399 | header[0x0100 + (total_sectors / 8)] = 0xFF >> (total_sectors % 8); |
| 400 | |
| 401 | err = floppy_write_sector(floppy, 0, 0, 1, 0, &header[0x0000], 256, 0); |
| 402 | if (err) |
| 403 | return err; |
| 404 | |
| 405 | err = floppy_write_sector(floppy, 0, 0, 2, 0, &header[0x0100], 256, 0); |
| 406 | if (err) |
| 407 | return err; |
| 408 | |
| 409 | err = floppy_write_sector(floppy, 0, 0, 3, 0, &header[0x0200], 256, 0); |
| 410 | if (err) |
| 411 | return err; |
| 412 | |
| 413 | err = floppy_write_sector(floppy, 0, 0, 4, 0, &header[0x0300], 256, 0); |
| 414 | if (err) |
| 415 | return err; |
| 416 | |
| 417 | return FLOPPY_ERROR_SUCCESS; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | |
| 422 | static FLOPPY_IDENTIFY(coco_os9_identify) |
| 423 | { |
| 424 | struct basicdsk_geometry geometry; |
| 425 | *vote = coco_os9_readheader(floppy, &geometry) ? 0 : 100; |
| 426 | return FLOPPY_ERROR_SUCCESS; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | |
| 431 | static FLOPPY_CONSTRUCT(coco_os9_construct) |
| 432 | { |
| 433 | floperr_t err; |
| 434 | struct basicdsk_geometry geometry; |
| 435 | |
| 436 | if (params) |
| 437 | { |
| 438 | /* create */ |
| 439 | memset(&geometry, 0, sizeof(geometry)); |
| 440 | geometry.heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 441 | geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 442 | geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 443 | geometry.first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID); |
| 444 | geometry.sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | /* open */ |
| 449 | err = coco_os9_readheader(floppy, &geometry); |
| 450 | if (err) |
| 451 | return err; |
| 452 | } |
| 453 | |
| 454 | /* actually construct the image */ |
| 455 | err = basicdsk_construct(floppy, &geometry); |
| 456 | floppy_callbacks(floppy)->post_format = coco_os9_post_format; |
| 457 | return err; |
| 458 | } |
| 459 | |
| 460 | |
| 461 | |
| 462 | /* ----------------------------------------------------------------------- |
| 463 | * VDK file format |
| 464 | * |
| 465 | * Used by Paul Burgin's PC-Dragon emulator |
| 466 | * |
| 467 | * Offset Bytes Field Description |
| 468 | * ------ ----- ------------ ----------- |
| 469 | * 0 1 magic1 Signature byte 1 ('d') |
| 470 | * 1 1 magic2 Signature byte 2 ('k') |
| 471 | * 2 2 header_len Total header length (little endian) |
| 472 | * 4 1 ver_actual Version of the VDK format (0x10) |
| 473 | * 5 1 ver_compat Backwards compatibility version (0x10) |
| 474 | * 6 1 source_id Identify of the file source |
| 475 | * 7 1 source_ver Version of the file source |
| 476 | * 8 1 tracks Number of tracks |
| 477 | * 9 1 sides Number of sides (1-2) |
| 478 | * 10 1 flags Various flags |
| 479 | * bit 0: Write protect |
| 480 | * bit 1: A Lock |
| 481 | * bit 2: F Lock |
| 482 | * bit 3: Disk set |
| 483 | * 11 1 compression Compression flags (bits 0-2) and name length |
| 484 | * ----------------------------------------------------------------------- */ |
| 485 | |
| 486 | |
| 487 | |
| 488 | static int coco_vdk_decode_header(floppy_image *floppy, struct basicdsk_geometry *geometry) |
| 489 | { |
| 490 | UINT8 header[12]; |
| 491 | int heads, tracks, sectors, sector_length, offset; |
| 492 | UINT64 size; |
| 493 | |
| 494 | size = floppy_image_size(floppy); |
| 495 | |
| 496 | floppy_image_read(floppy, header, 0, sizeof(header)); |
| 497 | |
| 498 | if (header[0] != 'd') |
| 499 | return -1; |
| 500 | if (header[1] != 'k') |
| 501 | return -1; |
| 502 | if (header[5] != 0x10) |
| 503 | return -1; |
| 504 | if (header[11] & 0x07) |
| 505 | return -1; |
| 506 | |
| 507 | heads = header[9]; |
| 508 | tracks = header[8]; |
| 509 | sectors = 18; |
| 510 | sector_length = 0x100; |
| 511 | |
| 512 | offset = ((int) header[3]) * 0x100 + header[2]; |
| 513 | |
| 514 | if (size != (heads * tracks * sectors * sector_length + offset)) |
| 515 | return -1; |
| 516 | |
| 517 | if (geometry) |
| 518 | { |
| 519 | memset(geometry, 0, sizeof(*geometry)); |
| 520 | geometry->heads = heads; |
| 521 | geometry->tracks = tracks; |
| 522 | geometry->sectors = sectors; |
| 523 | geometry->first_sector_id = 1; |
| 524 | geometry->sector_length = sector_length; |
| 525 | geometry->offset = offset; |
| 526 | } |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | |
| 531 | |
| 532 | static FLOPPY_IDENTIFY(coco_vdk_identify) |
| 533 | { |
| 534 | *vote = coco_vdk_decode_header(floppy, NULL) ? 0 : 100; |
| 535 | return FLOPPY_ERROR_SUCCESS; |
| 536 | } |
| 537 | |
| 538 | |
| 539 | |
| 540 | static FLOPPY_CONSTRUCT(coco_vdk_construct) |
| 541 | { |
| 542 | struct basicdsk_geometry geometry; |
| 543 | UINT8 header[12]; |
| 544 | |
| 545 | if (params) |
| 546 | { |
| 547 | /* create */ |
| 548 | memset(&geometry, 0, sizeof(geometry)); |
| 549 | geometry.heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 550 | geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 551 | geometry.sectors = 18; |
| 552 | geometry.first_sector_id = 1; |
| 553 | geometry.sector_length = 256; |
| 554 | geometry.offset = sizeof(header); |
| 555 | |
| 556 | memset(&header, 0, sizeof(header)); |
| 557 | header[0] = 'd'; |
| 558 | header[1] = 'k'; |
| 559 | header[2] = (UINT8) (sizeof(header) >> 0); |
| 560 | header[3] = (UINT8) (sizeof(header) >> 8); |
| 561 | header[4] = 0x10; |
| 562 | header[5] = 0x10; |
| 563 | header[8] = (UINT8) geometry.tracks; |
| 564 | header[9] = (UINT8) geometry.heads; |
| 565 | |
| 566 | floppy_image_write(floppy, header, 0, sizeof(header)); |
| 567 | } |
| 568 | else |
| 569 | { |
| 570 | /* load */ |
| 571 | if (coco_vdk_decode_header(floppy, &geometry)) |
| 572 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 573 | } |
| 574 | return basicdsk_construct(floppy, &geometry); |
| 575 | } |
| 576 | |
| 577 | |
| 578 | |
| 579 | /* ----------------------------------------------------------------------- |
| 580 | * DMK file format |
| 581 | * |
| 582 | * David M. Keil's disk image format is aptly called an 'on disk' image |
| 583 | * format. This means that whatever written to the disk is enocded into |
| 584 | * the image file. IDAMS, sector headers, traling CRCs, and intra sector |
| 585 | * spacing. * |
| 586 | * |
| 587 | * HEADER DESCRIPTION: |
| 588 | * |
| 589 | * Offset Bytes Field Description |
| 590 | * ------ ----- ------------ ----------- |
| 591 | * 0 1 write_prot 0xff = Writed Protected, 0x00 = R/W |
| 592 | * 1 1 tracks Number of tracks |
| 593 | * 2 2 track_length Bytes per track (little endian) |
| 594 | * 4 1 disk_options Miscellaneous flags |
| 595 | * bit 0-3: Unused |
| 596 | * bit 4: 1=single sided 0=dbl |
| 597 | * bit 5: Unused |
| 598 | * bit 6: Single density? |
| 599 | * bit 6: Ignore density flags? |
| 600 | * 5 7 reserved Reserved for future use |
| 601 | * 12 4 real_disk_code If this is 0x12345678 (little endian) |
| 602 | * then access a real disk drive |
| 603 | * (unsupported) |
| 604 | * |
| 605 | * Each track begins with a track TOC, consisting of 64 little endian 16-bit |
| 606 | * integers. Each integer has the following format: |
| 607 | * bit 0-13: Offset from begining of track to 'FE' byte of IDAM |
| 608 | * Note these are always sorted from first to last. All empty |
| 609 | * entries are 0x00 |
| 610 | * bit 14: Undefined (reserved) |
| 611 | * bit 15: Sector double density (0=SD 1=DD) |
| 612 | * ----------------------------------------------------------------------- */ |
| 613 | |
| 614 | struct dmk_tag |
| 615 | { |
| 616 | int heads; |
| 617 | int tracks; |
| 618 | UINT32 track_size; |
| 619 | }; |
| 620 | |
| 621 | #define DMK_HEADER_LEN 16 |
| 622 | #define DMK_TOC_LEN 64 |
| 623 | #define DMK_IDAM_LENGTH 7 |
| 624 | #define DMK_DATA_GAP 80 |
| 625 | #define DMK_LEAD_IN 32 |
| 626 | #define DMK_EXTRA_TRACK_LENGTH 156 |
| 627 | |
| 628 | #define dmk_idam_type(x) (x)[0] |
| 629 | #define dmk_idam_track(x) (x)[1] |
| 630 | #define dmk_idam_side(x) (x)[2] |
| 631 | #define dmk_idam_sector(x) (x)[3] |
| 632 | #define dmk_idam_sectorlength(x) (x)[4] |
| 633 | #define dmk_idam_crc(x) (((x)[5] << 8) + (x)[6]) |
| 634 | #define dmk_idam_set_crc(x, crc) (x)[5] = ((crc) >> 8); (x)[6] = ((crc) >> 0); |
| 635 | |
| 636 | |
| 637 | static struct dmk_tag *get_dmk_tag(floppy_image *floppy) |
| 638 | { |
| 639 | return (dmk_tag *)floppy_tag(floppy); |
| 640 | } |
| 641 | |
| 642 | |
| 643 | static floperr_t coco_dmk_get_offset(floppy_image *floppy, int head, int track, UINT64 *offset) |
| 644 | { |
| 645 | struct dmk_tag *tag = get_dmk_tag(floppy); |
| 646 | |
| 647 | if ((head < 0) || (head >= tag->heads) || (track < 0) || (track >= tag->tracks)) |
| 648 | return FLOPPY_ERROR_SEEKERROR; |
| 649 | |
| 650 | *offset = track; |
| 651 | *offset *= tag->heads; |
| 652 | *offset += head; |
| 653 | *offset *= tag->track_size; |
| 654 | *offset += DMK_HEADER_LEN; |
| 655 | return FLOPPY_ERROR_SUCCESS; |
| 656 | } |
| 657 | |
| 658 | |
| 659 | |
| 660 | static UINT32 coco_dmk_min_track_size(int sectors, int sector_length) |
| 661 | { |
| 662 | int sector_physical_length; |
| 663 | sector_physical_length = 8 + 3 + DMK_IDAM_LENGTH + 22 + 12 + 3 + 1 + sector_length + 2 + 24; |
| 664 | return DMK_TOC_LEN * 2 + DMK_LEAD_IN + (sectors * sector_physical_length); |
| 665 | } |
| 666 | |
| 667 | |
| 668 | |
| 669 | static floperr_t coco_dmk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 670 | { |
| 671 | floperr_t err; |
| 672 | UINT64 track_offset; |
| 673 | |
| 674 | err = coco_dmk_get_offset(floppy, head, track, &track_offset); |
| 675 | if (err) |
| 676 | return err; |
| 677 | |
| 678 | floppy_image_read(floppy, buffer, offset + track_offset, buflen); |
| 679 | return FLOPPY_ERROR_SUCCESS; |
| 680 | } |
| 681 | |
| 682 | |
| 683 | |
| 684 | static floperr_t coco_dmk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 685 | { |
| 686 | floperr_t err; |
| 687 | UINT64 track_offset; |
| 688 | |
| 689 | err = coco_dmk_get_offset(floppy, head, track, &track_offset); |
| 690 | if (err) |
| 691 | return err; |
| 692 | |
| 693 | floppy_image_write(floppy, buffer, offset + track_offset, buflen); |
| 694 | return FLOPPY_ERROR_SUCCESS; |
| 695 | } |
| 696 | |
| 697 | |
| 698 | |
| 699 | static floperr_t coco_dmk_get_track_data_offset(floppy_image *floppy, int head, int track, UINT64 *offset) |
| 700 | { |
| 701 | *offset = DMK_TOC_LEN + 1; |
| 702 | return FLOPPY_ERROR_SUCCESS; |
| 703 | } |
| 704 | |
| 705 | |
| 706 | |
| 707 | static floperr_t coco_dmk_format_track(floppy_image *floppy, int head, int track, option_resolution *params) |
| 708 | { |
| 709 | int sectors; |
| 710 | int sector_length; |
| 711 | int interleave; |
| 712 | int first_sector_id; |
| 713 | floperr_t err; |
| 714 | int physical_sector; |
| 715 | int logical_sector; |
| 716 | int track_position; |
| 717 | UINT16 idam_offset; |
| 718 | UINT16 crc; |
| 719 | UINT8 *track_data; |
| 720 | void *track_data_v; |
| 721 | UINT32 max_track_size; |
| 722 | int *sector_map = NULL; |
| 723 | |
| 724 | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 725 | sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 726 | interleave = option_resolution_lookup_int(params, PARAM_INTERLEAVE); |
| 727 | first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID); |
| 728 | |
| 729 | max_track_size = get_dmk_tag(floppy)->track_size; |
| 730 | |
| 731 | if (sectors > DMK_TOC_LEN) |
| 732 | { |
| 733 | err = FLOPPY_ERROR_INTERNAL; |
| 734 | goto done; |
| 735 | } |
| 736 | |
| 737 | if (max_track_size < coco_dmk_min_track_size(sectors, sector_length)) |
| 738 | { |
| 739 | err = FLOPPY_ERROR_NOSPACE; |
| 740 | goto done; |
| 741 | } |
| 742 | |
| 743 | err = floppy_load_track(floppy, head, track, TRUE, &track_data_v, NULL); |
| 744 | if (err) |
| 745 | goto done; |
| 746 | track_data = (UINT8 *) track_data_v; |
| 747 | |
| 748 | /* set up sector map */ |
| 749 | sector_map = (int*)malloc(sectors * sizeof(*sector_map)); |
| 750 | if (!sector_map) |
| 751 | { |
| 752 | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 753 | goto done; |
| 754 | } |
| 755 | memset(sector_map, 0xFF, sectors * sizeof(*sector_map)); |
| 756 | |
| 757 | physical_sector = 0; |
| 758 | for (logical_sector = 0; logical_sector < sectors; logical_sector++) |
| 759 | { |
| 760 | while(sector_map[physical_sector] >= 0) |
| 761 | { |
| 762 | physical_sector++; |
| 763 | physical_sector %= sectors; |
| 764 | } |
| 765 | |
| 766 | sector_map[physical_sector] = logical_sector + first_sector_id; |
| 767 | physical_sector += interleave + 1; |
| 768 | physical_sector %= sectors; |
| 769 | } |
| 770 | |
| 771 | /* set up track table of contents */ |
| 772 | physical_sector = 0; |
| 773 | track_position = DMK_TOC_LEN * 2 + DMK_LEAD_IN; |
| 774 | while(physical_sector < DMK_TOC_LEN) |
| 775 | { |
| 776 | if (physical_sector >= sectors) |
| 777 | { |
| 778 | /* no more sectors */ |
| 779 | idam_offset = 0; |
| 780 | } |
| 781 | else |
| 782 | { |
| 783 | /* this is a sector */ |
| 784 | logical_sector = sector_map[physical_sector]; |
| 785 | |
| 786 | /* write the sector */ |
| 787 | memset(&track_data[track_position], 0x00, 8); |
| 788 | track_position += 8; |
| 789 | |
| 790 | memset(&track_data[track_position], 0xA1, 3); |
| 791 | track_position += 3; |
| 792 | |
| 793 | idam_offset = track_position | 0x8000; |
| 794 | dmk_idam_type( &track_data[track_position]) = 0xFE; |
| 795 | dmk_idam_track( &track_data[track_position]) = track; |
| 796 | dmk_idam_side( &track_data[track_position]) = head; |
| 797 | dmk_idam_sector( &track_data[track_position]) = logical_sector; |
| 798 | dmk_idam_sectorlength( &track_data[track_position]) = compute_log2(sector_length / 128); |
| 799 | crc = ccitt_crc16(0xcdb4, &track_data[track_position], DMK_IDAM_LENGTH - 2); |
| 800 | dmk_idam_set_crc( &track_data[track_position], crc); |
| 801 | track_position += DMK_IDAM_LENGTH; |
| 802 | |
| 803 | memset(&track_data[track_position], 0x4E, 22); |
| 804 | track_position += 22; |
| 805 | |
| 806 | memset(&track_data[track_position], 0x00, 12); |
| 807 | track_position += 12; |
| 808 | |
| 809 | memset(&track_data[track_position], 0xA1, 3); |
| 810 | track_position += 3; |
| 811 | |
| 812 | /* write sector body */ |
| 813 | track_data[track_position] = 0xFB; |
| 814 | memset(&track_data[track_position + 1], floppy_get_filler(floppy), sector_length); |
| 815 | crc = ccitt_crc16(0xcdb4, &track_data[track_position], sector_length + 1); |
| 816 | track_data[track_position + sector_length + 1] = (UINT8) (crc >> 8); |
| 817 | track_data[track_position + sector_length + 2] = (UINT8) (crc >> 0); |
| 818 | track_position += sector_length + 3; |
| 819 | |
| 820 | /* write sector footer */ |
| 821 | memset(&track_data[track_position], 0x4E, 24); |
| 822 | track_position += 24; |
| 823 | } |
| 824 | |
| 825 | /* write the TOC entry */ |
| 826 | track_data[physical_sector * 2 + 0] = (UINT8) (idam_offset >> 0); |
| 827 | track_data[physical_sector * 2 + 1] = (UINT8) (idam_offset >> 8); |
| 828 | |
| 829 | physical_sector++; |
| 830 | } |
| 831 | |
| 832 | /* write track lead in */ |
| 833 | memset(&track_data[physical_sector * 2], 0x4e, DMK_LEAD_IN); |
| 834 | |
| 835 | /* write track footer */ |
| 836 | assert(max_track_size >= (UINT32)track_position); |
| 837 | memset(&track_data[track_position], 0x4e, max_track_size - track_position); |
| 838 | |
| 839 | done: |
| 840 | if (sector_map) |
| 841 | free(sector_map); |
| 842 | return FLOPPY_ERROR_SUCCESS; |
| 843 | } |
| 844 | |
| 845 | |
| 846 | |
| 847 | static int coco_dmk_get_heads_per_disk(floppy_image *floppy) |
| 848 | { |
| 849 | return get_dmk_tag(floppy)->heads; |
| 850 | } |
| 851 | |
| 852 | |
| 853 | |
| 854 | static int coco_dmk_get_tracks_per_disk(floppy_image *floppy) |
| 855 | { |
| 856 | return get_dmk_tag(floppy)->tracks; |
| 857 | } |
| 858 | |
| 859 | |
| 860 | |
| 861 | static UINT32 coco_dmk_get_track_size(floppy_image *floppy, int head, int track) |
| 862 | { |
| 863 | return get_dmk_tag(floppy)->track_size; |
| 864 | } |
| 865 | |
| 866 | |
| 867 | |
| 868 | static floperr_t coco_dmk_seek_sector_in_track(floppy_image *floppy, int head, int track, int sector, int sector_is_index, int dirtify, UINT8 **sector_data, UINT32 *sector_length) |
| 869 | { |
| 870 | struct dmk_tag *tag = get_dmk_tag(floppy); |
| 871 | floperr_t err; |
| 872 | UINT32 idam_offset = 0; |
| 873 | UINT16 calculated_crc; |
| 874 | size_t i; |
| 875 | size_t offs; |
| 876 | int state; |
| 877 | UINT8 *track_data; |
| 878 | void *track_data_v; |
| 879 | size_t track_length; |
| 880 | size_t sec_len; |
| 881 | |
| 882 | err = floppy_load_track(floppy, head, track, dirtify, &track_data_v, &track_length); |
| 883 | if (err) |
| 884 | return err; |
| 885 | track_data = (UINT8 *) track_data_v; |
| 886 | |
| 887 | /* search for matching IDAM */ |
| 888 | for (i = 0; i < DMK_TOC_LEN; i++) |
| 889 | { |
| 890 | idam_offset = track_data[i * 2 + 1]; |
| 891 | idam_offset <<= 8; |
| 892 | idam_offset |= track_data[i * 2 + 0]; |
| 893 | idam_offset &= 0x3FFF; |
| 894 | |
| 895 | if (idam_offset == 0) |
| 896 | { |
| 897 | /* we've reached the end of the road */ |
| 898 | i = DMK_TOC_LEN; |
| 899 | break; |
| 900 | } |
| 901 | |
| 902 | if ((idam_offset + DMK_IDAM_LENGTH) >= tag->track_size) |
| 903 | continue; |
| 904 | |
| 905 | calculated_crc = ccitt_crc16(0xCDB4, &track_data[idam_offset], DMK_IDAM_LENGTH - 2); |
| 906 | |
| 907 | if (calculated_crc == dmk_idam_crc(&track_data[idam_offset])) |
| 908 | { |
| 909 | if (sector_is_index) |
| 910 | { |
| 911 | /* the sector is indexed; decrement the index and go */ |
| 912 | if (sector-- == 0) |
| 913 | break; |
| 914 | } |
| 915 | else |
| 916 | { |
| 917 | /* check IDAM integrity and check for matching sector */ |
| 918 | if ((sector == dmk_idam_sector(&track_data[idam_offset])) |
| 919 | /* && (track == dmk_idam_track(&track_data[idam_offset])) */ |
| 920 | /* && (head == dmk_idam_side(&track_data[idam_offset])) */ |
| 921 | ) |
| 922 | break; |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | if (i >= DMK_TOC_LEN) |
| 928 | return FLOPPY_ERROR_SEEKERROR; |
| 929 | |
| 930 | /* we found a matching sector ID */ |
| 931 | state = 0; |
| 932 | offs = idam_offset + DMK_IDAM_LENGTH; |
| 933 | |
| 934 | /* find pattern 0xA1A1FB; this represents the start of a data sector */ |
| 935 | for (i = 0; i < DMK_DATA_GAP; i++) |
| 936 | { |
| 937 | /* overflowing the track? */ |
| 938 | if ((i + offs) >= tag->track_size) |
| 939 | return FLOPPY_ERROR_SEEKERROR; |
| 940 | |
| 941 | if (track_data[offs + i] == 0xA1) |
| 942 | state++; |
| 943 | else if ((track_data[offs + i] == 0xFB) && state) |
| 944 | break; |
| 945 | else |
| 946 | state = 0; |
| 947 | } |
| 948 | if (i >= DMK_DATA_GAP) |
| 949 | return FLOPPY_ERROR_SEEKERROR; |
| 950 | |
| 951 | offs += i + 1; |
| 952 | sec_len = 128 << dmk_idam_sectorlength(&track_data[idam_offset]); |
| 953 | |
| 954 | if ((offs + sec_len) > track_length) |
| 955 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 956 | |
| 957 | if (sector_data) |
| 958 | *sector_data = track_data + offs; |
| 959 | if (sector_length) |
| 960 | *sector_length = sec_len; |
| 961 | return FLOPPY_ERROR_SUCCESS; |
| 962 | } |
| 963 | |
| 964 | |
| 965 | |
| 966 | static floperr_t coco_dmk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 967 | { |
| 968 | return coco_dmk_seek_sector_in_track(floppy, head, track, sector, FALSE, FALSE, NULL, sector_length); |
| 969 | } |
| 970 | |
| 971 | |
| 972 | |
| 973 | static floperr_t coco_dmk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 974 | { |
| 975 | floperr_t err; |
| 976 | UINT32 idam_offset; |
| 977 | const UINT8 *track_data; |
| 978 | void *track_data_v; |
| 979 | |
| 980 | if (sector_index*2 >= DMK_TOC_LEN) |
| 981 | return FLOPPY_ERROR_SEEKERROR; |
| 982 | |
| 983 | err = floppy_load_track(floppy, head, track, FALSE, &track_data_v, NULL); |
| 984 | if (err) |
| 985 | return err; |
| 986 | track_data = (UINT8 *) track_data_v; |
| 987 | |
| 988 | idam_offset = track_data[sector_index * 2 + 1]; |
| 989 | idam_offset <<= 8; |
| 990 | idam_offset |= track_data[sector_index * 2 + 0]; |
| 991 | idam_offset &= 0x3FFF; |
| 992 | |
| 993 | if (idam_offset == 0) |
| 994 | return FLOPPY_ERROR_SEEKERROR; |
| 995 | |
| 996 | if (cylinder) |
| 997 | *cylinder = dmk_idam_track(&track_data[idam_offset]); |
| 998 | if (side) |
| 999 | *side = dmk_idam_side(&track_data[idam_offset]); |
| 1000 | if (sector) |
| 1001 | *sector = dmk_idam_sector(&track_data[idam_offset]); |
| 1002 | if (sector_length) |
| 1003 | *sector_length = 128 << dmk_idam_sectorlength(&track_data[idam_offset]); |
| 1004 | if (flags) |
| 1005 | /* TODO: read DAM or DDAM and determine flags */ |
| 1006 | *flags = 0; |
| 1007 | return FLOPPY_ERROR_SUCCESS; |
| 1008 | } |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | static floperr_t internal_coco_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen) |
| 1013 | { |
| 1014 | floperr_t err; |
| 1015 | UINT32 sector_length; |
| 1016 | UINT16 crc_on_disk; |
| 1017 | UINT16 calculated_crc; |
| 1018 | UINT8 *sector_data; |
| 1019 | |
| 1020 | err = coco_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, FALSE, §or_data, §or_length); |
| 1021 | if (err) |
| 1022 | return err; |
| 1023 | |
| 1024 | crc_on_disk = sector_data[sector_length + 0]; |
| 1025 | crc_on_disk <<= 8; |
| 1026 | crc_on_disk += sector_data[sector_length + 1]; |
| 1027 | |
| 1028 | calculated_crc = ccitt_crc16(0xE295, sector_data, sector_length); |
| 1029 | if (calculated_crc != crc_on_disk) |
| 1030 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1031 | |
| 1032 | memcpy(buffer, sector_data, MIN(sector_length, buflen)); |
| 1033 | |
| 1034 | return FLOPPY_ERROR_SUCCESS; |
| 1035 | } |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | static floperr_t internal_coco_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam) |
| 1040 | { |
| 1041 | floperr_t err; |
| 1042 | UINT32 sector_length; |
| 1043 | UINT8 *sector_data; |
| 1044 | UINT16 crc; |
| 1045 | |
| 1046 | err = coco_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, TRUE, §or_data, §or_length); |
| 1047 | if (err) |
| 1048 | return err; |
| 1049 | |
| 1050 | if (buflen > sector_length) |
| 1051 | return FLOPPY_ERROR_INTERNAL; |
| 1052 | |
| 1053 | memcpy(sector_data, buffer, buflen); |
| 1054 | |
| 1055 | crc = ccitt_crc16(0xE295, sector_data, sector_length); |
| 1056 | sector_data[sector_length + 0] = crc >> 8; |
| 1057 | sector_data[sector_length + 1] = crc >> 0; |
| 1058 | return FLOPPY_ERROR_SUCCESS; |
| 1059 | } |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | static floperr_t coco_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 1064 | { |
| 1065 | return internal_coco_dmk_read_sector(floppy, head, track, sector, FALSE, buffer, buflen); |
| 1066 | } |
| 1067 | |
| 1068 | static floperr_t coco_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 1069 | { |
| 1070 | return internal_coco_dmk_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam); |
| 1071 | } |
| 1072 | |
| 1073 | static floperr_t coco_dmk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 1074 | { |
| 1075 | return internal_coco_dmk_read_sector(floppy, head, track, sector, TRUE, buffer, buflen); |
| 1076 | } |
| 1077 | |
| 1078 | static floperr_t coco_dmk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 1079 | { |
| 1080 | return internal_coco_dmk_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam); |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | static void coco_dmk_interpret_header(floppy_image *floppy, int *heads, int *tracks, int *track_size) |
| 1086 | { |
| 1087 | UINT8 header[DMK_HEADER_LEN]; |
| 1088 | |
| 1089 | floppy_image_read(floppy, header, 0, DMK_HEADER_LEN); |
| 1090 | |
| 1091 | if (tracks) |
| 1092 | *tracks = header[1]; |
| 1093 | if (heads) |
| 1094 | *heads = (header[4] & 0x10) ? 1 : 2; |
| 1095 | if (track_size) |
| 1096 | *track_size = ((int) header[3]) * 0x100 + header[2]; |
| 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | |
| 1101 | FLOPPY_CONSTRUCT(coco_dmk_construct) |
| 1102 | { |
| 1103 | struct FloppyCallbacks *callbacks; |
| 1104 | struct dmk_tag *tag; |
| 1105 | UINT8 header[DMK_HEADER_LEN]; |
| 1106 | int heads, tracks, track_size, sectors, sector_length; |
| 1107 | |
| 1108 | if (params) |
| 1109 | { |
| 1110 | heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 1111 | tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 1112 | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 1113 | sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 1114 | |
| 1115 | track_size = coco_dmk_min_track_size(sectors, sector_length) + DMK_EXTRA_TRACK_LENGTH; |
| 1116 | |
| 1117 | memset(header, 0, sizeof(header)); |
| 1118 | header[1] = tracks; |
| 1119 | header[2] = track_size >> 0; |
| 1120 | header[3] = track_size >> 8; |
| 1121 | header[4] = (heads == 2) ? 0x00 : 0x10; |
| 1122 | |
| 1123 | floppy_image_write(floppy, header, 0, sizeof(header)); |
| 1124 | } |
| 1125 | else |
| 1126 | { |
| 1127 | coco_dmk_interpret_header(floppy, &heads, &tracks, &track_size); |
| 1128 | } |
| 1129 | |
| 1130 | tag = (dmk_tag *)floppy_create_tag(floppy, sizeof(struct dmk_tag)); |
| 1131 | if (!tag) |
| 1132 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 1133 | tag->heads = heads; |
| 1134 | tag->track_size = track_size; |
| 1135 | tag->tracks = tracks; |
| 1136 | |
| 1137 | callbacks = floppy_callbacks(floppy); |
| 1138 | callbacks->read_track = coco_dmk_read_track; |
| 1139 | callbacks->write_track = coco_dmk_write_track; |
| 1140 | callbacks->get_track_data_offset = coco_dmk_get_track_data_offset; |
| 1141 | callbacks->format_track = coco_dmk_format_track; |
| 1142 | callbacks->get_heads_per_disk = coco_dmk_get_heads_per_disk; |
| 1143 | callbacks->get_tracks_per_disk = coco_dmk_get_tracks_per_disk; |
| 1144 | callbacks->get_track_size = coco_dmk_get_track_size; |
| 1145 | callbacks->get_sector_length = coco_dmk_get_sector_length; |
| 1146 | callbacks->get_indexed_sector_info = coco_dmk_get_indexed_sector_info; |
| 1147 | callbacks->read_sector = coco_dmk_read_sector; |
| 1148 | callbacks->write_sector = coco_dmk_write_sector; |
| 1149 | callbacks->read_indexed_sector = coco_dmk_read_indexed_sector; |
| 1150 | callbacks->write_indexed_sector = coco_dmk_write_indexed_sector; |
| 1151 | |
| 1152 | return FLOPPY_ERROR_SUCCESS; |
| 1153 | } |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | FLOPPY_IDENTIFY(coco_dmk_identify) |
| 1158 | { |
| 1159 | int heads, tracks, track_size; |
| 1160 | UINT64 size, expected_size; |
| 1161 | |
| 1162 | size = floppy_image_size(floppy); |
| 1163 | coco_dmk_interpret_header(floppy, &heads, &tracks, &track_size); |
| 1164 | expected_size = DMK_HEADER_LEN + (heads * tracks * track_size); |
| 1165 | *vote = (size == expected_size) ? 100 : 0; |
| 1166 | return FLOPPY_ERROR_SUCCESS; |
| 1167 | } |
| 1168 | |
| 1169 | |
| 1170 | |
| 1171 | /* ----------------------------------------------------------------------- */ |
| 1172 | |
| 1173 | FLOPPY_OPTIONS_START( coco ) |
| 1174 | FLOPPY_OPTION( coco_jvc, "dsk", "CoCo JVC disk image", coco_jvc_identify, coco_jvc_construct, NULL, |
| 1175 | HEADS([1]-2) |
| 1176 | TRACKS([35]-255) |
| 1177 | SECTORS(1-[18]-255) |
| 1178 | SECTOR_LENGTH(128/[256]/512/1024) |
| 1179 | FIRST_SECTOR_ID(0-[1])) |
| 1180 | FLOPPY_OPTION( coco_os9, "os9", "CoCo OS-9 disk image", coco_os9_identify, coco_os9_construct, NULL, |
| 1181 | HEADS([1]-2) |
| 1182 | TRACKS([35]-255) |
| 1183 | SECTORS(1-[18]-255) |
| 1184 | SECTOR_LENGTH([256]) |
| 1185 | FIRST_SECTOR_ID([1])) |
| 1186 | FLOPPY_OPTION( coco_vdk, "vdk", "CoCo VDK disk image", coco_vdk_identify, coco_vdk_construct, NULL, |
| 1187 | HEADS([1]-2) |
| 1188 | TRACKS([35]-255) |
| 1189 | SECTORS([18]) |
| 1190 | SECTOR_LENGTH([256]) |
| 1191 | FIRST_SECTOR_ID([1])) |
| 1192 | FLOPPY_OPTION( coco_dmk, "dsk,dmk", "CoCo DMK disk image", coco_dmk_identify, coco_dmk_construct, NULL, |
| 1193 | HEADS([1]-2) |
| 1194 | TRACKS([35]-255) |
| 1195 | SECTORS(1-[18]) |
| 1196 | SECTOR_LENGTH(128/[256]/512/1024/2048/4096/8192) |
| 1197 | INTERLEAVE(0-[6]-17) |
| 1198 | FIRST_SECTOR_ID(0-[1])) |
| 1199 | FLOPPY_OPTIONS_END |
src/lib/formats/flopimg.c
| r0 | r11458 | |
| 1 | /********************************************************************* |
| 2 | |
| 3 | flopimg.c |
| 4 | |
| 5 | Floppy disk image abstraction code |
| 6 | |
| 7 | *********************************************************************/ |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <stdio.h> |
| 12 | #include <ctype.h> |
| 13 | #include <assert.h> |
| 14 | #include <limits.h> |
| 15 | |
| 16 | #include "osdcore.h" |
| 17 | #include "ioprocs.h" |
| 18 | #include "flopimg.h" |
| 19 | #include "pool.h" |
| 20 | #include "imageutl.h" |
| 21 | |
| 22 | #define TRACK_LOADED 0x01 |
| 23 | #define TRACK_DIRTY 0x02 |
| 24 | |
| 25 | |
| 26 | struct _floppy_image |
| 27 | { |
| 28 | struct io_generic io; |
| 29 | |
| 30 | const struct FloppyFormat *floppy_option; |
| 31 | struct FloppyCallbacks format; |
| 32 | |
| 33 | /* loaded track stuff */ |
| 34 | int loaded_track_head; |
| 35 | int loaded_track_index; |
| 36 | UINT32 loaded_track_size; |
| 37 | void *loaded_track_data; |
| 38 | UINT8 loaded_track_status; |
| 39 | UINT8 flags; |
| 40 | |
| 41 | /* tagging system */ |
| 42 | object_pool *tags; |
| 43 | void *tag_data; |
| 44 | }; |
| 45 | |
| 46 | |
| 47 | |
| 48 | struct _floppy_params |
| 49 | { |
| 50 | int param; |
| 51 | int value; |
| 52 | }; |
| 53 | |
| 54 | |
| 55 | |
| 56 | static floperr_t floppy_track_unload(floppy_image *floppy); |
| 57 | |
| 58 | OPTION_GUIDE_START(floppy_option_guide) |
| 59 | OPTION_INT('H', "heads", "Heads") |
| 60 | OPTION_INT('T', "tracks", "Tracks") |
| 61 | OPTION_INT('S', "sectors", "Sectors") |
| 62 | OPTION_INT('L', "sectorlength", "Sector Bytes") |
| 63 | OPTION_INT('I', "interleave", "Interleave") |
| 64 | OPTION_INT('F', "firstsectorid", "First Sector") |
| 65 | OPTION_GUIDE_END |
| 66 | |
| 67 | |
| 68 | static void floppy_close_internal(floppy_image *floppy, int close_file); |
| 69 | |
| 70 | /********************************************************************* |
| 71 | opening, closing and creating of floppy images |
| 72 | *********************************************************************/ |
| 73 | |
| 74 | /* basic floppy_image initialization common to floppy_open() and floppy_create() */ |
| 75 | static floppy_image *floppy_init(void *fp, const struct io_procs *procs, int flags) |
| 76 | { |
| 77 | floppy_image *floppy; |
| 78 | |
| 79 | floppy = (floppy_image *)malloc(sizeof(struct _floppy_image)); |
| 80 | if (!floppy) |
| 81 | return NULL; |
| 82 | |
| 83 | memset(floppy, 0, sizeof(*floppy)); |
| 84 | floppy->tags = pool_alloc_lib(NULL); |
| 85 | floppy->tag_data = NULL; |
| 86 | floppy->io.file = fp; |
| 87 | floppy->io.procs = procs; |
| 88 | floppy->io.filler = 0xFF; |
| 89 | floppy->flags = (UINT8) flags; |
| 90 | return floppy; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | |
| 95 | /* main code for identifying and maybe opening a disk image; not exposed |
| 96 | * directly because this function is big and hideous */ |
| 97 | static floperr_t floppy_open_internal(void *fp, const struct io_procs *procs, const char *extension, |
| 98 | const struct FloppyFormat *floppy_options, int max_options, int flags, floppy_image **outfloppy, |
| 99 | int *outoption) |
| 100 | { |
| 101 | floperr_t err; |
| 102 | floppy_image *floppy; |
| 103 | int best_option = -1; |
| 104 | int best_vote = 0; |
| 105 | int vote; |
| 106 | size_t i; |
| 107 | |
| 108 | floppy = floppy_init(fp, procs, flags); |
| 109 | if (!floppy) |
| 110 | { |
| 111 | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 112 | goto done; |
| 113 | } |
| 114 | |
| 115 | /* vote on the best format */ |
| 116 | for (i = 0; (i < max_options) && floppy_options[i].construct; i++) |
| 117 | { |
| 118 | if (!extension || !floppy_options[i].extensions || image_find_extension(floppy_options[i].extensions, extension)) |
| 119 | { |
| 120 | if (floppy_options[i].identify) |
| 121 | { |
| 122 | vote = 0; |
| 123 | err = floppy_options[i].identify(floppy, &floppy_options[i], &vote); |
| 124 | if (err) |
| 125 | goto done; |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | vote = 1; |
| 130 | } |
| 131 | |
| 132 | /* is this option a better one? */ |
| 133 | if (vote > best_vote) |
| 134 | { |
| 135 | best_vote = vote; |
| 136 | best_option = i; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /* did we find a format? */ |
| 142 | if (best_option == -1) |
| 143 | { |
| 144 | err = FLOPPY_ERROR_INVALIDIMAGE; |
| 145 | goto done; |
| 146 | } |
| 147 | |
| 148 | if (outfloppy) |
| 149 | { |
| 150 | /* call the format constructor */ |
| 151 | err = floppy_options[best_option].construct(floppy, &floppy_options[best_option], NULL); |
| 152 | if (err) |
| 153 | goto done; |
| 154 | |
| 155 | floppy->floppy_option = &floppy_options[best_option]; |
| 156 | } |
| 157 | |
| 158 | err = FLOPPY_ERROR_SUCCESS; |
| 159 | |
| 160 | done: |
| 161 | /* if we have a floppy disk and we either errored or are not keeping it, close it */ |
| 162 | if (floppy && (!outfloppy || err)) |
| 163 | { |
| 164 | floppy_close_internal(floppy, FALSE); |
| 165 | floppy = NULL; |
| 166 | } |
| 167 | |
| 168 | if (outoption) |
| 169 | *outoption = err ? -1 : best_option; |
| 170 | if (outfloppy) |
| 171 | *outfloppy = floppy; |
| 172 | return err; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
| 177 | floperr_t floppy_identify(void *fp, const struct io_procs *procs, const char *extension, |
| 178 | const struct FloppyFormat *formats, int *identified_format) |
| 179 | { |
| 180 | return floppy_open_internal(fp, procs, extension, formats, INT_MAX, FLOPPY_FLAGS_READONLY, NULL, identified_format); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | |
| 185 | floperr_t floppy_open(void *fp, const struct io_procs *procs, const char *extension, |
| 186 | const struct FloppyFormat *format, int flags, floppy_image **outfloppy) |
| 187 | { |
| 188 | return floppy_open_internal(fp, procs, extension, format, 1, flags, outfloppy, NULL); |
| 189 | } |
| 190 | |
| 191 | |
| 192 | |
| 193 | floperr_t floppy_open_choices(void *fp, const struct io_procs *procs, const char *extension, |
| 194 | const struct FloppyFormat *formats, int flags, floppy_image **outfloppy) |
| 195 | { |
| 196 | return floppy_open_internal(fp, procs, extension, formats, INT_MAX, flags, outfloppy, NULL); |
| 197 | } |
| 198 | |
| 199 | |
| 200 | |
| 201 | static floperr_t option_to_floppy_error(optreserr_t oerr) |
| 202 | { |
| 203 | floperr_t err; |
| 204 | switch(oerr) { |
| 205 | case OPTIONRESOLUTION_ERROR_SUCCESS: |
| 206 | err = FLOPPY_ERROR_SUCCESS; |
| 207 | break; |
| 208 | case OPTIONRESOLUTION_ERROR_OUTOFMEMORY: |
| 209 | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 210 | break; |
| 211 | case OPTIONRESOLUTION_ERROR_PARAMOUTOFRANGE: |
| 212 | case OPTIONRESOLUTION_ERROR_PARAMNOTSPECIFIED: |
| 213 | case OPTIONRESOLUTION_ERROR_PARAMNOTFOUND: |
| 214 | case OPTIONRESOLUTION_ERROR_PARAMALREADYSPECIFIED: |
| 215 | case OPTIONRESOLUTION_ERROR_BADPARAM: |
| 216 | case OPTIONRESOLUTION_ERROR_SYNTAX: |
| 217 | default: |
| 218 | err = FLOPPY_ERROR_INTERNAL; |
| 219 | break; |
| 220 | }; |
| 221 | return err; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | floperr_t floppy_create(void *fp, const struct io_procs *procs, const struct FloppyFormat *format, option_resolution *parameters, floppy_image **outfloppy) |
| 227 | { |
| 228 | floppy_image *floppy = NULL; |
| 229 | optreserr_t oerr; |
| 230 | floperr_t err; |
| 231 | int heads, tracks, h, t; |
| 232 | option_resolution *alloc_resolution = NULL; |
| 233 | |
| 234 | assert(format); |
| 235 | |
| 236 | /* create the new image */ |
| 237 | floppy = floppy_init(fp, procs, 0); |
| 238 | if (!floppy) |
| 239 | { |
| 240 | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 241 | goto done; |
| 242 | } |
| 243 | |
| 244 | /* if this format expects creation parameters and none were specified, create some */ |
| 245 | if (!parameters && format->param_guidelines) |
| 246 | { |
| 247 | alloc_resolution = option_resolution_create(floppy_option_guide, format->param_guidelines); |
| 248 | if (!alloc_resolution) |
| 249 | { |
| 250 | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 251 | goto done; |
| 252 | } |
| 253 | parameters = alloc_resolution; |
| 254 | } |
| 255 | |
| 256 | /* finish the parameters, if specified */ |
| 257 | if (parameters) |
| 258 | { |
| 259 | oerr = option_resolution_finish(parameters); |
| 260 | if (oerr) |
| 261 | { |
| 262 | err = option_to_floppy_error(oerr); |
| 263 | goto done; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* call the format constructor */ |
| 268 | err = format->construct(floppy, format, parameters); |
| 269 | if (err) |
| 270 | goto done; |
| 271 | |
| 272 | /* format the disk, ignoring if formatting not implemented */ |
| 273 | if (floppy->format.format_track) |
| 274 | { |
| 275 | heads = floppy_get_heads_per_disk(floppy); |
| 276 | tracks = floppy_get_tracks_per_disk(floppy); |
| 277 | |
| 278 | for (h = 0; h < heads; h++) |
| 279 | { |
| 280 | for (t = 0; t < tracks; t++) |
| 281 | { |
| 282 | err = floppy->format.format_track(floppy, h, t, parameters); |
| 283 | if (err) |
| 284 | goto done; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /* call the post_format function, if present */ |
| 290 | if (floppy->format.post_format) |
| 291 | { |
| 292 | err = floppy->format.post_format(floppy, parameters); |
| 293 | if (err) |
| 294 | goto done; |
| 295 | } |
| 296 | |
| 297 | floppy->floppy_option = format; |
| 298 | err = FLOPPY_ERROR_SUCCESS; |
| 299 | |
| 300 | done: |
| 301 | if (err && floppy) |
| 302 | { |
| 303 | floppy_close_internal(floppy, FALSE); |
| 304 | floppy = NULL; |
| 305 | } |
| 306 | |
| 307 | if (outfloppy) |
| 308 | *outfloppy = floppy; |
| 309 | else if (floppy) |
| 310 | floppy_close_internal(floppy, FALSE); |
| 311 | |
| 312 | if (alloc_resolution) |
| 313 | option_resolution_close(alloc_resolution); |
| 314 | return err; |
| 315 | } |
| 316 | |
| 317 | |
| 318 | |
| 319 | static void floppy_close_internal(floppy_image *floppy, int close_file) |
| 320 | { |
| 321 | if (floppy) { |
| 322 | floppy_track_unload(floppy); |
| 323 | |
| 324 | if(floppy->floppy_option->destruct) |
| 325 | floppy->floppy_option->destruct(floppy, floppy->floppy_option); |
| 326 | if (close_file) |
| 327 | io_generic_close(&floppy->io); |
| 328 | if (floppy->loaded_track_data) |
| 329 | free(floppy->loaded_track_data); |
| 330 | pool_free_lib(floppy->tags); |
| 331 | |
| 332 | free(floppy); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | |
| 337 | |
| 338 | void floppy_close(floppy_image *floppy) |
| 339 | { |
| 340 | floppy_close_internal(floppy, TRUE); |
| 341 | } |
| 342 | |
| 343 | |
| 344 | |
| 345 | /********************************************************************* |
| 346 | functions useful in format constructors |
| 347 | *********************************************************************/ |
| 348 | |
| 349 | struct FloppyCallbacks *floppy_callbacks(floppy_image *floppy) |
| 350 | { |
| 351 | assert(floppy); |
| 352 | return &floppy->format; |
| 353 | } |
| 354 | |
| 355 | |
| 356 | |
| 357 | void *floppy_tag(floppy_image *floppy) |
| 358 | { |
| 359 | assert(floppy); |
| 360 | return floppy->tag_data; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | |
| 365 | void *floppy_create_tag(floppy_image *floppy, size_t tagsize) |
| 366 | { |
| 367 | floppy->tag_data = pool_malloc_lib(floppy->tags,tagsize); |
| 368 | return floppy->tag_data; |
| 369 | } |
| 370 | |
| 371 | |
| 372 | |
| 373 | UINT8 floppy_get_filler(floppy_image *floppy) |
| 374 | { |
| 375 | return floppy->io.filler; |
| 376 | } |
| 377 | |
| 378 | |
| 379 | |
| 380 | void floppy_set_filler(floppy_image *floppy, UINT8 filler) |
| 381 | { |
| 382 | floppy->io.filler = filler; |
| 383 | } |
| 384 | |
| 385 | |
| 386 | |
| 387 | /********************************************************************* |
| 388 | calls for accessing the raw disk image |
| 389 | *********************************************************************/ |
| 390 | |
| 391 | void floppy_image_read(floppy_image *floppy, void *buffer, UINT64 offset, size_t length) |
| 392 | { |
| 393 | io_generic_read(&floppy->io, buffer, offset, length); |
| 394 | } |
| 395 | |
| 396 | |
| 397 | |
| 398 | void floppy_image_write(floppy_image *floppy, const void *buffer, UINT64 offset, size_t length) |
| 399 | { |
| 400 | io_generic_write(&floppy->io, buffer, offset, length); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | |
| 405 | void floppy_image_write_filler(floppy_image *floppy, UINT8 filler, UINT64 offset, size_t length) |
| 406 | { |
| 407 | io_generic_write_filler(&floppy->io, filler, offset, length); |
| 408 | } |
| 409 | |
| 410 | |
| 411 | |
| 412 | UINT64 floppy_image_size(floppy_image *floppy) |
| 413 | { |
| 414 | return io_generic_size(&floppy->io); |
| 415 | } |
| 416 | |
| 417 | |
| 418 | |
| 419 | /********************************************************************* |
| 420 | calls for accessing disk image data |
| 421 | *********************************************************************/ |
| 422 | |
| 423 | static floperr_t floppy_readwrite_sector(floppy_image *floppy, int head, int track, int sector, int offset, |
| 424 | void *buffer, size_t buffer_len, int writing, int indexed, int ddam) |
| 425 | { |
| 426 | floperr_t err; |
| 427 | const struct FloppyCallbacks *fmt; |
| 428 | size_t this_buffer_len; |
| 429 | UINT8 *alloc_buf = NULL; |
| 430 | UINT32 sector_length; |
| 431 | UINT8 *buffer_ptr = (UINT8 *)buffer; |
| 432 | floperr_t (*read_sector)(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen); |
| 433 | floperr_t (*write_sector)(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam); |
| 434 | |
| 435 | fmt = floppy_callbacks(floppy); |
| 436 | |
| 437 | /* choose proper calls for indexed vs non-indexed */ |
| 438 | if (indexed) |
| 439 | { |
| 440 | read_sector = fmt->read_indexed_sector; |
| 441 | write_sector = fmt->write_indexed_sector; |
| 442 | if (!fmt->get_indexed_sector_info) |
| 443 | { |
| 444 | err = FLOPPY_ERROR_UNSUPPORTED; |
| 445 | goto done; |
| 446 | } |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | read_sector = fmt->read_sector; |
| 451 | write_sector = fmt->write_sector; |
| 452 | if (!fmt->get_sector_length) |
| 453 | { |
| 454 | err = FLOPPY_ERROR_UNSUPPORTED; |
| 455 | goto done; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /* check to make sure that the operation is supported */ |
| 460 | if (!read_sector || (writing && !write_sector)) |
| 461 | { |
| 462 | err = FLOPPY_ERROR_UNSUPPORTED; |
| 463 | goto done; |
| 464 | } |
| 465 | |
| 466 | /* main loop */ |
| 467 | while(buffer_len > 0) |
| 468 | { |
| 469 | /* find out the size of this sector */ |
| 470 | if (indexed) |
| 471 | err = fmt->get_indexed_sector_info(floppy, head, track, sector, NULL, NULL, NULL, §or_length, NULL); |
| 472 | else |
| 473 | err = fmt->get_sector_length(floppy, head, track, sector, §or_length); |
| 474 | if (err) |
| 475 | goto done; |
| 476 | |
| 477 | /* do we even do anything with this sector? */ |
| 478 | if (offset < sector_length) |
| 479 | { |
| 480 | /* ok we will be doing something */ |
| 481 | if ((offset > 0) || (buffer_len < sector_length)) |
| 482 | { |
| 483 | /* we will be doing an partial read/write; in other words we |
| 484 | * will not be reading/writing a full sector */ |
| 485 | if (alloc_buf) free(alloc_buf); |
| 486 | alloc_buf = (UINT8*)malloc(sector_length); |
| 487 | if (!alloc_buf) |
| 488 | { |
| 489 | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 490 | goto done; |
| 491 | } |
| 492 | |
| 493 | /* read the sector (we need to do this even when writing */ |
| 494 | err = read_sector(floppy, head, track, sector, alloc_buf, sector_length); |
| 495 | if (err) |
| 496 | goto done; |
| 497 | |
| 498 | this_buffer_len = MIN(buffer_len, sector_length - offset); |
| 499 | |
| 500 | if (writing) |
| 501 | { |
| 502 | memcpy(alloc_buf + offset, buffer_ptr, this_buffer_len); |
| 503 | |
| 504 | err = write_sector(floppy, head, track, sector, alloc_buf, sector_length, ddam); |
| 505 | if (err) |
| 506 | goto done; |
| 507 | } |
| 508 | else |
| 509 | { |
| 510 | memcpy(buffer_ptr, alloc_buf + offset, this_buffer_len); |
| 511 | } |
| 512 | offset += this_buffer_len; |
| 513 | offset %= sector_length; |
| 514 | } |
| 515 | else |
| 516 | { |
| 517 | this_buffer_len = sector_length; |
| 518 | |
| 519 | if (writing) |
| 520 | err = write_sector(floppy, head, track, sector, buffer_ptr, sector_length, ddam); |
| 521 | else |
| 522 | err = read_sector(floppy, head, track, sector, buffer_ptr, sector_length); |
| 523 | if (err) |
| 524 | goto done; |
| 525 | } |
| 526 | } |
| 527 | else |
| 528 | { |
| 529 | /* skip this sector */ |
| 530 | offset -= sector_length; |
| 531 | this_buffer_len = 0; |
| 532 | } |
| 533 | |
| 534 | buffer_ptr += this_buffer_len; |
| 535 | buffer_len -= this_buffer_len; |
| 536 | sector++; |
| 537 | } |
| 538 | |
| 539 | err = FLOPPY_ERROR_SUCCESS; |
| 540 | |
| 541 | done: |
| 542 | if (alloc_buf) |
| 543 | free(alloc_buf); |
| 544 | return err; |
| 545 | } |
| 546 | |
| 547 | |
| 548 | |
| 549 | floperr_t floppy_read_sector(floppy_image *floppy, int head, int track, int sector, int offset, void *buffer, size_t buffer_len) |
| 550 | { |
| 551 | return floppy_readwrite_sector(floppy, head, track, sector, offset, buffer, buffer_len, FALSE, FALSE, 0); |
| 552 | } |
| 553 | |
| 554 | |
| 555 | |
| 556 | floperr_t floppy_write_sector(floppy_image *floppy, int head, int track, int sector, int offset, const void *buffer, size_t buffer_len, int ddam) |
| 557 | { |
| 558 | return floppy_readwrite_sector(floppy, head, track, sector, offset, (void *) buffer, buffer_len, TRUE, FALSE, ddam); |
| 559 | } |
| 560 | |
| 561 | |
| 562 | |
| 563 | floperr_t floppy_read_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, void *buffer, size_t buffer_len) |
| 564 | { |
| 565 | return floppy_readwrite_sector(floppy, head, track, sector_index, offset, buffer, buffer_len, FALSE, TRUE, 0); |
| 566 | } |
| 567 | |
| 568 | |
| 569 | |
| 570 | floperr_t floppy_write_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, const void *buffer, size_t buffer_len, int ddam) |
| 571 | { |
| 572 | return floppy_readwrite_sector(floppy, head, track, sector_index, offset, (void *) buffer, buffer_len, TRUE, TRUE, ddam); |
| 573 | } |
| 574 | |
| 575 | |
| 576 | static floperr_t floppy_get_track_data_offset(floppy_image *floppy, int head, int track, UINT64 *offset) |
| 577 | { |
| 578 | floperr_t err; |
| 579 | const struct FloppyCallbacks *callbacks; |
| 580 | |
| 581 | *offset = 0; |
| 582 | callbacks = floppy_callbacks(floppy); |
| 583 | if (callbacks->get_track_data_offset) |
| 584 | { |
| 585 | err = callbacks->get_track_data_offset(floppy, head, track, offset); |
| 586 | if (err) |
| 587 | return err; |
| 588 | } |
| 589 | return FLOPPY_ERROR_SUCCESS; |
| 590 | } |
| 591 | |
| 592 | |
| 593 | |
| 594 | static floperr_t floppy_read_track_offset(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buffer_len) |
| 595 | { |
| 596 | floperr_t err; |
| 597 | const struct FloppyCallbacks *format; |
| 598 | |
| 599 | format = floppy_callbacks(floppy); |
| 600 | |
| 601 | if (!format->read_track) |
| 602 | return FLOPPY_ERROR_UNSUPPORTED; |
| 603 | |
| 604 | err = floppy_track_unload(floppy); |
| 605 | if (err) |
| 606 | return err; |
| 607 | |
| 608 | err = format->read_track(floppy, head, track, offset, buffer, buffer_len); |
| 609 | if (err) |
| 610 | return err; |
| 611 | |
| 612 | return FLOPPY_ERROR_SUCCESS; |
| 613 | } |
| 614 | |
| 615 | |
| 616 | |
| 617 | floperr_t floppy_read_track(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len) |
| 618 | { |
| 619 | return floppy_read_track_offset(floppy, head, track, 0, buffer, buffer_len); |
| 620 | } |
| 621 | |
| 622 | |
| 623 | |
| 624 | floperr_t floppy_read_track_data(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len) |
| 625 | { |
| 626 | floperr_t err; |
| 627 | UINT64 offset; |
| 628 | |
| 629 | err = floppy_get_track_data_offset(floppy, head, track, &offset); |
| 630 | if (err) |
| 631 | return err; |
| 632 | |
| 633 | return floppy_read_track_offset(floppy, head, track, offset, buffer, buffer_len); |
| 634 | } |
| 635 | |
| 636 | |
| 637 | |
| 638 | static floperr_t floppy_write_track_offset(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buffer_len) |
| 639 | { |
| 640 | floperr_t err; |
| 641 | |
| 642 | /* track writing supported? */ |
| 643 | if (!floppy_callbacks(floppy)->write_track) |
| 644 | return FLOPPY_ERROR_UNSUPPORTED; |
| 645 | |
| 646 | /* read only? */ |
| 647 | if (floppy->flags & FLOPPY_FLAGS_READONLY) |
| 648 | return FLOPPY_ERROR_READONLY; |
| 649 | |
| 650 | err = floppy_track_unload(floppy); |
| 651 | if (err) |
| 652 | return err; |
| 653 | |
| 654 | err = floppy_callbacks(floppy)->write_track(floppy, head, track, offset, buffer, buffer_len); |
| 655 | if (err) |
| 656 | return err; |
| 657 | |
| 658 | return FLOPPY_ERROR_SUCCESS; |
| 659 | } |
| 660 | |
| 661 | |
| 662 | |
| 663 | floperr_t floppy_write_track(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len) |
| 664 | { |
| 665 | return floppy_write_track_offset(floppy, head, track, 0, buffer, buffer_len); |
| 666 | } |
| 667 | |
| 668 | |
| 669 | |
| 670 | floperr_t floppy_write_track_data(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len) |
| 671 | { |
| 672 | floperr_t err; |
| 673 | UINT64 offset; |
| 674 | |
| 675 | err = floppy_get_track_data_offset(floppy, head, track, &offset); |
| 676 | if (err) |
| 677 | return err; |
| 678 | |
| 679 | return floppy_write_track_offset(floppy, head, track, offset, buffer, buffer_len); |
| 680 | } |
| 681 | |
| 682 | |
| 683 | |
| 684 | floperr_t floppy_format_track(floppy_image *floppy, int head, int track, option_resolution *parameters) |
| 685 | { |
| 686 | floperr_t err; |
| 687 | struct FloppyCallbacks *format; |
| 688 | option_resolution *alloc_resolution = NULL; |
| 689 | optreserr_t oerr; |
| 690 | |
| 691 | /* supported? */ |
| 692 | format = floppy_callbacks(floppy); |
| 693 | if (!format->format_track) |
| 694 | { |
| 695 | err = FLOPPY_ERROR_UNSUPPORTED; |
| 696 | goto done; |
| 697 | } |
| 698 | |
| 699 | /* create a dummy resolution; if no parameters were specified */ |
| 700 | if (!parameters) |
| 701 | { |
| 702 | alloc_resolution = option_resolution_create(floppy_option_guide, floppy->floppy_option->param_guidelines); |
| 703 | if (!alloc_resolution) |
| 704 | { |
| 705 | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 706 | goto done; |
| 707 | } |
| 708 | parameters = alloc_resolution; |
| 709 | } |
| 710 | |
| 711 | oerr = option_resolution_finish(parameters); |
| 712 | if (oerr) |
| 713 | { |
| 714 | err = option_to_floppy_error(oerr); |
| 715 | goto done; |
| 716 | } |
| 717 | |
| 718 | err = format->format_track(floppy, head, track, parameters); |
| 719 | if (err) |
| 720 | goto done; |
| 721 | |
| 722 | done: |
| 723 | if (alloc_resolution) |
| 724 | option_resolution_close(alloc_resolution); |
| 725 | return err; |
| 726 | } |
| 727 | |
| 728 | |
| 729 | |
| 730 | int floppy_get_tracks_per_disk(floppy_image *floppy) |
| 731 | { |
| 732 | return floppy_callbacks(floppy)->get_tracks_per_disk(floppy); |
| 733 | } |
| 734 | |
| 735 | |
| 736 | |
| 737 | int floppy_get_heads_per_disk(floppy_image *floppy) |
| 738 | { |
| 739 | return floppy_callbacks(floppy)->get_heads_per_disk(floppy); |
| 740 | } |
| 741 | |
| 742 | |
| 743 | |
| 744 | UINT32 floppy_get_track_size(floppy_image *floppy, int head, int track) |
| 745 | { |
| 746 | const struct FloppyCallbacks *fmt; |
| 747 | |
| 748 | fmt = floppy_callbacks(floppy); |
| 749 | if (!fmt->get_track_size) |
| 750 | return 0; |
| 751 | |
| 752 | return fmt->get_track_size(floppy, head, track); |
| 753 | } |
| 754 | |
| 755 | |
| 756 | |
| 757 | floperr_t floppy_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 758 | { |
| 759 | const struct FloppyCallbacks *fmt; |
| 760 | |
| 761 | fmt = floppy_callbacks(floppy); |
| 762 | if (!fmt->get_sector_length) |
| 763 | return FLOPPY_ERROR_UNSUPPORTED; |
| 764 | |
| 765 | return fmt->get_sector_length(floppy, head, track, sector, sector_length); |
| 766 | } |
| 767 | |
| 768 | |
| 769 | |
| 770 | floperr_t floppy_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 771 | { |
| 772 | const struct FloppyCallbacks *fmt; |
| 773 | |
| 774 | fmt = floppy_callbacks(floppy); |
| 775 | if (!fmt->get_indexed_sector_info) |
| 776 | return FLOPPY_ERROR_UNSUPPORTED; |
| 777 | |
| 778 | return fmt->get_indexed_sector_info(floppy, head, track, sector_index, cylinder, side, sector, sector_length, flags); |
| 779 | } |
| 780 | |
| 781 | |
| 782 | |
| 783 | floperr_t floppy_get_sector_count(floppy_image *floppy, int head, int track, int *sector_count) |
| 784 | { |
| 785 | floperr_t err; |
| 786 | int sector_index = 0; |
| 787 | |
| 788 | do |
| 789 | { |
| 790 | err = floppy_get_indexed_sector_info(floppy, head, track, sector_index, NULL, NULL, NULL, NULL, NULL); |
| 791 | if (!err) |
| 792 | sector_index++; |
| 793 | } |
| 794 | while(!err); |
| 795 | |
| 796 | if (sector_index && (err == FLOPPY_ERROR_SEEKERROR)) |
| 797 | err = FLOPPY_ERROR_SUCCESS; |
| 798 | if (sector_count) |
| 799 | *sector_count = err ? 0 : sector_index; |
| 800 | return err; |
| 801 | } |
| 802 | |
| 803 | |
| 804 | |
| 805 | int floppy_is_read_only(floppy_image *floppy) |
| 806 | { |
| 807 | return floppy->flags & FLOPPY_FLAGS_READONLY; |
| 808 | } |
| 809 | |
| 810 | |
| 811 | |
| 812 | UINT8 floppy_random_byte(floppy_image *floppy) |
| 813 | { |
| 814 | /* can't use mame_rand(); this might not be in the core */ |
| 815 | #ifdef rand |
| 816 | #undef rand |
| 817 | #endif |
| 818 | return rand(); |
| 819 | } |
| 820 | |
| 821 | |
| 822 | |
| 823 | /********************************************************************* |
| 824 | calls for track based IO |
| 825 | *********************************************************************/ |
| 826 | |
| 827 | floperr_t floppy_load_track(floppy_image *floppy, int head, int track, int dirtify, void **track_data, size_t *track_length) |
| 828 | { |
| 829 | floperr_t err; |
| 830 | void *new_loaded_track_data; |
| 831 | UINT32 track_size; |
| 832 | |
| 833 | /* have we already loaded this track? */ |
| 834 | if (((floppy->loaded_track_status & TRACK_LOADED) == 0) || (head != floppy->loaded_track_head) || (track != floppy->loaded_track_index)) |
| 835 | { |
| 836 | err = floppy_track_unload(floppy); |
| 837 | if (err) |
| 838 | goto error; |
| 839 | |
| 840 | track_size = floppy_callbacks(floppy)->get_track_size(floppy, head, track); |
| 841 | |
| 842 | if (floppy->loaded_track_data) free(floppy->loaded_track_data); |
| 843 | new_loaded_track_data = malloc(track_size); |
| 844 | if (!new_loaded_track_data) |
| 845 | { |
| 846 | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 847 | goto error; |
| 848 | } |
| 849 | |
| 850 | floppy->loaded_track_data = new_loaded_track_data; |
| 851 | floppy->loaded_track_size = track_size; |
| 852 | floppy->loaded_track_head = head; |
| 853 | floppy->loaded_track_index = track; |
| 854 | |
| 855 | err = floppy_callbacks(floppy)->read_track(floppy, floppy->loaded_track_head, floppy->loaded_track_index, 0, floppy->loaded_track_data, floppy->loaded_track_size); |
| 856 | if (err) |
| 857 | goto error; |
| 858 | |
| 859 | floppy->loaded_track_status |= TRACK_LOADED | (dirtify ? TRACK_DIRTY : 0); |
| 860 | } |
| 861 | else |
| 862 | floppy->loaded_track_status |= (dirtify ? TRACK_DIRTY : 0); |
| 863 | |
| 864 | if (track_data) |
| 865 | *track_data = floppy->loaded_track_data; |
| 866 | if (track_length) |
| 867 | *track_length = floppy->loaded_track_size; |
| 868 | return FLOPPY_ERROR_SUCCESS; |
| 869 | |
| 870 | error: |
| 871 | if (track_data) |
| 872 | *track_data = NULL; |
| 873 | if (track_length) |
| 874 | *track_length = 0; |
| 875 | return err; |
| 876 | } |
| 877 | |
| 878 | |
| 879 | |
| 880 | static floperr_t floppy_track_unload(floppy_image *floppy) |
| 881 | { |
| 882 | int err; |
| 883 | if (floppy->loaded_track_status & TRACK_DIRTY) |
| 884 | { |
| 885 | err = floppy_callbacks(floppy)->write_track(floppy, floppy->loaded_track_head, floppy->loaded_track_index, 0, floppy->loaded_track_data, floppy->loaded_track_size); |
| 886 | if (err) |
| 887 | return (floperr_t)err; |
| 888 | } |
| 889 | |
| 890 | floppy->loaded_track_status &= ~(TRACK_LOADED | TRACK_DIRTY); |
| 891 | return FLOPPY_ERROR_SUCCESS; |
| 892 | } |
| 893 | |
| 894 | |
| 895 | |
| 896 | /********************************************************************* |
| 897 | accessors for meta information about the image |
| 898 | *********************************************************************/ |
| 899 | |
| 900 | const char *floppy_format_description(floppy_image *floppy) |
| 901 | { |
| 902 | return floppy->floppy_option->description; |
| 903 | } |
| 904 | |
| 905 | |
| 906 | |
| 907 | /********************************************************************* |
| 908 | misc calls |
| 909 | *********************************************************************/ |
| 910 | |
| 911 | const char *floppy_error(floperr_t err) |
| 912 | { |
| 913 | static const char *const error_messages[] = |
| 914 | { |
| 915 | "The operation completed successfully", |
| 916 | "Fatal internal error", |
| 917 | "This operation is unsupported", |
| 918 | "Out of memory", |
| 919 | "Seek error", |
| 920 | "Invalid image", |
| 921 | "Attempted to write to read only image", |
| 922 | "No space left on image", |
| 923 | "Parameter out of range", |
| 924 | "Required parameter not specified" |
| 925 | }; |
| 926 | |
| 927 | if ((err < 0) || (err >= ARRAY_LENGTH(error_messages))) |
| 928 | return NULL; |
| 929 | return error_messages[err]; |
| 930 | } |
| 931 | |
| 932 | |
| 933 | FLOPPY_OPTIONS_START(default) |
| 934 | FLOPPY_OPTIONS_END |
src/lib/formats/flopimg.h
| r0 | r11458 | |
| 1 | /********************************************************************* |
| 2 | |
| 3 | flopimg.h |
| 4 | |
| 5 | Floppy disk image abstraction code |
| 6 | |
| 7 | *********************************************************************/ |
| 8 | |
| 9 | #ifndef FLOPIMG_H |
| 10 | #define FLOPIMG_H |
| 11 | |
| 12 | #include "osdcore.h" |
| 13 | #include "ioprocs.h" |
| 14 | #include "opresolv.h" |
| 15 | |
| 16 | #ifndef logerror |
| 17 | #define logerror printf |
| 18 | #endif |
| 19 | |
| 20 | /*************************************************************************** |
| 21 | |
| 22 | Constants |
| 23 | |
| 24 | ***************************************************************************/ |
| 25 | |
| 26 | #define FLOPPY_FLAGS_READWRITE 0 |
| 27 | #define FLOPPY_FLAGS_READONLY 1 |
| 28 | |
| 29 | /* sector has a deleted data address mark */ |
| 30 | #define ID_FLAG_DELETED_DATA 0x0001 |
| 31 | /* CRC error in id field */ |
| 32 | #define ID_FLAG_CRC_ERROR_IN_ID_FIELD 0x0002 |
| 33 | /* CRC error in data field */ |
| 34 | #define ID_FLAG_CRC_ERROR_IN_DATA_FIELD 0x0004 |
| 35 | |
| 36 | |
| 37 | /*************************************************************************** |
| 38 | |
| 39 | Type definitions |
| 40 | |
| 41 | ***************************************************************************/ |
| 42 | |
| 43 | typedef enum |
| 44 | { |
| 45 | FLOPPY_ERROR_SUCCESS, /* no error */ |
| 46 | FLOPPY_ERROR_INTERNAL, /* fatal internal error */ |
| 47 | FLOPPY_ERROR_UNSUPPORTED, /* this operation is unsupported */ |
| 48 | FLOPPY_ERROR_OUTOFMEMORY, /* ran out of memory */ |
| 49 | FLOPPY_ERROR_SEEKERROR, /* attempted to seek to nonexistant location */ |
| 50 | FLOPPY_ERROR_INVALIDIMAGE, /* this image in invalid */ |
| 51 | FLOPPY_ERROR_READONLY, /* attempt to write to read-only image */ |
| 52 | FLOPPY_ERROR_NOSPACE, |
| 53 | FLOPPY_ERROR_PARAMOUTOFRANGE, |
| 54 | FLOPPY_ERROR_PARAMNOTSPECIFIED |
| 55 | } |
| 56 | floperr_t; |
| 57 | |
| 58 | typedef struct _floppy_image floppy_image; |
| 59 | |
| 60 | struct FloppyCallbacks |
| 61 | { |
| 62 | floperr_t (*read_sector)(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen); |
| 63 | floperr_t (*write_sector)(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam); |
| 64 | floperr_t (*read_indexed_sector)(floppy_image *floppy, int head, int track, int sector_index, void *buffer, size_t buflen); |
| 65 | floperr_t (*write_indexed_sector)(floppy_image *floppy, int head, int track, int sector_index, const void *buffer, size_t buflen, int ddam); |
| 66 | floperr_t (*read_track)(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen); |
| 67 | floperr_t (*write_track)(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen); |
| 68 | floperr_t (*format_track)(floppy_image *floppy, int head, int track, option_resolution *params); |
| 69 | floperr_t (*post_format)(floppy_image *floppy, option_resolution *params); |
| 70 | int (*get_heads_per_disk)(floppy_image *floppy); |
| 71 | int (*get_tracks_per_disk)(floppy_image *floppy); |
| 72 | int (*get_sectors_per_track)(floppy_image *floppy, int head, int track); |
| 73 | UINT32 (*get_track_size)(floppy_image *floppy, int head, int track); |
| 74 | floperr_t (*get_sector_length)(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length); |
| 75 | floperr_t (*get_indexed_sector_info)(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags); |
| 76 | floperr_t (*get_track_data_offset)(floppy_image *floppy, int head, int track, UINT64 *offset); |
| 77 | }; |
| 78 | |
| 79 | |
| 80 | |
| 81 | struct FloppyFormat |
| 82 | { |
| 83 | const char *name; |
| 84 | const char *extensions; |
| 85 | const char *description; |
| 86 | floperr_t (*identify)(floppy_image *floppy, const struct FloppyFormat *format, int *vote); |
| 87 | floperr_t (*construct)(floppy_image *floppy, const struct FloppyFormat *format, option_resolution *params); |
| 88 | floperr_t (*destruct)(floppy_image *floppy, const struct FloppyFormat *format); |
| 89 | const char *param_guidelines; |
| 90 | }; |
| 91 | |
| 92 | #define FLOPPY_IDENTIFY(name) floperr_t name(floppy_image *floppy, const struct FloppyFormat *format, int *vote) |
| 93 | #define FLOPPY_CONSTRUCT(name) floperr_t name(floppy_image *floppy, const struct FloppyFormat *format, option_resolution *params) |
| 94 | #define FLOPPY_DESTRUCT(name) floperr_t name(floppy_image *floppy, const struct FloppyFormat *format) |
| 95 | |
| 96 | FLOPPY_IDENTIFY(td0_dsk_identify); |
| 97 | FLOPPY_CONSTRUCT(td0_dsk_construct); |
| 98 | FLOPPY_DESTRUCT(td0_dsk_destruct); |
| 99 | |
| 100 | FLOPPY_IDENTIFY(imd_dsk_identify); |
| 101 | FLOPPY_CONSTRUCT(imd_dsk_construct); |
| 102 | |
| 103 | FLOPPY_IDENTIFY(cqm_dsk_identify); |
| 104 | FLOPPY_CONSTRUCT(cqm_dsk_construct); |
| 105 | |
| 106 | FLOPPY_IDENTIFY(dsk_dsk_identify); |
| 107 | FLOPPY_CONSTRUCT(dsk_dsk_construct); |
| 108 | |
| 109 | FLOPPY_IDENTIFY(d88_dsk_identify); |
| 110 | FLOPPY_CONSTRUCT(d88_dsk_construct); |
| 111 | |
| 112 | FLOPPY_IDENTIFY(fdi_dsk_identify); |
| 113 | FLOPPY_CONSTRUCT(fdi_dsk_construct); |
| 114 | |
| 115 | #define FLOPPY_OPTIONS_NAME(name) floppyoptions_##name |
| 116 | |
| 117 | #define FLOPPY_OPTIONS_START(name) \ |
| 118 | const struct FloppyFormat floppyoptions_##name[] = \ |
| 119 | { \ |
| 120 | |
| 121 | #define FLOPPY_OPTIONS_END0 \ |
| 122 | { NULL } \ |
| 123 | }; |
| 124 | |
| 125 | #define FLOPPY_OPTIONS_EXTERN(name) \ |
| 126 | extern const struct FloppyFormat floppyoptions_##name[] \ |
| 127 | |
| 128 | #define FLOPPY_OPTION(name, extensions_, description_, identify_, construct_, destruct_, ranges_)\ |
| 129 | { #name, extensions_, description_, identify_, construct_, destruct_, ranges_ }, \ |
| 130 | |
| 131 | #define FLOPPY_OPTIONS_END \ |
| 132 | FLOPPY_OPTION( fdi, "fdi", "Formatted Disk Image", fdi_dsk_identify, fdi_dsk_construct, NULL, NULL) \ |
| 133 | FLOPPY_OPTION( td0, "td0", "Teledisk floppy disk image", td0_dsk_identify, td0_dsk_construct, td0_dsk_destruct, NULL) \ |
| 134 | FLOPPY_OPTION( imd, "imd", "IMD floppy disk image", imd_dsk_identify, imd_dsk_construct, NULL, NULL) \ |
| 135 | FLOPPY_OPTION( cqm, "cqm,dsk", "CopyQM floppy disk image", cqm_dsk_identify, cqm_dsk_construct, NULL, NULL) \ |
| 136 | FLOPPY_OPTION( dsk, "dsk", "DSK floppy disk image", dsk_dsk_identify, dsk_dsk_construct, NULL, NULL) \ |
| 137 | FLOPPY_OPTION( d88, "d77,d88,1dd", "D88 Floppy Disk image", d88_dsk_identify, d88_dsk_construct, NULL, NULL) \ |
| 138 | FLOPPY_OPTIONS_END0 |
| 139 | |
| 140 | FLOPPY_OPTIONS_EXTERN(default); |
| 141 | |
| 142 | #define PARAM_END '\0' |
| 143 | #define PARAM_HEADS 'H' |
| 144 | #define PARAM_TRACKS 'T' |
| 145 | #define PARAM_SECTORS 'S' |
| 146 | #define PARAM_SECTOR_LENGTH 'L' |
| 147 | #define PARAM_INTERLEAVE 'I' |
| 148 | #define PARAM_FIRST_SECTOR_ID 'F' |
| 149 | |
| 150 | #define HEADS(range) "H" #range |
| 151 | #define TRACKS(range) "T" #range |
| 152 | #define SECTORS(range) "S" #range |
| 153 | #define SECTOR_LENGTH(range) "L" #range |
| 154 | #define INTERLEAVE(range) "I" #range |
| 155 | #define FIRST_SECTOR_ID(range) "F" #range |
| 156 | |
| 157 | |
| 158 | /*************************************************************************** |
| 159 | |
| 160 | Prototypes |
| 161 | |
| 162 | ***************************************************************************/ |
| 163 | |
| 164 | OPTION_GUIDE_EXTERN(floppy_option_guide); |
| 165 | |
| 166 | /* opening, closing and creating of floppy images */ |
| 167 | floperr_t floppy_open(void *fp, const struct io_procs *procs, const char *extension, const struct FloppyFormat *format, int flags, floppy_image **outfloppy); |
| 168 | floperr_t floppy_open_choices(void *fp, const struct io_procs *procs, const char *extension, const struct FloppyFormat *formats, int flags, floppy_image **outfloppy); |
| 169 | floperr_t floppy_create(void *fp, const struct io_procs *procs, const struct FloppyFormat *format, option_resolution *parameters, floppy_image **outfloppy); |
| 170 | void floppy_close(floppy_image *floppy); |
| 171 | |
| 172 | /* useful for identifying a floppy image */ |
| 173 | floperr_t floppy_identify(void *fp, const struct io_procs *procs, const char *extension, |
| 174 | const struct FloppyFormat *formats, int *identified_format); |
| 175 | |
| 176 | /* functions useful within format constructors */ |
| 177 | void *floppy_tag(floppy_image *floppy); |
| 178 | void *floppy_create_tag(floppy_image *floppy, size_t tagsize); |
| 179 | struct FloppyCallbacks *floppy_callbacks(floppy_image *floppy); |
| 180 | UINT8 floppy_get_filler(floppy_image *floppy); |
| 181 | void floppy_set_filler(floppy_image *floppy, UINT8 filler); |
| 182 | |
| 183 | /* calls for accessing disk image data */ |
| 184 | floperr_t floppy_read_sector(floppy_image *floppy, int head, int track, int sector, int offset, void *buffer, size_t buffer_len); |
| 185 | floperr_t floppy_write_sector(floppy_image *floppy, int head, int track, int sector, int offset, const void *buffer, size_t buffer_len, int ddam); |
| 186 | floperr_t floppy_read_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, void *buffer, size_t buffer_len); |
| 187 | floperr_t floppy_write_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int offset, const void *buffer, size_t buffer_len, int ddam); |
| 188 | floperr_t floppy_read_track(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len); |
| 189 | floperr_t floppy_write_track(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len); |
| 190 | floperr_t floppy_read_track_data(floppy_image *floppy, int head, int track, void *buffer, size_t buffer_len); |
| 191 | floperr_t floppy_write_track_data(floppy_image *floppy, int head, int track, const void *buffer, size_t buffer_len); |
| 192 | floperr_t floppy_format_track(floppy_image *floppy, int head, int track, option_resolution *params); |
| 193 | int floppy_get_tracks_per_disk(floppy_image *floppy); |
| 194 | int floppy_get_heads_per_disk(floppy_image *floppy); |
| 195 | UINT32 floppy_get_track_size(floppy_image *floppy, int head, int track); |
| 196 | floperr_t floppy_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length); |
| 197 | floperr_t floppy_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags); |
| 198 | floperr_t floppy_get_sector_count(floppy_image *floppy, int head, int track, int *sector_count); |
| 199 | floperr_t floppy_load_track(floppy_image *floppy, int head, int track, int dirtify, void **track_data, size_t *track_length); |
| 200 | int floppy_is_read_only(floppy_image *floppy); |
| 201 | UINT8 floppy_random_byte(floppy_image *floppy); |
| 202 | |
| 203 | /* accessors for meta information about the image */ |
| 204 | const char *floppy_format_description(floppy_image *floppy); |
| 205 | |
| 206 | /* calls for accessing the raw disk image */ |
| 207 | void floppy_image_read(floppy_image *floppy, void *buffer, UINT64 offset, size_t length); |
| 208 | void floppy_image_write(floppy_image *floppy, const void *buffer, UINT64 offset, size_t length); |
| 209 | void floppy_image_write_filler(floppy_image *floppy, UINT8 filler, UINT64 offset, size_t length); |
| 210 | UINT64 floppy_image_size(floppy_image *floppy); |
| 211 | |
| 212 | /* misc */ |
| 213 | const char *floppy_error(floperr_t err); |
| 214 | |
| 215 | |
| 216 | #endif /* FLOPIMG_H */ |
| 217 | |
src/lib/formats/ap_dsk35.c
| r0 | r11458 | |
| 1 | /********************************************************************* |
| 2 | |
| 3 | ap_dsk35.c |
| 4 | |
| 5 | Apple 3.5" disk images |
| 6 | |
| 7 | This code supports 3.5" 400k/800k disks used in early Macintoshes |
| 8 | and the Apple IIgs, and 3.5" 1440k MFM disks used on most Macintoshes. |
| 9 | |
| 10 | These disks have the following properties: |
| 11 | |
| 12 | 400k: 80 tracks, 1 head |
| 13 | 800k: 80 tracks, 2 heads |
| 14 | |
| 15 | Tracks 0-15 have 12 sectors each |
| 16 | Tracks 16-31 have 11 sectors each |
| 17 | Tracks 32-47 have 10 sectors each |
| 18 | Tracks 48-63 have 9 sectors each |
| 19 | Tracks 64-79 have 8 sectors each |
| 20 | |
| 21 | 1440k disks are simply 80 tracks, 2 heads and 18 sectors per track. |
| 22 | |
| 23 | Each sector on 400/800k disks has 524 bytes, 512 of which are really used by the Macintosh |
| 24 | |
| 25 | (80 tracks) * (avg of 10 sectors) * (512 bytes) * (2 sides) = 800 kB |
| 26 | |
| 27 | Data is nibblized : 3 data bytes -> 4 bytes on disk. |
| 28 | |
| 29 | In addition to 512 logical bytes, each sector contains 800 physical |
| 30 | bytes. Here is the layout of the physical sector: |
| 31 | |
| 32 | Pos |
| 33 | 0 0xFF (pad byte where head is turned on ???) |
| 34 | 1-35 self synch 0xFFs (7*5) (42 bytes actually written to media) |
| 35 | 36 0xD5 |
| 36 | 37 0xAA |
| 37 | 38 0x96 |
| 38 | 39 diskbytes[(track number) & 0x3F] |
| 39 | 40 diskbytes[(sector number)] |
| 40 | 41 diskbytes[("side")] |
| 41 | 42 diskbytes[("format byte")] |
| 42 | 43 diskbytes[("sum")] |
| 43 | 44 0xDE |
| 44 | 45 0xAA |
| 45 | 46 pad byte where head is turned off/on (0xFF here) |
| 46 | 47-51 self synch 0xFFs (6 bytes actually written to media) |
| 47 | 52 0xD5 |
| 48 | 53 0xAA |
| 49 | 54 0xAD |
| 50 | 55 spare byte, generally diskbytes[(sector number)] |
| 51 | 56-754 "nibblized" sector data ... |
| 52 | 755-758 checksum |
| 53 | 759 0xDE |
| 54 | 760 0xAA |
| 55 | 761 pad byte where head is turned off (0xFF here) |
| 56 | |
| 57 | MFM Track layout for 1440K disks: |
| 58 | Pos |
| 59 | --------- track ID |
| 60 | 0 0x4E (x80) |
| 61 | 80 00 (x12) |
| 62 | 92 C2 (x3) Mark byte |
| 63 | 93 FC Index mark |
| 64 | 94 4E (x50) |
| 65 | --------- sector ID |
| 66 | 144 00 (x12) |
| 67 | 156 A1 (x3) Mark byte |
| 68 | 159 FE Address mark |
| 69 | 160 xx Track number |
| 70 | 161 xx Side number |
| 71 | 162 xx Sector number |
| 72 | 163 xx Sector length |
| 73 | 164/165 xx CRC |
| 74 | 166 4E (x22) |
| 75 | --------- sector data |
| 76 | 188 00 (x12) |
| 77 | 200 A1 (x3) Mark byte |
| 78 | 203 FB Data address mark |
| 79 | 204 xx (x256) data |
| 80 | 460 4E (x54) |
| 81 | (repeat from sector ID to fill track; end of track is padded with 4E) |
| 82 | |
| 83 | Note : "Self synch refers to a technique whereby two zeroes are inserted |
| 84 | between each synch byte written to the disk.", i.e. "0xFF, 0xFF, 0xFF, |
| 85 | 0xFF, 0xFF" is actually "0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF" on disk. |
| 86 | Since the IWM assumes the data transfer is complete when the MSBit of its |
| 87 | shift register is 1, we do read 4 0xFF, even though they are not |
| 88 | contiguous on the disk media. Some reflexion shows that 4 synch bytes |
| 89 | allow the IWM to synchronize with the trailing data. |
| 90 | |
| 91 | Format byte codes: |
| 92 | 0x00 Apple II |
| 93 | 0x01 Lisa |
| 94 | 0x02 Mac MFS (single sided)? |
| 95 | 0x22 Mac MFS (double sided)? |
| 96 | |
| 97 | *********************************************************************/ |
| 98 | |
| 99 | #include <stdio.h> |
| 100 | #include <assert.h> |
| 101 | |
| 102 | #include "formats/ap_dsk35.h" |
| 103 | |
| 104 | struct apple35_tag |
| 105 | { |
| 106 | UINT32 data_offset; |
| 107 | UINT32 data_size; |
| 108 | UINT8 format_byte; |
| 109 | UINT8 sides; |
| 110 | unsigned int is_1440k : 1; |
| 111 | |
| 112 | /* stuff used in DiskCopy images */ |
| 113 | UINT32 tag_offset; |
| 114 | UINT32 tag_size; |
| 115 | }; |
| 116 | |
| 117 | |
| 118 | |
| 119 | /* normal number of sector for each track */ |
| 120 | static const UINT8 apple35_tracklen_800kb[80] = |
| 121 | { |
| 122 | 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, |
| 123 | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 124 | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 125 | 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, |
| 126 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 |
| 127 | }; |
| 128 | |
| 129 | /* blocks of data used to nibblize tracks */ |
| 130 | static const UINT8 diskbytes[] = |
| 131 | { |
| 132 | 0x96, 0x97, 0x9A, 0x9B, 0x9D, 0x9E, 0x9F, 0xA6, /* 0x00 */ |
| 133 | 0xA7, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB2, 0xB3, |
| 134 | 0xB4, 0xB5, 0xB6, 0xB7, 0xB9, 0xBA, 0xBB, 0xBC, /* 0x10 */ |
| 135 | 0xBD, 0xBE, 0xBF, 0xCB, 0xCD, 0xCE, 0xCF, 0xD3, |
| 136 | 0xD6, 0xD7, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, /* 0x20 */ |
| 137 | 0xDF, 0xE5, 0xE6, 0xE7, 0xE9, 0xEA, 0xEB, 0xEC, |
| 138 | 0xED, 0xEE, 0xEF, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, /* 0x30 */ |
| 139 | 0xF7, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF |
| 140 | }; |
| 141 | |
| 142 | /* reverse lookup of diskbytes */ |
| 143 | static const INT16 rev_diskbytes[] = |
| 144 | { |
| 145 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */ |
| 146 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 147 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 */ |
| 148 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 149 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20 */ |
| 150 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 151 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x30 */ |
| 152 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 153 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40 */ |
| 154 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 155 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50 */ |
| 156 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 157 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x60 */ |
| 158 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 159 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x70 */ |
| 160 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 161 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80 */ |
| 162 | -1, -1, -1, -1, -1, -1, -1, -1, |
| 163 | -1, -1, -1, -1, -1, -1, 0x00, 0x01, /* 0x90 */ |
| 164 | -1, -1, 0x02, 0x03, -1, 0x04, 0x05, 0x06, |
| 165 | -1, -1, -1, -1, -1, -1, 0x07, 0x08, /* 0xA0 */ |
| 166 | -1, -1, -1, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, |
| 167 | -1, -1, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, /* 0xB0 */ |
| 168 | -1, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, |
| 169 | -1, -1, -1, -1, -1, -1, -1, -1, /* 0xC0 */ |
| 170 | -1, -1, -1, 0x1B, -1, 0x1C, 0x1D, 0x1E, |
| 171 | -1, -1, -1, 0x1F, -1, -1, 0x20, 0x21, /* 0xD0 */ |
| 172 | -1, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, |
| 173 | -1, -1, -1, -1, -1, 0x29, 0x2A, 0x2B, /* 0xE0 */ |
| 174 | -1, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, |
| 175 | -1, -1, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, /* 0xF0 */ |
| 176 | -1, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F |
| 177 | }; |
| 178 | |
| 179 | static const UINT8 blk1[] = |
| 180 | { |
| 181 | /*0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 182 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 183 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 184 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 185 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 186 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 187 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 188 | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 189 | 0xFF, 0xFF, 0xD5, 0xAA, 0x96*/ |
| 190 | 0xFF, |
| 191 | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 192 | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 193 | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 194 | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 195 | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 196 | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 197 | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 198 | 0xD5, 0xAA, 0x96 |
| 199 | }; |
| 200 | static const UINT8 blk2[] = |
| 201 | { |
| 202 | 0xDE, 0xAA, 0xFF, 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, 0xD5, 0xAA, 0xAD |
| 203 | }; |
| 204 | static const UINT8 blk3[] = |
| 205 | { |
| 206 | 0xDE, 0xAA, 0xFF |
| 207 | }; |
| 208 | |
| 209 | |
| 210 | |
| 211 | static struct apple35_tag *get_apple35_tag(floppy_image *floppy) |
| 212 | { |
| 213 | struct apple35_tag *tag; |
| 214 | tag = (struct apple35_tag *) floppy_tag(floppy); |
| 215 | return tag; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | |
| 220 | int apple35_sectors_per_track(floppy_image *image, int track) |
| 221 | { |
| 222 | int sectors; |
| 223 | |
| 224 | assert(track >= 0); |
| 225 | assert(track < ARRAY_LENGTH(apple35_tracklen_800kb)); |
| 226 | |
| 227 | if (get_apple35_tag(image)->is_1440k) |
| 228 | sectors = 18; |
| 229 | else |
| 230 | sectors = apple35_tracklen_800kb[track]; |
| 231 | return sectors; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | |
| 236 | /* |
| 237 | converts data to its nibblized representation, and generate checksum |
| 238 | |
| 239 | now handles 524-byte-long sectors |
| 240 | |
| 241 | tag data IS important, since it allows data recovery when the catalog is trashed |
| 242 | */ |
| 243 | static void sony_nibblize35(const UINT8 *in, UINT8 *nib_ptr, UINT8 *csum) |
| 244 | { |
| 245 | int i, j; |
| 246 | UINT32 c1, c2, c3, c4; |
| 247 | UINT8 val; |
| 248 | UINT8 w1, w2, w3, w4; |
| 249 | UINT8 b1[175], b2[175], b3[175]; |
| 250 | |
| 251 | /* Copy from the user's buffer to our buffer, while computing |
| 252 | * the three-byte data checksum |
| 253 | */ |
| 254 | |
| 255 | i = 0; |
| 256 | j = 0; |
| 257 | c1 = 0; |
| 258 | c2 = 0; |
| 259 | c3 = 0; |
| 260 | while (1) |
| 261 | { |
| 262 | c1 = (c1 & 0xFF) << 1; |
| 263 | if (c1 & 0x0100) |
| 264 | c1++; |
| 265 | |
| 266 | val = in[i++]; |
| 267 | c3 += val; |
| 268 | if (c1 & 0x0100) |
| 269 | { |
| 270 | c3++; |
| 271 | c1 &= 0xFF; |
| 272 | } |
| 273 | b1[j] = (val ^ c1) & 0xFF; |
| 274 | |
| 275 | val = in[i++]; |
| 276 | c2 += val; |
| 277 | if (c3 > 0xFF) |
| 278 | { |
| 279 | c2++; |
| 280 | c3 &= 0xFF; |
| 281 | } |
| 282 | b2[j] = (val ^ c3) & 0xFF; |
| 283 | |
| 284 | if (i == 524) break; |
| 285 | |
| 286 | val = in[i++]; |
| 287 | c1 += val; |
| 288 | if (c2 > 0xFF) |
| 289 | { |
| 290 | c1++; |
| 291 | c2 &= 0xFF; |
| 292 | } |
| 293 | b3[j] = (val ^ c2) & 0xFF; |
| 294 | j++; |
| 295 | } |
| 296 | c4 = ((c1 & 0xC0) >> 6) | ((c2 & 0xC0) >> 4) | ((c3 & 0xC0) >> 2); |
| 297 | b3[174] = 0; |
| 298 | |
| 299 | j = 0; |
| 300 | for (i = 0; i <= 174; i++) |
| 301 | { |
| 302 | w1 = b1[i] & 0x3F; |
| 303 | w2 = b2[i] & 0x3F; |
| 304 | w3 = b3[i] & 0x3F; |
| 305 | w4 = ((b1[i] & 0xC0) >> 2); |
| 306 | w4 |= ((b2[i] & 0xC0) >> 4); |
| 307 | w4 |= ((b3[i] & 0xC0) >> 6); |
| 308 | |
| 309 | nib_ptr[j++] = w4; |
| 310 | nib_ptr[j++] = w1; |
| 311 | nib_ptr[j++] = w2; |
| 312 | |
| 313 | if (i != 174) nib_ptr[j++] = w3; |
| 314 | } |
| 315 | |
| 316 | csum[0] = c1 & 0x3F; |
| 317 | csum[1] = c2 & 0x3F; |
| 318 | csum[2] = c3 & 0x3F; |
| 319 | csum[3] = c4 & 0x3F; |
| 320 | } |
| 321 | |
| 322 | |
| 323 | |
| 324 | /* |
| 325 | does the reverse process of sony_nibblize35 |
| 326 | */ |
| 327 | static void sony_denibblize35(UINT8 *out, const UINT8 *nib_ptr, UINT8 *checksum) |
| 328 | { |
| 329 | int i, j; |
| 330 | UINT32 c1,c2,c3,c4; |
| 331 | UINT8 val; |
| 332 | UINT8 w1,w2,w3=0,w4; |
| 333 | UINT8 b1[175],b2[175],b3[175]; |
| 334 | |
| 335 | j = 0; |
| 336 | for (i=0; i<=174; i++) |
| 337 | { |
| 338 | w4 = nib_ptr[j++]; |
| 339 | w1 = nib_ptr[j++]; |
| 340 | w2 = nib_ptr[j++]; |
| 341 | |
| 342 | if (i != 174) w3 = nib_ptr[j++]; |
| 343 | |
| 344 | b1[i] = (w1 & 0x3F) | ((w4 << 2) & 0xC0); |
| 345 | b2[i] = (w2 & 0x3F) | ((w4 << 4) & 0xC0); |
| 346 | b3[i] = (w3 & 0x3F) | ((w4 << 6) & 0xC0); |
| 347 | } |
| 348 | |
| 349 | /* Copy from the user's buffer to our buffer, while computing |
| 350 | * the three-byte data checksum |
| 351 | */ |
| 352 | |
| 353 | i = 0; |
| 354 | j = 0; |
| 355 | c1 = 0; |
| 356 | c2 = 0; |
| 357 | c3 = 0; |
| 358 | while (1) |
| 359 | { |
| 360 | c1 = (c1 & 0xFF) << 1; |
| 361 | if (c1 & 0x0100) |
| 362 | c1++; |
| 363 | |
| 364 | val = (b1[j] ^ c1) & 0xFF; |
| 365 | c3 += val; |
| 366 | if (c1 & 0x0100) |
| 367 | { |
| 368 | c3++; |
| 369 | c1 &= 0xFF; |
| 370 | } |
| 371 | out[i++] = val; |
| 372 | |
| 373 | val = (b2[j] ^ c3) & 0xFF; |
| 374 | c2 += val; |
| 375 | if (c3 > 0xFF) |
| 376 | { |
| 377 | c2++; |
| 378 | c3 &= 0xFF; |
| 379 | } |
| 380 | out[i++] = val; |
| 381 | |
| 382 | if (i == 524) break; |
| 383 | |
| 384 | val = (b3[j] ^ c2) & 0xFF; |
| 385 | c1 += val; |
| 386 | if (c2 > 0xFF) |
| 387 | { |
| 388 | c1++; |
| 389 | c2 &= 0xFF; |
| 390 | } |
| 391 | out[i++] = val; |
| 392 | j++; |
| 393 | } |
| 394 | c4 = ((c1 & 0xC0) >> 6) | ((c2 & 0xC0) >> 4) | ((c3 & 0xC0) >> 2); |
| 395 | b3[174] = 0; |
| 396 | |
| 397 | checksum[0] = c1 & 0x3F; |
| 398 | checksum[1] = c2 & 0x3F; |
| 399 | checksum[2] = c3 & 0x3F; |
| 400 | checksum[3] = c4 & 0x3F; |
| 401 | } |
| 402 | |
| 403 | |
| 404 | |
| 405 | void sony_filltrack(UINT8 *buffer, size_t buffer_len, size_t *pos, UINT8 data) |
| 406 | { |
| 407 | buffer[*pos / 8] &= 0xFF << (8 - (*pos % 8)); |
| 408 | buffer[*pos / 8] |= data >> (*pos % 8); |
| 409 | |
| 410 | *pos += 8; |
| 411 | *pos %= buffer_len * 8; |
| 412 | |
| 413 | buffer[*pos / 8] &= 0xFF >> (*pos % 8); |
| 414 | buffer[*pos / 8] |= data << (8 - (*pos % 8)); |
| 415 | } |
| 416 | |
| 417 | |
| 418 | |
| 419 | UINT8 sony_fetchtrack(const UINT8 *buffer, size_t buffer_len, size_t *pos) |
| 420 | { |
| 421 | UINT8 data; |
| 422 | |
| 423 | data = buffer[*pos / 8] << (*pos % 8); |
| 424 | *pos += 8; |
| 425 | *pos %= (buffer_len * 8); |
| 426 | data |= buffer[*pos / 8] >> (8 - (*pos % 8)); |
| 427 | |
| 428 | while ((data & 0x80) == 0) |
| 429 | { |
| 430 | /* this code looks weird because it isn't simply rotating the new bit |
| 431 | * in, but for some reason it won't work if I rotate the bit in; I |
| 432 | * have to match the algorithm used by the old code */ |
| 433 | data <<= 1; |
| 434 | data |= (buffer[*pos / 8] >> (8 - ((*pos % 8) + 1))); |
| 435 | (*pos)++; |
| 436 | *pos %= (buffer_len * 8); |
| 437 | } |
| 438 | return data; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | |
| 443 | static UINT32 apple35_get_offset(floppy_image *floppy, int head, int track, int sector, UINT32 *tag_offset) |
| 444 | { |
| 445 | int i; |
| 446 | UINT32 sector_index = 0; |
| 447 | struct apple35_tag *tag; |
| 448 | |
| 449 | tag = get_apple35_tag(floppy); |
| 450 | |
| 451 | if (track >= ARRAY_LENGTH(apple35_tracklen_800kb)) |
| 452 | return ~0; |
| 453 | if (head >= tag->sides) |
| 454 | return ~0; |
| 455 | if (sector >= apple35_sectors_per_track(floppy, track)) |
| 456 | return ~0; |
| 457 | |
| 458 | for (i = 0; i < track; i++) |
| 459 | sector_index += apple35_sectors_per_track(floppy, i); |
| 460 | sector_index *= tag->sides; |
| 461 | if (head) |
| 462 | sector_index += apple35_sectors_per_track(floppy, i); |
| 463 | sector_index += sector; |
| 464 | |
| 465 | if (tag_offset) |
| 466 | { |
| 467 | *tag_offset = sector_index * 12 + tag->tag_offset; |
| 468 | if (*tag_offset >= tag->tag_size) |
| 469 | *tag_offset = ~0; |
| 470 | } |
| 471 | return sector_index * 0x200 + tag->data_offset; |
| 472 | } |
| 473 | |
| 474 | |
| 475 | |
| 476 | static floperr_t apple35_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 477 | { |
| 478 | UINT32 data_offset; |
| 479 | // printf("Read sector: T %d S %d H %d\n", track, sector, head); |
| 480 | data_offset = apple35_get_offset(floppy, head, track, sector, NULL); |
| 481 | if (data_offset == ~0) |
| 482 | return FLOPPY_ERROR_SEEKERROR; |
| 483 | floppy_image_read(floppy, buffer, data_offset, buflen); |
| 484 | return FLOPPY_ERROR_SUCCESS; |
| 485 | } |
| 486 | |
| 487 | |
| 488 | |
| 489 | static floperr_t apple35_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 490 | { |
| 491 | UINT32 data_offset; |
| 492 | |
| 493 | data_offset = apple35_get_offset(floppy, head, track, sector, NULL); |
| 494 | if (data_offset == ~0) |
| 495 | return FLOPPY_ERROR_SEEKERROR; |
| 496 | floppy_image_write(floppy, buffer, data_offset, buflen); |
| 497 | return FLOPPY_ERROR_SUCCESS; |
| 498 | } |
| 499 | |
| 500 | |
| 501 | |
| 502 | static floperr_t apple35_read_sector_td(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 503 | { |
| 504 | floperr_t err; |
| 505 | UINT32 tag_offset = 0; |
| 506 | |
| 507 | assert(buflen == 524); |
| 508 | |
| 509 | // printf("Read sector TD: T %d S %d H %d\n", track, sector, head); |
| 510 | |
| 511 | /* first read the sector */ |
| 512 | err = apple35_read_sector(floppy, head, track, sector, ((UINT8 *) buffer) + 12, 512); |
| 513 | if (err) |
| 514 | return err; |
| 515 | |
| 516 | /* read the tag data, if possible */ |
| 517 | memset(buffer, '\0', 12); |
| 518 | apple35_get_offset(floppy, head, track, sector, &tag_offset); |
| 519 | if (tag_offset != ~0) |
| 520 | floppy_image_read(floppy, buffer, tag_offset, 12); |
| 521 | |
| 522 | return FLOPPY_ERROR_SUCCESS; |
| 523 | } |
| 524 | |
| 525 | |
| 526 | |
| 527 | static floperr_t apple35_write_sector_td(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 528 | { |
| 529 | floperr_t err; |
| 530 | UINT32 tag_offset = 0; |
| 531 | |
| 532 | assert(buflen == 524); |
| 533 | |
| 534 | /* first write the sector */ |
| 535 | err = apple35_write_sector(floppy, head, track, sector, ((const UINT8 *) buffer) + 12, 512, 0); |
| 536 | if (err) |
| 537 | return err; |
| 538 | |
| 539 | /* write the tag data, if possible */ |
| 540 | apple35_get_offset(floppy, head, track, sector, &tag_offset); |
| 541 | if (tag_offset != ~0) |
| 542 | floppy_image_write(floppy, buffer, tag_offset, 12); |
| 543 | |
| 544 | return FLOPPY_ERROR_SUCCESS; |
| 545 | } |
| 546 | |
| 547 | |
| 548 | |
| 549 | static floperr_t apple35_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 550 | { |
| 551 | *sector_length = 512; |
| 552 | return FLOPPY_ERROR_SUCCESS; |
| 553 | } |
| 554 | |
| 555 | |
| 556 | |
| 557 | static int apple35_get_heads_per_disk(floppy_image *floppy) |
| 558 | { |
| 559 | return get_apple35_tag(floppy)->sides; |
| 560 | } |
| 561 | |
| 562 | |
| 563 | |
| 564 | static int apple35_get_tracks_per_disk(floppy_image *floppy) |
| 565 | { |
| 566 | return 80; |
| 567 | } |
| 568 | |
| 569 | |
| 570 | |
| 571 | static UINT32 apple35_get_track_size(floppy_image *floppy, int head, int track) |
| 572 | { |
| 573 | if ((track < 0) || (track >= 80)) |
| 574 | return 0; |
| 575 | return apple35_sectors_per_track(floppy, track) * 800; |
| 576 | } |
| 577 | |
| 578 | |
| 579 | |
| 580 | static UINT8 calculate_side(int head, int track) |
| 581 | { |
| 582 | UINT8 side; |
| 583 | side = head ? 0x20 : 0x00; |
| 584 | if (track & 0x40) |
| 585 | side |= 0x01; |
| 586 | return side; |
| 587 | } |
| 588 | |
| 589 | |
| 590 | |
| 591 | static floperr_t apple35_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 592 | { |
| 593 | floperr_t err; |
| 594 | size_t pos = 0; |
| 595 | int sector_count, sector, i; |
| 596 | UINT8 sum, side; |
| 597 | struct apple35_tag *tag; |
| 598 | UINT8 sector_data[524]; |
| 599 | UINT8 nibble_data[699]; |
| 600 | UINT8 checksum[4]; |
| 601 | |
| 602 | tag = get_apple35_tag(floppy); |
| 603 | |
| 604 | if (track >= ARRAY_LENGTH(apple35_tracklen_800kb)) |
| 605 | return FLOPPY_ERROR_SEEKERROR; |
| 606 | if (offset != 0) |
| 607 | return FLOPPY_ERROR_UNSUPPORTED; |
| 608 | |
| 609 | memset(buffer, 0xFF, buflen); |
| 610 | sector_count = apple35_sectors_per_track(floppy, track); |
| 611 | side = calculate_side(head, track); |
| 612 | |
| 613 | for (sector = 0; sector < sector_count; sector++) |
| 614 | { |
| 615 | /* read the sector */ |
| 616 | err = apple35_read_sector_td(floppy, head, track, sector, sector_data, ARRAY_LENGTH(sector_data)); |
| 617 | if (err) |
| 618 | return err; |
| 619 | sony_nibblize35(sector_data, nibble_data, checksum); |
| 620 | |
| 621 | for (i = 0; i < ARRAY_LENGTH(blk1); i++) |
| 622 | sony_filltrack((UINT8*)buffer, buflen, &pos, blk1[i]); |
| 623 | |
| 624 | sum = (track ^ sector ^ side ^ tag->format_byte) & 0x3F; |
| 625 | |
| 626 | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[track & 0x3f]); |
| 627 | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[sector]); |
| 628 | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[side]); |
| 629 | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[tag->format_byte]); |
| 630 | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[sum]); |
| 631 | |
| 632 | for (i = 0; i < ARRAY_LENGTH(blk2); i++) |
| 633 | sony_filltrack((UINT8*)buffer, buflen, &pos, blk2[i]); |
| 634 | |
| 635 | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[sector]); |
| 636 | |
| 637 | for (i = 0; i < ARRAY_LENGTH(nibble_data); i++) |
| 638 | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[nibble_data[i]]); |
| 639 | |
| 640 | for (i = 3; i >= 0; i--) |
| 641 | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[checksum[i]]); |
| 642 | |
| 643 | for (i = 0; i < ARRAY_LENGTH(blk3); i++) |
| 644 | sony_filltrack((UINT8*)buffer, buflen, &pos, blk3[i]); |
| 645 | } |
| 646 | return FLOPPY_ERROR_SUCCESS; |
| 647 | } |
| 648 | |
| 649 | |
| 650 | |
| 651 | static floperr_t apple35_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 652 | { |
| 653 | floperr_t err; |
| 654 | size_t pos = 0; |
| 655 | int sector_count, sector, i, j; |
| 656 | struct apple35_tag *tag; |
| 657 | UINT8 sum, format_byte, val, side; |
| 658 | UINT32 found_sectors = 0; |
| 659 | UINT8 sector_data[524]; |
| 660 | UINT8 nibble_data[699]; |
| 661 | UINT8 checksum[4]; |
| 662 | |
| 663 | tag = get_apple35_tag(floppy); |
| 664 | |
| 665 | if (track >= ARRAY_LENGTH(apple35_tracklen_800kb)) |
| 666 | return FLOPPY_ERROR_SEEKERROR; |
| 667 | if (offset != 0) |
| 668 | return FLOPPY_ERROR_UNSUPPORTED; |
| 669 | |
| 670 | sector_count = apple35_sectors_per_track(floppy, track); |
| 671 | side = calculate_side(head, track); |
| 672 | |
| 673 | /* do 2 rotations, in case the bit slip stuff prevent us to read the first sector */ |
| 674 | for (j = 0; j < (buflen * 2); j++) |
| 675 | { |
| 676 | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xD5) |
| 677 | continue; |
| 678 | j++; |
| 679 | |
| 680 | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAA) |
| 681 | continue; |
| 682 | j++; |
| 683 | |
| 684 | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0x96) |
| 685 | continue; |
| 686 | j++; |
| 687 | |
| 688 | if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != (track & 0x3F)) |
| 689 | continue; |
| 690 | j++; |
| 691 | |
| 692 | sector = rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)]; |
| 693 | if ((sector < 0) || (sector >= sector_count)) |
| 694 | continue; |
| 695 | j++; |
| 696 | |
| 697 | if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != side) |
| 698 | continue; |
| 699 | j++; |
| 700 | |
| 701 | format_byte = rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)]; |
| 702 | if (format_byte != tag->format_byte) |
| 703 | { |
| 704 | /* this is an error, but not THAT critical, I guess */ |
| 705 | } |
| 706 | j++; |
| 707 | |
| 708 | sum = track ^ sector ^ side ^ format_byte; |
| 709 | if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != sum) |
| 710 | continue; |
| 711 | j++; |
| 712 | |
| 713 | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xDE) |
| 714 | continue; |
| 715 | j++; |
| 716 | |
| 717 | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAA) |
| 718 | continue; |
| 719 | j++; |
| 720 | |
| 721 | while((val = sony_fetchtrack((UINT8*)buffer, buflen, &pos)) == 0xFF) |
| 722 | j++; |
| 723 | if (val != 0xD5) |
| 724 | continue; /* lost bit slip mark! */ |
| 725 | j++; |
| 726 | |
| 727 | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAA) |
| 728 | continue; |
| 729 | j++; |
| 730 | |
| 731 | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAD) |
| 732 | continue; |
| 733 | j++; |
| 734 | |
| 735 | /* should this be regarded as a critical error ??? */ |
| 736 | if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != sector) |
| 737 | continue; |
| 738 | j++; |
| 739 | |
| 740 | for (i = 0; i < ARRAY_LENGTH(nibble_data); i++) |
| 741 | { |
| 742 | nibble_data[i] = rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)]; |
| 743 | j++; |
| 744 | } |
| 745 | |
| 746 | for (i = 3; i >= 0; i--) |
| 747 | { |
| 748 | /* should be checking checksum */ |
| 749 | sony_fetchtrack((UINT8*)buffer, buflen, &pos); |
| 750 | } |
| 751 | |
| 752 | sony_fetchtrack((UINT8*)buffer, buflen, &pos); /* should get 0xDE */ |
| 753 | sony_fetchtrack((UINT8*)buffer, buflen, &pos); /* should get 0xAA */ |
| 754 | sony_fetchtrack((UINT8*)buffer, buflen, &pos); /* should get 0xFF */ |
| 755 | |
| 756 | /* did we already write this sector? */ |
| 757 | if ((found_sectors & (1 << sector)) == 0) |
| 758 | { |
| 759 | sony_denibblize35(sector_data, nibble_data, checksum); |
| 760 | |
| 761 | /* write the sector */ |
| 762 | err = apple35_write_sector_td(floppy, head, track, sector, sector_data, ARRAY_LENGTH(sector_data), 0); |
| 763 | if (err) |
| 764 | return err; |
| 765 | |
| 766 | found_sectors |= 1 << sector; |
| 767 | } |
| 768 | } |
| 769 | return FLOPPY_ERROR_SUCCESS; |
| 770 | } |
| 771 | |
| 772 | |
| 773 | |
| 774 | static floperr_t apple35_construct(floppy_image *floppy, UINT32 data_offset, UINT32 data_size, |
| 775 | UINT32 tag_offset, UINT32 tag_size, INT16 format_byte, UINT8 sides, int is_1440k) |
| 776 | { |
| 777 | struct apple35_tag *tag; |
| 778 | struct FloppyCallbacks *format; |
| 779 | |
| 780 | /* figure out format byte if not specified */ |
| 781 | if (format_byte < 0) |
| 782 | { |
| 783 | switch(sides) |
| 784 | { |
| 785 | case 1: |
| 786 | format_byte = 0x02; |
| 787 | break; |
| 788 | case 2: |
| 789 | format_byte = 0x22; |
| 790 | break; |
| 791 | default: |
| 792 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | /* create tag */ |
| 797 | tag = (struct apple35_tag *) floppy_create_tag(floppy, sizeof(struct apple35_tag)); |
| 798 | if (!tag) |
| 799 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 800 | tag->data_offset = data_offset; |
| 801 | tag->data_size = data_size; |
| 802 | tag->tag_offset = tag_offset; |
| 803 | tag->tag_size = tag_size; |
| 804 | tag->format_byte = (UINT8) format_byte; |
| 805 | tag->sides = sides; |
| 806 | tag->is_1440k = is_1440k ? 1 : 0; |
| 807 | |
| 808 | /* set up format callbacks */ |
| 809 | format = floppy_callbacks(floppy); |
| 810 | format->read_sector = apple35_read_sector; |
| 811 | format->write_sector = apple35_write_sector; |
| 812 | format->read_track = apple35_read_track; |
| 813 | format->write_track = apple35_write_track; |
| 814 | format->get_sector_length = apple35_get_sector_length; |
| 815 | format->get_heads_per_disk = apple35_get_heads_per_disk; |
| 816 | format->get_tracks_per_disk = apple35_get_tracks_per_disk; |
| 817 | format->get_track_size = apple35_get_track_size; |
| 818 | return FLOPPY_ERROR_SUCCESS; |
| 819 | } |
| 820 | |
| 821 | |
| 822 | |
| 823 | /* ----------------------------------------------------------------------- */ |
| 824 | |
| 825 | static FLOPPY_IDENTIFY(apple35_raw_identify) |
| 826 | { |
| 827 | UINT64 size; |
| 828 | size = floppy_image_size(floppy); |
| 829 | *vote = ((size == 80*1*10*512) || (size == 80*2*10*512) || (size == (80*2*18*512)+84) |
| 830 | || (size == 80*2*18*512)) ? 100 : 0; |
| 831 | return FLOPPY_ERROR_SUCCESS; |
| 832 | } |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | static FLOPPY_CONSTRUCT(apple35_raw_construct) |
| 838 | { |
| 839 | UINT64 size; |
| 840 | UINT8 sides; |
| 841 | int is_1440k; |
| 842 | |
| 843 | if (params) |
| 844 | { |
| 845 | /* create */ |
| 846 | sides = option_resolution_lookup_int(params, PARAM_HEADS); |
| 847 | size = 80*sides*10*512; |
| 848 | is_1440k = FALSE; |
| 849 | } |
| 850 | else |
| 851 | { |
| 852 | /* load */ |
| 853 | size = floppy_image_size(floppy); |
| 854 | if (size == 80*1*10*512) |
| 855 | { |
| 856 | sides = 1; |
| 857 | is_1440k = FALSE; |
| 858 | } |
| 859 | else if ((size == 80*2*10*512) || (size == 80*2*18*512) || (size == (80*2*18*512)+84)) |
| 860 | { |
| 861 | sides = 2; |
| 862 | is_1440k = (size == 80*2*18*512) || (size == (80*2*18*512)+84); |
| 863 | } |
| 864 | else |
| 865 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 866 | } |
| 867 | |
| 868 | return apple35_construct(floppy, 0, (UINT32) size, 0, 0, -1, sides, is_1440k); |
| 869 | } |
| 870 | |
| 871 | |
| 872 | |
| 873 | /* ----------------------------------------------------------------------- */ |
| 874 | |
| 875 | struct header_diskcopy |
| 876 | { |
| 877 | UINT8 disk_name[64]; /* name of the disk */ |
| 878 | UINT32 data_size; /* total size of data for all sectors (512*number_of_sectors) */ |
| 879 | UINT32 tag_size; /* total size of tag data for all sectors (12*number_of_sectors for GCR 3.5" floppies, 20*number_of_sectors for HD20, 0 otherwise) */ |
| 880 | UINT32 data_checksum; /* CRC32 checksum of all sector data */ |
| 881 | UINT32 tag_checksum; /* CRC32 checksum of all tag data */ |
| 882 | UINT8 disk_format; /* 0 = 400K, 1 = 800K, 2 = 720K, 3 = 1440K (other values reserved) */ |
| 883 | UINT8 format_byte; /* should be $00 Apple II, $01 Lisa, $02 Mac MFS ??? */ |
| 884 | /* $12 = 400K, $22 = >400K Macintosh (DiskCopy uses this value for |
| 885 | all Apple II disks not 800K in size, and even for some of those), |
| 886 | $24 = 800K Apple II disk */ |
| 887 | UINT16 magic; /* always $0100 (otherwise, the file may be in a different format. */ |
| 888 | }; |
| 889 | |
| 890 | |
| 891 | |
| 892 | static floperr_t apple35_diskcopy_headerdecode(floppy_image *floppy, UINT32 *data_offset, |
| 893 | UINT32 *data_size, UINT32 *tag_offset, UINT32 *tag_size, UINT8 *format_byte, UINT8 *sides) |
| 894 | { |
| 895 | UINT64 size; |
| 896 | struct header_diskcopy header; |
| 897 | |
| 898 | if (data_offset) |
| 899 | *data_offset = 0; |
| 900 | if (data_size) |
| 901 | *data_size = 0; |
| 902 | if (tag_offset) |
| 903 | *tag_offset = 0; |
| 904 | if (tag_size) |
| 905 | *tag_size = 0; |
| 906 | if (format_byte) |
| 907 | *format_byte = 0; |
| 908 | if (sides) |
| 909 | *sides = 0; |
| 910 | |
| 911 | size = floppy_image_size(floppy); |
| 912 | if (size < sizeof(struct header_diskcopy)) |
| 913 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 914 | |
| 915 | floppy_image_read(floppy, &header, 0, sizeof(header)); |
| 916 | |
| 917 | header.data_size = BIG_ENDIANIZE_INT32(header.data_size); |
| 918 | header.tag_size = BIG_ENDIANIZE_INT32(header.tag_size); |
| 919 | header.data_checksum = BIG_ENDIANIZE_INT32(header.data_checksum); |
| 920 | header.tag_checksum = BIG_ENDIANIZE_INT32(header.tag_checksum); |
| 921 | header.magic = BIG_ENDIANIZE_INT16(header.magic); |
| 922 | |
| 923 | if (header.disk_name[0] >= sizeof(header.disk_name)) |
| 924 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 925 | if (header.magic != 0x0100) |
| 926 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 927 | if (size != (header.data_size + header.tag_size + sizeof(header))) |
| 928 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 929 | |
| 930 | if (header.data_size == 80*1*10*512) |
| 931 | { |
| 932 | if (sides) |
| 933 | *sides = 1; |
| 934 | } |
| 935 | else if (header.data_size == 80*2*10*512) |
| 936 | { |
| 937 | if (sides) |
| 938 | *sides = 2; |
| 939 | } |
| 940 | else |
| 941 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 942 | |
| 943 | if (data_offset) |
| 944 | *data_offset = sizeof(header); |
| 945 | if (data_size) |
| 946 | *data_size = header.data_size; |
| 947 | if (tag_offset) |
| 948 | *tag_offset = sizeof(header) + header.data_size; |
| 949 | if (tag_size) |
| 950 | *tag_size = header.tag_size; |
| 951 | if (format_byte) |
| 952 | *format_byte = header.format_byte; |
| 953 | return FLOPPY_ERROR_SUCCESS; |
| 954 | } |
| 955 | |
| 956 | |
| 957 | |
| 958 | static FLOPPY_IDENTIFY(apple35_diskcopy_identify) |
| 959 | { |
| 960 | *vote = apple35_diskcopy_headerdecode(floppy, NULL, NULL, NULL, NULL, NULL, NULL) ? 0 : 100; |
| 961 | return FLOPPY_ERROR_SUCCESS; |
| 962 | } |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | static FLOPPY_CONSTRUCT(apple35_diskcopy_construct) |
| 968 | { |
| 969 | floperr_t err; |
| 970 | UINT8 format_byte, sides; |
| 971 | UINT32 data_offset, data_size; |
| 972 | UINT32 tag_offset, tag_size; |
| 973 | INT16 format_byte_param = -1; |
| 974 | struct header_diskcopy header; |
| 975 | |
| 976 | if (params) |
| 977 | { |
| 978 | /* create */ |
| 979 | sides = option_resolution_lookup_int(params, PARAM_HEADS); |
| 980 | |
| 981 | data_size = 80*sides*10*512; |
| 982 | tag_size = 80*sides*10*12; |
| 983 | |
| 984 | memset(&header, 0, sizeof(header)); |
| 985 | header.data_size = BIG_ENDIANIZE_INT32(data_size); |
| 986 | header.tag_size = BIG_ENDIANIZE_INT32(tag_size); |
| 987 | header.disk_format = (sides > 1) ? 1 : 0; |
| 988 | header.magic = BIG_ENDIANIZE_INT16(0x100); |
| 989 | |
| 990 | floppy_image_write(floppy, &header, 0, sizeof(header)); |
| 991 | floppy_image_write_filler(floppy, 0, sizeof(header), data_size + tag_size); |
| 992 | } |
| 993 | |
| 994 | /* load */ |
| 995 | err = apple35_diskcopy_headerdecode(floppy, &data_offset, &data_size, |
| 996 | &tag_offset, &tag_size, &format_byte, &sides); |
| 997 | if (err) |
| 998 | return err; |
| 999 | |
| 1000 | format_byte_param = format_byte; |
| 1001 | |
| 1002 | return apple35_construct(floppy, data_offset, data_size, |
| 1003 | tag_offset, tag_size, format_byte_param, sides, FALSE); |
| 1004 | } |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | /* ----------------------------------------------------------------------- */ |
| 1009 | |
| 1010 | struct header_2img |
| 1011 | { |
| 1012 | char magic[4]; /* '2IMG' */ |
| 1013 | char creator[4]; /* signature; 'MESS' for MESS */ |
| 1014 | UINT16 header_length; |
| 1015 | UINT16 version; |
| 1016 | UINT32 image_format; |
| 1017 | UINT32 flags; |
| 1018 | UINT32 block_count; |
| 1019 | UINT32 data_offset; |
| 1020 | UINT32 data_length; |
| 1021 | UINT32 comment_offset; |
| 1022 | UINT32 comment_length; |
| 1023 | UINT32 creator_offset; |
| 1024 | UINT32 creator_length; |
| 1025 | UINT32 padding[4]; |
| 1026 | }; |
| 1027 | |
| 1028 | #define IMAGE_FORMAT_DO 0 |
| 1029 | #define IMAGE_FORMAT_PO 1 |
| 1030 | #define IMAGE_FORMAT_NIB 2 |
| 1031 | #define IMAGE_FLAGS_LOCKED 0x80000000 |
| 1032 | |
| 1033 | |
| 1034 | |
| 1035 | static floperr_t apple35_2img_decode(floppy_image *floppy, UINT32 *image_format, |
| 1036 | UINT32 *data_offset, UINT32 *data_length) |
| 1037 | { |
| 1038 | struct header_2img header; |
| 1039 | UINT64 size; |
| 1040 | |
| 1041 | if (image_format) |
| 1042 | *image_format = 0; |
| 1043 | if (data_offset) |
| 1044 | *data_offset = 0; |
| 1045 | if (data_length) |
| 1046 | *data_length = 0; |
| 1047 | |
| 1048 | size = floppy_image_size(floppy); |
| 1049 | if (size < sizeof(header)) |
| 1050 | { |
| 1051 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1052 | } |
| 1053 | |
| 1054 | floppy_image_read(floppy, &header, 0, sizeof(header)); |
| 1055 | |
| 1056 | if (memcmp(header.magic, "2IMG", 4)) |
| 1057 | { |
| 1058 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1059 | } |
| 1060 | |
| 1061 | header.header_length = LITTLE_ENDIANIZE_INT16(header.header_length); |
| 1062 | header.version = LITTLE_ENDIANIZE_INT16(header.version); |
| 1063 | header.image_format = LITTLE_ENDIANIZE_INT32(header.image_format); |
| 1064 | header.flags = LITTLE_ENDIANIZE_INT32(header.flags); |
| 1065 | header.block_count = LITTLE_ENDIANIZE_INT32(header.block_count); |
| 1066 | header.data_offset = LITTLE_ENDIANIZE_INT32(header.data_offset); |
| 1067 | header.data_length = LITTLE_ENDIANIZE_INT32(header.data_length); |
| 1068 | header.comment_offset = LITTLE_ENDIANIZE_INT32(header.comment_offset); |
| 1069 | header.comment_length = LITTLE_ENDIANIZE_INT32(header.comment_length); |
| 1070 | header.creator_offset = LITTLE_ENDIANIZE_INT32(header.creator_offset); |
| 1071 | header.creator_length = LITTLE_ENDIANIZE_INT32(header.creator_length); |
| 1072 | |
| 1073 | // at least some images "in the wild" (e.g. TOSEC Minor Set 1) have big-endian data sizes |
| 1074 | // even though that's against the .2mg spec |
| 1075 | if (header.data_length == 0x800c00) |
| 1076 | { |
| 1077 | logerror("ap_dsk35: corrected bad-endian data length\n"); |
| 1078 | header.data_length = 0x0c8000; |
| 1079 | } |
| 1080 | |
| 1081 | if ((((UINT64) header.data_offset) + header.data_length) > size) |
| 1082 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1083 | if ((((UINT64) header.comment_offset) + header.comment_length) > size) |
| 1084 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1085 | if ((((UINT64) header.creator_offset) + header.creator_length) > size) |
| 1086 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1087 | if ((header.image_format != IMAGE_FORMAT_DO) && |
| 1088 | (header.image_format != IMAGE_FORMAT_PO) && |
| 1089 | (header.image_format != IMAGE_FORMAT_NIB)) |
| 1090 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1091 | |
| 1092 | if (image_format) |
| 1093 | *image_format = header.image_format; |
| 1094 | if (data_offset) |
| 1095 | *data_offset = header.data_offset; |
| 1096 | if (data_length) |
| 1097 | *data_length = header.data_length; |
| 1098 | return FLOPPY_ERROR_SUCCESS; |
| 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | |
| 1103 | static FLOPPY_IDENTIFY(apple35_2img_identify) |
| 1104 | { |
| 1105 | *vote = apple35_2img_decode(floppy, NULL, NULL, NULL) ? 0 : 100; |
| 1106 | return FLOPPY_ERROR_SUCCESS; |
| 1107 | } |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | static FLOPPY_CONSTRUCT(apple35_2img_construct) |
| 1112 | { |
| 1113 | floperr_t err; |
| 1114 | UINT32 image_format; |
| 1115 | UINT32 data_offset; |
| 1116 | UINT32 data_size; |
| 1117 | UINT8 sides = 2; |
| 1118 | struct header_2img header; |
| 1119 | |
| 1120 | if (params) |
| 1121 | { |
| 1122 | /* create */ |
| 1123 | sides = option_resolution_lookup_int(params, PARAM_HEADS); |
| 1124 | |
| 1125 | data_offset = sizeof(header); |
| 1126 | data_size = 80*sides*10*512; |
| 1127 | |
| 1128 | memset(&header, 0, sizeof(header)); |
| 1129 | header.header_length = LITTLE_ENDIANIZE_INT16(sizeof(header)); |
| 1130 | header.block_count = LITTLE_ENDIANIZE_INT32(80*sides*10); |
| 1131 | header.data_offset = LITTLE_ENDIANIZE_INT32(data_offset); |
| 1132 | header.data_length = LITTLE_ENDIANIZE_INT32(data_size); |
| 1133 | |
| 1134 | floppy_image_write(floppy, &header, 0, sizeof(header)); |
| 1135 | floppy_image_write_filler(floppy, 0, sizeof(header), data_size); |
| 1136 | } |
| 1137 | else |
| 1138 | { |
| 1139 | /* load */ |
| 1140 | err = apple35_2img_decode(floppy, &image_format, &data_offset, &data_size); |
| 1141 | if (err) |
| 1142 | return err; |
| 1143 | |
| 1144 | if (data_size == 80*1*10*512) |
| 1145 | sides = 1; /* single sided */ |
| 1146 | else if (data_size == 80*2*10*512) |
| 1147 | sides = 2; /* double sided */ |
| 1148 | else |
| 1149 | sides = 2; /* unknown... what to do... */ |
| 1150 | } |
| 1151 | |
| 1152 | return apple35_construct(floppy, data_offset, data_size, |
| 1153 | 0, 0, -1, sides, FALSE); |
| 1154 | } |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | FLOPPY_OPTIONS_START( apple35_mac ) |
| 1159 | FLOPPY_OPTION( apple35_raw, "dsk,img,image", "Apple raw 3.5\" disk image", apple35_raw_identify, apple35_raw_construct, NULL, |
| 1160 | HEADS([1]-2) |
| 1161 | TRACKS([80]) |
| 1162 | SECTOR_LENGTH([512]) |
| 1163 | FIRST_SECTOR_ID([0])) |
| 1164 | FLOPPY_OPTION( apple35_dc, "dc,dsk,img,image", "Apple DiskCopy disk image", apple35_diskcopy_identify, apple35_diskcopy_construct, NULL, |
| 1165 | HEADS([1]-2) |
| 1166 | TRACKS([80]) |
| 1167 | SECTOR_LENGTH([512]) |
| 1168 | FIRST_SECTOR_ID([0])) |
| 1169 | FLOPPY_OPTIONS_END |
| 1170 | |
| 1171 | FLOPPY_OPTIONS_START( apple35_iigs ) |
| 1172 | FLOPPY_OPTION( apple35_raw, "dsk,img,image", "Apple raw 3.5\" disk image", apple35_raw_identify, apple35_raw_construct, NULL, |
| 1173 | HEADS([1]-2) |
| 1174 | TRACKS([80]) |
| 1175 | SECTOR_LENGTH([512]) |
| 1176 | FIRST_SECTOR_ID([0])) |
| 1177 | FLOPPY_OPTION( apple35_dc, "dc,dsk,img,image", "Apple DiskCopy disk image", apple35_diskcopy_identify, apple35_diskcopy_construct, NULL, |
| 1178 | HEADS([1]-2) |
| 1179 | TRACKS([80]) |
| 1180 | SECTOR_LENGTH([512]) |
| 1181 | FIRST_SECTOR_ID([0])) |
| 1182 | FLOPPY_OPTION( apple35_2img, "2img,2mg", "Apple ][gs 2IMG disk image", apple35_2img_identify, apple35_2img_construct, NULL, |
| 1183 | HEADS([1]-2) |
| 1184 | TRACKS([80]) |
| 1185 | SECTOR_LENGTH([512]) |
| 1186 | FIRST_SECTOR_ID([0])) |
| 1187 | FLOPPY_OPTIONS_END |
src/lib/formats/d64_dsk.c
| r0 | r11458 | |
| 1 | /********************************************************************* |
| 2 | |
| 3 | formats/d64_dsk.c |
| 4 | |
| 5 | Floppy format code for Commodore 1541/2040/8050 disk images |
| 6 | |
| 7 | *********************************************************************/ |
| 8 | |
| 9 | /* |
| 10 | |
| 11 | TODO: |
| 12 | |
| 13 | - fix MAX_ERROR_SECTORS, it is too small for d80/d82 |
| 14 | - write to disk |
| 15 | - disk errors 24, 25, 26, 28, 74 |
| 16 | - variable gaps |
| 17 | |
| 18 | */ |
| 19 | |
| 20 | #define XTAL_16MHz 16000000 |
| 21 | #define XTAL_12MHz 12000000 |
| 22 | #include "g64_dsk.h" |
| 23 | #include "flopimg.h" |
| 24 | #include "d64_dsk.h" |
| 25 | |
| 26 | /*************************************************************************** |
| 27 | PARAMETERS |
| 28 | ***************************************************************************/ |
| 29 | |
| 30 | #define LOG 1 |
| 31 | |
| 32 | #define MAX_HEADS 2 |
| 33 | #define MAX_TRACKS 84 |
| 34 | #define MAX_ERROR_SECTORS 802 // TODO this is too small for .d80 files |
| 35 | #define SECTOR_SIZE 256 |
| 36 | #define SECTOR_SIZE_GCR 368 |
| 37 | |
| 38 | #define INVALID_OFFSET 0xbadbad |
| 39 | |
| 40 | #define D64_SIZE_35_TRACKS 174848 |
| 41 | #define D64_SIZE_35_TRACKS_WITH_ERRORS 175531 |
| 42 | #define D64_SIZE_40_TRACKS 196608 |
| 43 | #define D64_SIZE_40_TRACKS_WITH_ERRORS 197376 |
| 44 | #define D64_SIZE_42_TRACKS 205312 |
| 45 | #define D64_SIZE_42_TRACKS_WITH_ERRORS 206114 |
| 46 | #define D67_SIZE_35_TRACKS 176640 |
| 47 | #define D71_SIZE_70_TRACKS 349696 |
| 48 | #define D71_SIZE_70_TRACKS_WITH_ERRORS 351062 |
| 49 | #define D80_SIZE_77_TRACKS 533248 |
| 50 | #define D82_SIZE_154_TRACKS 1066496 |
| 51 | |
| 52 | enum |
| 53 | { |
| 54 | DOS1, |
| 55 | DOS2, |
| 56 | DOS25 |
| 57 | }; |
| 58 | |
| 59 | static const char *const DOS_VERSION[] = { "1.0", "2.0", "2.5" }; |
| 60 | |
| 61 | enum |
| 62 | { |
| 63 | ERROR_00 = 1, |
| 64 | ERROR_20, /* header block not found */ |
| 65 | ERROR_21, /* no sync character */ |
| 66 | ERROR_22, /* data block not present */ |
| 67 | ERROR_23, /* checksum error in data block */ |
| 68 | ERROR_24, /* write verify (on format) UNIMPLEMENTED */ |
| 69 | ERROR_25, /* write verify error UNIMPLEMENTED */ |
| 70 | ERROR_26, /* write protect on UNIMPLEMENTED */ |
| 71 | ERROR_27, /* checksum error in header block */ |
| 72 | ERROR_28, /* write error UNIMPLEMENTED */ |
| 73 | ERROR_29, /* disk ID mismatch */ |
| 74 | ERROR_74, /* disk not ready (no device 1) UNIMPLEMENTED */ |
| 75 | }; |
| 76 | |
| 77 | static const char *const ERROR_CODE[] = { "00", "00", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "74" }; |
| 78 | |
| 79 | static const UINT8 bin_2_gcr[] = |
| 80 | { |
| 81 | 0x0a, 0x0b, 0x12, 0x13, 0x0e, 0x0f, 0x16, 0x17, |
| 82 | 0x09, 0x19, 0x1a, 0x1b, 0x0d, 0x1d, 0x1e, 0x15 |
| 83 | }; |
| 84 | |
| 85 | /* This could be of use if we ever implement saving in .d64 format, to convert back GCR -> d64 */ |
| 86 | /* |
| 87 | static const int gcr_2_bin[] = |
| 88 | { |
| 89 | -1, -1, -1, -1, |
| 90 | -1, -1, -1, -1, |
| 91 | -1, 0x08, 0x00, 0x01, |
| 92 | -1, 0x0c, 0x04, 0x05, |
| 93 | -1, -1, 0x02, 0x03, |
| 94 | -1, 0x0f, 0x06, 0x07, |
| 95 | -1, 0x09, 0x0a, 0x0b, |
| 96 | -1, 0x0d, 0x0e, -1 |
| 97 | }; |
| 98 | */ |
| 99 | |
| 100 | static const int DOS1_SECTORS_PER_TRACK[] = |
| 101 | { |
| 102 | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, |
| 103 | 20, 20, 20, 20, 20, 20, 20, |
| 104 | 18, 18, 18, 18, 18, 18, |
| 105 | 17, 17, 17, 17, 17, |
| 106 | 17, 17, 17, 17, 17, |
| 107 | 17, 17 |
| 108 | }; |
| 109 | |
| 110 | static const int DOS1_SPEED_ZONE[] = |
| 111 | { |
| 112 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 113 | 2, 2, 2, 2, 2, 2, 2, |
| 114 | 1, 1, 1, 1, 1, 1, |
| 115 | 0, 0, 0, 0, 0, |
| 116 | 0, 0, 0, 0, 0, |
| 117 | 0, 0 |
| 118 | }; |
| 119 | |
| 120 | static const int DOS2_SECTORS_PER_TRACK[] = |
| 121 | { |
| 122 | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, |
| 123 | 19, 19, 19, 19, 19, 19, 19, |
| 124 | 18, 18, 18, 18, 18, 18, |
| 125 | 17, 17, 17, 17, 17, |
| 126 | 17, 17, 17, 17, 17, |
| 127 | 17, 17 |
| 128 | }; |
| 129 | |
| 130 | static const int DOS25_SECTORS_PER_TRACK[] = |
| 131 | { |
| 132 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, |
| 133 | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, /* 1-39 */ |
| 134 | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, /* 40-53 */ |
| 135 | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, /* 54-64 */ |
| 136 | 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, /* 65-77 */ |
| 137 | 23, 23, 23, 23, 23, 23, 23 /* 78-84 */ |
| 138 | }; |
| 139 | |
| 140 | static const int DOS25_SPEED_ZONE[] = |
| 141 | { |
| 142 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 143 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 1-39 */ |
| 144 | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 40-53 */ |
| 145 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 54-64 */ |
| 146 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 65-77 */ |
| 147 | 0, 0, 0, 0, 0, 0, 0 /* 78-84 */ |
| 148 | }; |
| 149 | |
| 150 | /*************************************************************************** |
| 151 | TYPE DEFINITIONS |
| 152 | ***************************************************************************/ |
| 153 | |
| 154 | struct d64dsk_tag |
| 155 | { |
| 156 | int dos; /* CBM DOS version */ |
| 157 | int heads; /* number of physical heads */ |
| 158 | int tracks; /* number of physical tracks */ |
| 159 | int dos_tracks; /* number of logical tracks */ |
| 160 | int track_offset[MAX_HEADS][MAX_TRACKS]; /* offset within image for each physical track */ |
| 161 | UINT32 speed_zone[MAX_TRACKS]; /* speed zone for each physical track */ |
| 162 | bool has_errors; /* flag to check for available error codes */ |
| 163 | UINT8 error[MAX_ERROR_SECTORS]; /* error code for each logical sector */ |
| 164 | int error_offset[MAX_HEADS][MAX_TRACKS]; /* offset within error array for sector 0 of each logical track */ |
| 165 | |
| 166 | UINT8 id1, id2; /* DOS disk format ID */ |
| 167 | }; |
| 168 | |
| 169 | /*************************************************************************** |
| 170 | INLINE FUNCTIONS |
| 171 | ***************************************************************************/ |
| 172 | |
| 173 | INLINE struct d64dsk_tag *get_tag(floppy_image *floppy) |
| 174 | { |
| 175 | return (d64dsk_tag *)floppy_tag(floppy); |
| 176 | } |
| 177 | |
| 178 | INLINE float get_dos_track(int track) |
| 179 | { |
| 180 | return ((float)track / 2) + 1; |
| 181 | } |
| 182 | |
| 183 | /*************************************************************************** |
| 184 | IMPLEMENTATION |
| 185 | ***************************************************************************/ |
| 186 | |
| 187 | /*------------------------------------------------- |
| 188 | d64_get_heads_per_disk - returns the number |
| 189 | of heads in the disk image |
| 190 | -------------------------------------------------*/ |
| 191 | |
| 192 | static int d64_get_heads_per_disk(floppy_image *floppy) |
| 193 | { |
| 194 | return get_tag(floppy)->heads; |
| 195 | } |
| 196 | |
| 197 | /*------------------------------------------------- |
| 198 | d64_get_tracks_per_disk - returns the number |
| 199 | of DOS tracks in the disk image |
| 200 | -------------------------------------------------*/ |
| 201 | |
| 202 | static int d64_get_tracks_per_disk(floppy_image *floppy) |
| 203 | { |
| 204 | return get_tag(floppy)->tracks; |
| 205 | } |
| 206 | |
| 207 | /*------------------------------------------------- |
| 208 | d64_get_sectors_per_track - returns the number |
| 209 | of sectors per given track |
| 210 | -------------------------------------------------*/ |
| 211 | |
| 212 | static int d64_get_sectors_per_track(floppy_image *floppy, int head, int track) |
| 213 | { |
| 214 | int sectors_per_track = 0; |
| 215 | |
| 216 | switch (get_tag(floppy)->dos) |
| 217 | { |
| 218 | case DOS1: sectors_per_track = DOS1_SECTORS_PER_TRACK[track / 2]; break; |
| 219 | case DOS2: sectors_per_track = DOS2_SECTORS_PER_TRACK[track / 2]; break; |
| 220 | case DOS25: sectors_per_track = DOS25_SECTORS_PER_TRACK[track]; break; |
| 221 | } |
| 222 | |
| 223 | return sectors_per_track; |
| 224 | } |
| 225 | |
| 226 | /*------------------------------------------------- |
| 227 | get_track_offset - returns the offset within |
| 228 | the disk image for a given track |
| 229 | -------------------------------------------------*/ |
| 230 | |
| 231 | static floperr_t get_track_offset(floppy_image *floppy, int head, int track, UINT64 *offset) |
| 232 | { |
| 233 | struct d64dsk_tag *tag = get_tag(floppy); |
| 234 | UINT64 offs = 0; |
| 235 | |
| 236 | if ((track < 0) || (track >= tag->tracks)) |
| 237 | return FLOPPY_ERROR_SEEKERROR; |
| 238 | |
| 239 | offs = tag->track_offset[head][track]; |
| 240 | |
| 241 | if (offset) |
| 242 | *offset = offs; |
| 243 | |
| 244 | return FLOPPY_ERROR_SUCCESS; |
| 245 | } |
| 246 | |
| 247 | /*------------------------------------------------- |
| 248 | d64_get_track_size - returns the track size |
| 249 | -------------------------------------------------*/ |
| 250 | |
| 251 | static UINT32 d64_get_track_size(floppy_image *floppy, int head, int track) |
| 252 | { |
| 253 | struct d64dsk_tag *tag = get_tag(floppy); |
| 254 | |
| 255 | if (tag->track_offset[head][track] == INVALID_OFFSET) |
| 256 | return 0; |
| 257 | |
| 258 | /* determine number of sectors per track */ |
| 259 | int sectors_per_track = d64_get_sectors_per_track(floppy, head, track); |
| 260 | |
| 261 | /* allocate temporary GCR track data buffer */ |
| 262 | UINT32 track_length = sectors_per_track * SECTOR_SIZE_GCR; |
| 263 | |
| 264 | return track_length; |
| 265 | } |
| 266 | |
| 267 | /*------------------------------------------------- |
| 268 | get_sector_error_code - returns the error |
| 269 | code for the given sector |
| 270 | -------------------------------------------------*/ |
| 271 | |
| 272 | static int get_sector_error_code(floppy_image *floppy, int head, int dos_track, int sector) |
| 273 | { |
| 274 | struct d64dsk_tag *tag = get_tag(floppy); |
| 275 | |
| 276 | if (!tag->has_errors) |
| 277 | return ERROR_00; |
| 278 | |
| 279 | int sector_error = tag->error[tag->error_offset[head][dos_track] + sector]; // TODO index out of bounds!!! |
| 280 | |
| 281 | if (sector_error != ERROR_00) |
| 282 | { |
| 283 | logerror("D64 error %s head %u track %u sector %u\n", ERROR_CODE[sector_error], head, dos_track, sector); |
| 284 | } |
| 285 | |
| 286 | return sector_error; |
| 287 | } |
| 288 | |
| 289 | /*------------------------------------------------- |
| 290 | gcr_double_2_gcr - GCR decodes given data |
| 291 | -------------------------------------------------*/ |
| 292 | |
| 293 | /* gcr_double_2_gcr takes 4 bytes (a, b, c, d) and shuffles their nibbles to obtain 5 bytes in dest */ |
| 294 | /* The result is basically res = (enc(a) << 15) | (enc(b) << 10) | (enc(c) << 5) | enc(d) |
| 295 | * with res being 5 bytes long and enc(x) being the GCR encode of x. |
| 296 | * In fact, we store the result as five separate bytes in the dest argument |
| 297 | */ |
| 298 | |
| 299 | /* |
| 300 | |
| 301 | Commodore GCR format |
| 302 | |
| 303 | Original Encoded |
| 304 | 4 bits 5 bits |
| 305 | |
| 306 | 0000 -> 01010 = 0x0a |
| 307 | 0001 -> 01011 = 0x0b |
| 308 | 0010 -> 10010 = 0x12 |
| 309 | 0011 -> 10011 = 0x13 |
| 310 | 0100 -> 01110 = 0x0e |
| 311 | 0101 -> 01111 = 0x0f |
| 312 | 0110 -> 10110 = 0x16 |
| 313 | 0111 -> 10111 = 0x17 |
| 314 | 1000 -> 01001 = 0x09 |
| 315 | 1001 -> 11001 = 0x19 |
| 316 | 1010 -> 11010 = 0x1a |
| 317 | 1011 -> 11011 = 0x1b |
| 318 | 1100 -> 01101 = 0x0d |
| 319 | 1101 -> 11101 = 0x1d |
| 320 | 1110 -> 11110 = 0x1e |
| 321 | 1111 -> 10101 = 0x15 |
| 322 | |
| 323 | We use the encoded values in bytes because we use them to encode |
| 324 | groups of 4 bytes into groups of 5 bytes, below. |
| 325 | |
| 326 | */ |
| 327 | |
| 328 | static void gcr_double_2_gcr(UINT8 a, UINT8 b, UINT8 c, UINT8 d, UINT8 *dest) |
| 329 | { |
| 330 | UINT8 gcr[8]; |
| 331 | |
| 332 | /* Encode each nibble to 5 bits */ |
| 333 | gcr[0] = bin_2_gcr[a >> 4]; |
| 334 | gcr[1] = bin_2_gcr[a & 0x0f]; |
| 335 | gcr[2] = bin_2_gcr[b >> 4]; |
| 336 | gcr[3] = bin_2_gcr[b & 0x0f]; |
| 337 | gcr[4] = bin_2_gcr[c >> 4]; |
| 338 | gcr[5] = bin_2_gcr[c & 0x0f]; |
| 339 | gcr[6] = bin_2_gcr[d >> 4]; |
| 340 | gcr[7] = bin_2_gcr[d & 0x0f]; |
| 341 | |
| 342 | /* Re-order the encoded data to only keep the 5 lower bits of each byte */ |
| 343 | dest[0] = (gcr[0] << 3) | (gcr[1] >> 2); |
| 344 | dest[1] = (gcr[1] << 6) | (gcr[2] << 1) | (gcr[3] >> 4); |
| 345 | dest[2] = (gcr[3] << 4) | (gcr[4] >> 1); |
| 346 | dest[3] = (gcr[4] << 7) | (gcr[5] << 2) | (gcr[6] >> 3); |
| 347 | dest[4] = (gcr[6] << 5) | gcr[7]; |
| 348 | } |
| 349 | |
| 350 | /*------------------------------------------------- |
| 351 | d64_read_track - reads a full track from the |
| 352 | disk image |
| 353 | -------------------------------------------------*/ |
| 354 | |
| 355 | /* |
| 356 | |
| 357 | Commodore sector format |
| 358 | |
| 359 | SYNC FF * 5 |
| 360 | 08 |
| 361 | CHECKSUM sector ^ track ^ id2 ^ id1 |
| 362 | SECTOR 0..20 (2040), 0..28 (8050) |
| 363 | TRACK 1..35 (2040), 1..77 (8050), 1..70 (1571) |
| 364 | ID2 |
| 365 | ID1 |
| 366 | GAP 1 55 * 9 (2040), 55 * 8 (1541) |
| 367 | |
| 368 | SYNC FF * 5 |
| 369 | 07 |
| 370 | NEXT TRACK |
| 371 | NEXT SECTOR |
| 372 | 254 BYTES OF DATA |
| 373 | CHECKSUM |
| 374 | GAP 2 55 * 8..19 |
| 375 | |
| 376 | */ |
| 377 | |
| 378 | static floperr_t d64_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 379 | { |
| 380 | struct d64dsk_tag *tag = get_tag(floppy); |
| 381 | floperr_t err; |
| 382 | UINT64 track_offset; |
| 383 | |
| 384 | /* get track offset */ |
| 385 | err = get_track_offset(floppy, head, track, &track_offset); |
| 386 | |
| 387 | if (err) |
| 388 | return err; |
| 389 | |
| 390 | if (track_offset != INVALID_OFFSET) |
| 391 | { |
| 392 | UINT8 id1 = tag->id1; |
| 393 | UINT8 id2 = tag->id2; |
| 394 | int sectors_per_track; |
| 395 | UINT16 d64_track_size; |
| 396 | UINT8 *d64_track_data; |
| 397 | UINT16 gcr_track_size; |
| 398 | UINT8 *gcr_track_data; |
| 399 | UINT64 gcr_pos = 0; |
| 400 | |
| 401 | /* determine logical track number */ |
| 402 | int dos_track = get_dos_track(track); |
| 403 | |
| 404 | if (tag->dos == DOS25) |
| 405 | { |
| 406 | dos_track = track + 1; |
| 407 | } |
| 408 | |
| 409 | /* logical track numbers continue on the flip side */ |
| 410 | if (head == 1) dos_track += tag->dos_tracks; |
| 411 | |
| 412 | /* determine number of sectors per track */ |
| 413 | sectors_per_track = d64_get_sectors_per_track(floppy, head, track); |
| 414 | |
| 415 | /* allocate D64 track data buffer */ |
| 416 | d64_track_size = sectors_per_track * SECTOR_SIZE; |
| 417 | d64_track_data = (UINT8 *)alloca(d64_track_size); |
| 418 | |
| 419 | /* allocate temporary GCR track data buffer */ |
| 420 | gcr_track_size = sectors_per_track * SECTOR_SIZE_GCR; |
| 421 | gcr_track_data = (UINT8 *)alloca(gcr_track_size); |
| 422 | |
| 423 | if (buflen < gcr_track_size) { printf("D64 track buffer too small: %u!", (UINT32)buflen); exit(-1); } |
| 424 | |
| 425 | /* read D64 track data */ |
| 426 | floppy_image_read(floppy, d64_track_data, track_offset, d64_track_size); |
| 427 | |
| 428 | /* GCR encode D64 sector data */ |
| 429 | for (int sector = 0; sector < sectors_per_track; sector++) |
| 430 | { |
| 431 | // here we convert the sector data to gcr directly! |
| 432 | // IMPORTANT: errors in reading sectors can modify e.g. header info $01 & $05 |
| 433 | int sector_error = get_sector_error_code(floppy, head, track, sector); |
| 434 | |
| 435 | /* first we set the position at which sector data starts in the image */ |
| 436 | UINT64 d64_pos = sector * SECTOR_SIZE; |
| 437 | int i; |
| 438 | |
| 439 | /* |
| 440 | 1. Header sync FF FF FF FF FF (40 'on' bits, not GCR encoded) |
| 441 | 2. Header info 52 54 B5 29 4B 7A 5E 95 55 55 (10 GCR bytes) |
| 442 | 3. Header gap 55 55 55 55 55 55 55 55 55 (9 bytes, never read) |
| 443 | 4. Data sync FF FF FF FF FF (40 'on' bits, not GCR encoded) |
| 444 | 5. Data block 55...4A (325 GCR bytes) |
| 445 | 6. Inter-sector gap 55 55 55 55...55 55 (4 to 19 bytes, never read) |
| 446 | */ |
| 447 | |
| 448 | if (sector_error == ERROR_29) |
| 449 | id1 ^= 0xff; |
| 450 | |
| 451 | /* Header sync */ |
| 452 | if (sector_error != ERROR_21) |
| 453 | { |
| 454 | for (i = 0; i < 5; i++) |
| 455 | gcr_track_data[gcr_pos + i] = 0xff; |
| 456 | gcr_pos += 5; |
| 457 | } |
| 458 | |
| 459 | /* Header info */ |
| 460 | /* These are 8 bytes unencoded, which become 10 bytes encoded */ |
| 461 | // $00 - header block ID ($08) // this byte can be modified by error code 20 -> 0xff |
| 462 | // $01 - header block checksum (EOR of $02-$05) // this byte can be modified by error code 27 -> ^ 0xff |
| 463 | // $02 - Sector# of data block |
| 464 | // $03 - Track# of data block |
| 465 | UINT8 header_block_id = (sector_error == ERROR_20) ? 0xff : 0x08; |
| 466 | UINT8 header_block_checksum = sector ^ dos_track ^ id2 ^ id1; |
| 467 | |
| 468 | if (sector_error == ERROR_27) |
| 469 | header_block_checksum ^= 0xff; |
| 470 | |
| 471 | gcr_double_2_gcr(header_block_id, header_block_checksum, sector, dos_track, gcr_track_data + gcr_pos); |
| 472 | gcr_pos += 5; |
| 473 | |
| 474 | // $04 - Format ID byte #2 |
| 475 | // $05 - Format ID byte #1 |
| 476 | // $06 - $0F ("off" byte) |
| 477 | // $07 - $0F ("off" byte) |
| 478 | gcr_double_2_gcr(id2, id1, 0x0f, 0x0f, gcr_track_data + gcr_pos); |
| 479 | gcr_pos += 5; |
| 480 | |
| 481 | /* Header gap */ |
| 482 | for (i = 0; i < 9; i++) |
| 483 | gcr_track_data[gcr_pos + i] = 0x55; |
| 484 | gcr_pos += 9; |
| 485 | |
| 486 | /* Data sync */ |
| 487 | for (i = 0; i < 5; i++) |
| 488 | gcr_track_data[gcr_pos + i] = 0xff; |
| 489 | gcr_pos += 5; |
| 490 | |
| 491 | /* Data block */ |
| 492 | UINT8 data_block_id = (sector_error == ERROR_22) ? 0xff : 0x07; |
| 493 | |
| 494 | // we first need to calculate the checksum of the 256 bytes of the sector |
| 495 | UINT8 sector_checksum = d64_track_data[d64_pos]; |
| 496 | for (i = 1; i < 256; i++) |
| 497 | sector_checksum ^= d64_track_data[d64_pos + i]; |
| 498 | |
| 499 | if (sector_error == ERROR_23) |
| 500 | sector_checksum ^= 0xff; |
| 501 | |
| 502 | /* |
| 503 | $00 - data block ID ($07) |
| 504 | $01-100 - 256 bytes sector data |
| 505 | $101 - data block checksum (EOR of $01-100) |
| 506 | $102-103 - $00 ("off" bytes, to make the sector size a multiple of 5) |
| 507 | */ |
| 508 | gcr_double_2_gcr(data_block_id, d64_track_data[d64_pos], d64_track_data[d64_pos + 1], d64_track_data[d64_pos + 2], gcr_track_data + gcr_pos); |
| 509 | gcr_pos += 5; |
| 510 | |
| 511 | for (i = 1; i < 64; i++) |
| 512 | { |
| 513 | gcr_double_2_gcr(d64_track_data[d64_pos + 4 * i - 1], d64_track_data[d64_pos + 4 * i], |
| 514 | d64_track_data[d64_pos + 4 * i + 1], d64_track_data[d64_pos + 4 * i + 2], gcr_track_data + gcr_pos); |
| 515 | gcr_pos += 5; |
| 516 | } |
| 517 | |
| 518 | gcr_double_2_gcr(d64_track_data[d64_pos + 255], sector_checksum, 0x00, 0x00, gcr_track_data + gcr_pos); |
| 519 | gcr_pos += 5; |
| 520 | |
| 521 | /* Inter-sector gap */ |
| 522 | // "In tests that the author conducted on a real 1541 disk, gap sizes of 8 to 19 bytes were seen." |
| 523 | // Here we put 14 as an average... |
| 524 | for (i = 0; i < 14; i++) |
| 525 | gcr_track_data[gcr_pos + i] = 0x55; |
| 526 | gcr_pos += 14; |
| 527 | } |
| 528 | |
| 529 | /* copy GCR track data to buffer */ |
| 530 | memcpy(buffer, gcr_track_data, gcr_track_size); |
| 531 | } |
| 532 | else /* half tracks */ |
| 533 | { |
| 534 | /* set track length to 0 */ |
| 535 | memset(buffer, 0, buflen); |
| 536 | } |
| 537 | |
| 538 | return FLOPPY_ERROR_SUCCESS; |
| 539 | } |
| 540 | |
| 541 | /*------------------------------------------------- |
| 542 | d64_write_track - writes a full track to the |
| 543 | disk image |
| 544 | -------------------------------------------------*/ |
| 545 | |
| 546 | static floperr_t d64_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 547 | { |
| 548 | return FLOPPY_ERROR_UNSUPPORTED; |
| 549 | } |
| 550 | |
| 551 | /*------------------------------------------------- |
| 552 | d64_identify - identifies the disk image |
| 553 | -------------------------------------------------*/ |
| 554 | |
| 555 | static void d64_identify(floppy_image *floppy, int *dos, int *heads, int *tracks, bool *has_errors) |
| 556 | { |
| 557 | switch (floppy_image_size(floppy)) |
| 558 | { |
| 559 | /* 2040/3040 */ |
| 560 | case D67_SIZE_35_TRACKS: *dos = DOS1; *heads = 1; *tracks = 35; *has_errors = false; break; |
| 561 | |
| 562 | /* 4040/2031/1541/1551 */ |
| 563 | case D64_SIZE_35_TRACKS: *dos = DOS2; *heads = 1; *tracks = 35; *has_errors = false; break; |
| 564 | case D64_SIZE_35_TRACKS_WITH_ERRORS: *dos = DOS2; *heads = 1; *tracks = 35; *has_errors = true; break; |
| 565 | case D64_SIZE_40_TRACKS: *dos = DOS2; *heads = 1; *tracks = 40; *has_errors = false; break; |
| 566 | case D64_SIZE_40_TRACKS_WITH_ERRORS: *dos = DOS2; *heads = 1; *tracks = 40; *has_errors = true; break; |
| 567 | case D64_SIZE_42_TRACKS: *dos = DOS2; *heads = 1; *tracks = 42; *has_errors = false; break; |
| 568 | case D64_SIZE_42_TRACKS_WITH_ERRORS: *dos = DOS2; *heads = 1; *tracks = 42; *has_errors = true; break; |
| 569 | |
| 570 | /* 1571 */ |
| 571 | case D71_SIZE_70_TRACKS: *dos = DOS2; *heads = 2; *tracks = 35; *has_errors = false; break; |
| 572 | case D71_SIZE_70_TRACKS_WITH_ERRORS: *dos = DOS2; *heads = 2; *tracks = 35; *has_errors = true; break; |
| 573 | |
| 574 | /* 8050 */ |
| 575 | case D80_SIZE_77_TRACKS: *dos = DOS25; *heads = 1; *tracks = 77; *has_errors = false; break; |
| 576 | |
| 577 | /* 8250/SFD1001 */ |
| 578 | case D82_SIZE_154_TRACKS: *dos = DOS25; *heads = 2; *tracks = 77; *has_errors = false; break; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | /*------------------------------------------------- |
| 583 | FLOPPY_IDENTIFY( d64_dsk_identify ) |
| 584 | -------------------------------------------------*/ |
| 585 | |
| 586 | FLOPPY_IDENTIFY( d64_dsk_identify ) |
| 587 | { |
| 588 | int dos = 0, heads, tracks; |
| 589 | bool has_errors = false; |
| 590 | |
| 591 | *vote = 0; |
| 592 | |
| 593 | d64_identify(floppy, &dos, &heads, &tracks, &has_errors); |
| 594 | |
| 595 | if (dos == DOS2 && heads == 1) |
| 596 | { |
| 597 | *vote = 100; |
| 598 | } |
| 599 | |
| 600 | return FLOPPY_ERROR_SUCCESS; |
| 601 | } |
| 602 | |
| 603 | /*------------------------------------------------- |
| 604 | FLOPPY_IDENTIFY( d67_dsk_identify ) |
| 605 | -------------------------------------------------*/ |
| 606 | |
| 607 | FLOPPY_IDENTIFY( d67_dsk_identify ) |
| 608 | { |
| 609 | *vote = 0; |
| 610 | |
| 611 | if (floppy_image_size(floppy) == D67_SIZE_35_TRACKS) |
| 612 | { |
| 613 | *vote = 100; |
| 614 | } |
| 615 | |
| 616 | return FLOPPY_ERROR_SUCCESS; |
| 617 | } |
| 618 | |
| 619 | /*------------------------------------------------- |
| 620 | FLOPPY_IDENTIFY( d71_dsk_identify ) |
| 621 | -------------------------------------------------*/ |
| 622 | |
| 623 | FLOPPY_IDENTIFY( d71_dsk_identify ) |
| 624 | { |
| 625 | int heads = 0, tracks = 0, dos = -1; |
| 626 | bool has_errors = false; |
| 627 | |
| 628 | *vote = 0; |
| 629 | |
| 630 | d64_identify(floppy, &dos, &heads, &tracks, &has_errors); |
| 631 | |
| 632 | if (dos == DOS2 && heads == 2) |
| 633 | { |
| 634 | *vote = 100; |
| 635 | } |
| 636 | |
| 637 | return FLOPPY_ERROR_SUCCESS; |
| 638 | } |
| 639 | |
| 640 | /*------------------------------------------------- |
| 641 | FLOPPY_IDENTIFY( d80_dsk_identify ) |
| 642 | -------------------------------------------------*/ |
| 643 | |
| 644 | FLOPPY_IDENTIFY( d80_dsk_identify ) |
| 645 | { |
| 646 | *vote = 0; |
| 647 | |
| 648 | if (floppy_image_size(floppy) == D80_SIZE_77_TRACKS) |
| 649 | { |
| 650 | *vote = 100; |
| 651 | } |
| 652 | |
| 653 | return FLOPPY_ERROR_SUCCESS; |
| 654 | } |
| 655 | |
| 656 | /*------------------------------------------------- |
| 657 | FLOPPY_IDENTIFY( d82_dsk_identify ) |
| 658 | -------------------------------------------------*/ |
| 659 | |
| 660 | FLOPPY_IDENTIFY( d82_dsk_identify ) |
| 661 | { |
| 662 | *vote = 0; |
| 663 | |
| 664 | if (floppy_image_size(floppy) == D82_SIZE_154_TRACKS) |
| 665 | { |
| 666 | *vote = 100; |
| 667 | } |
| 668 | |
| 669 | return FLOPPY_ERROR_SUCCESS; |
| 670 | } |
| 671 | |
| 672 | /*------------------------------------------------- |
| 673 | FLOPPY_CONSTRUCT( d64_dsk_construct ) |
| 674 | -------------------------------------------------*/ |
| 675 | |
| 676 | FLOPPY_CONSTRUCT( d64_dsk_construct ) |
| 677 | { |
| 678 | struct FloppyCallbacks *callbacks; |
| 679 | struct d64dsk_tag *tag; |
| 680 | UINT8 id[2]; |
| 681 | |
| 682 | int track_offset = 0; |
| 683 | int head, track; |
| 684 | |
| 685 | int heads = 0, dos_tracks = 0, dos = 0; |
| 686 | bool has_errors = 0; |
| 687 | int errors_size = 0; |
| 688 | |
| 689 | if (params) |
| 690 | { |
| 691 | /* create not supported */ |
| 692 | return FLOPPY_ERROR_UNSUPPORTED; |
| 693 | } |
| 694 | |
| 695 | tag = (struct d64dsk_tag *) floppy_create_tag(floppy, sizeof(struct d64dsk_tag)); |
| 696 | |
| 697 | if (!tag) return FLOPPY_ERROR_OUTOFMEMORY; |
| 698 | |
| 699 | /* identify image type */ |
| 700 | d64_identify(floppy, &dos, &heads, &dos_tracks, &has_errors); |
| 701 | |
| 702 | tag->dos = dos; |
| 703 | tag->heads = heads; |
| 704 | tag->tracks = MAX_TRACKS; |
| 705 | tag->dos_tracks = dos_tracks; |
| 706 | tag->has_errors = has_errors; |
| 707 | |
| 708 | if (LOG) |
| 709 | { |
| 710 | logerror("D64 size: %04x\n", (UINT32)floppy_image_size(floppy)); |
| 711 | logerror("D64 heads: %u\n", heads); |
| 712 | logerror("D64 tracks: %u\n", dos_tracks); |
| 713 | logerror("D64 DOS version: %s\n", DOS_VERSION[dos]); |
| 714 | logerror("D64 error codes: %s\n", has_errors ? "yes" : "no"); |
| 715 | } |
| 716 | |
| 717 | /* clear track data offsets */ |
| 718 | for (head = 0; head < MAX_HEADS; head++) |
| 719 | { |
| 720 | for (track = 0; track < MAX_TRACKS; track++) |
| 721 | { |
| 722 | tag->track_offset[head][track] = INVALID_OFFSET; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | /* determine track data offsets */ |
| 727 | for (head = 0; head < heads; head++) |
| 728 | { |
| 729 | for (track = 0; track < tag->tracks; track++) |
| 730 | { |
| 731 | if (dos == DOS25) |
| 732 | { |
| 733 | if (track >= dos_tracks) |
| 734 | { |
| 735 | /* track out of range */ |
| 736 | tag->track_offset[head][track] = INVALID_OFFSET; |
| 737 | } |
| 738 | else |
| 739 | { |
| 740 | tag->track_offset[head][track] = track_offset; |
| 741 | tag->error_offset[head][track] = errors_size; |
| 742 | |
| 743 | track_offset += DOS25_SECTORS_PER_TRACK[track] * SECTOR_SIZE; |
| 744 | /* also store an error entry for each sector */ |
| 745 | errors_size += DOS25_SECTORS_PER_TRACK[track]; |
| 746 | |
| 747 | if (LOG) |
| 748 | { |
| 749 | logerror("D64 head %u track %u data offset: %04x\n", head, track + 1, tag->track_offset[head][track]); |
| 750 | if (has_errors) logerror("D64 head %u track %u error offset: %04x\n", head, track + 1, tag->error_offset[head][track]); |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | else |
| 755 | { |
| 756 | if ((track % 2) || ((track / 2) >= dos_tracks)) |
| 757 | { |
| 758 | /* half track or out of range */ |
| 759 | tag->track_offset[head][track] = INVALID_OFFSET; |
| 760 | } |
| 761 | else |
| 762 | { |
| 763 | /* full track */ |
| 764 | tag->track_offset[head][track] = track_offset; |
| 765 | tag->error_offset[head][track] = errors_size; |
| 766 | |
| 767 | if (dos == DOS1) |
| 768 | { |
| 769 | track_offset += DOS1_SECTORS_PER_TRACK[track / 2] * SECTOR_SIZE; |
| 770 | /* also store an error entry for each sector */ |
| 771 | errors_size += DOS1_SECTORS_PER_TRACK[track / 2]; |
| 772 | } |
| 773 | else |
| 774 | { |
| 775 | track_offset += DOS2_SECTORS_PER_TRACK[track / 2] * SECTOR_SIZE; |
| 776 | /* also store an error entry for each sector */ |
| 777 | errors_size += DOS2_SECTORS_PER_TRACK[track / 2]; |
| 778 | } |
| 779 | |
| 780 | if (LOG) |
| 781 | { |
| 782 | logerror("D64 head %u track %.1f data offset: %04x\n", head, get_dos_track(track), tag->track_offset[head][track]); |
| 783 | if (has_errors) logerror("D64 head %u track %.1f error offset: %04x\n", head, get_dos_track(track), tag->error_offset[head][track]); |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /* determine speed zones */ |
| 791 | for (track = 0; track < tag->tracks; track++) |
| 792 | { |
| 793 | if (dos == DOS25) |
| 794 | { |
| 795 | tag->speed_zone[track] = DOS25_SPEED_ZONE[track]; |
| 796 | |
| 797 | if (LOG) logerror("D64 track %u speed zone: %u\n", track + 1, tag->speed_zone[track]); |
| 798 | } |
| 799 | else |
| 800 | { |
| 801 | tag->speed_zone[track] = DOS1_SPEED_ZONE[track / 2]; |
| 802 | |
| 803 | if (LOG) logerror("D64 track %.1f speed zone: %u\n", get_dos_track(track), tag->speed_zone[track]); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | /* read format ID from directory */ |
| 808 | /* |
| 809 | id1, id2 are the same for extended d64 (i.e. with error tables), for d67 and for d71 |
| 810 | |
| 811 | for d81 they are at track 40 bytes 0x17 & 0x18 |
| 812 | for d80 & d82 they are at track 39 bytes 0x18 & 0x19 |
| 813 | */ |
| 814 | if (dos == DOS25) |
| 815 | floppy_image_read(floppy, id, tag->track_offset[0][38] + 0x18, 2); |
| 816 | else |
| 817 | floppy_image_read(floppy, id, tag->track_offset[0][34] + 0xa2, 2); |
| 818 | |
| 819 | tag->id1 = id[0]; |
| 820 | tag->id2 = id[1]; |
| 821 | |
| 822 | if (LOG) logerror("D64 format ID: %02x%02x\n", id[0], id[1]); |
| 823 | |
| 824 | /* read errors */ |
| 825 | if (tag->has_errors) |
| 826 | { |
| 827 | if (LOG) logerror("D64 error blocks: %u %u\n", errors_size, track_offset); |
| 828 | floppy_image_read(floppy, tag->error, track_offset, errors_size); |
| 829 | } |
| 830 | else |
| 831 | { |
| 832 | memset(tag->error, ERROR_00, MAX_ERROR_SECTORS); |
| 833 | } |
| 834 | |
| 835 | /* set callbacks */ |
| 836 | callbacks = floppy_callbacks(floppy); |
| 837 | |
| 838 | callbacks->read_track = d64_read_track; |
| 839 | callbacks->write_track = d64_write_track; |
| 840 | callbacks->get_heads_per_disk = d64_get_heads_per_disk; |
| 841 | callbacks->get_tracks_per_disk = d64_get_tracks_per_disk; |
| 842 | callbacks->get_track_size = d64_get_track_size; |
| 843 | |
| 844 | return FLOPPY_ERROR_SUCCESS; |
| 845 | } |
src/lib/formats/thom_dsk.c
| r0 | r11458 | |
| 1 | /********************************************************************* |
| 2 | |
| 3 | formats/thom_dsk.c |
| 4 | |
| 5 | Thomson disk images |
| 6 | |
| 7 | Based on work of Antoine Mine' |
| 8 | |
| 9 | *********************************************************************/ |
| 10 | |
| 11 | #include <string.h> |
| 12 | #include "thom_dsk.h" |
| 13 | #include "basicdsk.h" |
| 14 | |
| 15 | static const int sap_magic_num = 0xB3; /* simple XOR crypt */ |
| 16 | |
| 17 | |
| 18 | static const char sap_header[] = |
| 19 | "\001SYSTEME D'ARCHIVAGE PUKALL S.A.P. " |
| 20 | "(c) Alexandre PUKALL Avril 1998"; |
| 21 | |
| 22 | |
| 23 | static const UINT16 sap_crc[] = |
| 24 | { |
| 25 | 0x0000, 0x1081, 0x2102, 0x3183, 0x4204, 0x5285, 0x6306, 0x7387, |
| 26 | 0x8408, 0x9489, 0xa50a, 0xb58b, 0xc60c, 0xd68d, 0xe70e, 0xf78f, |
| 27 | }; |
| 28 | |
| 29 | struct sap_dsk_tag |
| 30 | { |
| 31 | int tracks; |
| 32 | int sector_size; |
| 33 | int sector_pos[80][16]; /* remember sector position in file */ |
| 34 | }; |
| 35 | |
| 36 | static UINT16 thom_sap_crc( UINT8* data, int size ) |
| 37 | { |
| 38 | int i; |
| 39 | UINT16 crc = 0xffff, crc2; |
| 40 | for ( i = 0; i < size; i++ ) |
| 41 | { |
| 42 | crc2 = ( crc >> 4 ) ^ sap_crc[ ( crc ^ data[i] ) & 15 ]; |
| 43 | crc = ( crc2 >> 4 ) ^ sap_crc[ ( crc2 ^ (data[i] >> 4) ) & 15 ]; |
| 44 | } |
| 45 | return crc; |
| 46 | } |
| 47 | |
| 48 | static struct sap_dsk_tag *get_tag(floppy_image *floppy) |
| 49 | { |
| 50 | struct sap_dsk_tag *tag; |
| 51 | tag = (sap_dsk_tag *)floppy_tag(floppy); |
| 52 | return tag; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | static FLOPPY_IDENTIFY(sap_dsk_identify) |
| 57 | { |
| 58 | char header[0x100]; |
| 59 | floppy_image_read(floppy, header, 0, sizeof(sap_header)); |
| 60 | if (!memcmp( header, sap_header, sizeof(sap_header) ) ) |
| 61 | { |
| 62 | *vote= 100; |
| 63 | } else { |
| 64 | *vote = 0; |
| 65 | } |
| 66 | return FLOPPY_ERROR_SUCCESS; |
| 67 | } |
| 68 | static int sap_get_heads_per_disk(floppy_image *floppy) |
| 69 | { |
| 70 | return 1; |
| 71 | } |
| 72 | |
| 73 | static int sap_get_tracks_per_disk(floppy_image *floppy) |
| 74 | { |
| 75 | return get_tag(floppy)->tracks; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset) |
| 80 | { |
| 81 | UINT64 offs; |
| 82 | struct sap_dsk_tag *tag = get_tag(floppy); |
| 83 | /* translate the sector to a raw sector */ |
| 84 | if (!sector_is_index) |
| 85 | { |
| 86 | sector -= 1; |
| 87 | } |
| 88 | /* check to see if we are out of range */ |
| 89 | if ((head < 0) || (head >= 1) || (track < 0) || (track >=tag->tracks) |
| 90 | || (sector < 0) || (sector >= 16)) |
| 91 | return FLOPPY_ERROR_SEEKERROR; |
| 92 | |
| 93 | offs = tag->sector_pos[track][sector]; |
| 94 | if (offs <= 0 ) |
| 95 | return FLOPPY_ERROR_SEEKERROR; |
| 96 | |
| 97 | if (offset) |
| 98 | *offset = offs; |
| 99 | return FLOPPY_ERROR_SUCCESS; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | |
| 104 | static floperr_t internal_sap_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen) |
| 105 | { |
| 106 | UINT64 offset; |
| 107 | floperr_t err; |
| 108 | int i; |
| 109 | UINT8 *buf; |
| 110 | err = get_offset(floppy, head, track, sector, sector_is_index, &offset); |
| 111 | if (err) |
| 112 | return err; |
| 113 | |
| 114 | floppy_image_read(floppy, buffer, offset+4, buflen); |
| 115 | buf = (UINT8*)buffer; |
| 116 | for (i=0;i<buflen;i++) { |
| 117 | buf[i] ^= sap_magic_num; |
| 118 | } |
| 119 | return FLOPPY_ERROR_SUCCESS; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | |
| 124 | static floperr_t internal_sap_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam) |
| 125 | { |
| 126 | UINT64 offset; |
| 127 | floperr_t err; |
| 128 | UINT8 buf[256+6]; |
| 129 | UINT16 crc; |
| 130 | int i; |
| 131 | err = get_offset(floppy, head, track, sector, sector_is_index, &offset); |
| 132 | if (err) |
| 133 | return err; |
| 134 | |
| 135 | /* buf = 4-byte header + sector + 2-byte CRC */ |
| 136 | floppy_image_read(floppy, buf, offset, 4); |
| 137 | for (i=0;i<buflen;i++) { |
| 138 | buf[i+4] = ((UINT8*)buffer)[i]; |
| 139 | } |
| 140 | crc = thom_sap_crc( buf, buflen+4 ); |
| 141 | buf[buflen+4] = crc >> 8; |
| 142 | buf[buflen+5] = crc & 0xff; |
| 143 | for (i=0;i<buflen;i++) { |
| 144 | buf[i+4] ^= sap_magic_num; |
| 145 | } |
| 146 | floppy_image_write(floppy, buf, offset, buflen+6); |
| 147 | |
| 148 | return FLOPPY_ERROR_SUCCESS; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | static floperr_t sap_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 153 | { |
| 154 | return internal_sap_read_sector(floppy, head, track, sector, FALSE, buffer, buflen); |
| 155 | } |
| 156 | |
| 157 | static floperr_t sap_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 158 | { |
| 159 | return internal_sap_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam); |
| 160 | } |
| 161 | |
| 162 | static floperr_t sap_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 163 | { |
| 164 | return internal_sap_read_sector(floppy, head, track, sector, TRUE, buffer, buflen); |
| 165 | } |
| 166 | |
| 167 | static floperr_t sap_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 168 | { |
| 169 | return internal_sap_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam); |
| 170 | } |
| 171 | |
| 172 | static floperr_t sap_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 173 | { |
| 174 | floperr_t err; |
| 175 | err = get_offset(floppy, head, track, sector, FALSE, NULL); |
| 176 | if (err) |
| 177 | return err; |
| 178 | |
| 179 | if (sector_length) { |
| 180 | *sector_length = get_tag(floppy)->sector_size; |
| 181 | } |
| 182 | return FLOPPY_ERROR_SUCCESS; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | |
| 187 | static floperr_t sap_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 188 | { |
| 189 | floperr_t err; |
| 190 | UINT8 header[4]; |
| 191 | UINT64 offset = 0; |
| 192 | sector_index += 1; |
| 193 | err = get_offset(floppy, head, track, sector_index, FALSE, &offset); |
| 194 | |
| 195 | floppy_image_read(floppy, header, offset, 4); |
| 196 | if (cylinder) |
| 197 | *cylinder = header[2]; |
| 198 | if (side) |
| 199 | *side = head; |
| 200 | if (sector) |
| 201 | *sector = header[3]; |
| 202 | if (sector_length) |
| 203 | *sector_length = get_tag(floppy)->sector_size; |
| 204 | if (flags) |
| 205 | /* TODO: read DAM or DDAM and determine flags */ |
| 206 | *flags = 0; |
| 207 | return err; |
| 208 | } |
| 209 | |
| 210 | static floperr_t sap_post_format(floppy_image *floppy, option_resolution *params) |
| 211 | { |
| 212 | int track,sector; |
| 213 | int pos; |
| 214 | UINT8 buf[256], header[4]; |
| 215 | struct sap_dsk_tag *tag; |
| 216 | tag = (struct sap_dsk_tag *) floppy_create_tag(floppy, sizeof(struct sap_dsk_tag)); |
| 217 | |
| 218 | /* default options */ |
| 219 | if ( !tag->tracks ) |
| 220 | { |
| 221 | tag->tracks = 80; |
| 222 | tag->sector_size = 256; |
| 223 | } |
| 224 | |
| 225 | /* create SAP file header */ |
| 226 | floppy_image_write( floppy, sap_header, 0, 66 ); |
| 227 | |
| 228 | for ( track = 0; track < 80; track++ ) |
| 229 | for ( sector = 0; sector < 16; sector++ ) |
| 230 | tag->sector_pos[track][sector] = 0; |
| 231 | |
| 232 | /* create all sectors with valid header and CRC */ |
| 233 | memset(buf, 0xe5, 256); |
| 234 | pos = 0x42; |
| 235 | header[0] = (tag->sector_size==128) ? 1 : 0; |
| 236 | header[1] = 0; |
| 237 | for ( track = 0, pos = 0x42; track < tag->tracks; track++ ) |
| 238 | for ( sector = 0; sector < 16; sector++, pos += tag->sector_size + 6 ) { |
| 239 | tag->sector_pos[track][sector] = pos; |
| 240 | header[2] = track; |
| 241 | header[3] = sector + 1; |
| 242 | floppy_image_write(floppy, header, pos, 4); |
| 243 | sap_write_indexed_sector( floppy, 0, track, sector, buf, tag->sector_size, 0 ); |
| 244 | } |
| 245 | |
| 246 | return FLOPPY_ERROR_SUCCESS; |
| 247 | } |
| 248 | |
| 249 | |
| 250 | static FLOPPY_CONSTRUCT(sap_dsk_construct) |
| 251 | { |
| 252 | struct FloppyCallbacks *callbacks; |
| 253 | struct sap_dsk_tag *tag; |
| 254 | int i,j; |
| 255 | UINT8 fmt; |
| 256 | tag = (struct sap_dsk_tag *) floppy_create_tag(floppy, sizeof(struct sap_dsk_tag)); |
| 257 | if (!tag) |
| 258 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 259 | |
| 260 | /* guess format */ |
| 261 | floppy_image_read(floppy, &fmt, 0x42, 1); |
| 262 | if ( fmt==1 ) tag->sector_size = 128; else tag->sector_size = 256; |
| 263 | |
| 264 | /* start with an empty offset table */ |
| 265 | tag->tracks = 0; |
| 266 | for ( i = 0; i < 80; i++ ) |
| 267 | for ( j = 0; j < 16; j++ ) |
| 268 | tag->sector_pos[i][j] = 0; |
| 269 | |
| 270 | /* count tracks & fill sector offset table */ |
| 271 | for ( i = 0x42; i+4 < floppy_image_size(floppy); i += tag->sector_size + 6 ) // CRC 2 bytes + 4 bytes sector header |
| 272 | { |
| 273 | UINT8 sector, track; |
| 274 | floppy_image_read(floppy, &track, i+2, 1); |
| 275 | floppy_image_read(floppy, §or, i+3, 1); |
| 276 | if ( track >= 80 || sector < 1 || sector > 16 ) continue; |
| 277 | if ( track > tag->tracks ) tag->tracks = track+1; |
| 278 | tag->sector_pos[track][sector-1] = i; |
| 279 | } |
| 280 | callbacks = floppy_callbacks(floppy); |
| 281 | callbacks->read_sector = sap_read_sector; |
| 282 | callbacks->write_sector = sap_write_sector; |
| 283 | callbacks->read_indexed_sector = sap_read_indexed_sector; |
| 284 | callbacks->write_indexed_sector = sap_write_indexed_sector; |
| 285 | callbacks->get_sector_length = sap_get_sector_length; |
| 286 | callbacks->get_heads_per_disk = sap_get_heads_per_disk; |
| 287 | callbacks->get_tracks_per_disk = sap_get_tracks_per_disk; |
| 288 | callbacks->get_indexed_sector_info = sap_get_indexed_sector_info; |
| 289 | callbacks->post_format = sap_post_format; |
| 290 | |
| 291 | return FLOPPY_ERROR_SUCCESS; |
| 292 | } |
| 293 | |
| 294 | static FLOPPY_IDENTIFY(qdd_dsk_identify) |
| 295 | { |
| 296 | *vote = (floppy_image_size(floppy) == (51200)) ? 100 : 0; |
| 297 | return FLOPPY_ERROR_SUCCESS; |
| 298 | } |
| 299 | |
| 300 | /* fixed interlacing map for QDDs */ |
| 301 | static int thom_qdd_map[400]; |
| 302 | |
| 303 | static int qdd_translate_sector(floppy_image *floppy, int sector) |
| 304 | { |
| 305 | return thom_qdd_map[sector-1]; |
| 306 | } |
| 307 | |
| 308 | static void thom_qdd_compute_map ( void ) |
| 309 | { |
| 310 | /* this map is hardcoded in the QDD BIOS */ |
| 311 | static const int p[6][4] = |
| 312 | { |
| 313 | { 20, 2, 14, 8 }, { 21, 19, 13, 7 }, |
| 314 | { 22, 18, 12, 6 }, { 23, 17, 11, 5 }, |
| 315 | { 24, 16, 10, 4 }, { 1, 15, 9, 3 } |
| 316 | }; |
| 317 | static const int q[4] = { 0, 8, 4, 12 }; |
| 318 | int t, s; |
| 319 | for ( t = 0; t < 24; t++ ) |
| 320 | { |
| 321 | for ( s = 0; s < 16; s++ ) |
| 322 | { |
| 323 | thom_qdd_map[ t*16 + s ] = p[ t/4 ][ s%4 ] * 16 + (s/4) + 4*(t%4); |
| 324 | } |
| 325 | } |
| 326 | for ( s = 0; s < 16; s++ ) |
| 327 | { |
| 328 | thom_qdd_map[ 24*16 + s ] = q[ s%4 ] + (s/4); |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | static FLOPPY_CONSTRUCT(qdd_dsk_construct) |
| 333 | { |
| 334 | struct basicdsk_geometry geometry; |
| 335 | |
| 336 | thom_qdd_compute_map(); |
| 337 | |
| 338 | memset(&geometry, 0, sizeof(geometry)); |
| 339 | geometry.heads = 1; |
| 340 | geometry.first_sector_id = 1; |
| 341 | geometry.sector_length = 128; |
| 342 | geometry.tracks = 1; |
| 343 | geometry.sectors = 400; |
| 344 | geometry.translate_sector = qdd_translate_sector; |
| 345 | return basicdsk_construct(floppy, &geometry); |
| 346 | } |
| 347 | |
| 348 | |
| 349 | /* ----------------------------------------------------------------------- */ |
| 350 | |
| 351 | FLOPPY_OPTIONS_START(thomson) |
| 352 | |
| 353 | FLOPPY_OPTION(fdmfm2, "fd", "Thomson FD (MFM) 80 tracks disk image (3\"1/2 DD)", basicdsk_identify_default, basicdsk_construct_default, NULL, |
| 354 | HEADS([1]) |
| 355 | TRACKS([80]) |
| 356 | SECTORS([16]) |
| 357 | SECTOR_LENGTH([256]) |
| 358 | FIRST_SECTOR_ID([1])) |
| 359 | |
| 360 | FLOPPY_OPTION(fdmfm, "fd", "Thomson FD (MFM) 40 tracks disk image (5\"1/4 DD)", basicdsk_identify_default, basicdsk_construct_default, NULL, |
| 361 | HEADS([1]) |
| 362 | TRACKS([40]) |
| 363 | SECTORS([16]) |
| 364 | SECTOR_LENGTH([256]) |
| 365 | FIRST_SECTOR_ID([1])) |
| 366 | |
| 367 | FLOPPY_OPTION(fd2, "fd", "Thomson FD (FM) 80 tracks disk image (3\"1/2 SD)", basicdsk_identify_default, basicdsk_construct_default, NULL, |
| 368 | HEADS([1]) |
| 369 | TRACKS([80]) |
| 370 | SECTORS([16]) |
| 371 | SECTOR_LENGTH([128]) |
| 372 | FIRST_SECTOR_ID([1])) |
| 373 | |
| 374 | FLOPPY_OPTION(fd, "fd", "Thomson FD (FM) 40 tracks disk image (5\"1/4 SD)", basicdsk_identify_default, basicdsk_construct_default, NULL, |
| 375 | HEADS([1]) |
| 376 | TRACKS([40]) |
| 377 | SECTORS([16]) |
| 378 | SECTOR_LENGTH([128]) |
| 379 | FIRST_SECTOR_ID([1])) |
| 380 | |
| 381 | FLOPPY_OPTION(sap,"sap", "Thomson SAP floppy disk image", sap_dsk_identify, sap_dsk_construct, NULL, NULL) |
| 382 | |
| 383 | FLOPPY_OPTION(qdd,"qd", "Thomson QDD floppy disk image (2\"8 SD)", qdd_dsk_identify, qdd_dsk_construct, NULL, |
| 384 | HEADS([1]) |
| 385 | TRACKS([1]) |
| 386 | SECTORS([400]) |
| 387 | SECTOR_LENGTH([128]) |
| 388 | FIRST_SECTOR_ID([0])) |
| 389 | |
| 390 | FLOPPY_OPTIONS_END |
| 391 | |
src/lib/formats/cassimg.c
| r0 | r11458 | |
| 1 | /********************************************************************* |
| 2 | |
| 3 | cassimg.c |
| 4 | |
| 5 | Cassette tape image abstraction code |
| 6 | |
| 7 | *********************************************************************/ |
| 8 | |
| 9 | |
| 10 | #include <string.h> |
| 11 | |
| 12 | #include "imageutl.h" |
| 13 | #include "pool.h" |
| 14 | #include "cassimg.h" |
| 15 | |
| 16 | |
| 17 | /* debugging parameters */ |
| 18 | #define LOG_PUT_SAMPLES 0 |
| 19 | #define DUMP_CASSETTES 0 |
| 20 | |
| 21 | #define SAMPLES_PER_BLOCK 0x40000 |
| 22 | #define CASSETTE_FLAG_DIRTY 0x10000 |
| 23 | |
| 24 | |
| 25 | CASSETTE_FORMATLIST_START(cassette_default_formats) |
| 26 | CASSETTE_FORMATLIST_END |
| 27 | |
| 28 | |
| 29 | |
| 30 | /********************************************************************* |
| 31 | helper code |
| 32 | *********************************************************************/ |
| 33 | |
| 34 | static double map_double(double d, UINT64 low, UINT64 high, UINT64 value) |
| 35 | { |
| 36 | #if defined(_MSC_VER) && (_MSC_VER <= 1200) |
| 37 | /* casting unsigned __int64 to double is not supported on VC6 or before */ |
| 38 | return d * (INT64)(value - low) / (INT64)(high - low); |
| 39 | #else |
| 40 | return d * (value - low) / (high - low); |
| 41 | #endif |
| 42 | } |
| 43 | |
| 44 | |
| 45 | |
| 46 | static size_t waveform_bytes_per_sample(int waveform_flags) |
| 47 | { |
| 48 | return (size_t) (1 << ((waveform_flags & 0x06) / 2)); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | |
| 53 | /********************************************************************* |
| 54 | extrapolation and interpolation |
| 55 | *********************************************************************/ |
| 56 | |
| 57 | static INT32 extrapolate8(INT8 value) |
| 58 | { |
| 59 | return ((INT32) value) << 24; |
| 60 | } |
| 61 | |
| 62 | static INT32 extrapolate16(INT16 value) |
| 63 | { |
| 64 | return ((INT32) value) << 16; |
| 65 | } |
| 66 | |
| 67 | static INT8 interpolate8(INT32 value) |
| 68 | { |
| 69 | return (INT8) (value >> 24); |
| 70 | } |
| 71 | |
| 72 | static INT16 interpolate16(INT32 value) |
| 73 | { |
| 74 | return (INT16) (value >> 16); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | /********************************************************************* |
| 80 | initialization and termination |
| 81 | *********************************************************************/ |
| 82 | |
| 83 | static cassette_image *cassette_init(const struct CassetteFormat *format, void *file, const struct io_procs *procs, int flags) |
| 84 | { |
| 85 | cassette_image *cassette; |
| 86 | |
| 87 | cassette = (cassette_image *)malloc(sizeof(struct _cassette_image)); |
| 88 | if (!cassette) |
| 89 | return NULL; |
| 90 | |
| 91 | memset(cassette, 0, sizeof(*cassette)); |
| 92 | cassette->format = format; |
| 93 | cassette->io.file = file; |
| 94 | cassette->io.procs = procs; |
| 95 | cassette->flags = flags; |
| 96 | cassette->pool = pool_alloc_lib(NULL); |
| 97 | return cassette; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | |
| 102 | static void cassette_finishinit(casserr_t err, cassette_image *cassette, cassette_image **outcassette) |
| 103 | { |
| 104 | if (cassette && (err || !outcassette)) |
| 105 | { |
| 106 | cassette_close(cassette); |
| 107 | cassette = NULL; |
| 108 | } |
| 109 | if (outcassette) |
| 110 | *outcassette = cassette; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | static int good_format(const struct CassetteFormat *format, const char *extension, int flags) |
| 116 | { |
| 117 | if (extension && !image_find_extension(format->extensions, extension)) |
| 118 | return FALSE; |
| 119 | if (((flags & CASSETTE_FLAG_READONLY) == 0) && !format->save) |
| 120 | return FALSE; |
| 121 | return TRUE; |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | casserr_t cassette_open_choices(void *file, const struct io_procs *procs, const char *extension, |
| 127 | const struct CassetteFormat *const *formats, int flags, cassette_image **outcassette) |
| 128 | { |
| 129 | casserr_t err; |
| 130 | cassette_image *cassette; |
| 131 | const struct CassetteFormat *format; |
| 132 | struct CassetteOptions opts = {0, }; |
| 133 | int i; |
| 134 | |
| 135 | /* if not specified, use the dummy arguments */ |
| 136 | if (!formats) |
| 137 | formats = cassette_default_formats; |
| 138 | |
| 139 | /* create the cassette object */ |
| 140 | cassette = cassette_init(NULL, file, procs, flags); |
| 141 | if (!cassette) |
| 142 | { |
| 143 | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 144 | goto done; |
| 145 | } |
| 146 | |
| 147 | /* identify the image */ |
| 148 | format = NULL; |
| 149 | for (i = 0; !format && formats[i]; i++) |
| 150 | { |
| 151 | if (good_format(formats[i], extension, flags)) |
| 152 | { |
| 153 | format = formats[i]; |
| 154 | memset(&opts, 0, sizeof(opts)); |
| 155 | err = format->identify(cassette, &opts); |
| 156 | if (err == CASSETTE_ERROR_INVALIDIMAGE) |
| 157 | format = NULL; |
| 158 | else if (err) |
| 159 | goto done; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | /* have we found a proper format */ |
| 164 | if (!format) |
| 165 | { |
| 166 | err = CASSETTE_ERROR_INVALIDIMAGE; |
| 167 | goto done; |
| 168 | } |
| 169 | cassette->format = format; |
| 170 | |
| 171 | /* read the options */ |
| 172 | cassette->channels = opts.channels; |
| 173 | cassette->sample_frequency = opts.sample_frequency; |
| 174 | |
| 175 | /* load the image */ |
| 176 | err = format->load(cassette); |
| 177 | if (err) |
| 178 | goto done; |
| 179 | |
| 180 | /* success */ |
| 181 | cassette->flags &= ~CASSETTE_FLAG_DIRTY; |
| 182 | err = CASSETTE_ERROR_SUCCESS; |
| 183 | |
| 184 | done: |
| 185 | cassette_finishinit(err, cassette, outcassette); |
| 186 | return err; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | casserr_t cassette_open(void *file, const struct io_procs *procs, |
| 192 | const struct CassetteFormat *format, int flags, cassette_image **outcassette) |
| 193 | { |
| 194 | const struct CassetteFormat *formats[2]; |
| 195 | formats[0] = format; |
| 196 | formats[1] = NULL; |
| 197 | return cassette_open_choices(file, procs, NULL, formats, flags, outcassette); |
| 198 | } |
| 199 | |
| 200 | |
| 201 | |
| 202 | casserr_t cassette_create(void *file, const struct io_procs *procs, const struct CassetteFormat *format, |
| 203 | const struct CassetteOptions *opts, int flags, cassette_image **outcassette) |
| 204 | { |
| 205 | casserr_t err; |
| 206 | cassette_image *cassette; |
| 207 | static const struct CassetteOptions default_options = { 1, 16, 44100 }; |
| 208 | |
| 209 | /* cannot create to a read only image */ |
| 210 | if (flags & CASSETTE_FLAG_READONLY) |
| 211 | return CASSETTE_ERROR_INVALIDIMAGE; |
| 212 | |
| 213 | /* is this a good format? */ |
| 214 | if (!good_format(format, NULL, flags)) |
| 215 | return CASSETTE_ERROR_INVALIDIMAGE; |
| 216 | |
| 217 | /* normalize arguments */ |
| 218 | if (!opts) |
| 219 | opts = &default_options; |
| 220 | |
| 221 | /* create the cassette object */ |
| 222 | cassette = cassette_init(format, file, procs, flags); |
| 223 | if (!cassette) |
| 224 | { |
| 225 | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 226 | goto done; |
| 227 | } |
| 228 | |
| 229 | /* read the options */ |
| 230 | cassette->channels = opts->channels; |
| 231 | cassette->sample_frequency = opts->sample_frequency; |
| 232 | |
| 233 | err = CASSETTE_ERROR_SUCCESS; |
| 234 | |
| 235 | done: |
| 236 | cassette_finishinit(err, cassette, outcassette); |
| 237 | return err; |
| 238 | } |
| 239 | |
| 240 | |
| 241 | |
| 242 | static casserr_t cassette_perform_save(cassette_image *cassette) |
| 243 | { |
| 244 | struct CassetteInfo info; |
| 245 | cassette_get_info(cassette, &info); |
| 246 | return cassette->format->save(cassette, &info); |
| 247 | } |
| 248 | |
| 249 | |
| 250 | |
| 251 | casserr_t cassette_save(cassette_image *cassette) |
| 252 | { |
| 253 | casserr_t err; |
| 254 | |
| 255 | if (!cassette->format || !cassette->format->save) |
| 256 | return CASSETTE_ERROR_UNSUPPORTED; |
| 257 | |
| 258 | err = cassette_perform_save(cassette); |
| 259 | if (err) |
| 260 | return err; |
| 261 | |
| 262 | cassette->flags &= ~CASSETTE_FLAG_DIRTY; |
| 263 | return CASSETTE_ERROR_SUCCESS; |
| 264 | } |
| 265 | |
| 266 | |
| 267 | |
| 268 | void cassette_get_info(cassette_image *cassette, struct CassetteInfo *info) |
| 269 | { |
| 270 | memset(info, 0, sizeof(*info)); |
| 271 | info->channels = cassette->channels; |
| 272 | info->sample_count = cassette->sample_count; |
| 273 | info->sample_frequency = cassette->sample_frequency; |
| 274 | info->bits_per_sample = (int) waveform_bytes_per_sample(cassette->flags) * 8; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | |
| 279 | void cassette_close(cassette_image *cassette) |
| 280 | { |
| 281 | if (cassette) |
| 282 | { |
| 283 | if ((cassette->flags & CASSETTE_FLAG_DIRTY) && (cassette->flags & CASSETTE_FLAG_SAVEONEXIT)) |
| 284 | cassette_save(cassette); |
| 285 | pool_free_lib(cassette->pool); |
| 286 | free(cassette); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | |
| 291 | |
| 292 | void cassette_change(cassette_image *cassette, void *file, const struct io_procs *procs, const struct CassetteFormat *format, int flags) |
| 293 | { |
| 294 | if ((flags & CASSETTE_FLAG_READONLY) == 0) |
| 295 | flags |= CASSETTE_FLAG_DIRTY; |
| 296 | cassette->io.file = file; |
| 297 | cassette->io.procs = procs; |
| 298 | cassette->format = format; |
| 299 | cassette->flags = flags; |
| 300 | } |
| 301 | |
| 302 | |
| 303 | |
| 304 | /********************************************************************* |
| 305 | calls for accessing the raw cassette image |
| 306 | *********************************************************************/ |
| 307 | |
| 308 | void cassette_image_read(cassette_image *cassette, void *buffer, UINT64 offset, size_t length) |
| 309 | { |
| 310 | io_generic_read(&cassette->io, buffer, offset, length); |
| 311 | } |
| 312 | |
| 313 | |
| 314 | |
| 315 | void cassette_image_write(cassette_image *cassette, const void *buffer, UINT64 offset, size_t length) |
| 316 | { |
| 317 | io_generic_write(&cassette->io, buffer, offset, length); |
| 318 | } |
| 319 | |
| 320 | |
| 321 | |
| 322 | UINT64 cassette_image_size(cassette_image *cassette) |
| 323 | { |
| 324 | return io_generic_size(&cassette->io); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | |
| 329 | /********************************************************************* |
| 330 | waveform accesses |
| 331 | *********************************************************************/ |
| 332 | |
| 333 | struct manipulation_ranges |
| 334 | { |
| 335 | int channel_first; |
| 336 | int channel_last; |
| 337 | size_t sample_first; |
| 338 | size_t sample_last; |
| 339 | }; |
| 340 | |
| 341 | |
| 342 | |
| 343 | static size_t my_round(double d) |
| 344 | { |
| 345 | size_t result; |
| 346 | d += 0.5; |
| 347 | result = (size_t) d; |
| 348 | return result; |
| 349 | } |
| 350 | |
| 351 | |
| 352 | |
| 353 | static casserr_t compute_manipulation_ranges(cassette_image *cassette, int channel, |
| 354 | double time_index, double sample_period, struct manipulation_ranges *ranges) |
| 355 | { |
| 356 | if (channel < 0) |
| 357 | { |
| 358 | ranges->channel_first = 0; |
| 359 | ranges->channel_last = cassette->channels - 1; |
| 360 | } |
| 361 | else |
| 362 | { |
| 363 | ranges->channel_first = channel; |
| 364 | ranges->channel_last = channel; |
| 365 | } |
| 366 | |
| 367 | ranges->sample_first = my_round(time_index * cassette->sample_frequency); |
| 368 | ranges->sample_last = my_round((time_index + sample_period) * cassette->sample_frequency); |
| 369 | |
| 370 | if (ranges->sample_last > ranges->sample_first) |
| 371 | ranges->sample_last--; |
| 372 | |
| 373 | return CASSETTE_ERROR_SUCCESS; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | |
| 378 | static casserr_t lookup_sample(cassette_image *cassette, int channel, size_t sample, int allocate, INT32 **ptr) |
| 379 | { |
| 380 | size_t sample_block; |
| 381 | size_t sample_index; |
| 382 | size_t sample_size; |
| 383 | size_t new_block_count; |
| 384 | size_t new_block_sample_count; |
| 385 | INT32 *new_block; |
| 386 | struct sample_block *new_blocks; |
| 387 | struct sample_block *block; |
| 388 | |
| 389 | *ptr = NULL; |
| 390 | sample_block = (sample / SAMPLES_PER_BLOCK) * cassette->channels + channel; |
| 391 | sample_index = sample % SAMPLES_PER_BLOCK; |
| 392 | sample_size = sizeof(block->block[0]); |
| 393 | |
| 394 | /* is this block beyond the edge of our waveform? */ |
| 395 | if (sample_block >= cassette->block_count) |
| 396 | { |
| 397 | if (!allocate) |
| 398 | return CASSETTE_ERROR_SUCCESS; |
| 399 | |
| 400 | /* allocate new blocks */ |
| 401 | new_block_count = sample_block + 1; |
| 402 | new_blocks = (struct sample_block *)pool_realloc_lib(cassette->pool, cassette->blocks, new_block_count * sizeof(cassette->blocks[0])); |
| 403 | if (!new_blocks) |
| 404 | return CASSETTE_ERROR_OUTOFMEMORY; |
| 405 | |
| 406 | cassette->blocks = new_blocks; |
| 407 | memset(&cassette->blocks[cassette->block_count], 0, (new_block_count - cassette->block_count) * sizeof(cassette->blocks[0])); |
| 408 | cassette->block_count = new_block_count; |
| 409 | } |
| 410 | |
| 411 | block = &cassette->blocks[sample_block]; |
| 412 | |
| 413 | /* is this sample access off the current block? */ |
| 414 | if (sample_index >= block->sample_count) |
| 415 | { |
| 416 | if (!allocate) |
| 417 | return CASSETTE_ERROR_SUCCESS; |
| 418 | |
| 419 | new_block_sample_count = SAMPLES_PER_BLOCK; |
| 420 | |
| 421 | new_block = (INT32*)pool_realloc_lib(cassette->pool, block->block, new_block_sample_count * sample_size); |
| 422 | if (!new_block) |
| 423 | return CASSETTE_ERROR_OUTOFMEMORY; |
| 424 | |
| 425 | block->block = new_block; |
| 426 | memset(&block->block[block->sample_count], 0, (new_block_sample_count - block->sample_count) * sample_size); |
| 427 | block->sample_count = new_block_sample_count; |
| 428 | } |
| 429 | |
| 430 | *ptr = &block->block[sample_index]; |
| 431 | return CASSETTE_ERROR_SUCCESS; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | |
| 436 | /********************************************************************* |
| 437 | waveform accesses |
| 438 | *********************************************************************/ |
| 439 | |
| 440 | casserr_t cassette_get_samples(cassette_image *cassette, int channel, |
| 441 | double time_index, double sample_period, size_t sample_count, size_t sample_bytes, |
| 442 | void *samples, int waveform_flags) |
| 443 | { |
| 444 | casserr_t err; |
| 445 | struct manipulation_ranges ranges; |
| 446 | size_t sample_index; |
| 447 | size_t cassette_sample_index; |
| 448 | UINT8 *dest_ptr; |
| 449 | const INT32 *source_ptr; |
| 450 | double d; |
| 451 | INT16 word; |
| 452 | INT32 dword; |
| 453 | INT64 sum; |
| 454 | |
| 455 | // assert(cassette); |
| 456 | |
| 457 | err = compute_manipulation_ranges(cassette, channel, time_index, sample_period, &ranges); |
| 458 | if (err) |
| 459 | return err; |
| 460 | |
| 461 | for (sample_index = 0; sample_index < sample_count; sample_index++) |
| 462 | { |
| 463 | sum = 0; |
| 464 | |
| 465 | for (channel = ranges.channel_first; channel <= ranges.channel_last; channel++) |
| 466 | { |
| 467 | /* find the sample that we are putting */ |
| 468 | d = map_double(ranges.sample_last + 1 - ranges.sample_first, 0, sample_count, sample_index) + ranges.sample_first; |
| 469 | cassette_sample_index = (size_t) d; |
| 470 | err = lookup_sample(cassette, channel, cassette_sample_index, TRUE, (INT32 **) &source_ptr); |
| 471 | if (err) |
| 472 | return err; |
| 473 | |
| 474 | sum += *source_ptr; |
| 475 | } |
| 476 | |
| 477 | /* average out the samples */ |
| 478 | sum /= (ranges.channel_last + 1 - ranges.channel_first); |
| 479 | |
| 480 | /* and write out the result */ |
| 481 | dest_ptr = (UINT8*)samples; |
| 482 | dest_ptr += waveform_bytes_per_sample(waveform_flags) * sample_index; |
| 483 | switch(waveform_bytes_per_sample(waveform_flags)) |
| 484 | { |
| 485 | case 1: |
| 486 | *((INT8 *) dest_ptr) = interpolate8(sum); |
| 487 | break; |
| 488 | case 2: |
| 489 | word = interpolate16(sum); |
| 490 | if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP) |
| 491 | word = FLIPENDIAN_INT16(word); |
| 492 | *((INT16 *) dest_ptr) = word; |
| 493 | break; |
| 494 | case 4: |
| 495 | dword = sum; |
| 496 | if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP) |
| 497 | dword = FLIPENDIAN_INT32(dword); |
| 498 | *((INT32 *) dest_ptr) = dword; |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | return CASSETTE_ERROR_SUCCESS; |
| 503 | } |
| 504 | |
| 505 | |
| 506 | |
| 507 | casserr_t cassette_put_samples(cassette_image *cassette, int channel, |
| 508 | double time_index, double sample_period, size_t sample_count, size_t sample_bytes, |
| 509 | const void *samples, int waveform_flags) |
| 510 | { |
| 511 | casserr_t err; |
| 512 | struct manipulation_ranges ranges; |
| 513 | size_t sample_index; |
| 514 | INT32 *dest_ptr; |
| 515 | INT32 dest_value; |
| 516 | INT16 word; |
| 517 | INT32 dword; |
| 518 | const UINT8 *source_ptr; |
| 519 | double d; |
| 520 | |
| 521 | if (!cassette) |
| 522 | return CASSETTE_ERROR_SUCCESS; |
| 523 | |
| 524 | if (sample_period == 0) |
| 525 | return CASSETTE_ERROR_SUCCESS; |
| 526 | |
| 527 | err = compute_manipulation_ranges(cassette, channel, time_index, sample_period, &ranges); |
| 528 | if (err) |
| 529 | return err; |
| 530 | |
| 531 | if (cassette->sample_count < ranges.sample_last+1) |
| 532 | cassette->sample_count = ranges.sample_last + 1; |
| 533 | cassette->flags |= CASSETTE_FLAG_DIRTY; |
| 534 | |
| 535 | if (LOG_PUT_SAMPLES) |
| 536 | { |
| 537 | logerror("cassette_put_samples(): Putting samples TIME=[%2.6g..%2.6g] INDEX=[%i..%i]\n", |
| 538 | time_index, time_index + sample_period, |
| 539 | (int)ranges.sample_first, (int)ranges.sample_last); |
| 540 | } |
| 541 | |
| 542 | for (sample_index = ranges.sample_first; sample_index <= ranges.sample_last; sample_index++) |
| 543 | { |
| 544 | /* figure out the source pointer */ |
| 545 | d = map_double(sample_count, ranges.sample_first, ranges.sample_last + 1, sample_index); |
| 546 | source_ptr = (const UINT8*)samples; |
| 547 | source_ptr += ((size_t) d) * sample_bytes; |
| 548 | |
| 549 | /* compute the value that we are writing */ |
| 550 | switch(waveform_bytes_per_sample(waveform_flags)) { |
| 551 | case 1: |
| 552 | dest_value = extrapolate8(*((INT8 *) source_ptr)); |
| 553 | break; |
| 554 | case 2: |
| 555 | word = *((INT16 *) source_ptr); |
| 556 | if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP) |
| 557 | word = FLIPENDIAN_INT16(word); |
| 558 | dest_value = extrapolate16(word); |
| 559 | break; |
| 560 | case 4: |
| 561 | dword = *((INT32 *) source_ptr); |
| 562 | if (waveform_flags & CASSETTE_WAVEFORM_ENDIAN_FLIP) |
| 563 | dword = FLIPENDIAN_INT32(dword); |
| 564 | dest_value = dword; |
| 565 | break; |
| 566 | default: |
| 567 | return CASSETTE_ERROR_INTERNAL; |
| 568 | } |
| 569 | |
| 570 | for (channel = ranges.channel_first; channel <= ranges.channel_last; channel++) |
| 571 | { |
| 572 | /* find the sample that we are putting */ |
| 573 | err = lookup_sample(cassette, channel, sample_index, TRUE, &dest_ptr); |
| 574 | if (err) |
| 575 | return err; |
| 576 | *dest_ptr = dest_value; |
| 577 | } |
| 578 | } |
| 579 | return CASSETTE_ERROR_SUCCESS; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | |
| 584 | casserr_t cassette_get_sample(cassette_image *cassette, int channel, |
| 585 | double time_index, double sample_period, INT32 *sample) |
| 586 | { |
| 587 | return cassette_get_samples(cassette, channel, time_index, |
| 588 | sample_period, 1, 0, sample, CASSETTE_WAVEFORM_32BIT); |
| 589 | } |
| 590 | |
| 591 | |
| 592 | |
| 593 | casserr_t cassette_put_sample(cassette_image *cassette, int channel, |
| 594 | double time_index, double sample_period, INT32 sample) |
| 595 | { |
| 596 | return cassette_put_samples(cassette, channel, time_index, |
| 597 | sample_period, 1, 0, &sample, CASSETTE_WAVEFORM_32BIT); |
| 598 | } |
| 599 | |
| 600 | |
| 601 | |
| 602 | /********************************************************************* |
| 603 | waveform accesses to/from the raw image |
| 604 | *********************************************************************/ |
| 605 | |
| 606 | casserr_t cassette_read_samples(cassette_image *cassette, int channels, double time_index, |
| 607 | double sample_period, size_t sample_count, UINT64 offset, int waveform_flags) |
| 608 | { |
| 609 | casserr_t err; |
| 610 | size_t chunk_sample_count; |
| 611 | size_t bytes_per_sample; |
| 612 | size_t sample_bytes; |
| 613 | size_t samples_loaded = 0; |
| 614 | double chunk_time_index; |
| 615 | double chunk_sample_period; |
| 616 | int channel; |
| 617 | UINT8 buffer[8192]; |
| 618 | |
| 619 | bytes_per_sample = waveform_bytes_per_sample(waveform_flags); |
| 620 | sample_bytes = bytes_per_sample * channels; |
| 621 | |
| 622 | while(samples_loaded < sample_count) |
| 623 | { |
| 624 | chunk_sample_count = MIN(sizeof(buffer) / sample_bytes, (sample_count - samples_loaded)); |
| 625 | chunk_sample_period = map_double(sample_period, 0, sample_count, chunk_sample_count); |
| 626 | chunk_time_index = time_index + map_double(sample_period, 0, sample_count, samples_loaded); |
| 627 | |
| 628 | cassette_image_read(cassette, buffer, offset, chunk_sample_count * sample_bytes); |
| 629 | |
| 630 | for (channel = 0; channel < channels; channel++) |
| 631 | { |
| 632 | err = cassette_put_samples(cassette, channel, chunk_time_index, chunk_sample_period, |
| 633 | chunk_sample_count, sample_bytes, &buffer[channel * bytes_per_sample], waveform_flags); |
| 634 | if (err) |
| 635 | return err; |
| 636 | } |
| 637 | |
| 638 | offset += chunk_sample_count * sample_bytes; |
| 639 | samples_loaded += chunk_sample_count; |
| 640 | } |
| 641 | return CASSETTE_ERROR_SUCCESS; |
| 642 | } |
| 643 | |
| 644 | |
| 645 | |
| 646 | casserr_t cassette_write_samples(cassette_image *cassette, int channels, double time_index, |
| 647 | double sample_period, size_t sample_count, UINT64 offset, int waveform_flags) |
| 648 | { |
| 649 | casserr_t err; |
| 650 | size_t chunk_sample_count; |
| 651 | size_t bytes_per_sample; |
| 652 | size_t sample_bytes; |
| 653 | size_t samples_saved = 0; |
| 654 | double chunk_time_index; |
| 655 | double chunk_sample_period; |
| 656 | int channel; |
| 657 | UINT8 buffer[8192]; |
| 658 | |
| 659 | bytes_per_sample = waveform_bytes_per_sample(waveform_flags); |
| 660 | sample_bytes = bytes_per_sample * channels; |
| 661 | |
| 662 | while(samples_saved < sample_count) |
| 663 | { |
| 664 | chunk_sample_count = MIN(sizeof(buffer) / sample_bytes, (sample_count - samples_saved)); |
| 665 | chunk_sample_period = map_double(sample_period, 0, sample_count, chunk_sample_count); |
| 666 | chunk_time_index = time_index + map_double(sample_period, 0, sample_count, samples_saved); |
| 667 | |
| 668 | for (channel = 0; channel < channels; channel++) |
| 669 | { |
| 670 | err = cassette_get_samples(cassette, channel, chunk_time_index, chunk_sample_period, |
| 671 | chunk_sample_count, sample_bytes, &buffer[channel * bytes_per_sample], waveform_flags); |
| 672 | if (err) |
| 673 | return err; |
| 674 | } |
| 675 | |
| 676 | cassette_image_write(cassette, buffer, offset, chunk_sample_count * sample_bytes); |
| 677 | |
| 678 | offset += chunk_sample_count * sample_bytes; |
| 679 | samples_saved += chunk_sample_count; |
| 680 | } |
| 681 | return CASSETTE_ERROR_SUCCESS; |
| 682 | } |
| 683 | |
| 684 | |
| 685 | |
| 686 | /********************************************************************* |
| 687 | waveform accesses to/from the raw image |
| 688 | *********************************************************************/ |
| 689 | |
| 690 | static const INT8 *choose_wave(const struct CassetteModulation *modulation, size_t *wave_bytes_length) |
| 691 | { |
| 692 | static const INT8 square_wave[] = { -128, 127 }; |
| 693 | static const INT8 sine_wave[] = { 0, 48, 89, 117, 127, 117, 89, 48, 0, -48, -89, -117, -127, -117, -89, -48 }; |
| 694 | |
| 695 | if (modulation->flags & CASSETTE_MODULATION_SINEWAVE) |
| 696 | { |
| 697 | *wave_bytes_length = ARRAY_LENGTH(sine_wave); |
| 698 | return sine_wave; |
| 699 | } |
| 700 | else |
| 701 | { |
| 702 | *wave_bytes_length = ARRAY_LENGTH(square_wave); |
| 703 | return square_wave; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | |
| 708 | |
| 709 | casserr_t cassette_modulation_identify(cassette_image *cassette, const struct CassetteModulation *modulation, |
| 710 | struct CassetteOptions *opts) |
| 711 | { |
| 712 | size_t wave_bytes_length; |
| 713 | choose_wave(modulation, &wave_bytes_length); |
| 714 | opts->bits_per_sample = 8; |
| 715 | opts->channels = 1; |
| 716 | opts->sample_frequency = (UINT32) (MAX(modulation->zero_frequency_high, modulation->one_frequency_high) * wave_bytes_length * 2); |
| 717 | return CASSETTE_ERROR_SUCCESS; |
| 718 | } |
| 719 | |
| 720 | |
| 721 | |
| 722 | casserr_t cassette_put_modulated_data(cassette_image *cassette, int channel, double time_index, |
| 723 | const void *data, size_t data_length, const struct CassetteModulation *modulation, |
| 724 | double *time_displacement) |
| 725 | { |
| 726 | casserr_t err; |
| 727 | const UINT8 *data_bytes = (const UINT8 *)data; |
| 728 | const INT8 *wave_bytes; |
| 729 | size_t wave_bytes_length; |
| 730 | double total_displacement = 0.0; |
| 731 | double pulse_period; |
| 732 | double pulse_frequency; |
| 733 | UINT8 b; |
| 734 | int i; |
| 735 | |
| 736 | wave_bytes = choose_wave(modulation, &wave_bytes_length); |
| 737 | |
| 738 | while(data_length--) |
| 739 | { |
| 740 | b = *(data_bytes++); |
| 741 | for (i = 0; i < 8; i++) |
| 742 | { |
| 743 | pulse_frequency = (b & (1 << i)) ? modulation->one_frequency_cannonical : modulation->zero_frequency_cannonical; |
| 744 | pulse_period = 1 / pulse_frequency; |
| 745 | err = cassette_put_samples(cassette, 0, time_index, pulse_period, wave_bytes_length, 1, wave_bytes, CASSETTE_WAVEFORM_8BIT); |
| 746 | if (err) |
| 747 | goto done; |
| 748 | time_index += pulse_period; |
| 749 | total_displacement += pulse_period; |
| 750 | } |
| 751 | } |
| 752 | err = CASSETTE_ERROR_SUCCESS; |
| 753 | |
| 754 | done: |
| 755 | if (time_displacement) |
| 756 | *time_displacement = total_displacement; |
| 757 | return err; |
| 758 | } |
| 759 | |
| 760 | |
| 761 | |
| 762 | casserr_t cassette_put_modulated_filler(cassette_image *cassette, int channel, double time_index, |
| 763 | UINT8 filler, size_t filler_length, const struct CassetteModulation *modulation, |
| 764 | double *time_displacement) |
| 765 | { |
| 766 | casserr_t err; |
| 767 | double delta; |
| 768 | double total_displacement = 0.0; |
| 769 | |
| 770 | while(filler_length--) |
| 771 | { |
| 772 | err = cassette_put_modulated_data(cassette, channel, time_index, &filler, 1, modulation, &delta); |
| 773 | if (err) |
| 774 | return err; |
| 775 | total_displacement += delta; |
| 776 | time_index += delta; |
| 777 | } |
| 778 | |
| 779 | if (time_displacement) |
| 780 | *time_displacement = total_displacement; |
| 781 | return CASSETTE_ERROR_SUCCESS; |
| 782 | } |
| 783 | |
| 784 | |
| 785 | |
| 786 | casserr_t cassette_read_modulated_data(cassette_image *cassette, int channel, double time_index, |
| 787 | UINT64 offset, UINT64 length, const struct CassetteModulation *modulation, |
| 788 | double *time_displacement) |
| 789 | { |
| 790 | casserr_t err; |
| 791 | UINT8 buffer_stack[1024]; |
| 792 | UINT8 *buffer; |
| 793 | UINT8 *alloc_buffer = NULL; |
| 794 | double delta; |
| 795 | double total_displacement = 0.0; |
| 796 | size_t this_length; |
| 797 | size_t buffer_length; |
| 798 | |
| 799 | if (length <= sizeof(buffer_stack)) |
| 800 | { |
| 801 | buffer = buffer_stack; |
| 802 | buffer_length = sizeof(buffer_stack); |
| 803 | } |
| 804 | else |
| 805 | { |
| 806 | buffer_length = MIN(length, 100000); |
| 807 | alloc_buffer = (UINT8*)malloc(buffer_length); |
| 808 | if (!alloc_buffer) |
| 809 | { |
| 810 | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 811 | goto done; |
| 812 | } |
| 813 | buffer = alloc_buffer; |
| 814 | } |
| 815 | |
| 816 | while(length > 0) |
| 817 | { |
| 818 | this_length = (size_t) MIN(length, buffer_length); |
| 819 | cassette_image_read(cassette, buffer, offset, this_length); |
| 820 | |
| 821 | err = cassette_put_modulated_data(cassette, channel, time_index, buffer, this_length, modulation, &delta); |
| 822 | if (err) |
| 823 | goto done; |
| 824 | total_displacement += delta; |
| 825 | time_index += delta; |
| 826 | length -= this_length; |
| 827 | } |
| 828 | |
| 829 | if (time_displacement) |
| 830 | *time_displacement = total_displacement; |
| 831 | err = CASSETTE_ERROR_SUCCESS; |
| 832 | |
| 833 | done: |
| 834 | if (alloc_buffer) |
| 835 | free(alloc_buffer); |
| 836 | return err; |
| 837 | } |
| 838 | |
| 839 | |
| 840 | |
| 841 | casserr_t cassette_put_modulated_data_bit(cassette_image *cassette, int channel, double time_index, |
| 842 | UINT8 data, const struct CassetteModulation *modulation, |
| 843 | double *time_displacement) |
| 844 | { |
| 845 | casserr_t err; |
| 846 | const INT8 *wave_bytes; |
| 847 | size_t wave_bytes_length; |
| 848 | double total_displacement = 0.0; |
| 849 | double pulse_period; |
| 850 | double pulse_frequency; |
| 851 | |
| 852 | wave_bytes = choose_wave(modulation, &wave_bytes_length); |
| 853 | |
| 854 | pulse_frequency = (data) ? modulation->one_frequency_cannonical : modulation->zero_frequency_cannonical; |
| 855 | pulse_period = 1 / pulse_frequency; |
| 856 | err = cassette_put_samples(cassette, 0, time_index, pulse_period, wave_bytes_length, 1, wave_bytes, CASSETTE_WAVEFORM_8BIT); |
| 857 | if (err) |
| 858 | goto done; |
| 859 | time_index += pulse_period; |
| 860 | total_displacement += pulse_period; |
| 861 | |
| 862 | err = CASSETTE_ERROR_SUCCESS; |
| 863 | |
| 864 | done: |
| 865 | if (time_displacement) |
| 866 | *time_displacement = total_displacement; |
| 867 | return err; |
| 868 | } |
| 869 | |
| 870 | |
| 871 | |
| 872 | /********************************************************************* |
| 873 | waveform accesses to/from the raw image |
| 874 | *********************************************************************/ |
| 875 | |
| 876 | casserr_t cassette_legacy_identify(cassette_image *cassette, struct CassetteOptions *opts, |
| 877 | const struct CassetteLegacyWaveFiller *legacy_args) |
| 878 | { |
| 879 | opts->channels = 1; |
| 880 | opts->bits_per_sample = 16; |
| 881 | opts->sample_frequency = legacy_args->sample_frequency; |
| 882 | return CASSETTE_ERROR_SUCCESS; |
| 883 | } |
| 884 | |
| 885 | |
| 886 | |
| 887 | casserr_t cassette_legacy_construct(cassette_image *cassette, |
| 888 | const struct CassetteLegacyWaveFiller *legacy_args) |
| 889 | { |
| 890 | casserr_t err; |
| 891 | int length; |
| 892 | int sample_count; |
| 893 | void *bytes = NULL; |
| 894 | void *chunk = NULL; |
| 895 | INT16 *samples = NULL; |
| 896 | int pos = 0; |
| 897 | UINT64 offset = 0; |
| 898 | UINT64 size; |
| 899 | struct CassetteLegacyWaveFiller args; |
| 900 | |
| 901 | /* sanity check the args */ |
| 902 | //assert(legacy_args->header_samples >= -1); |
| 903 | //assert(legacy_args->trailer_samples >= 0); |
| 904 | //assert(legacy_args->fill_wave); |
| 905 | |
| 906 | size = cassette_image_size(cassette); |
| 907 | |
| 908 | /* normalize the args */ |
| 909 | args = *legacy_args; |
| 910 | if (args.chunk_size == 0) |
| 911 | args.chunk_size = 1; |
| 912 | else if (args.chunk_size < 0) |
| 913 | args.chunk_size = cassette_image_size(cassette); |
| 914 | if (args.sample_frequency == 0) |
| 915 | args.sample_frequency = 11025; |
| 916 | |
| 917 | /* allocate a buffer for the binary data */ |
| 918 | chunk = malloc(args.chunk_size); |
| 919 | if (!chunk) |
| 920 | { |
| 921 | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 922 | goto done; |
| 923 | } |
| 924 | |
| 925 | /* determine number of samples */ |
| 926 | if (args.chunk_sample_calc) |
| 927 | { |
| 928 | if (size > 0x7FFFFFFF) |
| 929 | { |
| 930 | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 931 | goto done; |
| 932 | } |
| 933 | |
| 934 | bytes = malloc(size); |
| 935 | if (!bytes) |
| 936 | { |
| 937 | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 938 | goto done; |
| 939 | } |
| 940 | cassette_image_read(cassette, bytes, 0, size); |
| 941 | sample_count = args.chunk_sample_calc((const UINT8*)bytes, (int) size); |
| 942 | |
| 943 | if (args.header_samples < 0) |
| 944 | args.header_samples = sample_count; |
| 945 | } |
| 946 | else |
| 947 | { |
| 948 | sample_count = ((size + args.chunk_size - 1) / args.chunk_size) |
| 949 | * args.chunk_samples; |
| 950 | } |
| 951 | sample_count += args.header_samples + args.trailer_samples; |
| 952 | |
| 953 | /* allocate a buffer for the completed samples */ |
| 954 | samples = (INT16*)malloc(sample_count * sizeof(*samples)); |
| 955 | if (!samples) |
| 956 | { |
| 957 | err = CASSETTE_ERROR_OUTOFMEMORY; |
| 958 | goto done; |
| 959 | } |
| 960 | |
| 961 | /* if there has to be a header */ |
| 962 | if (args.header_samples > 0) |
| 963 | { |
| 964 | length = args.fill_wave(samples + pos, sample_count - pos, CODE_HEADER); |
| 965 | if (length < 0) |
| 966 | { |
| 967 | err = CASSETTE_ERROR_INVALIDIMAGE; |
| 968 | goto done; |
| 969 | } |
| 970 | pos += length; |
| 971 | } |
| 972 | |
| 973 | /* convert the file data to samples */ |
| 974 | while((pos < sample_count) && (offset < size)) |
| 975 | { |
| 976 | cassette_image_read(cassette, chunk, offset, args.chunk_size); |
| 977 | offset += args.chunk_size; |
| 978 | |
| 979 | length = args.fill_wave(samples + pos, sample_count - pos, (UINT8*)chunk); |
| 980 | if (length < 0) |
| 981 | { |
| 982 | err = CASSETTE_ERROR_INVALIDIMAGE; |
| 983 | goto done; |
| 984 | } |
| 985 | pos += length; |
| 986 | if (length == 0) |
| 987 | break; |
| 988 | } |
| 989 | |
| 990 | /* if there has to be a trailer */ |
| 991 | if (args.trailer_samples > 0) |
| 992 | { |
| 993 | length = args.fill_wave(samples + pos, sample_count - pos, CODE_TRAILER); |
| 994 | if (length < 0) |
| 995 | { |
| 996 | err = CASSETTE_ERROR_INVALIDIMAGE; |
| 997 | goto done; |
| 998 | } |
| 999 | pos += length; |
| 1000 | } |
| 1001 | |
| 1002 | /* specify the wave */ |
| 1003 | err = cassette_put_samples(cassette, 0, 0.0, ((double) pos) / args.sample_frequency, |
| 1004 | pos, 2, samples, CASSETTE_WAVEFORM_16BIT); |
| 1005 | if (err) |
| 1006 | goto done; |
| 1007 | |
| 1008 | /* success! */ |
| 1009 | err = CASSETTE_ERROR_SUCCESS; |
| 1010 | |
| 1011 | #if DUMP_CASSETTES |
| 1012 | cassette_dump(cassette, "C:\\TEMP\\CASDUMP.WAV"); |
| 1013 | #endif |
| 1014 | |
| 1015 | done: |
| 1016 | if (samples) |
| 1017 | free(samples); |
| 1018 | if (chunk) |
| 1019 | free(chunk); |
| 1020 | if (bytes) |
| 1021 | free(bytes); |
| 1022 | return err; |
| 1023 | } |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | /********************************************************************* |
| 1028 | cassette_dump |
| 1029 | |
| 1030 | A debugging call to dump a casette image to a disk based wave file |
| 1031 | *********************************************************************/ |
| 1032 | |
| 1033 | void cassette_dump(cassette_image *image, const char *filename) |
| 1034 | { |
| 1035 | FILE *f; |
| 1036 | struct io_generic saved_io; |
| 1037 | const struct CassetteFormat *saved_format; |
| 1038 | |
| 1039 | f = fopen(filename, "wb"); |
| 1040 | if (!f) |
| 1041 | return; |
| 1042 | |
| 1043 | memcpy(&saved_io, &image->io, sizeof(saved_io)); |
| 1044 | saved_format = image->format; |
| 1045 | |
| 1046 | image->io.file = f; |
| 1047 | image->io.procs = &stdio_ioprocs_noclose; |
| 1048 | image->format = &wavfile_format; |
| 1049 | cassette_perform_save(image); |
| 1050 | |
| 1051 | memcpy(&image->io, &saved_io, sizeof(saved_io)); |
| 1052 | image->format = saved_format; |
| 1053 | |
| 1054 | fclose(f); |
| 1055 | } |
| 1056 | |
| 1057 | |
src/lib/formats/z80ne_dsk.c
| r0 | r11458 | |
| 1 | /********************************************************************* |
| 2 | |
| 3 | formats/z80ne_dsk.c |
| 4 | |
| 5 | Nuova Elettronica Z80NE disk images |
| 6 | |
| 7 | *********************************************************************/ |
| 8 | |
| 9 | #include "z80ne_dsk.h" |
| 10 | #include "basicdsk.h" |
| 11 | #include "imageutl.h" |
| 12 | |
| 13 | /* ----------------------------------------------------------------------- |
| 14 | * DMK file format |
| 15 | * |
| 16 | * David M. Keil's disk image format is aptly called an 'on disk' image |
| 17 | * format. This means that whatever written to the disk is encoded into |
| 18 | * the image file. IDAMS, sector headers, trailing CRCs, and intra-sector |
| 19 | * spacing. * |
| 20 | * |
| 21 | * HEADER DESCRIPTION: |
| 22 | * |
| 23 | * Offset Bytes Field Description |
| 24 | * ------ ----- ------------ ----------- |
| 25 | * 0 1 write_prot 0xff = Write Protected, 0x00 = R/W |
| 26 | * 1 1 tracks Number of tracks |
| 27 | * 2 2 track_length Bytes per track on DMK file (little endian) |
| 28 | * 4 1 disk_options Miscellaneous flags |
| 29 | * bit 0-3: Unused |
| 30 | * bit 4: Sides on DMK file |
| 31 | * 1=single, 0=double |
| 32 | * bit 5: Unused |
| 33 | * bit 6: Single Density DMK file |
| 34 | * 1=single (bytes written once |
| 35 | * on IDAMs) |
| 36 | * 0=double (bytes written |
| 37 | * two times for SD IDAMs) |
| 38 | * 5 7 reserved Reserved for future use |
| 39 | * 12 4 real_disk_code If this is 0x12345678 (little endian) |
| 40 | * then access a real disk drive |
| 41 | * (unsupported) |
| 42 | * |
| 43 | * Each track begins with a track TOC, consisting of 64 little endian 16-bit |
| 44 | * integers. Each integer has the following format: |
| 45 | * bit 0-13: Offset from beginning of track to 'FE' byte of IDAM |
| 46 | * Note these are always sorted from first to last. All empty |
| 47 | * entries are 0x00 |
| 48 | * bit 14: Undefined (reserved) |
| 49 | * bit 15: Sector double density (0=SD 1=DD) |
| 50 | * If not set and SD DMK flag is set then bytes are written |
| 51 | * once. |
| 52 | * ----------------------------------------------------------------------- */ |
| 53 | |
| 54 | struct dmk_tag |
| 55 | { |
| 56 | int heads; |
| 57 | int tracks; |
| 58 | UINT32 track_size; |
| 59 | }; |
| 60 | |
| 61 | /* DMK file structure |
| 62 | * See WD1711 documentation |
| 63 | */ |
| 64 | /* DMK file header */ |
| 65 | #define DMK_HEADER_LEN 16 /* DMK header */ |
| 66 | /* For every track (40) */ |
| 67 | #define DMK_TOC_LEN 128 /* 64 16-bit relative offsets to IDAMs */ |
| 68 | #define DMK_GAP1_LEN 32 /* GAP1 (26 bytes 0xFF and 6 bytes 0x00) */ |
| 69 | |
| 70 | /* For every sector (10) 301 bytes */ |
| 71 | #define DMK_IDAM_LEN 7 /* IDAM (0xFE, Track, 0x00, Sector, 0x00, CRChi, CRClo) */ |
| 72 | #define DMK_ID_GAP_LEN 17 /* GAP2 (11 bytes 0xFF and 6 bytes 0x00) */ |
| 73 | #define DMK_DAM_LEN 1 /* Data Address Mark (0xFB) */ |
| 74 | #define DMK_DATA_LEN 256 /* Data */ |
| 75 | #define DMK_DATA_CRC_LEN 2 /* Data CRC (CRChi, CRClo) */ |
| 76 | #define DMK_DATA_GAP_LEN 18 /* GAP3 (12 bytes 0xFF and 6 bytes 0x00) (not for last sector) */ |
| 77 | |
| 78 | #define DMK_EXTRA_TRACK_LENGTH (176) /* GAP4 (192 bytes 0xFF) */ |
| 79 | /* End of DMK file structure */ |
| 80 | |
| 81 | #define dmk_idam_type(x) (x)[0] |
| 82 | #define dmk_idam_track(x) (x)[1] |
| 83 | #define dmk_idam_side(x) (x)[2] |
| 84 | #define dmk_idam_sector(x) (x)[3] |
| 85 | #define dmk_idam_sectorlength(x) (x)[4] |
| 86 | #define dmk_idam_crc(x) (((x)[5] << 8) + (x)[6]) |
| 87 | #define dmk_idam_set_crc(x, crc) (x)[5] = ((crc) >> 8); (x)[6] = ((crc) >> 0); |
| 88 | |
| 89 | static const char needle_data[] = "\x00\x00\x00\x00\x00\x00\x00\x00\xFB\xFB"; |
| 90 | static const char needle_deleted_data_fa[] = "\x00\x00\x00\x00\x00\x00\x00\x00\xFA\xFA"; |
| 91 | static const char needle_deleted_data_f8[] = "\x00\x00\x00\x00\x00\x00\x00\x00\xF8\xF8"; |
| 92 | |
| 93 | |
| 94 | static struct dmk_tag *get_dmk_tag(floppy_image *floppy) |
| 95 | { |
| 96 | return (dmk_tag *)floppy_tag(floppy); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | static floperr_t z80ne_dmk_get_track_offset(floppy_image *floppy, int head, int track, UINT64 *offset) |
| 101 | { |
| 102 | struct dmk_tag *tag = get_dmk_tag(floppy); |
| 103 | |
| 104 | if ((head < 0) || (head >= tag->heads) || (track < 0) || (track >= tag->tracks)) |
| 105 | return FLOPPY_ERROR_SEEKERROR; |
| 106 | |
| 107 | *offset = track; |
| 108 | *offset *= tag->heads; |
| 109 | *offset += head; |
| 110 | *offset *= tag->track_size; |
| 111 | *offset += DMK_HEADER_LEN; |
| 112 | return FLOPPY_ERROR_SUCCESS; |
| 113 | } |
| 114 | |
| 115 | |
| 116 | static UINT32 z80ne_dmk_min_track_size(int sectors, int sector_length) |
| 117 | { |
| 118 | int sector_physical_length; |
| 119 | sector_physical_length = (DMK_IDAM_LEN + DMK_ID_GAP_LEN + DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN + DMK_DATA_GAP_LEN); |
| 120 | return DMK_TOC_LEN + 2 * (DMK_GAP1_LEN + (sectors * sector_physical_length) - DMK_DATA_GAP_LEN + DMK_EXTRA_TRACK_LENGTH); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | static floperr_t z80ne_dmk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 126 | { |
| 127 | floperr_t err; |
| 128 | UINT64 track_offset; |
| 129 | |
| 130 | err = z80ne_dmk_get_track_offset(floppy, head, track, &track_offset); |
| 131 | if (err) |
| 132 | return err; |
| 133 | |
| 134 | floppy_image_read(floppy, buffer, offset + track_offset, buflen); |
| 135 | return FLOPPY_ERROR_SUCCESS; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | |
| 140 | static floperr_t z80ne_dmk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 141 | { |
| 142 | floperr_t err; |
| 143 | UINT64 track_offset; |
| 144 | |
| 145 | err = z80ne_dmk_get_track_offset(floppy, head, track, &track_offset); |
| 146 | if (err) |
| 147 | return err; |
| 148 | |
| 149 | floppy_image_write(floppy, buffer, offset + track_offset, buflen); |
| 150 | return FLOPPY_ERROR_SUCCESS; |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | static floperr_t z80ne_dmk_get_track_data_offset(floppy_image *floppy, int head, int track, UINT64 *offset) |
| 156 | { |
| 157 | *offset = DMK_TOC_LEN + 1; |
| 158 | return FLOPPY_ERROR_SUCCESS; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | static floperr_t z80ne_dmk_format_track(floppy_image *floppy, int head, int track, option_resolution *params) |
| 163 | { |
| 164 | int sectors; |
| 165 | int sector_length; |
| 166 | int interleave; |
| 167 | int first_sector_id; |
| 168 | floperr_t err; |
| 169 | int physical_sector; |
| 170 | int logical_sector; |
| 171 | int track_position; |
| 172 | UINT16 idam_offset; |
| 173 | UINT16 crc; |
| 174 | UINT8 *track_data; |
| 175 | void *track_data_v; |
| 176 | UINT32 max_track_size; |
| 177 | int *sector_map = NULL; |
| 178 | UINT8 *local_sector = NULL; |
| 179 | int local_sector_size; |
| 180 | int sector_position; |
| 181 | int i; |
| 182 | |
| 183 | err = FLOPPY_ERROR_SUCCESS; |
| 184 | |
| 185 | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 186 | sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 187 | interleave = option_resolution_lookup_int(params, PARAM_INTERLEAVE); |
| 188 | first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID); |
| 189 | |
| 190 | max_track_size = get_dmk_tag(floppy)->track_size; |
| 191 | |
| 192 | if (sectors > DMK_TOC_LEN/2) |
| 193 | { |
| 194 | err = FLOPPY_ERROR_INTERNAL; |
| 195 | goto done; |
| 196 | } |
| 197 | |
| 198 | if (max_track_size < z80ne_dmk_min_track_size(sectors, sector_length)) |
| 199 | { |
| 200 | err = FLOPPY_ERROR_NOSPACE; |
| 201 | goto done; |
| 202 | } |
| 203 | |
| 204 | err = floppy_load_track(floppy, head, track, TRUE, &track_data_v, NULL); |
| 205 | if (err) |
| 206 | goto done; |
| 207 | track_data = (UINT8 *) track_data_v; |
| 208 | |
| 209 | /* set up sector map */ |
| 210 | sector_map = (int*)malloc(sectors * sizeof(*sector_map)); |
| 211 | if (!sector_map) |
| 212 | { |
| 213 | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 214 | goto done; |
| 215 | } |
| 216 | memset(sector_map, 0xFF, sectors * sizeof(*sector_map)); |
| 217 | |
| 218 | physical_sector = 0; |
| 219 | for (logical_sector = 0; logical_sector < sectors; logical_sector++) |
| 220 | { |
| 221 | while(sector_map[physical_sector] >= 0) |
| 222 | { |
| 223 | physical_sector++; |
| 224 | physical_sector %= sectors; |
| 225 | } |
| 226 | |
| 227 | sector_map[physical_sector] = logical_sector + first_sector_id; |
| 228 | physical_sector += interleave + 1; |
| 229 | physical_sector %= sectors; |
| 230 | } |
| 231 | |
| 232 | /* set up a local physical sector template */ |
| 233 | local_sector_size = (DMK_IDAM_LEN + |
| 234 | DMK_ID_GAP_LEN + |
| 235 | DMK_DAM_LEN + |
| 236 | sector_length + |
| 237 | DMK_DATA_CRC_LEN + |
| 238 | DMK_DATA_GAP_LEN); |
| 239 | local_sector = (UINT8*)malloc(local_sector_size); |
| 240 | if (!local_sector) |
| 241 | { |
| 242 | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 243 | goto done; |
| 244 | } |
| 245 | memset(local_sector, 0x00, local_sector_size); |
| 246 | |
| 247 | /* set up track table of contents */ |
| 248 | physical_sector = 0; |
| 249 | |
| 250 | /* write GAP1 (track lead in) */ |
| 251 | memset(&track_data[DMK_TOC_LEN], 0xFF, 52); |
| 252 | memset(&track_data[DMK_TOC_LEN+52], 0x00, 12); |
| 253 | track_position = DMK_TOC_LEN + DMK_GAP1_LEN * 2; |
| 254 | |
| 255 | /* |
| 256 | * prepare template in local sector |
| 257 | */ |
| 258 | |
| 259 | /* IDAM */ |
| 260 | sector_position = 0; |
| 261 | dmk_idam_type( &local_sector[sector_position]) = 0xFE; |
| 262 | dmk_idam_track( &local_sector[sector_position]) = track; |
| 263 | dmk_idam_side( &local_sector[sector_position]) = head; |
| 264 | dmk_idam_sector( &local_sector[sector_position]) = 0; /* replace in sector instances */ |
| 265 | dmk_idam_sectorlength( &local_sector[sector_position]) = compute_log2(sector_length / 128); |
| 266 | dmk_idam_set_crc( &local_sector[sector_position], 0); /* replace in sector instances */ |
| 267 | sector_position += 7; |
| 268 | |
| 269 | /* GAP2 (ID GAP) */ |
| 270 | memset(&local_sector[sector_position], 0xFF, 11); |
| 271 | sector_position += 11; |
| 272 | memset(&local_sector[sector_position], 0x00, 6); |
| 273 | sector_position += 6; |
| 274 | |
| 275 | /* sector body */ |
| 276 | local_sector[sector_position] = 0xFB; |
| 277 | memset(&local_sector[sector_position + 1], floppy_get_filler(floppy), sector_length); |
| 278 | crc = ccitt_crc16(0xffff, &local_sector[sector_position], sector_length + 1); |
| 279 | local_sector[sector_position + sector_length + 1] = (UINT8) (crc >> 8); |
| 280 | local_sector[sector_position + sector_length + 2] = (UINT8) (crc >> 0); |
| 281 | sector_position += sector_length + 3; |
| 282 | |
| 283 | /* GAP3 (Data GAP) */ |
| 284 | memset(&local_sector[sector_position], 0xFF, 12); |
| 285 | sector_position += 12; |
| 286 | memset(&local_sector[sector_position], 0x00, 6); |
| 287 | sector_position += 6; |
| 288 | |
| 289 | /* |
| 290 | * start of track data |
| 291 | */ |
| 292 | while(physical_sector < DMK_TOC_LEN/2) |
| 293 | { |
| 294 | if (physical_sector >= sectors) |
| 295 | { |
| 296 | /* no more sectors */ |
| 297 | idam_offset = 0; |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | /* this is a sector */ |
| 302 | idam_offset = track_position; |
| 303 | logical_sector = sector_map[physical_sector]; |
| 304 | |
| 305 | /* update IDAM in local sector template */ |
| 306 | dmk_idam_sector( &local_sector[0]) = logical_sector; /* update sector number in sector instance */ |
| 307 | crc = ccitt_crc16(0xffff, &local_sector[0], DMK_IDAM_LEN - 2); /* and recalculate crc */ |
| 308 | dmk_idam_set_crc( &local_sector[0], crc); |
| 309 | |
| 310 | /* write local sector in track doubling every byte (DMK double density mode) */ |
| 311 | for(i=0; i<local_sector_size; i++) |
| 312 | track_data[track_position + i*2] = track_data[track_position + (i*2) + 1] = local_sector[i]; |
| 313 | track_position += (2 * local_sector_size); |
| 314 | if (physical_sector == (sectors-1)) /* on last sector remove GAP3 */ |
| 315 | track_position -= (2 * DMK_DATA_GAP_LEN); |
| 316 | } |
| 317 | |
| 318 | /* write the TOC entry */ |
| 319 | track_data[physical_sector * 2 + 0] = (UINT8) (idam_offset >> 0); |
| 320 | track_data[physical_sector * 2 + 1] = (UINT8) (idam_offset >> 8); |
| 321 | |
| 322 | physical_sector++; |
| 323 | } |
| 324 | |
| 325 | /* write track footer GAP4 */ |
| 326 | //assert(max_track_size >= (UINT32)track_position); |
| 327 | memset(&track_data[track_position], 0xFF, max_track_size - track_position); |
| 328 | |
| 329 | done: |
| 330 | if (sector_map) |
| 331 | free(sector_map); |
| 332 | if (local_sector) |
| 333 | free(local_sector); |
| 334 | |
| 335 | return err; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | |
| 340 | static int z80ne_dmk_get_heads_per_disk(floppy_image *floppy) |
| 341 | { |
| 342 | return get_dmk_tag(floppy)->heads; |
| 343 | } |
| 344 | |
| 345 | |
| 346 | |
| 347 | static int z80ne_dmk_get_tracks_per_disk(floppy_image *floppy) |
| 348 | { |
| 349 | return get_dmk_tag(floppy)->tracks; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | |
| 354 | static UINT32 z80ne_dmk_get_track_size(floppy_image *floppy, int head, int track) |
| 355 | { |
| 356 | return get_dmk_tag(floppy)->track_size; |
| 357 | } |
| 358 | |
| 359 | static UINT8 * my_memmem(UINT8 *haystack, size_t haystacklen, |
| 360 | UINT8 *needle, size_t needlelen) |
| 361 | { |
| 362 | register UINT8 *h = haystack; |
| 363 | register UINT8 *n = needle; |
| 364 | register size_t hl = haystacklen; |
| 365 | register size_t nl = needlelen; |
| 366 | register size_t i; |
| 367 | |
| 368 | if (nl == 0) return haystack; /* The first occurrence of the empty string is deemed to occur at |
| 369 | the beginning of the string. */ |
| 370 | if (hl < nl) |
| 371 | return NULL; |
| 372 | |
| 373 | for( i = 0; (i < hl) && (i + nl <= hl ); i++ ) |
| 374 | if (h[i] == *n) /* first match */ |
| 375 | if ( !memcmp(&h[i]+1, n + 1, nl - 1) ) |
| 376 | return (haystack+i); /* returns a pointer to the substring */ |
| 377 | |
| 378 | return (UINT8 *)NULL; /* not found */ |
| 379 | } |
| 380 | |
| 381 | |
| 382 | |
| 383 | static floperr_t z80ne_dmk_seek_sector_in_track(floppy_image *floppy, int head, int track, int sector, int sector_is_index, int dirtify, UINT8 **sector_data, UINT32 *sector_length) |
| 384 | { |
| 385 | struct dmk_tag *tag = get_dmk_tag(floppy); |
| 386 | floperr_t err; |
| 387 | size_t idam_offset = 0; |
| 388 | UINT16 calculated_crc; |
| 389 | size_t i, j; |
| 390 | size_t offs; |
| 391 | /*int state;*/ |
| 392 | UINT8 *track_data; |
| 393 | void *track_data_v; |
| 394 | size_t track_length; |
| 395 | size_t sec_len; |
| 396 | UINT8 *local_idam = NULL; |
| 397 | int local_idam_size; |
| 398 | UINT8 *sec_data; |
| 399 | |
| 400 | |
| 401 | err = FLOPPY_ERROR_SUCCESS; |
| 402 | |
| 403 | err = floppy_load_track(floppy, head, track, dirtify, &track_data_v, &track_length); |
| 404 | if (err) |
| 405 | goto done; |
| 406 | track_data = (UINT8 *) track_data_v; |
| 407 | |
| 408 | /* set up a local IDAM space */ |
| 409 | local_idam_size = (DMK_IDAM_LEN + |
| 410 | DMK_ID_GAP_LEN + |
| 411 | DMK_DAM_LEN + |
| 412 | DMK_DATA_GAP_LEN); |
| 413 | local_idam = (UINT8*)malloc(local_idam_size); |
| 414 | if (!local_idam) |
| 415 | { |
| 416 | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 417 | goto done; |
| 418 | } |
| 419 | memset(local_idam, 0x00, local_idam_size); |
| 420 | |
| 421 | /* search for matching IDAM */ |
| 422 | for (i = 0; i < DMK_TOC_LEN / 2; i++) |
| 423 | { |
| 424 | idam_offset = track_data[i * 2 + 1]; |
| 425 | idam_offset <<= 8; |
| 426 | idam_offset |= track_data[i * 2 + 0]; |
| 427 | idam_offset &= 0x3FFF; |
| 428 | |
| 429 | if (idam_offset == 0) |
| 430 | { |
| 431 | /* we've reached the end of the road */ |
| 432 | i = DMK_TOC_LEN; |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | if ((idam_offset + DMK_IDAM_LEN * 2) >= tag->track_size) |
| 437 | continue; |
| 438 | |
| 439 | /* overflowing the track? */ |
| 440 | if ((idam_offset + local_idam_size * 2) >= tag->track_size) |
| 441 | { |
| 442 | err = FLOPPY_ERROR_SEEKERROR; |
| 443 | goto done; |
| 444 | } |
| 445 | |
| 446 | /* create a local copy of sector of IDAM */ |
| 447 | for(j = 0; j < (DMK_IDAM_LEN + DMK_ID_GAP_LEN + DMK_DAM_LEN); j++) |
| 448 | local_idam[j] = track_data[idam_offset + j*2]; |
| 449 | |
| 450 | /* from now on all operations are done in local copy of IDAM */ |
| 451 | calculated_crc = ccitt_crc16(0xffff, &local_idam[0], DMK_IDAM_LEN - 2); |
| 452 | |
| 453 | /* check IDAM integrity and check for matching sector */ |
| 454 | if (calculated_crc == dmk_idam_crc(&local_idam[0])) |
| 455 | { |
| 456 | if (sector_is_index) |
| 457 | { |
| 458 | /* the sector is indexed; decrement the index and go */ |
| 459 | if (sector-- == 0) |
| 460 | break; |
| 461 | } |
| 462 | else |
| 463 | { |
| 464 | /* check for matching sector ID */ |
| 465 | if (sector == dmk_idam_sector(&local_idam[0])) |
| 466 | break; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | if (i >= DMK_TOC_LEN / 2) |
| 472 | { |
| 473 | err = FLOPPY_ERROR_SEEKERROR; |
| 474 | goto done; |
| 475 | } |
| 476 | |
| 477 | /* we found a matching sector ID */ |
| 478 | /*state = 0;*/ |
| 479 | offs = idam_offset + 2 * DMK_IDAM_LEN; |
| 480 | |
| 481 | /* find pattern 0x0000FB; this represents the start of a data sector */ |
| 482 | sec_data = my_memmem(&track_data[offs], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_data, 10); |
| 483 | if(!sec_data) |
| 484 | { |
| 485 | /* find pattern 0x0000FA; this represents the start of a deleted data sector (used for system sectors) */ |
| 486 | sec_data = my_memmem(&track_data[offs], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_fa, 10); |
| 487 | } |
| 488 | if(!sec_data) |
| 489 | { |
| 490 | /* find pattern 0x0000F8; this represents the start of a deleted data sector (used for system sectors) */ |
| 491 | sec_data = my_memmem(&track_data[offs], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_f8, 10); |
| 492 | } |
| 493 | |
| 494 | if(sec_data) |
| 495 | sec_data += 10; |
| 496 | else |
| 497 | { |
| 498 | err = FLOPPY_ERROR_SEEKERROR; |
| 499 | goto done; |
| 500 | } |
| 501 | offs = sec_data - &track_data[offs]; |
| 502 | |
| 503 | sec_len = 128 << dmk_idam_sectorlength(&local_idam[0]); |
| 504 | |
| 505 | if ((offs + 2 * sec_len) > track_length) |
| 506 | { |
| 507 | err = FLOPPY_ERROR_INVALIDIMAGE; |
| 508 | goto done; |
| 509 | } |
| 510 | |
| 511 | if (sector_data) |
| 512 | *sector_data = sec_data; |
| 513 | if (sector_length) |
| 514 | *sector_length = sec_len; |
| 515 | done: |
| 516 | if (local_idam) |
| 517 | free(local_idam); |
| 518 | return err; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | |
| 523 | static floperr_t z80ne_dmk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 524 | { |
| 525 | return z80ne_dmk_seek_sector_in_track(floppy, head, track, sector, FALSE, FALSE, NULL, sector_length); |
| 526 | } |
| 527 | |
| 528 | |
| 529 | |
| 530 | static floperr_t z80ne_dmk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 531 | { |
| 532 | floperr_t err; |
| 533 | size_t idam_offset; |
| 534 | UINT8 *track_data; |
| 535 | void *track_data_v; |
| 536 | unsigned long dam; |
| 537 | |
| 538 | if (sector_index * 2 >= DMK_TOC_LEN) |
| 539 | return FLOPPY_ERROR_SEEKERROR; |
| 540 | |
| 541 | err = floppy_load_track(floppy, head, track, FALSE, &track_data_v, NULL); |
| 542 | if (err) |
| 543 | return err; |
| 544 | track_data = (UINT8 *) track_data_v; |
| 545 | |
| 546 | idam_offset = track_data[sector_index * 2 + 1]; |
| 547 | idam_offset <<= 8; |
| 548 | idam_offset |= track_data[sector_index * 2 + 0]; |
| 549 | idam_offset &= 0x3FFF; |
| 550 | |
| 551 | if (idam_offset == 0) |
| 552 | return FLOPPY_ERROR_SEEKERROR; |
| 553 | |
| 554 | /* determine type of data sector */ |
| 555 | /* find pattern 0x0000FB; this represents the start of a data sector */ |
| 556 | if ( my_memmem(&track_data[idam_offset+14], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_data, 10) ) |
| 557 | dam = 0; |
| 558 | /* find pattern 0x0000FA; this represents the start of a deleted data sector (used for system sectors) */ |
| 559 | else if ( my_memmem(&track_data[idam_offset+14], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_fa, 10) ) |
| 560 | dam = ID_FLAG_DELETED_DATA; |
| 561 | /* find pattern 0x0000F8; this represents the start of a deleted data sector (used for system sectors) */ |
| 562 | else if ( my_memmem(&track_data[idam_offset+14], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_f8, 10) ) |
| 563 | dam = ID_FLAG_DELETED_DATA; |
| 564 | else |
| 565 | return FLOPPY_ERROR_SEEKERROR; |
| 566 | |
| 567 | if (cylinder) |
| 568 | *cylinder = (&track_data[idam_offset])[2]; |
| 569 | if (side) |
| 570 | *side = (&track_data[idam_offset])[4]; |
| 571 | if (sector) |
| 572 | *sector = (&track_data[idam_offset])[6]; |
| 573 | if (sector_length) |
| 574 | *sector_length = 128 << (&track_data[idam_offset])[8]; |
| 575 | if (flags) |
| 576 | *flags = dam; |
| 577 | return FLOPPY_ERROR_SUCCESS; |
| 578 | } |
| 579 | |
| 580 | |
| 581 | |
| 582 | static floperr_t internal_z80ne_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen) |
| 583 | { |
| 584 | floperr_t err; |
| 585 | UINT32 sector_length; |
| 586 | UINT16 crc_on_disk; |
| 587 | UINT16 calculated_crc; |
| 588 | UINT8 *sector_data = NULL; |
| 589 | UINT8 *local_sector = NULL; |
| 590 | int local_sector_size; |
| 591 | int i; |
| 592 | |
| 593 | err = FLOPPY_ERROR_SUCCESS; |
| 594 | |
| 595 | /* get sector length and data pointer */ |
| 596 | err = z80ne_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, FALSE, §or_data, §or_length); |
| 597 | if (err) |
| 598 | goto done; |
| 599 | |
| 600 | /* set up a local physical sector space (DAM + data + crc + GAP) */ |
| 601 | local_sector_size = (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN + DMK_DATA_GAP_LEN); |
| 602 | local_sector = (UINT8*)malloc(local_sector_size); |
| 603 | if (!local_sector) |
| 604 | { |
| 605 | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 606 | goto done; |
| 607 | } |
| 608 | memset(local_sector, 0x00, local_sector_size); |
| 609 | |
| 610 | /* get sector data */ |
| 611 | /* create a local copy of sector data including DAM (for crc calculation) */ |
| 612 | for(i = 0; i < (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN); i++) |
| 613 | local_sector[i] = (sector_data - 2)[i*2]; |
| 614 | |
| 615 | crc_on_disk = local_sector[sector_length + 0 + 1]; |
| 616 | crc_on_disk <<= 8; |
| 617 | crc_on_disk += local_sector[sector_length + 1 + 1]; |
| 618 | |
| 619 | /* crc calculated including DAM */ |
| 620 | calculated_crc = ccitt_crc16(0xffff, local_sector, sector_length+1); |
| 621 | if (calculated_crc != crc_on_disk) |
| 622 | { |
| 623 | err = FLOPPY_ERROR_INVALIDIMAGE; |
| 624 | goto done; |
| 625 | } |
| 626 | |
| 627 | /* copy local sector data excluding DAM */ |
| 628 | memcpy(buffer, local_sector+1, MIN(sector_length, buflen)); |
| 629 | |
| 630 | done: |
| 631 | if (local_sector) |
| 632 | free(local_sector); |
| 633 | return err; |
| 634 | } |
| 635 | |
| 636 | |
| 637 | |
| 638 | static floperr_t internal_z80ne_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam) |
| 639 | { |
| 640 | floperr_t err; |
| 641 | UINT32 sector_length; |
| 642 | UINT8 *sector_data; |
| 643 | UINT16 crc; |
| 644 | UINT8 *local_sector = NULL; |
| 645 | int local_sector_size; |
| 646 | int i; |
| 647 | |
| 648 | err = FLOPPY_ERROR_SUCCESS; |
| 649 | |
| 650 | /* get sector length */ |
| 651 | err = z80ne_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, TRUE, §or_data, §or_length); |
| 652 | if (err) |
| 653 | goto done; |
| 654 | |
| 655 | /* set up a local physical sector space */ |
| 656 | local_sector_size = (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN + DMK_DATA_GAP_LEN); |
| 657 | local_sector = (UINT8*)malloc(local_sector_size); |
| 658 | if (!local_sector) |
| 659 | { |
| 660 | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 661 | goto done; |
| 662 | } |
| 663 | memset(local_sector, 0x00, local_sector_size); |
| 664 | if(!ddam) |
| 665 | local_sector[0] = 0xFB; |
| 666 | else |
| 667 | local_sector[0] = 0xFA; |
| 668 | |
| 669 | if (buflen > sector_length) |
| 670 | { |
| 671 | err = FLOPPY_ERROR_INTERNAL; |
| 672 | goto done; |
| 673 | } |
| 674 | |
| 675 | memcpy(&local_sector[1], buffer, buflen); |
| 676 | |
| 677 | crc = ccitt_crc16(0xffff, local_sector, DMK_DAM_LEN + sector_length); |
| 678 | local_sector[DMK_DAM_LEN + sector_length + 0] = crc >> 8; |
| 679 | local_sector[DMK_DAM_LEN + sector_length + 1] = crc >> 0; |
| 680 | |
| 681 | /* write local sector in track doubling every byte (DMK double density mode) */ |
| 682 | /* write DAM or DDAM too */ |
| 683 | sector_data -= 2; |
| 684 | for(i=0; i < (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN); i++) |
| 685 | sector_data[i*2] = sector_data[(i*2)+1] = local_sector[i]; |
| 686 | |
| 687 | done: |
| 688 | if (local_sector) |
| 689 | free(local_sector); |
| 690 | return err; |
| 691 | } |
| 692 | |
| 693 | |
| 694 | |
| 695 | static floperr_t z80ne_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 696 | { |
| 697 | return internal_z80ne_dmk_read_sector(floppy, head, track, sector, FALSE, buffer, buflen); |
| 698 | } |
| 699 | |
| 700 | static floperr_t z80ne_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 701 | { |
| 702 | return internal_z80ne_dmk_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam); |
| 703 | } |
| 704 | |
| 705 | static floperr_t z80ne_dmk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 706 | { |
| 707 | return internal_z80ne_dmk_read_sector(floppy, head, track, sector, TRUE, buffer, buflen); |
| 708 | } |
| 709 | |
| 710 | static floperr_t z80ne_dmk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 711 | { |
| 712 | return internal_z80ne_dmk_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam); |
| 713 | } |
| 714 | |
| 715 | |
| 716 | |
| 717 | static void z80ne_dmk_interpret_header(floppy_image *floppy, int *heads, int *tracks, int * sectors, int *track_size) |
| 718 | { |
| 719 | UINT8 header[DMK_HEADER_LEN + DMK_TOC_LEN]; |
| 720 | int i; |
| 721 | int idam_offset; |
| 722 | int sects; |
| 723 | |
| 724 | floppy_image_read(floppy, header, 0, DMK_HEADER_LEN + DMK_TOC_LEN); |
| 725 | |
| 726 | if (tracks) |
| 727 | *tracks = header[1]; |
| 728 | if (heads) |
| 729 | *heads = (header[4] & 0x10) ? 1 : 2; |
| 730 | if (track_size) |
| 731 | *track_size = ((int) header[3]) * 0x100 + header[2]; |
| 732 | if(sectors) |
| 733 | { |
| 734 | /* count sectors on first track */ |
| 735 | sects = 0; |
| 736 | for (i = 0; i < DMK_TOC_LEN / 2; i++) |
| 737 | { |
| 738 | idam_offset = header[DMK_HEADER_LEN + i * 2 + 1]; |
| 739 | idam_offset <<= 8; |
| 740 | idam_offset |= header[DMK_HEADER_LEN + i * 2 + 0]; |
| 741 | idam_offset &= 0x3FFF; |
| 742 | |
| 743 | if (idam_offset == 0) |
| 744 | break; |
| 745 | else |
| 746 | sects++; |
| 747 | } |
| 748 | *sectors = sects; |
| 749 | } |
| 750 | |
| 751 | } |
| 752 | |
| 753 | |
| 754 | |
| 755 | FLOPPY_CONSTRUCT(z80ne_dmk_construct) |
| 756 | { |
| 757 | struct FloppyCallbacks *callbacks; |
| 758 | struct dmk_tag *tag; |
| 759 | UINT8 header[DMK_HEADER_LEN]; |
| 760 | int heads, tracks, track_size, sectors, sector_length; |
| 761 | |
| 762 | if (params) |
| 763 | { |
| 764 | heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 765 | tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 766 | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 767 | sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 768 | |
| 769 | track_size = z80ne_dmk_min_track_size(sectors, sector_length); |
| 770 | |
| 771 | memset(header, 0, sizeof(header)); |
| 772 | header[1] = tracks; |
| 773 | header[2] = track_size >> 0; |
| 774 | header[3] = track_size >> 8; |
| 775 | header[4] = (heads == 2) ? 0x00 : 0x10; |
| 776 | |
| 777 | floppy_image_write(floppy, header, 0, sizeof(header)); |
| 778 | } |
| 779 | else |
| 780 | { |
| 781 | z80ne_dmk_interpret_header(floppy, &heads, &tracks, §ors, &track_size); |
| 782 | } |
| 783 | |
| 784 | tag = (dmk_tag*)floppy_create_tag(floppy, sizeof(struct dmk_tag)); |
| 785 | if (!tag) |
| 786 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 787 | tag->heads = heads; |
| 788 | tag->track_size = track_size; |
| 789 | tag->tracks = tracks; |
| 790 | |
| 791 | callbacks = floppy_callbacks(floppy); |
| 792 | callbacks->read_track = z80ne_dmk_read_track; |
| 793 | callbacks->write_track = z80ne_dmk_write_track; |
| 794 | callbacks->get_track_data_offset = z80ne_dmk_get_track_data_offset; |
| 795 | callbacks->format_track = z80ne_dmk_format_track; |
| 796 | callbacks->get_heads_per_disk = z80ne_dmk_get_heads_per_disk; |
| 797 | callbacks->get_tracks_per_disk = z80ne_dmk_get_tracks_per_disk; |
| 798 | callbacks->get_track_size = z80ne_dmk_get_track_size; |
| 799 | callbacks->get_sector_length = z80ne_dmk_get_sector_length; |
| 800 | callbacks->get_indexed_sector_info = z80ne_dmk_get_indexed_sector_info; |
| 801 | callbacks->read_sector = z80ne_dmk_read_sector; |
| 802 | callbacks->write_sector = z80ne_dmk_write_sector; |
| 803 | callbacks->read_indexed_sector = z80ne_dmk_read_indexed_sector; |
| 804 | callbacks->write_indexed_sector = z80ne_dmk_write_indexed_sector; |
| 805 | |
| 806 | return FLOPPY_ERROR_SUCCESS; |
| 807 | } |
| 808 | |
| 809 | |
| 810 | |
| 811 | FLOPPY_IDENTIFY(z80ne_dmk_identify) |
| 812 | { |
| 813 | int heads, tracks, sectors, track_size; |
| 814 | UINT64 size, expected_size; |
| 815 | |
| 816 | size = floppy_image_size(floppy); |
| 817 | z80ne_dmk_interpret_header(floppy, &heads, &tracks, §ors, &track_size); |
| 818 | expected_size = DMK_HEADER_LEN + (heads * tracks * track_size); |
| 819 | *vote = (size == expected_size) ? 90 : 0; |
| 820 | *vote += (sectors == 10) ? 10 : 0; |
| 821 | return FLOPPY_ERROR_SUCCESS; |
| 822 | } |
| 823 | |
| 824 | |
| 825 | |
| 826 | /* ----------------------------------------------------------------------- */ |
| 827 | |
| 828 | FLOPPY_OPTIONS_START( z80ne ) |
| 829 | /* |
| 830 | * Single side |
| 831 | * Single density |
| 832 | * FM, 125 kbit/s, |
| 833 | * 40 tracks, track ID: 0..39 |
| 834 | * 10 sectors/track, sector ID: 0..9 |
| 835 | * 256 bytes/sector |
| 836 | * Interleave 1 (sector ID sequence on track: 0 5 1 6 2 7 3 8 4 9) |
| 837 | * 3125 byte/track unformatted |
| 838 | * Rotation speed 300 rpm (5 rps) |
| 839 | */ |
| 840 | FLOPPY_OPTION( z80ne_dmk, "zmk", "Z80NE DMK disk image", z80ne_dmk_identify, z80ne_dmk_construct, NULL, |
| 841 | HEADS([1]) |
| 842 | TRACKS([40]) |
| 843 | SECTORS([10]) |
| 844 | SECTOR_LENGTH([256]) |
| 845 | INTERLEAVE([1]) |
| 846 | FIRST_SECTOR_ID([0])) |
| 847 | FLOPPY_OPTIONS_END |
src/lib/formats/coupedsk.c
| r0 | r11458 | |
| 1 | /************************************************************************* |
| 2 | |
| 3 | formats/coupedsk.c |
| 4 | |
| 5 | SAM Coupe disk image formats |
| 6 | |
| 7 | **************************************************************************/ |
| 8 | |
| 9 | #include "coupedsk.h" |
| 10 | #include "formats/basicdsk.h" |
| 11 | |
| 12 | |
| 13 | #define SAD_HEADER_LEN 22 |
| 14 | #define SAD_SIGNATURE "Aley's disk backup" |
| 15 | |
| 16 | #define SDF_TRACKSIZE (512 * 12) |
| 17 | |
| 18 | |
| 19 | struct sdf_tag |
| 20 | { |
| 21 | UINT8 heads; |
| 22 | UINT8 tracks; |
| 23 | }; |
| 24 | |
| 25 | |
| 26 | |
| 27 | /************************************* |
| 28 | * |
| 29 | * MGT disk image format |
| 30 | * |
| 31 | *************************************/ |
| 32 | |
| 33 | /* |
| 34 | Straight rip without any header |
| 35 | |
| 36 | Data: |
| 37 | |
| 38 | Side 0 Track 0 |
| 39 | Side 1 Track 0 |
| 40 | Side 0 Track 1 |
| 41 | Side 1 Track 1 |
| 42 | ... |
| 43 | */ |
| 44 | |
| 45 | |
| 46 | FLOPPY_CONSTRUCT( coupe_mgt_construct ) |
| 47 | { |
| 48 | struct basicdsk_geometry geometry; |
| 49 | |
| 50 | memset(&geometry, 0, sizeof(geometry)); |
| 51 | geometry.heads = 2; |
| 52 | geometry.tracks = 80; |
| 53 | geometry.sector_length = 512; |
| 54 | geometry.first_sector_id = 1; |
| 55 | |
| 56 | if (params) |
| 57 | { |
| 58 | /* create */ |
| 59 | geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | /* load */ |
| 64 | UINT64 size = floppy_image_size(floppy); |
| 65 | |
| 66 | /* verify size */ |
| 67 | if (size != 737280 && size != 819200) |
| 68 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 69 | |
| 70 | geometry.sectors = size / 81920; |
| 71 | |
| 72 | } |
| 73 | |
| 74 | return basicdsk_construct(floppy, &geometry); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | FLOPPY_IDENTIFY( coupe_mgt_identify ) |
| 79 | { |
| 80 | UINT64 size; |
| 81 | |
| 82 | size = floppy_image_size(floppy); |
| 83 | |
| 84 | *vote = (size == 737280 || size == 819200) ? 100 : 0; |
| 85 | return FLOPPY_ERROR_SUCCESS; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | /************************************* |
| 91 | * |
| 92 | * SAD disk image format |
| 93 | * |
| 94 | *************************************/ |
| 95 | |
| 96 | /* |
| 97 | Header (22 bytes): |
| 98 | |
| 99 | 0-17 "Aley's disk backup" |
| 100 | 18 number of sides |
| 101 | 19 number of tracks |
| 102 | 20 number of sectors per track |
| 103 | 21 sector size divided by 64 |
| 104 | |
| 105 | Data: |
| 106 | |
| 107 | Side 0 Track 0 |
| 108 | Side 0 Track 1 |
| 109 | Side 0 Track 2 |
| 110 | ... |
| 111 | Side 1 Track 0 |
| 112 | Side 1 Track 1 |
| 113 | ... |
| 114 | */ |
| 115 | |
| 116 | |
| 117 | static UINT64 coupe_sad_translate_offset(floppy_image *floppy, |
| 118 | const struct basicdsk_geometry *geom, int track, int head, int sector) |
| 119 | { |
| 120 | return head * geom->tracks * geom->sectors + geom->sectors * track + sector; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static void coupe_sad_interpret_header(floppy_image *floppy, |
| 125 | int *heads, int *tracks, int *sectors, int *sector_size) |
| 126 | { |
| 127 | UINT8 header[SAD_HEADER_LEN]; |
| 128 | |
| 129 | floppy_image_read(floppy, header, 0, SAD_HEADER_LEN); |
| 130 | |
| 131 | *heads = header[18]; |
| 132 | *tracks = header[19]; |
| 133 | *sectors = header[20]; |
| 134 | *sector_size = header[21] << 6; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | FLOPPY_CONSTRUCT( coupe_sad_construct ) |
| 139 | { |
| 140 | struct basicdsk_geometry geometry; |
| 141 | int heads, tracks, sectors, sector_length; |
| 142 | |
| 143 | memset(&geometry, 0, sizeof(geometry)); |
| 144 | |
| 145 | if (params) |
| 146 | { |
| 147 | /* create */ |
| 148 | UINT8 header[SAD_HEADER_LEN]; |
| 149 | |
| 150 | /* get format options */ |
| 151 | heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 152 | tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 153 | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 154 | sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 155 | |
| 156 | /* build the header */ |
| 157 | memset(header, 0, SAD_HEADER_LEN); |
| 158 | memcpy(header, SAD_SIGNATURE, 18); |
| 159 | header[18] = heads; |
| 160 | header[19] = tracks; |
| 161 | header[20] = sectors; |
| 162 | header[21] = sector_length >> 6; |
| 163 | |
| 164 | /* and write it to disk */ |
| 165 | floppy_image_write(floppy, header, 0, sizeof(header)); |
| 166 | } |
| 167 | else |
| 168 | { |
| 169 | /* load */ |
| 170 | coupe_sad_interpret_header(floppy, &heads, &tracks, §ors, §or_length); |
| 171 | } |
| 172 | |
| 173 | /* fill in the data */ |
| 174 | geometry.offset = SAD_HEADER_LEN; |
| 175 | geometry.heads = heads; |
| 176 | geometry.tracks = tracks; |
| 177 | geometry.sectors = sectors; |
| 178 | geometry.first_sector_id = 1; |
| 179 | geometry.sector_length = sector_length; |
| 180 | geometry.translate_offset = coupe_sad_translate_offset; |
| 181 | |
| 182 | return basicdsk_construct(floppy, &geometry); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | FLOPPY_IDENTIFY( coupe_sad_identify ) |
| 187 | { |
| 188 | int heads, tracks, sectors, sector_size; |
| 189 | UINT64 size, calculated_size; |
| 190 | |
| 191 | size = floppy_image_size(floppy); |
| 192 | |
| 193 | /* read values from SAD header */ |
| 194 | coupe_sad_interpret_header(floppy, &heads, &tracks, §ors, §or_size); |
| 195 | |
| 196 | /* calculate expected disk image size */ |
| 197 | calculated_size = SAD_HEADER_LEN + heads * tracks * sectors * sector_size; |
| 198 | |
| 199 | *vote = (size == calculated_size) ? 100 : 0; |
| 200 | return FLOPPY_ERROR_SUCCESS; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | |
| 205 | /************************************* |
| 206 | * |
| 207 | * SDF disk image format |
| 208 | * |
| 209 | * TODO: wd17xx status codes are |
| 210 | * currently ignored |
| 211 | * |
| 212 | *************************************/ |
| 213 | |
| 214 | static int coupe_sdf_get_heads_per_disk(floppy_image *floppy) |
| 215 | { |
| 216 | struct sdf_tag *tag = (sdf_tag *)floppy_tag(floppy); |
| 217 | return tag->heads; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | static int coupe_sdf_get_tracks_per_disk(floppy_image *floppy) |
| 222 | { |
| 223 | struct sdf_tag *tag = (sdf_tag *)floppy_tag(floppy); |
| 224 | return tag->tracks; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | static UINT32 coupe_sdf_get_track_size(floppy_image *floppy, int head, int track) |
| 229 | { |
| 230 | return SDF_TRACKSIZE; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | static floperr_t coupe_sdf_get_offset(floppy_image *floppy, |
| 235 | int head, int track, UINT64 *offset) |
| 236 | { |
| 237 | struct sdf_tag *tag = (sdf_tag *)floppy_tag(floppy); |
| 238 | |
| 239 | if (head > tag->heads || track > tag->tracks) |
| 240 | return FLOPPY_ERROR_SEEKERROR; |
| 241 | |
| 242 | *offset = (head * tag->tracks + track) * SDF_TRACKSIZE; |
| 243 | |
| 244 | return FLOPPY_ERROR_SUCCESS; |
| 245 | } |
| 246 | |
| 247 | |
| 248 | static floperr_t coupe_sdf_read_track(floppy_image *floppy, |
| 249 | int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 250 | { |
| 251 | floperr_t err; |
| 252 | UINT64 track_offset; |
| 253 | |
| 254 | /* get the offset to this track */ |
| 255 | err = coupe_sdf_get_offset(floppy, head, track, &track_offset); |
| 256 | if (err) return err; |
| 257 | |
| 258 | /* read track data into buffer */ |
| 259 | floppy_image_read(floppy, buffer, offset + track_offset, buflen); |
| 260 | |
| 261 | return FLOPPY_ERROR_SUCCESS; |
| 262 | } |
| 263 | |
| 264 | |
| 265 | static floperr_t coupe_sdf_write_track(floppy_image *floppy, |
| 266 | int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 267 | { |
| 268 | floperr_t err; |
| 269 | UINT64 track_offset; |
| 270 | |
| 271 | /* get the offset to this track */ |
| 272 | err = coupe_sdf_get_offset(floppy, head, track, &track_offset); |
| 273 | if (err) return err; |
| 274 | |
| 275 | /* write buffer to image */ |
| 276 | floppy_image_write(floppy, buffer, offset + track_offset, buflen); |
| 277 | |
| 278 | return FLOPPY_ERROR_SUCCESS; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | static floperr_t coupe_sdf_get_sector_offset(floppy_image *floppy, |
| 283 | int head, int track, int sector, UINT64 *offset) |
| 284 | { |
| 285 | floperr_t err; |
| 286 | UINT8 buffer[SDF_TRACKSIZE]; |
| 287 | int i, buffer_pos = 1; |
| 288 | |
| 289 | /* get track data */ |
| 290 | err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE); |
| 291 | if (err) return err; |
| 292 | |
| 293 | /* check if the sector is available in this track */ |
| 294 | if (sector >= buffer[0]) |
| 295 | return FLOPPY_ERROR_SEEKERROR; |
| 296 | |
| 297 | /* find the right sector in this track */ |
| 298 | for (i = 0; i < buffer[0]; i++) |
| 299 | { |
| 300 | int sector_number = buffer[buffer_pos + 4]; |
| 301 | |
| 302 | if (sector_number - 1 == sector) |
| 303 | { |
| 304 | *offset = buffer_pos; |
| 305 | return FLOPPY_ERROR_SUCCESS; |
| 306 | } |
| 307 | |
| 308 | buffer_pos += (128 << buffer[buffer_pos + 5]) + 8; |
| 309 | } |
| 310 | |
| 311 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 312 | } |
| 313 | |
| 314 | |
| 315 | static floperr_t coupe_sdf_get_total_sector_offset(floppy_image *floppy, |
| 316 | int head, int track, int sector, UINT64 *offset) |
| 317 | { |
| 318 | floperr_t err; |
| 319 | UINT64 track_offset, sector_offset; |
| 320 | |
| 321 | /* get offset to the track start */ |
| 322 | err = coupe_sdf_get_offset(floppy, head, track, &track_offset); |
| 323 | if (err) return err; |
| 324 | |
| 325 | /* get offset to the start of the sector */ |
| 326 | err = coupe_sdf_get_sector_offset(floppy, head, track, sector, §or_offset); |
| 327 | if (err) return err; |
| 328 | |
| 329 | *offset = track_offset + sector_offset; |
| 330 | |
| 331 | return FLOPPY_ERROR_SUCCESS; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | static floperr_t coupe_sdf_get_sector_length(floppy_image *floppy, |
| 336 | int head, int track, int sector, UINT32 *sector_length) |
| 337 | { |
| 338 | floperr_t err; |
| 339 | UINT8 buffer[SDF_TRACKSIZE]; |
| 340 | UINT64 offset; |
| 341 | |
| 342 | /* get track data */ |
| 343 | err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE); |
| 344 | if (err) return err; |
| 345 | |
| 346 | /* get offset to the start of the sector */ |
| 347 | err = coupe_sdf_get_sector_offset(floppy, head, track, sector, &offset); |
| 348 | if (err) return err; |
| 349 | |
| 350 | /* get size */ |
| 351 | *sector_length = 128 << buffer[offset + 5]; |
| 352 | |
| 353 | return FLOPPY_ERROR_SUCCESS; |
| 354 | } |
| 355 | |
| 356 | |
| 357 | static floperr_t coupe_sdf_get_indexed_sector_info(floppy_image *floppy, |
| 358 | int head, int track, int sector_index, |
| 359 | int *cylinder, int *side, int *sector, UINT32 *sector_length, |
| 360 | unsigned long *flags) |
| 361 | { |
| 362 | floperr_t err; |
| 363 | UINT8 buffer[SDF_TRACKSIZE]; |
| 364 | UINT64 offset; |
| 365 | |
| 366 | /* get track data */ |
| 367 | err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE); |
| 368 | if (err) return err; |
| 369 | |
| 370 | /* get offset to the start of the sector */ |
| 371 | err = coupe_sdf_get_sector_offset(floppy, head, track, sector_index, &offset); |
| 372 | if (err) return err; |
| 373 | |
| 374 | /* extract data */ |
| 375 | if (cylinder) *cylinder = buffer[offset + 2]; |
| 376 | if (side) *side = buffer[offset + 3]; |
| 377 | if (sector) *sector = buffer[offset + 4]; |
| 378 | if (sector_length) *sector_length = 128 << buffer[offset + 5]; |
| 379 | if (flags) *flags = 0; /* TODO: read DAM or DDAM and determine flags */ |
| 380 | |
| 381 | return FLOPPY_ERROR_SUCCESS; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | static floperr_t coupe_sdf_read_indexed_sector(floppy_image *floppy, |
| 386 | int head, int track, int sector, void *buffer, size_t buflen) |
| 387 | { |
| 388 | floperr_t err; |
| 389 | UINT64 offset; |
| 390 | |
| 391 | err = coupe_sdf_get_total_sector_offset(floppy, head, track, sector, &offset); |
| 392 | if (err) return err; |
| 393 | |
| 394 | /* read sector data into buffer */ |
| 395 | floppy_image_read(floppy, buffer, offset + 8, buflen); |
| 396 | |
| 397 | return FLOPPY_ERROR_SUCCESS; |
| 398 | } |
| 399 | |
| 400 | |
| 401 | static floperr_t coupe_sdf_write_indexed_sector(floppy_image *floppy, |
| 402 | int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 403 | { |
| 404 | floperr_t err; |
| 405 | UINT64 offset; |
| 406 | |
| 407 | err = coupe_sdf_get_total_sector_offset(floppy, head, track, sector, &offset); |
| 408 | if (err) return err; |
| 409 | |
| 410 | /* write buffer into image */ |
| 411 | floppy_image_write(floppy, buffer, offset + 8, buflen); |
| 412 | |
| 413 | return FLOPPY_ERROR_SUCCESS; |
| 414 | } |
| 415 | |
| 416 | |
| 417 | static void coupe_sdf_interpret_header(floppy_image *floppy, int *heads, int *tracks) |
| 418 | { |
| 419 | UINT64 size = floppy_image_size(floppy); |
| 420 | |
| 421 | if (size % SDF_TRACKSIZE == 0) |
| 422 | { |
| 423 | *heads = 2; |
| 424 | *tracks = size / (SDF_TRACKSIZE * 2); |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | *heads = 0; |
| 429 | *tracks = 0; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | |
| 434 | FLOPPY_CONSTRUCT( coupe_sdf_construct ) |
| 435 | { |
| 436 | struct FloppyCallbacks *callbacks; |
| 437 | struct sdf_tag *tag; |
| 438 | int heads, tracks; |
| 439 | |
| 440 | if (params) |
| 441 | { |
| 442 | /* create */ |
| 443 | return FLOPPY_ERROR_UNSUPPORTED; |
| 444 | } |
| 445 | else |
| 446 | { |
| 447 | /* load */ |
| 448 | coupe_sdf_interpret_header(floppy, &heads, &tracks); |
| 449 | } |
| 450 | |
| 451 | tag = (sdf_tag *)floppy_create_tag(floppy, sizeof(struct sdf_tag)); |
| 452 | |
| 453 | if (!tag) |
| 454 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 455 | |
| 456 | tag->heads = heads; |
| 457 | tag->tracks = tracks; |
| 458 | |
| 459 | callbacks = floppy_callbacks(floppy); |
| 460 | callbacks->read_track = coupe_sdf_read_track; |
| 461 | callbacks->write_track = coupe_sdf_write_track; |
| 462 | callbacks->get_heads_per_disk = coupe_sdf_get_heads_per_disk; |
| 463 | callbacks->get_tracks_per_disk = coupe_sdf_get_tracks_per_disk; |
| 464 | callbacks->get_track_size = coupe_sdf_get_track_size; |
| 465 | callbacks->get_sector_length = coupe_sdf_get_sector_length; |
| 466 | callbacks->read_indexed_sector = coupe_sdf_read_indexed_sector; |
| 467 | callbacks->write_indexed_sector = coupe_sdf_write_indexed_sector; |
| 468 | callbacks->get_indexed_sector_info = coupe_sdf_get_indexed_sector_info; |
| 469 | |
| 470 | return FLOPPY_ERROR_SUCCESS; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | FLOPPY_IDENTIFY( coupe_sdf_identify ) |
| 475 | { |
| 476 | int heads, tracks; |
| 477 | |
| 478 | /* read header values */ |
| 479 | coupe_sdf_interpret_header(floppy, &heads, &tracks); |
| 480 | |
| 481 | /* check for sensible values */ |
| 482 | if (heads > 0 && heads < 3 && tracks > 0 && tracks < 84) |
| 483 | *vote = 100; |
| 484 | else |
| 485 | *vote = 0; |
| 486 | |
| 487 | return FLOPPY_ERROR_SUCCESS; |
| 488 | } |
src/lib/formats/ti99_dsk.c
| r0 | r11458 | |
| 1 | /********************************************************************* |
| 2 | * |
| 3 | * formats/ti99_dsk.c |
| 4 | * |
| 5 | * TI99 and Geneve disk images |
| 6 | * Sector Dump and Track Dump format |
| 7 | * |
| 8 | * used by TI-99/4, TI-99/4A, TI-99/8, SGCPU ("TI-99/4P"), and Geneve |
| 9 | * |
| 10 | * The Sector Dump Format is also known as v9t9 (named after the first TI |
| 11 | * emulator to use this format). It is a contiguous sequence of sector contents |
| 12 | * without track data. The first sector of the disk is located at the start of |
| 13 | * the image, while the last sector is at its end. |
| 14 | * |
| 15 | * The Track Dump Format is also known as pc99 (again, named after the first |
| 16 | * TI emulator to use this format). It is a contiguous sequence of track |
| 17 | * contents, containing all information including address marks and CRC, but it |
| 18 | * does not contain clock signals. Therefore, the format requires that the |
| 19 | * address marks be at the same positions within each track. |
| 20 | * |
| 21 | * Both formats allow for a broad range of medium sizes. All sectors are 256 |
| 22 | * bytes long. The most common formats are 9 sectors per track, single-sided, |
| 23 | * 40 tracks, which yields 90 KiB of sector data (known as SSSD), and 18 |
| 24 | * sectors per track, double-sided, and 40 tracks, which is 360 KiB (known as |
| 25 | * DSDD). There are rare occurances of 8/16 sectors/track |
| 26 | * (prototypical TI double-density controller) and 35 track media. Newer |
| 27 | * controllers and ROMs allow for up to 36 sectors per track and 80 tracks on |
| 28 | * both sides, which is 2,88 MiB (DSHD80). |
| 29 | * |
| 30 | * Different to earlier implementations, these format implementations provide |
| 31 | * full track read/write capabilities. For the SDF format, the missing track |
| 32 | * information is recreated, and a common interleave is assumed. |
| 33 | * |
| 34 | * Note that in principle, writing tracks may change track sizes. |
| 35 | * As neither one of the formats contains meta-information about track lengths, |
| 36 | * and as the image consists of a contiguous sequence of the tracks, we |
| 37 | * cannot allow different track sizes currently. Accordingly, the track size |
| 38 | * cannot be changed after the image has been created by imgtool. |
| 39 | * Hence, also sector count per tracks and recording type (FM or MFM) cannot be |
| 40 | * changed without breaking the image. |
| 41 | * For example, MFM disks with 18 sectors per track can only be reformatted to |
| 42 | * MFM/18. Moreover, while this could be handled in a future revision, |
| 43 | * the number of tracks should also remain unchanged. |
| 44 | * |
| 45 | * You can use blank image files (filled with zeros or arbitrary data) and use |
| 46 | * the Disk Manager cartridge to properly format them. Also, this can be done |
| 47 | * by imgtool. The image file must match the format length (representing the |
| 48 | * space on the physical medium). |
| 49 | * |
| 50 | * 80 track drive handling |
| 51 | * |
| 52 | * For the TI system there may be a situation where 40 track disks are used |
| 53 | * in 80 track drives. In these cases, the disk driver applies double-stepping, |
| 54 | * that is, it advances the head twice in order to seek the next track. We |
| 55 | * emulate this by dividing the track number by 2, which means that two tracks |
| 56 | * are mapped to one (0,1 -> 0; 2,3 -> 1; 4,5 -> 2; ...; 78,79 -> 39) |
| 57 | * |
| 58 | * Technical detail: The size of the track must be given before actually |
| 59 | * starting write_track as the controller implementation uses buffered |
| 60 | * transfer. |
| 61 | * |
| 62 | * Michael Zapf, Feb 2010 |
| 63 | * |
| 64 | * TODO: Link to imgtool. imgtool still uses its own format creation |
| 65 | * |
| 66 | * FIXME: If image is broken with good first track and defect higher tracks, |
| 67 | * tdf_guess_geometry fails to define a geometry. The guessing should |
| 68 | * always be done. |
| 69 | ********************************************************************/ |
| 70 | |
| 71 | #include <string.h> |
| 72 | #include <assert.h> |
| 73 | #include <time.h> |
| 74 | |
| 75 | #include "osdepend.h" |
| 76 | #include "imageutl.h" |
| 77 | #include "ti99_dsk.h" |
| 78 | |
| 79 | #define SECTOR_SIZE 256 |
| 80 | #define TI99_IDAM_LENGTH 7 |
| 81 | |
| 82 | /* |
| 83 | Determines whether we are using 80 track drives. This variable is |
| 84 | necessary unless we get information about the dip |
| 85 | switch settings. It is set by the disk controller implementations |
| 86 | (bwg, hfdc, ti_fdc). |
| 87 | */ |
| 88 | static int use_80_track_drives = FALSE; |
| 89 | |
| 90 | struct ti99dsk_geometry |
| 91 | { |
| 92 | UINT8 sides; |
| 93 | UINT8 tracksperside; |
| 94 | UINT8 secspertrack; |
| 95 | UINT8 density; |
| 96 | }; |
| 97 | |
| 98 | struct ti99dsk_tag |
| 99 | { |
| 100 | int heads; |
| 101 | int tracks; |
| 102 | int sectors; |
| 103 | int track_size; // Complete track; to be calculated |
| 104 | int format; |
| 105 | int first_idam; |
| 106 | int dam_offset; |
| 107 | }; |
| 108 | |
| 109 | /* |
| 110 | Parametrizes the format to use 80 track drives. For TI systems we |
| 111 | need to be able to emulate 40 track drives and 80 track drives. |
| 112 | This is set in the configuration, but the format has no direct access. |
| 113 | So as a preliminary solution we use this global function which sets the |
| 114 | flag. This function is called by the disk controller implementations |
| 115 | (bwg, hfdc, ti_fdc). |
| 116 | */ |
| 117 | void ti99_set_80_track_drives(int use80) |
| 118 | { |
| 119 | use_80_track_drives = use80; |
| 120 | } |
| 121 | |
| 122 | #define TI99_DSK_TAG "ti99dsktag" |
| 123 | #define TI99DSK_BLOCKNOTFOUND -1 |
| 124 | |
| 125 | /* |
| 126 | Searches a block containing number * byte, starting at the given |
| 127 | position. Returns the position of the first byte of the block. |
| 128 | */ |
| 129 | static int find_block(const UINT8 *buffer, int start, int stop, UINT8 byte, size_t number) |
| 130 | { |
| 131 | int i = start; |
| 132 | size_t current = number; |
| 133 | while (i < stop && current > 0) |
| 134 | { |
| 135 | if (buffer[i++] != byte) |
| 136 | { |
| 137 | current = number; |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | current--; |
| 142 | } |
| 143 | } |
| 144 | if (current==0) |
| 145 | { |
| 146 | return i - number; |
| 147 | } |
| 148 | else |
| 149 | return TI99DSK_BLOCKNOTFOUND; |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | Queried by flopdrv. Instead of the image tracks, we return the tracks |
| 154 | of the floppy drive. We assume we always have two heads. |
| 155 | */ |
| 156 | static int ti99_get_heads_per_disk(floppy_image *floppy) |
| 157 | { |
| 158 | // struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 159 | // return tag->heads; |
| 160 | return 2; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | Queried by flopdrv. Instead of the image tracks, we return the tracks |
| 165 | of the floppy drive. |
| 166 | */ |
| 167 | static int ti99_get_tracks_per_disk(floppy_image *floppy) |
| 168 | { |
| 169 | int drivetracks = 42; |
| 170 | |
| 171 | // This may fail on startup; therefore we use a special function in |
| 172 | // in the floppy controller implementations to explicitly set the geometry. |
| 173 | if (use_80_track_drives) |
| 174 | { |
| 175 | drivetracks = 83; |
| 176 | } |
| 177 | return drivetracks; |
| 178 | } |
| 179 | |
| 180 | static floperr_t ti99_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 181 | { |
| 182 | *sector_length=SECTOR_SIZE; |
| 183 | return FLOPPY_ERROR_SUCCESS; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | Return the track size. We do not allow different track |
| 188 | sizes, so we store the track size in the tag. |
| 189 | */ |
| 190 | static UINT32 ti99_get_track_size(floppy_image *floppy, int head, int track) |
| 191 | { |
| 192 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 193 | return tag->track_size; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | static void create_vib(UINT8 *sector0, option_resolution *params) |
| 198 | { |
| 199 | const char *name = "UNNAMED "; |
| 200 | int sides, tracksperside,secspertrack,totalsectors,ausize,allocbytes,i; |
| 201 | |
| 202 | enum { NAME=0, TOTALSECS=10, SECSPERTRACK=11, SIG=12, PROT=16, TRACKS=17, SIDES=18, DENSITY=19, EXT=20, ALLOC=56 }; |
| 203 | |
| 204 | sides = option_resolution_lookup_int(params, PARAM_HEADS); |
| 205 | tracksperside = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 206 | secspertrack = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 207 | |
| 208 | totalsectors = sides * tracksperside * secspertrack; |
| 209 | |
| 210 | memcpy(§or0[NAME], name, 10); |
| 211 | |
| 212 | sector0[TOTALSECS] = (totalsectors>>8)&0xff; |
| 213 | sector0[TOTALSECS+1] = (totalsectors&0xff); |
| 214 | |
| 215 | sector0[SECSPERTRACK] = secspertrack; |
| 216 | sector0[SIG] = 'D'; |
| 217 | sector0[SIG+1] = 'S'; |
| 218 | sector0[SIG+2] = 'K'; |
| 219 | |
| 220 | sector0[PROT] = ' '; |
| 221 | sector0[TRACKS] = tracksperside; |
| 222 | sector0[SIDES] = sides; |
| 223 | sector0[DENSITY] = (secspertrack<10)? 0x01 : 0x02; |
| 224 | |
| 225 | for (i=EXT; i < ALLOC; i++) |
| 226 | sector0[i] = 0; |
| 227 | |
| 228 | ausize = (totalsectors / 1441) + 1; |
| 229 | |
| 230 | allocbytes = totalsectors / ausize / 8; |
| 231 | |
| 232 | for (i=ALLOC; i < ALLOC+allocbytes; i++) |
| 233 | sector0[i] = 0; |
| 234 | |
| 235 | while (i++ < SECTOR_SIZE) |
| 236 | sector0[i] = 0xff; |
| 237 | } |
| 238 | */ |
| 239 | /* ========================================================================= |
| 240 | Sector dump format |
| 241 | =========================================================================*/ |
| 242 | |
| 243 | static floperr_t ti99_sdf_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen); |
| 244 | static floperr_t ti99_sdf_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam); |
| 245 | |
| 246 | /* ----------------------------------------------------------------------- |
| 247 | * Guess the geometry of the sector dump disk. |
| 248 | * |
| 249 | * Sector size is always 256 bytes. |
| 250 | * |
| 251 | * Common formats: |
| 252 | * 90 KiB = 40 tracks / 1 side / 9 sectors = SSSD |
| 253 | * 180 KiB = 40 tracks / 2 side / 9 sectors = DSSD (most common) |
| 254 | * = 40 tracks / 1 side / 18 sectors = SSDD (rare) |
| 255 | * = 80 tracks / 1 side / 9 sectors = SSSD80 (rare) |
| 256 | * 360 KiB = 40 tracks / 2 side / 18 sectors = DSDD (most common) |
| 257 | * = 80 tracks / 2 side / 9 sectors = DSSD80 (rare) |
| 258 | * = 80 tracks / 1 side / 18 sectors = SSDD80 (rare) |
| 259 | * = 40 tracks / 1 side / 36 sectors = SSHD (rare) |
| 260 | * 720 KiB = 80 tracks / 2 side / 18 sectors = DSDD80 (most common) |
| 261 | * 1440 KiB= 80 tracks / 2 side / 36 sectors = DSHD80 (most common) |
| 262 | * |
| 263 | * Moreover, there may be formats with 35 tracks (ancient) and 8 sectors/track. |
| 264 | * We only check for the 8 sector formats. |
| 265 | * 160 KiB = 40 tracks / 1 side / 16 sectors = SSDD8 |
| 266 | * 320 KiB = 40 tracks / 2 side / 16 sectors = DSDD8 |
| 267 | * |
| 268 | * The Volume Information Block may contain suitable information, but not |
| 269 | * before the disk is formatted and initialized. |
| 270 | * |
| 271 | * returns 100 if recognized, 0 if not recognized |
| 272 | * ----------------------------------------------------------------------- */ |
| 273 | |
| 274 | static int ti99_sdf_guess_geometry(floppy_image *floppy, UINT64 size, |
| 275 | struct ti99dsk_geometry *geometry) |
| 276 | { |
| 277 | int totsecs; |
| 278 | typedef struct ti99_vib |
| 279 | { |
| 280 | char name[10]; // volume name (10 characters, pad with spaces) |
| 281 | UINT8 totsecsMSB; // disk length in sectors (big-endian) (usually 360, 720 or 1440) |
| 282 | UINT8 totsecsLSB; |
| 283 | UINT8 secspertrack; // sectors per track (usually 9 (FM) or 18 (MFM)) |
| 284 | UINT8 id[3]; // String "DSK" |
| 285 | UINT8 protection; // 'P' if disk is protected, ' ' otherwise. |
| 286 | UINT8 tracksperside; // tracks per side (usually 40) |
| 287 | UINT8 sides; // sides (1 or 2) |
| 288 | UINT8 density; // 0,1 (FM) or 2,3,4 (MFM) |
| 289 | UINT8 res[36]; // Empty for traditional disks, or up to 3 directory pointers |
| 290 | UINT8 abm[200]; // allocation bitmap: a 1 for each sector in use (sector 0 is LSBit of byte 0, |
| 291 | // sector 7 is MSBit of byte 0, sector 8 is LSBit of byte 1, etc.) |
| 292 | } ti99_vib; |
| 293 | |
| 294 | ti99_vib vib; |
| 295 | UINT32 file_size; |
| 296 | struct ti99dsk_geometry dummy_geometry; |
| 297 | |
| 298 | if (geometry) |
| 299 | memset(geometry, 0, sizeof(*geometry)); |
| 300 | else |
| 301 | geometry = &dummy_geometry; |
| 302 | |
| 303 | if (size > 0xFFFFFFFF) |
| 304 | return 0; |
| 305 | |
| 306 | file_size = (UINT32) size; |
| 307 | |
| 308 | // Read the Volume Information Block |
| 309 | floppy_image_read(floppy, &vib, 0, sizeof(vib)); |
| 310 | |
| 311 | // If we have read the sector successfully, let us parse it |
| 312 | totsecs = (vib.totsecsMSB << 8) | vib.totsecsLSB; |
| 313 | geometry->secspertrack = vib.secspertrack; |
| 314 | if (geometry->secspertrack == 0) |
| 315 | // Some images might be like this, because the original SSSD |
| 316 | // TI controller always assumes 9. |
| 317 | geometry->secspertrack = 9; |
| 318 | geometry->tracksperside = vib.tracksperside; |
| 319 | if (geometry->tracksperside == 0) |
| 320 | // Some images are like this, because the original SSSD TI controller |
| 321 | // always assumes 40. |
| 322 | geometry->tracksperside = 40; |
| 323 | geometry->sides = vib.sides; |
| 324 | if (geometry->sides == 0) |
| 325 | // Some images are like this, because the original SSSD TI controller |
| 326 | // always assumes that tracks beyond 40 are on side 2. */ |
| 327 | geometry->sides = totsecs / (geometry->secspertrack * geometry->tracksperside); |
| 328 | geometry->density = vib.density; |
| 329 | // check that the format makes sense |
| 330 | if (((geometry->secspertrack * geometry->tracksperside * geometry->sides) == totsecs) |
| 331 | && (geometry->density <= 4) && (totsecs >= 2) && (! memcmp(vib.id, "DSK", 3)) |
| 332 | && (file_size == totsecs*256)) |
| 333 | { |
| 334 | logerror("SDF/VIB consistent; tracks = %d, heads = %d, sectors = %d\n", geometry->tracksperside, geometry->sides, geometry->secspertrack); |
| 335 | return 100; |
| 336 | } |
| 337 | logerror("SDF/VIB not consistent; guessing format\n"); |
| 338 | |
| 339 | // So that was not consistent. We guess the size from the file size |
| 340 | // and assume that the VIB did not contain reliable data. For the |
| 341 | // ambiguous case we choose the most common format. |
| 342 | |
| 343 | switch (file_size) |
| 344 | { |
| 345 | case 1*40*9*256: |
| 346 | // 90kbytes: SSSD |
| 347 | case 0: |
| 348 | /*default:*/ |
| 349 | geometry->sides = 1; |
| 350 | geometry->tracksperside = 40; |
| 351 | geometry->secspertrack = 9; |
| 352 | geometry->density = 1; |
| 353 | break; |
| 354 | |
| 355 | case 2*40*9*256: |
| 356 | // 180kbytes: either DSSD or 18-sector-per-track SSDD. |
| 357 | // We assume DSSD since DSSD is more common and is supported by |
| 358 | // the original TI SD disk controller. |
| 359 | geometry->sides = 2; |
| 360 | geometry->tracksperside = 40; |
| 361 | geometry->secspertrack = 9; |
| 362 | geometry->density = 1; |
| 363 | break; |
| 364 | |
| 365 | case 1*40*16*256: |
| 366 | // 160kbytes: 16-sector-per-track SSDD (standard format for TI |
| 367 | // DD disk controller prototype, and the TI hexbus disk |
| 368 | // controller?) */ |
| 369 | geometry->sides = 1; |
| 370 | geometry->tracksperside = 40; |
| 371 | geometry->secspertrack = 16; |
| 372 | geometry->density = 2; |
| 373 | break; |
| 374 | |
| 375 | case 2*40*16*256: |
| 376 | // 320kbytes: 16-sector-per-track DSDD (standard format for TI |
| 377 | // DD disk controller prototype, and TI hexbus disk |
| 378 | // controller?) |
| 379 | geometry->sides = 2; |
| 380 | geometry->tracksperside = 40; |
| 381 | geometry->secspertrack = 16; |
| 382 | geometry->density = 2; |
| 383 | break; |
| 384 | |
| 385 | case 2*40*18*256: |
| 386 | // 360kbytes: 18-sector-per-track DSDD (standard format for most |
| 387 | // third-party DD disk controllers, but reportedly not supported by |
| 388 | // the original TI DD disk controller prototype) |
| 389 | geometry->sides = 2; |
| 390 | geometry->tracksperside = 40; |
| 391 | geometry->secspertrack = 18; |
| 392 | geometry->density = 2; |
| 393 | break; |
| 394 | |
| 395 | case 2*80*18*256: |
| 396 | // 720kbytes: 18-sector-per-track 80-track DSDD (Myarc only) |
| 397 | geometry->sides = 2; |
| 398 | geometry->tracksperside = 80; |
| 399 | geometry->secspertrack = 18; |
| 400 | geometry->density = 2; |
| 401 | break; |
| 402 | |
| 403 | case 2*80*36*256: |
| 404 | // 1.44Mbytes: DSHD (Myarc only) |
| 405 | geometry->sides = 2; |
| 406 | geometry->tracksperside = 80; |
| 407 | geometry->secspertrack = 36; |
| 408 | geometry->density = 3; |
| 409 | break; |
| 410 | |
| 411 | default: |
| 412 | logerror("Unrecognized disk image geometry\n"); |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | logerror("SDF geometry guess: tracks = %d, heads = %d, sectors = %d\n", geometry->tracksperside, geometry->sides, geometry->secspertrack); |
| 417 | return 100; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | Figure out the interleave for the sector arrangement in the track. For |
| 422 | SDF format this is required for the read_track function. |
| 423 | |
| 424 | Interleave: |
| 425 | 0 7 5 3 1 8 6 4 2; |
| 426 | start with 0, 3, 6, 0, 3, 6, ... |
| 427 | 0 11 4 15 8 1 12 5 16 9 2 13 6 17 10 3 14 7 |
| 428 | start with 0 for every track |
| 429 | |
| 430 | Rare formats: 8 sectors |
| 431 | 0 3 6 1 4 7 2 5 (i*3)%8 |
| 432 | |
| 433 | 16 sectors |
| 434 | 0 9 2 11 4 13 6 15 8 1 10 3 12 5 14 7 (i*9)%16 |
| 435 | |
| 436 | 36 sectors |
| 437 | 0 11 22 ... 3 14 25 (i*11)%36 |
| 438 | */ |
| 439 | static void guess_interleave(int sectors, int *step, int *initial) |
| 440 | { |
| 441 | switch (sectors) |
| 442 | { |
| 443 | case 8: |
| 444 | *step = 3; |
| 445 | /* initial is the offset from track to track+1 of the starting sector number */ |
| 446 | *initial = 0; |
| 447 | break; |
| 448 | case 9: |
| 449 | *step = 7; |
| 450 | *initial = 3; |
| 451 | break; |
| 452 | case 16: |
| 453 | *step = 9; |
| 454 | *initial = 0; |
| 455 | break; |
| 456 | case 18: |
| 457 | *step = 11; |
| 458 | *initial = 0; |
| 459 | break; |
| 460 | case 36: |
| 461 | *step = 11; |
| 462 | *initial = 0; |
| 463 | break; |
| 464 | default: |
| 465 | *step = 11; |
| 466 | *initial = 0; |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | Delivers a whole track. As the image consists only of |
| 473 | sector data, we must reconstruct the track data and |
| 474 | then insert the sector data. |
| 475 | |
| 476 | The track layout is exactly the Track Dump Format, except that the |
| 477 | CRC values must be correctly calculated. |
| 478 | |
| 479 | Total length (FM) = 247 + sectors*334; (sectors=8 or 9) |
| 480 | Total length (MFM) = 752 + sectors*340; (sectors=16 or 18) |
| 481 | */ |
| 482 | static floperr_t ti99_sdf_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 483 | { |
| 484 | int sector; |
| 485 | int startstep; |
| 486 | int step; |
| 487 | int position=0; |
| 488 | unsigned short crc; |
| 489 | int i; |
| 490 | floperr_t err; |
| 491 | int imgtrack = track; |
| 492 | |
| 493 | UINT8 *trackdata = (UINT8*)buffer; |
| 494 | |
| 495 | /* Determine from the image geometry whether we have FM or MFM */ |
| 496 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 497 | |
| 498 | // Do we have a 40 track disk in an 80 track drive? In that case we |
| 499 | // double the "width" of the tracks. */ |
| 500 | if (use_80_track_drives && tag->tracks<=40) |
| 501 | { |
| 502 | imgtrack = track / 2; |
| 503 | } |
| 504 | |
| 505 | guess_interleave(tag->sectors, &step, &startstep); |
| 506 | |
| 507 | if (tag->sectors < 10) /* must be FM */ |
| 508 | { |
| 509 | /* Write lead-in. */ |
| 510 | memset(trackdata, 0x00, 16); |
| 511 | position += 16; |
| 512 | for (i=0; i < tag->sectors; i++) |
| 513 | { |
| 514 | /* Create interleaving */ |
| 515 | sector = (i*step + imgtrack*startstep) % tag->sectors; |
| 516 | |
| 517 | memset(&trackdata[position], 0x00, 6); |
| 518 | position += 6; |
| 519 | /* Set IDAM */ |
| 520 | trackdata[position++] = 0xfe; |
| 521 | trackdata[position++] = imgtrack; |
| 522 | trackdata[position++] = head; |
| 523 | trackdata[position++] = sector; |
| 524 | trackdata[position++] = 0x01; |
| 525 | /* Calculate CRC16. Preset to 0xffff (what is 0xcdb4?) */ |
| 526 | crc = ccitt_crc16(0xffff, &trackdata[position-(TI99_IDAM_LENGTH-2)], TI99_IDAM_LENGTH - 2); |
| 527 | trackdata[position++] = (crc>>8)&0xff; |
| 528 | trackdata[position++] = crc & 0xff; |
| 529 | |
| 530 | /* Write Gap2 */ |
| 531 | memset(&trackdata[position], 0xff, 11); |
| 532 | position += 11; |
| 533 | memset(&trackdata[position], 0x00, 6); |
| 534 | position += 6; |
| 535 | /* Write DAM */ |
| 536 | trackdata[position++] = 0xfb; |
| 537 | |
| 538 | /* Locate the sector content in the image and load it. */ |
| 539 | err = ti99_sdf_read_sector(floppy, head, imgtrack, sector, &trackdata[position], SECTOR_SIZE); |
| 540 | if (err) |
| 541 | return err; |
| 542 | |
| 543 | position += SECTOR_SIZE; |
| 544 | |
| 545 | /* Set CRC16 */ |
| 546 | crc = ccitt_crc16(0xffff, &trackdata[position-(SECTOR_SIZE+1)], SECTOR_SIZE+1); |
| 547 | trackdata[position++] = (crc>>8)&0xff; |
| 548 | trackdata[position++] = crc & 0xff; |
| 549 | |
| 550 | /* Write Gap3 */ |
| 551 | memset(&trackdata[position], 0xff, 45); |
| 552 | position += 45; |
| 553 | } |
| 554 | /* Write lead-out */ |
| 555 | memset(&trackdata[position], 0xff, 231); |
| 556 | position += 231; |
| 557 | } |
| 558 | else /* we have MFM */ |
| 559 | { |
| 560 | /* Write lead-in. */ |
| 561 | memset(trackdata, 0x4e, 40); |
| 562 | position += 40; |
| 563 | for (i=0; i < tag->sectors; i++) |
| 564 | { |
| 565 | sector = (i*step + imgtrack*startstep) % tag->sectors; |
| 566 | |
| 567 | memset(&trackdata[position], 0x00, 10); |
| 568 | position += 10; |
| 569 | /* Write sync */ |
| 570 | memset(&trackdata[position], 0xa1, 3); |
| 571 | position += 3; |
| 572 | /* Set IDAM */ |
| 573 | trackdata[position++] = 0xfe; |
| 574 | trackdata[position++] = imgtrack; |
| 575 | trackdata[position++] = head; |
| 576 | trackdata[position++] = sector; |
| 577 | trackdata[position++] = 0x01; |
| 578 | /* Set CRC16 */ |
| 579 | crc = ccitt_crc16(0xffff, &trackdata[position-TI99_IDAM_LENGTH+2], TI99_IDAM_LENGTH - 2); |
| 580 | trackdata[position++] = (crc>>8)&0xff; |
| 581 | trackdata[position++] = crc & 0xff; |
| 582 | |
| 583 | /* Write Gap2 */ |
| 584 | memset(&trackdata[position], 0x4e, 22); |
| 585 | position += 22; |
| 586 | memset(&trackdata[position], 0x00, 12); |
| 587 | position += 12; |
| 588 | /* Write sync */ |
| 589 | memset(&trackdata[position], 0xa1, 3); |
| 590 | position += 3; |
| 591 | /* Write DAM */ |
| 592 | trackdata[position++] = 0xfb; |
| 593 | |
| 594 | /* Locate the sector content in the image and load it. */ |
| 595 | err = ti99_sdf_read_sector(floppy, head, track, sector, &trackdata[position], SECTOR_SIZE); |
| 596 | if (err) |
| 597 | return err; |
| 598 | |
| 599 | position += SECTOR_SIZE; |
| 600 | |
| 601 | /* Set CRC16 */ |
| 602 | crc = ccitt_crc16(0xffff, &trackdata[position-SECTOR_SIZE-1], SECTOR_SIZE+1); |
| 603 | trackdata[position++] = (crc>>8)&0xff; |
| 604 | trackdata[position++] = crc & 0xff; |
| 605 | |
| 606 | /* Write Gap3 */ |
| 607 | memset(&trackdata[position], 0x4e, 24); |
| 608 | position += 24; |
| 609 | } |
| 610 | /* Write lead-out */ |
| 611 | memset(&trackdata[position], 0x4e, 712); |
| 612 | position += 712; |
| 613 | } |
| 614 | return FLOPPY_ERROR_SUCCESS; |
| 615 | } |
| 616 | |
| 617 | /* |
| 618 | Writes a whole track. As the sector dump format does not include track |
| 619 | data, we have to extract the sector contents and to write them |
| 620 | in sequence into the image. All the rest (gaps, crc, sync bytes) are |
| 621 | dropped here. |
| 622 | Bytes written to locations beyond the image end will be droppped |
| 623 | silently (as if the medium is unwritable from that point). |
| 624 | */ |
| 625 | static floperr_t ti99_sdf_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 626 | { |
| 627 | int current_pos = 0; |
| 628 | UINT8 *track_image; |
| 629 | int leadin, gap1, gap2; |
| 630 | int is_fm, found; |
| 631 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 632 | int imgtrack = track; |
| 633 | |
| 634 | // Find out if we are going to write an FM or MFM track. Note that the |
| 635 | // original TI controller is known to have a bug in the formatting |
| 636 | // routine (using a MOVB instead of a MOV instruction when setting the |
| 637 | // pointer to the track data), outputting false bytes at the |
| 638 | // beginning of the track. So we do not rely on absolute positions |
| 639 | // (and neither does the controller). |
| 640 | track_image = (UINT8*)buffer; |
| 641 | |
| 642 | if (use_80_track_drives && tag->tracks<=40) |
| 643 | { |
| 644 | imgtrack = track / 2; |
| 645 | } |
| 646 | |
| 647 | /* Only search in the first 100 bytes for the start. */ |
| 648 | leadin = 40; |
| 649 | gap1 = 10; |
| 650 | gap2 = 12; |
| 651 | is_fm = FALSE; |
| 652 | |
| 653 | current_pos = find_block(track_image, 0, 100, 0x4e, leadin); |
| 654 | |
| 655 | // In case of defect formats, we continue as far as possible. This |
| 656 | // may lead to sectors not being written. */ |
| 657 | if (current_pos==TI99DSK_BLOCKNOTFOUND) |
| 658 | { |
| 659 | /* Not MFM, possibly FM image */ |
| 660 | /* Again, find Lead-in */ |
| 661 | leadin = 16; |
| 662 | current_pos = find_block(track_image, 0, 100, 0x00, leadin); |
| 663 | if (current_pos==TI99DSK_BLOCKNOTFOUND) |
| 664 | { |
| 665 | /* If neither, forget about this process completely. */ |
| 666 | logerror("Cannot find lead-in for track %d, head %d.\n", track, head); |
| 667 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 668 | } |
| 669 | gap1 = 6; |
| 670 | gap2 = 6; |
| 671 | is_fm = TRUE; |
| 672 | } |
| 673 | |
| 674 | found = FALSE; |
| 675 | /* Get behind lead-in */ |
| 676 | current_pos += leadin; |
| 677 | while (current_pos < buflen) |
| 678 | { |
| 679 | /* We must find the address block to determine the sector. */ |
| 680 | int new_pos = find_block(track_image, current_pos, buflen, 0x00, gap1); |
| 681 | if (new_pos==TI99DSK_BLOCKNOTFOUND) |
| 682 | { |
| 683 | /* Forget about the rest. */ |
| 684 | if (found) break; /* we were already successful, so all ok */ |
| 685 | logerror("Cannot find gap1 for track %d, head %d.\n", track, head); |
| 686 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 687 | } |
| 688 | found = TRUE; |
| 689 | |
| 690 | if (!is_fm) |
| 691 | new_pos += 3; /* skip sync bytes in MFM */ |
| 692 | |
| 693 | if (track_image[new_pos + gap1]==0xfe) |
| 694 | { |
| 695 | current_pos = new_pos + gap1 + 1; |
| 696 | /* IDAM found. */ |
| 697 | int wtrack = track_image[current_pos]; |
| 698 | int whead = track_image[current_pos+1]; |
| 699 | int sector = track_image[current_pos+2]; |
| 700 | if (wtrack == imgtrack && whead == head) |
| 701 | { |
| 702 | /* We skip the first part of gap2. */ |
| 703 | new_pos = find_block(track_image, current_pos, buflen, 0x00, gap2); |
| 704 | if (current_pos==TI99DSK_BLOCKNOTFOUND) |
| 705 | { |
| 706 | logerror("Cannot find gap2 for track %d, head %d.\n", imgtrack, head); |
| 707 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 708 | } |
| 709 | else |
| 710 | { |
| 711 | new_pos += gap2; |
| 712 | if (!is_fm) |
| 713 | new_pos += 3; /* skip sync */ |
| 714 | current_pos = new_pos; |
| 715 | if (track_image[current_pos]==0xfb) |
| 716 | { |
| 717 | /* DAM found. We now write the sector content to the image. */ |
| 718 | ti99_sdf_write_sector(floppy, head, track, sector, &track_image[current_pos+1], SECTOR_SIZE, 0); |
| 719 | current_pos += SECTOR_SIZE; |
| 720 | } |
| 721 | else |
| 722 | logerror("DAM not found. Not writing write sector %d\n", sector); |
| 723 | /* else not found, ignore this sector. */ |
| 724 | } |
| 725 | } |
| 726 | else |
| 727 | { |
| 728 | // else the sector head data do not match the |
| 729 | // current track and head. Ignore the sector. |
| 730 | logerror("Wrong track: wtrack=%d, imgtrack=%d, whead=%d, imghead=%d\n",wtrack,imgtrack, whead,head); |
| 731 | } |
| 732 | } |
| 733 | else |
| 734 | { |
| 735 | /* One step forward. Retry. */ |
| 736 | current_pos++; |
| 737 | } |
| 738 | } |
| 739 | return FLOPPY_ERROR_SUCCESS; |
| 740 | } |
| 741 | |
| 742 | /* |
| 743 | This method creates a properly formatted image. |
| 744 | |
| 745 | For a proper image, we need to format all tracks, and to create a |
| 746 | minimal filesystem: |
| 747 | |
| 748 | name = "UNNAMED " |
| 749 | number of sectors |
| 750 | sectors/track |
| 751 | "DSK" |
| 752 | protection = 0x00 |
| 753 | tracks/side |
| 754 | sides |
| 755 | density (0,1 = FM, 2 = MFM) |
| 756 | 0x14 - 0x37 : 0x00 |
| 757 | Allocation bitmap: 0xFF except for the first #sectors/AU size |
| 758 | |
| 759 | AU size = 1 (360 - 1440 sectors) |
| 760 | = 2 (1441 - 2880 sectors) |
| 761 | = 4 (2881 - 5760 sectors) |
| 762 | = 8 (5760 - 11520 sectors) |
| 763 | |
| 764 | sector 0 = |
| 765 | sector 1 = 256* 0x00 |
| 766 | */ |
| 767 | /* static floperr_t ti99_sdf_format_track(floppy_image *floppy, int head, int track, option_resolution *params) |
| 768 | { |
| 769 | UINT8 *sector0 = (UINT8*)malloc(SECTOR_SIZE); |
| 770 | create_vib(sector0, params); |
| 771 | free(sector0); |
| 772 | if (true) |
| 773 | { |
| 774 | printf("not implemented\n"); |
| 775 | return FLOPPY_ERROR_SEEKERROR; |
| 776 | } |
| 777 | return FLOPPY_ERROR_SUCCESS; |
| 778 | } |
| 779 | */ |
| 780 | |
| 781 | /* |
| 782 | Get the offset from the start of the image file. In this format, logical |
| 783 | tracks on two-sided disks are arranged as follows (example for 40 |
| 784 | tracks; accordingly for 35 or 80) |
| 785 | |
| 786 | head-track |
| 787 | 0-0 |
| 788 | 0-1 |
| 789 | 0-2 |
| 790 | 0-3 |
| 791 | ... |
| 792 | 0-39 |
| 793 | 1-39 |
| 794 | 1-38 |
| 795 | 1-37 |
| 796 | ... |
| 797 | 1-0 |
| 798 | Note that we take the imgtrack, which may be half of the drive track. |
| 799 | */ |
| 800 | static floperr_t ti99_sdf_get_offset(floppy_image *floppy, int head, int imgtrack, int sector, UINT64 *offset) |
| 801 | { |
| 802 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 803 | |
| 804 | if ((head < 0) || (head >= tag->heads) |
| 805 | || (imgtrack < 0) || (imgtrack >= tag->tracks) |
| 806 | || (sector < 0) || (sector > tag->sectors)) |
| 807 | return FLOPPY_ERROR_SEEKERROR; |
| 808 | |
| 809 | if (head == 0) /* track numbers increasing towards inner track */ |
| 810 | { |
| 811 | *offset = (imgtrack * tag->sectors + sector) * SECTOR_SIZE; |
| 812 | } |
| 813 | else /* track numbers increasing towards outer track */ |
| 814 | { |
| 815 | *offset = (((2*tag->tracks)-1-imgtrack) * tag->sectors + sector) * SECTOR_SIZE; |
| 816 | } |
| 817 | return FLOPPY_ERROR_SUCCESS; |
| 818 | } |
| 819 | |
| 820 | /* |
| 821 | Read one sector at the specified position. |
| 822 | */ |
| 823 | static floperr_t ti99_sdf_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 824 | { |
| 825 | floperr_t err; |
| 826 | UINT64 offset; |
| 827 | int imgtrack = track; |
| 828 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 829 | |
| 830 | if (use_80_track_drives && tag->tracks<=40) |
| 831 | { |
| 832 | imgtrack = track / 2; |
| 833 | } |
| 834 | |
| 835 | err = ti99_sdf_get_offset(floppy, head, imgtrack, sector, &offset); |
| 836 | if (err) |
| 837 | { |
| 838 | return err; |
| 839 | } |
| 840 | floppy_image_read(floppy, buffer, offset, SECTOR_SIZE); |
| 841 | |
| 842 | return FLOPPY_ERROR_SUCCESS; |
| 843 | } |
| 844 | |
| 845 | /* |
| 846 | For the SDF format, sectors are always numbered in ascending order, |
| 847 | starting with the lowest numbered sector in each track. In other words, |
| 848 | we have nothing to calculate here. |
| 849 | */ |
| 850 | static floperr_t ti99_sdf_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 851 | { |
| 852 | return ti99_sdf_read_sector(floppy, head, track, sector, buffer, buflen); |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | Write one sector at the specified position. |
| 857 | */ |
| 858 | static floperr_t ti99_sdf_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 859 | { |
| 860 | UINT64 offset; |
| 861 | floperr_t err; |
| 862 | int imgtrack = track; |
| 863 | |
| 864 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 865 | |
| 866 | if (use_80_track_drives && tag->tracks<=40) |
| 867 | { |
| 868 | imgtrack = track / 2; |
| 869 | } |
| 870 | |
| 871 | err = ti99_sdf_get_offset(floppy, head, imgtrack, sector, &offset); |
| 872 | if (err) |
| 873 | { |
| 874 | return err; |
| 875 | } |
| 876 | floppy_image_write(floppy, buffer, offset, SECTOR_SIZE); |
| 877 | |
| 878 | return FLOPPY_ERROR_SUCCESS; |
| 879 | } |
| 880 | |
| 881 | /* |
| 882 | See above; indexing and numbering coincide. |
| 883 | */ |
| 884 | static floperr_t ti99_sdf_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 885 | { |
| 886 | return ti99_sdf_write_sector(floppy, head, track, sector, buffer, buflen, ddam); |
| 887 | } |
| 888 | |
| 889 | /* |
| 890 | Required for ReadAddress command |
| 891 | */ |
| 892 | static floperr_t ti99_sdf_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 893 | { |
| 894 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 895 | |
| 896 | if (use_80_track_drives && tag->tracks<=40) |
| 897 | { |
| 898 | track = track / 2; |
| 899 | } |
| 900 | |
| 901 | if (sector_length) |
| 902 | *sector_length=SECTOR_SIZE; |
| 903 | if (cylinder) |
| 904 | *cylinder = track; |
| 905 | if (side) |
| 906 | *side = head; |
| 907 | if (sector) |
| 908 | *sector = sector_index; |
| 909 | if (flags) |
| 910 | *flags = 0; |
| 911 | if (sector_index > tag->sectors) |
| 912 | { |
| 913 | return FLOPPY_ERROR_SEEKERROR; |
| 914 | } |
| 915 | |
| 916 | return FLOPPY_ERROR_SUCCESS; |
| 917 | } |
| 918 | |
| 919 | /* |
| 920 | Try to identify the image as an SDF format image. |
| 921 | */ |
| 922 | static FLOPPY_IDENTIFY(ti99_sdf_identify) |
| 923 | { |
| 924 | UINT64 size; |
| 925 | size = floppy_image_size(floppy); |
| 926 | *vote = ti99_sdf_guess_geometry(floppy, size, NULL); |
| 927 | logerror("SDF voting %d\n", *vote); |
| 928 | return FLOPPY_ERROR_SUCCESS; |
| 929 | } |
| 930 | |
| 931 | /* |
| 932 | Create the SDF data structures and set the callbacks. |
| 933 | */ |
| 934 | static FLOPPY_CONSTRUCT(ti99_sdf_construct) |
| 935 | { |
| 936 | struct ti99dsk_geometry geometry; |
| 937 | struct FloppyCallbacks *callbacks; |
| 938 | struct ti99dsk_tag *tag; |
| 939 | logerror("Reading image as SDF\n"); |
| 940 | |
| 941 | if (params) |
| 942 | { |
| 943 | /* create */ |
| 944 | memset(&geometry, 0, sizeof(geometry)); |
| 945 | geometry.sides = option_resolution_lookup_int(params, PARAM_HEADS); |
| 946 | geometry.tracksperside = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 947 | geometry.secspertrack = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 948 | |
| 949 | /* We don't have headers for geometry */ |
| 950 | /* check for usage in imgtool - we want to be able to create useful disks */ |
| 951 | } |
| 952 | else |
| 953 | { |
| 954 | /* load */ |
| 955 | if (ti99_sdf_guess_geometry(floppy, floppy_image_size(floppy), &geometry)<50) |
| 956 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 957 | } |
| 958 | |
| 959 | assert(geometry.sides); |
| 960 | assert(geometry.tracksperside); |
| 961 | assert(geometry.secspertrack); |
| 962 | |
| 963 | tag = (struct ti99dsk_tag *) floppy_create_tag(floppy, sizeof(struct ti99dsk_tag)); |
| 964 | |
| 965 | if (!tag) |
| 966 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 967 | |
| 968 | tag->heads = geometry.sides; |
| 969 | tag->tracks = geometry.tracksperside; |
| 970 | tag->sectors = geometry.secspertrack; |
| 971 | if (tag->sectors < 10) |
| 972 | /* FM mode */ |
| 973 | tag->track_size = 16 + tag->sectors*334 + 231; |
| 974 | else |
| 975 | tag->track_size = 40 + tag->sectors*340 + 712; |
| 976 | |
| 977 | /* set up format callbacks */ |
| 978 | callbacks = floppy_callbacks(floppy); |
| 979 | callbacks->read_sector = ti99_sdf_read_sector; |
| 980 | callbacks->write_sector = ti99_sdf_write_sector; |
| 981 | callbacks->read_indexed_sector = ti99_sdf_read_indexed_sector; |
| 982 | callbacks->write_indexed_sector = ti99_sdf_write_indexed_sector; |
| 983 | callbacks->read_track = ti99_sdf_read_track; |
| 984 | callbacks->write_track = ti99_sdf_write_track; |
| 985 | /* callbacks->format_track = ti99_sdf_format_track; */ |
| 986 | callbacks->get_track_size = ti99_get_track_size; |
| 987 | /* post format unset */ |
| 988 | callbacks->get_heads_per_disk = ti99_get_heads_per_disk; |
| 989 | callbacks->get_tracks_per_disk = ti99_get_tracks_per_disk; |
| 990 | callbacks->get_sector_length = ti99_get_sector_length; |
| 991 | callbacks->get_indexed_sector_info = ti99_sdf_get_indexed_sector_info; |
| 992 | |
| 993 | return FLOPPY_ERROR_SUCCESS; |
| 994 | } |
| 995 | |
| 996 | /* ----------------------------------------------------------------------- |
| 997 | * Track Dump Format, aka PC99 format |
| 998 | * This format includes all track data, including address marks and gaps. |
| 999 | * However, CRC is not stored; instead, a f7f7 is stored at that location. |
| 1000 | * Two track images exist, one for FM and one for MFM format. |
| 1001 | * |
| 1002 | * In this implementation, however, we do calculate the CRC. |
| 1003 | * |
| 1004 | * All tracks have exactly the same size, so offsets are calculated easily. |
| 1005 | * |
| 1006 | * This format is a precise map of the data contents of the track; |
| 1007 | * however, it does not store clock patterns. |
| 1008 | * |
| 1009 | * The TDF format does not have a special index part, so any sector on it |
| 1010 | * must be searched (as done by the disk controller) |
| 1011 | * |
| 1012 | * Interesting detail: The original TI disk controller has a bug which has |
| 1013 | * just recently been discovered (using MESS!): The lead-in has another 9 |
| 1014 | * bytes as a prefix; this is due to a wrongly set pointer in the format |
| 1015 | * routine. Strictly speaking, it produces invalid track images. |
| 1016 | * The original hardware does not care, however, so neither should we. |
| 1017 | * To keep the emulation realistic (and to allow for a |
| 1018 | * possible future extension with clock bits), we search for the |
| 1019 | * beginning of the track. From there on, however, we accept no further |
| 1020 | * deviations. This may make the resulting format unusable with the PC99 |
| 1021 | * emulator, but it only appears when formatting with the TI disk |
| 1022 | * controller anyway. If the image is created with another disk controller or |
| 1023 | * using imgtool, the format is correctly created. |
| 1024 | * |
| 1025 | * FM track image |
| 1026 | * |
| 1027 | * Lead-in: 16* 00 |
| 1028 | * Pre-ID gap: 6* 00 ---+ |
| 1029 | * IDAM: fe | |
| 1030 | * Track: tt | |
| 1031 | * Head: hh | |
| 1032 | * Sector: ss | |
| 1033 | * Size: 01 | |
| 1034 | * CRC: 2* f7 +--- repeat 9 times (8 in rare cases) |
| 1035 | * Gap2: 11* ff | |
| 1036 | * 6* 00 | |
| 1037 | * DAM: fb | |
| 1038 | * Content: 256* xx | |
| 1039 | * CRC: 2* f7 | |
| 1040 | * Gap3: 45* ff ---+ |
| 1041 | * Lead-out: 231* ff |
| 1042 | * |
| 1043 | * -------------- |
| 1044 | * |
| 1045 | * MFM track image |
| 1046 | * |
| 1047 | * Lead-in: 40* 4e |
| 1048 | * Pre-ID gap: 10* 00 ---+ |
| 1049 | * Sync: 3* a1 | |
| 1050 | * IDAM: fe | |
| 1051 | * Track: tt | |
| 1052 | * Head: hh | |
| 1053 | * Sector: ss | |
| 1054 | * Size: 01 | |
| 1055 | * CRC: 2* f7 +--- repeat 18 times (16 in rare cases) |
| 1056 | * Gap2: 22* 4e | |
| 1057 | * 12* 00 | |
| 1058 | * Sync: 3* a1 | |
| 1059 | * DAM: fb | |
| 1060 | * Content: 256* xx | |
| 1061 | * CRC: 2* f7 | |
| 1062 | * Gap3: 24* 4e ---+ |
| 1063 | * Lead-out: 712* 4e |
| 1064 | * |
| 1065 | * ------------------ |
| 1066 | * The tracks are located on the image as follows (different to SDF!): |
| 1067 | * |
| 1068 | * head-track |
| 1069 | * 0-0 |
| 1070 | * 0-1 |
| 1071 | * 0-2 |
| 1072 | * 0-3 |
| 1073 | * ... |
| 1074 | * 0-39 |
| 1075 | * 1-0 |
| 1076 | * 1-1 |
| 1077 | * 1-2 |
| 1078 | * ... |
| 1079 | * 1-39 |
| 1080 | * ----------------------------------------------------------------------- */ |
| 1081 | |
| 1082 | #define TI99_FM 1 |
| 1083 | #define TI99_MFM 2 |
| 1084 | |
| 1085 | /* For the emulation of the clock pattern, we add a 9th bit. */ |
| 1086 | #define TI99_DAM 0x1fb |
| 1087 | #define TI99_IDAM 0x1fe |
| 1088 | |
| 1089 | #define TI99_FM_DAM_OFFSET 24 |
| 1090 | #define TI99_MFM_DAM_OFFSET 44 |
| 1091 | |
| 1092 | #define TI99_FM_TRACK_ADD 247 |
| 1093 | #define TI99_MFM_TRACK_ADD 752 |
| 1094 | |
| 1095 | #define TI99_FM_TOTAL_SECTOR 334 |
| 1096 | #define TI99_MFM_TOTAL_SECTOR 340 |
| 1097 | |
| 1098 | /* |
| 1099 | Determine offset of the first IDAM. This is the base of the simulation |
| 1100 | of the clock bytes. We use a heuristic to guess where the track begins |
| 1101 | and simply check whether we can locate the first IDAM and DAM. Return |
| 1102 | value is the offset to the first IDAM. |
| 1103 | */ |
| 1104 | static floperr_t determine_offset(int format, UINT8 *track, int *offset) |
| 1105 | { |
| 1106 | floperr_t retval; |
| 1107 | int current_pos; |
| 1108 | |
| 1109 | if (format==TI99_FM) |
| 1110 | { |
| 1111 | // printf("Trying FM\n"); |
| 1112 | // We are seeking the lead-in and pre-id gap from |
| 1113 | // position 0 to position 50. This is just to make sure that |
| 1114 | // if there are bad bytes at the beginning (original TI controller) |
| 1115 | // we will skip them. |
| 1116 | current_pos = find_block(track, 0, 50, 0x00, 22); |
| 1117 | if (current_pos==TI99DSK_BLOCKNOTFOUND) |
| 1118 | { |
| 1119 | logerror("Lead-in not found\n"); |
| 1120 | retval = FLOPPY_ERROR_SEEKERROR; |
| 1121 | } |
| 1122 | else |
| 1123 | { |
| 1124 | current_pos += 22; |
| 1125 | if (track[current_pos]==0xfe /* IDAM */ && |
| 1126 | track[current_pos+4]==0x01 /* sector length, always 256 for TI */ && |
| 1127 | track[current_pos+24]==0xfb) /* DAM */ |
| 1128 | { |
| 1129 | /* We're pretty sure this is the beginning. */ |
| 1130 | *offset = current_pos; |
| 1131 | retval = FLOPPY_ERROR_SUCCESS; |
| 1132 | } |
| 1133 | else |
| 1134 | { |
| 1135 | logerror("IDAM or DAM not found\n"); |
| 1136 | retval = FLOPPY_ERROR_SEEKERROR; |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | else |
| 1141 | { |
| 1142 | /* MFM */ |
| 1143 | // printf("Trying MFM\n"); |
| 1144 | int track_start = find_block(track, 0, 100, 0x4e, 40); |
| 1145 | if (track_start==TI99DSK_BLOCKNOTFOUND) |
| 1146 | { |
| 1147 | logerror("Lead-in not found\n"); |
| 1148 | retval = FLOPPY_ERROR_SEEKERROR; |
| 1149 | } |
| 1150 | else |
| 1151 | { |
| 1152 | track_start += 40; |
| 1153 | current_pos = find_block(track, track_start, track_start+10, 0x00, 10); |
| 1154 | if (current_pos==TI99DSK_BLOCKNOTFOUND) |
| 1155 | { |
| 1156 | logerror("Pre-gap not found\n"); |
| 1157 | retval = FLOPPY_ERROR_SEEKERROR; |
| 1158 | } |
| 1159 | else |
| 1160 | { |
| 1161 | current_pos += 10; |
| 1162 | if (track[current_pos] == 0xa1 /* First sync */ && |
| 1163 | track[current_pos+3] == 0xfe /* IDAM */ && |
| 1164 | track[current_pos+44] == 0xa1 /* First sync */ && |
| 1165 | track[current_pos+47] == 0xfb) /* DAM */ |
| 1166 | { |
| 1167 | /* We're pretty sure this is the beginning. */ |
| 1168 | *offset = current_pos + 3; |
| 1169 | retval = FLOPPY_ERROR_SUCCESS; |
| 1170 | } |
| 1171 | else |
| 1172 | { |
| 1173 | logerror("Sync/IDAM/DAM not found\n"); |
| 1174 | retval = FLOPPY_ERROR_SEEKERROR; |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | /* printf("TDF offset = 0x%x\n", *offset); */ |
| 1180 | return retval; |
| 1181 | } |
| 1182 | |
| 1183 | /* |
| 1184 | Gets a byte from the track. In this format we do not have a clock |
| 1185 | signal, so we will simulate it. Thus, this function returns a 9-bit |
| 1186 | value, with the first bit set as a marker for an address mark. |
| 1187 | |
| 1188 | This may be a little more complicated than necessary, but it correctly |
| 1189 | emulates the behavior of the controller (and it allows for an |
| 1190 | alternative format which actually supports clock and data). |
| 1191 | */ |
| 1192 | static int read_byte(int format, int first_idam, UINT8 *track, int position) |
| 1193 | { |
| 1194 | int totalseclen; |
| 1195 | int dam_offset; |
| 1196 | int byte; |
| 1197 | |
| 1198 | /* Check whether we are at any IDAM or DAM in this track. */ |
| 1199 | if (format==TI99_FM) |
| 1200 | { |
| 1201 | totalseclen = TI99_FM_TOTAL_SECTOR; |
| 1202 | dam_offset = TI99_FM_DAM_OFFSET; |
| 1203 | } |
| 1204 | else |
| 1205 | { |
| 1206 | totalseclen = TI99_MFM_TOTAL_SECTOR; |
| 1207 | dam_offset = TI99_MFM_DAM_OFFSET; |
| 1208 | } |
| 1209 | |
| 1210 | int curpos = (position - first_idam) % totalseclen; |
| 1211 | if (curpos==0 && track[position]==0xfe) |
| 1212 | byte = TI99_IDAM; |
| 1213 | else |
| 1214 | { |
| 1215 | if (curpos==dam_offset && track[position]==0xfb) |
| 1216 | byte = TI99_DAM; |
| 1217 | else |
| 1218 | byte = track[position] & 0xff; |
| 1219 | } |
| 1220 | return byte; |
| 1221 | } |
| 1222 | |
| 1223 | /* |
| 1224 | Determine the position of this track in the image. |
| 1225 | */ |
| 1226 | static floperr_t ti99_tdf_get_offset(floppy_image *floppy, int head, int imgtrack, UINT64 *offset) |
| 1227 | { |
| 1228 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1229 | |
| 1230 | if ((head < 0) || (head >= tag->heads) || (imgtrack < 0) || (imgtrack >= tag->tracks)) |
| 1231 | return FLOPPY_ERROR_SEEKERROR; |
| 1232 | |
| 1233 | // For head 0, tracks increase from 0 to tracks-1; then for head 1, |
| 1234 | // tracks also increase from 0 to tracks-1. */ |
| 1235 | *offset = head * (tag->tracks * tag->track_size) + imgtrack * tag->track_size; |
| 1236 | |
| 1237 | return FLOPPY_ERROR_SUCCESS; |
| 1238 | } |
| 1239 | |
| 1240 | /* |
| 1241 | Reads a track. We just copy the contents from the format and recreate |
| 1242 | the CRC if it is the "blank" CRC as F7F7. |
| 1243 | This method assumes that the track division be done already. |
| 1244 | */ |
| 1245 | static floperr_t ti99_tdf_read_track_internal(floppy_image *floppy, int head, int imgtrack, UINT64 offset, void *buffer, size_t buflen) |
| 1246 | { |
| 1247 | floperr_t err; |
| 1248 | UINT64 track_offset; |
| 1249 | int first_idam = 0; |
| 1250 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1251 | int i, byte, crc; |
| 1252 | |
| 1253 | UINT8 *track_data = (UINT8*)buffer; |
| 1254 | |
| 1255 | err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset); |
| 1256 | if (err) |
| 1257 | return err; |
| 1258 | |
| 1259 | floppy_image_read(floppy, buffer, track_offset, buflen); |
| 1260 | |
| 1261 | // Rebuild the CRCs. PC99 did not store the CRC but put |
| 1262 | // F7F7 in its place. |
| 1263 | // The first CRC is at position IDAM + 5 |
| 1264 | // The second CRC is at position IDAM + 0x119 (FM) or +0x12d (MFM) |
| 1265 | // All this is repeated for each sector in the track |
| 1266 | |
| 1267 | if (determine_offset(tag->format, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1268 | return FLOPPY_ERROR_SEEKERROR; |
| 1269 | |
| 1270 | i = 0; |
| 1271 | while (i < buflen) |
| 1272 | { |
| 1273 | byte = read_byte(tag->format, first_idam, track_data, i); |
| 1274 | if (byte == TI99_IDAM) |
| 1275 | { |
| 1276 | // Do we have a valid CRCs already? Then this image |
| 1277 | // already contains proper CRCs handling. Do not recreate them. |
| 1278 | if (track_data[i+5] != 0xf7 || track_data[i+6] != 0xf7) |
| 1279 | return FLOPPY_ERROR_SUCCESS; |
| 1280 | |
| 1281 | crc = ccitt_crc16(0xffff, &track_data[i], 5); |
| 1282 | track_data[i+5] = (crc>>8) & 0xff; |
| 1283 | track_data[i+6] = (crc & 0xff); |
| 1284 | } |
| 1285 | else |
| 1286 | { |
| 1287 | if (byte == TI99_DAM) |
| 1288 | { |
| 1289 | crc = ccitt_crc16(0xffff, &track_data[i], SECTOR_SIZE+1); |
| 1290 | track_data[i+SECTOR_SIZE+1] = (crc>>8) & 0xff; |
| 1291 | track_data[i+SECTOR_SIZE+2] = (crc & 0xff); |
| 1292 | } |
| 1293 | } |
| 1294 | i++; |
| 1295 | } |
| 1296 | // dump_contents(track_data, buflen); |
| 1297 | return FLOPPY_ERROR_SUCCESS; |
| 1298 | } |
| 1299 | |
| 1300 | /* |
| 1301 | Reads a track. We just copy the contents from the format, without |
| 1302 | changes. |
| 1303 | */ |
| 1304 | static floperr_t ti99_tdf_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 1305 | { |
| 1306 | int imgtrack = track; |
| 1307 | |
| 1308 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1309 | if (use_80_track_drives && tag->tracks<=40) |
| 1310 | { |
| 1311 | imgtrack = track / 2; |
| 1312 | } |
| 1313 | return ti99_tdf_read_track_internal(floppy, head, imgtrack, offset, buffer, buflen); |
| 1314 | } |
| 1315 | |
| 1316 | /* |
| 1317 | Writes a track. |
| 1318 | */ |
| 1319 | static floperr_t ti99_tdf_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 1320 | { |
| 1321 | floperr_t err; |
| 1322 | UINT64 track_offset; |
| 1323 | int imgtrack = track; |
| 1324 | |
| 1325 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1326 | if (use_80_track_drives && tag->tracks<=40) |
| 1327 | { |
| 1328 | imgtrack = track / 2; |
| 1329 | } |
| 1330 | |
| 1331 | err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset); |
| 1332 | if (err) |
| 1333 | return err; |
| 1334 | |
| 1335 | // According to the TDF format, we don't need the CRC but replace it |
| 1336 | // with F7F7. For now we keep it and see whether PC99 can cope with |
| 1337 | // that. (Otherwise we would have to copy the const buffer). */ |
| 1338 | floppy_image_write(floppy, buffer, offset + track_offset, buflen); |
| 1339 | return FLOPPY_ERROR_SUCCESS; |
| 1340 | } |
| 1341 | |
| 1342 | /* |
| 1343 | static floperr_t ti99_tdf_format_track(floppy_image *floppy, int head, int track, option_resolution *params) |
| 1344 | { |
| 1345 | int sectors; |
| 1346 | int sector_length; |
| 1347 | int interleave; |
| 1348 | int first_sector_id; |
| 1349 | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 1350 | sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 1351 | interleave = option_resolution_lookup_int(params, PARAM_INTERLEAVE); |
| 1352 | first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID); |
| 1353 | return FLOPPY_ERROR_SUCCESS; |
| 1354 | } |
| 1355 | */ |
| 1356 | |
| 1357 | /* |
| 1358 | Get a sector from a track. This function takes a complete track, |
| 1359 | searches for the sector, and passes a pointer to the sector contents |
| 1360 | back. |
| 1361 | As the original TI controller creates bad bytes at the start of a track |
| 1362 | we do *not* assume a simple layout. The strategy is to search the |
| 1363 | lead-in of the track, and from this offset, step through the track |
| 1364 | in search for the sector. Since we do not have index marks (we cannot |
| 1365 | distinguish them from ordinary data, as we do not store the clock), we |
| 1366 | do not attempt to detect IDAM or DAM. |
| 1367 | */ |
| 1368 | static floperr_t ti99_tdf_seek_sector_in_track(floppy_image *floppy, int head, int track, int sector, UINT8 *track_data, UINT8 **sector_data) |
| 1369 | { |
| 1370 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1371 | |
| 1372 | int byte; |
| 1373 | int state; |
| 1374 | enum { SEARCHIDAM, SEARCHDAM }; |
| 1375 | |
| 1376 | state = SEARCHIDAM; |
| 1377 | if (tag->first_idam==0) /* should we check for every track? */ |
| 1378 | { |
| 1379 | if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1380 | { |
| 1381 | logerror("could not determine offset\n"); |
| 1382 | return FLOPPY_ERROR_SEEKERROR; |
| 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | for (int i=0; i < tag->track_size; i++) |
| 1387 | { |
| 1388 | byte = read_byte(tag->format, tag->first_idam, track_data, i); |
| 1389 | |
| 1390 | switch (state) |
| 1391 | { |
| 1392 | case SEARCHIDAM: |
| 1393 | /* search for the IDAM */ |
| 1394 | if (byte==TI99_IDAM) |
| 1395 | { |
| 1396 | if ((track_data[i+1]==track) && (track_data[i+2]==head) && (track_data[i+3]==sector)) |
| 1397 | state = SEARCHDAM; |
| 1398 | else |
| 1399 | state = SEARCHIDAM; |
| 1400 | } |
| 1401 | break; |
| 1402 | case SEARCHDAM: |
| 1403 | if (byte==TI99_DAM) |
| 1404 | { |
| 1405 | *sector_data = &track_data[i+1]; |
| 1406 | return FLOPPY_ERROR_SUCCESS; |
| 1407 | } |
| 1408 | break; |
| 1409 | } |
| 1410 | } |
| 1411 | logerror("Sector not found.\n"); |
| 1412 | return FLOPPY_ERROR_SEEKERROR; |
| 1413 | } |
| 1414 | |
| 1415 | static floperr_t ti99_tdf_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 1416 | { |
| 1417 | floperr_t err; |
| 1418 | UINT8 *sector_data; |
| 1419 | UINT8 *track_data; |
| 1420 | int imgtrack = track; |
| 1421 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1422 | |
| 1423 | track_data = (UINT8*)malloc(tag->track_size); |
| 1424 | |
| 1425 | if (use_80_track_drives && tag->tracks<=40) |
| 1426 | { |
| 1427 | imgtrack = track / 2; |
| 1428 | } |
| 1429 | |
| 1430 | err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size); |
| 1431 | if (err) |
| 1432 | return err; |
| 1433 | |
| 1434 | err = ti99_tdf_seek_sector_in_track(floppy, head, imgtrack, sector, track_data, §or_data); |
| 1435 | if (err) |
| 1436 | return err; |
| 1437 | /* verify CRC? */ |
| 1438 | |
| 1439 | memcpy(buffer, sector_data, SECTOR_SIZE); |
| 1440 | free(track_data); |
| 1441 | return FLOPPY_ERROR_SUCCESS; |
| 1442 | } |
| 1443 | |
| 1444 | /* |
| 1445 | Writes a sector into the track. Note that the indexed_write method |
| 1446 | may be used instead of this method, according to the implementation |
| 1447 | of flopimg/flopdrv. |
| 1448 | */ |
| 1449 | static floperr_t ti99_tdf_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 1450 | { |
| 1451 | floperr_t err; |
| 1452 | UINT8 *sector_data; |
| 1453 | UINT8 *track_data; |
| 1454 | UINT64 track_offset; |
| 1455 | int imgtrack = track; |
| 1456 | UINT64 offset; |
| 1457 | UINT8 crc_field[2]; |
| 1458 | int crc; |
| 1459 | |
| 1460 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1461 | |
| 1462 | track_data = (UINT8*)malloc(tag->track_size); |
| 1463 | if (use_80_track_drives && tag->tracks<=40) |
| 1464 | { |
| 1465 | imgtrack = track / 2; |
| 1466 | } |
| 1467 | |
| 1468 | err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size); |
| 1469 | if (err) |
| 1470 | return err; |
| 1471 | err = ti99_tdf_seek_sector_in_track(floppy, head, imgtrack, sector, track_data, §or_data); |
| 1472 | if (err) |
| 1473 | return err; |
| 1474 | |
| 1475 | err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset); |
| 1476 | if (err) |
| 1477 | return err; |
| 1478 | |
| 1479 | offset = track_offset + (sector_data - track_data); |
| 1480 | |
| 1481 | floppy_image_write(floppy, buffer, offset, SECTOR_SIZE); |
| 1482 | |
| 1483 | // Recalculate CRC. We init the CRC with bf84 (DAM fb is |
| 1484 | // already included; would be ffff without) |
| 1485 | crc = ccitt_crc16(0xbf84, (UINT8*)buffer, SECTOR_SIZE); |
| 1486 | crc_field[0] = (crc>>8) & 0xff; |
| 1487 | crc_field[1] = (crc & 0xff); |
| 1488 | floppy_image_write(floppy, crc_field, offset+SECTOR_SIZE, 2); |
| 1489 | |
| 1490 | return FLOPPY_ERROR_SUCCESS; |
| 1491 | } |
| 1492 | |
| 1493 | static floperr_t ti99_tdf_write_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, const void *buffer, size_t buflen, int ddam) |
| 1494 | { |
| 1495 | /* Read track, head */ |
| 1496 | int byte; |
| 1497 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1498 | UINT8 *track_data; |
| 1499 | UINT8 *sector_data; |
| 1500 | UINT64 track_offset; |
| 1501 | UINT64 offset; |
| 1502 | UINT8 crc_field[2]; |
| 1503 | |
| 1504 | int crc; |
| 1505 | int imgtrack = track; |
| 1506 | |
| 1507 | int i; |
| 1508 | floperr_t err; |
| 1509 | |
| 1510 | track_data = (UINT8*)malloc(tag->track_size); |
| 1511 | |
| 1512 | if (use_80_track_drives && tag->tracks<=40) |
| 1513 | { |
| 1514 | imgtrack = track / 2; |
| 1515 | } |
| 1516 | |
| 1517 | err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size); |
| 1518 | if (err) |
| 1519 | { |
| 1520 | free(track_data); |
| 1521 | return err; |
| 1522 | } |
| 1523 | |
| 1524 | /* Search for the sector_index-th sector. */ |
| 1525 | if (tag->first_idam==0) /* should we check for every track? */ |
| 1526 | { |
| 1527 | if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1528 | { |
| 1529 | free(track_data); |
| 1530 | return FLOPPY_ERROR_SEEKERROR; |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | /* Search for the IDAM of the n-th sector */ |
| 1535 | i=0; |
| 1536 | while (i < tag->track_size && sector_index>=0) |
| 1537 | { |
| 1538 | byte = read_byte(tag->format, tag->first_idam, track_data, i); |
| 1539 | if (byte == TI99_IDAM) |
| 1540 | { |
| 1541 | sector_index--; |
| 1542 | } |
| 1543 | i++; |
| 1544 | } |
| 1545 | i--; |
| 1546 | |
| 1547 | /* Not that many sectors in this track? */ |
| 1548 | if (sector_index>0) |
| 1549 | { |
| 1550 | free(track_data); |
| 1551 | return FLOPPY_ERROR_SEEKERROR; |
| 1552 | } |
| 1553 | |
| 1554 | /* Find the DAM. */ |
| 1555 | while (i < tag->track_size) |
| 1556 | { |
| 1557 | byte = read_byte(tag->format, tag->first_idam, track_data, i); |
| 1558 | if (byte == TI99_DAM) |
| 1559 | break; |
| 1560 | else |
| 1561 | i++; |
| 1562 | } |
| 1563 | |
| 1564 | /* There was no DAM. Image seems to be broken. */ |
| 1565 | if (i == tag->track_size) |
| 1566 | { |
| 1567 | free(track_data); |
| 1568 | return FLOPPY_ERROR_SEEKERROR; |
| 1569 | } |
| 1570 | |
| 1571 | /* Sector data is right after the DAM. */ |
| 1572 | sector_data = &track_data[i+1]; |
| 1573 | |
| 1574 | /* Get the position of the track in the image. */ |
| 1575 | err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset); |
| 1576 | if (err) |
| 1577 | return err; |
| 1578 | |
| 1579 | offset = track_offset + (sector_data - track_data); |
| 1580 | |
| 1581 | /* Write the sector data at that position. */ |
| 1582 | floppy_image_write(floppy, buffer, offset, SECTOR_SIZE); |
| 1583 | |
| 1584 | // Recalculate CRC. We init the CRC with bf84 (DAM fb is |
| 1585 | // already included; would be ffff without) |
| 1586 | crc = ccitt_crc16(0xbf84, (UINT8*)buffer, SECTOR_SIZE); |
| 1587 | crc_field[0] = (crc>>8) & 0xff; |
| 1588 | crc_field[1] = (crc & 0xff); |
| 1589 | floppy_image_write(floppy, crc_field, offset+SECTOR_SIZE, 2); |
| 1590 | |
| 1591 | /* Free the temporary buffer */ |
| 1592 | free(track_data); |
| 1593 | |
| 1594 | return FLOPPY_ERROR_SUCCESS; |
| 1595 | } |
| 1596 | |
| 1597 | |
| 1598 | static floperr_t ti99_tdf_find_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, void *buffer, unsigned long *flags) |
| 1599 | { |
| 1600 | /* Read track, head */ |
| 1601 | int byte = 0; |
| 1602 | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1603 | UINT8 *track_data; |
| 1604 | int imgtrack = track; |
| 1605 | int i; |
| 1606 | floperr_t err; |
| 1607 | floperr_t retval; |
| 1608 | track_data = (UINT8*)malloc(tag->track_size); |
| 1609 | |
| 1610 | if (use_80_track_drives && tag->tracks<=40) |
| 1611 | { |
| 1612 | imgtrack = track / 2; |
| 1613 | } |
| 1614 | |
| 1615 | err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size); |
| 1616 | if (err) |
| 1617 | { |
| 1618 | free(track_data); |
| 1619 | return err; |
| 1620 | } |
| 1621 | |
| 1622 | /* Search for the sector_index-th sector. */ |
| 1623 | |
| 1624 | if (tag->first_idam==0) /* should we check for every track? */ |
| 1625 | { |
| 1626 | if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1627 | { |
| 1628 | free(track_data); |
| 1629 | return FLOPPY_ERROR_SEEKERROR; |
| 1630 | } |
| 1631 | } |
| 1632 | |
| 1633 | i=0; |
| 1634 | sector_index = sector_index+1; |
| 1635 | while (i++ < tag->track_size && sector_index>0) |
| 1636 | { |
| 1637 | byte = read_byte(tag->format, tag->first_idam, track_data, i); |
| 1638 | if (byte == TI99_IDAM) |
| 1639 | { |
| 1640 | sector_index--; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | retval = FLOPPY_ERROR_SEEKERROR; |
| 1645 | if (sector_index==0) |
| 1646 | { |
| 1647 | /* If desired, return the ID field */ |
| 1648 | if (cylinder) |
| 1649 | { |
| 1650 | *cylinder = track_data[i]; |
| 1651 | // printf("cylinder=%d", *cylinder); |
| 1652 | |
| 1653 | } |
| 1654 | if (side) |
| 1655 | { |
| 1656 | *side = track_data[i+1]; |
| 1657 | // printf(", side=%d", *side); |
| 1658 | } |
| 1659 | if (sector) |
| 1660 | { |
| 1661 | *sector = track_data[i+2]; |
| 1662 | // printf(", sector=%d", *sector); |
| 1663 | } |
| 1664 | if (sector_length) |
| 1665 | { |
| 1666 | *sector_length = 128 << track_data[i+3]; |
| 1667 | // printf(", sector_length=%d\n", *sector_length); |
| 1668 | } |
| 1669 | if (flags) |
| 1670 | { |
| 1671 | *flags = 0; |
| 1672 | } |
| 1673 | retval = FLOPPY_ERROR_SUCCESS; |
| 1674 | |
| 1675 | /* If desired, return the sector contents. */ |
| 1676 | if (buffer) |
| 1677 | { |
| 1678 | while (i++ < tag->track_size) |
| 1679 | { |
| 1680 | byte = read_byte(tag->format, tag->first_idam, track_data, i); |
| 1681 | if (byte == TI99_DAM) |
| 1682 | break; |
| 1683 | } |
| 1684 | if (byte == TI99_DAM) |
| 1685 | { |
| 1686 | memcpy(buffer, &track_data[i+1], SECTOR_SIZE); |
| 1687 | } |
| 1688 | else |
| 1689 | { |
| 1690 | retval = FLOPPY_ERROR_SEEKERROR; |
| 1691 | } |
| 1692 | } |
| 1693 | } |
| 1694 | free(track_data); |
| 1695 | return retval; |
| 1696 | } |
| 1697 | |
| 1698 | /* |
| 1699 | Required for ReadAddress command. This function gets the 1st, 2nd, 3rd, |
| 1700 | sector (and so on) from the track, not sector 1, 2, 3. That is, the |
| 1701 | returned sector info depends on the interleave and the start sector. |
| 1702 | */ |
| 1703 | static floperr_t ti99_tdf_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 1704 | { |
| 1705 | return ti99_tdf_find_indexed_sector(floppy, head, track, sector_index, cylinder, side, sector, sector_length, NULL, flags); |
| 1706 | } |
| 1707 | |
| 1708 | static floperr_t ti99_tdf_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 1709 | { |
| 1710 | return ti99_tdf_find_indexed_sector(floppy, head, track, sector, (int*)NULL, (int*)NULL, (int*)NULL, (UINT32*)NULL, buffer, (unsigned long*)NULL); |
| 1711 | } |
| 1712 | |
| 1713 | |
| 1714 | /* |
| 1715 | Guess the geometry of the disk. |
| 1716 | 1. Check whether the disk is formatted, and if so, whether it is |
| 1717 | a FM or a MFM disk |
| 1718 | 2. If this fails, guess from the file size which format it should have |
| 1719 | |
| 1720 | SDF formats have sizes which are multiples of 10KiB. |
| 1721 | TDF: full sector size FM (MFM) = 334 (340) bytes |
| 1722 | additional track data FM (MFM) = 247 (752) bytes |
| 1723 | |
| 1724 | => check whether size is dividable by 40 (or 35) and by 2 |
| 1725 | => get track size |
| 1726 | => subtract track data |
| 1727 | => divide by 334 or 340 |
| 1728 | => number of sectors should be 8, 9, 16, 18, 32, or 36 |
| 1729 | |
| 1730 | SSDD is 274880, but DSSD is 260240, so they differ (unlike in the SDF format) |
| 1731 | Still, we cannot tell apart single-sided 80 track formats from double-sided |
| 1732 | 40 tracks. We assume the latter. Moreover, we only attempt to detect |
| 1733 | 9/18/36 sectors (no 8/16) and 40 tracks (no 35). |
| 1734 | |
| 1735 | SSSD40 = 130120 |
| 1736 | DSSD = 260240 |
| 1737 | SSDD = 274880 |
| 1738 | DSDD = 549760 |
| 1739 | DSHD = 1039360 |
| 1740 | DSDD80 = 1099520 |
| 1741 | DSHD80 = 2078720 |
| 1742 | */ |
| 1743 | static int ti99_tdf_guess_geometry(floppy_image *floppy, UINT64 size, |
| 1744 | struct ti99dsk_geometry *geometry) |
| 1745 | { |
| 1746 | int idamcnt, state, track, head, i, totalseclen = 0, format = 0, byte, tracklength, trackadd = 0; |
| 1747 | int first_idam = 0; |
| 1748 | UINT8 *track_data; |
| 1749 | /*int offset;*/ |
| 1750 | |
| 1751 | struct ti99dsk_geometry dummy_geometry; |
| 1752 | |
| 1753 | if (geometry) |
| 1754 | memset(geometry, 0, sizeof(*geometry)); |
| 1755 | else |
| 1756 | geometry = &dummy_geometry; |
| 1757 | |
| 1758 | /* Allocate enough bytes to hold the longest supported track. */ |
| 1759 | track_data = (UINT8*)malloc(13000); |
| 1760 | floppy_image_read(floppy, track_data, 0, 13000); |
| 1761 | |
| 1762 | if (determine_offset(TI99_MFM, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1763 | { |
| 1764 | /* MFM failed. */ |
| 1765 | if (determine_offset(TI99_FM, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1766 | { |
| 1767 | logerror("IDAM not found. Unformatted disk.\n"); |
| 1768 | |
| 1769 | // FM failed as well. Disk is not formatted. We assume |
| 1770 | // a format that fits into the space provided by the file. |
| 1771 | // We only give a moderate vote in this case, so SDF |
| 1772 | // make take it. |
| 1773 | |
| 1774 | free(track_data); |
| 1775 | if (size < 130120 || size > 2078720) |
| 1776 | { |
| 1777 | logerror("Unknown format size: %d\n", (int)size); |
| 1778 | return 0; |
| 1779 | } |
| 1780 | if (size >= 130120 && size < 260240) |
| 1781 | { |
| 1782 | /* SSSD */ |
| 1783 | geometry->sides = 1; |
| 1784 | geometry->tracksperside = 40; |
| 1785 | geometry->secspertrack = 9; |
| 1786 | geometry->density = 1; |
| 1787 | return 50; |
| 1788 | } |
| 1789 | if (size >= 260240 && size < 274880) |
| 1790 | { |
| 1791 | /* DSSD */ |
| 1792 | geometry->sides = 2; |
| 1793 | geometry->tracksperside = 40; |
| 1794 | geometry->secspertrack = 9; |
| 1795 | geometry->density = 1; |
| 1796 | return 50; |
| 1797 | } |
| 1798 | if (size >= 274880 && size < 549760) |
| 1799 | { |
| 1800 | /* SSDD */ |
| 1801 | geometry->sides = 1; |
| 1802 | geometry->tracksperside = 40; |
| 1803 | geometry->secspertrack = 18; |
| 1804 | geometry->density = 2; |
| 1805 | return 50; |
| 1806 | } |
| 1807 | if (size >= 549760 && size < 1039360) |
| 1808 | { |
| 1809 | /* DSDD */ |
| 1810 | geometry->sides = 2; |
| 1811 | geometry->tracksperside = 40; |
| 1812 | geometry->secspertrack = 18; |
| 1813 | geometry->density = 2; |
| 1814 | return 50; |
| 1815 | } |
| 1816 | if (size >= 1039360 && size < 1099520) |
| 1817 | { |
| 1818 | /* DSHD */ |
| 1819 | geometry->sides = 2; |
| 1820 | geometry->tracksperside = 40; |
| 1821 | geometry->secspertrack = 36; |
| 1822 | geometry->density = 2; |
| 1823 | return 50; |
| 1824 | } |
| 1825 | if (size >= 1099520 && size < 2078720) |
| 1826 | { |
| 1827 | /* DSDD80 */ |
| 1828 | geometry->sides = 2; |
| 1829 | geometry->tracksperside = 80; |
| 1830 | geometry->secspertrack = 18; |
| 1831 | geometry->density = 2; |
| 1832 | return 50; |
| 1833 | } |
| 1834 | if (size == 2078720) |
| 1835 | { |
| 1836 | /* DSHD80 */ |
| 1837 | geometry->sides = 2; |
| 1838 | geometry->tracksperside = 80; |
| 1839 | geometry->secspertrack = 36; |
| 1840 | geometry->density = 2; |
| 1841 | return 50; |
| 1842 | } |
| 1843 | } |
| 1844 | else |
| 1845 | { |
| 1846 | /* FM format */ |
| 1847 | totalseclen = TI99_FM_TOTAL_SECTOR; |
| 1848 | trackadd = TI99_FM_TRACK_ADD; /* additional track data (gaps etc.) */ |
| 1849 | geometry->density = 1; |
| 1850 | format = TI99_FM; |
| 1851 | } |
| 1852 | } |
| 1853 | else |
| 1854 | { |
| 1855 | /* MFM format */ |
| 1856 | totalseclen = TI99_MFM_TOTAL_SECTOR; |
| 1857 | trackadd = TI99_MFM_TRACK_ADD; |
| 1858 | geometry->density = 2; |
| 1859 | format = TI99_MFM; |
| 1860 | } |
| 1861 | |
| 1862 | /* Count the number of IDAMs on track 0, head 0 */ |
| 1863 | idamcnt = 0; |
| 1864 | state = 0; |
| 1865 | track = 0; |
| 1866 | head = 0; |
| 1867 | i=0; |
| 1868 | |
| 1869 | while (i++ < 13000 && head == 0 && track == 0) |
| 1870 | { |
| 1871 | byte = read_byte(format, first_idam, track_data, i); |
| 1872 | switch (state) |
| 1873 | { |
| 1874 | case 0: |
| 1875 | if (byte==TI99_IDAM) |
| 1876 | state = 1; |
| 1877 | break; |
| 1878 | case 1: |
| 1879 | track = byte; |
| 1880 | state = 2; |
| 1881 | break; |
| 1882 | case 2: |
| 1883 | head = byte; |
| 1884 | state = 3; |
| 1885 | break; |
| 1886 | case 3: |
| 1887 | idamcnt++; |
| 1888 | state = 0; |
| 1889 | } |
| 1890 | } |
| 1891 | logerror("Determined %d sectors\n", idamcnt); |
| 1892 | |
| 1893 | // Now calculate the geometry |
| 1894 | // The track size, in theory, may change due to reformatting, |
| 1895 | // and it may also vary between tracks, but the TDF does not support |
| 1896 | // varying track lengths. So every track has the same length as track 0. |
| 1897 | // Also, we don't yet know whether the drive and the disk have the |
| 1898 | // same track count. For 80 track drives and 40 track disks, we need |
| 1899 | // double-stepping; this will be known as soon as the controller is |
| 1900 | // initialized; we need to check that in the access methods. |
| 1901 | |
| 1902 | tracklength = idamcnt * totalseclen + trackadd; |
| 1903 | /*offset = 0;*/ |
| 1904 | |
| 1905 | // Read the last track. We assume that all tracks are properly |
| 1906 | // formatted; otherwise, the above calculations would already fail. |
| 1907 | // That is, the last track in the image tells us whether it is for |
| 1908 | // head 0 or for head 1. |
| 1909 | |
| 1910 | geometry->sides = 1; |
| 1911 | floppy_image_read(floppy, track_data, size-tracklength, tracklength); |
| 1912 | if (determine_offset(format, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1913 | { |
| 1914 | /* error ... what now? */ |
| 1915 | logerror("Error when reading last track. Image broken.\n"); |
| 1916 | free(track_data); |
| 1917 | return 50; |
| 1918 | } |
| 1919 | else |
| 1920 | { |
| 1921 | if (track_data[first_idam+2]==0) |
| 1922 | { |
| 1923 | geometry->sides = 1; |
| 1924 | } |
| 1925 | else |
| 1926 | { |
| 1927 | if (track_data[first_idam+2]==1) |
| 1928 | { |
| 1929 | geometry->sides = 2; |
| 1930 | } |
| 1931 | else |
| 1932 | { |
| 1933 | logerror("Error: Last track has invalid first IDAM: head = %d.\n", track_data[first_idam+2]); |
| 1934 | } |
| 1935 | |
| 1936 | } |
| 1937 | } |
| 1938 | |
| 1939 | |
| 1940 | geometry->tracksperside = (size / tracklength) / geometry->sides; |
| 1941 | geometry->secspertrack = idamcnt; |
| 1942 | |
| 1943 | if (geometry->tracksperside < 35 || geometry->tracksperside > 80) |
| 1944 | { |
| 1945 | logerror("Unsupported track count: %d\n", geometry->tracksperside); |
| 1946 | free(track_data); |
| 1947 | return 0; |
| 1948 | } |
| 1949 | |
| 1950 | free(track_data); |
| 1951 | return 100; |
| 1952 | } |
| 1953 | |
| 1954 | static FLOPPY_IDENTIFY(ti99_tdf_identify) |
| 1955 | { |
| 1956 | UINT64 size; |
| 1957 | size = floppy_image_size(floppy); |
| 1958 | *vote = ti99_tdf_guess_geometry(floppy, size, NULL); |
| 1959 | logerror("TDF voting %d\n", *vote); |
| 1960 | return FLOPPY_ERROR_SUCCESS; |
| 1961 | } |
| 1962 | |
| 1963 | static FLOPPY_CONSTRUCT(ti99_tdf_construct) |
| 1964 | { |
| 1965 | struct ti99dsk_geometry geometry; |
| 1966 | struct FloppyCallbacks *callbacks; |
| 1967 | struct ti99dsk_tag *tag; |
| 1968 | logerror("Reading image as TDF\n"); |
| 1969 | if (params) |
| 1970 | { |
| 1971 | /* create */ |
| 1972 | memset(&geometry, 0, sizeof(geometry)); |
| 1973 | /* |
| 1974 | geometry.sides = option_resolution_lookup_int(params, PARAM_HEADS); |
| 1975 | geometry.tracksperside = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 1976 | geometry.secspertrack = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 1977 | */ |
| 1978 | } |
| 1979 | else |
| 1980 | { |
| 1981 | /* load */ |
| 1982 | if (ti99_tdf_guess_geometry(floppy, floppy_image_size(floppy), &geometry)<50) |
| 1983 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1984 | } |
| 1985 | |
| 1986 | assert(geometry.sides); |
| 1987 | assert(geometry.tracksperside); |
| 1988 | assert(geometry.secspertrack); |
| 1989 | |
| 1990 | tag = (struct ti99dsk_tag *) floppy_create_tag(floppy, sizeof(struct ti99dsk_tag)); |
| 1991 | |
| 1992 | tag->heads = geometry.sides; |
| 1993 | tag->tracks = geometry.tracksperside; |
| 1994 | |
| 1995 | if (geometry.density==TI99_FM) |
| 1996 | tag->track_size = geometry.secspertrack * TI99_FM_TOTAL_SECTOR + TI99_FM_TRACK_ADD; |
| 1997 | else |
| 1998 | tag->track_size = geometry.secspertrack * TI99_MFM_TOTAL_SECTOR + TI99_MFM_TRACK_ADD; |
| 1999 | |
| 2000 | tag->format = geometry.density; |
| 2001 | tag->first_idam = 0; /* let's find out later */ |
| 2002 | tag->dam_offset = 0; |
| 2003 | |
| 2004 | /* set up format callbacks */ |
| 2005 | callbacks = floppy_callbacks(floppy); |
| 2006 | callbacks->read_sector = ti99_tdf_read_sector; |
| 2007 | callbacks->write_sector = ti99_tdf_write_sector; |
| 2008 | callbacks->read_indexed_sector = ti99_tdf_read_indexed_sector; |
| 2009 | callbacks->write_indexed_sector = ti99_tdf_write_indexed_sector; |
| 2010 | callbacks->read_track = ti99_tdf_read_track; |
| 2011 | callbacks->write_track = ti99_tdf_write_track; |
| 2012 | /* callbacks->format_track = ti99_tdf_format_track; */ |
| 2013 | callbacks->get_track_size = ti99_get_track_size; |
| 2014 | /* post format unset */ |
| 2015 | callbacks->get_heads_per_disk = ti99_get_heads_per_disk; |
| 2016 | callbacks->get_tracks_per_disk = ti99_get_tracks_per_disk; |
| 2017 | callbacks->get_sector_length = ti99_get_sector_length; |
| 2018 | callbacks->get_indexed_sector_info = ti99_tdf_get_indexed_sector_info; |
| 2019 | |
| 2020 | return FLOPPY_ERROR_SUCCESS; |
| 2021 | } |
| 2022 | |
| 2023 | /* ----------------------------------------------------------------------- */ |
| 2024 | |
| 2025 | FLOPPY_OPTIONS_START( ti99 ) |
| 2026 | FLOPPY_OPTION( ti99_sdf, "dsk", "TI99 sector dump (v9t9)", ti99_sdf_identify, ti99_sdf_construct, NULL, |
| 2027 | HEADS([1]-2) |
| 2028 | TRACKS(35-[40]-80) |
| 2029 | SECTORS(8/9/16/[18]/36) |
| 2030 | SECTOR_LENGTH([256]) |
| 2031 | FIRST_SECTOR_ID(0)) |
| 2032 | FLOPPY_OPTION( ti99_tdf, "dsk,dtk", "TI99 track dump (pc99)", ti99_tdf_identify, ti99_tdf_construct, NULL, |
| 2033 | TRACKS(35-[40]-80) |
| 2034 | SECTORS(8/9/16/[18]/36) |
| 2035 | SECTOR_LENGTH([256]) |
| 2036 | FIRST_SECTOR_ID(0)) |
| 2037 | FLOPPY_OPTIONS_END |
src/lib/formats/ap2_dsk.c
| r0 | r11458 | |
| 1 | /********************************************************************* |
| 2 | |
| 3 | ap2_dsk.c |
| 4 | |
| 5 | Apple II disk images |
| 6 | |
| 7 | *********************************************************************/ |
| 8 | |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include <assert.h> |
| 12 | |
| 13 | #include "ap2_dsk.h" |
| 14 | #include "basicdsk.h" |
| 15 | |
| 16 | |
| 17 | #define APPLE2_IMAGE_DO 0 |
| 18 | #define APPLE2_IMAGE_PO 1 |
| 19 | #define APPLE2_IMAGE_NIB 2 |
| 20 | |
| 21 | |
| 22 | /* used in for all Apple II images */ |
| 23 | static UINT32 apple2_get_track_size(floppy_image *floppy, int head, int track); |
| 24 | static int disk_decode_nib(UINT8 *data, const UINT8 *nibble, int *volume, int *track, int *sector); |
| 25 | static void disk_encode_nib(UINT8 *nibble, const UINT8 *data, int volume, int track, int sector); |
| 26 | |
| 27 | /* used in DOS/ProDOS order images */ |
| 28 | static int apple2_do_translate_sector(floppy_image *floppy, int sector); |
| 29 | static int apple2_po_translate_sector(floppy_image *floppy, int sector); |
| 30 | static floperr_t apple2_dsk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen); |
| 31 | static floperr_t apple2_dsk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen); |
| 32 | |
| 33 | /* used in nibble order images */ |
| 34 | static floperr_t apple2_nib_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen); |
| 35 | static floperr_t apple2_nib_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen); |
| 36 | static floperr_t apple2_nib_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen); |
| 37 | static floperr_t apple2_nib_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam); |
| 38 | static floperr_t apple2_nib_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length); |
| 39 | |
| 40 | |
| 41 | static const UINT8 translate6[0x40] = |
| 42 | { |
| 43 | 0x96, 0x97, 0x9a, 0x9b, 0x9d, 0x9e, 0x9f, 0xa6, |
| 44 | 0xa7, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb2, 0xb3, |
| 45 | 0xb4, 0xb5, 0xb6, 0xb7, 0xb9, 0xba, 0xbb, 0xbc, |
| 46 | 0xbd, 0xbe, 0xbf, 0xcb, 0xcd, 0xce, 0xcf, 0xd3, |
| 47 | 0xd6, 0xd7, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, |
| 48 | 0xdf, 0xe5, 0xe6, 0xe7, 0xe9, 0xea, 0xeb, 0xec, |
| 49 | 0xed, 0xee, 0xef, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, |
| 50 | 0xf7, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | |
| 55 | /* ----------------------------------------------------------------------- |
| 56 | * Utility code |
| 57 | * ----------------------------------------------------------------------- */ |
| 58 | |
| 59 | static const UINT8 *get_untranslate6_map(void) |
| 60 | { |
| 61 | static UINT8 map[256]; |
| 62 | static int map_inited = 0; |
| 63 | UINT8 i; |
| 64 | |
| 65 | if (!map_inited) |
| 66 | { |
| 67 | memset(map, 0xff, sizeof(map)); |
| 68 | for (i = 0; i < ARRAY_LENGTH(translate6); i++) |
| 69 | map[translate6[i]] = i; |
| 70 | map_inited = 1; |
| 71 | } |
| 72 | return map; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | |
| 77 | /* ----------------------------------------------------------------------- |
| 78 | * Core constructor |
| 79 | * ----------------------------------------------------------------------- */ |
| 80 | |
| 81 | |
| 82 | static floperr_t apple2_general_construct(floppy_image *floppy, int floppy_type) |
| 83 | { |
| 84 | floperr_t err; |
| 85 | struct basicdsk_geometry geometry; |
| 86 | struct FloppyCallbacks *format; |
| 87 | |
| 88 | format = floppy_callbacks(floppy); |
| 89 | |
| 90 | switch(floppy_type) { |
| 91 | case APPLE2_IMAGE_DO: |
| 92 | case APPLE2_IMAGE_PO: |
| 93 | memset(&geometry, 0, sizeof(geometry)); |
| 94 | geometry.heads = 1; |
| 95 | geometry.tracks = APPLE2_TRACK_COUNT; |
| 96 | geometry.sectors = APPLE2_SECTOR_COUNT; |
| 97 | geometry.sector_length = APPLE2_SECTOR_SIZE; |
| 98 | geometry.translate_sector = (floppy_type == APPLE2_IMAGE_DO) ? apple2_do_translate_sector : apple2_po_translate_sector; |
| 99 | |
| 100 | err = basicdsk_construct(floppy, &geometry); |
| 101 | if (err) |
| 102 | return err; |
| 103 | |
| 104 | format->read_track = apple2_dsk_read_track; |
| 105 | format->write_track = apple2_dsk_write_track; |
| 106 | break; |
| 107 | |
| 108 | case APPLE2_IMAGE_NIB: |
| 109 | format->read_track = apple2_nib_read_track; |
| 110 | format->write_track = apple2_nib_write_track; |
| 111 | format->read_sector = apple2_nib_read_sector; |
| 112 | format->write_sector = apple2_nib_write_sector; |
| 113 | format->get_sector_length = apple2_nib_get_sector_length; |
| 114 | break; |
| 115 | |
| 116 | default: |
| 117 | assert(0); |
| 118 | return FLOPPY_ERROR_INTERNAL; |
| 119 | } |
| 120 | |
| 121 | format->get_track_size = apple2_get_track_size; |
| 122 | |
| 123 | return FLOPPY_ERROR_SUCCESS; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | |
| 128 | /* ----------------------------------------------------------------------- |
| 129 | * DOS order and ProDOS order code |
| 130 | * ----------------------------------------------------------------------- */ |
| 131 | |
| 132 | static FLOPPY_IDENTIFY(apple2_dsk_identify) |
| 133 | { |
| 134 | UINT64 size; |
| 135 | UINT64 expected_size; |
| 136 | |
| 137 | size = floppy_image_size(floppy); |
| 138 | expected_size = APPLE2_TRACK_COUNT * APPLE2_SECTOR_COUNT * APPLE2_SECTOR_SIZE; |
| 139 | |
| 140 | if (size == expected_size) |
| 141 | *vote = 100; |
| 142 | else if (size - expected_size < 8 && size - expected_size > -8) |
| 143 | *vote = 90; /* tolerate images with up to eight fewer/extra bytes (bug #638) */ |
| 144 | else |
| 145 | *vote = 0; |
| 146 | return FLOPPY_ERROR_SUCCESS; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | |
| 151 | static int apple2_do_translate_sector(floppy_image *floppy, int sector) |
| 152 | { |
| 153 | static const UINT8 skewing[] = |
| 154 | { |
| 155 | /* DOS order (*.do) */ |
| 156 | 0x00, 0x07, 0x0E, 0x06, 0x0D, 0x05, 0x0C, 0x04, |
| 157 | 0x0B, 0x03, 0x0A, 0x02, 0x09, 0x01, 0x08, 0x0F |
| 158 | }; |
| 159 | return skewing[sector]; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | static int apple2_po_translate_sector(floppy_image *floppy, int sector) |
| 165 | { |
| 166 | static const UINT8 skewing[] = |
| 167 | { |
| 168 | /* ProDOS order (*.po) */ |
| 169 | 0x00, 0x08, 0x01, 0x09, 0x02, 0x0A, 0x03, 0x0B, |
| 170 | 0x04, 0x0C, 0x05, 0x0D, 0x06, 0x0E, 0x07, 0x0F |
| 171 | }; |
| 172 | return skewing[sector]; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
| 177 | static FLOPPY_CONSTRUCT(apple2_do_construct) |
| 178 | { |
| 179 | return apple2_general_construct(floppy, APPLE2_IMAGE_DO); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | |
| 184 | static FLOPPY_CONSTRUCT(apple2_po_construct) |
| 185 | { |
| 186 | return apple2_general_construct(floppy, APPLE2_IMAGE_PO); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | static floperr_t apple2_dsk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 192 | { |
| 193 | UINT8 sector_buffer[APPLE2_SECTOR_SIZE]; |
| 194 | int sector; |
| 195 | UINT8 *nibble; |
| 196 | |
| 197 | if (buflen < APPLE2_NIBBLE_SIZE*APPLE2_SECTOR_COUNT) |
| 198 | return FLOPPY_ERROR_INTERNAL; |
| 199 | if (offset != 0) |
| 200 | return FLOPPY_ERROR_UNSUPPORTED; |
| 201 | |
| 202 | memset(buffer, 0, buflen); |
| 203 | |
| 204 | for (sector = 0; sector < APPLE2_SECTOR_COUNT; sector++) |
| 205 | { |
| 206 | nibble = (UINT8 *)buffer; |
| 207 | nibble += sector * APPLE2_SMALL_NIBBLE_SIZE; |
| 208 | |
| 209 | floppy_read_sector(floppy, head, track, sector, 0, sector_buffer, sizeof(sector_buffer)); |
| 210 | disk_encode_nib(nibble, sector_buffer, 254, track, sector); |
| 211 | } |
| 212 | return FLOPPY_ERROR_SUCCESS; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | |
| 217 | static floperr_t apple2_dsk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 218 | { |
| 219 | int sector; |
| 220 | UINT8 sector_buffer[APPLE2_SECTOR_SIZE]; |
| 221 | const UINT8 *nibble; |
| 222 | |
| 223 | if (offset != 0) |
| 224 | return FLOPPY_ERROR_UNSUPPORTED; |
| 225 | |
| 226 | for (sector = 0; sector < APPLE2_SECTOR_COUNT; sector++) |
| 227 | { |
| 228 | nibble = (UINT8 *)buffer; |
| 229 | nibble += sector * APPLE2_SMALL_NIBBLE_SIZE; |
| 230 | |
| 231 | disk_decode_nib(sector_buffer, nibble, NULL, NULL, NULL); |
| 232 | floppy_write_sector(floppy, head, track, sector, 0, sector_buffer, sizeof(sector_buffer), 0); |
| 233 | } |
| 234 | |
| 235 | return FLOPPY_ERROR_SUCCESS; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | |
| 240 | /* ----------------------------------------------------------------------- |
| 241 | * Nibble order code |
| 242 | * ----------------------------------------------------------------------- */ |
| 243 | |
| 244 | static FLOPPY_IDENTIFY(apple2_nib_identify) |
| 245 | { |
| 246 | UINT64 size; |
| 247 | size = floppy_image_size(floppy); |
| 248 | *vote = (size == APPLE2_TRACK_COUNT * APPLE2_SECTOR_COUNT * APPLE2_NIBBLE_SIZE) ? 100 : 0; |
| 249 | return FLOPPY_ERROR_SUCCESS; |
| 250 | } |
| 251 | |
| 252 | |
| 253 | |
| 254 | static FLOPPY_CONSTRUCT(apple2_nib_construct) |
| 255 | { |
| 256 | return apple2_general_construct(floppy, APPLE2_IMAGE_NIB); |
| 257 | } |
| 258 | |
| 259 | |
| 260 | |
| 261 | static floperr_t apple2_nib_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 262 | { |
| 263 | if ((head != 0) || (track < 0) || (track >= APPLE2_TRACK_COUNT)) |
| 264 | return FLOPPY_ERROR_SEEKERROR; |
| 265 | if (offset != 0) |
| 266 | return FLOPPY_ERROR_UNSUPPORTED; |
| 267 | floppy_image_read(floppy, buffer, track * APPLE2_NIBBLE_SIZE * APPLE2_SECTOR_COUNT, buflen); |
| 268 | return FLOPPY_ERROR_SUCCESS; |
| 269 | } |
| 270 | |
| 271 | |
| 272 | |
| 273 | static floperr_t apple2_nib_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 274 | { |
| 275 | if ((head != 0) || (track < 0) || (track >= APPLE2_TRACK_COUNT)) |
| 276 | return FLOPPY_ERROR_SEEKERROR; |
| 277 | if (offset != 0) |
| 278 | return FLOPPY_ERROR_UNSUPPORTED; |
| 279 | floppy_image_write(floppy, buffer, track * APPLE2_NIBBLE_SIZE * APPLE2_SECTOR_COUNT, buflen); |
| 280 | return FLOPPY_ERROR_SUCCESS; |
| 281 | } |
| 282 | |
| 283 | |
| 284 | |
| 285 | /* ----------------------------------------------------------------------- |
| 286 | * Track conversion |
| 287 | * ----------------------------------------------------------------------- */ |
| 288 | |
| 289 | static int decode_nibbyte(UINT8 *nibint, const UINT8 *nibdata) |
| 290 | { |
| 291 | if ((nibdata[0] & 0xAA) != 0xAA) |
| 292 | return 1; |
| 293 | if ((nibdata[1] & 0xAA) != 0xAA) |
| 294 | return 1; |
| 295 | |
| 296 | *nibint = (nibdata[0] & ~0xAA) << 1; |
| 297 | *nibint |= (nibdata[1] & ~0xAA) << 0; |
| 298 | return 0; |
| 299 | } |
| 300 | |
| 301 | |
| 302 | |
| 303 | static int disk_decode_nib(UINT8 *data, const UINT8 *nibble, int *volume, int *track, int *sector) |
| 304 | { |
| 305 | UINT8 read_volume; |
| 306 | UINT8 read_track; |
| 307 | UINT8 read_sector; |
| 308 | UINT8 read_checksum; |
| 309 | int i; |
| 310 | UINT8 b, xorvalue, newvalue; |
| 311 | |
| 312 | const UINT8 *untranslate6 = get_untranslate6_map(); |
| 313 | |
| 314 | /* pick apart the volume/track/sector info and checksum */ |
| 315 | if (decode_nibbyte(&read_volume, &nibble[10])) |
| 316 | return 1; |
| 317 | if (decode_nibbyte(&read_track, &nibble[12])) |
| 318 | return 1; |
| 319 | if (decode_nibbyte(&read_sector, &nibble[14])) |
| 320 | return 1; |
| 321 | if (decode_nibbyte(&read_checksum, &nibble[16])) |
| 322 | return 1; |
| 323 | if (read_checksum != (read_volume ^ read_track ^ read_sector)) |
| 324 | return 1; |
| 325 | |
| 326 | /* decode the nibble core */ |
| 327 | xorvalue = 0; |
| 328 | for (i = 0; i < 342; i++) |
| 329 | { |
| 330 | b = untranslate6[nibble[i+28]]; |
| 331 | if (b == 0xff) |
| 332 | return 1; |
| 333 | newvalue = b ^ xorvalue; |
| 334 | |
| 335 | if (i >= 0x56) |
| 336 | { |
| 337 | /* 6 bit */ |
| 338 | data[i - 0x56] |= (newvalue << 2); |
| 339 | } |
| 340 | else |
| 341 | { |
| 342 | /* 3 * 2 bit */ |
| 343 | data[i + 0x00] = ((newvalue >> 1) & 0x01) | ((newvalue << 1) & 0x02); |
| 344 | data[i + 0x56] = ((newvalue >> 3) & 0x01) | ((newvalue >> 1) & 0x02); |
| 345 | if (i + 0xAC < APPLE2_SECTOR_SIZE) |
| 346 | data[i + 0xAC] = ((newvalue >> 5) & 0x01) | ((newvalue >> 3) & 0x02); |
| 347 | } |
| 348 | xorvalue = newvalue; |
| 349 | } |
| 350 | |
| 351 | /* success; write out values if pointers not null */ |
| 352 | if (volume) |
| 353 | *volume = read_volume; |
| 354 | if (track) |
| 355 | *track = read_track; |
| 356 | if (sector) |
| 357 | *sector = read_sector; |
| 358 | return 0; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | |
| 363 | static floperr_t apple2_nib_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 364 | { |
| 365 | floperr_t err; |
| 366 | const UINT8 *nibble; |
| 367 | UINT8 *track_data; |
| 368 | void *track_data_v; |
| 369 | |
| 370 | if ((sector < 0) || (sector >= APPLE2_SECTOR_COUNT)) |
| 371 | return FLOPPY_ERROR_SEEKERROR; |
| 372 | if (buflen != APPLE2_SECTOR_SIZE) |
| 373 | return FLOPPY_ERROR_INTERNAL; |
| 374 | |
| 375 | err = floppy_load_track(floppy, head, track, FALSE, &track_data_v, NULL); |
| 376 | if (err) |
| 377 | return err; |
| 378 | track_data = (UINT8 *) track_data_v; |
| 379 | |
| 380 | nibble = track_data + (sector * APPLE2_NIBBLE_SIZE); |
| 381 | |
| 382 | if (disk_decode_nib((UINT8 *)buffer, nibble, NULL, NULL, NULL)) |
| 383 | return FLOPPY_ERROR_INVALIDIMAGE; |
| 384 | |
| 385 | return FLOPPY_ERROR_SUCCESS; |
| 386 | } |
| 387 | |
| 388 | |
| 389 | |
| 390 | static void disk_encode_nib(UINT8 *nibble, const UINT8 *data, int volume, int track, int sector) |
| 391 | { |
| 392 | int checksum, oldvalue, xorvalue, i; |
| 393 | |
| 394 | /* setup header values */ |
| 395 | checksum = volume ^ track ^ sector; |
| 396 | |
| 397 | memset(nibble, 0xFF, APPLE2_NIBBLE_SIZE); |
| 398 | nibble[ 7] = 0xD5; |
| 399 | nibble[ 8] = 0xAA; |
| 400 | nibble[ 9] = 0x96; |
| 401 | nibble[10] = (volume >> 1) | 0xAA; |
| 402 | nibble[11] = volume | 0xAA; |
| 403 | nibble[12] = (track >> 1) | 0xAA; |
| 404 | nibble[13] = track | 0xAA; |
| 405 | nibble[14] = (sector >> 1) | 0xAA; |
| 406 | nibble[15] = sector | 0xAA; |
| 407 | nibble[16] = (checksum >> 1) | 0xAA; |
| 408 | nibble[17] = (checksum) | 0xAA; |
| 409 | nibble[18] = 0xDE; |
| 410 | nibble[19] = 0xAA; |
| 411 | nibble[20] = 0xEB; |
| 412 | nibble[25] = 0xD5; |
| 413 | nibble[26] = 0xAA; |
| 414 | nibble[27] = 0xAD; |
| 415 | nibble[27+344] = 0xDE; |
| 416 | nibble[27+345] = 0xAA; |
| 417 | nibble[27+346] = 0xEB; |
| 418 | xorvalue = 0; |
| 419 | |
| 420 | for (i = 0; i < 342; i++) |
| 421 | { |
| 422 | if (i >= 0x56) |
| 423 | { |
| 424 | /* 6 bit */ |
| 425 | oldvalue = data[i - 0x56]; |
| 426 | oldvalue = oldvalue >> 2; |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | /* 3 * 2 bit */ |
| 431 | oldvalue = 0; |
| 432 | oldvalue |= (data[i + 0x00] & 0x01) << 1; |
| 433 | oldvalue |= (data[i + 0x00] & 0x02) >> 1; |
| 434 | oldvalue |= (data[i + 0x56] & 0x01) << 3; |
| 435 | oldvalue |= (data[i + 0x56] & 0x02) << 1; |
| 436 | if (i + 0xAC < APPLE2_SECTOR_SIZE) |
| 437 | { |
| 438 | oldvalue |= (data[i + 0xAC] & 0x01) << 5; |
| 439 | oldvalue |= (data[i + 0xAC] & 0x02) << 3; |
| 440 | } |
| 441 | |
| 442 | } |
| 443 | xorvalue ^= oldvalue; |
| 444 | nibble[28+i] = translate6[xorvalue & 0x3F]; |
| 445 | xorvalue = oldvalue; |
| 446 | } |
| 447 | |
| 448 | nibble[27+343] = translate6[xorvalue & 0x3F]; |
| 449 | } |
| 450 | |
| 451 | |
| 452 | |
| 453 | static floperr_t apple2_nib_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 454 | { |
| 455 | floperr_t err; |
| 456 | UINT8 *track_data; |
| 457 | void *track_data_v; |
| 458 | |
| 459 | if ((sector < 0) || (sector >= APPLE2_SECTOR_COUNT)) |
| 460 | return FLOPPY_ERROR_SEEKERROR; |
| 461 | if (buflen != APPLE2_SECTOR_SIZE) |
| 462 | return FLOPPY_ERROR_INTERNAL; |
| 463 | |
| 464 | err = floppy_load_track(floppy, head, track, TRUE, &track_data_v, NULL); |
| 465 | if (err) |
| 466 | return err; |
| 467 | track_data = (UINT8 *) track_data_v; |
| 468 | |
| 469 | disk_encode_nib(track_data + sector * APPLE2_NIBBLE_SIZE, (const UINT8 *)buffer, 254, track, sector); |
| 470 | return FLOPPY_ERROR_SUCCESS; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | |
| 475 | static floperr_t apple2_nib_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 476 | { |
| 477 | *sector_length = APPLE2_SECTOR_SIZE; |
| 478 | return FLOPPY_ERROR_SUCCESS; |
| 479 | } |
| 480 | |
| 481 | |
| 482 | |
| 483 | static UINT32 apple2_get_track_size(floppy_image *floppy, int head, int track) |
| 484 | { |
| 485 | return APPLE2_NIBBLE_SIZE * APPLE2_SECTOR_COUNT; |
| 486 | } |
| 487 | |
| 488 | |
| 489 | |
| 490 | /* ----------------------------------------------------------------------- */ |
| 491 | |
| 492 | FLOPPY_OPTIONS_START( apple2 ) |
| 493 | FLOPPY_OPTION( apple2_do, "do,dsk,bin", "Apple ][ DOS order disk image", apple2_dsk_identify, apple2_do_construct, NULL, |
| 494 | HEADS([1]) |
| 495 | TRACKS([35]) |
| 496 | SECTORS([16]) |
| 497 | SECTOR_LENGTH([256]) |
| 498 | FIRST_SECTOR_ID([0])) |
| 499 | FLOPPY_OPTION( apple2_po, "po,dsk,bin", "Apple ][ ProDOS order disk image", apple2_dsk_identify, apple2_po_construct, NULL, |
| 500 | HEADS([1]) |
| 501 | TRACKS([35]) |
| 502 | SECTORS([16]) |
| 503 | SECTOR_LENGTH([256]) |
| 504 | FIRST_SECTOR_ID([0])) |
| 505 | FLOPPY_OPTION( apple2_nib, "dsk,nib", "Apple ][ Nibble order disk image", apple2_nib_identify, apple2_nib_construct, NULL, |
| 506 | HEADS([1]) |
| 507 | TRACKS([35]) |
| 508 | SECTORS([16]) |
| 509 | SECTOR_LENGTH([256]) |
| 510 | FIRST_SECTOR_ID([0])) |
| 511 | FLOPPY_OPTIONS_END |
src/lib/formats/td0_dsk.c
| r0 | r11458 | |
| 1 | /********************************************************************* |
| 2 | |
| 3 | formats/td0_dsk.c |
| 4 | |
| 5 | TD0 disk images |
| 6 | |
| 7 | *********************************************************************/ |
| 8 | /* |
| 9 | * Based on Japanese version 29-NOV-1988 |
| 10 | * LZSS coded by Haruhiko OKUMURA |
| 11 | * Adaptive Huffman Coding coded by Haruyasu YOSHIZAKI |
| 12 | * Edited and translated to English by Kenji RIKITAKE |
| 13 | */ |
| 14 | |
| 15 | #include <string.h> |
| 16 | #include "flopimg.h" |
| 17 | |
| 18 | #define BUFSZ 512 // new input buffer |
| 19 | |
| 20 | /* LZSS Parameters */ |
| 21 | |
| 22 | #define N 4096 /* Size of string buffer */ |
| 23 | #define F 60 /* Size of look-ahead buffer */ |
| 24 | #define THRESHOLD 2 |
| 25 | #define NIL N /* End of tree's node */ |
| 26 | |
| 27 | |
| 28 | /* Huffman coding parameters */ |
| 29 | |
| 30 | #define N_CHAR (256 - THRESHOLD + F) |
| 31 | /* character code (= 0..N_CHAR-1) */ |
| 32 | #define T (N_CHAR * 2 - 1) /* Size of table */ |
| 33 | #define R (T - 1) /* root position */ |
| 34 | #define MAX_FREQ 0x8000 |
| 35 | /* update when cumulative frequency */ |
| 36 | /* reaches to this value */ |
| 37 | |
| 38 | struct td0dsk_tag |
| 39 | { |
| 40 | int heads; |
| 41 | int tracks; |
| 42 | int sector_size; |
| 43 | UINT64 track_offsets[84*2]; /* offset within data for each track */ |
| 44 | UINT8 *data; |
| 45 | }; |
| 46 | |
| 47 | struct tdlzhuf { |
| 48 | UINT16 r, |
| 49 | bufcnt,bufndx,bufpos, // string buffer |
| 50 | // the following to allow block reads from input in next_word() |
| 51 | ibufcnt,ibufndx; // input buffer counters |
| 52 | UINT8 inbuf[BUFSZ]; // input buffer |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | struct td0dsk_t |
| 57 | { |
| 58 | floppy_image *floppy_file; |
| 59 | UINT64 floppy_file_offset; |
| 60 | |
| 61 | struct tdlzhuf tdctl; |
| 62 | UINT8 text_buf[N + F - 1]; |
| 63 | UINT16 freq[T + 1]; /* cumulative freq table */ |
| 64 | |
| 65 | /* |
| 66 | * pointing parent nodes. |
| 67 | * area [T..(T + N_CHAR - 1)] are pointers for leaves |
| 68 | */ |
| 69 | INT16 prnt[T + N_CHAR]; |
| 70 | |
| 71 | /* pointing children nodes (son[], son[] + 1)*/ |
| 72 | INT16 son[T]; |
| 73 | |
| 74 | UINT16 getbuf; |
| 75 | UINT8 getlen; |
| 76 | |
| 77 | int data_read(UINT8 *buf, UINT16 size); |
| 78 | int next_word(); |
| 79 | int GetBit(); |
| 80 | int GetByte(); |
| 81 | void StartHuff(); |
| 82 | void reconst(); |
| 83 | void update(int c); |
| 84 | INT16 DecodeChar(); |
| 85 | INT16 DecodePosition(); |
| 86 | void init_Decode(); |
| 87 | int Decode(UINT8 *buf, int len); |
| 88 | }; |
| 89 | |
| 90 | static td0dsk_t td0dsk; |
| 91 | |
| 92 | |
| 93 | static struct td0dsk_tag *get_tag(floppy_image *floppy) |
| 94 | { |
| 95 | struct td0dsk_tag *tag; |
| 96 | tag = (td0dsk_tag *)floppy_tag(floppy); |
| 97 | return tag; |
| 98 | } |
| 99 | |
| 100 | |
| 101 | |
| 102 | FLOPPY_IDENTIFY( td0_dsk_identify ) |
| 103 | { |
| 104 | UINT8 header[2]; |
| 105 | |
| 106 | floppy_image_read(floppy, header, 0, 2); |
| 107 | if (header[0]=='T' && header[1]=='D') { |
| 108 | *vote = 100; |
| 109 | } else if (header[0]=='t' && header[1]=='d') { |
| 110 | *vote = 100; |
| 111 | } else { |
| 112 | *vote = 0; |
| 113 | } |
| 114 | return FLOPPY_ERROR_SUCCESS; |
| 115 | } |
| 116 | |
| 117 | static int td0_get_heads_per_disk(floppy_image *floppy) |
| 118 | { |
| 119 | return get_tag(floppy)->heads; |
| 120 | } |
| 121 | |
| 122 | static int td0_get_tracks_per_disk(floppy_image *floppy) |
| 123 | { |
| 124 | return get_tag(floppy)->tracks; |
| 125 | } |
| 126 | |
| 127 | static UINT64 td0_get_track_offset(floppy_image *floppy, int head, int track) |
| 128 | { |
| 129 | return get_tag(floppy)->track_offsets[(track<<1) + head]; |
| 130 | } |
| 131 | |
| 132 | static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset) |
| 133 | { |
| 134 | UINT64 offs; |
| 135 | UINT8 *header; |
| 136 | UINT8 sectors_per_track; |
| 137 | int i; |
| 138 | |
| 139 | if ((head < 0) || (head >= get_tag(floppy)->heads) || (track < 0) || (track >= get_tag(floppy)->tracks) |
| 140 | || (sector < 0) ) |
| 141 | return FLOPPY_ERROR_SEEKERROR; |
| 142 | |
| 143 | // position on begining of track data |
| 144 | offs = td0_get_track_offset(floppy, head, track); |
| 145 | |
| 146 | // read track header |
| 147 | header = get_tag(floppy)->data + offs - 4; |
| 148 | |
| 149 | // take number of sectors per track |
| 150 | sectors_per_track = header[0]; |
| 151 | |
| 152 | if (!sector_is_index) { |
| 153 | // when taking ID's return seek error if number is over counter |
| 154 | if (sector > sectors_per_track) { |
| 155 | return FLOPPY_ERROR_SEEKERROR; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // move trought sectors |
| 160 | for(i=0;i < sector-1;i++) { |
| 161 | header = get_tag(floppy)->data + offs; |
| 162 | offs+= 6; |
| 163 | if ((header[4] & 0x30)==0) { |
| 164 | offs+= 2; |
| 165 | offs+= header[6] + (header[7]<<8); |
| 166 | } |
| 167 | } |
| 168 | // read size of sector |
| 169 | header = get_tag(floppy)->data + offs; |
| 170 | get_tag(floppy)->sector_size = 1 << (header[3] + 7); |
| 171 | |
| 172 | if (offset) |
| 173 | *offset = offs; |
| 174 | return FLOPPY_ERROR_SUCCESS; |
| 175 | } |
| 176 | |
| 177 | |
| 178 | |
| 179 | static floperr_t internal_td0_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen) |
| 180 | { |
| 181 | UINT64 offset; |
| 182 | floperr_t err; |
| 183 | UINT8 *header; |
| 184 | int size,realsize,i; |
| 185 | int buff_pos; |
| 186 | int data_pos; |
| 187 | UINT8 *data; |
| 188 | UINT8 *buf; |
| 189 | |
| 190 | buf = (UINT8*)buffer; |
| 191 | // take sector offset |
| 192 | err = get_offset(floppy, head, track, sector, sector_is_index, &offset); |
| 193 | if (err) |
| 194 | return err; |
| 195 | |
| 196 | // read sector header |
| 197 | header = get_tag(floppy)->data + offset; |
| 198 | offset+=6; |
| 199 | // if there is no date just jump out |
| 200 | if ((header[4] & 0x30)!=0) return FLOPPY_ERROR_SUCCESS; |
| 201 | |
| 202 | offset+=3; |
| 203 | // take data size |
| 204 | size = header[6] + (header[7]<<8)-1; |
| 205 | // take real sector size |
| 206 | realsize = 1 << (header[3] + 7); |
| 207 | |
| 208 | // read sector data |
| 209 | data = get_tag(floppy)->data + offset; |
| 210 | buff_pos = 0; |
| 211 | data_pos = 0; |
| 212 | |
| 213 | switch(header[8]) { |
| 214 | case 0: |
| 215 | // encoding type 0 |
| 216 | // - plain data |
| 217 | memcpy(buffer,data,size); |
| 218 | break; |
| 219 | case 1: |
| 220 | // encoding type 1 |
| 221 | // - 2 bytes size |
| 222 | // - 2 bytes of data |
| 223 | // data is reapeted specified number of times |
| 224 | while(buff_pos<realsize) { |
| 225 | for (i=0;i<data[data_pos]+(data[data_pos+1] << 8);i++) { |
| 226 | buf[buff_pos] = data[data_pos+2];buff_pos++; |
| 227 | buf[buff_pos] = data[data_pos+3];buff_pos++; |
| 228 | } |
| 229 | data_pos+=4; |
| 230 | } |
| 231 | break; |
| 232 | case 2: |
| 233 | // encoding type 2 |
| 234 | // - if first byte is zero next byte represent size of |
| 235 | // plain data after it |
| 236 | // - if different then zero when multiply by 2 represent |
| 237 | // size of data that should be reapeted next byte times |
| 238 | while(buff_pos<realsize) { |
| 239 | if (data[data_pos]==0x00) { |
| 240 | int size_ = data[data_pos+1]; |
| 241 | memcpy(buf+buff_pos,data + data_pos + 2,size_); |
| 242 | data_pos += 2 + size_; |
| 243 | buff_pos += size_; |
| 244 | } else { |
| 245 | int size_ = 2*data[data_pos]; |
| 246 | int repeat = data[data_pos+1]; |
| 247 | data_pos+=2; |
| 248 | |
| 249 | for (i=0;i<repeat;i++) { |
| 250 | memcpy(buf + buff_pos,data + data_pos,size_); |
| 251 | buff_pos += size_; |
| 252 | } |
| 253 | data_pos += size_; |
| 254 | } |
| 255 | } |
| 256 | break; |
| 257 | default: |
| 258 | return FLOPPY_ERROR_INTERNAL; |
| 259 | } |
| 260 | return FLOPPY_ERROR_SUCCESS; |
| 261 | } |
| 262 | |
| 263 | |
| 264 | static floperr_t td0_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 265 | { |
| 266 | return internal_td0_read_sector(floppy, head, track, sector, FALSE, buffer, buflen); |
| 267 | } |
| 268 | |
| 269 | static floperr_t td0_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 270 | { |
| 271 | return internal_td0_read_sector(floppy, head, track, sector, TRUE, buffer, buflen); |
| 272 | } |
| 273 | |
| 274 | static floperr_t td0_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 275 | { |
| 276 | floperr_t err; |
| 277 | err = get_offset(floppy, head, track, sector, FALSE, NULL); |
| 278 | if (err) |
| 279 | return err; |
| 280 | |
| 281 | if (sector_length) { |
| 282 | *sector_length = get_tag(floppy)->sector_size; |
| 283 | } |
| 284 | return FLOPPY_ERROR_SUCCESS; |
| 285 | } |
| 286 | |
| 287 | static floperr_t td0_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 288 | { |
| 289 | floperr_t retVal; |
| 290 | UINT64 offset; |
| 291 | UINT8 *sector_info; |
| 292 | |
| 293 | retVal = get_offset(floppy, head, track, sector_index, FALSE, &offset); |
| 294 | sector_info = get_tag(floppy)->data + offset; |
| 295 | if (cylinder) |
| 296 | *cylinder = sector_info[0]; |
| 297 | if (side) |
| 298 | *side = sector_info[1]; |
| 299 | if (sector) |
| 300 | *sector = sector_info[2]; |
| 301 | if (sector_length) { |
| 302 | *sector_length = 1 << (sector_info[3] + 7); |
| 303 | } |
| 304 | if (flags) { |
| 305 | *flags = 0; |
| 306 | if (sector_info[4] & 0x02) *flags |= ID_FLAG_CRC_ERROR_IN_DATA_FIELD; |
| 307 | if (sector_info[4] & 0x04) *flags |= ID_FLAG_DELETED_DATA; |
| 308 | } |
| 309 | |
| 310 | return retVal; |
| 311 | } |
| 312 | |
| 313 | int td0dsk_t::data_read(UINT8 *buf, UINT16 size) |
| 314 | { |
| 315 | if (floppy_file_offset + size > floppy_image_size(floppy_file) ) { |
| 316 | size = floppy_image_size(floppy_file) - floppy_file_offset; |
| 317 | } |
| 318 | floppy_image_read(floppy_file,buf,floppy_file_offset,size); |
| 319 | floppy_file_offset += size; |
| 320 | return size; |
| 321 | } |
| 322 | |
| 323 | |
| 324 | /* |
| 325 | * Tables for encoding/decoding upper 6 bits of |
| 326 | * sliding dictionary pointer |
| 327 | */ |
| 328 | |
| 329 | /* decoder table */ |
| 330 | static const UINT8 d_code[256] = { |
| 331 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 332 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 333 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 334 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 335 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, |
| 336 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, |
| 337 | 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, |
| 338 | 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, |
| 339 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 340 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 341 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 342 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 343 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 344 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 345 | 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, |
| 346 | 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, |
| 347 | 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, |
| 348 | 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, |
| 349 | 0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D, |
| 350 | 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F, |
| 351 | 0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11, |
| 352 | 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13, |
| 353 | 0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15, |
| 354 | 0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17, |
| 355 | 0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B, |
| 356 | 0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F, |
| 357 | 0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23, |
| 358 | 0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27, |
| 359 | 0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B, |
| 360 | 0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F, |
| 361 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 362 | 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, |
| 363 | }; |
| 364 | |
| 365 | static const UINT8 d_len[256] = { |
| 366 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 367 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 368 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 369 | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 370 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 371 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 372 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 373 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 374 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 375 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 376 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 377 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 378 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 379 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 380 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 381 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 382 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 383 | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 384 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 385 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 386 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 387 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 388 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 389 | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 390 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 391 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 392 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 393 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 394 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 395 | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 396 | 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, |
| 397 | 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, |
| 398 | }; |
| 399 | |
| 400 | int td0dsk_t::next_word() |
| 401 | { |
| 402 | if(tdctl.ibufndx >= tdctl.ibufcnt) |
| 403 | { |
| 404 | tdctl.ibufndx = 0; |
| 405 | tdctl.ibufcnt = data_read(tdctl.inbuf,BUFSZ); |
| 406 | if(tdctl.ibufcnt <= 0) |
| 407 | return(-1); |
| 408 | } |
| 409 | while (getlen <= 8) { // typically reads a word at a time |
| 410 | getbuf |= tdctl.inbuf[tdctl.ibufndx++] << (8 - getlen); |
| 411 | getlen += 8; |
| 412 | } |
| 413 | return(0); |
| 414 | } |
| 415 | |
| 416 | |
| 417 | int td0dsk_t::GetBit() /* get one bit */ |
| 418 | { |
| 419 | INT16 i; |
| 420 | if(next_word() < 0) |
| 421 | return(-1); |
| 422 | i = getbuf; |
| 423 | getbuf <<= 1; |
| 424 | getlen--; |
| 425 | if(i < 0) |
| 426 | return(1); |
| 427 | else |
| 428 | return(0); |
| 429 | } |
| 430 | |
| 431 | int td0dsk_t::GetByte() /* get a byte */ |
| 432 | { |
| 433 | UINT16 i; |
| 434 | if(next_word() != 0) |
| 435 | return(-1); |
| 436 | i = getbuf; |
| 437 | getbuf <<= 8; |
| 438 | getlen -= 8; |
| 439 | i = i >> 8; |
| 440 | return((int) i); |
| 441 | } |
| 442 | |
| 443 | |
| 444 | |
| 445 | /* initialize freq tree */ |
| 446 | |
| 447 | void td0dsk_t::StartHuff() |
| 448 | { |
| 449 | int i, j; |
| 450 | |
| 451 | for (i = 0; i < N_CHAR; i++) { |
| 452 | freq[i] = 1; |
| 453 | son[i] = i + T; |
| 454 | prnt[i + T] = i; |
| 455 | } |
| 456 | i = 0; j = N_CHAR; |
| 457 | while (j <= R) { |
| 458 | freq[j] = freq[i] + freq[i + 1]; |
| 459 | son[j] = i; |
| 460 | prnt[i] = prnt[i + 1] = j; |
| 461 | i += 2; j++; |
| 462 | } |
| 463 | freq[T] = 0xffff; |
| 464 | prnt[R] = 0; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | /* reconstruct freq tree */ |
| 469 | |
| 470 | void td0dsk_t::reconst() |
| 471 | { |
| 472 | INT16 i, j, k; |
| 473 | UINT16 f, l; |
| 474 | |
| 475 | /* halven cumulative freq for leaf nodes */ |
| 476 | j = 0; |
| 477 | for (i = 0; i < T; i++) { |
| 478 | if (son[i] >= T) { |
| 479 | freq[j] = (freq[i] + 1) / 2; |
| 480 | son[j] = son[i]; |
| 481 | j++; |
| 482 | } |
| 483 | } |
| 484 | /* make a tree : first, connect children nodes */ |
| 485 | for (i = 0, j = N_CHAR; j < T; i += 2, j++) { |
| 486 | k = i + 1; |
| 487 | f = freq[j] = freq[i] + freq[k]; |
| 488 | for (k = j - 1; f < freq[k]; k--); |
| 489 | k++; |
| 490 | l = (j - k) * 2; |
| 491 | |
| 492 | /* movmem() is Turbo-C dependent |
| 493 | rewritten to memmove() by Kenji */ |
| 494 | |
| 495 | /* movmem(&freq[k], &freq[k + 1], l); */ |
| 496 | (void)memmove(&freq[k + 1], &freq[k], l); |
| 497 | freq[k] = f; |
| 498 | /* movmem(&son[k], &son[k + 1], l); */ |
| 499 | (void)memmove(&son[k + 1], &son[k], l); |
| 500 | son[k] = i; |
| 501 | } |
| 502 | /* connect parent nodes */ |
| 503 | for (i = 0; i < T; i++) { |
| 504 | if ((k = son[i]) >= T) { |
| 505 | prnt[k] = i; |
| 506 | } else { |
| 507 | prnt[k] = prnt[k + 1] = i; |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | |
| 513 | /* update freq tree */ |
| 514 | |
| 515 | void td0dsk_t::update(int c) |
| 516 | { |
| 517 | int i, j, k, l; |
| 518 | |
| 519 | if (freq[R] == MAX_FREQ) { |
| 520 | reconst(); |
| 521 | } |
| 522 | c = prnt[c + T]; |
| 523 | do { |
| 524 | k = ++freq[c]; |
| 525 | |
| 526 | /* swap nodes to keep the tree freq-ordered */ |
| 527 | if (k > freq[l = c + 1]) { |
| 528 | while (k > freq[++l]); |
| 529 | l--; |
| 530 | freq[c] = freq[l]; |
| 531 | freq[l] = k; |
| 532 | |
| 533 | i = son[c]; |
| 534 | prnt[i] = l; |
| 535 | if (i < T) prnt[i + 1] = l; |
| 536 | |
| 537 | j = son[l]; |
| 538 | son[l] = i; |
| 539 | |
| 540 | prnt[j] = c; |
| 541 | if (j < T) prnt[j + 1] = c; |
| 542 | son[c] = j; |
| 543 | |
| 544 | c = l; |
| 545 | } |
| 546 | } while ((c = prnt[c]) != 0); /* do it until reaching the root */ |
| 547 | } |
| 548 | |
| 549 | |
| 550 | INT16 td0dsk_t::DecodeChar() |
| 551 | { |
| 552 | int ret; |
| 553 | UINT16 c; |
| 554 | |
| 555 | c = son[R]; |
| 556 | |
| 557 | /* |
| 558 | * start searching tree from the root to leaves. |
| 559 | * choose node #(son[]) if input bit == 0 |
| 560 | * else choose #(son[]+1) (input bit == 1) |
| 561 | */ |
| 562 | while (c < T) { |
| 563 | if((ret = GetBit()) < 0) |
| 564 | return(-1); |
| 565 | c += (unsigned) ret; |
| 566 | c = son[c]; |
| 567 | } |
| 568 | c -= T; |
| 569 | update(c); |
| 570 | return c; |
| 571 | } |
| 572 | |
| 573 | INT16 td0dsk_t::DecodePosition() |
| 574 | { |
| 575 | INT16 bit; |
| 576 | UINT16 i, j, c; |
| 577 | |
| 578 | /* decode upper 6 bits from given table */ |
| 579 | if((bit=GetByte()) < 0) |
| 580 | return(-1); |
| 581 | i = (UINT16) bit; |
| 582 | c = (UINT16)d_code[i] << 6; |
| 583 | j = d_len[i]; |
| 584 | |
| 585 | /* input lower 6 bits directly */ |
| 586 | j -= 2; |
| 587 | while (j--) { |
| 588 | if((bit = GetBit()) < 0) |
| 589 | return(-1); |
| 590 | i = (i << 1) + bit; |
| 591 | } |
| 592 | return(c | (i & 0x3f)); |
| 593 | } |
| 594 | |
| 595 | /* DeCompression |
| 596 | |
| 597 | split out initialization code to init_Decode() |
| 598 | |
| 599 | */ |
| 600 | |
| 601 | void td0dsk_t::init_Decode() |
| 602 | { |
| 603 | int i; |
| 604 | getbuf = 0; |
| 605 | getlen = 0; |
| 606 | tdctl.ibufcnt= tdctl.ibufndx = 0; // input buffer is empty |
| 607 | tdctl.bufcnt = 0; |
| 608 | StartHuff(); |
| 609 | for (i = 0; i < N - F; i++) |
| 610 | text_buf[i] = ' '; |
| 611 | tdctl.r = N - F; |
| 612 | } |
| 613 | |
| 614 | |
| 615 | int td0dsk_t::Decode(UINT8 *buf, int len) /* Decoding/Uncompressing */ |
| 616 | { |
| 617 | INT16 c,pos; |
| 618 | int count; // was an unsigned long, seems unnecessary |
| 619 | for (count = 0; count < len; ) { |
| 620 | if(tdctl.bufcnt == 0) { |
| 621 | if((c = DecodeChar()) < 0) |
| 622 | return(count); // fatal error |
| 623 | if (c < 256) { |
| 624 | *(buf++) = c; |
| 625 | text_buf[tdctl.r++] = c; |
| 626 | tdctl.r &= (N - 1); |
| 627 | count++; |
| 628 | } |
| 629 | else { |
| 630 | if((pos = DecodePosition()) < 0) |
| 631 | return(count); // fatal error |
| 632 | tdctl.bufpos = (tdctl.r - pos - 1) & (N - 1); |
| 633 | tdctl.bufcnt = c - 255 + THRESHOLD; |
| 634 | tdctl.bufndx = 0; |
| 635 | } |
| 636 | } |
| 637 | else { // still chars from last string |
| 638 | while( tdctl.bufndx < tdctl.bufcnt && count < len ) { |
| 639 | c = text_buf[(tdctl.bufpos + tdctl.bufndx) & (N - 1)]; |
| 640 | *(buf++) = c; |
| 641 | tdctl.bufndx++; |
| 642 | text_buf[tdctl.r++] = c; |
| 643 | tdctl.r &= (N - 1); |
| 644 | count++; |
| 645 | } |
| 646 | // reset bufcnt after copy string from text_buf[] |
| 647 | if(tdctl.bufndx >= tdctl.bufcnt) |
| 648 | tdctl.bufndx = tdctl.bufcnt = 0; |
| 649 | } |
| 650 | } |
| 651 | return(count); // count == len, success |
| 652 | } |
| 653 | |
| 654 | |
| 655 | FLOPPY_CONSTRUCT( td0_dsk_construct ) |
| 656 | { |
| 657 | td0dsk_t *state = &td0dsk; |
| 658 | struct FloppyCallbacks *callbacks; |
| 659 | struct td0dsk_tag *tag; |
| 660 | UINT8 *header; |
| 661 | int number_of_sectors; |
| 662 | int position; |
| 663 | int i; |
| 664 | int track; |
| 665 | |
| 666 | if(params) |
| 667 | { |
| 668 | // create |
| 669 | return FLOPPY_ERROR_UNSUPPORTED; |
| 670 | } |
| 671 | |
| 672 | tag = (struct td0dsk_tag *) floppy_create_tag(floppy, sizeof(struct td0dsk_tag)); |
| 673 | if (!tag) |
| 674 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 675 | |
| 676 | tag->data = (UINT8*)malloc(floppy_image_size(floppy)); |
| 677 | if (tag->data==NULL) { |
| 678 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 679 | } |
| 680 | floppy_image_read(floppy, tag->data, 0, floppy_image_size(floppy)); |
| 681 | header = tag->data; |
| 682 | |
| 683 | if (header[0]=='t') { |
| 684 | UINT8 obuf[BUFSZ]; |
| 685 | int rd; |
| 686 | int off = 12; |
| 687 | int size = 0; |
| 688 | state->floppy_file = floppy; |
| 689 | state->init_Decode(); |
| 690 | state->floppy_file_offset = 12; |
| 691 | do |
| 692 | { |
| 693 | if((rd = state->Decode(obuf, BUFSZ)) > 0) size += rd; |
| 694 | } while(rd == BUFSZ); |
| 695 | memcpy(obuf,tag->data,12); |
| 696 | free(tag->data); |
| 697 | tag->data = (UINT8*)malloc(size+12); |
| 698 | if (tag->data==NULL) { |
| 699 | return FLOPPY_ERROR_OUTOFMEMORY; |
| 700 | } |
| 701 | memcpy(tag->data,obuf,12); |
| 702 | state->floppy_file_offset = 12; |
| 703 | state->init_Decode(); |
| 704 | do |
| 705 | { |
| 706 | if((rd = state->Decode(obuf, BUFSZ)) > 0) { |
| 707 | memcpy(tag->data+off,obuf,rd); |
| 708 | off += rd; |
| 709 | } |
| 710 | } while(rd == BUFSZ); |
| 711 | } |
| 712 | header = tag->data; |
| 713 | tag->heads = header[9]; |
| 714 | if (tag->heads > 1) { |
| 715 | tag->heads = 2; |
| 716 | } |
| 717 | |
| 718 | // header len + comment header + comment len |
| 719 | position = 12; |
| 720 | if (header[7] & 0x80) { |
| 721 | position += 10 + header[14] + (header[15]<<8); |
| 722 | } |
| 723 | tag->tracks = 0; |
| 724 | do { |
| 725 | // read track header |
| 726 | header = tag->data + position; |
| 727 | track = header[1]; |
| 728 | number_of_sectors = header[0]; |
| 729 | if (number_of_sectors!=0xff){ |
| 730 | position+=4; |
| 731 | tag->track_offsets[(track<<1) + (header[2] & 1)] = position; |
| 732 | for(i=0;i<number_of_sectors;i++) { |
| 733 | // read sector header |
| 734 | header = tag->data + position; |
| 735 | position+=6; |
| 736 | // read sector size |
| 737 | if ((header[4] & 0x30)==0) { |
| 738 | // if there is sector data |
| 739 | header = tag->data + position; |
| 740 | position+=2; |
| 741 | // skip sector data |
| 742 | position+= header[0] + (header[1]<<8); |
| 743 | } |
| 744 | } |
| 745 | tag->tracks++; |
| 746 | } |
| 747 | } while(number_of_sectors!=0xff); |
| 748 | tag->tracks++; |
| 749 | |
| 750 | callbacks = floppy_callbacks(floppy); |
| 751 | callbacks->read_sector = td0_read_sector; |
| 752 | callbacks->read_indexed_sector = td0_read_indexed_sector; |
| 753 | callbacks->get_sector_length = td0_get_sector_length; |
| 754 | callbacks->get_heads_per_disk = td0_get_heads_per_disk; |
| 755 | callbacks->get_tracks_per_disk = td0_get_tracks_per_disk; |
| 756 | callbacks->get_indexed_sector_info = td0_get_indexed_sector_info; |
| 757 | return FLOPPY_ERROR_SUCCESS; |
| 758 | } |
| 759 | |
| 760 | FLOPPY_DESTRUCT( td0_dsk_destruct ) |
| 761 | { |
| 762 | struct td0dsk_tag *tag = get_tag(floppy); |
| 763 | free(tag->data); |
| 764 | tag->data = NULL; |
| 765 | return FLOPPY_ERROR_SUCCESS; |
| 766 | } |
src/lib/formats/coco_cas.c
| r0 | r11458 | |
| 1 | /************************************************************************** |
| 2 | |
| 3 | coco_cas.c |
| 4 | |
| 5 | Format code for CoCo CAS (*.cas) files |
| 6 | |
| 7 | This is the actual code that processes the CAS data. CAS files are a |
| 8 | problem because they are a legacy of previous CoCo emulators, and most |
| 9 | of these emulators patch the system as a shortcut. Thus, the ones and |
| 10 | zeroes in the file do not truly represent modulated pulses in the |
| 11 | waveform, but what the BIOS reads and writes. |
| 12 | |
| 13 | In doing so, they make the CoCo more tolerant of short headers and lack |
| 14 | of delays between blocks. This legacy is reflected in most CAS files in |
| 15 | use, and thus presents a problem for a pure hardware emulation like MESS. |
| 16 | |
| 17 | One alternative is to preprocess the data on the CAS, file by file, but |
| 18 | this proves to be problematic because in the process, legitimate data |
| 19 | that is unrecognized by the preprocessor may get dropped. |
| 20 | |
| 21 | The approach taken here is a hybrid approach - it retrieves the data |
| 22 | block by block until an error occurs; be it the end of the CAS or a |
| 23 | corrupt (?!) block. When "legitimate" blocks are done processing, the |
| 24 | remainder of the data is added to the waveform in a traditional manner. |
| 25 | The result has proven to work quite well. |
| 26 | |
| 27 | 2005-May-04, P.Harvey-Smith, improved handling of some "copy protected" |
| 28 | Dragon games that have odd blocks in odd places, this has slightly |
| 29 | increased loading time but does mean games like "Rommel's Revenge" will |
| 30 | now load. |
| 31 | |
| 32 | **************************************************************************/ |
| 33 | |
| 34 | #include "coco_cas.h" |
| 35 | |
| 36 | #define COCO_WAVESAMPLES_HEADER (1.0) |
| 37 | #define COCO_WAVESAMPLES_TRAILER (1.0) |
| 38 | #define COCO_LONGSILENCE (5.0) |
| 39 | |
| 40 | //some games load with only 5s, but most games need 15s |
| 41 | #define ALICE32_WAVESAMPLES_HEADER (15.0) |
| 42 | |
| 43 | // PAIR is an endian-safe union useful for representing 32-bit CPU registers |
| 44 | union PAIR |
| 45 | { |
| 46 | #ifdef LSB_FIRST |
| 47 | struct { UINT8 l,h,h2,h3; } b; |
| 48 | struct { UINT16 l,h; } w; |
| 49 | struct { INT8 l,h,h2,h3; } sb; |
| 50 | struct { INT16 l,h; } sw; |
| 51 | #else |
| 52 | struct { UINT8 h3,h2,h,l; } b; |
| 53 | struct { INT8 h3,h2,h,l; } sb; |
| 54 | struct { UINT16 h,l; } w; |
| 55 | struct { INT16 h,l; } sw; |
| 56 | #endif |
| 57 | UINT32 d; |
| 58 | INT32 sd; |
| 59 | }; |
| 60 | |
| 61 | static int synccount; |
| 62 | |
| 63 | const struct CassetteModulation coco_cas_modulation = |
| 64 | { |
| 65 | CASSETTE_MODULATION_SINEWAVE, |
| 66 | 600.0, 1200.0, 1500.0, |
| 67 | 1500.0, 2400.0, 3000.0 |
| 68 | }; |
| 69 | |
| 70 | |
| 71 | |
| 72 | static casserr_t coco_cas_identify(cassette_image *cassette, struct CassetteOptions *opts) |
| 73 | { |
| 74 | return cassette_modulation_identify(cassette, &coco_cas_modulation, opts); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | |
| 79 | static int get_cas_block(cassette_image *cassette, UINT64 *offset, UINT8 *block, int *block_len) |
| 80 | { |
| 81 | UINT8 block_length = 0; |
| 82 | UINT8 block_checksum = 0; |
| 83 | UINT64 current_offset; |
| 84 | UINT64 image_size; |
| 85 | PAIR p; |
| 86 | int i; |
| 87 | int state = 0; |
| 88 | int phase = 0; |
| 89 | |
| 90 | synccount = 0; |
| 91 | p.w.l = 0; |
| 92 | image_size = cassette_image_size(cassette); |
| 93 | current_offset = *offset; |
| 94 | |
| 95 | while(current_offset < image_size) |
| 96 | { |
| 97 | cassette_image_read(cassette, &p.b.h, current_offset, 1); |
| 98 | current_offset++; |
| 99 | |
| 100 | for (i = 0; i < 8; i++) |
| 101 | { |
| 102 | p.w.l >>= 1; |
| 103 | |
| 104 | if (state == 0) |
| 105 | { |
| 106 | /* searching for a block */ |
| 107 | if (p.b.l == 0x3C) |
| 108 | { |
| 109 | /* found one! */ |
| 110 | phase = i; |
| 111 | state++; |
| 112 | } |
| 113 | else if (p.b.l == 0x55) |
| 114 | { |
| 115 | synccount++; |
| 116 | } |
| 117 | } |
| 118 | else if (i == phase) |
| 119 | { |
| 120 | *(block++) = p.b.l; |
| 121 | switch(state) { |
| 122 | case 1: |
| 123 | /* found file type */ |
| 124 | block_checksum = p.b.l; |
| 125 | state++; |
| 126 | break; |
| 127 | case 2: |
| 128 | /* found file size */ |
| 129 | block_length = p.b.l; |
| 130 | *block_len = ((int) block_length) + 3; |
| 131 | block_checksum += p.b.l; |
| 132 | state++; |
| 133 | break; |
| 134 | |
| 135 | case 3: |
| 136 | /* data byte */ |
| 137 | if (block_length) |
| 138 | { |
| 139 | block_length--; |
| 140 | block_checksum += p.b.l; |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | /* end of block */ |
| 145 | if (p.b.l != block_checksum) |
| 146 | { |
| 147 | /* checksum failure */ |
| 148 | return FALSE; |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | /* checksum success */ |
| 153 | *offset = current_offset; |
| 154 | return TRUE; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /* no more blocks */ |
| 163 | return FALSE; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | |
| 168 | static casserr_t cas_load(cassette_image *cassette, UINT8 silence) |
| 169 | { |
| 170 | casserr_t err; |
| 171 | UINT64 offset; |
| 172 | UINT64 image_size; |
| 173 | UINT8 block[258]; /* 255 bytes per block + 3 (type, length, checksum) */ |
| 174 | int block_length = 0; |
| 175 | UINT8 last_blocktype; |
| 176 | double time_index = 0.0; |
| 177 | double time_displacement; |
| 178 | static const UINT8 magic_bytes[2] = { 0x55, 0x3C }; |
| 179 | |
| 180 | #if 0 |
| 181 | { |
| 182 | static const UINT8 dummy_bytes[] = |
| 183 | { |
| 184 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 185 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 186 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 187 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 188 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 189 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 190 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 191 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 192 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 193 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 194 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 195 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 196 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 197 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 198 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 199 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 200 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 201 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 202 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 203 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 204 | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 205 | 0x3C, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A |
| 206 | }; |
| 207 | time_index = 10.0; |
| 208 | return cassette_put_modulated_data(cassette, 0, time_index, dummy_bytes, sizeof(dummy_bytes), &coco_cas_modulation, &time_displacement); |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | err = cassette_put_sample(cassette, 0, time_index, COCO_WAVESAMPLES_HEADER, 0); |
| 213 | if (err) |
| 214 | return err; |
| 215 | time_index += COCO_WAVESAMPLES_HEADER; |
| 216 | |
| 217 | offset = 0; |
| 218 | last_blocktype = 0; |
| 219 | image_size = cassette_image_size(cassette); |
| 220 | |
| 221 | /* try to find a block that we can untangle */ |
| 222 | while(get_cas_block(cassette, &offset, block, &block_length)) |
| 223 | { |
| 224 | /* Forcing a silence before a filename block, improves the ability to load some */ |
| 225 | /* copy protected Dragon games, e.g. Rommel's Revenge */ |
| 226 | /* was the last block a filename block? */ |
| 227 | if ((last_blocktype == 0) || (last_blocktype == 0xFF) || (block[0] == 0)) |
| 228 | { |
| 229 | /* silence */ |
| 230 | err = cassette_put_sample(cassette, 0, time_index, silence, 0); |
| 231 | if (err) |
| 232 | return err; |
| 233 | time_index += silence; |
| 234 | |
| 235 | /* sync data */ |
| 236 | err = cassette_put_modulated_filler(cassette, 0, time_index, 0x55, 128, &coco_cas_modulation, &time_displacement); |
| 237 | if (err) |
| 238 | return err; |
| 239 | time_index += time_displacement; |
| 240 | } |
| 241 | else if (synccount != 0) /* If we have multiple sync bytes in cas file, make sure they */ |
| 242 | { /* are passed through */ |
| 243 | /* sync data */ |
| 244 | err = cassette_put_modulated_filler(cassette, 0, time_index, 0x55, synccount, &coco_cas_modulation, &time_displacement); |
| 245 | if (err) |
| 246 | return err; |
| 247 | time_index += time_displacement; |
| 248 | } |
| 249 | |
| 250 | /* now fill in the magic bytes */ |
| 251 | err = cassette_put_modulated_data(cassette, 0, time_index, magic_bytes, sizeof(magic_bytes), &coco_cas_modulation, &time_displacement); |
| 252 | if (err) |
| 253 | return err; |
| 254 | time_index += time_displacement; |
| 255 | |
| 256 | /* now fill in the block */ |
| 257 | err = cassette_put_modulated_data(cassette, 0, time_index, block, block_length, &coco_cas_modulation, &time_displacement); |
| 258 | if (err) |
| 259 | return err; |
| 260 | time_index += time_displacement; |
| 261 | |
| 262 | /* and the last magic byte */ |
| 263 | err = cassette_put_modulated_filler(cassette, 0, time_index, 0x55, 1, &coco_cas_modulation, &time_displacement); |
| 264 | if (err) |
| 265 | return err; |
| 266 | time_index += time_displacement; |
| 267 | |
| 268 | last_blocktype = block[0]; |
| 269 | } |
| 270 | |
| 271 | /* all futher data is undecipherable, so output it verbatim */ |
| 272 | err = cassette_read_modulated_data(cassette, 0, time_index, offset, image_size - offset, &coco_cas_modulation, &time_displacement); |
| 273 | if (err) |
| 274 | return err; |
| 275 | time_index += time_displacement; |
| 276 | |
| 277 | return CASSETTE_ERROR_SUCCESS; |
| 278 | } |
| 279 | |
| 280 | static casserr_t coco_cas_load(cassette_image *cassette) |
| 281 | { |
| 282 | return cas_load(cassette, COCO_WAVESAMPLES_HEADER); |
| 283 | } |
| 284 | |
| 285 | static casserr_t alice32_cas_load(cassette_image *cassette) |
| 286 | { |
| 287 | return cas_load(cassette, ALICE32_WAVESAMPLES_HEADER); |
| 288 | } |
| 289 | |
| 290 | const struct CassetteFormat coco_cas_format = |
| 291 | { |
| 292 | "cas", |
| 293 | coco_cas_identify, |
| 294 | coco_cas_load, |
| 295 | NULL |
| 296 | }; |
| 297 | |
| 298 | const struct CassetteFormat alice32_cas_format = |
| 299 | { |
| 300 | "cas,c10,k7", |
| 301 | coco_cas_identify, |
| 302 | alice32_cas_load, |
| 303 | NULL |
| 304 | }; |
| 305 | |
| 306 | |
| 307 | CASSETTE_FORMATLIST_START(coco_cassette_formats) |
| 308 | CASSETTE_FORMAT(coco_cas_format) |
| 309 | CASSETTE_FORMATLIST_END |
| 310 | |
| 311 | CASSETTE_FORMATLIST_START(alice32_cassette_formats) |
| 312 | CASSETTE_FORMAT(alice32_cas_format) |
| 313 | CASSETTE_FORMATLIST_END |
src/lib/formats/thom_cas.c
| r0 | r11458 | |
| 1 | /********************************************************************** |
| 2 | |
| 3 | Copyright (C) Antoine Mine' 2006 |
| 4 | |
| 5 | Thomson 8-bit computers |
| 6 | |
| 7 | **********************************************************************/ |
| 8 | |
| 9 | #include <math.h> |
| 10 | |
| 11 | #include "pool.h" |
| 12 | #include "cassimg.h" |
| 13 | #include "thom_cas.h" |
| 14 | |
| 15 | |
| 16 | /***************************** configuration **************************/ |
| 17 | |
| 18 | #define K7_SPEED_HACK 0 |
| 19 | /* When set to 1, bits extracted from k7 files are not transformed into a |
| 20 | raw wave. It saves some time & memory but disables the ability to "hear" the |
| 21 | cassette. |
| 22 | This does not affect raw wave files. |
| 23 | Also, we do not do this for k5 files because the the corresponding wave |
| 24 | files are more compact anyway. */ |
| 25 | /* It must be set accordingly in machine/thomson.c */ |
| 26 | |
| 27 | #define VERBOSE 0 /* 0, 1 or 2 */ |
| 28 | |
| 29 | |
| 30 | /***************************** utilities **************************/ |
| 31 | |
| 32 | #define PRINT(x) printf x |
| 33 | |
| 34 | #define LOG(x) do { if (VERBOSE > 0) printf x; } while (0) |
| 35 | #define VLOG(x) do { if (VERBOSE > 1) printf x; } while (0) |
| 36 | |
| 37 | |
| 38 | #define TO7_BIT_LENGTH 0.001114 |
| 39 | |
| 40 | #define TO7_FREQ_CASS_0 4500. |
| 41 | #define TO7_FREQ_CASS_1 6300. |
| 42 | |
| 43 | #define TO7_PERIOD_CASS_0 ( 1. / TO7_FREQ_CASS_0 ) |
| 44 | #define TO7_PERIOD_CASS_1 ( 1. / TO7_FREQ_CASS_1 ) |
| 45 | |
| 46 | static UINT32 to7_k7_bitsize; |
| 47 | static UINT8* to7_k7_bits; |
| 48 | |
| 49 | #define MO5_BIT_LENGTH 0.000833 |
| 50 | #define MO5_HBIT_LENGTH (MO5_BIT_LENGTH / 2.) |
| 51 | |
| 52 | /************************* k7 format *************************/ |
| 53 | |
| 54 | /* The k7 format is widespread among existing Thomson emulators. |
| 55 | It represents the stream of bytes returned by the BIOS cassette routine, |
| 56 | and so, is used by emulators that bypass the BIOS routine. |
| 57 | It is generally hacked from the raw image more or less by hand |
| 58 | (in particular, to by-pass special loaders and copy-protection schemes). |
| 59 | |
| 60 | As we run the original BIOS routine, we need to hack back the k7 file |
| 61 | into the original stream. |
| 62 | |
| 63 | In theory, the byte stream is a sequence of blocks of the form: |
| 64 | - 2-byte header: 01 3C |
| 65 | - 1-byte type |
| 66 | 00 = start of file, the block contains the filename |
| 67 | 01 = chunk of file data |
| 68 | FF = end of file |
| 69 | - 1-byte size |
| 70 | - size bytes of data |
| 71 | - one byte of CRC |
| 72 | |
| 73 | These are separated by 0xff fillers. |
| 74 | |
| 75 | There are also a variety of non-standard blocks, and we try to handle them. |
| 76 | |
| 77 | Our main problem is to guess where the places where the motor will be |
| 78 | cut-off and introduce there sequences of 1 bit for synchronization. |
| 79 | |
| 80 | Note: as a nice effect, if our routine succeeds, it constructs a wave |
| 81 | file that should be usable on a real computer. |
| 82 | */ |
| 83 | |
| 84 | |
| 85 | |
| 86 | static const struct CassetteModulation to7_k7_modulation = |
| 87 | { |
| 88 | CASSETTE_MODULATION_SQUAREWAVE, |
| 89 | 4000.0, 4500.0, 5000.0, |
| 90 | 5500.0, 6300.0, 7500.0 |
| 91 | }; |
| 92 | |
| 93 | |
| 94 | |
| 95 | static casserr_t to7_k7_identify ( cassette_image *cass, struct CassetteOptions *opts ) |
| 96 | { |
| 97 | casserr_t e = cassette_modulation_identify( cass, &to7_k7_modulation, opts ); |
| 98 | return e; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | |
| 103 | static casserr_t to7_k7_load( cassette_image *cass ) |
| 104 | { |
| 105 | #if ! K7_SPEED_HACK |
| 106 | static const INT8 square_wave[] = { -128, 127 }; |
| 107 | double time = 0.; |
| 108 | #endif |
| 109 | size_t size = cassette_image_size( cass ), pos = 0; |
| 110 | int i, sz, sz2, bitmax = 1024, invalid = 0; |
| 111 | UINT8 in, typ, block[264]; |
| 112 | |
| 113 | LOG (( "to7_k7_load: start conversion, size=%li\n", (long)size )); |
| 114 | PRINT (( "to7_k7_load: open cassette, length: %li bytes\n", (long) size )); |
| 115 | |
| 116 | if ( to7_k7_bits ) |
| 117 | { |
| 118 | free( to7_k7_bits ); |
| 119 | to7_k7_bits = NULL; |
| 120 | } |
| 121 | |
| 122 | to7_k7_bitsize = 0; |
| 123 | to7_k7_bits = (UINT8*)malloc(bitmax ); |
| 124 | |
| 125 | /* store one period */ |
| 126 | #if K7_SPEED_HACK |
| 127 | #define K7_PUT( PERIOD ) |
| 128 | #else |
| 129 | #define K7_PUT( PERIOD ) \ |
| 130 | do \ |
| 131 | { \ |
| 132 | casserr_t err; \ |
| 133 | err = cassette_put_samples( cass, 0, time, (PERIOD), 2, 1, \ |
| 134 | square_wave, CASSETTE_WAVEFORM_8BIT ); \ |
| 135 | if ( err ) \ |
| 136 | return err; \ |
| 137 | time += (PERIOD); \ |
| 138 | } while (0) |
| 139 | #endif |
| 140 | |
| 141 | /* store one bit */ |
| 142 | #define K7_PUT_BIT( BIT ) \ |
| 143 | do \ |
| 144 | { \ |
| 145 | int b; \ |
| 146 | if ( BIT ) \ |
| 147 | { \ |
| 148 | for ( b = 0; b < 7; b++ ) \ |
| 149 | K7_PUT( TO7_PERIOD_CASS_1 ); \ |
| 150 | } \ |
| 151 | else \ |
| 152 | { \ |
| 153 | for ( b = 0; b < 5; b++ ) \ |
| 154 | K7_PUT( TO7_PERIOD_CASS_0 ); \ |
| 155 | } \ |
| 156 | if ( to7_k7_bitsize + 1 >= bitmax ) \ |
| 157 | { \ |
| 158 | UINT8* a = (UINT8*)malloc(bitmax * 2); \ |
| 159 | memcpy ( a, to7_k7_bits, bitmax ); \ |
| 160 | bitmax *= 2; \ |
| 161 | to7_k7_bits = a; \ |
| 162 | } \ |
| 163 | to7_k7_bits[ to7_k7_bitsize++ ] = (BIT); \ |
| 164 | } while (0) |
| 165 | |
| 166 | /* store one byte, with start / stop bits */ |
| 167 | #define K7_PUT_BYTE( BYTE ) \ |
| 168 | do \ |
| 169 | { \ |
| 170 | UINT8 x; \ |
| 171 | K7_PUT_BIT( 0 ); \ |
| 172 | for ( x = 0; x < 8; x++ ) \ |
| 173 | K7_PUT_BIT( ( (BYTE) >> x ) & 1 ); \ |
| 174 | K7_PUT_BIT( 1 ); \ |
| 175 | K7_PUT_BIT( 1 ); \ |
| 176 | } while (0) |
| 177 | |
| 178 | #define K7_FILL_1( SIZE ) \ |
| 179 | do \ |
| 180 | { \ |
| 181 | if ( (SIZE) > 0 ) { \ |
| 182 | int ii; \ |
| 183 | LOG (( "to7_k7_load: 1-filler size=%i bitstart=%i\n", \ |
| 184 | (SIZE), to7_k7_bitsize )); \ |
| 185 | for ( ii = 0; ii < (SIZE); ii++ ) K7_PUT_BIT( 1 ); \ |
| 186 | } \ |
| 187 | } while (0) |
| 188 | |
| 189 | #define K7_FILL_ff( SIZE ) \ |
| 190 | do \ |
| 191 | { \ |
| 192 | if ( (SIZE) > 0 ) \ |
| 193 | { \ |
| 194 | int ii; \ |
| 195 | LOG (( "to7_k7_load: 0xff filler size=%i bitstart=%i\n", (SIZE), to7_k7_bitsize )); \ |
| 196 | for ( ii = 0; ii < (SIZE); ii++ ) \ |
| 197 | K7_PUT_BYTE( 0xff ); \ |
| 198 | } \ |
| 199 | } while (0) |
| 200 | |
| 201 | /* check format */ |
| 202 | cassette_image_read( cass, block, 0, 64 ); |
| 203 | for ( i = 3; ; i++ ) |
| 204 | { |
| 205 | if ( ( i >= size ) || ( i >= 64 ) ) |
| 206 | { |
| 207 | /* ? */ |
| 208 | PRINT (( "to7_k7_load: WARNING: this does not look like a MO or TO cassette.\n" )); |
| 209 | break; |
| 210 | } |
| 211 | else if ( ( block[i-3] == 0x01 ) && ( block[i-2] == 0x3c ) && ( block[i-1] == 0x5a ) && ! block[i] ) |
| 212 | { |
| 213 | /* MO */ |
| 214 | PRINT (( "to7_k7_load: WARNING: this looks like a MO cassette, not a TO one.\n" )); |
| 215 | break; |
| 216 | } |
| 217 | else if ( ( block[i-3] == 0xff ) && ( block[i-2] == 0x01 ) && ( block[i-1] == 0x3c ) && ! block[i] ) |
| 218 | { |
| 219 | /* TO */ |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /* skip to first 0xff filler */ |
| 225 | for ( sz = 0; pos < size; pos++, sz++ ) |
| 226 | { |
| 227 | cassette_image_read( cass, &in, pos, 1 ); |
| 228 | if ( in == 0xff ) |
| 229 | break; |
| 230 | } |
| 231 | if ( sz > 0 ) |
| 232 | LOG (( "to7_k7_load: skip %i trash bytes\n", sz )); |
| 233 | |
| 234 | /* loop over regular blocks */ |
| 235 | while ( pos < size ) |
| 236 | { |
| 237 | |
| 238 | rebounce: |
| 239 | /* skip 0xff filler */ |
| 240 | for ( sz = 0; pos < size; pos++, sz++ ) |
| 241 | { |
| 242 | cassette_image_read( cass, &in, pos, 1 ); |
| 243 | /* actually, we are bit laxist and treat as 0xff bytes with at least |
| 244 | 5 bits out of 8 set to 1 |
| 245 | */ |
| 246 | for ( i = 0; in; in >>= 1 ) |
| 247 | i += (in & 1); |
| 248 | if ( i < 5 ) |
| 249 | break; |
| 250 | } |
| 251 | |
| 252 | /* get block header */ |
| 253 | if ( pos + 4 > size ) |
| 254 | { |
| 255 | pos -= sz; |
| 256 | break; |
| 257 | } |
| 258 | cassette_image_read( cass, block, pos, 4 ); |
| 259 | typ = block[2]; |
| 260 | sz2 = block[3]+1; |
| 261 | if ( block[0] != 0x01 || block[1] != 0x3c || ( typ != 0x00 && typ != 0x01 && typ != 0xff ) ) |
| 262 | { |
| 263 | pos -= sz; |
| 264 | break; |
| 265 | } |
| 266 | pos += 4; |
| 267 | |
| 268 | /* get block */ |
| 269 | cassette_image_read( cass, block+4, pos, sz2 ); |
| 270 | pos += sz2; |
| 271 | |
| 272 | /* 1-filler and 0xff-filler */ |
| 273 | if ( typ == 0 || typ == 0xff ) |
| 274 | K7_FILL_1( 1000 ); |
| 275 | K7_FILL_ff( sz ); |
| 276 | |
| 277 | /* put block */ |
| 278 | LOG (( "to7_k7_load: block off=$%x type=$%02X size=%i bitstart=%i\n", (int) pos-sz2-4, typ, sz2, to7_k7_bitsize )); |
| 279 | VLOG (( "to7_k7_load: data:" )); |
| 280 | for ( i = 0; i < sz2 + 4; i ++) |
| 281 | { |
| 282 | VLOG (( " $%02X", block[i] )); |
| 283 | K7_PUT_BYTE( block[i] ); |
| 284 | } |
| 285 | VLOG (( "\n" )); |
| 286 | |
| 287 | /* if it is a directory enty, says so to the user */ |
| 288 | if ( typ == 0 ) |
| 289 | { |
| 290 | char name[] = "01234567.ABC"; |
| 291 | UINT8 t = block[15]; |
| 292 | UINT8 u = block[16]; |
| 293 | int p = (to7_k7_bitsize - sz2 - 4 - sz) * TO7_BIT_LENGTH; |
| 294 | memcpy( name, block+4, 8 ); |
| 295 | memcpy( name+9, block+12, 3 ); |
| 296 | for ( i = 0; name[i]; i++) |
| 297 | { |
| 298 | if ( name[i] < ' ' || name[i] >= 127 ) |
| 299 | name[i] = '?'; |
| 300 | } |
| 301 | PRINT (( "to7_k7_load: file \"%s\" type=%s,%s at %imn %is\n", |
| 302 | name, |
| 303 | (t==0) ? "bas" : (t==1) ? "dat" : (t==2) ? "bin" : "???", |
| 304 | (u == 0) ? "a" : (u == 0xff) ? "b" : "?", |
| 305 | p / 60, p % 60 )); |
| 306 | } |
| 307 | |
| 308 | /* extra 1-fillers */ |
| 309 | if ( typ == 0 || typ == 0xff ) |
| 310 | K7_FILL_1( 1000 ); |
| 311 | } |
| 312 | |
| 313 | /* trailing data with invalid block structure |
| 314 | => dump it in a raw form, but stay alert for hidden block starts |
| 315 | */ |
| 316 | if ( pos < size ) |
| 317 | { |
| 318 | invalid++; |
| 319 | LOG (( "to7_k7_load: trailing raw bytes off=$%x bitstart=%i\n", (int) pos, to7_k7_bitsize )); |
| 320 | |
| 321 | /* put block */ |
| 322 | for (; pos < size; pos++ ) |
| 323 | { |
| 324 | cassette_image_read( cass, &in, pos, 1 ); |
| 325 | for ( sz = 0; pos < size && in == 0xff; sz++ ) |
| 326 | { |
| 327 | pos++; |
| 328 | cassette_image_read( cass, &in, pos, 1 ); |
| 329 | } |
| 330 | if ( invalid < 10 && sz > 4 && in == 0x01 && pos + 4 <= size ) |
| 331 | { |
| 332 | UINT8 in1,in2; |
| 333 | cassette_image_read( cass, &in1, pos+1, 1 ); |
| 334 | cassette_image_read( cass, &in2, pos+2, 1 ); |
| 335 | if ( (in1 == 0x3c) && ((in2 == 0x00) || (in2 == 0x01) ) ) |
| 336 | { |
| 337 | /* seems we have a regular block hidden in raw data => rebounce */ |
| 338 | LOG (( "to7_k7_load: hidden regular block found\n" )); |
| 339 | pos -= sz; |
| 340 | goto rebounce; |
| 341 | } |
| 342 | if ( ( ( in1 == 0x3d ) && ( in2 == 0 ) ) || ( ( in1 == 0x57 ) && ( in2 == 0x49 ) ) ) |
| 343 | { |
| 344 | /* special block (Infogrames) => just prepend filler */ |
| 345 | K7_FILL_1 ( 500 ); |
| 346 | LOG (( "to7_k7_load: special $%02X $%02X $%02X block found off=$%x bitstart=%i\n", in, in1, in2, (int) pos, to7_k7_bitsize )); |
| 347 | } |
| 348 | } |
| 349 | for ( i = 0; i < sz; i++ ) |
| 350 | K7_PUT_BYTE( 0xff ); |
| 351 | K7_PUT_BYTE( in ); |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | if ( invalid ) |
| 356 | PRINT (( "to7_k7_load: WARNING: the k7 has an unknown structure and may not work\n" )); |
| 357 | |
| 358 | sz = to7_k7_bitsize * TO7_BIT_LENGTH; |
| 359 | PRINT (( "to7_k7_load: cassette length: %imn %is (%i samples)\n", sz / 60, sz % 60, to7_k7_bitsize )); |
| 360 | |
| 361 | return CASSETTE_ERROR_SUCCESS; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | |
| 366 | static const struct CassetteFormat to7_k7 = |
| 367 | { "k7", to7_k7_identify, to7_k7_load, NULL /* no save */ }; |
| 368 | |
| 369 | |
| 370 | |
| 371 | /********************* TO7 WAV format ************************/ |
| 372 | |
| 373 | |
| 374 | |
| 375 | static casserr_t to7_wav_identify ( cassette_image *cass, |
| 376 | struct CassetteOptions *opts ) |
| 377 | { |
| 378 | casserr_t e = wavfile_format.identify( cass, opts ); |
| 379 | return e; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | |
| 384 | static casserr_t to7_wav_load ( cassette_image *cass ) |
| 385 | { |
| 386 | casserr_t e = wavfile_format.load( cass ); |
| 387 | struct CassetteInfo info; |
| 388 | double len; |
| 389 | |
| 390 | if ( to7_k7_bits ) |
| 391 | { |
| 392 | free( to7_k7_bits ); |
| 393 | to7_k7_bits = NULL; |
| 394 | } |
| 395 | |
| 396 | if ( e != CASSETTE_ERROR_SUCCESS ) |
| 397 | return e; |
| 398 | |
| 399 | cassette_get_info( cass, &info ); |
| 400 | |
| 401 | len = (double) info.sample_count / info.sample_frequency; |
| 402 | |
| 403 | PRINT (( "to7_wav_load: loading cassette, length %imn %is, %i Hz, %i bps, %i bits\n", |
| 404 | (int) len / 60, (int) len % 60, |
| 405 | info.sample_frequency, info.bits_per_sample, |
| 406 | (int) (len / TO7_BIT_LENGTH) )); |
| 407 | |
| 408 | return CASSETTE_ERROR_SUCCESS; |
| 409 | } |
| 410 | |
| 411 | |
| 412 | |
| 413 | static casserr_t to7_wav_save ( cassette_image *cass, const struct CassetteInfo *info ) |
| 414 | { |
| 415 | int len = info->sample_count / info->sample_frequency; |
| 416 | PRINT (( "to7_wav_save: saving cassette, length %imn %is, %i Hz, %i bps\n", len / 60, len % 60, info->sample_frequency, info->bits_per_sample )); |
| 417 | return wavfile_format.save( cass, info ); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | |
| 422 | /* overloaded wav: dump info */ |
| 423 | static const struct CassetteFormat to7_wav = |
| 424 | { "wav", to7_wav_identify, to7_wav_load, to7_wav_save }; |
| 425 | |
| 426 | |
| 427 | |
| 428 | /************************* k5 format *************************/ |
| 429 | |
| 430 | |
| 431 | |
| 432 | static casserr_t mo5_k5_identify ( cassette_image *cass, |
| 433 | struct CassetteOptions *opts ) |
| 434 | { |
| 435 | opts -> bits_per_sample = 8; |
| 436 | opts -> channels = 1; |
| 437 | opts -> sample_frequency = 22100;//11050; |
| 438 | return CASSETTE_ERROR_SUCCESS; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | |
| 443 | /* As the k7 format, the k5 format represents the stream of bytes returned by |
| 444 | the BIOS cassette routine and need to be hacked back into the original |
| 445 | cassette wave. |
| 446 | |
| 447 | The format of blocks is a bit different from that of TO7 cassettes: |
| 448 | - 2-byte header is: 3C 5A |
| 449 | - 1-byte type |
| 450 | 00 = start of file, the block contains the filename |
| 451 | 01 = chunk of file data |
| 452 | FF = end of file |
| 453 | - 1-byte size |
| 454 | - size - 1 bytes of data (255 bytes of data if size = 0) |
| 455 | - one byte of CRC: sum from (and including) header up to (and including) |
| 456 | CRC byte should be 0 modulo 256 |
| 457 | |
| 458 | They are separated by 0x01 filler bytes (generally 10 of them), |
| 459 | for synchronisation. There are also extra 0 filler bits between certain |
| 460 | kinds of blocks, when the motor is supposed to go off and back on. |
| 461 | */ |
| 462 | |
| 463 | static casserr_t mo5_k5_load( cassette_image *cass ) |
| 464 | { |
| 465 | size_t size = cassette_image_size( cass ), pos = 0; |
| 466 | int i, sz, sz2, hbit = 0; |
| 467 | UINT8 in, in2, in3, typ, block[264], sum; |
| 468 | int invalid = 0, hbitsize = 0, dcmoto = 0; |
| 469 | |
| 470 | LOG (( "mo5_k5_load: start conversion, size=%li\n", (long)size )); |
| 471 | PRINT (( "mo5_k5_load: open cassette, length: %li bytes\n", (long) size )); |
| 472 | |
| 473 | /* store a half-bit */ |
| 474 | #define K5_PUT_HBIT \ |
| 475 | do \ |
| 476 | { \ |
| 477 | cassette_put_sample ( cass, 0, hbitsize * MO5_HBIT_LENGTH, MO5_HBIT_LENGTH, (hbit ? 1 : -1) << 30 ); \ |
| 478 | hbitsize++; \ |
| 479 | } while ( 0 ) |
| 480 | |
| 481 | /* store one bit */ |
| 482 | #define K5_PUT_BIT( BIT ) \ |
| 483 | do \ |
| 484 | { \ |
| 485 | if ( BIT ) \ |
| 486 | { \ |
| 487 | K5_PUT_HBIT; \ |
| 488 | hbit = !hbit; \ |
| 489 | K5_PUT_HBIT; \ |
| 490 | } \ |
| 491 | else \ |
| 492 | { \ |
| 493 | K5_PUT_HBIT; \ |
| 494 | K5_PUT_HBIT; \ |
| 495 | } \ |
| 496 | hbit = !hbit; \ |
| 497 | } while (0) |
| 498 | |
| 499 | /* store one byte, no start / stop bit, converse order from TO7 */ |
| 500 | #define K5_PUT_BYTE( BYTE ) \ |
| 501 | do \ |
| 502 | { \ |
| 503 | UINT8 b = BYTE; \ |
| 504 | int x; \ |
| 505 | for ( x = 0; x < 8; x++ ) \ |
| 506 | K5_PUT_BIT( (b >> (7 - x)) & 1 ); \ |
| 507 | } while (0) |
| 508 | |
| 509 | /* store filler */ |
| 510 | #define K5_FILL_0( SIZE ) \ |
| 511 | do \ |
| 512 | { \ |
| 513 | if ( (SIZE) > 0 ) \ |
| 514 | { \ |
| 515 | int j; \ |
| 516 | LOG (( "mo5_k5_load: 0-filler size=%i hbitstart=%i\n", (SIZE), hbitsize )); \ |
| 517 | for ( j = 0; j < (SIZE); j++ ) \ |
| 518 | K5_PUT_BIT( 0 ); \ |
| 519 | } \ |
| 520 | } while (0) |
| 521 | |
| 522 | #define K5_FILL_01( SIZE ) \ |
| 523 | do \ |
| 524 | { \ |
| 525 | if ( (SIZE) > 0 ) \ |
| 526 | { \ |
| 527 | int j; \ |
| 528 | LOG (( "mo5_k5_load: 0x01 filler size=%i bitstart=%i\n", (SIZE), hbitsize )); \ |
| 529 | for ( j = 0; j < (SIZE); j++ ) \ |
| 530 | K5_PUT_BYTE( 0x01 ); \ |
| 531 | } \ |
| 532 | } while (0) |
| 533 | |
| 534 | /* check format */ |
| 535 | cassette_image_read( cass, block, 0, 64 ); |
| 536 | for ( i = 3; ; i++ ) |
| 537 | { |
| 538 | if ( ( i >= size ) || ( i >= 64 ) ) |
| 539 | { |
| 540 | /* ? */ |
| 541 | PRINT (( "to5_k5_load: WARNING: this does not look like a MO or TO cassette.\n" )); |
| 542 | break; |
| 543 | } |
| 544 | else if ( ( block[i-3] == 0x01 ) && ( block[i-2] == 0x3c ) && ( block[i-1] == 0x5a ) && ! block[i] ) |
| 545 | { |
| 546 | /* MO */ |
| 547 | break; |
| 548 | } |
| 549 | else if ( ( block[i-3] == 0xff ) && ( block[i-2] == 0x01 ) && ( block[i-1] == 0x3c ) && ! block[i] ) |
| 550 | { |
| 551 | /* TO */ |
| 552 | PRINT (( "to5_k5_load: WARNING: this looks like a TO cassette, not a MO one.\n" )); |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | cassette_image_read( cass, block, pos, 6 ); |
| 558 | if ( ! memcmp( block, "DCMOTO", 6 ) || ! memcmp( block, "DCMO5", 5 ) || ! memcmp( block, "DCMO6", 5 ) ) |
| 559 | dcmoto = 1; |
| 560 | |
| 561 | /* loop over regular blocks */ |
| 562 | while ( pos < size ) |
| 563 | { |
| 564 | |
| 565 | rebounce: |
| 566 | /* skip DCMOTO header*/ |
| 567 | if ( dcmoto ) |
| 568 | { |
| 569 | cassette_image_read( cass, block, pos, 6 ); |
| 570 | if ( ! memcmp( block, "DCMOTO", 6 ) ) |
| 571 | { |
| 572 | LOG (( "mo5_k5_load: DCMOTO signature found at off=$%x\n", (int)pos )); |
| 573 | pos += 6; |
| 574 | } |
| 575 | else if ( ! memcmp( block, "DCMO", 4 ) ) |
| 576 | { |
| 577 | LOG (( "mo5_k5_load: DCMO* signature found at off=$%x\n", (int)pos )); |
| 578 | pos += 5; |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | /* skip 0x01 filler */ |
| 583 | for ( sz = 0; pos < size; pos++, sz++ ) |
| 584 | { |
| 585 | cassette_image_read( cass, &in, pos, 1 ); |
| 586 | if ( in != 0x01 ) |
| 587 | break; |
| 588 | } |
| 589 | |
| 590 | /* get block header */ |
| 591 | if ( pos + 4 > size ) |
| 592 | { |
| 593 | pos -= sz; |
| 594 | break; |
| 595 | } |
| 596 | cassette_image_read( cass, block, pos, 4 ); |
| 597 | typ = block[2]; |
| 598 | sz2 = (UINT8) (block[3]-1); |
| 599 | if ( block[0] != 0x3c || block[1] != 0x5a || ( typ != 0x00 && typ != 0x01 && typ != 0xff ) || pos+sz2 > size ) |
| 600 | { |
| 601 | pos -= sz; |
| 602 | break; |
| 603 | } |
| 604 | pos += 4; |
| 605 | |
| 606 | /* get block */ |
| 607 | cassette_image_read( cass, block+4, pos, sz2 ); |
| 608 | pos += sz2; |
| 609 | |
| 610 | /* 0-fillers and 0x01-fillers */ |
| 611 | if ( typ == 0 || typ == 0xff ) |
| 612 | K5_FILL_0( 1200 ); |
| 613 | else |
| 614 | K5_FILL_0( 300 ); /* for MO6 */ |
| 615 | K5_FILL_01( sz < 10 ? 10 : sz ); |
| 616 | |
| 617 | /* put block */ |
| 618 | LOG (( "mo5_k5_load: block off=$%x type=$%02X size=%i hbitstart=%i\n", (int) pos-sz2-4, typ, sz2, hbitsize )); |
| 619 | VLOG (( "mo5_k5_load: data:" )); |
| 620 | for ( i = 0; i < sz2 + 4; i ++) |
| 621 | { |
| 622 | VLOG (( " $%02X", block[i] )); |
| 623 | K5_PUT_BYTE( block[i] ); |
| 624 | } |
| 625 | VLOG (( "\n" )); |
| 626 | |
| 627 | /* checksum */ |
| 628 | for ( i = 0, sum = 0; i < sz2; i++ ) |
| 629 | sum += block[i+4]; |
| 630 | if ( sum ) |
| 631 | LOG(( "mo5_k5_load: invalid checksum $%02X (should be 0)\n", sum )); |
| 632 | |
| 633 | /* if it is a directory enty, says so to the user */ |
| 634 | if ( typ == 0 ) |
| 635 | { |
| 636 | char name[] = "01234567.ABC"; |
| 637 | UINT8 t = block[15]; |
| 638 | UINT8 u = block[16]; |
| 639 | int p = (hbitsize - sz2 - 4 - sz) * MO5_HBIT_LENGTH; |
| 640 | memcpy( name, block+4, 8 ); |
| 641 | memcpy( name+9, block+12, 3 ); |
| 642 | for ( i = 0; name[i]; i++) |
| 643 | { |
| 644 | if ( name[i] < ' ' || name[i] >= 127 ) |
| 645 | name[i] = '?'; |
| 646 | } |
| 647 | PRINT (( "mo5_k5_load: file \"%s\" type=%s,%s at %imn %is\n", |
| 648 | name, |
| 649 | (t==0) ? "bas" : (t==1) ? "dat" : (t==2) ? "bin" : "???", |
| 650 | (u == 0) ? "a" : (u == 0xff) ? "b" : "?", |
| 651 | p / 60, p % 60 )); |
| 652 | } |
| 653 | |
| 654 | /* 0-fillers */ |
| 655 | if ( typ == 0xff || typ == 0x00 ) |
| 656 | K5_FILL_0( 1800 ); |
| 657 | } |
| 658 | |
| 659 | /* dump trailing bytes, but also look for beginings of blocks */ |
| 660 | if ( pos < size ) |
| 661 | { |
| 662 | invalid++; |
| 663 | K5_FILL_0( 1200 ); |
| 664 | LOG (( "mo5_k5_load: trailing trash off=$%x size=%i hbitstart=%i\n", (int) pos, (int) (size-pos), hbitsize )); |
| 665 | for ( ; pos < size; pos++ ) |
| 666 | { |
| 667 | cassette_image_read( cass, &in, pos, 1 ); |
| 668 | if ( dcmoto && in=='D' ) |
| 669 | { |
| 670 | /* skip DCMOTO header*/ |
| 671 | cassette_image_read( cass, block, pos, 6 ); |
| 672 | if ( ! memcmp( block, "DCMOTO", 6 ) ) |
| 673 | { |
| 674 | LOG (( "mo5_k5_load: DCMOTO signature found at off=$%x\n", (int)pos )); |
| 675 | pos += 6; |
| 676 | cassette_image_read( cass, &in, pos, 1 ); |
| 677 | } |
| 678 | else if ( ! memcmp( block, "DCMO", 4 ) ) |
| 679 | { |
| 680 | LOG (( "mo5_k5_load: DCMO* signature found at off=$%x\n", (int)pos )); |
| 681 | pos += 5; |
| 682 | cassette_image_read( cass, &in, pos, 1 ); |
| 683 | } |
| 684 | } |
| 685 | for ( sz = 0; pos < size && in == 0x01; sz++ ) |
| 686 | { |
| 687 | pos++; |
| 688 | cassette_image_read( cass, &in, pos, 1 ); |
| 689 | } |
| 690 | if ( sz > 6 ) |
| 691 | { |
| 692 | cassette_image_read( cass, &in2, pos+1, 1 ); |
| 693 | cassette_image_read( cass, &in3, pos+2, 1 ); |
| 694 | if ( invalid < 10 && in == 0x3c && in2 == 0x5a && (in3 == 0x00 || in3 == 0x01 || in3 == 0xff ) ) |
| 695 | { |
| 696 | /* regular block found */ |
| 697 | LOG (( "mo5_k5_load: hidden regular block found\n" )); |
| 698 | pos -= sz; |
| 699 | goto rebounce; |
| 700 | } |
| 701 | if ( invalid < 10 && sz > 6 && ( (in == 0x3c && in2 == 0x5a) || (in == 0xc3 && in2 == 0x5a) || (in == 0xc3 && in2 == 0x3c) || (in == 0x87 && in2 == 0x4a) ) ) |
| 702 | { |
| 703 | /* special block found */ |
| 704 | K5_FILL_0( 1200 ); |
| 705 | LOG (( "mo5_k5_load: special block $%02X $%02X found off=$%x hbitstart=%i\n", in, in2, (int) pos-sz, hbitsize )); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | VLOG (( "mo5_k5_load: special data:" )); |
| 710 | for ( i = 0; i < sz; i++ ) |
| 711 | { |
| 712 | K5_PUT_BYTE( 0x01 ); |
| 713 | VLOG (( " $01" )); |
| 714 | } |
| 715 | K5_PUT_BYTE( in ); |
| 716 | VLOG (( " $%02X\n", in )); |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | if ( invalid ) |
| 721 | PRINT (( "mo5_k5_load: WARNING: the k5 has an unknown structure and may not work\n" )); |
| 722 | |
| 723 | sz = hbitsize * MO5_HBIT_LENGTH; |
| 724 | PRINT (( "mo5_k5_load: cassette length: %imn %is (%i half-bits)\n", sz / 60, sz % 60, hbitsize )); |
| 725 | |
| 726 | return CASSETTE_ERROR_SUCCESS; |
| 727 | } |
| 728 | |
| 729 | |
| 730 | |
| 731 | static const struct CassetteFormat mo5_k5 = |
| 732 | { "k5,k7", mo5_k5_identify, mo5_k5_load, NULL /* no save */ }; |
| 733 | |
| 734 | |
| 735 | /********************* MO5 WAV format ************************/ |
| 736 | |
| 737 | |
| 738 | |
| 739 | static casserr_t mo5_wav_identify ( cassette_image *cass, |
| 740 | struct CassetteOptions *opts ) |
| 741 | { |
| 742 | casserr_t e = wavfile_format.identify( cass, opts ); |
| 743 | return e; |
| 744 | } |
| 745 | |
| 746 | |
| 747 | |
| 748 | static casserr_t mo5_wav_load ( cassette_image *cass ) |
| 749 | { |
| 750 | casserr_t e = wavfile_format.load( cass ); |
| 751 | struct CassetteInfo info; |
| 752 | int len; |
| 753 | if ( e == CASSETTE_ERROR_SUCCESS ) |
| 754 | { |
| 755 | cassette_get_info( cass, &info ); |
| 756 | len = info.sample_count / info.sample_frequency; |
| 757 | PRINT (( "mo5_wav_load: loading cassette, length %imn %is, %i Hz, %i bps\n", len / 60, len % 60, info.sample_frequency, info.bits_per_sample )); |
| 758 | } |
| 759 | return e; |
| 760 | } |
| 761 | |
| 762 | |
| 763 | |
| 764 | static casserr_t mo5_wav_save ( cassette_image *cass, const struct CassetteInfo *info ) |
| 765 | { |
| 766 | int len = info->sample_count / info->sample_frequency; |
| 767 | PRINT (( "mo5_wav_save: saving cassette, length %imn %is, %i Hz, %i bps\n", len / 60, len % 60, info->sample_frequency, info->bits_per_sample )); |
| 768 | return wavfile_format.save( cass, info ); |
| 769 | } |
| 770 | |
| 771 | |
| 772 | |
| 773 | /* overloaded wav: dump info */ |
| 774 | static const struct CassetteFormat mo5_wav = |
| 775 | { "wav", mo5_wav_identify, mo5_wav_load, mo5_wav_save }; |
| 776 | |
| 777 | |
| 778 | |
| 779 | /********************* formats ************************/ |
| 780 | |
| 781 | |
| 782 | const struct CassetteFormat *const to7_cassette_formats[] = |
| 783 | { &to7_wav, &to7_k7, NULL }; |
| 784 | |
| 785 | |
| 786 | const struct CassetteFormat *const mo5_cassette_formats[] = |
| 787 | { &mo5_wav, &mo5_k5, NULL }; |
src/lib/util/imageutl.c
| r11457 | r11458 | |
| 1 | | /*************************************************************************** |
| 2 | | |
| 3 | | imageutl.h |
| 4 | | |
| 5 | | Image related utilities |
| 6 | | |
| 7 | | **************************************************************************** |
| 8 | | |
| 9 | | Copyright Aaron Giles |
| 10 | | All rights reserved. |
| 11 | | |
| 12 | | Redistribution and use in source and binary forms, with or without |
| 13 | | modification, are permitted provided that the following conditions are |
| 14 | | met: |
| 15 | | |
| 16 | | * Redistributions of source code must retain the above copyright |
| 17 | | notice, this list of conditions and the following disclaimer. |
| 18 | | * Redistributions in binary form must reproduce the above copyright |
| 19 | | notice, this list of conditions and the following disclaimer in |
| 20 | | the documentation and/or other materials provided with the |
| 21 | | distribution. |
| 22 | | * Neither the name 'MAME' nor the names of its contributors may be |
| 23 | | used to endorse or promote products derived from this software |
| 24 | | without specific prior written permission. |
| 25 | | |
| 26 | | THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR |
| 27 | | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 28 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 29 | | DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT, |
| 30 | | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 31 | | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 32 | | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 33 | | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 34 | | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 35 | | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 36 | | POSSIBILITY OF SUCH DAMAGE. |
| 37 | | |
| 38 | | ***************************************************************************/ |
| 39 | | |
| 40 | | #include <ctype.h> |
| 41 | | #include "imageutl.h" |
| 42 | | |
| 43 | | int compute_log2(int val) |
| 44 | | { |
| 45 | | int count = 0; |
| 46 | | |
| 47 | | while(val > 1) |
| 48 | | { |
| 49 | | if (val & 1) |
| 50 | | return -1; |
| 51 | | val /= 2; |
| 52 | | count++; |
| 53 | | } |
| 54 | | return (val == 0) ? -1 : count; |
| 55 | | } |
| 56 | | |
| 57 | | /* |
| 58 | | Compute CCITT CRC-16 using the correct bit order for floppy disks. |
| 59 | | CRC code courtesy of Tim Mann. |
| 60 | | */ |
| 61 | | |
| 62 | | /* Accelerator table to compute the CRC eight bits at a time */ |
| 63 | | static const unsigned short ccitt_crc16_table[256] = |
| 64 | | { |
| 65 | | 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, |
| 66 | | 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, |
| 67 | | 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, |
| 68 | | 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, |
| 69 | | 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, |
| 70 | | 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, |
| 71 | | 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, |
| 72 | | 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, |
| 73 | | 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, |
| 74 | | 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, |
| 75 | | 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, |
| 76 | | 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, |
| 77 | | 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, |
| 78 | | 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, |
| 79 | | 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, |
| 80 | | 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, |
| 81 | | 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, |
| 82 | | 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, |
| 83 | | 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, |
| 84 | | 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, |
| 85 | | 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, |
| 86 | | 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, |
| 87 | | 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, |
| 88 | | 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, |
| 89 | | 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, |
| 90 | | 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, |
| 91 | | 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, |
| 92 | | 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, |
| 93 | | 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, |
| 94 | | 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, |
| 95 | | 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, |
| 96 | | 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 |
| 97 | | }; |
| 98 | | |
| 99 | | unsigned short ccitt_crc16(unsigned short crc, const unsigned char *buffer, size_t buffer_len) |
| 100 | | { |
| 101 | | size_t i; |
| 102 | | for (i = 0; i < buffer_len; i++) |
| 103 | | crc = (crc << 8) ^ ccitt_crc16_table[(crc >> 8) ^ buffer[i]]; |
| 104 | | return crc; |
| 105 | | } |
| 106 | | |
| 107 | | unsigned short ccitt_crc16_one( unsigned short crc, const unsigned char data ) |
| 108 | | { |
| 109 | | return (crc << 8) ^ ccitt_crc16_table[(crc >> 8) ^ data]; |
| 110 | | } |
| 111 | | |
| 112 | | |
| 113 | | /*------------------------------------------------- |
| 114 | | is_delim - returns whether c is a comma or \0 |
| 115 | | -------------------------------------------------*/ |
| 116 | | |
| 117 | | static int is_delim(char c) |
| 118 | | { |
| 119 | | return (c == '\0') || (c == ','); |
| 120 | | } |
| 121 | | |
| 122 | | |
| 123 | | |
| 124 | | /*------------------------------------------------- |
| 125 | | image_internal_find_extension - find an extension in |
| 126 | | an extension list |
| 127 | | -------------------------------------------------*/ |
| 128 | | |
| 129 | | static int image_internal_find_extension(const char *extension_list, const char *target_extension) |
| 130 | | { |
| 131 | | /* this version allows target_extension to be delimited with a comma */ |
| 132 | | int pos = 0; |
| 133 | | int i; |
| 134 | | |
| 135 | | while(extension_list[pos] != '\0') |
| 136 | | { |
| 137 | | /* compare a file extension */ |
| 138 | | i = 0; |
| 139 | | while(!is_delim(extension_list[pos + i]) |
| 140 | | && !is_delim(target_extension[i]) |
| 141 | | && (tolower(extension_list[pos + i]) == tolower(target_extension[i]))) |
| 142 | | { |
| 143 | | i++; |
| 144 | | } |
| 145 | | |
| 146 | | /* check to see if it was found */ |
| 147 | | if (is_delim(extension_list[pos + i]) && is_delim(target_extension[i])) |
| 148 | | return TRUE; |
| 149 | | |
| 150 | | /* move to next position in the buffer */ |
| 151 | | pos += i; |
| 152 | | while(!is_delim(extension_list[pos])) |
| 153 | | pos++; |
| 154 | | while(extension_list[pos] == ',') |
| 155 | | pos++; |
| 156 | | } |
| 157 | | |
| 158 | | /* not found */ |
| 159 | | return FALSE; |
| 160 | | } |
| 161 | | |
| 162 | | |
| 163 | | |
| 164 | | /*------------------------------------------------- |
| 165 | | image_find_extension - find an extension in an extension |
| 166 | | list |
| 167 | | -------------------------------------------------*/ |
| 168 | | |
| 169 | | int image_find_extension(const char *extension_list, const char *target_extension) |
| 170 | | { |
| 171 | | /* the internal function allows something that we do not */ |
| 172 | | if (strchr(target_extension, ',')) |
| 173 | | return FALSE; |
| 174 | | |
| 175 | | /* special case to allow ext to be in the form '.EXT' */ |
| 176 | | if (*target_extension == '.') |
| 177 | | target_extension++; |
| 178 | | |
| 179 | | /* do the actual work */ |
| 180 | | return image_internal_find_extension(extension_list, target_extension); |
| 181 | | } |
| 182 | | |
| 183 | | |
| 184 | | |
| 185 | | /*------------------------------------------------- |
| 186 | | image_specify_extension - merge a comma-delimited |
| 187 | | list of file extensions onto an existing list |
| 188 | | -------------------------------------------------*/ |
| 189 | | |
| 190 | | void image_specify_extension(char *buffer, size_t buffer_len, const char *extension) |
| 191 | | { |
| 192 | | int extension_pos = 0; |
| 193 | | int len; |
| 194 | | int found; |
| 195 | | |
| 196 | | /* determine the length of the buffer */ |
| 197 | | len = strlen(buffer); |
| 198 | | |
| 199 | | /* be aware that extension can be NULL */ |
| 200 | | if (extension != NULL) |
| 201 | | { |
| 202 | | while(extension[extension_pos] != '\0') |
| 203 | | { |
| 204 | | /* try to find the file extension */ |
| 205 | | found = image_internal_find_extension(buffer, &extension[extension_pos]); |
| 206 | | |
| 207 | | /* append a delimiter if we have to */ |
| 208 | | if (!found && (len > 0)) |
| 209 | | len += snprintf(&buffer[len], buffer_len - len, ","); |
| 210 | | |
| 211 | | /* move to the next extension, appending the extension if not found */ |
| 212 | | while(!is_delim(extension[extension_pos])) |
| 213 | | { |
| 214 | | if (!found) |
| 215 | | len += snprintf(&buffer[len], buffer_len - len, "%c", extension[extension_pos]); |
| 216 | | extension_pos++; |
| 217 | | } |
| 218 | | while(extension[extension_pos] == ',') |
| 219 | | extension_pos++; |
| 220 | | } |
| 221 | | } |
| 222 | | } |
| 223 | | |
src/mess/formats/tzx_cas.c
| r11457 | r11458 | |
| 1 | | /* |
| 2 | | |
| 3 | | TZX (currently spectrum only) and spectrum TAP cassette format support by Wilbert Pol |
| 4 | | |
| 5 | | TODO: |
| 6 | | Add support for the remaining block types: |
| 7 | | case 0x15: Direct Recording |
| 8 | | case 0x18: CSW Recording |
| 9 | | case 0x19: Generalized Data Block |
| 10 | | case 0x21: Group Start |
| 11 | | case 0x22: Group End |
| 12 | | case 0x23: Jump To Block |
| 13 | | case 0x24: Loop Start |
| 14 | | case 0x25: Loop End |
| 15 | | case 0x26: Call Sequence |
| 16 | | case 0x27: Return From Sequence |
| 17 | | case 0x28: Select Block |
| 18 | | case 0x2A: Stop Tape if in 48K Mode |
| 19 | | case 0x2B: Set signal level |
| 20 | | case 0x5A: Merge Block |
| 21 | | Add support for the deprecated block types? Only if there is some image which need them: |
| 22 | | case 0x16: C64 ROM type data block |
| 23 | | case 0x17: C64 turbo tape data block |
| 24 | | case 0x34: Emulation info |
| 25 | | case 0x40: Snapshot block |
| 26 | | |
| 27 | | Notes: |
| 28 | | |
| 29 | | TZX format specification lists |
| 30 | | 8064 pulses for a header block and 3220 for a data block |
| 31 | | |
| 32 | | but the documentaiton on worldofspectrum lists |
| 33 | | 8063 pulses for a header block and 3223 for a data block |
| 34 | | |
| 35 | | see http://www.worldofspectrum.org/faq/reference/48kreference.htm#TapeDataStructure |
| 36 | | |
| 37 | | We are currently using the numbers from the TZX specification... |
| 38 | | |
| 39 | | */ |
| 40 | | |
| 41 | | #include "emu.h" |
| 42 | | #include "formats/tzx_cas.h" |
| 43 | | |
| 44 | | |
| 45 | | #define TZX_WAV_FREQUENCY 44100 |
| 46 | | #define WAVE_LOW -0x5a9e |
| 47 | | #define WAVE_HIGH 0x5a9e |
| 48 | | #define WAVE_NULL 0 |
| 49 | | |
| 50 | | #define SUPPORTED_VERSION_MAJOR 0x01 |
| 51 | | |
| 52 | | #define INITIAL_MAX_BLOCK_COUNT 256 |
| 53 | | #define BLOCK_COUNT_INCREMENTS 256 |
| 54 | | |
| 55 | | static const UINT8 TZX_HEADER[8] = { 'Z','X','T','a','p','e','!',0x1a }; |
| 56 | | |
| 57 | | /* |
| 58 | | Global variables |
| 59 | | |
| 60 | | Initialized by tzx_cas_get_wave_size, used (and cleaned up) by tzx_cas_fill_wave |
| 61 | | */ |
| 62 | | |
| 63 | | static INT16 wave_data = 0; |
| 64 | | static int block_count = 0; |
| 65 | | static UINT8** blocks = NULL; |
| 66 | | static float t_scale = 1; /* for scaling T-states to the 4MHz CPC */ |
| 67 | | |
| 68 | | static void toggle_wave_data(void) |
| 69 | | { |
| 70 | | if (wave_data == WAVE_LOW) |
| 71 | | { |
| 72 | | wave_data = WAVE_HIGH; |
| 73 | | } |
| 74 | | else |
| 75 | | { |
| 76 | | wave_data = WAVE_LOW; |
| 77 | | } |
| 78 | | } |
| 79 | | |
| 80 | | static void tzx_cas_get_blocks( const UINT8 *casdata, int caslen ) |
| 81 | | { |
| 82 | | int pos = sizeof(TZX_HEADER) + 2; |
| 83 | | int max_block_count = INITIAL_MAX_BLOCK_COUNT; |
| 84 | | |
| 85 | | blocks = (UINT8**)malloc(max_block_count); |
| 86 | | memset(blocks,0,max_block_count); |
| 87 | | block_count = 0; |
| 88 | | |
| 89 | | while (pos < caslen) |
| 90 | | { |
| 91 | | UINT32 datasize; |
| 92 | | UINT8 blocktype = casdata[pos]; |
| 93 | | |
| 94 | | if (block_count == max_block_count) |
| 95 | | { |
| 96 | | void *old_blocks = blocks; |
| 97 | | int old_max_block_count = max_block_count; |
| 98 | | printf("realoc"); |
| 99 | | max_block_count = max_block_count + BLOCK_COUNT_INCREMENTS; |
| 100 | | blocks = (UINT8**)malloc(max_block_count); // SHOULD NOT BE USING auto_alloc_array() |
| 101 | | memset(blocks,0,max_block_count); |
| 102 | | memcpy(blocks, old_blocks, old_max_block_count * sizeof(UINT8*)); |
| 103 | | if (blocks) free(old_blocks); |
| 104 | | } |
| 105 | | |
| 106 | | blocks[block_count] = (UINT8*)&casdata[pos]; |
| 107 | | |
| 108 | | pos += 1; |
| 109 | | |
| 110 | | switch (blocktype) |
| 111 | | { |
| 112 | | case 0x10: |
| 113 | | pos += 2; |
| 114 | | datasize = casdata[pos] + (casdata[pos + 1] << 8); |
| 115 | | pos += 2 + datasize; |
| 116 | | break; |
| 117 | | case 0x11: |
| 118 | | pos += 0x0f; |
| 119 | | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16); |
| 120 | | pos += 3 + datasize; |
| 121 | | break; |
| 122 | | case 0x12: |
| 123 | | pos += 4; |
| 124 | | break; |
| 125 | | case 0x13: |
| 126 | | datasize = casdata[pos]; |
| 127 | | pos += 1 + 2 * datasize; |
| 128 | | break; |
| 129 | | case 0x14: |
| 130 | | pos += 7; |
| 131 | | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16); |
| 132 | | pos += 3 + datasize; |
| 133 | | break; |
| 134 | | case 0x15: |
| 135 | | pos += 5; |
| 136 | | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16); |
| 137 | | pos += 3 + datasize; |
| 138 | | break; |
| 139 | | case 0x20: case 0x23: case 0x24: |
| 140 | | pos += 2; |
| 141 | | break; |
| 142 | | case 0x21: case 0x30: |
| 143 | | datasize = casdata[pos]; |
| 144 | | pos += 1 + datasize; |
| 145 | | break; |
| 146 | | case 0x22: case 0x25: case 0x27: |
| 147 | | break; |
| 148 | | case 0x26: |
| 149 | | datasize = casdata[pos] + (casdata[pos + 1] << 8); |
| 150 | | pos += 2 + 2 * datasize; |
| 151 | | break; |
| 152 | | case 0x28: case 0x32: |
| 153 | | datasize = casdata[pos] + (casdata[pos + 1] << 8); |
| 154 | | pos += 2 + datasize; |
| 155 | | break; |
| 156 | | case 0x31: |
| 157 | | pos += 1; |
| 158 | | datasize = casdata[pos]; |
| 159 | | pos += 1 + datasize; |
| 160 | | break; |
| 161 | | case 0x33: |
| 162 | | datasize = casdata[pos]; |
| 163 | | pos += 1 + 3 * datasize; |
| 164 | | break; |
| 165 | | case 0x34: |
| 166 | | pos += 8; |
| 167 | | break; |
| 168 | | case 0x35: |
| 169 | | pos += 0x10; |
| 170 | | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16) + (casdata[pos + 3] << 24); |
| 171 | | pos += 4 + datasize; |
| 172 | | break; |
| 173 | | case 0x40: |
| 174 | | pos += 1; |
| 175 | | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16); |
| 176 | | pos += 3 + datasize; |
| 177 | | break; |
| 178 | | case 0x5A: |
| 179 | | pos += 9; |
| 180 | | break; |
| 181 | | default: |
| 182 | | datasize = casdata[pos] + (casdata[pos + 1] << 8) + (casdata[pos + 2] << 16) + (casdata[pos + 3] << 24); |
| 183 | | pos += 4 + datasize; |
| 184 | | break; |
| 185 | | } |
| 186 | | |
| 187 | | block_count++; |
| 188 | | } |
| 189 | | } |
| 190 | | |
| 191 | | INLINE int millisec_to_samplecount( int millisec ) |
| 192 | | { |
| 193 | | return (int) (millisec * ((double)TZX_WAV_FREQUENCY / 1000.0)); |
| 194 | | } |
| 195 | | |
| 196 | | INLINE int tcycles_to_samplecount( int tcycles ) |
| 197 | | { |
| 198 | | return (int) ((0.5 + (((double)TZX_WAV_FREQUENCY / 3500000) * (double)tcycles)) * t_scale); |
| 199 | | } |
| 200 | | |
| 201 | | static void tzx_output_wave( INT16 **buffer, int length ) |
| 202 | | { |
| 203 | | if (buffer == NULL) |
| 204 | | { |
| 205 | | return; |
| 206 | | } |
| 207 | | |
| 208 | | for ( ; length > 0; length--) |
| 209 | | { |
| 210 | | **buffer = wave_data; |
| 211 | | *buffer = *buffer + 1; |
| 212 | | } |
| 213 | | } |
| 214 | | |
| 215 | | static int tzx_cas_handle_block( INT16 **buffer, const UINT8 *bytes, int pause, int data_size, int pilot, int pilot_length, int sync1, int sync2, int bit0, int bit1, int bits_in_last_byte ) |
| 216 | | { |
| 217 | | int pilot_samples = tcycles_to_samplecount(pilot); |
| 218 | | int sync1_samples = tcycles_to_samplecount(sync1); |
| 219 | | int sync2_samples = tcycles_to_samplecount(sync2); |
| 220 | | int bit0_samples = tcycles_to_samplecount(bit0); |
| 221 | | int bit1_samples = tcycles_to_samplecount(bit1); |
| 222 | | int data_index; |
| 223 | | int size = 0; |
| 224 | | |
| 225 | | /* Uncomment this to include into error.log a fully detailed analysis of each block */ |
| 226 | | // logerror("tzx_cas_block_size: pilot_length = %d, pilot_samples = %d, sync1_samples = %d, sync2_samples = %d, bit0_samples = %d, bit1_samples = %d\n", pilot_length, pilot_samples, sync1_samples, sync2_samples, bit0_samples, bit1_samples); |
| 227 | | |
| 228 | | /* PILOT */ |
| 229 | | for ( ; pilot_length > 0; pilot_length--) |
| 230 | | { |
| 231 | | tzx_output_wave(buffer, pilot_samples); |
| 232 | | size += pilot_samples; |
| 233 | | toggle_wave_data(); |
| 234 | | } |
| 235 | | /* SYNC1 */ |
| 236 | | if (sync1_samples > 0) |
| 237 | | { |
| 238 | | tzx_output_wave(buffer, sync1_samples); |
| 239 | | size += sync1_samples; |
| 240 | | toggle_wave_data(); |
| 241 | | } |
| 242 | | /* SYNC2 */ |
| 243 | | if (sync2_samples > 0) |
| 244 | | { |
| 245 | | tzx_output_wave(buffer, sync2_samples); |
| 246 | | size += sync2_samples; |
| 247 | | toggle_wave_data(); |
| 248 | | } |
| 249 | | /* data */ |
| 250 | | for (data_index = 0; data_index < data_size; data_index++) |
| 251 | | { |
| 252 | | UINT8 byte = bytes[data_index]; |
| 253 | | int bits_to_go = (data_index == (data_size - 1)) ? bits_in_last_byte : 8; |
| 254 | | |
| 255 | | for ( ; bits_to_go > 0; byte <<= 1, bits_to_go--) |
| 256 | | { |
| 257 | | int bit_samples = (byte & 0x80) ? bit1_samples : bit0_samples; |
| 258 | | tzx_output_wave(buffer, bit_samples); |
| 259 | | size += bit_samples; |
| 260 | | toggle_wave_data(); |
| 261 | | tzx_output_wave(buffer, bit_samples); |
| 262 | | size += bit_samples; |
| 263 | | toggle_wave_data(); |
| 264 | | } |
| 265 | | } |
| 266 | | /* pause */ |
| 267 | | if (pause > 0) |
| 268 | | { |
| 269 | | int start_pause_samples = millisec_to_samplecount(1); |
| 270 | | int rest_pause_samples = millisec_to_samplecount(pause - 1); |
| 271 | | |
| 272 | | tzx_output_wave(buffer, start_pause_samples); |
| 273 | | size += start_pause_samples; |
| 274 | | wave_data = WAVE_LOW; |
| 275 | | tzx_output_wave(buffer, rest_pause_samples); |
| 276 | | size += rest_pause_samples; |
| 277 | | } |
| 278 | | return size; |
| 279 | | } |
| 280 | | |
| 281 | | static void ascii_block_common_log( const char *block_type_string, UINT8 block_type ) |
| 282 | | { |
| 283 | | logerror("%s (type %02x) encountered.\n", block_type_string, block_type); |
| 284 | | logerror("This block contains info on the .tzx file you are loading.\n"); |
| 285 | | logerror("Please include the following info in your bug reports, if the image has issue in M.E.S.S.\n"); |
| 286 | | } |
| 287 | | |
| 288 | | static const char *const archive_ident[] = |
| 289 | | { |
| 290 | | "Full title", |
| 291 | | "Software house/publisher", |
| 292 | | "Author(s)", |
| 293 | | "Year of publication", |
| 294 | | "Language", |
| 295 | | "Game/utility type", |
| 296 | | "Price", |
| 297 | | "Protection scheme/loader", |
| 298 | | "Origin", |
| 299 | | }; |
| 300 | | |
| 301 | | static const char *const hw_info[] = |
| 302 | | { |
| 303 | | "Tape runs on this machine / this hardware", |
| 304 | | "Tape needs this machine / this hardware", |
| 305 | | "Tape runs on this machine / this hardware, but does not require its special features", |
| 306 | | "Tape does not run on this machine / this hardware", |
| 307 | | }; |
| 308 | | |
| 309 | | /* Will go through blocks and calculate number of samples needed. |
| 310 | | If buffer is not NULL the sample data will also be written. */ |
| 311 | | static int tzx_cas_do_work( INT16 **buffer ) |
| 312 | | { |
| 313 | | int current_block = 0; |
| 314 | | int size = 0; |
| 315 | | |
| 316 | | wave_data = WAVE_LOW; |
| 317 | | |
| 318 | | while (current_block < block_count) |
| 319 | | { |
| 320 | | int pause_time; |
| 321 | | int data_size, text_size, total_size, i; |
| 322 | | int pilot, pilot_length, sync1, sync2; |
| 323 | | int bit0, bit1, bits_in_last_byte; |
| 324 | | UINT8 *cur_block = blocks[current_block]; |
| 325 | | UINT8 block_type = cur_block[0]; |
| 326 | | |
| 327 | | /* Uncomment this to include into error.log a list of the types each block */ |
| 328 | | // logerror("tzx_cas_fill_wave: block %d, block_type %02x\n", current_block, block_type); |
| 329 | | |
| 330 | | switch (block_type) |
| 331 | | { |
| 332 | | case 0x10: /* Standard Speed Data Block (.TAP block) */ |
| 333 | | pause_time = cur_block[1] + (cur_block[2] << 8); |
| 334 | | data_size = cur_block[3] + (cur_block[4] << 8); |
| 335 | | pilot_length = (cur_block[5] == 0x00) ? 8064 : 3220; |
| 336 | | size += tzx_cas_handle_block(buffer, &cur_block[5], pause_time, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8); |
| 337 | | current_block++; |
| 338 | | break; |
| 339 | | case 0x11: /* Turbo Loading Data Block */ |
| 340 | | pilot = cur_block[1] + (cur_block[2] << 8); |
| 341 | | sync1 = cur_block[3] + (cur_block[4] << 8); |
| 342 | | sync2 = cur_block[5] + (cur_block[6] << 8); |
| 343 | | bit0 = cur_block[7] + (cur_block[8] << 8); |
| 344 | | bit1 = cur_block[9] + (cur_block[10] << 8); |
| 345 | | pilot_length = cur_block[11] + (cur_block[12] << 8); |
| 346 | | bits_in_last_byte = cur_block[13]; |
| 347 | | pause_time = cur_block[14] + (cur_block[15] << 8); |
| 348 | | data_size = cur_block[16] + (cur_block[17] << 8) + (cur_block[18] << 16); |
| 349 | | size += tzx_cas_handle_block(buffer, &cur_block[19], pause_time, data_size, pilot, pilot_length, sync1, sync2, bit0, bit1, bits_in_last_byte); |
| 350 | | current_block++; |
| 351 | | break; |
| 352 | | case 0x12: /* Pure Tone */ |
| 353 | | pilot = cur_block[1] + (cur_block[2] << 8); |
| 354 | | pilot_length = cur_block[3] + (cur_block[4] << 8); |
| 355 | | size += tzx_cas_handle_block(buffer, cur_block, 0, 0, pilot, pilot_length, 0, 0, 0, 0, 0); |
| 356 | | current_block++; |
| 357 | | break; |
| 358 | | case 0x13: /* Sequence of Pulses of Different Lengths */ |
| 359 | | for (data_size = 0; data_size < cur_block[1]; data_size++) |
| 360 | | { |
| 361 | | pilot = cur_block[2 + 2 * data_size] + (cur_block[3 + 2 * data_size] << 8); |
| 362 | | size += tzx_cas_handle_block(buffer, cur_block, 0, 0, pilot, 1, 0, 0, 0, 0, 0); |
| 363 | | } |
| 364 | | current_block++; |
| 365 | | break; |
| 366 | | case 0x14: /* Pure Data Block */ |
| 367 | | bit0 = cur_block[1] + (cur_block[2] << 8); |
| 368 | | bit1 = cur_block[3] + (cur_block[4] << 8); |
| 369 | | bits_in_last_byte = cur_block[5]; |
| 370 | | pause_time = cur_block[6] + (cur_block[7] << 8); |
| 371 | | data_size = cur_block[8] + (cur_block[9] << 8) + (cur_block[10] << 16); |
| 372 | | size += tzx_cas_handle_block(buffer, &cur_block[11], pause_time, data_size, 0, 0, 0, 0, bit0, bit1, bits_in_last_byte); |
| 373 | | current_block++; |
| 374 | | break; |
| 375 | | case 0x20: /* Pause (Silence) or 'Stop the Tape' Command */ |
| 376 | | pause_time = cur_block[1] + (cur_block[2] << 8); |
| 377 | | if (pause_time == 0) |
| 378 | | { |
| 379 | | /* pause = 0 is used to let an emulator automagically stop the tape |
| 380 | | in MESS we do not do that, so we insert a 5 second pause. */ |
| 381 | | pause_time = 5000; |
| 382 | | } |
| 383 | | size += tzx_cas_handle_block(buffer, cur_block, pause_time, 0, 0, 0, 0, 0, 0, 0, 0); |
| 384 | | current_block++; |
| 385 | | break; |
| 386 | | case 0x16: /* C64 ROM Type Data Block */ // Deprecated in TZX 1.20 |
| 387 | | case 0x17: /* C64 Turbo Tape Data Block */ // Deprecated in TZX 1.20 |
| 388 | | case 0x34: /* Emulation Info */ // Deprecated in TZX 1.20 |
| 389 | | case 0x40: /* Snapshot Block */ // Deprecated in TZX 1.20 |
| 390 | | logerror("Deprecated block type (%02x) encountered.\n", block_type); |
| 391 | | logerror("Please look for an updated .tzx file.\n"); |
| 392 | | current_block++; |
| 393 | | break; |
| 394 | | case 0x30: /* Text Description */ |
| 395 | | ascii_block_common_log("Text Description Block", block_type); |
| 396 | | for (data_size = 0; data_size < cur_block[1]; data_size++) |
| 397 | | logerror("%c", cur_block[2 + data_size]); |
| 398 | | logerror("\n"); |
| 399 | | current_block++; |
| 400 | | break; |
| 401 | | case 0x31: /* Message Block */ |
| 402 | | ascii_block_common_log("Message Block", block_type); |
| 403 | | logerror("Expected duration of the message display: %02x\n", cur_block[1]); |
| 404 | | logerror("Message: \n"); |
| 405 | | for (data_size = 0; data_size < cur_block[2]; data_size++) |
| 406 | | { |
| 407 | | logerror("%c", cur_block[3 + data_size]); |
| 408 | | if (cur_block[3 + data_size] == 0x0d) |
| 409 | | logerror("\n"); |
| 410 | | } |
| 411 | | logerror("\n"); |
| 412 | | current_block++; |
| 413 | | break; |
| 414 | | case 0x32: /* Archive Info */ |
| 415 | | ascii_block_common_log("Archive Info Block", block_type); |
| 416 | | total_size = cur_block[1] + (cur_block[2] << 8); |
| 417 | | text_size = 0; |
| 418 | | for (data_size = 0; data_size < cur_block[3]; data_size++) // data_size = number of text blocks, in this case |
| 419 | | { |
| 420 | | if (cur_block[4 + text_size] < 0x09) |
| 421 | | logerror("%s: \n", archive_ident[cur_block[4 + text_size]]); |
| 422 | | else |
| 423 | | logerror("Comment(s): \n"); |
| 424 | | |
| 425 | | for (i = 0; i < cur_block[4 + text_size + 1]; i++) |
| 426 | | { |
| 427 | | logerror("%c", cur_block[4 + text_size + 2 + i]); |
| 428 | | } |
| 429 | | text_size += 2 + i; |
| 430 | | } |
| 431 | | logerror("\n"); |
| 432 | | if (text_size != total_size) |
| 433 | | logerror("Malformed Archive Info Block (Text length different from the declared one).\n Please verify your tape image.\n"); |
| 434 | | current_block++; |
| 435 | | break; |
| 436 | | case 0x33: /* Hardware Type */ |
| 437 | | ascii_block_common_log("Hardware Type Block", block_type); |
| 438 | | for (data_size = 0; data_size < cur_block[1]; data_size++) // data_size = number of hardware blocks, in this case |
| 439 | | { |
| 440 | | logerror("Hardware Type %02x - Hardware ID %02x - ", cur_block[2 + data_size * 3], cur_block[2 + data_size * 3 + 1]); |
| 441 | | logerror("%s \n ", hw_info[cur_block[2 + data_size * 3 + 2]]); |
| 442 | | } |
| 443 | | current_block++; |
| 444 | | break; |
| 445 | | case 0x35: /* Custom Info Block */ |
| 446 | | ascii_block_common_log("Custom Info Block", block_type); |
| 447 | | for (data_size = 0; data_size < 10; data_size++) |
| 448 | | { |
| 449 | | logerror("%c", cur_block[1 + data_size]); |
| 450 | | } |
| 451 | | logerror(":\n"); |
| 452 | | text_size = cur_block[11] + (cur_block[12] << 8) + (cur_block[13] << 16) + (cur_block[14] << 24); |
| 453 | | for (data_size = 0; data_size < text_size; data_size++) |
| 454 | | logerror("%c", cur_block[15 + data_size]); |
| 455 | | logerror("\n"); |
| 456 | | current_block++; |
| 457 | | break; |
| 458 | | case 0x5A: /* "Glue" Block */ |
| 459 | | logerror("Glue Block (type %02x) encountered.\n", block_type); |
| 460 | | logerror("Please use a .tzx handling utility to split the merged tape files.\n"); |
| 461 | | current_block++; |
| 462 | | break; |
| 463 | | case 0x15: /* Direct Recording */ |
| 464 | | case 0x18: /* CSW Recording */ |
| 465 | | case 0x19: /* Generalized Data Block */ |
| 466 | | case 0x21: /* Group Start */ |
| 467 | | case 0x22: /* Group End */ |
| 468 | | case 0x23: /* Jump To Block */ |
| 469 | | case 0x24: /* Loop Start */ |
| 470 | | case 0x25: /* Loop End */ |
| 471 | | case 0x26: /* Call Sequence */ |
| 472 | | case 0x27: /* Return From Sequence */ |
| 473 | | case 0x28: /* Select Block */ |
| 474 | | case 0x2A: /* Stop Tape if in 48K Mode */ |
| 475 | | case 0x2B: /* Set signal level */ |
| 476 | | default: |
| 477 | | logerror("Unsupported block type (%02x) encountered.\n", block_type); |
| 478 | | current_block++; |
| 479 | | break; |
| 480 | | } |
| 481 | | } |
| 482 | | return size; |
| 483 | | } |
| 484 | | |
| 485 | | static int tzx_cas_to_wav_size( const UINT8 *casdata, int caslen ) |
| 486 | | { |
| 487 | | int size = 0; |
| 488 | | |
| 489 | | /* Header size plus major and minor version number */ |
| 490 | | if (caslen < 10) |
| 491 | | { |
| 492 | | logerror("tzx_cas_to_wav_size: cassette image too small\n"); |
| 493 | | goto cleanup; |
| 494 | | } |
| 495 | | |
| 496 | | /* Check for correct header */ |
| 497 | | if (memcmp(casdata, TZX_HEADER, sizeof(TZX_HEADER))) |
| 498 | | { |
| 499 | | logerror("tzx_cas_to_wav_size: cassette image has incompatible header\n"); |
| 500 | | goto cleanup; |
| 501 | | } |
| 502 | | |
| 503 | | /* Check major version number in header */ |
| 504 | | if (casdata[0x08] > SUPPORTED_VERSION_MAJOR) |
| 505 | | { |
| 506 | | logerror("tzx_cas_to_wav_size: unsupported version\n"); |
| 507 | | goto cleanup; |
| 508 | | } |
| 509 | | |
| 510 | | tzx_cas_get_blocks(casdata, caslen); |
| 511 | | |
| 512 | | logerror("tzx_cas_to_wav_size: %d blocks found\n", block_count); |
| 513 | | |
| 514 | | if (block_count == 0) |
| 515 | | { |
| 516 | | logerror("tzx_cas_to_wav_size: no blocks found!\n"); |
| 517 | | goto cleanup; |
| 518 | | } |
| 519 | | |
| 520 | | size = tzx_cas_do_work(NULL); |
| 521 | | |
| 522 | | return size; |
| 523 | | |
| 524 | | cleanup: |
| 525 | | return -1; |
| 526 | | } |
| 527 | | |
| 528 | | static int tzx_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) |
| 529 | | { |
| 530 | | INT16 *p = buffer; |
| 531 | | int size = 0; |
| 532 | | t_scale = 1.0; |
| 533 | | size = tzx_cas_do_work(&p); |
| 534 | | return size; |
| 535 | | } |
| 536 | | |
| 537 | | static int cdt_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) |
| 538 | | { |
| 539 | | INT16 *p = buffer; |
| 540 | | int size = 0; |
| 541 | | t_scale = (40 / 35); /* scale to 4MHz */ |
| 542 | | size = tzx_cas_do_work(&p); |
| 543 | | return size; |
| 544 | | } |
| 545 | | |
| 546 | | static int tap_cas_to_wav_size( const UINT8 *casdata, int caslen ) |
| 547 | | { |
| 548 | | int size = 0; |
| 549 | | const UINT8 *p = casdata; |
| 550 | | |
| 551 | | while (p < casdata + caslen) |
| 552 | | { |
| 553 | | int data_size = p[0] + (p[1] << 8); |
| 554 | | int pilot_length = (p[2] == 0x00) ? 8064 : 3220; /* TZX specification */ |
| 555 | | // int pilot_length = (p[2] == 0x00) ? 8063 : 3223; /* worldofspectrum */ |
| 556 | | logerror("tap_cas_to_wav_size: Handling TAP block containing 0x%X bytes", data_size); |
| 557 | | p += 2; |
| 558 | | size += tzx_cas_handle_block(NULL, p, 1000, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8); |
| 559 | | logerror(", total size is now: %d\n", size); |
| 560 | | p += data_size; |
| 561 | | } |
| 562 | | return size; |
| 563 | | } |
| 564 | | |
| 565 | | static int tap_cas_fill_wave( INT16 *buffer, int length, UINT8 *bytes ) |
| 566 | | { |
| 567 | | INT16 *p = buffer; |
| 568 | | int size = 0; |
| 569 | | |
| 570 | | while (size < length) |
| 571 | | { |
| 572 | | int data_size = bytes[0] + (bytes[1] << 8); |
| 573 | | int pilot_length = (bytes[2] == 0x00) ? 8064 : 3220; /* TZX specification */ |
| 574 | | // int pilot_length = (bytes[2] == 0x00) ? 8063 : 3223; /* worldofspectrum */ |
| 575 | | logerror("tap_cas_fill_wave: Handling TAP block containing 0x%X bytes\n", data_size); |
| 576 | | bytes += 2; |
| 577 | | size += tzx_cas_handle_block(&p, bytes, 1000, data_size, 2168, pilot_length, 667, 735, 855, 1710, 8); |
| 578 | | bytes += data_size; |
| 579 | | } |
| 580 | | return size; |
| 581 | | } |
| 582 | | |
| 583 | | static const struct CassetteLegacyWaveFiller tzx_legacy_fill_wave = |
| 584 | | { |
| 585 | | tzx_cas_fill_wave, /* fill_wave */ |
| 586 | | -1, /* chunk_size */ |
| 587 | | 0, /* chunk_samples */ |
| 588 | | tzx_cas_to_wav_size, /* chunk_sample_calc */ |
| 589 | | TZX_WAV_FREQUENCY, /* sample_frequency */ |
| 590 | | 0, /* header_samples */ |
| 591 | | 0 /* trailer_samples */ |
| 592 | | }; |
| 593 | | |
| 594 | | static const struct CassetteLegacyWaveFiller tap_legacy_fill_wave = |
| 595 | | { |
| 596 | | tap_cas_fill_wave, /* fill_wave */ |
| 597 | | -1, /* chunk_size */ |
| 598 | | 0, /* chunk_samples */ |
| 599 | | tap_cas_to_wav_size, /* chunk_sample_calc */ |
| 600 | | TZX_WAV_FREQUENCY, /* sample_frequency */ |
| 601 | | 0, /* header_samples */ |
| 602 | | 0 /* trailer_samples */ |
| 603 | | }; |
| 604 | | |
| 605 | | static const struct CassetteLegacyWaveFiller cdt_legacy_fill_wave = |
| 606 | | { |
| 607 | | cdt_cas_fill_wave, /* fill_wave */ |
| 608 | | -1, /* chunk_size */ |
| 609 | | 0, /* chunk_samples */ |
| 610 | | tzx_cas_to_wav_size, /* chunk_sample_calc */ |
| 611 | | TZX_WAV_FREQUENCY, /* sample_frequency */ |
| 612 | | 0, /* header_samples */ |
| 613 | | 0 /* trailer_samples */ |
| 614 | | }; |
| 615 | | |
| 616 | | static casserr_t tzx_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) |
| 617 | | { |
| 618 | | return cassette_legacy_identify(cassette, opts, &tzx_legacy_fill_wave); |
| 619 | | } |
| 620 | | |
| 621 | | static casserr_t tap_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) |
| 622 | | { |
| 623 | | return cassette_legacy_identify(cassette, opts, &tap_legacy_fill_wave); |
| 624 | | } |
| 625 | | |
| 626 | | static casserr_t cdt_cassette_identify( cassette_image *cassette, struct CassetteOptions *opts ) |
| 627 | | { |
| 628 | | return cassette_legacy_identify(cassette, opts, &cdt_legacy_fill_wave); |
| 629 | | } |
| 630 | | |
| 631 | | static casserr_t tzx_cassette_load( cassette_image *cassette ) |
| 632 | | { |
| 633 | | return cassette_legacy_construct(cassette, &tzx_legacy_fill_wave); |
| 634 | | } |
| 635 | | |
| 636 | | static casserr_t tap_cassette_load( cassette_image *cassette ) |
| 637 | | { |
| 638 | | return cassette_legacy_construct(cassette, &tap_legacy_fill_wave); |
| 639 | | } |
| 640 | | |
| 641 | | static casserr_t cdt_cassette_load( cassette_image *cassette ) |
| 642 | | { |
| 643 | | return cassette_legacy_construct(cassette, &cdt_legacy_fill_wave); |
| 644 | | } |
| 645 | | |
| 646 | | static const struct CassetteFormat tzx_cassette_format = |
| 647 | | { |
| 648 | | "tzx", |
| 649 | | tzx_cassette_identify, |
| 650 | | tzx_cassette_load, |
| 651 | | NULL |
| 652 | | }; |
| 653 | | |
| 654 | | static const struct CassetteFormat tap_cassette_format = |
| 655 | | { |
| 656 | | "tap,blk", |
| 657 | | tap_cassette_identify, |
| 658 | | tap_cassette_load, |
| 659 | | NULL |
| 660 | | }; |
| 661 | | |
| 662 | | static const struct CassetteFormat cdt_cassette_format = |
| 663 | | { |
| 664 | | "cdt", |
| 665 | | cdt_cassette_identify, |
| 666 | | cdt_cassette_load, |
| 667 | | NULL |
| 668 | | }; |
| 669 | | |
| 670 | | CASSETTE_FORMATLIST_START(tzx_cassette_formats) |
| 671 | | CASSETTE_FORMAT(tzx_cassette_format) |
| 672 | | CASSETTE_FORMAT(tap_cassette_format) |
| 673 | | CASSETTE_FORMATLIST_END |
| 674 | | |
| 675 | | CASSETTE_FORMATLIST_START(cdt_cassette_formats) |
| 676 | | CASSETTE_FORMAT(cdt_cassette_format) |
| 677 | | CASSETTE_FORMATLIST_END |
| 678 | | |
src/mess/formats/coco_dsk.c
| r11457 | r11458 | |
| 1 | | /********************************************************************* |
| 2 | | |
| 3 | | formats/coco_dsk.c |
| 4 | | |
| 5 | | Tandy Color Computer / Dragon disk images |
| 6 | | |
| 7 | | *********************************************************************/ |
| 8 | | |
| 9 | | #include <string.h> |
| 10 | | #include <assert.h> |
| 11 | | #include <time.h> |
| 12 | | |
| 13 | | #include "osdepend.h" |
| 14 | | #include "formats/coco_dsk.h" |
| 15 | | #include "formats/basicdsk.h" |
| 16 | | #include "imageutl.h" |
| 17 | | |
| 18 | | /* ----------------------------------------------------------------------- |
| 19 | | * JVC (Jeff Vavasour CoCo) format |
| 20 | | * |
| 21 | | * Used by Jeff Vavasour's CoCo Emulators |
| 22 | | * |
| 23 | | * Documentation taken from Tim Linder's web site: |
| 24 | | * http://home.netcom.com/~tlindner/JVC.html |
| 25 | | * |
| 26 | | * A. Header length |
| 27 | | * The header length is determined by the file length modulo 256: |
| 28 | | * headerSize = fileLength % 256; |
| 29 | | * This means that the header is variable length and the minimum size |
| 30 | | * is zero bytes, and the maximum size of 255 bytes. |
| 31 | | * |
| 32 | | * B. Header |
| 33 | | * Here is a description of the header bytes: |
| 34 | | * Byte Offset Description Default |
| 35 | | * ----------- ----------------- ------- |
| 36 | | * 0 Sectors per track 18 |
| 37 | | * 1 Side count 1 |
| 38 | | * 2 Sector size code 1 |
| 39 | | * 3 First sector ID 1 |
| 40 | | * 4 Sector attribute flag 0 |
| 41 | | * |
| 42 | | * If the sector attribute flag is zero then the track count is determined |
| 43 | | * by the formula: |
| 44 | | * |
| 45 | | * (fileLength - headerSize) / (sectorsPerTrack * (128 << |
| 46 | | * sectorSizeCode)) / sideCount |
| 47 | | * |
| 48 | | * If the sector attribute flag is non zero then the track count is |
| 49 | | * determined by the more complex formula: |
| 50 | | * |
| 51 | | * (fileLength - headerSize) / (sectorsPerTrack * ((128 << |
| 52 | | * sectorSizeCode) + 1) ) / sideCount |
| 53 | | * |
| 54 | | * If the length of the header is to short to contain the geometry desired, |
| 55 | | * then the default values are assumed. If the header length is zero the all |
| 56 | | * of the geometry is assumed. When creating disk images it is desirable to |
| 57 | | * make the header length as short as possible. The header should only be |
| 58 | | * used to deviate from the default values. |
| 59 | | * |
| 60 | | * The sector data begins immediately after the header. If the header length |
| 61 | | * is zero then the sector data is at the beginning file. |
| 62 | | * |
| 63 | | * C. Sectors per track |
| 64 | | * This is the number of sectors per track (ones based). A value of 18 |
| 65 | | * means there are 18 sectors per track |
| 66 | | * |
| 67 | | * D. Side Count |
| 68 | | * This is the number of sides in the disk image. Values of 1 or 2 are |
| 69 | | * acceptable. If there are two sides then the tracks are interleaved. |
| 70 | | * The first track in the image file is track zero side 1, the second |
| 71 | | * track in the image file is track zero side 2. |
| 72 | | * |
| 73 | | * E. Sector size |
| 74 | | * The is the same value that is stored in the wd179x ID field to |
| 75 | | * determine sector size: |
| 76 | | * |
| 77 | | * 0x00 128 bytes |
| 78 | | * 0x01 256 bytes |
| 79 | | * 0x02 512 bytes |
| 80 | | * 0x03 1024 bytes |
| 81 | | * |
| 82 | | * Other values are undefined. Every sector in the disk image must be the |
| 83 | | * same size. |
| 84 | | * |
| 85 | | * F. First sector ID |
| 86 | | * This determines the first sector ID for each track. Each successive |
| 87 | | * sector adds one to the previous ID. If the first sector ID is 1, then |
| 88 | | * the second sector has an ID of 2, and the third has an ID of 3. |
| 89 | | * |
| 90 | | * G. Sector Attribute Flag |
| 91 | | * If this byte is non zero, then each sector contains an additional |
| 92 | | * byte prepended to the sector data. If the attribute flag is zero then |
| 93 | | * there are no extra bytes in front of the sector data. |
| 94 | | * |
| 95 | | * H. Sector attribute byte |
| 96 | | * This byte is put at the begining of every sector if the header flag |
| 97 | | * is turned on. The information this byte contains is the same as the |
| 98 | | * status register (of the wd179x) would contain when a 'Read Sector' |
| 99 | | * command was issued. The bit fields are defined as: |
| 100 | | * |
| 101 | | * Bit position: |
| 102 | | * --------------- |
| 103 | | * 7 6 5 4 3 2 1 0 |
| 104 | | * | | | | | | | | |
| 105 | | * | | | | | | | +--- Not used. Set to zero. |
| 106 | | * | | | | | | +----- Not used. Set to zero. |
| 107 | | * | | | | | +------- Not used. Set to zero. |
| 108 | | * | | | | +--------- Set on CRC error. |
| 109 | | * | | | +----------- Set if sector not found. |
| 110 | | * | | +------------- Record type: 1 - Deleted Data Mark, 0 - Data Mark. |
| 111 | | * | +--------------- Not Used. Set to zero. |
| 112 | | * +----------------- Not Used. Set to zero. |
| 113 | | * |
| 114 | | * ----------------------------------------------------------------------- */ |
| 115 | | |
| 116 | | static int coco_jvc_decode_header(floppy_image *floppy, UINT64 size, |
| 117 | | struct basicdsk_geometry *geometry) |
| 118 | | { |
| 119 | | UINT8 header[256]; |
| 120 | | UINT8 sector_attribute_flag; |
| 121 | | UINT16 physical_bytes_per_sector; |
| 122 | | UINT32 header_size, file_size; |
| 123 | | struct basicdsk_geometry dummy_geometry; |
| 124 | | |
| 125 | | if (geometry) |
| 126 | | memset(geometry, 0, sizeof(*geometry)); |
| 127 | | else |
| 128 | | geometry = &dummy_geometry; |
| 129 | | |
| 130 | | if (size > 0xFFFFFFFF) |
| 131 | | return -1; |
| 132 | | file_size = (UINT32) size; |
| 133 | | |
| 134 | | /* read the header */ |
| 135 | | header_size = (UINT32) file_size % 0x100; |
| 136 | | floppy_image_read(floppy, header, 0, header_size); |
| 137 | | geometry->offset = header_size; |
| 138 | | |
| 139 | | /* byte offset 0 - sectors per track */ |
| 140 | | geometry->sectors = (header_size > 0) ? header[0] : 18; |
| 141 | | if (geometry->sectors <= 0) |
| 142 | | return -1; |
| 143 | | |
| 144 | | /* byte offset 1 - side count */ |
| 145 | | geometry->heads = (header_size > 1) ? header[1] : 1; |
| 146 | | if (geometry->heads <= 0) |
| 147 | | return -1; |
| 148 | | |
| 149 | | /* byte offset 2 - sector size code */ |
| 150 | | geometry->sector_length = 128 << ((header_size > 2) ? header[2] : 1); |
| 151 | | if (geometry->sector_length <= 0) |
| 152 | | return -1; |
| 153 | | |
| 154 | | /* byte offset 3 - first sector ID */ |
| 155 | | geometry->first_sector_id = (header_size > 3) ? header[3] : 1; |
| 156 | | |
| 157 | | /* byte offset 4 - sector attribute flag */ |
| 158 | | sector_attribute_flag = (header_size > 4) ? header[4] : 0; |
| 159 | | if (sector_attribute_flag != 0) |
| 160 | | return -1; /* we do not support sector attribute flags */ |
| 161 | | |
| 162 | | physical_bytes_per_sector = geometry->sector_length; |
| 163 | | if (sector_attribute_flag) |
| 164 | | physical_bytes_per_sector++; |
| 165 | | |
| 166 | | geometry->tracks = (file_size - header_size) / geometry->sectors / geometry->heads / physical_bytes_per_sector; |
| 167 | | |
| 168 | | /* do we have an oddball size? reject this file if not */ |
| 169 | | if ((file_size - header_size) % physical_bytes_per_sector) |
| 170 | | return -1; |
| 171 | | |
| 172 | | /* minimum of 35 tracks; support degenerate JVC files */ |
| 173 | | if (geometry->tracks < 35) |
| 174 | | geometry->tracks = 35; |
| 175 | | |
| 176 | | return 0; |
| 177 | | } |
| 178 | | |
| 179 | | |
| 180 | | |
| 181 | | static FLOPPY_IDENTIFY(coco_jvc_identify) |
| 182 | | { |
| 183 | | UINT64 size; |
| 184 | | size = floppy_image_size(floppy); |
| 185 | | *vote = coco_jvc_decode_header(floppy, size, NULL) ? 0 : 100; |
| 186 | | return FLOPPY_ERROR_SUCCESS; |
| 187 | | } |
| 188 | | |
| 189 | | |
| 190 | | |
| 191 | | static FLOPPY_CONSTRUCT(coco_jvc_construct) |
| 192 | | { |
| 193 | | struct basicdsk_geometry geometry; |
| 194 | | UINT8 header[5]; |
| 195 | | size_t header_size; |
| 196 | | |
| 197 | | if (params) |
| 198 | | { |
| 199 | | /* create */ |
| 200 | | memset(&geometry, 0, sizeof(geometry)); |
| 201 | | geometry.heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 202 | | geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 203 | | geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 204 | | geometry.first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID); |
| 205 | | geometry.sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 206 | | |
| 207 | | header[0] = (UINT8) geometry.sectors; |
| 208 | | header[1] = (UINT8) geometry.heads; |
| 209 | | header[2] = (UINT8) compute_log2(geometry.sector_length) - 7; |
| 210 | | header[3] = (UINT8) geometry.first_sector_id; |
| 211 | | header[4] = 0; |
| 212 | | |
| 213 | | /* now that we have the header computed, figure out the header size */ |
| 214 | | header_size = 0; |
| 215 | | if (header[0] != 18) |
| 216 | | header_size = 1; |
| 217 | | if (header[1] != 1) |
| 218 | | header_size = 2; |
| 219 | | if (header[2] != 1) |
| 220 | | header_size = 3; |
| 221 | | if (header[3] != 1) |
| 222 | | header_size = 4; |
| 223 | | if (header[4] != 0) |
| 224 | | header_size = 5; |
| 225 | | |
| 226 | | geometry.offset = header_size; |
| 227 | | |
| 228 | | floppy_image_write(floppy, header, 0, header_size); |
| 229 | | } |
| 230 | | else |
| 231 | | { |
| 232 | | /* load */ |
| 233 | | if (coco_jvc_decode_header(floppy, floppy_image_size(floppy), &geometry)) |
| 234 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 235 | | } |
| 236 | | return basicdsk_construct(floppy, &geometry); |
| 237 | | } |
| 238 | | |
| 239 | | |
| 240 | | /* ----------------------------------------------------------------------- |
| 241 | | * OS-9 file format |
| 242 | | * |
| 243 | | * This file format is largely a hack because there are a large amount of |
| 244 | | * disk images that do not have geometry image separate from the disk image |
| 245 | | * itself. So we support OS-9 images with are simply basic disks whose |
| 246 | | * geometry is determined by the disk image. |
| 247 | | * |
| 248 | | * OS-9 images identified by an LSN; which are simply blocks of 256 bytes |
| 249 | | * |
| 250 | | * LSN0 |
| 251 | | * Byte size use |
| 252 | | * $00 3 sectors on disk |
| 253 | | * $03 1 track size in sectors |
| 254 | | * $04 2 bytes in allocation bit map; typically 1bit/sector so for |
| 255 | | * 35 tracks of 18 sectors each that's $4E (single sided disk) |
| 256 | | * 40 tracks per side, 18 sectors each = $B4 |
| 257 | | * $06 2 sectors per bit in allocation map; normally 1 |
| 258 | | * $08 3 LSN of root directory; normally 2 but depends on size of |
| 259 | | * $04 value |
| 260 | | * $0B 2 owner's user number; normally 0 |
| 261 | | * $0D 1 disk attributes; normally $FF |
| 262 | | * $0E 2 psuedo random number for identification |
| 263 | | * $10 1 disk format; typical is 3 |
| 264 | | * %00000001 0=single side 1=double side |
| 265 | | * %00000010 0=single density (non Coco) 1=double density |
| 266 | | * %00000100 0=48tracks/inch 1=96tracks/inch |
| 267 | | * $11 2 sectors per track; normal is $12 skip several not needed |
| 268 | | * for Format |
| 269 | | * $1A 5 date of creation Y:M:D:H:M |
| 270 | | * $1F 32 ASCII name of disk, last letter has $80 added to it, |
| 271 | | * the full 32 bytes do not need to be used. |
| 272 | | * |
| 273 | | * Allocation bit map, fill with zeros and set bits from low to high as |
| 274 | | * sectors are used. So, for a fresh disk sectors LSN0,LSN1,LSN2, and LSN3 |
| 275 | | * will be in use so the first byte will be $FF $C0 and all others |
| 276 | | * in the map are $00 |
| 277 | | * |
| 278 | | * Root directory LSN2 |
| 279 | | * Byte size use |
| 280 | | * $00 1 attributes will be $BF |
| 281 | | * $01 2 owners ID will be $0000 |
| 282 | | * $03 5 date last modified will be creation date Y:M:D:H:M |
| 283 | | * $08 1 link count; set to $02 |
| 284 | | * $09 4 file size in bytes, set to $40 |
| 285 | | * $0D 3 date created Y:M:D |
| 286 | | * $10 3 block LSN set to current sector number+1 ie $03 in this case |
| 287 | | * $13 2 size in sectors of directory block, set to $07 |
| 288 | | * All other bytes in sector set to $00 |
| 289 | | * LSN3 first sector of directory with names |
| 290 | | * Fill sector with all $00 and then set listed bytes |
| 291 | | * $00 2 value $2EAE which is .. with last byte+$80 |
| 292 | | * $1F 1 value $02 LSN for start of this directory as there is |
| 293 | | * none higher in tree |
| 294 | | * $20 1 value $AE which is . with $80 added |
| 295 | | * $3F 1 value $02 LSN for start of this directory |
| 296 | | * ----------------------------------------------------------------------- */ |
| 297 | | |
| 298 | | static floperr_t coco_os9_readheader(floppy_image *floppy, struct basicdsk_geometry *geometry) |
| 299 | | { |
| 300 | | UINT8 header[0x20]; |
| 301 | | int total_sectors; |
| 302 | | |
| 303 | | floppy_image_read(floppy, header, 0, sizeof(header)); |
| 304 | | |
| 305 | | total_sectors = (header[0x00] << 16) | (header[0x01] << 8) | header[0x02]; |
| 306 | | |
| 307 | | memset(geometry, 0, sizeof(*geometry)); |
| 308 | | geometry->first_sector_id = 1; |
| 309 | | geometry->sector_length = 256; |
| 310 | | geometry->sectors = (header[0x11] << 8) + header[0x12]; |
| 311 | | geometry->heads = (header[0x10] & 0x01) ? 2 : 1; |
| 312 | | |
| 313 | | if (!geometry->sectors) |
| 314 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 315 | | |
| 316 | | geometry->tracks = total_sectors / geometry->sectors / geometry->heads; |
| 317 | | |
| 318 | | if (total_sectors != geometry->tracks * geometry->sectors * geometry->heads) |
| 319 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 320 | | |
| 321 | | return FLOPPY_ERROR_SUCCESS; |
| 322 | | } |
| 323 | | |
| 324 | | |
| 325 | | |
| 326 | | static floperr_t coco_os9_post_format(floppy_image *floppy, option_resolution *params) |
| 327 | | { |
| 328 | | UINT8 header[0x0400]; |
| 329 | | floperr_t err; |
| 330 | | time_t t; |
| 331 | | struct tm *ltime; |
| 332 | | int heads, tracks, sectors, total_sectors; |
| 333 | | |
| 334 | | heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 335 | | tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 336 | | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 337 | | total_sectors = heads * tracks * sectors; |
| 338 | | |
| 339 | | /* write the initial header */ |
| 340 | | time(&t); |
| 341 | | ltime = localtime(&t); |
| 342 | | |
| 343 | | memset(&header, 0, sizeof(header)); |
| 344 | | header[0x0000] = (UINT8) (total_sectors >> 16); |
| 345 | | header[0x0001] = (UINT8) (total_sectors >> 8); |
| 346 | | header[0x0002] = (UINT8) (total_sectors >> 0); |
| 347 | | header[0x0003] = (UINT8) sectors; |
| 348 | | header[0x0004] = (UINT8) (((total_sectors + 7) / 8) >> 8); |
| 349 | | header[0x0005] = (UINT8) (((total_sectors + 7) / 8) >> 0); |
| 350 | | header[0x0006] = 0x00; |
| 351 | | header[0x0007] = 0x01; |
| 352 | | header[0x0008] = 0x00; |
| 353 | | header[0x0009] = 0x00; |
| 354 | | header[0x000a] = 0x02; |
| 355 | | header[0x000b] = 0x00; |
| 356 | | header[0x000c] = 0x00; |
| 357 | | header[0x000d] = 0xff; |
| 358 | | header[0x000e] = floppy_random_byte(floppy); |
| 359 | | header[0x000f] = floppy_random_byte(floppy); |
| 360 | | header[0x0010] = (heads == 2) ? 3 : 2; |
| 361 | | header[0x0011] = (UINT8) (sectors >> 8); |
| 362 | | header[0x0012] = (UINT8) (sectors >> 0); |
| 363 | | header[0x001A] = (UINT8) ltime->tm_year; |
| 364 | | header[0x001B] = (UINT8) ltime->tm_mon + 1; |
| 365 | | header[0x001C] = (UINT8) ltime->tm_mday; |
| 366 | | header[0x001D] = (UINT8) ltime->tm_hour; |
| 367 | | header[0x001E] = (UINT8) ltime->tm_min; |
| 368 | | header[0x001F] = 0xA0; |
| 369 | | header[0x0100] = 0xFF; |
| 370 | | header[0x0101] = 0xC0; |
| 371 | | header[0x0200] = 0xBF; |
| 372 | | header[0x0201] = 0x00; |
| 373 | | header[0x0202] = 0x00; |
| 374 | | header[0x0203] = (UINT8) ltime->tm_year; |
| 375 | | header[0x0204] = (UINT8) ltime->tm_mon + 1; |
| 376 | | header[0x0205] = (UINT8) ltime->tm_mday; |
| 377 | | header[0x0206] = (UINT8) ltime->tm_hour; |
| 378 | | header[0x0207] = (UINT8) ltime->tm_min; |
| 379 | | header[0x0208] = 0x02; |
| 380 | | header[0x0209] = 0x00; |
| 381 | | header[0x020A] = 0x00; |
| 382 | | header[0x020B] = 0x00; |
| 383 | | header[0x020C] = 0x40; |
| 384 | | header[0x020D] = (UINT8) (ltime->tm_year % 100); |
| 385 | | header[0x020E] = (UINT8) ltime->tm_mon; |
| 386 | | header[0x020F] = (UINT8) ltime->tm_mday; |
| 387 | | header[0x0210] = 0x00; |
| 388 | | header[0x0211] = 0x00; |
| 389 | | header[0x0212] = 0x03; |
| 390 | | header[0x0213] = 0x00; |
| 391 | | header[0x0214] = 0x07; |
| 392 | | header[0x0300] = 0x2E; |
| 393 | | header[0x0301] = 0xAE; |
| 394 | | header[0x031F] = 0x02; |
| 395 | | header[0x0320] = 0xAE; |
| 396 | | header[0x033F] = 0x02; |
| 397 | | |
| 398 | | if (total_sectors % 8) |
| 399 | | header[0x0100 + (total_sectors / 8)] = 0xFF >> (total_sectors % 8); |
| 400 | | |
| 401 | | err = floppy_write_sector(floppy, 0, 0, 1, 0, &header[0x0000], 256, 0); |
| 402 | | if (err) |
| 403 | | return err; |
| 404 | | |
| 405 | | err = floppy_write_sector(floppy, 0, 0, 2, 0, &header[0x0100], 256, 0); |
| 406 | | if (err) |
| 407 | | return err; |
| 408 | | |
| 409 | | err = floppy_write_sector(floppy, 0, 0, 3, 0, &header[0x0200], 256, 0); |
| 410 | | if (err) |
| 411 | | return err; |
| 412 | | |
| 413 | | err = floppy_write_sector(floppy, 0, 0, 4, 0, &header[0x0300], 256, 0); |
| 414 | | if (err) |
| 415 | | return err; |
| 416 | | |
| 417 | | return FLOPPY_ERROR_SUCCESS; |
| 418 | | } |
| 419 | | |
| 420 | | |
| 421 | | |
| 422 | | static FLOPPY_IDENTIFY(coco_os9_identify) |
| 423 | | { |
| 424 | | struct basicdsk_geometry geometry; |
| 425 | | *vote = coco_os9_readheader(floppy, &geometry) ? 0 : 100; |
| 426 | | return FLOPPY_ERROR_SUCCESS; |
| 427 | | } |
| 428 | | |
| 429 | | |
| 430 | | |
| 431 | | static FLOPPY_CONSTRUCT(coco_os9_construct) |
| 432 | | { |
| 433 | | floperr_t err; |
| 434 | | struct basicdsk_geometry geometry; |
| 435 | | |
| 436 | | if (params) |
| 437 | | { |
| 438 | | /* create */ |
| 439 | | memset(&geometry, 0, sizeof(geometry)); |
| 440 | | geometry.heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 441 | | geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 442 | | geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 443 | | geometry.first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID); |
| 444 | | geometry.sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 445 | | } |
| 446 | | else |
| 447 | | { |
| 448 | | /* open */ |
| 449 | | err = coco_os9_readheader(floppy, &geometry); |
| 450 | | if (err) |
| 451 | | return err; |
| 452 | | } |
| 453 | | |
| 454 | | /* actually construct the image */ |
| 455 | | err = basicdsk_construct(floppy, &geometry); |
| 456 | | floppy_callbacks(floppy)->post_format = coco_os9_post_format; |
| 457 | | return err; |
| 458 | | } |
| 459 | | |
| 460 | | |
| 461 | | |
| 462 | | /* ----------------------------------------------------------------------- |
| 463 | | * VDK file format |
| 464 | | * |
| 465 | | * Used by Paul Burgin's PC-Dragon emulator |
| 466 | | * |
| 467 | | * Offset Bytes Field Description |
| 468 | | * ------ ----- ------------ ----------- |
| 469 | | * 0 1 magic1 Signature byte 1 ('d') |
| 470 | | * 1 1 magic2 Signature byte 2 ('k') |
| 471 | | * 2 2 header_len Total header length (little endian) |
| 472 | | * 4 1 ver_actual Version of the VDK format (0x10) |
| 473 | | * 5 1 ver_compat Backwards compatibility version (0x10) |
| 474 | | * 6 1 source_id Identify of the file source |
| 475 | | * 7 1 source_ver Version of the file source |
| 476 | | * 8 1 tracks Number of tracks |
| 477 | | * 9 1 sides Number of sides (1-2) |
| 478 | | * 10 1 flags Various flags |
| 479 | | * bit 0: Write protect |
| 480 | | * bit 1: A Lock |
| 481 | | * bit 2: F Lock |
| 482 | | * bit 3: Disk set |
| 483 | | * 11 1 compression Compression flags (bits 0-2) and name length |
| 484 | | * ----------------------------------------------------------------------- */ |
| 485 | | |
| 486 | | |
| 487 | | |
| 488 | | static int coco_vdk_decode_header(floppy_image *floppy, struct basicdsk_geometry *geometry) |
| 489 | | { |
| 490 | | UINT8 header[12]; |
| 491 | | int heads, tracks, sectors, sector_length, offset; |
| 492 | | UINT64 size; |
| 493 | | |
| 494 | | size = floppy_image_size(floppy); |
| 495 | | |
| 496 | | floppy_image_read(floppy, header, 0, sizeof(header)); |
| 497 | | |
| 498 | | if (header[0] != 'd') |
| 499 | | return -1; |
| 500 | | if (header[1] != 'k') |
| 501 | | return -1; |
| 502 | | if (header[5] != 0x10) |
| 503 | | return -1; |
| 504 | | if (header[11] & 0x07) |
| 505 | | return -1; |
| 506 | | |
| 507 | | heads = header[9]; |
| 508 | | tracks = header[8]; |
| 509 | | sectors = 18; |
| 510 | | sector_length = 0x100; |
| 511 | | |
| 512 | | offset = ((int) header[3]) * 0x100 + header[2]; |
| 513 | | |
| 514 | | if (size != (heads * tracks * sectors * sector_length + offset)) |
| 515 | | return -1; |
| 516 | | |
| 517 | | if (geometry) |
| 518 | | { |
| 519 | | memset(geometry, 0, sizeof(*geometry)); |
| 520 | | geometry->heads = heads; |
| 521 | | geometry->tracks = tracks; |
| 522 | | geometry->sectors = sectors; |
| 523 | | geometry->first_sector_id = 1; |
| 524 | | geometry->sector_length = sector_length; |
| 525 | | geometry->offset = offset; |
| 526 | | } |
| 527 | | return 0; |
| 528 | | } |
| 529 | | |
| 530 | | |
| 531 | | |
| 532 | | static FLOPPY_IDENTIFY(coco_vdk_identify) |
| 533 | | { |
| 534 | | *vote = coco_vdk_decode_header(floppy, NULL) ? 0 : 100; |
| 535 | | return FLOPPY_ERROR_SUCCESS; |
| 536 | | } |
| 537 | | |
| 538 | | |
| 539 | | |
| 540 | | static FLOPPY_CONSTRUCT(coco_vdk_construct) |
| 541 | | { |
| 542 | | struct basicdsk_geometry geometry; |
| 543 | | UINT8 header[12]; |
| 544 | | |
| 545 | | if (params) |
| 546 | | { |
| 547 | | /* create */ |
| 548 | | memset(&geometry, 0, sizeof(geometry)); |
| 549 | | geometry.heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 550 | | geometry.tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 551 | | geometry.sectors = 18; |
| 552 | | geometry.first_sector_id = 1; |
| 553 | | geometry.sector_length = 256; |
| 554 | | geometry.offset = sizeof(header); |
| 555 | | |
| 556 | | memset(&header, 0, sizeof(header)); |
| 557 | | header[0] = 'd'; |
| 558 | | header[1] = 'k'; |
| 559 | | header[2] = (UINT8) (sizeof(header) >> 0); |
| 560 | | header[3] = (UINT8) (sizeof(header) >> 8); |
| 561 | | header[4] = 0x10; |
| 562 | | header[5] = 0x10; |
| 563 | | header[8] = (UINT8) geometry.tracks; |
| 564 | | header[9] = (UINT8) geometry.heads; |
| 565 | | |
| 566 | | floppy_image_write(floppy, header, 0, sizeof(header)); |
| 567 | | } |
| 568 | | else |
| 569 | | { |
| 570 | | /* load */ |
| 571 | | if (coco_vdk_decode_header(floppy, &geometry)) |
| 572 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 573 | | } |
| 574 | | return basicdsk_construct(floppy, &geometry); |
| 575 | | } |
| 576 | | |
| 577 | | |
| 578 | | |
| 579 | | /* ----------------------------------------------------------------------- |
| 580 | | * DMK file format |
| 581 | | * |
| 582 | | * David M. Keil's disk image format is aptly called an 'on disk' image |
| 583 | | * format. This means that whatever written to the disk is enocded into |
| 584 | | * the image file. IDAMS, sector headers, traling CRCs, and intra sector |
| 585 | | * spacing. * |
| 586 | | * |
| 587 | | * HEADER DESCRIPTION: |
| 588 | | * |
| 589 | | * Offset Bytes Field Description |
| 590 | | * ------ ----- ------------ ----------- |
| 591 | | * 0 1 write_prot 0xff = Writed Protected, 0x00 = R/W |
| 592 | | * 1 1 tracks Number of tracks |
| 593 | | * 2 2 track_length Bytes per track (little endian) |
| 594 | | * 4 1 disk_options Miscellaneous flags |
| 595 | | * bit 0-3: Unused |
| 596 | | * bit 4: 1=single sided 0=dbl |
| 597 | | * bit 5: Unused |
| 598 | | * bit 6: Single density? |
| 599 | | * bit 6: Ignore density flags? |
| 600 | | * 5 7 reserved Reserved for future use |
| 601 | | * 12 4 real_disk_code If this is 0x12345678 (little endian) |
| 602 | | * then access a real disk drive |
| 603 | | * (unsupported) |
| 604 | | * |
| 605 | | * Each track begins with a track TOC, consisting of 64 little endian 16-bit |
| 606 | | * integers. Each integer has the following format: |
| 607 | | * bit 0-13: Offset from begining of track to 'FE' byte of IDAM |
| 608 | | * Note these are always sorted from first to last. All empty |
| 609 | | * entries are 0x00 |
| 610 | | * bit 14: Undefined (reserved) |
| 611 | | * bit 15: Sector double density (0=SD 1=DD) |
| 612 | | * ----------------------------------------------------------------------- */ |
| 613 | | |
| 614 | | struct dmk_tag |
| 615 | | { |
| 616 | | int heads; |
| 617 | | int tracks; |
| 618 | | UINT32 track_size; |
| 619 | | }; |
| 620 | | |
| 621 | | #define DMK_HEADER_LEN 16 |
| 622 | | #define DMK_TOC_LEN 64 |
| 623 | | #define DMK_IDAM_LENGTH 7 |
| 624 | | #define DMK_DATA_GAP 80 |
| 625 | | #define DMK_LEAD_IN 32 |
| 626 | | #define DMK_EXTRA_TRACK_LENGTH 156 |
| 627 | | |
| 628 | | #define dmk_idam_type(x) (x)[0] |
| 629 | | #define dmk_idam_track(x) (x)[1] |
| 630 | | #define dmk_idam_side(x) (x)[2] |
| 631 | | #define dmk_idam_sector(x) (x)[3] |
| 632 | | #define dmk_idam_sectorlength(x) (x)[4] |
| 633 | | #define dmk_idam_crc(x) (((x)[5] << 8) + (x)[6]) |
| 634 | | #define dmk_idam_set_crc(x, crc) (x)[5] = ((crc) >> 8); (x)[6] = ((crc) >> 0); |
| 635 | | |
| 636 | | |
| 637 | | static struct dmk_tag *get_dmk_tag(floppy_image *floppy) |
| 638 | | { |
| 639 | | return (dmk_tag *)floppy_tag(floppy); |
| 640 | | } |
| 641 | | |
| 642 | | |
| 643 | | static floperr_t coco_dmk_get_offset(floppy_image *floppy, int head, int track, UINT64 *offset) |
| 644 | | { |
| 645 | | struct dmk_tag *tag = get_dmk_tag(floppy); |
| 646 | | |
| 647 | | if ((head < 0) || (head >= tag->heads) || (track < 0) || (track >= tag->tracks)) |
| 648 | | return FLOPPY_ERROR_SEEKERROR; |
| 649 | | |
| 650 | | *offset = track; |
| 651 | | *offset *= tag->heads; |
| 652 | | *offset += head; |
| 653 | | *offset *= tag->track_size; |
| 654 | | *offset += DMK_HEADER_LEN; |
| 655 | | return FLOPPY_ERROR_SUCCESS; |
| 656 | | } |
| 657 | | |
| 658 | | |
| 659 | | |
| 660 | | static UINT32 coco_dmk_min_track_size(int sectors, int sector_length) |
| 661 | | { |
| 662 | | int sector_physical_length; |
| 663 | | sector_physical_length = 8 + 3 + DMK_IDAM_LENGTH + 22 + 12 + 3 + 1 + sector_length + 2 + 24; |
| 664 | | return DMK_TOC_LEN * 2 + DMK_LEAD_IN + (sectors * sector_physical_length); |
| 665 | | } |
| 666 | | |
| 667 | | |
| 668 | | |
| 669 | | static floperr_t coco_dmk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 670 | | { |
| 671 | | floperr_t err; |
| 672 | | UINT64 track_offset; |
| 673 | | |
| 674 | | err = coco_dmk_get_offset(floppy, head, track, &track_offset); |
| 675 | | if (err) |
| 676 | | return err; |
| 677 | | |
| 678 | | floppy_image_read(floppy, buffer, offset + track_offset, buflen); |
| 679 | | return FLOPPY_ERROR_SUCCESS; |
| 680 | | } |
| 681 | | |
| 682 | | |
| 683 | | |
| 684 | | static floperr_t coco_dmk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 685 | | { |
| 686 | | floperr_t err; |
| 687 | | UINT64 track_offset; |
| 688 | | |
| 689 | | err = coco_dmk_get_offset(floppy, head, track, &track_offset); |
| 690 | | if (err) |
| 691 | | return err; |
| 692 | | |
| 693 | | floppy_image_write(floppy, buffer, offset + track_offset, buflen); |
| 694 | | return FLOPPY_ERROR_SUCCESS; |
| 695 | | } |
| 696 | | |
| 697 | | |
| 698 | | |
| 699 | | static floperr_t coco_dmk_get_track_data_offset(floppy_image *floppy, int head, int track, UINT64 *offset) |
| 700 | | { |
| 701 | | *offset = DMK_TOC_LEN + 1; |
| 702 | | return FLOPPY_ERROR_SUCCESS; |
| 703 | | } |
| 704 | | |
| 705 | | |
| 706 | | |
| 707 | | static floperr_t coco_dmk_format_track(floppy_image *floppy, int head, int track, option_resolution *params) |
| 708 | | { |
| 709 | | int sectors; |
| 710 | | int sector_length; |
| 711 | | int interleave; |
| 712 | | int first_sector_id; |
| 713 | | floperr_t err; |
| 714 | | int physical_sector; |
| 715 | | int logical_sector; |
| 716 | | int track_position; |
| 717 | | UINT16 idam_offset; |
| 718 | | UINT16 crc; |
| 719 | | UINT8 *track_data; |
| 720 | | void *track_data_v; |
| 721 | | UINT32 max_track_size; |
| 722 | | int *sector_map = NULL; |
| 723 | | |
| 724 | | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 725 | | sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 726 | | interleave = option_resolution_lookup_int(params, PARAM_INTERLEAVE); |
| 727 | | first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID); |
| 728 | | |
| 729 | | max_track_size = get_dmk_tag(floppy)->track_size; |
| 730 | | |
| 731 | | if (sectors > DMK_TOC_LEN) |
| 732 | | { |
| 733 | | err = FLOPPY_ERROR_INTERNAL; |
| 734 | | goto done; |
| 735 | | } |
| 736 | | |
| 737 | | if (max_track_size < coco_dmk_min_track_size(sectors, sector_length)) |
| 738 | | { |
| 739 | | err = FLOPPY_ERROR_NOSPACE; |
| 740 | | goto done; |
| 741 | | } |
| 742 | | |
| 743 | | err = floppy_load_track(floppy, head, track, TRUE, &track_data_v, NULL); |
| 744 | | if (err) |
| 745 | | goto done; |
| 746 | | track_data = (UINT8 *) track_data_v; |
| 747 | | |
| 748 | | /* set up sector map */ |
| 749 | | sector_map = (int*)malloc(sectors * sizeof(*sector_map)); |
| 750 | | if (!sector_map) |
| 751 | | { |
| 752 | | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 753 | | goto done; |
| 754 | | } |
| 755 | | memset(sector_map, 0xFF, sectors * sizeof(*sector_map)); |
| 756 | | |
| 757 | | physical_sector = 0; |
| 758 | | for (logical_sector = 0; logical_sector < sectors; logical_sector++) |
| 759 | | { |
| 760 | | while(sector_map[physical_sector] >= 0) |
| 761 | | { |
| 762 | | physical_sector++; |
| 763 | | physical_sector %= sectors; |
| 764 | | } |
| 765 | | |
| 766 | | sector_map[physical_sector] = logical_sector + first_sector_id; |
| 767 | | physical_sector += interleave + 1; |
| 768 | | physical_sector %= sectors; |
| 769 | | } |
| 770 | | |
| 771 | | /* set up track table of contents */ |
| 772 | | physical_sector = 0; |
| 773 | | track_position = DMK_TOC_LEN * 2 + DMK_LEAD_IN; |
| 774 | | while(physical_sector < DMK_TOC_LEN) |
| 775 | | { |
| 776 | | if (physical_sector >= sectors) |
| 777 | | { |
| 778 | | /* no more sectors */ |
| 779 | | idam_offset = 0; |
| 780 | | } |
| 781 | | else |
| 782 | | { |
| 783 | | /* this is a sector */ |
| 784 | | logical_sector = sector_map[physical_sector]; |
| 785 | | |
| 786 | | /* write the sector */ |
| 787 | | memset(&track_data[track_position], 0x00, 8); |
| 788 | | track_position += 8; |
| 789 | | |
| 790 | | memset(&track_data[track_position], 0xA1, 3); |
| 791 | | track_position += 3; |
| 792 | | |
| 793 | | idam_offset = track_position | 0x8000; |
| 794 | | dmk_idam_type( &track_data[track_position]) = 0xFE; |
| 795 | | dmk_idam_track( &track_data[track_position]) = track; |
| 796 | | dmk_idam_side( &track_data[track_position]) = head; |
| 797 | | dmk_idam_sector( &track_data[track_position]) = logical_sector; |
| 798 | | dmk_idam_sectorlength( &track_data[track_position]) = compute_log2(sector_length / 128); |
| 799 | | crc = ccitt_crc16(0xcdb4, &track_data[track_position], DMK_IDAM_LENGTH - 2); |
| 800 | | dmk_idam_set_crc( &track_data[track_position], crc); |
| 801 | | track_position += DMK_IDAM_LENGTH; |
| 802 | | |
| 803 | | memset(&track_data[track_position], 0x4E, 22); |
| 804 | | track_position += 22; |
| 805 | | |
| 806 | | memset(&track_data[track_position], 0x00, 12); |
| 807 | | track_position += 12; |
| 808 | | |
| 809 | | memset(&track_data[track_position], 0xA1, 3); |
| 810 | | track_position += 3; |
| 811 | | |
| 812 | | /* write sector body */ |
| 813 | | track_data[track_position] = 0xFB; |
| 814 | | memset(&track_data[track_position + 1], floppy_get_filler(floppy), sector_length); |
| 815 | | crc = ccitt_crc16(0xcdb4, &track_data[track_position], sector_length + 1); |
| 816 | | track_data[track_position + sector_length + 1] = (UINT8) (crc >> 8); |
| 817 | | track_data[track_position + sector_length + 2] = (UINT8) (crc >> 0); |
| 818 | | track_position += sector_length + 3; |
| 819 | | |
| 820 | | /* write sector footer */ |
| 821 | | memset(&track_data[track_position], 0x4E, 24); |
| 822 | | track_position += 24; |
| 823 | | } |
| 824 | | |
| 825 | | /* write the TOC entry */ |
| 826 | | track_data[physical_sector * 2 + 0] = (UINT8) (idam_offset >> 0); |
| 827 | | track_data[physical_sector * 2 + 1] = (UINT8) (idam_offset >> 8); |
| 828 | | |
| 829 | | physical_sector++; |
| 830 | | } |
| 831 | | |
| 832 | | /* write track lead in */ |
| 833 | | memset(&track_data[physical_sector * 2], 0x4e, DMK_LEAD_IN); |
| 834 | | |
| 835 | | /* write track footer */ |
| 836 | | assert(max_track_size >= (UINT32)track_position); |
| 837 | | memset(&track_data[track_position], 0x4e, max_track_size - track_position); |
| 838 | | |
| 839 | | done: |
| 840 | | if (sector_map) |
| 841 | | free(sector_map); |
| 842 | | return FLOPPY_ERROR_SUCCESS; |
| 843 | | } |
| 844 | | |
| 845 | | |
| 846 | | |
| 847 | | static int coco_dmk_get_heads_per_disk(floppy_image *floppy) |
| 848 | | { |
| 849 | | return get_dmk_tag(floppy)->heads; |
| 850 | | } |
| 851 | | |
| 852 | | |
| 853 | | |
| 854 | | static int coco_dmk_get_tracks_per_disk(floppy_image *floppy) |
| 855 | | { |
| 856 | | return get_dmk_tag(floppy)->tracks; |
| 857 | | } |
| 858 | | |
| 859 | | |
| 860 | | |
| 861 | | static UINT32 coco_dmk_get_track_size(floppy_image *floppy, int head, int track) |
| 862 | | { |
| 863 | | return get_dmk_tag(floppy)->track_size; |
| 864 | | } |
| 865 | | |
| 866 | | |
| 867 | | |
| 868 | | static floperr_t coco_dmk_seek_sector_in_track(floppy_image *floppy, int head, int track, int sector, int sector_is_index, int dirtify, UINT8 **sector_data, UINT32 *sector_length) |
| 869 | | { |
| 870 | | struct dmk_tag *tag = get_dmk_tag(floppy); |
| 871 | | floperr_t err; |
| 872 | | UINT32 idam_offset = 0; |
| 873 | | UINT16 calculated_crc; |
| 874 | | size_t i; |
| 875 | | size_t offs; |
| 876 | | int state; |
| 877 | | UINT8 *track_data; |
| 878 | | void *track_data_v; |
| 879 | | size_t track_length; |
| 880 | | size_t sec_len; |
| 881 | | |
| 882 | | err = floppy_load_track(floppy, head, track, dirtify, &track_data_v, &track_length); |
| 883 | | if (err) |
| 884 | | return err; |
| 885 | | track_data = (UINT8 *) track_data_v; |
| 886 | | |
| 887 | | /* search for matching IDAM */ |
| 888 | | for (i = 0; i < DMK_TOC_LEN; i++) |
| 889 | | { |
| 890 | | idam_offset = track_data[i * 2 + 1]; |
| 891 | | idam_offset <<= 8; |
| 892 | | idam_offset |= track_data[i * 2 + 0]; |
| 893 | | idam_offset &= 0x3FFF; |
| 894 | | |
| 895 | | if (idam_offset == 0) |
| 896 | | { |
| 897 | | /* we've reached the end of the road */ |
| 898 | | i = DMK_TOC_LEN; |
| 899 | | break; |
| 900 | | } |
| 901 | | |
| 902 | | if ((idam_offset + DMK_IDAM_LENGTH) >= tag->track_size) |
| 903 | | continue; |
| 904 | | |
| 905 | | calculated_crc = ccitt_crc16(0xCDB4, &track_data[idam_offset], DMK_IDAM_LENGTH - 2); |
| 906 | | |
| 907 | | if (calculated_crc == dmk_idam_crc(&track_data[idam_offset])) |
| 908 | | { |
| 909 | | if (sector_is_index) |
| 910 | | { |
| 911 | | /* the sector is indexed; decrement the index and go */ |
| 912 | | if (sector-- == 0) |
| 913 | | break; |
| 914 | | } |
| 915 | | else |
| 916 | | { |
| 917 | | /* check IDAM integrity and check for matching sector */ |
| 918 | | if ((sector == dmk_idam_sector(&track_data[idam_offset])) |
| 919 | | /* && (track == dmk_idam_track(&track_data[idam_offset])) */ |
| 920 | | /* && (head == dmk_idam_side(&track_data[idam_offset])) */ |
| 921 | | ) |
| 922 | | break; |
| 923 | | } |
| 924 | | } |
| 925 | | } |
| 926 | | |
| 927 | | if (i >= DMK_TOC_LEN) |
| 928 | | return FLOPPY_ERROR_SEEKERROR; |
| 929 | | |
| 930 | | /* we found a matching sector ID */ |
| 931 | | state = 0; |
| 932 | | offs = idam_offset + DMK_IDAM_LENGTH; |
| 933 | | |
| 934 | | /* find pattern 0xA1A1FB; this represents the start of a data sector */ |
| 935 | | for (i = 0; i < DMK_DATA_GAP; i++) |
| 936 | | { |
| 937 | | /* overflowing the track? */ |
| 938 | | if ((i + offs) >= tag->track_size) |
| 939 | | return FLOPPY_ERROR_SEEKERROR; |
| 940 | | |
| 941 | | if (track_data[offs + i] == 0xA1) |
| 942 | | state++; |
| 943 | | else if ((track_data[offs + i] == 0xFB) && state) |
| 944 | | break; |
| 945 | | else |
| 946 | | state = 0; |
| 947 | | } |
| 948 | | if (i >= DMK_DATA_GAP) |
| 949 | | return FLOPPY_ERROR_SEEKERROR; |
| 950 | | |
| 951 | | offs += i + 1; |
| 952 | | sec_len = 128 << dmk_idam_sectorlength(&track_data[idam_offset]); |
| 953 | | |
| 954 | | if ((offs + sec_len) > track_length) |
| 955 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 956 | | |
| 957 | | if (sector_data) |
| 958 | | *sector_data = track_data + offs; |
| 959 | | if (sector_length) |
| 960 | | *sector_length = sec_len; |
| 961 | | return FLOPPY_ERROR_SUCCESS; |
| 962 | | } |
| 963 | | |
| 964 | | |
| 965 | | |
| 966 | | static floperr_t coco_dmk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 967 | | { |
| 968 | | return coco_dmk_seek_sector_in_track(floppy, head, track, sector, FALSE, FALSE, NULL, sector_length); |
| 969 | | } |
| 970 | | |
| 971 | | |
| 972 | | |
| 973 | | static floperr_t coco_dmk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 974 | | { |
| 975 | | floperr_t err; |
| 976 | | UINT32 idam_offset; |
| 977 | | const UINT8 *track_data; |
| 978 | | void *track_data_v; |
| 979 | | |
| 980 | | if (sector_index*2 >= DMK_TOC_LEN) |
| 981 | | return FLOPPY_ERROR_SEEKERROR; |
| 982 | | |
| 983 | | err = floppy_load_track(floppy, head, track, FALSE, &track_data_v, NULL); |
| 984 | | if (err) |
| 985 | | return err; |
| 986 | | track_data = (UINT8 *) track_data_v; |
| 987 | | |
| 988 | | idam_offset = track_data[sector_index * 2 + 1]; |
| 989 | | idam_offset <<= 8; |
| 990 | | idam_offset |= track_data[sector_index * 2 + 0]; |
| 991 | | idam_offset &= 0x3FFF; |
| 992 | | |
| 993 | | if (idam_offset == 0) |
| 994 | | return FLOPPY_ERROR_SEEKERROR; |
| 995 | | |
| 996 | | if (cylinder) |
| 997 | | *cylinder = dmk_idam_track(&track_data[idam_offset]); |
| 998 | | if (side) |
| 999 | | *side = dmk_idam_side(&track_data[idam_offset]); |
| 1000 | | if (sector) |
| 1001 | | *sector = dmk_idam_sector(&track_data[idam_offset]); |
| 1002 | | if (sector_length) |
| 1003 | | *sector_length = 128 << dmk_idam_sectorlength(&track_data[idam_offset]); |
| 1004 | | if (flags) |
| 1005 | | /* TODO: read DAM or DDAM and determine flags */ |
| 1006 | | *flags = 0; |
| 1007 | | return FLOPPY_ERROR_SUCCESS; |
| 1008 | | } |
| 1009 | | |
| 1010 | | |
| 1011 | | |
| 1012 | | static floperr_t internal_coco_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen) |
| 1013 | | { |
| 1014 | | floperr_t err; |
| 1015 | | UINT32 sector_length; |
| 1016 | | UINT16 crc_on_disk; |
| 1017 | | UINT16 calculated_crc; |
| 1018 | | UINT8 *sector_data; |
| 1019 | | |
| 1020 | | err = coco_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, FALSE, §or_data, §or_length); |
| 1021 | | if (err) |
| 1022 | | return err; |
| 1023 | | |
| 1024 | | crc_on_disk = sector_data[sector_length + 0]; |
| 1025 | | crc_on_disk <<= 8; |
| 1026 | | crc_on_disk += sector_data[sector_length + 1]; |
| 1027 | | |
| 1028 | | calculated_crc = ccitt_crc16(0xE295, sector_data, sector_length); |
| 1029 | | if (calculated_crc != crc_on_disk) |
| 1030 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1031 | | |
| 1032 | | memcpy(buffer, sector_data, MIN(sector_length, buflen)); |
| 1033 | | |
| 1034 | | return FLOPPY_ERROR_SUCCESS; |
| 1035 | | } |
| 1036 | | |
| 1037 | | |
| 1038 | | |
| 1039 | | static floperr_t internal_coco_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam) |
| 1040 | | { |
| 1041 | | floperr_t err; |
| 1042 | | UINT32 sector_length; |
| 1043 | | UINT8 *sector_data; |
| 1044 | | UINT16 crc; |
| 1045 | | |
| 1046 | | err = coco_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, TRUE, §or_data, §or_length); |
| 1047 | | if (err) |
| 1048 | | return err; |
| 1049 | | |
| 1050 | | if (buflen > sector_length) |
| 1051 | | return FLOPPY_ERROR_INTERNAL; |
| 1052 | | |
| 1053 | | memcpy(sector_data, buffer, buflen); |
| 1054 | | |
| 1055 | | crc = ccitt_crc16(0xE295, sector_data, sector_length); |
| 1056 | | sector_data[sector_length + 0] = crc >> 8; |
| 1057 | | sector_data[sector_length + 1] = crc >> 0; |
| 1058 | | return FLOPPY_ERROR_SUCCESS; |
| 1059 | | } |
| 1060 | | |
| 1061 | | |
| 1062 | | |
| 1063 | | static floperr_t coco_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 1064 | | { |
| 1065 | | return internal_coco_dmk_read_sector(floppy, head, track, sector, FALSE, buffer, buflen); |
| 1066 | | } |
| 1067 | | |
| 1068 | | static floperr_t coco_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 1069 | | { |
| 1070 | | return internal_coco_dmk_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam); |
| 1071 | | } |
| 1072 | | |
| 1073 | | static floperr_t coco_dmk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 1074 | | { |
| 1075 | | return internal_coco_dmk_read_sector(floppy, head, track, sector, TRUE, buffer, buflen); |
| 1076 | | } |
| 1077 | | |
| 1078 | | static floperr_t coco_dmk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 1079 | | { |
| 1080 | | return internal_coco_dmk_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam); |
| 1081 | | } |
| 1082 | | |
| 1083 | | |
| 1084 | | |
| 1085 | | static void coco_dmk_interpret_header(floppy_image *floppy, int *heads, int *tracks, int *track_size) |
| 1086 | | { |
| 1087 | | UINT8 header[DMK_HEADER_LEN]; |
| 1088 | | |
| 1089 | | floppy_image_read(floppy, header, 0, DMK_HEADER_LEN); |
| 1090 | | |
| 1091 | | if (tracks) |
| 1092 | | *tracks = header[1]; |
| 1093 | | if (heads) |
| 1094 | | *heads = (header[4] & 0x10) ? 1 : 2; |
| 1095 | | if (track_size) |
| 1096 | | *track_size = ((int) header[3]) * 0x100 + header[2]; |
| 1097 | | } |
| 1098 | | |
| 1099 | | |
| 1100 | | |
| 1101 | | FLOPPY_CONSTRUCT(coco_dmk_construct) |
| 1102 | | { |
| 1103 | | struct FloppyCallbacks *callbacks; |
| 1104 | | struct dmk_tag *tag; |
| 1105 | | UINT8 header[DMK_HEADER_LEN]; |
| 1106 | | int heads, tracks, track_size, sectors, sector_length; |
| 1107 | | |
| 1108 | | if (params) |
| 1109 | | { |
| 1110 | | heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 1111 | | tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 1112 | | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 1113 | | sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 1114 | | |
| 1115 | | track_size = coco_dmk_min_track_size(sectors, sector_length) + DMK_EXTRA_TRACK_LENGTH; |
| 1116 | | |
| 1117 | | memset(header, 0, sizeof(header)); |
| 1118 | | header[1] = tracks; |
| 1119 | | header[2] = track_size >> 0; |
| 1120 | | header[3] = track_size >> 8; |
| 1121 | | header[4] = (heads == 2) ? 0x00 : 0x10; |
| 1122 | | |
| 1123 | | floppy_image_write(floppy, header, 0, sizeof(header)); |
| 1124 | | } |
| 1125 | | else |
| 1126 | | { |
| 1127 | | coco_dmk_interpret_header(floppy, &heads, &tracks, &track_size); |
| 1128 | | } |
| 1129 | | |
| 1130 | | tag = (dmk_tag *)floppy_create_tag(floppy, sizeof(struct dmk_tag)); |
| 1131 | | if (!tag) |
| 1132 | | return FLOPPY_ERROR_OUTOFMEMORY; |
| 1133 | | tag->heads = heads; |
| 1134 | | tag->track_size = track_size; |
| 1135 | | tag->tracks = tracks; |
| 1136 | | |
| 1137 | | callbacks = floppy_callbacks(floppy); |
| 1138 | | callbacks->read_track = coco_dmk_read_track; |
| 1139 | | callbacks->write_track = coco_dmk_write_track; |
| 1140 | | callbacks->get_track_data_offset = coco_dmk_get_track_data_offset; |
| 1141 | | callbacks->format_track = coco_dmk_format_track; |
| 1142 | | callbacks->get_heads_per_disk = coco_dmk_get_heads_per_disk; |
| 1143 | | callbacks->get_tracks_per_disk = coco_dmk_get_tracks_per_disk; |
| 1144 | | callbacks->get_track_size = coco_dmk_get_track_size; |
| 1145 | | callbacks->get_sector_length = coco_dmk_get_sector_length; |
| 1146 | | callbacks->get_indexed_sector_info = coco_dmk_get_indexed_sector_info; |
| 1147 | | callbacks->read_sector = coco_dmk_read_sector; |
| 1148 | | callbacks->write_sector = coco_dmk_write_sector; |
| 1149 | | callbacks->read_indexed_sector = coco_dmk_read_indexed_sector; |
| 1150 | | callbacks->write_indexed_sector = coco_dmk_write_indexed_sector; |
| 1151 | | |
| 1152 | | return FLOPPY_ERROR_SUCCESS; |
| 1153 | | } |
| 1154 | | |
| 1155 | | |
| 1156 | | |
| 1157 | | FLOPPY_IDENTIFY(coco_dmk_identify) |
| 1158 | | { |
| 1159 | | int heads, tracks, track_size; |
| 1160 | | UINT64 size, expected_size; |
| 1161 | | |
| 1162 | | size = floppy_image_size(floppy); |
| 1163 | | coco_dmk_interpret_header(floppy, &heads, &tracks, &track_size); |
| 1164 | | expected_size = DMK_HEADER_LEN + (heads * tracks * track_size); |
| 1165 | | *vote = (size == expected_size) ? 100 : 0; |
| 1166 | | return FLOPPY_ERROR_SUCCESS; |
| 1167 | | } |
| 1168 | | |
| 1169 | | |
| 1170 | | |
| 1171 | | /* ----------------------------------------------------------------------- */ |
| 1172 | | |
| 1173 | | FLOPPY_OPTIONS_START( coco ) |
| 1174 | | FLOPPY_OPTION( coco_jvc, "dsk", "CoCo JVC disk image", coco_jvc_identify, coco_jvc_construct, NULL, |
| 1175 | | HEADS([1]-2) |
| 1176 | | TRACKS([35]-255) |
| 1177 | | SECTORS(1-[18]-255) |
| 1178 | | SECTOR_LENGTH(128/[256]/512/1024) |
| 1179 | | FIRST_SECTOR_ID(0-[1])) |
| 1180 | | FLOPPY_OPTION( coco_os9, "os9", "CoCo OS-9 disk image", coco_os9_identify, coco_os9_construct, NULL, |
| 1181 | | HEADS([1]-2) |
| 1182 | | TRACKS([35]-255) |
| 1183 | | SECTORS(1-[18]-255) |
| 1184 | | SECTOR_LENGTH([256]) |
| 1185 | | FIRST_SECTOR_ID([1])) |
| 1186 | | FLOPPY_OPTION( coco_vdk, "vdk", "CoCo VDK disk image", coco_vdk_identify, coco_vdk_construct, NULL, |
| 1187 | | HEADS([1]-2) |
| 1188 | | TRACKS([35]-255) |
| 1189 | | SECTORS([18]) |
| 1190 | | SECTOR_LENGTH([256]) |
| 1191 | | FIRST_SECTOR_ID([1])) |
| 1192 | | FLOPPY_OPTION( coco_dmk, "dsk,dmk", "CoCo DMK disk image", coco_dmk_identify, coco_dmk_construct, NULL, |
| 1193 | | HEADS([1]-2) |
| 1194 | | TRACKS([35]-255) |
| 1195 | | SECTORS(1-[18]) |
| 1196 | | SECTOR_LENGTH(128/[256]/512/1024/2048/4096/8192) |
| 1197 | | INTERLEAVE(0-[6]-17) |
| 1198 | | FIRST_SECTOR_ID(0-[1])) |
| 1199 | | FLOPPY_OPTIONS_END |
src/mess/formats/ap_dsk35.c
| r11457 | r11458 | |
| 1 | | /********************************************************************* |
| 2 | | |
| 3 | | ap_dsk35.c |
| 4 | | |
| 5 | | Apple 3.5" disk images |
| 6 | | |
| 7 | | This code supports 3.5" 400k/800k disks used in early Macintoshes |
| 8 | | and the Apple IIgs, and 3.5" 1440k MFM disks used on most Macintoshes. |
| 9 | | |
| 10 | | These disks have the following properties: |
| 11 | | |
| 12 | | 400k: 80 tracks, 1 head |
| 13 | | 800k: 80 tracks, 2 heads |
| 14 | | |
| 15 | | Tracks 0-15 have 12 sectors each |
| 16 | | Tracks 16-31 have 11 sectors each |
| 17 | | Tracks 32-47 have 10 sectors each |
| 18 | | Tracks 48-63 have 9 sectors each |
| 19 | | Tracks 64-79 have 8 sectors each |
| 20 | | |
| 21 | | 1440k disks are simply 80 tracks, 2 heads and 18 sectors per track. |
| 22 | | |
| 23 | | Each sector on 400/800k disks has 524 bytes, 512 of which are really used by the Macintosh |
| 24 | | |
| 25 | | (80 tracks) * (avg of 10 sectors) * (512 bytes) * (2 sides) = 800 kB |
| 26 | | |
| 27 | | Data is nibblized : 3 data bytes -> 4 bytes on disk. |
| 28 | | |
| 29 | | In addition to 512 logical bytes, each sector contains 800 physical |
| 30 | | bytes. Here is the layout of the physical sector: |
| 31 | | |
| 32 | | Pos |
| 33 | | 0 0xFF (pad byte where head is turned on ???) |
| 34 | | 1-35 self synch 0xFFs (7*5) (42 bytes actually written to media) |
| 35 | | 36 0xD5 |
| 36 | | 37 0xAA |
| 37 | | 38 0x96 |
| 38 | | 39 diskbytes[(track number) & 0x3F] |
| 39 | | 40 diskbytes[(sector number)] |
| 40 | | 41 diskbytes[("side")] |
| 41 | | 42 diskbytes[("format byte")] |
| 42 | | 43 diskbytes[("sum")] |
| 43 | | 44 0xDE |
| 44 | | 45 0xAA |
| 45 | | 46 pad byte where head is turned off/on (0xFF here) |
| 46 | | 47-51 self synch 0xFFs (6 bytes actually written to media) |
| 47 | | 52 0xD5 |
| 48 | | 53 0xAA |
| 49 | | 54 0xAD |
| 50 | | 55 spare byte, generally diskbytes[(sector number)] |
| 51 | | 56-754 "nibblized" sector data ... |
| 52 | | 755-758 checksum |
| 53 | | 759 0xDE |
| 54 | | 760 0xAA |
| 55 | | 761 pad byte where head is turned off (0xFF here) |
| 56 | | |
| 57 | | MFM Track layout for 1440K disks: |
| 58 | | Pos |
| 59 | | --------- track ID |
| 60 | | 0 0x4E (x80) |
| 61 | | 80 00 (x12) |
| 62 | | 92 C2 (x3) Mark byte |
| 63 | | 93 FC Index mark |
| 64 | | 94 4E (x50) |
| 65 | | --------- sector ID |
| 66 | | 144 00 (x12) |
| 67 | | 156 A1 (x3) Mark byte |
| 68 | | 159 FE Address mark |
| 69 | | 160 xx Track number |
| 70 | | 161 xx Side number |
| 71 | | 162 xx Sector number |
| 72 | | 163 xx Sector length |
| 73 | | 164/165 xx CRC |
| 74 | | 166 4E (x22) |
| 75 | | --------- sector data |
| 76 | | 188 00 (x12) |
| 77 | | 200 A1 (x3) Mark byte |
| 78 | | 203 FB Data address mark |
| 79 | | 204 xx (x256) data |
| 80 | | 460 4E (x54) |
| 81 | | (repeat from sector ID to fill track; end of track is padded with 4E) |
| 82 | | |
| 83 | | Note : "Self synch refers to a technique whereby two zeroes are inserted |
| 84 | | between each synch byte written to the disk.", i.e. "0xFF, 0xFF, 0xFF, |
| 85 | | 0xFF, 0xFF" is actually "0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF" on disk. |
| 86 | | Since the IWM assumes the data transfer is complete when the MSBit of its |
| 87 | | shift register is 1, we do read 4 0xFF, even though they are not |
| 88 | | contiguous on the disk media. Some reflexion shows that 4 synch bytes |
| 89 | | allow the IWM to synchronize with the trailing data. |
| 90 | | |
| 91 | | Format byte codes: |
| 92 | | 0x00 Apple II |
| 93 | | 0x01 Lisa |
| 94 | | 0x02 Mac MFS (single sided)? |
| 95 | | 0x22 Mac MFS (double sided)? |
| 96 | | |
| 97 | | *********************************************************************/ |
| 98 | | |
| 99 | | #include <stdio.h> |
| 100 | | #include <assert.h> |
| 101 | | |
| 102 | | #include "formats/ap_dsk35.h" |
| 103 | | |
| 104 | | struct apple35_tag |
| 105 | | { |
| 106 | | UINT32 data_offset; |
| 107 | | UINT32 data_size; |
| 108 | | UINT8 format_byte; |
| 109 | | UINT8 sides; |
| 110 | | unsigned int is_1440k : 1; |
| 111 | | |
| 112 | | /* stuff used in DiskCopy images */ |
| 113 | | UINT32 tag_offset; |
| 114 | | UINT32 tag_size; |
| 115 | | }; |
| 116 | | |
| 117 | | |
| 118 | | |
| 119 | | /* normal number of sector for each track */ |
| 120 | | static const UINT8 apple35_tracklen_800kb[80] = |
| 121 | | { |
| 122 | | 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, |
| 123 | | 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, |
| 124 | | 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, |
| 125 | | 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, |
| 126 | | 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 |
| 127 | | }; |
| 128 | | |
| 129 | | /* blocks of data used to nibblize tracks */ |
| 130 | | static const UINT8 diskbytes[] = |
| 131 | | { |
| 132 | | 0x96, 0x97, 0x9A, 0x9B, 0x9D, 0x9E, 0x9F, 0xA6, /* 0x00 */ |
| 133 | | 0xA7, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF, 0xB2, 0xB3, |
| 134 | | 0xB4, 0xB5, 0xB6, 0xB7, 0xB9, 0xBA, 0xBB, 0xBC, /* 0x10 */ |
| 135 | | 0xBD, 0xBE, 0xBF, 0xCB, 0xCD, 0xCE, 0xCF, 0xD3, |
| 136 | | 0xD6, 0xD7, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, /* 0x20 */ |
| 137 | | 0xDF, 0xE5, 0xE6, 0xE7, 0xE9, 0xEA, 0xEB, 0xEC, |
| 138 | | 0xED, 0xEE, 0xEF, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, /* 0x30 */ |
| 139 | | 0xF7, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF |
| 140 | | }; |
| 141 | | |
| 142 | | /* reverse lookup of diskbytes */ |
| 143 | | static const INT16 rev_diskbytes[] = |
| 144 | | { |
| 145 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */ |
| 146 | | -1, -1, -1, -1, -1, -1, -1, -1, |
| 147 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 */ |
| 148 | | -1, -1, -1, -1, -1, -1, -1, -1, |
| 149 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x20 */ |
| 150 | | -1, -1, -1, -1, -1, -1, -1, -1, |
| 151 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x30 */ |
| 152 | | -1, -1, -1, -1, -1, -1, -1, -1, |
| 153 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x40 */ |
| 154 | | -1, -1, -1, -1, -1, -1, -1, -1, |
| 155 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x50 */ |
| 156 | | -1, -1, -1, -1, -1, -1, -1, -1, |
| 157 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x60 */ |
| 158 | | -1, -1, -1, -1, -1, -1, -1, -1, |
| 159 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x70 */ |
| 160 | | -1, -1, -1, -1, -1, -1, -1, -1, |
| 161 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 0x80 */ |
| 162 | | -1, -1, -1, -1, -1, -1, -1, -1, |
| 163 | | -1, -1, -1, -1, -1, -1, 0x00, 0x01, /* 0x90 */ |
| 164 | | -1, -1, 0x02, 0x03, -1, 0x04, 0x05, 0x06, |
| 165 | | -1, -1, -1, -1, -1, -1, 0x07, 0x08, /* 0xA0 */ |
| 166 | | -1, -1, -1, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, |
| 167 | | -1, -1, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, /* 0xB0 */ |
| 168 | | -1, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, |
| 169 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 0xC0 */ |
| 170 | | -1, -1, -1, 0x1B, -1, 0x1C, 0x1D, 0x1E, |
| 171 | | -1, -1, -1, 0x1F, -1, -1, 0x20, 0x21, /* 0xD0 */ |
| 172 | | -1, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, |
| 173 | | -1, -1, -1, -1, -1, 0x29, 0x2A, 0x2B, /* 0xE0 */ |
| 174 | | -1, 0x2C, 0x2D, 0x2E, 0x2F, 0x30, 0x31, 0x32, |
| 175 | | -1, -1, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, /* 0xF0 */ |
| 176 | | -1, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F |
| 177 | | }; |
| 178 | | |
| 179 | | static const UINT8 blk1[] = |
| 180 | | { |
| 181 | | /*0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 182 | | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 183 | | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 184 | | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 185 | | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 186 | | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 187 | | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 188 | | 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
| 189 | | 0xFF, 0xFF, 0xD5, 0xAA, 0x96*/ |
| 190 | | 0xFF, |
| 191 | | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 192 | | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 193 | | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 194 | | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 195 | | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 196 | | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 197 | | 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, |
| 198 | | 0xD5, 0xAA, 0x96 |
| 199 | | }; |
| 200 | | static const UINT8 blk2[] = |
| 201 | | { |
| 202 | | 0xDE, 0xAA, 0xFF, 0xFF, 0x3F, 0xCF, 0xF3, 0xFC, 0xFF, 0xD5, 0xAA, 0xAD |
| 203 | | }; |
| 204 | | static const UINT8 blk3[] = |
| 205 | | { |
| 206 | | 0xDE, 0xAA, 0xFF |
| 207 | | }; |
| 208 | | |
| 209 | | |
| 210 | | |
| 211 | | static struct apple35_tag *get_apple35_tag(floppy_image *floppy) |
| 212 | | { |
| 213 | | struct apple35_tag *tag; |
| 214 | | tag = (struct apple35_tag *) floppy_tag(floppy); |
| 215 | | return tag; |
| 216 | | } |
| 217 | | |
| 218 | | |
| 219 | | |
| 220 | | int apple35_sectors_per_track(floppy_image *image, int track) |
| 221 | | { |
| 222 | | int sectors; |
| 223 | | |
| 224 | | assert(track >= 0); |
| 225 | | assert(track < ARRAY_LENGTH(apple35_tracklen_800kb)); |
| 226 | | |
| 227 | | if (get_apple35_tag(image)->is_1440k) |
| 228 | | sectors = 18; |
| 229 | | else |
| 230 | | sectors = apple35_tracklen_800kb[track]; |
| 231 | | return sectors; |
| 232 | | } |
| 233 | | |
| 234 | | |
| 235 | | |
| 236 | | /* |
| 237 | | converts data to its nibblized representation, and generate checksum |
| 238 | | |
| 239 | | now handles 524-byte-long sectors |
| 240 | | |
| 241 | | tag data IS important, since it allows data recovery when the catalog is trashed |
| 242 | | */ |
| 243 | | static void sony_nibblize35(const UINT8 *in, UINT8 *nib_ptr, UINT8 *csum) |
| 244 | | { |
| 245 | | int i, j; |
| 246 | | UINT32 c1, c2, c3, c4; |
| 247 | | UINT8 val; |
| 248 | | UINT8 w1, w2, w3, w4; |
| 249 | | UINT8 b1[175], b2[175], b3[175]; |
| 250 | | |
| 251 | | /* Copy from the user's buffer to our buffer, while computing |
| 252 | | * the three-byte data checksum |
| 253 | | */ |
| 254 | | |
| 255 | | i = 0; |
| 256 | | j = 0; |
| 257 | | c1 = 0; |
| 258 | | c2 = 0; |
| 259 | | c3 = 0; |
| 260 | | while (1) |
| 261 | | { |
| 262 | | c1 = (c1 & 0xFF) << 1; |
| 263 | | if (c1 & 0x0100) |
| 264 | | c1++; |
| 265 | | |
| 266 | | val = in[i++]; |
| 267 | | c3 += val; |
| 268 | | if (c1 & 0x0100) |
| 269 | | { |
| 270 | | c3++; |
| 271 | | c1 &= 0xFF; |
| 272 | | } |
| 273 | | b1[j] = (val ^ c1) & 0xFF; |
| 274 | | |
| 275 | | val = in[i++]; |
| 276 | | c2 += val; |
| 277 | | if (c3 > 0xFF) |
| 278 | | { |
| 279 | | c2++; |
| 280 | | c3 &= 0xFF; |
| 281 | | } |
| 282 | | b2[j] = (val ^ c3) & 0xFF; |
| 283 | | |
| 284 | | if (i == 524) break; |
| 285 | | |
| 286 | | val = in[i++]; |
| 287 | | c1 += val; |
| 288 | | if (c2 > 0xFF) |
| 289 | | { |
| 290 | | c1++; |
| 291 | | c2 &= 0xFF; |
| 292 | | } |
| 293 | | b3[j] = (val ^ c2) & 0xFF; |
| 294 | | j++; |
| 295 | | } |
| 296 | | c4 = ((c1 & 0xC0) >> 6) | ((c2 & 0xC0) >> 4) | ((c3 & 0xC0) >> 2); |
| 297 | | b3[174] = 0; |
| 298 | | |
| 299 | | j = 0; |
| 300 | | for (i = 0; i <= 174; i++) |
| 301 | | { |
| 302 | | w1 = b1[i] & 0x3F; |
| 303 | | w2 = b2[i] & 0x3F; |
| 304 | | w3 = b3[i] & 0x3F; |
| 305 | | w4 = ((b1[i] & 0xC0) >> 2); |
| 306 | | w4 |= ((b2[i] & 0xC0) >> 4); |
| 307 | | w4 |= ((b3[i] & 0xC0) >> 6); |
| 308 | | |
| 309 | | nib_ptr[j++] = w4; |
| 310 | | nib_ptr[j++] = w1; |
| 311 | | nib_ptr[j++] = w2; |
| 312 | | |
| 313 | | if (i != 174) nib_ptr[j++] = w3; |
| 314 | | } |
| 315 | | |
| 316 | | csum[0] = c1 & 0x3F; |
| 317 | | csum[1] = c2 & 0x3F; |
| 318 | | csum[2] = c3 & 0x3F; |
| 319 | | csum[3] = c4 & 0x3F; |
| 320 | | } |
| 321 | | |
| 322 | | |
| 323 | | |
| 324 | | /* |
| 325 | | does the reverse process of sony_nibblize35 |
| 326 | | */ |
| 327 | | static void sony_denibblize35(UINT8 *out, const UINT8 *nib_ptr, UINT8 *checksum) |
| 328 | | { |
| 329 | | int i, j; |
| 330 | | UINT32 c1,c2,c3,c4; |
| 331 | | UINT8 val; |
| 332 | | UINT8 w1,w2,w3=0,w4; |
| 333 | | UINT8 b1[175],b2[175],b3[175]; |
| 334 | | |
| 335 | | j = 0; |
| 336 | | for (i=0; i<=174; i++) |
| 337 | | { |
| 338 | | w4 = nib_ptr[j++]; |
| 339 | | w1 = nib_ptr[j++]; |
| 340 | | w2 = nib_ptr[j++]; |
| 341 | | |
| 342 | | if (i != 174) w3 = nib_ptr[j++]; |
| 343 | | |
| 344 | | b1[i] = (w1 & 0x3F) | ((w4 << 2) & 0xC0); |
| 345 | | b2[i] = (w2 & 0x3F) | ((w4 << 4) & 0xC0); |
| 346 | | b3[i] = (w3 & 0x3F) | ((w4 << 6) & 0xC0); |
| 347 | | } |
| 348 | | |
| 349 | | /* Copy from the user's buffer to our buffer, while computing |
| 350 | | * the three-byte data checksum |
| 351 | | */ |
| 352 | | |
| 353 | | i = 0; |
| 354 | | j = 0; |
| 355 | | c1 = 0; |
| 356 | | c2 = 0; |
| 357 | | c3 = 0; |
| 358 | | while (1) |
| 359 | | { |
| 360 | | c1 = (c1 & 0xFF) << 1; |
| 361 | | if (c1 & 0x0100) |
| 362 | | c1++; |
| 363 | | |
| 364 | | val = (b1[j] ^ c1) & 0xFF; |
| 365 | | c3 += val; |
| 366 | | if (c1 & 0x0100) |
| 367 | | { |
| 368 | | c3++; |
| 369 | | c1 &= 0xFF; |
| 370 | | } |
| 371 | | out[i++] = val; |
| 372 | | |
| 373 | | val = (b2[j] ^ c3) & 0xFF; |
| 374 | | c2 += val; |
| 375 | | if (c3 > 0xFF) |
| 376 | | { |
| 377 | | c2++; |
| 378 | | c3 &= 0xFF; |
| 379 | | } |
| 380 | | out[i++] = val; |
| 381 | | |
| 382 | | if (i == 524) break; |
| 383 | | |
| 384 | | val = (b3[j] ^ c2) & 0xFF; |
| 385 | | c1 += val; |
| 386 | | if (c2 > 0xFF) |
| 387 | | { |
| 388 | | c1++; |
| 389 | | c2 &= 0xFF; |
| 390 | | } |
| 391 | | out[i++] = val; |
| 392 | | j++; |
| 393 | | } |
| 394 | | c4 = ((c1 & 0xC0) >> 6) | ((c2 & 0xC0) >> 4) | ((c3 & 0xC0) >> 2); |
| 395 | | b3[174] = 0; |
| 396 | | |
| 397 | | checksum[0] = c1 & 0x3F; |
| 398 | | checksum[1] = c2 & 0x3F; |
| 399 | | checksum[2] = c3 & 0x3F; |
| 400 | | checksum[3] = c4 & 0x3F; |
| 401 | | } |
| 402 | | |
| 403 | | |
| 404 | | |
| 405 | | void sony_filltrack(UINT8 *buffer, size_t buffer_len, size_t *pos, UINT8 data) |
| 406 | | { |
| 407 | | buffer[*pos / 8] &= 0xFF << (8 - (*pos % 8)); |
| 408 | | buffer[*pos / 8] |= data >> (*pos % 8); |
| 409 | | |
| 410 | | *pos += 8; |
| 411 | | *pos %= buffer_len * 8; |
| 412 | | |
| 413 | | buffer[*pos / 8] &= 0xFF >> (*pos % 8); |
| 414 | | buffer[*pos / 8] |= data << (8 - (*pos % 8)); |
| 415 | | } |
| 416 | | |
| 417 | | |
| 418 | | |
| 419 | | UINT8 sony_fetchtrack(const UINT8 *buffer, size_t buffer_len, size_t *pos) |
| 420 | | { |
| 421 | | UINT8 data; |
| 422 | | |
| 423 | | data = buffer[*pos / 8] << (*pos % 8); |
| 424 | | *pos += 8; |
| 425 | | *pos %= (buffer_len * 8); |
| 426 | | data |= buffer[*pos / 8] >> (8 - (*pos % 8)); |
| 427 | | |
| 428 | | while ((data & 0x80) == 0) |
| 429 | | { |
| 430 | | /* this code looks weird because it isn't simply rotating the new bit |
| 431 | | * in, but for some reason it won't work if I rotate the bit in; I |
| 432 | | * have to match the algorithm used by the old code */ |
| 433 | | data <<= 1; |
| 434 | | data |= (buffer[*pos / 8] >> (8 - ((*pos % 8) + 1))); |
| 435 | | (*pos)++; |
| 436 | | *pos %= (buffer_len * 8); |
| 437 | | } |
| 438 | | return data; |
| 439 | | } |
| 440 | | |
| 441 | | |
| 442 | | |
| 443 | | static UINT32 apple35_get_offset(floppy_image *floppy, int head, int track, int sector, UINT32 *tag_offset) |
| 444 | | { |
| 445 | | int i; |
| 446 | | UINT32 sector_index = 0; |
| 447 | | struct apple35_tag *tag; |
| 448 | | |
| 449 | | tag = get_apple35_tag(floppy); |
| 450 | | |
| 451 | | if (track >= ARRAY_LENGTH(apple35_tracklen_800kb)) |
| 452 | | return ~0; |
| 453 | | if (head >= tag->sides) |
| 454 | | return ~0; |
| 455 | | if (sector >= apple35_sectors_per_track(floppy, track)) |
| 456 | | return ~0; |
| 457 | | |
| 458 | | for (i = 0; i < track; i++) |
| 459 | | sector_index += apple35_sectors_per_track(floppy, i); |
| 460 | | sector_index *= tag->sides; |
| 461 | | if (head) |
| 462 | | sector_index += apple35_sectors_per_track(floppy, i); |
| 463 | | sector_index += sector; |
| 464 | | |
| 465 | | if (tag_offset) |
| 466 | | { |
| 467 | | *tag_offset = sector_index * 12 + tag->tag_offset; |
| 468 | | if (*tag_offset >= tag->tag_size) |
| 469 | | *tag_offset = ~0; |
| 470 | | } |
| 471 | | return sector_index * 0x200 + tag->data_offset; |
| 472 | | } |
| 473 | | |
| 474 | | |
| 475 | | |
| 476 | | static floperr_t apple35_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 477 | | { |
| 478 | | UINT32 data_offset; |
| 479 | | // printf("Read sector: T %d S %d H %d\n", track, sector, head); |
| 480 | | data_offset = apple35_get_offset(floppy, head, track, sector, NULL); |
| 481 | | if (data_offset == ~0) |
| 482 | | return FLOPPY_ERROR_SEEKERROR; |
| 483 | | floppy_image_read(floppy, buffer, data_offset, buflen); |
| 484 | | return FLOPPY_ERROR_SUCCESS; |
| 485 | | } |
| 486 | | |
| 487 | | |
| 488 | | |
| 489 | | static floperr_t apple35_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 490 | | { |
| 491 | | UINT32 data_offset; |
| 492 | | |
| 493 | | data_offset = apple35_get_offset(floppy, head, track, sector, NULL); |
| 494 | | if (data_offset == ~0) |
| 495 | | return FLOPPY_ERROR_SEEKERROR; |
| 496 | | floppy_image_write(floppy, buffer, data_offset, buflen); |
| 497 | | return FLOPPY_ERROR_SUCCESS; |
| 498 | | } |
| 499 | | |
| 500 | | |
| 501 | | |
| 502 | | static floperr_t apple35_read_sector_td(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 503 | | { |
| 504 | | floperr_t err; |
| 505 | | UINT32 tag_offset = 0; |
| 506 | | |
| 507 | | assert(buflen == 524); |
| 508 | | |
| 509 | | // printf("Read sector TD: T %d S %d H %d\n", track, sector, head); |
| 510 | | |
| 511 | | /* first read the sector */ |
| 512 | | err = apple35_read_sector(floppy, head, track, sector, ((UINT8 *) buffer) + 12, 512); |
| 513 | | if (err) |
| 514 | | return err; |
| 515 | | |
| 516 | | /* read the tag data, if possible */ |
| 517 | | memset(buffer, '\0', 12); |
| 518 | | apple35_get_offset(floppy, head, track, sector, &tag_offset); |
| 519 | | if (tag_offset != ~0) |
| 520 | | floppy_image_read(floppy, buffer, tag_offset, 12); |
| 521 | | |
| 522 | | return FLOPPY_ERROR_SUCCESS; |
| 523 | | } |
| 524 | | |
| 525 | | |
| 526 | | |
| 527 | | static floperr_t apple35_write_sector_td(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 528 | | { |
| 529 | | floperr_t err; |
| 530 | | UINT32 tag_offset = 0; |
| 531 | | |
| 532 | | assert(buflen == 524); |
| 533 | | |
| 534 | | /* first write the sector */ |
| 535 | | err = apple35_write_sector(floppy, head, track, sector, ((const UINT8 *) buffer) + 12, 512, 0); |
| 536 | | if (err) |
| 537 | | return err; |
| 538 | | |
| 539 | | /* write the tag data, if possible */ |
| 540 | | apple35_get_offset(floppy, head, track, sector, &tag_offset); |
| 541 | | if (tag_offset != ~0) |
| 542 | | floppy_image_write(floppy, buffer, tag_offset, 12); |
| 543 | | |
| 544 | | return FLOPPY_ERROR_SUCCESS; |
| 545 | | } |
| 546 | | |
| 547 | | |
| 548 | | |
| 549 | | static floperr_t apple35_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 550 | | { |
| 551 | | *sector_length = 512; |
| 552 | | return FLOPPY_ERROR_SUCCESS; |
| 553 | | } |
| 554 | | |
| 555 | | |
| 556 | | |
| 557 | | static int apple35_get_heads_per_disk(floppy_image *floppy) |
| 558 | | { |
| 559 | | return get_apple35_tag(floppy)->sides; |
| 560 | | } |
| 561 | | |
| 562 | | |
| 563 | | |
| 564 | | static int apple35_get_tracks_per_disk(floppy_image *floppy) |
| 565 | | { |
| 566 | | return 80; |
| 567 | | } |
| 568 | | |
| 569 | | |
| 570 | | |
| 571 | | static UINT32 apple35_get_track_size(floppy_image *floppy, int head, int track) |
| 572 | | { |
| 573 | | if ((track < 0) || (track >= 80)) |
| 574 | | return 0; |
| 575 | | return apple35_sectors_per_track(floppy, track) * 800; |
| 576 | | } |
| 577 | | |
| 578 | | |
| 579 | | |
| 580 | | static UINT8 calculate_side(int head, int track) |
| 581 | | { |
| 582 | | UINT8 side; |
| 583 | | side = head ? 0x20 : 0x00; |
| 584 | | if (track & 0x40) |
| 585 | | side |= 0x01; |
| 586 | | return side; |
| 587 | | } |
| 588 | | |
| 589 | | |
| 590 | | |
| 591 | | static floperr_t apple35_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 592 | | { |
| 593 | | floperr_t err; |
| 594 | | size_t pos = 0; |
| 595 | | int sector_count, sector, i; |
| 596 | | UINT8 sum, side; |
| 597 | | struct apple35_tag *tag; |
| 598 | | UINT8 sector_data[524]; |
| 599 | | UINT8 nibble_data[699]; |
| 600 | | UINT8 checksum[4]; |
| 601 | | |
| 602 | | tag = get_apple35_tag(floppy); |
| 603 | | |
| 604 | | if (track >= ARRAY_LENGTH(apple35_tracklen_800kb)) |
| 605 | | return FLOPPY_ERROR_SEEKERROR; |
| 606 | | if (offset != 0) |
| 607 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 608 | | |
| 609 | | memset(buffer, 0xFF, buflen); |
| 610 | | sector_count = apple35_sectors_per_track(floppy, track); |
| 611 | | side = calculate_side(head, track); |
| 612 | | |
| 613 | | for (sector = 0; sector < sector_count; sector++) |
| 614 | | { |
| 615 | | /* read the sector */ |
| 616 | | err = apple35_read_sector_td(floppy, head, track, sector, sector_data, ARRAY_LENGTH(sector_data)); |
| 617 | | if (err) |
| 618 | | return err; |
| 619 | | sony_nibblize35(sector_data, nibble_data, checksum); |
| 620 | | |
| 621 | | for (i = 0; i < ARRAY_LENGTH(blk1); i++) |
| 622 | | sony_filltrack((UINT8*)buffer, buflen, &pos, blk1[i]); |
| 623 | | |
| 624 | | sum = (track ^ sector ^ side ^ tag->format_byte) & 0x3F; |
| 625 | | |
| 626 | | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[track & 0x3f]); |
| 627 | | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[sector]); |
| 628 | | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[side]); |
| 629 | | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[tag->format_byte]); |
| 630 | | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[sum]); |
| 631 | | |
| 632 | | for (i = 0; i < ARRAY_LENGTH(blk2); i++) |
| 633 | | sony_filltrack((UINT8*)buffer, buflen, &pos, blk2[i]); |
| 634 | | |
| 635 | | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[sector]); |
| 636 | | |
| 637 | | for (i = 0; i < ARRAY_LENGTH(nibble_data); i++) |
| 638 | | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[nibble_data[i]]); |
| 639 | | |
| 640 | | for (i = 3; i >= 0; i--) |
| 641 | | sony_filltrack((UINT8*)buffer, buflen, &pos, diskbytes[checksum[i]]); |
| 642 | | |
| 643 | | for (i = 0; i < ARRAY_LENGTH(blk3); i++) |
| 644 | | sony_filltrack((UINT8*)buffer, buflen, &pos, blk3[i]); |
| 645 | | } |
| 646 | | return FLOPPY_ERROR_SUCCESS; |
| 647 | | } |
| 648 | | |
| 649 | | |
| 650 | | |
| 651 | | static floperr_t apple35_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 652 | | { |
| 653 | | floperr_t err; |
| 654 | | size_t pos = 0; |
| 655 | | int sector_count, sector, i, j; |
| 656 | | struct apple35_tag *tag; |
| 657 | | UINT8 sum, format_byte, val, side; |
| 658 | | UINT32 found_sectors = 0; |
| 659 | | UINT8 sector_data[524]; |
| 660 | | UINT8 nibble_data[699]; |
| 661 | | UINT8 checksum[4]; |
| 662 | | |
| 663 | | tag = get_apple35_tag(floppy); |
| 664 | | |
| 665 | | if (track >= ARRAY_LENGTH(apple35_tracklen_800kb)) |
| 666 | | return FLOPPY_ERROR_SEEKERROR; |
| 667 | | if (offset != 0) |
| 668 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 669 | | |
| 670 | | sector_count = apple35_sectors_per_track(floppy, track); |
| 671 | | side = calculate_side(head, track); |
| 672 | | |
| 673 | | /* do 2 rotations, in case the bit slip stuff prevent us to read the first sector */ |
| 674 | | for (j = 0; j < (buflen * 2); j++) |
| 675 | | { |
| 676 | | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xD5) |
| 677 | | continue; |
| 678 | | j++; |
| 679 | | |
| 680 | | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAA) |
| 681 | | continue; |
| 682 | | j++; |
| 683 | | |
| 684 | | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0x96) |
| 685 | | continue; |
| 686 | | j++; |
| 687 | | |
| 688 | | if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != (track & 0x3F)) |
| 689 | | continue; |
| 690 | | j++; |
| 691 | | |
| 692 | | sector = rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)]; |
| 693 | | if ((sector < 0) || (sector >= sector_count)) |
| 694 | | continue; |
| 695 | | j++; |
| 696 | | |
| 697 | | if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != side) |
| 698 | | continue; |
| 699 | | j++; |
| 700 | | |
| 701 | | format_byte = rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)]; |
| 702 | | if (format_byte != tag->format_byte) |
| 703 | | { |
| 704 | | /* this is an error, but not THAT critical, I guess */ |
| 705 | | } |
| 706 | | j++; |
| 707 | | |
| 708 | | sum = track ^ sector ^ side ^ format_byte; |
| 709 | | if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != sum) |
| 710 | | continue; |
| 711 | | j++; |
| 712 | | |
| 713 | | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xDE) |
| 714 | | continue; |
| 715 | | j++; |
| 716 | | |
| 717 | | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAA) |
| 718 | | continue; |
| 719 | | j++; |
| 720 | | |
| 721 | | while((val = sony_fetchtrack((UINT8*)buffer, buflen, &pos)) == 0xFF) |
| 722 | | j++; |
| 723 | | if (val != 0xD5) |
| 724 | | continue; /* lost bit slip mark! */ |
| 725 | | j++; |
| 726 | | |
| 727 | | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAA) |
| 728 | | continue; |
| 729 | | j++; |
| 730 | | |
| 731 | | if (sony_fetchtrack((UINT8*)buffer, buflen, &pos) != 0xAD) |
| 732 | | continue; |
| 733 | | j++; |
| 734 | | |
| 735 | | /* should this be regarded as a critical error ??? */ |
| 736 | | if (rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)] != sector) |
| 737 | | continue; |
| 738 | | j++; |
| 739 | | |
| 740 | | for (i = 0; i < ARRAY_LENGTH(nibble_data); i++) |
| 741 | | { |
| 742 | | nibble_data[i] = rev_diskbytes[sony_fetchtrack((UINT8*)buffer, buflen, &pos)]; |
| 743 | | j++; |
| 744 | | } |
| 745 | | |
| 746 | | for (i = 3; i >= 0; i--) |
| 747 | | { |
| 748 | | /* should be checking checksum */ |
| 749 | | sony_fetchtrack((UINT8*)buffer, buflen, &pos); |
| 750 | | } |
| 751 | | |
| 752 | | sony_fetchtrack((UINT8*)buffer, buflen, &pos); /* should get 0xDE */ |
| 753 | | sony_fetchtrack((UINT8*)buffer, buflen, &pos); /* should get 0xAA */ |
| 754 | | sony_fetchtrack((UINT8*)buffer, buflen, &pos); /* should get 0xFF */ |
| 755 | | |
| 756 | | /* did we already write this sector? */ |
| 757 | | if ((found_sectors & (1 << sector)) == 0) |
| 758 | | { |
| 759 | | sony_denibblize35(sector_data, nibble_data, checksum); |
| 760 | | |
| 761 | | /* write the sector */ |
| 762 | | err = apple35_write_sector_td(floppy, head, track, sector, sector_data, ARRAY_LENGTH(sector_data), 0); |
| 763 | | if (err) |
| 764 | | return err; |
| 765 | | |
| 766 | | found_sectors |= 1 << sector; |
| 767 | | } |
| 768 | | } |
| 769 | | return FLOPPY_ERROR_SUCCESS; |
| 770 | | } |
| 771 | | |
| 772 | | |
| 773 | | |
| 774 | | static floperr_t apple35_construct(floppy_image *floppy, UINT32 data_offset, UINT32 data_size, |
| 775 | | UINT32 tag_offset, UINT32 tag_size, INT16 format_byte, UINT8 sides, int is_1440k) |
| 776 | | { |
| 777 | | struct apple35_tag *tag; |
| 778 | | struct FloppyCallbacks *format; |
| 779 | | |
| 780 | | /* figure out format byte if not specified */ |
| 781 | | if (format_byte < 0) |
| 782 | | { |
| 783 | | switch(sides) |
| 784 | | { |
| 785 | | case 1: |
| 786 | | format_byte = 0x02; |
| 787 | | break; |
| 788 | | case 2: |
| 789 | | format_byte = 0x22; |
| 790 | | break; |
| 791 | | default: |
| 792 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 793 | | } |
| 794 | | } |
| 795 | | |
| 796 | | /* create tag */ |
| 797 | | tag = (struct apple35_tag *) floppy_create_tag(floppy, sizeof(struct apple35_tag)); |
| 798 | | if (!tag) |
| 799 | | return FLOPPY_ERROR_OUTOFMEMORY; |
| 800 | | tag->data_offset = data_offset; |
| 801 | | tag->data_size = data_size; |
| 802 | | tag->tag_offset = tag_offset; |
| 803 | | tag->tag_size = tag_size; |
| 804 | | tag->format_byte = (UINT8) format_byte; |
| 805 | | tag->sides = sides; |
| 806 | | tag->is_1440k = is_1440k ? 1 : 0; |
| 807 | | |
| 808 | | /* set up format callbacks */ |
| 809 | | format = floppy_callbacks(floppy); |
| 810 | | format->read_sector = apple35_read_sector; |
| 811 | | format->write_sector = apple35_write_sector; |
| 812 | | format->read_track = apple35_read_track; |
| 813 | | format->write_track = apple35_write_track; |
| 814 | | format->get_sector_length = apple35_get_sector_length; |
| 815 | | format->get_heads_per_disk = apple35_get_heads_per_disk; |
| 816 | | format->get_tracks_per_disk = apple35_get_tracks_per_disk; |
| 817 | | format->get_track_size = apple35_get_track_size; |
| 818 | | return FLOPPY_ERROR_SUCCESS; |
| 819 | | } |
| 820 | | |
| 821 | | |
| 822 | | |
| 823 | | /* ----------------------------------------------------------------------- */ |
| 824 | | |
| 825 | | static FLOPPY_IDENTIFY(apple35_raw_identify) |
| 826 | | { |
| 827 | | UINT64 size; |
| 828 | | size = floppy_image_size(floppy); |
| 829 | | *vote = ((size == 80*1*10*512) || (size == 80*2*10*512) || (size == (80*2*18*512)+84) |
| 830 | | || (size == 80*2*18*512)) ? 100 : 0; |
| 831 | | return FLOPPY_ERROR_SUCCESS; |
| 832 | | } |
| 833 | | |
| 834 | | |
| 835 | | |
| 836 | | |
| 837 | | static FLOPPY_CONSTRUCT(apple35_raw_construct) |
| 838 | | { |
| 839 | | UINT64 size; |
| 840 | | UINT8 sides; |
| 841 | | int is_1440k; |
| 842 | | |
| 843 | | if (params) |
| 844 | | { |
| 845 | | /* create */ |
| 846 | | sides = option_resolution_lookup_int(params, PARAM_HEADS); |
| 847 | | size = 80*sides*10*512; |
| 848 | | is_1440k = FALSE; |
| 849 | | } |
| 850 | | else |
| 851 | | { |
| 852 | | /* load */ |
| 853 | | size = floppy_image_size(floppy); |
| 854 | | if (size == 80*1*10*512) |
| 855 | | { |
| 856 | | sides = 1; |
| 857 | | is_1440k = FALSE; |
| 858 | | } |
| 859 | | else if ((size == 80*2*10*512) || (size == 80*2*18*512) || (size == (80*2*18*512)+84)) |
| 860 | | { |
| 861 | | sides = 2; |
| 862 | | is_1440k = (size == 80*2*18*512) || (size == (80*2*18*512)+84); |
| 863 | | } |
| 864 | | else |
| 865 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 866 | | } |
| 867 | | |
| 868 | | return apple35_construct(floppy, 0, (UINT32) size, 0, 0, -1, sides, is_1440k); |
| 869 | | } |
| 870 | | |
| 871 | | |
| 872 | | |
| 873 | | /* ----------------------------------------------------------------------- */ |
| 874 | | |
| 875 | | struct header_diskcopy |
| 876 | | { |
| 877 | | UINT8 disk_name[64]; /* name of the disk */ |
| 878 | | UINT32 data_size; /* total size of data for all sectors (512*number_of_sectors) */ |
| 879 | | UINT32 tag_size; /* total size of tag data for all sectors (12*number_of_sectors for GCR 3.5" floppies, 20*number_of_sectors for HD20, 0 otherwise) */ |
| 880 | | UINT32 data_checksum; /* CRC32 checksum of all sector data */ |
| 881 | | UINT32 tag_checksum; /* CRC32 checksum of all tag data */ |
| 882 | | UINT8 disk_format; /* 0 = 400K, 1 = 800K, 2 = 720K, 3 = 1440K (other values reserved) */ |
| 883 | | UINT8 format_byte; /* should be $00 Apple II, $01 Lisa, $02 Mac MFS ??? */ |
| 884 | | /* $12 = 400K, $22 = >400K Macintosh (DiskCopy uses this value for |
| 885 | | all Apple II disks not 800K in size, and even for some of those), |
| 886 | | $24 = 800K Apple II disk */ |
| 887 | | UINT16 magic; /* always $0100 (otherwise, the file may be in a different format. */ |
| 888 | | }; |
| 889 | | |
| 890 | | |
| 891 | | |
| 892 | | static floperr_t apple35_diskcopy_headerdecode(floppy_image *floppy, UINT32 *data_offset, |
| 893 | | UINT32 *data_size, UINT32 *tag_offset, UINT32 *tag_size, UINT8 *format_byte, UINT8 *sides) |
| 894 | | { |
| 895 | | UINT64 size; |
| 896 | | struct header_diskcopy header; |
| 897 | | |
| 898 | | if (data_offset) |
| 899 | | *data_offset = 0; |
| 900 | | if (data_size) |
| 901 | | *data_size = 0; |
| 902 | | if (tag_offset) |
| 903 | | *tag_offset = 0; |
| 904 | | if (tag_size) |
| 905 | | *tag_size = 0; |
| 906 | | if (format_byte) |
| 907 | | *format_byte = 0; |
| 908 | | if (sides) |
| 909 | | *sides = 0; |
| 910 | | |
| 911 | | size = floppy_image_size(floppy); |
| 912 | | if (size < sizeof(struct header_diskcopy)) |
| 913 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 914 | | |
| 915 | | floppy_image_read(floppy, &header, 0, sizeof(header)); |
| 916 | | |
| 917 | | header.data_size = BIG_ENDIANIZE_INT32(header.data_size); |
| 918 | | header.tag_size = BIG_ENDIANIZE_INT32(header.tag_size); |
| 919 | | header.data_checksum = BIG_ENDIANIZE_INT32(header.data_checksum); |
| 920 | | header.tag_checksum = BIG_ENDIANIZE_INT32(header.tag_checksum); |
| 921 | | header.magic = BIG_ENDIANIZE_INT16(header.magic); |
| 922 | | |
| 923 | | if (header.disk_name[0] >= sizeof(header.disk_name)) |
| 924 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 925 | | if (header.magic != 0x0100) |
| 926 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 927 | | if (size != (header.data_size + header.tag_size + sizeof(header))) |
| 928 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 929 | | |
| 930 | | if (header.data_size == 80*1*10*512) |
| 931 | | { |
| 932 | | if (sides) |
| 933 | | *sides = 1; |
| 934 | | } |
| 935 | | else if (header.data_size == 80*2*10*512) |
| 936 | | { |
| 937 | | if (sides) |
| 938 | | *sides = 2; |
| 939 | | } |
| 940 | | else |
| 941 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 942 | | |
| 943 | | if (data_offset) |
| 944 | | *data_offset = sizeof(header); |
| 945 | | if (data_size) |
| 946 | | *data_size = header.data_size; |
| 947 | | if (tag_offset) |
| 948 | | *tag_offset = sizeof(header) + header.data_size; |
| 949 | | if (tag_size) |
| 950 | | *tag_size = header.tag_size; |
| 951 | | if (format_byte) |
| 952 | | *format_byte = header.format_byte; |
| 953 | | return FLOPPY_ERROR_SUCCESS; |
| 954 | | } |
| 955 | | |
| 956 | | |
| 957 | | |
| 958 | | static FLOPPY_IDENTIFY(apple35_diskcopy_identify) |
| 959 | | { |
| 960 | | *vote = apple35_diskcopy_headerdecode(floppy, NULL, NULL, NULL, NULL, NULL, NULL) ? 0 : 100; |
| 961 | | return FLOPPY_ERROR_SUCCESS; |
| 962 | | } |
| 963 | | |
| 964 | | |
| 965 | | |
| 966 | | |
| 967 | | static FLOPPY_CONSTRUCT(apple35_diskcopy_construct) |
| 968 | | { |
| 969 | | floperr_t err; |
| 970 | | UINT8 format_byte, sides; |
| 971 | | UINT32 data_offset, data_size; |
| 972 | | UINT32 tag_offset, tag_size; |
| 973 | | INT16 format_byte_param = -1; |
| 974 | | struct header_diskcopy header; |
| 975 | | |
| 976 | | if (params) |
| 977 | | { |
| 978 | | /* create */ |
| 979 | | sides = option_resolution_lookup_int(params, PARAM_HEADS); |
| 980 | | |
| 981 | | data_size = 80*sides*10*512; |
| 982 | | tag_size = 80*sides*10*12; |
| 983 | | |
| 984 | | memset(&header, 0, sizeof(header)); |
| 985 | | header.data_size = BIG_ENDIANIZE_INT32(data_size); |
| 986 | | header.tag_size = BIG_ENDIANIZE_INT32(tag_size); |
| 987 | | header.disk_format = (sides > 1) ? 1 : 0; |
| 988 | | header.magic = BIG_ENDIANIZE_INT16(0x100); |
| 989 | | |
| 990 | | floppy_image_write(floppy, &header, 0, sizeof(header)); |
| 991 | | floppy_image_write_filler(floppy, 0, sizeof(header), data_size + tag_size); |
| 992 | | } |
| 993 | | |
| 994 | | /* load */ |
| 995 | | err = apple35_diskcopy_headerdecode(floppy, &data_offset, &data_size, |
| 996 | | &tag_offset, &tag_size, &format_byte, &sides); |
| 997 | | if (err) |
| 998 | | return err; |
| 999 | | |
| 1000 | | format_byte_param = format_byte; |
| 1001 | | |
| 1002 | | return apple35_construct(floppy, data_offset, data_size, |
| 1003 | | tag_offset, tag_size, format_byte_param, sides, FALSE); |
| 1004 | | } |
| 1005 | | |
| 1006 | | |
| 1007 | | |
| 1008 | | /* ----------------------------------------------------------------------- */ |
| 1009 | | |
| 1010 | | struct header_2img |
| 1011 | | { |
| 1012 | | char magic[4]; /* '2IMG' */ |
| 1013 | | char creator[4]; /* signature; 'MESS' for MESS */ |
| 1014 | | UINT16 header_length; |
| 1015 | | UINT16 version; |
| 1016 | | UINT32 image_format; |
| 1017 | | UINT32 flags; |
| 1018 | | UINT32 block_count; |
| 1019 | | UINT32 data_offset; |
| 1020 | | UINT32 data_length; |
| 1021 | | UINT32 comment_offset; |
| 1022 | | UINT32 comment_length; |
| 1023 | | UINT32 creator_offset; |
| 1024 | | UINT32 creator_length; |
| 1025 | | UINT32 padding[4]; |
| 1026 | | }; |
| 1027 | | |
| 1028 | | #define IMAGE_FORMAT_DO 0 |
| 1029 | | #define IMAGE_FORMAT_PO 1 |
| 1030 | | #define IMAGE_FORMAT_NIB 2 |
| 1031 | | #define IMAGE_FLAGS_LOCKED 0x80000000 |
| 1032 | | |
| 1033 | | |
| 1034 | | |
| 1035 | | static floperr_t apple35_2img_decode(floppy_image *floppy, UINT32 *image_format, |
| 1036 | | UINT32 *data_offset, UINT32 *data_length) |
| 1037 | | { |
| 1038 | | struct header_2img header; |
| 1039 | | UINT64 size; |
| 1040 | | |
| 1041 | | if (image_format) |
| 1042 | | *image_format = 0; |
| 1043 | | if (data_offset) |
| 1044 | | *data_offset = 0; |
| 1045 | | if (data_length) |
| 1046 | | *data_length = 0; |
| 1047 | | |
| 1048 | | size = floppy_image_size(floppy); |
| 1049 | | if (size < sizeof(header)) |
| 1050 | | { |
| 1051 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1052 | | } |
| 1053 | | |
| 1054 | | floppy_image_read(floppy, &header, 0, sizeof(header)); |
| 1055 | | |
| 1056 | | if (memcmp(header.magic, "2IMG", 4)) |
| 1057 | | { |
| 1058 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1059 | | } |
| 1060 | | |
| 1061 | | header.header_length = LITTLE_ENDIANIZE_INT16(header.header_length); |
| 1062 | | header.version = LITTLE_ENDIANIZE_INT16(header.version); |
| 1063 | | header.image_format = LITTLE_ENDIANIZE_INT32(header.image_format); |
| 1064 | | header.flags = LITTLE_ENDIANIZE_INT32(header.flags); |
| 1065 | | header.block_count = LITTLE_ENDIANIZE_INT32(header.block_count); |
| 1066 | | header.data_offset = LITTLE_ENDIANIZE_INT32(header.data_offset); |
| 1067 | | header.data_length = LITTLE_ENDIANIZE_INT32(header.data_length); |
| 1068 | | header.comment_offset = LITTLE_ENDIANIZE_INT32(header.comment_offset); |
| 1069 | | header.comment_length = LITTLE_ENDIANIZE_INT32(header.comment_length); |
| 1070 | | header.creator_offset = LITTLE_ENDIANIZE_INT32(header.creator_offset); |
| 1071 | | header.creator_length = LITTLE_ENDIANIZE_INT32(header.creator_length); |
| 1072 | | |
| 1073 | | // at least some images "in the wild" (e.g. TOSEC Minor Set 1) have big-endian data sizes |
| 1074 | | // even though that's against the .2mg spec |
| 1075 | | if (header.data_length == 0x800c00) |
| 1076 | | { |
| 1077 | | logerror("ap_dsk35: corrected bad-endian data length\n"); |
| 1078 | | header.data_length = 0x0c8000; |
| 1079 | | } |
| 1080 | | |
| 1081 | | if ((((UINT64) header.data_offset) + header.data_length) > size) |
| 1082 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1083 | | if ((((UINT64) header.comment_offset) + header.comment_length) > size) |
| 1084 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1085 | | if ((((UINT64) header.creator_offset) + header.creator_length) > size) |
| 1086 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1087 | | if ((header.image_format != IMAGE_FORMAT_DO) && |
| 1088 | | (header.image_format != IMAGE_FORMAT_PO) && |
| 1089 | | (header.image_format != IMAGE_FORMAT_NIB)) |
| 1090 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1091 | | |
| 1092 | | if (image_format) |
| 1093 | | *image_format = header.image_format; |
| 1094 | | if (data_offset) |
| 1095 | | *data_offset = header.data_offset; |
| 1096 | | if (data_length) |
| 1097 | | *data_length = header.data_length; |
| 1098 | | return FLOPPY_ERROR_SUCCESS; |
| 1099 | | } |
| 1100 | | |
| 1101 | | |
| 1102 | | |
| 1103 | | static FLOPPY_IDENTIFY(apple35_2img_identify) |
| 1104 | | { |
| 1105 | | *vote = apple35_2img_decode(floppy, NULL, NULL, NULL) ? 0 : 100; |
| 1106 | | return FLOPPY_ERROR_SUCCESS; |
| 1107 | | } |
| 1108 | | |
| 1109 | | |
| 1110 | | |
| 1111 | | static FLOPPY_CONSTRUCT(apple35_2img_construct) |
| 1112 | | { |
| 1113 | | floperr_t err; |
| 1114 | | UINT32 image_format; |
| 1115 | | UINT32 data_offset; |
| 1116 | | UINT32 data_size; |
| 1117 | | UINT8 sides = 2; |
| 1118 | | struct header_2img header; |
| 1119 | | |
| 1120 | | if (params) |
| 1121 | | { |
| 1122 | | /* create */ |
| 1123 | | sides = option_resolution_lookup_int(params, PARAM_HEADS); |
| 1124 | | |
| 1125 | | data_offset = sizeof(header); |
| 1126 | | data_size = 80*sides*10*512; |
| 1127 | | |
| 1128 | | memset(&header, 0, sizeof(header)); |
| 1129 | | header.header_length = LITTLE_ENDIANIZE_INT16(sizeof(header)); |
| 1130 | | header.block_count = LITTLE_ENDIANIZE_INT32(80*sides*10); |
| 1131 | | header.data_offset = LITTLE_ENDIANIZE_INT32(data_offset); |
| 1132 | | header.data_length = LITTLE_ENDIANIZE_INT32(data_size); |
| 1133 | | |
| 1134 | | floppy_image_write(floppy, &header, 0, sizeof(header)); |
| 1135 | | floppy_image_write_filler(floppy, 0, sizeof(header), data_size); |
| 1136 | | } |
| 1137 | | else |
| 1138 | | { |
| 1139 | | /* load */ |
| 1140 | | err = apple35_2img_decode(floppy, &image_format, &data_offset, &data_size); |
| 1141 | | if (err) |
| 1142 | | return err; |
| 1143 | | |
| 1144 | | if (data_size == 80*1*10*512) |
| 1145 | | sides = 1; /* single sided */ |
| 1146 | | else if (data_size == 80*2*10*512) |
| 1147 | | sides = 2; /* double sided */ |
| 1148 | | else |
| 1149 | | sides = 2; /* unknown... what to do... */ |
| 1150 | | } |
| 1151 | | |
| 1152 | | return apple35_construct(floppy, data_offset, data_size, |
| 1153 | | 0, 0, -1, sides, FALSE); |
| 1154 | | } |
| 1155 | | |
| 1156 | | |
| 1157 | | |
| 1158 | | FLOPPY_OPTIONS_START( apple35_mac ) |
| 1159 | | FLOPPY_OPTION( apple35_raw, "dsk,img,image", "Apple raw 3.5\" disk image", apple35_raw_identify, apple35_raw_construct, NULL, |
| 1160 | | HEADS([1]-2) |
| 1161 | | TRACKS([80]) |
| 1162 | | SECTOR_LENGTH([512]) |
| 1163 | | FIRST_SECTOR_ID([0])) |
| 1164 | | FLOPPY_OPTION( apple35_dc, "dc,dsk,img,image", "Apple DiskCopy disk image", apple35_diskcopy_identify, apple35_diskcopy_construct, NULL, |
| 1165 | | HEADS([1]-2) |
| 1166 | | TRACKS([80]) |
| 1167 | | SECTOR_LENGTH([512]) |
| 1168 | | FIRST_SECTOR_ID([0])) |
| 1169 | | FLOPPY_OPTIONS_END |
| 1170 | | |
| 1171 | | FLOPPY_OPTIONS_START( apple35_iigs ) |
| 1172 | | FLOPPY_OPTION( apple35_raw, "dsk,img,image", "Apple raw 3.5\" disk image", apple35_raw_identify, apple35_raw_construct, NULL, |
| 1173 | | HEADS([1]-2) |
| 1174 | | TRACKS([80]) |
| 1175 | | SECTOR_LENGTH([512]) |
| 1176 | | FIRST_SECTOR_ID([0])) |
| 1177 | | FLOPPY_OPTION( apple35_dc, "dc,dsk,img,image", "Apple DiskCopy disk image", apple35_diskcopy_identify, apple35_diskcopy_construct, NULL, |
| 1178 | | HEADS([1]-2) |
| 1179 | | TRACKS([80]) |
| 1180 | | SECTOR_LENGTH([512]) |
| 1181 | | FIRST_SECTOR_ID([0])) |
| 1182 | | FLOPPY_OPTION( apple35_2img, "2img,2mg", "Apple ][gs 2IMG disk image", apple35_2img_identify, apple35_2img_construct, NULL, |
| 1183 | | HEADS([1]-2) |
| 1184 | | TRACKS([80]) |
| 1185 | | SECTOR_LENGTH([512]) |
| 1186 | | FIRST_SECTOR_ID([0])) |
| 1187 | | FLOPPY_OPTIONS_END |
src/mess/formats/d64_dsk.c
| r11457 | r11458 | |
| 1 | | /********************************************************************* |
| 2 | | |
| 3 | | formats/d64_dsk.c |
| 4 | | |
| 5 | | Floppy format code for Commodore 1541/2040/8050 disk images |
| 6 | | |
| 7 | | *********************************************************************/ |
| 8 | | |
| 9 | | /* |
| 10 | | |
| 11 | | TODO: |
| 12 | | |
| 13 | | - fix MAX_ERROR_SECTORS, it is too small for d80/d82 |
| 14 | | - write to disk |
| 15 | | - disk errors 24, 25, 26, 28, 74 |
| 16 | | - variable gaps |
| 17 | | |
| 18 | | */ |
| 19 | | |
| 20 | | #include "emu.h" |
| 21 | | #include "g64_dsk.h" |
| 22 | | #include "imagedev/flopimg.h" |
| 23 | | #include "formats/d64_dsk.h" |
| 24 | | #include "imagedev/flopdrv.h" |
| 25 | | |
| 26 | | /*************************************************************************** |
| 27 | | PARAMETERS |
| 28 | | ***************************************************************************/ |
| 29 | | |
| 30 | | #define LOG 1 |
| 31 | | |
| 32 | | #define MAX_HEADS 2 |
| 33 | | #define MAX_TRACKS 84 |
| 34 | | #define MAX_ERROR_SECTORS 802 // TODO this is too small for .d80 files |
| 35 | | #define SECTOR_SIZE 256 |
| 36 | | #define SECTOR_SIZE_GCR 368 |
| 37 | | |
| 38 | | #define INVALID_OFFSET 0xbadbad |
| 39 | | |
| 40 | | #define D64_SIZE_35_TRACKS 174848 |
| 41 | | #define D64_SIZE_35_TRACKS_WITH_ERRORS 175531 |
| 42 | | #define D64_SIZE_40_TRACKS 196608 |
| 43 | | #define D64_SIZE_40_TRACKS_WITH_ERRORS 197376 |
| 44 | | #define D64_SIZE_42_TRACKS 205312 |
| 45 | | #define D64_SIZE_42_TRACKS_WITH_ERRORS 206114 |
| 46 | | #define D67_SIZE_35_TRACKS 176640 |
| 47 | | #define D71_SIZE_70_TRACKS 349696 |
| 48 | | #define D71_SIZE_70_TRACKS_WITH_ERRORS 351062 |
| 49 | | #define D80_SIZE_77_TRACKS 533248 |
| 50 | | #define D82_SIZE_154_TRACKS 1066496 |
| 51 | | |
| 52 | | enum |
| 53 | | { |
| 54 | | DOS1, |
| 55 | | DOS2, |
| 56 | | DOS25 |
| 57 | | }; |
| 58 | | |
| 59 | | static const char *const DOS_VERSION[] = { "1.0", "2.0", "2.5" }; |
| 60 | | |
| 61 | | enum |
| 62 | | { |
| 63 | | ERROR_00 = 1, |
| 64 | | ERROR_20, /* header block not found */ |
| 65 | | ERROR_21, /* no sync character */ |
| 66 | | ERROR_22, /* data block not present */ |
| 67 | | ERROR_23, /* checksum error in data block */ |
| 68 | | ERROR_24, /* write verify (on format) UNIMPLEMENTED */ |
| 69 | | ERROR_25, /* write verify error UNIMPLEMENTED */ |
| 70 | | ERROR_26, /* write protect on UNIMPLEMENTED */ |
| 71 | | ERROR_27, /* checksum error in header block */ |
| 72 | | ERROR_28, /* write error UNIMPLEMENTED */ |
| 73 | | ERROR_29, /* disk ID mismatch */ |
| 74 | | ERROR_74, /* disk not ready (no device 1) UNIMPLEMENTED */ |
| 75 | | }; |
| 76 | | |
| 77 | | static const char *const ERROR_CODE[] = { "00", "00", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "74" }; |
| 78 | | |
| 79 | | static const UINT8 bin_2_gcr[] = |
| 80 | | { |
| 81 | | 0x0a, 0x0b, 0x12, 0x13, 0x0e, 0x0f, 0x16, 0x17, |
| 82 | | 0x09, 0x19, 0x1a, 0x1b, 0x0d, 0x1d, 0x1e, 0x15 |
| 83 | | }; |
| 84 | | |
| 85 | | /* This could be of use if we ever implement saving in .d64 format, to convert back GCR -> d64 */ |
| 86 | | /* |
| 87 | | static const int gcr_2_bin[] = |
| 88 | | { |
| 89 | | -1, -1, -1, -1, |
| 90 | | -1, -1, -1, -1, |
| 91 | | -1, 0x08, 0x00, 0x01, |
| 92 | | -1, 0x0c, 0x04, 0x05, |
| 93 | | -1, -1, 0x02, 0x03, |
| 94 | | -1, 0x0f, 0x06, 0x07, |
| 95 | | -1, 0x09, 0x0a, 0x0b, |
| 96 | | -1, 0x0d, 0x0e, -1 |
| 97 | | }; |
| 98 | | */ |
| 99 | | |
| 100 | | static const int DOS1_SECTORS_PER_TRACK[] = |
| 101 | | { |
| 102 | | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, |
| 103 | | 20, 20, 20, 20, 20, 20, 20, |
| 104 | | 18, 18, 18, 18, 18, 18, |
| 105 | | 17, 17, 17, 17, 17, |
| 106 | | 17, 17, 17, 17, 17, |
| 107 | | 17, 17 |
| 108 | | }; |
| 109 | | |
| 110 | | static const int DOS1_SPEED_ZONE[] = |
| 111 | | { |
| 112 | | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 113 | | 2, 2, 2, 2, 2, 2, 2, |
| 114 | | 1, 1, 1, 1, 1, 1, |
| 115 | | 0, 0, 0, 0, 0, |
| 116 | | 0, 0, 0, 0, 0, |
| 117 | | 0, 0 |
| 118 | | }; |
| 119 | | |
| 120 | | static const int DOS2_SECTORS_PER_TRACK[] = |
| 121 | | { |
| 122 | | 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, |
| 123 | | 19, 19, 19, 19, 19, 19, 19, |
| 124 | | 18, 18, 18, 18, 18, 18, |
| 125 | | 17, 17, 17, 17, 17, |
| 126 | | 17, 17, 17, 17, 17, |
| 127 | | 17, 17 |
| 128 | | }; |
| 129 | | |
| 130 | | static const int DOS25_SECTORS_PER_TRACK[] = |
| 131 | | { |
| 132 | | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, |
| 133 | | 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, /* 1-39 */ |
| 134 | | 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, 27, /* 40-53 */ |
| 135 | | 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, /* 54-64 */ |
| 136 | | 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, /* 65-77 */ |
| 137 | | 23, 23, 23, 23, 23, 23, 23 /* 78-84 */ |
| 138 | | }; |
| 139 | | |
| 140 | | static const int DOS25_SPEED_ZONE[] = |
| 141 | | { |
| 142 | | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, |
| 143 | | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 1-39 */ |
| 144 | | 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 40-53 */ |
| 145 | | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 54-64 */ |
| 146 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 65-77 */ |
| 147 | | 0, 0, 0, 0, 0, 0, 0 /* 78-84 */ |
| 148 | | }; |
| 149 | | |
| 150 | | /*************************************************************************** |
| 151 | | TYPE DEFINITIONS |
| 152 | | ***************************************************************************/ |
| 153 | | |
| 154 | | struct d64dsk_tag |
| 155 | | { |
| 156 | | int dos; /* CBM DOS version */ |
| 157 | | int heads; /* number of physical heads */ |
| 158 | | int tracks; /* number of physical tracks */ |
| 159 | | int dos_tracks; /* number of logical tracks */ |
| 160 | | int track_offset[MAX_HEADS][MAX_TRACKS]; /* offset within image for each physical track */ |
| 161 | | UINT32 speed_zone[MAX_TRACKS]; /* speed zone for each physical track */ |
| 162 | | bool has_errors; /* flag to check for available error codes */ |
| 163 | | UINT8 error[MAX_ERROR_SECTORS]; /* error code for each logical sector */ |
| 164 | | int error_offset[MAX_HEADS][MAX_TRACKS]; /* offset within error array for sector 0 of each logical track */ |
| 165 | | |
| 166 | | UINT8 id1, id2; /* DOS disk format ID */ |
| 167 | | }; |
| 168 | | |
| 169 | | /*************************************************************************** |
| 170 | | INLINE FUNCTIONS |
| 171 | | ***************************************************************************/ |
| 172 | | |
| 173 | | INLINE struct d64dsk_tag *get_tag(floppy_image *floppy) |
| 174 | | { |
| 175 | | return (d64dsk_tag *)floppy_tag(floppy); |
| 176 | | } |
| 177 | | |
| 178 | | INLINE float get_dos_track(int track) |
| 179 | | { |
| 180 | | return ((float)track / 2) + 1; |
| 181 | | } |
| 182 | | |
| 183 | | /*************************************************************************** |
| 184 | | IMPLEMENTATION |
| 185 | | ***************************************************************************/ |
| 186 | | |
| 187 | | /*------------------------------------------------- |
| 188 | | d64_get_heads_per_disk - returns the number |
| 189 | | of heads in the disk image |
| 190 | | -------------------------------------------------*/ |
| 191 | | |
| 192 | | static int d64_get_heads_per_disk(floppy_image *floppy) |
| 193 | | { |
| 194 | | return get_tag(floppy)->heads; |
| 195 | | } |
| 196 | | |
| 197 | | /*------------------------------------------------- |
| 198 | | d64_get_tracks_per_disk - returns the number |
| 199 | | of DOS tracks in the disk image |
| 200 | | -------------------------------------------------*/ |
| 201 | | |
| 202 | | static int d64_get_tracks_per_disk(floppy_image *floppy) |
| 203 | | { |
| 204 | | return get_tag(floppy)->tracks; |
| 205 | | } |
| 206 | | |
| 207 | | /*------------------------------------------------- |
| 208 | | d64_get_sectors_per_track - returns the number |
| 209 | | of sectors per given track |
| 210 | | -------------------------------------------------*/ |
| 211 | | |
| 212 | | static int d64_get_sectors_per_track(floppy_image *floppy, int head, int track) |
| 213 | | { |
| 214 | | int sectors_per_track = 0; |
| 215 | | |
| 216 | | switch (get_tag(floppy)->dos) |
| 217 | | { |
| 218 | | case DOS1: sectors_per_track = DOS1_SECTORS_PER_TRACK[track / 2]; break; |
| 219 | | case DOS2: sectors_per_track = DOS2_SECTORS_PER_TRACK[track / 2]; break; |
| 220 | | case DOS25: sectors_per_track = DOS25_SECTORS_PER_TRACK[track]; break; |
| 221 | | } |
| 222 | | |
| 223 | | return sectors_per_track; |
| 224 | | } |
| 225 | | |
| 226 | | /*------------------------------------------------- |
| 227 | | get_track_offset - returns the offset within |
| 228 | | the disk image for a given track |
| 229 | | -------------------------------------------------*/ |
| 230 | | |
| 231 | | static floperr_t get_track_offset(floppy_image *floppy, int head, int track, UINT64 *offset) |
| 232 | | { |
| 233 | | struct d64dsk_tag *tag = get_tag(floppy); |
| 234 | | UINT64 offs = 0; |
| 235 | | |
| 236 | | if ((track < 0) || (track >= tag->tracks)) |
| 237 | | return FLOPPY_ERROR_SEEKERROR; |
| 238 | | |
| 239 | | offs = tag->track_offset[head][track]; |
| 240 | | |
| 241 | | if (offset) |
| 242 | | *offset = offs; |
| 243 | | |
| 244 | | return FLOPPY_ERROR_SUCCESS; |
| 245 | | } |
| 246 | | |
| 247 | | /*------------------------------------------------- |
| 248 | | d64_get_track_size - returns the track size |
| 249 | | -------------------------------------------------*/ |
| 250 | | |
| 251 | | static UINT32 d64_get_track_size(floppy_image *floppy, int head, int track) |
| 252 | | { |
| 253 | | struct d64dsk_tag *tag = get_tag(floppy); |
| 254 | | |
| 255 | | if (tag->track_offset[head][track] == INVALID_OFFSET) |
| 256 | | return 0; |
| 257 | | |
| 258 | | /* determine number of sectors per track */ |
| 259 | | int sectors_per_track = d64_get_sectors_per_track(floppy, head, track); |
| 260 | | |
| 261 | | /* allocate temporary GCR track data buffer */ |
| 262 | | UINT32 track_length = sectors_per_track * SECTOR_SIZE_GCR; |
| 263 | | |
| 264 | | return track_length; |
| 265 | | } |
| 266 | | |
| 267 | | /*------------------------------------------------- |
| 268 | | get_sector_error_code - returns the error |
| 269 | | code for the given sector |
| 270 | | -------------------------------------------------*/ |
| 271 | | |
| 272 | | static int get_sector_error_code(floppy_image *floppy, int head, int dos_track, int sector) |
| 273 | | { |
| 274 | | struct d64dsk_tag *tag = get_tag(floppy); |
| 275 | | |
| 276 | | if (!tag->has_errors) |
| 277 | | return ERROR_00; |
| 278 | | |
| 279 | | int sector_error = tag->error[tag->error_offset[head][dos_track] + sector]; // TODO index out of bounds!!! |
| 280 | | |
| 281 | | if (sector_error != ERROR_00) |
| 282 | | { |
| 283 | | logerror("D64 error %s head %u track %u sector %u\n", ERROR_CODE[sector_error], head, dos_track, sector); |
| 284 | | } |
| 285 | | |
| 286 | | return sector_error; |
| 287 | | } |
| 288 | | |
| 289 | | /*------------------------------------------------- |
| 290 | | gcr_double_2_gcr - GCR decodes given data |
| 291 | | -------------------------------------------------*/ |
| 292 | | |
| 293 | | /* gcr_double_2_gcr takes 4 bytes (a, b, c, d) and shuffles their nibbles to obtain 5 bytes in dest */ |
| 294 | | /* The result is basically res = (enc(a) << 15) | (enc(b) << 10) | (enc(c) << 5) | enc(d) |
| 295 | | * with res being 5 bytes long and enc(x) being the GCR encode of x. |
| 296 | | * In fact, we store the result as five separate bytes in the dest argument |
| 297 | | */ |
| 298 | | |
| 299 | | /* |
| 300 | | |
| 301 | | Commodore GCR format |
| 302 | | |
| 303 | | Original Encoded |
| 304 | | 4 bits 5 bits |
| 305 | | |
| 306 | | 0000 -> 01010 = 0x0a |
| 307 | | 0001 -> 01011 = 0x0b |
| 308 | | 0010 -> 10010 = 0x12 |
| 309 | | 0011 -> 10011 = 0x13 |
| 310 | | 0100 -> 01110 = 0x0e |
| 311 | | 0101 -> 01111 = 0x0f |
| 312 | | 0110 -> 10110 = 0x16 |
| 313 | | 0111 -> 10111 = 0x17 |
| 314 | | 1000 -> 01001 = 0x09 |
| 315 | | 1001 -> 11001 = 0x19 |
| 316 | | 1010 -> 11010 = 0x1a |
| 317 | | 1011 -> 11011 = 0x1b |
| 318 | | 1100 -> 01101 = 0x0d |
| 319 | | 1101 -> 11101 = 0x1d |
| 320 | | 1110 -> 11110 = 0x1e |
| 321 | | 1111 -> 10101 = 0x15 |
| 322 | | |
| 323 | | We use the encoded values in bytes because we use them to encode |
| 324 | | groups of 4 bytes into groups of 5 bytes, below. |
| 325 | | |
| 326 | | */ |
| 327 | | |
| 328 | | static void gcr_double_2_gcr(UINT8 a, UINT8 b, UINT8 c, UINT8 d, UINT8 *dest) |
| 329 | | { |
| 330 | | UINT8 gcr[8]; |
| 331 | | |
| 332 | | /* Encode each nibble to 5 bits */ |
| 333 | | gcr[0] = bin_2_gcr[a >> 4]; |
| 334 | | gcr[1] = bin_2_gcr[a & 0x0f]; |
| 335 | | gcr[2] = bin_2_gcr[b >> 4]; |
| 336 | | gcr[3] = bin_2_gcr[b & 0x0f]; |
| 337 | | gcr[4] = bin_2_gcr[c >> 4]; |
| 338 | | gcr[5] = bin_2_gcr[c & 0x0f]; |
| 339 | | gcr[6] = bin_2_gcr[d >> 4]; |
| 340 | | gcr[7] = bin_2_gcr[d & 0x0f]; |
| 341 | | |
| 342 | | /* Re-order the encoded data to only keep the 5 lower bits of each byte */ |
| 343 | | dest[0] = (gcr[0] << 3) | (gcr[1] >> 2); |
| 344 | | dest[1] = (gcr[1] << 6) | (gcr[2] << 1) | (gcr[3] >> 4); |
| 345 | | dest[2] = (gcr[3] << 4) | (gcr[4] >> 1); |
| 346 | | dest[3] = (gcr[4] << 7) | (gcr[5] << 2) | (gcr[6] >> 3); |
| 347 | | dest[4] = (gcr[6] << 5) | gcr[7]; |
| 348 | | } |
| 349 | | |
| 350 | | /*------------------------------------------------- |
| 351 | | d64_read_track - reads a full track from the |
| 352 | | disk image |
| 353 | | -------------------------------------------------*/ |
| 354 | | |
| 355 | | /* |
| 356 | | |
| 357 | | Commodore sector format |
| 358 | | |
| 359 | | SYNC FF * 5 |
| 360 | | 08 |
| 361 | | CHECKSUM sector ^ track ^ id2 ^ id1 |
| 362 | | SECTOR 0..20 (2040), 0..28 (8050) |
| 363 | | TRACK 1..35 (2040), 1..77 (8050), 1..70 (1571) |
| 364 | | ID2 |
| 365 | | ID1 |
| 366 | | GAP 1 55 * 9 (2040), 55 * 8 (1541) |
| 367 | | |
| 368 | | SYNC FF * 5 |
| 369 | | 07 |
| 370 | | NEXT TRACK |
| 371 | | NEXT SECTOR |
| 372 | | 254 BYTES OF DATA |
| 373 | | CHECKSUM |
| 374 | | GAP 2 55 * 8..19 |
| 375 | | |
| 376 | | */ |
| 377 | | |
| 378 | | static floperr_t d64_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 379 | | { |
| 380 | | struct d64dsk_tag *tag = get_tag(floppy); |
| 381 | | floperr_t err; |
| 382 | | UINT64 track_offset; |
| 383 | | |
| 384 | | /* get track offset */ |
| 385 | | err = get_track_offset(floppy, head, track, &track_offset); |
| 386 | | |
| 387 | | if (err) |
| 388 | | return err; |
| 389 | | |
| 390 | | if (track_offset != INVALID_OFFSET) |
| 391 | | { |
| 392 | | UINT8 id1 = tag->id1; |
| 393 | | UINT8 id2 = tag->id2; |
| 394 | | int sectors_per_track; |
| 395 | | UINT16 d64_track_size; |
| 396 | | UINT8 *d64_track_data; |
| 397 | | UINT16 gcr_track_size; |
| 398 | | UINT8 *gcr_track_data; |
| 399 | | UINT64 gcr_pos = 0; |
| 400 | | |
| 401 | | /* determine logical track number */ |
| 402 | | int dos_track = get_dos_track(track); |
| 403 | | |
| 404 | | if (tag->dos == DOS25) |
| 405 | | { |
| 406 | | dos_track = track + 1; |
| 407 | | } |
| 408 | | |
| 409 | | /* logical track numbers continue on the flip side */ |
| 410 | | if (head == 1) dos_track += tag->dos_tracks; |
| 411 | | |
| 412 | | /* determine number of sectors per track */ |
| 413 | | sectors_per_track = d64_get_sectors_per_track(floppy, head, track); |
| 414 | | |
| 415 | | /* allocate D64 track data buffer */ |
| 416 | | d64_track_size = sectors_per_track * SECTOR_SIZE; |
| 417 | | d64_track_data = (UINT8 *)alloca(d64_track_size); |
| 418 | | |
| 419 | | /* allocate temporary GCR track data buffer */ |
| 420 | | gcr_track_size = sectors_per_track * SECTOR_SIZE_GCR; |
| 421 | | gcr_track_data = (UINT8 *)alloca(gcr_track_size); |
| 422 | | |
| 423 | | if (buflen < gcr_track_size) fatalerror("D64 track buffer too small: %u!", (UINT32)buflen); |
| 424 | | |
| 425 | | /* read D64 track data */ |
| 426 | | floppy_image_read(floppy, d64_track_data, track_offset, d64_track_size); |
| 427 | | |
| 428 | | /* GCR encode D64 sector data */ |
| 429 | | for (int sector = 0; sector < sectors_per_track; sector++) |
| 430 | | { |
| 431 | | // here we convert the sector data to gcr directly! |
| 432 | | // IMPORTANT: errors in reading sectors can modify e.g. header info $01 & $05 |
| 433 | | int sector_error = get_sector_error_code(floppy, head, track, sector); |
| 434 | | |
| 435 | | /* first we set the position at which sector data starts in the image */ |
| 436 | | UINT64 d64_pos = sector * SECTOR_SIZE; |
| 437 | | int i; |
| 438 | | |
| 439 | | /* |
| 440 | | 1. Header sync FF FF FF FF FF (40 'on' bits, not GCR encoded) |
| 441 | | 2. Header info 52 54 B5 29 4B 7A 5E 95 55 55 (10 GCR bytes) |
| 442 | | 3. Header gap 55 55 55 55 55 55 55 55 55 (9 bytes, never read) |
| 443 | | 4. Data sync FF FF FF FF FF (40 'on' bits, not GCR encoded) |
| 444 | | 5. Data block 55...4A (325 GCR bytes) |
| 445 | | 6. Inter-sector gap 55 55 55 55...55 55 (4 to 19 bytes, never read) |
| 446 | | */ |
| 447 | | |
| 448 | | if (sector_error == ERROR_29) |
| 449 | | id1 ^= 0xff; |
| 450 | | |
| 451 | | /* Header sync */ |
| 452 | | if (sector_error != ERROR_21) |
| 453 | | { |
| 454 | | for (i = 0; i < 5; i++) |
| 455 | | gcr_track_data[gcr_pos + i] = 0xff; |
| 456 | | gcr_pos += 5; |
| 457 | | } |
| 458 | | |
| 459 | | /* Header info */ |
| 460 | | /* These are 8 bytes unencoded, which become 10 bytes encoded */ |
| 461 | | // $00 - header block ID ($08) // this byte can be modified by error code 20 -> 0xff |
| 462 | | // $01 - header block checksum (EOR of $02-$05) // this byte can be modified by error code 27 -> ^ 0xff |
| 463 | | // $02 - Sector# of data block |
| 464 | | // $03 - Track# of data block |
| 465 | | UINT8 header_block_id = (sector_error == ERROR_20) ? 0xff : 0x08; |
| 466 | | UINT8 header_block_checksum = sector ^ dos_track ^ id2 ^ id1; |
| 467 | | |
| 468 | | if (sector_error == ERROR_27) |
| 469 | | header_block_checksum ^= 0xff; |
| 470 | | |
| 471 | | gcr_double_2_gcr(header_block_id, header_block_checksum, sector, dos_track, gcr_track_data + gcr_pos); |
| 472 | | gcr_pos += 5; |
| 473 | | |
| 474 | | // $04 - Format ID byte #2 |
| 475 | | // $05 - Format ID byte #1 |
| 476 | | // $06 - $0F ("off" byte) |
| 477 | | // $07 - $0F ("off" byte) |
| 478 | | gcr_double_2_gcr(id2, id1, 0x0f, 0x0f, gcr_track_data + gcr_pos); |
| 479 | | gcr_pos += 5; |
| 480 | | |
| 481 | | /* Header gap */ |
| 482 | | for (i = 0; i < 9; i++) |
| 483 | | gcr_track_data[gcr_pos + i] = 0x55; |
| 484 | | gcr_pos += 9; |
| 485 | | |
| 486 | | /* Data sync */ |
| 487 | | for (i = 0; i < 5; i++) |
| 488 | | gcr_track_data[gcr_pos + i] = 0xff; |
| 489 | | gcr_pos += 5; |
| 490 | | |
| 491 | | /* Data block */ |
| 492 | | UINT8 data_block_id = (sector_error == ERROR_22) ? 0xff : 0x07; |
| 493 | | |
| 494 | | // we first need to calculate the checksum of the 256 bytes of the sector |
| 495 | | UINT8 sector_checksum = d64_track_data[d64_pos]; |
| 496 | | for (i = 1; i < 256; i++) |
| 497 | | sector_checksum ^= d64_track_data[d64_pos + i]; |
| 498 | | |
| 499 | | if (sector_error == ERROR_23) |
| 500 | | sector_checksum ^= 0xff; |
| 501 | | |
| 502 | | /* |
| 503 | | $00 - data block ID ($07) |
| 504 | | $01-100 - 256 bytes sector data |
| 505 | | $101 - data block checksum (EOR of $01-100) |
| 506 | | $102-103 - $00 ("off" bytes, to make the sector size a multiple of 5) |
| 507 | | */ |
| 508 | | gcr_double_2_gcr(data_block_id, d64_track_data[d64_pos], d64_track_data[d64_pos + 1], d64_track_data[d64_pos + 2], gcr_track_data + gcr_pos); |
| 509 | | gcr_pos += 5; |
| 510 | | |
| 511 | | for (i = 1; i < 64; i++) |
| 512 | | { |
| 513 | | gcr_double_2_gcr(d64_track_data[d64_pos + 4 * i - 1], d64_track_data[d64_pos + 4 * i], |
| 514 | | d64_track_data[d64_pos + 4 * i + 1], d64_track_data[d64_pos + 4 * i + 2], gcr_track_data + gcr_pos); |
| 515 | | gcr_pos += 5; |
| 516 | | } |
| 517 | | |
| 518 | | gcr_double_2_gcr(d64_track_data[d64_pos + 255], sector_checksum, 0x00, 0x00, gcr_track_data + gcr_pos); |
| 519 | | gcr_pos += 5; |
| 520 | | |
| 521 | | /* Inter-sector gap */ |
| 522 | | // "In tests that the author conducted on a real 1541 disk, gap sizes of 8 to 19 bytes were seen." |
| 523 | | // Here we put 14 as an average... |
| 524 | | for (i = 0; i < 14; i++) |
| 525 | | gcr_track_data[gcr_pos + i] = 0x55; |
| 526 | | gcr_pos += 14; |
| 527 | | } |
| 528 | | |
| 529 | | /* copy GCR track data to buffer */ |
| 530 | | memcpy(buffer, gcr_track_data, gcr_track_size); |
| 531 | | } |
| 532 | | else /* half tracks */ |
| 533 | | { |
| 534 | | /* set track length to 0 */ |
| 535 | | memset(buffer, 0, buflen); |
| 536 | | } |
| 537 | | |
| 538 | | return FLOPPY_ERROR_SUCCESS; |
| 539 | | } |
| 540 | | |
| 541 | | /*------------------------------------------------- |
| 542 | | d64_write_track - writes a full track to the |
| 543 | | disk image |
| 544 | | -------------------------------------------------*/ |
| 545 | | |
| 546 | | static floperr_t d64_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 547 | | { |
| 548 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 549 | | } |
| 550 | | |
| 551 | | /*------------------------------------------------- |
| 552 | | d64_identify - identifies the disk image |
| 553 | | -------------------------------------------------*/ |
| 554 | | |
| 555 | | static void d64_identify(floppy_image *floppy, int *dos, int *heads, int *tracks, bool *has_errors) |
| 556 | | { |
| 557 | | switch (floppy_image_size(floppy)) |
| 558 | | { |
| 559 | | /* 2040/3040 */ |
| 560 | | case D67_SIZE_35_TRACKS: *dos = DOS1; *heads = 1; *tracks = 35; *has_errors = false; break; |
| 561 | | |
| 562 | | /* 4040/2031/1541/1551 */ |
| 563 | | case D64_SIZE_35_TRACKS: *dos = DOS2; *heads = 1; *tracks = 35; *has_errors = false; break; |
| 564 | | case D64_SIZE_35_TRACKS_WITH_ERRORS: *dos = DOS2; *heads = 1; *tracks = 35; *has_errors = true; break; |
| 565 | | case D64_SIZE_40_TRACKS: *dos = DOS2; *heads = 1; *tracks = 40; *has_errors = false; break; |
| 566 | | case D64_SIZE_40_TRACKS_WITH_ERRORS: *dos = DOS2; *heads = 1; *tracks = 40; *has_errors = true; break; |
| 567 | | case D64_SIZE_42_TRACKS: *dos = DOS2; *heads = 1; *tracks = 42; *has_errors = false; break; |
| 568 | | case D64_SIZE_42_TRACKS_WITH_ERRORS: *dos = DOS2; *heads = 1; *tracks = 42; *has_errors = true; break; |
| 569 | | |
| 570 | | /* 1571 */ |
| 571 | | case D71_SIZE_70_TRACKS: *dos = DOS2; *heads = 2; *tracks = 35; *has_errors = false; break; |
| 572 | | case D71_SIZE_70_TRACKS_WITH_ERRORS: *dos = DOS2; *heads = 2; *tracks = 35; *has_errors = true; break; |
| 573 | | |
| 574 | | /* 8050 */ |
| 575 | | case D80_SIZE_77_TRACKS: *dos = DOS25; *heads = 1; *tracks = 77; *has_errors = false; break; |
| 576 | | |
| 577 | | /* 8250/SFD1001 */ |
| 578 | | case D82_SIZE_154_TRACKS: *dos = DOS25; *heads = 2; *tracks = 77; *has_errors = false; break; |
| 579 | | } |
| 580 | | } |
| 581 | | |
| 582 | | /*------------------------------------------------- |
| 583 | | FLOPPY_IDENTIFY( d64_dsk_identify ) |
| 584 | | -------------------------------------------------*/ |
| 585 | | |
| 586 | | FLOPPY_IDENTIFY( d64_dsk_identify ) |
| 587 | | { |
| 588 | | int dos = 0, heads, tracks; |
| 589 | | bool has_errors = false; |
| 590 | | |
| 591 | | *vote = 0; |
| 592 | | |
| 593 | | d64_identify(floppy, &dos, &heads, &tracks, &has_errors); |
| 594 | | |
| 595 | | if (dos == DOS2 && heads == 1) |
| 596 | | { |
| 597 | | *vote = 100; |
| 598 | | } |
| 599 | | |
| 600 | | return FLOPPY_ERROR_SUCCESS; |
| 601 | | } |
| 602 | | |
| 603 | | /*------------------------------------------------- |
| 604 | | FLOPPY_IDENTIFY( d67_dsk_identify ) |
| 605 | | -------------------------------------------------*/ |
| 606 | | |
| 607 | | FLOPPY_IDENTIFY( d67_dsk_identify ) |
| 608 | | { |
| 609 | | *vote = 0; |
| 610 | | |
| 611 | | if (floppy_image_size(floppy) == D67_SIZE_35_TRACKS) |
| 612 | | { |
| 613 | | *vote = 100; |
| 614 | | } |
| 615 | | |
| 616 | | return FLOPPY_ERROR_SUCCESS; |
| 617 | | } |
| 618 | | |
| 619 | | /*------------------------------------------------- |
| 620 | | FLOPPY_IDENTIFY( d71_dsk_identify ) |
| 621 | | -------------------------------------------------*/ |
| 622 | | |
| 623 | | FLOPPY_IDENTIFY( d71_dsk_identify ) |
| 624 | | { |
| 625 | | int heads = 0, tracks = 0, dos = -1; |
| 626 | | bool has_errors = false; |
| 627 | | |
| 628 | | *vote = 0; |
| 629 | | |
| 630 | | d64_identify(floppy, &dos, &heads, &tracks, &has_errors); |
| 631 | | |
| 632 | | if (dos == DOS2 && heads == 2) |
| 633 | | { |
| 634 | | *vote = 100; |
| 635 | | } |
| 636 | | |
| 637 | | return FLOPPY_ERROR_SUCCESS; |
| 638 | | } |
| 639 | | |
| 640 | | /*------------------------------------------------- |
| 641 | | FLOPPY_IDENTIFY( d80_dsk_identify ) |
| 642 | | -------------------------------------------------*/ |
| 643 | | |
| 644 | | FLOPPY_IDENTIFY( d80_dsk_identify ) |
| 645 | | { |
| 646 | | *vote = 0; |
| 647 | | |
| 648 | | if (floppy_image_size(floppy) == D80_SIZE_77_TRACKS) |
| 649 | | { |
| 650 | | *vote = 100; |
| 651 | | } |
| 652 | | |
| 653 | | return FLOPPY_ERROR_SUCCESS; |
| 654 | | } |
| 655 | | |
| 656 | | /*------------------------------------------------- |
| 657 | | FLOPPY_IDENTIFY( d82_dsk_identify ) |
| 658 | | -------------------------------------------------*/ |
| 659 | | |
| 660 | | FLOPPY_IDENTIFY( d82_dsk_identify ) |
| 661 | | { |
| 662 | | *vote = 0; |
| 663 | | |
| 664 | | if (floppy_image_size(floppy) == D82_SIZE_154_TRACKS) |
| 665 | | { |
| 666 | | *vote = 100; |
| 667 | | } |
| 668 | | |
| 669 | | return FLOPPY_ERROR_SUCCESS; |
| 670 | | } |
| 671 | | |
| 672 | | /*------------------------------------------------- |
| 673 | | FLOPPY_CONSTRUCT( d64_dsk_construct ) |
| 674 | | -------------------------------------------------*/ |
| 675 | | |
| 676 | | FLOPPY_CONSTRUCT( d64_dsk_construct ) |
| 677 | | { |
| 678 | | struct FloppyCallbacks *callbacks; |
| 679 | | struct d64dsk_tag *tag; |
| 680 | | UINT8 id[2]; |
| 681 | | |
| 682 | | int track_offset = 0; |
| 683 | | int head, track; |
| 684 | | |
| 685 | | int heads = 0, dos_tracks = 0, dos = 0; |
| 686 | | bool has_errors = 0; |
| 687 | | int errors_size = 0; |
| 688 | | |
| 689 | | if (params) |
| 690 | | { |
| 691 | | /* create not supported */ |
| 692 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 693 | | } |
| 694 | | |
| 695 | | tag = (struct d64dsk_tag *) floppy_create_tag(floppy, sizeof(struct d64dsk_tag)); |
| 696 | | |
| 697 | | if (!tag) return FLOPPY_ERROR_OUTOFMEMORY; |
| 698 | | |
| 699 | | /* identify image type */ |
| 700 | | d64_identify(floppy, &dos, &heads, &dos_tracks, &has_errors); |
| 701 | | |
| 702 | | tag->dos = dos; |
| 703 | | tag->heads = heads; |
| 704 | | tag->tracks = MAX_TRACKS; |
| 705 | | tag->dos_tracks = dos_tracks; |
| 706 | | tag->has_errors = has_errors; |
| 707 | | |
| 708 | | if (LOG) |
| 709 | | { |
| 710 | | logerror("D64 size: %04x\n", (UINT32)floppy_image_size(floppy)); |
| 711 | | logerror("D64 heads: %u\n", heads); |
| 712 | | logerror("D64 tracks: %u\n", dos_tracks); |
| 713 | | logerror("D64 DOS version: %s\n", DOS_VERSION[dos]); |
| 714 | | logerror("D64 error codes: %s\n", has_errors ? "yes" : "no"); |
| 715 | | } |
| 716 | | |
| 717 | | /* clear track data offsets */ |
| 718 | | for (head = 0; head < MAX_HEADS; head++) |
| 719 | | { |
| 720 | | for (track = 0; track < MAX_TRACKS; track++) |
| 721 | | { |
| 722 | | tag->track_offset[head][track] = INVALID_OFFSET; |
| 723 | | } |
| 724 | | } |
| 725 | | |
| 726 | | /* determine track data offsets */ |
| 727 | | for (head = 0; head < heads; head++) |
| 728 | | { |
| 729 | | for (track = 0; track < tag->tracks; track++) |
| 730 | | { |
| 731 | | if (dos == DOS25) |
| 732 | | { |
| 733 | | if (track >= dos_tracks) |
| 734 | | { |
| 735 | | /* track out of range */ |
| 736 | | tag->track_offset[head][track] = INVALID_OFFSET; |
| 737 | | } |
| 738 | | else |
| 739 | | { |
| 740 | | tag->track_offset[head][track] = track_offset; |
| 741 | | tag->error_offset[head][track] = errors_size; |
| 742 | | |
| 743 | | track_offset += DOS25_SECTORS_PER_TRACK[track] * SECTOR_SIZE; |
| 744 | | /* also store an error entry for each sector */ |
| 745 | | errors_size += DOS25_SECTORS_PER_TRACK[track]; |
| 746 | | |
| 747 | | if (LOG) |
| 748 | | { |
| 749 | | logerror("D64 head %u track %u data offset: %04x\n", head, track + 1, tag->track_offset[head][track]); |
| 750 | | if (has_errors) logerror("D64 head %u track %u error offset: %04x\n", head, track + 1, tag->error_offset[head][track]); |
| 751 | | } |
| 752 | | } |
| 753 | | } |
| 754 | | else |
| 755 | | { |
| 756 | | if ((track % 2) || ((track / 2) >= dos_tracks)) |
| 757 | | { |
| 758 | | /* half track or out of range */ |
| 759 | | tag->track_offset[head][track] = INVALID_OFFSET; |
| 760 | | } |
| 761 | | else |
| 762 | | { |
| 763 | | /* full track */ |
| 764 | | tag->track_offset[head][track] = track_offset; |
| 765 | | tag->error_offset[head][track] = errors_size; |
| 766 | | |
| 767 | | if (dos == DOS1) |
| 768 | | { |
| 769 | | track_offset += DOS1_SECTORS_PER_TRACK[track / 2] * SECTOR_SIZE; |
| 770 | | /* also store an error entry for each sector */ |
| 771 | | errors_size += DOS1_SECTORS_PER_TRACK[track / 2]; |
| 772 | | } |
| 773 | | else |
| 774 | | { |
| 775 | | track_offset += DOS2_SECTORS_PER_TRACK[track / 2] * SECTOR_SIZE; |
| 776 | | /* also store an error entry for each sector */ |
| 777 | | errors_size += DOS2_SECTORS_PER_TRACK[track / 2]; |
| 778 | | } |
| 779 | | |
| 780 | | if (LOG) |
| 781 | | { |
| 782 | | logerror("D64 head %u track %.1f data offset: %04x\n", head, get_dos_track(track), tag->track_offset[head][track]); |
| 783 | | if (has_errors) logerror("D64 head %u track %.1f error offset: %04x\n", head, get_dos_track(track), tag->error_offset[head][track]); |
| 784 | | } |
| 785 | | } |
| 786 | | } |
| 787 | | } |
| 788 | | } |
| 789 | | |
| 790 | | /* determine speed zones */ |
| 791 | | for (track = 0; track < tag->tracks; track++) |
| 792 | | { |
| 793 | | if (dos == DOS25) |
| 794 | | { |
| 795 | | tag->speed_zone[track] = DOS25_SPEED_ZONE[track]; |
| 796 | | |
| 797 | | if (LOG) logerror("D64 track %u speed zone: %u\n", track + 1, tag->speed_zone[track]); |
| 798 | | } |
| 799 | | else |
| 800 | | { |
| 801 | | tag->speed_zone[track] = DOS1_SPEED_ZONE[track / 2]; |
| 802 | | |
| 803 | | if (LOG) logerror("D64 track %.1f speed zone: %u\n", get_dos_track(track), tag->speed_zone[track]); |
| 804 | | } |
| 805 | | } |
| 806 | | |
| 807 | | /* read format ID from directory */ |
| 808 | | /* |
| 809 | | id1, id2 are the same for extended d64 (i.e. with error tables), for d67 and for d71 |
| 810 | | |
| 811 | | for d81 they are at track 40 bytes 0x17 & 0x18 |
| 812 | | for d80 & d82 they are at track 39 bytes 0x18 & 0x19 |
| 813 | | */ |
| 814 | | if (dos == DOS25) |
| 815 | | floppy_image_read(floppy, id, tag->track_offset[0][38] + 0x18, 2); |
| 816 | | else |
| 817 | | floppy_image_read(floppy, id, tag->track_offset[0][34] + 0xa2, 2); |
| 818 | | |
| 819 | | tag->id1 = id[0]; |
| 820 | | tag->id2 = id[1]; |
| 821 | | |
| 822 | | if (LOG) logerror("D64 format ID: %02x%02x\n", id[0], id[1]); |
| 823 | | |
| 824 | | /* read errors */ |
| 825 | | if (tag->has_errors) |
| 826 | | { |
| 827 | | if (LOG) logerror("D64 error blocks: %u %u\n", errors_size, track_offset); |
| 828 | | floppy_image_read(floppy, tag->error, track_offset, errors_size); |
| 829 | | } |
| 830 | | else |
| 831 | | { |
| 832 | | memset(tag->error, ERROR_00, MAX_ERROR_SECTORS); |
| 833 | | } |
| 834 | | |
| 835 | | /* set callbacks */ |
| 836 | | callbacks = floppy_callbacks(floppy); |
| 837 | | |
| 838 | | callbacks->read_track = d64_read_track; |
| 839 | | callbacks->write_track = d64_write_track; |
| 840 | | callbacks->get_heads_per_disk = d64_get_heads_per_disk; |
| 841 | | callbacks->get_tracks_per_disk = d64_get_tracks_per_disk; |
| 842 | | callbacks->get_track_size = d64_get_track_size; |
| 843 | | |
| 844 | | return FLOPPY_ERROR_SUCCESS; |
| 845 | | } |
src/mess/formats/thom_dsk.c
| r11457 | r11458 | |
| 1 | | /********************************************************************* |
| 2 | | |
| 3 | | formats/thom_dsk.c |
| 4 | | |
| 5 | | Thomson disk images |
| 6 | | |
| 7 | | Based on work of Antoine Mine' |
| 8 | | |
| 9 | | *********************************************************************/ |
| 10 | | |
| 11 | | #include <string.h> |
| 12 | | #include "emu.h" |
| 13 | | #include "formats/thom_dsk.h" |
| 14 | | #include "formats/basicdsk.h" |
| 15 | | |
| 16 | | static const int sap_magic_num = 0xB3; /* simple XOR crypt */ |
| 17 | | |
| 18 | | |
| 19 | | static const char sap_header[] = |
| 20 | | "\001SYSTEME D'ARCHIVAGE PUKALL S.A.P. " |
| 21 | | "(c) Alexandre PUKALL Avril 1998"; |
| 22 | | |
| 23 | | |
| 24 | | static const UINT16 sap_crc[] = |
| 25 | | { |
| 26 | | 0x0000, 0x1081, 0x2102, 0x3183, 0x4204, 0x5285, 0x6306, 0x7387, |
| 27 | | 0x8408, 0x9489, 0xa50a, 0xb58b, 0xc60c, 0xd68d, 0xe70e, 0xf78f, |
| 28 | | }; |
| 29 | | |
| 30 | | struct sap_dsk_tag |
| 31 | | { |
| 32 | | int tracks; |
| 33 | | int sector_size; |
| 34 | | int sector_pos[80][16]; /* remember sector position in file */ |
| 35 | | }; |
| 36 | | |
| 37 | | static UINT16 thom_sap_crc( UINT8* data, int size ) |
| 38 | | { |
| 39 | | int i; |
| 40 | | UINT16 crc = 0xffff, crc2; |
| 41 | | for ( i = 0; i < size; i++ ) |
| 42 | | { |
| 43 | | crc2 = ( crc >> 4 ) ^ sap_crc[ ( crc ^ data[i] ) & 15 ]; |
| 44 | | crc = ( crc2 >> 4 ) ^ sap_crc[ ( crc2 ^ (data[i] >> 4) ) & 15 ]; |
| 45 | | } |
| 46 | | return crc; |
| 47 | | } |
| 48 | | |
| 49 | | static struct sap_dsk_tag *get_tag(floppy_image *floppy) |
| 50 | | { |
| 51 | | struct sap_dsk_tag *tag; |
| 52 | | tag = (sap_dsk_tag *)floppy_tag(floppy); |
| 53 | | return tag; |
| 54 | | } |
| 55 | | |
| 56 | | |
| 57 | | static FLOPPY_IDENTIFY(sap_dsk_identify) |
| 58 | | { |
| 59 | | char header[0x100]; |
| 60 | | floppy_image_read(floppy, header, 0, sizeof(sap_header)); |
| 61 | | if (!memcmp( header, sap_header, sizeof(sap_header) ) ) |
| 62 | | { |
| 63 | | *vote= 100; |
| 64 | | } else { |
| 65 | | *vote = 0; |
| 66 | | } |
| 67 | | return FLOPPY_ERROR_SUCCESS; |
| 68 | | } |
| 69 | | static int sap_get_heads_per_disk(floppy_image *floppy) |
| 70 | | { |
| 71 | | return 1; |
| 72 | | } |
| 73 | | |
| 74 | | static int sap_get_tracks_per_disk(floppy_image *floppy) |
| 75 | | { |
| 76 | | return get_tag(floppy)->tracks; |
| 77 | | } |
| 78 | | |
| 79 | | |
| 80 | | static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset) |
| 81 | | { |
| 82 | | UINT64 offs; |
| 83 | | struct sap_dsk_tag *tag = get_tag(floppy); |
| 84 | | /* translate the sector to a raw sector */ |
| 85 | | if (!sector_is_index) |
| 86 | | { |
| 87 | | sector -= 1; |
| 88 | | } |
| 89 | | /* check to see if we are out of range */ |
| 90 | | if ((head < 0) || (head >= 1) || (track < 0) || (track >=tag->tracks) |
| 91 | | || (sector < 0) || (sector >= 16)) |
| 92 | | return FLOPPY_ERROR_SEEKERROR; |
| 93 | | |
| 94 | | offs = tag->sector_pos[track][sector]; |
| 95 | | if (offs <= 0 ) |
| 96 | | return FLOPPY_ERROR_SEEKERROR; |
| 97 | | |
| 98 | | if (offset) |
| 99 | | *offset = offs; |
| 100 | | return FLOPPY_ERROR_SUCCESS; |
| 101 | | } |
| 102 | | |
| 103 | | |
| 104 | | |
| 105 | | static floperr_t internal_sap_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen) |
| 106 | | { |
| 107 | | UINT64 offset; |
| 108 | | floperr_t err; |
| 109 | | int i; |
| 110 | | UINT8 *buf; |
| 111 | | err = get_offset(floppy, head, track, sector, sector_is_index, &offset); |
| 112 | | if (err) |
| 113 | | return err; |
| 114 | | |
| 115 | | floppy_image_read(floppy, buffer, offset+4, buflen); |
| 116 | | buf = (UINT8*)buffer; |
| 117 | | for (i=0;i<buflen;i++) { |
| 118 | | buf[i] ^= sap_magic_num; |
| 119 | | } |
| 120 | | return FLOPPY_ERROR_SUCCESS; |
| 121 | | } |
| 122 | | |
| 123 | | |
| 124 | | |
| 125 | | static floperr_t internal_sap_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam) |
| 126 | | { |
| 127 | | UINT64 offset; |
| 128 | | floperr_t err; |
| 129 | | UINT8 buf[256+6]; |
| 130 | | UINT16 crc; |
| 131 | | int i; |
| 132 | | err = get_offset(floppy, head, track, sector, sector_is_index, &offset); |
| 133 | | if (err) |
| 134 | | return err; |
| 135 | | |
| 136 | | /* buf = 4-byte header + sector + 2-byte CRC */ |
| 137 | | floppy_image_read(floppy, buf, offset, 4); |
| 138 | | for (i=0;i<buflen;i++) { |
| 139 | | buf[i+4] = ((UINT8*)buffer)[i]; |
| 140 | | } |
| 141 | | crc = thom_sap_crc( buf, buflen+4 ); |
| 142 | | buf[buflen+4] = crc >> 8; |
| 143 | | buf[buflen+5] = crc & 0xff; |
| 144 | | for (i=0;i<buflen;i++) { |
| 145 | | buf[i+4] ^= sap_magic_num; |
| 146 | | } |
| 147 | | floppy_image_write(floppy, buf, offset, buflen+6); |
| 148 | | |
| 149 | | return FLOPPY_ERROR_SUCCESS; |
| 150 | | } |
| 151 | | |
| 152 | | |
| 153 | | static floperr_t sap_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 154 | | { |
| 155 | | return internal_sap_read_sector(floppy, head, track, sector, FALSE, buffer, buflen); |
| 156 | | } |
| 157 | | |
| 158 | | static floperr_t sap_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 159 | | { |
| 160 | | return internal_sap_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam); |
| 161 | | } |
| 162 | | |
| 163 | | static floperr_t sap_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 164 | | { |
| 165 | | return internal_sap_read_sector(floppy, head, track, sector, TRUE, buffer, buflen); |
| 166 | | } |
| 167 | | |
| 168 | | static floperr_t sap_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 169 | | { |
| 170 | | return internal_sap_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam); |
| 171 | | } |
| 172 | | |
| 173 | | static floperr_t sap_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 174 | | { |
| 175 | | floperr_t err; |
| 176 | | err = get_offset(floppy, head, track, sector, FALSE, NULL); |
| 177 | | if (err) |
| 178 | | return err; |
| 179 | | |
| 180 | | if (sector_length) { |
| 181 | | *sector_length = get_tag(floppy)->sector_size; |
| 182 | | } |
| 183 | | return FLOPPY_ERROR_SUCCESS; |
| 184 | | } |
| 185 | | |
| 186 | | |
| 187 | | |
| 188 | | static floperr_t sap_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 189 | | { |
| 190 | | floperr_t err; |
| 191 | | UINT8 header[4]; |
| 192 | | UINT64 offset = 0; |
| 193 | | sector_index += 1; |
| 194 | | err = get_offset(floppy, head, track, sector_index, FALSE, &offset); |
| 195 | | |
| 196 | | floppy_image_read(floppy, header, offset, 4); |
| 197 | | if (cylinder) |
| 198 | | *cylinder = header[2]; |
| 199 | | if (side) |
| 200 | | *side = head; |
| 201 | | if (sector) |
| 202 | | *sector = header[3]; |
| 203 | | if (sector_length) |
| 204 | | *sector_length = get_tag(floppy)->sector_size; |
| 205 | | if (flags) |
| 206 | | /* TODO: read DAM or DDAM and determine flags */ |
| 207 | | *flags = 0; |
| 208 | | return err; |
| 209 | | } |
| 210 | | |
| 211 | | static floperr_t sap_post_format(floppy_image *floppy, option_resolution *params) |
| 212 | | { |
| 213 | | int track,sector; |
| 214 | | int pos; |
| 215 | | UINT8 buf[256], header[4]; |
| 216 | | struct sap_dsk_tag *tag; |
| 217 | | tag = (struct sap_dsk_tag *) floppy_create_tag(floppy, sizeof(struct sap_dsk_tag)); |
| 218 | | |
| 219 | | /* default options */ |
| 220 | | if ( !tag->tracks ) |
| 221 | | { |
| 222 | | tag->tracks = 80; |
| 223 | | tag->sector_size = 256; |
| 224 | | } |
| 225 | | |
| 226 | | /* create SAP file header */ |
| 227 | | floppy_image_write( floppy, sap_header, 0, 66 ); |
| 228 | | |
| 229 | | for ( track = 0; track < 80; track++ ) |
| 230 | | for ( sector = 0; sector < 16; sector++ ) |
| 231 | | tag->sector_pos[track][sector] = 0; |
| 232 | | |
| 233 | | /* create all sectors with valid header and CRC */ |
| 234 | | memset(buf, 0xe5, 256); |
| 235 | | pos = 0x42; |
| 236 | | header[0] = (tag->sector_size==128) ? 1 : 0; |
| 237 | | header[1] = 0; |
| 238 | | for ( track = 0, pos = 0x42; track < tag->tracks; track++ ) |
| 239 | | for ( sector = 0; sector < 16; sector++, pos += tag->sector_size + 6 ) { |
| 240 | | tag->sector_pos[track][sector] = pos; |
| 241 | | header[2] = track; |
| 242 | | header[3] = sector + 1; |
| 243 | | floppy_image_write(floppy, header, pos, 4); |
| 244 | | sap_write_indexed_sector( floppy, 0, track, sector, buf, tag->sector_size, 0 ); |
| 245 | | } |
| 246 | | |
| 247 | | return FLOPPY_ERROR_SUCCESS; |
| 248 | | } |
| 249 | | |
| 250 | | |
| 251 | | static FLOPPY_CONSTRUCT(sap_dsk_construct) |
| 252 | | { |
| 253 | | struct FloppyCallbacks *callbacks; |
| 254 | | struct sap_dsk_tag *tag; |
| 255 | | int i,j; |
| 256 | | UINT8 fmt; |
| 257 | | tag = (struct sap_dsk_tag *) floppy_create_tag(floppy, sizeof(struct sap_dsk_tag)); |
| 258 | | if (!tag) |
| 259 | | return FLOPPY_ERROR_OUTOFMEMORY; |
| 260 | | |
| 261 | | /* guess format */ |
| 262 | | floppy_image_read(floppy, &fmt, 0x42, 1); |
| 263 | | if ( fmt==1 ) tag->sector_size = 128; else tag->sector_size = 256; |
| 264 | | |
| 265 | | /* start with an empty offset table */ |
| 266 | | tag->tracks = 0; |
| 267 | | for ( i = 0; i < 80; i++ ) |
| 268 | | for ( j = 0; j < 16; j++ ) |
| 269 | | tag->sector_pos[i][j] = 0; |
| 270 | | |
| 271 | | /* count tracks & fill sector offset table */ |
| 272 | | for ( i = 0x42; i+4 < floppy_image_size(floppy); i += tag->sector_size + 6 ) // CRC 2 bytes + 4 bytes sector header |
| 273 | | { |
| 274 | | UINT8 sector, track; |
| 275 | | floppy_image_read(floppy, &track, i+2, 1); |
| 276 | | floppy_image_read(floppy, §or, i+3, 1); |
| 277 | | if ( track >= 80 || sector < 1 || sector > 16 ) continue; |
| 278 | | if ( track > tag->tracks ) tag->tracks = track+1; |
| 279 | | tag->sector_pos[track][sector-1] = i; |
| 280 | | } |
| 281 | | callbacks = floppy_callbacks(floppy); |
| 282 | | callbacks->read_sector = sap_read_sector; |
| 283 | | callbacks->write_sector = sap_write_sector; |
| 284 | | callbacks->read_indexed_sector = sap_read_indexed_sector; |
| 285 | | callbacks->write_indexed_sector = sap_write_indexed_sector; |
| 286 | | callbacks->get_sector_length = sap_get_sector_length; |
| 287 | | callbacks->get_heads_per_disk = sap_get_heads_per_disk; |
| 288 | | callbacks->get_tracks_per_disk = sap_get_tracks_per_disk; |
| 289 | | callbacks->get_indexed_sector_info = sap_get_indexed_sector_info; |
| 290 | | callbacks->post_format = sap_post_format; |
| 291 | | |
| 292 | | return FLOPPY_ERROR_SUCCESS; |
| 293 | | } |
| 294 | | |
| 295 | | static FLOPPY_IDENTIFY(qdd_dsk_identify) |
| 296 | | { |
| 297 | | *vote = (floppy_image_size(floppy) == (51200)) ? 100 : 0; |
| 298 | | return FLOPPY_ERROR_SUCCESS; |
| 299 | | } |
| 300 | | |
| 301 | | /* fixed interlacing map for QDDs */ |
| 302 | | static int thom_qdd_map[400]; |
| 303 | | |
| 304 | | static int qdd_translate_sector(floppy_image *floppy, int sector) |
| 305 | | { |
| 306 | | return thom_qdd_map[sector-1]; |
| 307 | | } |
| 308 | | |
| 309 | | static void thom_qdd_compute_map ( void ) |
| 310 | | { |
| 311 | | /* this map is hardcoded in the QDD BIOS */ |
| 312 | | static const int p[6][4] = |
| 313 | | { |
| 314 | | { 20, 2, 14, 8 }, { 21, 19, 13, 7 }, |
| 315 | | { 22, 18, 12, 6 }, { 23, 17, 11, 5 }, |
| 316 | | { 24, 16, 10, 4 }, { 1, 15, 9, 3 } |
| 317 | | }; |
| 318 | | static const int q[4] = { 0, 8, 4, 12 }; |
| 319 | | int t, s; |
| 320 | | for ( t = 0; t < 24; t++ ) |
| 321 | | { |
| 322 | | for ( s = 0; s < 16; s++ ) |
| 323 | | { |
| 324 | | thom_qdd_map[ t*16 + s ] = p[ t/4 ][ s%4 ] * 16 + (s/4) + 4*(t%4); |
| 325 | | } |
| 326 | | } |
| 327 | | for ( s = 0; s < 16; s++ ) |
| 328 | | { |
| 329 | | thom_qdd_map[ 24*16 + s ] = q[ s%4 ] + (s/4); |
| 330 | | } |
| 331 | | } |
| 332 | | |
| 333 | | static FLOPPY_CONSTRUCT(qdd_dsk_construct) |
| 334 | | { |
| 335 | | struct basicdsk_geometry geometry; |
| 336 | | |
| 337 | | thom_qdd_compute_map(); |
| 338 | | |
| 339 | | memset(&geometry, 0, sizeof(geometry)); |
| 340 | | geometry.heads = 1; |
| 341 | | geometry.first_sector_id = 1; |
| 342 | | geometry.sector_length = 128; |
| 343 | | geometry.tracks = 1; |
| 344 | | geometry.sectors = 400; |
| 345 | | geometry.translate_sector = qdd_translate_sector; |
| 346 | | return basicdsk_construct(floppy, &geometry); |
| 347 | | } |
| 348 | | |
| 349 | | |
| 350 | | /* ----------------------------------------------------------------------- */ |
| 351 | | |
| 352 | | FLOPPY_OPTIONS_START(thomson) |
| 353 | | |
| 354 | | FLOPPY_OPTION(fdmfm2, "fd", "Thomson FD (MFM) 80 tracks disk image (3\"1/2 DD)", basicdsk_identify_default, basicdsk_construct_default, NULL, |
| 355 | | HEADS([1]) |
| 356 | | TRACKS([80]) |
| 357 | | SECTORS([16]) |
| 358 | | SECTOR_LENGTH([256]) |
| 359 | | FIRST_SECTOR_ID([1])) |
| 360 | | |
| 361 | | FLOPPY_OPTION(fdmfm, "fd", "Thomson FD (MFM) 40 tracks disk image (5\"1/4 DD)", basicdsk_identify_default, basicdsk_construct_default, NULL, |
| 362 | | HEADS([1]) |
| 363 | | TRACKS([40]) |
| 364 | | SECTORS([16]) |
| 365 | | SECTOR_LENGTH([256]) |
| 366 | | FIRST_SECTOR_ID([1])) |
| 367 | | |
| 368 | | FLOPPY_OPTION(fd2, "fd", "Thomson FD (FM) 80 tracks disk image (3\"1/2 SD)", basicdsk_identify_default, basicdsk_construct_default, NULL, |
| 369 | | HEADS([1]) |
| 370 | | TRACKS([80]) |
| 371 | | SECTORS([16]) |
| 372 | | SECTOR_LENGTH([128]) |
| 373 | | FIRST_SECTOR_ID([1])) |
| 374 | | |
| 375 | | FLOPPY_OPTION(fd, "fd", "Thomson FD (FM) 40 tracks disk image (5\"1/4 SD)", basicdsk_identify_default, basicdsk_construct_default, NULL, |
| 376 | | HEADS([1]) |
| 377 | | TRACKS([40]) |
| 378 | | SECTORS([16]) |
| 379 | | SECTOR_LENGTH([128]) |
| 380 | | FIRST_SECTOR_ID([1])) |
| 381 | | |
| 382 | | FLOPPY_OPTION(sap,"sap", "Thomson SAP floppy disk image", sap_dsk_identify, sap_dsk_construct, NULL, NULL) |
| 383 | | |
| 384 | | FLOPPY_OPTION(qdd,"qd", "Thomson QDD floppy disk image (2\"8 SD)", qdd_dsk_identify, qdd_dsk_construct, NULL, |
| 385 | | HEADS([1]) |
| 386 | | TRACKS([1]) |
| 387 | | SECTORS([400]) |
| 388 | | SECTOR_LENGTH([128]) |
| 389 | | FIRST_SECTOR_ID([0])) |
| 390 | | |
| 391 | | FLOPPY_OPTIONS_END |
| 392 | | |
src/mess/formats/z80ne_dsk.c
| r11457 | r11458 | |
| 1 | | /********************************************************************* |
| 2 | | |
| 3 | | formats/z80ne_dsk.c |
| 4 | | |
| 5 | | Nuova Elettronica Z80NE disk images |
| 6 | | |
| 7 | | *********************************************************************/ |
| 8 | | |
| 9 | | #include "emu.h" |
| 10 | | #include "formats/z80ne_dsk.h" |
| 11 | | #include "formats/basicdsk.h" |
| 12 | | #include "imagedev/flopdrv.h" |
| 13 | | #include "imageutl.h" |
| 14 | | |
| 15 | | /* ----------------------------------------------------------------------- |
| 16 | | * DMK file format |
| 17 | | * |
| 18 | | * David M. Keil's disk image format is aptly called an 'on disk' image |
| 19 | | * format. This means that whatever written to the disk is encoded into |
| 20 | | * the image file. IDAMS, sector headers, trailing CRCs, and intra-sector |
| 21 | | * spacing. * |
| 22 | | * |
| 23 | | * HEADER DESCRIPTION: |
| 24 | | * |
| 25 | | * Offset Bytes Field Description |
| 26 | | * ------ ----- ------------ ----------- |
| 27 | | * 0 1 write_prot 0xff = Write Protected, 0x00 = R/W |
| 28 | | * 1 1 tracks Number of tracks |
| 29 | | * 2 2 track_length Bytes per track on DMK file (little endian) |
| 30 | | * 4 1 disk_options Miscellaneous flags |
| 31 | | * bit 0-3: Unused |
| 32 | | * bit 4: Sides on DMK file |
| 33 | | * 1=single, 0=double |
| 34 | | * bit 5: Unused |
| 35 | | * bit 6: Single Density DMK file |
| 36 | | * 1=single (bytes written once |
| 37 | | * on IDAMs) |
| 38 | | * 0=double (bytes written |
| 39 | | * two times for SD IDAMs) |
| 40 | | * 5 7 reserved Reserved for future use |
| 41 | | * 12 4 real_disk_code If this is 0x12345678 (little endian) |
| 42 | | * then access a real disk drive |
| 43 | | * (unsupported) |
| 44 | | * |
| 45 | | * Each track begins with a track TOC, consisting of 64 little endian 16-bit |
| 46 | | * integers. Each integer has the following format: |
| 47 | | * bit 0-13: Offset from beginning of track to 'FE' byte of IDAM |
| 48 | | * Note these are always sorted from first to last. All empty |
| 49 | | * entries are 0x00 |
| 50 | | * bit 14: Undefined (reserved) |
| 51 | | * bit 15: Sector double density (0=SD 1=DD) |
| 52 | | * If not set and SD DMK flag is set then bytes are written |
| 53 | | * once. |
| 54 | | * ----------------------------------------------------------------------- */ |
| 55 | | |
| 56 | | struct dmk_tag |
| 57 | | { |
| 58 | | int heads; |
| 59 | | int tracks; |
| 60 | | UINT32 track_size; |
| 61 | | }; |
| 62 | | |
| 63 | | /* DMK file structure |
| 64 | | * See WD1711 documentation |
| 65 | | */ |
| 66 | | /* DMK file header */ |
| 67 | | #define DMK_HEADER_LEN 16 /* DMK header */ |
| 68 | | /* For every track (40) */ |
| 69 | | #define DMK_TOC_LEN 128 /* 64 16-bit relative offsets to IDAMs */ |
| 70 | | #define DMK_GAP1_LEN 32 /* GAP1 (26 bytes 0xFF and 6 bytes 0x00) */ |
| 71 | | |
| 72 | | /* For every sector (10) 301 bytes */ |
| 73 | | #define DMK_IDAM_LEN 7 /* IDAM (0xFE, Track, 0x00, Sector, 0x00, CRChi, CRClo) */ |
| 74 | | #define DMK_ID_GAP_LEN 17 /* GAP2 (11 bytes 0xFF and 6 bytes 0x00) */ |
| 75 | | #define DMK_DAM_LEN 1 /* Data Address Mark (0xFB) */ |
| 76 | | #define DMK_DATA_LEN 256 /* Data */ |
| 77 | | #define DMK_DATA_CRC_LEN 2 /* Data CRC (CRChi, CRClo) */ |
| 78 | | #define DMK_DATA_GAP_LEN 18 /* GAP3 (12 bytes 0xFF and 6 bytes 0x00) (not for last sector) */ |
| 79 | | |
| 80 | | #define DMK_EXTRA_TRACK_LENGTH (176) /* GAP4 (192 bytes 0xFF) */ |
| 81 | | /* End of DMK file structure */ |
| 82 | | |
| 83 | | #define dmk_idam_type(x) (x)[0] |
| 84 | | #define dmk_idam_track(x) (x)[1] |
| 85 | | #define dmk_idam_side(x) (x)[2] |
| 86 | | #define dmk_idam_sector(x) (x)[3] |
| 87 | | #define dmk_idam_sectorlength(x) (x)[4] |
| 88 | | #define dmk_idam_crc(x) (((x)[5] << 8) + (x)[6]) |
| 89 | | #define dmk_idam_set_crc(x, crc) (x)[5] = ((crc) >> 8); (x)[6] = ((crc) >> 0); |
| 90 | | |
| 91 | | static const char needle_data[] = "\x00\x00\x00\x00\x00\x00\x00\x00\xFB\xFB"; |
| 92 | | static const char needle_deleted_data_fa[] = "\x00\x00\x00\x00\x00\x00\x00\x00\xFA\xFA"; |
| 93 | | static const char needle_deleted_data_f8[] = "\x00\x00\x00\x00\x00\x00\x00\x00\xF8\xF8"; |
| 94 | | |
| 95 | | |
| 96 | | static struct dmk_tag *get_dmk_tag(floppy_image *floppy) |
| 97 | | { |
| 98 | | return (dmk_tag *)floppy_tag(floppy); |
| 99 | | } |
| 100 | | |
| 101 | | |
| 102 | | static floperr_t z80ne_dmk_get_track_offset(floppy_image *floppy, int head, int track, UINT64 *offset) |
| 103 | | { |
| 104 | | struct dmk_tag *tag = get_dmk_tag(floppy); |
| 105 | | |
| 106 | | if ((head < 0) || (head >= tag->heads) || (track < 0) || (track >= tag->tracks)) |
| 107 | | return FLOPPY_ERROR_SEEKERROR; |
| 108 | | |
| 109 | | *offset = track; |
| 110 | | *offset *= tag->heads; |
| 111 | | *offset += head; |
| 112 | | *offset *= tag->track_size; |
| 113 | | *offset += DMK_HEADER_LEN; |
| 114 | | return FLOPPY_ERROR_SUCCESS; |
| 115 | | } |
| 116 | | |
| 117 | | |
| 118 | | static UINT32 z80ne_dmk_min_track_size(int sectors, int sector_length) |
| 119 | | { |
| 120 | | int sector_physical_length; |
| 121 | | sector_physical_length = (DMK_IDAM_LEN + DMK_ID_GAP_LEN + DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN + DMK_DATA_GAP_LEN); |
| 122 | | return DMK_TOC_LEN + 2 * (DMK_GAP1_LEN + (sectors * sector_physical_length) - DMK_DATA_GAP_LEN + DMK_EXTRA_TRACK_LENGTH); |
| 123 | | } |
| 124 | | |
| 125 | | |
| 126 | | |
| 127 | | static floperr_t z80ne_dmk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 128 | | { |
| 129 | | floperr_t err; |
| 130 | | UINT64 track_offset; |
| 131 | | |
| 132 | | err = z80ne_dmk_get_track_offset(floppy, head, track, &track_offset); |
| 133 | | if (err) |
| 134 | | return err; |
| 135 | | |
| 136 | | floppy_image_read(floppy, buffer, offset + track_offset, buflen); |
| 137 | | return FLOPPY_ERROR_SUCCESS; |
| 138 | | } |
| 139 | | |
| 140 | | |
| 141 | | |
| 142 | | static floperr_t z80ne_dmk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 143 | | { |
| 144 | | floperr_t err; |
| 145 | | UINT64 track_offset; |
| 146 | | |
| 147 | | err = z80ne_dmk_get_track_offset(floppy, head, track, &track_offset); |
| 148 | | if (err) |
| 149 | | return err; |
| 150 | | |
| 151 | | floppy_image_write(floppy, buffer, offset + track_offset, buflen); |
| 152 | | return FLOPPY_ERROR_SUCCESS; |
| 153 | | } |
| 154 | | |
| 155 | | |
| 156 | | |
| 157 | | static floperr_t z80ne_dmk_get_track_data_offset(floppy_image *floppy, int head, int track, UINT64 *offset) |
| 158 | | { |
| 159 | | *offset = DMK_TOC_LEN + 1; |
| 160 | | return FLOPPY_ERROR_SUCCESS; |
| 161 | | } |
| 162 | | |
| 163 | | |
| 164 | | static floperr_t z80ne_dmk_format_track(floppy_image *floppy, int head, int track, option_resolution *params) |
| 165 | | { |
| 166 | | int sectors; |
| 167 | | int sector_length; |
| 168 | | int interleave; |
| 169 | | int first_sector_id; |
| 170 | | floperr_t err; |
| 171 | | int physical_sector; |
| 172 | | int logical_sector; |
| 173 | | int track_position; |
| 174 | | UINT16 idam_offset; |
| 175 | | UINT16 crc; |
| 176 | | UINT8 *track_data; |
| 177 | | void *track_data_v; |
| 178 | | UINT32 max_track_size; |
| 179 | | int *sector_map = NULL; |
| 180 | | UINT8 *local_sector = NULL; |
| 181 | | int local_sector_size; |
| 182 | | int sector_position; |
| 183 | | int i; |
| 184 | | |
| 185 | | err = FLOPPY_ERROR_SUCCESS; |
| 186 | | |
| 187 | | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 188 | | sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 189 | | interleave = option_resolution_lookup_int(params, PARAM_INTERLEAVE); |
| 190 | | first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID); |
| 191 | | |
| 192 | | max_track_size = get_dmk_tag(floppy)->track_size; |
| 193 | | |
| 194 | | if (sectors > DMK_TOC_LEN/2) |
| 195 | | { |
| 196 | | err = FLOPPY_ERROR_INTERNAL; |
| 197 | | goto done; |
| 198 | | } |
| 199 | | |
| 200 | | if (max_track_size < z80ne_dmk_min_track_size(sectors, sector_length)) |
| 201 | | { |
| 202 | | err = FLOPPY_ERROR_NOSPACE; |
| 203 | | goto done; |
| 204 | | } |
| 205 | | |
| 206 | | err = floppy_load_track(floppy, head, track, TRUE, &track_data_v, NULL); |
| 207 | | if (err) |
| 208 | | goto done; |
| 209 | | track_data = (UINT8 *) track_data_v; |
| 210 | | |
| 211 | | /* set up sector map */ |
| 212 | | sector_map = (int*)malloc(sectors * sizeof(*sector_map)); |
| 213 | | if (!sector_map) |
| 214 | | { |
| 215 | | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 216 | | goto done; |
| 217 | | } |
| 218 | | memset(sector_map, 0xFF, sectors * sizeof(*sector_map)); |
| 219 | | |
| 220 | | physical_sector = 0; |
| 221 | | for (logical_sector = 0; logical_sector < sectors; logical_sector++) |
| 222 | | { |
| 223 | | while(sector_map[physical_sector] >= 0) |
| 224 | | { |
| 225 | | physical_sector++; |
| 226 | | physical_sector %= sectors; |
| 227 | | } |
| 228 | | |
| 229 | | sector_map[physical_sector] = logical_sector + first_sector_id; |
| 230 | | physical_sector += interleave + 1; |
| 231 | | physical_sector %= sectors; |
| 232 | | } |
| 233 | | |
| 234 | | /* set up a local physical sector template */ |
| 235 | | local_sector_size = (DMK_IDAM_LEN + |
| 236 | | DMK_ID_GAP_LEN + |
| 237 | | DMK_DAM_LEN + |
| 238 | | sector_length + |
| 239 | | DMK_DATA_CRC_LEN + |
| 240 | | DMK_DATA_GAP_LEN); |
| 241 | | local_sector = (UINT8*)malloc(local_sector_size); |
| 242 | | if (!local_sector) |
| 243 | | { |
| 244 | | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 245 | | goto done; |
| 246 | | } |
| 247 | | memset(local_sector, 0x00, local_sector_size); |
| 248 | | |
| 249 | | /* set up track table of contents */ |
| 250 | | physical_sector = 0; |
| 251 | | |
| 252 | | /* write GAP1 (track lead in) */ |
| 253 | | memset(&track_data[DMK_TOC_LEN], 0xFF, 52); |
| 254 | | memset(&track_data[DMK_TOC_LEN+52], 0x00, 12); |
| 255 | | track_position = DMK_TOC_LEN + DMK_GAP1_LEN * 2; |
| 256 | | |
| 257 | | /* |
| 258 | | * prepare template in local sector |
| 259 | | */ |
| 260 | | |
| 261 | | /* IDAM */ |
| 262 | | sector_position = 0; |
| 263 | | dmk_idam_type( &local_sector[sector_position]) = 0xFE; |
| 264 | | dmk_idam_track( &local_sector[sector_position]) = track; |
| 265 | | dmk_idam_side( &local_sector[sector_position]) = head; |
| 266 | | dmk_idam_sector( &local_sector[sector_position]) = 0; /* replace in sector instances */ |
| 267 | | dmk_idam_sectorlength( &local_sector[sector_position]) = compute_log2(sector_length / 128); |
| 268 | | dmk_idam_set_crc( &local_sector[sector_position], 0); /* replace in sector instances */ |
| 269 | | sector_position += 7; |
| 270 | | |
| 271 | | /* GAP2 (ID GAP) */ |
| 272 | | memset(&local_sector[sector_position], 0xFF, 11); |
| 273 | | sector_position += 11; |
| 274 | | memset(&local_sector[sector_position], 0x00, 6); |
| 275 | | sector_position += 6; |
| 276 | | |
| 277 | | /* sector body */ |
| 278 | | local_sector[sector_position] = 0xFB; |
| 279 | | memset(&local_sector[sector_position + 1], floppy_get_filler(floppy), sector_length); |
| 280 | | crc = ccitt_crc16(0xffff, &local_sector[sector_position], sector_length + 1); |
| 281 | | local_sector[sector_position + sector_length + 1] = (UINT8) (crc >> 8); |
| 282 | | local_sector[sector_position + sector_length + 2] = (UINT8) (crc >> 0); |
| 283 | | sector_position += sector_length + 3; |
| 284 | | |
| 285 | | /* GAP3 (Data GAP) */ |
| 286 | | memset(&local_sector[sector_position], 0xFF, 12); |
| 287 | | sector_position += 12; |
| 288 | | memset(&local_sector[sector_position], 0x00, 6); |
| 289 | | sector_position += 6; |
| 290 | | |
| 291 | | /* |
| 292 | | * start of track data |
| 293 | | */ |
| 294 | | while(physical_sector < DMK_TOC_LEN/2) |
| 295 | | { |
| 296 | | if (physical_sector >= sectors) |
| 297 | | { |
| 298 | | /* no more sectors */ |
| 299 | | idam_offset = 0; |
| 300 | | } |
| 301 | | else |
| 302 | | { |
| 303 | | /* this is a sector */ |
| 304 | | idam_offset = track_position; |
| 305 | | logical_sector = sector_map[physical_sector]; |
| 306 | | |
| 307 | | /* update IDAM in local sector template */ |
| 308 | | dmk_idam_sector( &local_sector[0]) = logical_sector; /* update sector number in sector instance */ |
| 309 | | crc = ccitt_crc16(0xffff, &local_sector[0], DMK_IDAM_LEN - 2); /* and recalculate crc */ |
| 310 | | dmk_idam_set_crc( &local_sector[0], crc); |
| 311 | | |
| 312 | | /* write local sector in track doubling every byte (DMK double density mode) */ |
| 313 | | for(i=0; i<local_sector_size; i++) |
| 314 | | track_data[track_position + i*2] = track_data[track_position + (i*2) + 1] = local_sector[i]; |
| 315 | | track_position += (2 * local_sector_size); |
| 316 | | if (physical_sector == (sectors-1)) /* on last sector remove GAP3 */ |
| 317 | | track_position -= (2 * DMK_DATA_GAP_LEN); |
| 318 | | } |
| 319 | | |
| 320 | | /* write the TOC entry */ |
| 321 | | track_data[physical_sector * 2 + 0] = (UINT8) (idam_offset >> 0); |
| 322 | | track_data[physical_sector * 2 + 1] = (UINT8) (idam_offset >> 8); |
| 323 | | |
| 324 | | physical_sector++; |
| 325 | | } |
| 326 | | |
| 327 | | /* write track footer GAP4 */ |
| 328 | | assert(max_track_size >= (UINT32)track_position); |
| 329 | | memset(&track_data[track_position], 0xFF, max_track_size - track_position); |
| 330 | | |
| 331 | | done: |
| 332 | | if (sector_map) |
| 333 | | free(sector_map); |
| 334 | | if (local_sector) |
| 335 | | free(local_sector); |
| 336 | | |
| 337 | | return err; |
| 338 | | } |
| 339 | | |
| 340 | | |
| 341 | | |
| 342 | | static int z80ne_dmk_get_heads_per_disk(floppy_image *floppy) |
| 343 | | { |
| 344 | | return get_dmk_tag(floppy)->heads; |
| 345 | | } |
| 346 | | |
| 347 | | |
| 348 | | |
| 349 | | static int z80ne_dmk_get_tracks_per_disk(floppy_image *floppy) |
| 350 | | { |
| 351 | | return get_dmk_tag(floppy)->tracks; |
| 352 | | } |
| 353 | | |
| 354 | | |
| 355 | | |
| 356 | | static UINT32 z80ne_dmk_get_track_size(floppy_image *floppy, int head, int track) |
| 357 | | { |
| 358 | | return get_dmk_tag(floppy)->track_size; |
| 359 | | } |
| 360 | | |
| 361 | | static UINT8 * my_memmem(UINT8 *haystack, size_t haystacklen, |
| 362 | | UINT8 *needle, size_t needlelen) |
| 363 | | { |
| 364 | | register UINT8 *h = haystack; |
| 365 | | register UINT8 *n = needle; |
| 366 | | register size_t hl = haystacklen; |
| 367 | | register size_t nl = needlelen; |
| 368 | | register size_t i; |
| 369 | | |
| 370 | | if (nl == 0) return haystack; /* The first occurrence of the empty string is deemed to occur at |
| 371 | | the beginning of the string. */ |
| 372 | | if (hl < nl) |
| 373 | | return NULL; |
| 374 | | |
| 375 | | for( i = 0; (i < hl) && (i + nl <= hl ); i++ ) |
| 376 | | if (h[i] == *n) /* first match */ |
| 377 | | if ( !memcmp(&h[i]+1, n + 1, nl - 1) ) |
| 378 | | return (haystack+i); /* returns a pointer to the substring */ |
| 379 | | |
| 380 | | return (UINT8 *)NULL; /* not found */ |
| 381 | | } |
| 382 | | |
| 383 | | |
| 384 | | |
| 385 | | static floperr_t z80ne_dmk_seek_sector_in_track(floppy_image *floppy, int head, int track, int sector, int sector_is_index, int dirtify, UINT8 **sector_data, UINT32 *sector_length) |
| 386 | | { |
| 387 | | struct dmk_tag *tag = get_dmk_tag(floppy); |
| 388 | | floperr_t err; |
| 389 | | size_t idam_offset = 0; |
| 390 | | UINT16 calculated_crc; |
| 391 | | size_t i, j; |
| 392 | | size_t offs; |
| 393 | | /*int state;*/ |
| 394 | | UINT8 *track_data; |
| 395 | | void *track_data_v; |
| 396 | | size_t track_length; |
| 397 | | size_t sec_len; |
| 398 | | UINT8 *local_idam = NULL; |
| 399 | | int local_idam_size; |
| 400 | | UINT8 *sec_data; |
| 401 | | |
| 402 | | |
| 403 | | err = FLOPPY_ERROR_SUCCESS; |
| 404 | | |
| 405 | | err = floppy_load_track(floppy, head, track, dirtify, &track_data_v, &track_length); |
| 406 | | if (err) |
| 407 | | goto done; |
| 408 | | track_data = (UINT8 *) track_data_v; |
| 409 | | |
| 410 | | /* set up a local IDAM space */ |
| 411 | | local_idam_size = (DMK_IDAM_LEN + |
| 412 | | DMK_ID_GAP_LEN + |
| 413 | | DMK_DAM_LEN + |
| 414 | | DMK_DATA_GAP_LEN); |
| 415 | | local_idam = (UINT8*)malloc(local_idam_size); |
| 416 | | if (!local_idam) |
| 417 | | { |
| 418 | | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 419 | | goto done; |
| 420 | | } |
| 421 | | memset(local_idam, 0x00, local_idam_size); |
| 422 | | |
| 423 | | /* search for matching IDAM */ |
| 424 | | for (i = 0; i < DMK_TOC_LEN / 2; i++) |
| 425 | | { |
| 426 | | idam_offset = track_data[i * 2 + 1]; |
| 427 | | idam_offset <<= 8; |
| 428 | | idam_offset |= track_data[i * 2 + 0]; |
| 429 | | idam_offset &= 0x3FFF; |
| 430 | | |
| 431 | | if (idam_offset == 0) |
| 432 | | { |
| 433 | | /* we've reached the end of the road */ |
| 434 | | i = DMK_TOC_LEN; |
| 435 | | break; |
| 436 | | } |
| 437 | | |
| 438 | | if ((idam_offset + DMK_IDAM_LEN * 2) >= tag->track_size) |
| 439 | | continue; |
| 440 | | |
| 441 | | /* overflowing the track? */ |
| 442 | | if ((idam_offset + local_idam_size * 2) >= tag->track_size) |
| 443 | | { |
| 444 | | err = FLOPPY_ERROR_SEEKERROR; |
| 445 | | goto done; |
| 446 | | } |
| 447 | | |
| 448 | | /* create a local copy of sector of IDAM */ |
| 449 | | for(j = 0; j < (DMK_IDAM_LEN + DMK_ID_GAP_LEN + DMK_DAM_LEN); j++) |
| 450 | | local_idam[j] = track_data[idam_offset + j*2]; |
| 451 | | |
| 452 | | /* from now on all operations are done in local copy of IDAM */ |
| 453 | | calculated_crc = ccitt_crc16(0xffff, &local_idam[0], DMK_IDAM_LEN - 2); |
| 454 | | |
| 455 | | /* check IDAM integrity and check for matching sector */ |
| 456 | | if (calculated_crc == dmk_idam_crc(&local_idam[0])) |
| 457 | | { |
| 458 | | if (sector_is_index) |
| 459 | | { |
| 460 | | /* the sector is indexed; decrement the index and go */ |
| 461 | | if (sector-- == 0) |
| 462 | | break; |
| 463 | | } |
| 464 | | else |
| 465 | | { |
| 466 | | /* check for matching sector ID */ |
| 467 | | if (sector == dmk_idam_sector(&local_idam[0])) |
| 468 | | break; |
| 469 | | } |
| 470 | | } |
| 471 | | } |
| 472 | | |
| 473 | | if (i >= DMK_TOC_LEN / 2) |
| 474 | | { |
| 475 | | err = FLOPPY_ERROR_SEEKERROR; |
| 476 | | goto done; |
| 477 | | } |
| 478 | | |
| 479 | | /* we found a matching sector ID */ |
| 480 | | /*state = 0;*/ |
| 481 | | offs = idam_offset + 2 * DMK_IDAM_LEN; |
| 482 | | |
| 483 | | /* find pattern 0x0000FB; this represents the start of a data sector */ |
| 484 | | sec_data = my_memmem(&track_data[offs], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_data, 10); |
| 485 | | if(!sec_data) |
| 486 | | { |
| 487 | | /* find pattern 0x0000FA; this represents the start of a deleted data sector (used for system sectors) */ |
| 488 | | sec_data = my_memmem(&track_data[offs], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_fa, 10); |
| 489 | | } |
| 490 | | if(!sec_data) |
| 491 | | { |
| 492 | | /* find pattern 0x0000F8; this represents the start of a deleted data sector (used for system sectors) */ |
| 493 | | sec_data = my_memmem(&track_data[offs], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_f8, 10); |
| 494 | | } |
| 495 | | |
| 496 | | if(sec_data) |
| 497 | | sec_data += 10; |
| 498 | | else |
| 499 | | { |
| 500 | | err = FLOPPY_ERROR_SEEKERROR; |
| 501 | | goto done; |
| 502 | | } |
| 503 | | offs = sec_data - &track_data[offs]; |
| 504 | | |
| 505 | | sec_len = 128 << dmk_idam_sectorlength(&local_idam[0]); |
| 506 | | |
| 507 | | if ((offs + 2 * sec_len) > track_length) |
| 508 | | { |
| 509 | | err = FLOPPY_ERROR_INVALIDIMAGE; |
| 510 | | goto done; |
| 511 | | } |
| 512 | | |
| 513 | | if (sector_data) |
| 514 | | *sector_data = sec_data; |
| 515 | | if (sector_length) |
| 516 | | *sector_length = sec_len; |
| 517 | | done: |
| 518 | | if (local_idam) |
| 519 | | free(local_idam); |
| 520 | | return err; |
| 521 | | } |
| 522 | | |
| 523 | | |
| 524 | | |
| 525 | | static floperr_t z80ne_dmk_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 526 | | { |
| 527 | | return z80ne_dmk_seek_sector_in_track(floppy, head, track, sector, FALSE, FALSE, NULL, sector_length); |
| 528 | | } |
| 529 | | |
| 530 | | |
| 531 | | |
| 532 | | static floperr_t z80ne_dmk_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 533 | | { |
| 534 | | floperr_t err; |
| 535 | | size_t idam_offset; |
| 536 | | UINT8 *track_data; |
| 537 | | void *track_data_v; |
| 538 | | unsigned long dam; |
| 539 | | |
| 540 | | if (sector_index * 2 >= DMK_TOC_LEN) |
| 541 | | return FLOPPY_ERROR_SEEKERROR; |
| 542 | | |
| 543 | | err = floppy_load_track(floppy, head, track, FALSE, &track_data_v, NULL); |
| 544 | | if (err) |
| 545 | | return err; |
| 546 | | track_data = (UINT8 *) track_data_v; |
| 547 | | |
| 548 | | idam_offset = track_data[sector_index * 2 + 1]; |
| 549 | | idam_offset <<= 8; |
| 550 | | idam_offset |= track_data[sector_index * 2 + 0]; |
| 551 | | idam_offset &= 0x3FFF; |
| 552 | | |
| 553 | | if (idam_offset == 0) |
| 554 | | return FLOPPY_ERROR_SEEKERROR; |
| 555 | | |
| 556 | | /* determine type of data sector */ |
| 557 | | /* find pattern 0x0000FB; this represents the start of a data sector */ |
| 558 | | if ( my_memmem(&track_data[idam_offset+14], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_data, 10) ) |
| 559 | | dam = 0; |
| 560 | | /* find pattern 0x0000FA; this represents the start of a deleted data sector (used for system sectors) */ |
| 561 | | else if ( my_memmem(&track_data[idam_offset+14], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_fa, 10) ) |
| 562 | | dam = ID_FLAG_DELETED_DATA; |
| 563 | | /* find pattern 0x0000F8; this represents the start of a deleted data sector (used for system sectors) */ |
| 564 | | else if ( my_memmem(&track_data[idam_offset+14], 4 * (DMK_ID_GAP_LEN + DMK_DAM_LEN), (UINT8 *)needle_deleted_data_f8, 10) ) |
| 565 | | dam = ID_FLAG_DELETED_DATA; |
| 566 | | else |
| 567 | | return FLOPPY_ERROR_SEEKERROR; |
| 568 | | |
| 569 | | if (cylinder) |
| 570 | | *cylinder = (&track_data[idam_offset])[2]; |
| 571 | | if (side) |
| 572 | | *side = (&track_data[idam_offset])[4]; |
| 573 | | if (sector) |
| 574 | | *sector = (&track_data[idam_offset])[6]; |
| 575 | | if (sector_length) |
| 576 | | *sector_length = 128 << (&track_data[idam_offset])[8]; |
| 577 | | if (flags) |
| 578 | | *flags = dam; |
| 579 | | return FLOPPY_ERROR_SUCCESS; |
| 580 | | } |
| 581 | | |
| 582 | | |
| 583 | | |
| 584 | | static floperr_t internal_z80ne_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen) |
| 585 | | { |
| 586 | | floperr_t err; |
| 587 | | UINT32 sector_length; |
| 588 | | UINT16 crc_on_disk; |
| 589 | | UINT16 calculated_crc; |
| 590 | | UINT8 *sector_data = NULL; |
| 591 | | UINT8 *local_sector = NULL; |
| 592 | | int local_sector_size; |
| 593 | | int i; |
| 594 | | |
| 595 | | err = FLOPPY_ERROR_SUCCESS; |
| 596 | | |
| 597 | | /* get sector length and data pointer */ |
| 598 | | err = z80ne_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, FALSE, §or_data, §or_length); |
| 599 | | if (err) |
| 600 | | goto done; |
| 601 | | |
| 602 | | /* set up a local physical sector space (DAM + data + crc + GAP) */ |
| 603 | | local_sector_size = (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN + DMK_DATA_GAP_LEN); |
| 604 | | local_sector = (UINT8*)malloc(local_sector_size); |
| 605 | | if (!local_sector) |
| 606 | | { |
| 607 | | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 608 | | goto done; |
| 609 | | } |
| 610 | | memset(local_sector, 0x00, local_sector_size); |
| 611 | | |
| 612 | | /* get sector data */ |
| 613 | | /* create a local copy of sector data including DAM (for crc calculation) */ |
| 614 | | for(i = 0; i < (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN); i++) |
| 615 | | local_sector[i] = (sector_data - 2)[i*2]; |
| 616 | | |
| 617 | | crc_on_disk = local_sector[sector_length + 0 + 1]; |
| 618 | | crc_on_disk <<= 8; |
| 619 | | crc_on_disk += local_sector[sector_length + 1 + 1]; |
| 620 | | |
| 621 | | /* crc calculated including DAM */ |
| 622 | | calculated_crc = ccitt_crc16(0xffff, local_sector, sector_length+1); |
| 623 | | if (calculated_crc != crc_on_disk) |
| 624 | | { |
| 625 | | err = FLOPPY_ERROR_INVALIDIMAGE; |
| 626 | | goto done; |
| 627 | | } |
| 628 | | |
| 629 | | /* copy local sector data excluding DAM */ |
| 630 | | memcpy(buffer, local_sector+1, MIN(sector_length, buflen)); |
| 631 | | |
| 632 | | done: |
| 633 | | if (local_sector) |
| 634 | | free(local_sector); |
| 635 | | return err; |
| 636 | | } |
| 637 | | |
| 638 | | |
| 639 | | |
| 640 | | static floperr_t internal_z80ne_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, const void *buffer, size_t buflen, int ddam) |
| 641 | | { |
| 642 | | floperr_t err; |
| 643 | | UINT32 sector_length; |
| 644 | | UINT8 *sector_data; |
| 645 | | UINT16 crc; |
| 646 | | UINT8 *local_sector = NULL; |
| 647 | | int local_sector_size; |
| 648 | | int i; |
| 649 | | |
| 650 | | err = FLOPPY_ERROR_SUCCESS; |
| 651 | | |
| 652 | | /* get sector length */ |
| 653 | | err = z80ne_dmk_seek_sector_in_track(floppy, head, track, sector, sector_is_index, TRUE, §or_data, §or_length); |
| 654 | | if (err) |
| 655 | | goto done; |
| 656 | | |
| 657 | | /* set up a local physical sector space */ |
| 658 | | local_sector_size = (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN + DMK_DATA_GAP_LEN); |
| 659 | | local_sector = (UINT8*)malloc(local_sector_size); |
| 660 | | if (!local_sector) |
| 661 | | { |
| 662 | | err = FLOPPY_ERROR_OUTOFMEMORY; |
| 663 | | goto done; |
| 664 | | } |
| 665 | | memset(local_sector, 0x00, local_sector_size); |
| 666 | | if(!ddam) |
| 667 | | local_sector[0] = 0xFB; |
| 668 | | else |
| 669 | | local_sector[0] = 0xFA; |
| 670 | | |
| 671 | | if (buflen > sector_length) |
| 672 | | { |
| 673 | | err = FLOPPY_ERROR_INTERNAL; |
| 674 | | goto done; |
| 675 | | } |
| 676 | | |
| 677 | | memcpy(&local_sector[1], buffer, buflen); |
| 678 | | |
| 679 | | crc = ccitt_crc16(0xffff, local_sector, DMK_DAM_LEN + sector_length); |
| 680 | | local_sector[DMK_DAM_LEN + sector_length + 0] = crc >> 8; |
| 681 | | local_sector[DMK_DAM_LEN + sector_length + 1] = crc >> 0; |
| 682 | | |
| 683 | | /* write local sector in track doubling every byte (DMK double density mode) */ |
| 684 | | /* write DAM or DDAM too */ |
| 685 | | sector_data -= 2; |
| 686 | | for(i=0; i < (DMK_DAM_LEN + sector_length + DMK_DATA_CRC_LEN); i++) |
| 687 | | sector_data[i*2] = sector_data[(i*2)+1] = local_sector[i]; |
| 688 | | |
| 689 | | done: |
| 690 | | if (local_sector) |
| 691 | | free(local_sector); |
| 692 | | return err; |
| 693 | | } |
| 694 | | |
| 695 | | |
| 696 | | |
| 697 | | static floperr_t z80ne_dmk_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 698 | | { |
| 699 | | return internal_z80ne_dmk_read_sector(floppy, head, track, sector, FALSE, buffer, buflen); |
| 700 | | } |
| 701 | | |
| 702 | | static floperr_t z80ne_dmk_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 703 | | { |
| 704 | | return internal_z80ne_dmk_write_sector(floppy, head, track, sector, FALSE, buffer, buflen, ddam); |
| 705 | | } |
| 706 | | |
| 707 | | static floperr_t z80ne_dmk_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 708 | | { |
| 709 | | return internal_z80ne_dmk_read_sector(floppy, head, track, sector, TRUE, buffer, buflen); |
| 710 | | } |
| 711 | | |
| 712 | | static floperr_t z80ne_dmk_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 713 | | { |
| 714 | | return internal_z80ne_dmk_write_sector(floppy, head, track, sector, TRUE, buffer, buflen, ddam); |
| 715 | | } |
| 716 | | |
| 717 | | |
| 718 | | |
| 719 | | static void z80ne_dmk_interpret_header(floppy_image *floppy, int *heads, int *tracks, int * sectors, int *track_size) |
| 720 | | { |
| 721 | | UINT8 header[DMK_HEADER_LEN + DMK_TOC_LEN]; |
| 722 | | int i; |
| 723 | | int idam_offset; |
| 724 | | int sects; |
| 725 | | |
| 726 | | floppy_image_read(floppy, header, 0, DMK_HEADER_LEN + DMK_TOC_LEN); |
| 727 | | |
| 728 | | if (tracks) |
| 729 | | *tracks = header[1]; |
| 730 | | if (heads) |
| 731 | | *heads = (header[4] & 0x10) ? 1 : 2; |
| 732 | | if (track_size) |
| 733 | | *track_size = ((int) header[3]) * 0x100 + header[2]; |
| 734 | | if(sectors) |
| 735 | | { |
| 736 | | /* count sectors on first track */ |
| 737 | | sects = 0; |
| 738 | | for (i = 0; i < DMK_TOC_LEN / 2; i++) |
| 739 | | { |
| 740 | | idam_offset = header[DMK_HEADER_LEN + i * 2 + 1]; |
| 741 | | idam_offset <<= 8; |
| 742 | | idam_offset |= header[DMK_HEADER_LEN + i * 2 + 0]; |
| 743 | | idam_offset &= 0x3FFF; |
| 744 | | |
| 745 | | if (idam_offset == 0) |
| 746 | | break; |
| 747 | | else |
| 748 | | sects++; |
| 749 | | } |
| 750 | | *sectors = sects; |
| 751 | | } |
| 752 | | |
| 753 | | } |
| 754 | | |
| 755 | | |
| 756 | | |
| 757 | | FLOPPY_CONSTRUCT(z80ne_dmk_construct) |
| 758 | | { |
| 759 | | struct FloppyCallbacks *callbacks; |
| 760 | | struct dmk_tag *tag; |
| 761 | | UINT8 header[DMK_HEADER_LEN]; |
| 762 | | int heads, tracks, track_size, sectors, sector_length; |
| 763 | | |
| 764 | | if (params) |
| 765 | | { |
| 766 | | heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 767 | | tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 768 | | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 769 | | sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 770 | | |
| 771 | | track_size = z80ne_dmk_min_track_size(sectors, sector_length); |
| 772 | | |
| 773 | | memset(header, 0, sizeof(header)); |
| 774 | | header[1] = tracks; |
| 775 | | header[2] = track_size >> 0; |
| 776 | | header[3] = track_size >> 8; |
| 777 | | header[4] = (heads == 2) ? 0x00 : 0x10; |
| 778 | | |
| 779 | | floppy_image_write(floppy, header, 0, sizeof(header)); |
| 780 | | } |
| 781 | | else |
| 782 | | { |
| 783 | | z80ne_dmk_interpret_header(floppy, &heads, &tracks, §ors, &track_size); |
| 784 | | } |
| 785 | | |
| 786 | | tag = (dmk_tag*)floppy_create_tag(floppy, sizeof(struct dmk_tag)); |
| 787 | | if (!tag) |
| 788 | | return FLOPPY_ERROR_OUTOFMEMORY; |
| 789 | | tag->heads = heads; |
| 790 | | tag->track_size = track_size; |
| 791 | | tag->tracks = tracks; |
| 792 | | |
| 793 | | callbacks = floppy_callbacks(floppy); |
| 794 | | callbacks->read_track = z80ne_dmk_read_track; |
| 795 | | callbacks->write_track = z80ne_dmk_write_track; |
| 796 | | callbacks->get_track_data_offset = z80ne_dmk_get_track_data_offset; |
| 797 | | callbacks->format_track = z80ne_dmk_format_track; |
| 798 | | callbacks->get_heads_per_disk = z80ne_dmk_get_heads_per_disk; |
| 799 | | callbacks->get_tracks_per_disk = z80ne_dmk_get_tracks_per_disk; |
| 800 | | callbacks->get_track_size = z80ne_dmk_get_track_size; |
| 801 | | callbacks->get_sector_length = z80ne_dmk_get_sector_length; |
| 802 | | callbacks->get_indexed_sector_info = z80ne_dmk_get_indexed_sector_info; |
| 803 | | callbacks->read_sector = z80ne_dmk_read_sector; |
| 804 | | callbacks->write_sector = z80ne_dmk_write_sector; |
| 805 | | callbacks->read_indexed_sector = z80ne_dmk_read_indexed_sector; |
| 806 | | callbacks->write_indexed_sector = z80ne_dmk_write_indexed_sector; |
| 807 | | |
| 808 | | return FLOPPY_ERROR_SUCCESS; |
| 809 | | } |
| 810 | | |
| 811 | | |
| 812 | | |
| 813 | | FLOPPY_IDENTIFY(z80ne_dmk_identify) |
| 814 | | { |
| 815 | | int heads, tracks, sectors, track_size; |
| 816 | | UINT64 size, expected_size; |
| 817 | | |
| 818 | | size = floppy_image_size(floppy); |
| 819 | | z80ne_dmk_interpret_header(floppy, &heads, &tracks, §ors, &track_size); |
| 820 | | expected_size = DMK_HEADER_LEN + (heads * tracks * track_size); |
| 821 | | *vote = (size == expected_size) ? 90 : 0; |
| 822 | | *vote += (sectors == 10) ? 10 : 0; |
| 823 | | return FLOPPY_ERROR_SUCCESS; |
| 824 | | } |
| 825 | | |
| 826 | | |
| 827 | | |
| 828 | | /* ----------------------------------------------------------------------- */ |
| 829 | | |
| 830 | | FLOPPY_OPTIONS_START( z80ne ) |
| 831 | | /* |
| 832 | | * Single side |
| 833 | | * Single density |
| 834 | | * FM, 125 kbit/s, |
| 835 | | * 40 tracks, track ID: 0..39 |
| 836 | | * 10 sectors/track, sector ID: 0..9 |
| 837 | | * 256 bytes/sector |
| 838 | | * Interleave 1 (sector ID sequence on track: 0 5 1 6 2 7 3 8 4 9) |
| 839 | | * 3125 byte/track unformatted |
| 840 | | * Rotation speed 300 rpm (5 rps) |
| 841 | | */ |
| 842 | | FLOPPY_OPTION( z80ne_dmk, "zmk", "Z80NE DMK disk image", z80ne_dmk_identify, z80ne_dmk_construct, NULL, |
| 843 | | HEADS([1]) |
| 844 | | TRACKS([40]) |
| 845 | | SECTORS([10]) |
| 846 | | SECTOR_LENGTH([256]) |
| 847 | | INTERLEAVE([1]) |
| 848 | | FIRST_SECTOR_ID([0])) |
| 849 | | FLOPPY_OPTIONS_END |
src/mess/formats/coupedsk.c
| r11457 | r11458 | |
| 1 | | /************************************************************************* |
| 2 | | |
| 3 | | formats/coupedsk.c |
| 4 | | |
| 5 | | SAM Coupe disk image formats |
| 6 | | |
| 7 | | **************************************************************************/ |
| 8 | | |
| 9 | | #include "coupedsk.h" |
| 10 | | #include "formats/basicdsk.h" |
| 11 | | |
| 12 | | |
| 13 | | #define SAD_HEADER_LEN 22 |
| 14 | | #define SAD_SIGNATURE "Aley's disk backup" |
| 15 | | |
| 16 | | #define SDF_TRACKSIZE (512 * 12) |
| 17 | | |
| 18 | | |
| 19 | | struct sdf_tag |
| 20 | | { |
| 21 | | UINT8 heads; |
| 22 | | UINT8 tracks; |
| 23 | | }; |
| 24 | | |
| 25 | | |
| 26 | | |
| 27 | | /************************************* |
| 28 | | * |
| 29 | | * MGT disk image format |
| 30 | | * |
| 31 | | *************************************/ |
| 32 | | |
| 33 | | /* |
| 34 | | Straight rip without any header |
| 35 | | |
| 36 | | Data: |
| 37 | | |
| 38 | | Side 0 Track 0 |
| 39 | | Side 1 Track 0 |
| 40 | | Side 0 Track 1 |
| 41 | | Side 1 Track 1 |
| 42 | | ... |
| 43 | | */ |
| 44 | | |
| 45 | | |
| 46 | | FLOPPY_CONSTRUCT( coupe_mgt_construct ) |
| 47 | | { |
| 48 | | struct basicdsk_geometry geometry; |
| 49 | | |
| 50 | | memset(&geometry, 0, sizeof(geometry)); |
| 51 | | geometry.heads = 2; |
| 52 | | geometry.tracks = 80; |
| 53 | | geometry.sector_length = 512; |
| 54 | | geometry.first_sector_id = 1; |
| 55 | | |
| 56 | | if (params) |
| 57 | | { |
| 58 | | /* create */ |
| 59 | | geometry.sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 60 | | } |
| 61 | | else |
| 62 | | { |
| 63 | | /* load */ |
| 64 | | UINT64 size = floppy_image_size(floppy); |
| 65 | | |
| 66 | | /* verify size */ |
| 67 | | if (size != 737280 && size != 819200) |
| 68 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 69 | | |
| 70 | | geometry.sectors = size / 81920; |
| 71 | | |
| 72 | | } |
| 73 | | |
| 74 | | return basicdsk_construct(floppy, &geometry); |
| 75 | | } |
| 76 | | |
| 77 | | |
| 78 | | FLOPPY_IDENTIFY( coupe_mgt_identify ) |
| 79 | | { |
| 80 | | UINT64 size; |
| 81 | | |
| 82 | | size = floppy_image_size(floppy); |
| 83 | | |
| 84 | | *vote = (size == 737280 || size == 819200) ? 100 : 0; |
| 85 | | return FLOPPY_ERROR_SUCCESS; |
| 86 | | } |
| 87 | | |
| 88 | | |
| 89 | | |
| 90 | | /************************************* |
| 91 | | * |
| 92 | | * SAD disk image format |
| 93 | | * |
| 94 | | *************************************/ |
| 95 | | |
| 96 | | /* |
| 97 | | Header (22 bytes): |
| 98 | | |
| 99 | | 0-17 "Aley's disk backup" |
| 100 | | 18 number of sides |
| 101 | | 19 number of tracks |
| 102 | | 20 number of sectors per track |
| 103 | | 21 sector size divided by 64 |
| 104 | | |
| 105 | | Data: |
| 106 | | |
| 107 | | Side 0 Track 0 |
| 108 | | Side 0 Track 1 |
| 109 | | Side 0 Track 2 |
| 110 | | ... |
| 111 | | Side 1 Track 0 |
| 112 | | Side 1 Track 1 |
| 113 | | ... |
| 114 | | */ |
| 115 | | |
| 116 | | |
| 117 | | static UINT64 coupe_sad_translate_offset(floppy_image *floppy, |
| 118 | | const struct basicdsk_geometry *geom, int track, int head, int sector) |
| 119 | | { |
| 120 | | return head * geom->tracks * geom->sectors + geom->sectors * track + sector; |
| 121 | | } |
| 122 | | |
| 123 | | |
| 124 | | static void coupe_sad_interpret_header(floppy_image *floppy, |
| 125 | | int *heads, int *tracks, int *sectors, int *sector_size) |
| 126 | | { |
| 127 | | UINT8 header[SAD_HEADER_LEN]; |
| 128 | | |
| 129 | | floppy_image_read(floppy, header, 0, SAD_HEADER_LEN); |
| 130 | | |
| 131 | | *heads = header[18]; |
| 132 | | *tracks = header[19]; |
| 133 | | *sectors = header[20]; |
| 134 | | *sector_size = header[21] << 6; |
| 135 | | } |
| 136 | | |
| 137 | | |
| 138 | | FLOPPY_CONSTRUCT( coupe_sad_construct ) |
| 139 | | { |
| 140 | | struct basicdsk_geometry geometry; |
| 141 | | int heads, tracks, sectors, sector_length; |
| 142 | | |
| 143 | | memset(&geometry, 0, sizeof(geometry)); |
| 144 | | |
| 145 | | if (params) |
| 146 | | { |
| 147 | | /* create */ |
| 148 | | UINT8 header[SAD_HEADER_LEN]; |
| 149 | | |
| 150 | | /* get format options */ |
| 151 | | heads = option_resolution_lookup_int(params, PARAM_HEADS); |
| 152 | | tracks = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 153 | | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 154 | | sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 155 | | |
| 156 | | /* build the header */ |
| 157 | | memset(header, 0, SAD_HEADER_LEN); |
| 158 | | memcpy(header, SAD_SIGNATURE, 18); |
| 159 | | header[18] = heads; |
| 160 | | header[19] = tracks; |
| 161 | | header[20] = sectors; |
| 162 | | header[21] = sector_length >> 6; |
| 163 | | |
| 164 | | /* and write it to disk */ |
| 165 | | floppy_image_write(floppy, header, 0, sizeof(header)); |
| 166 | | } |
| 167 | | else |
| 168 | | { |
| 169 | | /* load */ |
| 170 | | coupe_sad_interpret_header(floppy, &heads, &tracks, §ors, §or_length); |
| 171 | | } |
| 172 | | |
| 173 | | /* fill in the data */ |
| 174 | | geometry.offset = SAD_HEADER_LEN; |
| 175 | | geometry.heads = heads; |
| 176 | | geometry.tracks = tracks; |
| 177 | | geometry.sectors = sectors; |
| 178 | | geometry.first_sector_id = 1; |
| 179 | | geometry.sector_length = sector_length; |
| 180 | | geometry.translate_offset = coupe_sad_translate_offset; |
| 181 | | |
| 182 | | return basicdsk_construct(floppy, &geometry); |
| 183 | | } |
| 184 | | |
| 185 | | |
| 186 | | FLOPPY_IDENTIFY( coupe_sad_identify ) |
| 187 | | { |
| 188 | | int heads, tracks, sectors, sector_size; |
| 189 | | UINT64 size, calculated_size; |
| 190 | | |
| 191 | | size = floppy_image_size(floppy); |
| 192 | | |
| 193 | | /* read values from SAD header */ |
| 194 | | coupe_sad_interpret_header(floppy, &heads, &tracks, §ors, §or_size); |
| 195 | | |
| 196 | | /* calculate expected disk image size */ |
| 197 | | calculated_size = SAD_HEADER_LEN + heads * tracks * sectors * sector_size; |
| 198 | | |
| 199 | | *vote = (size == calculated_size) ? 100 : 0; |
| 200 | | return FLOPPY_ERROR_SUCCESS; |
| 201 | | } |
| 202 | | |
| 203 | | |
| 204 | | |
| 205 | | /************************************* |
| 206 | | * |
| 207 | | * SDF disk image format |
| 208 | | * |
| 209 | | * TODO: wd17xx status codes are |
| 210 | | * currently ignored |
| 211 | | * |
| 212 | | *************************************/ |
| 213 | | |
| 214 | | static int coupe_sdf_get_heads_per_disk(floppy_image *floppy) |
| 215 | | { |
| 216 | | struct sdf_tag *tag = (sdf_tag *)floppy_tag(floppy); |
| 217 | | return tag->heads; |
| 218 | | } |
| 219 | | |
| 220 | | |
| 221 | | static int coupe_sdf_get_tracks_per_disk(floppy_image *floppy) |
| 222 | | { |
| 223 | | struct sdf_tag *tag = (sdf_tag *)floppy_tag(floppy); |
| 224 | | return tag->tracks; |
| 225 | | } |
| 226 | | |
| 227 | | |
| 228 | | static UINT32 coupe_sdf_get_track_size(floppy_image *floppy, int head, int track) |
| 229 | | { |
| 230 | | return SDF_TRACKSIZE; |
| 231 | | } |
| 232 | | |
| 233 | | |
| 234 | | static floperr_t coupe_sdf_get_offset(floppy_image *floppy, |
| 235 | | int head, int track, UINT64 *offset) |
| 236 | | { |
| 237 | | struct sdf_tag *tag = (sdf_tag *)floppy_tag(floppy); |
| 238 | | |
| 239 | | if (head > tag->heads || track > tag->tracks) |
| 240 | | return FLOPPY_ERROR_SEEKERROR; |
| 241 | | |
| 242 | | *offset = (head * tag->tracks + track) * SDF_TRACKSIZE; |
| 243 | | |
| 244 | | return FLOPPY_ERROR_SUCCESS; |
| 245 | | } |
| 246 | | |
| 247 | | |
| 248 | | static floperr_t coupe_sdf_read_track(floppy_image *floppy, |
| 249 | | int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 250 | | { |
| 251 | | floperr_t err; |
| 252 | | UINT64 track_offset; |
| 253 | | |
| 254 | | /* get the offset to this track */ |
| 255 | | err = coupe_sdf_get_offset(floppy, head, track, &track_offset); |
| 256 | | if (err) return err; |
| 257 | | |
| 258 | | /* read track data into buffer */ |
| 259 | | floppy_image_read(floppy, buffer, offset + track_offset, buflen); |
| 260 | | |
| 261 | | return FLOPPY_ERROR_SUCCESS; |
| 262 | | } |
| 263 | | |
| 264 | | |
| 265 | | static floperr_t coupe_sdf_write_track(floppy_image *floppy, |
| 266 | | int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 267 | | { |
| 268 | | floperr_t err; |
| 269 | | UINT64 track_offset; |
| 270 | | |
| 271 | | /* get the offset to this track */ |
| 272 | | err = coupe_sdf_get_offset(floppy, head, track, &track_offset); |
| 273 | | if (err) return err; |
| 274 | | |
| 275 | | /* write buffer to image */ |
| 276 | | floppy_image_write(floppy, buffer, offset + track_offset, buflen); |
| 277 | | |
| 278 | | return FLOPPY_ERROR_SUCCESS; |
| 279 | | } |
| 280 | | |
| 281 | | |
| 282 | | static floperr_t coupe_sdf_get_sector_offset(floppy_image *floppy, |
| 283 | | int head, int track, int sector, UINT64 *offset) |
| 284 | | { |
| 285 | | floperr_t err; |
| 286 | | UINT8 buffer[SDF_TRACKSIZE]; |
| 287 | | int i, buffer_pos = 1; |
| 288 | | |
| 289 | | /* get track data */ |
| 290 | | err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE); |
| 291 | | if (err) return err; |
| 292 | | |
| 293 | | /* check if the sector is available in this track */ |
| 294 | | if (sector >= buffer[0]) |
| 295 | | return FLOPPY_ERROR_SEEKERROR; |
| 296 | | |
| 297 | | /* find the right sector in this track */ |
| 298 | | for (i = 0; i < buffer[0]; i++) |
| 299 | | { |
| 300 | | int sector_number = buffer[buffer_pos + 4]; |
| 301 | | |
| 302 | | if (sector_number - 1 == sector) |
| 303 | | { |
| 304 | | *offset = buffer_pos; |
| 305 | | return FLOPPY_ERROR_SUCCESS; |
| 306 | | } |
| 307 | | |
| 308 | | buffer_pos += (128 << buffer[buffer_pos + 5]) + 8; |
| 309 | | } |
| 310 | | |
| 311 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 312 | | } |
| 313 | | |
| 314 | | |
| 315 | | static floperr_t coupe_sdf_get_total_sector_offset(floppy_image *floppy, |
| 316 | | int head, int track, int sector, UINT64 *offset) |
| 317 | | { |
| 318 | | floperr_t err; |
| 319 | | UINT64 track_offset, sector_offset; |
| 320 | | |
| 321 | | /* get offset to the track start */ |
| 322 | | err = coupe_sdf_get_offset(floppy, head, track, &track_offset); |
| 323 | | if (err) return err; |
| 324 | | |
| 325 | | /* get offset to the start of the sector */ |
| 326 | | err = coupe_sdf_get_sector_offset(floppy, head, track, sector, §or_offset); |
| 327 | | if (err) return err; |
| 328 | | |
| 329 | | *offset = track_offset + sector_offset; |
| 330 | | |
| 331 | | return FLOPPY_ERROR_SUCCESS; |
| 332 | | } |
| 333 | | |
| 334 | | |
| 335 | | static floperr_t coupe_sdf_get_sector_length(floppy_image *floppy, |
| 336 | | int head, int track, int sector, UINT32 *sector_length) |
| 337 | | { |
| 338 | | floperr_t err; |
| 339 | | UINT8 buffer[SDF_TRACKSIZE]; |
| 340 | | UINT64 offset; |
| 341 | | |
| 342 | | /* get track data */ |
| 343 | | err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE); |
| 344 | | if (err) return err; |
| 345 | | |
| 346 | | /* get offset to the start of the sector */ |
| 347 | | err = coupe_sdf_get_sector_offset(floppy, head, track, sector, &offset); |
| 348 | | if (err) return err; |
| 349 | | |
| 350 | | /* get size */ |
| 351 | | *sector_length = 128 << buffer[offset + 5]; |
| 352 | | |
| 353 | | return FLOPPY_ERROR_SUCCESS; |
| 354 | | } |
| 355 | | |
| 356 | | |
| 357 | | static floperr_t coupe_sdf_get_indexed_sector_info(floppy_image *floppy, |
| 358 | | int head, int track, int sector_index, |
| 359 | | int *cylinder, int *side, int *sector, UINT32 *sector_length, |
| 360 | | unsigned long *flags) |
| 361 | | { |
| 362 | | floperr_t err; |
| 363 | | UINT8 buffer[SDF_TRACKSIZE]; |
| 364 | | UINT64 offset; |
| 365 | | |
| 366 | | /* get track data */ |
| 367 | | err = coupe_sdf_read_track(floppy, head, track, 0, &buffer, SDF_TRACKSIZE); |
| 368 | | if (err) return err; |
| 369 | | |
| 370 | | /* get offset to the start of the sector */ |
| 371 | | err = coupe_sdf_get_sector_offset(floppy, head, track, sector_index, &offset); |
| 372 | | if (err) return err; |
| 373 | | |
| 374 | | /* extract data */ |
| 375 | | if (cylinder) *cylinder = buffer[offset + 2]; |
| 376 | | if (side) *side = buffer[offset + 3]; |
| 377 | | if (sector) *sector = buffer[offset + 4]; |
| 378 | | if (sector_length) *sector_length = 128 << buffer[offset + 5]; |
| 379 | | if (flags) *flags = 0; /* TODO: read DAM or DDAM and determine flags */ |
| 380 | | |
| 381 | | return FLOPPY_ERROR_SUCCESS; |
| 382 | | } |
| 383 | | |
| 384 | | |
| 385 | | static floperr_t coupe_sdf_read_indexed_sector(floppy_image *floppy, |
| 386 | | int head, int track, int sector, void *buffer, size_t buflen) |
| 387 | | { |
| 388 | | floperr_t err; |
| 389 | | UINT64 offset; |
| 390 | | |
| 391 | | err = coupe_sdf_get_total_sector_offset(floppy, head, track, sector, &offset); |
| 392 | | if (err) return err; |
| 393 | | |
| 394 | | /* read sector data into buffer */ |
| 395 | | floppy_image_read(floppy, buffer, offset + 8, buflen); |
| 396 | | |
| 397 | | return FLOPPY_ERROR_SUCCESS; |
| 398 | | } |
| 399 | | |
| 400 | | |
| 401 | | static floperr_t coupe_sdf_write_indexed_sector(floppy_image *floppy, |
| 402 | | int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 403 | | { |
| 404 | | floperr_t err; |
| 405 | | UINT64 offset; |
| 406 | | |
| 407 | | err = coupe_sdf_get_total_sector_offset(floppy, head, track, sector, &offset); |
| 408 | | if (err) return err; |
| 409 | | |
| 410 | | /* write buffer into image */ |
| 411 | | floppy_image_write(floppy, buffer, offset + 8, buflen); |
| 412 | | |
| 413 | | return FLOPPY_ERROR_SUCCESS; |
| 414 | | } |
| 415 | | |
| 416 | | |
| 417 | | static void coupe_sdf_interpret_header(floppy_image *floppy, int *heads, int *tracks) |
| 418 | | { |
| 419 | | UINT64 size = floppy_image_size(floppy); |
| 420 | | |
| 421 | | if (size % SDF_TRACKSIZE == 0) |
| 422 | | { |
| 423 | | *heads = 2; |
| 424 | | *tracks = size / (SDF_TRACKSIZE * 2); |
| 425 | | } |
| 426 | | else |
| 427 | | { |
| 428 | | *heads = 0; |
| 429 | | *tracks = 0; |
| 430 | | } |
| 431 | | } |
| 432 | | |
| 433 | | |
| 434 | | FLOPPY_CONSTRUCT( coupe_sdf_construct ) |
| 435 | | { |
| 436 | | struct FloppyCallbacks *callbacks; |
| 437 | | struct sdf_tag *tag; |
| 438 | | int heads, tracks; |
| 439 | | |
| 440 | | if (params) |
| 441 | | { |
| 442 | | /* create */ |
| 443 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 444 | | } |
| 445 | | else |
| 446 | | { |
| 447 | | /* load */ |
| 448 | | coupe_sdf_interpret_header(floppy, &heads, &tracks); |
| 449 | | } |
| 450 | | |
| 451 | | tag = (sdf_tag *)floppy_create_tag(floppy, sizeof(struct sdf_tag)); |
| 452 | | |
| 453 | | if (!tag) |
| 454 | | return FLOPPY_ERROR_OUTOFMEMORY; |
| 455 | | |
| 456 | | tag->heads = heads; |
| 457 | | tag->tracks = tracks; |
| 458 | | |
| 459 | | callbacks = floppy_callbacks(floppy); |
| 460 | | callbacks->read_track = coupe_sdf_read_track; |
| 461 | | callbacks->write_track = coupe_sdf_write_track; |
| 462 | | callbacks->get_heads_per_disk = coupe_sdf_get_heads_per_disk; |
| 463 | | callbacks->get_tracks_per_disk = coupe_sdf_get_tracks_per_disk; |
| 464 | | callbacks->get_track_size = coupe_sdf_get_track_size; |
| 465 | | callbacks->get_sector_length = coupe_sdf_get_sector_length; |
| 466 | | callbacks->read_indexed_sector = coupe_sdf_read_indexed_sector; |
| 467 | | callbacks->write_indexed_sector = coupe_sdf_write_indexed_sector; |
| 468 | | callbacks->get_indexed_sector_info = coupe_sdf_get_indexed_sector_info; |
| 469 | | |
| 470 | | return FLOPPY_ERROR_SUCCESS; |
| 471 | | } |
| 472 | | |
| 473 | | |
| 474 | | FLOPPY_IDENTIFY( coupe_sdf_identify ) |
| 475 | | { |
| 476 | | int heads, tracks; |
| 477 | | |
| 478 | | /* read header values */ |
| 479 | | coupe_sdf_interpret_header(floppy, &heads, &tracks); |
| 480 | | |
| 481 | | /* check for sensible values */ |
| 482 | | if (heads > 0 && heads < 3 && tracks > 0 && tracks < 84) |
| 483 | | *vote = 100; |
| 484 | | else |
| 485 | | *vote = 0; |
| 486 | | |
| 487 | | return FLOPPY_ERROR_SUCCESS; |
| 488 | | } |
src/mess/formats/ti99_dsk.c
| r11457 | r11458 | |
| 1 | | /********************************************************************* |
| 2 | | * |
| 3 | | * formats/ti99_dsk.c |
| 4 | | * |
| 5 | | * TI99 and Geneve disk images |
| 6 | | * Sector Dump and Track Dump format |
| 7 | | * |
| 8 | | * used by TI-99/4, TI-99/4A, TI-99/8, SGCPU ("TI-99/4P"), and Geneve |
| 9 | | * |
| 10 | | * The Sector Dump Format is also known as v9t9 (named after the first TI |
| 11 | | * emulator to use this format). It is a contiguous sequence of sector contents |
| 12 | | * without track data. The first sector of the disk is located at the start of |
| 13 | | * the image, while the last sector is at its end. |
| 14 | | * |
| 15 | | * The Track Dump Format is also known as pc99 (again, named after the first |
| 16 | | * TI emulator to use this format). It is a contiguous sequence of track |
| 17 | | * contents, containing all information including address marks and CRC, but it |
| 18 | | * does not contain clock signals. Therefore, the format requires that the |
| 19 | | * address marks be at the same positions within each track. |
| 20 | | * |
| 21 | | * Both formats allow for a broad range of medium sizes. All sectors are 256 |
| 22 | | * bytes long. The most common formats are 9 sectors per track, single-sided, |
| 23 | | * 40 tracks, which yields 90 KiB of sector data (known as SSSD), and 18 |
| 24 | | * sectors per track, double-sided, and 40 tracks, which is 360 KiB (known as |
| 25 | | * DSDD). There are rare occurances of 8/16 sectors/track |
| 26 | | * (prototypical TI double-density controller) and 35 track media. Newer |
| 27 | | * controllers and ROMs allow for up to 36 sectors per track and 80 tracks on |
| 28 | | * both sides, which is 2,88 MiB (DSHD80). |
| 29 | | * |
| 30 | | * Different to earlier implementations, these format implementations provide |
| 31 | | * full track read/write capabilities. For the SDF format, the missing track |
| 32 | | * information is recreated, and a common interleave is assumed. |
| 33 | | * |
| 34 | | * Note that in principle, writing tracks may change track sizes. |
| 35 | | * As neither one of the formats contains meta-information about track lengths, |
| 36 | | * and as the image consists of a contiguous sequence of the tracks, we |
| 37 | | * cannot allow different track sizes currently. Accordingly, the track size |
| 38 | | * cannot be changed after the image has been created by imgtool. |
| 39 | | * Hence, also sector count per tracks and recording type (FM or MFM) cannot be |
| 40 | | * changed without breaking the image. |
| 41 | | * For example, MFM disks with 18 sectors per track can only be reformatted to |
| 42 | | * MFM/18. Moreover, while this could be handled in a future revision, |
| 43 | | * the number of tracks should also remain unchanged. |
| 44 | | * |
| 45 | | * You can use blank image files (filled with zeros or arbitrary data) and use |
| 46 | | * the Disk Manager cartridge to properly format them. Also, this can be done |
| 47 | | * by imgtool. The image file must match the format length (representing the |
| 48 | | * space on the physical medium). |
| 49 | | * |
| 50 | | * 80 track drive handling |
| 51 | | * |
| 52 | | * For the TI system there may be a situation where 40 track disks are used |
| 53 | | * in 80 track drives. In these cases, the disk driver applies double-stepping, |
| 54 | | * that is, it advances the head twice in order to seek the next track. We |
| 55 | | * emulate this by dividing the track number by 2, which means that two tracks |
| 56 | | * are mapped to one (0,1 -> 0; 2,3 -> 1; 4,5 -> 2; ...; 78,79 -> 39) |
| 57 | | * |
| 58 | | * Technical detail: The size of the track must be given before actually |
| 59 | | * starting write_track as the controller implementation uses buffered |
| 60 | | * transfer. |
| 61 | | * |
| 62 | | * Michael Zapf, Feb 2010 |
| 63 | | * |
| 64 | | * TODO: Link to imgtool. imgtool still uses its own format creation |
| 65 | | * |
| 66 | | * FIXME: If image is broken with good first track and defect higher tracks, |
| 67 | | * tdf_guess_geometry fails to define a geometry. The guessing should |
| 68 | | * always be done. |
| 69 | | ********************************************************************/ |
| 70 | | |
| 71 | | #include <string.h> |
| 72 | | #include <assert.h> |
| 73 | | #include <time.h> |
| 74 | | |
| 75 | | #include "osdepend.h" |
| 76 | | #include "imageutl.h" |
| 77 | | #include "formats/ti99_dsk.h" |
| 78 | | |
| 79 | | #define SECTOR_SIZE 256 |
| 80 | | #define TI99_IDAM_LENGTH 7 |
| 81 | | |
| 82 | | /* |
| 83 | | Determines whether we are using 80 track drives. This variable is |
| 84 | | necessary unless we get information about the dip |
| 85 | | switch settings. It is set by the disk controller implementations |
| 86 | | (bwg, hfdc, ti_fdc). |
| 87 | | */ |
| 88 | | static int use_80_track_drives = FALSE; |
| 89 | | |
| 90 | | struct ti99dsk_geometry |
| 91 | | { |
| 92 | | UINT8 sides; |
| 93 | | UINT8 tracksperside; |
| 94 | | UINT8 secspertrack; |
| 95 | | UINT8 density; |
| 96 | | }; |
| 97 | | |
| 98 | | struct ti99dsk_tag |
| 99 | | { |
| 100 | | int heads; |
| 101 | | int tracks; |
| 102 | | int sectors; |
| 103 | | int track_size; // Complete track; to be calculated |
| 104 | | int format; |
| 105 | | int first_idam; |
| 106 | | int dam_offset; |
| 107 | | }; |
| 108 | | |
| 109 | | /* |
| 110 | | Parametrizes the format to use 80 track drives. For TI systems we |
| 111 | | need to be able to emulate 40 track drives and 80 track drives. |
| 112 | | This is set in the configuration, but the format has no direct access. |
| 113 | | So as a preliminary solution we use this global function which sets the |
| 114 | | flag. This function is called by the disk controller implementations |
| 115 | | (bwg, hfdc, ti_fdc). |
| 116 | | */ |
| 117 | | void ti99_set_80_track_drives(int use80) |
| 118 | | { |
| 119 | | use_80_track_drives = use80; |
| 120 | | } |
| 121 | | |
| 122 | | #define TI99_DSK_TAG "ti99dsktag" |
| 123 | | #define TI99DSK_BLOCKNOTFOUND -1 |
| 124 | | |
| 125 | | /* |
| 126 | | Searches a block containing number * byte, starting at the given |
| 127 | | position. Returns the position of the first byte of the block. |
| 128 | | */ |
| 129 | | static int find_block(const UINT8 *buffer, int start, int stop, UINT8 byte, size_t number) |
| 130 | | { |
| 131 | | int i = start; |
| 132 | | size_t current = number; |
| 133 | | while (i < stop && current > 0) |
| 134 | | { |
| 135 | | if (buffer[i++] != byte) |
| 136 | | { |
| 137 | | current = number; |
| 138 | | } |
| 139 | | else |
| 140 | | { |
| 141 | | current--; |
| 142 | | } |
| 143 | | } |
| 144 | | if (current==0) |
| 145 | | { |
| 146 | | return i - number; |
| 147 | | } |
| 148 | | else |
| 149 | | return TI99DSK_BLOCKNOTFOUND; |
| 150 | | } |
| 151 | | |
| 152 | | /* |
| 153 | | Queried by flopdrv. Instead of the image tracks, we return the tracks |
| 154 | | of the floppy drive. We assume we always have two heads. |
| 155 | | */ |
| 156 | | static int ti99_get_heads_per_disk(floppy_image *floppy) |
| 157 | | { |
| 158 | | // struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 159 | | // return tag->heads; |
| 160 | | return 2; |
| 161 | | } |
| 162 | | |
| 163 | | /* |
| 164 | | Queried by flopdrv. Instead of the image tracks, we return the tracks |
| 165 | | of the floppy drive. |
| 166 | | */ |
| 167 | | static int ti99_get_tracks_per_disk(floppy_image *floppy) |
| 168 | | { |
| 169 | | int drivetracks = 42; |
| 170 | | |
| 171 | | // This may fail on startup; therefore we use a special function in |
| 172 | | // in the floppy controller implementations to explicitly set the geometry. |
| 173 | | if (use_80_track_drives) |
| 174 | | { |
| 175 | | drivetracks = 83; |
| 176 | | } |
| 177 | | return drivetracks; |
| 178 | | } |
| 179 | | |
| 180 | | static floperr_t ti99_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 181 | | { |
| 182 | | *sector_length=SECTOR_SIZE; |
| 183 | | return FLOPPY_ERROR_SUCCESS; |
| 184 | | } |
| 185 | | |
| 186 | | /* |
| 187 | | Return the track size. We do not allow different track |
| 188 | | sizes, so we store the track size in the tag. |
| 189 | | */ |
| 190 | | static UINT32 ti99_get_track_size(floppy_image *floppy, int head, int track) |
| 191 | | { |
| 192 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 193 | | return tag->track_size; |
| 194 | | } |
| 195 | | |
| 196 | | /* |
| 197 | | static void create_vib(UINT8 *sector0, option_resolution *params) |
| 198 | | { |
| 199 | | const char *name = "UNNAMED "; |
| 200 | | int sides, tracksperside,secspertrack,totalsectors,ausize,allocbytes,i; |
| 201 | | |
| 202 | | enum { NAME=0, TOTALSECS=10, SECSPERTRACK=11, SIG=12, PROT=16, TRACKS=17, SIDES=18, DENSITY=19, EXT=20, ALLOC=56 }; |
| 203 | | |
| 204 | | sides = option_resolution_lookup_int(params, PARAM_HEADS); |
| 205 | | tracksperside = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 206 | | secspertrack = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 207 | | |
| 208 | | totalsectors = sides * tracksperside * secspertrack; |
| 209 | | |
| 210 | | memcpy(§or0[NAME], name, 10); |
| 211 | | |
| 212 | | sector0[TOTALSECS] = (totalsectors>>8)&0xff; |
| 213 | | sector0[TOTALSECS+1] = (totalsectors&0xff); |
| 214 | | |
| 215 | | sector0[SECSPERTRACK] = secspertrack; |
| 216 | | sector0[SIG] = 'D'; |
| 217 | | sector0[SIG+1] = 'S'; |
| 218 | | sector0[SIG+2] = 'K'; |
| 219 | | |
| 220 | | sector0[PROT] = ' '; |
| 221 | | sector0[TRACKS] = tracksperside; |
| 222 | | sector0[SIDES] = sides; |
| 223 | | sector0[DENSITY] = (secspertrack<10)? 0x01 : 0x02; |
| 224 | | |
| 225 | | for (i=EXT; i < ALLOC; i++) |
| 226 | | sector0[i] = 0; |
| 227 | | |
| 228 | | ausize = (totalsectors / 1441) + 1; |
| 229 | | |
| 230 | | allocbytes = totalsectors / ausize / 8; |
| 231 | | |
| 232 | | for (i=ALLOC; i < ALLOC+allocbytes; i++) |
| 233 | | sector0[i] = 0; |
| 234 | | |
| 235 | | while (i++ < SECTOR_SIZE) |
| 236 | | sector0[i] = 0xff; |
| 237 | | } |
| 238 | | */ |
| 239 | | /* ========================================================================= |
| 240 | | Sector dump format |
| 241 | | =========================================================================*/ |
| 242 | | |
| 243 | | static floperr_t ti99_sdf_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen); |
| 244 | | static floperr_t ti99_sdf_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam); |
| 245 | | |
| 246 | | /* ----------------------------------------------------------------------- |
| 247 | | * Guess the geometry of the sector dump disk. |
| 248 | | * |
| 249 | | * Sector size is always 256 bytes. |
| 250 | | * |
| 251 | | * Common formats: |
| 252 | | * 90 KiB = 40 tracks / 1 side / 9 sectors = SSSD |
| 253 | | * 180 KiB = 40 tracks / 2 side / 9 sectors = DSSD (most common) |
| 254 | | * = 40 tracks / 1 side / 18 sectors = SSDD (rare) |
| 255 | | * = 80 tracks / 1 side / 9 sectors = SSSD80 (rare) |
| 256 | | * 360 KiB = 40 tracks / 2 side / 18 sectors = DSDD (most common) |
| 257 | | * = 80 tracks / 2 side / 9 sectors = DSSD80 (rare) |
| 258 | | * = 80 tracks / 1 side / 18 sectors = SSDD80 (rare) |
| 259 | | * = 40 tracks / 1 side / 36 sectors = SSHD (rare) |
| 260 | | * 720 KiB = 80 tracks / 2 side / 18 sectors = DSDD80 (most common) |
| 261 | | * 1440 KiB= 80 tracks / 2 side / 36 sectors = DSHD80 (most common) |
| 262 | | * |
| 263 | | * Moreover, there may be formats with 35 tracks (ancient) and 8 sectors/track. |
| 264 | | * We only check for the 8 sector formats. |
| 265 | | * 160 KiB = 40 tracks / 1 side / 16 sectors = SSDD8 |
| 266 | | * 320 KiB = 40 tracks / 2 side / 16 sectors = DSDD8 |
| 267 | | * |
| 268 | | * The Volume Information Block may contain suitable information, but not |
| 269 | | * before the disk is formatted and initialized. |
| 270 | | * |
| 271 | | * returns 100 if recognized, 0 if not recognized |
| 272 | | * ----------------------------------------------------------------------- */ |
| 273 | | |
| 274 | | static int ti99_sdf_guess_geometry(floppy_image *floppy, UINT64 size, |
| 275 | | struct ti99dsk_geometry *geometry) |
| 276 | | { |
| 277 | | int totsecs; |
| 278 | | typedef struct ti99_vib |
| 279 | | { |
| 280 | | char name[10]; // volume name (10 characters, pad with spaces) |
| 281 | | UINT8 totsecsMSB; // disk length in sectors (big-endian) (usually 360, 720 or 1440) |
| 282 | | UINT8 totsecsLSB; |
| 283 | | UINT8 secspertrack; // sectors per track (usually 9 (FM) or 18 (MFM)) |
| 284 | | UINT8 id[3]; // String "DSK" |
| 285 | | UINT8 protection; // 'P' if disk is protected, ' ' otherwise. |
| 286 | | UINT8 tracksperside; // tracks per side (usually 40) |
| 287 | | UINT8 sides; // sides (1 or 2) |
| 288 | | UINT8 density; // 0,1 (FM) or 2,3,4 (MFM) |
| 289 | | UINT8 res[36]; // Empty for traditional disks, or up to 3 directory pointers |
| 290 | | UINT8 abm[200]; // allocation bitmap: a 1 for each sector in use (sector 0 is LSBit of byte 0, |
| 291 | | // sector 7 is MSBit of byte 0, sector 8 is LSBit of byte 1, etc.) |
| 292 | | } ti99_vib; |
| 293 | | |
| 294 | | ti99_vib vib; |
| 295 | | UINT32 file_size; |
| 296 | | struct ti99dsk_geometry dummy_geometry; |
| 297 | | |
| 298 | | if (geometry) |
| 299 | | memset(geometry, 0, sizeof(*geometry)); |
| 300 | | else |
| 301 | | geometry = &dummy_geometry; |
| 302 | | |
| 303 | | if (size > 0xFFFFFFFF) |
| 304 | | return 0; |
| 305 | | |
| 306 | | file_size = (UINT32) size; |
| 307 | | |
| 308 | | // Read the Volume Information Block |
| 309 | | floppy_image_read(floppy, &vib, 0, sizeof(vib)); |
| 310 | | |
| 311 | | // If we have read the sector successfully, let us parse it |
| 312 | | totsecs = (vib.totsecsMSB << 8) | vib.totsecsLSB; |
| 313 | | geometry->secspertrack = vib.secspertrack; |
| 314 | | if (geometry->secspertrack == 0) |
| 315 | | // Some images might be like this, because the original SSSD |
| 316 | | // TI controller always assumes 9. |
| 317 | | geometry->secspertrack = 9; |
| 318 | | geometry->tracksperside = vib.tracksperside; |
| 319 | | if (geometry->tracksperside == 0) |
| 320 | | // Some images are like this, because the original SSSD TI controller |
| 321 | | // always assumes 40. |
| 322 | | geometry->tracksperside = 40; |
| 323 | | geometry->sides = vib.sides; |
| 324 | | if (geometry->sides == 0) |
| 325 | | // Some images are like this, because the original SSSD TI controller |
| 326 | | // always assumes that tracks beyond 40 are on side 2. */ |
| 327 | | geometry->sides = totsecs / (geometry->secspertrack * geometry->tracksperside); |
| 328 | | geometry->density = vib.density; |
| 329 | | // check that the format makes sense |
| 330 | | if (((geometry->secspertrack * geometry->tracksperside * geometry->sides) == totsecs) |
| 331 | | && (geometry->density <= 4) && (totsecs >= 2) && (! memcmp(vib.id, "DSK", 3)) |
| 332 | | && (file_size == totsecs*256)) |
| 333 | | { |
| 334 | | logerror("SDF/VIB consistent; tracks = %d, heads = %d, sectors = %d\n", geometry->tracksperside, geometry->sides, geometry->secspertrack); |
| 335 | | return 100; |
| 336 | | } |
| 337 | | logerror("SDF/VIB not consistent; guessing format\n"); |
| 338 | | |
| 339 | | // So that was not consistent. We guess the size from the file size |
| 340 | | // and assume that the VIB did not contain reliable data. For the |
| 341 | | // ambiguous case we choose the most common format. |
| 342 | | |
| 343 | | switch (file_size) |
| 344 | | { |
| 345 | | case 1*40*9*256: |
| 346 | | // 90kbytes: SSSD |
| 347 | | case 0: |
| 348 | | /*default:*/ |
| 349 | | geometry->sides = 1; |
| 350 | | geometry->tracksperside = 40; |
| 351 | | geometry->secspertrack = 9; |
| 352 | | geometry->density = 1; |
| 353 | | break; |
| 354 | | |
| 355 | | case 2*40*9*256: |
| 356 | | // 180kbytes: either DSSD or 18-sector-per-track SSDD. |
| 357 | | // We assume DSSD since DSSD is more common and is supported by |
| 358 | | // the original TI SD disk controller. |
| 359 | | geometry->sides = 2; |
| 360 | | geometry->tracksperside = 40; |
| 361 | | geometry->secspertrack = 9; |
| 362 | | geometry->density = 1; |
| 363 | | break; |
| 364 | | |
| 365 | | case 1*40*16*256: |
| 366 | | // 160kbytes: 16-sector-per-track SSDD (standard format for TI |
| 367 | | // DD disk controller prototype, and the TI hexbus disk |
| 368 | | // controller?) */ |
| 369 | | geometry->sides = 1; |
| 370 | | geometry->tracksperside = 40; |
| 371 | | geometry->secspertrack = 16; |
| 372 | | geometry->density = 2; |
| 373 | | break; |
| 374 | | |
| 375 | | case 2*40*16*256: |
| 376 | | // 320kbytes: 16-sector-per-track DSDD (standard format for TI |
| 377 | | // DD disk controller prototype, and TI hexbus disk |
| 378 | | // controller?) |
| 379 | | geometry->sides = 2; |
| 380 | | geometry->tracksperside = 40; |
| 381 | | geometry->secspertrack = 16; |
| 382 | | geometry->density = 2; |
| 383 | | break; |
| 384 | | |
| 385 | | case 2*40*18*256: |
| 386 | | // 360kbytes: 18-sector-per-track DSDD (standard format for most |
| 387 | | // third-party DD disk controllers, but reportedly not supported by |
| 388 | | // the original TI DD disk controller prototype) |
| 389 | | geometry->sides = 2; |
| 390 | | geometry->tracksperside = 40; |
| 391 | | geometry->secspertrack = 18; |
| 392 | | geometry->density = 2; |
| 393 | | break; |
| 394 | | |
| 395 | | case 2*80*18*256: |
| 396 | | // 720kbytes: 18-sector-per-track 80-track DSDD (Myarc only) |
| 397 | | geometry->sides = 2; |
| 398 | | geometry->tracksperside = 80; |
| 399 | | geometry->secspertrack = 18; |
| 400 | | geometry->density = 2; |
| 401 | | break; |
| 402 | | |
| 403 | | case 2*80*36*256: |
| 404 | | // 1.44Mbytes: DSHD (Myarc only) |
| 405 | | geometry->sides = 2; |
| 406 | | geometry->tracksperside = 80; |
| 407 | | geometry->secspertrack = 36; |
| 408 | | geometry->density = 3; |
| 409 | | break; |
| 410 | | |
| 411 | | default: |
| 412 | | logerror("Unrecognized disk image geometry\n"); |
| 413 | | return 0; |
| 414 | | } |
| 415 | | |
| 416 | | logerror("SDF geometry guess: tracks = %d, heads = %d, sectors = %d\n", geometry->tracksperside, geometry->sides, geometry->secspertrack); |
| 417 | | return 100; |
| 418 | | } |
| 419 | | |
| 420 | | /* |
| 421 | | Figure out the interleave for the sector arrangement in the track. For |
| 422 | | SDF format this is required for the read_track function. |
| 423 | | |
| 424 | | Interleave: |
| 425 | | 0 7 5 3 1 8 6 4 2; |
| 426 | | start with 0, 3, 6, 0, 3, 6, ... |
| 427 | | 0 11 4 15 8 1 12 5 16 9 2 13 6 17 10 3 14 7 |
| 428 | | start with 0 for every track |
| 429 | | |
| 430 | | Rare formats: 8 sectors |
| 431 | | 0 3 6 1 4 7 2 5 (i*3)%8 |
| 432 | | |
| 433 | | 16 sectors |
| 434 | | 0 9 2 11 4 13 6 15 8 1 10 3 12 5 14 7 (i*9)%16 |
| 435 | | |
| 436 | | 36 sectors |
| 437 | | 0 11 22 ... 3 14 25 (i*11)%36 |
| 438 | | */ |
| 439 | | static void guess_interleave(int sectors, int *step, int *initial) |
| 440 | | { |
| 441 | | switch (sectors) |
| 442 | | { |
| 443 | | case 8: |
| 444 | | *step = 3; |
| 445 | | /* initial is the offset from track to track+1 of the starting sector number */ |
| 446 | | *initial = 0; |
| 447 | | break; |
| 448 | | case 9: |
| 449 | | *step = 7; |
| 450 | | *initial = 3; |
| 451 | | break; |
| 452 | | case 16: |
| 453 | | *step = 9; |
| 454 | | *initial = 0; |
| 455 | | break; |
| 456 | | case 18: |
| 457 | | *step = 11; |
| 458 | | *initial = 0; |
| 459 | | break; |
| 460 | | case 36: |
| 461 | | *step = 11; |
| 462 | | *initial = 0; |
| 463 | | break; |
| 464 | | default: |
| 465 | | *step = 11; |
| 466 | | *initial = 0; |
| 467 | | break; |
| 468 | | } |
| 469 | | } |
| 470 | | |
| 471 | | /* |
| 472 | | Delivers a whole track. As the image consists only of |
| 473 | | sector data, we must reconstruct the track data and |
| 474 | | then insert the sector data. |
| 475 | | |
| 476 | | The track layout is exactly the Track Dump Format, except that the |
| 477 | | CRC values must be correctly calculated. |
| 478 | | |
| 479 | | Total length (FM) = 247 + sectors*334; (sectors=8 or 9) |
| 480 | | Total length (MFM) = 752 + sectors*340; (sectors=16 or 18) |
| 481 | | */ |
| 482 | | static floperr_t ti99_sdf_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 483 | | { |
| 484 | | int sector; |
| 485 | | int startstep; |
| 486 | | int step; |
| 487 | | int position=0; |
| 488 | | unsigned short crc; |
| 489 | | int i; |
| 490 | | floperr_t err; |
| 491 | | int imgtrack = track; |
| 492 | | |
| 493 | | UINT8 *trackdata = (UINT8*)buffer; |
| 494 | | |
| 495 | | /* Determine from the image geometry whether we have FM or MFM */ |
| 496 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 497 | | |
| 498 | | // Do we have a 40 track disk in an 80 track drive? In that case we |
| 499 | | // double the "width" of the tracks. */ |
| 500 | | if (use_80_track_drives && tag->tracks<=40) |
| 501 | | { |
| 502 | | imgtrack = track / 2; |
| 503 | | } |
| 504 | | |
| 505 | | guess_interleave(tag->sectors, &step, &startstep); |
| 506 | | |
| 507 | | if (tag->sectors < 10) /* must be FM */ |
| 508 | | { |
| 509 | | /* Write lead-in. */ |
| 510 | | memset(trackdata, 0x00, 16); |
| 511 | | position += 16; |
| 512 | | for (i=0; i < tag->sectors; i++) |
| 513 | | { |
| 514 | | /* Create interleaving */ |
| 515 | | sector = (i*step + imgtrack*startstep) % tag->sectors; |
| 516 | | |
| 517 | | memset(&trackdata[position], 0x00, 6); |
| 518 | | position += 6; |
| 519 | | /* Set IDAM */ |
| 520 | | trackdata[position++] = 0xfe; |
| 521 | | trackdata[position++] = imgtrack; |
| 522 | | trackdata[position++] = head; |
| 523 | | trackdata[position++] = sector; |
| 524 | | trackdata[position++] = 0x01; |
| 525 | | /* Calculate CRC16. Preset to 0xffff (what is 0xcdb4?) */ |
| 526 | | crc = ccitt_crc16(0xffff, &trackdata[position-(TI99_IDAM_LENGTH-2)], TI99_IDAM_LENGTH - 2); |
| 527 | | trackdata[position++] = (crc>>8)&0xff; |
| 528 | | trackdata[position++] = crc & 0xff; |
| 529 | | |
| 530 | | /* Write Gap2 */ |
| 531 | | memset(&trackdata[position], 0xff, 11); |
| 532 | | position += 11; |
| 533 | | memset(&trackdata[position], 0x00, 6); |
| 534 | | position += 6; |
| 535 | | /* Write DAM */ |
| 536 | | trackdata[position++] = 0xfb; |
| 537 | | |
| 538 | | /* Locate the sector content in the image and load it. */ |
| 539 | | err = ti99_sdf_read_sector(floppy, head, imgtrack, sector, &trackdata[position], SECTOR_SIZE); |
| 540 | | if (err) |
| 541 | | return err; |
| 542 | | |
| 543 | | position += SECTOR_SIZE; |
| 544 | | |
| 545 | | /* Set CRC16 */ |
| 546 | | crc = ccitt_crc16(0xffff, &trackdata[position-(SECTOR_SIZE+1)], SECTOR_SIZE+1); |
| 547 | | trackdata[position++] = (crc>>8)&0xff; |
| 548 | | trackdata[position++] = crc & 0xff; |
| 549 | | |
| 550 | | /* Write Gap3 */ |
| 551 | | memset(&trackdata[position], 0xff, 45); |
| 552 | | position += 45; |
| 553 | | } |
| 554 | | /* Write lead-out */ |
| 555 | | memset(&trackdata[position], 0xff, 231); |
| 556 | | position += 231; |
| 557 | | } |
| 558 | | else /* we have MFM */ |
| 559 | | { |
| 560 | | /* Write lead-in. */ |
| 561 | | memset(trackdata, 0x4e, 40); |
| 562 | | position += 40; |
| 563 | | for (i=0; i < tag->sectors; i++) |
| 564 | | { |
| 565 | | sector = (i*step + imgtrack*startstep) % tag->sectors; |
| 566 | | |
| 567 | | memset(&trackdata[position], 0x00, 10); |
| 568 | | position += 10; |
| 569 | | /* Write sync */ |
| 570 | | memset(&trackdata[position], 0xa1, 3); |
| 571 | | position += 3; |
| 572 | | /* Set IDAM */ |
| 573 | | trackdata[position++] = 0xfe; |
| 574 | | trackdata[position++] = imgtrack; |
| 575 | | trackdata[position++] = head; |
| 576 | | trackdata[position++] = sector; |
| 577 | | trackdata[position++] = 0x01; |
| 578 | | /* Set CRC16 */ |
| 579 | | crc = ccitt_crc16(0xffff, &trackdata[position-TI99_IDAM_LENGTH+2], TI99_IDAM_LENGTH - 2); |
| 580 | | trackdata[position++] = (crc>>8)&0xff; |
| 581 | | trackdata[position++] = crc & 0xff; |
| 582 | | |
| 583 | | /* Write Gap2 */ |
| 584 | | memset(&trackdata[position], 0x4e, 22); |
| 585 | | position += 22; |
| 586 | | memset(&trackdata[position], 0x00, 12); |
| 587 | | position += 12; |
| 588 | | /* Write sync */ |
| 589 | | memset(&trackdata[position], 0xa1, 3); |
| 590 | | position += 3; |
| 591 | | /* Write DAM */ |
| 592 | | trackdata[position++] = 0xfb; |
| 593 | | |
| 594 | | /* Locate the sector content in the image and load it. */ |
| 595 | | err = ti99_sdf_read_sector(floppy, head, track, sector, &trackdata[position], SECTOR_SIZE); |
| 596 | | if (err) |
| 597 | | return err; |
| 598 | | |
| 599 | | position += SECTOR_SIZE; |
| 600 | | |
| 601 | | /* Set CRC16 */ |
| 602 | | crc = ccitt_crc16(0xffff, &trackdata[position-SECTOR_SIZE-1], SECTOR_SIZE+1); |
| 603 | | trackdata[position++] = (crc>>8)&0xff; |
| 604 | | trackdata[position++] = crc & 0xff; |
| 605 | | |
| 606 | | /* Write Gap3 */ |
| 607 | | memset(&trackdata[position], 0x4e, 24); |
| 608 | | position += 24; |
| 609 | | } |
| 610 | | /* Write lead-out */ |
| 611 | | memset(&trackdata[position], 0x4e, 712); |
| 612 | | position += 712; |
| 613 | | } |
| 614 | | return FLOPPY_ERROR_SUCCESS; |
| 615 | | } |
| 616 | | |
| 617 | | /* |
| 618 | | Writes a whole track. As the sector dump format does not include track |
| 619 | | data, we have to extract the sector contents and to write them |
| 620 | | in sequence into the image. All the rest (gaps, crc, sync bytes) are |
| 621 | | dropped here. |
| 622 | | Bytes written to locations beyond the image end will be droppped |
| 623 | | silently (as if the medium is unwritable from that point). |
| 624 | | */ |
| 625 | | static floperr_t ti99_sdf_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 626 | | { |
| 627 | | int current_pos = 0; |
| 628 | | UINT8 *track_image; |
| 629 | | int leadin, gap1, gap2; |
| 630 | | int is_fm, found; |
| 631 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 632 | | int imgtrack = track; |
| 633 | | |
| 634 | | // Find out if we are going to write an FM or MFM track. Note that the |
| 635 | | // original TI controller is known to have a bug in the formatting |
| 636 | | // routine (using a MOVB instead of a MOV instruction when setting the |
| 637 | | // pointer to the track data), outputting false bytes at the |
| 638 | | // beginning of the track. So we do not rely on absolute positions |
| 639 | | // (and neither does the controller). |
| 640 | | track_image = (UINT8*)buffer; |
| 641 | | |
| 642 | | if (use_80_track_drives && tag->tracks<=40) |
| 643 | | { |
| 644 | | imgtrack = track / 2; |
| 645 | | } |
| 646 | | |
| 647 | | /* Only search in the first 100 bytes for the start. */ |
| 648 | | leadin = 40; |
| 649 | | gap1 = 10; |
| 650 | | gap2 = 12; |
| 651 | | is_fm = FALSE; |
| 652 | | |
| 653 | | current_pos = find_block(track_image, 0, 100, 0x4e, leadin); |
| 654 | | |
| 655 | | // In case of defect formats, we continue as far as possible. This |
| 656 | | // may lead to sectors not being written. */ |
| 657 | | if (current_pos==TI99DSK_BLOCKNOTFOUND) |
| 658 | | { |
| 659 | | /* Not MFM, possibly FM image */ |
| 660 | | /* Again, find Lead-in */ |
| 661 | | leadin = 16; |
| 662 | | current_pos = find_block(track_image, 0, 100, 0x00, leadin); |
| 663 | | if (current_pos==TI99DSK_BLOCKNOTFOUND) |
| 664 | | { |
| 665 | | /* If neither, forget about this process completely. */ |
| 666 | | logerror("Cannot find lead-in for track %d, head %d.\n", track, head); |
| 667 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 668 | | } |
| 669 | | gap1 = 6; |
| 670 | | gap2 = 6; |
| 671 | | is_fm = TRUE; |
| 672 | | } |
| 673 | | |
| 674 | | found = FALSE; |
| 675 | | /* Get behind lead-in */ |
| 676 | | current_pos += leadin; |
| 677 | | while (current_pos < buflen) |
| 678 | | { |
| 679 | | /* We must find the address block to determine the sector. */ |
| 680 | | int new_pos = find_block(track_image, current_pos, buflen, 0x00, gap1); |
| 681 | | if (new_pos==TI99DSK_BLOCKNOTFOUND) |
| 682 | | { |
| 683 | | /* Forget about the rest. */ |
| 684 | | if (found) break; /* we were already successful, so all ok */ |
| 685 | | logerror("Cannot find gap1 for track %d, head %d.\n", track, head); |
| 686 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 687 | | } |
| 688 | | found = TRUE; |
| 689 | | |
| 690 | | if (!is_fm) |
| 691 | | new_pos += 3; /* skip sync bytes in MFM */ |
| 692 | | |
| 693 | | if (track_image[new_pos + gap1]==0xfe) |
| 694 | | { |
| 695 | | current_pos = new_pos + gap1 + 1; |
| 696 | | /* IDAM found. */ |
| 697 | | int wtrack = track_image[current_pos]; |
| 698 | | int whead = track_image[current_pos+1]; |
| 699 | | int sector = track_image[current_pos+2]; |
| 700 | | if (wtrack == imgtrack && whead == head) |
| 701 | | { |
| 702 | | /* We skip the first part of gap2. */ |
| 703 | | new_pos = find_block(track_image, current_pos, buflen, 0x00, gap2); |
| 704 | | if (current_pos==TI99DSK_BLOCKNOTFOUND) |
| 705 | | { |
| 706 | | logerror("Cannot find gap2 for track %d, head %d.\n", imgtrack, head); |
| 707 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 708 | | } |
| 709 | | else |
| 710 | | { |
| 711 | | new_pos += gap2; |
| 712 | | if (!is_fm) |
| 713 | | new_pos += 3; /* skip sync */ |
| 714 | | current_pos = new_pos; |
| 715 | | if (track_image[current_pos]==0xfb) |
| 716 | | { |
| 717 | | /* DAM found. We now write the sector content to the image. */ |
| 718 | | ti99_sdf_write_sector(floppy, head, track, sector, &track_image[current_pos+1], SECTOR_SIZE, 0); |
| 719 | | current_pos += SECTOR_SIZE; |
| 720 | | } |
| 721 | | else |
| 722 | | logerror("DAM not found. Not writing write sector %d\n", sector); |
| 723 | | /* else not found, ignore this sector. */ |
| 724 | | } |
| 725 | | } |
| 726 | | else |
| 727 | | { |
| 728 | | // else the sector head data do not match the |
| 729 | | // current track and head. Ignore the sector. |
| 730 | | logerror("Wrong track: wtrack=%d, imgtrack=%d, whead=%d, imghead=%d\n",wtrack,imgtrack, whead,head); |
| 731 | | } |
| 732 | | } |
| 733 | | else |
| 734 | | { |
| 735 | | /* One step forward. Retry. */ |
| 736 | | current_pos++; |
| 737 | | } |
| 738 | | } |
| 739 | | return FLOPPY_ERROR_SUCCESS; |
| 740 | | } |
| 741 | | |
| 742 | | /* |
| 743 | | This method creates a properly formatted image. |
| 744 | | |
| 745 | | For a proper image, we need to format all tracks, and to create a |
| 746 | | minimal filesystem: |
| 747 | | |
| 748 | | name = "UNNAMED " |
| 749 | | number of sectors |
| 750 | | sectors/track |
| 751 | | "DSK" |
| 752 | | protection = 0x00 |
| 753 | | tracks/side |
| 754 | | sides |
| 755 | | density (0,1 = FM, 2 = MFM) |
| 756 | | 0x14 - 0x37 : 0x00 |
| 757 | | Allocation bitmap: 0xFF except for the first #sectors/AU size |
| 758 | | |
| 759 | | AU size = 1 (360 - 1440 sectors) |
| 760 | | = 2 (1441 - 2880 sectors) |
| 761 | | = 4 (2881 - 5760 sectors) |
| 762 | | = 8 (5760 - 11520 sectors) |
| 763 | | |
| 764 | | sector 0 = |
| 765 | | sector 1 = 256* 0x00 |
| 766 | | */ |
| 767 | | /* static floperr_t ti99_sdf_format_track(floppy_image *floppy, int head, int track, option_resolution *params) |
| 768 | | { |
| 769 | | UINT8 *sector0 = (UINT8*)malloc(SECTOR_SIZE); |
| 770 | | create_vib(sector0, params); |
| 771 | | free(sector0); |
| 772 | | if (true) |
| 773 | | { |
| 774 | | printf("not implemented\n"); |
| 775 | | return FLOPPY_ERROR_SEEKERROR; |
| 776 | | } |
| 777 | | return FLOPPY_ERROR_SUCCESS; |
| 778 | | } |
| 779 | | */ |
| 780 | | |
| 781 | | /* |
| 782 | | Get the offset from the start of the image file. In this format, logical |
| 783 | | tracks on two-sided disks are arranged as follows (example for 40 |
| 784 | | tracks; accordingly for 35 or 80) |
| 785 | | |
| 786 | | head-track |
| 787 | | 0-0 |
| 788 | | 0-1 |
| 789 | | 0-2 |
| 790 | | 0-3 |
| 791 | | ... |
| 792 | | 0-39 |
| 793 | | 1-39 |
| 794 | | 1-38 |
| 795 | | 1-37 |
| 796 | | ... |
| 797 | | 1-0 |
| 798 | | Note that we take the imgtrack, which may be half of the drive track. |
| 799 | | */ |
| 800 | | static floperr_t ti99_sdf_get_offset(floppy_image *floppy, int head, int imgtrack, int sector, UINT64 *offset) |
| 801 | | { |
| 802 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 803 | | |
| 804 | | if ((head < 0) || (head >= tag->heads) |
| 805 | | || (imgtrack < 0) || (imgtrack >= tag->tracks) |
| 806 | | || (sector < 0) || (sector > tag->sectors)) |
| 807 | | return FLOPPY_ERROR_SEEKERROR; |
| 808 | | |
| 809 | | if (head == 0) /* track numbers increasing towards inner track */ |
| 810 | | { |
| 811 | | *offset = (imgtrack * tag->sectors + sector) * SECTOR_SIZE; |
| 812 | | } |
| 813 | | else /* track numbers increasing towards outer track */ |
| 814 | | { |
| 815 | | *offset = (((2*tag->tracks)-1-imgtrack) * tag->sectors + sector) * SECTOR_SIZE; |
| 816 | | } |
| 817 | | return FLOPPY_ERROR_SUCCESS; |
| 818 | | } |
| 819 | | |
| 820 | | /* |
| 821 | | Read one sector at the specified position. |
| 822 | | */ |
| 823 | | static floperr_t ti99_sdf_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 824 | | { |
| 825 | | floperr_t err; |
| 826 | | UINT64 offset; |
| 827 | | int imgtrack = track; |
| 828 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 829 | | |
| 830 | | if (use_80_track_drives && tag->tracks<=40) |
| 831 | | { |
| 832 | | imgtrack = track / 2; |
| 833 | | } |
| 834 | | |
| 835 | | err = ti99_sdf_get_offset(floppy, head, imgtrack, sector, &offset); |
| 836 | | if (err) |
| 837 | | { |
| 838 | | return err; |
| 839 | | } |
| 840 | | floppy_image_read(floppy, buffer, offset, SECTOR_SIZE); |
| 841 | | |
| 842 | | return FLOPPY_ERROR_SUCCESS; |
| 843 | | } |
| 844 | | |
| 845 | | /* |
| 846 | | For the SDF format, sectors are always numbered in ascending order, |
| 847 | | starting with the lowest numbered sector in each track. In other words, |
| 848 | | we have nothing to calculate here. |
| 849 | | */ |
| 850 | | static floperr_t ti99_sdf_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 851 | | { |
| 852 | | return ti99_sdf_read_sector(floppy, head, track, sector, buffer, buflen); |
| 853 | | } |
| 854 | | |
| 855 | | /* |
| 856 | | Write one sector at the specified position. |
| 857 | | */ |
| 858 | | static floperr_t ti99_sdf_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 859 | | { |
| 860 | | UINT64 offset; |
| 861 | | floperr_t err; |
| 862 | | int imgtrack = track; |
| 863 | | |
| 864 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 865 | | |
| 866 | | if (use_80_track_drives && tag->tracks<=40) |
| 867 | | { |
| 868 | | imgtrack = track / 2; |
| 869 | | } |
| 870 | | |
| 871 | | err = ti99_sdf_get_offset(floppy, head, imgtrack, sector, &offset); |
| 872 | | if (err) |
| 873 | | { |
| 874 | | return err; |
| 875 | | } |
| 876 | | floppy_image_write(floppy, buffer, offset, SECTOR_SIZE); |
| 877 | | |
| 878 | | return FLOPPY_ERROR_SUCCESS; |
| 879 | | } |
| 880 | | |
| 881 | | /* |
| 882 | | See above; indexing and numbering coincide. |
| 883 | | */ |
| 884 | | static floperr_t ti99_sdf_write_indexed_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 885 | | { |
| 886 | | return ti99_sdf_write_sector(floppy, head, track, sector, buffer, buflen, ddam); |
| 887 | | } |
| 888 | | |
| 889 | | /* |
| 890 | | Required for ReadAddress command |
| 891 | | */ |
| 892 | | static floperr_t ti99_sdf_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 893 | | { |
| 894 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 895 | | |
| 896 | | if (use_80_track_drives && tag->tracks<=40) |
| 897 | | { |
| 898 | | track = track / 2; |
| 899 | | } |
| 900 | | |
| 901 | | if (sector_length) |
| 902 | | *sector_length=SECTOR_SIZE; |
| 903 | | if (cylinder) |
| 904 | | *cylinder = track; |
| 905 | | if (side) |
| 906 | | *side = head; |
| 907 | | if (sector) |
| 908 | | *sector = sector_index; |
| 909 | | if (flags) |
| 910 | | *flags = 0; |
| 911 | | if (sector_index > tag->sectors) |
| 912 | | { |
| 913 | | return FLOPPY_ERROR_SEEKERROR; |
| 914 | | } |
| 915 | | |
| 916 | | return FLOPPY_ERROR_SUCCESS; |
| 917 | | } |
| 918 | | |
| 919 | | /* |
| 920 | | Try to identify the image as an SDF format image. |
| 921 | | */ |
| 922 | | static FLOPPY_IDENTIFY(ti99_sdf_identify) |
| 923 | | { |
| 924 | | UINT64 size; |
| 925 | | size = floppy_image_size(floppy); |
| 926 | | *vote = ti99_sdf_guess_geometry(floppy, size, NULL); |
| 927 | | logerror("SDF voting %d\n", *vote); |
| 928 | | return FLOPPY_ERROR_SUCCESS; |
| 929 | | } |
| 930 | | |
| 931 | | /* |
| 932 | | Create the SDF data structures and set the callbacks. |
| 933 | | */ |
| 934 | | static FLOPPY_CONSTRUCT(ti99_sdf_construct) |
| 935 | | { |
| 936 | | struct ti99dsk_geometry geometry; |
| 937 | | struct FloppyCallbacks *callbacks; |
| 938 | | struct ti99dsk_tag *tag; |
| 939 | | logerror("Reading image as SDF\n"); |
| 940 | | |
| 941 | | if (params) |
| 942 | | { |
| 943 | | /* create */ |
| 944 | | memset(&geometry, 0, sizeof(geometry)); |
| 945 | | geometry.sides = option_resolution_lookup_int(params, PARAM_HEADS); |
| 946 | | geometry.tracksperside = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 947 | | geometry.secspertrack = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 948 | | |
| 949 | | /* We don't have headers for geometry */ |
| 950 | | /* check for usage in imgtool - we want to be able to create useful disks */ |
| 951 | | } |
| 952 | | else |
| 953 | | { |
| 954 | | /* load */ |
| 955 | | if (ti99_sdf_guess_geometry(floppy, floppy_image_size(floppy), &geometry)<50) |
| 956 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 957 | | } |
| 958 | | |
| 959 | | assert(geometry.sides); |
| 960 | | assert(geometry.tracksperside); |
| 961 | | assert(geometry.secspertrack); |
| 962 | | |
| 963 | | tag = (struct ti99dsk_tag *) floppy_create_tag(floppy, sizeof(struct ti99dsk_tag)); |
| 964 | | |
| 965 | | if (!tag) |
| 966 | | return FLOPPY_ERROR_OUTOFMEMORY; |
| 967 | | |
| 968 | | tag->heads = geometry.sides; |
| 969 | | tag->tracks = geometry.tracksperside; |
| 970 | | tag->sectors = geometry.secspertrack; |
| 971 | | if (tag->sectors < 10) |
| 972 | | /* FM mode */ |
| 973 | | tag->track_size = 16 + tag->sectors*334 + 231; |
| 974 | | else |
| 975 | | tag->track_size = 40 + tag->sectors*340 + 712; |
| 976 | | |
| 977 | | /* set up format callbacks */ |
| 978 | | callbacks = floppy_callbacks(floppy); |
| 979 | | callbacks->read_sector = ti99_sdf_read_sector; |
| 980 | | callbacks->write_sector = ti99_sdf_write_sector; |
| 981 | | callbacks->read_indexed_sector = ti99_sdf_read_indexed_sector; |
| 982 | | callbacks->write_indexed_sector = ti99_sdf_write_indexed_sector; |
| 983 | | callbacks->read_track = ti99_sdf_read_track; |
| 984 | | callbacks->write_track = ti99_sdf_write_track; |
| 985 | | /* callbacks->format_track = ti99_sdf_format_track; */ |
| 986 | | callbacks->get_track_size = ti99_get_track_size; |
| 987 | | /* post format unset */ |
| 988 | | callbacks->get_heads_per_disk = ti99_get_heads_per_disk; |
| 989 | | callbacks->get_tracks_per_disk = ti99_get_tracks_per_disk; |
| 990 | | callbacks->get_sector_length = ti99_get_sector_length; |
| 991 | | callbacks->get_indexed_sector_info = ti99_sdf_get_indexed_sector_info; |
| 992 | | |
| 993 | | return FLOPPY_ERROR_SUCCESS; |
| 994 | | } |
| 995 | | |
| 996 | | /* ----------------------------------------------------------------------- |
| 997 | | * Track Dump Format, aka PC99 format |
| 998 | | * This format includes all track data, including address marks and gaps. |
| 999 | | * However, CRC is not stored; instead, a f7f7 is stored at that location. |
| 1000 | | * Two track images exist, one for FM and one for MFM format. |
| 1001 | | * |
| 1002 | | * In this implementation, however, we do calculate the CRC. |
| 1003 | | * |
| 1004 | | * All tracks have exactly the same size, so offsets are calculated easily. |
| 1005 | | * |
| 1006 | | * This format is a precise map of the data contents of the track; |
| 1007 | | * however, it does not store clock patterns. |
| 1008 | | * |
| 1009 | | * The TDF format does not have a special index part, so any sector on it |
| 1010 | | * must be searched (as done by the disk controller) |
| 1011 | | * |
| 1012 | | * Interesting detail: The original TI disk controller has a bug which has |
| 1013 | | * just recently been discovered (using MESS!): The lead-in has another 9 |
| 1014 | | * bytes as a prefix; this is due to a wrongly set pointer in the format |
| 1015 | | * routine. Strictly speaking, it produces invalid track images. |
| 1016 | | * The original hardware does not care, however, so neither should we. |
| 1017 | | * To keep the emulation realistic (and to allow for a |
| 1018 | | * possible future extension with clock bits), we search for the |
| 1019 | | * beginning of the track. From there on, however, we accept no further |
| 1020 | | * deviations. This may make the resulting format unusable with the PC99 |
| 1021 | | * emulator, but it only appears when formatting with the TI disk |
| 1022 | | * controller anyway. If the image is created with another disk controller or |
| 1023 | | * using imgtool, the format is correctly created. |
| 1024 | | * |
| 1025 | | * FM track image |
| 1026 | | * |
| 1027 | | * Lead-in: 16* 00 |
| 1028 | | * Pre-ID gap: 6* 00 ---+ |
| 1029 | | * IDAM: fe | |
| 1030 | | * Track: tt | |
| 1031 | | * Head: hh | |
| 1032 | | * Sector: ss | |
| 1033 | | * Size: 01 | |
| 1034 | | * CRC: 2* f7 +--- repeat 9 times (8 in rare cases) |
| 1035 | | * Gap2: 11* ff | |
| 1036 | | * 6* 00 | |
| 1037 | | * DAM: fb | |
| 1038 | | * Content: 256* xx | |
| 1039 | | * CRC: 2* f7 | |
| 1040 | | * Gap3: 45* ff ---+ |
| 1041 | | * Lead-out: 231* ff |
| 1042 | | * |
| 1043 | | * -------------- |
| 1044 | | * |
| 1045 | | * MFM track image |
| 1046 | | * |
| 1047 | | * Lead-in: 40* 4e |
| 1048 | | * Pre-ID gap: 10* 00 ---+ |
| 1049 | | * Sync: 3* a1 | |
| 1050 | | * IDAM: fe | |
| 1051 | | * Track: tt | |
| 1052 | | * Head: hh | |
| 1053 | | * Sector: ss | |
| 1054 | | * Size: 01 | |
| 1055 | | * CRC: 2* f7 +--- repeat 18 times (16 in rare cases) |
| 1056 | | * Gap2: 22* 4e | |
| 1057 | | * 12* 00 | |
| 1058 | | * Sync: 3* a1 | |
| 1059 | | * DAM: fb | |
| 1060 | | * Content: 256* xx | |
| 1061 | | * CRC: 2* f7 | |
| 1062 | | * Gap3: 24* 4e ---+ |
| 1063 | | * Lead-out: 712* 4e |
| 1064 | | * |
| 1065 | | * ------------------ |
| 1066 | | * The tracks are located on the image as follows (different to SDF!): |
| 1067 | | * |
| 1068 | | * head-track |
| 1069 | | * 0-0 |
| 1070 | | * 0-1 |
| 1071 | | * 0-2 |
| 1072 | | * 0-3 |
| 1073 | | * ... |
| 1074 | | * 0-39 |
| 1075 | | * 1-0 |
| 1076 | | * 1-1 |
| 1077 | | * 1-2 |
| 1078 | | * ... |
| 1079 | | * 1-39 |
| 1080 | | * ----------------------------------------------------------------------- */ |
| 1081 | | |
| 1082 | | #define TI99_FM 1 |
| 1083 | | #define TI99_MFM 2 |
| 1084 | | |
| 1085 | | /* For the emulation of the clock pattern, we add a 9th bit. */ |
| 1086 | | #define TI99_DAM 0x1fb |
| 1087 | | #define TI99_IDAM 0x1fe |
| 1088 | | |
| 1089 | | #define TI99_FM_DAM_OFFSET 24 |
| 1090 | | #define TI99_MFM_DAM_OFFSET 44 |
| 1091 | | |
| 1092 | | #define TI99_FM_TRACK_ADD 247 |
| 1093 | | #define TI99_MFM_TRACK_ADD 752 |
| 1094 | | |
| 1095 | | #define TI99_FM_TOTAL_SECTOR 334 |
| 1096 | | #define TI99_MFM_TOTAL_SECTOR 340 |
| 1097 | | |
| 1098 | | /* |
| 1099 | | Determine offset of the first IDAM. This is the base of the simulation |
| 1100 | | of the clock bytes. We use a heuristic to guess where the track begins |
| 1101 | | and simply check whether we can locate the first IDAM and DAM. Return |
| 1102 | | value is the offset to the first IDAM. |
| 1103 | | */ |
| 1104 | | static floperr_t determine_offset(int format, UINT8 *track, int *offset) |
| 1105 | | { |
| 1106 | | floperr_t retval; |
| 1107 | | int current_pos; |
| 1108 | | |
| 1109 | | if (format==TI99_FM) |
| 1110 | | { |
| 1111 | | // printf("Trying FM\n"); |
| 1112 | | // We are seeking the lead-in and pre-id gap from |
| 1113 | | // position 0 to position 50. This is just to make sure that |
| 1114 | | // if there are bad bytes at the beginning (original TI controller) |
| 1115 | | // we will skip them. |
| 1116 | | current_pos = find_block(track, 0, 50, 0x00, 22); |
| 1117 | | if (current_pos==TI99DSK_BLOCKNOTFOUND) |
| 1118 | | { |
| 1119 | | logerror("Lead-in not found\n"); |
| 1120 | | retval = FLOPPY_ERROR_SEEKERROR; |
| 1121 | | } |
| 1122 | | else |
| 1123 | | { |
| 1124 | | current_pos += 22; |
| 1125 | | if (track[current_pos]==0xfe /* IDAM */ && |
| 1126 | | track[current_pos+4]==0x01 /* sector length, always 256 for TI */ && |
| 1127 | | track[current_pos+24]==0xfb) /* DAM */ |
| 1128 | | { |
| 1129 | | /* We're pretty sure this is the beginning. */ |
| 1130 | | *offset = current_pos; |
| 1131 | | retval = FLOPPY_ERROR_SUCCESS; |
| 1132 | | } |
| 1133 | | else |
| 1134 | | { |
| 1135 | | logerror("IDAM or DAM not found\n"); |
| 1136 | | retval = FLOPPY_ERROR_SEEKERROR; |
| 1137 | | } |
| 1138 | | } |
| 1139 | | } |
| 1140 | | else |
| 1141 | | { |
| 1142 | | /* MFM */ |
| 1143 | | // printf("Trying MFM\n"); |
| 1144 | | int track_start = find_block(track, 0, 100, 0x4e, 40); |
| 1145 | | if (track_start==TI99DSK_BLOCKNOTFOUND) |
| 1146 | | { |
| 1147 | | logerror("Lead-in not found\n"); |
| 1148 | | retval = FLOPPY_ERROR_SEEKERROR; |
| 1149 | | } |
| 1150 | | else |
| 1151 | | { |
| 1152 | | track_start += 40; |
| 1153 | | current_pos = find_block(track, track_start, track_start+10, 0x00, 10); |
| 1154 | | if (current_pos==TI99DSK_BLOCKNOTFOUND) |
| 1155 | | { |
| 1156 | | logerror("Pre-gap not found\n"); |
| 1157 | | retval = FLOPPY_ERROR_SEEKERROR; |
| 1158 | | } |
| 1159 | | else |
| 1160 | | { |
| 1161 | | current_pos += 10; |
| 1162 | | if (track[current_pos] == 0xa1 /* First sync */ && |
| 1163 | | track[current_pos+3] == 0xfe /* IDAM */ && |
| 1164 | | track[current_pos+44] == 0xa1 /* First sync */ && |
| 1165 | | track[current_pos+47] == 0xfb) /* DAM */ |
| 1166 | | { |
| 1167 | | /* We're pretty sure this is the beginning. */ |
| 1168 | | *offset = current_pos + 3; |
| 1169 | | retval = FLOPPY_ERROR_SUCCESS; |
| 1170 | | } |
| 1171 | | else |
| 1172 | | { |
| 1173 | | logerror("Sync/IDAM/DAM not found\n"); |
| 1174 | | retval = FLOPPY_ERROR_SEEKERROR; |
| 1175 | | } |
| 1176 | | } |
| 1177 | | } |
| 1178 | | } |
| 1179 | | /* printf("TDF offset = 0x%x\n", *offset); */ |
| 1180 | | return retval; |
| 1181 | | } |
| 1182 | | |
| 1183 | | /* |
| 1184 | | Gets a byte from the track. In this format we do not have a clock |
| 1185 | | signal, so we will simulate it. Thus, this function returns a 9-bit |
| 1186 | | value, with the first bit set as a marker for an address mark. |
| 1187 | | |
| 1188 | | This may be a little more complicated than necessary, but it correctly |
| 1189 | | emulates the behavior of the controller (and it allows for an |
| 1190 | | alternative format which actually supports clock and data). |
| 1191 | | */ |
| 1192 | | static int read_byte(int format, int first_idam, UINT8 *track, int position) |
| 1193 | | { |
| 1194 | | int totalseclen; |
| 1195 | | int dam_offset; |
| 1196 | | int byte; |
| 1197 | | |
| 1198 | | /* Check whether we are at any IDAM or DAM in this track. */ |
| 1199 | | if (format==TI99_FM) |
| 1200 | | { |
| 1201 | | totalseclen = TI99_FM_TOTAL_SECTOR; |
| 1202 | | dam_offset = TI99_FM_DAM_OFFSET; |
| 1203 | | } |
| 1204 | | else |
| 1205 | | { |
| 1206 | | totalseclen = TI99_MFM_TOTAL_SECTOR; |
| 1207 | | dam_offset = TI99_MFM_DAM_OFFSET; |
| 1208 | | } |
| 1209 | | |
| 1210 | | int curpos = (position - first_idam) % totalseclen; |
| 1211 | | if (curpos==0 && track[position]==0xfe) |
| 1212 | | byte = TI99_IDAM; |
| 1213 | | else |
| 1214 | | { |
| 1215 | | if (curpos==dam_offset && track[position]==0xfb) |
| 1216 | | byte = TI99_DAM; |
| 1217 | | else |
| 1218 | | byte = track[position] & 0xff; |
| 1219 | | } |
| 1220 | | return byte; |
| 1221 | | } |
| 1222 | | |
| 1223 | | /* |
| 1224 | | Determine the position of this track in the image. |
| 1225 | | */ |
| 1226 | | static floperr_t ti99_tdf_get_offset(floppy_image *floppy, int head, int imgtrack, UINT64 *offset) |
| 1227 | | { |
| 1228 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1229 | | |
| 1230 | | if ((head < 0) || (head >= tag->heads) || (imgtrack < 0) || (imgtrack >= tag->tracks)) |
| 1231 | | return FLOPPY_ERROR_SEEKERROR; |
| 1232 | | |
| 1233 | | // For head 0, tracks increase from 0 to tracks-1; then for head 1, |
| 1234 | | // tracks also increase from 0 to tracks-1. */ |
| 1235 | | *offset = head * (tag->tracks * tag->track_size) + imgtrack * tag->track_size; |
| 1236 | | |
| 1237 | | return FLOPPY_ERROR_SUCCESS; |
| 1238 | | } |
| 1239 | | |
| 1240 | | /* |
| 1241 | | Reads a track. We just copy the contents from the format and recreate |
| 1242 | | the CRC if it is the "blank" CRC as F7F7. |
| 1243 | | This method assumes that the track division be done already. |
| 1244 | | */ |
| 1245 | | static floperr_t ti99_tdf_read_track_internal(floppy_image *floppy, int head, int imgtrack, UINT64 offset, void *buffer, size_t buflen) |
| 1246 | | { |
| 1247 | | floperr_t err; |
| 1248 | | UINT64 track_offset; |
| 1249 | | int first_idam = 0; |
| 1250 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1251 | | int i, byte, crc; |
| 1252 | | |
| 1253 | | UINT8 *track_data = (UINT8*)buffer; |
| 1254 | | |
| 1255 | | err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset); |
| 1256 | | if (err) |
| 1257 | | return err; |
| 1258 | | |
| 1259 | | floppy_image_read(floppy, buffer, track_offset, buflen); |
| 1260 | | |
| 1261 | | // Rebuild the CRCs. PC99 did not store the CRC but put |
| 1262 | | // F7F7 in its place. |
| 1263 | | // The first CRC is at position IDAM + 5 |
| 1264 | | // The second CRC is at position IDAM + 0x119 (FM) or +0x12d (MFM) |
| 1265 | | // All this is repeated for each sector in the track |
| 1266 | | |
| 1267 | | if (determine_offset(tag->format, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1268 | | return FLOPPY_ERROR_SEEKERROR; |
| 1269 | | |
| 1270 | | i = 0; |
| 1271 | | while (i < buflen) |
| 1272 | | { |
| 1273 | | byte = read_byte(tag->format, first_idam, track_data, i); |
| 1274 | | if (byte == TI99_IDAM) |
| 1275 | | { |
| 1276 | | // Do we have a valid CRCs already? Then this image |
| 1277 | | // already contains proper CRCs handling. Do not recreate them. |
| 1278 | | if (track_data[i+5] != 0xf7 || track_data[i+6] != 0xf7) |
| 1279 | | return FLOPPY_ERROR_SUCCESS; |
| 1280 | | |
| 1281 | | crc = ccitt_crc16(0xffff, &track_data[i], 5); |
| 1282 | | track_data[i+5] = (crc>>8) & 0xff; |
| 1283 | | track_data[i+6] = (crc & 0xff); |
| 1284 | | } |
| 1285 | | else |
| 1286 | | { |
| 1287 | | if (byte == TI99_DAM) |
| 1288 | | { |
| 1289 | | crc = ccitt_crc16(0xffff, &track_data[i], SECTOR_SIZE+1); |
| 1290 | | track_data[i+SECTOR_SIZE+1] = (crc>>8) & 0xff; |
| 1291 | | track_data[i+SECTOR_SIZE+2] = (crc & 0xff); |
| 1292 | | } |
| 1293 | | } |
| 1294 | | i++; |
| 1295 | | } |
| 1296 | | // dump_contents(track_data, buflen); |
| 1297 | | return FLOPPY_ERROR_SUCCESS; |
| 1298 | | } |
| 1299 | | |
| 1300 | | /* |
| 1301 | | Reads a track. We just copy the contents from the format, without |
| 1302 | | changes. |
| 1303 | | */ |
| 1304 | | static floperr_t ti99_tdf_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 1305 | | { |
| 1306 | | int imgtrack = track; |
| 1307 | | |
| 1308 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1309 | | if (use_80_track_drives && tag->tracks<=40) |
| 1310 | | { |
| 1311 | | imgtrack = track / 2; |
| 1312 | | } |
| 1313 | | return ti99_tdf_read_track_internal(floppy, head, imgtrack, offset, buffer, buflen); |
| 1314 | | } |
| 1315 | | |
| 1316 | | /* |
| 1317 | | Writes a track. |
| 1318 | | */ |
| 1319 | | static floperr_t ti99_tdf_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 1320 | | { |
| 1321 | | floperr_t err; |
| 1322 | | UINT64 track_offset; |
| 1323 | | int imgtrack = track; |
| 1324 | | |
| 1325 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1326 | | if (use_80_track_drives && tag->tracks<=40) |
| 1327 | | { |
| 1328 | | imgtrack = track / 2; |
| 1329 | | } |
| 1330 | | |
| 1331 | | err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset); |
| 1332 | | if (err) |
| 1333 | | return err; |
| 1334 | | |
| 1335 | | // According to the TDF format, we don't need the CRC but replace it |
| 1336 | | // with F7F7. For now we keep it and see whether PC99 can cope with |
| 1337 | | // that. (Otherwise we would have to copy the const buffer). */ |
| 1338 | | floppy_image_write(floppy, buffer, offset + track_offset, buflen); |
| 1339 | | return FLOPPY_ERROR_SUCCESS; |
| 1340 | | } |
| 1341 | | |
| 1342 | | /* |
| 1343 | | static floperr_t ti99_tdf_format_track(floppy_image *floppy, int head, int track, option_resolution *params) |
| 1344 | | { |
| 1345 | | int sectors; |
| 1346 | | int sector_length; |
| 1347 | | int interleave; |
| 1348 | | int first_sector_id; |
| 1349 | | sectors = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 1350 | | sector_length = option_resolution_lookup_int(params, PARAM_SECTOR_LENGTH); |
| 1351 | | interleave = option_resolution_lookup_int(params, PARAM_INTERLEAVE); |
| 1352 | | first_sector_id = option_resolution_lookup_int(params, PARAM_FIRST_SECTOR_ID); |
| 1353 | | return FLOPPY_ERROR_SUCCESS; |
| 1354 | | } |
| 1355 | | */ |
| 1356 | | |
| 1357 | | /* |
| 1358 | | Get a sector from a track. This function takes a complete track, |
| 1359 | | searches for the sector, and passes a pointer to the sector contents |
| 1360 | | back. |
| 1361 | | As the original TI controller creates bad bytes at the start of a track |
| 1362 | | we do *not* assume a simple layout. The strategy is to search the |
| 1363 | | lead-in of the track, and from this offset, step through the track |
| 1364 | | in search for the sector. Since we do not have index marks (we cannot |
| 1365 | | distinguish them from ordinary data, as we do not store the clock), we |
| 1366 | | do not attempt to detect IDAM or DAM. |
| 1367 | | */ |
| 1368 | | static floperr_t ti99_tdf_seek_sector_in_track(floppy_image *floppy, int head, int track, int sector, UINT8 *track_data, UINT8 **sector_data) |
| 1369 | | { |
| 1370 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1371 | | |
| 1372 | | int byte; |
| 1373 | | int state; |
| 1374 | | enum { SEARCHIDAM, SEARCHDAM }; |
| 1375 | | |
| 1376 | | state = SEARCHIDAM; |
| 1377 | | if (tag->first_idam==0) /* should we check for every track? */ |
| 1378 | | { |
| 1379 | | if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1380 | | { |
| 1381 | | logerror("could not determine offset\n"); |
| 1382 | | return FLOPPY_ERROR_SEEKERROR; |
| 1383 | | } |
| 1384 | | } |
| 1385 | | |
| 1386 | | for (int i=0; i < tag->track_size; i++) |
| 1387 | | { |
| 1388 | | byte = read_byte(tag->format, tag->first_idam, track_data, i); |
| 1389 | | |
| 1390 | | switch (state) |
| 1391 | | { |
| 1392 | | case SEARCHIDAM: |
| 1393 | | /* search for the IDAM */ |
| 1394 | | if (byte==TI99_IDAM) |
| 1395 | | { |
| 1396 | | if ((track_data[i+1]==track) && (track_data[i+2]==head) && (track_data[i+3]==sector)) |
| 1397 | | state = SEARCHDAM; |
| 1398 | | else |
| 1399 | | state = SEARCHIDAM; |
| 1400 | | } |
| 1401 | | break; |
| 1402 | | case SEARCHDAM: |
| 1403 | | if (byte==TI99_DAM) |
| 1404 | | { |
| 1405 | | *sector_data = &track_data[i+1]; |
| 1406 | | return FLOPPY_ERROR_SUCCESS; |
| 1407 | | } |
| 1408 | | break; |
| 1409 | | } |
| 1410 | | } |
| 1411 | | logerror("Sector not found.\n"); |
| 1412 | | return FLOPPY_ERROR_SEEKERROR; |
| 1413 | | } |
| 1414 | | |
| 1415 | | static floperr_t ti99_tdf_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 1416 | | { |
| 1417 | | floperr_t err; |
| 1418 | | UINT8 *sector_data; |
| 1419 | | UINT8 *track_data; |
| 1420 | | int imgtrack = track; |
| 1421 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1422 | | |
| 1423 | | track_data = (UINT8*)malloc(tag->track_size); |
| 1424 | | |
| 1425 | | if (use_80_track_drives && tag->tracks<=40) |
| 1426 | | { |
| 1427 | | imgtrack = track / 2; |
| 1428 | | } |
| 1429 | | |
| 1430 | | err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size); |
| 1431 | | if (err) |
| 1432 | | return err; |
| 1433 | | |
| 1434 | | err = ti99_tdf_seek_sector_in_track(floppy, head, imgtrack, sector, track_data, §or_data); |
| 1435 | | if (err) |
| 1436 | | return err; |
| 1437 | | /* verify CRC? */ |
| 1438 | | |
| 1439 | | memcpy(buffer, sector_data, SECTOR_SIZE); |
| 1440 | | free(track_data); |
| 1441 | | return FLOPPY_ERROR_SUCCESS; |
| 1442 | | } |
| 1443 | | |
| 1444 | | /* |
| 1445 | | Writes a sector into the track. Note that the indexed_write method |
| 1446 | | may be used instead of this method, according to the implementation |
| 1447 | | of flopimg/flopdrv. |
| 1448 | | */ |
| 1449 | | static floperr_t ti99_tdf_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 1450 | | { |
| 1451 | | floperr_t err; |
| 1452 | | UINT8 *sector_data; |
| 1453 | | UINT8 *track_data; |
| 1454 | | UINT64 track_offset; |
| 1455 | | int imgtrack = track; |
| 1456 | | UINT64 offset; |
| 1457 | | UINT8 crc_field[2]; |
| 1458 | | int crc; |
| 1459 | | |
| 1460 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1461 | | |
| 1462 | | track_data = (UINT8*)malloc(tag->track_size); |
| 1463 | | if (use_80_track_drives && tag->tracks<=40) |
| 1464 | | { |
| 1465 | | imgtrack = track / 2; |
| 1466 | | } |
| 1467 | | |
| 1468 | | err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size); |
| 1469 | | if (err) |
| 1470 | | return err; |
| 1471 | | err = ti99_tdf_seek_sector_in_track(floppy, head, imgtrack, sector, track_data, §or_data); |
| 1472 | | if (err) |
| 1473 | | return err; |
| 1474 | | |
| 1475 | | err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset); |
| 1476 | | if (err) |
| 1477 | | return err; |
| 1478 | | |
| 1479 | | offset = track_offset + (sector_data - track_data); |
| 1480 | | |
| 1481 | | floppy_image_write(floppy, buffer, offset, SECTOR_SIZE); |
| 1482 | | |
| 1483 | | // Recalculate CRC. We init the CRC with bf84 (DAM fb is |
| 1484 | | // already included; would be ffff without) |
| 1485 | | crc = ccitt_crc16(0xbf84, (UINT8*)buffer, SECTOR_SIZE); |
| 1486 | | crc_field[0] = (crc>>8) & 0xff; |
| 1487 | | crc_field[1] = (crc & 0xff); |
| 1488 | | floppy_image_write(floppy, crc_field, offset+SECTOR_SIZE, 2); |
| 1489 | | |
| 1490 | | return FLOPPY_ERROR_SUCCESS; |
| 1491 | | } |
| 1492 | | |
| 1493 | | static floperr_t ti99_tdf_write_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, const void *buffer, size_t buflen, int ddam) |
| 1494 | | { |
| 1495 | | /* Read track, head */ |
| 1496 | | int byte; |
| 1497 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1498 | | UINT8 *track_data; |
| 1499 | | UINT8 *sector_data; |
| 1500 | | UINT64 track_offset; |
| 1501 | | UINT64 offset; |
| 1502 | | UINT8 crc_field[2]; |
| 1503 | | |
| 1504 | | int crc; |
| 1505 | | int imgtrack = track; |
| 1506 | | |
| 1507 | | int i; |
| 1508 | | floperr_t err; |
| 1509 | | |
| 1510 | | track_data = (UINT8*)malloc(tag->track_size); |
| 1511 | | |
| 1512 | | if (use_80_track_drives && tag->tracks<=40) |
| 1513 | | { |
| 1514 | | imgtrack = track / 2; |
| 1515 | | } |
| 1516 | | |
| 1517 | | err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size); |
| 1518 | | if (err) |
| 1519 | | { |
| 1520 | | free(track_data); |
| 1521 | | return err; |
| 1522 | | } |
| 1523 | | |
| 1524 | | /* Search for the sector_index-th sector. */ |
| 1525 | | if (tag->first_idam==0) /* should we check for every track? */ |
| 1526 | | { |
| 1527 | | if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1528 | | { |
| 1529 | | free(track_data); |
| 1530 | | return FLOPPY_ERROR_SEEKERROR; |
| 1531 | | } |
| 1532 | | } |
| 1533 | | |
| 1534 | | /* Search for the IDAM of the n-th sector */ |
| 1535 | | i=0; |
| 1536 | | while (i < tag->track_size && sector_index>=0) |
| 1537 | | { |
| 1538 | | byte = read_byte(tag->format, tag->first_idam, track_data, i); |
| 1539 | | if (byte == TI99_IDAM) |
| 1540 | | { |
| 1541 | | sector_index--; |
| 1542 | | } |
| 1543 | | i++; |
| 1544 | | } |
| 1545 | | i--; |
| 1546 | | |
| 1547 | | /* Not that many sectors in this track? */ |
| 1548 | | if (sector_index>0) |
| 1549 | | { |
| 1550 | | free(track_data); |
| 1551 | | return FLOPPY_ERROR_SEEKERROR; |
| 1552 | | } |
| 1553 | | |
| 1554 | | /* Find the DAM. */ |
| 1555 | | while (i < tag->track_size) |
| 1556 | | { |
| 1557 | | byte = read_byte(tag->format, tag->first_idam, track_data, i); |
| 1558 | | if (byte == TI99_DAM) |
| 1559 | | break; |
| 1560 | | else |
| 1561 | | i++; |
| 1562 | | } |
| 1563 | | |
| 1564 | | /* There was no DAM. Image seems to be broken. */ |
| 1565 | | if (i == tag->track_size) |
| 1566 | | { |
| 1567 | | free(track_data); |
| 1568 | | return FLOPPY_ERROR_SEEKERROR; |
| 1569 | | } |
| 1570 | | |
| 1571 | | /* Sector data is right after the DAM. */ |
| 1572 | | sector_data = &track_data[i+1]; |
| 1573 | | |
| 1574 | | /* Get the position of the track in the image. */ |
| 1575 | | err = ti99_tdf_get_offset(floppy, head, imgtrack, &track_offset); |
| 1576 | | if (err) |
| 1577 | | return err; |
| 1578 | | |
| 1579 | | offset = track_offset + (sector_data - track_data); |
| 1580 | | |
| 1581 | | /* Write the sector data at that position. */ |
| 1582 | | floppy_image_write(floppy, buffer, offset, SECTOR_SIZE); |
| 1583 | | |
| 1584 | | // Recalculate CRC. We init the CRC with bf84 (DAM fb is |
| 1585 | | // already included; would be ffff without) |
| 1586 | | crc = ccitt_crc16(0xbf84, (UINT8*)buffer, SECTOR_SIZE); |
| 1587 | | crc_field[0] = (crc>>8) & 0xff; |
| 1588 | | crc_field[1] = (crc & 0xff); |
| 1589 | | floppy_image_write(floppy, crc_field, offset+SECTOR_SIZE, 2); |
| 1590 | | |
| 1591 | | /* Free the temporary buffer */ |
| 1592 | | free(track_data); |
| 1593 | | |
| 1594 | | return FLOPPY_ERROR_SUCCESS; |
| 1595 | | } |
| 1596 | | |
| 1597 | | |
| 1598 | | static floperr_t ti99_tdf_find_indexed_sector(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, void *buffer, unsigned long *flags) |
| 1599 | | { |
| 1600 | | /* Read track, head */ |
| 1601 | | int byte = 0; |
| 1602 | | struct ti99dsk_tag *tag = (ti99dsk_tag*)floppy_tag(floppy); |
| 1603 | | UINT8 *track_data; |
| 1604 | | int imgtrack = track; |
| 1605 | | int i; |
| 1606 | | floperr_t err; |
| 1607 | | floperr_t retval; |
| 1608 | | track_data = (UINT8*)malloc(tag->track_size); |
| 1609 | | |
| 1610 | | if (use_80_track_drives && tag->tracks<=40) |
| 1611 | | { |
| 1612 | | imgtrack = track / 2; |
| 1613 | | } |
| 1614 | | |
| 1615 | | err = ti99_tdf_read_track_internal(floppy, head, imgtrack, 0, track_data, tag->track_size); |
| 1616 | | if (err) |
| 1617 | | { |
| 1618 | | free(track_data); |
| 1619 | | return err; |
| 1620 | | } |
| 1621 | | |
| 1622 | | /* Search for the sector_index-th sector. */ |
| 1623 | | |
| 1624 | | if (tag->first_idam==0) /* should we check for every track? */ |
| 1625 | | { |
| 1626 | | if (determine_offset(tag->format, track_data, &tag->first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1627 | | { |
| 1628 | | free(track_data); |
| 1629 | | return FLOPPY_ERROR_SEEKERROR; |
| 1630 | | } |
| 1631 | | } |
| 1632 | | |
| 1633 | | i=0; |
| 1634 | | sector_index = sector_index+1; |
| 1635 | | while (i++ < tag->track_size && sector_index>0) |
| 1636 | | { |
| 1637 | | byte = read_byte(tag->format, tag->first_idam, track_data, i); |
| 1638 | | if (byte == TI99_IDAM) |
| 1639 | | { |
| 1640 | | sector_index--; |
| 1641 | | } |
| 1642 | | } |
| 1643 | | |
| 1644 | | retval = FLOPPY_ERROR_SEEKERROR; |
| 1645 | | if (sector_index==0) |
| 1646 | | { |
| 1647 | | /* If desired, return the ID field */ |
| 1648 | | if (cylinder) |
| 1649 | | { |
| 1650 | | *cylinder = track_data[i]; |
| 1651 | | // printf("cylinder=%d", *cylinder); |
| 1652 | | |
| 1653 | | } |
| 1654 | | if (side) |
| 1655 | | { |
| 1656 | | *side = track_data[i+1]; |
| 1657 | | // printf(", side=%d", *side); |
| 1658 | | } |
| 1659 | | if (sector) |
| 1660 | | { |
| 1661 | | *sector = track_data[i+2]; |
| 1662 | | // printf(", sector=%d", *sector); |
| 1663 | | } |
| 1664 | | if (sector_length) |
| 1665 | | { |
| 1666 | | *sector_length = 128 << track_data[i+3]; |
| 1667 | | // printf(", sector_length=%d\n", *sector_length); |
| 1668 | | } |
| 1669 | | if (flags) |
| 1670 | | { |
| 1671 | | *flags = 0; |
| 1672 | | } |
| 1673 | | retval = FLOPPY_ERROR_SUCCESS; |
| 1674 | | |
| 1675 | | /* If desired, return the sector contents. */ |
| 1676 | | if (buffer) |
| 1677 | | { |
| 1678 | | while (i++ < tag->track_size) |
| 1679 | | { |
| 1680 | | byte = read_byte(tag->format, tag->first_idam, track_data, i); |
| 1681 | | if (byte == TI99_DAM) |
| 1682 | | break; |
| 1683 | | } |
| 1684 | | if (byte == TI99_DAM) |
| 1685 | | { |
| 1686 | | memcpy(buffer, &track_data[i+1], SECTOR_SIZE); |
| 1687 | | } |
| 1688 | | else |
| 1689 | | { |
| 1690 | | retval = FLOPPY_ERROR_SEEKERROR; |
| 1691 | | } |
| 1692 | | } |
| 1693 | | } |
| 1694 | | free(track_data); |
| 1695 | | return retval; |
| 1696 | | } |
| 1697 | | |
| 1698 | | /* |
| 1699 | | Required for ReadAddress command. This function gets the 1st, 2nd, 3rd, |
| 1700 | | sector (and so on) from the track, not sector 1, 2, 3. That is, the |
| 1701 | | returned sector info depends on the interleave and the start sector. |
| 1702 | | */ |
| 1703 | | static floperr_t ti99_tdf_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 1704 | | { |
| 1705 | | return ti99_tdf_find_indexed_sector(floppy, head, track, sector_index, cylinder, side, sector, sector_length, NULL, flags); |
| 1706 | | } |
| 1707 | | |
| 1708 | | static floperr_t ti99_tdf_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 1709 | | { |
| 1710 | | return ti99_tdf_find_indexed_sector(floppy, head, track, sector, (int*)NULL, (int*)NULL, (int*)NULL, (UINT32*)NULL, buffer, (unsigned long*)NULL); |
| 1711 | | } |
| 1712 | | |
| 1713 | | |
| 1714 | | /* |
| 1715 | | Guess the geometry of the disk. |
| 1716 | | 1. Check whether the disk is formatted, and if so, whether it is |
| 1717 | | a FM or a MFM disk |
| 1718 | | 2. If this fails, guess from the file size which format it should have |
| 1719 | | |
| 1720 | | SDF formats have sizes which are multiples of 10KiB. |
| 1721 | | TDF: full sector size FM (MFM) = 334 (340) bytes |
| 1722 | | additional track data FM (MFM) = 247 (752) bytes |
| 1723 | | |
| 1724 | | => check whether size is dividable by 40 (or 35) and by 2 |
| 1725 | | => get track size |
| 1726 | | => subtract track data |
| 1727 | | => divide by 334 or 340 |
| 1728 | | => number of sectors should be 8, 9, 16, 18, 32, or 36 |
| 1729 | | |
| 1730 | | SSDD is 274880, but DSSD is 260240, so they differ (unlike in the SDF format) |
| 1731 | | Still, we cannot tell apart single-sided 80 track formats from double-sided |
| 1732 | | 40 tracks. We assume the latter. Moreover, we only attempt to detect |
| 1733 | | 9/18/36 sectors (no 8/16) and 40 tracks (no 35). |
| 1734 | | |
| 1735 | | SSSD40 = 130120 |
| 1736 | | DSSD = 260240 |
| 1737 | | SSDD = 274880 |
| 1738 | | DSDD = 549760 |
| 1739 | | DSHD = 1039360 |
| 1740 | | DSDD80 = 1099520 |
| 1741 | | DSHD80 = 2078720 |
| 1742 | | */ |
| 1743 | | static int ti99_tdf_guess_geometry(floppy_image *floppy, UINT64 size, |
| 1744 | | struct ti99dsk_geometry *geometry) |
| 1745 | | { |
| 1746 | | int idamcnt, state, track, head, i, totalseclen = 0, format = 0, byte, tracklength, trackadd = 0; |
| 1747 | | int first_idam = 0; |
| 1748 | | UINT8 *track_data; |
| 1749 | | /*int offset;*/ |
| 1750 | | |
| 1751 | | struct ti99dsk_geometry dummy_geometry; |
| 1752 | | |
| 1753 | | if (geometry) |
| 1754 | | memset(geometry, 0, sizeof(*geometry)); |
| 1755 | | else |
| 1756 | | geometry = &dummy_geometry; |
| 1757 | | |
| 1758 | | /* Allocate enough bytes to hold the longest supported track. */ |
| 1759 | | track_data = (UINT8*)malloc(13000); |
| 1760 | | floppy_image_read(floppy, track_data, 0, 13000); |
| 1761 | | |
| 1762 | | if (determine_offset(TI99_MFM, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1763 | | { |
| 1764 | | /* MFM failed. */ |
| 1765 | | if (determine_offset(TI99_FM, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1766 | | { |
| 1767 | | logerror("IDAM not found. Unformatted disk.\n"); |
| 1768 | | |
| 1769 | | // FM failed as well. Disk is not formatted. We assume |
| 1770 | | // a format that fits into the space provided by the file. |
| 1771 | | // We only give a moderate vote in this case, so SDF |
| 1772 | | // make take it. |
| 1773 | | |
| 1774 | | free(track_data); |
| 1775 | | if (size < 130120 || size > 2078720) |
| 1776 | | { |
| 1777 | | logerror("Unknown format size: %d\n", (int)size); |
| 1778 | | return 0; |
| 1779 | | } |
| 1780 | | if (size >= 130120 && size < 260240) |
| 1781 | | { |
| 1782 | | /* SSSD */ |
| 1783 | | geometry->sides = 1; |
| 1784 | | geometry->tracksperside = 40; |
| 1785 | | geometry->secspertrack = 9; |
| 1786 | | geometry->density = 1; |
| 1787 | | return 50; |
| 1788 | | } |
| 1789 | | if (size >= 260240 && size < 274880) |
| 1790 | | { |
| 1791 | | /* DSSD */ |
| 1792 | | geometry->sides = 2; |
| 1793 | | geometry->tracksperside = 40; |
| 1794 | | geometry->secspertrack = 9; |
| 1795 | | geometry->density = 1; |
| 1796 | | return 50; |
| 1797 | | } |
| 1798 | | if (size >= 274880 && size < 549760) |
| 1799 | | { |
| 1800 | | /* SSDD */ |
| 1801 | | geometry->sides = 1; |
| 1802 | | geometry->tracksperside = 40; |
| 1803 | | geometry->secspertrack = 18; |
| 1804 | | geometry->density = 2; |
| 1805 | | return 50; |
| 1806 | | } |
| 1807 | | if (size >= 549760 && size < 1039360) |
| 1808 | | { |
| 1809 | | /* DSDD */ |
| 1810 | | geometry->sides = 2; |
| 1811 | | geometry->tracksperside = 40; |
| 1812 | | geometry->secspertrack = 18; |
| 1813 | | geometry->density = 2; |
| 1814 | | return 50; |
| 1815 | | } |
| 1816 | | if (size >= 1039360 && size < 1099520) |
| 1817 | | { |
| 1818 | | /* DSHD */ |
| 1819 | | geometry->sides = 2; |
| 1820 | | geometry->tracksperside = 40; |
| 1821 | | geometry->secspertrack = 36; |
| 1822 | | geometry->density = 2; |
| 1823 | | return 50; |
| 1824 | | } |
| 1825 | | if (size >= 1099520 && size < 2078720) |
| 1826 | | { |
| 1827 | | /* DSDD80 */ |
| 1828 | | geometry->sides = 2; |
| 1829 | | geometry->tracksperside = 80; |
| 1830 | | geometry->secspertrack = 18; |
| 1831 | | geometry->density = 2; |
| 1832 | | return 50; |
| 1833 | | } |
| 1834 | | if (size == 2078720) |
| 1835 | | { |
| 1836 | | /* DSHD80 */ |
| 1837 | | geometry->sides = 2; |
| 1838 | | geometry->tracksperside = 80; |
| 1839 | | geometry->secspertrack = 36; |
| 1840 | | geometry->density = 2; |
| 1841 | | return 50; |
| 1842 | | } |
| 1843 | | } |
| 1844 | | else |
| 1845 | | { |
| 1846 | | /* FM format */ |
| 1847 | | totalseclen = TI99_FM_TOTAL_SECTOR; |
| 1848 | | trackadd = TI99_FM_TRACK_ADD; /* additional track data (gaps etc.) */ |
| 1849 | | geometry->density = 1; |
| 1850 | | format = TI99_FM; |
| 1851 | | } |
| 1852 | | } |
| 1853 | | else |
| 1854 | | { |
| 1855 | | /* MFM format */ |
| 1856 | | totalseclen = TI99_MFM_TOTAL_SECTOR; |
| 1857 | | trackadd = TI99_MFM_TRACK_ADD; |
| 1858 | | geometry->density = 2; |
| 1859 | | format = TI99_MFM; |
| 1860 | | } |
| 1861 | | |
| 1862 | | /* Count the number of IDAMs on track 0, head 0 */ |
| 1863 | | idamcnt = 0; |
| 1864 | | state = 0; |
| 1865 | | track = 0; |
| 1866 | | head = 0; |
| 1867 | | i=0; |
| 1868 | | |
| 1869 | | while (i++ < 13000 && head == 0 && track == 0) |
| 1870 | | { |
| 1871 | | byte = read_byte(format, first_idam, track_data, i); |
| 1872 | | switch (state) |
| 1873 | | { |
| 1874 | | case 0: |
| 1875 | | if (byte==TI99_IDAM) |
| 1876 | | state = 1; |
| 1877 | | break; |
| 1878 | | case 1: |
| 1879 | | track = byte; |
| 1880 | | state = 2; |
| 1881 | | break; |
| 1882 | | case 2: |
| 1883 | | head = byte; |
| 1884 | | state = 3; |
| 1885 | | break; |
| 1886 | | case 3: |
| 1887 | | idamcnt++; |
| 1888 | | state = 0; |
| 1889 | | } |
| 1890 | | } |
| 1891 | | logerror("Determined %d sectors\n", idamcnt); |
| 1892 | | |
| 1893 | | // Now calculate the geometry |
| 1894 | | // The track size, in theory, may change due to reformatting, |
| 1895 | | // and it may also vary between tracks, but the TDF does not support |
| 1896 | | // varying track lengths. So every track has the same length as track 0. |
| 1897 | | // Also, we don't yet know whether the drive and the disk have the |
| 1898 | | // same track count. For 80 track drives and 40 track disks, we need |
| 1899 | | // double-stepping; this will be known as soon as the controller is |
| 1900 | | // initialized; we need to check that in the access methods. |
| 1901 | | |
| 1902 | | tracklength = idamcnt * totalseclen + trackadd; |
| 1903 | | /*offset = 0;*/ |
| 1904 | | |
| 1905 | | // Read the last track. We assume that all tracks are properly |
| 1906 | | // formatted; otherwise, the above calculations would already fail. |
| 1907 | | // That is, the last track in the image tells us whether it is for |
| 1908 | | // head 0 or for head 1. |
| 1909 | | |
| 1910 | | geometry->sides = 1; |
| 1911 | | floppy_image_read(floppy, track_data, size-tracklength, tracklength); |
| 1912 | | if (determine_offset(format, track_data, &first_idam)==FLOPPY_ERROR_SEEKERROR) |
| 1913 | | { |
| 1914 | | /* error ... what now? */ |
| 1915 | | logerror("Error when reading last track. Image broken.\n"); |
| 1916 | | free(track_data); |
| 1917 | | return 50; |
| 1918 | | } |
| 1919 | | else |
| 1920 | | { |
| 1921 | | if (track_data[first_idam+2]==0) |
| 1922 | | { |
| 1923 | | geometry->sides = 1; |
| 1924 | | } |
| 1925 | | else |
| 1926 | | { |
| 1927 | | if (track_data[first_idam+2]==1) |
| 1928 | | { |
| 1929 | | geometry->sides = 2; |
| 1930 | | } |
| 1931 | | else |
| 1932 | | { |
| 1933 | | logerror("Error: Last track has invalid first IDAM: head = %d.\n", track_data[first_idam+2]); |
| 1934 | | } |
| 1935 | | |
| 1936 | | } |
| 1937 | | } |
| 1938 | | |
| 1939 | | |
| 1940 | | geometry->tracksperside = (size / tracklength) / geometry->sides; |
| 1941 | | geometry->secspertrack = idamcnt; |
| 1942 | | |
| 1943 | | if (geometry->tracksperside < 35 || geometry->tracksperside > 80) |
| 1944 | | { |
| 1945 | | logerror("Unsupported track count: %d\n", geometry->tracksperside); |
| 1946 | | free(track_data); |
| 1947 | | return 0; |
| 1948 | | } |
| 1949 | | |
| 1950 | | free(track_data); |
| 1951 | | return 100; |
| 1952 | | } |
| 1953 | | |
| 1954 | | static FLOPPY_IDENTIFY(ti99_tdf_identify) |
| 1955 | | { |
| 1956 | | UINT64 size; |
| 1957 | | size = floppy_image_size(floppy); |
| 1958 | | *vote = ti99_tdf_guess_geometry(floppy, size, NULL); |
| 1959 | | logerror("TDF voting %d\n", *vote); |
| 1960 | | return FLOPPY_ERROR_SUCCESS; |
| 1961 | | } |
| 1962 | | |
| 1963 | | static FLOPPY_CONSTRUCT(ti99_tdf_construct) |
| 1964 | | { |
| 1965 | | struct ti99dsk_geometry geometry; |
| 1966 | | struct FloppyCallbacks *callbacks; |
| 1967 | | struct ti99dsk_tag *tag; |
| 1968 | | logerror("Reading image as TDF\n"); |
| 1969 | | if (params) |
| 1970 | | { |
| 1971 | | /* create */ |
| 1972 | | memset(&geometry, 0, sizeof(geometry)); |
| 1973 | | /* |
| 1974 | | geometry.sides = option_resolution_lookup_int(params, PARAM_HEADS); |
| 1975 | | geometry.tracksperside = option_resolution_lookup_int(params, PARAM_TRACKS); |
| 1976 | | geometry.secspertrack = option_resolution_lookup_int(params, PARAM_SECTORS); |
| 1977 | | */ |
| 1978 | | } |
| 1979 | | else |
| 1980 | | { |
| 1981 | | /* load */ |
| 1982 | | if (ti99_tdf_guess_geometry(floppy, floppy_image_size(floppy), &geometry)<50) |
| 1983 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 1984 | | } |
| 1985 | | |
| 1986 | | assert(geometry.sides); |
| 1987 | | assert(geometry.tracksperside); |
| 1988 | | assert(geometry.secspertrack); |
| 1989 | | |
| 1990 | | tag = (struct ti99dsk_tag *) floppy_create_tag(floppy, sizeof(struct ti99dsk_tag)); |
| 1991 | | |
| 1992 | | tag->heads = geometry.sides; |
| 1993 | | tag->tracks = geometry.tracksperside; |
| 1994 | | |
| 1995 | | if (geometry.density==TI99_FM) |
| 1996 | | tag->track_size = geometry.secspertrack * TI99_FM_TOTAL_SECTOR + TI99_FM_TRACK_ADD; |
| 1997 | | else |
| 1998 | | tag->track_size = geometry.secspertrack * TI99_MFM_TOTAL_SECTOR + TI99_MFM_TRACK_ADD; |
| 1999 | | |
| 2000 | | tag->format = geometry.density; |
| 2001 | | tag->first_idam = 0; /* let's find out later */ |
| 2002 | | tag->dam_offset = 0; |
| 2003 | | |
| 2004 | | /* set up format callbacks */ |
| 2005 | | callbacks = floppy_callbacks(floppy); |
| 2006 | | callbacks->read_sector = ti99_tdf_read_sector; |
| 2007 | | callbacks->write_sector = ti99_tdf_write_sector; |
| 2008 | | callbacks->read_indexed_sector = ti99_tdf_read_indexed_sector; |
| 2009 | | callbacks->write_indexed_sector = ti99_tdf_write_indexed_sector; |
| 2010 | | callbacks->read_track = ti99_tdf_read_track; |
| 2011 | | callbacks->write_track = ti99_tdf_write_track; |
| 2012 | | /* callbacks->format_track = ti99_tdf_format_track; */ |
| 2013 | | callbacks->get_track_size = ti99_get_track_size; |
| 2014 | | /* post format unset */ |
| 2015 | | callbacks->get_heads_per_disk = ti99_get_heads_per_disk; |
| 2016 | | callbacks->get_tracks_per_disk = ti99_get_tracks_per_disk; |
| 2017 | | callbacks->get_sector_length = ti99_get_sector_length; |
| 2018 | | callbacks->get_indexed_sector_info = ti99_tdf_get_indexed_sector_info; |
| 2019 | | |
| 2020 | | return FLOPPY_ERROR_SUCCESS; |
| 2021 | | } |
| 2022 | | |
| 2023 | | /* ----------------------------------------------------------------------- */ |
| 2024 | | |
| 2025 | | FLOPPY_OPTIONS_START( ti99 ) |
| 2026 | | FLOPPY_OPTION( ti99_sdf, "dsk", "TI99 sector dump (v9t9)", ti99_sdf_identify, ti99_sdf_construct, NULL, |
| 2027 | | HEADS([1]-2) |
| 2028 | | TRACKS(35-[40]-80) |
| 2029 | | SECTORS(8/9/16/[18]/36) |
| 2030 | | SECTOR_LENGTH([256]) |
| 2031 | | FIRST_SECTOR_ID(0)) |
| 2032 | | FLOPPY_OPTION( ti99_tdf, "dsk,dtk", "TI99 track dump (pc99)", ti99_tdf_identify, ti99_tdf_construct, NULL, |
| 2033 | | TRACKS(35-[40]-80) |
| 2034 | | SECTORS(8/9/16/[18]/36) |
| 2035 | | SECTOR_LENGTH([256]) |
| 2036 | | FIRST_SECTOR_ID(0)) |
| 2037 | | FLOPPY_OPTIONS_END |
src/mess/formats/ap2_dsk.c
| r11457 | r11458 | |
| 1 | | /********************************************************************* |
| 2 | | |
| 3 | | ap2_dsk.c |
| 4 | | |
| 5 | | Apple II disk images |
| 6 | | |
| 7 | | *********************************************************************/ |
| 8 | | |
| 9 | | #include <stdlib.h> |
| 10 | | #include <string.h> |
| 11 | | #include <assert.h> |
| 12 | | |
| 13 | | #include "ap2_dsk.h" |
| 14 | | #include "formats/basicdsk.h" |
| 15 | | |
| 16 | | |
| 17 | | #define APPLE2_IMAGE_DO 0 |
| 18 | | #define APPLE2_IMAGE_PO 1 |
| 19 | | #define APPLE2_IMAGE_NIB 2 |
| 20 | | |
| 21 | | |
| 22 | | /* used in for all Apple II images */ |
| 23 | | static UINT32 apple2_get_track_size(floppy_image *floppy, int head, int track); |
| 24 | | static int disk_decode_nib(UINT8 *data, const UINT8 *nibble, int *volume, int *track, int *sector); |
| 25 | | static void disk_encode_nib(UINT8 *nibble, const UINT8 *data, int volume, int track, int sector); |
| 26 | | |
| 27 | | /* used in DOS/ProDOS order images */ |
| 28 | | static int apple2_do_translate_sector(floppy_image *floppy, int sector); |
| 29 | | static int apple2_po_translate_sector(floppy_image *floppy, int sector); |
| 30 | | static floperr_t apple2_dsk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen); |
| 31 | | static floperr_t apple2_dsk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen); |
| 32 | | |
| 33 | | /* used in nibble order images */ |
| 34 | | static floperr_t apple2_nib_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen); |
| 35 | | static floperr_t apple2_nib_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen); |
| 36 | | static floperr_t apple2_nib_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen); |
| 37 | | static floperr_t apple2_nib_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam); |
| 38 | | static floperr_t apple2_nib_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length); |
| 39 | | |
| 40 | | |
| 41 | | static const UINT8 translate6[0x40] = |
| 42 | | { |
| 43 | | 0x96, 0x97, 0x9a, 0x9b, 0x9d, 0x9e, 0x9f, 0xa6, |
| 44 | | 0xa7, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb2, 0xb3, |
| 45 | | 0xb4, 0xb5, 0xb6, 0xb7, 0xb9, 0xba, 0xbb, 0xbc, |
| 46 | | 0xbd, 0xbe, 0xbf, 0xcb, 0xcd, 0xce, 0xcf, 0xd3, |
| 47 | | 0xd6, 0xd7, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, |
| 48 | | 0xdf, 0xe5, 0xe6, 0xe7, 0xe9, 0xea, 0xeb, 0xec, |
| 49 | | 0xed, 0xee, 0xef, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, |
| 50 | | 0xf7, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff |
| 51 | | }; |
| 52 | | |
| 53 | | |
| 54 | | |
| 55 | | /* ----------------------------------------------------------------------- |
| 56 | | * Utility code |
| 57 | | * ----------------------------------------------------------------------- */ |
| 58 | | |
| 59 | | static const UINT8 *get_untranslate6_map(void) |
| 60 | | { |
| 61 | | static UINT8 map[256]; |
| 62 | | static int map_inited = 0; |
| 63 | | UINT8 i; |
| 64 | | |
| 65 | | if (!map_inited) |
| 66 | | { |
| 67 | | memset(map, 0xff, sizeof(map)); |
| 68 | | for (i = 0; i < ARRAY_LENGTH(translate6); i++) |
| 69 | | map[translate6[i]] = i; |
| 70 | | map_inited = 1; |
| 71 | | } |
| 72 | | return map; |
| 73 | | } |
| 74 | | |
| 75 | | |
| 76 | | |
| 77 | | /* ----------------------------------------------------------------------- |
| 78 | | * Core constructor |
| 79 | | * ----------------------------------------------------------------------- */ |
| 80 | | |
| 81 | | |
| 82 | | static floperr_t apple2_general_construct(floppy_image *floppy, int floppy_type) |
| 83 | | { |
| 84 | | floperr_t err; |
| 85 | | struct basicdsk_geometry geometry; |
| 86 | | struct FloppyCallbacks *format; |
| 87 | | |
| 88 | | format = floppy_callbacks(floppy); |
| 89 | | |
| 90 | | switch(floppy_type) { |
| 91 | | case APPLE2_IMAGE_DO: |
| 92 | | case APPLE2_IMAGE_PO: |
| 93 | | memset(&geometry, 0, sizeof(geometry)); |
| 94 | | geometry.heads = 1; |
| 95 | | geometry.tracks = APPLE2_TRACK_COUNT; |
| 96 | | geometry.sectors = APPLE2_SECTOR_COUNT; |
| 97 | | geometry.sector_length = APPLE2_SECTOR_SIZE; |
| 98 | | geometry.translate_sector = (floppy_type == APPLE2_IMAGE_DO) ? apple2_do_translate_sector : apple2_po_translate_sector; |
| 99 | | |
| 100 | | err = basicdsk_construct(floppy, &geometry); |
| 101 | | if (err) |
| 102 | | return err; |
| 103 | | |
| 104 | | format->read_track = apple2_dsk_read_track; |
| 105 | | format->write_track = apple2_dsk_write_track; |
| 106 | | break; |
| 107 | | |
| 108 | | case APPLE2_IMAGE_NIB: |
| 109 | | format->read_track = apple2_nib_read_track; |
| 110 | | format->write_track = apple2_nib_write_track; |
| 111 | | format->read_sector = apple2_nib_read_sector; |
| 112 | | format->write_sector = apple2_nib_write_sector; |
| 113 | | format->get_sector_length = apple2_nib_get_sector_length; |
| 114 | | break; |
| 115 | | |
| 116 | | default: |
| 117 | | assert(0); |
| 118 | | return FLOPPY_ERROR_INTERNAL; |
| 119 | | } |
| 120 | | |
| 121 | | format->get_track_size = apple2_get_track_size; |
| 122 | | |
| 123 | | return FLOPPY_ERROR_SUCCESS; |
| 124 | | } |
| 125 | | |
| 126 | | |
| 127 | | |
| 128 | | /* ----------------------------------------------------------------------- |
| 129 | | * DOS order and ProDOS order code |
| 130 | | * ----------------------------------------------------------------------- */ |
| 131 | | |
| 132 | | static FLOPPY_IDENTIFY(apple2_dsk_identify) |
| 133 | | { |
| 134 | | UINT64 size; |
| 135 | | UINT64 expected_size; |
| 136 | | |
| 137 | | size = floppy_image_size(floppy); |
| 138 | | expected_size = APPLE2_TRACK_COUNT * APPLE2_SECTOR_COUNT * APPLE2_SECTOR_SIZE; |
| 139 | | |
| 140 | | if (size == expected_size) |
| 141 | | *vote = 100; |
| 142 | | else if (size - expected_size < 8 && size - expected_size > -8) |
| 143 | | *vote = 90; /* tolerate images with up to eight fewer/extra bytes (bug #638) */ |
| 144 | | else |
| 145 | | *vote = 0; |
| 146 | | return FLOPPY_ERROR_SUCCESS; |
| 147 | | } |
| 148 | | |
| 149 | | |
| 150 | | |
| 151 | | static int apple2_do_translate_sector(floppy_image *floppy, int sector) |
| 152 | | { |
| 153 | | static const UINT8 skewing[] = |
| 154 | | { |
| 155 | | /* DOS order (*.do) */ |
| 156 | | 0x00, 0x07, 0x0E, 0x06, 0x0D, 0x05, 0x0C, 0x04, |
| 157 | | 0x0B, 0x03, 0x0A, 0x02, 0x09, 0x01, 0x08, 0x0F |
| 158 | | }; |
| 159 | | return skewing[sector]; |
| 160 | | } |
| 161 | | |
| 162 | | |
| 163 | | |
| 164 | | static int apple2_po_translate_sector(floppy_image *floppy, int sector) |
| 165 | | { |
| 166 | | static const UINT8 skewing[] = |
| 167 | | { |
| 168 | | /* ProDOS order (*.po) */ |
| 169 | | 0x00, 0x08, 0x01, 0x09, 0x02, 0x0A, 0x03, 0x0B, |
| 170 | | 0x04, 0x0C, 0x05, 0x0D, 0x06, 0x0E, 0x07, 0x0F |
| 171 | | }; |
| 172 | | return skewing[sector]; |
| 173 | | } |
| 174 | | |
| 175 | | |
| 176 | | |
| 177 | | static FLOPPY_CONSTRUCT(apple2_do_construct) |
| 178 | | { |
| 179 | | return apple2_general_construct(floppy, APPLE2_IMAGE_DO); |
| 180 | | } |
| 181 | | |
| 182 | | |
| 183 | | |
| 184 | | static FLOPPY_CONSTRUCT(apple2_po_construct) |
| 185 | | { |
| 186 | | return apple2_general_construct(floppy, APPLE2_IMAGE_PO); |
| 187 | | } |
| 188 | | |
| 189 | | |
| 190 | | |
| 191 | | static floperr_t apple2_dsk_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 192 | | { |
| 193 | | UINT8 sector_buffer[APPLE2_SECTOR_SIZE]; |
| 194 | | int sector; |
| 195 | | UINT8 *nibble; |
| 196 | | |
| 197 | | if (buflen < APPLE2_NIBBLE_SIZE*APPLE2_SECTOR_COUNT) |
| 198 | | return FLOPPY_ERROR_INTERNAL; |
| 199 | | if (offset != 0) |
| 200 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 201 | | |
| 202 | | memset(buffer, 0, buflen); |
| 203 | | |
| 204 | | for (sector = 0; sector < APPLE2_SECTOR_COUNT; sector++) |
| 205 | | { |
| 206 | | nibble = (UINT8 *)buffer; |
| 207 | | nibble += sector * APPLE2_SMALL_NIBBLE_SIZE; |
| 208 | | |
| 209 | | floppy_read_sector(floppy, head, track, sector, 0, sector_buffer, sizeof(sector_buffer)); |
| 210 | | disk_encode_nib(nibble, sector_buffer, 254, track, sector); |
| 211 | | } |
| 212 | | return FLOPPY_ERROR_SUCCESS; |
| 213 | | } |
| 214 | | |
| 215 | | |
| 216 | | |
| 217 | | static floperr_t apple2_dsk_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 218 | | { |
| 219 | | int sector; |
| 220 | | UINT8 sector_buffer[APPLE2_SECTOR_SIZE]; |
| 221 | | const UINT8 *nibble; |
| 222 | | |
| 223 | | if (offset != 0) |
| 224 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 225 | | |
| 226 | | for (sector = 0; sector < APPLE2_SECTOR_COUNT; sector++) |
| 227 | | { |
| 228 | | nibble = (UINT8 *)buffer; |
| 229 | | nibble += sector * APPLE2_SMALL_NIBBLE_SIZE; |
| 230 | | |
| 231 | | disk_decode_nib(sector_buffer, nibble, NULL, NULL, NULL); |
| 232 | | floppy_write_sector(floppy, head, track, sector, 0, sector_buffer, sizeof(sector_buffer), 0); |
| 233 | | } |
| 234 | | |
| 235 | | return FLOPPY_ERROR_SUCCESS; |
| 236 | | } |
| 237 | | |
| 238 | | |
| 239 | | |
| 240 | | /* ----------------------------------------------------------------------- |
| 241 | | * Nibble order code |
| 242 | | * ----------------------------------------------------------------------- */ |
| 243 | | |
| 244 | | static FLOPPY_IDENTIFY(apple2_nib_identify) |
| 245 | | { |
| 246 | | UINT64 size; |
| 247 | | size = floppy_image_size(floppy); |
| 248 | | *vote = (size == APPLE2_TRACK_COUNT * APPLE2_SECTOR_COUNT * APPLE2_NIBBLE_SIZE) ? 100 : 0; |
| 249 | | return FLOPPY_ERROR_SUCCESS; |
| 250 | | } |
| 251 | | |
| 252 | | |
| 253 | | |
| 254 | | static FLOPPY_CONSTRUCT(apple2_nib_construct) |
| 255 | | { |
| 256 | | return apple2_general_construct(floppy, APPLE2_IMAGE_NIB); |
| 257 | | } |
| 258 | | |
| 259 | | |
| 260 | | |
| 261 | | static floperr_t apple2_nib_read_track(floppy_image *floppy, int head, int track, UINT64 offset, void *buffer, size_t buflen) |
| 262 | | { |
| 263 | | if ((head != 0) || (track < 0) || (track >= APPLE2_TRACK_COUNT)) |
| 264 | | return FLOPPY_ERROR_SEEKERROR; |
| 265 | | if (offset != 0) |
| 266 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 267 | | floppy_image_read(floppy, buffer, track * APPLE2_NIBBLE_SIZE * APPLE2_SECTOR_COUNT, buflen); |
| 268 | | return FLOPPY_ERROR_SUCCESS; |
| 269 | | } |
| 270 | | |
| 271 | | |
| 272 | | |
| 273 | | static floperr_t apple2_nib_write_track(floppy_image *floppy, int head, int track, UINT64 offset, const void *buffer, size_t buflen) |
| 274 | | { |
| 275 | | if ((head != 0) || (track < 0) || (track >= APPLE2_TRACK_COUNT)) |
| 276 | | return FLOPPY_ERROR_SEEKERROR; |
| 277 | | if (offset != 0) |
| 278 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 279 | | floppy_image_write(floppy, buffer, track * APPLE2_NIBBLE_SIZE * APPLE2_SECTOR_COUNT, buflen); |
| 280 | | return FLOPPY_ERROR_SUCCESS; |
| 281 | | } |
| 282 | | |
| 283 | | |
| 284 | | |
| 285 | | /* ----------------------------------------------------------------------- |
| 286 | | * Track conversion |
| 287 | | * ----------------------------------------------------------------------- */ |
| 288 | | |
| 289 | | static int decode_nibbyte(UINT8 *nibint, const UINT8 *nibdata) |
| 290 | | { |
| 291 | | if ((nibdata[0] & 0xAA) != 0xAA) |
| 292 | | return 1; |
| 293 | | if ((nibdata[1] & 0xAA) != 0xAA) |
| 294 | | return 1; |
| 295 | | |
| 296 | | *nibint = (nibdata[0] & ~0xAA) << 1; |
| 297 | | *nibint |= (nibdata[1] & ~0xAA) << 0; |
| 298 | | return 0; |
| 299 | | } |
| 300 | | |
| 301 | | |
| 302 | | |
| 303 | | static int disk_decode_nib(UINT8 *data, const UINT8 *nibble, int *volume, int *track, int *sector) |
| 304 | | { |
| 305 | | UINT8 read_volume; |
| 306 | | UINT8 read_track; |
| 307 | | UINT8 read_sector; |
| 308 | | UINT8 read_checksum; |
| 309 | | int i; |
| 310 | | UINT8 b, xorvalue, newvalue; |
| 311 | | |
| 312 | | const UINT8 *untranslate6 = get_untranslate6_map(); |
| 313 | | |
| 314 | | /* pick apart the volume/track/sector info and checksum */ |
| 315 | | if (decode_nibbyte(&read_volume, &nibble[10])) |
| 316 | | return 1; |
| 317 | | if (decode_nibbyte(&read_track, &nibble[12])) |
| 318 | | return 1; |
| 319 | | if (decode_nibbyte(&read_sector, &nibble[14])) |
| 320 | | return 1; |
| 321 | | if (decode_nibbyte(&read_checksum, &nibble[16])) |
| 322 | | return 1; |
| 323 | | if (read_checksum != (read_volume ^ read_track ^ read_sector)) |
| 324 | | return 1; |
| 325 | | |
| 326 | | /* decode the nibble core */ |
| 327 | | xorvalue = 0; |
| 328 | | for (i = 0; i < 342; i++) |
| 329 | | { |
| 330 | | b = untranslate6[nibble[i+28]]; |
| 331 | | if (b == 0xff) |
| 332 | | return 1; |
| 333 | | newvalue = b ^ xorvalue; |
| 334 | | |
| 335 | | if (i >= 0x56) |
| 336 | | { |
| 337 | | /* 6 bit */ |
| 338 | | data[i - 0x56] |= (newvalue << 2); |
| 339 | | } |
| 340 | | else |
| 341 | | { |
| 342 | | /* 3 * 2 bit */ |
| 343 | | data[i + 0x00] = ((newvalue >> 1) & 0x01) | ((newvalue << 1) & 0x02); |
| 344 | | data[i + 0x56] = ((newvalue >> 3) & 0x01) | ((newvalue >> 1) & 0x02); |
| 345 | | if (i + 0xAC < APPLE2_SECTOR_SIZE) |
| 346 | | data[i + 0xAC] = ((newvalue >> 5) & 0x01) | ((newvalue >> 3) & 0x02); |
| 347 | | } |
| 348 | | xorvalue = newvalue; |
| 349 | | } |
| 350 | | |
| 351 | | /* success; write out values if pointers not null */ |
| 352 | | if (volume) |
| 353 | | *volume = read_volume; |
| 354 | | if (track) |
| 355 | | *track = read_track; |
| 356 | | if (sector) |
| 357 | | *sector = read_sector; |
| 358 | | return 0; |
| 359 | | } |
| 360 | | |
| 361 | | |
| 362 | | |
| 363 | | static floperr_t apple2_nib_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 364 | | { |
| 365 | | floperr_t err; |
| 366 | | const UINT8 *nibble; |
| 367 | | UINT8 *track_data; |
| 368 | | void *track_data_v; |
| 369 | | |
| 370 | | if ((sector < 0) || (sector >= APPLE2_SECTOR_COUNT)) |
| 371 | | return FLOPPY_ERROR_SEEKERROR; |
| 372 | | if (buflen != APPLE2_SECTOR_SIZE) |
| 373 | | return FLOPPY_ERROR_INTERNAL; |
| 374 | | |
| 375 | | err = floppy_load_track(floppy, head, track, FALSE, &track_data_v, NULL); |
| 376 | | if (err) |
| 377 | | return err; |
| 378 | | track_data = (UINT8 *) track_data_v; |
| 379 | | |
| 380 | | nibble = track_data + (sector * APPLE2_NIBBLE_SIZE); |
| 381 | | |
| 382 | | if (disk_decode_nib((UINT8 *)buffer, nibble, NULL, NULL, NULL)) |
| 383 | | return FLOPPY_ERROR_INVALIDIMAGE; |
| 384 | | |
| 385 | | return FLOPPY_ERROR_SUCCESS; |
| 386 | | } |
| 387 | | |
| 388 | | |
| 389 | | |
| 390 | | static void disk_encode_nib(UINT8 *nibble, const UINT8 *data, int volume, int track, int sector) |
| 391 | | { |
| 392 | | int checksum, oldvalue, xorvalue, i; |
| 393 | | |
| 394 | | /* setup header values */ |
| 395 | | checksum = volume ^ track ^ sector; |
| 396 | | |
| 397 | | memset(nibble, 0xFF, APPLE2_NIBBLE_SIZE); |
| 398 | | nibble[ 7] = 0xD5; |
| 399 | | nibble[ 8] = 0xAA; |
| 400 | | nibble[ 9] = 0x96; |
| 401 | | nibble[10] = (volume >> 1) | 0xAA; |
| 402 | | nibble[11] = volume | 0xAA; |
| 403 | | nibble[12] = (track >> 1) | 0xAA; |
| 404 | | nibble[13] = track | 0xAA; |
| 405 | | nibble[14] = (sector >> 1) | 0xAA; |
| 406 | | nibble[15] = sector | 0xAA; |
| 407 | | nibble[16] = (checksum >> 1) | 0xAA; |
| 408 | | nibble[17] = (checksum) | 0xAA; |
| 409 | | nibble[18] = 0xDE; |
| 410 | | nibble[19] = 0xAA; |
| 411 | | nibble[20] = 0xEB; |
| 412 | | nibble[25] = 0xD5; |
| 413 | | nibble[26] = 0xAA; |
| 414 | | nibble[27] = 0xAD; |
| 415 | | nibble[27+344] = 0xDE; |
| 416 | | nibble[27+345] = 0xAA; |
| 417 | | nibble[27+346] = 0xEB; |
| 418 | | xorvalue = 0; |
| 419 | | |
| 420 | | for (i = 0; i < 342; i++) |
| 421 | | { |
| 422 | | if (i >= 0x56) |
| 423 | | { |
| 424 | | /* 6 bit */ |
| 425 | | oldvalue = data[i - 0x56]; |
| 426 | | oldvalue = oldvalue >> 2; |
| 427 | | } |
| 428 | | else |
| 429 | | { |
| 430 | | /* 3 * 2 bit */ |
| 431 | | oldvalue = 0; |
| 432 | | oldvalue |= (data[i + 0x00] & 0x01) << 1; |
| 433 | | oldvalue |= (data[i + 0x00] & 0x02) >> 1; |
| 434 | | oldvalue |= (data[i + 0x56] & 0x01) << 3; |
| 435 | | oldvalue |= (data[i + 0x56] & 0x02) << 1; |
| 436 | | if (i + 0xAC < APPLE2_SECTOR_SIZE) |
| 437 | | { |
| 438 | | oldvalue |= (data[i + 0xAC] & 0x01) << 5; |
| 439 | | oldvalue |= (data[i + 0xAC] & 0x02) << 3; |
| 440 | | } |
| 441 | | |
| 442 | | } |
| 443 | | xorvalue ^= oldvalue; |
| 444 | | nibble[28+i] = translate6[xorvalue & 0x3F]; |
| 445 | | xorvalue = oldvalue; |
| 446 | | } |
| 447 | | |
| 448 | | nibble[27+343] = translate6[xorvalue & 0x3F]; |
| 449 | | } |
| 450 | | |
| 451 | | |
| 452 | | |
| 453 | | static floperr_t apple2_nib_write_sector(floppy_image *floppy, int head, int track, int sector, const void *buffer, size_t buflen, int ddam) |
| 454 | | { |
| 455 | | floperr_t err; |
| 456 | | UINT8 *track_data; |
| 457 | | void *track_data_v; |
| 458 | | |
| 459 | | if ((sector < 0) || (sector >= APPLE2_SECTOR_COUNT)) |
| 460 | | return FLOPPY_ERROR_SEEKERROR; |
| 461 | | if (buflen != APPLE2_SECTOR_SIZE) |
| 462 | | return FLOPPY_ERROR_INTERNAL; |
| 463 | | |
| 464 | | err = floppy_load_track(floppy, head, track, TRUE, &track_data_v, NULL); |
| 465 | | if (err) |
| 466 | | return err; |
| 467 | | track_data = (UINT8 *) track_data_v; |
| 468 | | |
| 469 | | disk_encode_nib(track_data + sector * APPLE2_NIBBLE_SIZE, (const UINT8 *)buffer, 254, track, sector); |
| 470 | | return FLOPPY_ERROR_SUCCESS; |
| 471 | | } |
| 472 | | |
| 473 | | |
| 474 | | |
| 475 | | static floperr_t apple2_nib_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 476 | | { |
| 477 | | *sector_length = APPLE2_SECTOR_SIZE; |
| 478 | | return FLOPPY_ERROR_SUCCESS; |
| 479 | | } |
| 480 | | |
| 481 | | |
| 482 | | |
| 483 | | static UINT32 apple2_get_track_size(floppy_image *floppy, int head, int track) |
| 484 | | { |
| 485 | | return APPLE2_NIBBLE_SIZE * APPLE2_SECTOR_COUNT; |
| 486 | | } |
| 487 | | |
| 488 | | |
| 489 | | |
| 490 | | /* ----------------------------------------------------------------------- */ |
| 491 | | |
| 492 | | FLOPPY_OPTIONS_START( apple2 ) |
| 493 | | FLOPPY_OPTION( apple2_do, "do,dsk,bin", "Apple ][ DOS order disk image", apple2_dsk_identify, apple2_do_construct, NULL, |
| 494 | | HEADS([1]) |
| 495 | | TRACKS([35]) |
| 496 | | SECTORS([16]) |
| 497 | | SECTOR_LENGTH([256]) |
| 498 | | FIRST_SECTOR_ID([0])) |
| 499 | | FLOPPY_OPTION( apple2_po, "po,dsk,bin", "Apple ][ ProDOS order disk image", apple2_dsk_identify, apple2_po_construct, NULL, |
| 500 | | HEADS([1]) |
| 501 | | TRACKS([35]) |
| 502 | | SECTORS([16]) |
| 503 | | SECTOR_LENGTH([256]) |
| 504 | | FIRST_SECTOR_ID([0])) |
| 505 | | FLOPPY_OPTION( apple2_nib, "dsk,nib", "Apple ][ Nibble order disk image", apple2_nib_identify, apple2_nib_construct, NULL, |
| 506 | | HEADS([1]) |
| 507 | | TRACKS([35]) |
| 508 | | SECTORS([16]) |
| 509 | | SECTOR_LENGTH([256]) |
| 510 | | FIRST_SECTOR_ID([0])) |
| 511 | | FLOPPY_OPTIONS_END |
src/mess/formats/td0_dsk.c
| r11457 | r11458 | |
| 1 | | /********************************************************************* |
| 2 | | |
| 3 | | formats/td0_dsk.c |
| 4 | | |
| 5 | | TD0 disk images |
| 6 | | |
| 7 | | *********************************************************************/ |
| 8 | | /* |
| 9 | | * Based on Japanese version 29-NOV-1988 |
| 10 | | * LZSS coded by Haruhiko OKUMURA |
| 11 | | * Adaptive Huffman Coding coded by Haruyasu YOSHIZAKI |
| 12 | | * Edited and translated to English by Kenji RIKITAKE |
| 13 | | */ |
| 14 | | |
| 15 | | #include <string.h> |
| 16 | | #include "emu.h" |
| 17 | | #include "imagedev/flopimg.h" |
| 18 | | #include "imagedev/flopdrv.h" |
| 19 | | |
| 20 | | #define BUFSZ 512 // new input buffer |
| 21 | | |
| 22 | | /* LZSS Parameters */ |
| 23 | | |
| 24 | | #define N 4096 /* Size of string buffer */ |
| 25 | | #define F 60 /* Size of look-ahead buffer */ |
| 26 | | #define THRESHOLD 2 |
| 27 | | #define NIL N /* End of tree's node */ |
| 28 | | |
| 29 | | |
| 30 | | /* Huffman coding parameters */ |
| 31 | | |
| 32 | | #define N_CHAR (256 - THRESHOLD + F) |
| 33 | | /* character code (= 0..N_CHAR-1) */ |
| 34 | | #define T (N_CHAR * 2 - 1) /* Size of table */ |
| 35 | | #define R (T - 1) /* root position */ |
| 36 | | #define MAX_FREQ 0x8000 |
| 37 | | /* update when cumulative frequency */ |
| 38 | | /* reaches to this value */ |
| 39 | | |
| 40 | | struct td0dsk_tag |
| 41 | | { |
| 42 | | int heads; |
| 43 | | int tracks; |
| 44 | | int sector_size; |
| 45 | | UINT64 track_offsets[84*2]; /* offset within data for each track */ |
| 46 | | UINT8 *data; |
| 47 | | }; |
| 48 | | |
| 49 | | struct tdlzhuf { |
| 50 | | UINT16 r, |
| 51 | | bufcnt,bufndx,bufpos, // string buffer |
| 52 | | // the following to allow block reads from input in next_word() |
| 53 | | ibufcnt,ibufndx; // input buffer counters |
| 54 | | UINT8 inbuf[BUFSZ]; // input buffer |
| 55 | | }; |
| 56 | | |
| 57 | | |
| 58 | | struct td0dsk_t |
| 59 | | { |
| 60 | | floppy_image *floppy_file; |
| 61 | | UINT64 floppy_file_offset; |
| 62 | | |
| 63 | | struct tdlzhuf tdctl; |
| 64 | | UINT8 text_buf[N + F - 1]; |
| 65 | | UINT16 freq[T + 1]; /* cumulative freq table */ |
| 66 | | |
| 67 | | /* |
| 68 | | * pointing parent nodes. |
| 69 | | * area [T..(T + N_CHAR - 1)] are pointers for leaves |
| 70 | | */ |
| 71 | | INT16 prnt[T + N_CHAR]; |
| 72 | | |
| 73 | | /* pointing children nodes (son[], son[] + 1)*/ |
| 74 | | INT16 son[T]; |
| 75 | | |
| 76 | | UINT16 getbuf; |
| 77 | | UINT8 getlen; |
| 78 | | |
| 79 | | int data_read(UINT8 *buf, UINT16 size); |
| 80 | | int next_word(); |
| 81 | | int GetBit(); |
| 82 | | int GetByte(); |
| 83 | | void StartHuff(); |
| 84 | | void reconst(); |
| 85 | | void update(int c); |
| 86 | | INT16 DecodeChar(); |
| 87 | | INT16 DecodePosition(); |
| 88 | | void init_Decode(); |
| 89 | | int Decode(UINT8 *buf, int len); |
| 90 | | }; |
| 91 | | |
| 92 | | static td0dsk_t td0dsk; |
| 93 | | |
| 94 | | |
| 95 | | static struct td0dsk_tag *get_tag(floppy_image *floppy) |
| 96 | | { |
| 97 | | struct td0dsk_tag *tag; |
| 98 | | tag = (td0dsk_tag *)floppy_tag(floppy); |
| 99 | | return tag; |
| 100 | | } |
| 101 | | |
| 102 | | |
| 103 | | |
| 104 | | FLOPPY_IDENTIFY( td0_dsk_identify ) |
| 105 | | { |
| 106 | | UINT8 header[2]; |
| 107 | | |
| 108 | | floppy_image_read(floppy, header, 0, 2); |
| 109 | | if (header[0]=='T' && header[1]=='D') { |
| 110 | | *vote = 100; |
| 111 | | } else if (header[0]=='t' && header[1]=='d') { |
| 112 | | *vote = 100; |
| 113 | | } else { |
| 114 | | *vote = 0; |
| 115 | | } |
| 116 | | return FLOPPY_ERROR_SUCCESS; |
| 117 | | } |
| 118 | | |
| 119 | | static int td0_get_heads_per_disk(floppy_image *floppy) |
| 120 | | { |
| 121 | | return get_tag(floppy)->heads; |
| 122 | | } |
| 123 | | |
| 124 | | static int td0_get_tracks_per_disk(floppy_image *floppy) |
| 125 | | { |
| 126 | | return get_tag(floppy)->tracks; |
| 127 | | } |
| 128 | | |
| 129 | | static UINT64 td0_get_track_offset(floppy_image *floppy, int head, int track) |
| 130 | | { |
| 131 | | return get_tag(floppy)->track_offsets[(track<<1) + head]; |
| 132 | | } |
| 133 | | |
| 134 | | static floperr_t get_offset(floppy_image *floppy, int head, int track, int sector, int sector_is_index, UINT64 *offset) |
| 135 | | { |
| 136 | | UINT64 offs; |
| 137 | | UINT8 *header; |
| 138 | | UINT8 sectors_per_track; |
| 139 | | int i; |
| 140 | | |
| 141 | | if ((head < 0) || (head >= get_tag(floppy)->heads) || (track < 0) || (track >= get_tag(floppy)->tracks) |
| 142 | | || (sector < 0) ) |
| 143 | | return FLOPPY_ERROR_SEEKERROR; |
| 144 | | |
| 145 | | // position on begining of track data |
| 146 | | offs = td0_get_track_offset(floppy, head, track); |
| 147 | | |
| 148 | | // read track header |
| 149 | | header = get_tag(floppy)->data + offs - 4; |
| 150 | | |
| 151 | | // take number of sectors per track |
| 152 | | sectors_per_track = header[0]; |
| 153 | | |
| 154 | | if (!sector_is_index) { |
| 155 | | // when taking ID's return seek error if number is over counter |
| 156 | | if (sector > sectors_per_track) { |
| 157 | | return FLOPPY_ERROR_SEEKERROR; |
| 158 | | } |
| 159 | | } |
| 160 | | |
| 161 | | // move trought sectors |
| 162 | | for(i=0;i < sector-1;i++) { |
| 163 | | header = get_tag(floppy)->data + offs; |
| 164 | | offs+= 6; |
| 165 | | if ((header[4] & 0x30)==0) { |
| 166 | | offs+= 2; |
| 167 | | offs+= header[6] + (header[7]<<8); |
| 168 | | } |
| 169 | | } |
| 170 | | // read size of sector |
| 171 | | header = get_tag(floppy)->data + offs; |
| 172 | | get_tag(floppy)->sector_size = 1 << (header[3] + 7); |
| 173 | | |
| 174 | | if (offset) |
| 175 | | *offset = offs; |
| 176 | | return FLOPPY_ERROR_SUCCESS; |
| 177 | | } |
| 178 | | |
| 179 | | |
| 180 | | |
| 181 | | static floperr_t internal_td0_read_sector(floppy_image *floppy, int head, int track, int sector, int sector_is_index, void *buffer, size_t buflen) |
| 182 | | { |
| 183 | | UINT64 offset; |
| 184 | | floperr_t err; |
| 185 | | UINT8 *header; |
| 186 | | int size,realsize,i; |
| 187 | | int buff_pos; |
| 188 | | int data_pos; |
| 189 | | UINT8 *data; |
| 190 | | UINT8 *buf; |
| 191 | | |
| 192 | | buf = (UINT8*)buffer; |
| 193 | | // take sector offset |
| 194 | | err = get_offset(floppy, head, track, sector, sector_is_index, &offset); |
| 195 | | if (err) |
| 196 | | return err; |
| 197 | | |
| 198 | | // read sector header |
| 199 | | header = get_tag(floppy)->data + offset; |
| 200 | | offset+=6; |
| 201 | | // if there is no date just jump out |
| 202 | | if ((header[4] & 0x30)!=0) return FLOPPY_ERROR_SUCCESS; |
| 203 | | |
| 204 | | offset+=3; |
| 205 | | // take data size |
| 206 | | size = header[6] + (header[7]<<8)-1; |
| 207 | | // take real sector size |
| 208 | | realsize = 1 << (header[3] + 7); |
| 209 | | |
| 210 | | // read sector data |
| 211 | | data = get_tag(floppy)->data + offset; |
| 212 | | buff_pos = 0; |
| 213 | | data_pos = 0; |
| 214 | | |
| 215 | | switch(header[8]) { |
| 216 | | case 0: |
| 217 | | // encoding type 0 |
| 218 | | // - plain data |
| 219 | | memcpy(buffer,data,size); |
| 220 | | break; |
| 221 | | case 1: |
| 222 | | // encoding type 1 |
| 223 | | // - 2 bytes size |
| 224 | | // - 2 bytes of data |
| 225 | | // data is reapeted specified number of times |
| 226 | | while(buff_pos<realsize) { |
| 227 | | for (i=0;i<data[data_pos]+(data[data_pos+1] << 8);i++) { |
| 228 | | buf[buff_pos] = data[data_pos+2];buff_pos++; |
| 229 | | buf[buff_pos] = data[data_pos+3];buff_pos++; |
| 230 | | } |
| 231 | | data_pos+=4; |
| 232 | | } |
| 233 | | break; |
| 234 | | case 2: |
| 235 | | // encoding type 2 |
| 236 | | // - if first byte is zero next byte represent size of |
| 237 | | // plain data after it |
| 238 | | // - if different then zero when multiply by 2 represent |
| 239 | | // size of data that should be reapeted next byte times |
| 240 | | while(buff_pos<realsize) { |
| 241 | | if (data[data_pos]==0x00) { |
| 242 | | int size_ = data[data_pos+1]; |
| 243 | | memcpy(buf+buff_pos,data + data_pos + 2,size_); |
| 244 | | data_pos += 2 + size_; |
| 245 | | buff_pos += size_; |
| 246 | | } else { |
| 247 | | int size_ = 2*data[data_pos]; |
| 248 | | int repeat = data[data_pos+1]; |
| 249 | | data_pos+=2; |
| 250 | | |
| 251 | | for (i=0;i<repeat;i++) { |
| 252 | | memcpy(buf + buff_pos,data + data_pos,size_); |
| 253 | | buff_pos += size_; |
| 254 | | } |
| 255 | | data_pos += size_; |
| 256 | | } |
| 257 | | } |
| 258 | | break; |
| 259 | | default: |
| 260 | | return FLOPPY_ERROR_INTERNAL; |
| 261 | | } |
| 262 | | return FLOPPY_ERROR_SUCCESS; |
| 263 | | } |
| 264 | | |
| 265 | | |
| 266 | | static floperr_t td0_read_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 267 | | { |
| 268 | | return internal_td0_read_sector(floppy, head, track, sector, FALSE, buffer, buflen); |
| 269 | | } |
| 270 | | |
| 271 | | static floperr_t td0_read_indexed_sector(floppy_image *floppy, int head, int track, int sector, void *buffer, size_t buflen) |
| 272 | | { |
| 273 | | return internal_td0_read_sector(floppy, head, track, sector, TRUE, buffer, buflen); |
| 274 | | } |
| 275 | | |
| 276 | | static floperr_t td0_get_sector_length(floppy_image *floppy, int head, int track, int sector, UINT32 *sector_length) |
| 277 | | { |
| 278 | | floperr_t err; |
| 279 | | err = get_offset(floppy, head, track, sector, FALSE, NULL); |
| 280 | | if (err) |
| 281 | | return err; |
| 282 | | |
| 283 | | if (sector_length) { |
| 284 | | *sector_length = get_tag(floppy)->sector_size; |
| 285 | | } |
| 286 | | return FLOPPY_ERROR_SUCCESS; |
| 287 | | } |
| 288 | | |
| 289 | | static floperr_t td0_get_indexed_sector_info(floppy_image *floppy, int head, int track, int sector_index, int *cylinder, int *side, int *sector, UINT32 *sector_length, unsigned long *flags) |
| 290 | | { |
| 291 | | floperr_t retVal; |
| 292 | | UINT64 offset; |
| 293 | | UINT8 *sector_info; |
| 294 | | |
| 295 | | retVal = get_offset(floppy, head, track, sector_index, FALSE, &offset); |
| 296 | | sector_info = get_tag(floppy)->data + offset; |
| 297 | | if (cylinder) |
| 298 | | *cylinder = sector_info[0]; |
| 299 | | if (side) |
| 300 | | *side = sector_info[1]; |
| 301 | | if (sector) |
| 302 | | *sector = sector_info[2]; |
| 303 | | if (sector_length) { |
| 304 | | *sector_length = 1 << (sector_info[3] + 7); |
| 305 | | } |
| 306 | | if (flags) { |
| 307 | | *flags = 0; |
| 308 | | if (sector_info[4] & 0x02) *flags |= ID_FLAG_CRC_ERROR_IN_DATA_FIELD; |
| 309 | | if (sector_info[4] & 0x04) *flags |= ID_FLAG_DELETED_DATA; |
| 310 | | } |
| 311 | | |
| 312 | | return retVal; |
| 313 | | } |
| 314 | | |
| 315 | | int td0dsk_t::data_read(UINT8 *buf, UINT16 size) |
| 316 | | { |
| 317 | | if (floppy_file_offset + size > floppy_image_size(floppy_file) ) { |
| 318 | | size = floppy_image_size(floppy_file) - floppy_file_offset; |
| 319 | | } |
| 320 | | floppy_image_read(floppy_file,buf,floppy_file_offset,size); |
| 321 | | floppy_file_offset += size; |
| 322 | | return size; |
| 323 | | } |
| 324 | | |
| 325 | | |
| 326 | | /* |
| 327 | | * Tables for encoding/decoding upper 6 bits of |
| 328 | | * sliding dictionary pointer |
| 329 | | */ |
| 330 | | |
| 331 | | /* decoder table */ |
| 332 | | static const UINT8 d_code[256] = { |
| 333 | | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 334 | | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 335 | | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 336 | | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 337 | | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, |
| 338 | | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, |
| 339 | | 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, |
| 340 | | 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, |
| 341 | | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 342 | | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 343 | | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 344 | | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 345 | | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 346 | | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 347 | | 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, |
| 348 | | 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, |
| 349 | | 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, |
| 350 | | 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, |
| 351 | | 0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D, |
| 352 | | 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F, |
| 353 | | 0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11, |
| 354 | | 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13, |
| 355 | | 0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15, |
| 356 | | 0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17, |
| 357 | | 0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B, |
| 358 | | 0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F, |
| 359 | | 0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23, |
| 360 | | 0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27, |
| 361 | | 0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B, |
| 362 | | 0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F, |
| 363 | | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, |
| 364 | | 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, |
| 365 | | }; |
| 366 | | |
| 367 | | static const UINT8 d_len[256] = { |
| 368 | | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 369 | | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 370 | | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 371 | | 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, |
| 372 | | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 373 | | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 374 | | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 375 | | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 376 | | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 377 | | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, |
| 378 | | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 379 | | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 380 | | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 381 | | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 382 | | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 383 | | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 384 | | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 385 | | 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, |
| 386 | | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 387 | | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 388 | | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 389 | | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 390 | | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 391 | | 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, |
| 392 | | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 393 | | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 394 | | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 395 | | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 396 | | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 397 | | 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, |
| 398 | | 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, |
| 399 | | 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, |
| 400 | | }; |
| 401 | | |
| 402 | | int td0dsk_t::next_word() |
| 403 | | { |
| 404 | | if(tdctl.ibufndx >= tdctl.ibufcnt) |
| 405 | | { |
| 406 | | tdctl.ibufndx = 0; |
| 407 | | tdctl.ibufcnt = data_read(tdctl.inbuf,BUFSZ); |
| 408 | | if(tdctl.ibufcnt <= 0) |
| 409 | | return(-1); |
| 410 | | } |
| 411 | | while (getlen <= 8) { // typically reads a word at a time |
| 412 | | getbuf |= tdctl.inbuf[tdctl.ibufndx++] << (8 - getlen); |
| 413 | | getlen += 8; |
| 414 | | } |
| 415 | | return(0); |
| 416 | | } |
| 417 | | |
| 418 | | |
| 419 | | int td0dsk_t::GetBit() /* get one bit */ |
| 420 | | { |
| 421 | | INT16 i; |
| 422 | | if(next_word() < 0) |
| 423 | | return(-1); |
| 424 | | i = getbuf; |
| 425 | | getbuf <<= 1; |
| 426 | | getlen--; |
| 427 | | if(i < 0) |
| 428 | | return(1); |
| 429 | | else |
| 430 | | return(0); |
| 431 | | } |
| 432 | | |
| 433 | | int td0dsk_t::GetByte() /* get a byte */ |
| 434 | | { |
| 435 | | UINT16 i; |
| 436 | | if(next_word() != 0) |
| 437 | | return(-1); |
| 438 | | i = getbuf; |
| 439 | | getbuf <<= 8; |
| 440 | | getlen -= 8; |
| 441 | | i = i >> 8; |
| 442 | | return((int) i); |
| 443 | | } |
| 444 | | |
| 445 | | |
| 446 | | |
| 447 | | /* initialize freq tree */ |
| 448 | | |
| 449 | | void td0dsk_t::StartHuff() |
| 450 | | { |
| 451 | | int i, j; |
| 452 | | |
| 453 | | for (i = 0; i < N_CHAR; i++) { |
| 454 | | freq[i] = 1; |
| 455 | | son[i] = i + T; |
| 456 | | prnt[i + T] = i; |
| 457 | | } |
| 458 | | i = 0; j = N_CHAR; |
| 459 | | while (j <= R) { |
| 460 | | freq[j] = freq[i] + freq[i + 1]; |
| 461 | | son[j] = i; |
| 462 | | prnt[i] = prnt[i + 1] = j; |
| 463 | | i += 2; j++; |
| 464 | | } |
| 465 | | freq[T] = 0xffff; |
| 466 | | prnt[R] = 0; |
| 467 | | } |
| 468 | | |
| 469 | | |
| 470 | | /* reconstruct freq tree */ |
| 471 | | |
| 472 | | void td0dsk_t::reconst() |
| 473 | | { |
| 474 | | INT16 i, j, k; |
| 475 | | UINT16 f, l; |
| 476 | | |
| 477 | | /* halven cumulative freq for leaf nodes */ |
| 478 | | j = 0; |
| 479 | | for (i = 0; i < T; i++) { |
| 480 | | if (son[i] >= T) { |
| 481 | | freq[j] = (freq[i] + 1) / 2; |
| 482 | | son[j] = son[i]; |
| 483 | | j++; |
| 484 | | } |
| 485 | | } |
| 486 | | /* make a tree : first, connect children nodes */ |
| 487 | | for (i = 0, j = N_CHAR; j < T; i += 2, j++) { |
| 488 | | k = i + 1; |
| 489 | | f = freq[j] = freq[i] + freq[k]; |
| 490 | | for (k = j - 1; f < freq[k]; k--); |
| 491 | | k++; |
| 492 | | l = (j - k) * 2; |
| 493 | | |
| 494 | | /* movmem() is Turbo-C dependent |
| 495 | | rewritten to memmove() by Kenji */ |
| 496 | | |
| 497 | | /* movmem(&freq[k], &freq[k + 1], l); */ |
| 498 | | (void)memmove(&freq[k + 1], &freq[k], l); |
| 499 | | freq[k] = f; |
| 500 | | /* movmem(&son[k], &son[k + 1], l); */ |
| 501 | | (void)memmove(&son[k + 1], &son[k], l); |
| 502 | | son[k] = i; |
| 503 | | } |
| 504 | | /* connect parent nodes */ |
| 505 | | for (i = 0; i < T; i++) { |
| 506 | | if ((k = son[i]) >= T) { |
| 507 | | prnt[k] = i; |
| 508 | | } else { |
| 509 | | prnt[k] = prnt[k + 1] = i; |
| 510 | | } |
| 511 | | } |
| 512 | | } |
| 513 | | |
| 514 | | |
| 515 | | /* update freq tree */ |
| 516 | | |
| 517 | | void td0dsk_t::update(int c) |
| 518 | | { |
| 519 | | int i, j, k, l; |
| 520 | | |
| 521 | | if (freq[R] == MAX_FREQ) { |
| 522 | | reconst(); |
| 523 | | } |
| 524 | | c = prnt[c + T]; |
| 525 | | do { |
| 526 | | k = ++freq[c]; |
| 527 | | |
| 528 | | /* swap nodes to keep the tree freq-ordered */ |
| 529 | | if (k > freq[l = c + 1]) { |
| 530 | | while (k > freq[++l]); |
| 531 | | l--; |
| 532 | | freq[c] = freq[l]; |
| 533 | | freq[l] = k; |
| 534 | | |
| 535 | | i = son[c]; |
| 536 | | prnt[i] = l; |
| 537 | | if (i < T) prnt[i + 1] = l; |
| 538 | | |
| 539 | | j = son[l]; |
| 540 | | son[l] = i; |
| 541 | | |
| 542 | | prnt[j] = c; |
| 543 | | if (j < T) prnt[j + 1] = c; |
| 544 | | son[c] = j; |
| 545 | | |
| 546 | | c = l; |
| 547 | | } |
| 548 | | } while ((c = prnt[c]) != 0); /* do it until reaching the root */ |
| 549 | | } |
| 550 | | |
| 551 | | |
| 552 | | INT16 td0dsk_t::DecodeChar() |
| 553 | | { |
| 554 | | int ret; |
| 555 | | UINT16 c; |
| 556 | | |
| 557 | | c = son[R]; |
| 558 | | |
| 559 | | /* |
| 560 | | * start searching tree from the root to leaves. |
| 561 | | * choose node #(son[]) if input bit == 0 |
| 562 | | * else choose #(son[]+1) (input bit == 1) |
| 563 | | */ |
| 564 | | while (c < T) { |
| 565 | | if((ret = GetBit()) < 0) |
| 566 | | return(-1); |
| 567 | | c += (unsigned) ret; |
| 568 | | c = son[c]; |
| 569 | | } |
| 570 | | c -= T; |
| 571 | | update(c); |
| 572 | | return c; |
| 573 | | } |
| 574 | | |
| 575 | | INT16 td0dsk_t::DecodePosition() |
| 576 | | { |
| 577 | | INT16 bit; |
| 578 | | UINT16 i, j, c; |
| 579 | | |
| 580 | | /* decode upper 6 bits from given table */ |
| 581 | | if((bit=GetByte()) < 0) |
| 582 | | return(-1); |
| 583 | | i = (UINT16) bit; |
| 584 | | c = (UINT16)d_code[i] << 6; |
| 585 | | j = d_len[i]; |
| 586 | | |
| 587 | | /* input lower 6 bits directly */ |
| 588 | | j -= 2; |
| 589 | | while (j--) { |
| 590 | | if((bit = GetBit()) < 0) |
| 591 | | return(-1); |
| 592 | | i = (i << 1) + bit; |
| 593 | | } |
| 594 | | return(c | (i & 0x3f)); |
| 595 | | } |
| 596 | | |
| 597 | | /* DeCompression |
| 598 | | |
| 599 | | split out initialization code to init_Decode() |
| 600 | | |
| 601 | | */ |
| 602 | | |
| 603 | | void td0dsk_t::init_Decode() |
| 604 | | { |
| 605 | | int i; |
| 606 | | getbuf = 0; |
| 607 | | getlen = 0; |
| 608 | | tdctl.ibufcnt= tdctl.ibufndx = 0; // input buffer is empty |
| 609 | | tdctl.bufcnt = 0; |
| 610 | | StartHuff(); |
| 611 | | for (i = 0; i < N - F; i++) |
| 612 | | text_buf[i] = ' '; |
| 613 | | tdctl.r = N - F; |
| 614 | | } |
| 615 | | |
| 616 | | |
| 617 | | int td0dsk_t::Decode(UINT8 *buf, int len) /* Decoding/Uncompressing */ |
| 618 | | { |
| 619 | | INT16 c,pos; |
| 620 | | int count; // was an unsigned long, seems unnecessary |
| 621 | | for (count = 0; count < len; ) { |
| 622 | | if(tdctl.bufcnt == 0) { |
| 623 | | if((c = DecodeChar()) < 0) |
| 624 | | return(count); // fatal error |
| 625 | | if (c < 256) { |
| 626 | | *(buf++) = c; |
| 627 | | text_buf[tdctl.r++] = c; |
| 628 | | tdctl.r &= (N - 1); |
| 629 | | count++; |
| 630 | | } |
| 631 | | else { |
| 632 | | if((pos = DecodePosition()) < 0) |
| 633 | | return(count); // fatal error |
| 634 | | tdctl.bufpos = (tdctl.r - pos - 1) & (N - 1); |
| 635 | | tdctl.bufcnt = c - 255 + THRESHOLD; |
| 636 | | tdctl.bufndx = 0; |
| 637 | | } |
| 638 | | } |
| 639 | | else { // still chars from last string |
| 640 | | while( tdctl.bufndx < tdctl.bufcnt && count < len ) { |
| 641 | | c = text_buf[(tdctl.bufpos + tdctl.bufndx) & (N - 1)]; |
| 642 | | *(buf++) = c; |
| 643 | | tdctl.bufndx++; |
| 644 | | text_buf[tdctl.r++] = c; |
| 645 | | tdctl.r &= (N - 1); |
| 646 | | count++; |
| 647 | | } |
| 648 | | // reset bufcnt after copy string from text_buf[] |
| 649 | | if(tdctl.bufndx >= tdctl.bufcnt) |
| 650 | | tdctl.bufndx = tdctl.bufcnt = 0; |
| 651 | | } |
| 652 | | } |
| 653 | | return(count); // count == len, success |
| 654 | | } |
| 655 | | |
| 656 | | |
| 657 | | FLOPPY_CONSTRUCT( td0_dsk_construct ) |
| 658 | | { |
| 659 | | td0dsk_t *state = &td0dsk; |
| 660 | | struct FloppyCallbacks *callbacks; |
| 661 | | struct td0dsk_tag *tag; |
| 662 | | UINT8 *header; |
| 663 | | int number_of_sectors; |
| 664 | | int position; |
| 665 | | int i; |
| 666 | | int track; |
| 667 | | |
| 668 | | if(params) |
| 669 | | { |
| 670 | | // create |
| 671 | | return FLOPPY_ERROR_UNSUPPORTED; |
| 672 | | } |
| 673 | | |
| 674 | | tag = (struct td0dsk_tag *) floppy_create_tag(floppy, sizeof(struct td0dsk_tag)); |
| 675 | | if (!tag) |
| 676 | | return FLOPPY_ERROR_OUTOFMEMORY; |
| 677 | | |
| 678 | | tag->data = global_alloc_array_clear(UINT8,floppy_image_size(floppy)); |
| 679 | | if (tag->data==NULL) { |
| 680 | | return FLOPPY_ERROR_OUTOFMEMORY; |
| 681 | | } |
| 682 | | floppy_image_read(floppy, tag->data, 0, floppy_image_size(floppy)); |
| 683 | | header = tag->data; |
| 684 | | |
| 685 | | if (header[0]=='t') { |
| 686 | | UINT8 obuf[BUFSZ]; |
| 687 | | int rd; |
| 688 | | int off = 12; |
| 689 | | int size = 0; |
| 690 | | state->floppy_file = floppy; |
| 691 | | state->init_Decode(); |
| 692 | | state->floppy_file_offset = 12; |
| 693 | | do |
| 694 | | { |
| 695 | | if((rd = state->Decode(obuf, BUFSZ)) > 0) size += rd; |
| 696 | | } while(rd == BUFSZ); |
| 697 | | memcpy(obuf,tag->data,12); |
| 698 | | free(tag->data); |
| 699 | | tag->data = (UINT8*)malloc(size+12); |
| 700 | | if (tag->data==NULL) { |
| 701 | | return FLOPPY_ERROR_OUTOFMEMORY; |
| 702 | | } |
| 703 | | memcpy(tag->data,obuf,12); |
| 704 | | state->floppy_file_offset = 12; |
| 705 | | state->init_Decode(); |
| 706 | | do |
| 707 | | { |
| 708 | | if((rd = state->Decode(obuf, BUFSZ)) > 0) { |
| 709 | | memcpy(tag->data+off,obuf,rd); |
| 710 | | off += rd; |
| 711 | | } |
| 712 | | } while(rd == BUFSZ); |
| 713 | | } |
| 714 | | header = tag->data; |
| 715 | | tag->heads = header[9]; |
| 716 | | if (tag->heads > 1) { |
| 717 | | tag->heads = 2; |
| 718 | | } |
| 719 | | |
| 720 | | // header len + comment header + comment len |
| 721 | | position = 12; |
| 722 | | if (header[7] & 0x80) { |
| 723 | | position += 10 + header[14] + (header[15]<<8); |
| 724 | | } |
| 725 | | tag->tracks = 0; |
| 726 | | do { |
| 727 | | // read track header |
| 728 | | header = tag->data + position; |
| 729 | | track = header[1]; |
| 730 | | number_of_sectors = header[0]; |
| 731 | | if (number_of_sectors!=0xff){ |
| 732 | | position+=4; |
| 733 | | tag->track_offsets[(track<<1) + (header[2] & 1)] = position; |
| 734 | | for(i=0;i<number_of_sectors;i++) { |
| 735 | | // read sector header |
| 736 | | header = tag->data + position; |
| 737 | | position+=6; |
| 738 | | // read sector size |
| 739 | | if ((header[4] & 0x30)==0) { |
| 740 | | // if there is sector data |
| 741 | | header = tag->data + position; |
| 742 | | position+=2; |
| 743 | | // skip sector data |
| 744 | | position+= header[0] + (header[1]<<8); |
| 745 | | } |
| 746 | | } |
| 747 | | tag->tracks++; |
| 748 | | } |
| 749 | | } while(number_of_sectors!=0xff); |
| 750 | | tag->tracks++; |
| 751 | | |
| 752 | | callbacks = floppy_callbacks(floppy); |
| 753 | | callbacks->read_sector = td0_read_sector; |
| 754 | | callbacks->read_indexed_sector = td0_read_indexed_sector; |
| 755 | | callbacks->get_sector_length = td0_get_sector_length; |
| 756 | | callbacks->get_heads_per_disk = td0_get_heads_per_disk; |
| 757 | | callbacks->get_tracks_per_disk = td0_get_tracks_per_disk; |
| 758 | | callbacks->get_indexed_sector_info = td0_get_indexed_sector_info; |
| 759 | | return FLOPPY_ERROR_SUCCESS; |
| 760 | | } |
| 761 | | |
| 762 | | FLOPPY_DESTRUCT( td0_dsk_destruct ) |
| 763 | | { |
| 764 | | struct td0dsk_tag *tag = get_tag(floppy); |
| 765 | | global_free(tag->data); |
| 766 | | tag->data = NULL; |
| 767 | | return FLOPPY_ERROR_SUCCESS; |
| 768 | | } |
| | No newline at end of file |
src/mess/formats/coco_cas.c
| r11457 | r11458 | |
| 1 | | /************************************************************************** |
| 2 | | |
| 3 | | coco_cas.c |
| 4 | | |
| 5 | | Format code for CoCo CAS (*.cas) files |
| 6 | | |
| 7 | | This is the actual code that processes the CAS data. CAS files are a |
| 8 | | problem because they are a legacy of previous CoCo emulators, and most |
| 9 | | of these emulators patch the system as a shortcut. Thus, the ones and |
| 10 | | zeroes in the file do not truly represent modulated pulses in the |
| 11 | | waveform, but what the BIOS reads and writes. |
| 12 | | |
| 13 | | In doing so, they make the CoCo more tolerant of short headers and lack |
| 14 | | of delays between blocks. This legacy is reflected in most CAS files in |
| 15 | | use, and thus presents a problem for a pure hardware emulation like MESS. |
| 16 | | |
| 17 | | One alternative is to preprocess the data on the CAS, file by file, but |
| 18 | | this proves to be problematic because in the process, legitimate data |
| 19 | | that is unrecognized by the preprocessor may get dropped. |
| 20 | | |
| 21 | | The approach taken here is a hybrid approach - it retrieves the data |
| 22 | | block by block until an error occurs; be it the end of the CAS or a |
| 23 | | corrupt (?!) block. When "legitimate" blocks are done processing, the |
| 24 | | remainder of the data is added to the waveform in a traditional manner. |
| 25 | | The result has proven to work quite well. |
| 26 | | |
| 27 | | 2005-May-04, P.Harvey-Smith, improved handling of some "copy protected" |
| 28 | | Dragon games that have odd blocks in odd places, this has slightly |
| 29 | | increased loading time but does mean games like "Rommel's Revenge" will |
| 30 | | now load. |
| 31 | | |
| 32 | | **************************************************************************/ |
| 33 | | |
| 34 | | #include "coco_cas.h" |
| 35 | | #include "timer.h" |
| 36 | | |
| 37 | | #define COCO_WAVESAMPLES_HEADER (1.0) |
| 38 | | #define COCO_WAVESAMPLES_TRAILER (1.0) |
| 39 | | #define COCO_LONGSILENCE (5.0) |
| 40 | | |
| 41 | | //some games load with only 5s, but most games need 15s |
| 42 | | #define ALICE32_WAVESAMPLES_HEADER (15.0) |
| 43 | | |
| 44 | | static int synccount; |
| 45 | | |
| 46 | | const struct CassetteModulation coco_cas_modulation = |
| 47 | | { |
| 48 | | CASSETTE_MODULATION_SINEWAVE, |
| 49 | | 600.0, 1200.0, 1500.0, |
| 50 | | 1500.0, 2400.0, 3000.0 |
| 51 | | }; |
| 52 | | |
| 53 | | |
| 54 | | |
| 55 | | static casserr_t coco_cas_identify(cassette_image *cassette, struct CassetteOptions *opts) |
| 56 | | { |
| 57 | | return cassette_modulation_identify(cassette, &coco_cas_modulation, opts); |
| 58 | | } |
| 59 | | |
| 60 | | |
| 61 | | |
| 62 | | static int get_cas_block(cassette_image *cassette, UINT64 *offset, UINT8 *block, int *block_len) |
| 63 | | { |
| 64 | | UINT8 block_length = 0; |
| 65 | | UINT8 block_checksum = 0; |
| 66 | | UINT64 current_offset; |
| 67 | | UINT64 image_size; |
| 68 | | PAIR p; |
| 69 | | int i; |
| 70 | | int state = 0; |
| 71 | | int phase = 0; |
| 72 | | |
| 73 | | synccount = 0; |
| 74 | | p.w.l = 0; |
| 75 | | image_size = cassette_image_size(cassette); |
| 76 | | current_offset = *offset; |
| 77 | | |
| 78 | | while(current_offset < image_size) |
| 79 | | { |
| 80 | | cassette_image_read(cassette, &p.b.h, current_offset, 1); |
| 81 | | current_offset++; |
| 82 | | |
| 83 | | for (i = 0; i < 8; i++) |
| 84 | | { |
| 85 | | p.w.l >>= 1; |
| 86 | | |
| 87 | | if (state == 0) |
| 88 | | { |
| 89 | | /* searching for a block */ |
| 90 | | if (p.b.l == 0x3C) |
| 91 | | { |
| 92 | | /* found one! */ |
| 93 | | phase = i; |
| 94 | | state++; |
| 95 | | } |
| 96 | | else if (p.b.l == 0x55) |
| 97 | | { |
| 98 | | synccount++; |
| 99 | | } |
| 100 | | } |
| 101 | | else if (i == phase) |
| 102 | | { |
| 103 | | *(block++) = p.b.l; |
| 104 | | switch(state) { |
| 105 | | case 1: |
| 106 | | /* found file type */ |
| 107 | | block_checksum = p.b.l; |
| 108 | | state++; |
| 109 | | break; |
| 110 | | case 2: |
| 111 | | /* found file size */ |
| 112 | | block_length = p.b.l; |
| 113 | | *block_len = ((int) block_length) + 3; |
| 114 | | block_checksum += p.b.l; |
| 115 | | state++; |
| 116 | | break; |
| 117 | | |
| 118 | | case 3: |
| 119 | | /* data byte */ |
| 120 | | if (block_length) |
| 121 | | { |
| 122 | | block_length--; |
| 123 | | block_checksum += p.b.l; |
| 124 | | } |
| 125 | | else |
| 126 | | { |
| 127 | | /* end of block */ |
| 128 | | if (p.b.l != block_checksum) |
| 129 | | { |
| 130 | | /* checksum failure */ |
| 131 | | return FALSE; |
| 132 | | } |
| 133 | | else |
| 134 | | { |
| 135 | | /* checksum success */ |
| 136 | | *offset = current_offset; |
| 137 | | return TRUE; |
| 138 | | } |
| 139 | | } |
| 140 | | } |
| 141 | | } |
| 142 | | } |
| 143 | | } |
| 144 | | |
| 145 | | /* no more blocks */ |
| 146 | | return FALSE; |
| 147 | | } |
| 148 | | |
| 149 | | |
| 150 | | |
| 151 | | static casserr_t cas_load(cassette_image *cassette, UINT8 silence) |
| 152 | | { |
| 153 | | casserr_t err; |
| 154 | | UINT64 offset; |
| 155 | | UINT64 image_size; |
| 156 | | UINT8 block[258]; /* 255 bytes per block + 3 (type, length, checksum) */ |
| 157 | | int block_length = 0; |
| 158 | | UINT8 last_blocktype; |
| 159 | | double time_index = 0.0; |
| 160 | | double time_displacement; |
| 161 | | static const UINT8 magic_bytes[2] = { 0x55, 0x3C }; |
| 162 | | |
| 163 | | #if 0 |
| 164 | | { |
| 165 | | static const UINT8 dummy_bytes[] = |
| 166 | | { |
| 167 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 168 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 169 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 170 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 171 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 172 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 173 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 174 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 175 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 176 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 177 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 178 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 179 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 180 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 181 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 182 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 183 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 184 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 185 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 186 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 187 | | 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, |
| 188 | | 0x3C, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A |
| 189 | | }; |
| 190 | | time_index = 10.0; |
| 191 | | return cassette_put_modulated_data(cassette, 0, time_index, dummy_bytes, sizeof(dummy_bytes), &coco_cas_modulation, &time_displacement); |
| 192 | | } |
| 193 | | #endif |
| 194 | | |
| 195 | | err = cassette_put_sample(cassette, 0, time_index, COCO_WAVESAMPLES_HEADER, 0); |
| 196 | | if (err) |
| 197 | | return err; |
| 198 | | time_index += COCO_WAVESAMPLES_HEADER; |
| 199 | | |
| 200 | | offset = 0; |
| 201 | | last_blocktype = 0; |
| 202 | | image_size = cassette_image_size(cassette); |
| 203 | | |
| 204 | | /* try to find a block that we can untangle */ |
| 205 | | while(get_cas_block(cassette, &offset, block, &block_length)) |
| 206 | | { |
| 207 | | /* Forcing a silence before a filename block, improves the ability to load some */ |
| 208 | | /* copy protected Dragon games, e.g. Rommel's Revenge */ |
| 209 | | /* was the last block a filename block? */ |
| 210 | | if ((last_blocktype == 0) || (last_blocktype == 0xFF) || (block[0] == 0)) |
| 211 | | { |
| 212 | | /* silence */ |
| 213 | | err = cassette_put_sample(cassette, 0, time_index, silence, 0); |
| 214 | | if (err) |
| 215 | | return err; |
| 216 | | time_index += silence; |
| 217 | | |
| 218 | | /* sync data */ |
| 219 | | err = cassette_put_modulated_filler(cassette, 0, time_index, 0x55, 128, &coco_cas_modulation, &time_displacement); |
| 220 | | if (err) |
| 221 | | return err; |
| 222 | | time_index += time_displacement; |
| 223 | | } |
| 224 | | else if (synccount != 0) /* If we have multiple sync bytes in cas file, make sure they */ |
| 225 | | { /* are passed through */ |
| 226 | | /* sync data */ |
| 227 | | err = cassette_put_modulated_filler(cassette, 0, time_index, 0x55, synccount, &coco_cas_modulation, &time_displacement); |
| 228 | | if (err) |
| 229 | | return err; |
| 230 | | time_index += time_displacement; |
| 231 | | } |
| 232 | | |
| 233 | | /* now fill in the magic bytes */ |
| 234 | | err = cassette_put_modulated_data(cassette, 0, time_index, magic_bytes, sizeof(magic_bytes), &coco_cas_modulation, &time_displacement); |
| 235 | | if (err) |
| 236 | | return err; |
| 237 | | time_index += time_displacement; |
| 238 | | |
| 239 | | /* now fill in the block */ |
| 240 | | err = cassette_put_modulated_data(cassette, 0, time_index, block, block_length, &coco_cas_modulation, &time_displacement); |
| 241 | | if (err) |
| 242 | | return err; |
| 243 | | time_index += time_displacement; |
| 244 | | |
| 245 | | /* and the last magic byte */ |
| 246 | | err = cassette_put_modulated_filler(cassette, 0, time_index, 0x55, 1, &coco_cas_modulation, &time_displacement); |
| 247 | | if (err) |
| 248 | | return err; |
| 249 | | time_index += time_displacement; |
| 250 | | |
| 251 | | last_blocktype = block[0]; |
| 252 | | } |
| 253 | | |
| 254 | | /* all futher data is undecipherable, so output it verbatim */ |
| 255 | | err = cassette_read_modulated_data(cassette, 0, time_index, offset, image_size - offset, &coco_cas_modulation, &time_displacement); |
| 256 | | if (err) |
| 257 | | return err; |
| 258 | | time_index += time_displacement; |
| 259 | | |
| 260 | | return CASSETTE_ERROR_SUCCESS; |
| 261 | | } |
| 262 | | |
| 263 | | static casserr_t coco_cas_load(cassette_image *cassette) |
| 264 | | { |
| 265 | | return cas_load(cassette, COCO_WAVESAMPLES_HEADER); |
| 266 | | } |
| 267 | | |
| 268 | | static casserr_t alice32_cas_load(cassette_image *cassette) |
| 269 | | { |
| 270 | | return cas_load(cassette, ALICE32_WAVESAMPLES_HEADER); |
| 271 | | } |
| 272 | | |
| 273 | | const struct CassetteFormat coco_cas_format = |
| 274 | | { |
| 275 | | "cas", |
| 276 | | coco_cas_identify, |
| 277 | | coco_cas_load, |
| 278 | | NULL |
| 279 | | }; |
| 280 | | |
| 281 | | const struct CassetteFormat alice32_cas_format = |
| 282 | | { |
| 283 | | "cas,c10,k7", |
| 284 | | coco_cas_identify, |
| 285 | | alice32_cas_load, |
| 286 | | NULL |
| 287 | | }; |
| 288 | | |
| 289 | | |
| 290 | | CASSETTE_FORMATLIST_START(coco_cassette_formats) |
| 291 | | CASSETTE_FORMAT(coco_cas_format) |
| 292 | | CASSETTE_FORMATLIST_END |
| 293 | | |
| 294 | | CASSETTE_FORMATLIST_START(alice32_cassette_formats) |
| 295 | | CASSETTE_FORMAT(alice32_cas_format) |
| 296 | | CASSETTE_FORMATLIST_END |
src/mess/formats/thom_cas.c
| r11457 | r11458 | |
| 1 | | /********************************************************************** |
| 2 | | |
| 3 | | Copyright (C) Antoine Mine' 2006 |
| 4 | | |
| 5 | | Thomson 8-bit computers |
| 6 | | |
| 7 | | **********************************************************************/ |
| 8 | | |
| 9 | | #include <math.h> |
| 10 | | |
| 11 | | #include "emu.h" |
| 12 | | #include "imagedev/cassette.h" |
| 13 | | #include "imagedev/cassimg.h" |
| 14 | | #include "includes/thomson.h" |
| 15 | | #include "formats/thom_cas.h" |
| 16 | | |
| 17 | | |
| 18 | | /***************************** configuration **************************/ |
| 19 | | |
| 20 | | #define K7_SPEED_HACK 0 |
| 21 | | /* When set to 1, bits extracted from k7 files are not transformed into a |
| 22 | | raw wave. It saves some time & memory but disables the ability to "hear" the |
| 23 | | cassette. |
| 24 | | This does not affect raw wave files. |
| 25 | | Also, we do not do this for k5 files because the the corresponding wave |
| 26 | | files are more compact anyway. */ |
| 27 | | /* It must be set accordingly in machine/thomson.c */ |
| 28 | | |
| 29 | | #define VERBOSE 0 /* 0, 1 or 2 */ |
| 30 | | |
| 31 | | |
| 32 | | /***************************** utilities **************************/ |
| 33 | | |
| 34 | | #define PRINT(x) mame_printf_info x |
| 35 | | |
| 36 | | #define LOG(x) do { if (VERBOSE > 0) logerror x; } while (0) |
| 37 | | #define VLOG(x) do { if (VERBOSE > 1) logerror x; } while (0) |
| 38 | | |
| 39 | | |
| 40 | | #define TO7_BIT_LENGTH 0.001114 |
| 41 | | |
| 42 | | #define TO7_FREQ_CASS_0 4500. |
| 43 | | #define TO7_FREQ_CASS_1 6300. |
| 44 | | |
| 45 | | #define TO7_PERIOD_CASS_0 ( 1. / TO7_FREQ_CASS_0 ) |
| 46 | | #define TO7_PERIOD_CASS_1 ( 1. / TO7_FREQ_CASS_1 ) |
| 47 | | |
| 48 | | static UINT32 to7_k7_bitsize; |
| 49 | | static UINT8* to7_k7_bits; |
| 50 | | |
| 51 | | #define MO5_BIT_LENGTH 0.000833 |
| 52 | | #define MO5_HBIT_LENGTH (MO5_BIT_LENGTH / 2.) |
| 53 | | |
| 54 | | /************************* k7 format *************************/ |
| 55 | | |
| 56 | | /* The k7 format is widespread among existing Thomson emulators. |
| 57 | | It represents the stream of bytes returned by the BIOS cassette routine, |
| 58 | | and so, is used by emulators that bypass the BIOS routine. |
| 59 | | It is generally hacked from the raw image more or less by hand |
| 60 | | (in particular, to by-pass special loaders and copy-protection schemes). |
| 61 | | |
| 62 | | As we run the original BIOS routine, we need to hack back the k7 file |
| 63 | | into the original stream. |
| 64 | | |
| 65 | | In theory, the byte stream is a sequence of blocks of the form: |
| 66 | | - 2-byte header: 01 3C |
| 67 | | - 1-byte type |
| 68 | | 00 = start of file, the block contains the filename |
| 69 | | 01 = chunk of file data |
| 70 | | FF = end of file |
| 71 | | - 1-byte size |
| 72 | | - size bytes of data |
| 73 | | - one byte of CRC |
| 74 | | |
| 75 | | These are separated by 0xff fillers. |
| 76 | | |
| 77 | | There are also a variety of non-standard blocks, and we try to handle them. |
| 78 | | |
| 79 | | Our main problem is to guess where the places where the motor will be |
| 80 | | cut-off and introduce there sequences of 1 bit for synchronization. |
| 81 | | |
| 82 | | Note: as a nice effect, if our routine succeeds, it constructs a wave |
| 83 | | file that should be usable on a real computer. |
| 84 | | */ |
| 85 | | |
| 86 | | |
| 87 | | |
| 88 | | static const struct CassetteModulation to7_k7_modulation = |
| 89 | | { |
| 90 | | CASSETTE_MODULATION_SQUAREWAVE, |
| 91 | | 4000.0, 4500.0, 5000.0, |
| 92 | | 5500.0, 6300.0, 7500.0 |
| 93 | | }; |
| 94 | | |
| 95 | | |
| 96 | | |
| 97 | | static casserr_t to7_k7_identify ( cassette_image *cass, struct CassetteOptions *opts ) |
| 98 | | { |
| 99 | | casserr_t e = cassette_modulation_identify( cass, &to7_k7_modulation, opts ); |
| 100 | | return e; |
| 101 | | } |
| 102 | | |
| 103 | | |
| 104 | | |
| 105 | | static casserr_t to7_k7_load( cassette_image *cass ) |
| 106 | | { |
| 107 | | #if ! K7_SPEED_HACK |
| 108 | | static const INT8 square_wave[] = { -128, 127 }; |
| 109 | | double time = 0.; |
| 110 | | #endif |
| 111 | | size_t size = cassette_image_size( cass ), pos = 0; |
| 112 | | int i, sz, sz2, bitmax = 1024, invalid = 0; |
| 113 | | UINT8 in, typ, block[264]; |
| 114 | | |
| 115 | | LOG (( "to7_k7_load: start conversion, size=%li\n", (long)size )); |
| 116 | | PRINT (( "to7_k7_load: open cassette, length: %li bytes\n", (long) size )); |
| 117 | | |
| 118 | | if ( to7_k7_bits ) |
| 119 | | { |
| 120 | | free( to7_k7_bits ); |
| 121 | | to7_k7_bits = NULL; |
| 122 | | } |
| 123 | | |
| 124 | | to7_k7_bitsize = 0; |
| 125 | | to7_k7_bits = global_alloc_array_clear(UINT8, bitmax ); |
| 126 | | |
| 127 | | /* store one period */ |
| 128 | | #if K7_SPEED_HACK |
| 129 | | #define K7_PUT( PERIOD ) |
| 130 | | #else |
| 131 | | #define K7_PUT( PERIOD ) \ |
| 132 | | do \ |
| 133 | | { \ |
| 134 | | casserr_t err; \ |
| 135 | | err = cassette_put_samples( cass, 0, time, (PERIOD), 2, 1, \ |
| 136 | | square_wave, CASSETTE_WAVEFORM_8BIT ); \ |
| 137 | | if ( err ) \ |
| 138 | | return err; \ |
| 139 | | time += (PERIOD); \ |
| 140 | | } while (0) |
| 141 | | #endif |
| 142 | | |
| 143 | | /* store one bit */ |
| 144 | | #define K7_PUT_BIT( BIT ) \ |
| 145 | | do \ |
| 146 | | { \ |
| 147 | | int b; \ |
| 148 | | if ( BIT ) \ |
| 149 | | { \ |
| 150 | | for ( b = 0; b < 7; b++ ) \ |
| 151 | | K7_PUT( TO7_PERIOD_CASS_1 ); \ |
| 152 | | } \ |
| 153 | | else \ |
| 154 | | { \ |
| 155 | | for ( b = 0; b < 5; b++ ) \ |
| 156 | | K7_PUT( TO7_PERIOD_CASS_0 ); \ |
| 157 | | } \ |
| 158 | | if ( to7_k7_bitsize + 1 >= bitmax ) \ |
| 159 | | { \ |
| 160 | | UINT8* a = global_alloc_array_clear(UINT8, bitmax * 2); \ |
| 161 | | memcpy ( a, to7_k7_bits, bitmax ); \ |
| 162 | | bitmax *= 2; \ |
| 163 | | to7_k7_bits = a; \ |
| 164 | | } \ |
| 165 | | to7_k7_bits[ to7_k7_bitsize++ ] = (BIT); \ |
| 166 | | } while (0) |
| 167 | | |
| 168 | | /* store one byte, with start / stop bits */ |
| 169 | | #define K7_PUT_BYTE( BYTE ) \ |
| 170 | | do \ |
| 171 | | { \ |
| 172 | | UINT8 x; \ |
| 173 | | K7_PUT_BIT( 0 ); \ |
| 174 | | for ( x = 0; x < 8; x++ ) \ |
| 175 | | K7_PUT_BIT( ( (BYTE) >> x ) & 1 ); \ |
| 176 | | K7_PUT_BIT( 1 ); \ |
| 177 | | K7_PUT_BIT( 1 ); \ |
| 178 | | } while (0) |
| 179 | | |
| 180 | | #define K7_FILL_1( SIZE ) \ |
| 181 | | do \ |
| 182 | | { \ |
| 183 | | if ( (SIZE) > 0 ) { \ |
| 184 | | int ii; \ |
| 185 | | LOG (( "to7_k7_load: 1-filler size=%i bitstart=%i\n", \ |
| 186 | | (SIZE), to7_k7_bitsize )); \ |
| 187 | | for ( ii = 0; ii < (SIZE); ii++ ) K7_PUT_BIT( 1 ); \ |
| 188 | | } \ |
| 189 | | } while (0) |
| 190 | | |
| 191 | | #define K7_FILL_ff( SIZE ) \ |
| 192 | | do \ |
| 193 | | { \ |
| 194 | | if ( (SIZE) > 0 ) \ |
| 195 | | { \ |
| 196 | | int ii; \ |
| 197 | | LOG (( "to7_k7_load: 0xff filler size=%i bitstart=%i\n", (SIZE), to7_k7_bitsize )); \ |
| 198 | | for ( ii = 0; ii < (SIZE); ii++ ) \ |
| 199 | | K7_PUT_BYTE( 0xff ); \ |
| 200 | | } \ |
| 201 | | } while (0) |
| 202 | | |
| 203 | | /* check format */ |
| 204 | | cassette_image_read( cass, block, 0, 64 ); |
| 205 | | for ( i = 3; ; i++ ) |
| 206 | | { |
| 207 | | if ( ( i >= size ) || ( i >= 64 ) ) |
| 208 | | { |
| 209 | | /* ? */ |
| 210 | | PRINT (( "to7_k7_load: WARNING: this does not look like a MO or TO cassette.\n" )); |
| 211 | | break; |
| 212 | | } |
| 213 | | else if ( ( block[i-3] == 0x01 ) && ( block[i-2] == 0x3c ) && ( block[i-1] == 0x5a ) && ! block[i] ) |
| 214 | | { |
| 215 | | /* MO */ |
| 216 | | PRINT (( "to7_k7_load: WARNING: this looks like a MO cassette, not a TO one.\n" )); |
| 217 | | break; |
| 218 | | } |
| 219 | | else if ( ( block[i-3] == 0xff ) && ( block[i-2] == 0x01 ) && ( block[i-1] == 0x3c ) && ! block[i] ) |
| 220 | | { |
| 221 | | /* TO */ |
| 222 | | break; |
| 223 | | } |
| 224 | | } |
| 225 | | |
| 226 | | /* skip to first 0xff filler */ |
| 227 | | for ( sz = 0; pos < size; pos++, sz++ ) |
| 228 | | { |
| 229 | | cassette_image_read( cass, &in, pos, 1 ); |
| 230 | | if ( in == 0xff ) |
| 231 | | break; |
| 232 | | } |
| 233 | | if ( sz > 0 ) |
| 234 | | LOG (( "to7_k7_load: skip %i trash bytes\n", sz )); |
| 235 | | |
| 236 | | /* loop over regular blocks */ |
| 237 | | while ( pos < size ) |
| 238 | | { |
| 239 | | |
| 240 | | rebounce: |
| 241 | | /* skip 0xff filler */ |
| 242 | | for ( sz = 0; pos < size; pos++, sz++ ) |
| 243 | | { |
| 244 | | cassette_image_read( cass, &in, pos, 1 ); |
| 245 | | /* actually, we are bit laxist and treat as 0xff bytes with at least |
| 246 | | 5 bits out of 8 set to 1 |
| 247 | | */ |
| 248 | | for ( i = 0; in; in >>= 1 ) |
| 249 | | i += (in & 1); |
| 250 | | if ( i < 5 ) |
| 251 | | break; |
| 252 | | } |
| 253 | | |
| 254 | | /* get block header */ |
| 255 | | if ( pos + 4 > size ) |
| 256 | | { |
| 257 | | pos -= sz; |
| 258 | | break; |
| 259 | | } |
| 260 | | cassette_image_read( cass, block, pos, 4 ); |
| 261 | | typ = block[2]; |
| 262 | | sz2 = block[3]+1; |
| 263 | | if ( block[0] != 0x01 || block[1] != 0x3c || ( typ != 0x00 && typ != 0x01 && typ != 0xff ) ) |
| 264 | | { |
| 265 | | pos -= sz; |
| 266 | | break; |
| 267 | | } |
| 268 | | pos += 4; |
| 269 | | |
| 270 | | /* get block */ |
| 271 | | cassette_image_read( cass, block+4, pos, sz2 ); |
| 272 | | pos += sz2; |
| 273 | | |
| 274 | | /* 1-filler and 0xff-filler */ |
| 275 | | if ( typ == 0 || typ == 0xff ) |
| 276 | | K7_FILL_1( 1000 ); |
| 277 | | K7_FILL_ff( sz ); |
| 278 | | |
| 279 | | /* put block */ |
| 280 | | LOG (( "to7_k7_load: block off=$%x type=$%02X size=%i bitstart=%i\n", (int) pos-sz2-4, typ, sz2, to7_k7_bitsize )); |
| 281 | | VLOG (( "to7_k7_load: data:" )); |
| 282 | | for ( i = 0; i < sz2 + 4; i ++) |
| 283 | | { |
| 284 | | VLOG (( " $%02X", block[i] )); |
| 285 | | K7_PUT_BYTE( block[i] ); |
| 286 | | } |
| 287 | | VLOG (( "\n" )); |
| 288 | | |
| 289 | | /* if it is a directory enty, says so to the user */ |
| 290 | | if ( typ == 0 ) |
| 291 | | { |
| 292 | | char name[] = "01234567.ABC"; |
| 293 | | UINT8 t = block[15]; |
| 294 | | UINT8 u = block[16]; |
| 295 | | int p = (to7_k7_bitsize - sz2 - 4 - sz) * TO7_BIT_LENGTH; |
| 296 | | memcpy( name, block+4, 8 ); |
| 297 | | memcpy( name+9, block+12, 3 ); |
| 298 | | for ( i = 0; name[i]; i++) |
| 299 | | { |
| 300 | | if ( name[i] < ' ' || name[i] >= 127 ) |
| 301 | | name[i] = '?'; |
| 302 | | } |
| 303 | | PRINT (( "to7_k7_load: file \"%s\" type=%s,%s at %imn %is\n", |
| 304 | | name, |
| 305 | | (t==0) ? "bas" : (t==1) ? "dat" : (t==2) ? "bin" : "???", |
| 306 | | (u == 0) ? "a" : (u == 0xff) ? "b" : "?", |
| 307 | | p / 60, p % 60 )); |
| 308 | | } |
| 309 | | |
| 310 | | /* extra 1-fillers */ |
| 311 | | if ( typ == 0 || typ == 0xff ) |
| 312 | | K7_FILL_1( 1000 ); |
| 313 | | } |
| 314 | | |
| 315 | | /* trailing data with invalid block structure |
| 316 | | => dump it in a raw form, but stay alert for hidden block starts |
| 317 | | */ |
| 318 | | if ( pos < size ) |
| 319 | | { |
| 320 | | invalid++; |
| 321 | | LOG (( "to7_k7_load: trailing raw bytes off=$%x bitstart=%i\n", (int) pos, to7_k7_bitsize )); |
| 322 | | |
| 323 | | /* put block */ |
| 324 | | for (; pos < size; pos++ ) |
| 325 | | { |
| 326 | | cassette_image_read( cass, &in, pos, 1 ); |
| 327 | | for ( sz = 0; pos < size && in == 0xff; sz++ ) |
| 328 | | { |
| 329 | | pos++; |
| 330 | | cassette_image_read( cass, &in, pos, 1 ); |
| 331 | | } |
| 332 | | if ( invalid < 10 && sz > 4 && in == 0x01 && pos + 4 <= size ) |
| 333 | | { |
| 334 | | UINT8 in1,in2; |
| 335 | | cassette_image_read( cass, &in1, pos+1, 1 ); |
| 336 | | cassette_image_read( cass, &in2, pos+2, 1 ); |
| 337 | | if ( (in1 == 0x3c) && ((in2 == 0x00) || (in2 == 0x01) ) ) |
| 338 | | { |
| 339 | | /* seems we have a regular block hidden in raw data => rebounce */ |
| 340 | | LOG (( "to7_k7_load: hidden regular block found\n" )); |
| 341 | | pos -= sz; |
| 342 | | goto rebounce; |
| 343 | | } |
| 344 | | if ( ( ( in1 == 0x3d ) && ( in2 == 0 ) ) || ( ( in1 == 0x57 ) && ( in2 == 0x49 ) ) ) |
| 345 | | { |
| 346 | | /* special block (Infogrames) => just prepend filler */ |
| 347 | | K7_FILL_1 ( 500 ); |
| 348 | | LOG (( "to7_k7_load: special $%02X $%02X $%02X block found off=$%x bitstart=%i\n", in, in1, in2, (int) pos, to7_k7_bitsize )); |
| 349 | | } |
| 350 | | } |
| 351 | | for ( i = 0; i < sz; i++ ) |
| 352 | | K7_PUT_BYTE( 0xff ); |
| 353 | | K7_PUT_BYTE( in ); |
| 354 | | } |
| 355 | | } |
| 356 | | |
| 357 | | if ( invalid ) |
| 358 | | PRINT (( "to7_k7_load: WARNING: the k7 has an unknown structure and may not work\n" )); |
| 359 | | |
| 360 | | sz = to7_k7_bitsize * TO7_BIT_LENGTH; |
| 361 | | PRINT (( "to7_k7_load: cassette length: %imn %is (%i samples)\n", sz / 60, sz % 60, to7_k7_bitsize )); |
| 362 | | |
| 363 | | return CASSETTE_ERROR_SUCCESS; |
| 364 | | } |
| 365 | | |
| 366 | | |
| 367 | | |
| 368 | | static const struct CassetteFormat to7_k7 = |
| 369 | | { "k7", to7_k7_identify, to7_k7_load, NULL /* no save */ }; |
| 370 | | |
| 371 | | |
| 372 | | |
| 373 | | /********************* TO7 WAV format ************************/ |
| 374 | | |
| 375 | | |
| 376 | | |
| 377 | | static casserr_t to7_wav_identify ( cassette_image *cass, |
| 378 | | struct CassetteOptions *opts ) |
| 379 | | { |
| 380 | | casserr_t e = wavfile_format.identify( cass, opts ); |
| 381 | | return e; |
| 382 | | } |
| 383 | | |
| 384 | | |
| 385 | | |
| 386 | | static casserr_t to7_wav_load ( cassette_image *cass ) |
| 387 | | { |
| 388 | | casserr_t e = wavfile_format.load( cass ); |
| 389 | | struct CassetteInfo info; |
| 390 | | double len; |
| 391 | | |
| 392 | | if ( to7_k7_bits ) |
| 393 | | { |
| 394 | | free( to7_k7_bits ); |
| 395 | | to7_k7_bits = NULL; |
| 396 | | } |
| 397 | | |
| 398 | | if ( e != CASSETTE_ERROR_SUCCESS ) |
| 399 | | return e; |
| 400 | | |
| 401 | | cassette_get_info( cass, &info ); |
| 402 | | |
| 403 | | len = (double) info.sample_count / info.sample_frequency; |
| 404 | | |
| 405 | | PRINT (( "to7_wav_load: loading cassette, length %imn %is, %i Hz, %i bps, %i bits\n", |
| 406 | | (int) len / 60, (int) len % 60, |
| 407 | | info.sample_frequency, info.bits_per_sample, |
| 408 | | (int) (len / TO7_BIT_LENGTH) )); |
| 409 | | |
| 410 | | return CASSETTE_ERROR_SUCCESS; |
| 411 | | } |
| 412 | | |
| 413 | | |
| 414 | | |
| 415 | | static casserr_t to7_wav_save ( cassette_image *cass, const struct CassetteInfo *info ) |
| 416 | | { |
| 417 | | int len = info->sample_count / info->sample_frequency; |
| 418 | | PRINT (( "to7_wav_save: saving cassette, length %imn %is, %i Hz, %i bps\n", len / 60, len % 60, info->sample_frequency, info->bits_per_sample )); |
| 419 | | return wavfile_format.save( cass, info ); |
| 420 | | } |
| 421 | | |
| 422 | | |
| 423 | | |
| 424 | | /* overloaded wav: dump info */ |
| 425 | | static const struct CassetteFormat to7_wav = |
| 426 | | { "wav", to7_wav_identify, to7_wav_load, to7_wav_save }; |
| 427 | | |
| 428 | | |
| 429 | | |
| 430 | | /************************* k5 format *************************/ |
| 431 | | |
| 432 | | |
| 433 | | |
| 434 | | static casserr_t mo5_k5_identify ( cassette_image *cass, |
| 435 | | struct CassetteOptions *opts ) |
| 436 | | { |
| 437 | | opts -> bits_per_sample = 8; |
| 438 | | opts -> channels = 1; |
| 439 | | opts -> sample_frequency = 22100;//11050; |
| 440 | | return CASSETTE_ERROR_SUCCESS; |
| 441 | | } |
| 442 | | |
| 443 | | |
| 444 | | |
| 445 | | /* As the k7 format, the k5 format represents the stream of bytes returned by |
| 446 | | the BIOS cassette routine and need to be hacked back into the original |
| 447 | | cassette wave. |
| 448 | | |
| 449 | | The format of blocks is a bit different from that of TO7 cassettes: |
| 450 | | - 2-byte header is: 3C 5A |
| 451 | | - 1-byte type |
| 452 | | 00 = start of file, the block contains the filename |
| 453 | | 01 = chunk of file data |
| 454 | | FF = end of file |
| 455 | | - 1-byte size |
| 456 | | - size - 1 bytes of data (255 bytes of data if size = 0) |
| 457 | | - one byte of CRC: sum from (and including) header up to (and including) |
| 458 | | CRC byte should be 0 modulo 256 |
| 459 | | |
| 460 | | They are separated by 0x01 filler bytes (generally 10 of them), |
| 461 | | for synchronisation. There are also extra 0 filler bits between certain |
| 462 | | kinds of blocks, when the motor is supposed to go off and back on. |
| 463 | | */ |
| 464 | | |
| 465 | | static casserr_t mo5_k5_load( cassette_image *cass ) |
| 466 | | { |
| 467 | | size_t size = cassette_image_size( cass ), pos = 0; |
| 468 | | int i, sz, sz2, hbit = 0; |
| 469 | | UINT8 in, in2, in3, typ, block[264], sum; |
| 470 | | int invalid = 0, hbitsize = 0, dcmoto = 0; |
| 471 | | |
| 472 | | LOG (( "mo5_k5_load: start conversion, size=%li\n", (long)size )); |
| 473 | | PRINT (( "mo5_k5_load: open cassette, length: %li bytes\n", (long) size )); |
| 474 | | |
| 475 | | /* store a half-bit */ |
| 476 | | #define K5_PUT_HBIT \ |
| 477 | | do \ |
| 478 | | { \ |
| 479 | | cassette_put_sample ( cass, 0, hbitsize * MO5_HBIT_LENGTH, MO5_HBIT_LENGTH, (hbit ? 1 : -1) << 30 ); \ |
| 480 | | hbitsize++; \ |
| 481 | | } while ( 0 ) |
| 482 | | |
| 483 | | /* store one bit */ |
| 484 | | #define K5_PUT_BIT( BIT ) \ |
| 485 | | do \ |
| 486 | | { \ |
| 487 | | if ( BIT ) \ |
| 488 | | { \ |
| 489 | | K5_PUT_HBIT; \ |
| 490 | | hbit = !hbit; \ |
| 491 | | K5_PUT_HBIT; \ |
| 492 | | } \ |
| 493 | | else \ |
| 494 | | { \ |
| 495 | | K5_PUT_HBIT; \ |
| 496 | | K5_PUT_HBIT; \ |
| 497 | | } \ |
| 498 | | hbit = !hbit; \ |
| 499 | | } while (0) |
| 500 | | |
| 501 | | /* store one byte, no start / stop bit, converse order from TO7 */ |
| 502 | | #define K5_PUT_BYTE( BYTE ) \ |
| 503 | | do \ |
| 504 | | { \ |
| 505 | | UINT8 b = BYTE; \ |
| 506 | | int x; \ |
| 507 | | for ( x = 0; x < 8; x++ ) \ |
| 508 | | K5_PUT_BIT( (b >> (7 - x)) & 1 ); \ |
| 509 | | } while (0) |
| 510 | | |
| 511 | | /* store filler */ |
| 512 | | #define K5_FILL_0( SIZE ) \ |
| 513 | | do \ |
| 514 | | { \ |
| 515 | | if ( (SIZE) > 0 ) \ |
| 516 | | { \ |
| 517 | | int j; \ |
| 518 | | LOG (( "mo5_k5_load: 0-filler size=%i hbitstart=%i\n", (SIZE), hbitsize )); \ |
| 519 | | for ( j = 0; j < (SIZE); j++ ) \ |
| 520 | | K5_PUT_BIT( 0 ); \ |
| 521 | | } \ |
| 522 | | } while (0) |
| 523 | | |
| 524 | | #define K5_FILL_01( SIZE ) \ |
| 525 | | do \ |
| 526 | | { \ |
| 527 | | if ( (SIZE) > 0 ) \ |
| 528 | | { \ |
| 529 | | int j; \ |
| 530 | | LOG (( "mo5_k5_load: 0x01 filler size=%i bitstart=%i\n", (SIZE), hbitsize )); \ |
| 531 | | for ( j = 0; j < (SIZE); j++ ) \ |
| 532 | | K5_PUT_BYTE( 0x01 ); \ |
| 533 | | } \ |
| 534 | | } while (0) |
| 535 | | |
| 536 | | /* check format */ |
| 537 | | cassette_image_read( cass, block, 0, 64 ); |
| 538 | | for ( i = 3; ; i++ ) |
| 539 | | { |
| 540 | | if ( ( i >= size ) || ( i >= 64 ) ) |
| 541 | | { |
| 542 | | /* ? */ |
| 543 | | PRINT (( "to5_k5_load: WARNING: this does not look like a MO or TO cassette.\n" )); |
| 544 | | break; |
| 545 | | } |
| 546 | | else if ( ( block[i-3] == 0x01 ) && ( block[i-2] == 0x3c ) && ( block[i-1] == 0x5a ) && ! block[i] ) |
| 547 | | { |
| 548 | | /* MO */ |
| 549 | | break; |
| 550 | | } |
| 551 | | else if ( ( block[i-3] == 0xff ) && ( block[i-2] == 0x01 ) && ( block[i-1] == 0x3c ) && ! block[i] ) |
| 552 | | { |
| 553 | | /* TO */ |
| 554 | | PRINT (( "to5_k5_load: WARNING: this looks like a TO cassette, not a MO one.\n" )); |
| 555 | | break; |
| 556 | | } |
| 557 | | } |
| 558 | | |
| 559 | | cassette_image_read( cass, block, pos, 6 ); |
| 560 | | if ( ! memcmp( block, "DCMOTO", 6 ) || ! memcmp( block, "DCMO5", 5 ) || ! memcmp( block, "DCMO6", 5 ) ) |
| 561 | | dcmoto = 1; |
| 562 | | |
| 563 | | /* loop over regular blocks */ |
| 564 | | while ( pos < size ) |
| 565 | | { |
| 566 | | |
| 567 | | rebounce: |
| 568 | | /* skip DCMOTO header*/ |
| 569 | | if ( dcmoto ) |
| 570 | | { |
| 571 | | cassette_image_read( cass, block, pos, 6 ); |
| 572 | | if ( ! memcmp( block, "DCMOTO", 6 ) ) |
| 573 | | { |
| 574 | | LOG (( "mo5_k5_load: DCMOTO signature found at off=$%x\n", (int)pos )); |
| 575 | | pos += 6; |
| 576 | | } |
| 577 | | else if ( ! memcmp( block, "DCMO", 4 ) ) |
| 578 | | { |
| 579 | | LOG (( "mo5_k5_load: DCMO* signature found at off=$%x\n", (int)pos )); |
| 580 | | pos += 5; |
| 581 | | } |
| 582 | | } |
| 583 | | |
| 584 | | /* skip 0x01 filler */ |
| 585 | | for ( sz = 0; pos < size; pos++, sz++ ) |
| 586 | | { |
| 587 | | cassette_image_read( cass, &in, pos, 1 ); |
| 588 | | if ( in != 0x01 ) |
| 589 | | break; |
| 590 | | } |
| 591 | | |
| 592 | | /* get block header */ |
| 593 | | if ( pos + 4 > size ) |
| 594 | | { |
| 595 | | pos -= sz; |
| 596 | | break; |
| 597 | | } |
| 598 | | cassette_image_read( cass, block, pos, 4 ); |
| 599 | | typ = block[2]; |
| 600 | | sz2 = (UINT8) (block[3]-1); |
| 601 | | if ( block[0] != 0x3c || block[1] != 0x5a || ( typ != 0x00 && typ != 0x01 && typ != 0xff ) || pos+sz2 > size ) |
| 602 | | { |
| 603 | | pos -= sz; |
| 604 | | break; |
| 605 | | } |
| 606 | | pos += 4; |
| 607 | | |
| 608 | | /* get block */ |
| 609 | | cassette_image_read( cass, block+4, pos, sz2 ); |
| 610 | | pos += sz2; |
| 611 | | |
| 612 | | /* 0-fillers and 0x01-fillers */ |
| 613 | | if ( typ == 0 || typ == 0xff ) |
| 614 | | K5_FILL_0( 1200 ); |
| 615 | | else |
| 616 | | K5_FILL_0( 300 ); /* for MO6 */ |
| 617 | | K5_FILL_01( sz < 10 ? 10 : sz ); |
| 618 | | |
| 619 | | /* put block */ |
| 620 | | LOG (( "mo5_k5_load: block off=$%x type=$%02X size=%i hbitstart=%i\n", (int) pos-sz2-4, typ, sz2, hbitsize )); |
| 621 | | VLOG (( "mo5_k5_load: data:" )); |
| 622 | | for ( i = 0; i < sz2 + 4; i ++) |
| 623 | | { |
| 624 | | VLOG (( " $%02X", block[i] )); |
| 625 | | K5_PUT_BYTE( block[i] ); |
| 626 | | } |
| 627 | | VLOG (( "\n" )); |
| 628 | | |
| 629 | | /* checksum */ |
| 630 | | for ( i = 0, sum = 0; i < sz2; i++ ) |
| 631 | | sum += block[i+4]; |
| 632 | | if ( sum ) |
| 633 | | LOG(( "mo5_k5_load: invalid checksum $%02X (should be 0)\n", sum )); |
| 634 | | |
| 635 | | /* if it is a directory enty, says so to the user */ |
| 636 | | if ( typ == 0 ) |
| 637 | | { |
| 638 | | char name[] = "01234567.ABC"; |
| 639 | | UINT8 t = block[15]; |
| 640 | | UINT8 u = block[16]; |
| 641 | | int p = (hbitsize - sz2 - 4 - sz) * MO5_HBIT_LENGTH; |
| 642 | | memcpy( name, block+4, 8 ); |
| 643 | | memcpy( name+9, block+12, 3 ); |
| 644 | | for ( i = 0; name[i]; i++) |
| 645 | | { |
| 646 | | if ( name[i] < ' ' || name[i] >= 127 ) |
| 647 | | name[i] = '?'; |
| 648 | | } |
| 649 | | PRINT (( "mo5_k5_load: file \"%s\" type=%s,%s at %imn %is\n", |
| 650 | | name, |
| 651 | | (t==0) ? "bas" : (t==1) ? "dat" : (t==2) ? "bin" : "???", |
| 652 | | (u == 0) ? "a" : (u == 0xff) ? "b" : "?", |
| 653 | | p / 60, p % 60 )); |
| 654 | | } |
| 655 | | |
| 656 | | /* 0-fillers */ |
| 657 | | if ( typ == 0xff || typ == 0x00 ) |
| 658 | | K5_FILL_0( 1800 ); |
| 659 | | } |
| 660 | | |
| 661 | | /* dump trailing bytes, but also look for beginings of blocks */ |
| 662 | | if ( pos < size ) |
| 663 | | { |
| 664 | | invalid++; |
| 665 | | K5_FILL_0( 1200 ); |
| 666 | | LOG (( "mo5_k5_load: trailing trash off=$%x size=%i hbitstart=%i\n", (int) pos, (int) (size-pos), hbitsize )); |
| 667 | | for ( ; pos < size; pos++ ) |
| 668 | | { |
| 669 | | cassette_image_read( cass, &in, pos, 1 ); |
| 670 | | if ( dcmoto && in=='D' ) |
| 671 | | { |
| 672 | | /* skip DCMOTO header*/ |
| 673 | | cassette_image_read( cass, block, pos, 6 ); |
| 674 | | if ( ! memcmp( block, "DCMOTO", 6 ) ) |
| 675 | | { |
| 676 | | LOG (( "mo5_k5_load: DCMOTO signature found at off=$%x\n", (int)pos )); |
| 677 | | pos += 6; |
| 678 | | cassette_image_read( cass, &in, pos, 1 ); |
| 679 | | } |
| 680 | | else if ( ! memcmp( block, "DCMO", 4 ) ) |
| 681 | | { |
| 682 | | LOG (( "mo5_k5_load: DCMO* signature found at off=$%x\n", (int)pos )); |
| 683 | | pos += 5; |
| 684 | | cassette_image_read( cass, &in, pos, 1 ); |
| 685 | | } |
| 686 | | } |
| 687 | | for ( sz = 0; pos < size && in == 0x01; sz++ ) |
| 688 | | { |
| 689 | | pos++; |
| 690 | | cassette_image_read( cass, &in, pos, 1 ); |
| 691 | | } |
| 692 | | if ( sz > 6 ) |
| 693 | | { |
| 694 | | cassette_image_read( cass, &in2, pos+1, 1 ); |
| 695 | | cassette_image_read( cass, &in3, pos+2, 1 ); |
| 696 | | if ( invalid < 10 && in == 0x3c && in2 == 0x5a && (in3 == 0x00 || in3 == 0x01 || in3 == 0xff ) ) |
| 697 | | { |
| 698 | | /* regular block found */ |
| 699 | | LOG (( "mo5_k5_load: hidden regular block found\n" )); |
| 700 | | pos -= sz; |
| 701 | | goto rebounce; |
| 702 | | } |
| 703 | | if ( invalid < 10 && sz > 6 && ( (in == 0x3c && in2 == 0x5a) || (in == 0xc3 && in2 == 0x5a) || (in == 0xc3 && in2 == 0x3c) || (in == 0x87 && in2 == 0x4a) ) ) |
| 704 | | { |
| 705 | | /* special block found */ |
| 706 | | K5_FILL_0( 1200 ); |
| 707 | | LOG (( "mo5_k5_load: special block $%02X $%02X found off=$%x hbitstart=%i\n", in, in2, (int) pos-sz, hbitsize )); |
| 708 | | } |
| 709 | | } |
| 710 | | |
| 711 | | VLOG (( "mo5_k5_load: special data:" )); |
| 712 | | for ( i = 0; i < sz; i++ ) |
| 713 | | { |
| 714 | | K5_PUT_BYTE( 0x01 ); |
| 715 | | VLOG (( " $01" )); |
| 716 | | } |
| 717 | | K5_PUT_BYTE( in ); |
| 718 | | VLOG (( " $%02X\n", in )); |
| 719 | | } |
| 720 | | } |
| 721 | | |
| 722 | | if ( invalid ) |
| 723 | | PRINT (( "mo5_k5_load: WARNING: the k5 has an unknown structure and may not work\n" )); |
| 724 | | |
| 725 | | sz = hbitsize * MO5_HBIT_LENGTH; |
| 726 | | PRINT (( "mo5_k5_load: cassette length: %imn %is (%i half-bits)\n", sz / 60, sz % 60, hbitsize )); |
| 727 | | |
| 728 | | return CASSETTE_ERROR_SUCCESS; |
| 729 | | } |
| 730 | | |
| 731 | | |
| 732 | | |
| 733 | | static const struct CassetteFormat mo5_k5 = |
| 734 | | { "k5,k7", mo5_k5_identify, mo5_k5_load, NULL /* no save */ }; |
| 735 | | |
| 736 | | |
| 737 | | /********************* MO5 WAV format ************************/ |
| 738 | | |
| 739 | | |
| 740 | | |
| 741 | | static casserr_t mo5_wav_identify ( cassette_image *cass, |
| 742 | | struct CassetteOptions *opts ) |
| 743 | | { |
| 744 | | casserr_t e = wavfile_format.identify( cass, opts ); |
| 745 | | return e; |
| 746 | | } |
| 747 | | |
| 748 | | |
| 749 | | |
| 750 | | static casserr_t mo5_wav_load ( cassette_image *cass ) |
| 751 | | { |
| 752 | | casserr_t e = wavfile_format.load( cass ); |
| 753 | | struct CassetteInfo info; |
| 754 | | int len; |
| 755 | | if ( e == CASSETTE_ERROR_SUCCESS ) |
| 756 | | { |
| 757 | | cassette_get_info( cass, &info ); |
| 758 | | len = info.sample_count / info.sample_frequency; |
| 759 | | PRINT (( "mo5_wav_load: loading cassette, length %imn %is, %i Hz, %i bps\n", len / 60, len % 60, info.sample_frequency, info.bits_per_sample )); |
| 760 | | } |
| 761 | | return e; |
| 762 | | } |
| 763 | | |
| 764 | | |
| 765 | | |
| 766 | | static casserr_t mo5_wav_save ( cassette_image *cass, const struct CassetteInfo *info ) |
| 767 | | { |
| 768 | | int len = info->sample_count / info->sample_frequency; |
| 769 | | PRINT (( "mo5_wav_save: saving cassette, length %imn %is, %i Hz, %i bps\n", len / 60, len % 60, info->sample_frequency, info->bits_per_sample )); |
| 770 | | return wavfile_format.save( cass, info ); |
| 771 | | } |
| 772 | | |
| 773 | | |
| 774 | | |
| 775 | | /* overloaded wav: dump info */ |
| 776 | | static const struct CassetteFormat mo5_wav = |
| 777 | | { "wav", mo5_wav_identify, mo5_wav_load, mo5_wav_save }; |
| 778 | | |
| 779 | | |
| 780 | | |
| 781 | | /********************* formats ************************/ |
| 782 | | |
| 783 | | |
| 784 | | static const struct CassetteFormat *const to7_cassette_formats[] = |
| 785 | | { &to7_wav, &to7_k7, NULL }; |
| 786 | | |
| 787 | | |
| 788 | | static const struct CassetteFormat *const mo5_cassette_formats[] = |
| 789 | | { &mo5_wav, &mo5_k5, NULL }; |
| 790 | | |
| 791 | | |
| 792 | | |
| 793 | | /********************* devices ************************/ |
| 794 | | |
| 795 | | const cassette_config to7_cassette_config = |
| 796 | | { |
| 797 | | to7_cassette_formats, |
| 798 | | NULL, |
| 799 | | (cassette_state)(CASSETTE_PLAY | CASSETTE_MOTOR_DISABLED | CASSETTE_SPEAKER_ENABLED), |
| 800 | | NULL |
| 801 | | }; |
| 802 | | |
| 803 | | const cassette_config mo5_cassette_config = |
| 804 | | { |
| 805 | | mo5_cassette_formats, |
| 806 | | NULL, |
| 807 | | (cassette_state)(CASSETTE_PLAY | CASSETTE_MOTOR_DISABLED | CASSETTE_SPEAKER_ENABLED), |
| 808 | | NULL |
| 809 | | }; |
| 810 | | |