OSDN Git Service

[VM][Towns] Initial start.Still not working.
[csp-qt/common_source_project-fm7.git] / source / src / vm / fmtowns / cmos.cpp
1 /*
2         FUJITSU FMR-50 Emulator 'eFMR-50'
3         FUJITSU FMR-60 Emulator 'eFMR-60'
4
5         Author : Takeda.Toshiya
6         Date   : 2008.05.01 -
7
8         [ cmos ]
9 */
10
11 #include "cmos.h"
12
13 void CMOS::initialize()
14 {
15         // load cmos image
16         memset(cmos, 0, sizeof(cmos));
17         modified = false;
18         
19         FILEIO* fio = new FILEIO();
20         if(fio->Fopen(create_local_path(_T("CMOS.BIN")), FILEIO_READ_BINARY)) {
21                 fio->Fread(cmos, sizeof(cmos), 1);
22                 fio->Fclose();
23         }
24         delete fio;
25 }
26
27 void CMOS::release()
28 {
29         if(modified) {
30                 FILEIO* fio = new FILEIO();
31                 if(fio->Fopen(create_local_path(_T("CMOS.BIN")), FILEIO_WRITE_BINARY)) {
32                         fio->Fwrite(cmos, sizeof(cmos), 1);
33                         fio->Fclose();
34                 }
35                 delete fio;
36         }
37 }
38
39 void CMOS::reset()
40 {
41         bank = 0;
42 }
43
44 void CMOS::write_io8(uint32_t addr, uint32_t data)
45 {
46         switch(addr) {
47         case 0x90:
48                 bank = data & 3;
49                 break;
50         default:
51                 if(!(addr & 1)) {
52                         if(cmos[bank][(addr >> 1) & 0x7ff] != data) {
53                                 cmos[bank][(addr >> 1) & 0x7ff] = data;
54                                 modified = true;
55                         }
56                 }
57                 break;
58         }
59 }
60
61 uint32_t CMOS::read_io8(uint32_t addr)
62 {
63         if(!(addr & 1)) {
64                 return cmos[bank][(addr >> 1) & 0x7ff];
65         }
66         return 0xff;
67 }
68
69 #define STATE_VERSION   1
70
71 void CMOS::save_state(FILEIO* state_fio)
72 {
73         state_fio->FputUint32(STATE_VERSION);
74         state_fio->FputInt32(this_device_id);
75         
76         state_fio->Fwrite(cmos, sizeof(cmos), 1);
77         state_fio->FputBool(modified);
78         state_fio->FputUint8(bank);
79 }
80
81 bool CMOS::load_state(FILEIO* state_fio)
82 {
83         if(state_fio->FgetUint32() != STATE_VERSION) {
84                 return false;
85         }
86         if(state_fio->FgetInt32() != this_device_id) {
87                 return false;
88         }
89         state_fio->Fread(cmos, sizeof(cmos), 1);
90         modified = state_fio->FgetBool();
91         bank = state_fio->FgetUint8();
92         return true;
93 }
94