arm_test_common.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 <tuple>
  6. #include <unordered_map>
  7. #include <vector>
  8. #include "common/common_types.h"
  9. #include "core/memory_hook.h"
  10. namespace ArmTests {
  11. struct WriteRecord {
  12. WriteRecord(size_t size, VAddr addr, u64 data) : size(size), addr(addr), data(data) {}
  13. size_t size;
  14. VAddr addr;
  15. u64 data;
  16. bool operator==(const WriteRecord& o) const {
  17. return std::tie(size, addr, data) == std::tie(o.size, o.addr, o.data);
  18. }
  19. };
  20. class TestEnvironment final {
  21. public:
  22. /*
  23. * Inititalise test environment
  24. * @param mutable_memory If false, writes to memory can never be read back.
  25. * (Memory is immutable.)
  26. */
  27. explicit TestEnvironment(bool mutable_memory = false);
  28. /// Shutdown test environment
  29. ~TestEnvironment();
  30. /// Sets value at memory location vaddr.
  31. void SetMemory8(VAddr vaddr, u8 value);
  32. void SetMemory16(VAddr vaddr, u16 value);
  33. void SetMemory32(VAddr vaddr, u32 value);
  34. void SetMemory64(VAddr vaddr, u64 value);
  35. /**
  36. * Whenever Memory::Write{8,16,32,64} is called within the test environment,
  37. * a new write-record is made.
  38. * @returns A vector of write records made since they were last cleared.
  39. */
  40. std::vector<WriteRecord> GetWriteRecords() const;
  41. /// Empties the internal write-record store.
  42. void ClearWriteRecords();
  43. private:
  44. friend struct TestMemory;
  45. struct TestMemory final : Memory::MemoryHook {
  46. explicit TestMemory(TestEnvironment* env_) : env(env_) {}
  47. TestEnvironment* env;
  48. ~TestMemory() override;
  49. boost::optional<bool> IsValidAddress(VAddr addr) override;
  50. boost::optional<u8> Read8(VAddr addr) override;
  51. boost::optional<u16> Read16(VAddr addr) override;
  52. boost::optional<u32> Read32(VAddr addr) override;
  53. boost::optional<u64> Read64(VAddr addr) override;
  54. bool ReadBlock(VAddr src_addr, void* dest_buffer, size_t size) override;
  55. bool Write8(VAddr addr, u8 data) override;
  56. bool Write16(VAddr addr, u16 data) override;
  57. bool Write32(VAddr addr, u32 data) override;
  58. bool Write64(VAddr addr, u64 data) override;
  59. bool WriteBlock(VAddr dest_addr, const void* src_buffer, size_t size) override;
  60. std::unordered_map<VAddr, u8> data;
  61. };
  62. bool mutable_memory;
  63. std::shared_ptr<TestMemory> test_memory;
  64. std::vector<WriteRecord> write_records;
  65. };
  66. } // namespace ArmTests