OSDN Git Service

[BUILD] Set SOVERSION and GIT hash automatically.
[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 "../i286.h"
22 #include "./i286.h"
23 #include "../io.h"
24 #include "../memory.h"
25 #include "../noise.h"
26 #include "../pcm1bit.h"
27 #include "../sn76489an.h"
28 #include "../upd765a.h"
29
30 #ifdef USE_DEBUGGER
31 #include "../debugger.h"
32 #endif
33
34 #include "display.h"
35 #include "floppy.h"
36 #include "keyboard.h"
37 #include "speaker.h"
38
39 // ----------------------------------------------------------------------------
40 // initialize
41 // ----------------------------------------------------------------------------
42
43 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
44 {
45         // create devices
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"));
50         
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));
65         
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)
72                 2 I/O Channel
73                 3 Asynchronous Port Interrupt (RS-232C)
74                 4 Modem Interrupt
75                 5 Vertical Retrace Interrupt (Display)
76                 6 Diskette Interrupt (WDT)
77                 7 I/O Channel (Parallel Printer)
78         */
79         
80         // set contexts
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());
86
87         // cpu bus
88         cpu->set_context_mem(mem);
89         cpu->set_context_io(io);
90         cpu->set_context_intr(pic);
91 #ifdef USE_DEBUGGER
92         cpu->set_context_debugger(new DEBUGGER(this, emu));
93 #endif
94         
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);
98         
99         /* PIT  0 Interrupt
100                 2 Speaker
101         */
102         pit->set_constant_clock(0, CPU_CLOCKS / 4);
103 #ifdef TIMER_HACK
104         pit->set_constant_clock(1, CPU_CLOCKS / 4);
105 #endif
106         pit->set_constant_clock(2, CPU_CLOCKS / 4);
107         pit->set_context_ch0(pic, SIG_I8259_IR0, 1);                    // to IRQ0
108 #ifndef TIMER_HACK
109         pit->set_context_ch0(keyboard, SIG_KEYBOARD_TIMER, 1);          // to clock of timer1
110 #endif
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);
117         
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);
127         
128         // memory bus
129         //      00000-7FFFF     RAM
130         //      80000-B7FFF     KANJI ROM ???
131         //      A0000-A7FFF     EXT-VRAM
132         //      B8000-BFFFF     VRAM
133         //      D0000-FFFFF     CART+IPL
134         
135         memset(font, 0xff, sizeof(font));
136         memset(kanji, 0xff, sizeof(kanji));
137         memset(ram, 0, sizeof(ram));
138         memset(ipl, 0xff, sizeof(ipl));
139         
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);
146         
147         mem->set_memory_rw(0x00000, 0x7ffff, ram);
148 //      mem->set_memory_r(0x80000, 0xb7fff, kanji);
149         mem->set_memory_r(0xd0000, 0xfffff, ipl);
150         
151         display->set_font_ptr(font);
152         display->set_kanji_ptr(kanji);
153         
154         // i/o bus
155         for(int i = 0x20; i <= 0x27; i++) {
156                 io->set_iomap_alias_rw(i, pic, i & 1);
157         }
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);
162         
163         io->set_iomap_single_w(0xf2, floppy);
164         io->set_iomap_range_rw(0xf4, 0xf5, fdc);
165         
166         io->set_iomap_single_rw(0x1ff, display);
167         
168 ///     io->set_iovalue_range_r(0x200, 0x207, 0);
169         
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);
173         
174         // initialize all devices
175 #if defined(__GIT_REPO_VERSION)
176         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
177 #endif
178         for(DEVICE* device = first_device; device; device = device->next_device) {
179                 device->initialize();
180         }
181         for(int i = 0; i < 4; i++) {
182                 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
183         }
184 }
185
186 VM::~VM()
187 {
188         // delete all devices
189         for(DEVICE* device = first_device; device;) {
190                 DEVICE *next_device = device->next_device;
191                 device->release();
192                 delete device;
193                 device = next_device;
194         }
195 }
196
197 DEVICE* VM::get_device(int id)
198 {
199         for(DEVICE* device = first_device; device; device = device->next_device) {
200                 if(device->this_device_id == id) {
201                         return device;
202                 }
203         }
204         return NULL;
205 }
206
207 // ----------------------------------------------------------------------------
208 // drive virtual machine
209 // ----------------------------------------------------------------------------
210
211 void VM::reset()
212 {
213         // reset all devices
214         for(DEVICE* device = first_device; device; device = device->next_device) {
215                 device->reset();
216         }
217         
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
224 }
225
226 void VM::run()
227 {
228         event->drive();
229 }
230
231 // ----------------------------------------------------------------------------
232 // debugger
233 // ----------------------------------------------------------------------------
234
235 #ifdef USE_DEBUGGER
236 DEVICE *VM::get_cpu(int index)
237 {
238         if(index == 0) {
239                 return cpu;
240         }
241         return NULL;
242 }
243 #endif
244
245 // ----------------------------------------------------------------------------
246 // draw screen
247 // ----------------------------------------------------------------------------
248
249 void VM::draw_screen()
250 {
251         display->draw_screen();
252 }
253
254 // ----------------------------------------------------------------------------
255 // soud manager
256 // ----------------------------------------------------------------------------
257
258 void VM::initialize_sound(int rate, int samples)
259 {
260         // init sound manager
261         event->initialize_sound(rate, samples);
262         
263         // init sound gen
264         pcm->initialize_sound(rate, 8000);
265         psg->initialize_sound(rate, 3579545, 8000);
266 }
267
268 uint16_t* VM::create_sound(int* extra_frames)
269 {
270         return event->create_sound(extra_frames);
271 }
272
273 int VM::get_sound_buffer_ptr()
274 {
275         return event->get_sound_buffer_ptr();
276 }
277
278 #ifdef USE_SOUND_VOLUME
279 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
280 {
281         if(ch == 0) {
282                 pcm->set_volume(0, decibel_l, decibel_r);
283         } else if(ch == 1) {
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);
287         }
288 }
289 #endif
290
291 // ----------------------------------------------------------------------------
292 // notify key
293 // ----------------------------------------------------------------------------
294
295 void VM::key_down(int code, bool repeat)
296 {
297         keyboard->key_down(code);
298 }
299
300 void VM::key_up(int code)
301 {
302         keyboard->key_up(code);
303 }
304
305 // ----------------------------------------------------------------------------
306 // user interface
307 // ----------------------------------------------------------------------------
308
309 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
310 {
311         fdc->open_disk(drv, file_path, bank);
312 }
313
314 void VM::close_floppy_disk(int drv)
315 {
316         fdc->close_disk(drv);
317 }
318
319 bool VM::is_floppy_disk_inserted(int drv)
320 {
321         return fdc->is_disk_inserted(drv);
322 }
323
324 void VM::is_floppy_disk_protected(int drv, bool value)
325 {
326         fdc->is_disk_protected(drv, value);
327 }
328
329 bool VM::is_floppy_disk_protected(int drv)
330 {
331         return fdc->is_disk_protected(drv);
332 }
333
334 uint32_t VM::is_floppy_disk_accessed()
335 {
336         return fdc->read_signal(0);
337 }
338
339 bool VM::is_frame_skippable()
340 {
341         return event->is_frame_skippable();
342 }
343
344 void VM::update_config()
345 {
346         for(DEVICE* device = first_device; device; device = device->next_device) {
347                 device->update_config();
348         }
349 }
350