From 58b16c545906f70b257fcf6f317628aad3517e6a Mon Sep 17 00:00:00 2001
From: B3n30 <benediktthomas@gmail.com>
Date: Tue, 13 Feb 2018 11:48:26 +0100
Subject: [PATCH 1/5] File_Sys: Add a size dependend delay for each file read

---
 src/core/file_sys/archive_selfncch.cpp | 11 +++++++++++
 src/core/file_sys/disk_archive.cpp     | 14 ++++++++++++++
 src/core/file_sys/disk_archive.h       |  1 +
 src/core/file_sys/file_backend.h       | 19 +++++++++++++++++++
 src/core/file_sys/ivfc_archive.cpp     | 11 +++++++++++
 src/core/file_sys/ivfc_archive.h       |  1 +
 src/core/hle/service/fs/archive.cpp    |  8 ++++++++
 7 files changed, 65 insertions(+)

diff --git a/src/core/file_sys/archive_selfncch.cpp b/src/core/file_sys/archive_selfncch.cpp
index 0e6a6d61b..32d5fff23 100644
--- a/src/core/file_sys/archive_selfncch.cpp
+++ b/src/core/file_sys/archive_selfncch.cpp
@@ -56,6 +56,17 @@ public:
         return ERROR_UNSUPPORTED_OPEN_FLAGS;
     }
 
+    u64 GetReadDelayNs(size_t length) const {
+        // The delay was measured on O3DS and O2DS with
+        // https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
+        // from the results the average of each length was taken.
+        static constexpr u64 slope(94);
+        static constexpr u64 offset(582778);
+        static constexpr u64 minimum(663124);
+        u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
+        return IPCDelayNanoseconds;
+    }
+
     u64 GetSize() const override {
         return data->size();
     }
diff --git a/src/core/file_sys/disk_archive.cpp b/src/core/file_sys/disk_archive.cpp
index 677f92467..0fae8301f 100644
--- a/src/core/file_sys/disk_archive.cpp
+++ b/src/core/file_sys/disk_archive.cpp
@@ -36,6 +36,20 @@ ResultVal<size_t> DiskFile::Write(const u64 offset, const size_t length, const b
     return MakeResult<size_t>(written);
 }
 
+u64 DiskFile::GetReadDelayNs(size_t length) const {
+    // TODO(B3N30): figure out the time a 3ds needs for those write
+    // for that backend.
+    // For now take the results from the romfs test.
+    // The delay was measured on O3DS and O2DS with
+    // https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
+    // from the results the average of each length was taken.
+    static constexpr u64 slope(183);
+    static constexpr u64 offset(524879);
+    static constexpr u64 minimum(631826);
+    u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
+    return IPCDelayNanoseconds;
+}
+
 u64 DiskFile::GetSize() const {
     return file->GetSize();
 }
diff --git a/src/core/file_sys/disk_archive.h b/src/core/file_sys/disk_archive.h
index 912d264e8..2c22da661 100644
--- a/src/core/file_sys/disk_archive.h
+++ b/src/core/file_sys/disk_archive.h
@@ -29,6 +29,7 @@ public:
 
     ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
     ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override;
+    u64 GetReadDelayNs(size_t length) const override;
     u64 GetSize() const override;
     bool SetSize(u64 size) const override;
     bool Close() const override;
diff --git a/src/core/file_sys/file_backend.h b/src/core/file_sys/file_backend.h
index 009ee139a..003e24429 100644
--- a/src/core/file_sys/file_backend.h
+++ b/src/core/file_sys/file_backend.h
@@ -4,6 +4,7 @@
 
 #pragma once
 
+#include <algorithm>
 #include <cstddef>
 #include "common/common_types.h"
 #include "core/hle/result.h"
@@ -37,6 +38,24 @@ public:
      */
     virtual ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) = 0;
 
+    /**
+     * Get the amount of time a 3ds needs to read those data
+     * @param length Length in bytes of data read from file
+     * @return Nanoseconds for the delay
+     */
+    virtual u64 GetReadDelayNs(size_t length) const {
+        // Return the default delay for the case that the subclass backend didn't
+        // implement one. We take the one measured for romfs reads
+        // This should be removed as soon as every subclass backend
+        // has one implemented
+        LOG_WARNING(Service_FS, "Using default delay for read");
+        static constexpr u64 slope(94);
+        static constexpr u64 offset(582778);
+        static constexpr u64 minimum(663124);
+        u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
+        return IPCDelayNanoseconds;
+    }
+
     /**
      * Get the size of the file in bytes
      * @return Size of the file in bytes
diff --git a/src/core/file_sys/ivfc_archive.cpp b/src/core/file_sys/ivfc_archive.cpp
index 7cd09ed07..18b260487 100644
--- a/src/core/file_sys/ivfc_archive.cpp
+++ b/src/core/file_sys/ivfc_archive.cpp
@@ -107,6 +107,17 @@ ResultVal<size_t> IVFCFile::Write(const u64 offset, const size_t length, const b
     return MakeResult<size_t>(0);
 }
 
+u64 IVFCFile::GetReadDelayNs(size_t length) const {
+    // The delay was measured on O3DS and O2DS with
+    // https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
+    // from the results the average of each length was taken.
+    static constexpr u64 slope(94);
+    static constexpr u64 offset(582778);
+    static constexpr u64 minimum(663124);
+    u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
+    return IPCDelayNanoseconds;
+}
+
 u64 IVFCFile::GetSize() const {
     return data_size;
 }
diff --git a/src/core/file_sys/ivfc_archive.h b/src/core/file_sys/ivfc_archive.h
index 768084036..a6f26199c 100644
--- a/src/core/file_sys/ivfc_archive.h
+++ b/src/core/file_sys/ivfc_archive.h
@@ -54,6 +54,7 @@ public:
 
     ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
     ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override;
+    u64 GetReadDelayNs(size_t length) const override;
     u64 GetSize() const override;
     bool SetSize(u64 size) const override;
     bool Close() const override {
diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp
index db304812c..3370302dd 100644
--- a/src/core/hle/service/fs/archive.cpp
+++ b/src/core/hle/service/fs/archive.cpp
@@ -31,6 +31,7 @@
 #include "core/hle/ipc_helpers.h"
 #include "core/hle/kernel/client_port.h"
 #include "core/hle/kernel/client_session.h"
+#include "core/hle/kernel/event.h"
 #include "core/hle/kernel/handle_table.h"
 #include "core/hle/kernel/server_session.h"
 #include "core/hle/result.h"
@@ -121,6 +122,13 @@ void File::Read(Kernel::HLERequestContext& ctx) {
         rb.Push<u32>(*read);
     }
     rb.PushMappedBuffer(buffer);
+
+    u64 read_timeout_ns = backend->GetReadDelayNs(length);
+    ctx.SleepClientThread(Kernel::GetCurrentThread(), "file::read", read_timeout_ns,
+                          [](Kernel::SharedPtr<Kernel::Thread> thread,
+                             Kernel::HLERequestContext& ctx, ThreadWakeupReason reason) {
+                              // Nothing to do here
+                          });
 }
 
 void File::Write(Kernel::HLERequestContext& ctx) {

From 06a7676ed1241046546df9d516dabd0a445aed48 Mon Sep 17 00:00:00 2001
From: B3n30 <benediktthomas@gmail.com>
Date: Sat, 24 Feb 2018 14:14:54 +0100
Subject: [PATCH 2/5] Add DelayGenerator for all file backends

---
 src/core/CMakeLists.txt                   |  1 +
 src/core/file_sys/archive_extsavedata.cpp | 23 ++++++++++--
 src/core/file_sys/archive_ncch.cpp        | 12 +++++--
 src/core/file_sys/archive_ncch.h          |  4 +--
 src/core/file_sys/archive_sdmc.cpp        | 18 +++++++++-
 src/core/file_sys/archive_selfncch.cpp    | 33 ++++++++---------
 src/core/file_sys/delay_generator.h       | 29 +++++++++++++++
 src/core/file_sys/disk_archive.cpp        | 14 --------
 src/core/file_sys/disk_archive.h          |  5 +--
 src/core/file_sys/file_backend.h          | 22 ++++++------
 src/core/file_sys/ivfc_archive.cpp        | 21 ++++-------
 src/core/file_sys/ivfc_archive.h          | 44 +++++++++++++++++++++--
 src/core/file_sys/savedata_archive.cpp    | 17 ++++++++-
 13 files changed, 172 insertions(+), 71 deletions(-)
 create mode 100644 src/core/file_sys/delay_generator.h

diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index f838b6b04..4afeec200 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -57,6 +57,7 @@ add_library(core STATIC
     file_sys/disk_archive.h
     file_sys/errors.h
     file_sys/file_backend.h
+    file_sys/delay_generator.h
     file_sys/ivfc_archive.cpp
     file_sys/ivfc_archive.h
     file_sys/ncch_container.cpp
diff --git a/src/core/file_sys/archive_extsavedata.cpp b/src/core/file_sys/archive_extsavedata.cpp
index 77a735292..4066461fe 100644
--- a/src/core/file_sys/archive_extsavedata.cpp
+++ b/src/core/file_sys/archive_extsavedata.cpp
@@ -27,7 +27,9 @@ namespace FileSys {
  */
 class FixSizeDiskFile : public DiskFile {
 public:
-    FixSizeDiskFile(FileUtil::IOFile&& file, const Mode& mode) : DiskFile(std::move(file), mode) {
+    FixSizeDiskFile(FileUtil::IOFile&& file, const Mode& mode,
+                    std::unique_ptr<DelayGenerator> delay_generator_)
+        : DiskFile(std::move(file), mode, std::move(delay_generator_)) {
         size = GetSize();
     }
 
@@ -53,6 +55,20 @@ private:
     u64 size{};
 };
 
+class ExtSaveDataDelayGenerator : public DelayGenerator {
+public:
+    u64 GetReadDelayNs(size_t length) override {
+        // This is the delay measured for a savedate read,
+        // not for extsaveData
+        // For now we will take that
+        static constexpr u64 slope(183);
+        static constexpr u64 offset(524879);
+        static constexpr u64 minimum(631826);
+        u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
+        return IPCDelayNanoseconds;
+    }
+};
+
 /**
  * Archive backend for general extsave data archive type.
  * The behaviour of ExtSaveDataArchive is almost the same as SaveDataArchive, except for
@@ -118,7 +134,10 @@ public:
         Mode rwmode;
         rwmode.write_flag.Assign(1);
         rwmode.read_flag.Assign(1);
-        auto disk_file = std::make_unique<FixSizeDiskFile>(std::move(file), rwmode);
+        std::unique_ptr<DelayGenerator> delay_generator =
+            std::make_unique<ExtSaveDataDelayGenerator>();
+        auto disk_file =
+            std::make_unique<FixSizeDiskFile>(std::move(file), rwmode, std::move(delay_generator));
         return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file));
     }
 
diff --git a/src/core/file_sys/archive_ncch.cpp b/src/core/file_sys/archive_ncch.cpp
index 2c4ed2d45..6dd1f384a 100644
--- a/src/core/file_sys/archive_ncch.cpp
+++ b/src/core/file_sys/archive_ncch.cpp
@@ -77,14 +77,17 @@ ResultVal<std::unique_ptr<FileBackend>> NCCHArchive::OpenFile(const Path& path,
         u64 romfs_size = 0;
 
         result = ncch_container.ReadRomFS(romfs_file, romfs_offset, romfs_size);
-        file = std::make_unique<IVFCFile>(std::move(romfs_file), romfs_offset, romfs_size);
+        std::unique_ptr<DelayGenerator> delay_generator = std::make_unique<RomFSDelayGenerator>();
+        file = std::make_unique<IVFCFile>(std::move(romfs_file), romfs_offset, romfs_size,
+                                          std::move(delay_generator));
     } else if (filepath_type == NCCHFilePathType::Code ||
                filepath_type == NCCHFilePathType::ExeFS) {
         std::vector<u8> buffer;
 
         // Load NCCH .code or icon/banner/logo
         result = ncch_container.LoadSectionExeFS(openfile_path.exefs_filepath.data(), buffer);
-        file = std::make_unique<NCCHFile>(std::move(buffer));
+        std::unique_ptr<DelayGenerator> delay_generator = std::make_unique<ExeFSDelayGenerator>();
+        file = std::make_unique<NCCHFile>(std::move(buffer), std::move(delay_generator));
     } else {
         LOG_ERROR(Service_FS, "Unknown NCCH archive type %u!", openfile_path.filepath_type);
         result = Loader::ResultStatus::Error;
@@ -194,7 +197,10 @@ u64 NCCHArchive::GetFreeBytes() const {
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 
-NCCHFile::NCCHFile(std::vector<u8> buffer) : file_buffer(std::move(buffer)) {}
+NCCHFile::NCCHFile(std::vector<u8> buffer, std::unique_ptr<DelayGenerator> delay_generator_)
+    : file_buffer(std::move(buffer)) {
+    delay_generator = std::move(delay_generator_);
+}
 
 ResultVal<size_t> NCCHFile::Read(const u64 offset, const size_t length, u8* buffer) const {
     LOG_TRACE(Service_FS, "called offset=%" PRIu64 ", length=%zu", offset, length);
diff --git a/src/core/file_sys/archive_ncch.h b/src/core/file_sys/archive_ncch.h
index f2ba888d8..c62d27f7c 100644
--- a/src/core/file_sys/archive_ncch.h
+++ b/src/core/file_sys/archive_ncch.h
@@ -17,7 +17,7 @@ namespace Service {
 namespace FS {
 enum class MediaType : u32;
 }
-}
+} // namespace Service
 
 namespace FileSys {
 
@@ -51,7 +51,7 @@ protected:
 // File backend for NCCH files
 class NCCHFile : public FileBackend {
 public:
-    explicit NCCHFile(std::vector<u8> buffer);
+    explicit NCCHFile(std::vector<u8> buffer, std::unique_ptr<DelayGenerator> delay_generator_);
 
     ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
     ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override;
diff --git a/src/core/file_sys/archive_sdmc.cpp b/src/core/file_sys/archive_sdmc.cpp
index fe3dce5d4..0d0c725e9 100644
--- a/src/core/file_sys/archive_sdmc.cpp
+++ b/src/core/file_sys/archive_sdmc.cpp
@@ -17,6 +17,20 @@
 
 namespace FileSys {
 
+class SDMCDelayGenerator : public DelayGenerator {
+public:
+    u64 GetReadDelayNs(size_t length) override {
+        // This is the delay measured on O3DS and O2DS with
+        // https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
+        // from the results the average of each length was taken.
+        static constexpr u64 slope(183);
+        static constexpr u64 offset(524879);
+        static constexpr u64 minimum(631826);
+        u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
+        return IPCDelayNanoseconds;
+    }
+};
+
 ResultVal<std::unique_ptr<FileBackend>> SDMCArchive::OpenFile(const Path& path,
                                                               const Mode& mode) const {
     Mode modified_mode;
@@ -82,7 +96,8 @@ ResultVal<std::unique_ptr<FileBackend>> SDMCArchive::OpenFileBase(const Path& pa
         return ERROR_NOT_FOUND;
     }
 
-    auto disk_file = std::make_unique<DiskFile>(std::move(file), mode);
+    std::unique_ptr<DelayGenerator> delay_generator = std::make_unique<SDMCDelayGenerator>();
+    auto disk_file = std::make_unique<DiskFile>(std::move(file), mode, std::move(delay_generator));
     return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file));
 }
 
@@ -343,6 +358,7 @@ u64 SDMCArchive::GetFreeBytes() const {
 
 ArchiveFactory_SDMC::ArchiveFactory_SDMC(const std::string& sdmc_directory)
     : sdmc_directory(sdmc_directory) {
+
     LOG_DEBUG(Service_FS, "Directory %s set as SDMC.", sdmc_directory.c_str());
 }
 
diff --git a/src/core/file_sys/archive_selfncch.cpp b/src/core/file_sys/archive_selfncch.cpp
index 32d5fff23..e5864953d 100644
--- a/src/core/file_sys/archive_selfncch.cpp
+++ b/src/core/file_sys/archive_selfncch.cpp
@@ -56,17 +56,6 @@ public:
         return ERROR_UNSUPPORTED_OPEN_FLAGS;
     }
 
-    u64 GetReadDelayNs(size_t length) const {
-        // The delay was measured on O3DS and O2DS with
-        // https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
-        // from the results the average of each length was taken.
-        static constexpr u64 slope(94);
-        static constexpr u64 offset(582778);
-        static constexpr u64 minimum(663124);
-        u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
-        return IPCDelayNanoseconds;
-    }
-
     u64 GetSize() const override {
         return data->size();
     }
@@ -182,8 +171,11 @@ public:
 private:
     ResultVal<std::unique_ptr<FileBackend>> OpenRomFS() const {
         if (ncch_data.romfs_file) {
-            return MakeResult<std::unique_ptr<FileBackend>>(std::make_unique<IVFCFile>(
-                ncch_data.romfs_file, ncch_data.romfs_offset, ncch_data.romfs_size));
+            std::unique_ptr<DelayGenerator> delay_generator =
+                std::make_unique<RomFSDelayGenerator>();
+            return MakeResult<std::unique_ptr<FileBackend>>(
+                std::make_unique<IVFCFile>(ncch_data.romfs_file, ncch_data.romfs_offset,
+                                           ncch_data.romfs_size, std::move(delay_generator)));
         } else {
             LOG_INFO(Service_FS, "Unable to read RomFS");
             return ERROR_ROMFS_NOT_FOUND;
@@ -192,9 +184,11 @@ private:
 
     ResultVal<std::unique_ptr<FileBackend>> OpenUpdateRomFS() const {
         if (ncch_data.update_romfs_file) {
+            std::unique_ptr<DelayGenerator> delay_generator =
+                std::make_unique<RomFSDelayGenerator>();
             return MakeResult<std::unique_ptr<FileBackend>>(std::make_unique<IVFCFile>(
                 ncch_data.update_romfs_file, ncch_data.update_romfs_offset,
-                ncch_data.update_romfs_size));
+                ncch_data.update_romfs_size, std::move(delay_generator)));
         } else {
             LOG_INFO(Service_FS, "Unable to read update RomFS");
             return ERROR_ROMFS_NOT_FOUND;
@@ -251,8 +245,9 @@ void ArchiveFactory_SelfNCCH::Register(Loader::AppLoader& app_loader) {
               program_id);
 
     if (ncch_data.find(program_id) != ncch_data.end()) {
-        LOG_WARNING(Service_FS, "Registering program %016" PRIX64
-                                " with SelfNCCH will override existing mapping",
+        LOG_WARNING(Service_FS,
+                    "Registering program %016" PRIX64
+                    " with SelfNCCH will override existing mapping",
                     program_id);
     }
 
@@ -266,9 +261,9 @@ void ArchiveFactory_SelfNCCH::Register(Loader::AppLoader& app_loader) {
     }
 
     std::shared_ptr<FileUtil::IOFile> update_romfs_file;
-    if (Loader::ResultStatus::Success ==
-        app_loader.ReadUpdateRomFS(update_romfs_file, data.update_romfs_offset,
-                                   data.update_romfs_size)) {
+    if (Loader::ResultStatus::Success == app_loader.ReadUpdateRomFS(update_romfs_file,
+                                                                    data.update_romfs_offset,
+                                                                    data.update_romfs_size)) {
 
         data.update_romfs_file = std::move(update_romfs_file);
     }
diff --git a/src/core/file_sys/delay_generator.h b/src/core/file_sys/delay_generator.h
new file mode 100644
index 000000000..410f60ee8
--- /dev/null
+++ b/src/core/file_sys/delay_generator.h
@@ -0,0 +1,29 @@
+// Copyright 2018 Citra Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+namespace FileSys {
+
+class DelayGenerator {
+public:
+    virtual u64 GetReadDelayNs(size_t length) = 0;
+
+    // TODO (B3N30): Add getter for all other file/directory io operations
+};
+
+class DefaultDelayGenerator : public DelayGenerator {
+public:
+    u64 GetReadDelayNs(size_t length) override {
+        // This is the delay measured for a romfs read.
+        // For now we will take that as a default
+        static constexpr u64 slope(94);
+        static constexpr u64 offset(582778);
+        static constexpr u64 minimum(663124);
+        u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
+        return IPCDelayNanoseconds;
+    }
+};
+
+} // namespace FileSys
diff --git a/src/core/file_sys/disk_archive.cpp b/src/core/file_sys/disk_archive.cpp
index 0fae8301f..677f92467 100644
--- a/src/core/file_sys/disk_archive.cpp
+++ b/src/core/file_sys/disk_archive.cpp
@@ -36,20 +36,6 @@ ResultVal<size_t> DiskFile::Write(const u64 offset, const size_t length, const b
     return MakeResult<size_t>(written);
 }
 
-u64 DiskFile::GetReadDelayNs(size_t length) const {
-    // TODO(B3N30): figure out the time a 3ds needs for those write
-    // for that backend.
-    // For now take the results from the romfs test.
-    // The delay was measured on O3DS and O2DS with
-    // https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
-    // from the results the average of each length was taken.
-    static constexpr u64 slope(183);
-    static constexpr u64 offset(524879);
-    static constexpr u64 minimum(631826);
-    u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
-    return IPCDelayNanoseconds;
-}
-
 u64 DiskFile::GetSize() const {
     return file->GetSize();
 }
diff --git a/src/core/file_sys/disk_archive.h b/src/core/file_sys/disk_archive.h
index 2c22da661..0de3747a1 100644
--- a/src/core/file_sys/disk_archive.h
+++ b/src/core/file_sys/disk_archive.h
@@ -22,14 +22,15 @@ namespace FileSys {
 
 class DiskFile : public FileBackend {
 public:
-    DiskFile(FileUtil::IOFile&& file_, const Mode& mode_)
+    DiskFile(FileUtil::IOFile&& file_, const Mode& mode_,
+             std::unique_ptr<DelayGenerator> delay_generator_)
         : file(new FileUtil::IOFile(std::move(file_))) {
+        delay_generator = std::move(delay_generator_);
         mode.hex = mode_.hex;
     }
 
     ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
     ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override;
-    u64 GetReadDelayNs(size_t length) const override;
     u64 GetSize() const override;
     bool SetSize(u64 size) const override;
     bool Close() const override;
diff --git a/src/core/file_sys/file_backend.h b/src/core/file_sys/file_backend.h
index 003e24429..182e6e7b1 100644
--- a/src/core/file_sys/file_backend.h
+++ b/src/core/file_sys/file_backend.h
@@ -8,6 +8,7 @@
 #include <cstddef>
 #include "common/common_types.h"
 #include "core/hle/result.h"
+#include "delay_generator.h"
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 // FileSys namespace
@@ -43,17 +44,13 @@ public:
      * @param length Length in bytes of data read from file
      * @return Nanoseconds for the delay
      */
-    virtual u64 GetReadDelayNs(size_t length) const {
-        // Return the default delay for the case that the subclass backend didn't
-        // implement one. We take the one measured for romfs reads
-        // This should be removed as soon as every subclass backend
-        // has one implemented
-        LOG_WARNING(Service_FS, "Using default delay for read");
-        static constexpr u64 slope(94);
-        static constexpr u64 offset(582778);
-        static constexpr u64 minimum(663124);
-        u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
-        return IPCDelayNanoseconds;
+    u64 GetReadDelayNs(size_t length) {
+        if (delay_generator != nullptr) {
+            return delay_generator->GetReadDelayNs(length);
+        }
+        LOG_ERROR(Service_FS, "Delay generator was not initalized. Using default");
+        delay_generator = std::make_unique<DefaultDelayGenerator>();
+        return delay_generator->GetReadDelayNs(length);
     }
 
     /**
@@ -79,6 +76,9 @@ public:
      * Flushes the file
      */
     virtual void Flush() const = 0;
+
+protected:
+    std::unique_ptr<DelayGenerator> delay_generator;
 };
 
 } // namespace FileSys
diff --git a/src/core/file_sys/ivfc_archive.cpp b/src/core/file_sys/ivfc_archive.cpp
index 18b260487..15c2cc383 100644
--- a/src/core/file_sys/ivfc_archive.cpp
+++ b/src/core/file_sys/ivfc_archive.cpp
@@ -23,8 +23,9 @@ std::string IVFCArchive::GetName() const {
 
 ResultVal<std::unique_ptr<FileBackend>> IVFCArchive::OpenFile(const Path& path,
                                                               const Mode& mode) const {
+    std::unique_ptr<DelayGenerator> delay_generator = std::make_unique<IVFCDelayGenerator>();
     return MakeResult<std::unique_ptr<FileBackend>>(
-        std::make_unique<IVFCFile>(romfs_file, data_offset, data_size));
+        std::make_unique<IVFCFile>(romfs_file, data_offset, data_size, std::move(delay_generator)));
 }
 
 ResultCode IVFCArchive::DeleteFile(const Path& path) const {
@@ -89,8 +90,11 @@ u64 IVFCArchive::GetFreeBytes() const {
 
 ////////////////////////////////////////////////////////////////////////////////////////////////////
 
-IVFCFile::IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size)
-    : romfs_file(std::move(file)), data_offset(offset), data_size(size) {}
+IVFCFile::IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size,
+                   std::unique_ptr<DelayGenerator> delay_generator_)
+    : romfs_file(std::move(file)), data_offset(offset), data_size(size) {
+    delay_generator = std::move(delay_generator_);
+}
 
 ResultVal<size_t> IVFCFile::Read(const u64 offset, const size_t length, u8* buffer) const {
     LOG_TRACE(Service_FS, "called offset=%llu, length=%zu", offset, length);
@@ -107,17 +111,6 @@ ResultVal<size_t> IVFCFile::Write(const u64 offset, const size_t length, const b
     return MakeResult<size_t>(0);
 }
 
-u64 IVFCFile::GetReadDelayNs(size_t length) const {
-    // The delay was measured on O3DS and O2DS with
-    // https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
-    // from the results the average of each length was taken.
-    static constexpr u64 slope(94);
-    static constexpr u64 offset(582778);
-    static constexpr u64 minimum(663124);
-    u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
-    return IPCDelayNanoseconds;
-}
-
 u64 IVFCFile::GetSize() const {
     return data_size;
 }
diff --git a/src/core/file_sys/ivfc_archive.h b/src/core/file_sys/ivfc_archive.h
index a6f26199c..d21379b57 100644
--- a/src/core/file_sys/ivfc_archive.h
+++ b/src/core/file_sys/ivfc_archive.h
@@ -19,6 +19,46 @@
 
 namespace FileSys {
 
+class IVFCDelayGenerator : public DelayGenerator {
+    u64 GetReadDelayNs(size_t length) override {
+        // This is the delay measured for a romfs read.
+        // For now we will take that as a default
+        static constexpr u64 slope(94);
+        static constexpr u64 offset(582778);
+        static constexpr u64 minimum(663124);
+        u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
+        return IPCDelayNanoseconds;
+    }
+};
+
+class RomFSDelayGenerator : public DelayGenerator {
+public:
+    u64 GetReadDelayNs(size_t length) override {
+        // The delay was measured on O3DS and O2DS with
+        // https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
+        // from the results the average of each length was taken.
+        static constexpr u64 slope(94);
+        static constexpr u64 offset(582778);
+        static constexpr u64 minimum(663124);
+        u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
+        return IPCDelayNanoseconds;
+    }
+};
+
+class ExeFSDelayGenerator : public DelayGenerator {
+public:
+    u64 GetReadDelayNs(size_t length) override {
+        // The delay was measured on O3DS and O2DS with
+        // https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
+        // from the results the average of each length was taken.
+        static constexpr u64 slope(94);
+        static constexpr u64 offset(582778);
+        static constexpr u64 minimum(663124);
+        u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
+        return IPCDelayNanoseconds;
+    }
+};
+
 /**
  * Helper which implements an interface to deal with IVFC images used in some archives
  * This should be subclassed by concrete archive types, which will provide the
@@ -50,11 +90,11 @@ protected:
 
 class IVFCFile : public FileBackend {
 public:
-    IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size);
+    IVFCFile(std::shared_ptr<FileUtil::IOFile> file, u64 offset, u64 size,
+             std::unique_ptr<DelayGenerator> delay_generator_);
 
     ResultVal<size_t> Read(u64 offset, size_t length, u8* buffer) const override;
     ResultVal<size_t> Write(u64 offset, size_t length, bool flush, const u8* buffer) override;
-    u64 GetReadDelayNs(size_t length) const override;
     u64 GetSize() const override;
     bool SetSize(u64 size) const override;
     bool Close() const override {
diff --git a/src/core/file_sys/savedata_archive.cpp b/src/core/file_sys/savedata_archive.cpp
index f8f811ba0..52a935cbc 100644
--- a/src/core/file_sys/savedata_archive.cpp
+++ b/src/core/file_sys/savedata_archive.cpp
@@ -13,6 +13,20 @@
 
 namespace FileSys {
 
+class SaveDataDelayGenerator : public DelayGenerator {
+public:
+    u64 GetReadDelayNs(size_t length) override {
+        // The delay was measured on O3DS and O2DS with
+        // https://gist.github.com/B3n30/ac40eac20603f519ff106107f4ac9182
+        // from the results the average of each length was taken.
+        static constexpr u64 slope(183);
+        static constexpr u64 offset(524879);
+        static constexpr u64 minimum(631826);
+        u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
+        return IPCDelayNanoseconds;
+    }
+};
+
 ResultVal<std::unique_ptr<FileBackend>> SaveDataArchive::OpenFile(const Path& path,
                                                                   const Mode& mode) const {
     LOG_DEBUG(Service_FS, "called path=%s mode=%01X", path.DebugStr().c_str(), mode.hex);
@@ -67,7 +81,8 @@ ResultVal<std::unique_ptr<FileBackend>> SaveDataArchive::OpenFile(const Path& pa
         return ERROR_FILE_NOT_FOUND;
     }
 
-    auto disk_file = std::make_unique<DiskFile>(std::move(file), mode);
+    std::unique_ptr<DelayGenerator> delay_generator = std::make_unique<SaveDataDelayGenerator>();
+    auto disk_file = std::make_unique<DiskFile>(std::move(file), mode, std::move(delay_generator));
     return MakeResult<std::unique_ptr<FileBackend>>(std::move(disk_file));
 }
 

From 143878a4744a4d5a09a207f530c32c435a34b045 Mon Sep 17 00:00:00 2001
From: B3n30 <benediktthomas@gmail.com>
Date: Sat, 24 Feb 2018 14:25:02 +0100
Subject: [PATCH 3/5] Rebased usage of SleepClientThread

---
 src/core/hle/service/fs/archive.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/core/hle/service/fs/archive.cpp b/src/core/hle/service/fs/archive.cpp
index 3370302dd..1fdd66d7e 100644
--- a/src/core/hle/service/fs/archive.cpp
+++ b/src/core/hle/service/fs/archive.cpp
@@ -123,7 +123,7 @@ void File::Read(Kernel::HLERequestContext& ctx) {
     }
     rb.PushMappedBuffer(buffer);
 
-    u64 read_timeout_ns = backend->GetReadDelayNs(length);
+    std::chrono::nanoseconds read_timeout_ns{backend->GetReadDelayNs(length)};
     ctx.SleepClientThread(Kernel::GetCurrentThread(), "file::read", read_timeout_ns,
                           [](Kernel::SharedPtr<Kernel::Thread> thread,
                              Kernel::HLERequestContext& ctx, ThreadWakeupReason reason) {

From d8ac3a3435cd487e21172e5ca7f260d5a84d04d2 Mon Sep 17 00:00:00 2001
From: B3n30 <benediktthomas@gmail.com>
Date: Sat, 24 Feb 2018 14:46:42 +0100
Subject: [PATCH 4/5] Fix include

---
 src/core/file_sys/file_backend.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/core/file_sys/file_backend.h b/src/core/file_sys/file_backend.h
index 182e6e7b1..0f2a8c730 100644
--- a/src/core/file_sys/file_backend.h
+++ b/src/core/file_sys/file_backend.h
@@ -6,6 +6,7 @@
 
 #include <algorithm>
 #include <cstddef>
+#include <memory>
 #include "common/common_types.h"
 #include "core/hle/result.h"
 #include "delay_generator.h"

From 739f8e5367471e58ac0410fa4b92eaecd4ffa3e7 Mon Sep 17 00:00:00 2001
From: James Rowe <jroweboy@gmail.com>
Date: Sun, 4 Mar 2018 20:59:42 -0700
Subject: [PATCH 5/5] Fix clang format

---
 src/core/file_sys/archive_extsavedata.cpp |  5 +++--
 src/core/file_sys/archive_selfncch.cpp    | 11 +++++------
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/src/core/file_sys/archive_extsavedata.cpp b/src/core/file_sys/archive_extsavedata.cpp
index 4066461fe..1dbe787c7 100644
--- a/src/core/file_sys/archive_extsavedata.cpp
+++ b/src/core/file_sys/archive_extsavedata.cpp
@@ -64,8 +64,9 @@ public:
         static constexpr u64 slope(183);
         static constexpr u64 offset(524879);
         static constexpr u64 minimum(631826);
-        u64 IPCDelayNanoseconds = std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
-        return IPCDelayNanoseconds;
+        u64 ipc_delay_nanoseconds =
+            std::max<u64>(static_cast<u64>(length) * slope + offset, minimum);
+        return ipc_delay_nanoseconds;
     }
 };
 
diff --git a/src/core/file_sys/archive_selfncch.cpp b/src/core/file_sys/archive_selfncch.cpp
index e5864953d..08237d9ef 100644
--- a/src/core/file_sys/archive_selfncch.cpp
+++ b/src/core/file_sys/archive_selfncch.cpp
@@ -245,9 +245,8 @@ void ArchiveFactory_SelfNCCH::Register(Loader::AppLoader& app_loader) {
               program_id);
 
     if (ncch_data.find(program_id) != ncch_data.end()) {
-        LOG_WARNING(Service_FS,
-                    "Registering program %016" PRIX64
-                    " with SelfNCCH will override existing mapping",
+        LOG_WARNING(Service_FS, "Registering program %016" PRIX64
+                                " with SelfNCCH will override existing mapping",
                     program_id);
     }
 
@@ -261,9 +260,9 @@ void ArchiveFactory_SelfNCCH::Register(Loader::AppLoader& app_loader) {
     }
 
     std::shared_ptr<FileUtil::IOFile> update_romfs_file;
-    if (Loader::ResultStatus::Success == app_loader.ReadUpdateRomFS(update_romfs_file,
-                                                                    data.update_romfs_offset,
-                                                                    data.update_romfs_size)) {
+    if (Loader::ResultStatus::Success ==
+        app_loader.ReadUpdateRomFS(update_romfs_file, data.update_romfs_offset,
+                                   data.update_romfs_size)) {
 
         data.update_romfs_file = std::move(update_romfs_file);
     }