2019-02-20 12:50:51 +00:00
|
|
|
#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
|
|
|
|
|
2022-01-07 17:53:20 +00:00
|
|
|
#define WOZMAGIC 0xAA
|
2019-02-20 12:50:51 +00:00
|
|
|
|
2021-01-23 02:07:43 +00:00
|
|
|
WozSerializer::WozSerializer() : Woz(false,0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
WozSerializer::~WozSerializer()
|
2020-06-27 22:00:59 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-06-28 03:09:43 +00:00
|
|
|
const char *WozSerializer::diskName()
|
|
|
|
{
|
|
|
|
if (fd != -1) {
|
|
|
|
return g_filemanager->fileName(fd);
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2019-02-20 12:50:51 +00:00
|
|
|
bool WozSerializer::Serialize(int8_t fd)
|
|
|
|
{
|
2020-06-28 12:29:26 +00:00
|
|
|
// 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
|
2022-01-07 17:53:20 +00:00
|
|
|
|
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);
|
2022-01-07 17:53:20 +00:00
|
|
|
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;
|
2019-02-20 12:50:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-01-07 17:53:20 +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;
|
2019-02-20 12:50:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-22 04:06:08 +00:00
|
|
|
bool WozSerializer::flush()
|
|
|
|
{
|
|
|
|
// Flush the entire disk image if it's dirty. We could make this
|
|
|
|
// smarter later.
|
2022-01-07 17:53:20 +00:00
|
|
|
// FIXME hard-coded number of tracks?
|
|
|
|
bool trackDirty = false;
|
|
|
|
for (int i=0; i<160; i++) {
|
|
|
|
if (tracks[i].dirty)
|
|
|
|
trackDirty = true;
|
|
|
|
}
|
|
|
|
if (!trackDirty) {
|
2021-01-22 04:06:08 +00:00
|
|
|
return true;
|
2022-01-07 17:53:20 +00:00
|
|
|
}
|
2021-01-22 04:06:08 +00:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2022-01-07 17:53:20 +00:00
|
|
|
g_filemanager->flush();
|
2021-01-22 04:06:08 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|