OSDN Git Service

[VM] TRY:Use namespace {VMNAME} to separate around VMs. This feature still apply...
[csp-qt/common_source_project-fm7.git] / source / src / vm / bmjr / memory.cpp
index c5d8af2..02643b3 100644 (file)
@@ -10,6 +10,7 @@
 #include "memory.h"
 #include "../datarec.h"
 
+namespace BMJR {
 #define SET_BANK(s, e, w, r) { \
        int sb = (s) >> 11, eb = (e) >> 11; \
        for(int i = sb; i <= eb; i++) { \
@@ -87,8 +88,7 @@ void MEMORY::initialize()
        update_bank();
        
        // initialize inputs
-       key_stat = emu->key_buffer();
-       joy_stat = emu->joy_buffer();
+       key_stat = emu->get_key_buffer();
        
        // initialize display
        for(int i = 0; i < 8; i++) {
@@ -101,6 +101,7 @@ void MEMORY::initialize()
 
 void MEMORY::reset()
 {
+       touch_sound();
        memory_bank = 0;
        update_bank();
        
@@ -111,17 +112,17 @@ void MEMORY::reset()
        screen_mode = 0;
        screen_reversed = false;
        
-       drec_in = false;
+       drec_bit = drec_in = false;
        
        key_column = 0;
        nmi_enb = break_pressed = false;
        
        sound_sample = 0;
        sound_accum = 0;
-       sound_clock = sound_mix_clock = current_clock();
+       sound_clock = sound_mix_clock = get_current_clock();
 }
 
-void MEMORY::write_data8(uint32 addr, uint32 data)
+void MEMORY::write_data8(uint32_t addr, uint32_t data)
 {
        if((addr & 0xfe00) == 0xee00) {
                // EE00h - EFFFh
@@ -131,8 +132,9 @@ void MEMORY::write_data8(uint32 addr, uint32 data)
                        break;
                case 0xee80:
                        if(sound_sample != ((data >> 1) & 0x1f)) {
-                               sound_accum += (double)sound_sample * passed_usec(sound_clock);
-                               sound_clock = current_clock();
+                               touch_sound();
+                               sound_accum += (double)sound_sample * get_passed_usec(sound_clock);
+                               sound_clock = get_current_clock();
                                sound_sample = (data >> 1) & 0x1f;
                        }
                        d_drec->write_signal(SIG_DATAREC_MIC, ~data, 0x01);
@@ -154,7 +156,7 @@ void MEMORY::write_data8(uint32 addr, uint32 data)
                return;
        }
        if((addr & 0xf800) == 0xe800 && !(memory_bank & 2)) {
-               // E800h - EDFFh
+               // E800h - EFFFh
                switch(addr & 0xffff) {
                case 0xe800:
                case 0xe801:
@@ -182,7 +184,7 @@ void MEMORY::write_data8(uint32 addr, uint32 data)
        wbank[(addr >> 11) & 0x1f][addr & 0x7ff] = data;
 }
 
-uint32 MEMORY::read_data8(uint32 addr)
+uint32_t MEMORY::read_data8(uint32_t addr)
 {
        if((addr & 0xfe00) == 0xee00) {
                // EE00h - EFFFh
@@ -192,7 +194,7 @@ uint32 MEMORY::read_data8(uint32 addr)
                        d_drec->write_signal(SIG_DATAREC_REMOTE, addr, 0x20);
                        return 0x01;
                case 0xee80:
-                       return drec_in ? 0x80 : 0;
+                       return drec_bit ? 0x80 : 0;
                case 0xeec0:
                        return key_data;
                case 0xef00:
@@ -210,7 +212,7 @@ uint32 MEMORY::read_data8(uint32 addr)
                return 0xff;
        }
        if((addr & 0xf800) == 0xe800 && !(memory_bank & 2)) {
-               // E800h - EDFFh
+               // E800h - EFFFh
                switch(addr & 0xffff) {
                case 0xe800:
                case 0xe801:
@@ -229,10 +231,23 @@ uint32 MEMORY::read_data8(uint32 addr)
        return rbank[(addr >> 11) & 0x1f][addr & 0x7ff];
 }
 
-void MEMORY::write_signal(int id, uint32 data, uint32 mask)
+void MEMORY::write_signal(int id, uint32_t data, uint32_t mask)
 {
        if(id == SIG_MEMORY_DATAREC_EAR) {
-               drec_in = ((data & mask) != 0);
+               bool new_in = ((data & mask) != 0);
+               if(!drec_in && new_in) {
+                       // L -> H
+                       drec_clock = get_current_clock();
+               } else if(drec_in && !new_in) {
+                       // H -> L
+                       int usec = (int)get_passed_usec(drec_clock);
+                       if(usec > 417 - 42 && usec < 417 + 42) {
+                               drec_bit = false;       // 1200Hz
+                       } else if(usec > 208 - 21 && usec < 208 + 21) {
+                               drec_bit = true;        // 2400Hz
+                       }
+               }
+               drec_in = new_in;
        }
 }
 
@@ -245,11 +260,20 @@ void MEMORY::event_frame()
                if(key_stat[key_table[key_column][2]]) key_data &= ~0x04;
                if(key_stat[key_table[key_column][3]]) key_data &= ~0x08;
        }
-       // this is same as "\93ú\97§\83x\81[\83V\83b\83N\83}\83X\83^\81[Jr.(MB-6885)\83G\83~\83\85\83\8c\81[\83^ bm2"
-       if(key_stat[0xa2]) key_data &= ~0x10; // \89p\90\94     -> L-CTRL
-       if(key_stat[0xa0]) key_data &= ~0x20; // \89p\8bL\8d\86   -> L-SHIFT
-       if(key_stat[0xa1]) key_data &= ~0x40; // \83J\83i\8bL\8d\86 -> R-SHIFT
-       if(key_stat[0xa3]) key_data &= ~0x80; // \83J\83i     -> R-CTRL
+
+#if defined(_USE_QT)
+       // If same as bm2, not effect below keys at Qt version.
+       if(key_stat[VK_LCONTROL]) key_data &= ~0x10; // 英数     -> LCTRL
+       if(key_stat[VK_LSHIFT  ]) key_data &= ~0x20; // 英記号   -> L-SHIFT
+       if(key_stat[VK_RWIN    ]) key_data &= ~0x40; // カナ記号 -> R-Win
+       if(key_stat[VK_KANA    ]) key_data &= ~0x80; // カナ     -> カタカナひらがな
+#else
+       // this is same as "日立ベーシックマスターJr.(MB-6885)エミュレータ bm2"
+       if(key_stat[0xa2]) key_data &= ~0x10; // 英数     -> L-CTRL
+       if(key_stat[0xa0]) key_data &= ~0x20; // 英記号   -> L-SHIFT
+       if(key_stat[0xa1]) key_data &= ~0x40; // カナ記号 -> R-SHIFT
+       if(key_stat[0xa3]) key_data &= ~0x80; // カナ     -> R-CTRL
+#endif
 }
 
 void MEMORY::key_down(int code)
@@ -283,34 +307,43 @@ void MEMORY::update_bank()
        }
 }
 
-void MEMORY::mix(int32* buffer, int cnt)
+void MEMORY::mix(int32_t* buffer, int cnt)
 {
-       int32 volume = 0;
-       if(passed_clock(sound_mix_clock) != 0) {
-               sound_accum += (double)sound_sample * passed_usec(sound_clock);
-               volume = (int32)(SOUND_VOLUME * sound_accum / (31.0 * passed_usec(sound_mix_clock)));
+       int32_t volume = 0;
+       if(get_passed_clock(sound_mix_clock) != 0) {
+               sound_accum += (double)sound_sample * get_passed_usec(sound_clock);
+               volume = (int32_t)(SOUND_VOLUME * sound_accum / (31.0 * get_passed_usec(sound_mix_clock)));
        }
        sound_accum = 0;
-       sound_clock = sound_mix_clock = current_clock();
+       sound_clock = sound_mix_clock = get_current_clock();
+       
+       int32_t vol_l = apply_volume(volume, volume_l);
+       int32_t vol_r = apply_volume(volume, volume_r);
        
        for(int i = 0; i < cnt; i++) {
-               *buffer++ = volume;
-               *buffer++ = volume;
+               *buffer++ += vol_l; // L
+               *buffer++ += vol_r; // R
        }
 }
 
+void MEMORY::set_volume(int ch, int decibel_l, int decibel_r)
+{
+       volume_l = decibel_to_volume(decibel_l);
+       volume_r = decibel_to_volume(decibel_r);
+}
+
 void MEMORY::draw_screen()
 {
        if(!(screen_mode & 0x80)) {
                // text
-               scrntype fore = palette_pc[screen_reversed ? 0 : 7];
-               scrntype back = palette_pc[screen_reversed ? 7 : 0];
+               scrntype_t fore = palette_pc[screen_reversed ? 0 : 7];
+               scrntype_t back = palette_pc[screen_reversed ? 7 : 0];
                int taddr = 0x100;
                
                for(int y = 0, yy = 0; y < 24; y++, yy += 8) {
                        for(int x = 0, xx = 0; x < 32; x++, xx += 8) {
                                if(mp1710_enb & 1) {
-                                       uint8 color = color_table[taddr];
+                                       uint8_t color = color_table[taddr];
                                        if(screen_reversed) {
                                                color = (color >> 4) | (color << 4);
                                        }
@@ -319,8 +352,8 @@ void MEMORY::draw_screen()
                                }
                                int code = ram[taddr] << 3;
                                for(int l = 0; l < 8; l++) {
-                                       scrntype* dest = emu->screen_buffer(yy + l) + xx;
-                                       uint8 pat = font[code + l];
+                                       scrntype_t* dest = emu->get_screen_buffer(yy + l) + xx;
+                                       uint8_t pat = font[code + l];
                                        dest[0] = (pat & 0x80) ? fore : back;
                                        dest[1] = (pat & 0x40) ? fore : back;
                                        dest[2] = (pat & 0x20) ? fore : back;
@@ -335,15 +368,15 @@ void MEMORY::draw_screen()
                }
        } else {
                // graph
-               scrntype fore = palette_pc[screen_reversed ? 0 : 7];
-               scrntype back = palette_pc[screen_reversed ? 7 : 0];
+               scrntype_t fore = palette_pc[screen_reversed ? 0 : 7];
+               scrntype_t back = palette_pc[screen_reversed ? 7 : 0];
                int taddr = 0x100;
                int gaddr = 0x900 + ((screen_mode & 0x0f) << 9);
                
                for(int y = 0, yy = 0; y < 24; y++, yy += 8) {
                        for(int x = 0, xx = 0; x < 32; x++, xx += 8) {
                                if(mp1710_enb & 1) {
-                                       uint8 color = color_table[taddr];
+                                       uint8_t color = color_table[taddr];
                                        if(screen_reversed) {
                                                color = (color >> 4) | (color << 4);
                                        }
@@ -351,8 +384,8 @@ void MEMORY::draw_screen()
                                        back = palette_pc[(color >> 4) & 7];
                                }
                                for(int l = 0, ll = 0; l < 8; l++, ll += 32) {
-                                       scrntype* dest = emu->screen_buffer(yy + l) + xx;
-                                       uint8 pat = ram[gaddr + ll];
+                                       scrntype_t* dest = emu->get_screen_buffer(yy + l) + xx;
+                                       uint8_t pat = ram[gaddr + ll];
                                        dest[0] = (pat & 0x80) ? fore : back;
                                        dest[1] = (pat & 0x40) ? fore : back;
                                        dest[2] = (pat & 0x20) ? fore : back;
@@ -371,60 +404,40 @@ void MEMORY::draw_screen()
 //     emu->screen_skip_line(false);
 }
 
-#define STATE_VERSION  1
+#define STATE_VERSION  2
 
-void MEMORY::save_state(FILEIO* state_fio)
+bool MEMORY::process_state(FILEIO* state_fio, bool loading)
 {
-       state_fio->FputUint32(STATE_VERSION);
-       state_fio->FputInt32(this_device_id);
-       
-       state_fio->Fwrite(ram, sizeof(ram), 1);
-       state_fio->FputUint8(memory_bank);
-       state_fio->Fwrite(color_table, sizeof(color_table), 1);
-       state_fio->FputUint8(char_color);
-       state_fio->FputUint8(back_color);
-       state_fio->FputUint8(mp1710_enb);
-       state_fio->FputUint8(screen_mode);
-       state_fio->FputBool(screen_reversed);
-       state_fio->FputBool(drec_in);
-       state_fio->FputUint8(key_column);
-       state_fio->FputUint8(key_data);
-       state_fio->FputBool(nmi_enb);
-       state_fio->FputBool(break_pressed);
-       state_fio->FputUint8(sound_sample);
-       state_fio->FputDouble(sound_accum);
-       state_fio->FputUint32(sound_clock);
-       state_fio->FputUint32(sound_mix_clock);
-}
-
-bool MEMORY::load_state(FILEIO* state_fio)
-{
-       if(state_fio->FgetUint32() != STATE_VERSION) {
+       if(!state_fio->StateCheckUint32(STATE_VERSION)) {
                return false;
        }
-       if(state_fio->FgetInt32() != this_device_id) {
+       if(!state_fio->StateCheckInt32(this_device_id)) {
                return false;
        }
-       state_fio->Fread(ram, sizeof(ram), 1);
-       memory_bank = state_fio->FgetUint8();
-       state_fio->Fread(color_table, sizeof(color_table), 1);
-       char_color = state_fio->FgetUint8();
-       back_color = state_fio->FgetUint8();
-       mp1710_enb = state_fio->FgetUint8();
-       screen_mode = state_fio->FgetUint8();
-       screen_reversed = state_fio->FgetBool();
-       drec_in = state_fio->FgetBool();
-       key_column = state_fio->FgetUint8();
-       key_data = state_fio->FgetUint8();
-       nmi_enb = state_fio->FgetBool();
-       break_pressed = state_fio->FgetBool();
-       sound_sample = state_fio->FgetUint8();
-       sound_accum = state_fio->FgetDouble();
-       sound_clock = state_fio->FgetUint32();
-       sound_mix_clock = state_fio->FgetUint32();
+       state_fio->StateBuffer(ram, sizeof(ram), 1);
+       state_fio->StateUint8(memory_bank);
+       state_fio->StateBuffer(color_table, sizeof(color_table), 1);
+       state_fio->StateUint8(char_color);
+       state_fio->StateUint8(back_color);
+       state_fio->StateUint8(mp1710_enb);
+       state_fio->StateUint8(screen_mode);
+       state_fio->StateBool(screen_reversed);
+       state_fio->StateBool(drec_bit);
+       state_fio->StateBool(drec_in);
+       state_fio->StateUint32(drec_clock);
+       state_fio->StateUint8(key_column);
+       state_fio->StateUint8(key_data);
+       state_fio->StateBool(nmi_enb);
+       state_fio->StateBool(break_pressed);
+       state_fio->StateUint8(sound_sample);
+       state_fio->StateDouble(sound_accum);
+       state_fio->StateUint32(sound_clock);
+       state_fio->StateUint32(sound_mix_clock);
        
        // post process
-       update_bank();
+       if(loading) {
+               update_bank();
+       }
        return true;
 }
-
+}