1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/* This file is part of the dynarmic project.
* Copyright (c) 2023 MerryMage
* SPDX-License-Identifier: 0BSD
*/
#include <array>
#include <exception>
#include <map>
#include <catch2/catch_test_macros.hpp>
#include <mcl/stdint.hpp>
#include <oaknut/oaknut.hpp>
#include "dynarmic/interface/A64/a64.h"
using namespace Dynarmic;
namespace {
class MyEnvironment final : public A64::UserCallbacks {
public:
u64 ticks_left = 0;
std::map<u64, u8> memory{};
u8 MemoryRead8(u64 vaddr) override {
return memory[vaddr];
}
u16 MemoryRead16(u64 vaddr) override {
return u16(MemoryRead8(vaddr)) | u16(MemoryRead8(vaddr + 1)) << 8;
}
u32 MemoryRead32(u64 vaddr) override {
return u32(MemoryRead16(vaddr)) | u32(MemoryRead16(vaddr + 2)) << 16;
}
u64 MemoryRead64(u64 vaddr) override {
return u64(MemoryRead32(vaddr)) | u64(MemoryRead32(vaddr + 4)) << 32;
}
std::array<u64, 2> MemoryRead128(u64 vaddr) override {
return {MemoryRead64(vaddr), MemoryRead64(vaddr + 8)};
}
void MemoryWrite8(u64 vaddr, u8 value) override {
memory[vaddr] = value;
}
void MemoryWrite16(u64 vaddr, u16 value) override {
MemoryWrite8(vaddr, u8(value));
MemoryWrite8(vaddr + 1, u8(value >> 8));
}
void MemoryWrite32(u64 vaddr, u32 value) override {
MemoryWrite16(vaddr, u16(value));
MemoryWrite16(vaddr + 2, u16(value >> 16));
}
void MemoryWrite64(u64 vaddr, u64 value) override {
MemoryWrite32(vaddr, u32(value));
MemoryWrite32(vaddr + 4, u32(value >> 32));
}
void MemoryWrite128(u64 vaddr, std::array<u64, 2> value) override {
MemoryWrite64(vaddr, value[0]);
MemoryWrite64(vaddr + 8, value[1]);
}
void InterpreterFallback(u64, size_t) override {
// This is never called in practice.
std::terminate();
}
void CallSVC(u32) override {
// Do something.
}
void ExceptionRaised(u64, A64::Exception) override {
cpu->HaltExecution();
}
void AddTicks(u64) override {
}
u64 GetTicksRemaining() override {
return 1000000000000;
}
std::uint64_t GetCNTPCT() override {
return 0;
}
A64::Jit* cpu;
};
} // namespace
TEST_CASE("A64: fibonacci", "[a64]") {
MyEnvironment env;
A64::UserConfig user_config;
user_config.callbacks = &env;
A64::Jit cpu{user_config};
env.cpu = &cpu;
std::vector<u32> instructions(1024);
oaknut::CodeGenerator code{instructions.data(), nullptr};
using namespace oaknut::util;
oaknut::Label start, end, zero, recurse;
code.l(start);
code.STP(X29, X30, SP, PRE_INDEXED, -32);
code.STP(X20, X19, SP, 16);
code.MOV(X29, SP);
code.MOV(W19, W0);
code.SUBS(W0, W0, 1);
code.B(LT, zero);
code.B(NE, recurse);
code.MOV(W0, 1);
code.B(end);
code.l(zero);
code.MOV(W0, WZR);
code.B(end);
code.l(recurse);
code.BL(start);
code.MOV(W20, W0);
code.SUB(W0, W19, 2);
code.BL(start);
code.ADD(W0, W0, W20);
code.l(end);
code.LDP(X20, X19, SP, 16);
code.LDP(X29, X30, SP, POST_INDEXED, 32);
code.RET();
for (size_t i = 0; i < 1024; i++) {
env.MemoryWrite32(i * 4, instructions[i]);
}
env.MemoryWrite32(8888, 0xd4200000);
cpu.SetRegister(30, 8888);
cpu.SetRegister(0, 10);
cpu.SetSP(0xffff0000);
cpu.SetPC(0);
cpu.Run();
REQUIRE(cpu.GetRegister(0) == 55);
cpu.SetRegister(0, 20);
cpu.SetSP(0xffff0000);
cpu.SetPC(0);
cpu.Run();
REQUIRE(cpu.GetRegister(0) == 6765);
cpu.SetRegister(0, 30);
cpu.SetSP(0xffff0000);
cpu.SetPC(0);
cpu.Run();
REQUIRE(cpu.GetRegister(0) == 832040);
}
|