OSDN Git Service

ade2c387d23c9afb01f7f0a81a576e86f4e9002e
[csp-qt/common_source_project-fm7.git] / source / src / vm / pc100 / pc100.cpp
1 /*
2         NEC PC-100 Emulator 'ePC-100'
3
4         Author : Takeda.Toshiya
5         Date   : 2008.07.12 -
6
7         [ virtual machine ]
8 */
9
10 #include "pc100.h"
11 #include "../../emu.h"
12 #include "../device.h"
13 #include "../event.h"
14
15 #include "../and.h"
16 #include "../beep.h"
17 #include "../disk.h"
18 #include "../i8251.h"
19 #include "../i8255.h"
20 #include "../i8259.h"
21 #include "../i286.h"
22 #include "../io.h"
23 #include "../memory.h"
24 #include "../msm58321.h"
25 #include "../noise.h"
26 #include "../pcm1bit.h"
27 #include "../upd765a.h"
28
29 #ifdef USE_DEBUGGER
30 #include "../debugger.h"
31 #endif
32
33 #include "crtc.h"
34 #include "ioctrl.h"
35 #include "kanji.h"
36
37 // ----------------------------------------------------------------------------
38 // initialize
39 // ----------------------------------------------------------------------------
40
41 VM::VM(EMU* parent_emu) : VM_TEMPLATE(parent_emu)
42 {
43         // create devices
44         first_device = last_device = NULL;
45         dummy = new DEVICE(this, emu);  // must be 1st device
46         event = new EVENT(this, emu);   // must be 2nd device
47         
48         and_drq = new AND(this, emu);
49         beep = new BEEP(this, emu);
50         sio = new I8251(this, emu);
51         pio0 = new I8255(this, emu);
52         pio0->set_device_name(_T("8255 PIO (RTC)"));
53         pio1 = new I8255(this, emu);
54         pio1->set_device_name(_T("8255 PIO (CRTC)"));
55         pic = new I8259(this, emu);
56         cpu = new I286(this, emu);
57         io = new IO(this, emu);
58         memory = new MEMORY(this, emu);
59         rtc = new MSM58321(this, emu);
60         pcm = new PCM1BIT(this, emu);
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         crtc = new CRTC(this, emu);
67         ioctrl = new IOCTRL(this, emu);
68         kanji = new KANJI(this, emu);
69         
70         // set contexts
71         event->set_context_cpu(cpu);
72         event->set_context_sound(beep);
73         event->set_context_sound(pcm);
74         event->set_context_sound(fdc->get_context_noise_seek());
75         event->set_context_sound(fdc->get_context_noise_head_down());
76         event->set_context_sound(fdc->get_context_noise_head_up());
77         
78         and_drq->set_context_out(cpu, SIG_CPU_NMI, 1);
79         and_drq->set_mask(SIG_AND_BIT_0 | SIG_AND_BIT_1);
80         sio->set_context_rxrdy(pic, SIG_I8259_IR1, 1);
81         pio0->set_context_port_a(rtc, SIG_MSM58321_READ, 1, 0);
82         pio0->set_context_port_a(rtc, SIG_MSM58321_WRITE, 2, 0);
83         pio0->set_context_port_a(rtc, SIG_MSM58321_ADDR_WRITE, 4, 0);
84         pio0->set_context_port_c(rtc, SIG_MSM58321_DATA, 0x0f, 0);
85         pio1->set_context_port_a(crtc, SIG_CRTC_BITMASK_LOW, 0xff, 0);
86         pio1->set_context_port_b(crtc, SIG_CRTC_BITMASK_HIGH, 0xff, 0);
87         pio1->set_context_port_c(crtc, SIG_CRTC_VRAM_PLANE, 0x3f, 0);
88         pio1->set_context_port_c(and_drq, SIG_AND_BIT_0, 0x80, 0);
89         pio1->set_context_port_c(ioctrl, SIG_IOCTRL_RESET, 0x40, 0);
90         pic->set_context_cpu(cpu);
91         rtc->set_context_data(pio0, SIG_I8255_PORT_C, 0x0f, 0);
92         rtc->set_context_busy(pio0, SIG_I8255_PORT_C, 0x10);
93         fdc->set_context_irq(cpu, SIG_CPU_NMI, 1);
94         fdc->set_context_drq(and_drq, SIG_AND_BIT_1, 1);
95         
96         crtc->set_context_pic(pic);
97         ioctrl->set_context_pic(pic);
98         ioctrl->set_context_fdc(fdc);
99         ioctrl->set_context_beep(beep);
100         ioctrl->set_context_pcm(pcm);
101         
102         // cpu bus
103         cpu->set_context_mem(memory);
104         cpu->set_context_io(io);
105         cpu->set_context_intr(pic);
106 #ifdef USE_DEBUGGER
107         cpu->set_context_debugger(new DEBUGGER(this, emu));
108 #endif
109         
110         // memory bus
111         memset(ram, 0, sizeof(ram));
112         memset(ipl, 0xff, sizeof(ipl));
113         
114         memory->read_bios(_T("IPL.ROM"), ipl, sizeof(ipl));
115         
116         memory->set_memory_rw(0x00000, 0xbffff, ram);
117         memory->set_memory_mapped_io_rw(0xc0000, 0xdffff, crtc);
118         memory->set_memory_r(0xf8000, 0xfffff, ipl);
119         
120         // i/o bus
121         io->set_iomap_alias_rw(0x00, pic, 0);
122         io->set_iomap_alias_rw(0x02, pic, 1);
123 //      io->set_iomap_alias_rw(0x04, dma, 0);
124 //      io->set_iomap_alias_rw(0x06, dma, 1);
125         io->set_iomap_alias_r(0x08, fdc, 0);
126         io->set_iomap_alias_rw(0x0a, fdc, 1);
127         io->set_iomap_alias_rw(0x10, pio0, 0);
128         io->set_iomap_alias_rw(0x12, pio0, 1);
129         io->set_iomap_alias_rw(0x14, pio0, 2);
130         io->set_iomap_alias_rw(0x16, pio0, 3);
131         io->set_iomap_alias_rw(0x18, pio1, 0);
132         io->set_iomap_alias_rw(0x1a, pio1, 1);
133         io->set_iomap_alias_rw(0x1c, pio1, 2);
134         io->set_iomap_alias_rw(0x1e, pio1, 3);
135         io->set_iomap_single_r(0x20, ioctrl);
136         io->set_iomap_single_rw(0x22, ioctrl);
137         io->set_iomap_single_w(0x24, ioctrl);
138         io->set_iomap_alias_rw(0x28, sio, 0);
139         io->set_iomap_alias_rw(0x2a, sio, 1);
140         io->set_iomap_single_rw(0x30, crtc);
141         io->set_iomap_single_rw(0x38, crtc);
142         io->set_iomap_single_rw(0x3a, crtc);
143         io->set_iomap_single_rw(0x3c, crtc);
144         io->set_iomap_single_rw(0x3e, crtc);
145         for(int i = 0x40; i < 0x62; i++) {
146                 io->set_iomap_single_rw(i, crtc);
147         }
148         io->set_iomap_single_rw(0x80, kanji);
149         io->set_iomap_single_rw(0x81, kanji);
150         io->set_iomap_single_w(0x84, kanji);
151         io->set_iomap_single_w(0x86, kanji);
152         
153         // initialize all devices
154 #if defined(__GIT_REPO_VERSION)
155         strncpy(_git_revision, __GIT_REPO_VERSION, sizeof(_git_revision) - 1);
156 #endif
157         for(DEVICE* device = first_device; device; device = device->next_device) {
158                 device->initialize();
159         }
160         decl_state();
161 }
162
163 VM::~VM()
164 {
165         // delete all devices
166         for(DEVICE* device = first_device; device;) {
167                 DEVICE *next_device = device->next_device;
168                 device->release();
169                 delete device;
170                 device = next_device;
171         }
172 }
173
174 DEVICE* VM::get_device(int id)
175 {
176         for(DEVICE* device = first_device; device; device = device->next_device) {
177                 if(device->this_device_id == id) {
178                         return device;
179                 }
180         }
181         return NULL;
182 }
183
184 // ----------------------------------------------------------------------------
185 // drive virtual machine
186 // ----------------------------------------------------------------------------
187
188 void VM::reset()
189 {
190         // reset all devices
191         for(DEVICE* device = first_device; device; device = device->next_device) {
192                 device->reset();
193         }
194         for(int i = 0; i < 4; i++) {
195                 if(config.drive_type) {
196                         fdc->set_drive_type(i, DRIVE_TYPE_2DD);
197                 } else {
198                         fdc->set_drive_type(i, DRIVE_TYPE_2D);
199                 }
200         }
201 }
202
203 void VM::run()
204 {
205         event->drive();
206 }
207
208 // ----------------------------------------------------------------------------
209 // debugger
210 // ----------------------------------------------------------------------------
211
212 #ifdef USE_DEBUGGER
213 DEVICE *VM::get_cpu(int index)
214 {
215         if(index == 0) {
216                 return cpu;
217         }
218         return NULL;
219 }
220 #endif
221
222 // ----------------------------------------------------------------------------
223 // draw screen
224 // ----------------------------------------------------------------------------
225
226 void VM::draw_screen()
227 {
228         crtc->draw_screen();
229 }
230
231 // ----------------------------------------------------------------------------
232 // soud manager
233 // ----------------------------------------------------------------------------
234
235 void VM::initialize_sound(int rate, int samples)
236 {
237         // init sound manager
238         event->initialize_sound(rate, samples);
239         
240         // init sound gen
241         beep->initialize_sound(rate, 2400, 8000);
242         pcm->initialize_sound(rate, 8000);
243 }
244
245 uint16_t* VM::create_sound(int* extra_frames)
246 {
247         return event->create_sound(extra_frames);
248 }
249
250 int VM::get_sound_buffer_ptr()
251 {
252         return event->get_sound_buffer_ptr();
253 }
254
255 #ifdef USE_SOUND_VOLUME
256 void VM::set_sound_device_volume(int ch, int decibel_l, int decibel_r)
257 {
258         if(ch == 0) {
259                 beep->set_volume(0, decibel_l, decibel_r);
260         } else if(ch == 1) {
261                 pcm->set_volume(0, decibel_l, decibel_r);
262         } else if(ch == 2) {
263                 fdc->get_context_noise_seek()->set_volume(0, decibel_l, decibel_r);
264                 fdc->get_context_noise_head_down()->set_volume(0, decibel_l, decibel_r);
265                 fdc->get_context_noise_head_up()->set_volume(0, decibel_l, decibel_r);
266         }
267 }
268 #endif
269
270 // ----------------------------------------------------------------------------
271 // notify key
272 // ----------------------------------------------------------------------------
273
274 void VM::key_down(int code, bool repeat)
275 {
276         ioctrl->key_down(code);
277 }
278
279 void VM::key_up(int code)
280 {
281         ioctrl->key_up(code);
282 }
283
284 bool VM::get_caps_locked()
285 {
286         return ioctrl->get_caps_locked();
287 }
288
289 bool VM::get_kana_locked()
290 {
291         return ioctrl->get_kana_locked();
292 }
293
294 // ----------------------------------------------------------------------------
295 // user interface
296 // ----------------------------------------------------------------------------
297
298 void VM::open_floppy_disk(int drv, const _TCHAR* file_path, int bank)
299 {
300         fdc->open_disk(drv, file_path, bank);
301 }
302
303 void VM::close_floppy_disk(int drv)
304 {
305         fdc->close_disk(drv);
306 }
307
308 bool VM::is_floppy_disk_inserted(int drv)
309 {
310         return fdc->is_disk_inserted(drv);
311 }
312
313 void VM::is_floppy_disk_protected(int drv, bool value)
314 {
315         fdc->is_disk_protected(drv, value);
316 }
317
318 bool VM::is_floppy_disk_protected(int drv)
319 {
320         return fdc->is_disk_protected(drv);
321 }
322
323 uint32_t VM::is_floppy_disk_accessed()
324 {
325         return fdc->read_signal(0);
326 }
327
328 bool VM::is_frame_skippable()
329 {
330         return event->is_frame_skippable();
331 }
332
333 void VM::update_config()
334 {
335         for(DEVICE* device = first_device; device; device = device->next_device) {
336                 device->update_config();
337         }
338 }
339
340 #define STATE_VERSION   3
341
342 #include "../../statesub.h"
343 #include "../../qt/gui/csp_logger.h"
344 extern CSP_Logger DLL_PREFIX_I *csp_logger;
345
346 void VM::decl_state(void)
347 {
348         state_entry = new csp_state_utils(STATE_VERSION, 0, (_TCHAR *)(_T("CSP::PC_100_HEAD")), csp_logger);
349         DECL_STATE_ENTRY_1D_ARRAY(ram, sizeof(ram));
350         for(DEVICE* device = first_device; device; device = device->next_device) {
351                 device->decl_state();
352         }
353 }
354
355 void VM::save_state(FILEIO* state_fio)
356 {
357         //state_fio->FputUint32(STATE_VERSION);
358         
359         if(state_entry != NULL) {
360                 state_entry->save_state(state_fio);
361         }
362         for(DEVICE* device = first_device; device; device = device->next_device) {
363                 device->save_state(state_fio);
364         }
365         //state_fio->Fwrite(ram, sizeof(ram), 1);
366 }
367
368 bool VM::load_state(FILEIO* state_fio)
369 {
370         //if(state_fio->FgetUint32() != STATE_VERSION) {
371         //      return false;
372         //}
373         bool mb = false;
374         if(state_entry != NULL) {
375                 mb = state_entry->load_state(state_fio);
376         }
377         if(!mb) {
378                 emu->out_debug_log("INFO: HEADER DATA ERROR");
379                 return false;
380         }
381         for(DEVICE* device = first_device; device; device = device->next_device) {
382                 if(!device->load_state(state_fio)) {
383                         return false;
384                 }
385         }
386         state_fio->Fread(ram, sizeof(ram), 1);
387         return true;
388 }
389
390 bool VM::process_state(FILEIO* state_fio, bool loading)
391 {
392         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
393                 return false;
394         }
395         for(DEVICE* device = first_device; device; device = device->next_device) {
396                 const char *name = typeid(*device).name() + 6; // skip "class "
397                 int len = strlen(name);
398                 
399                 if(!state_fio->StateCheckInt32(len)) {
400                         return false;
401                 }
402                 if(!state_fio->StateCheckBuffer(name, len, 1)) {
403                         return false;
404                 }
405                 if(!device->process_state(state_fio, loading)) {
406                         return false;
407                 }
408         }
409         state_fio->StateBuffer(ram, sizeof(ram), 1);
410         return true;
411 }