OSDN Git Service

4a2d43856f614e602684f6dfde17c6408e92fd76
[csp-qt/common_source_project-fm7.git] / source / src / vm / fmtowns / fmtowns.cpp
1 /*
2         FUJITSU FM-Towns Emulator 'eFMR-60'
3
4         Author : Kyuma Ohta <whatisthis.sowhat _at_ gmail.com>
5         Date   : 216.12.28 -
6
7         [ virtual machine ]
8         History: 
9                 2016-12-28 Copy from eFMR-50.
10 */
11
12 #include "fmtowns.h"
13 #include "../../emu.h"
14 #include "../device.h"
15 #include "../event.h"
16
17 //#include "../hd46505.h"
18 #include "../i8251.h"
19 #include "../i8253.h"
20 #include "../i8259.h"
21
22 #include "../i386.h"
23
24 #include "../io.h"
25 #include "../mb8877.h"
26 #include "../msm58321.h"
27 #include "../pcm1bit.h"
28 #include "../scsi_hdd.h"
29 #include "../scsi_host.h"
30 #include "../upd71071.h"
31
32 #include "towns_crtc.h"
33 // Electric Volume
34 //#include "mb87078.h"
35 //YM-2612 "OPN2"
36 //#include "../ym2612.h"
37 //RF5C68 PCM
38 #include "rp5c68.h"
39 //AD7820 ADC
40 #include "ad7820.h"
41 // 80387?
42
43 #ifdef USE_DEBUGGER
44 #include "../debugger.h"
45 #endif
46
47 #include "cmos.h"
48 #include "floppy.h"
49 #include "keyboard.h"
50 #include "memory.h"
51 #include "scsi.h"
52 //#include "serial.h"
53 #include "timer.h"
54
55 // ----------------------------------------------------------------------------
56 // initialize
57 // ----------------------------------------------------------------------------
58
59 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
60 {
61 /*
62         Machine ID & CPU ID
63
64         FMR-50FD/HD/LT  0xF8
65         FMR-50FX/HX     0xE0
66         FMR-50SFX/SHX   0xE8
67         FMR-50LT        0xF8
68         FMR-50NBX       0x28
69         FMR-50NB        0x60
70         FMR-50NE/T      0x08
71         FMR-CARD        0x70
72
73         80286           0x00
74         80386           0x01
75         80386SX         0x03
76         80486           0x02
77 */
78         
79         // create devices
80         first_device = last_device = NULL;
81         dummy = new DEVICE(this, emu);  // must be 1st device
82         event = new EVENT(this, emu);   // must be 2nd device
83 #if defined(_USE_QT)
84         dummy->set_device_name(_T("1st Dummy"));
85         event->set_device_name(_T("EVENT"));
86 #endif  
87
88         cpu = new I386(this, emu);
89 #if defined(_USE_QT)
90   #if defined(HAS_I386)
91         cpu->set_device_name(_T("CPU(i386)"));
92   #elif defined(HAS_I486)
93         cpu->set_device_name(_T("CPU(i486)"));
94   #elif defined(HAS_PENTIUM)
95         cpu->set_device_name(_T("CPU(Pentium)"));
96   #endif
97 #endif  
98
99         io = new IO(this, emu);
100         
101         crtc = new TOWNS_CRTC(this, emu);
102         cdc  = new CDC(this, emu);
103         cdc_scsi = new SCSI_HOST(this, emu);
104         cdrom = new TOWNS_CDROM(this, emu);
105
106         memory = new TOWNS_MEMORY(this, emu);
107         vram = new TOWNS_VRAM(this, emu);
108         sprite = new TOWNS_SPRITE(this, emu);
109         sysrom = new SYSROM(this, emu);
110         msdosrom = new MSDOSROM(this, emu);
111         fontrom = new FONT_ROM(this, emu);
112         dictionary = new DICTIONARY(this, emu);
113 #if defined(HAS_20PIX_FONTS)
114         fontrom_20pix = new FONT_ROM_20PIX(this, emu);
115 #endif
116         serialrom = new SERIAL_ROM(this, emu);
117
118         adc = new AD7820KR(this, emu);
119         adpcm = new RF5C68(this, emu);
120         e_volume[0] = new MB87878(this, emu);
121         e_volume[1] = new MB87878(this, emu);
122         
123         sio = new I8251(this, emu);
124         pit0 = new I8253(this, emu);
125         pit1 = new I8253(this, emu);
126         pic = new I8259(this, emu);
127         fdc = new MB8877(this, emu);
128         rtc = new MSM58321(this, emu);
129         beep = new PCM1BIT(this, emu);
130         opn2 = new YM2612(this, emu);
131         
132         scsi_host = new SCSI_HOST(this, emu);
133         scsi_host->set_device_name(_T("SCSI HOST"));
134         for(int i = 0; i < 7; i++) {
135                 if(FILEIO::IsFileExisting(create_local_path(_T("SCSI%d.DAT"), i))) {
136                         SCSI_HDD* scsi_hdd = new SCSI_HDD(this, emu);
137 #if defined(_USE_QT)
138                         char d_name[64] = {0};
139                         snprintf(d_name, 64, "SCSI DISK #%d", i + 1);
140                         scsi_hdd->set_device_name(d_name);
141 #endif                  
142                         scsi_hdd->scsi_id = i;
143                         scsi_hdd->set_context_interface(scsi_host);
144                         scsi_host->set_context_target(scsi_hdd);
145                 }
146         }
147         dma = new UPD71071(this, emu);
148         extra_dma = new UPD71071(this, emu);
149
150         floppy = new FLOPPY(this, emu);
151         keyboard = new KEYBOARD(this, emu);
152         memory = new TOWNS_MEMORY(this, emu);
153         scsi = new SCSI(this, emu);
154         timer = new TIMER(this, emu);
155         
156         uint16_t machine_id = 0x0100; // FM-Towns1
157         uint16_t cpu_id = 0x0001;     // i386DX
158         uint32_t cpu_clock = 16000 * 1000; // 16MHz
159 #if defined(_FMTOWNS1_2ND_GEN)
160         machine_id = 0x0200;   // 1F/2F/1H/2H
161 #elif defined(_FMTOWNS_UX_VARIANTS)
162         machine_id = 0x0300;   // UX10/20/40
163         cpu_id = 0x0003;       // i386SX
164 #elif defined(_FMTOWNS1_3RD_GEN)
165         machine_id = 0x0400;  // 10F/20F.40H/80H
166 #elif defined(_FMTOWNS2_CX_VARIANTS)
167         machine_id = 0x0500;  // CX10/20/30/40
168 #elif defined(_FMTOWNS_UG_VARIANTS)
169         machine_id = 0x0600;  // UG10/20/40/80
170         cpu_id = 0x0003;      // i386SX
171         cpu_clock = 20000 * 1000; // 20MHz
172 #elif defined(_FMTOWNS_HR_VARIANTS)
173         machine_id = 0x0700;
174         cpu_id = 0x0002;      // i486SX
175         cpu_clock = 20000 * 1000; // 20MHz
176 #elif defined(_FMTOWNS_HG_VARIANTS)
177         machine_id = 0x0800;
178         cpu_clock = 20000 * 1000; // 20MHz
179 #elif defined(_FMTOWNS_SG_VARIANTS)
180         machine_id = 0x0800; // OK?
181 #elif defined(_FMTOWNS_SR_VARIANTS)
182         machine_id = 0x0700; // OK?
183         cpu_id = 0x0002;      // i486SX
184         cpu_clock = 20000 * 1000; // 20MHz
185 #elif defined(_FMTOWNS_MA_VARIANTS)
186         machine_id = 0x0b00; // OK?
187         cpu_id = 0x0002;      // i486SX
188         cpu_clock = 33000 * 1000; // 33MHz
189 #elif defined(_FMTOWNS_ME_VARIANTS)
190         machine_id = 0x0d00; // OK?
191         cpu_id = 0x0002;      // i486SX
192         cpu_clock = 25000 * 1000; // 25MHz
193 #elif defined(_FMTOWNS_MF_VARIANTS)
194         machine_id = 0x0f00; // OK?
195         cpu_id = 0x0002;      // i486SX
196         cpu_clock = 33000 * 1000; // 33MHz
197 #elif defined(_FMTOWNS_MX_VARIANTS)
198         machine_id = 0x0c00; // OK?
199         cpu_id = 0x0002;      // i486DX (With FPU?)
200         cpu_clock = 66000 * 1000; // 66MHz
201 #elif defined(_FMTOWNS_MX_VARIANTS)
202         machine_id = 0x0c00; // OK?
203         cpu_id = 0x0002;      // i486DX (With FPU?)
204         cpu_clock = 66000 * 1000; // 66MHz
205 #else
206         // ToDo: Pentium Model (After HB).
207
208
209 #endif
210         set_machine_type(machine_id, cpu_id);
211         
212         // set contexts
213         event->set_context_cpu(cpu, cpu_clock);
214         event->set_context_sound(beep);
215         event->set_context_sound(opn2);
216         event->set_context_sound(adpcm);
217         event->set_context_sound(cdc);
218         //event->set_context_sound_in(cdc);
219         //event->set_context_sound_in(line_in);
220         //event->set_context_sound_in(mic);
221         //event->set_context_sound_in(modem);
222
223         if(fdc->load_sound_data(MB8877_SND_TYPE_SEEK, _T("FDDSEEK.WAV"))) {
224                 event->set_context_sound(fdc);
225         }
226         
227 /*      pic     0       timer
228                 1       keyboard
229                 2       rs-232c
230                 3       ex rs-232c
231                 4       (option)
232                 5       (option)
233                 6       floppy drive or dma ???
234                 7       (slave)
235                 8       scsi
236                 9       cd-rom controller
237                 10      (option)
238                 11      crtc vsync
239                 12      printer
240                 13      sound (OPN2 + ADPCM)
241                 14      (option)
242                 15      (reserve)
243         nmi 0   keyboard (RAS)
244         1   extend slot
245         dma     0       floppy drive
246                 1       hard drive
247                 2       printer
248                 3       cd-rom controller
249     dma 4   extend slot
250         5   (reserve)
251         6   (reserve)
252         7   (reserve)
253
254
255 */
256         
257         
258         
259         pit0->set_context_ch0(timer, SIG_TIMER_CH0, 1);
260         pit0->set_context_ch1(timer, SIG_TIMER_CH1, 1);
261         pit0->set_context_ch2(beep, SIG_PCM1BIT_SIGNAL, 1);
262         pit0->set_constant_clock(0, 307200);
263         pit0->set_constant_clock(1, 307200);
264         pit0->set_constant_clock(2, 307200);
265         pit1->set_constant_clock(1, 1228800);
266         pic->set_context_cpu(cpu);
267         fdc->set_context_drq(dma, SIG_UPD71071_CH0, 1);
268         fdc->set_context_irq(floppy, SIG_FLOPPY_IRQ, 1);
269         rtc->set_context_data(timer, SIG_TIMER_RTC, 0x0f, 0);
270         rtc->set_context_busy(timer, SIG_TIMER_RTC, 0x80);
271         scsi_host->set_context_irq(scsi, SIG_SCSI_IRQ, 1);
272         scsi_host->set_context_drq(scsi, SIG_SCSI_DRQ, 1);
273         dma->set_context_memory(memory);
274         dma->set_context_ch0(fdc);
275         dma->set_context_ch1(scsi_host);
276         //dma->set_context_ch2(printer);
277         dma->set_context_ch3(cdc);
278         dma->set_context_child_dma(extra_dma);
279         
280         floppy->set_context_fdc(fdc);
281         floppy->set_context_pic(pic);
282         keyboard->set_context_pic(pic);
283         
284         sprite->set_context_vram(vram);
285
286         //e_volume[0]->set_context_ch0(line_in, MB87878_VOLUME_LEFT);
287         //e_volume[0]->set_context_ch1(line_in, MB87878_VOLUME_RIGHT);
288         //e_volume[0]->set_context_ch2(NULL, MB87878_VOLUME_LEFT);
289         //e_volume[0]->set_context_ch3(NULL, MB87878_VOLUME_RIGHT);
290         e_volume[1]->set_context_ch0(cdc, MB87878_VOLUME_LEFT);
291         e_volume[1]->set_context_ch1(cdc, MB87878_VOLUME_RIGHT);
292         //e_volume[1]->set_context_ch2(mic, MB87878_VOLUME_LEFT | MB87878_VOLUME_RIGHT);
293         //e_volume[1]->set_context_ch3(modem, MB87878_VOLUME_LEFT | MB87878_VOLUME_RIGHT);
294         
295         memory->set_context_vram(vram);
296         memory->set_context_rom(sys_rom);
297         memory->set_context_msdos(msdos_rom);
298         memory->set_context_dictionary(dict_rom);
299         memory->set_context_beep(beep);
300         memory->set_context_serial_rom(serial_rom);
301         memory->set_context_sprite(sprite);
302         memory->set_context_machine_id(machine_id);
303         memory->set_context_cpu_id(cpu_id);
304         memory->set_context_cpu(cpu);
305
306         cdc->set_context_cdrom(cdrom);
307         cdc->set_context_scsi_host(cdc_scsi);
308         cdc->set_context_drq(dma, SIG_UPD71071_CH3, 0xff);
309         cdc->set_context_pic(pic, SIG_I8259_CHIP1 | SIG_I8259_IR1);
310         
311         crtc->set_context_vsync(pic, SIG_I8259_CHIP1 | SIG_I8259_IR3); // VSYNC
312         sound->set_context_pic(pic, SIG_I8259_CHIP1 | SIG_I8259_IR5); // ADPCM AND OPN2
313         sound->set_context_opn2(opn2);
314         sound->set_context_adpcm(adpcm);
315         sound->set_context_adc(adc);
316
317         
318         
319         
320         scsi->set_context_dma(dma);
321         scsi->set_context_pic(pic);
322         scsi->set_context_host(scsi_host);
323         timer->set_context_pcm(beep);
324         timer->set_context_pic(pic);
325         timer->set_context_rtc(rtc);
326         
327         // cpu bus
328         cpu->set_context_mem(memory);
329         cpu->set_context_io(io);
330         cpu->set_context_intr(pic);
331         cpu->set_context_dma(dma);
332
333
334 #ifdef USE_DEBUGGER
335         cpu->set_context_debugger(new DEBUGGER(this, emu));
336 #endif
337
338         
339         // i/o bus
340         io->set_iomap_alias_rw(0x00, pic, I8259_ADDR_CHIP0 | 0);
341         io->set_iomap_alias_rw(0x02, pic, I8259_ADDR_CHIP0 | 1);
342         io->set_iomap_alias_rw(0x10, pic, I8259_ADDR_CHIP1 | 0);
343         io->set_iomap_alias_rw(0x12, pic, I8259_ADDR_CHIP1 | 1);
344         io->set_iomap_single_rw(0x20, memory);  // reset
345         io->set_iomap_single_r(0x21, memory);   // cpu misc
346         io->set_iomap_single_w(0x22, memory);   // power
347         //io->set_iomap_single_rw(0x24, memory);        // dma
348         io->set_iomap_single_r(0x25, memory);   // cpu_misc4 (after Towns2)
349         //io->set_iomap_single_r(0x26, timer);
350         //io->set_iomap_single_r(0x27, timer);
351         io->set_iomap_single_r(0x28, memory);   // NMI MASK (after Towns2)
352         io->set_iomap_single_r(0x30, memory);   // cpu id
353         io->set_iomap_single_r(0x31, memory);   // cpu id
354         
355         io->set_iomap_single_rw(0x32, serial_rom);      // serial rom
356
357         io->set_iomap_alias_rw(0x40, pit0, 0);
358         io->set_iomap_alias_rw(0x42, pit0, 1);
359         io->set_iomap_alias_rw(0x44, pit0, 2);
360         io->set_iomap_alias_rw(0x46, pit0, 3);
361         io->set_iomap_alias_rw(0x50, pit1, 0);
362         io->set_iomap_alias_rw(0x52, pit1, 1);
363         io->set_iomap_alias_rw(0x54, pit1, 2);
364         io->set_iomap_alias_rw(0x56, pit1, 3);
365         
366         io->set_iomap_single_rw(0x60, timer);
367         io->set_iomap_single_rw(0x68, timer); // Interval timer register2 (after Towns 10F).
368         io->set_iomap_single_rw(0x6a, timer); // Interval timer register2 (after Towns 10F).
369         io->set_iomap_single_rw(0x6b, timer); // Interval timer register2 (after Towns 10F).
370         io->set_iomap_single_rw(0x6c, memory); // 1uS wait register (after Towns 10F).
371         
372         io->set_iomap_single_rw(0x70, timer);
373         io->set_iomap_single_w(0x80, timer);
374         
375         io->set_iomap_range_rw(0xa0, 0xaf, dma);
376         io->set_iomap_range_rw(0xb0, 0xbf, extra_dma);
377         
378         io->set_iomap_alias_rw(0x200, fdc, 0);
379         io->set_iomap_alias_rw(0x202, fdc, 1);
380         io->set_iomap_alias_rw(0x204, fdc, 2);
381         io->set_iomap_alias_rw(0x206, fdc, 3);
382         io->set_iomap_single_rw(0x208, floppy);
383         io->set_iomap_single_rw(0x20c, floppy);
384         io->set_iomap_single_rw(0x20e, floppy); // Towns drive SW
385         
386         io->set_iomap_single_rw(0x400, memory); // System Status
387         //io->set_iomap_single_rw(0x402, memory);
388         io->set_iomap_single_rw(0x404, memory); // System status
389         
390         io->set_iomap_range_rw(0x440, 0x443, crtc); // CRTC
391         io->set_iomap_range_rw(0x448, 0x44c, vram); // 
392         io->set_iomap_single_rw(0x450, sprite); //
393         io->set_iomap_single_rw(0x452, sprite); //
394         
395         io->set_iomap_range_rw(0x458, 0x45f, vram); // CRTC
396         
397         io->set_iomap_single_rw(0x480, memory); //
398         io->set_iomap_single_rw(0x484, memory); // Dictionary
399         //io->set_iomap_alias_r(0x48a, memory_card, 0); //
400         //io->set_iomap_alias_rw(0x490, memory_card); // After Towns2
401         //io->set_iomap_alias_rw(0x491, memory_card); // After Towns2
402         
403         io->set_iomap_range_rw(0x4c0, 0x4cf, cdc); // CDROM
404         // PAD, Sound
405         io->set_iomap_alias_r(0x4d0, pad, 0); // Pad1
406         io->set_iomap_alias_r(0x4d2, pad, 1); // Pad 2
407         io->set_iomap_alias_rw(0x4d5, sound, 0); // mute 
408         io->set_iomap_alias_w(0x4d6, pad, 3); // Pad out
409         
410         // OPN2(YM2612)
411         io->set_iomap_alias_rw(0x4d8, opn2, 0); // STATUS(R)/Addrreg 0(W)
412         io->set_iomap_alias_w(0x4da, opn2, 1);  // Datareg 0(W)
413         io->set_iomap_alias_w(0x4dc, opn2, 2);  // Addrreg 1(W)
414         io->set_iomap_alias_w(0x4de, opn2, 3);  // Datareg 1(W)
415         // Electrical volume
416         io->set_iomap_alias_rw(0x4e0, e_volume[0], 0);
417         io->set_iomap_alias_rw(0x4e1, e_volume[0], 1);
418         io->set_iomap_alias_rw(0x4e2, e_volume[1], 0);
419         io->set_iomap_alias_rw(0x4e3, e_volume[1], 1);
420
421         // ADPCM
422         io->set_iomap_single_w(0x4e7, 0x4ff, adpcm); // 
423
424         io->set_iomap_single_rw(0x5c0, memory); // NMI MASK
425         io->set_iomap_single_r(0x5c2, memory);  // NMI STATUS
426         io->set_iomap_single_r(0x5c8, vram); // TVRAM EMULATION
427         io->set_iomap_single_w(0x5ca, vram); // VSYNC INTERRUPT
428         
429         io->set_iomap_single_r(0x5e8, memory); // RAM capacity register.(later Towns1H/2H/1F/2F).
430         io->set_iomap_single_r(0x5ec, memory); // RAM Wait register , ofcially after Towns2, but exists after Towns1H.
431         
432         io->set_iomap_single_rw(0x600, keyboard);
433         io->set_iomap_single_rw(0x602, keyboard);
434         io->set_iomap_single_rw(0x604, keyboard);
435         //io->set_iomap_single_r(0x606, keyboard); // BufFul (After Towns2)
436
437         //io->set_iomap_single_rw(0x800, printer);
438         //io->set_iomap_single_rw(0x802, printer);
439         //io->set_iomap_single_rw(0x804, printer);
440         
441         io->set_iomap_alias_rw(0xa00, sio, 0);
442         io->set_iomap_alias_rw(0xa02, sio, 1);
443 //      io->set_iomap_single_r(0xa04, serial);
444 //      io->set_iomap_single_r(0xa06, serial);
445 //      io->set_iomap_single_w(0xa08, serial);
446 //      io->set_iomap_single_rw(0xa0a, modem);
447         
448         io->set_iomap_single_rw(0xc30, scsi);
449         io->set_iomap_single_rw(0xc32, scsi);
450
451         
452         io->set_iomap_range_rw(0x3000, 0x3fff, memory); // CMOS
453         io->set_iomap_range_rw(0xfd90, 0xfda0, vram);   // Palette and CRTC
454
455         // Vram allocation may be before initialize().
456         bool alloc_failed = false;
457         for(int bank = 0; bank < 2; bank++) {
458                 if(alloc_failed) break;
459                 for(int layer = 0; layer < 2; layer++) {
460                         d_renderbuffer[bank][layer] = NULL;
461                         renderbuffer_size[bank][layer] = 0;
462                         
463                         uint32_t initial_width = 640;
464                         uint32_t initial_height = 480;
465                         uint32_t initial_stride = 640;
466                         uint32_t __size = initial_stride * initial_height * sizeof(scrntype_t);
467                         scrntype_t *p = (scrntype_t*)malloc(__size);
468                         if(p == NULL) {
469                                 alloc_faled = true;
470                                 break;
471                         } else {
472                                 memset(p, 0x00, __size);
473                                 renderbuffer_size[bank][layer] = __size;
474                                 d_renderbuffer[bank][layer] = p;
475                                 d_vram->set_context_renderbuffer(p, layer, bank, width, height, stride);
476                         }
477                 }
478         }
479         if(alloc_failed) {
480                 for(int bank = 0; bank < 2; bank++) {
481                         for(int layer = 0; layer < 2; layer++) {
482                                 renderbuffer_size[bank][layer] = 0;
483                                 if(d_renderbuffer[bank][layer] != NULL) {
484                                         free((void *)(d_renderbuffer[bank][layer]));
485                                 }
486                                 d_renderbuffer[bank][layer] = NULL;
487                                 d_vram->set_context_renderbuffer(NULL, layer, bank, 0, 0, 0);
488                         }
489                 }
490         }
491         // initialize all devices
492 #if defined(__GIT_REPO_VERSION)
493         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
494 #endif
495         for(DEVICE* device = first_device; device; device = device->next_device) {
496                 device->initialize();
497         }
498 }
499
500 VM::~VM()
501 {
502         // delete all devices
503         for(DEVICE* device = first_device; device;) {
504                 DEVICE *next_device = device->next_device;
505                 device->release();
506                 delete device;
507                 device = next_device;
508         }
509 }
510
511 DEVICE* VM::get_device(int id)
512 {
513         for(DEVICE* device = first_device; device; device = device->next_device) {
514                 if(device->this_device_id == id) {
515                         return device;
516                 }
517         }
518         return NULL;
519 }
520
521 void VM::set_machine_type(uint16_t machine_id, uint16_t cpu_id)
522 {
523         if(memory != NULL) {
524                 memory->set_cpu_id(cpu_id);
525                 memory->set_machine_id(machine_id);
526         }
527         if(vram != NULL) {
528                 vram->set_cpu_id(cpu_id);
529                 vram->set_machine_id(machine_id);
530         }
531         if(sprite != NULL) {
532                 sprite->set_cpu_id(cpu_id);
533                 sprite->set_machine_id(machine_id);
534         }
535         if(sys_rom != NULL) {
536                 sysrom->set_cpu_id(cpu_id);
537                 sysrom->set_machine_id(machine_id);
538         }
539         if(msdos_rom != NULL) {
540                 msdosrom->set_cpu_id(cpu_id);
541                 msdosrom->set_machine_id(machine_id);
542         }
543         if(dictinoary != NULL) {
544                 dictionary->set_cpu_id(cpu_id);
545                 dictionary->set_machine_id(machine_id);
546         }
547         if(fontrom != NULL) {
548                 fontrom->set_cpu_id(cpu_id);
549                 fontrom->set_machine_id(machine_id);
550         }
551         if(serialrom != NULL) {
552                 serialrom->set_cpu_id(cpu_id);
553                 serialrom->set_machine_id(machine_id);
554         }
555         if(crtc != NULL) {
556                 crtc->set_cpu_id(cpu_id);
557                 crtc->set_machine_id(machine_id);
558         }
559         if(cdc != NULL) {
560                 cdc->set_cpu_id(cpu_id);
561                 cdc->set_machine_id(machine_id);
562         }
563 #if defined(HAS_20PIX_FONTS)
564         if(fontrom_20pix != NULL) {
565                 fontrom_20pix->set_cpu_id(cpu_id);
566                 fontrom_20pix->set_machine_id(machine_id);
567         }
568 #endif
569         
570 }               
571
572
573 // ----------------------------------------------------------------------------
574 // drive virtual machine
575 // ----------------------------------------------------------------------------
576
577 void VM::reset()
578 {
579         // reset all devices
580         for(DEVICE* device = first_device; device; device = device->next_device) {
581                 device->reset();
582         }
583         // temporary fix...
584         for(DEVICE* device = first_device; device; device = device->next_device) {
585                 device->reset();
586         }
587 }
588
589 void VM::run()
590 {
591         event->drive();
592 }
593
594 // ----------------------------------------------------------------------------
595 // debugger
596 // ----------------------------------------------------------------------------
597
598 #ifdef USE_DEBUGGER
599 DEVICE *VM::get_cpu(int index)
600 {
601         if(index == 0) {
602                 return cpu;
603         }
604         return NULL;
605 }
606 #endif
607
608 // ----------------------------------------------------------------------------
609 // draw screen
610 // ----------------------------------------------------------------------------
611
612 void VM::draw_screen()
613 {
614         memory->draw_screen();
615 }
616
617 uint32_t VM::is_floppy_disk_accessed()
618 {
619         return fdc->read_signal(0);
620 }
621
622 // ----------------------------------------------------------------------------
623 // soud manager
624 // ----------------------------------------------------------------------------
625
626 void VM::initialize_sound(int rate, int samples)
627 {
628         // init sound manager
629         event->initialize_sound(rate, samples);
630         
631         // init sound gen
632         beep->initialize_sound(rate, 8000);
633 }
634
635 uint16_t* VM::create_sound(int* extra_frames)
636 {
637         return event->create_sound(extra_frames);
638 }
639
640 int VM::get_sound_buffer_ptr()
641 {
642         return event->get_sound_buffer_ptr();
643 }
644
645 // ToDo: Sound IN as double buffer.
646 bool VM::clear_sound_in()
647 {
648         bool f = false;
649         sound_in_buf_bank = !sound_in_buf_bank;
650         lock_soundbuf();
651         
652         sound_in_buf = sound_in_buf_pool[(sound_in_buf_bank) ? 1 : 0];
653         if(sound_in_buf != NULL) {
654                 memset(sound_in_buf, 0x00, sound_in_bufsize * 2 * sizeof(int32_t));
655                 f = true;
656         } else {
657                 f = false;
658         }
659         for(int ch = 0; ch < 4; ch++) {
660                 sound_in_bufptr[ch] = 0;
661         }
662         sound_in_outptr = 0;
663         unlock_soundbuf();
664         return f;
665 }
666
667 void VM::initialize_sample_in_buf(int samples)
668 {
669         sound_in_bufsize = 0;
670         sound_in_buf =  NULL;
671         if(samples <= 0) return;
672         
673         sound_in_bufsize = samples;
674         sound_in_buf_pool = (int32_t*)malloc(samples * sizeof(int32_t) * 2);
675
676 }
677
678 int VM::get_samples_in_buf(int ch, int32* dst, int samples)
679 {
680         bool over = false;
681         if(samples <= 0) return 0;
682         if(samples >= sound_in_bufsize) return 0;
683         if((sound_in_outptr + samples) >= sound_in_bufsize) {
684                 samples = sound_in_bufsize - sound_in_outptr;
685                 over = true;
686         }
687         lock_soundbuf();
688         memcpy(dst, &(sound_in_buf[sound_in_outptr << 1]), samples * sizeof(int32_t) * 2);
689         unlock_soundbuf();
690                 
691         return samples;
692 }
693
694 int VM::get_samples_length_sound_in()
695 {
696         return sound_in_bufsize;
697 }
698
699 int VM::sound_in(int ch, int32* src, int samples)
700 {
701         if(ch < 0) return 0;
702         if(ch >= 4) return 0;
703         int left = samples;
704         int np = sound_in_bufptr[ch];
705         int sp = 0;
706         for(;left > 0; left--) {
707                 if(np  >= (sound_in_bufsize << 1)) break;
708                 lock_soundbuf();
709                 sound_in_buf[np + 0] = sound_in_buf[np + 0] + src[sp + 0];
710                 sound_in_buf[np + 1] = sound_in_buf[np + 1] + src[sp + 1];
711                 if(sound_in_buf[np + 0] > 32767) sound_in_buf[np + 0] = 32767;
712                 if(sound_in_buf[np + 0] < -32768) sound_in_buf[np + 0] = -32768;
713                 if(sound_in_buf[np + 1] > 32767) sound_in_buf[np + 1] = 32767;
714                 if(sound_in_buf[np + 1] < -32768) sound_in_buf[np + 1] = -32768;
715                 unlock_soundbuf();
716                 np = np + 2;
717                 sp = sp + 2;
718         }
719         return left;
720 }
721
722 #ifdef USE_SOUND_VOLUME
723 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
724 {
725 #ifndef HAS_LINEIN_SOUND
726         if(ch >= 4) ch++;
727 #endif
728 #ifndef HAS_MIC_SOUND
729         if(ch >= 5) ch++;
730 #endif
731 #ifndef HAS_MODEM_SOUND
732         if(ch >= 6) ch++;
733 #endif
734 #ifndef HAS_2ND_ADPCM
735         if(ch >= 7) ch++;
736 #endif
737         if(ch == 0) { // BEEP
738                 beep->set_volume(0, decibel_l, decibel_r);
739         }
740         else if(ch == 1) { // CD-ROM
741                 e_volume[1]->set_volume(0, decibel_l);
742                 e_volume[1]->set_volume(1, decibel_r);
743         }
744         else if(ch == 2) { // OPN2
745                 opn2->set_volume(0, decibel_l, decibel_r);
746         }
747         else if(ch == 3) { // ADPCM
748                 adpcm->set_volume(0, decibel_l, decibel_r);
749         }
750         else if(ch == 4) { // LINE IN
751                 e_volume[0]->set_volume(0, decibel_l);
752                 e_volume[0]->set_volume(1, decibel_r);
753         } 
754         else if(ch == 5) { // MIC
755                 e_volume[1]->set_volume(2, (decibel_l + decibel_r) / 2);
756         } 
757         else if(ch == 6) { // MODEM
758                 e_volume[1]->set_volume(2, (decibel_l + decibel_r) / 2);
759         }
760         else if(ch == 7) { // ADPCM
761                 adpcm2->set_volume(0, decibel_l, decibel_r);
762         }
763         else if(ch == 8) { // FDD
764                 fdc->set_volume(0, decibel_l, decibel_r);
765         }
766         else if(ch == 9) { // HDD(ToDo)
767                 fdc->set_volume(0, decibel_l, decibel_r);
768         }       
769
770 }
771 #endif
772
773 // ----------------------------------------------------------------------------
774 // notify key
775 // ----------------------------------------------------------------------------
776
777 void VM::key_down(int code, bool repeat)
778 {
779         keyboard->key_down(code);
780 }
781
782 void VM::key_up(int code)
783 {
784         keyboard->key_up(code);
785 }
786
787 // ----------------------------------------------------------------------------
788 // user interface
789 // ----------------------------------------------------------------------------
790
791 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
792 {
793         fdc->open_disk(drv, file_path, bank);
794         floppy->change_disk(drv);
795 }
796
797 void VM::close_floppy_disk(int drv)
798 {
799         fdc->close_disk(drv);
800 }
801
802 bool VM::is_floppy_disk_inserted(int drv)
803 {
804         return fdc->is_disk_inserted(drv);
805 }
806
807 void VM::is_floppy_disk_protected(int drv, bool value)
808 {
809         fdc->is_disk_protected(drv, value);
810 }
811
812 bool VM::is_floppy_disk_protected(int drv)
813 {
814         return fdc->is_disk_protected(drv);
815 }
816
817 bool VM::is_frame_skippable()
818 {
819         return event->is_frame_skippable();
820 }
821
822 void VM::update_config()
823 {
824         for(DEVICE* device = first_device; device; device = device->next_device) {
825                 device->update_config();
826         }
827 }
828
829 #define STATE_VERSION   3
830
831 void VM::save_state(FILEIO* state_fio)
832 {
833         state_fio->FputUint32(STATE_VERSION);
834         
835         for(DEVICE* device = first_device; device; device = device->next_device) {
836                 device->save_state(state_fio);
837         }
838 }
839
840 bool VM::load_state(FILEIO* state_fio)
841 {
842         if(state_fio->FgetUint32() != STATE_VERSION) {
843                 return false;
844         }
845         for(DEVICE* device = first_device; device; device = device->next_device) {
846                 if(!device->load_state(state_fio)) {
847                         return false;
848                 }
849         }
850         return true;
851 }
852