OSDN Git Service

[VM][STATE] Apply new state framework to some devices a lot (excepts some devices...
[csp-qt/common_source_project-fm7.git] / source / src / vm / pc6001 / pc6001.cpp
1 /*
2         NEC PC-6001 Emulator 'yaPC-6001'
3         NEC PC-6001mkII Emulator 'yaPC-6201'
4         NEC PC-6001mkIISR Emulator 'yaPC-6401'
5         NEC PC-6601 Emulator 'yaPC-6601'
6         NEC PC-6601SR Emulator 'yaPC-6801'
7
8         Author : tanam
9         Date   : 2013.07.15-
10
11         [ virtual machine ]
12 */
13
14 #include "pc6001.h"
15 #include "../../emu.h"
16 #include "../device.h"
17 #include "../event.h"
18
19 #include "../disk.h"
20 #include "../i8255.h"
21 #include "../io.h"
22 #ifdef _PC6001
23 #include "../mc6847.h"
24 #include "display.h"
25 #else
26 #include "../upd7752.h"
27 #endif
28 #include "../noise.h"
29 #include "../pc6031.h"
30 #include "../pc80s31k.h"
31 #include "../prnfile.h"
32 #include "../upd765a.h"
33 #if defined(_PC6001MK2SR) || defined(_PC6601SR)
34 #include "../ym2203.h"
35 #else
36 #include "../ay_3_891x.h"
37 #endif
38 #include "../z80.h"
39
40 #include "../datarec.h"
41 #include "../mcs48.h"
42
43 #ifdef USE_DEBUGGER
44 #include "../debugger.h"
45 #endif
46
47 #if defined(_PC6601) || defined(_PC6601SR)
48 #include "floppy.h"
49 #endif
50 #include "joystick.h"
51 #include "memory.h"
52 #include "psub.h"
53 #include "sub.h"
54 #include "timer.h"
55
56 // ----------------------------------------------------------------------------
57 // initialize
58 // ----------------------------------------------------------------------------
59
60 VM::VM(EMU* parent_emu) : emu(parent_emu)
61 {
62         support_pc80s31k = FILEIO::IsFileExisting(create_local_path(_T("DISK.ROM")));
63 #ifdef _PC6601SR
64         support_sub_cpu = false;
65 #else
66         support_sub_cpu = FILEIO::IsFileExisting(create_local_path(_T(SUB_CPU_ROM_FILE_NAME)));
67 #endif
68         
69         // create devices
70         first_device = last_device = NULL;
71         dummy = new DEVICE(this, emu);  // must be 1st device
72         event = new EVENT(this, emu);   // must be 2nd device
73         
74         pio_sub = new I8255(this, emu);
75         io = new IO(this, emu);
76         noise_seek = new NOISE(this, emu);
77         noise_head_down = new NOISE(this, emu);
78         noise_head_up = new NOISE(this, emu);
79 #if defined(_PC6001MK2SR) || defined(_PC6601SR)
80         psg = new YM2203(this, emu);
81 #else
82         psg = new AY_3_891X(this, emu);
83 #endif
84         cpu = new Z80(this, emu);
85         
86         if(config.printer_type == 0) {
87                 printer = new PRNFILE(this, emu);
88         } else {
89                 printer = dummy;
90         }
91         
92 #if defined(_PC6601) || defined(_PC6601SR)
93         floppy = new FLOPPY(this, emu);
94         floppy->set_context_noise_seek(noise_seek);
95 //      floppy->set_context_noise_head_down(noise_head_down);
96 //      floppy->set_context_noise_head_up(noise_head_up);
97 #endif
98         joystick = new JOYSTICK(this, emu);
99         memory = new MEMORY(this, emu);
100         timer = new TIMER(this, emu);
101         
102         // set contexts
103         event->set_context_cpu(cpu);
104         event->set_context_sound(psg);
105         event->set_context_sound(noise_seek);
106         event->set_context_sound(noise_head_down);
107         event->set_context_sound(noise_head_up);
108         
109         pio_sub->set_context_port_b(printer, SIG_PRINTER_DATA, 0xff, 0);
110         pio_sub->set_context_port_c(printer, SIG_PRINTER_STROBE, 0x01, 0);
111         pio_sub->set_context_port_c(memory, SIG_MEMORY_PIO_PORT_C, 0x06, 0);    // CRTKILL,CGSWN
112         
113 #ifdef _PC6001
114         display = new DISPLAY(this, emu);
115         vdp = new MC6847(this, emu);
116         display->set_context_vdp(vdp);
117         display->set_vram_ptr(memory->get_vram());
118         display->set_context_timer(timer);
119         vdp->load_font_image(create_local_path(_T("CGROM60.60")));
120         vdp->set_context_cpu(cpu);
121         pio_sub->set_context_port_c(vdp, SIG_MC6847_ENABLE, 0x02, 0);   // CRTKILL
122 #else
123         voice = new UPD7752(this, emu);
124         event->set_context_sound(voice);
125         memory->set_context_timer(timer);
126 #endif
127         memory->set_context_cpu(cpu);
128         joystick->set_context_psg(psg);
129         
130         timer->set_context_cpu(cpu);
131 #ifndef _PC6001
132         timer->set_context_memory(memory);
133 #endif
134         if(support_sub_cpu) {
135                 cpu_sub = new MCS48(this, emu);
136                 cpu_sub->set_device_name(_T("MCS48 MCU (Sub)"));
137                 sub = new SUB(this, emu);
138                 drec = new DATAREC(this, emu);
139                 drec->set_device_name(_T("Data Recorder (Sub)"));
140                 drec->set_context_noise_play(new NOISE(this, emu));
141                 drec->set_context_noise_stop(new NOISE(this, emu));
142                 drec->set_context_noise_fast(new NOISE(this, emu));
143                 event->set_context_cpu(cpu_sub, 8000000);
144                 event->set_context_sound(drec);
145                 event->set_context_sound(drec->get_context_noise_play());
146                 event->set_context_sound(drec->get_context_noise_stop());
147                 event->set_context_sound(drec->get_context_noise_fast());
148                 cpu_sub->set_context_mem(new MCS48MEM(this, emu));
149                 cpu_sub->set_context_io(sub);
150 #ifdef USE_DEBUGGER
151                 cpu_sub->set_context_debugger(new DEBUGGER(this, emu));
152 #endif
153                 sub->set_context_pio(pio_sub);
154                 sub->set_context_drec(drec);
155                 sub->set_context_timer(timer);
156                 pio_sub->set_context_port_c(cpu_sub, SIG_CPU_IRQ, 0x80, 0);
157                 drec->set_context_ear(sub, SIG_SUB_DATAREC, 1);
158                 timer->set_context_sub(sub);
159         } else {
160                 psub = new PSUB(this, emu);
161                 psub->set_context_pio(pio_sub);
162                 psub->set_context_timer(timer);
163                 timer->set_context_sub(psub);
164                 cpu_sub = NULL;
165         }
166         if(support_pc80s31k) {
167                 pio_fdd = new I8255(this, emu);
168                 pio_fdd->set_device_name(_T("8255 PIO (FDD I/F)"));
169                 pio_pc80s31k = new I8255(this, emu);
170                 pio_pc80s31k->set_device_name(_T("8255 PIO (320KB FDD)"));
171                 pc80s31k = new PC80S31K(this, emu);
172                 pc80s31k->set_device_name(_T("PC-80S31K (320KB FDD)"));
173                 fdc_pc80s31k = new UPD765A(this, emu);
174                 fdc_pc80s31k->set_device_name(_T("uPD765A FDC (320KB FDD)"));
175                 cpu_pc80s31k = new Z80(this, emu);
176                 cpu_pc80s31k->set_device_name(_T("Z80 CPU (320KB FDD)"));
177                 
178                 event->set_context_cpu(cpu_pc80s31k, 4000000);
179                 
180                 pc80s31k->set_context_cpu(cpu_pc80s31k);
181                 pc80s31k->set_context_fdc(fdc_pc80s31k);
182                 pc80s31k->set_context_pio(pio_pc80s31k);
183                 pio_fdd->set_context_port_a(pio_pc80s31k, SIG_I8255_PORT_B, 0xff, 0);
184                 pio_fdd->set_context_port_b(pio_pc80s31k, SIG_I8255_PORT_A, 0xff, 0);
185                 pio_fdd->set_context_port_c(pio_pc80s31k, SIG_I8255_PORT_C, 0x0f, 4);
186                 pio_fdd->set_context_port_c(pio_pc80s31k, SIG_I8255_PORT_C, 0xf0, -4);
187                 pio_fdd->clear_ports_by_cmdreg = true;
188                 pio_pc80s31k->set_context_port_a(pio_fdd, SIG_I8255_PORT_B, 0xff, 0);
189                 pio_pc80s31k->set_context_port_b(pio_fdd, SIG_I8255_PORT_A, 0xff, 0);
190                 pio_pc80s31k->set_context_port_c(pio_fdd, SIG_I8255_PORT_C, 0x0f, 4);
191                 pio_pc80s31k->set_context_port_c(pio_fdd, SIG_I8255_PORT_C, 0xf0, -4);
192                 pio_pc80s31k->clear_ports_by_cmdreg = true;
193                 fdc_pc80s31k->set_context_irq(cpu_pc80s31k, SIG_CPU_IRQ, 1);
194                 fdc_pc80s31k->set_context_noise_seek(noise_seek);
195                 fdc_pc80s31k->set_context_noise_head_down(noise_head_down);
196                 fdc_pc80s31k->set_context_noise_head_up(noise_head_up);
197                 cpu_pc80s31k->set_context_mem(pc80s31k);
198                 cpu_pc80s31k->set_context_io(pc80s31k);
199                 cpu_pc80s31k->set_context_intr(pc80s31k);
200 #ifdef USE_DEBUGGER
201                 cpu_pc80s31k->set_context_debugger(new DEBUGGER(this, emu));
202 #endif
203 #if defined(_PC6601) || defined(_PC6601SR)
204                 floppy->set_context_ext(pio_fdd);
205 #endif
206         } else {
207                 pc6031 = new PC6031(this, emu);
208                 pc6031->set_context_noise_seek(noise_seek);
209 //              pc6031->set_context_noise_head_down(noise_head_down);
210 //              pc6031->set_context_noise_head_up(noise_head_up);
211 #if defined(_PC6601) || defined(_PC6601SR)
212                 floppy->set_context_ext(pc6031);
213 #endif
214                 cpu_pc80s31k = NULL;
215         }
216         
217         // cpu bus
218         cpu->set_context_mem(memory);
219         cpu->set_context_io(io);
220         cpu->set_context_intr(timer);
221 #ifdef USE_DEBUGGER
222         cpu->set_context_debugger(new DEBUGGER(this, emu));
223 #endif
224         
225         // i/o bus
226         if(support_sub_cpu) {
227                 io->set_iomap_range_rw(0x90, 0x93, sub);
228         } else {
229                 io->set_iomap_range_rw(0x90, 0x93, psub);
230         }
231         io->set_iomap_alias_w(0xa0, psg, 0);                    // PSG ch
232         io->set_iomap_alias_w(0xa1, psg, 1);                    // PSG data
233         io->set_iomap_alias_r(0xa2, psg, 1);                    // PSG data
234 #if defined(_PC6601SR) || defined(_PC6001MK2SR)
235         io->set_iomap_alias_r(0xa3, psg, 0);                    // FM status
236         io->set_iomap_range_rw(0x40, 0x6f, memory);             // VRAM addr
237 #endif
238 #ifdef _PC6001
239         io->set_iomap_single_w(0xb0, display);                  // VRAM addr
240         io->set_iomap_single_w(0x00, memory);                   // MEMORY MAP
241 #else
242         io->set_iomap_single_w(0xb0, memory);                   // VRAM addr
243         io->set_iomap_range_rw(0xc0, 0xcf, memory);             // VRAM addr
244         io->set_iomap_range_rw(0xe0, 0xe3, voice);              // VOICE
245 #if defined(_PC6601) || defined(_PC6601SR)
246         io->set_iomap_range_rw(0xb8, 0xbf, timer);              // IRQ
247         io->set_iomap_range_rw(0xfa, 0xfb, timer);              // IRQ
248 #endif
249         io->set_iomap_range_rw(0xf3, 0xf7, timer);              // IRQ/Timer
250 #endif
251         
252 #if defined(_PC6601) || defined(_PC6601SR)
253         io->set_iomap_range_rw(0xb1, 0xb3, floppy);             // DISK DRIVE
254         io->set_iomap_range_rw(0xb5, 0xb7, floppy);             // DISK DRIVE (Mirror)
255         io->set_iomap_range_rw(0xd0, 0xde, floppy);             // DISK DRIVE
256 #else
257         io->set_iovalue_single_r(0xb1, 0x01);
258 #if defined(_PC6601SR)
259         io->set_iovalue_single_r(0xb2, 0x02);
260 #elif defined(_PC6001MK2SR)
261         io->set_iovalue_single_r(0xb2, 0x00);
262 #endif
263         
264         if(support_pc80s31k) {
265                 io->set_iomap_range_rw(0xd0, 0xd2, pio_fdd);
266                 io->set_iomap_single_w(0xd3, pio_fdd);
267         } else {
268                 io->set_iomap_range_rw(0xd0, 0xd3, pc6031);
269         }
270 #endif
271         io->set_iomap_range_rw(0xf0, 0xf2, memory);             // MEMORY MAP
272         
273         // initialize all devices
274         for(DEVICE* device = first_device; device; device = device->next_device) {
275                 device->initialize();
276         }
277         decl_state();
278         if(support_sub_cpu) {
279                 // load rom images after cpustate is allocated
280 #ifdef _PC6601SR
281 #else
282                 cpu_sub->load_rom_image(create_local_path(_T(SUB_CPU_ROM_FILE_NAME)));
283 #endif
284         }
285         int drive_num = 0;
286 #if defined(_PC6601) || defined(_PC6601SR)
287         floppy->get_disk_handler(0)->drive_num = drive_num++;
288         floppy->get_disk_handler(1)->drive_num = drive_num++;
289 #endif
290         if(support_pc80s31k) {
291                 fdc_pc80s31k->get_disk_handler(0)->drive_num = drive_num++;
292                 fdc_pc80s31k->get_disk_handler(1)->drive_num = drive_num++;
293         } else {
294                 pc6031->get_disk_handler(0)->drive_num = drive_num++;
295                 pc6031->get_disk_handler(1)->drive_num = drive_num++;
296         }
297 }
298
299 VM::~VM()
300 {
301         // delete all devices
302         for(DEVICE* device = first_device; device;) {
303                 DEVICE *next_device = device->next_device;
304                 device->release();
305                 delete device;
306                 device = next_device;
307         }
308 }
309
310 DEVICE* VM::get_device(int id)
311 {
312         for(DEVICE* device = first_device; device; device = device->next_device) {
313                 if(device->this_device_id == id) {
314                         return device;
315                 }
316         }
317         return NULL;
318 }
319
320 // ----------------------------------------------------------------------------
321 // drive virtual machine
322 // ----------------------------------------------------------------------------
323 void VM::reset()
324 {
325         // reset all devices
326         for(DEVICE* device = first_device; device; device = device->next_device) {
327                 device->reset();
328         }
329         if(support_pc80s31k) {
330                 pio_fdd->write_signal(SIG_I8255_PORT_C, 0, 0xff);
331                 pio_pc80s31k->write_signal(SIG_I8255_PORT_C, 0, 0xff);
332         }
333 }
334
335 void VM::run()
336 {
337         event->drive();
338 }
339
340 // ----------------------------------------------------------------------------
341 // debugger
342 // ----------------------------------------------------------------------------
343
344 #ifdef USE_DEBUGGER
345 DEVICE *VM::get_cpu(int index)
346 {
347         if(index == 0) {
348                 return cpu;
349         } else if(index == 1) {
350                 return cpu_sub;
351         } else if(index == 2) {
352                 return cpu_pc80s31k;
353         }
354         return NULL;
355 }
356 #endif
357
358 // ----------------------------------------------------------------------------
359 // draw screen
360 // ----------------------------------------------------------------------------
361
362 void VM::draw_screen()
363 {
364 #ifdef _PC6001
365         display->draw_screen();
366 #else
367         memory->draw_screen();
368 #endif
369 }
370 // ----------------------------------------------------------------------------
371 // soud manager
372 // ----------------------------------------------------------------------------
373
374 void VM::initialize_sound(int rate, int samples)
375 {
376         // init sound manager
377         event->initialize_sound(rate, samples);
378         
379         // init sound gen
380         psg->initialize_sound(rate, 4000000, samples, 0, 0);
381 #ifndef _PC6001
382         voice->initialize_sound(rate);
383 #endif
384 }
385
386 uint16_t* VM::create_sound(int* extra_frames)
387 {
388         return event->create_sound(extra_frames);
389 }
390
391 int VM::get_sound_buffer_ptr()
392 {
393         return event->get_sound_buffer_ptr();
394 }
395
396 #ifdef USE_SOUND_VOLUME
397 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
398 {
399         if(ch-- == 0) {
400                 psg->set_volume(1, decibel_l, decibel_r);
401 #if !defined(_PC6001)
402         } else if(ch-- == 0) {
403                 voice->set_volume(0, decibel_l, decibel_r);
404 #endif
405         } else if(ch-- == 0) {
406                 if(support_sub_cpu) {
407                         drec->set_volume(0, decibel_l, decibel_r);
408                 }
409         } else if(ch-- == 0) {
410                 noise_seek->set_volume(0, decibel_l, decibel_r);
411                 noise_head_down->set_volume(0, decibel_l, decibel_r);
412                 noise_head_up->set_volume(0, decibel_l, decibel_r);
413         } else if(ch-- == 0) {
414                 if(support_sub_cpu) {
415                         drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
416                         drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
417                         drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
418                 }
419         }
420 }
421 #endif
422
423 // ----------------------------------------------------------------------------
424 // notify key
425 // ----------------------------------------------------------------------------
426
427 void VM::key_down(int code, bool repeat)
428 {
429         if(!support_sub_cpu) {
430                 psub->key_down(code);
431         }
432 }
433
434 void VM::key_up(int code)
435 {
436         if(!support_sub_cpu) {
437                 psub->key_up(code);
438         }
439 }
440
441 // ----------------------------------------------------------------------------
442 // user interface
443 // ----------------------------------------------------------------------------
444
445 void VM::open_cart(int drv, const _TCHAR* file_path)
446 {
447         if(drv == 0) {
448                 memory->open_cart(file_path);
449                 reset();
450         }
451 }
452
453 void VM::close_cart(int drv)
454 {
455         if(drv == 0) {
456                 memory->close_cart();
457                 reset();
458         }
459 }
460
461 bool VM::is_cart_inserted(int drv)
462 {
463         if(drv == 0) {
464                 return memory->is_cart_inserted();
465         } else {
466                 return false;
467         }
468 }
469
470 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
471 {
472 #if defined(_PC6601) || defined(_PC6601SR)
473         if(drv < 2) {
474                 floppy->open_disk(drv, file_path, bank);
475                 return;
476         } else {
477                 drv -= 2;
478         }
479 #endif
480         if(support_pc80s31k) {
481                 fdc_pc80s31k->open_disk(drv, file_path, bank);
482         } else {
483                 pc6031->open_disk(drv, file_path, bank);
484         }
485 }
486
487 void VM::close_floppy_disk(int drv)
488 {
489 #if defined(_PC6601) || defined(_PC6601SR)
490         if(drv < 2) {
491                 floppy->close_disk(drv);
492                 return;
493         } else {
494                 drv -= 2;
495         }
496 #endif
497         if(support_pc80s31k) {
498                 fdc_pc80s31k->close_disk(drv);
499         } else {
500                 pc6031->close_disk(drv);
501         }
502 }
503
504 bool VM::is_floppy_disk_inserted(int drv)
505 {
506 #if defined(_PC6601) || defined(_PC6601SR)
507         if(drv < 2) {
508                 return floppy->is_disk_inserted(drv);
509         } else {
510                 drv -= 2;
511         }
512 #endif
513         if(support_pc80s31k) {
514                 return fdc_pc80s31k->is_disk_inserted(drv);
515         } else {
516                 return pc6031->is_disk_inserted(drv);
517         }
518 }
519
520 void VM::is_floppy_disk_protected(int drv, bool value)
521 {
522 #if defined(_PC6601) || defined(_PC6601SR)
523         if(drv < 2) {
524                 floppy->is_disk_protected(drv, value);
525                 return;
526         } else {
527                 drv -= 2;
528         }
529 #endif
530         if(support_pc80s31k) {
531                 fdc_pc80s31k->is_disk_protected(drv, value);
532         } else {
533                 pc6031->is_disk_protected(drv, value);
534         }
535 }
536
537 bool VM::is_floppy_disk_protected(int drv)
538 {
539 #if defined(_PC6601) || defined(_PC6601SR)
540         if(drv < 2) {
541                 return floppy->is_disk_protected(drv);
542         } else {
543                 drv -= 2;
544         }
545 #endif
546         if(support_pc80s31k) {
547                 return fdc_pc80s31k->is_disk_protected(drv);
548         } else {
549                 return pc6031->is_disk_protected(drv);
550         }
551 }
552
553 uint32_t VM::is_floppy_disk_accessed()
554 {
555         uint32_t status = 0; /// fdc->read_signal(0);
556         if(support_pc80s31k) {
557                 status |= fdc_pc80s31k->read_signal(0);
558         } else {
559                 status |= pc6031->read_signal(0);
560         }
561 #if defined(_PC6601) || defined(_PC6601SR)
562         status <<= 2;
563         status |= floppy->read_signal(0);
564 #endif
565         return status;
566 }
567
568 void VM::play_tape(int drv, const _TCHAR* file_path)
569 {
570         if(support_sub_cpu) {
571                 // support both p6/p6t and wav
572                 drec->play_tape(file_path);
573 //              drec->set_remote(true);
574         } else {
575                 // support only p6/p6t
576                 psub->play_tape(file_path);
577         }
578 }
579
580 void VM::rec_tape(int drv, const _TCHAR* file_path)
581 {
582         if(support_sub_cpu) {
583                 // support both p6/p6t and wav
584                 sub->rec_tape(file_path);       // temporary
585 //              drec->rec_tape(file_path);
586 //              drec->set_remote(true);
587         } else {
588                 // support both p6/p6t and wav
589                 psub->rec_tape(file_path);
590         }
591 }
592
593 void VM::close_tape(int drv)
594 {
595         if(support_sub_cpu) {
596                 if(sub->is_tape_inserted()) {
597                         sub->close_tape();      // temporary
598                 } else {
599                         emu->lock_vm();
600                         drec->close_tape();
601                         emu->unlock_vm();
602 //                      drec->set_remote(false);
603                 }
604         } else {
605                 psub->close_tape();
606         }
607 }
608
609 bool VM::is_tape_inserted(int drv)
610 {
611         if(support_sub_cpu) {
612                 return drec->is_tape_inserted() || sub->is_tape_inserted();
613         } else {
614                 return psub->is_tape_inserted();
615         }
616 }
617
618 bool VM::is_tape_playing(int drv)
619 {
620         if(support_sub_cpu) {
621                 return drec->is_tape_playing();
622         } else {
623                 return false;
624         }
625 }
626
627 bool VM::is_tape_recording(int drv)
628 {
629         if(support_sub_cpu) {
630                 return drec->is_tape_recording();
631         } else {
632                 return false;
633         }
634 }
635
636 int VM::get_tape_position(int drv)
637 {
638         if(support_sub_cpu) {
639                 return drec->get_tape_position();
640         } else {
641                 return 0;
642         }
643 }
644
645 const _TCHAR* VM::get_tape_message(int drv)
646 {
647         if(support_sub_cpu) {
648                 return drec->get_message();
649         } else {
650                 return NULL;
651         }
652 }
653
654 void VM::push_play(int drv)
655 {
656         if(support_sub_cpu) {
657                 drec->set_ff_rew(0);
658                 drec->set_remote(true);
659         }
660 }
661
662 void VM::push_stop(int drv)
663 {
664         if(support_sub_cpu) {
665                 drec->set_remote(false);
666         }
667 }
668
669 void VM::push_fast_forward(int drv)
670 {
671         if(support_sub_cpu) {
672                 drec->set_ff_rew(1);
673                 drec->set_remote(true);
674         }
675 }
676
677 void VM::push_fast_rewind(int drv)
678 {
679         if(support_sub_cpu) {
680                 drec->set_ff_rew(-1);
681                 drec->set_remote(true);
682         }
683 }
684
685 bool VM::is_frame_skippable()
686 {
687         return event->is_frame_skippable();
688 }
689
690 void VM::update_config()
691 {
692         for(DEVICE* device = first_device; device; device = device->next_device) {
693                 device->update_config();
694         }
695 }
696
697 #define STATE_VERSION   6
698
699 #include "../../statesub.h"
700 #include "../../qt/gui/csp_logger.h"
701 extern CSP_Logger DLL_PREFIX_I *csp_logger;
702
703 void VM::decl_state(void)
704 {
705 #if defined(_PC6001)
706         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PC_6001_HEAD")), csp_logger);
707 #elif defined(_PC6001MK2)
708         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PC_6001_MK2_HEAD")), csp_logger);
709 #elif defined(_PC6001MK2SR)
710         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PC_6001_MK2_SR_HEAD")), csp_logger);
711 #elif defined(_PC6601)
712         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PC_6601_HEAD")), csp_logger);
713 #elif defined(_PC601SR)
714         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PC_6601_SR_HEAD")), csp_logger);
715 #else
716         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PC_6001_SERIES_HEAD")), csp_logger);
717 #endif
718         DECL_STATE_ENTRY_INT32(sr_mode);
719         for(DEVICE* device = first_device; device; device = device->next_device) {
720                 device->decl_state();
721         }
722 }
723
724 void VM::save_state(FILEIO* state_fio)
725 {
726         //state_fio->FputUint32(STATE_VERSION);
727         
728         if(state_entry != NULL) {
729                 state_entry->save_state(state_fio);
730         }
731         for(DEVICE* device = first_device; device; device = device->next_device) {
732                 device->save_state(state_fio);
733         }
734         //state_fio->FputInt32(sr_mode);
735 }
736
737 bool VM::load_state(FILEIO* state_fio)
738 {
739         //if(state_fio->FgetUint32() != STATE_VERSION) {
740         //      return false;
741         //}
742         bool mb = false;
743         if(state_entry != NULL) {
744                 mb = state_entry->load_state(state_fio);
745         }
746         if(!mb) {
747                 emu->out_debug_log("INFO: HEADER DATA ERROR");
748                 return false;
749         }
750         for(DEVICE* device = first_device; device; device = device->next_device) {
751                 if(!device->load_state(state_fio)) {
752                         return false;
753                 }
754         }
755         //sr_mode = state_fio->FgetInt32();
756         return true;
757 }
758