OSDN Git Service

[VM][LOGGER][OSD][VM_TEMPLATE] Add API to log with VM's time.
[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 using PV1000::JOYSTICK;
28 using PV1000::PSG;
29 using PV1000::VDP;
30
31 // ----------------------------------------------------------------------------
32 // initialize
33 // ----------------------------------------------------------------------------
34
35 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
36 {
37         // create devices
38         first_device = last_device = NULL;
39         dummy = new DEVICE(this, emu);  // must be 1st device
40         event = new EVENT(this, emu);   // must be 2nd device
41         dummy->set_device_name(_T("1st Dummy"));
42         
43         io = new IO(this, emu);
44         memory = new MEMORY(this, emu);
45         
46         cpu = new Z80(this, emu);
47         
48         joystick = new JOYSTICK(this, emu);
49         psg = new PSG(this, emu);
50         vdp = new VDP(this, emu);
51         
52         // set contexts
53         event->set_context_cpu(cpu);
54         event->set_context_sound(psg);
55         
56         vdp->set_context_cpu(cpu);
57         vdp->set_memory_ptr(mem);
58         
59         // cpu bus
60         cpu->set_context_mem(memory);
61         cpu->set_context_io(io);
62         cpu->set_context_intr(dummy);
63 #ifdef USE_DEBUGGER
64         cpu->set_context_debugger(new DEBUGGER(this, emu));
65 #endif
66         
67         // memory bus
68         memset(mem, 0xff, 0x8000);
69         memset(mem + 0x8000, 0, 0x8000);
70         
71         memory->set_memory_r(0x0000, 0x7fff, mem);
72         memory->set_memory_rw(0xb800, 0xbfff, mem + 0xb800);
73         
74         // i/o bus
75         io->set_iomap_range_w(0xf8, 0xfa, psg);
76         io->set_iomap_range_rw(0xfc, 0xfd, joystick);
77         io->set_iomap_range_w(0xfe, 0xff, vdp);
78         
79         // initialize all devices
80 #if defined(__GIT_REPO_VERSION)
81         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
82 #endif
83         for(DEVICE* device = first_device; device; device = device->next_device) {
84                 device->initialize();
85         }
86         inserted = false;
87 }
88
89 VM::~VM()
90 {
91         // delete all devices
92         for(DEVICE* device = first_device; device;) {
93                 DEVICE *next_device = device->next_device;
94                 device->release();
95                 delete device;
96                 device = next_device;
97         }
98 }
99
100 DEVICE* VM::get_device(int id)
101 {
102         for(DEVICE* device = first_device; device; device = device->next_device) {
103                 if(device->this_device_id == id) {
104                         return device;
105                 }
106         }
107         return NULL;
108 }
109
110 // ----------------------------------------------------------------------------
111 // drive virtual machine
112 // ----------------------------------------------------------------------------
113
114 void VM::reset()
115 {
116         // reset all devices
117         for(DEVICE* device = first_device; device; device = device->next_device) {
118                 device->reset();
119         }
120 }
121
122 void VM::run()
123 {
124         event->drive();
125 }
126
127 // ----------------------------------------------------------------------------
128 // debugger
129 // ----------------------------------------------------------------------------
130
131 #ifdef USE_DEBUGGER
132 DEVICE *VM::get_cpu(int index)
133 {
134         if(index == 0) {
135                 return cpu;
136         }
137         return NULL;
138 }
139 #endif
140
141 // ----------------------------------------------------------------------------
142 // draw screen
143 // ----------------------------------------------------------------------------
144
145 void VM::draw_screen()
146 {
147         vdp->draw_screen();
148 }
149
150 // ----------------------------------------------------------------------------
151 // soud manager
152 // ----------------------------------------------------------------------------
153
154 void VM::initialize_sound(int rate, int samples)
155 {
156         // init sound manager
157         event->initialize_sound(rate, samples);
158         
159         // init sound gen
160         psg->initialize_sound(rate);
161 }
162
163 uint16_t* VM::create_sound(int* extra_frames)
164 {
165         return event->create_sound(extra_frames);
166 }
167
168 int VM::get_sound_buffer_ptr()
169 {
170         return event->get_sound_buffer_ptr();
171 }
172
173 #ifdef USE_SOUND_VOLUME
174 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
175 {
176         if(ch == 0) {
177                 psg->set_volume(0, decibel_l, decibel_r);
178         }
179 }
180 #endif
181
182 // ----------------------------------------------------------------------------
183 // user interface
184 // ----------------------------------------------------------------------------
185
186 void VM::open_cart(int drv, const _TCHAR* file_path)
187 {
188         if(drv == 0) {
189                 memset(mem, 0xff, 0x8000);
190                 inserted = memory->read_image(file_path, mem, 0x8000);
191                 reset();
192         }
193 }
194
195 void VM::close_cart(int drv)
196 {
197         if(drv == 0) {
198                 memset(mem, 0xff, 0x8000);
199                 inserted = false;
200                 reset();
201         }
202 }
203
204 bool VM::is_cart_inserted(int drv)
205 {
206         if(drv == 0) {
207                 return inserted;
208         } else {
209                 return false;
210         }
211 }
212
213 bool VM::is_frame_skippable()
214 {
215         return event->is_frame_skippable();
216 }
217
218 void VM::update_config()
219 {
220         for(DEVICE* device = first_device; device; device = device->next_device) {
221                 device->update_config();
222         }
223 }
224
225 double VM::get_current_usec()
226 {
227         if(event == NULL) return 0.0;
228         return event->get_current_usec();
229 }
230
231 uint64_t VM::get_current_clock_uint64()
232 {
233                 if(event == NULL) return (uint64_t)0;
234                 return event->get_current_clock_uint64();
235 }
236
237 #define STATE_VERSION   2
238
239 bool VM::process_state(FILEIO* state_fio, bool loading)
240 {
241         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
242                 return false;
243         }
244         for(DEVICE* device = first_device; device; device = device->next_device) {
245                 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
246                 // const char *name = typeid(*device).name();
247                 //       But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
248                 const char *name = device->get_device_name();
249                 int len = strlen(name);
250                 
251                 if(!state_fio->StateCheckInt32(len)) {
252                         if(loading) {
253                                 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
254                         }
255                         return false;
256                 }
257                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
258                         if(loading) {
259                                 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
260                         }
261                         return false;
262                 }
263                 if(!device->process_state(state_fio, loading)) {
264                         if(loading) {
265                                 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
266                         }
267                         return false;
268                 }
269         }
270         // Machine specified.
271         state_fio->StateArray(mem, sizeof(mem), 1);
272         state_fio->StateValue(inserted);
273         return true;
274 }