lsnes rr2-β24
[lsnes.git] / src / core / instance.cpp
blob1836f47663560a3e183221a021b6042a6e5a8081
1 #include "core/advdumper.hpp"
2 #include "core/audioapi.hpp"
3 #include "core/command.hpp"
4 #include "core/controllerframe.hpp"
5 #include "core/controller.hpp"
6 #include "core/debug.hpp"
7 #include "core/dispatch.hpp"
8 #include "core/emustatus.hpp"
9 #include "core/framebuffer.hpp"
10 #include "core/framerate.hpp"
11 #include "core/instance.hpp"
12 #include "core/inthread.hpp"
13 #include "core/jukebox.hpp"
14 #include "core/keymapper.hpp"
15 #include "core/mbranch.hpp"
16 #include "core/memorymanip.hpp"
17 #include "core/memorywatch.hpp"
18 #include "core/messages.hpp"
19 #include "core/misc.hpp"
20 #include "core/moviedata.hpp"
21 #include "core/movie.hpp"
22 #include "core/multitrack.hpp"
23 #include "core/project.hpp"
24 #include "core/queue.hpp"
25 #include "core/random.hpp"
26 #include "core/rom.hpp"
27 #include "core/runmode.hpp"
28 #include "core/settings.hpp"
29 #include "fonts/wrapper.hpp"
30 #include "library/command.hpp"
31 #include "library/framebuffer.hpp"
32 #include "library/keyboard.hpp"
33 #include "library/keyboard-mapper.hpp"
34 #include "library/lua-base.hpp"
35 #include "library/memoryspace.hpp"
36 #include "library/settingvar.hpp"
37 #include "lua/lua.hpp"
39 #include <deque>
40 #ifdef __linux__
41 #include <execinfo.h>
42 #endif
44 dtor_list::dtor_list()
46 list = NULL;
49 dtor_list::~dtor_list()
51 destroy();
54 void dtor_list::destroy()
56 dtor_list::entry* e = list;
57 while(e) {
58 e->free1(e->ptr);
59 e = e->prev;
61 e = list;
62 while(e) {
63 dtor_list::entry* f = e;
64 e = e->prev;
65 f->free2(f->ptr);
66 delete f;
68 list = NULL;
71 emulator_instance::emulator_instance()
73 //Preinit.
74 D.prealloc(fbuf);
75 D.prealloc(project);
76 D.prealloc(buttons);
77 D.prealloc(dispatch);
78 D.prealloc(supdater);
79 D.prealloc(rom);
81 D.init(dispatch);
82 D.init(command);
83 D.init(iqueue, *command);
84 D.init(mlogic);
85 D.init(slotcache, *mlogic, *command);
86 D.init(memory);
87 D.init(settings);
88 D.init(lua);
89 D.init(lua2, *lua, *command, *settings);
90 D.init(mwatch, *memory, *project, *fbuf, *rom);
91 D.init(jukebox, *settings, *command);
92 D.init(setcache, *settings);
93 D.init(audio);
94 D.init(commentary, *settings, *dispatch, *audio, *command);
95 D.init(subtitles, *mlogic, *fbuf, *dispatch, *command);
96 D.init(mbranch, *mlogic, *dispatch, *supdater);
97 D.init(controls, *project, *mlogic, *buttons, *dispatch, *supdater, *command);
98 D.init(keyboard);
99 D.init(mapper, *keyboard, *command);
100 D.init(rom);
101 D.init(fbuf, *subtitles, *settings, *mwatch, *keyboard, *dispatch, *lua2, *rom, *supdater, *command,
102 *iqueue);
103 D.init(buttons, *controls, *mapper, *keyboard, *fbuf, *dispatch, *lua2, *command);
104 D.init(mteditor, *mlogic, *controls, *dispatch, *supdater, *buttons, *command);
105 D.init(status_A);
106 D.init(status_B);
107 D.init(status_C);
108 D.init(status, *status_A, *status_B, *status_C);
109 D.init(abindmanager, *mapper, *command);
110 D.init(nrrdata);
111 D.init(cmapper, *memory, *mlogic, *rom);
112 D.init(project, *commentary, *mwatch, *command, *controls, *settings, *buttons, *dispatch, *iqueue, *rom,
113 *supdater);
114 D.init(dbg, *dispatch, *rom, *memory, *command);
115 D.init(framerate, *command);
116 D.init(mdumper, *lua2);
117 D.init(runmode);
118 D.init(supdater, *project, *mlogic, *commentary, *status, *runmode, *mdumper, *jukebox, *slotcache,
119 *framerate, *controls, *mteditor, *lua2, *rom, *mwatch, *dispatch);
121 status_A->valid = false;
122 status_B->valid = false;
123 status_C->valid = false;
124 command->add_set(lsnes_cmds);
125 mapper->add_invbind_set(lsnes_invbinds);
126 settings->add_set(lsnes_setgrp);
127 dispatch->set_error_streams(&messages.getstream());
128 random_seed_value = 0;
131 emulator_instance::~emulator_instance()
133 D.destroy();
136 emulator_instance lsnes_instance;
138 emulator_instance& CORE()
140 if(threads::id() != lsnes_instance.emu_thread) {
141 std::cerr << "WARNING: CORE() called in wrong thread." << std::endl;
142 #ifdef __linux__
143 void* arr[256];
144 backtrace_symbols_fd(arr, 256, 2);
145 #endif
147 return lsnes_instance;
150 namespace
152 uint32_t digits(size_t num)
154 if(num < 10) return 1;
155 return 1 + digits(num / 10);
158 command::fnptr<> CMD_memory_use(lsnes_cmds, "show-memory", "Show memory usage",
159 "show-memory\nShow memory usage",
160 []() throw(std::bad_alloc, std::runtime_error) {
161 auto report = memtracker::singleton().report();
162 uint32_t maxwidth_left = 0;
163 uint32_t maxwidth_right = 0;
164 for(auto i : report) {
165 maxwidth_left = std::max(maxwidth_left, main_font.get_width(i.first));
166 maxwidth_right = std::max(maxwidth_right, digits(i.second));
168 for(auto i : report) {
169 uint32_t pad_spaces = maxwidth_left - main_font.get_width(i.first) + maxwidth_right -
170 digits(i.second) + 1;
171 messages << i.first << std::string(pad_spaces, ' ') << i.second << std::endl;