OSDN Git Service

365a25a3517c0d2a606bba96847d64ba60dbb916
[csp-qt/common_source_project-fm7.git] / source / src / vm / pasopia / rampac2.cpp
1 /*
2         TOSHIBA PASOPIA Emulator 'EmuPIA'
3         TOSHIBA PASOPIA 7 Emulator 'EmuPIA7'
4
5         Author : Takeda.Toshiya
6         Date   : 2006.09.20 -
7
8         [ ram pac 2 (32kbytes) ]
9 */
10
11 #include "rampac2.h"
12
13 static const uint8_t header[16] = {
14         0xaa, 0x1f, 0x04, 0x00, 0x04, 0x80, 0x00, 0x01, 0x04, 0x04, 0x01, 0x03, 0x08, 0x00, 0x00, 0x00
15 };
16
17 void RAMPAC2::initialize(int id)
18 {
19         opened = modified = false;
20         
21         // note: rampac2 id must be 1 or 2 !!!
22         if(_tcscmp(config.recent_binary_path[id - 1][0], _T("")) != 0) {
23                 // open last file
24                 open_file(config.recent_binary_path[id - 1][0]);
25         } else {
26                 // open default rampac2 file
27                 open_file(create_local_path(_T("RAMPAC%d.BIN"), id));
28         }
29 }
30
31 void RAMPAC2::release()
32 {
33         // save modified data
34         if(opened && modified) {
35                 FILEIO* fio = new FILEIO();
36                 if(fio->Fopen(path, FILEIO_WRITE_BINARY)) {
37                         fio->Fwrite(ram, sizeof(ram), 1);
38                         fio->Fclose();
39                 }
40                 delete fio;
41         }
42 }
43
44 void RAMPAC2::reset()
45 {
46         ptr = 0;
47 }
48
49 void RAMPAC2::write_io8(uint32_t addr, uint32_t data)
50 {
51         switch(addr & 0xff) {
52         case 0x18:
53                 ptr = (ptr & 0x7f00) | data;
54                 break;
55         case 0x19:
56                 ptr = (ptr & 0x00ff) | ((data & 0x7f) << 8);
57                 break;
58         case 0x1a:
59                 if(ram[ptr & 0x7fff] != data) {
60                         modified = true;
61                 }
62                 ram[ptr & 0x7fff] = data;
63                 break;
64         }
65 }
66
67 uint32_t RAMPAC2::read_io8(uint32_t addr)
68 {
69         return ram[ptr & 0x7fff];
70 }
71
72 void RAMPAC2::open_file(const _TCHAR* file_path)
73 {
74         // save modified data
75         release();
76         
77         // load specified file
78         FILEIO* fio = new FILEIO();
79         if(fio->Fopen(file_path, FILEIO_READ_BINARY)) {
80                 fio->Fread(ram, sizeof(ram), 1);
81                 fio->Fclose();
82         } else {
83                 // initialize formatted image
84                 memset(ram, 0, sizeof(ram));
85                 memcpy(ram, header, sizeof(header));
86                 memset(ram + 0x20, 0xff, 0x200);
87                 memset(ram + 0x300, 0xfe, 0x004);
88                 memset(ram + 0x304, 0xff, 0x0fc);
89         }
90         delete fio;
91         
92         my_tcscpy_s(path, _MAX_PATH, file_path);
93         opened = true;
94         modified = false;
95 }
96
97 #define STATE_VERSION   1
98
99 #include "../../statesub.h"
100
101 void RAMPAC2::decl_state()
102 {
103         state_entry = new csp_state_utils(STATE_VERSION, 1, (const _TCHAR *)_T("PAC2SLOT::RAMPAC2"), NULL);
104
105         DECL_STATE_ENTRY_1D_ARRAY(ram, sizeof(ram));
106         DECL_STATE_ENTRY_UINT32(ptr);
107         
108         DECL_STATE_ENTRY_BOOL(opened);
109         DECL_STATE_ENTRY_BOOL(modified);
110         //leave_decl_state();
111 }
112
113
114 void RAMPAC2::save_state(FILEIO* state_fio)
115 {
116         if(state_entry != NULL) {
117                 state_entry->save_state(state_fio);
118         }
119 //      state_fio->FputUint32(STATE_VERSION);
120         
121 //      state_fio->Fwrite(ram, sizeof(ram), 1);
122 //      state_fio->FputUint32(ptr);
123 //      state_fio->FputBool(opened);
124 //      state_fio->FputBool(modified);
125 }
126
127 bool RAMPAC2::load_state(FILEIO* state_fio)
128 {
129         bool mb = false;
130         if(state_entry != NULL) {
131                 mb = state_entry->load_state(state_fio);
132         }
133         if(!mb) return false;
134 //      if(state_fio->FgetUint32() != STATE_VERSION) {
135 //              return false;
136 //      }
137 //      state_fio->Fread(ram, sizeof(ram), 1);
138 //      ptr = state_fio->FgetUint32();
139 //      opened = state_fio->FgetBool();
140 //      modified = state_fio->FgetBool();
141         return true;
142 }
143