2 FUJITSU FM-Towns Emulator 'eFMTowns'
4 Author : Kyuma Ohta <whatisthis.sowhat _at_ gmail.com>
9 2016-12-28 Copy from eFMR-50.
13 #include "../../emu.h"
14 #include "../device.h"
17 //#include "../hd46505.h"
22 #include "../i386_np21.h"
23 //#include "../i386.h"
26 #include "../mb8877.h"
27 #include "../msm58321.h"
29 #include "../pcm1bit.h"
30 #include "../harddisk.h"
31 #include "../scsi_hdd.h"
32 #include "./towns_scsi_host.h"
33 #include "../upd71071.h"
37 #include "./dictionary.h"
39 #include "./towns_memory.h"
47 //#include "../ym2612.h"
56 #include "../debugger.h"
62 #include "./fontroms.h"
63 #include "./joystick.h"
64 #include "./joypad_2btn.h"
65 #include "./joypad_6btn.h"
66 #include "./keyboard.h"
68 #include "./msdosrom.h"
70 #include "./serialrom.h"
74 #include "./planevram.h"
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
81 using FMTOWNS::DICTIONARY;
82 using FMTOWNS::FLOPPY;
83 using FMTOWNS::FONT_ROMS;
84 using FMTOWNS::JOYSTICK;
85 using FMTOWNS::JOYPAD_2BTN;
86 using FMTOWNS::JOYPAD_6BTN;
88 using FMTOWNS::KEYBOARD;
90 using FMTOWNS::MSDOSROM;
92 using FMTOWNS::SERIAL_ROM;
93 using FMTOWNS::SYSROM;
95 using FMTOWNS::TOWNS_ICCARD;
97 using FMTOWNS::TOWNS_CDROM;
98 using FMTOWNS::TOWNS_CRTC;
99 using FMTOWNS::TOWNS_DMAC;
100 using FMTOWNS::TOWNS_MEMORY;
101 using FMTOWNS::TOWNS_SCSI_HOST;
102 using FMTOWNS::TOWNS_SPRITE;
103 using FMTOWNS::TOWNS_VRAM;
104 using FMTOWNS::PLANEVRAM;
107 VM::VM(EMU_TEMPLATE* parent_emu) : VM_TEMPLATE(parent_emu)
128 //first_device = last_device = nullptr;
129 dummy = new DEVICE(this, emu); // must be 1st device
130 event = new EVENT(this, emu); // must be 2nd device
132 dummy->set_device_name(_T("1st Dummy"));
133 event->set_device_name(_T("EVENT"));
136 cpu = new I386(this, emu);
138 #if defined(HAS_I386)
139 cpu->set_device_name(_T("CPU(i386)"));
140 #elif defined(HAS_I486)
141 cpu->set_device_name(_T("CPU(i486)"));
142 #elif defined(HAS_PENTIUM)
143 cpu->set_device_name(_T("CPU(Pentium)"));
147 io = new IO(this, emu);
148 io->space = _IO_SPACE;
149 io->bus_width = _IO_BUS_WIDTH;
151 crtc = new TOWNS_CRTC(this, emu);
152 cdrom = new TOWNS_CDROM(this, emu);
154 memory = new TOWNS_MEMORY(this, emu);
155 memory->space = _MEMORY_SPACE;
156 memory->bank_size = _MEMORY_BANK_SIZE;
157 memory->bus_width = _MEMORY_BUS_WIDTH;
159 vram = new TOWNS_VRAM(this, emu);
160 sprite = new TOWNS_SPRITE(this, emu);
161 sysrom = new SYSROM(this, emu);
162 msdosrom = new MSDOSROM(this, emu);
163 fontrom = new FONT_ROMS(this, emu);
164 dictionary = new DICTIONARY(this, emu);
165 #if defined(HAS_20PIX_FONTS)
166 fontrom_20pix = new FONT_ROM_20PIX(this, emu);
168 serialrom = new SERIAL_ROM(this, emu);
170 adpcm = new ADPCM(this, emu);
171 // mixer = new MIXER(this, emu); // Pseudo mixer.
173 planevram = new PLANEVRAM(this, emu);
175 adc = new AD7820KR(this, emu);
176 rf5c68 = new RF5C68(this, emu);
177 e_volumes[0] = new MB87078(this, emu);
178 e_volumes[1] = new MB87078(this, emu);
180 sio = new I8251(this, emu);
181 pit0 = new I8253(this, emu);
182 pit0->device_model = INTEL_8253;
184 pit1 = new I8253(this, emu);
185 pit1->device_model = INTEL_8253;
187 pic = new I8259(this, emu);
189 fdc = new MB8877(this, emu);
190 rtc = new MSM58321(this, emu);
191 beep = new PCM1BIT(this, emu);
192 opn2 = new YM2612(this, emu);
194 seek_sound = new NOISE(this, emu);
195 head_up_sound = new NOISE(this, emu);
196 head_down_sound = new NOISE(this, emu);
198 // scsi_host = new TOWNS_SCSI_HOST(this, emu);
199 scsi_host = new SCSI_HOST(this, emu);
201 for(int i = 0; i < 7; i++) {
202 scsi_hdd[i] = nullptr;
204 #if defined(USE_HARD_DISK)
205 for(int i = 0; i < USE_HARD_DISK; i++) {
206 scsi_hdd[i] = new SCSI_HDD(this, emu);
207 scsi_hdd[i]->set_device_name(_T("SCSI Hard Disk Drive #%d"), i + 1);
208 scsi_hdd[i]->scsi_id = i ;
209 scsi_hdd[i]->set_disk_handler(0, new HARDDISK(emu));
210 scsi_hdd[i]->set_context_interface(scsi_host);
211 my_sprintf_s(scsi_hdd[i]->vendor_id, 9, "FUJITSU");
212 my_sprintf_s(scsi_hdd[i]->product_id, 17, "SCSI-HDD");
213 scsi_host->set_context_target(scsi_hdd[i]);
216 dma = new TOWNS_DMAC(this, emu);
217 extra_dma = new TOWNS_DMAC(this, emu);
219 floppy = new FLOPPY(this, emu);
220 keyboard = new KEYBOARD(this, emu);
221 joystick = new JOYSTICK(this, emu);
222 scsi = new SCSI(this, emu);
223 timer = new TIMER(this, emu);
225 iccard1 = new TOWNS_ICCARD(this, emu);
227 iccard2 = new TOWNS_ICCARD(this, emu);
231 for(int i = 0; i < 2; i++) {
232 joypad_2btn[i] = new JOYPAD_2BTN(this, emu);
233 joypad_6btn[i] = new JOYPAD_6BTN(this, emu);
234 mouse[i] = new MOUSE(this, emu);
237 uint16_t machine_id = 0x0100; // FM-Towns1
238 uint16_t cpu_id = 0x0001; // i386DX
239 uint32_t cpu_clock = 16000 * 1000; // 16MHz
240 #if defined(_FMTOWNS1_2ND_GEN)
241 machine_id = 0x0200; // 1F/2F/1H/2H
242 #elif defined(_FMTOWNS_UX_VARIANTS)
243 machine_id = 0x0300; // UX10/20/40
244 cpu_id = 0x0003; // i386SX
245 #elif defined(_FMTOWNS1_3RD_GEN)
246 machine_id = 0x0400; // 10F/20F.40H/80H
247 #elif defined(_FMTOWNS2_CX_VARIANTS)
248 machine_id = 0x0500; // CX10/20/30/40
249 #elif defined(_FMTOWNS_UG_VARIANTS)
250 machine_id = 0x0600; // UG10/20/40/80
251 cpu_id = 0x0003; // i386SX
252 cpu_clock = 20000 * 1000; // 20MHz
253 #elif defined(_FMTOWNS_HR_VARIANTS)
255 cpu_id = 0x0002; // i486SX
256 cpu_clock = 20000 * 1000; // 20MHz
257 #elif defined(_FMTOWNS_HG_VARIANTS)
259 cpu_clock = 20000 * 1000; // 20MHz
260 #elif defined(_FMTOWNS_SG_VARIANTS)
261 machine_id = 0x0800; // OK?
262 #elif defined(_FMTOWNS_SR_VARIANTS)
263 machine_id = 0x0700; // OK?
264 cpu_id = 0x0002; // i486SX
265 cpu_clock = 20000 * 1000; // 20MHz
266 #elif defined(_FMTOWNS_UR_VARIANTS)
267 machine_id = 0x0900; // UR10/20/40/80
268 cpu_id = 0x0002; // i486DX
269 cpu_clock = 20000 * 1000; // ToDo: Correct frequency.
270 #elif defined(_FMTOWNS_MA_VARIANTS)
271 machine_id = 0x0b00; // OK?
272 cpu_id = 0x0002; // i486SX
273 cpu_clock = 33000 * 1000; // 33MHz
274 #elif defined(_FMTOWNS_ME_VARIANTS)
275 machine_id = 0x0d00; // OK?
276 cpu_id = 0x0002; // i486SX
277 cpu_clock = 25000 * 1000; // 25MHz
278 #elif defined(_FMTOWNS_MF_VARIANTS)
279 machine_id = 0x0f00; // OK?
280 cpu_id = 0x0002; // i486SX
281 cpu_clock = 33000 * 1000; // 33MHz
282 #elif defined(_FMTOWNS_MX_VARIANTS)
283 machine_id = 0x0c00; // OK?
284 cpu_id = 0x0002; // i486DX (With FPU?)
285 cpu_clock = 66000 * 1000; // 66MHz
286 #elif defined(_FMTOWNS_HC_VARIANTS)
288 // From FMTowns::MachineID() of TSUGARU,
289 // git 83d4ec2309ac9fcbb8c01f26061ff0d49c5321e4.
290 machine_id = 0x1100; // OK?
291 cpu_id = 0x0002; // Pentium (With FPU?)
292 cpu_clock = 50000 * 1000; // ToDo: Correctness frequency.
294 // ToDo: Pentium Model (After HB).
298 event->set_frames_per_sec(FRAMES_PER_SEC);
299 event->set_lines_per_frame(LINES_PER_FRAME);
301 set_machine_type(machine_id, cpu_id);
303 event->set_context_cpu(cpu, cpu_clock);
310 // Use pseudo mixer instead of event.Due to using ADC.
311 // Temporally not use mixer.
312 event->set_context_sound(beep);
313 event->set_context_sound(opn2);
314 event->set_context_sound(rf5c68);
315 event->set_context_sound(cdrom);
317 fdc->set_context_noise_seek(seek_sound);
318 fdc->set_context_noise_head_down(head_down_sound);
319 fdc->set_context_noise_head_up(head_up_sound);
320 event->set_context_sound(seek_sound);
321 event->set_context_sound(head_down_sound);
322 event->set_context_sound(head_up_sound);
325 pit0->set_context_debugger(new DEBUGGER(this, emu));
326 pit1->set_context_debugger(new DEBUGGER(this, emu));
328 pit0->set_context_ch0(timer, SIG_TIMER_CH0, 1);
329 pit0->set_context_ch1(timer, SIG_TIMER_CH1, 1);
330 pit0->set_context_ch2(beep, SIG_PCM1BIT_SIGNAL, 1);
331 pit0->set_constant_clock(0, 307200);
332 pit0->set_constant_clock(1, 307200);
333 pit0->set_constant_clock(2, 307200);
334 pit1->set_constant_clock(0, 1229900);
335 pit1->set_constant_clock(1, 1229900);
336 pit1->set_constant_clock(2, 1229900);
337 pic->set_context_cpu(cpu);
338 //pic->set_context_cpu(memory);
339 fdc->set_context_irq(floppy, SIG_FLOPPY_IRQ, 1);
340 rtc->set_context_data(timer, SIG_TIMER_RTC, 0x0f, 0);
341 rtc->set_context_busy(timer, SIG_TIMER_RTC_BUSY, 0x80);
342 scsi_host->set_context_irq(scsi, SIG_SCSI_IRQ, 1);
343 scsi_host->set_context_drq(scsi, SIG_SCSI_DRQ, 1);
344 scsi_host->set_context_drq(keyboard, SIG_KEYBOARD_BOOTSEQ_END, 1);
347 dma->set_context_debugger(new DEBUGGER(this, emu));
348 extra_dma->set_context_debugger(new DEBUGGER(this, emu));
350 //dma->set_context_cpu(cpu);
351 dma->set_context_memory(memory);
352 dma->set_context_ch0(fdc);
353 dma->set_context_ch1(scsi_host);
354 //dma->set_context_ch2(printer);
355 dma->set_context_ch3(cdrom);
356 dma->set_context_tc1(scsi, SIG_SCSI_EOT, 0xffffffff);
357 dma->set_context_tc3(cdrom, SIG_TOWNS_CDROM_DMAINT, 0xffffffff);
359 //dma->set_context_ack1(scsi_host, SIG_SCSI_ACK, 0xffffffff);
360 dma->set_context_ack3(cdrom, SIG_TOWNS_CDROM_DMAACK, 0xffffffff);
361 dma->set_context_ube1(scsi_host, SIG_SCSI_16BIT_BUS, 0x02);
362 dma->set_context_child_dma(extra_dma);
364 floppy->set_context_fdc(fdc);
366 sprite->set_context_vram(vram);
367 // sprite->set_context_font(fontrom);
368 // sprite->set_context_crtc(crtc);
370 sprite->set_context_debugger(new DEBUGGER(this, emu));
373 planevram->set_context_vram(vram);
374 planevram->set_context_sprite(sprite);
375 planevram->set_context_crtc(crtc);
377 crtc->set_context_sprite(sprite);
378 crtc->set_context_vram(vram);
379 crtc->set_context_font(fontrom);
381 // e_volumes[0]->set_context_device(0, line_in, 0,
382 // MB87078_TYPE_MASK_LEFT);
383 // e_volumes[0]->set_context_device(1, line_in, 0,
384 // MB87078_TYPE_MASK_RIGHT);
385 e_volumes[1]->set_context_device(0, cdrom, 0,
386 MB87078_TYPE_SET_LEFT, SIG_TOWNS_CDROM_MUTE_L,
391 e_volumes[1]->set_context_device(1, cdrom, 0,
392 MB87078_TYPE_SET_RIGHT, SIG_TOWNS_CDROM_MUTE_R,
398 memory->set_context_cpu(cpu);
399 memory->set_context_dmac(dma);
400 memory->set_context_vram(vram);
401 memory->set_context_planevram(planevram);
402 memory->set_context_crtc(crtc);
403 memory->set_context_system_rom(sysrom);
404 memory->set_context_msdos(msdosrom);
405 memory->set_context_dictionary(dictionary);
406 memory->set_context_font_rom(fontrom);
407 memory->set_context_timer(timer);
408 memory->set_context_serial_rom(serialrom);
409 memory->set_context_sprite(sprite);
410 memory->set_context_pcm(rf5c68);
411 memory->set_context_iccard(iccard1, 0);
412 memory->set_context_iccard(iccard2, 1);
414 adpcm->set_context_opn2(opn2);
415 adpcm->set_context_rf5c68(rf5c68);
416 adpcm->set_context_adc(adc);
418 rf5c68->set_context_interrupt_boundary(adpcm, SIG_ADPCM_WRITE_INTERRUPT, 0xffffffff);
420 rf5c68->set_context_debugger(new DEBUGGER(this, emu));
422 opn2->set_context_irq(adpcm, SIG_ADPCM_OPX_INTR, 0xffffffff);
424 adc->set_sample_rate(19200);
425 adc->set_sound_bank(-1);
426 adc->set_context_interrupt(adpcm, SIG_ADPCM_ADC_INTR, 0xffffffff);
428 scsi->set_context_dma(dma);
429 scsi->set_context_host(scsi_host);
430 scsi->set_context_pic(pic);
431 timer->set_context_pcm(beep);
432 timer->set_context_rtc(rtc);
433 //timer->set_context_halt_line(cpu, SIG_CPU_HALTREQ, 0xffffffff);
434 timer->set_context_halt_line(cpu, SIG_CPU_BUSREQ, 0xffffffff);
436 for(int i = 0; i < 2; i++) {
438 // 0: Towns PAD 2buttons
439 // 1: Towns PAD 6buttons
441 // 3: Analog Pad (reserved)
442 // 4: Libble Rabble stick (reserved)
443 joystick->set_context_joystick(i, joypad_2btn[i]);
444 joystick->set_context_joystick(i, joypad_6btn[i]);
445 joystick->set_context_joystick(i, mouse[i]);
448 joystick->set_context_debugger(new DEBUGGER(this, emu));
450 // ToDo: Selective by config.
451 for(int i = 0; i < 2; i++) {
452 joypad_2btn[i]->set_context_pad_num(i);
453 joypad_2btn[i]->set_context_parent_port(i, joystick, 0, 0xff);
454 joypad_2btn[i]->set_negative_logic(true);
455 joypad_2btn[i]->set_enable(true);
457 for(int i = 0; i < 2; i++) {
458 joypad_6btn[i]->set_context_pad_num(i);
459 joypad_6btn[i]->set_context_parent_port(i, joystick, 0, 0xff);
460 joypad_6btn[i]->set_negative_logic(true);
461 joypad_6btn[i]->set_enable(false);
463 for(int i = 0; i < 2; i++) {
464 mouse[i]->set_context_pad_num(i);
465 mouse[i]->set_context_parent_port(i, joystick, 0, 0xff);
466 mouse[i]->set_negative_logic(true);
467 mouse[i]->set_enable(false);
469 joystick->set_using_pad(0, -1);
470 joystick->set_using_pad(1, -1);
473 cpu->set_context_mem(memory);
474 cpu->set_context_io(io);
475 cpu->set_context_intr(pic);
476 cpu->set_context_dma(dma);
477 cpu->set_context_bios(nullptr);
478 cpu->set_context_extreset(memory, SIG_FMTOWNS_NOTIFY_RESET, 0xffffffff);
480 cpu->set_context_debugger(new DEBUGGER(this, emu));
485 // IRQ2 : USART (ToDo)
486 // IRQ3 : EXTRA USART (ToDo)
487 // IRQ4 : EXTRA I/O (Maybe not implement)
488 // IRQ5 : EXTRA I/O (Maybe not implement)
490 // IRQ7 : Deisy chain (to IRQ8 - 15)
491 timer->set_context_intr_line(pic, SIG_I8259_CHIP0 | SIG_I8259_IR0, 0xffffffff);
492 keyboard->set_context_intr_line(pic, SIG_I8259_CHIP0 | SIG_I8259_IR1, 0xffffffff);
493 floppy->set_context_intr_line(pic, SIG_I8259_CHIP0 | SIG_I8259_IR6, 0xffffffff);
495 // IRQ8 : SCSI (-> scsi.cpp)
497 // IRQ10 : EXTRA I/O (Maybe not implement)
499 // IRQ12 : PRINTER (ToDo)
500 // IRQ13 : ADPCM AND OPN2 (Route to adpcm.cpp)
501 // IRQ14 : EXTRA I/O (Maybe not implement)
503 cdrom->set_context_mpuint_line(pic, SIG_I8259_CHIP1 | SIG_I8259_IR1, 0xffffffff);
504 crtc->set_context_vsync(pic, SIG_I8259_CHIP1 | SIG_I8259_IR3, 0xffffffff);
505 adpcm->set_context_intr_line(pic, SIG_I8259_CHIP1 | SIG_I8259_IR5, 0xffffffff);
508 // DMA1 : SCSI (-> scsi.cpp)
509 // DMA2 : PRINTER (ToDo)
511 // EXTRA DMA0 : EXTRA SLOT (Maybe not implement)
512 // EXTRA DMA1 : Reserved
513 // EXTRA DMA2 : Reserved
514 // EXTRA DMA3 : Reserved
515 fdc->set_context_drq(dma, SIG_UPD71071_CH0, 1);
516 fdc->set_context_drq(keyboard, SIG_KEYBOARD_BOOTSEQ_END, 1);
517 cdrom->set_context_drq_line(dma, SIG_UPD71071_CH3, 0xff);
518 cdrom->set_context_drq_line(keyboard, SIG_KEYBOARD_BOOTSEQ_END, 1);
520 // NMI0 : KEYBOARD (RAS)
521 // NMI1 : Extra SLOT (Maybe not implement)
522 keyboard->set_context_nmi_line(memory, SIG_CPU_NMI, 0xffffffff);
524 cdrom->set_context_dmac(dma);
525 // For Debugging, will remove 20200822 K.O
526 cdrom->set_context_cpu(cpu);
529 io->set_iowait_range_rw(0x0000, 0xffff, 6); // ToDo: May variable wait.
531 io->set_iomap_alias_rw (0x0000, pic, I8259_ADDR_CHIP0 | 0);
532 io->set_iomap_alias_rw (0x0002, pic, I8259_ADDR_CHIP0 | 1);
533 io->set_iomap_alias_rw (0x0010, pic, I8259_ADDR_CHIP1 | 0);
534 io->set_iomap_alias_rw (0x0012, pic, I8259_ADDR_CHIP1 | 1);
536 io->set_iomap_range_rw (0x0020, 0x0025, memory);
537 io->set_iomap_range_rw (0x0026, 0x0027, timer); // Freerun counter
538 io->set_iomap_single_rw(0x0028, memory);
540 io->set_iomap_range_r (0x0030, 0x0031, memory); // cpu id / machine id
541 io->set_iomap_single_rw(0x0032, memory); // serial rom (routed from memory)
542 io->set_iomap_single_r (0x0034, scsi); // ENABLE/ UNABLE to WORD DMA for SCSI
544 io->set_iomap_alias_rw(0x0040, pit0, 0);
545 io->set_iomap_alias_rw(0x0042, pit0, 1);
546 io->set_iomap_alias_rw(0x0044, pit0, 2);
547 io->set_iomap_alias_rw(0x0046, pit0, 3);
548 io->set_iomap_alias_rw(0x0050, pit1, 0);
549 io->set_iomap_alias_rw(0x0052, pit1, 1);
550 io->set_iomap_alias_rw(0x0054, pit1, 2);
551 io->set_iomap_alias_rw(0x0056, pit1, 3);
553 io->set_iomap_single_rw(0x0060, timer); // Beep and interrupts register
554 io->set_iomap_single_rw(0x0068, timer); // Interval timer register2 (after Towns 10F).
555 io->set_iomap_single_rw(0x006a, timer); // Interval timer register2 (after Towns 10F).
556 io->set_iomap_single_rw(0x006b, timer); // Interval timer register2 (after Towns 10F).
557 io->set_iomap_single_rw(0x006c, timer); // 1uS wait register (after Towns 10F).
559 io->set_iomap_single_rw(0x0070, timer); // RTC DATA
560 io->set_iomap_single_w (0x0080, timer); // RTC COMMAND
562 io->set_iomap_range_rw (0x00a0, 0x00af, dma);
563 io->set_iomap_range_rw (0x00b0, 0x00bf, extra_dma);
565 io->set_iomap_single_rw(0x00c0, memory); // CACHE CONTROLLER
566 io->set_iomap_single_rw(0x00c2, memory); // CACHE CONTROLLER
568 io->set_iomap_alias_rw (0x0200, fdc, 0); // STATUS/COMMAND
569 io->set_iomap_alias_rw (0x0202, fdc, 1); // TRACK
570 io->set_iomap_alias_rw (0x0204, fdc, 2); // SECTOR
571 io->set_iomap_alias_rw (0x0206, fdc, 3); // DATA
572 io->set_iomap_single_rw(0x0208, floppy); // DRIVE STATUS / DRIVE CONTROL
573 io->set_iomap_single_rw(0x020c, floppy); // DRIVE SELECT
574 io->set_iomap_single_r (0x020d, floppy); // FDDVEXT (after HG/HR).
575 io->set_iomap_single_rw(0x020e, floppy); // Towns drive SW
577 io->set_iomap_range_rw (0x0400, 0x0404, memory); // System Status
578 // io->set_iomap_range_rw (0x0406, 0x043f, memory); // Reserved
580 io->set_iomap_range_rw(0x0440, 0x0443, crtc); // CRTC
581 io->set_iomap_range_rw(0x0448, 0x044f, crtc); // VIDEO OUT (CRTC)
583 io->set_iomap_range_rw(0x0450, 0x0452, sprite); // SPRITE
585 io->set_iomap_single_rw(0x0458, vram); // VRAM ACCESS CONTROLLER (ADDRESS)
586 io->set_iomap_range_rw (0x045a, 0x045b, vram); // VRAM ACCESS CONTROLLER (DATA)
588 io->set_iomap_single_rw(0x0480, memory); // MEMORY REGISTER
589 io->set_iomap_single_rw(0x0484, dictionary); // Dictionary
591 io->set_iomap_alias_r(0x48a, iccard1, 0); //
592 //io->set_iomap_alias_rw(0x490, memory_card); // After Towns2
593 //io->set_iomap_alias_rw(0x491, memory_card); // After Towns2
595 io->set_iomap_range_rw(0x04c0, 0x04c6, cdrom); // CDROM
596 io->set_iomap_range_r (0x04cc, 0x04cd, cdrom); // CDROM
599 io->set_iomap_single_r(0x04d0, joystick); // Pad1
600 io->set_iomap_single_r(0x04d2, joystick); // Pad 2
601 io->set_iomap_single_w(0x04d6, joystick); // Pad out
603 io->set_iomap_single_rw(0x04d5, adpcm); // mute
605 io->set_iomap_alias_rw(0x04d8, opn2, 0); // STATUS(R)/Addrreg 0(W)
606 io->set_iomap_alias_w (0x04da, opn2, 1); // Datareg 0(W)
607 io->set_iomap_alias_w (0x04dc, opn2, 2); // Addrreg 1(W)
608 io->set_iomap_alias_w (0x04de, opn2, 3); // Datareg 1(W)
610 io->set_iomap_alias_rw(0x04e0, e_volumes[0], 0);
611 io->set_iomap_alias_rw(0x04e1, e_volumes[0], 1);
612 io->set_iomap_alias_rw(0x04e2, e_volumes[1], 0);
613 io->set_iomap_alias_rw(0x04e3, e_volumes[1], 1);
616 io->set_iomap_range_rw(0x04e7, 0x04ec, adpcm); // A/D SAMPLING DATA REG
617 io->set_iomap_range_rw(0x04f0, 0x04f8, rf5c68); // A/D SAMPLING DATA REG
619 io->set_iomap_single_rw(0x05c0, memory); // NMI MASK
620 io->set_iomap_single_r (0x05c2, memory); // NMI STATUS
621 io->set_iomap_single_r (0x05c8, sprite); // TVRAM EMULATION
622 io->set_iomap_single_w (0x05ca, crtc); // VSYNC INTERRUPT
624 io->set_iomap_single_rw(0x05e0, memory); // Hidden MEMORY WAIT REGISTER from AB.COM (Towns 1/2)
625 io->set_iomap_single_rw(0x05e2, memory); // Hidden MEMORY WAIT REGISTER from AB.COM (After Towns 1H/1F/2H/2F )
626 io->set_iomap_single_rw(0x05e6, memory); // Hidden VRAM WAIT REGISTER from TSUGARU (Maybe after Towns 10H/10F/20H/20F )
627 io->set_iomap_single_r (0x05e8, memory); // RAM capacity register.(later Towns1H/2H/1F/2F).
628 io->set_iomap_single_rw(0x05ec, memory); // RAM Wait register , ofcially after Towns2, but exists after Towns1H.
629 io->set_iomap_single_r (0x05ed, memory); // Maximum clock register (After HR/HG).
630 io->set_iomap_single_rw(0x05ee, vram); // VRAM CACHE CONTROLLER
632 io->set_iomap_single_rw(0x0600, keyboard);
633 io->set_iomap_single_rw(0x0602, keyboard);
634 io->set_iomap_single_rw(0x0604, keyboard);
636 //io->set_iomap_single_rw(0x0800, printer);
637 //io->set_iomap_single_rw(0x0802, printer);
638 //io->set_iomap_single_rw(0x0804, printer);
640 io->set_iomap_alias_rw (0x0a00, sio, 0);
641 io->set_iomap_alias_rw (0x0a02, sio, 1);
642 // io->set_iomap_single_r (0x0a04, serial);
643 // io->set_iomap_single_r (0x0a06, serial);
644 // io->set_iomap_single_w (0x0a08, serial);
645 // io->set_iomap_single_rw(0x0a0a, modem);
647 io->set_iomap_single_rw(0x0c30, scsi);
648 io->set_iomap_single_rw(0x0c32, scsi);
649 io->set_iomap_single_r (0x0c34, scsi);
651 io->set_iomap_range_rw (0x3000, 0x3fff, dictionary); // CMOS
653 io->set_iomap_range_rw (0xfd90, 0xfda0, crtc); // Palette and CRTC
654 io->set_iomap_single_r (0xfda2, crtc); // CRTC
655 io->set_iomap_single_rw(0xfda4, memory); // memory
657 io->set_iomap_range_rw (0xff80, 0xff83, planevram); // MMIO
658 io->set_iomap_single_r (0xff84, planevram); // MMIO
659 io->set_iomap_single_rw(0xff86, planevram); // MMIO
660 io->set_iomap_single_rw(0xff88, memory); // MMIO
661 //io->set_iomap_range_rw (0xff94, 0xff97, fontrom); // MMIO
662 //io->set_iomap_range_rw (0xff98, 0xff99, memory); // MMIO
663 //io->set_iomap_range_rw (0xff9c, 0xff9f, memory); // MMIO
665 io->set_iomap_range_rw (0xff94, 0xff99, memory); // MMIO
666 io->set_iomap_range_r (0xff9c, 0xff9d, memory); // MMIO
667 io->set_iomap_single_rw(0xff9e, memory); // MMIO
668 io->set_iomap_single_rw(0xffa0, planevram); // MMIO
672 // Vram allocation may be before initialize().
673 // initialize all devices
674 #if defined(__GIT_REPO_VERSION)
675 set_git_repo_version(__GIT_REPO_VERSION);
677 // ToDo : Use config framework
678 int exram_size = config.current_ram_size;
680 if(machine_id < 0x0200) { // Model1 - 2H
682 } else if(machine_id == 0x0500) { // CX
684 } else if(machine_id < 0x0700) { // 10F,20H
686 } else if(machine_id == 0x0800) { // HG
692 if(exram_size < MIN_RAM_SIZE) {
693 exram_size = MIN_RAM_SIZE;
696 memory->set_extra_ram_size(exram_size);
698 #if defined(WITH_I386SX)
699 cpu->device_model = INTEL_80386;
700 #elif defined(WITH_I486SX)
701 cpu->device_model = INTEL_I486SX;
702 #elif defined(WITH_I486DX)
703 cpu->device_model = INTEL_I486DX;
704 #elif defined(WITH_PENTIUM)
705 cpu->device_model = INTEL_PENTIUM;
708 cpu->device_model = INTEL_80386;
711 initialize_devices();
712 // cpu->set_address_mask(0xffffffff);
717 // delete all devices
721 void VM::set_machine_type(uint16_t machine_id, uint16_t cpu_id)
723 if(memory != nullptr) {
724 memory->set_cpu_id(cpu_id);
725 memory->set_machine_id(machine_id);
727 if(crtc != nullptr) {
728 crtc->set_cpu_id(cpu_id);
729 crtc->set_machine_id(machine_id);
731 if(timer != nullptr) {
732 timer->set_cpu_id(cpu_id);
733 timer->set_machine_id(machine_id);
735 if(cdrom != nullptr) {
736 cdrom->set_cpu_id(cpu_id);
737 cdrom->set_machine_id(machine_id);
739 if(scsi != nullptr) {
740 scsi->set_cpu_id(cpu_id);
741 scsi->set_machine_id(machine_id);
743 if(serialrom != nullptr) {
744 serialrom->set_cpu_id(cpu_id);
745 serialrom->set_machine_id(machine_id);
747 if(floppy != nullptr) {
748 floppy->set_cpu_id(cpu_id);
749 floppy->set_machine_id(machine_id);
751 #if defined(HAS_20PIX_FONTS)
752 if(fontrom_20pix != nullptr) {
753 fontrom_20pix->set_cpu_id(cpu_id);
754 fontrom_20pix->set_machine_id(machine_id);
757 if(vram != nullptr) {
758 vram->set_cpu_id(cpu_id);
759 vram->set_machine_id(machine_id);
765 // ----------------------------------------------------------------------------
766 // drive virtual machine
767 // ----------------------------------------------------------------------------
773 VM_TEMPLATE::reset();
774 // cpu->set_address_mask(0xffffffff);
777 void VM::special_reset(int num)
782 VM_TEMPLATE::reset();
783 // cpu->set_address_mask(0xffffffff);
784 __LIKELY_IF(keyboard != nullptr) {
785 keyboard->special_reset(num);
791 __LIKELY_IF(event != nullptr) {
795 void VM::process_boot_sequence(uint32_t val)
799 if(keyboard != nullptr) {
800 keyboard->write_signal(SIG_KEYBOARD_BOOTSEQ_END, 0xffffffff, 0xffffffff);
807 // ----------------------------------------------------------------------------
809 // ----------------------------------------------------------------------------
812 DEVICE *VM::get_cpu(int index)
821 // ----------------------------------------------------------------------------
823 // ----------------------------------------------------------------------------
825 void VM::draw_screen()
827 __LIKELY_IF(crtc != nullptr) {
833 // ----------------------------------------------------------------------------
835 // ----------------------------------------------------------------------------
837 void VM::initialize_sound(int rate, int samples)
840 // init sound manager
841 event->initialize_sound(rate, samples);
844 beep->initialize_sound(rate, 8000);
848 // MASTER CLOCK MAYBE 600KHz * 12 = 7200KHz .
849 // From FM-Towns Technical Databook (Rev.2), Page 201
850 // opn2->initialize_sound(rate, (int)(600.0e3 * 12.0) , samples, 0.0, 0.0);
852 // But...20230330 K.O:
853 // From src/ym2612/ym2612.h of Tsugaru below quote.
854 // Thanks to YS-11 San.
856 // What exactly is the master clock given to YM2612 of TOWNS?
857 // FM Towns Technical Databook tells internal clock is 600KHz on page 201.
858 // However, after calibrating the tone to match real Towns, it must be 690KHz. Is it correct?
859 // Master clock must be the internal clock times pre scaler. But, we don't know the default pre scalar value.
860 // Let's say it is 3. Then, 690KHz times 3=2070KHz. Sounds reasonable.
861 // But, now FM77AV's YM2203C uses master clock frequency of 1228.8KHz.
862 // And after calibration we know the ratio between the two.
863 // From there, 1228.8*1698/1038=1999.46. 2MHz. Makes more sense.
864 opn2->initialize_sound(rate, (int)(((1228.8e3 * 1698.0) / 1038.0) * 4.0) , samples, 0.0, 0.0);
867 rf5c68->initialize_sound(rate, samples);
869 // add_sound_in_source() must add after per initialize_sound().
870 adc_in_ch = event->add_sound_in_source(rate, samples, 2);
871 adc->set_sample_rate(19200);
872 adc->set_sound_bank(adc_in_ch);
874 mixer->set_context_out_line(adc_in_ch);
875 mixer->set_context_sample_out(adc_in_ch, rate, samples); // Must be 2ch.
876 // ToDo: Check recording sample rate & channels.
877 mic_in_ch = event->add_sound_in_source(rate, samples, 2);
878 mixer->set_context_mic_in(mic_in_ch, rate, samples);
880 line_in_ch = event->add_sound_in_source(rate, samples, 2);
881 mixer->set_context_line_in(line_in_ch, rate, samples);
886 uint16_t* VM::create_sound(int* extra_frames)
888 __LIKELY_IF(event != nullptr) {
889 return event->create_sound(extra_frames);
891 return VM_TEMPLATE::create_sound(extra_frames);
894 int VM::get_sound_buffer_ptr()
896 __LIKELY_IF(event != nullptr) {
897 return event->get_sound_buffer_ptr();
899 return VM_TEMPLATE::get_sound_buffer_ptr();
902 void VM::clear_sound_in()
904 __UNLIKELY_IF(event == nullptr) return;
906 event->clear_sound_in_source(adc_in_ch);
907 event->clear_sound_in_source(mic_in_ch);
908 event->clear_sound_in_source(line_in_ch);
912 int VM::get_sound_in_data(int ch, int32_t* dst, int expect_samples, int expect_rate, int expect_channels)
914 if(dst == nullptr) return 0;
915 if(expect_samples <= 0) return 0;
928 if(n_ch < 0) return 0;
930 if(event != nullptr) {
931 samples = event->get_sound_in_data(n_ch, dst, expect_samples, expect_rate, expect_channels);
936 // Write to event's buffer
937 int VM::sound_in(int ch, int32_t* src, int samples)
940 if(ch >= 2) return 0;
943 case 0x100: // ADC in from MIXER, not connected.
952 if(n_ch < 0) return 0;
955 if(event != nullptr) {
957 ss = event->write_sound_in_buffer(n_ch, src, samples);
963 #if defined(USE_HARD_DISK)
964 void VM::open_hard_disk(int drv, const _TCHAR* file_path)
966 if((drv < USE_HARD_DISK) && (drv < 8) && (drv >= 0)) {
967 if(scsi_hdd[drv] != nullptr) {
968 scsi_hdd[drv]->open(0, file_path, 512);
973 void VM::close_hard_disk(int drv)
975 if((drv < USE_HARD_DISK) && (drv < 8) && (drv >= 0)) {
976 if(scsi_hdd[drv] != nullptr) {
977 scsi_hdd[drv]->close(0);
982 bool VM::is_hard_disk_inserted(int drv)
984 if((drv < USE_HARD_DISK) && (drv < 8) && (drv >= 0)) {
985 if(scsi_hdd[drv] != nullptr) {
986 return scsi_hdd[drv]->mounted(0);
992 uint32_t VM::is_hard_disk_accessed()
996 for(int drv = 0; drv < USE_HARD_DISK; drv++) {
997 if(scsi_hdd[drv] != nullptr) {
998 if(scsi_hdd[drv]->accessed(0)) {
1003 process_boot_sequence(status);
1006 #endif // USE_HARD_DISK
1008 void VM::open_compact_disc(int drv, const _TCHAR* file_path)
1010 if(cdrom != nullptr) {
1011 cdrom->open(file_path);
1015 void VM::close_compact_disc(int drv)
1017 if(cdrom != nullptr) {
1022 bool VM::is_compact_disc_inserted(int drv)
1024 if(cdrom == nullptr) return false;
1025 return cdrom->mounted();
1028 uint32_t VM::is_compact_disc_accessed()
1030 uint32_t status = cdrom->accessed();
1031 process_boot_sequence(status);
1035 #ifdef USE_SOUND_VOLUME
1036 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
1038 #ifndef HAS_LINEIN_SOUND
1039 // if(ch >= 7) ch++;
1041 #ifndef HAS_MIC_SOUND
1042 // if(ch >= 8) ch++;
1044 #ifndef HAS_MODEM_SOUND
1045 // if(ch >= 9) ch++;
1047 #ifndef HAS_2ND_ADPCM
1048 // if(ch >= 10) ch++;
1050 if(ch == 0) { // BEEP
1051 if(beep != nullptr) {
1052 beep->set_volume(0, decibel_l, decibel_r);
1055 else if(ch == 1) { // CD-ROM
1056 if(e_volumes[1] != nullptr) {
1057 e_volumes[1]->set_volumes(0, decibel_l, 1, decibel_r);
1058 } else if(cdrom != nullptr) {
1059 cdrom->set_volume(0, decibel_l, decibel_r);
1062 else if(ch == 2) { // OPN2
1063 if(opn2 != nullptr) {
1064 opn2->set_volume(0, decibel_l, decibel_r);
1067 else if(ch == 3) { // ADPCM
1068 if(rf5c68 != nullptr) {
1069 rf5c68->set_volume(0, decibel_l, decibel_r);
1072 else if(ch == 4) { // SEEK, HEAD UP / DOWN
1073 if(seek_sound != nullptr) {
1074 seek_sound->set_volume(0, decibel_l, decibel_r);
1076 if(head_up_sound != nullptr) {
1077 head_up_sound->set_volume(0, decibel_l, decibel_r);
1079 if(head_down_sound != nullptr) {
1080 head_down_sound->set_volume(0, decibel_l, decibel_r);
1086 // ----------------------------------------------------------------------------
1088 // ----------------------------------------------------------------------------
1090 void VM::key_down(int code, bool repeat)
1092 __LIKELY_IF(keyboard != nullptr) {
1093 keyboard->key_down(code);
1097 void VM::key_up(int code)
1099 __LIKELY_IF(keyboard != nullptr) {
1100 keyboard->key_up(code);
1104 // ----------------------------------------------------------------------------
1106 // ----------------------------------------------------------------------------
1107 void VM::open_cart(int drv, const _TCHAR* file_path)
1111 if(iccard1 != nullptr) {
1112 iccard1->open_cart(file_path);
1116 if(iccard2 != nullptr) {
1117 iccard2->open_cart(file_path);
1123 void VM::close_cart(int drv)
1127 if(iccard1 != nullptr) {
1128 iccard1->close_cart();
1132 if(iccard2 != nullptr) {
1133 iccard2->close_cart();
1139 bool VM::is_cart_inserted(int drv)
1143 if(iccard1 != nullptr) {
1144 return iccard1->is_cart_inserted();
1148 if(iccard2 != nullptr) {
1149 return iccard2->is_cart_inserted();
1156 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
1159 if(fdc != nullptr) {
1160 fdc->open_disk(drv, file_path, bank);
1161 floppy->change_disk(drv);
1165 void VM::close_floppy_disk(int drv)
1167 if(fdc != nullptr) {
1168 fdc->close_disk(drv);
1169 // floppy->change_disk(drv);
1172 uint32_t VM::is_floppy_disk_accessed()
1174 uint32_t val = fdc->read_signal(0);
1175 process_boot_sequence(val);
1179 bool VM::is_floppy_disk_inserted(int drv)
1181 if(fdc != nullptr) {
1182 return fdc->is_disk_inserted(drv);
1184 return VM_TEMPLATE::is_floppy_disk_inserted(drv);
1187 void VM::is_floppy_disk_protected(int drv, bool value)
1189 __LIKELY_IF(fdc != nullptr) {
1190 fdc->is_disk_protected(drv, value);
1194 bool VM::is_floppy_disk_protected(int drv)
1196 __LIKELY_IF(fdc != nullptr) {
1197 return fdc->is_disk_protected(drv);
1199 return VM_TEMPLATE::is_floppy_disk_protected(drv);
1202 bool VM::is_frame_skippable()
1204 __LIKELY_IF(event != nullptr) {
1205 return event->is_frame_skippable();
1207 return VM_TEMPLATE::is_frame_skippable();
1211 double VM::get_current_usec()
1213 __LIKELY_IF(event != nullptr) {
1214 return event->get_current_usec();
1216 return VM_TEMPLATE::get_current_usec();
1219 uint64_t VM::get_current_clock_uint64()
1221 __LIKELY_IF(event != nullptr) {
1222 return event->get_current_clock_uint64();
1224 return VM_TEMPLATE::get_current_clock_uint64();
1227 #define STATE_VERSION 4
1229 bool VM::process_state(FILEIO* state_fio, bool loading)
1231 if(!(VM_TEMPLATE::process_state_core(state_fio, loading, STATE_VERSION))) {
1235 // Machine specified.
1236 state_fio->StateValue(boot_seq);