OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[csp-qt/common_source_project-fm7.git] / source / src / vm / scv / scv.cpp
1 /*
2         EPOCH Super Cassette Vision Emulator 'eSCV'
3
4         Author : Takeda.Toshiya
5         Date   : 2006.08.21 -
6
7         [ virtual machine ]
8 */
9
10 #include "scv.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../upd7801.h"
16
17 #ifdef USE_DEBUGGER
18 #include "../debugger.h"
19 #endif
20
21 #include "io.h"
22 #include "memory.h"
23 #include "sound.h"
24 #include "vdp.h"
25
26 // ----------------------------------------------------------------------------
27 // initialize
28 // ----------------------------------------------------------------------------
29
30 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
31 {
32         // create devices
33         first_device = last_device = NULL;
34         dummy = new DEVICE(this, emu);  // must be 1st device
35         event = new EVENT(this, emu);   // must be 2nd device
36         dummy->set_device_name(_T("1st Dummy"));
37         cpu = new UPD7801(this, emu);
38         
39         io = new IO(this, emu);
40         memory = new MEMORY(this, emu);
41         sound = new SOUND(this, emu);
42         vdp = new VDP(this, emu);
43         
44         // set contexts
45         event->set_context_cpu(cpu);
46         event->set_context_sound(sound);
47         
48         io->set_context_mem(memory);
49         io->set_context_sound(sound);
50         memory->set_context_sound(sound);
51         sound->set_context_cpu(cpu);
52         vdp->set_context_cpu(cpu);
53         vdp->set_font_ptr(memory->get_font());
54         vdp->set_vram_ptr(memory->get_vram());
55         
56         // cpu bus
57         cpu->set_context_mem(memory);
58         cpu->set_context_io(io);
59 #ifdef USE_DEBUGGER
60         cpu->set_context_debugger(new DEBUGGER(this, emu));
61 #endif
62         
63         // initialize all devices
64 #if defined(__GIT_REPO_VERSION)
65         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
66 #endif
67         for(DEVICE* device = first_device; device; device = device->next_device) {
68                 device->initialize();
69         }
70         decl_state();
71 }
72
73 VM::~VM()
74 {
75         // delete all devices
76         for(DEVICE* device = first_device; device;) {
77                 DEVICE *next_device = device->next_device;
78                 device->release();
79                 delete device;
80                 device = next_device;
81         }
82 }
83
84 DEVICE* VM::get_device(int id)
85 {
86         for(DEVICE* device = first_device; device; device = device->next_device) {
87                 if(device->this_device_id == id) {
88                         return device;
89                 }
90         }
91         return NULL;
92 }
93
94 // ----------------------------------------------------------------------------
95 // drive virtual machine
96 // ----------------------------------------------------------------------------
97
98 void VM::reset()
99 {
100         // reset all devices
101         for(DEVICE* device = first_device; device; device = device->next_device) {
102                 device->reset();
103         }
104 }
105
106 void VM::run()
107 {
108         event->drive();
109 }
110
111 // ----------------------------------------------------------------------------
112 // debugger
113 // ----------------------------------------------------------------------------
114
115 #ifdef USE_DEBUGGER
116 DEVICE *VM::get_cpu(int index)
117 {
118         if(index == 0) {
119                 return cpu;
120         }
121         return NULL;
122 }
123 #endif
124
125 // ----------------------------------------------------------------------------
126 // draw screen
127 // ----------------------------------------------------------------------------
128
129 void VM::draw_screen()
130 {
131         vdp->draw_screen();
132 }
133
134 // ----------------------------------------------------------------------------
135 // soud manager
136 // ----------------------------------------------------------------------------
137
138 void VM::initialize_sound(int rate, int samples)
139 {
140         // init sound manager
141         event->initialize_sound(rate, samples);
142         
143         // init sound gen
144         sound->initialize_sound(rate);
145 }
146
147 uint16_t* VM::create_sound(int* extra_frames)
148 {
149         return event->create_sound(extra_frames);
150 }
151
152 int VM::get_sound_buffer_ptr()
153 {
154         return event->get_sound_buffer_ptr();
155 }
156
157 #ifdef USE_SOUND_VOLUME
158 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
159 {
160         sound->set_volume(ch, decibel_l, decibel_r);
161 }
162 #endif
163
164 // ----------------------------------------------------------------------------
165 // user interface
166 // ----------------------------------------------------------------------------
167
168 void VM::open_cart(int drv, const _TCHAR* file_path)
169 {
170         if(drv == 0) {
171                 memory->open_cart(file_path);
172                 reset();
173         }
174 }
175
176 void VM::close_cart(int drv)
177 {
178         if(drv == 0) {
179                 memory->close_cart();
180                 reset();
181         }
182 }
183
184 bool VM::is_cart_inserted(int drv)
185 {
186         if(drv == 0) {
187                 return memory->is_cart_inserted();
188         } else {
189                 return false;
190         }
191 }
192
193 bool VM::is_frame_skippable()
194 {
195         return event->is_frame_skippable();
196 }
197
198 void VM::update_config()
199 {
200         for(DEVICE* device = first_device; device; device = device->next_device) {
201                 device->update_config();
202         }
203 }
204
205 #define STATE_VERSION   2
206
207 #include "../../statesub.h"
208 #include "../../qt/gui/csp_logger.h"
209 extern CSP_Logger DLL_PREFIX_I *csp_logger;
210
211 void VM::decl_state(void)
212 {
213         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::SCV_HEAD")), csp_logger);
214         for(DEVICE* device = first_device; device; device = device->next_device) {
215                 device->decl_state();
216         }
217 }
218
219 void VM::save_state(FILEIO* state_fio)
220 {
221         //state_fio->FputUint32(STATE_VERSION);
222         
223         if(state_entry != NULL) {
224                 state_entry->save_state(state_fio);
225         }
226         for(DEVICE* device = first_device; device; device = device->next_device) {
227                 device->save_state(state_fio);
228         }
229 }
230
231 bool VM::load_state(FILEIO* state_fio)
232 {
233         //if(state_fio->FgetUint32() != STATE_VERSION) {
234         //      return false;
235         //}
236         bool mb = false;
237         if(state_entry != NULL) {
238                 mb = state_entry->load_state(state_fio);
239         }
240         if(!mb) {
241                 emu->out_debug_log("INFO: HEADER DATA ERROR");
242                 return false;
243         }
244         for(DEVICE* device = first_device; device; device = device->next_device) {
245                 if(!device->load_state(state_fio)) {
246                         return false;
247                 }
248         }
249         return true;
250 }
251