OSDN Git Service

[General] Merge Updtream 2017-03-15 . Still not build all of VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / jx / jx.cpp
1 /*
2         IBM Japan Ltd PC/JX Emulator 'eJX'
3
4         Author : Takeda.Toshiya
5         Date   : 2011.05.09-
6
7         [ virtual machine ]
8 */
9
10 #include "jx.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../disk.h"
16 #include "../hd46505.h"
17 #include "../i8251.h"
18 #include "../i8253.h"
19 #include "../i8255.h"
20 #include "../i8259.h"
21 #include "../i86.h"
22 #include "../io.h"
23 #include "../memory.h"
24 #include "../noise.h"
25 #include "../pcm1bit.h"
26 #include "../sn76489an.h"
27 #include "../upd765a.h"
28
29 #ifdef USE_DEBUGGER
30 #include "../debugger.h"
31 #endif
32
33 #include "display.h"
34 #include "floppy.h"
35 #include "keyboard.h"
36 #include "speaker.h"
37
38 // ----------------------------------------------------------------------------
39 // initialize
40 // ----------------------------------------------------------------------------
41
42 VM::VM(EMU* parent_emu) : emu(parent_emu)
43 {
44         // create devices
45         first_device = last_device = NULL;
46         dummy = new DEVICE(this, emu);  // must be 1st device
47         event = new EVENT(this, emu);   // must be 2nd device
48         dummy->set_device_name(_T("1st Dummy"));
49         
50         crtc = new HD46505(this, emu);
51         sio = new I8251(this, emu);
52         pit = new I8253(this, emu);
53         pio = new I8255(this, emu);
54         pic = new I8259(this, emu);
55         cpu = new I86(this, emu);       // 8088
56         io = new IO(this, emu);
57         mem = new MEMORY(this, emu);
58         pcm = new PCM1BIT(this, emu);
59         psg = new SN76489AN(this, emu); // SN76496N
60         fdc = new UPD765A(this, emu);
61         fdc->set_context_noise_seek(new NOISE(this, emu));
62         fdc->set_context_noise_head_down(new NOISE(this, emu));
63         fdc->set_context_noise_head_up(new NOISE(this, emu));
64         
65         display = new DISPLAY(this, emu);
66         floppy = new FLOPPY(this, emu);
67         keyboard = new KEYBOARD(this, emu);
68         speaker = new SPEAKER(this, emu);
69         /* IRQ  0 Timer Clock Interrupt
70                 1 I/O Channel (Reserved)
71                 2 I/O Channel
72                 3 Asynchronous Port Interrupt (RS-232C)
73                 4 Modem Interrupt
74                 5 Vertical Retrace Interrupt (Display)
75                 6 Diskette Interrupt (WDT)
76                 7 I/O Channel (Parallel Printer)
77         */
78         
79         // set contexts
80         event->set_context_cpu(cpu);
81         event->set_context_sound(pcm);
82         event->set_context_sound(fdc->get_context_noise_seek());
83         event->set_context_sound(fdc->get_context_noise_head_down());
84         event->set_context_sound(fdc->get_context_noise_head_up());
85
86         // cpu bus
87         cpu->set_context_mem(mem);
88         cpu->set_context_io(io);
89         cpu->set_context_intr(pic);
90 #ifdef USE_DEBUGGER
91         cpu->set_context_debugger(new DEBUGGER(this, emu));
92 #endif
93         
94         crtc->set_context_disp(display, SIG_DISPLAY_ENABLE, 1);
95         crtc->set_context_vblank(display, SIG_DISPLAY_VBLANK, 1);
96         crtc->set_context_vblank(pic, SIG_I8259_IR5, 1);
97         
98         /* PIT  0 Interrupt
99                 2 Speaker
100         */
101         pit->set_constant_clock(0, CPU_CLOCKS / 4);
102 #ifdef TIMER_HACK
103         pit->set_constant_clock(1, CPU_CLOCKS / 4);
104 #endif
105         pit->set_constant_clock(2, CPU_CLOCKS / 4);
106         pit->set_context_ch0(pic, SIG_I8259_IR0, 1);                    // to IRQ0
107 #ifndef TIMER_HACK
108         pit->set_context_ch0(keyboard, SIG_KEYBOARD_TIMER, 1);          // to clock of timer1
109 #endif
110         pit->set_context_ch2(pio, SIG_I8255_PORT_C, 0x20);              // to PC5
111         pit->set_context_ch2(pcm, SIG_PCM1BIT_SIGNAL, 0x20);            // to speaker
112         pio->set_context_port_b(pit, SIG_I8253_GATE_2, 0x01, 0);        // PB0
113         pio->set_context_port_b(speaker, SIG_SPEAKER_PIO, 0x62, 0);     // PB1+5+6
114         pio->set_context_port_b(display, SIG_DISPLAY_PIO, 0x04, 0);     // PB2
115         pic->set_context_cpu(cpu);
116         
117         display->set_context_mem(mem);
118         display->set_regs_ptr(crtc->get_regs());
119         floppy->set_context_fdc(fdc);
120         floppy->set_context_pic(pic);
121         keyboard->set_context_cpu(cpu);
122         keyboard->set_context_pio(pio);
123         keyboard->set_context_pit(pit);
124         speaker->set_context_pcm(pcm);
125         speaker->set_context_psg(psg);
126         
127         // memory bus
128         //      00000-7FFFF     RAM
129         //      80000-B7FFF     KANJI ROM ???
130         //      A0000-A7FFF     EXT-VRAM
131         //      B8000-BFFFF     VRAM
132         //      D0000-FFFFF     CART+IPL
133         
134         memset(font, 0xff, sizeof(font));
135         memset(kanji, 0xff, sizeof(kanji));
136         memset(ram, 0, sizeof(ram));
137         memset(ipl, 0xff, sizeof(ipl));
138         
139         mem->read_bios(_T("FONT.ROM"), font, sizeof(font));
140         mem->read_bios(_T("KANJI.ROM"), kanji, sizeof(kanji));
141         int length = mem->read_bios(_T("IPL.ROM"), ipl, sizeof(ipl));
142         int offset = 0x30000 - length;
143         memmove(ipl + offset, ipl, length);
144         memset(ipl, 0xff, offset);
145         
146         mem->set_memory_rw(0x00000, 0x7ffff, ram);
147 //      mem->set_memory_r(0x80000, 0xb7fff, kanji);
148         mem->set_memory_r(0xd0000, 0xfffff, ipl);
149         
150         display->set_font_ptr(font);
151         display->set_kanji_ptr(kanji);
152         
153         // i/o bus
154         for(int i = 0x20; i <= 0x27; i++) {
155                 io->set_iomap_alias_rw(i, pic, i & 1);
156         }
157         io->set_iomap_range_rw(0x40, 0x43, pit);
158         io->set_iomap_range_rw(0x60, 0x67, pio);
159         io->set_iomap_range_rw(0xa0, 0xa7, keyboard);
160         io->set_iomap_range_w(0xc0, 0xc7, psg);
161         
162         io->set_iomap_single_w(0xf2, floppy);
163         io->set_iomap_range_rw(0xf4, 0xf5, fdc);
164         
165         io->set_iomap_single_rw(0x1ff, display);
166         
167 ///     io->set_iovalue_range_r(0x200, 0x207, 0);
168         
169         io->set_iomap_range_rw(0x3d0, 0x3d1, crtc);
170         io->set_iomap_range_rw(0x3d4, 0x3d5, crtc);
171         io->set_iomap_range_rw(0x3d8, 0x3df, display);
172         
173         // initialize all devices
174         for(DEVICE* device = first_device; device; device = device->next_device) {
175                 device->initialize();
176         }
177         for(int i = 0; i < 4; i++) {
178                 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
179         }
180 }
181
182 VM::~VM()
183 {
184         // delete all devices
185         for(DEVICE* device = first_device; device;) {
186                 DEVICE *next_device = device->next_device;
187                 device->release();
188                 delete device;
189                 device = next_device;
190         }
191 }
192
193 DEVICE* VM::get_device(int id)
194 {
195         for(DEVICE* device = first_device; device; device = device->next_device) {
196                 if(device->this_device_id == id) {
197                         return device;
198                 }
199         }
200         return NULL;
201 }
202
203 // ----------------------------------------------------------------------------
204 // drive virtual machine
205 // ----------------------------------------------------------------------------
206
207 void VM::reset()
208 {
209         // reset all devices
210         for(DEVICE* device = first_device; device; device = device->next_device) {
211                 device->reset();
212         }
213         
214         // initial device settings
215         pio->write_signal(SIG_I8255_PORT_C, 0x02, 0x02);        // PC1=1: Modem card is not installed
216         pio->write_signal(SIG_I8255_PORT_C, 0x00, 0x04);        // PC2=0: Diskette card is installed
217 //      pio->write_signal(SIG_I8255_PORT_C, 0x04, 0x04);        // PC2=0: Diskette card is no installed
218         pio->write_signal(SIG_I8255_PORT_C, 0x00, 0x08);        // PC3=0: 64KB Memory and Display Expansion is installed
219         pio->write_signal(SIG_I8255_PORT_C, 0x00, 0x80);        // PC7=0: Keyboard cable is connected
220 }
221
222 void VM::run()
223 {
224         event->drive();
225 }
226
227 // ----------------------------------------------------------------------------
228 // debugger
229 // ----------------------------------------------------------------------------
230
231 #ifdef USE_DEBUGGER
232 DEVICE *VM::get_cpu(int index)
233 {
234         if(index == 0) {
235                 return cpu;
236         }
237         return NULL;
238 }
239 #endif
240
241 // ----------------------------------------------------------------------------
242 // draw screen
243 // ----------------------------------------------------------------------------
244
245 void VM::draw_screen()
246 {
247         display->draw_screen();
248 }
249
250 // ----------------------------------------------------------------------------
251 // soud manager
252 // ----------------------------------------------------------------------------
253
254 void VM::initialize_sound(int rate, int samples)
255 {
256         // init sound manager
257         event->initialize_sound(rate, samples);
258         
259         // init sound gen
260         pcm->initialize_sound(rate, 8000);
261         psg->initialize_sound(rate, 3579545, 8000);
262 }
263
264 uint16_t* VM::create_sound(int* extra_frames)
265 {
266         return event->create_sound(extra_frames);
267 }
268
269 int VM::get_sound_buffer_ptr()
270 {
271         return event->get_sound_buffer_ptr();
272 }
273
274 #ifdef USE_SOUND_VOLUME
275 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
276 {
277         if(ch == 0) {
278                 pcm->set_volume(0, decibel_l, decibel_r);
279         } else if(ch == 1) {
280                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
281                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
282                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
283         }
284 }
285 #endif
286
287 // ----------------------------------------------------------------------------
288 // notify key
289 // ----------------------------------------------------------------------------
290
291 void VM::key_down(int code, bool repeat)
292 {
293         keyboard->key_down(code);
294 }
295
296 void VM::key_up(int code)
297 {
298         keyboard->key_up(code);
299 }
300
301 // ----------------------------------------------------------------------------
302 // user interface
303 // ----------------------------------------------------------------------------
304
305 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
306 {
307         fdc->open_disk(drv, file_path, bank);
308 }
309
310 void VM::close_floppy_disk(int drv)
311 {
312         fdc->close_disk(drv);
313 }
314
315 bool VM::is_floppy_disk_inserted(int drv)
316 {
317         return fdc->is_disk_inserted(drv);
318 }
319
320 void VM::is_floppy_disk_protected(int drv, bool value)
321 {
322         fdc->is_disk_protected(drv, value);
323 }
324
325 bool VM::is_floppy_disk_protected(int drv)
326 {
327         return fdc->is_disk_protected(drv);
328 }
329
330 uint32_t VM::is_floppy_disk_accessed()
331 {
332         return fdc->read_signal(0);
333 }
334
335 bool VM::is_frame_skippable()
336 {
337         return event->is_frame_skippable();
338 }
339
340 void VM::update_config()
341 {
342         for(DEVICE* device = first_device; device; device = device->next_device) {
343                 device->update_config();
344         }
345 }
346