Skip to content

Commit

Permalink
[PC] merge lib/media/atari
Browse files Browse the repository at this point in the history
  • Loading branch information
a8jan committed Dec 20, 2023
1 parent 434861d commit 0842c1e
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 18 deletions.
8 changes: 4 additions & 4 deletions fujinet_pc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ set(SOURCES
# lib/media/apple/mediaTypeDSK.h lib/media/apple/mediaTypeDSK.cpp
# lib/media/apple/mediaTypePO.h lib/media/apple/mediaTypePO.cpp
# lib/media/apple/mediaTypeWOZ.h lib/media/apple/mediaTypeWOZ.cpp
# lib/media/atari/diskType.h lib/media/atari/diskType.cpp
# lib/media/atari/diskTypeAtr.h lib/media/atari/diskTypeAtr.cpp
# lib/media/atari/diskTypeAtx.h
# lib/media/atari/diskTypeXex.h lib/media/atari/diskTypeXex.cpp
lib/media/atari/diskType.h lib/media/atari/diskType.cpp
lib/media/atari/diskTypeAtr.h lib/media/atari/diskTypeAtr.cpp
lib/media/atari/diskTypeAtx.h
lib/media/atari/diskTypeXex.h lib/media/atari/diskTypeXex.cpp
lib/base64/base64.h lib/base64/base64.c
lib/encrypt/crypt.h lib/encrypt/crypt.cpp
lib/compat/compat_inet.c
Expand Down
4 changes: 4 additions & 0 deletions lib/media/atari/diskType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ void MediaType::unmount()
{
if (_disk_fileh != nullptr)
{
#ifdef ESP_PLATFORM
fclose(_disk_fileh);
#else
_disk_fileh->close();
#endif
_disk_fileh = nullptr;
}
}
Expand Down
8 changes: 6 additions & 2 deletions lib/media/atari/diskType.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#ifndef _MEDIATYPE_
#define _MEDIATYPE_

// #include <stdio.h>
#include <fujiHost.h>
#include <stdint.h>

#ifdef ESP_PLATFORM
#include <stdio.h>
#else
#include "fnFile.h"
#endif

#include "fujiHost.h"

#define INVALID_SECTOR_VALUE 65536

Expand Down
69 changes: 66 additions & 3 deletions lib/media/atari/diskTypeAtr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <memory.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include "../../include/debug.h"

Expand Down Expand Up @@ -52,7 +54,7 @@ uint32_t MediaTypeATR::_sector_to_offset(uint16_t sectorNum)
// Returns TRUE if an error condition occurred
bool MediaTypeATR::read(uint16_t sectornum, uint16_t *readcount)
{
Debug_print("ATR READ\r\n");
Debug_printf("ATR READ %d / %d\r\n", sectornum, _disk_num_sectors);

*readcount = 0;

Expand All @@ -72,11 +74,19 @@ bool MediaTypeATR::read(uint16_t sectornum, uint16_t *readcount)
if (sectornum != _disk_last_sector + 1)
{
uint32_t offset = _sector_to_offset(sectornum);
#ifdef ESP_PLATFORM
err = fseek(_disk_fileh, offset, SEEK_SET) != 0;
#else
err = _disk_fileh->seek(offset, SEEK_SET) != 0;
#endif
}

if (err == false)
#ifdef ESP_PLATFORM
err = fread(_disk_sectorbuff, 1, sectorSize, _disk_fileh) != sectorSize;
#else
err = _disk_fileh->read(_disk_sectorbuff, 1, sectorSize) != sectorSize;
#endif

if (err == false)
_disk_last_sector = sectornum;
Expand All @@ -96,12 +106,16 @@ bool inHighScoreRange(int minimum, int maximum, int val)
// Returns TRUE if an error condition occurred
bool MediaTypeATR::write(uint16_t sectornum, bool verify)
{
#ifdef ESP_PLATFORM
FILE *oldFileh, *hsFileh;
#else
FileHandler *oldFileh, *hsFileh;
#endif

oldFileh = nullptr;
hsFileh = nullptr;

Debug_printf("ATR WRITE\r\n", sectornum, _disk_num_sectors);
Debug_printf("ATR WRITE %d / %d\r\n", sectornum, _disk_num_sectors);

// Return an error if we're trying to write beyond the end of the disk
if (sectornum > _disk_num_sectors)
Expand All @@ -120,7 +134,11 @@ bool MediaTypeATR::write(uint16_t sectornum, bool verify)
else
{
oldFileh = _disk_fileh;
#ifdef ESP_PLATFORM
hsFileh = _disk_host->file_open(_disk_filename, _disk_filename, strlen(_disk_filename) + 1, "r+");
#else
hsFileh = _disk_host->filehandler_open(_disk_filename, _disk_filename, strlen(_disk_filename) + 1, "rb+");
#endif
_disk_fileh = hsFileh;
}
}
Expand All @@ -133,31 +151,47 @@ bool MediaTypeATR::write(uint16_t sectornum, bool verify)
int e;
if (sectornum != _disk_last_sector + 1)
{
#ifdef ESP_PLATFORM
e = fseek(_disk_fileh, offset, SEEK_SET);
#else
e = _disk_fileh->seek(offset, SEEK_SET);
#endif
if (e != 0)
{
Debug_printf("::write seek error %d\r\n", e);
return true;
}
}
// Write the data
#ifdef ESP_PLATFORM
e = fwrite(_disk_sectorbuff, 1, sectorSize, _disk_fileh);
#else
e = _disk_fileh->write(_disk_sectorbuff, 1, sectorSize);
#endif
if (e != sectorSize)
{
Debug_printf("::write error %d, %d\r\n", e, errno);
return true;
}

#ifdef ESP_PLATFORM
int ret = fflush(_disk_fileh); // This doesn't seem to be connected to anything in ESP-IDF VF, so it may not do anything
ret = fsync(fileno(_disk_fileh)); // Since we might get reset at any moment, go ahead and sync the file (not clear if fflush does this)
#else
int ret = _disk_fileh->flush();
#endif
Debug_printf("ATR::write fsync:%d\r\n", ret);

if (_high_score_sector != 0)
{
Debug_printf("Closing high score sector.\r\n");

if (hsFileh != nullptr)
#ifdef ESP_PLATFORM
fclose(hsFileh);
#else
hsFileh->close();
#endif

_disk_fileh = oldFileh;
_disk_last_sector = INVALID_SECTOR_VALUE; // force a cache invalidate.
Expand Down Expand Up @@ -218,7 +252,11 @@ bool MediaTypeATR::format(uint16_t *responsesize)
07-0F have two possible interpretations but are no critical for our use
*/
#ifdef ESP_PLATFORM
mediatype_t MediaTypeATR::mount(FILE *f, uint32_t disksize)
#else
mediatype_t MediaTypeATR::mount(FileHandler *f, uint32_t disksize)
#endif
{
Debug_print("ATR MOUNT\r\n");

Expand All @@ -230,12 +268,20 @@ mediatype_t MediaTypeATR::mount(FILE *f, uint32_t disksize)

// Get file and sector size from header
int i;
#ifdef ESP_PLATFORM
if ((i = fseek(f, 0, SEEK_SET)) < 0)
#else
if ((i = f->seek(0, SEEK_SET)) < 0)
#endif
{
Debug_printf("failed seeking to header on disk image (%d, %d)\r\n", i, errno);
return _disktype;
}
#ifdef ESP_PLATFORM
if ((i = fread(buf, 1, sizeof(buf), f)) != sizeof(buf))
#else
if ((i = f->read(buf, 1, sizeof(buf))) != sizeof(buf))
#endif
{
Debug_printf("failed reading header bytes (%d, %d)\r\n", i, errno);
return _disktype;
Expand Down Expand Up @@ -280,7 +326,11 @@ mediatype_t MediaTypeATR::mount(FILE *f, uint32_t disksize)
}

// Returns FALSE on error
#ifdef ESP_PLATFORM
bool MediaTypeATR::create(FILE *f, uint16_t sectorSize, uint16_t numSectors)
#else
bool MediaTypeATR::create(FileHandler *f, uint16_t sectorSize, uint16_t numSectors)
#endif
{
Debug_print("ATR CREATE\r\n");

Expand Down Expand Up @@ -328,7 +378,11 @@ bool MediaTypeATR::create(FILE *f, uint16_t sectorSize, uint16_t numSectors)
Debug_printf("Write header to ATR: sec_size=%d, sectors=%d, paragraphs=%d, bytes=%d\r\n",
sectorSize, numSectors, num_paragraphs, total_size);

#ifdef ESP_PLATFORM
uint32_t offset = fwrite(&atrHeader, 1, sizeof(atrHeader), f);
#else
uint32_t offset = f->write(&atrHeader, 1, sizeof(atrHeader));
#endif

// Write first three 128 uint8_t sectors
uint8_t blank[512] = {0};
Expand All @@ -337,10 +391,14 @@ bool MediaTypeATR::create(FILE *f, uint16_t sectorSize, uint16_t numSectors)
{
for (int i = 0; i < 3; i++)
{
#ifdef ESP_PLATFORM
size_t out = fwrite(blank, 1, 128, f);
#else
size_t out = f->write(blank, 1, 128);
#endif
if (out != 128)
{
Debug_printf("Error writing sector %hhu\r\n", i);
Debug_printf("Error writing sector %d\r\n", i);
return false;
}
offset += 128;
Expand All @@ -350,8 +408,13 @@ bool MediaTypeATR::create(FILE *f, uint16_t sectorSize, uint16_t numSectors)

// Write the rest of the sectors via sparse seek to the last sector
offset += (numSectors * sectorSize) - sectorSize;
#ifdef ESP_PLATFORM
fseek(f, offset, SEEK_SET);
size_t out = fwrite(blank, 1, sectorSize, f);
#else
f->seek(offset, SEEK_SET);
size_t out = f->write(blank, 1, sectorSize);
#endif

if (out != sectorSize)
{
Expand Down
14 changes: 14 additions & 0 deletions lib/media/atari/diskTypeXex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "diskTypeXex.h"

#include <cstring>
#include <stdlib.h>
#include <memory.h>

#include "../../include/debug.h"

Expand Down Expand Up @@ -174,13 +176,21 @@ bool MediaTypeXEX::read(uint16_t sectornum, uint16_t *readcount)
if (sectornum != _disk_last_sector + 1)
{
Debug_printf("seeking to offset %d in XEX\r\n", xex_offset);
#ifdef ESP_PLATFORM
err = fseek(_disk_fileh, xex_offset, SEEK_SET) != 0;
#else
err = _disk_fileh->seek(xex_offset, SEEK_SET) != 0;
#endif
}

if (err == false)
{
Debug_printf("requesting %d bytes from XEX\r\n", data_bytes);
#ifdef ESP_PLATFORM
int read = fread(_disk_sectorbuff, 1, data_bytes, _disk_fileh);
#else
int read = _disk_fileh->read(_disk_sectorbuff, 1, data_bytes);
#endif
Debug_printf("received %d bytes\r\n", read);

// Fill in the sector link data pointing to the next sector
Expand Down Expand Up @@ -225,7 +235,11 @@ MediaTypeXEX::~MediaTypeXEX()
unmount();
}

#ifdef ESP_PLATFORM
mediatype_t MediaTypeXEX::mount(FILE *f, uint32_t disksize)
#else
mediatype_t MediaTypeXEX::mount(FileHandler *f, uint32_t disksize)
#endif
{
Debug_print("XEX MOUNT\r\n");

Expand Down
12 changes: 3 additions & 9 deletions pc_merge_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,19 +292,13 @@

- TODO: lib/media/apple/mediaTypeWOZ.h lib/media/apple/mediaTypeWOZ.cpp

- **WIP**: lib/media/atari/diskType.h lib/media/atari/diskType.cpp
- **DONE** / merge: lib/media/atari/diskType.h lib/media/atari/diskType.cpp

diskType.h

- **WIP**: lib/media/atari/diskTypeAtr.h lib/media/atari/diskTypeAtr.cpp

diskTypeAtr.h
- **DONE** / merge: lib/media/atari/diskTypeAtr.h lib/media/atari/diskTypeAtr.cpp

- **DONE** / merge: lib/media/atari/diskTypeAtx.h

- **WIP**: lib/media/atari/diskTypeXex.h lib/media/atari/diskTypeXex.cpp

diskTypeXex.h
- **DONE** / merge: lib/media/atari/diskTypeXex.h lib/media/atari/diskTypeXex.cpp

- **DONE** / identical: lib/base64/base64.h lib/base64/base64.c

Expand Down

0 comments on commit 0842c1e

Please sign in to comment.