diff options
Diffstat (limited to 'src/lib/formats/st_dsk.cpp')
-rw-r--r-- | src/lib/formats/st_dsk.cpp | 120 |
1 files changed, 61 insertions, 59 deletions
diff --git a/src/lib/formats/st_dsk.cpp b/src/lib/formats/st_dsk.cpp index 9808dc56ff9..fc105de6fcb 100644 --- a/src/lib/formats/st_dsk.cpp +++ b/src/lib/formats/st_dsk.cpp @@ -2,62 +2,68 @@ // copyright-holders:Olivier Galibert /********************************************************************* - formats/st_dsk.c + formats/st_dsk.cpp All usual Atari ST formats *********************************************************************/ -#include <assert.h> - #include "formats/st_dsk.h" +#include "ioprocs.h" +#include "multibyte.h" + +#include <cstring> + + st_format::st_format() { } -const char *st_format::name() const +const char *st_format::name() const noexcept { return "st"; } -const char *st_format::description() const +const char *st_format::description() const noexcept { return "Atari ST floppy disk image"; } -const char *st_format::extensions() const +const char *st_format::extensions() const noexcept { return "st"; } -bool st_format::supports_save() const +bool st_format::supports_save() const noexcept { return true; } -void st_format::find_size(io_generic *io, uint8_t &track_count, uint8_t &head_count, uint8_t §or_count) +void st_format::find_size(util::random_read &io, uint8_t &track_count, uint8_t &head_count, uint8_t §or_count) { - uint64_t size = io_generic_size(io); - for(track_count=80; track_count <= 82; track_count++) - for(head_count=1; head_count <= 2; head_count++) - for(sector_count=9; sector_count <= 11; sector_count++) - if(size == (uint32_t)512*track_count*head_count*sector_count) - return; + uint64_t size; + if(!io.length(size)) { + for(track_count=80; track_count <= 82; track_count++) + for(head_count=1; head_count <= 2; head_count++) + for(sector_count=9; sector_count <= 11; sector_count++) + if(size == (uint32_t)512*track_count*head_count*sector_count) + return; + } track_count = head_count = sector_count = 0; } -int st_format::identify(io_generic *io, uint32_t form_factor) +int st_format::identify(util::random_read &io, uint32_t form_factor, const std::vector<uint32_t> &variants) const { uint8_t track_count, head_count, sector_count; find_size(io, track_count, head_count, sector_count); if(track_count) - return 50; + return FIFID_SIZE; return 0; } -bool st_format::load(io_generic *io, uint32_t form_factor, floppy_image *image) +bool st_format::load(util::random_read &io, uint32_t form_factor, const std::vector<uint32_t> &variants, floppy_image &image) const { uint8_t track_count, head_count, sector_count; find_size(io, track_count, head_count, sector_count); @@ -73,18 +79,18 @@ bool st_format::load(io_generic *io, uint32_t form_factor, floppy_image *image) int track_size = sector_count*512; for(int track=0; track < track_count; track++) { for(int head=0; head < head_count; head++) { - io_generic_read(io, sectdata, (track*head_count + head)*track_size, track_size); + /*auto const [err, actual] =*/ read_at(io, (track*head_count + head)*track_size, sectdata, track_size); // FIXME: check for errors and premature EOF generate_track(atari_st_fcp_get_desc(track, head, head_count, sector_count), track, head, sectors, sector_count, 100000, image); } } - image->set_variant(floppy_image::DSDD); + image.set_variant(floppy_image::DSDD); return true; } -bool st_format::save(io_generic *io, floppy_image *image) +bool st_format::save(util::random_read_write &io, const std::vector<uint32_t> &variants, const floppy_image &image) const { int track_count, head_count, sector_count; get_geometry_mfm_pc(image, 2000, track_count, head_count, sector_count); @@ -109,7 +115,7 @@ bool st_format::save(io_generic *io, floppy_image *image) for(int track=0; track < track_count; track++) { for(int head=0; head < head_count; head++) { get_track_data_mfm_pc(track, head, image, 2000, 512, sector_count, sectdata); - io_generic_write(io, sectdata, (track*head_count + head)*track_size, track_size); + /*auto const [err, actual] =*/ write_at(io, (track*head_count + head)*track_size, sectdata, track_size); // FIXME: check for errors } } @@ -121,35 +127,35 @@ msa_format::msa_format() { } -const char *msa_format::name() const +const char *msa_format::name() const noexcept { return "msa"; } -const char *msa_format::description() const +const char *msa_format::description() const noexcept { return "Atari MSA floppy disk image"; } -const char *msa_format::extensions() const +const char *msa_format::extensions() const noexcept { return "msa"; } -bool msa_format::supports_save() const +bool msa_format::supports_save() const noexcept { return true; } -void msa_format::read_header(io_generic *io, uint16_t &sign, uint16_t §, uint16_t &head, uint16_t &strack, uint16_t &etrack) +void msa_format::read_header(util::random_read &io, uint16_t &sign, uint16_t §, uint16_t &head, uint16_t &strack, uint16_t &etrack) { uint8_t h[10]; - io_generic_read(io, h, 0, 10); - sign = (h[0] << 8) | h[1]; - sect = (h[2] << 8) | h[3]; - head = (h[4] << 8) | h[5]; - strack = (h[6] << 8) | h[7]; - etrack = (h[8] << 8) | h[9]; + /*auto const [err, actual] =*/ read_at(io, 0, h, 10); // FIXME: check for errors and premature EOF + sign = get_u16be(&h[0]); + sect = get_u16be(&h[2]); + head = get_u16be(&h[4]); + strack = get_u16be(&h[6]); + etrack = get_u16be(&h[8]); } bool msa_format::uncompress(uint8_t *buffer, int csize, int usize) @@ -162,7 +168,7 @@ bool msa_format::uncompress(uint8_t *buffer, int csize, int usize) if(csize-src < 3) return false; c = buffer[src++]; - int count = (buffer[src] << 8) | buffer[src+1]; + int count = get_u16be(&buffer[src]); src += 2; if(usize-dst < count) return false; @@ -193,8 +199,8 @@ bool msa_format::compress(const uint8_t *buffer, int usize, uint8_t *dest, int & return false; dest[dst++] = 0xe5; dest[dst++] = c; - dest[dst++] = ncopy >> 8; - dest[dst++] = ncopy; + put_u16be(&dest[dst], ncopy); + dst += 2; } else { src -= ncopy-1; dest[dst++] = c; @@ -206,7 +212,7 @@ bool msa_format::compress(const uint8_t *buffer, int usize, uint8_t *dest, int & return dst < usize; } -int msa_format::identify(io_generic *io, uint32_t form_factor) +int msa_format::identify(util::random_read &io, uint32_t form_factor, const std::vector<uint32_t> &variants) const { uint16_t sign, sect, head, strack, etrack; read_header(io, sign, sect, head, strack, etrack); @@ -216,11 +222,11 @@ int msa_format::identify(io_generic *io, uint32_t form_factor) (head == 0 || head == 1) && strack <= etrack && etrack < 82) - return 100; + return FIFID_SIGN | FIFID_STRUCT; return 0; } -bool msa_format::load(io_generic *io, uint32_t form_factor, floppy_image *image) +bool msa_format::load(util::random_read &io, uint32_t form_factor, const std::vector<uint32_t> &variants, floppy_image &image) const { uint16_t sign, sect, heads, strack, etrack; read_header(io, sign, sect, heads, strack, etrack); @@ -228,7 +234,7 @@ bool msa_format::load(io_generic *io, uint32_t form_factor, floppy_image *image) uint8_t sectdata[11*512]; desc_s sectors[11]; for(int i=0; i<sect; i++) { - sectors[i].data = sectdata + 512*1; + sectors[i].data = sectdata + 512*i; sectors[i].size = 512; sectors[i].sector_id = i + 1; } @@ -239,10 +245,10 @@ bool msa_format::load(io_generic *io, uint32_t form_factor, floppy_image *image) for(int track=strack; track <= etrack; track++) { for(int head=0; head <= heads; head++) { uint8_t th[2]; - io_generic_read(io, th, pos, 2); + read_at(io, pos, th, 2); // FIXME: check for errors and premature EOF pos += 2; - int tsize = (th[0] << 8) | th[1]; - io_generic_read(io, sectdata, pos, tsize); + int tsize = get_u16be(th); + read_at(io, pos, sectdata, tsize); // FIXME: check for errors and premature EOF pos += tsize; if(tsize < track_size) { if(!uncompress(sectdata, tsize, track_size)) @@ -253,12 +259,12 @@ bool msa_format::load(io_generic *io, uint32_t form_factor, floppy_image *image) } } - image->set_variant(floppy_image::DSDD); + image.set_variant(floppy_image::DSDD); return true; } -bool msa_format::save(io_generic *io, floppy_image *image) +bool msa_format::save(util::random_read_write &io, const std::vector<uint32_t> &variants, const floppy_image &image) const { int track_count, head_count, sector_count; get_geometry_mfm_pc(image, 2000, track_count, head_count, sector_count); @@ -289,9 +295,9 @@ bool msa_format::save(io_generic *io, floppy_image *image) header[8] = 0; header[9] = track_count-1; - io_generic_write(io, header, 0, 10); - - int pos = 10; + if(io.seek(0, SEEK_SET)) + return false; + write(io, header, 10); // FIXME: check for errors uint8_t sectdata[11*512]; uint8_t compdata[11*512]; @@ -303,18 +309,14 @@ bool msa_format::save(io_generic *io, floppy_image *image) int csize; if(compress(sectdata, track_size, compdata, csize)) { uint8_t th[2]; - th[0] = csize >> 8; - th[1] = csize; - io_generic_write(io, th, pos, 2); - io_generic_write(io, compdata, pos+2, csize); - pos += 2+csize; + put_u16be(th, csize); + write(io, th, 2); // FIXME: check for errors + write(io, compdata, csize); // FIXME: check for errors } else { uint8_t th[2]; - th[0] = track_size >> 8; - th[1] = track_size; - io_generic_write(io, th, pos, 2); - io_generic_write(io, sectdata, pos+2, track_size); - pos += 2+track_size; + put_u16be(th, track_size); + write(io, th, 2); // FIXME: check for errors + write(io, sectdata, track_size); // FIXME: check for errors } } } @@ -322,5 +324,5 @@ bool msa_format::save(io_generic *io, floppy_image *image) return true; } -const floppy_format_type FLOPPY_ST_FORMAT = &floppy_image_format_creator<st_format>; -const floppy_format_type FLOPPY_MSA_FORMAT = &floppy_image_format_creator<msa_format>; +const st_format FLOPPY_ST_FORMAT; +const msa_format FLOPPY_MSA_FORMAT; |