OSDN Git Service

[VM][General][WIP] Apply new (Upstream 2016-02-21) APIs to VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / mz2500 / mz1r12.cpp
1 /*
2         SHARP MZ-80B Emulator 'EmuZ-80B'
3         SHARP MZ-2200 Emulator 'EmuZ-2200'
4
5         Author : Takeda.Toshiya
6         Date   : 2013.03.17-
7
8         [ MZ-1R12 (32KB SRAM) ]
9 */
10
11 #include "mz1r12.h"
12
13 void MZ1R12::initialize()
14 {
15         memset(sram, 0, sizeof(sram));
16         read_only = false;
17         
18         FILEIO* fio = new FILEIO();
19 #ifndef _MZ80B
20         if(fio->Fopen(create_local_path(_T("MZ-1E18.ROM")), FILEIO_READ_BINARY)) {
21                 fio->Fread(sram, sizeof(sram), 1);
22                 fio->Fclose();
23                 read_only = true;
24         } else
25 #endif
26         if(fio->Fopen(create_local_path(_T("MZ-1R12.BIN")), FILEIO_READ_BINARY)) {
27                 fio->Fread(sram, sizeof(sram), 1);
28                 fio->Fclose();
29         }
30         delete fio;
31         
32         address = 0;
33         crc32 = get_crc32(sram, sizeof(sram));
34 }
35
36 void MZ1R12::release()
37 {
38         if(!read_only && crc32 != get_crc32(sram, sizeof(sram))) {
39                 FILEIO* fio = new FILEIO();
40                 if(fio->Fopen(create_local_path(_T("MZ-1R12.BIN")), FILEIO_WRITE_BINARY)) {
41                         fio->Fwrite(sram, sizeof(sram), 1);
42                         fio->Fclose();
43                 }
44                 delete fio;
45         }
46 }
47
48 void MZ1R12::write_io8(uint32 addr, uint32 data)
49 {
50         switch(addr & 0xff) {
51         case 0xf8:
52                 address = (address & 0x00ff) | (data << 8);
53                 break;
54         case 0xf9:
55                 address = (address & 0xff00) | (data << 0);
56                 break;
57         case 0xfa:
58                 if(!read_only) {
59                         sram[address & 0x7fff] = data;
60                 }
61                 address++;
62                 break;
63         }
64 }
65
66 uint32 MZ1R12::read_io8(uint32 addr)
67 {
68         switch(addr & 0xff) {
69         case 0xf8:
70                 address = 0;
71                 break;
72         case 0xf9:
73                 return sram[(address++) & 0x7fff];
74         }
75         return 0xff;
76 }
77
78 #define STATE_VERSION   1
79
80 void MZ1R12::save_state(FILEIO* state_fio)
81 {
82         state_fio->FputUint32(STATE_VERSION);
83         state_fio->FputInt32(this_device_id);
84         
85         state_fio->Fwrite(sram, sizeof(sram), 1);
86         state_fio->FputBool(read_only);
87         state_fio->FputUint16(address);
88         state_fio->FputUint32(crc32);
89 }
90
91 bool MZ1R12::load_state(FILEIO* state_fio)
92 {
93         if(state_fio->FgetUint32() != STATE_VERSION) {
94                 return false;
95         }
96         if(state_fio->FgetInt32() != this_device_id) {
97                 return false;
98         }
99         state_fio->Fread(sram, sizeof(sram), 1);
100         read_only = state_fio->FgetBool();
101         address = state_fio->FgetUint16();
102         crc32 = state_fio->FgetUint32();
103         return true;
104 }
105