2019-11-19 10:30:04 +00:00
|
|
|
#pragma once
|
2020-10-18 04:43:09 +00:00
|
|
|
#include "bus.h"
|
2019-11-19 10:30:04 +00:00
|
|
|
#include "common/bitfield.h"
|
|
|
|
#include "cpu_types.h"
|
2022-07-08 12:43:38 +00:00
|
|
|
#include "util/jit_code_buffer.h"
|
|
|
|
#include "util/page_fault_handler.h"
|
2019-11-19 10:30:04 +00:00
|
|
|
#include <array>
|
2020-10-18 04:43:55 +00:00
|
|
|
#include <map>
|
2019-11-19 10:30:04 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-10-18 04:43:55 +00:00
|
|
|
#ifdef WITH_RECOMPILER
|
|
|
|
#include "cpu_recompiler_types.h"
|
|
|
|
#endif
|
|
|
|
|
2019-11-19 10:30:04 +00:00
|
|
|
namespace CPU {
|
|
|
|
|
2019-11-27 15:55:33 +00:00
|
|
|
union CodeBlockKey
|
|
|
|
{
|
|
|
|
u32 bits;
|
|
|
|
|
|
|
|
BitField<u32, bool, 0, 1> user_mode;
|
|
|
|
BitField<u32, u32, 2, 30> aligned_pc;
|
|
|
|
|
|
|
|
ALWAYS_INLINE u32 GetPC() const { return aligned_pc << 2; }
|
|
|
|
ALWAYS_INLINE void SetPC(u32 pc) { aligned_pc = pc >> 2; }
|
|
|
|
|
2019-12-12 13:34:53 +00:00
|
|
|
ALWAYS_INLINE u32 GetPCPhysicalAddress() const { return (aligned_pc << 2) & PHYSICAL_MEMORY_ADDRESS_MASK; }
|
|
|
|
|
2019-11-27 15:55:33 +00:00
|
|
|
ALWAYS_INLINE CodeBlockKey& operator=(const CodeBlockKey& rhs)
|
|
|
|
{
|
|
|
|
bits = rhs.bits;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool operator==(const CodeBlockKey& rhs) const { return bits == rhs.bits; }
|
|
|
|
ALWAYS_INLINE bool operator!=(const CodeBlockKey& rhs) const { return bits != rhs.bits; }
|
|
|
|
ALWAYS_INLINE bool operator<(const CodeBlockKey& rhs) const { return bits < rhs.bits; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CodeBlockInstruction
|
|
|
|
{
|
|
|
|
Instruction instruction;
|
|
|
|
u32 pc;
|
|
|
|
|
|
|
|
bool is_branch_instruction : 1;
|
2021-03-05 15:33:12 +00:00
|
|
|
bool is_direct_branch_instruction : 1;
|
2020-11-18 10:34:25 +00:00
|
|
|
bool is_unconditional_branch_instruction : 1;
|
2019-11-27 15:55:33 +00:00
|
|
|
bool is_branch_delay_slot : 1;
|
|
|
|
bool is_load_instruction : 1;
|
|
|
|
bool is_store_instruction : 1;
|
|
|
|
bool is_load_delay_slot : 1;
|
|
|
|
bool is_last_instruction : 1;
|
|
|
|
bool has_load_delay : 1;
|
|
|
|
bool can_trap : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct CodeBlock
|
|
|
|
{
|
2020-07-31 07:09:18 +00:00
|
|
|
using HostCodePointer = void (*)();
|
2019-11-27 15:55:33 +00:00
|
|
|
|
2021-05-22 04:55:25 +00:00
|
|
|
struct LinkInfo
|
|
|
|
{
|
|
|
|
CodeBlock* block;
|
|
|
|
void* host_pc;
|
|
|
|
void* host_resolve_pc;
|
|
|
|
u32 host_pc_size;
|
|
|
|
};
|
|
|
|
|
2019-11-27 15:55:33 +00:00
|
|
|
CodeBlock(const CodeBlockKey key_) : key(key_) {}
|
|
|
|
|
|
|
|
CodeBlockKey key;
|
|
|
|
u32 host_code_size = 0;
|
|
|
|
HostCodePointer host_code = nullptr;
|
|
|
|
|
|
|
|
std::vector<CodeBlockInstruction> instructions;
|
2021-05-22 04:55:25 +00:00
|
|
|
std::vector<LinkInfo> link_predecessors;
|
|
|
|
std::vector<LinkInfo> link_successors;
|
2019-11-27 15:55:33 +00:00
|
|
|
|
2020-08-29 12:07:33 +00:00
|
|
|
TickCount uncached_fetch_ticks = 0;
|
|
|
|
u32 icache_line_count = 0;
|
2020-10-18 04:43:55 +00:00
|
|
|
|
|
|
|
#ifdef WITH_RECOMPILER
|
|
|
|
std::vector<Recompiler::LoadStoreBackpatchInfo> loadstore_backpatch_info;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool contains_loadstore_instructions = false;
|
2020-11-18 10:34:25 +00:00
|
|
|
bool contains_double_branches = false;
|
2019-11-27 15:55:33 +00:00
|
|
|
bool invalidated = false;
|
2021-05-22 04:55:25 +00:00
|
|
|
bool can_link = true;
|
2019-11-27 15:55:33 +00:00
|
|
|
|
2021-04-26 16:56:55 +00:00
|
|
|
u32 recompile_frame_number = 0;
|
|
|
|
u32 recompile_count = 0;
|
2021-05-22 04:55:25 +00:00
|
|
|
u32 invalidate_frame_number = 0;
|
2021-04-26 16:56:55 +00:00
|
|
|
|
2022-08-10 04:33:20 +00:00
|
|
|
u32 GetPC() const { return key.GetPC(); }
|
|
|
|
u32 GetSizeInBytes() const { return static_cast<u32>(instructions.size()) * sizeof(Instruction); }
|
|
|
|
u32 GetStartPageIndex() const { return (key.GetPCPhysicalAddress() / HOST_PAGE_SIZE); }
|
|
|
|
u32 GetEndPageIndex() const { return ((key.GetPCPhysicalAddress() + GetSizeInBytes()) / HOST_PAGE_SIZE); }
|
2019-11-27 15:55:33 +00:00
|
|
|
bool IsInRAM() const
|
|
|
|
{
|
|
|
|
// TODO: Constant
|
2019-12-12 13:34:53 +00:00
|
|
|
return key.GetPCPhysicalAddress() < 0x200000;
|
2019-11-27 15:55:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
namespace CodeCache {
|
2019-11-19 10:30:04 +00:00
|
|
|
|
2021-07-20 02:33:37 +00:00
|
|
|
enum : u32
|
|
|
|
{
|
|
|
|
FAST_MAP_TABLE_COUNT = 0x10000,
|
|
|
|
FAST_MAP_TABLE_SIZE = 0x10000 / 4, // 16384
|
|
|
|
FAST_MAP_TABLE_SHIFT = 16,
|
|
|
|
};
|
|
|
|
|
|
|
|
using FastMapTable = CodeBlock::HostCodePointer*;
|
|
|
|
|
2020-10-18 04:43:55 +00:00
|
|
|
void Initialize();
|
2020-07-31 07:09:18 +00:00
|
|
|
void Shutdown();
|
|
|
|
void Execute();
|
2019-11-21 13:33:58 +00:00
|
|
|
|
2020-08-08 05:42:11 +00:00
|
|
|
#ifdef WITH_RECOMPILER
|
2020-10-18 04:43:09 +00:00
|
|
|
using DispatcherFunction = void (*)();
|
2022-07-08 12:43:38 +00:00
|
|
|
using SingleBlockDispatcherFunction = void (*)(const CodeBlock::HostCodePointer);
|
2020-10-18 04:43:09 +00:00
|
|
|
|
2021-07-20 02:33:37 +00:00
|
|
|
FastMapTable* GetFastMapPointer();
|
2020-08-08 05:42:11 +00:00
|
|
|
void ExecuteRecompiler();
|
|
|
|
#endif
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
/// Flushes the code cache, forcing all blocks to be recompiled.
|
|
|
|
void Flush();
|
2019-11-23 03:16:43 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
/// Changes whether the recompiler is enabled.
|
2020-10-18 04:43:55 +00:00
|
|
|
void Reinitialize();
|
2019-11-23 03:16:43 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
/// Invalidates all blocks which are in the range of the specified code page.
|
|
|
|
void InvalidateBlocksWithPageIndex(u32 page_index);
|
2019-11-23 03:16:43 +00:00
|
|
|
|
2021-12-02 08:40:31 +00:00
|
|
|
/// Invalidates all blocks in the cache.
|
|
|
|
void InvalidateAll();
|
|
|
|
|
2020-08-19 13:26:57 +00:00
|
|
|
template<PGXPMode pgxp_mode>
|
2020-07-31 07:09:18 +00:00
|
|
|
void InterpretCachedBlock(const CodeBlock& block);
|
2021-01-01 07:16:54 +00:00
|
|
|
|
|
|
|
template<PGXPMode pgxp_mode>
|
2020-07-31 07:09:18 +00:00
|
|
|
void InterpretUncachedBlock();
|
2019-11-23 03:16:43 +00:00
|
|
|
|
2020-10-18 04:43:09 +00:00
|
|
|
/// Invalidates any code pages which overlap the specified range.
|
|
|
|
ALWAYS_INLINE void InvalidateCodePages(PhysicalMemoryAddress address, u32 word_count)
|
|
|
|
{
|
2020-11-22 15:06:25 +00:00
|
|
|
const u32 start_page = address / HOST_PAGE_SIZE;
|
|
|
|
const u32 end_page = (address + word_count * sizeof(u32) - sizeof(u32)) / HOST_PAGE_SIZE;
|
2020-10-28 15:29:56 +00:00
|
|
|
for (u32 page = start_page; page <= end_page; page++)
|
2020-10-18 04:43:09 +00:00
|
|
|
{
|
|
|
|
if (Bus::m_ram_code_bits[page])
|
|
|
|
CPU::CodeCache::InvalidateBlocksWithPageIndex(page);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
}; // namespace CodeCache
|
2019-11-19 10:30:04 +00:00
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
} // namespace CPU
|