OSDN Git Service

[VM][General] Merge upstream 2016-03-01. (Pahse 1).
[csp-qt/common_source_project-fm7.git] / source / src / vm / phc25 / phc25.cpp
1 /*
2         SANYO PHC-25 Emulator 'ePHC-25'
3         SEIKO MAP-1010 Emulator 'eMAP-1010'
4
5         Author : Takeda.Toshiya
6         Date   : 2010.08.03-
7
8         [ virtual machine ]
9 */
10
11 #include "phc25.h"
12 #include "../../emu.h"
13 #include "../device.h"
14 #include "../event.h"
15
16 #include "../datarec.h"
17 #include "../io.h"
18 #include "../mc6847.h"
19 #include "../not.h"
20 #include "../ym2203.h"
21 #include "../z80.h"
22
23 #ifdef USE_DEBUGGER
24 #include "../debugger.h"
25 #endif
26
27 #include "joystick.h"
28 #include "keyboard.h"
29 #include "memory.h"
30 #include "system.h"
31
32 // ----------------------------------------------------------------------------
33 // initialize
34 // ----------------------------------------------------------------------------
35
36 VM::VM(EMU* parent_emu) : emu(parent_emu)
37 {
38         // create devices
39         first_device = last_device = NULL;
40         dummy = new DEVICE(this, emu);  // must be 1st device
41         event = new EVENT(this, emu);   // must be 2nd device
42         
43         drec = new DATAREC(this, emu);
44         io = new IO(this, emu);
45         vdp = new MC6847(this, emu);
46         not_vsync = new NOT(this, emu);
47         psg = new YM2203(this, emu);
48         cpu = new Z80(this, emu);
49         
50         joystick = new JOYSTICK(this, emu);
51         keyboard = new KEYBOARD(this, emu);
52         memory = new MEMORY(this, emu);
53         system = new SYSTEM(this, emu);
54         
55         // set contexts
56         event->set_context_cpu(cpu);
57         event->set_context_sound(psg);
58         event->set_context_sound(drec);
59         
60         vdp->load_font_image(create_local_path(_T("FONT.ROM")));
61         vdp->set_vram_ptr(memory->get_vram(), 0x1800);
62 //      vdp->set_context_cpu(cpu);
63         vdp->set_context_vsync(not_vsync, SIG_NOT_INPUT, 1);
64         not_vsync->set_context_out(cpu, SIG_CPU_IRQ, 1);
65         
66         vdp->set_context_vsync(system, SIG_SYSTEM_PORT, 0x10);
67         drec->set_context_ear(system, SIG_SYSTEM_PORT, 0x20);
68         // bit6: printer busy
69         vdp->set_context_hsync(system, SIG_SYSTEM_PORT, 0x80);
70         
71         joystick->set_context_psg(psg);
72 #ifdef _MAP1010
73         memory->set_context_keyboard(keyboard);
74 #endif
75         system->set_context_drec(drec);
76         system->set_context_vdp(vdp);
77         
78         // cpu bus
79         cpu->set_context_mem(memory);
80         cpu->set_context_io(io);
81         cpu->set_context_intr(dummy);
82 #ifdef USE_DEBUGGER
83         cpu->set_context_debugger(new DEBUGGER(this, emu));
84 #endif
85         
86         // i/o bus
87         io->set_iomap_single_rw(0x40, system);
88 #ifndef _MAP1010
89         io->set_iomap_range_r(0x80, 0x88, keyboard);
90 #endif
91         io->set_iomap_alias_w(0xc0, psg, 1);    // PSG data
92         io->set_iomap_alias_w(0xc1, psg, 0);    // PSG ch
93 //      io->set_iomap_alias_r(0xc0, psg, 1);
94         io->set_iomap_alias_r(0xc1, psg, 1);    // PSG data
95         
96         // initialize all devices
97         for(DEVICE* device = first_device; device; device = device->next_device) {
98                 device->initialize();
99         }
100 }
101
102 VM::~VM()
103 {
104         // delete all devices
105         for(DEVICE* device = first_device; device;) {
106                 DEVICE *next_device = device->next_device;
107                 device->release();
108                 delete device;
109                 device = next_device;
110         }
111 }
112
113 DEVICE* VM::get_device(int id)
114 {
115         for(DEVICE* device = first_device; device; device = device->next_device) {
116                 if(device->this_device_id == id) {
117                         return device;
118                 }
119         }
120         return NULL;
121 }
122
123 // ----------------------------------------------------------------------------
124 // drive virtual machine
125 // ----------------------------------------------------------------------------
126
127 void VM::reset()
128 {
129         // reset all devices
130         for(DEVICE* device = first_device; device; device = device->next_device) {
131                 device->reset();
132         }
133 }
134
135 void VM::run()
136 {
137         event->drive();
138 }
139
140 // ----------------------------------------------------------------------------
141 // debugger
142 // ----------------------------------------------------------------------------
143
144 #ifdef USE_DEBUGGER
145 DEVICE *VM::get_cpu(int index)
146 {
147         if(index == 0) {
148                 return cpu;
149         }
150         return NULL;
151 }
152 #endif
153
154 // ----------------------------------------------------------------------------
155 // draw screen
156 // ----------------------------------------------------------------------------
157
158 void VM::draw_screen()
159 {
160         vdp->draw_screen();
161 }
162
163 // ----------------------------------------------------------------------------
164 // soud manager
165 // ----------------------------------------------------------------------------
166
167 void VM::initialize_sound(int rate, int samples)
168 {
169         // init sound manager
170         event->initialize_sound(rate, samples);
171         
172         // init sound gen
173         psg->initialize_sound(rate, 1996750, samples, 0, 0);
174 }
175
176 uint16_t* VM::create_sound(int* extra_frames)
177 {
178         return event->create_sound(extra_frames);
179 }
180
181 int VM::get_sound_buffer_ptr()
182 {
183         return event->get_sound_buffer_ptr();
184 }
185
186 #ifdef USE_SOUND_VOLUME
187 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
188 {
189         if(ch == 0) {
190                 psg->set_volume(1, decibel_l, decibel_r);
191         } else if(ch == 1) {
192                 drec->set_volume(0, decibel_l, decibel_r);
193         }
194 }
195 #endif
196
197 // ----------------------------------------------------------------------------
198 // user interface
199 // ----------------------------------------------------------------------------
200
201 void VM::play_tape(const _TCHAR* file_path)
202 {
203         drec->play_tape(file_path);
204 }
205
206 void VM::rec_tape(const _TCHAR* file_path)
207 {
208         drec->rec_tape(file_path);
209 }
210
211 void VM::close_tape()
212 {
213         drec->close_tape();
214 }
215
216 bool VM::is_tape_inserted()
217 {
218         return drec->is_tape_inserted();
219 }
220
221 bool VM::is_tape_playing()
222 {
223         return drec->is_tape_playing();
224 }
225
226 bool VM::is_tape_recording()
227 {
228         return drec->is_tape_recording();
229 }
230
231 int VM::get_tape_position()
232 {
233         return drec->get_tape_position();
234 }
235
236 bool VM::is_frame_skippable()
237 {
238         return event->is_frame_skippable();
239 }
240
241 void VM::update_config()
242 {
243         for(DEVICE* device = first_device; device; device = device->next_device) {
244                 device->update_config();
245         }
246 }
247
248 #define STATE_VERSION   1
249
250 void VM::save_state(FILEIO* state_fio)
251 {
252         state_fio->FputUint32(STATE_VERSION);
253         
254         for(DEVICE* device = first_device; device; device = device->next_device) {
255                 device->save_state(state_fio);
256         }
257 }
258
259 bool VM::load_state(FILEIO* state_fio)
260 {
261         if(state_fio->FgetUint32() != STATE_VERSION) {
262                 return false;
263         }
264         for(DEVICE* device = first_device; device; device = device->next_device) {
265                 if(!device->load_state(state_fio)) {
266                         return false;
267                 }
268         }
269         return true;
270 }
271