OSDN Git Service

[VM][General] Merge updtream 2015-11-18.
[csp-qt/common_source_project-fm7.git] / source / src / vm / smc777 / smc777.cpp
1 /*
2         SONY SMC-777 Emulator 'eSMC-777'
3
4         Author : Takeda.Toshiya
5         Date   : 2015.08.13-
6
7         [ virtual machine ]
8 */
9
10 #include "smc777.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../datarec.h"
16 #include "../disk.h"
17 #include "../hd46505.h"
18 #include "../mb8877.h"
19 #include "../pcm1bit.h"
20 #include "../sn76489an.h"
21 #include "../z80.h"
22
23 #ifdef USE_DEBUGGER
24 #include "../debugger.h"
25 #endif
26
27 #include "io.h"
28
29 // ----------------------------------------------------------------------------
30 // initialize
31 // ----------------------------------------------------------------------------
32
33 VM::VM(EMU* parent_emu) : emu(parent_emu)
34 {
35         // create devices
36         first_device = last_device = NULL;
37         dummy = new DEVICE(this, emu);  // must be 1st device
38         event = new EVENT(this, emu);   // must be 2nd device
39         
40         drec = new DATAREC(this, emu);
41         crtc = new HD46505(this, emu);
42         fdc = new MB8877(this, emu);
43         pcm = new PCM1BIT(this, emu);
44         psg = new SN76489AN(this, emu);
45         cpu = new Z80(this, emu);
46         
47         io = new IO(this, emu);
48         
49         // set contexts
50         event->set_context_cpu(cpu);
51         event->set_context_sound(pcm);
52         event->set_context_sound(psg);
53         event->set_context_sound(drec);
54         
55         drec->set_context_ear(io, SIG_IO_DATAREC_IN, 1);
56         crtc->set_context_disp(io, SIG_IO_CRTC_DISP, 1);
57         crtc->set_context_vsync(io, SIG_IO_CRTC_VSYNC, 1);
58         fdc->set_context_drq(io, SIG_IO_FDC_DRQ, 1);
59         fdc->set_context_irq(io, SIG_IO_FDC_IRQ, 1);
60         
61         io->set_context_cpu(cpu);
62         io->set_context_crtc(crtc, crtc->get_regs());
63         io->set_context_drec(drec);
64         io->set_context_fdc(fdc);
65         io->set_context_pcm(pcm);
66         io->set_context_psg(psg);
67         
68         // cpu bus
69         cpu->set_context_mem(io);
70         cpu->set_context_io(io);
71         cpu->set_context_intr(dummy);
72 #ifdef USE_DEBUGGER
73         cpu->set_context_debugger(new DEBUGGER(this, emu));
74 #endif
75         
76         // initialize all devices
77         for(DEVICE* device = first_device; device; device = device->next_device) {
78                 device->initialize();
79         }
80         for(int i = 0; i < MAX_DRIVE; i++) {
81                 fdc->set_drive_type(i, DRIVE_TYPE_2DD); // 1DD
82                 fdc->set_drive_rpm(i, 600);
83         }
84         fdc->write_signal(SIG_MB8877_MOTOR, 1, 1);
85 }
86
87 VM::~VM()
88 {
89         // delete all devices
90         for(DEVICE* device = first_device; device;) {
91                 DEVICE *next_device = device->next_device;
92                 device->release();
93                 delete device;
94                 device = next_device;
95         }
96 }
97
98 DEVICE* VM::get_device(int id)
99 {
100         for(DEVICE* device = first_device; device; device = device->next_device) {
101                 if(device->this_device_id == id) {
102                         return device;
103                 }
104         }
105         return NULL;
106 }
107
108 // ----------------------------------------------------------------------------
109 // drive virtual machine
110 // ----------------------------------------------------------------------------
111
112 void VM::reset()
113 {
114         // reset all devices
115         for(DEVICE* device = first_device; device; device = device->next_device) {
116                 device->reset();
117         }
118         io->warm_start = false;
119 }
120
121 void VM::special_reset()
122 {
123         // reset all devices
124         for(DEVICE* device = first_device; device; device = device->next_device) {
125                 device->reset();
126         }
127         io->warm_start = true;
128 }
129
130 void VM::run()
131 {
132         event->drive();
133 }
134
135 double VM::frame_rate()
136 {
137         return event->frame_rate();
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         io->draw_screen();
161 }
162
163 int VM::access_lamp()
164 {
165         uint32 status = fdc->read_signal(0);
166         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
167 }
168
169 // ----------------------------------------------------------------------------
170 // soud manager
171 // ----------------------------------------------------------------------------
172
173 void VM::initialize_sound(int rate, int samples)
174 {
175         // init sound manager
176         event->initialize_sound(rate, samples);
177         
178         // init sound gen
179         pcm->init(rate, 5000);
180         psg->init(rate, CPU_CLOCKS, 5000);
181 }
182
183 uint16* VM::create_sound(int* extra_frames)
184 {
185         return event->create_sound(extra_frames);
186 }
187
188 int VM::sound_buffer_ptr()
189 {
190         return event->sound_buffer_ptr();
191 }
192
193 // ----------------------------------------------------------------------------
194 // notify key
195 // ----------------------------------------------------------------------------
196
197 void VM::key_down(int code, bool repeat)
198 {
199         if(!repeat) {
200                 io->key_down_up(code, true);
201         }
202 }
203
204 void VM::key_up(int code)
205 {
206         io->key_down_up(code, false);
207 }
208
209 // ----------------------------------------------------------------------------
210 // user interface
211 // ----------------------------------------------------------------------------
212
213 void VM::open_disk(int drv, const _TCHAR* file_path, int bank)
214 {
215         fdc->open_disk(drv, file_path, bank);
216 }
217
218 void VM::close_disk(int drv)
219 {
220         fdc->close_disk(drv);
221 }
222
223 bool VM::disk_inserted(int drv)
224 {
225         return fdc->disk_inserted(drv);
226 }
227
228 void VM::set_disk_protected(int drv, bool value)
229 {
230         fdc->set_disk_protected(drv, value);
231 }
232
233 bool VM::get_disk_protected(int drv)
234 {
235         return fdc->get_disk_protected(drv);
236 }
237
238 void VM::play_tape(const _TCHAR* file_path)
239 {
240         drec->play_tape(file_path);
241 }
242
243 void VM::rec_tape(const _TCHAR* file_path)
244 {
245         drec->rec_tape(file_path);
246 }
247
248 void VM::close_tape()
249 {
250         drec->close_tape();
251 }
252
253 bool VM::tape_inserted()
254 {
255         return drec->tape_inserted();
256 }
257
258 bool VM::tape_playing()
259 {
260         return drec->tape_playing();
261 }
262
263 bool VM::tape_recording()
264 {
265         return drec->tape_recording();
266 }
267
268 int VM::tape_position()
269 {
270         return drec->tape_position();
271 }
272
273 void VM::push_play()
274 {
275         drec->set_ff_rew(0);
276         drec->set_remote(true);
277 }
278
279 void VM::push_stop()
280 {
281         drec->set_remote(false);
282 }
283
284 void VM::push_fast_forward()
285 {
286         drec->set_ff_rew(1);
287         drec->set_remote(true);
288 }
289
290 void VM::push_fast_rewind()
291 {
292         drec->set_ff_rew(-1);
293         drec->set_remote(true);
294 }
295
296 bool VM::now_skip()
297 {
298         return event->now_skip();
299 }
300
301 void VM::update_config()
302 {
303         for(DEVICE* device = first_device; device; device = device->next_device) {
304                 device->update_config();
305         }
306 }
307
308 #define STATE_VERSION   1
309
310 void VM::save_state(FILEIO* state_fio)
311 {
312         state_fio->FputUint32(STATE_VERSION);
313         
314         for(DEVICE* device = first_device; device; device = device->next_device) {
315                 device->save_state(state_fio);
316         }
317 }
318
319 bool VM::load_state(FILEIO* state_fio)
320 {
321         if(state_fio->FgetUint32() != STATE_VERSION) {
322                 return false;
323         }
324         for(DEVICE* device = first_device; device; device = device->next_device) {
325                 if(!device->load_state(state_fio)) {
326                         return false;
327                 }
328         }
329         return true;
330 }
331