2022-09-25 21:49:24 +00:00
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// X68000 EMULATOR "XM6"
|
|
|
|
|
//
|
|
|
|
|
// Copyright (C) 2001-2006 PI.(ytanaka@ipc-tokai.or.jp)
|
|
|
|
|
// Copyright (C) 2014-2020 GIMONS
|
|
|
|
|
//
|
|
|
|
|
// XM6i
|
|
|
|
|
// Copyright (C) 2010-2015 isaki@NetBSD.org
|
|
|
|
|
// Copyright (C) 2010 Y.Sugahara
|
|
|
|
|
//
|
|
|
|
|
// Imported sava's Anex86/T98Next image and MO format support patch.
|
|
|
|
|
// Comments translated to english by akuker.
|
|
|
|
|
//
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
#include "disk_track.h"
|
|
|
|
|
#include "disk_cache.h"
|
2022-10-08 17:26:04 +00:00
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <cassert>
|
2022-11-02 06:36:25 +00:00
|
|
|
|
#include <algorithm>
|
2022-09-25 21:49:24 +00:00
|
|
|
|
|
2022-10-25 08:29:57 +00:00
|
|
|
|
DiskCache::DiskCache(const string& path, int size, uint32_t blocks, off_t imgoff)
|
|
|
|
|
: sec_path(path), sec_size(size), sec_blocks(blocks), imgoffset(imgoff)
|
2022-09-25 21:49:24 +00:00
|
|
|
|
{
|
|
|
|
|
assert(blocks > 0);
|
|
|
|
|
assert(imgoff >= 0);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-30 12:32:45 +00:00
|
|
|
|
bool DiskCache::Save()
|
2022-09-25 21:49:24 +00:00
|
|
|
|
{
|
2022-11-02 06:36:25 +00:00
|
|
|
|
// Save valid tracks
|
2023-10-15 06:38:15 +00:00
|
|
|
|
return ranges::none_of(cache.begin(), cache.end(), [this](const cache_t& c)
|
2023-10-30 12:32:45 +00:00
|
|
|
|
{ return c.disktrk != nullptr && !c.disktrk->Save(sec_path, cache_miss_write_count); });
|
2022-09-25 21:49:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-10-01 15:56:06 +00:00
|
|
|
|
shared_ptr<DiskTrack> DiskCache::GetTrack(uint32_t block)
|
2022-09-25 21:49:24 +00:00
|
|
|
|
{
|
|
|
|
|
// Update first
|
|
|
|
|
UpdateSerialNumber();
|
|
|
|
|
|
|
|
|
|
// Calculate track (fixed to 256 sectors/track)
|
|
|
|
|
int track = block >> 8;
|
|
|
|
|
|
2022-10-01 15:56:06 +00:00
|
|
|
|
// Get track data
|
|
|
|
|
return Assign(track);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
|
bool DiskCache::ReadSector(span<uint8_t> buf, uint32_t block)
|
2022-10-01 15:56:06 +00:00
|
|
|
|
{
|
|
|
|
|
shared_ptr<DiskTrack> disktrk = GetTrack(block);
|
2022-09-25 21:49:24 +00:00
|
|
|
|
if (disktrk == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read the track data to the cache
|
|
|
|
|
return disktrk->ReadSector(buf, block & 0xff);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-15 06:38:15 +00:00
|
|
|
|
bool DiskCache::WriteSector(span<const uint8_t> buf, uint32_t block)
|
2022-09-25 21:49:24 +00:00
|
|
|
|
{
|
2022-10-01 15:56:06 +00:00
|
|
|
|
shared_ptr<DiskTrack> disktrk = GetTrack(block);
|
2022-09-25 21:49:24 +00:00
|
|
|
|
if (disktrk == nullptr) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write the data to the cache
|
|
|
|
|
return disktrk->WriteSector(buf, block & 0xff);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Track Assignment
|
|
|
|
|
//
|
|
|
|
|
//---------------------------------------------------------------------------
|
2022-10-01 15:56:06 +00:00
|
|
|
|
shared_ptr<DiskTrack> DiskCache::Assign(int track)
|
2022-09-25 21:49:24 +00:00
|
|
|
|
{
|
|
|
|
|
assert(sec_size != 0);
|
|
|
|
|
assert(track >= 0);
|
|
|
|
|
|
|
|
|
|
// First, check if it is already assigned
|
|
|
|
|
for (cache_t& c : cache) {
|
|
|
|
|
if (c.disktrk && c.disktrk->GetTrack() == track) {
|
|
|
|
|
// Track match
|
|
|
|
|
c.serial = serial;
|
|
|
|
|
return c.disktrk;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Next, check for empty
|
2022-10-01 15:56:06 +00:00
|
|
|
|
for (size_t i = 0; i < cache.size(); i++) {
|
|
|
|
|
if (cache[i].disktrk == nullptr) {
|
2022-09-25 21:49:24 +00:00
|
|
|
|
// Try loading
|
2022-11-02 06:36:25 +00:00
|
|
|
|
if (Load(static_cast<int>(i), track, nullptr)) {
|
2022-09-25 21:49:24 +00:00
|
|
|
|
// Success loading
|
|
|
|
|
cache[i].serial = serial;
|
|
|
|
|
return cache[i].disktrk;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load failed
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Finally, find the youngest serial number and delete it
|
|
|
|
|
|
|
|
|
|
// Set index 0 as candidate c
|
|
|
|
|
uint32_t s = cache[0].serial;
|
2022-10-01 15:56:06 +00:00
|
|
|
|
size_t c = 0;
|
2022-09-25 21:49:24 +00:00
|
|
|
|
|
|
|
|
|
// Compare candidate with serial and update to smaller one
|
2022-10-01 15:56:06 +00:00
|
|
|
|
for (size_t i = 0; i < cache.size(); i++) {
|
2022-09-25 21:49:24 +00:00
|
|
|
|
assert(cache[i].disktrk);
|
|
|
|
|
|
|
|
|
|
// Compare and update the existing serial
|
|
|
|
|
if (cache[i].serial < s) {
|
|
|
|
|
s = cache[i].serial;
|
|
|
|
|
c = i;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save this track
|
2023-10-30 12:32:45 +00:00
|
|
|
|
if (!cache[c].disktrk->Save(sec_path, cache_miss_write_count)) {
|
2022-09-25 21:49:24 +00:00
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete this track
|
2022-10-01 15:56:06 +00:00
|
|
|
|
shared_ptr<DiskTrack> disktrk = cache[c].disktrk;
|
|
|
|
|
cache[c].disktrk.reset();
|
2022-09-25 21:49:24 +00:00
|
|
|
|
|
2022-11-02 06:36:25 +00:00
|
|
|
|
if (Load(static_cast<int>(c), track, disktrk)) {
|
2022-09-25 21:49:24 +00:00
|
|
|
|
// Successful loading
|
|
|
|
|
cache[c].serial = serial;
|
|
|
|
|
return cache[c].disktrk;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Load failed
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
//
|
|
|
|
|
// Load cache
|
|
|
|
|
//
|
|
|
|
|
//---------------------------------------------------------------------------
|
2022-10-01 15:56:06 +00:00
|
|
|
|
bool DiskCache::Load(int index, int track, shared_ptr<DiskTrack> disktrk)
|
2022-09-25 21:49:24 +00:00
|
|
|
|
{
|
2022-11-02 06:36:25 +00:00
|
|
|
|
assert(index >= 0 && index < static_cast<int>(cache.size()));
|
2022-09-25 21:49:24 +00:00
|
|
|
|
assert(track >= 0);
|
2022-10-01 15:56:06 +00:00
|
|
|
|
assert(cache[index].disktrk == nullptr);
|
2022-09-25 21:49:24 +00:00
|
|
|
|
|
|
|
|
|
// Get the number of sectors on this track
|
|
|
|
|
int sectors = sec_blocks - (track << 8);
|
|
|
|
|
assert(sectors > 0);
|
|
|
|
|
if (sectors > 0x100) {
|
|
|
|
|
sectors = 0x100;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (disktrk == nullptr) {
|
2022-10-01 15:56:06 +00:00
|
|
|
|
disktrk = make_shared<DiskTrack>();
|
2022-09-25 21:49:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disktrk->Init(track, sec_size, sectors, cd_raw, imgoffset);
|
|
|
|
|
|
|
|
|
|
// Try loading
|
2023-10-30 12:32:45 +00:00
|
|
|
|
if (!disktrk->Load(sec_path, cache_miss_read_count)) {
|
|
|
|
|
++read_error_count;
|
|
|
|
|
|
2022-09-25 21:49:24 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Allocation successful, work set
|
|
|
|
|
cache[index].disktrk = disktrk;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void DiskCache::UpdateSerialNumber()
|
|
|
|
|
{
|
|
|
|
|
// Update and do nothing except 0
|
|
|
|
|
serial++;
|
|
|
|
|
if (serial != 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Clear serial of all caches
|
|
|
|
|
for (cache_t& c : cache) {
|
|
|
|
|
c.serial = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-30 12:32:45 +00:00
|
|
|
|
vector<PbStatistics> DiskCache::GetStatistics(bool is_read_only) const
|
|
|
|
|
{
|
|
|
|
|
vector<PbStatistics> statistics;
|
|
|
|
|
|
|
|
|
|
PbStatistics s;
|
|
|
|
|
|
|
|
|
|
s.set_category(PbStatisticsCategory::CATEGORY_INFO);
|
|
|
|
|
|
|
|
|
|
s.set_key(CACHE_MISS_READ_COUNT);
|
|
|
|
|
s.set_value(cache_miss_read_count);
|
|
|
|
|
statistics.push_back(s);
|
|
|
|
|
|
|
|
|
|
if (!is_read_only) {
|
|
|
|
|
s.set_key(CACHE_MISS_WRITE_COUNT);
|
|
|
|
|
s.set_value(cache_miss_write_count);
|
|
|
|
|
statistics.push_back(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
s.set_category(PbStatisticsCategory::CATEGORY_ERROR);
|
|
|
|
|
|
|
|
|
|
s.set_key(READ_ERROR_COUNT);
|
|
|
|
|
s.set_value(read_error_count);
|
|
|
|
|
statistics.push_back(s);
|
|
|
|
|
|
|
|
|
|
if (!is_read_only) {
|
|
|
|
|
s.set_key(WRITE_ERROR_COUNT);
|
|
|
|
|
s.set_value(write_error_count);
|
|
|
|
|
statistics.push_back(s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return statistics;
|
|
|
|
|
}
|