OSDN Git Service

bd73533688b5204a4aeaa743469d0841b50afce3
[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         for(DEVICE* device = first_device; device; device = device->next_device) {
76                 device->initialize();
77         }
78         decl_state();
79         inserted = false;
80 }
81
82 VM::~VM()
83 {
84         // delete all devices
85         for(DEVICE* device = first_device; device;) {
86                 DEVICE *next_device = device->next_device;
87                 device->release();
88                 delete device;
89                 device = next_device;
90         }
91 }
92
93 DEVICE* VM::get_device(int id)
94 {
95         for(DEVICE* device = first_device; device; device = device->next_device) {
96                 if(device->this_device_id == id) {
97                         return device;
98                 }
99         }
100         return NULL;
101 }
102
103 // ----------------------------------------------------------------------------
104 // drive virtual machine
105 // ----------------------------------------------------------------------------
106
107 void VM::reset()
108 {
109         // reset all devices
110         for(DEVICE* device = first_device; device; device = device->next_device) {
111                 device->reset();
112         }
113 }
114
115 void VM::run()
116 {
117         event->drive();
118 }
119
120 // ----------------------------------------------------------------------------
121 // debugger
122 // ----------------------------------------------------------------------------
123
124 #ifdef USE_DEBUGGER
125 DEVICE *VM::get_cpu(int index)
126 {
127         if(index == 0) {
128                 return cpu;
129         }
130         return NULL;
131 }
132 #endif
133
134 // ----------------------------------------------------------------------------
135 // draw screen
136 // ----------------------------------------------------------------------------
137
138 void VM::draw_screen()
139 {
140         vdp->draw_screen();
141 }
142
143 // ----------------------------------------------------------------------------
144 // soud manager
145 // ----------------------------------------------------------------------------
146
147 void VM::initialize_sound(int rate, int samples)
148 {
149         // init sound manager
150         event->initialize_sound(rate, samples);
151         
152         // init sound gen
153         psg->initialize_sound(rate);
154 }
155
156 uint16_t* VM::create_sound(int* extra_frames)
157 {
158         return event->create_sound(extra_frames);
159 }
160
161 int VM::get_sound_buffer_ptr()
162 {
163         return event->get_sound_buffer_ptr();
164 }
165
166 #ifdef USE_SOUND_VOLUME
167 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
168 {
169         if(ch == 0) {
170                 psg->set_volume(0, decibel_l, decibel_r);
171         }
172 }
173 #endif
174
175 // ----------------------------------------------------------------------------
176 // user interface
177 // ----------------------------------------------------------------------------
178
179 void VM::open_cart(int drv, const _TCHAR* file_path)
180 {
181         if(drv == 0) {
182                 memset(mem, 0xff, 0x8000);
183                 inserted = memory->read_image(file_path, mem, 0x8000);
184                 reset();
185         }
186 }
187
188 void VM::close_cart(int drv)
189 {
190         if(drv == 0) {
191                 memset(mem, 0xff, 0x8000);
192                 inserted = false;
193                 reset();
194         }
195 }
196
197 bool VM::is_cart_inserted(int drv)
198 {
199         if(drv == 0) {
200                 return inserted;
201         } else {
202                 return false;
203         }
204 }
205
206 bool VM::is_frame_skippable()
207 {
208         return event->is_frame_skippable();
209 }
210
211 void VM::update_config()
212 {
213         for(DEVICE* device = first_device; device; device = device->next_device) {
214                 device->update_config();
215         }
216 }
217
218 #define STATE_VERSION   2
219
220 #include "../../statesub.h"
221 #include "../../qt/gui/csp_logger.h"
222 extern CSP_Logger DLL_PREFIX_I *csp_logger;
223
224 void VM::decl_state(void)
225 {
226         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PV_1000_HEAD")), csp_logger);
227
228         DECL_STATE_ENTRY_1D_ARRAY(mem, sizeof(mem));
229         DECL_STATE_ENTRY_BOOL(inserted);
230         for(DEVICE* device = first_device; device; device = device->next_device) {
231                 device->decl_state();
232         }
233 }
234
235 void VM::save_state(FILEIO* state_fio)
236 {
237         //state_fio->FputUint32(STATE_VERSION);
238         
239         if(state_entry != NULL) {
240                 state_entry->save_state(state_fio);
241         }
242         for(DEVICE* device = first_device; device; device = device->next_device) {
243                 device->save_state(state_fio);
244         }
245         //state_fio->Fwrite(mem, sizeof(mem), 1);
246         //state_fio->FputBool(inserted);
247 }
248
249 bool VM::load_state(FILEIO* state_fio)
250 {
251         //if(state_fio->FgetUint32() != STATE_VERSION) {
252         //      return false;
253         //}
254         bool mb = false;
255         if(state_entry != NULL) {
256                 mb = state_entry->load_state(state_fio);
257         }
258         if(!mb) {
259                 emu->out_debug_log("INFO: HEADER DATA ERROR");
260                 return false;
261         }
262         for(DEVICE* device = first_device; device; device = device->next_device) {
263                 if(!device->load_state(state_fio)) {
264                         return false;
265                 }
266         }
267         //state_fio->Fread(mem, sizeof(mem), 1);
268         //inserted = state_fio->FgetBool();
269         return true;
270 }
271