OSDN Git Service

[VM] Apply new APIs to all VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / pc100 / pc100.cpp
1 /*
2         NEC PC-100 Emulator 'ePC-100'
3
4         Author : Takeda.Toshiya
5         Date   : 2008.07.12 -
6
7         [ virtual machine ]
8 */
9
10 #include "pc100.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../and.h"
16 #include "../beep.h"
17 #include "../disk.h"
18 #include "../i8251.h"
19 #include "../i8255.h"
20 #include "../i8259.h"
21 #include "../i86.h"
22 #include "../io.h"
23 #include "../memory.h"
24 #include "../msm58321.h"
25 #include "../noise.h"
26 #include "../pcm1bit.h"
27 #include "../upd765a.h"
28
29 #ifdef USE_DEBUGGER
30 #include "../debugger.h"
31 #endif
32
33 #include "crtc.h"
34 #include "ioctrl.h"
35 #include "kanji.h"
36
37 using PC100::CRTC;
38 using PC100::IOCTRL;
39 using PC100::KANJI;
40
41 // ----------------------------------------------------------------------------
42 // initialize
43 // ----------------------------------------------------------------------------
44
45 VM::VM(EMU_TEMPLATE* parent_emu) : VM_TEMPLATE(parent_emu)
46 {
47         // create devices
48         first_device = last_device = NULL;
49         dummy = new DEVICE(this, emu);  // must be 1st device
50         event = new EVENT(this, emu);   // must be 2nd device
51         
52         and_drq = new AND(this, emu);
53         beep = new BEEP(this, emu);
54 #ifdef USE_DEBUGGER
55 //      beep->set_context_debugger(new DEBUGGER(this, emu));
56 #endif
57         sio = new I8251(this, emu);
58         pio0 = new I8255(this, emu);
59         pio0->set_device_name(_T("8255 PIO (RTC)"));
60         pio1 = new I8255(this, emu);
61         pio1->set_device_name(_T("8255 PIO (CRTC)"));
62         pic = new I8259(this, emu);
63         cpu = new I86(this, emu);
64         cpu->device_model = INTEL_8086;
65         
66         io = new IO(this, emu);
67         memory = new MEMORY(this, emu);
68         
69         rtc = new MSM58321(this, emu);
70         pcm = new PCM1BIT(this, emu);
71         fdc = new UPD765A(this, emu);
72 #ifdef USE_DEBUGGER
73 //      fdc->set_context_debugger(new DEBUGGER(this, emu));
74 #endif
75         fdc->set_context_noise_seek(new NOISE(this, emu));
76         fdc->set_context_noise_head_down(new NOISE(this, emu));
77         fdc->set_context_noise_head_up(new NOISE(this, emu));
78         
79         crtc = new CRTC(this, emu);
80         ioctrl = new IOCTRL(this, emu);
81         kanji = new KANJI(this, emu);
82         
83         // set contexts
84         event->set_context_cpu(cpu);
85         event->set_context_sound(beep);
86         event->set_context_sound(pcm);
87         event->set_context_sound(fdc->get_context_noise_seek());
88         event->set_context_sound(fdc->get_context_noise_head_down());
89         event->set_context_sound(fdc->get_context_noise_head_up());
90         
91         and_drq->set_context_out(cpu, SIG_CPU_NMI, 1);
92         and_drq->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
93         sio->set_context_rxrdy(pic, SIG_I8259_IR1, 1);
94         pio0->set_context_port_a(rtc, SIG_MSM58321_READ, 1, 0);
95         pio0->set_context_port_a(rtc, SIG_MSM58321_WRITE, 2, 0);
96         pio0->set_context_port_a(rtc, SIG_MSM58321_ADDR_WRITE, 4, 0);
97         pio0->set_context_port_c(rtc, SIG_MSM58321_DATA, 0x0f, 0);
98         pio1->set_context_port_a(crtc, SIG_CRTC_BITMASK_LOW, 0xff, 0);
99         pio1->set_context_port_b(crtc, SIG_CRTC_BITMASK_HIGH, 0xff, 0);
100         pio1->set_context_port_c(crtc, SIG_CRTC_VRAM_PLANE, 0x3f, 0);
101         pio1->set_context_port_c(and_drq, SIG_AND_BIT_0, 0x80, 0);
102         pio1->set_context_port_c(ioctrl, SIG_IOCTRL_RESET, 0x40, 0);
103         pic->set_context_cpu(cpu);
104         rtc->set_context_data(pio0, SIG_I8255_PORT_C, 0x0f, 0);
105         rtc->set_context_busy(pio0, SIG_I8255_PORT_C, 0x10);
106         fdc->set_context_irq(cpu, SIG_CPU_NMI, 1);
107         fdc->set_context_drq(and_drq, SIG_AND_BIT_1, 1);
108         
109         crtc->set_context_pic(pic);
110         ioctrl->set_context_pic(pic);
111         ioctrl->set_context_fdc(fdc);
112         ioctrl->set_context_beep(beep);
113         ioctrl->set_context_pcm(pcm);
114         
115         // cpu bus
116         cpu->set_context_mem(memory);
117         cpu->set_context_io(io);
118         cpu->set_context_intr(pic);
119 #ifdef USE_DEBUGGER
120         cpu->set_context_debugger(new DEBUGGER(this, emu));
121 #endif
122         
123         // memory bus
124         memset(ram, 0, sizeof(ram));
125         memset(ipl, 0xff, sizeof(ipl));
126         
127         memory->read_bios(_T("IPL.ROM"), ipl, sizeof(ipl));
128         
129         memory->set_memory_rw(0x00000, 0xbffff, ram);
130         memory->set_memory_mapped_io_rw(0xc0000, 0xdffff, crtc);
131         memory->set_memory_r(0xf8000, 0xfffff, ipl);
132         
133         // i/o bus
134         io->set_iomap_alias_rw(0x00, pic, 0);
135         io->set_iomap_alias_rw(0x02, pic, 1);
136 //      io->set_iomap_alias_rw(0x04, dma, 0);
137 //      io->set_iomap_alias_rw(0x06, dma, 1);
138         io->set_iomap_alias_r(0x08, fdc, 0);
139         io->set_iomap_alias_rw(0x0a, fdc, 1);
140         io->set_iomap_alias_rw(0x10, pio0, 0);
141         io->set_iomap_alias_rw(0x12, pio0, 1);
142         io->set_iomap_alias_rw(0x14, pio0, 2);
143         io->set_iomap_alias_rw(0x16, pio0, 3);
144         io->set_iomap_alias_rw(0x18, pio1, 0);
145         io->set_iomap_alias_rw(0x1a, pio1, 1);
146         io->set_iomap_alias_rw(0x1c, pio1, 2);
147         io->set_iomap_alias_rw(0x1e, pio1, 3);
148         io->set_iomap_single_r(0x20, ioctrl);
149         io->set_iomap_single_rw(0x22, ioctrl);
150         io->set_iomap_single_w(0x24, ioctrl);
151         io->set_iomap_alias_rw(0x28, sio, 0);
152         io->set_iomap_alias_rw(0x2a, sio, 1);
153         io->set_iomap_single_rw(0x30, crtc);
154         io->set_iomap_single_rw(0x38, crtc);
155         io->set_iomap_single_rw(0x3a, crtc);
156         io->set_iomap_single_rw(0x3c, crtc);
157         io->set_iomap_single_rw(0x3e, crtc);
158         for(int i = 0x40; i < 0x62; i++) {
159                 io->set_iomap_single_rw(i, crtc);
160         }
161         io->set_iomap_single_rw(0x80, kanji);
162         io->set_iomap_single_rw(0x81, kanji);
163         io->set_iomap_single_w(0x84, kanji);
164         io->set_iomap_single_w(0x86, kanji);
165         
166         // initialize all devices
167 #if defined(__GIT_REPO_VERSION)
168         set_git_repo_version(__GIT_REPO_VERSION);
169 #endif
170         initialize_devices();
171         
172         for(int i = 0; i < 4; i++) {
173                 if(config.drive_type) {
174                         fdc->set_drive_type(i, DRIVE_TYPE_2DD);
175                 } else {
176                         fdc->set_drive_type(i, DRIVE_TYPE_2D);
177                 }
178         }
179 }
180
181 VM::~VM()
182 {
183         // delete all devices
184         for(DEVICE* device = first_device; device;) {
185                 DEVICE *next_device = device->next_device;
186                 device->release();
187                 delete device;
188                 device = next_device;
189         }
190 }
191
192 DEVICE* VM::get_device(int id)
193 {
194         for(DEVICE* device = first_device; device; device = device->next_device) {
195                 if(device->this_device_id == id) {
196                         return device;
197                 }
198         }
199         return NULL;
200 }
201
202 // ----------------------------------------------------------------------------
203 // drive virtual machine
204 // ----------------------------------------------------------------------------
205
206 void VM::reset()
207 {
208         // reset all devices
209         for(DEVICE* device = first_device; device; device = device->next_device) {
210                 device->reset();
211         }
212         for(int i = 0; i < 4; i++) {
213                 if(config.drive_type) {
214                         fdc->set_drive_type(i, DRIVE_TYPE_2DD);
215                 } else {
216                         fdc->set_drive_type(i, DRIVE_TYPE_2D);
217                 }
218         }
219 }
220
221 void VM::run()
222 {
223         event->drive();
224 }
225
226 // ----------------------------------------------------------------------------
227 // debugger
228 // ----------------------------------------------------------------------------
229
230 #ifdef USE_DEBUGGER
231 DEVICE *VM::get_cpu(int index)
232 {
233         if(index == 0) {
234                 return cpu;
235         }
236         return NULL;
237 }
238 #endif
239
240 // ----------------------------------------------------------------------------
241 // draw screen
242 // ----------------------------------------------------------------------------
243
244 void VM::draw_screen()
245 {
246         crtc->draw_screen();
247 }
248
249 // ----------------------------------------------------------------------------
250 // soud manager
251 // ----------------------------------------------------------------------------
252
253 void VM::initialize_sound(int rate, int samples)
254 {
255         // init sound manager
256         event->initialize_sound(rate, samples);
257         
258         // init sound gen
259         beep->initialize_sound(rate, 2400, 8000);
260         pcm->initialize_sound(rate, 8000);
261 }
262
263 uint16_t* VM::create_sound(int* extra_frames)
264 {
265         return event->create_sound(extra_frames);
266 }
267
268 int VM::get_sound_buffer_ptr()
269 {
270         return event->get_sound_buffer_ptr();
271 }
272
273 #ifdef USE_SOUND_VOLUME
274 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
275 {
276         if(ch == 0) {
277                 beep->set_volume(0, decibel_l, decibel_r);
278         } else if(ch == 1) {
279                 pcm->set_volume(0, decibel_l, decibel_r);
280         } else if(ch == 2) {
281                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
282                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
283                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
284         }
285 }
286 #endif
287
288 // ----------------------------------------------------------------------------
289 // notify key
290 // ----------------------------------------------------------------------------
291
292 void VM::key_down(int code, bool repeat)
293 {
294         ioctrl->key_down(code);
295 }
296
297 void VM::key_up(int code)
298 {
299         ioctrl->key_up(code);
300 }
301
302 bool VM::get_caps_locked()
303 {
304         return ioctrl->get_caps_locked();
305 }
306
307 bool VM::get_kana_locked()
308 {
309         return ioctrl->get_kana_locked();
310 }
311
312 // ----------------------------------------------------------------------------
313 // user interface
314 // ----------------------------------------------------------------------------
315
316 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
317 {
318         fdc->open_disk(drv, file_path, bank);
319 }
320
321 void VM::close_floppy_disk(int drv)
322 {
323         fdc->close_disk(drv);
324 }
325
326 bool VM::is_floppy_disk_inserted(int drv)
327 {
328         return fdc->is_disk_inserted(drv);
329 }
330
331 void VM::is_floppy_disk_protected(int drv, bool value)
332 {
333         fdc->is_disk_protected(drv, value);
334 }
335
336 bool VM::is_floppy_disk_protected(int drv)
337 {
338         return fdc->is_disk_protected(drv);
339 }
340
341 uint32_t VM::is_floppy_disk_accessed()
342 {
343         return fdc->read_signal(0);
344 }
345
346 bool VM::is_frame_skippable()
347 {
348         return event->is_frame_skippable();
349 }
350
351 void VM::update_config()
352 {
353         for(DEVICE* device = first_device; device; device = device->next_device) {
354                 device->update_config();
355         }
356 }
357
358 double VM::get_current_usec()
359 {
360         if(event == NULL) return 0.0;
361         return event->get_current_usec();
362 }
363
364 uint64_t VM::get_current_clock_uint64()
365 {
366                 if(event == NULL) return (uint64_t)0;
367                 return event->get_current_clock_uint64();
368 }
369
370 #define STATE_VERSION   4
371
372 bool VM::process_state(FILEIO* state_fio, bool loading)
373 {
374         if(!(VM_TEMPLATE::process_state_core(state_fio, loading, STATE_VERSION))) {
375                 return false;
376         }
377         // Machine specified.
378         state_fio->StateArray(ram, sizeof(ram), 1);
379         return true;
380 }