A reimplementation of Mario Kart Wii's physics engine in C++
Loading...
Searching...
No Matches
main.cc
1#include "host/KReplaySystem.hh"
2#include "host/KTestSystem.hh"
3#include "host/Option.hh"
4
5#include <egg/core/ExpHeap.hh>
6
7#if defined(__arm64__) || defined(__aarch64__)
8static void FlushDenormalsToZero() {
9 uint64_t fpcr;
10 asm("mrs %0, fpcr" : "=r"(fpcr));
11 asm("msr fpcr, %0" ::"r"(fpcr | (1 << 24)));
12}
13#elif defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
14#include <immintrin.h>
15
16static void FlushDenormalsToZero() {
17 _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
18}
19#endif
20
21static void *s_memorySpace = nullptr;
22static EGG::Heap *s_rootHeap = nullptr;
23
24static void InitMemory() {
25 s_memorySpace = malloc(MEMORY_SPACE_SIZE);
26 s_rootHeap = EGG::ExpHeap::create(s_memorySpace, MEMORY_SPACE_SIZE, DEFAULT_OPT);
27 s_rootHeap->setName("EGGRoot");
28 s_rootHeap->becomeCurrentHeap();
29
30 EGG::SceneManager::SetRootHeap(s_rootHeap);
31}
32
33int main(int argc, char **argv) {
34 FlushDenormalsToZero();
35 InitMemory();
36
37 // The hashmap cannot be constexpr, as it heap-allocates
38 // Therefore, it cannot be static, as memory needs to be initialized first
39 // TODO: Allow memory initialization before any other static initializers
40 const std::unordered_map<std::string, std::function<KSystem *()>> modeMap = {
41 {"test", []() -> KSystem * { return KTestSystem::CreateInstance(); }},
42 {"replay", []() -> KSystem * { return KReplaySystem::CreateInstance(); }},
43 };
44
45 if (argc < 3) {
46 PANIC("Too few arguments!");
47 }
48
49 // The first argument is the executable, so we ignore it
50 // The second argument is the mode flag
51 // The third argument is the mode arg
52 // TODO: Iterate until we find the index of the mode flag
53 std::optional<Host::EOption> flag = Host::Option::CheckFlag(argv[1]);
54 if (!flag) {
55 PANIC("Not a flag!");
56 }
57
58 if (*flag != Host::EOption::Mode) {
59 PANIC("First flag expected to be mode!");
60 }
61
62 KSystem *sys = nullptr;
63 const std::string mode = argv[2];
64
65 auto it = modeMap.find(mode);
66 if (it != modeMap.end()) {
67 sys = it->second();
68 } else {
69 PANIC("Invalid mode!");
70 }
71
72 sys->parseOptions(argc - 3, argv + 3);
73 sys->init();
74 return sys->run() ? 0 : 1;
75}
A high-level representation of a memory heap for managing dynamic memory allocation....
Definition Heap.hh:22
Base interface for a Kinoko system.
Definition KSystem.hh:8
virtual void init()=0
Initializes the system.
virtual void parseOptions(int argc, char **argv)=0
Parses non-generic command line options.
virtual bool run()=0
Executes a run.