OSDN Git Service

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