OSDN Git Service

f09139073ec3bdb510927640de9b168ab382615f
[csp-qt/common_source_project-fm7.git] / source / src / vm / pasopia7 / 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 bool RAMPAC2::process_state(FILEIO* state_fio, bool loading)
100 {
101         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
102                 return false;
103         }
104         state_fio->StateBuffer(ram, sizeof(ram), 1);
105         state_fio->StateUint32(ptr);
106         state_fio->StateBool(opened);
107         state_fio->StateBool(modified);
108         return true;
109 }