OSDN Git Service

[VM][FM7] Fix memory leak. Not using MEMORY:: class.
[csp-qt/common_source_project-fm7.git] / source / src / vm / fm7 / fm7.cpp
1
2 /*
3  * FM7 -> VM
4  * (C) 2015 K.Ohta <whatisthis.sowhat _at_ gmail.com>
5  * History:
6  *   Feb 27, 2015 : Initial
7  */
8
9 #include "fm7.h"
10 #include "../../emu.h"
11 #include "../../config.h"
12 #include "../device.h"
13 #include "../event.h"
14 #include "../memory.h"
15 #ifdef USE_DEBUGGER
16 #include "../debugger.h"
17 #endif
18
19
20 #include "../datarec.h"
21 #include "../disk.h"
22
23 #include "../mc6809.h"
24 #include "../z80.h"
25 #include "../ym2203.h"
26 #include "../mb8877.h"
27 //#include "../beep.h"
28 #include "../pcm1bit.h"
29 #include "../ym2203.h"
30 #if defined(_FM77AV_VARIANTS)
31 #include "./mb61vh010.h"
32 #endif
33
34 #include "./fm7_mainio.h"
35 #include "./fm7_mainmem.h"
36 #include "./fm7_display.h"
37 #include "./fm7_keyboard.h"
38
39 #include "./kanjirom.h"
40
41 VM::VM(EMU* parent_emu): emu(parent_emu)
42 {
43         
44         first_device = last_device = NULL;
45         connect_opn = false;
46         connect_whg = false;
47         connect_thg = false;
48         opn[0] = opn[1] = opn[2] = psg = NULL; 
49    
50         dummy = new DEVICE(this, emu);  // must be 1st device
51         event = new EVENT(this, emu);   // must be 2nd device
52         
53         dummycpu = new DEVICE(this, emu);
54         // basic devices
55         maincpu = new MC6809(this, emu);
56         subcpu = new MC6809(this, emu);
57 #ifdef WITH_Z80
58         z80cpu = new Z80(this, emu);
59 #endif
60         keyboard = new KEYBOARD(this, emu);
61 #if defined(_FM77AV_VARIANTS)
62         alu = new MB61VH010(this, emu);
63 #endif  
64         // I/Os
65         drec = new DATAREC(this, emu);
66         pcm1bit = new PCM1BIT(this, emu);
67 //      beep = new BEEP(this, emu);
68         fdc  = new MB8877(this, emu);
69         
70         opn[0] = new YM2203(this, emu); // OPN
71         opn[1] = new YM2203(this, emu); // WHG
72         opn[2] = new YM2203(this, emu); // THG
73 #if !defined(_FM77AV_VARIANTS)
74         psg = new YM2203(this, emu);
75 #endif
76         kanjiclass1 = new KANJIROM(this, emu, false);
77 #ifdef CAPABLE_KANJI_CLASS2
78         kanjiclass2 = new KANJIROM(this, emu, true);
79 #endif
80         mainio  = new FM7_MAINIO(this, emu);
81         mainmem = new FM7_MAINMEM(this, emu);
82         display = new DISPLAY(this, emu);
83   
84         connect_bus();
85         initialize();
86 }
87
88 VM::~VM()
89 {
90         // delete all devices
91         for(DEVICE* device = first_device; device;) {
92                 DEVICE *next_device = device->next_device;
93                 device->release();
94                 delete device;
95                 device = next_device;
96         }
97 }
98
99 DEVICE* VM::get_device(int id)
100 {
101         for(DEVICE* device = first_device; device; device = device->next_device) {
102                 if(device->this_device_id == id) {
103                         return device;
104                 }
105         }
106         return NULL;
107 }
108
109
110 void VM::initialize(void)
111 {
112 #if defined(_FM8) || defined(_FM7)
113         cycle_steal = false;
114 #else
115         cycle_steal = true;
116 #endif
117         clock_low = false;
118         
119 }
120
121
122 void VM::connect_bus(void)
123 {
124         uint32 mainclock;
125         uint32 subclock;
126         int i;
127
128         /*
129          * CLASS CONSTRUCTION
130          *
131          * VM 
132          *  |-> MAINCPU -> MAINMEM -> MAINIO -> MAIN DEVICES
133          *  |             |        |      
134          *  | -> SUBCPU  -> SUBMEM  -> SUBIO -> SUB DEVICES
135          *  | -> DISPLAY
136          *  | -> KEYBOARD
137          *
138          *  MAINMEM can access SUBMEM/IO, when SUBCPU is halted.
139          *  MAINMEM and SUBMEM can access DISPLAY and KEYBOARD with exclusive.
140          *  MAINCPU can access MAINMEM.
141          *  SUBCPU  can access SUBMEM.
142          *  DISPLAY : R/W from MAINCPU and SUBCPU.
143          *  KEYBOARD : R/W
144          *
145          */
146         event->set_frames_per_sec(60.00);
147         event->set_lines_per_frame(400);
148 #if defined(_FM77AV40) || defined(_FM77AV20)
149         event->set_context_cpu(dummycpu, MAINCLOCK_FAST_MMR * 2);
150 #else
151         event->set_context_cpu(dummycpu, SUBCLOCK_NORMAL * 2);
152 #endif
153         
154 #if defined(_FM8)
155         mainclock = MAINCLOCK_SLOW;
156         subclock = SUBCLOCK_SLOW;
157 #else
158         if(config.cpu_type == 0) {
159                 // 2MHz
160                 subclock = SUBCLOCK_NORMAL;
161                 mainclock = MAINCLOCK_NORMAL;
162         } else {
163                 // 1.2MHz
164                 mainclock = MAINCLOCK_SLOW;
165                 subclock = SUBCLOCK_SLOW;
166         }
167 #endif
168 #if defined(_FM77AV40) || defined(_FM77AV20)
169         event->set_context_cpu(maincpu, MAINCLOCK_FAST_MMR);
170 #else
171         event->set_context_cpu(maincpu, MAINCLOCK_NORMAL);
172 #endif  
173         event->set_context_cpu(subcpu,  SUBCLOCK_NORMAL);
174    
175 #ifdef WITH_Z80
176         event->set_context_cpu(z80cpu,  4000000);
177         z80cpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
178 #endif
179
180         event->set_context_sound(pcm1bit);
181 #if !defined(_FM77AV_VARIANTS)
182         mainio->set_context_psg(psg);
183         event->set_context_sound(psg);
184 #endif
185         event->set_context_sound(opn[0]);
186         event->set_context_sound(opn[1]);
187         event->set_context_sound(opn[2]);
188         event->set_context_sound(drec);
189         //event->register_vline_event(display);
190         event->register_frame_event(display);
191         //event->register_vline_event(mainio);
192    
193         mainio->set_context_maincpu(maincpu);
194         mainio->set_context_subcpu(subcpu);
195         
196         mainio->set_context_display(display);
197         mainio->set_context_kanjirom_class1(kanjiclass1);
198         mainio->set_context_mainmem(mainmem);
199         mainio->set_context_keyboard(keyboard);
200    
201 #if defined(CAPABLE_KANJI_CLASS2)
202         mainio->set_context_kanjirom_class2(kanjiclass2);
203 #endif
204
205         keyboard->set_context_break_line(mainio, FM7_MAINIO_PUSH_BREAK, 0xffffffff);
206         keyboard->set_context_mainio(mainio);
207         keyboard->set_context_display(display);
208         
209         keyboard->set_context_rxrdy(keyboard, SIG_FM7KEY_RXRDY, 0x01);
210         keyboard->set_context_rxrdy(display, SIG_FM7KEY_RXRDY, 0x01);
211         
212         keyboard->set_context_key_ack(keyboard, SIG_FM7KEY_ACK, 0x01);
213         keyboard->set_context_key_ack(display, SIG_FM7KEY_ACK, 0x01);
214    
215         drec->set_context_out(mainio, FM7_MAINIO_CMT_RECV, 0xffffffff);
216         //drec->set_context_remote(mainio, FM7_MAINIO_CMT_REMOTE, 0xffffffff);
217         mainio->set_context_datarec(drec);
218         mainmem->set_context_mainio(mainio);
219         mainmem->set_context_display(display);
220         mainmem->set_context_maincpu(maincpu);
221   
222         display->set_context_mainio(mainio);
223         display->set_context_subcpu(subcpu);
224         display->set_context_keyboard(keyboard);
225         subcpu->set_context_bus_halt(display, SIG_FM7_SUB_HALT, 0xffffffff);
226
227         display->set_context_kanjiclass1(kanjiclass1);
228 #if defined(CAPABLE_KANJI_CLASS2)
229         display->set_context_kanjiclass2(kanjiclass2);
230 #endif   
231 #if defined(_FM77AV_VARIANTS)
232         display->set_context_alu(alu);
233         alu->set_context_memory(display);
234 #endif  
235         // Palette, VSYNC, HSYNC, Multi-page, display mode. 
236         mainio->set_context_display(display);
237         
238         //FDC
239         mainio->set_context_fdc(fdc);
240         fdc->set_context_irq(mainio, FM7_MAINIO_FDC_IRQ, 0x1);
241         fdc->set_context_drq(mainio, FM7_MAINIO_FDC_DRQ, 0x1);
242         // SOUND
243         mainio->set_context_beep(pcm1bit);
244         //mainio->set_context_beep(beep);
245         
246         opn[0]->set_context_irq(mainio, FM7_MAINIO_OPN_IRQ, 0xffffffff);
247         //opn[0]->set_context_port_a(mainio, FM7_MAINIO_OPNPORTA_CHANGED, 0xff, 0);
248         //opn[0]->set_context_port_b(mainio, FM7_MAINIO_OPNPORTB_CHANGED, 0xff, 0);
249         mainio->set_context_opn(opn[0], 0);
250         opn[1]->set_context_irq(mainio, FM7_MAINIO_WHG_IRQ, 0xffffffff);
251         mainio->set_context_opn(opn[1], 1);
252         opn[2]->set_context_irq(mainio, FM7_MAINIO_THG_IRQ, 0xffffffff);
253         mainio->set_context_opn(opn[2], 2);
254    
255         subcpu->set_context_bus_halt(display, SIG_FM7_SUB_HALT, 0xffffffff);
256         subcpu->set_context_bus_clr(display, SIG_FM7_SUB_USE_CLR, 0x0000000f);
257    
258         maincpu->set_context_mem(mainmem);
259         subcpu->set_context_mem(display);
260 #ifdef USE_DEBUGGER
261         maincpu->set_context_debugger(new DEBUGGER(this, emu));
262         subcpu->set_context_debugger(new DEBUGGER(this, emu));
263 #endif
264
265         for(DEVICE* device = first_device; device; device = device->next_device) {
266                 device->initialize();
267         }
268         //maincpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
269         //subcpu->write_signal(SIG_CPU_BUSREQ, 1, 1);
270    
271         for(int i = 0; i < 2; i++) {
272 #if defined(_FM77AV20) || defined(_FM77AV40SX) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
273                 fdc->set_drive_type(i, DRIVE_TYPE_2DD);
274 #else
275                 fdc->set_drive_type(i, DRIVE_TYPE_2D);
276 #endif
277                 fdc->set_drive_rpm(i, 360);
278                 fdc->set_drive_mfm(i, true);
279         }
280 #if defined(_FM77) || defined(_FM77L4)
281         for(int i = 2; i < 4; i++) {
282                 fdc->set_drive_type(i, DRIVE_TYPE_2HD);
283 //              fdc->set_drive_rpm(i, 300);
284 //              fdc->set_drive_mfm(i, true);
285         }
286 #endif
287         
288 }  
289
290 void VM::update_config()
291 {
292 #if !defined(_FM8)
293         switch(config.cpu_type){
294                 case 0:
295                         event->set_secondary_cpu_clock(maincpu, MAINCLOCK_NORMAL);
296                         break;
297                 case 1:
298                         event->set_secondary_cpu_clock(maincpu, MAINCLOCK_SLOW);
299                         break;
300         }
301 #endif
302
303         for(DEVICE* device = first_device; device; device = device->next_device) {
304                 device->update_config();
305         }
306         //update_dipswitch();
307 }
308
309 void VM::reset()
310 {
311         // reset all devices
312         for(DEVICE* device = first_device; device; device = device->next_device) {
313                 device->reset();
314         }
315         opn[0]->SetReg(0x2e, 0);        // set prescaler
316         opn[1]->SetReg(0x2e, 0);        // set prescaler
317         opn[2]->SetReg(0x2e, 0);        // set prescaler
318
319         // Init OPN/PSG.
320         // Parameters from XM7.
321         opn[0]->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
322         opn[1]->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
323         opn[2]->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
324
325 #if !defined(_FM77AV_VARIANTS)
326         psg->SetReg(0x27, 0); // stop timer
327         psg->SetReg(0x2e, 0);   // set prescaler
328         psg->write_signal(SIG_YM2203_MUTE, 0x00, 0x01); // Okay?
329 #endif  
330 }
331
332 void VM::special_reset()
333 {
334         // BREAK + RESET
335         mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
336         mainio->reset();
337         mainmem->reset();
338         
339         display->reset();
340         subcpu->reset();   
341         maincpu->reset();
342         mainio->write_signal(FM7_MAINIO_PUSH_BREAK, 1, 1);
343         event->register_event(mainio, EVENT_UP_BREAK, 10000.0 * 1000.0, false, NULL);
344 }
345
346 void VM::run()
347 {
348         event->drive();
349 }
350
351 double VM::frame_rate()
352 {
353         return event->frame_rate();
354 }
355
356 // ----------------------------------------------------------------------------
357 // debugger
358 // ----------------------------------------------------------------------------
359
360 #ifdef USE_DEBUGGER
361 DEVICE *VM::get_cpu(int index)
362 {
363         if(index == 0) {
364                 return maincpu;
365         } else if(index == 1) {
366                 return subcpu;
367         }
368 #if defined(_WITH_Z80)
369         else if(index == 2) {
370                 return z80cpu;
371         }
372 #endif
373         return NULL;
374 }
375 #endif
376
377 // ----------------------------------------------------------------------------
378 // draw screen
379 // ----------------------------------------------------------------------------
380
381 void VM::draw_screen()
382 {
383         display->draw_screen();
384 }
385
386 int VM::access_lamp()
387 {
388         uint32 status = fdc->read_signal(0);
389         return (status & (1 | 4)) ? 1 : (status & (2 | 8)) ? 2 : 0;
390 }
391
392 void VM::initialize_sound(int rate, int samples)
393 {
394         // init sound manager
395         event->initialize_sound(rate, samples);
396         // init sound gen
397         opn[0]->init(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
398         opn[1]->init(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
399         opn[2]->init(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
400 #if !defined(_FM77AV_VARIANTS)   
401         psg->init(rate, (int)(4.9152 * 1000.0 * 1000.0 / 4.0), samples, 0, 0);
402 #endif   
403         pcm1bit->init(rate, 2000);
404         //drec->init_pcm(rate, 0);
405 }
406
407 uint16* VM::create_sound(int* extra_frames)
408 {
409         uint16* p = event->create_sound(extra_frames);
410         return p;
411 }
412
413 int VM::sound_buffer_ptr()
414 {
415         int pos = event->sound_buffer_ptr();
416         return pos; 
417 }
418
419 // ----------------------------------------------------------------------------
420 // notify key
421 // ----------------------------------------------------------------------------
422
423 void VM::key_down(int code, bool repeat)
424 {
425         if(!repeat) {
426                 keyboard->key_down(code);
427         }
428 }
429
430 void VM::key_up(int code)
431 {
432         keyboard->key_up(code);
433 }
434
435 // ----------------------------------------------------------------------------
436 // user interface
437 // ----------------------------------------------------------------------------
438
439 void VM::open_disk(int drv, _TCHAR* file_path, int bank)
440 {
441         fdc->open_disk(drv, file_path, bank);
442 }
443
444 void VM::close_disk(int drv)
445 {
446         fdc->close_disk(drv);
447 }
448
449 bool VM::disk_inserted(int drv)
450 {
451         return fdc->disk_inserted(drv);
452 }
453
454 #if defined(USE_DISK_WRITE_PROTECT)
455 void VM::write_protect_fd(int drv, bool flag)
456 {
457         fdc->write_protect_fd(drv, flag);
458 }
459
460 bool VM::is_write_protect_fd(int drv)
461 {
462         return fdc->is_write_protect_fd(drv);
463 }
464 #endif
465
466 void VM::play_tape(_TCHAR* file_path)
467 {
468         bool value = drec->play_tape(file_path);
469 }
470
471 void VM::rec_tape(_TCHAR* file_path)
472 {
473         bool value = drec->rec_tape(file_path);
474 }
475
476 void VM::close_tape()
477 {
478         drec->close_tape();
479 }
480
481 bool VM::tape_inserted()
482 {
483         return drec->tape_inserted();
484 }
485
486 #if defined(USE_TAPE_PTR)
487 int VM::get_tape_ptr(void)
488 {
489         return drec->get_tape_ptr();
490 }
491 #endif
492
493 bool VM::now_skip()
494 {
495         return event->now_skip();
496 }
497
498 void VM::update_dipswitch()
499 {
500         // bit0         0=High 1=Standard
501         // bit2         0=5"2D 1=5"2HD
502   //    io->set_iovalue_single_r(0x1ff0, (config.monitor_type & 1) | ((config.drive_type & 1) << 2));
503 }
504
505 void VM::set_cpu_clock(DEVICE *cpu, uint32 clocks) {
506         event->set_secondary_cpu_clock(cpu, clocks);
507 }
508
509 #define STATE_VERSION   1
510