2 IBM Japan Ltd PC/JX Emulator 'eJX'
4 Author : Takeda.Toshiya
11 #include "../../emu.h"
12 #include "../device.h"
16 #include "../hd46505.h"
21 //#include "../i286.h"
24 #include "../memory.h"
26 #include "../pcm1bit.h"
27 #include "../sn76489an.h"
28 #include "../upd765a.h"
31 #include "../debugger.h"
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
46 first_device = last_device = NULL;
47 dummy = new DEVICE(this, emu); // must be 1st device
48 event = new EVENT(this, emu); // must be 2nd device
49 dummy->set_device_name(_T("1st Dummy"));
51 crtc = new HD46505(this, emu);
52 sio = new I8251(this, emu);
53 pit = new I8253(this, emu);
54 pio = new I8255(this, emu);
55 pic = new I8259(this, emu);
56 cpu = new I286(this, emu); // 8088
57 io = new IO(this, emu);
58 mem = new MEMORY(this, emu);
59 pcm = new PCM1BIT(this, emu);
60 psg = new SN76489AN(this, emu); // SN76496N
61 fdc = new UPD765A(this, emu);
62 fdc->set_context_noise_seek(new NOISE(this, emu));
63 fdc->set_context_noise_head_down(new NOISE(this, emu));
64 fdc->set_context_noise_head_up(new NOISE(this, emu));
66 display = new DISPLAY(this, emu);
67 floppy = new FLOPPY(this, emu);
68 keyboard = new KEYBOARD(this, emu);
69 speaker = new SPEAKER(this, emu);
70 /* IRQ 0 Timer Clock Interrupt
71 1 I/O Channel (Reserved)
73 3 Asynchronous Port Interrupt (RS-232C)
75 5 Vertical Retrace Interrupt (Display)
76 6 Diskette Interrupt (WDT)
77 7 I/O Channel (Parallel Printer)
81 event->set_context_cpu(cpu);
82 event->set_context_sound(pcm);
83 event->set_context_sound(fdc->get_context_noise_seek());
84 event->set_context_sound(fdc->get_context_noise_head_down());
85 event->set_context_sound(fdc->get_context_noise_head_up());
88 cpu->set_context_mem(mem);
89 cpu->set_context_io(io);
90 cpu->set_context_intr(pic);
92 cpu->set_context_debugger(new DEBUGGER(this, emu));
95 crtc->set_context_disp(display, SIG_DISPLAY_ENABLE, 1);
96 crtc->set_context_vblank(display, SIG_DISPLAY_VBLANK, 1);
97 crtc->set_context_vblank(pic, SIG_I8259_IR5, 1);
102 pit->set_constant_clock(0, CPU_CLOCKS / 4);
104 pit->set_constant_clock(1, CPU_CLOCKS / 4);
106 pit->set_constant_clock(2, CPU_CLOCKS / 4);
107 pit->set_context_ch0(pic, SIG_I8259_IR0, 1); // to IRQ0
109 pit->set_context_ch0(keyboard, SIG_KEYBOARD_TIMER, 1); // to clock of timer1
111 pit->set_context_ch2(pio, SIG_I8255_PORT_C, 0x20); // to PC5
112 pit->set_context_ch2(pcm, SIG_PCM1BIT_SIGNAL, 0x20); // to speaker
113 pio->set_context_port_b(pit, SIG_I8253_GATE_2, 0x01, 0); // PB0
114 pio->set_context_port_b(speaker, SIG_SPEAKER_PIO, 0x62, 0); // PB1+5+6
115 pio->set_context_port_b(display, SIG_DISPLAY_PIO, 0x04, 0); // PB2
116 pic->set_context_cpu(cpu);
118 display->set_context_mem(mem);
119 display->set_regs_ptr(crtc->get_regs());
120 floppy->set_context_fdc(fdc);
121 floppy->set_context_pic(pic);
122 keyboard->set_context_cpu(cpu);
123 keyboard->set_context_pio(pio);
124 keyboard->set_context_pit(pit);
125 speaker->set_context_pcm(pcm);
126 speaker->set_context_psg(psg);
130 // 80000-B7FFF KANJI ROM ???
131 // A0000-A7FFF EXT-VRAM
133 // D0000-FFFFF CART+IPL
135 memset(font, 0xff, sizeof(font));
136 memset(kanji, 0xff, sizeof(kanji));
137 memset(ram, 0, sizeof(ram));
138 memset(ipl, 0xff, sizeof(ipl));
140 mem->read_bios(_T("FONT.ROM"), font, sizeof(font));
141 mem->read_bios(_T("KANJI.ROM"), kanji, sizeof(kanji));
142 int length = mem->read_bios(_T("IPL.ROM"), ipl, sizeof(ipl));
143 int offset = 0x30000 - length;
144 memmove(ipl + offset, ipl, length);
145 memset(ipl, 0xff, offset);
147 mem->set_memory_rw(0x00000, 0x7ffff, ram);
148 // mem->set_memory_r(0x80000, 0xb7fff, kanji);
149 mem->set_memory_r(0xd0000, 0xfffff, ipl);
151 display->set_font_ptr(font);
152 display->set_kanji_ptr(kanji);
155 for(int i = 0x20; i <= 0x27; i++) {
156 io->set_iomap_alias_rw(i, pic, i & 1);
158 io->set_iomap_range_rw(0x40, 0x43, pit);
159 io->set_iomap_range_rw(0x60, 0x67, pio);
160 io->set_iomap_range_rw(0xa0, 0xa7, keyboard);
161 io->set_iomap_range_w(0xc0, 0xc7, psg);
163 io->set_iomap_single_w(0xf2, floppy);
164 io->set_iomap_range_rw(0xf4, 0xf5, fdc);
166 io->set_iomap_single_rw(0x1ff, display);
168 /// io->set_iovalue_range_r(0x200, 0x207, 0);
170 io->set_iomap_range_rw(0x3d0, 0x3d1, crtc);
171 io->set_iomap_range_rw(0x3d4, 0x3d5, crtc);
172 io->set_iomap_range_rw(0x3d8, 0x3df, display);
174 // initialize all devices
175 #if defined(__GIT_REPO_VERSION)
176 strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
178 for(DEVICE* device = first_device; device; device = device->next_device) {
179 device->initialize();
181 for(int i = 0; i < 4; i++) {
182 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
188 // delete all devices
189 for(DEVICE* device = first_device; device;) {
190 DEVICE *next_device = device->next_device;
193 device = next_device;
197 DEVICE* VM::get_device(int id)
199 for(DEVICE* device = first_device; device; device = device->next_device) {
200 if(device->this_device_id == id) {
207 // ----------------------------------------------------------------------------
208 // drive virtual machine
209 // ----------------------------------------------------------------------------
214 for(DEVICE* device = first_device; device; device = device->next_device) {
218 // initial device settings
219 pio->write_signal(SIG_I8255_PORT_C, 0x02, 0x02); // PC1=1: Modem card is not installed
220 pio->write_signal(SIG_I8255_PORT_C, 0x00, 0x04); // PC2=0: Diskette card is installed
221 // pio->write_signal(SIG_I8255_PORT_C, 0x04, 0x04); // PC2=0: Diskette card is no installed
222 pio->write_signal(SIG_I8255_PORT_C, 0x00, 0x08); // PC3=0: 64KB Memory and Display Expansion is installed
223 pio->write_signal(SIG_I8255_PORT_C, 0x00, 0x80); // PC7=0: Keyboard cable is connected
231 // ----------------------------------------------------------------------------
233 // ----------------------------------------------------------------------------
236 DEVICE *VM::get_cpu(int index)
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
249 void VM::draw_screen()
251 display->draw_screen();
254 // ----------------------------------------------------------------------------
256 // ----------------------------------------------------------------------------
258 void VM::initialize_sound(int rate, int samples)
260 // init sound manager
261 event->initialize_sound(rate, samples);
264 pcm->initialize_sound(rate, 8000);
265 psg->initialize_sound(rate, 3579545, 8000);
268 uint16_t* VM::create_sound(int* extra_frames)
270 return event->create_sound(extra_frames);
273 int VM::get_sound_buffer_ptr()
275 return event->get_sound_buffer_ptr();
278 #ifdef USE_SOUND_VOLUME
279 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
282 pcm->set_volume(0, decibel_l, decibel_r);
284 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
285 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
286 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
291 // ----------------------------------------------------------------------------
293 // ----------------------------------------------------------------------------
295 void VM::key_down(int code, bool repeat)
297 keyboard->key_down(code);
300 void VM::key_up(int code)
302 keyboard->key_up(code);
305 // ----------------------------------------------------------------------------
307 // ----------------------------------------------------------------------------
309 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
311 fdc->open_disk(drv, file_path, bank);
314 void VM::close_floppy_disk(int drv)
316 fdc->close_disk(drv);
319 bool VM::is_floppy_disk_inserted(int drv)
321 return fdc->is_disk_inserted(drv);
324 void VM::is_floppy_disk_protected(int drv, bool value)
326 fdc->is_disk_protected(drv, value);
329 bool VM::is_floppy_disk_protected(int drv)
331 return fdc->is_disk_protected(drv);
334 uint32_t VM::is_floppy_disk_accessed()
336 return fdc->read_signal(0);
339 bool VM::is_frame_skippable()
341 return event->is_frame_skippable();
344 void VM::update_config()
346 for(DEVICE* device = first_device; device; device = device->next_device) {
347 device->update_config();
351 #define STATE_VERSION 1
353 bool VM::process_state(FILEIO* state_fio, bool loading)
355 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
358 for(DEVICE* device = first_device; device; device = device->next_device) {
359 // Note: typeid(foo).name is fixed by recent ABI.Not dec 6.
360 // const char *name = typeid(*device).name();
361 // But, using get_device_name() instead of typeid(foo).name() 20181008 K.O
362 const char *name = device->get_device_name();
363 int len = strlen(name);
365 if(!state_fio->StateCheckInt32(len)) {
367 printf("Class name len Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
371 if(!state_fio->StateCheckBuffer(name, len, 1)) {
373 printf("Class name Error: DEVID=%d EXPECT=%s\n", device->this_device_id, name);
377 if(!device->process_state(state_fio, loading)) {
379 printf("Data loading Error: DEVID=%d\n", device->this_device_id);
384 state_fio->StateBuffer(ram, sizeof(ram), 1);