OSDN Git Service

[General] Merge Updtream 2020-08-10.
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz2500 / mz2500.cpp
index 8e85cce..6f082a4 100644 (file)
 
 #include "../datarec.h"
 #include "../disk.h"
+#include "../harddisk.h"
 #include "../i8253.h"
 #include "../i8255.h"
 #include "../io.h"
 #include "../mb8877.h"
+#include "../mz1p17.h"
+#include "../noise.h"
 #include "../pcm1bit.h"
+//#include "../pcpr201.h"
+#include "../prnfile.h"
 #include "../rp5c01.h"
+#include "../scsi_hdd.h"
+#include "../scsi_host.h"
 #include "../w3100a.h"
 #include "../ym2203.h"
 #include "../z80.h"
 #include "interrupt.h"
 #include "joystick.h"
 #include "keyboard.h"
-#include "memory.h"
+#include "./memory.h"
 #include "mouse.h"
 #include "mz1e26.h"
 #include "mz1e30.h"
 #include "mz1r13.h"
 #include "mz1r37.h"
+#include "printer.h"
 #include "serial.h"
 #include "timer.h"
 
+using MZ2500::CALENDAR;
+using MZ2500::CMT;
+using MZ2500::CRTC;
+using MZ2500::FLOPPY;
+using MZ2500::INTERRUPT;
+using MZ2500::JOYSTICK;
+using MZ2500::KEYBOARD;
+using MZ2500::MEMORY;
+using MZ2500::MOUSE;
+using MZ2500::MZ1E26;
+using MZ2500::MZ1E30;
+using MZ2500::MZ1R13;
+using MZ2500::MZ1R37;
+using MZ2500::PRINTER;
+using MZ2500::SERIAL;
+using MZ2500::TIMER;
+
 // ----------------------------------------------------------------------------
 // initialize
 // ----------------------------------------------------------------------------
 
-VM::VM(EMU* parent_emu) : emu(parent_emu)
+VM::VM(EMU_TEMPLATE* 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"));
        
        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));
        pit = new I8253(this, emu);
        pio_i = new I8255(this, emu);
        io = new IO(this, emu);
        fdc = new MB8877(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));
+#ifdef USE_DEBUGGER
+//     fdc->set_context_debugger(new DEBUGGER(this, emu));
+#endif
        pcm = new PCM1BIT(this, emu);
+#ifdef USE_DEBUGGER
+//     pcm->set_context_debugger(new DEBUGGER(this, emu));
+#endif
        rtc = new RP5C01(this, emu);    // RP-5C15
+       sasi_host = new SCSI_HOST(this, emu);
+       sasi_hdd = new SASI_HDD(this, emu);
+       sasi_hdd->set_device_name(_T("SASI Hard Disk Drive"));
+       sasi_hdd->scsi_id = 0;
+       sasi_hdd->bytes_per_sec = 32 * 1024; // 32KB/s
+       for(int i = 0; i < USE_HARD_DISK; i++) {
+               sasi_hdd->set_disk_handler(i, new HARDDISK(emu));
+       }
+       sasi_hdd->set_context_interface(sasi_host);
+       sasi_host->set_context_target(sasi_hdd);
        w3100a = new W3100A(this, emu);
        opn = new YM2203(this, emu);
+#ifdef USE_DEBUGGER
+       opn->set_context_debugger(new DEBUGGER(this, emu));
+#endif
        cpu = new Z80(this, emu);
        pio = new Z80PIO(this, emu);
        sio = new Z80SIO(this, emu);
+
+       pio_i->set_device_name(_T("i8255 PIO(CMT/CRTC)"));
+       pio->set_device_name(_T("Z80 PIO(KEYBOARD/CRTC)"));
+       sio->set_device_name(_T("Z80 SIO(MOUSE)"));
+
        
        calendar = new CALENDAR(this, emu);
        cmt = new CMT(this, emu);
@@ -83,16 +139,22 @@ VM::VM(EMU* parent_emu) : emu(parent_emu)
        mz1e30 = new MZ1E30(this, emu);
        mz1r13 = new MZ1R13(this, emu);
        mz1r37 = new MZ1R37(this, emu);
+       printer = new PRINTER(this, emu);
        serial = new SERIAL(this, emu);
        timer = new TIMER(this, emu);
-       
        // set contexts
        event->set_context_cpu(cpu);
        event->set_context_sound(opn);
        event->set_context_sound(pcm);
        event->set_context_sound(drec);
+       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());
+       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());
        
-       drec->set_context_out(cmt, SIG_CMT_OUT, 1);
+       drec->set_context_ear(cmt, SIG_CMT_OUT, 1);
        drec->set_context_remote(cmt, SIG_CMT_REMOTE, 1);
        drec->set_context_end(cmt, SIG_CMT_END, 1);
        drec->set_context_top(cmt, SIG_CMT_TOP, 1);
@@ -105,8 +167,14 @@ VM::VM(EMU* parent_emu) : emu(parent_emu)
        pio_i->set_context_port_c(cmt, SIG_CMT_PIO_PC, 0xff, 0);
        pio_i->set_context_port_c(crtc, SIG_CRTC_MASK, 0x01, 0);
        pio_i->set_context_port_c(pcm, SIG_PCM1BIT_SIGNAL, 0x04, 0);
+       // Sound:: Force realtime rendering. This is temporally fix. 20161024 K.O
+       //pcm->set_realtime_render(true);
+
+       
        rtc->set_context_alarm(interrupt, SIG_INTERRUPT_RP5C15, 1);
        rtc->set_context_pulse(opn, SIG_YM2203_PORT_B, 8);
+       sasi_host->set_context_irq(mz1e30, SIG_MZ1E30_IRQ, 1);
+       sasi_host->set_context_drq(mz1e30, SIG_MZ1E30_DRQ, 1);
        opn->set_context_port_a(floppy, SIG_FLOPPY_REVERSE, 0x02, 0);
        opn->set_context_port_a(crtc, SIG_CRTC_PALLETE, 0x04, 0);
        opn->set_context_port_a(mouse, SIG_MOUSE_SEL, 0x08, 0);
@@ -130,6 +198,18 @@ VM::VM(EMU* parent_emu) : emu(parent_emu)
        memory->set_context_cpu(cpu);
        memory->set_context_crtc(crtc);
        mouse->set_context_sio(sio);
+       mz1e30->set_context_host(sasi_host);
+       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_MZ1;
+               printer->set_context_prn(mz1p17);
+//     } else if(config.printer_type == 2) {
+//             printer->set_context_prn(new PCPR201(this, emu));
+       } else {
+               printer->set_context_prn(dummy);
+       }
        serial->set_context_sio(sio);
        timer->set_context_pit(pit);
        
@@ -173,6 +253,7 @@ VM::VM(EMU* parent_emu) : emu(parent_emu)
        io->set_iomap_single_rw(0xef, joystick);
        io->set_iomap_range_w(0xf0, 0xf3, timer);
        io->set_iomap_range_rw(0xf4, 0xf7, crtc);
+       io->set_iomap_range_rw(0xfe, 0xff, printer);
        
        io->set_iowait_range_rw(0xc8, 0xc9, 1);
        io->set_iowait_single_rw(0xcc, 3);
@@ -180,11 +261,24 @@ VM::VM(EMU* parent_emu) : emu(parent_emu)
        io->set_iowait_range_rw(0xe8, 0xeb, 1);
        
        // 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();
        }
-       for(int i = 0; i < MAX_DRIVE; i++) {
-               fdc->set_drive_type(i, DRIVE_TYPE_2DD);
+
+       for(int drv = 0; drv < MAX_DRIVE; drv++) {
+//             if(config.drive_type) {
+//                     fdc->set_drive_type(drv, DRIVE_TYPE_2D);
+//             } else {
+                       fdc->set_drive_type(drv, DRIVE_TYPE_2DD);
+//             }
+       }
+       for(int drv = 0; drv < USE_HARD_DISK; drv++) {
+               if(!(config.last_hard_disk_path[drv][0] != _T('\0') && FILEIO::IsFileExisting(config.last_hard_disk_path[drv]))) {
+                       create_local_path(config.last_hard_disk_path[drv], _MAX_PATH, _T("SASI%d.DAT"), drv);
+               }
        }
        monitor_type = config.monitor_type;
 }
@@ -225,13 +319,13 @@ void VM::reset()
        opn->write_signal(SIG_YM2203_PORT_B, (monitor_type & 2) ? 0x77 : 0x37, 0xff);
 }
 
-void VM::special_reset()
+void VM::special_reset(int num)
 {
        // reset all devices
 //     for(DEVICE* device = first_device; device; device = device->next_device) {
 //             device->special_reset();
 //     }
-       memory->special_reset();
+       memory->special_reset(num);
        cpu->reset();
 }
 
@@ -240,9 +334,9 @@ void VM::run()
        event->drive();
 }
 
-double VM::frame_rate()
+double VM::get_frame_rate()
 {
-       return event->frame_rate();
+       return event->get_frame_rate();
 }
 
 // ----------------------------------------------------------------------------
@@ -268,12 +362,6 @@ void VM::draw_screen()
        crtc->draw_screen();
 }
 
-int VM::access_lamp()
-{
-       uint32 status = fdc->read_signal(0) | mz1e30->read_signal(0);
-       return (status & 0x30) ? 4 : (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
-}
-
 // ----------------------------------------------------------------------------
 // soud manager
 // ----------------------------------------------------------------------------
@@ -284,116 +372,251 @@ void VM::initialize_sound(int rate, int samples)
        event->initialize_sound(rate, samples);
        
        // init sound gen
-       opn->init(rate, 2000000, samples, 0, -8);
-       pcm->init(rate, 4096);
+       opn->initialize_sound(rate, 2000000, samples, 0, -8);
+       pcm->initialize_sound(rate, 4096);
 }
 
-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()
-{
-       return event->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) {
+               opn->set_volume(0, decibel_l, decibel_r);
+       } else if(ch == 1) {
+               opn->set_volume(1, decibel_l, decibel_r);
+       } else if(ch == 2) {
+               pcm->set_volume(0, decibel_l, decibel_r);
+       } else if(ch == 3) {
+               drec->set_volume(0, decibel_l, decibel_r);
+       } else if(ch == 4) {
+               drec->set_volume(1, decibel_l, decibel_r);
+       } else if(ch == 5) {
+               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);
+       } else if(ch == 6) {
+               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
 
 // ----------------------------------------------------------------------------
 // socket
 // ----------------------------------------------------------------------------
 
-void VM::network_connected(int ch)
+void VM::notify_socket_connected(int ch)
 {
-       w3100a->connected(ch);
+       w3100a->notify_connected(ch);
 }
 
-void VM::network_disconnected(int ch)
+void VM::notify_socket_disconnected(int ch)
 {
-       w3100a->disconnected(ch);
+       w3100a->notify_disconnected(ch);
 }
 
-uint8* VM::get_sendbuffer(int ch, int* size)
+uint8_t* VM::get_socket_send_buffer(int ch, int* size)
 {
-       return w3100a->get_sendbuffer(ch, size);
+       return w3100a->get_send_buffer(ch, size);
 }
 
-void VM::inc_sendbuffer_ptr(int ch, int size)
+void VM::inc_socket_send_buffer_ptr(int ch, int size)
 {
-       w3100a->inc_sendbuffer_ptr(ch, size);
+       w3100a->inc_send_buffer_ptr(ch, size);
 }
 
-uint8* VM::get_recvbuffer0(int ch, int* size0, int* size1)
+uint8_t* VM::get_socket_recv_buffer0(int ch, int* size0, int* size1)
 {
-       return w3100a->get_recvbuffer0(ch, size0, size1);
+       return w3100a->get_recv_buffer0(ch, size0, size1);
 }
 
-uint8* VM::get_recvbuffer1(int ch)
+uint8_t* VM::get_socket_recv_buffer1(int ch)
 {
-       return w3100a->get_recvbuffer1(ch);
+       return w3100a->get_recv_buffer1(ch);
 }
 
-void VM::inc_recvbuffer_ptr(int ch, int size)
+void VM::inc_socket_recv_buffer_ptr(int ch, int size)
 {
-       w3100a->inc_recvbuffer_ptr(ch, size);
+       w3100a->inc_recv_buffer_ptr(ch, size);
 }
 
 // ----------------------------------------------------------------------------
 // user interface
 // ----------------------------------------------------------------------------
 
-void VM::open_disk(int drv, _TCHAR* file_path, int bank)
+void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
 {
        fdc->open_disk(drv, file_path, bank);
+       
+       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);
+               }
+       }
 }
 
-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)
+{
+       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);
+}
+
+void VM::open_hard_disk(int drv, const _TCHAR* file_path)
+{
+       if(drv < USE_HARD_DISK) {
+               sasi_hdd->open(drv, file_path, 256);
+       }
+}
+
+void VM::close_hard_disk(int drv)
 {
-       return fdc->disk_inserted(drv);
+       if(drv < USE_HARD_DISK) {
+               sasi_hdd->close(drv);
+       }
 }
 
-void VM::set_disk_protected(int drv, bool value)
+bool VM::is_hard_disk_inserted(int drv)
 {
-       fdc->set_disk_protected(drv, value);
+       if(drv < USE_HARD_DISK) {
+               return sasi_hdd->mounted(drv);
+       }
+       return false;
 }
 
-bool VM::get_disk_protected(int drv)
+uint32_t VM::is_hard_disk_accessed()
 {
-       return fdc->get_disk_protected(drv);
+       uint32_t status = 0;
+       
+       for(int drv = 0; drv < USE_HARD_DISK; drv++) {
+               if(sasi_hdd->accessed(drv)) {
+                       status |= 1 << drv;
+               }
+       }
+       return status;
 }
 
-void VM::play_tape(_TCHAR* file_path)
+void VM::play_tape(int drv, const _TCHAR* file_path)
 {
-       bool value = drec->play_tape(file_path);
+       bool remote = drec->get_remote();
+       bool opened = drec->play_tape(file_path);
+       
+       if(opened && remote) {
+               // if machine already sets remote on, start playing now
+               push_play(drv);
+       }
        cmt->close_tape();
-       cmt->play_tape(value);
+       cmt->play_tape(opened);
 }
 
-void VM::rec_tape(_TCHAR* file_path)
+void VM::rec_tape(int drv, const _TCHAR* file_path)
 {
-       bool value = drec->rec_tape(file_path);
+       bool remote = drec->get_remote();
+       bool opened = drec->rec_tape(file_path);
+       
+       if(opened && remote) {
+               // if machine already sets remote on, start recording now
+               push_play(drv);
+       }
        cmt->close_tape();
-       cmt->rec_tape(value);
+       cmt->rec_tape(opened);
 }
 
-void VM::close_tape()
+void VM::close_tape(int drv)
 {
+       emu->lock_vm();
        drec->close_tape();
+       emu->unlock_vm();
+       drec->set_remote(false);
        cmt->close_tape();
 }
 
-bool VM::tape_inserted()
+bool VM::is_tape_inserted(int drv)
+{
+       return drec->is_tape_inserted();
+}
+
+bool VM::is_tape_playing(int drv)
+{
+       return drec->is_tape_playing();
+}
+
+bool VM::is_tape_recording(int drv)
+{
+       return drec->is_tape_recording();
+}
+
+int VM::get_tape_position(int drv)
+{
+       return drec->get_tape_position();
+}
+
+const _TCHAR* VM::get_tape_message(int drv)
 {
-       return drec->tape_inserted();
+       return drec->get_message();
 }
 
-bool VM::now_skip()
+void VM::push_play(int drv)
 {
-       return event->now_skip();
+       drec->set_remote(false);
+       drec->set_ff_rew(0);
+       drec->set_remote(true);
+}
+
+void VM::push_stop(int drv)
+{
+       drec->set_remote(false);
+}
+
+void VM::push_fast_forward(int drv)
+{
+       drec->set_remote(false);
+       drec->set_ff_rew(1);
+       drec->set_remote(true);
+}
+
+void VM::push_fast_rewind(int drv)
+{
+       drec->set_remote(false);
+       drec->set_ff_rew(-1);
+       drec->set_remote(true);
+}
+
+bool VM::is_frame_skippable()
+{
+       return event->is_frame_skippable();
 }
 
 void VM::update_config()
@@ -403,29 +626,52 @@ void VM::update_config()
        }
 }
 
-#define STATE_VERSION  2
+double VM::get_current_usec()
+{
+       if(event == NULL) return 0.0;
+       return event->get_current_usec();
+}
 
-void VM::save_state(FILEIO* state_fio)
+uint64_t VM::get_current_clock_uint64()
 {
-       state_fio->FputUint32(STATE_VERSION);
-       
-       for(DEVICE* device = first_device; device; device = device->next_device) {
-               device->save_state(state_fio);
-       }
-       state_fio->FputInt32(monitor_type);
+               if(event == NULL) return (uint64_t)0;
+               return event->get_current_clock_uint64();
 }
 
-bool VM::load_state(FILEIO* state_fio)
+#define STATE_VERSION  8
+
+bool VM::process_state(FILEIO* state_fio, bool loading)
 {
-       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 = (int)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;
                }
-       }
-       monitor_type = state_fio->FgetInt32();
+               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;
+               }
+       }
+       // Machine specified.
+       state_fio->StateValue(monitor_type);
        return true;
 }
-