OSDN Git Service

9d8877ee146a6edfef36e6896f0ef276e01e2a58
[csp-qt/common_source_project-fm7.git] / source / src / vm / x07 / x07.cpp
1 /*
2         CANON X-07 Emulator 'eX-07'
3
4         Author : Takeda.Toshiya
5         Date   : 2007.12.26 -
6
7         [ virtual machine ]
8 */
9
10 #include "x07.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../beep.h"
16 #include "../memory.h"
17 #include "../z80.h"
18
19 #ifdef USE_DEBUGGER
20 #include "../debugger.h"
21 #endif
22
23 #include "io.h"
24
25 // ----------------------------------------------------------------------------
26 // initialize
27 // ----------------------------------------------------------------------------
28
29 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
30 {
31         // create devices
32         first_device = last_device = NULL;
33         dummy = new DEVICE(this, emu);  // must be 1st device
34         event = new EVENT(this, emu);   // must be 2nd device
35         dummy->set_device_name(_T("1st Dummy"));
36         
37         beep = new BEEP(this, emu);
38         memory = new MEMORY(this, emu);
39         cpu = new Z80(this, emu);
40         
41         io = new IO(this, emu);
42         // set contexts
43         event->set_context_cpu(cpu);
44         event->set_context_sound(beep);
45         
46         io->set_context_beep(beep);
47         io->set_context_cpu(cpu);
48         io->set_context_mem(memory, ram);
49         
50         // memory bus
51         memset(ram, 0, sizeof(ram));
52         memset(app, 0xff, sizeof(app));
53         memset(tv, 0xff, sizeof(tv));
54         memset(bas, 0xff, sizeof(bas));
55         
56         memory->read_bios(_T("APP.ROM"), app, sizeof(app));
57         memory->read_bios(_T("TV.ROM"), tv, sizeof(tv));
58         memory->read_bios(_T("BASIC.ROM"), bas, sizeof(bas));
59         
60         memory->set_memory_rw(0x0000, 0x5fff, ram);
61         memory->set_memory_r(0x6000, 0x7fff, app);
62         memory->set_memory_rw(0x8000, 0x97ff, vram);
63         memory->set_memory_r(0xa000, 0xafff, tv);
64         memory->set_memory_r(0xb000, 0xffff, bas);
65         
66         // cpu bus
67         cpu->set_context_mem(memory);
68         cpu->set_context_io(io);
69         cpu->set_context_intr(io);
70 #ifdef USE_DEBUGGER
71         cpu->set_context_debugger(new DEBUGGER(this, emu));
72 #endif
73         
74         // initialize all devices
75         for(DEVICE* device = first_device; device; device = device->next_device) {
76                 device->initialize();
77         }
78         decl_state();
79 }
80
81 VM::~VM()
82 {
83         // delete all devices
84         for(DEVICE* device = first_device; device;) {
85                 DEVICE *next_device = device->next_device;
86                 device->release();
87                 delete device;
88                 device = next_device;
89         }
90 }
91
92 DEVICE* VM::get_device(int id)
93 {
94         for(DEVICE* device = first_device; device; device = device->next_device) {
95                 if(device->this_device_id == id) {
96                         return device;
97                 }
98         }
99         return NULL;
100 }
101
102 // ----------------------------------------------------------------------------
103 // drive virtual machine
104 // ----------------------------------------------------------------------------
105
106 void VM::reset()
107 {
108         // reset all devices
109         for(DEVICE* device = first_device; device; device = device->next_device) {
110                 device->reset();
111         }
112 }
113
114 void VM::run()
115 {
116         event->drive();
117 }
118
119 // ----------------------------------------------------------------------------
120 // debugger
121 // ----------------------------------------------------------------------------
122
123 #ifdef USE_DEBUGGER
124 DEVICE *VM::get_cpu(int index)
125 {
126         if(index == 0) {
127                 return cpu;
128         }
129         return NULL;
130 }
131 #endif
132
133 // ----------------------------------------------------------------------------
134 // draw screen
135 // ----------------------------------------------------------------------------
136
137 void VM::draw_screen()
138 {
139         io->draw_screen();
140 }
141
142 // ----------------------------------------------------------------------------
143 // soud manager
144 // ----------------------------------------------------------------------------
145
146 void VM::initialize_sound(int rate, int samples)
147 {
148         // init sound manager
149         event->initialize_sound(rate, samples);
150         
151         // init sound gen
152         beep->initialize_sound(rate, 1000, 8000);
153 }
154
155 uint16_t* VM::create_sound(int* extra_frames)
156 {
157         return event->create_sound(extra_frames);
158 }
159
160 int VM::get_sound_buffer_ptr()
161 {
162         return event->get_sound_buffer_ptr();
163 }
164
165 #ifdef USE_SOUND_VOLUME
166 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
167 {
168         if(ch == 0) {
169                 beep->set_volume(0, decibel_l, decibel_r);
170         }
171 }
172 #endif
173
174 // ----------------------------------------------------------------------------
175 // notify key
176 // ----------------------------------------------------------------------------
177
178 void VM::key_down(int code, bool repeat)
179 {
180         io->key_down(code);
181 }
182
183 void VM::key_up(int code)
184 {
185         io->key_up(code);
186 }
187
188 bool VM::get_caps_locked()
189 {
190         return io->get_caps_locked();
191 }
192
193 bool VM::get_kana_locked()
194 {
195         return io->get_kana_locked();
196 }
197
198 // ----------------------------------------------------------------------------
199 // user interface
200 // ----------------------------------------------------------------------------
201
202 void VM::play_tape(int drv, const _TCHAR* file_path)
203 {
204         io->play_tape(file_path);
205 }
206
207 void VM::rec_tape(int drv, const _TCHAR* file_path)
208 {
209         io->rec_tape(file_path);
210 }
211
212 void VM::close_tape(int drv)
213 {
214         io->close_tape();
215 }
216
217 bool VM::is_tape_inserted(int drv)
218 {
219         return io->is_tape_inserted();
220 }
221
222 bool VM::is_frame_skippable()
223 {
224         return event->is_frame_skippable();
225 }
226
227 void VM::update_config()
228 {
229         for(DEVICE* device = first_device; device; device = device->next_device) {
230                 device->update_config();
231         }
232 }
233
234 #define STATE_VERSION   2
235
236 #include "../../statesub.h"
237 #include "../../qt/gui/csp_logger.h"
238 extern CSP_Logger DLL_PREFIX_I *csp_logger;
239
240 void VM::decl_state(void)
241 {
242
243         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::X07_HEAD")), csp_logger);
244         DECL_STATE_ENTRY_1D_ARRAY(ram, sizeof(ram));
245         DECL_STATE_ENTRY_1D_ARRAY(vram, sizeof(vram));
246         for(DEVICE* device = first_device; device; device = device->next_device) {
247                 device->decl_state();
248         }
249 }
250
251 void VM::save_state(FILEIO* state_fio)
252 {
253         //state_fio->FputUint32(STATE_VERSION);
254         
255         if(state_entry != NULL) {
256                 state_entry->save_state(state_fio);
257         }
258         for(DEVICE* device = first_device; device; device = device->next_device) {
259                 device->save_state(state_fio);
260         }
261         //state_fio->Fwrite(ram, sizeof(ram), 1);
262         //state_fio->Fwrite(vram, sizeof(vram), 1);
263 }
264
265 bool VM::load_state(FILEIO* state_fio)
266 {
267         //if(state_fio->FgetUint32() != STATE_VERSION) {
268         //      return false;
269         //}
270         bool mb = false;
271         if(state_entry != NULL) {
272                 mb = state_entry->load_state(state_fio);
273         }
274         if(!mb) {
275                 emu->out_debug_log("INFO: HEADER DATA ERROR");
276                 return false;
277         }
278         for(DEVICE* device = first_device; device; device = device->next_device) {
279                 if(!device->load_state(state_fio)) {
280                         return false;
281                 }
282         }
283         //state_fio->Fread(ram, sizeof(ram), 1);
284         //state_fio->Fread(vram, sizeof(vram), 1);
285         return true;
286 }
287