memory_hook.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright 2016 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <memory>
  6. #include <optional>
  7. #include "common/common_types.h"
  8. namespace Memory {
  9. /**
  10. * Memory hooks have two purposes:
  11. * 1. To allow reads and writes to a region of memory to be intercepted. This is used to implement
  12. * texture forwarding and memory breakpoints for debugging.
  13. * 2. To allow for the implementation of MMIO devices.
  14. *
  15. * A hook may be mapped to multiple regions of memory.
  16. *
  17. * If a std::nullopt or false is returned from a function, the read/write request is passed through
  18. * to the underlying memory region.
  19. */
  20. class MemoryHook {
  21. public:
  22. virtual ~MemoryHook();
  23. virtual std::optional<bool> IsValidAddress(VAddr addr) = 0;
  24. virtual std::optional<u8> Read8(VAddr addr) = 0;
  25. virtual std::optional<u16> Read16(VAddr addr) = 0;
  26. virtual std::optional<u32> Read32(VAddr addr) = 0;
  27. virtual std::optional<u64> Read64(VAddr addr) = 0;
  28. virtual bool ReadBlock(VAddr src_addr, void* dest_buffer, std::size_t size) = 0;
  29. virtual bool Write8(VAddr addr, u8 data) = 0;
  30. virtual bool Write16(VAddr addr, u16 data) = 0;
  31. virtual bool Write32(VAddr addr, u32 data) = 0;
  32. virtual bool Write64(VAddr addr, u64 data) = 0;
  33. virtual bool WriteBlock(VAddr dest_addr, const void* src_buffer, std::size_t size) = 0;
  34. };
  35. using MemoryHookPointer = std::shared_ptr<MemoryHook>;
  36. } // namespace Memory