OSDN Git Service

[VM][WIP] Use namespace to devices per VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz80k / mz80k.cpp
index a868253..6a9dee0 100644 (file)
@@ -1,5 +1,5 @@
 /*
-       SHARP MZ-80K Emulator 'EmuZ-80K'
+       SHARP MZ-80K/C Emulator 'EmuZ-80K'
        SHARP MZ-1200 Emulator 'EmuZ-1200'
 
        Author : Takeda.Toshiya
 #include "../i8253.h"
 #include "../i8255.h"
 #include "../ls393.h"
+#include "../mz1p17.h"
+#include "../noise.h"
 #include "../pcm1bit.h"
+#include "../prnfile.h"
 #include "../z80.h"
 
 #ifdef USE_DEBUGGER
 #include "../debugger.h"
 #endif
 
-#include "display.h"
 #include "keyboard.h"
 #include "memory.h"
+#include "printer.h"
 
-#ifdef SUPPORT_MZ80AIF
+#if defined(SUPPORT_MZ80AIF)
 #include "../io.h"
 #include "../disk.h"
 #include "../mb8877.h"
-#include "floppy.h"
+#include "mz80aif.h"
+#elif defined(SUPPORT_MZ80FIO)
+#include "../io.h"
+#include "../disk.h"
+#include "../t3444a.h"
+#include "mz80fio.h"
+#endif
+
+using MZ80::KEYBOARD;
+using MZ80::MEMORY;
+using MZ80::PRINTER;
+#if defined(SUPPORT_MZ80AIF)
+using MZ80::MZ80AIF;
+#elif defined(SUPPORT_MZ80FIO)
+using MZ80::MZ80FIO;
 #endif
 
 // ----------------------------------------------------------------------------
 // initialize
 // ----------------------------------------------------------------------------
 
-VM::VM(EMU* parent_emu) : emu(parent_emu)
+VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
 {
        // create devices
        first_device = last_device = NULL;
        dummy = new DEVICE(this, emu);  // must be 1st device
        event = new EVENT(this, emu);   // must be 2nd device
+       dummy->set_device_name(_T("1st Dummy"));
        
 #if defined(_MZ1200) || defined(_MZ80A)
-       l_and = new AND(this, emu);
+       and_int = new AND(this, emu);
+       and_int->set_device_name(_T("AND Gate (Interrupts)"));
 #endif
        drec = new DATAREC(this, emu);
+       drec->set_context_noise_play(new NOISE(this, emu));
+       drec->set_context_noise_stop(new NOISE(this, emu));
+       drec->set_context_noise_fast(new NOISE(this, emu));
        ctc = new I8253(this, emu);
        pio = new I8255(this, emu);
        counter = new LS393(this, emu);
        pcm = new PCM1BIT(this, emu);
        cpu = new Z80(this, emu);
+       counter->set_device_name(_T("74LS393 Counter (CTC/Sound)"));
        
-       display = new DISPLAY(this, emu);
        keyboard = new KEYBOARD(this, emu);
        memory = new MEMORY(this, emu);
-       
-#ifdef SUPPORT_MZ80AIF
+       printer = new PRINTER(this, emu);
+#if defined(SUPPORT_MZ80AIF)
        io = new IO(this, emu);
        fdc = new MB8877(this, emu);    // mb8866
-       floppy = new FLOPPY(this, emu);
+       fdc->set_context_noise_seek(new NOISE(this, emu));
+       fdc->set_context_noise_head_down(new NOISE(this, emu));
+       fdc->set_context_noise_head_up(new NOISE(this, emu));
+       mz80aif = new MZ80AIF(this, emu);
+#elif defined(SUPPORT_MZ80FIO)
+       io = new IO(this, emu);
+       fdc = new T3444A(this, emu);    // t3444m
+       fdc->set_context_noise_seek(new NOISE(this, emu));
+       fdc->set_context_noise_head_down(new NOISE(this, emu));
+       fdc->set_context_noise_head_up(new NOISE(this, emu));
+       mz80fio = new MZ80FIO(this, emu);
 #endif
        
        // set contexts
        event->set_context_cpu(cpu);
        event->set_context_sound(pcm);
        event->set_context_sound(drec);
+#if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
+       event->set_context_sound(fdc->get_context_noise_seek());
+       event->set_context_sound(fdc->get_context_noise_head_down());
+       event->set_context_sound(fdc->get_context_noise_head_up());
+#endif
+       event->set_context_sound(drec->get_context_noise_play());
+       event->set_context_sound(drec->get_context_noise_stop());
+       event->set_context_sound(drec->get_context_noise_fast());
        
 #if defined(_MZ1200) || defined(_MZ80A)
-       l_and->set_context_out(cpu, SIG_CPU_IRQ, 1);
-       l_and->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
+       and_int->set_context_out(cpu, SIG_CPU_IRQ, 1);
+       and_int->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
 #endif
-       drec->set_context_out(pio, SIG_I8255_PORT_C, 0x20);
+       drec->set_context_ear(pio, SIG_I8255_PORT_C, 0x20);
        drec->set_context_remote(pio, SIG_I8255_PORT_C, 0x10);
        ctc->set_context_ch0(counter, SIG_LS393_CLK, 1);
        ctc->set_context_ch1(ctc, SIG_I8253_CLOCK_2, 1);
 #if defined(_MZ1200) || defined(_MZ80A)
-       ctc->set_context_ch2(l_and, SIG_AND_BIT_0, 1);
+       ctc->set_context_ch2(and_int, SIG_AND_BIT_0, 1);
 #else
        ctc->set_context_ch2(cpu, SIG_CPU_IRQ, 1);
 #endif
        ctc->set_constant_clock(0, 2000000);
        ctc->set_constant_clock(1, 31250);
        pio->set_context_port_a(keyboard, SIG_KEYBOARD_COLUMN, 0x0f, 0);
-       pio->set_context_port_c(display, SIG_DISPLAY_VGATE, 1, 0);
-       pio->set_context_port_c(drec, SIG_DATAREC_OUT, 2, 0);
+       pio->set_context_port_c(memory, SIG_MEMORY_VGATE, 1, 0);
+       pio->set_context_port_c(drec, SIG_DATAREC_MIC, 2, 0);
 #if defined(_MZ1200) || defined(_MZ80A)
-       pio->set_context_port_c(l_and, SIG_AND_BIT_1, 4, 0);
+       pio->set_context_port_c(and_int, SIG_AND_BIT_1, 4, 0);
 #endif
        pio->set_context_port_c(drec, SIG_DATAREC_TRIG, 8, 0);
        counter->set_context_1qa(pcm, SIG_PCM1BIT_SIGNAL, 1);
+       // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
+       //pcm->set_realtime_render(true);
        
-       display->set_vram_ptr(memory->get_vram());
-#if defined(_MZ80A)
-       display->set_e200_ptr(memory->get_e200());
-#endif
        keyboard->set_context_pio(pio);
        memory->set_context_ctc(ctc);
        memory->set_context_pio(pio);
-#if defined(_MZ1200) || defined(_MZ80A)
-       memory->set_context_disp(display);
-#endif
        
-#ifdef SUPPORT_MZ80AIF
+#if defined(SUPPORT_MZ80AIF)
        fdc->set_context_irq(memory, SIG_MEMORY_FDC_IRQ, 1);
        fdc->set_context_drq(memory, SIG_MEMORY_FDC_DRQ, 1);
-       floppy->set_context_fdc(fdc);
+       mz80aif->set_context_fdc(fdc);
+#elif defined(SUPPORT_MZ80FIO)
+       mz80fio->set_context_fdc(fdc);
 #endif
        
+       if(config.printer_type == 0) {  
+               printer->set_context_prn(new PRNFILE(this, emu));
+       } else if(config.printer_type == 1) {
+               MZ1P17 *mz1p17 = new MZ1P17(this, emu);
+               mz1p17->mode = MZ1P17_MODE_MZ80P4;
+               printer->set_context_prn(mz1p17);
+       } else {
+               printer->set_context_prn(dummy);
+       }
+       
        // cpu bus
        cpu->set_context_mem(memory);
-#ifdef SUPPORT_MZ80AIF
+#if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
        cpu->set_context_io(io);
 #else
        cpu->set_context_io(dummy);
@@ -131,21 +178,22 @@ VM::VM(EMU* parent_emu) : emu(parent_emu)
        cpu->set_context_debugger(new DEBUGGER(this, emu));
 #endif
        
-#ifdef SUPPORT_MZ80AIF
        // i/o bus
+#if defined(SUPPORT_MZ80AIF)
        io->set_iomap_range_rw(0xd8, 0xdb, fdc);
-       io->set_iomap_range_w(0xdc, 0xdd, floppy);
+       io->set_iomap_range_w(0xdc, 0xdd, mz80aif);
+#elif defined(SUPPORT_MZ80FIO)
+       io->set_iomap_range_rw(0xf8, 0xfb, mz80fio);
 #endif
+       io->set_iomap_range_rw(0xfe, 0xff, printer);
        
        // initialize all devices
+#if defined(__GIT_REPO_VERSION)
+       strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
+#endif
        for(DEVICE* device = first_device; device; device = device->next_device) {
                device->initialize();
        }
-#ifdef SUPPORT_MZ80AIF
-       for(int i = 0; i < MAX_DRIVE; i++) {
-               fdc->set_drive_type(i, DRIVE_TYPE_2DD);
-       }
-#endif
 }
 
 VM::~VM()
@@ -157,6 +205,20 @@ VM::~VM()
                delete device;
                device = next_device;
        }
+#if defined(SUPPORT_MZ80AIF)
+       for(int drv = 0; drv < MAX_DRIVE; drv++) {
+//             if(config.drive_type) {
+                       fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
+//             } else {
+//                     fdc->set_drive_type(drv, DRIVE_TYPE_2D);
+//             }
+       }
+#elif defined(SUPPORT_MZ80FIO)
+       for(int drv = 0; drv < MAX_DRIVE; drv++) {
+               fdc->set_drive_type(drv, DRIVE_TYPE_2D);
+//             fdc->set_drive_mfm(drv, false);
+       }
+#endif
 }
 
 DEVICE* VM::get_device(int id)
@@ -180,8 +242,8 @@ void VM::reset()
                device->reset();
        }
 #if defined(_MZ1200) || defined(_MZ80A)
-       l_and->write_signal(SIG_AND_BIT_0, 0, 1);       // CLOCK = L
-       l_and->write_signal(SIG_AND_BIT_1, 1, 1);       // INTMASK = H
+       and_int->write_signal(SIG_AND_BIT_0, 0, 1);     // CLOCK = L
+       and_int->write_signal(SIG_AND_BIT_1, 1, 1);     // INTMASK = H
 #endif
 }
 
@@ -210,17 +272,9 @@ DEVICE *VM::get_cpu(int index)
 
 void VM::draw_screen()
 {
-       display->draw_screen();
+       memory->draw_screen();
 }
 
-#ifdef SUPPORT_MZ80AIF
-int VM::access_lamp()
-{
-       uint32 status = fdc->read_signal(0);
-       return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
-}
-#endif
-
 // ----------------------------------------------------------------------------
 // soud manager
 // ----------------------------------------------------------------------------
@@ -231,112 +285,186 @@ void VM::initialize_sound(int rate, int samples)
        event->initialize_sound(rate, samples);
        
        // init sound gen
-       pcm->init(rate, 8000);
+       pcm->initialize_sound(rate, 8000);
 }
 
-uint16* VM::create_sound(int* extra_frames)
+uint16_t* VM::create_sound(int* extra_frames)
 {
        return event->create_sound(extra_frames);
 }
 
-int VM::sound_buffer_ptr()
+int VM::get_sound_buffer_ptr()
+{
+       return event->get_sound_buffer_ptr();
+}
+
+#ifdef USE_SOUND_VOLUME
+void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
+{
+       if(ch-- == 0) {
+               pcm->set_volume(0, decibel_l, decibel_r);
+       } else if(ch-- == 0) {
+               drec->set_volume(0, decibel_l, decibel_r);
+#if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
+       } else if(ch-- == 0) {
+               fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
+               fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
+               fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
+#endif
+       } else if(ch-- == 0) {
+               drec->get_context_noise_play()->set_volume(0, decibel_l, decibel_r);
+               drec->get_context_noise_stop()->set_volume(0, decibel_l, decibel_r);
+               drec->get_context_noise_fast()->set_volume(0, decibel_l, decibel_r);
+       }
+}
+#endif
+
+// ----------------------------------------------------------------------------
+// notify key
+// ----------------------------------------------------------------------------
+
+void VM::key_down(int code, bool repeat)
+{
+       if(!repeat) {
+               keyboard->key_down(code);
+       }
+}
+
+void VM::key_up(int code)
+{
+//     keyboard->key_up(code);
+}
+
+bool VM::get_caps_locked()
+{
+       return keyboard->get_caps_locked();
+}
+
+bool VM::get_kana_locked()
 {
-       return event->sound_buffer_ptr();
+       return keyboard->get_kana_locked();
 }
 
 // ----------------------------------------------------------------------------
 // user interface
 // ----------------------------------------------------------------------------
 
-#ifdef SUPPORT_MZ80AIF
-void VM::open_disk(int drv, _TCHAR* file_path, int bank)
+#if defined(SUPPORT_MZ80AIF) || defined(SUPPORT_MZ80FIO)
+void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
 {
        fdc->open_disk(drv, file_path, bank);
+       
+#if defined(SUPPORT_MZ80AIF)
+       if(fdc->get_media_type(drv) == MEDIA_TYPE_2DD) {
+               if(fdc->get_drive_type(drv) == DRIVE_TYPE_2D) {
+                       fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
+               }
+       } else if(fdc->get_media_type(drv) == MEDIA_TYPE_2D) {
+               if(fdc->get_drive_type(drv) == DRIVE_TYPE_2DD) {
+                       fdc->set_drive_type(drv, DRIVE_TYPE_2D);
+               }
+       }
+#endif
 }
 
-void VM::close_disk(int drv)
+void VM::close_floppy_disk(int drv)
 {
        fdc->close_disk(drv);
 }
 
-bool VM::disk_inserted(int drv)
+bool VM::is_floppy_disk_inserted(int drv)
+{
+       return fdc->is_disk_inserted(drv);
+}
+
+void VM::is_floppy_disk_protected(int drv, bool value)
 {
-       return fdc->disk_inserted(drv);
+       fdc->is_disk_protected(drv, value);
 }
 
+bool VM::is_floppy_disk_protected(int drv)
+{
+       return fdc->is_disk_protected(drv);
+}
+
+uint32_t VM::is_floppy_disk_accessed()
+{
+       return fdc->read_signal(0);
+}
 #endif
 
-void VM::play_tape(_TCHAR* file_path)
+
+void VM::play_tape(int drv, const _TCHAR* file_path)
 {
        drec->play_tape(file_path);
-       drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
+//     drec->set_remote(true);
 }
 
-void VM::rec_tape(_TCHAR* file_path)
+void VM::rec_tape(int drv, const _TCHAR* file_path)
 {
        drec->rec_tape(file_path);
-       drec->write_signal(SIG_DATAREC_REMOTE, 1, 1);
+//     drec->set_remote(true);
 }
 
-void VM::close_tape()
+void VM::close_tape(int drv)
 {
+       emu->lock_vm();
        drec->close_tape();
-       drec->write_signal(SIG_DATAREC_REMOTE, 0, 0);
+       emu->unlock_vm();
+//     drec->set_remote(false);
 }
 
-bool VM::tape_inserted()
+bool VM::is_tape_inserted(int drv)
 {
-       return drec->tape_inserted();
+       return drec->is_tape_inserted();
 }
 
-#if defined(USE_TAPE_PTR)
-int VM::get_tape_ptr()
+bool VM::is_tape_playing(int drv)
 {
-       return drec->get_tape_ptr();
+       return drec->is_tape_playing();
 }
 
-void VM::set_disk_protected(int drv, bool value)
+bool VM::is_tape_recording(int drv)
 {
-       fdc->set_disk_protected(drv, value);
+       return drec->is_tape_recording();
 }
 
-bool VM::get_disk_protected(int drv)
+int VM::get_tape_position(int drv)
 {
-       return fdc->get_disk_protected(drv);
+       return drec->get_tape_position();
 }
-#endif
 
-void VM::push_play()
+const _TCHAR* VM::get_tape_message(int drv)
+{
+       return drec->get_message();
+}
+
+void VM::push_play(int drv)
 {
        drec->set_ff_rew(0);
        drec->set_remote(true);
 }
 
-void VM::push_stop()
+void VM::push_stop(int drv)
 {
        drec->set_remote(false);
 }
 
-void VM::push_fast_forward()
+void VM::push_fast_forward(int drv)
 {
        drec->set_ff_rew(1);
        drec->set_remote(true);
 }
 
-void VM::push_fast_rewind()
+void VM::push_fast_rewind(int drv)
 {
        drec->set_ff_rew(-1);
        drec->set_remote(true);
 }
 
-bool VM::get_tape_play(void)
-{
-       return drec->get_tape_play();
-}
-
-bool VM::now_skip()
+bool VM::is_frame_skippable()
 {
-       return event->now_skip();
+       return event->is_frame_skippable();
 }
 
 void VM::update_config()
@@ -346,27 +474,38 @@ void VM::update_config()
        }
 }
 
-#define STATE_VERSION  2
+#define STATE_VERSION  7
 
-void VM::save_state(FILEIO* state_fio)
+bool VM::process_state(FILEIO* state_fio, bool loading)
 {
-       state_fio->FputUint32(STATE_VERSION);
-       
-       for(DEVICE* device = first_device; device; device = device->next_device) {
-               device->save_state(state_fio);
-       }
-}
-
-bool VM::load_state(FILEIO* state_fio)
-{
-       if(state_fio->FgetUint32() != STATE_VERSION) {
+       if(!state_fio->StateCheckUint32(STATE_VERSION)) {
                return false;
        }
-       for(DEVICE* device = first_device; device; device = device->next_device) {
-               if(!device->load_state(state_fio)) {
+       for(DEVICE* device = first_device; device; device = device->next_device) {
+               // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
+               // const char *name = typeid(*device).name();
+               //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
+               const char *name = device->get_device_name();
+               int len = strlen(name);
+               
+               if(!state_fio->StateCheckInt32(len)) {
+                       if(loading) {
+                               printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
+                       }
                        return false;
                }
-       }
+               if(!state_fio->StateCheckBuffer(name, len, 1)) {
+                       if(loading) {
+                               printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
+                       }
+                       return false;
+               }
+               if(!device->process_state(state_fio, loading)) {
+                       if(loading) {
+                               printf("Data loading Error: DEVID=%d\n", device->this_device_id);
+                       }
+                       return false;
+               }
+       }
        return true;
 }
-