summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib
diff options
context:
space:
mode:
author AJR <ajrhacker@users.noreply.github.com>2021-01-01 19:11:47 -0500
committer AJR <ajrhacker@users.noreply.github.com>2021-01-01 19:15:51 -0500
commit0a5148e05a5b8de3d806cbb6a63af2121895fa7d (patch)
tree17f8635fe944bb4215a80d7cc7ab2db4dd490f8a /src/lib
parent29bc7e25b71d9af40943c0dc9d24fead65af4ece (diff)
Cassette image processing cleanup
- Add cassette_image::image_read_byte method for reading one byte at a time - coco_cas.cpp: Eliminate dependency on emucore.h - thom_cas.cpp: Declare some temporary variables much closer to where they are used - tvc_cas.cpp: Read and write entire sectors at a time
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/formats/aquarius_caq.cpp8
-rw-r--r--src/lib/formats/atom_tap.cpp18
-rw-r--r--src/lib/formats/cassimg.cpp9
-rw-r--r--src/lib/formats/cassimg.h1
-rw-r--r--src/lib/formats/coco_cas.cpp48
-rw-r--r--src/lib/formats/sc3000_bit.cpp8
-rw-r--r--src/lib/formats/sord_cas.cpp13
-rw-r--r--src/lib/formats/thom_cas.cpp33
-rw-r--r--src/lib/formats/tvc_cas.cpp39
9 files changed, 75 insertions, 102 deletions
diff --git a/src/lib/formats/aquarius_caq.cpp b/src/lib/formats/aquarius_caq.cpp
index 6a94b587939..16460d2eac8 100644
--- a/src/lib/formats/aquarius_caq.cpp
+++ b/src/lib/formats/aquarius_caq.cpp
@@ -52,19 +52,17 @@ static cassette_image::error aquarius_caq_load(cassette_image *cassette)
{
cassette_image::error err;
uint64_t image_size = cassette->image_size();
- uint64_t image_pos = 0;
double time_index = 0.0;
double time_displacement;
- uint8_t data;
/* silence */
err = cassette->put_sample(0, time_index, 0.5, 0);
if (err != cassette_image::error::SUCCESS) return err;
time_index += 0.5;
- while (image_pos < image_size)
+ for (uint64_t image_pos = 0; image_pos < image_size; image_pos++)
{
- cassette->image_read(&data, image_pos, 1);
+ uint8_t data = cassette->image_read_byte(image_pos);
/* start bit */
MODULATE(0);
@@ -78,8 +76,6 @@ static cassette_image::error aquarius_caq_load(cassette_image *cassette)
/* stop bits */
MODULATE(1);
MODULATE(1);
-
- image_pos++;
}
/* silence */
diff --git a/src/lib/formats/atom_tap.cpp b/src/lib/formats/atom_tap.cpp
index cfe83bc1bae..02a2416f856 100644
--- a/src/lib/formats/atom_tap.cpp
+++ b/src/lib/formats/atom_tap.cpp
@@ -68,17 +68,6 @@ static const cassette_image::Modulation atom_tap_modulation =
};
/*-------------------------------------------------
- cassette_image_read_uint8 - read tape data
--------------------------------------------------*/
-
-static uint8_t cassette_image_read_uint8(cassette_image *cassette, uint64_t offset)
-{
- uint8_t data;
- cassette->image_read(&data, offset, 1);
- return data;
-}
-
-/*-------------------------------------------------
atom_tap_identify - identify cassette
-------------------------------------------------*/
@@ -104,13 +93,12 @@ static cassette_image::error atom_tap_load(cassette_image *cassette)
{
cassette_image::error err;
uint64_t image_size = cassette->image_size();
- uint64_t image_pos = 0;
double time_index = 0.0;
double time_displacement;
- while (image_pos < image_size)
+ for (uint64_t image_pos = 0; image_pos < image_size; image_pos++)
{
- uint8_t data = cassette_image_read_uint8(cassette, image_pos);
+ uint8_t data = cassette->image_read_byte(image_pos);
/* start bit */
MODULATE(0);
@@ -124,8 +112,6 @@ static cassette_image::error atom_tap_load(cassette_image *cassette)
/* stop bits */
MODULATE(1);
MODULATE(1);
-
- image_pos++;
}
return cassette_image::error::SUCCESS;
diff --git a/src/lib/formats/cassimg.cpp b/src/lib/formats/cassimg.cpp
index 25b060e0311..2c46e61c485 100644
--- a/src/lib/formats/cassimg.cpp
+++ b/src/lib/formats/cassimg.cpp
@@ -355,6 +355,15 @@ void cassette_image::image_read(void *buffer, uint64_t offset, size_t length)
+uint8_t cassette_image::image_read_byte(uint64_t offset)
+{
+ uint8_t data;
+ io_generic_read(&m_io, &data, offset, 1);
+ return data;
+}
+
+
+
void cassette_image::image_write(const void *buffer, uint64_t offset, size_t length)
{
io_generic_write(&m_io, buffer, offset, length);
diff --git a/src/lib/formats/cassimg.h b/src/lib/formats/cassimg.h
index ed71c49cbf5..09f6aaa0099 100644
--- a/src/lib/formats/cassimg.h
+++ b/src/lib/formats/cassimg.h
@@ -140,6 +140,7 @@ public:
// calls for accessing the raw cassette image
void image_read(void *buffer, uint64_t offset, size_t length);
+ uint8_t image_read_byte(uint64_t offset);
void image_write(const void *buffer, uint64_t offset, size_t length);
uint64_t image_size();
diff --git a/src/lib/formats/coco_cas.cpp b/src/lib/formats/coco_cas.cpp
index c0ed5d616ff..a7afc1eeacf 100644
--- a/src/lib/formats/coco_cas.cpp
+++ b/src/lib/formats/coco_cas.cpp
@@ -35,7 +35,7 @@
#include "coco_cas.h"
-#include "emucore.h" // PAIR
+#include <cassert>
#define COCO_WAVESAMPLES_HEADER (1.0)
@@ -45,8 +45,6 @@
//some games load with only 5s, but most games need 15s
#define ALICE32_WAVESAMPLES_HEADER (15.0)
-static int synccount;
-
const cassette_image::Modulation coco_cas_modulation =
{
cassette_image::MODULATION_SINEWAVE,
@@ -63,59 +61,56 @@ static cassette_image::error coco_cas_identify(cassette_image *cassette, cassett
-static bool get_cas_block(cassette_image *cassette, uint64_t *offset, uint8_t *block, int *block_len)
+static bool get_cas_block(cassette_image *cassette, uint64_t &offset, uint8_t *block, int &block_len, int &synccount)
{
uint8_t block_length = 0;
uint8_t block_checksum = 0;
- uint64_t current_offset;
- uint64_t image_size;
- PAIR p;
- int i;
int state = 0;
int phase = 0;
synccount = 0;
- p.w.l = 0;
- image_size = cassette->image_size();
- current_offset = *offset;
+ uint16_t p = 0;
+ uint16_t image_size = cassette->image_size();
- while(current_offset < image_size)
+ for(uint64_t current_offset = offset; current_offset < image_size; )
{
- cassette->image_read(&p.b.h, current_offset, 1);
+ assert((p & 0xFF00) == 0);
+ p |= cassette->image_read_byte(current_offset) << 8;
current_offset++;
- for (i = 0; i < 8; i++)
+ for (int i = 0; i < 8; i++)
{
- p.w.l >>= 1;
+ p >>= 1;
if (state == 0)
{
/* searching for a block */
- if (p.b.l == 0x3C)
+ if ((p & 0xFF) == 0x3C)
{
/* found one! */
phase = i;
state++;
}
- else if (p.b.l == 0x55)
+ else if ((p & 0xFF) == 0x55)
{
synccount++;
}
}
else if (i == phase)
{
- *(block++) = p.b.l;
+ uint8_t b = p & 0xFF;
+ *(block++) = b;
switch(state) {
case 1:
/* found file type */
- block_checksum = p.b.l;
+ block_checksum = b;
state++;
break;
case 2:
/* found file size */
- block_length = p.b.l;
- *block_len = ((int) block_length) + 3;
- block_checksum += p.b.l;
+ block_length = b;
+ block_len = ((int) block_length) + 3;
+ block_checksum += b;
state++;
break;
@@ -124,12 +119,12 @@ static bool get_cas_block(cassette_image *cassette, uint64_t *offset, uint8_t *b
if (block_length)
{
block_length--;
- block_checksum += p.b.l;
+ block_checksum += b;
}
else
{
/* end of block */
- if (p.b.l != block_checksum)
+ if (b != block_checksum)
{
/* checksum failure */
return false;
@@ -137,7 +132,7 @@ static bool get_cas_block(cassette_image *cassette, uint64_t *offset, uint8_t *b
else
{
/* checksum success */
- *offset = current_offset;
+ offset = current_offset;
return true;
}
}
@@ -163,6 +158,7 @@ static cassette_image::error cas_load(cassette_image *cassette, uint8_t silence)
double time_index = 0.0;
double time_displacement;
static const uint8_t magic_bytes[2] = { 0x55, 0x3C };
+ int synccount;
#if 0
{
@@ -206,7 +202,7 @@ static cassette_image::error cas_load(cassette_image *cassette, uint8_t silence)
image_size = cassette->image_size();
/* try to find a block that we can untangle */
- while(get_cas_block(cassette, &offset, block, &block_length))
+ while(get_cas_block(cassette, offset, block, block_length, synccount))
{
/* Forcing a silence before a filename block, improves the ability to load some */
/* copy protected Dragon games, e.g. Rommel's Revenge */
diff --git a/src/lib/formats/sc3000_bit.cpp b/src/lib/formats/sc3000_bit.cpp
index ab0828e709a..e1ee814e147 100644
--- a/src/lib/formats/sc3000_bit.cpp
+++ b/src/lib/formats/sc3000_bit.cpp
@@ -57,14 +57,12 @@ static cassette_image::error sc3000_bit_load(cassette_image *cassette)
{
cassette_image::error err;
uint64_t image_size = cassette->image_size();
- uint64_t image_pos = 0;
double time_index = 0.0;
double time_displacement;
- uint8_t data;
- while (image_pos < image_size)
+ for (uint64_t image_pos = 0; image_pos < image_size; image_pos++)
{
- cassette->image_read(&data, image_pos, 1);
+ uint8_t data = cassette->image_read_byte(image_pos);
switch (data)
{
@@ -82,8 +80,6 @@ static cassette_image::error sc3000_bit_load(cassette_image *cassette)
time_index += 1/1200.0;
break;
}
-
- image_pos++;
}
return cassette_image::error::SUCCESS;
diff --git a/src/lib/formats/sord_cas.cpp b/src/lib/formats/sord_cas.cpp
index f90daafd248..600751088d0 100644
--- a/src/lib/formats/sord_cas.cpp
+++ b/src/lib/formats/sord_cas.cpp
@@ -25,13 +25,6 @@ static const cassette_image::Modulation sordm5_cas_modulation =
3150.0 - 600, 3150.0, 3150.0 + 600
};
-static uint8_t cassette_image_read_uint8( cassette_image *cassette, uint64_t offset)
-{
- uint8_t data;
- cassette->image_read(&data, offset, 1);
- return data;
-}
-
static cassette_image::error sordm5_tap_identify( cassette_image *cassette, cassette_image::Options *opts)
{
return cassette->modulation_identify(sordm5_cas_modulation, opts);
@@ -64,10 +57,10 @@ static cassette_image::error sordm5_tap_load( cassette_image *cassette)
while (image_pos < image_size)
{
// read block type
- block_type = cassette_image_read_uint8(cassette, image_pos + 0);
+ block_type = cassette->image_read_byte(image_pos + 0);
if ((block_type != 'H') && (block_type != 'D')) return cassette_image::error::INVALID_IMAGE;
// read block size
- block_size = cassette_image_read_uint8(cassette, image_pos + 1);
+ block_size = cassette->image_read_byte(image_pos + 1);
if (block_size == 0) block_size = 0x100;
block_size += 3;
// add silence (header block)
@@ -86,7 +79,7 @@ static cassette_image::error sordm5_tap_load( cassette_image *cassette)
for (i=0;i<block_size;i++)
{
// read byte
- byte = cassette_image_read_uint8(cassette, image_pos + i);
+ byte = cassette->image_read_byte(image_pos + i);
// calc/check checksum
#if 0
if (i == block_size)
diff --git a/src/lib/formats/thom_cas.cpp b/src/lib/formats/thom_cas.cpp
index 0d0cc4fc4eb..2d038369489 100644
--- a/src/lib/formats/thom_cas.cpp
+++ b/src/lib/formats/thom_cas.cpp
@@ -110,7 +110,7 @@ static cassette_image::error to7_k7_load( cassette_image *cass )
#endif
size_t size = cass->image_size( ), pos = 0;
int i, sz, sz2, bitmax = 1024, invalid = 0;
- uint8_t in, typ, block[264];
+ uint8_t typ, block[264];
LOG (( "to7_k7_load: start conversion, size=%li\n", (long)size ));
PRINT (( "to7_k7_load: open cassette, length: %li bytes\n", (long) size ));
@@ -226,8 +226,7 @@ static cassette_image::error to7_k7_load( cassette_image *cass )
/* skip to first 0xff filler */
for ( sz = 0; pos < size; pos++, sz++ )
{
- cass->image_read( &in, pos, 1 );
- if ( in == 0xff )
+ if ( cass->image_read_byte( pos ) == 0xff )
break;
}
if ( sz > 0 )
@@ -240,7 +239,7 @@ static cassette_image::error to7_k7_load( cassette_image *cass )
/* skip 0xff filler */
for ( sz = 0; pos < size; pos++, sz++ )
{
- cass->image_read( &in, pos, 1 );
+ uint8_t in = cass->image_read_byte( pos );
/* actually, we are bit laxist and treat as 0xff bytes with at least
5 bits out of 8 set to 1
*/
@@ -322,17 +321,16 @@ static cassette_image::error to7_k7_load( cassette_image *cass )
/* put block */
for (; pos < size; pos++ )
{
- cass->image_read( &in, pos, 1 );
+ uint8_t in = cass->image_read_byte( pos );
for ( sz = 0; pos < size && in == 0xff; sz++ )
{
pos++;
- cass->image_read( &in, pos, 1 );
+ in = cass->image_read_byte( pos );
}
if ( invalid < 10 && sz > 4 && in == 0x01 && pos + 4 <= size )
{
- uint8_t in1,in2;
- cass->image_read( &in1, pos+1, 1 );
- cass->image_read( &in2, pos+2, 1 );
+ uint8_t in1 = cass->image_read_byte( pos+1 );
+ uint8_t in2 = cass->image_read_byte( pos+2 );
if ( (in1 == 0x3c) && ((in2 == 0x00) || (in2 == 0x01) ) )
{
/* seems we have a regular block hidden in raw data => rebounce */
@@ -463,7 +461,7 @@ static cassette_image::error mo5_k5_load( cassette_image *cass )
{
size_t size = cass->image_size( ), pos = 0;
int i, sz, sz2, hbit = 0;
- uint8_t in, in2, in3, typ, block[264], sum;
+ uint8_t typ, block[264], sum;
int invalid = 0, hbitsize = 0, dcmoto = 0;
LOG (( "mo5_k5_load: start conversion, size=%li\n", (long)size ));
@@ -580,8 +578,7 @@ static cassette_image::error mo5_k5_load( cassette_image *cass )
/* skip 0x01 filler */
for ( sz = 0; pos < size; pos++, sz++ )
{
- cass->image_read( &in, pos, 1 );
- if ( in != 0x01 )
+ if ( cass->image_read_byte(pos) != 0x01 )
break;
}
@@ -662,7 +659,7 @@ static cassette_image::error mo5_k5_load( cassette_image *cass )
LOG (( "mo5_k5_load: trailing trash off=$%x size=%i hbitstart=%i\n", (int) pos, (int) (size-pos), hbitsize ));
for ( ; pos < size; pos++ )
{
- cass->image_read( &in, pos, 1 );
+ uint8_t in = cass->image_read_byte( pos );
if ( dcmoto && in=='D' )
{
/* skip DCMOTO header*/
@@ -671,24 +668,24 @@ static cassette_image::error mo5_k5_load( cassette_image *cass )
{
LOG (( "mo5_k5_load: DCMOTO signature found at off=$%x\n", (int)pos ));
pos += 6;
- cass->image_read( &in, pos, 1 );
+ in = cass->image_read_byte( pos );
}
else if ( ! memcmp( block, "DCMO", 4 ) )
{
LOG (( "mo5_k5_load: DCMO* signature found at off=$%x\n", (int)pos ));
pos += 5;
- cass->image_read( &in, pos, 1 );
+ in = cass->image_read_byte( pos );
}
}
for ( sz = 0; pos < size && in == 0x01; sz++ )
{
pos++;
- cass->image_read( &in, pos, 1 );
+ in = cass->image_read_byte( pos );
}
if ( sz > 6 )
{
- cass->image_read( &in2, pos+1, 1 );
- cass->image_read( &in3, pos+2, 1 );
+ uint8_t in2 = cass->image_read_byte( pos+1 );
+ uint8_t in3 = cass->image_read_byte( pos+2 );
if ( invalid < 10 && in == 0x3c && in2 == 0x5a && (in3 == 0x00 || in3 == 0x01 || in3 == 0xff ) )
{
/* regular block found */
diff --git a/src/lib/formats/tvc_cas.cpp b/src/lib/formats/tvc_cas.cpp
index e6d05451d9d..8fe50cdfd14 100644
--- a/src/lib/formats/tvc_cas.cpp
+++ b/src/lib/formats/tvc_cas.cpp
@@ -28,19 +28,23 @@ static void tvc64_emit_level(cassette_image *cass, double &time, int freq, int l
time += period;
}
-static cassette_image::error tvc64_output_byte(cassette_image *cass, double &time, uint8_t byte)
+static cassette_image::error tvc64_output_bytes(cassette_image *cass, double &time, const uint8_t *buffer, int size)
{
- for (int i=0; i<8; i++)
+ while (size-- > 0)
{
- if ((byte>>i) & 0x01)
+ uint8_t byte = *buffer++;
+ for (int i=0; i<8; i++)
{
- tvc64_emit_level(cass, time, TVC64_BIT1_FREQ*2, +1);
- tvc64_emit_level(cass, time, TVC64_BIT1_FREQ*2, -1);
- }
- else
- {
- tvc64_emit_level(cass, time, TVC64_BIT0_FREQ*2, +1);
- tvc64_emit_level(cass, time, TVC64_BIT0_FREQ*2, -1);
+ if ((byte>>i) & 0x01)
+ {
+ tvc64_emit_level(cass, time, TVC64_BIT1_FREQ*2, +1);
+ tvc64_emit_level(cass, time, TVC64_BIT1_FREQ*2, -1);
+ }
+ else
+ {
+ tvc64_emit_level(cass, time, TVC64_BIT0_FREQ*2, +1);
+ tvc64_emit_level(cass, time, TVC64_BIT0_FREQ*2, -1);
+ }
}
}
@@ -132,8 +136,7 @@ static cassette_image::error tvc64_cassette_load(cassette_image *cassette)
tvc64_emit_level(cassette, time, TVC64_SYNC_FREQ*2, -1);
// header data
- for (int i=0; i<buff_idx; i++)
- tvc64_output_byte(cassette, time, tmp_buff[i]);
+ tvc64_output_bytes(cassette, time, tmp_buff, buff_idx);
// 5 post data cycles
tvc64_output_predata(cassette, time, 5);
@@ -171,8 +174,8 @@ static cassette_image::error tvc64_cassette_load(cassette_image *cassette)
// sector data
int sector_size = (i == sector_num) ? (cas_size % 256) : 256;
- for (int z=0; z < sector_size; z++)
- cassette->image_read(&tmp_buff[buff_idx++], TVC64_HEADER_BYTES + i*256 + z, 1);
+ cassette->image_read(&tmp_buff[buff_idx], TVC64_HEADER_BYTES + i*256, sector_size);
+ buff_idx += sector_size;
if (i == sector_num || ((i+1) == sector_num && (cas_size % 256 ) == 0))
tmp_buff[buff_idx++] = 0xff; // last sector
@@ -185,8 +188,7 @@ static cassette_image::error tvc64_cassette_load(cassette_image *cassette)
tmp_buff[buff_idx++] = (crc>>8) & 0xff;
// output the sector
- for (int z=0; z<buff_idx; z++)
- tvc64_output_byte(cassette, time, tmp_buff[z]);
+ tvc64_output_bytes(cassette, time, tmp_buff, buff_idx);
buff_idx = 0;
}
@@ -202,10 +204,7 @@ static cassette_image::error tvc64_cassette_load(cassette_image *cassette)
static cassette_image::error tvc64_cassette_identify(cassette_image *cassette, cassette_image::Options *opts)
{
- uint8_t byte;
- cassette->image_read(&byte, 0, 1);
-
- if (byte == 0x11)
+ if (cassette->image_read_byte(0) == 0x11)
{
opts->bits_per_sample = 16;
opts->channels = 1;