OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[csp-qt/common_source_project-fm7.git] / source / src / vm / pv1000 / pv1000.cpp
1 /*
2         CASIO PV-1000 Emulator 'ePV-1000'
3
4         Author : Takeda.Toshiya
5         Date   : 2006.11.16 -
6
7         [ virtual machine ]
8 */
9
10 #include "pv1000.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../io.h"
16 #include "../memory.h"
17 #include "../z80.h"
18
19 #ifdef USE_DEBUGGER
20 #include "../debugger.h"
21 #endif
22
23 #include "joystick.h"
24 #include "psg.h"
25 #include "vdp.h"
26
27 // ----------------------------------------------------------------------------
28 // initialize
29 // ----------------------------------------------------------------------------
30
31 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
32 {
33         // create devices
34         first_device = last_device = NULL;
35         dummy = new DEVICE(this, emu);  // must be 1st device
36         event = new EVENT(this, emu);   // must be 2nd device
37         dummy->set_device_name(_T("1st Dummy"));
38         
39         io = new IO(this, emu);
40         memory = new MEMORY(this, emu);
41         cpu = new Z80(this, emu);
42         
43         joystick = new JOYSTICK(this, emu);
44         psg = new PSG(this, emu);
45         vdp = new VDP(this, emu);
46         
47         // set contexts
48         event->set_context_cpu(cpu);
49         event->set_context_sound(psg);
50         
51         vdp->set_context_cpu(cpu);
52         vdp->set_memory_ptr(mem);
53         
54         // cpu bus
55         cpu->set_context_mem(memory);
56         cpu->set_context_io(io);
57         cpu->set_context_intr(dummy);
58 #ifdef USE_DEBUGGER
59         cpu->set_context_debugger(new DEBUGGER(this, emu));
60 #endif
61         
62         // memory bus
63         memset(mem, 0xff, 0x8000);
64         memset(mem + 0x8000, 0, 0x8000);
65         
66         memory->set_memory_r(0x0000, 0x7fff, mem);
67         memory->set_memory_rw(0xb800, 0xbfff, mem + 0xb800);
68         
69         // i/o bus
70         io->set_iomap_range_w(0xf8, 0xfa, psg);
71         io->set_iomap_range_rw(0xfc, 0xfd, joystick);
72         io->set_iomap_range_w(0xfe, 0xff, vdp);
73         
74         // initialize all devices
75 #if defined(__GIT_REPO_VERSION)
76         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
77 #endif
78         for(DEVICE* device = first_device; device; device = device->next_device) {
79                 device->initialize();
80         }
81         decl_state();
82         inserted = false;
83 }
84
85 VM::~VM()
86 {
87         // delete all devices
88         for(DEVICE* device = first_device; device;) {
89                 DEVICE *next_device = device->next_device;
90                 device->release();
91                 delete device;
92                 device = next_device;
93         }
94 }
95
96 DEVICE* VM::get_device(int id)
97 {
98         for(DEVICE* device = first_device; device; device = device->next_device) {
99                 if(device->this_device_id == id) {
100                         return device;
101                 }
102         }
103         return NULL;
104 }
105
106 // ----------------------------------------------------------------------------
107 // drive virtual machine
108 // ----------------------------------------------------------------------------
109
110 void VM::reset()
111 {
112         // reset all devices
113         for(DEVICE* device = first_device; device; device = device->next_device) {
114                 device->reset();
115         }
116 }
117
118 void VM::run()
119 {
120         event->drive();
121 }
122
123 // ----------------------------------------------------------------------------
124 // debugger
125 // ----------------------------------------------------------------------------
126
127 #ifdef USE_DEBUGGER
128 DEVICE *VM::get_cpu(int index)
129 {
130         if(index == 0) {
131                 return cpu;
132         }
133         return NULL;
134 }
135 #endif
136
137 // ----------------------------------------------------------------------------
138 // draw screen
139 // ----------------------------------------------------------------------------
140
141 void VM::draw_screen()
142 {
143         vdp->draw_screen();
144 }
145
146 // ----------------------------------------------------------------------------
147 // soud manager
148 // ----------------------------------------------------------------------------
149
150 void VM::initialize_sound(int rate, int samples)
151 {
152         // init sound manager
153         event->initialize_sound(rate, samples);
154         
155         // init sound gen
156         psg->initialize_sound(rate);
157 }
158
159 uint16_t* VM::create_sound(int* extra_frames)
160 {
161         return event->create_sound(extra_frames);
162 }
163
164 int VM::get_sound_buffer_ptr()
165 {
166         return event->get_sound_buffer_ptr();
167 }
168
169 #ifdef USE_SOUND_VOLUME
170 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
171 {
172         if(ch == 0) {
173                 psg->set_volume(0, decibel_l, decibel_r);
174         }
175 }
176 #endif
177
178 // ----------------------------------------------------------------------------
179 // user interface
180 // ----------------------------------------------------------------------------
181
182 void VM::open_cart(int drv, const _TCHAR* file_path)
183 {
184         if(drv == 0) {
185                 memset(mem, 0xff, 0x8000);
186                 inserted = memory->read_image(file_path, mem, 0x8000);
187                 reset();
188         }
189 }
190
191 void VM::close_cart(int drv)
192 {
193         if(drv == 0) {
194                 memset(mem, 0xff, 0x8000);
195                 inserted = false;
196                 reset();
197         }
198 }
199
200 bool VM::is_cart_inserted(int drv)
201 {
202         if(drv == 0) {
203                 return inserted;
204         } else {
205                 return false;
206         }
207 }
208
209 bool VM::is_frame_skippable()
210 {
211         return event->is_frame_skippable();
212 }
213
214 void VM::update_config()
215 {
216         for(DEVICE* device = first_device; device; device = device->next_device) {
217                 device->update_config();
218         }
219 }
220
221 #define STATE_VERSION   2
222
223 #include "../../statesub.h"
224 #include "../../qt/gui/csp_logger.h"
225 extern CSP_Logger DLL_PREFIX_I *csp_logger;
226
227 void VM::decl_state(void)
228 {
229         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PV_1000_HEAD")), csp_logger);
230
231         DECL_STATE_ENTRY_1D_ARRAY(mem, sizeof(mem));
232         DECL_STATE_ENTRY_BOOL(inserted);
233         for(DEVICE* device = first_device; device; device = device->next_device) {
234                 device->decl_state();
235         }
236 }
237
238 void VM::save_state(FILEIO* state_fio)
239 {
240         //state_fio->FputUint32(STATE_VERSION);
241         
242         if(state_entry != NULL) {
243                 state_entry->save_state(state_fio);
244         }
245         for(DEVICE* device = first_device; device; device = device->next_device) {
246                 device->save_state(state_fio);
247         }
248         //state_fio->Fwrite(mem, sizeof(mem), 1);
249         //state_fio->FputBool(inserted);
250 }
251
252 bool VM::load_state(FILEIO* state_fio)
253 {
254         //if(state_fio->FgetUint32() != STATE_VERSION) {
255         //      return false;
256         //}
257         bool mb = false;
258         if(state_entry != NULL) {
259                 mb = state_entry->load_state(state_fio);
260         }
261         if(!mb) {
262                 emu->out_debug_log("INFO: HEADER DATA ERROR");
263                 return false;
264         }
265         for(DEVICE* device = first_device; device; device = device->next_device) {
266                 if(!device->load_state(state_fio)) {
267                         return false;
268                 }
269         }
270         //state_fio->Fread(mem, sizeof(mem), 1);
271         //inserted = state_fio->FgetBool();
272         return true;
273 }
274