aiie/apple/woz-serializer.cpp

139 lines
2.7 KiB
C++
Raw Normal View History

#include "woz-serializer.h"
2020-06-28 03:09:43 +00:00
#include "globals.h"
2021-01-10 02:32:40 +00:00
#include "serialize.h"
#ifdef TEENSYDUINO
#include "iocompat.h"
#endif
#define WOZMAGIC 0xAA
2021-01-23 02:07:43 +00:00
WozSerializer::WozSerializer() : Woz(false,0)
{
}
WozSerializer::~WozSerializer()
{
}
2020-06-28 03:09:43 +00:00
const char *WozSerializer::diskName()
{
if (fd != -1) {
return g_filemanager->fileName(fd);
}
return "";
}
bool WozSerializer::Serialize(int8_t fd)
{
// If we're being asked to serialize, make sure we've flushed any data first
flush();
2022-01-09 03:49:42 +00:00
2021-01-10 02:32:40 +00:00
serializeMagic(WOZMAGIC);
2022-01-09 03:49:42 +00:00
// The disk image and its cache will be reloaded completely, so no
// need to serialize tracks[] or diskinfo or imageType or metadata
//
// autoFlushTrackData is a configuration option, does not need
2021-01-10 02:32:40 +00:00
serialize32(trackPointer);
serialize32(trackBitCounter);
serialize8(trackByte);
2022-01-09 03:49:42 +00:00
serialize8(trackByteFromDataTrack);
2021-01-10 02:32:40 +00:00
serialize8(trackBitIdx);
serialize8(trackLoopCounter);
2022-01-09 03:49:42 +00:00
serialize8(randData);
serialize8(randPtr);
serializeMagic(WOZMAGIC);
2022-01-09 03:49:42 +00:00
2020-06-28 03:09:43 +00:00
return true;
2021-01-10 02:32:40 +00:00
err:
return false;
}
bool WozSerializer::Deserialize(int8_t fd)
{
2020-06-28 03:09:43 +00:00
// Before deserializing, the caller has to re-load the right disk image!
2021-01-10 02:32:40 +00:00
deserializeMagic(WOZMAGIC);
2022-01-09 03:49:42 +00:00
2021-01-10 02:32:40 +00:00
deserialize32(trackPointer);
deserialize32(trackBitCounter);
deserialize8(trackByte);
2022-01-09 03:49:42 +00:00
deserialize8(trackByteFromDataTrack);
2021-01-10 02:32:40 +00:00
deserialize8(trackBitIdx);
deserialize8(trackLoopCounter);
2022-01-09 03:49:42 +00:00
deserialize8(randData);
deserialize8(randPtr);
2020-06-28 03:09:43 +00:00
deserializeMagic(WOZMAGIC);
2022-01-09 03:49:42 +00:00
2020-06-28 03:09:43 +00:00
return true;
2021-01-10 02:32:40 +00:00
err:
return false;
}
bool WozSerializer::flush()
{
// Flush the entire disk image if it's dirty. We could make this
// smarter later.
// FIXME hard-coded number of tracks?
bool trackDirty = false;
for (int i=0; i<160; i++) {
if (tracks[i].dirty)
trackDirty = true;
}
if (!trackDirty) {
return true;
}
// The fd should still be open. If it's not, then we can't flush.
if (fd == -1)
return false;
bool ret = true;
switch (imageType) {
case T_WOZ:
ret = writeWozFile(fd, imageType);
break;
case T_DSK:
case T_PO:
ret = writeDskFile(fd, imageType);
break;
case T_NIB:
ret = writeNibFile(fd);
break;
default:
fprintf(stderr, "Error: unknown imageType; can't flush\n");
ret = false;
break;
}
g_filemanager->flush();
return true;
}
bool WozSerializer::writeNextWozBit(uint8_t datatrack, uint8_t bit)
{
return Woz::writeNextWozBit(datatrack, bit);
}
bool WozSerializer::writeNextWozByte(uint8_t datatrack, uint8_t b)
{
return Woz::writeNextWozByte(datatrack, b);
}
uint8_t WozSerializer::nextDiskBit(uint8_t datatrack)
{
return Woz::nextDiskBit(datatrack);
}
uint8_t WozSerializer::nextDiskByte(uint8_t datatrack)
{
return Woz::nextDiskByte(datatrack);
}