OSDN Git Service

[UI][Qt] Build even not supported write protection of FDs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / msx / msx.cpp
1 /*
2         ASCII MSX1 Emulator 'yaMSX1'
3         ASCII MSX2 Emulator 'yaMSX2'
4         Pioneer PX-7 Emulator 'ePX-7'
5
6         Author : tanam
7         Date   : 2013.06.29-
8
9         modified by Takeda.Toshiya
10         modified by umaiboux
11
12         [ virtual machine ]
13 */
14
15 #include "msx.h"
16 #include "../../emu.h"
17 #include "../device.h"
18 #include "../event.h"
19
20 #include "../datarec.h"
21 #include "../i8255.h"
22 #include "../io.h"
23 #if defined(_PX7)
24 #include "../ld700.h"
25 #endif
26 #include "../not.h"
27 #include "../ym2203.h"
28 #include "../pcm1bit.h"
29 #if defined(_MSX2)
30 #include "../rp5c01.h"
31 #include "../v99x8.h"
32 #else
33 #include "../tms9918a.h"
34 #endif
35 #include "../z80.h"
36
37 #ifdef USE_DEBUGGER
38 #include "../debugger.h"
39 #endif
40
41 #include "joystick.h"
42 #include "keyboard.h"
43 #include "memory.h"
44 #if defined(_MSX2)
45 #include "rtcif.h"
46 #endif
47
48 // ----------------------------------------------------------------------------
49 // initialize
50 // ----------------------------------------------------------------------------
51
52 VM::VM(EMU* parent_emu) : emu(parent_emu)
53 {
54         // create devices
55         first_device = last_device = NULL;
56         dummy = new DEVICE(this, emu);  // must be 1st device
57         event = new EVENT(this, emu);   // must be 2nd device
58         
59         drec = new DATAREC(this, emu);
60         pio = new I8255(this, emu);
61         io = new IO(this, emu);
62 #if defined(_PX7)
63         ldp = new LD700(this, emu);
64 #endif
65         d_not = new NOT(this, emu);
66         psg = new YM2203(this, emu);
67         pcm = new PCM1BIT(this, emu);
68 #if defined(_MSX2)
69         rtc = new RP5C01(this, emu);
70         vdp = new V99X8(this, emu);
71 #else
72         vdp = new TMS9918A(this, emu);
73 #endif
74         cpu = new Z80(this, emu);
75         
76         joystick = new JOYSTICK(this, emu);
77         keyboard = new KEYBOARD(this, emu);
78         memory = new MEMORY(this, emu);
79 #if defined(_MSX2)
80         rtcif = new RTCIF(this, emu);
81 #endif
82         slot0 = new SLOT0(this, emu);   // #0: main memory
83         slot1 = new SLOT1(this, emu);   // #1: rom-cartridge or msx-dos
84         slot2 = new SLOT2(this, emu);   // #2: fdd-cartridge or p-basic
85         slot3 = new SLOT3(this, emu);   // #3: rom-cartridge or ram-cartridge
86         
87         // set contexts
88         event->set_context_cpu(cpu);
89         event->set_context_sound(psg);
90         event->set_context_sound(pcm);
91         event->set_context_sound(drec);
92 #if defined(_PX7)
93         event->set_context_sound(ldp);
94 #endif
95         
96         drec->set_context_out(psg, SIG_YM2203_PORT_A, 0x80);
97         pio->set_context_port_a(memory, SIG_MEMORY_SEL, 0xff, 0);
98         pio->set_context_port_c(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
99         pio->set_context_port_c(d_not, SIG_NOT_INPUT, 0x10, 0);
100         d_not->set_context_out(drec, SIG_DATAREC_REMOTE, 1);
101         pio->set_context_port_c(drec, SIG_DATAREC_OUT, 0x20, 0);
102         pio->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x80, 0);
103         psg->set_context_port_b(joystick, SIG_JOYSTICK_SEL, 0x40, 0);
104         vdp->set_context_irq(cpu, SIG_CPU_IRQ, 1);
105 #if defined(_PX7)
106         pio->set_context_port_c(slot2, SIG_SLOT2_MUTE, 0x10, 0);
107         ldp->set_context_exv(slot2, SIG_SLOT2_EXV, 1);
108         ldp->set_context_ack(slot2, SIG_SLOT2_ACK, 1);
109         ldp->set_context_sound(psg, SIG_YM2203_PORT_A, 0x80);
110 #endif
111         
112         joystick->set_context_psg(psg);
113 //      keyboard->set_context_cpu(cpu);
114         keyboard->set_context_pio(pio);
115         memory->set_context_slot(0, slot0);
116         memory->set_context_slot(1, slot1);
117         memory->set_context_slot(2, slot2);
118         memory->set_context_slot(3, slot3);
119 #if defined(_MSX2)
120         rtcif->set_context_rtc(rtc);
121 #endif
122 #if defined(_PX7)
123         slot2->set_context_cpu(cpu);
124         slot2->set_context_ldp(ldp);
125         slot2->set_context_vdp(vdp);
126 #endif
127         
128         // cpu bus
129         cpu->set_context_mem(memory);
130         cpu->set_context_io(io);
131         cpu->set_context_intr(dummy);
132 #if !defined(_PX7)
133         cpu->set_context_bios(memory);
134 #endif
135 #ifdef USE_DEBUGGER
136         cpu->set_context_debugger(new DEBUGGER(this, emu));
137 #endif
138         
139         // i/o bus
140 #ifdef _MSX2
141         io->set_iomap_range_rw(0xb4, 0xb5, rtcif);
142         io->set_iomap_range_rw(0x98, 0x9b, vdp);
143 #else
144         io->set_iomap_range_rw(0x98, 0x99, vdp);
145 #endif
146         io->set_iomap_range_rw(0xa8, 0xab, pio);
147         io->set_iomap_alias_w(0xa0, psg, 0);    // PSG ch
148         io->set_iomap_alias_w(0xa1, psg, 1);    // PSG data
149         io->set_iomap_alias_r(0xa2, psg, 1);    // PSG data
150         io->set_iomap_range_rw(0xfc, 0xff, memory);
151         
152         // initialize all devices
153         for(DEVICE* device = first_device; device; device = device->next_device) {
154                 device->initialize();
155         }
156 }
157
158 VM::~VM()
159 {
160         // delete all devices
161         for(DEVICE* device = first_device; device;) {
162                 DEVICE *next_device = device->next_device;
163                 device->release();
164                 delete device;
165                 device = next_device;
166         }
167 }
168
169 DEVICE* VM::get_device(int id)
170 {
171         for(DEVICE* device = first_device; device; device = device->next_device) {
172                 if(device->this_device_id == id) {
173                         return device;
174                 }
175         }
176         return NULL;
177 }
178
179 // ----------------------------------------------------------------------------
180 // drive virtual machine
181 // ----------------------------------------------------------------------------
182
183 void VM::reset()
184 {
185         // reset all devices
186         for(DEVICE* device = first_device; device; device = device->next_device) {
187                 device->reset();
188         }
189 }
190
191 void VM::run()
192 {
193         event->drive();
194 }
195
196 // ----------------------------------------------------------------------------
197 // debugger
198 // ----------------------------------------------------------------------------
199
200 #ifdef USE_DEBUGGER
201 DEVICE *VM::get_cpu(int index)
202 {
203         if(index == 0) {
204                 return cpu;
205         }
206         return NULL;
207 }
208 #endif
209
210 // ----------------------------------------------------------------------------
211 // draw screen
212 // ----------------------------------------------------------------------------
213
214 void VM::draw_screen()
215 {
216         vdp->draw_screen();
217 }
218
219 // ----------------------------------------------------------------------------
220 // soud manager
221 // ----------------------------------------------------------------------------
222
223 void VM::initialize_sound(int rate, int samples)
224 {
225         // init sound manager
226         event->initialize_sound(rate, samples);
227         
228         // init sound gen
229         psg->init(rate, 3579545, samples, 0, 0);
230         pcm->init(rate, 8000);
231 #if defined(_PX7)
232         ldp->initialize_sound(rate, samples);
233 #endif
234 }
235
236 uint16* VM::create_sound(int* extra_frames)
237 {
238         return event->create_sound(extra_frames);
239 }
240
241 int VM::sound_buffer_ptr()
242 {
243         return event->sound_buffer_ptr();
244 }
245
246 #if defined(_PX7)
247 void VM::movie_sound_callback(uint8 *buffer, long size)
248 {
249         ldp->movie_sound_callback(buffer, size);
250 }
251 #endif
252
253 // ----------------------------------------------------------------------------
254 // user interface
255 // ----------------------------------------------------------------------------
256
257 void VM::open_cart(int drv, _TCHAR* file_path)
258 {
259         if(drv == 0) {
260                 slot1->open_cart(file_path);
261         } else {
262                 slot3->open_cart(file_path);
263         }
264         reset();
265 }
266
267 void VM::close_cart(int drv)
268 {
269         if(drv == 0) {
270                 slot1->close_cart();
271         } else {
272                 slot3->close_cart();
273         }
274         reset();
275 }
276
277 bool VM::cart_inserted(int drv)
278 {
279         if(drv == 0) {
280                 return slot1->cart_inserted();
281         } else {
282                 return slot3->cart_inserted();
283         }
284 }
285
286 void VM::play_tape(_TCHAR* file_path)
287 {
288         drec->play_tape(file_path);
289 }
290
291 void VM::rec_tape(_TCHAR* file_path)
292 {
293         drec->rec_tape(file_path);
294 }
295
296 void VM::close_tape()
297 {
298         drec->close_tape();
299 }
300
301 bool VM::tape_inserted()
302 {
303         return drec->tape_inserted();
304 }
305
306 int VM::get_tape_ptr(void)
307 {
308         return drec->get_tape_ptr();
309 }
310
311 #if defined(_PX7)
312 void VM::open_laser_disc(_TCHAR* file_path)
313 {
314         ldp->open_disc(file_path);
315 }
316
317 void VM::close_laser_disc()
318 {
319         ldp->close_disc();
320 }
321
322 bool VM::laser_disc_inserted()
323 {
324         return ldp->disc_inserted();
325 }
326 #else
327 void VM::open_disk(int drv, _TCHAR* file_path, int bank)
328 {
329         memory->open_disk(drv, file_path, bank);
330 }
331
332 void VM::close_disk(int drv)
333 {
334         memory->close_disk(drv);
335 }
336
337 bool VM::disk_inserted(int drv)
338 {
339         return memory->disk_inserted(drv);
340 }
341 void VM::write_protect_fd(int drv, bool flag)
342 {
343         //fdc->write_protect_fd(drv, flag);
344 }
345  bool VM::is_write_protect_fd(int drv)
346 {
347         //return fdc->is_write_protect_fd(drv);
348         return false;
349 }
350
351 //int VM::access_lamp()
352 //{
353 //      //uint32 status = fdc->read_signal(0);
354 //      //return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
355 //      return 0;
356 //}
357 #endif
358
359 bool VM::now_skip()
360 {
361         return event->now_skip();
362 }
363
364 void VM::update_config()
365 {
366         for(DEVICE* device = first_device; device; device = device->next_device) {
367                 device->update_config();
368         }
369 }
370
371 #define STATE_VERSION   1
372
373 void VM::save_state(FILEIO* state_fio)
374 {
375         state_fio->FputUint32(STATE_VERSION);
376         
377         for(DEVICE* device = first_device; device; device = device->next_device) {
378                 device->save_state(state_fio);
379         }
380 }
381
382 bool VM::load_state(FILEIO* state_fio)
383 {
384         if(state_fio->FgetUint32() != STATE_VERSION) {
385                 return false;
386         }
387         for(DEVICE* device = first_device; device; device = device->next_device) {
388                 if(!device->load_state(state_fio)) {
389                         return false;
390                 }
391         }
392         return true;
393 }
394