OSDN Git Service

[VM][General] Merge upstream 2016-02-13. Still don't implement OSD/Gui part of joysti...
[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) : emu(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         
38         io = new IO(this, emu);
39         memory = new MEMORY(this, emu);
40         cpu = new Z80(this, emu);
41         
42         joystick = new JOYSTICK(this, emu);
43         psg = new PSG(this, emu);
44         vdp = new VDP(this, emu);
45         
46         // set contexts
47         event->set_context_cpu(cpu);
48         event->set_context_sound(psg);
49         
50         vdp->set_context_cpu(cpu);
51         vdp->set_memory_ptr(mem);
52         
53         // cpu bus
54         cpu->set_context_mem(memory);
55         cpu->set_context_io(io);
56         cpu->set_context_intr(dummy);
57 #ifdef USE_DEBUGGER
58         cpu->set_context_debugger(new DEBUGGER(this, emu));
59 #endif
60         
61         // memory bus
62         memset(mem, 0xff, 0x8000);
63         memset(mem + 0x8000, 0, 0x8000);
64         
65         memory->set_memory_r(0x0000, 0x7fff, mem);
66         memory->set_memory_rw(0xb800, 0xbfff, mem + 0xb800);
67         
68         // i/o bus
69         io->set_iomap_range_w(0xf8, 0xfa, psg);
70         io->set_iomap_range_rw(0xfc, 0xfd, joystick);
71         io->set_iomap_range_w(0xfe, 0xff, vdp);
72         
73         // initialize all devices
74         for(DEVICE* device = first_device; device; device = device->next_device) {
75                 device->initialize();
76         }
77         inserted = false;
78 }
79
80 VM::~VM()
81 {
82         // delete all devices
83         for(DEVICE* device = first_device; device;) {
84                 DEVICE *next_device = device->next_device;
85                 device->release();
86                 delete device;
87                 device = next_device;
88         }
89 }
90
91 DEVICE* VM::get_device(int id)
92 {
93         for(DEVICE* device = first_device; device; device = device->next_device) {
94                 if(device->this_device_id == id) {
95                         return device;
96                 }
97         }
98         return NULL;
99 }
100
101 // ----------------------------------------------------------------------------
102 // drive virtual machine
103 // ----------------------------------------------------------------------------
104
105 void VM::reset()
106 {
107         // reset all devices
108         for(DEVICE* device = first_device; device; device = device->next_device) {
109                 device->reset();
110         }
111 }
112
113 void VM::run()
114 {
115         event->drive();
116 }
117
118 // ----------------------------------------------------------------------------
119 // debugger
120 // ----------------------------------------------------------------------------
121
122 #ifdef USE_DEBUGGER
123 DEVICE *VM::get_cpu(int index)
124 {
125         if(index == 0) {
126                 return cpu;
127         }
128         return NULL;
129 }
130 #endif
131
132 // ----------------------------------------------------------------------------
133 // draw screen
134 // ----------------------------------------------------------------------------
135
136 void VM::draw_screen()
137 {
138         vdp->draw_screen();
139 }
140
141 // ----------------------------------------------------------------------------
142 // soud manager
143 // ----------------------------------------------------------------------------
144
145 void VM::initialize_sound(int rate, int samples)
146 {
147         // init sound manager
148         event->initialize_sound(rate, samples);
149         
150         // init sound gen
151         psg->init(rate);
152 }
153
154 uint16* VM::create_sound(int* extra_frames)
155 {
156         return event->create_sound(extra_frames);
157 }
158
159 int VM::sound_buffer_ptr()
160 {
161         return event->sound_buffer_ptr();
162 }
163
164 #ifdef USE_SOUND_VOLUME
165 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
166 {
167         if(ch == 0) {
168                 psg->set_volume(0, decibel_l, decibel_r);
169         }
170 }
171 #endif
172
173 // ----------------------------------------------------------------------------
174 // user interface
175 // ----------------------------------------------------------------------------
176
177 void VM::open_cart(int drv, const _TCHAR* file_path)
178 {
179         if(drv == 0) {
180                 memset(mem, 0xff, 0x8000);
181                 inserted = memory->read_image(file_path, mem, 0x8000);
182                 reset();
183         }
184 }
185
186 void VM::close_cart(int drv)
187 {
188         if(drv == 0) {
189                 memset(mem, 0xff, 0x8000);
190                 inserted = false;
191                 reset();
192         }
193 }
194
195 bool VM::cart_inserted(int drv)
196 {
197         if(drv == 0) {
198                 return inserted;
199         } else {
200                 return false;
201         }
202 }
203
204 bool VM::now_skip()
205 {
206         return event->now_skip();
207 }
208
209 void VM::update_config()
210 {
211         for(DEVICE* device = first_device; device; device = device->next_device) {
212                 device->update_config();
213         }
214 }
215
216 #define STATE_VERSION   1
217
218 void VM::save_state(FILEIO* state_fio)
219 {
220         state_fio->FputUint32(STATE_VERSION);
221         
222         for(DEVICE* device = first_device; device; device = device->next_device) {
223                 device->save_state(state_fio);
224         }
225         state_fio->Fwrite(mem, sizeof(mem), 1);
226         state_fio->FputBool(inserted);
227 }
228
229 bool VM::load_state(FILEIO* state_fio)
230 {
231         if(state_fio->FgetUint32() != STATE_VERSION) {
232                 return false;
233         }
234         for(DEVICE* device = first_device; device; device = device->next_device) {
235                 if(!device->load_state(state_fio)) {
236                         return false;
237                 }
238         }
239         state_fio->Fread(mem, sizeof(mem), 1);
240         inserted = state_fio->FgetBool();
241         return true;
242 }
243