OSDN Git Service

[VM][PCM1BIT][AY3_891x] Add LPF feature.See initialize_sound().
[csp-qt/common_source_project-fm7.git] / source / src / vm / event.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2006.11.29-
6
7         [ event manager ]
8 */
9
10 /*
11  * BELOW INCLUDES ARE for run_cpu().
12  * ToDo: Minimum include.
13  */
14 #include "vm.h"
15
16 #if defined(USE_CPU_HD6301)
17 #include "hd6301.h"
18 #endif
19
20 #if defined(USE_CPU_HUC6280)
21 #include "huc6280.h"
22 #endif
23
24 #if defined(USE_CPU_I86) || defined(USE_CPU_I286) || defined(USE_CPU_I186) || defined(USE_CPU_V30)
25 #include "i286.h"
26 #endif
27
28 #if defined(USE_CPU_I386) || defined(USE_CPU_I486) || defined(USE_CPU_PENTIUM)
29 #include "i386.h"
30 #endif
31
32 #if defined(USE_CPU_I8080)
33 #include "i8080.h"
34 #endif
35
36 #if defined(USE_CPU_M6502) || defined(USE_CPU_N2A03)
37 #include "m6502.h"
38 #endif
39
40 #if defined(USE_CPU_MB8861)
41 #include "mb8861.h"
42 #endif
43
44 #if defined(USE_CPU_MC6800)
45 #include "mc6800.h"
46 #endif
47
48 #if defined(USE_CPU_MC6801)
49 #include "mc6801.h"
50 #endif
51
52 #if defined(USE_CPU_MC6809)
53 #include "mc6809.h"
54 #endif
55
56 #if defined(USE_CPU_MCS48)
57 #include "mcs48.h"
58 #endif
59
60 #if defined(USE_CPU_TMS9995)
61 #include "tms9995.h"
62 #endif
63
64 #if defined(USE_CPU_UPD7801)
65 #include "upd7801.h"
66 #endif
67
68 #if defined(USE_CPU_Z80)
69 #include "z80.h"
70 #endif
71
72 #include "event.h"
73
74 #define EVENT_MIX       0
75 #define EVENT_VLINE     1
76
77 void EVENT::initialize()
78 {
79         DEVICE::initialize();
80         // load config
81         if(!(0 <= config.cpu_power && config.cpu_power <= 4)) {
82                 config.cpu_power = 0;
83         }
84         power = config.cpu_power;
85         
86         // initialize sound buffer
87         sound_buffer = NULL;
88         sound_tmp = NULL;
89         
90         dont_skip_frames = 0;
91         prev_skip = next_skip = false;
92         sound_changed = false;
93         
94         // temporary
95         frame_clocks = (int)((double)d_cpu[0].cpu_clocks / (double)FRAMES_PER_SEC + 0.5);
96         vline_start_clock = 0;
97         cur_vline = 0;
98         vline_clocks[0] = (int)((double)d_cpu[0].cpu_clocks / (double)FRAMES_PER_SEC / (double)LINES_PER_FRAME + 0.5);
99 }
100
101 void EVENT::initialize_sound(int rate, int samples)
102 {
103         // initialize sound
104         sound_samples = samples;
105         sound_tmp_samples = samples * 2;
106         sound_buffer = (uint16_t*)malloc(sound_samples * sizeof(uint16_t) * 2);
107         memset(sound_buffer, 0, sound_samples * sizeof(uint16_t) * 2);
108         sound_tmp = (int32_t*)malloc(sound_tmp_samples * sizeof(int32_t) * 2);
109         memset(sound_tmp, 0, sound_tmp_samples * sizeof(int32_t) * 2);
110         buffer_ptr = 0;
111         mix_counter = 1;
112         mix_limit = (int)((double)(emu->get_sound_rate() / 2000.0)); // per 0.5ms.
113         // ToDo: Lock Mutex
114         for(int i = 0; i < MAX_SOUND_IN_BUFFERS; i++) {
115                 if(sound_in_rate[i] != rate) {
116                         release_sound_in_source(i);
117                         sound_in_samples[i] = samples;
118                 }
119                 if((sound_in_samples[i] > 0) && (sound_in_channels[i] > 0)) {
120                         if(sound_in_tmp_buffer[i] == NULL) {
121                                 sound_in_tmp_buffer[i] = (int16_t*)malloc(sound_in_samples[i] * sound_in_channels[i] * sizeof(int16_t));
122                                 if(sound_in_tmp_buffer[i] != NULL) {
123                                         clear_sound_in_source(i);
124                                 }
125                         }
126                 }
127         }
128         // ToDo: UnLock Mutex
129         // register event
130         this->register_event(this, EVENT_MIX, 1000000.0 / rate, true, NULL);
131 }
132
133 void EVENT::clear_sound_in_source(int bank)
134 {
135         if(bank < 0) return;
136         if(bank >= MAX_SOUND_IN_BUFFERS) return;
137
138         if(sound_in_tmp_buffer[bank] == NULL) return;
139         if(sound_in_samples[bank] <= 0) return;
140         if(sound_in_channels[bank] <= 0) return;
141         memset(sound_in_tmp_buffer[bank], 0x00, sound_in_samples[bank] * sound_in_channels[bank] * sizeof(int16_t));
142 }
143
144 int EVENT::add_sound_in_source(int rate, int samples, int channels)
145 {
146         int banknum;
147         for(banknum = 0; banknum < MAX_SOUND_IN_BUFFERS; banknum++) {
148                 if(sound_in_samples[banknum] == 0) break;
149         }
150         if(banknum < MAX_SOUND_IN_BUFFERS) {
151         // ToDo: Lock Mutex
152                 if(sound_in_tmp_buffer[banknum] != NULL) free(sound_in_tmp_buffer[banknum]);
153                 sound_in_tmp_buffer[banknum] = NULL;
154                 if((rate > 0) && (samples > 0)) {
155                         sound_in_rate[banknum] = rate;
156                         sound_in_samples[banknum] = samples;
157                         sound_in_channels[banknum] = channels;
158                         sound_in_tmp_buffer[banknum] = (int16_t*)malloc(samples * sizeof(int16_t) * channels);
159                 }
160         // ToDo: UnLock Mutex
161                 sound_in_writeptr[banknum] = 0;
162                 sound_in_readptr[banknum] = 0;
163                 sound_in_write_size[banknum] = 0;
164                 sound_in_read_size[banknum] = 0;
165                 sound_in_read_mod[banknum] = 0;
166                 return banknum;
167         }
168         return -1; // error
169 }
170
171 int EVENT::release_sound_in_source(int bank)
172 {
173         if(bank < 0) return -1;
174         if(bank >= MAX_SOUND_IN_BUFFERS) return -1;
175         // ToDo: Lock Mutex
176         if(sound_in_tmp_buffer[bank] != NULL) {
177                 free(sound_in_tmp_buffer[bank]);
178                 sound_in_tmp_buffer[bank] = NULL;
179         }
180         sound_in_readptr[bank] = 0;
181         sound_in_writeptr[bank] = 0;
182         sound_in_read_size[bank] = 0;
183         sound_in_write_size[bank] = 0;
184         clear_sound_in_source(bank);
185         // ToDo: UnLock Mutex
186         return bank;
187 }
188
189 void EVENT::release()
190 {
191         // release sound
192         for(int i = 0; i < MAX_SOUND_IN_BUFFERS; i++) {
193                 release_sound_in_source(i);
194         }
195         if(sound_buffer) {
196                 free(sound_buffer);
197         }
198         if(sound_tmp) {
199                 free(sound_tmp);
200         }
201 }
202
203 void EVENT::reset()
204 {
205         // clear events except loop event
206         for(int i = 0; i < MAX_EVENT; i++) {
207                 if(event[i].active && event[i].loop_clock == 0) {
208                         cancel_event(NULL, i);
209                 }
210         }
211         
212         event_remain = event_extra = 0;
213         cpu_remain = cpu_accum = cpu_done = 0;
214         
215         // reset sound
216         if(sound_buffer) {
217                 memset(sound_buffer, 0, sound_samples * sizeof(uint16_t) * 2);
218         }
219         if(sound_tmp) {
220                 memset(sound_tmp, 0, sound_tmp_samples * sizeof(int32_t) * 2);
221         }
222 //      buffer_ptr = 0;
223         
224 #ifdef _DEBUG_LOG
225         initialize_done = true;
226 #endif
227 }
228
229 #define RUN_CPU_N(num, _class, arg)                                                             \
230         return static_cast<_class *>(d_cpu[num].device)->run(arg)
231
232 #define RUN_CPU_GENERIC(num, arg)                       \
233         return d_cpu[num].device->run(arg)
234
235
236 int EVENT::run_cpu(uint32_t num, int cycles)
237 {
238         if(num < MAX_CPU) {
239                 uint32_t dom_num = cpu_type[num];
240                 switch(dom_num) {
241 #if defined(USE_CPU_HD6301)
242                 case EVENT_CPUTYPE_HD6301:
243                         RUN_CPU_N(num, HD6301, cycles);
244                         break;
245 #endif
246 #if defined(USE_CPU_HUC6280)
247                 case EVENT_CPUTYPE_HUC6280:
248                         RUN_CPU_N(num, HUC6280, cycles);
249                         break;
250 #endif
251 #if defined(USE_CPU_I286)
252                 case EVENT_CPUTYPE_I286:
253                         RUN_CPU_N(num, I286, cycles);
254                         break;
255 #endif
256 #if defined(USE_CPU_I386)
257                 case EVENT_CPUTYPE_I386:
258                         RUN_CPU_N(num, I386, cycles);
259                         break;
260 #endif
261 #if defined(USE_CPU_I8080)
262                 case EVENT_CPUTYPE_I8080:
263                         RUN_CPU_N(num, I8080, cycles);
264                         break;
265 #endif
266 #if defined(USE_CPU_M6502)
267                 case EVENT_CPUTYPE_M6502:
268                         RUN_CPU_N(num, M6502, cycles);
269                         break;
270 #endif
271 #if defined(USE_CPU_N2A03)
272                 case EVENT_CPUTYPE_N2A03:
273                         RUN_CPU_N(num, N2A03, cycles);
274                         break;
275 #endif
276 #if defined(USE_CPU_MB8861)
277                 case EVENT_CPUTYPE_MB8861:
278                         RUN_CPU_N(num, MB8861, cycles);
279                         break;
280 #endif
281 #if defined(USE_CPU_MC6800)
282                 case EVENT_CPUTYPE_MC6800:
283                         RUN_CPU_N(num, MC6800, cycles);
284                         break;
285 #endif
286 #if defined(USE_CPU_MC6801)
287                 case EVENT_CPUTYPE_MC6801:
288                         RUN_CPU_N(num, MC6801, cycles);
289                         break;
290 #endif
291 #if defined(USE_CPU_MC6809)
292                 case EVENT_CPUTYPE_MC6809:
293                         RUN_CPU_N(num, MC6809, cycles);
294                         break;
295 #endif
296 #if defined(USE_CPU_MCS48)
297                 case EVENT_CPUTYPE_MCS48:
298                         RUN_CPU_N(num, MCS48, cycles);
299                         break;
300 #endif
301 #if defined(USE_CPU_TMS9995)
302                 case EVENT_CPUTYPE_TMS9995:
303                         RUN_CPU_N(num, TMS9995, cycles);
304                         break;
305 #endif
306 #if defined(USE_CPU_UPD7801)
307                 case EVENT_CPUTYPE_UPD7801:
308                         RUN_CPU_N(num, UPD7801, cycles);
309                         break;
310 #endif
311 #if defined(USE_CPU_Z80)
312                 case EVENT_CPUTYPE_Z80:
313                         RUN_CPU_N(num, Z80, cycles);
314                         break;
315 #endif
316                 case EVENT_CPUTYPE_GENERIC:
317                 default:
318                         RUN_CPU_GENERIC(num, cycles);
319                         break;
320                 }
321         }
322         if(cycles <= 0) {
323                 return 1;
324         } else {
325                 return cycles;
326         }
327 }
328
329 //#define USE_SUPRESS_VTABLE
330 void EVENT::drive()
331 {
332         // raise pre frame events to update timing settings
333         for(int i = 0; i < frame_event_count; i++) {
334                 frame_event[i]->event_pre_frame();
335         }
336         
337         // generate clocks per line
338         if(frames_per_sec != next_frames_per_sec || lines_per_frame != next_lines_per_frame) {
339                 frames_per_sec = next_frames_per_sec;
340                 lines_per_frame = next_lines_per_frame;
341                 
342                 frame_clocks = (int)((double)d_cpu[0].cpu_clocks / frames_per_sec + 0.5);
343                 int remain = frame_clocks;
344                 
345                 for(int i = 0; i < lines_per_frame; i++) {
346                         assert(i < MAX_LINES);
347                         vline_clocks[i] = (int)(frame_clocks / lines_per_frame);
348                         remain -= vline_clocks[i];
349                 }
350                 for(int i = 0; i < remain; i++) {
351                         int index = (int)((double)lines_per_frame * (double)i / (double)remain);
352                         assert(index < MAX_LINES);
353                         vline_clocks[index]++;
354                 }
355                 for(int i = 1; i < dcount_cpu; i++) {
356                         d_cpu[i].update_clocks = (int)(1024.0 * (double)d_cpu[i].cpu_clocks / (double)d_cpu[0].cpu_clocks + 0.5);
357                 }
358                 for(DEVICE* device = vm->first_device; device; device = device->next_device) {
359                         if(device->get_event_manager_id() == this_device_id) {
360                                 device->update_timing(d_cpu[0].cpu_clocks, frames_per_sec, lines_per_frame);
361                         }
362                 }
363         }
364         
365         // run virtual machine for 1 frame period
366         for(int i = 0; i < frame_event_count; i++) {
367                 frame_event[i]->event_frame();
368         }
369         
370         cur_vline = 0;
371         vline_start_clock = get_current_clock();
372         
373         for(int i = 0; i < vline_event_count; i++) {
374                 vline_event[i]->event_vline(cur_vline, vline_clocks[cur_vline]);
375         }
376         this->register_event_by_clock(this, EVENT_VLINE, vline_clocks[cur_vline], false, NULL);
377         
378         if(event_remain < 0) {
379                 if(-event_remain > vline_clocks[cur_vline]) {
380                         update_event(vline_clocks[cur_vline]);
381                 } else {
382                         update_event(-event_remain);
383                 }
384         }
385         event_remain += frame_clocks;
386         cpu_remain += frame_clocks << power;
387         
388         while(event_remain > 0) {
389                 int event_done = event_remain;
390                 if(cpu_remain > 0) {
391                         event_extra = 0;
392                         int cpu_done_tmp;
393                         if(dcount_cpu == 1) {
394                                 // run one opecode on primary cpu
395                                 cpu_done_tmp = d_cpu[0].device->run(-1);
396                         } else {
397                                 // sync to sub cpus
398                                 if(cpu_done == 0) {
399                                         // run one opecode on primary cpu
400 #if !defined(USE_SUPRESS_VTABLE)
401                                         cpu_done = d_cpu[0].device->run(-1);
402 #else
403                                         cpu_done = run_cpu(0, -1);
404 #endif
405                                 }
406                                 
407                                 // sub cpu runs continuously and no events will be fired while the given clocks,
408                                 // so I need to give small enough clocks...
409                                 cpu_done_tmp = (event_extra > 0 || cpu_done < 4) ? cpu_done : 4;
410                                 cpu_done -= cpu_done_tmp;
411                                 
412                                 for(int i = 1; i < dcount_cpu; i++) {
413                                         // run sub cpus
414                                         d_cpu[i].accum_clocks += d_cpu[i].update_clocks * cpu_done_tmp;
415                                         int sub_clock = d_cpu[i].accum_clocks >> 10;
416                                         if(sub_clock) {
417                                                 d_cpu[i].accum_clocks -= sub_clock << 10;
418 #if !defined(USE_SUPRESS_VTABLE)
419                                                 d_cpu[i].device->run(sub_clock);
420 #else
421                                                 run_cpu(i, sub_clock);
422 #endif
423                                         }
424                                 }
425                         }
426                         cpu_remain -= cpu_done_tmp;
427                         cpu_accum += cpu_done_tmp;
428                         event_done = cpu_accum >> power;
429                         cpu_accum -= event_done << power;
430                         event_done -= event_extra;
431                 }
432                 if(event_done > 0) {
433                         if(event_remain > 0) {
434                                 if(event_done > event_remain) {
435                                         update_event(event_remain);
436                                 } else {
437                                         update_event(event_done);
438                                 }
439                         }
440                         event_remain -= event_done;
441                 }
442         }
443 }
444
445 void EVENT::update_extra_event(int clock)
446 {
447         // this is called from primary cpu while running one opecode
448         int event_done = clock >> power;
449         
450         if(event_done > 0) {
451                 if(event_remain > 0) {
452                         if(event_done > event_remain) {
453                                 update_event(event_remain);
454                         } else {
455                                 update_event(event_done);
456                         }
457                 }
458                 event_remain -= event_done;
459                 event_extra += event_done;
460         }
461 }
462
463 void EVENT::update_event(int clock)
464 {
465         uint64_t event_clocks_tmp = event_clocks + clock;
466         
467         while(first_fire_event != NULL && first_fire_event->expired_clock <= event_clocks_tmp) {
468                 event_t *event_handle = first_fire_event;
469                 uint64_t expired_clock = event_handle->expired_clock;
470                 
471                 first_fire_event = event_handle->next;
472                 if(first_fire_event != NULL) {
473                         first_fire_event->prev = NULL;
474                 }
475                 if(event_handle->loop_clock != 0) {
476                         event_handle->accum_clocks += event_handle->loop_clock;
477                         uint64_t clock_tmp = event_handle->accum_clocks >> 10;
478                         event_handle->accum_clocks -= clock_tmp << 10;
479                         event_handle->expired_clock += clock_tmp;
480                         insert_event(event_handle);
481                 } else {
482                         event_handle->active = false;
483                         event_handle->next = first_free_event;
484                         first_free_event = event_handle;
485                 }
486                 event_clocks = expired_clock;
487                 event_handle->device->event_callback(event_handle->event_id, 0);
488         }
489         event_clocks = event_clocks_tmp;
490 }
491
492 uint32_t EVENT::get_current_clock()
493 {
494         return (uint32_t)(event_clocks & 0xffffffff);
495 }
496
497 uint32_t EVENT::get_passed_clock(uint32_t prev)
498 {
499         uint32_t current = get_current_clock();
500         return (current > prev) ? current - prev : current + (0xffffffff - prev) + 1;
501 }
502
503 double EVENT::get_passed_usec(uint32_t prev)
504 {
505         return 1000000.0 * get_passed_clock(prev) / d_cpu[0].cpu_clocks;
506 }
507
508 uint32_t EVENT::get_passed_clock_since_vline()
509 {
510         return get_passed_clock(vline_start_clock);
511 }
512
513 double EVENT::get_passed_usec_since_vline()
514 {
515         return get_passed_usec(vline_start_clock);
516 }
517
518 uint32_t EVENT::get_cpu_pc(int index)
519 {
520         return d_cpu[index].device->get_pc();
521 }
522
523 void EVENT::register_event(DEVICE* device, int event_id, double usec, bool loop, int* register_id)
524 {
525 #ifdef _DEBUG_LOG
526         if(!initialize_done && !loop) {
527                 this->out_debug_log(_T("EVENT: non-loop event is registered before initialize is done\n"));
528         }
529 #endif
530         
531         // register event
532         if(first_free_event == NULL) {
533 #ifdef _DEBUG_LOG
534                 this->out_debug_log(_T("EVENT: too many events !!!\n"));
535 #endif
536                 if(register_id != NULL) {
537                         *register_id = -1;
538                 }
539                 return;
540         }
541         event_t *event_handle = first_free_event;
542         first_free_event = first_free_event->next;
543         
544         if(register_id != NULL) {
545                 *register_id = event_handle->index;
546         }
547         event_handle->active = true;
548         event_handle->device = device;
549         event_handle->event_id = event_id;
550         uint64_t clock;
551         if(loop) {
552                 event_handle->loop_clock = (uint64_t)(1024.0 * (double)d_cpu[0].cpu_clocks / 1000000.0 * usec + 0.5);
553                 event_handle->accum_clocks = event_handle->loop_clock;
554                 clock = event_handle->accum_clocks >> 10;
555                 event_handle->accum_clocks -= clock << 10;
556         } else {
557                 clock = (uint64_t)((double)d_cpu[0].cpu_clocks / 1000000.0 * usec + 0.5);
558                 event_handle->loop_clock = 0;
559                 event_handle->accum_clocks = 0;
560         }
561         event_handle->expired_clock = event_clocks + clock;
562         
563         insert_event(event_handle);
564 }
565
566 void EVENT::register_event_by_clock(DEVICE* device, int event_id, uint64_t clock, bool loop, int* register_id)
567 {
568 #ifdef _DEBUG_LOG
569         if(!initialize_done && !loop) {
570                 this->out_debug_log(_T("EVENT: device (name=%s, id=%d) registeres non-loop event before initialize is done\n"), device->this_device_name, device->this_device_id);
571         }
572 #endif
573         
574         // register event
575         if(first_free_event == NULL) {
576 #ifdef _DEBUG_LOG
577                 this->out_debug_log(_T("EVENT: too many events !!!\n"));
578 #endif
579                 if(register_id != NULL) {
580                         *register_id = -1;
581                 }
582                 return;
583         }
584         event_t *event_handle = first_free_event;
585         first_free_event = first_free_event->next;
586         
587         if(register_id != NULL) {
588                 *register_id = event_handle->index;
589         }
590         event_handle->active = true;
591         event_handle->device = device;
592         event_handle->event_id = event_id;
593         event_handle->expired_clock = event_clocks + clock;
594         event_handle->loop_clock = loop ? (clock << 10) : 0;
595         event_handle->accum_clocks = 0;
596         
597         insert_event(event_handle);
598 }
599
600 void EVENT::insert_event(event_t *event_handle)
601 {
602         if(first_fire_event == NULL) {
603                 first_fire_event = event_handle;
604                 event_handle->prev = event_handle->next = NULL;
605         } else {
606                 for(event_t *insert_pos = first_fire_event; insert_pos != NULL; insert_pos = insert_pos->next) {
607                         if(insert_pos->expired_clock > event_handle->expired_clock) {
608                                 if(insert_pos->prev != NULL) {
609                                         // insert
610                                         insert_pos->prev->next = event_handle;
611                                         event_handle->prev = insert_pos->prev;
612                                         event_handle->next = insert_pos;
613                                         insert_pos->prev = event_handle;
614                                         break;
615                                 } else {
616                                         // add to head
617                                         first_fire_event = event_handle;
618                                         event_handle->prev = NULL;
619                                         event_handle->next = insert_pos;
620                                         insert_pos->prev = event_handle;
621                                         break;
622                                 }
623                         } else if(insert_pos->next == NULL) {
624                                 // add to tail
625                                 insert_pos->next = event_handle;
626                                 event_handle->prev = insert_pos;
627                                 event_handle->next = NULL;
628                                 break;
629                         }
630                 }
631         }
632 }
633
634 void EVENT::cancel_event(DEVICE* device, int register_id)
635 {
636         // cancel registered event
637         if(0 <= register_id && register_id < MAX_EVENT) {
638                 event_t *event_handle = &event[register_id];
639                 if(device != NULL && device != event_handle->device) {
640                         this->out_debug_log(_T("EVENT: device (name=%s, id=%d) tries to cancel event %d that is not its own (owned by (name=%s id=%d))!!!\n"), device->this_device_name, device->this_device_id,
641                                                                 register_id,
642                                                                 event_handle->device->this_device_name,
643                                                                 event_handle->device->this_device_id);                  
644                         return;
645                 }
646                 if(event_handle->active) {
647                         if(event_handle->prev != NULL) {
648                                 event_handle->prev->next = event_handle->next;
649                         } else {
650                                 first_fire_event = event_handle->next;
651                         }
652                         if(event_handle->next != NULL) {
653                                 event_handle->next->prev = event_handle->prev;
654                         }
655                         event_handle->active = false;
656                         event_handle->next = first_free_event;
657                         first_free_event = event_handle;
658                 }
659         }
660 }
661
662 void EVENT::register_frame_event(DEVICE* device)
663 {
664         if(frame_event_count < MAX_EVENT) {
665                 for(int i = 0; i < frame_event_count; i++) {
666                         if(frame_event[i] == device) {
667 #ifdef _DEBUG_LOG
668                                 this->out_debug_log(_T("EVENT: device (name=%s, id=%d) has already registered frame event !!!\n"), device->this_device_name, device->this_device_id);
669 #endif
670                                 return;
671                         }
672                 }
673                 frame_event[frame_event_count++] = device;
674 #ifdef _DEBUG_LOG
675         } else {
676                 this->out_debug_log(_T("EVENT: too many frame events !!!\n"));
677 #endif
678         }
679 }
680
681 void EVENT::register_vline_event(DEVICE* device)
682 {
683         if(vline_event_count < MAX_EVENT) {
684                 for(int i = 0; i < vline_event_count; i++) {
685                         if(vline_event[i] == device) {
686 #ifdef _DEBUG_LOG
687                                 this->out_debug_log(_T("EVENT: device (name=%s, id=%d) has already registered vline event !!!\n"), device->this_device_name, device->this_device_id);
688 #endif
689                                 return;
690                         }
691                 }
692                 vline_event[vline_event_count++] = device;
693 #ifdef _DEBUG_LOG
694         } else {
695                 this->out_debug_log(_T("EVENT: too many vline events !!!\n"));
696 #endif
697         }
698 }
699
700 uint32_t EVENT::get_event_remaining_clock(int register_id)
701 {
702         if(0 <= register_id && register_id < MAX_EVENT) {
703                 event_t *event_handle = &event[register_id];
704                 if(event_handle->active && event->expired_clock > event_clocks) {
705                         return (uint32_t)(event->expired_clock - event_clocks);
706                 }
707         }
708         return 0;
709 }
710
711 double EVENT::get_event_remaining_usec(int register_id)
712 {
713         return 1000000.0 * get_event_remaining_clock(register_id) / d_cpu[0].cpu_clocks;
714 }
715
716 void EVENT::touch_sound()
717 {
718         if(!(config.sound_strict_rendering || (need_mix > 0))) {
719                 int samples = mix_counter;
720                 if(samples >= (sound_tmp_samples - buffer_ptr)) {
721                         samples = sound_tmp_samples - buffer_ptr;
722                 }
723                 if(samples > 0) {
724                         mix_sound(samples);
725                         mix_counter -= samples;
726                 }
727         }
728 }
729
730 void EVENT::set_realtime_render(DEVICE* device, bool flag)
731 {
732         assert(device != NULL && device->this_device_id < MAX_DEVICE);
733         if(dev_need_mix[device->this_device_id] != flag) {
734                 if(flag) {
735                         need_mix++;
736                 } else {
737                         assert(need_mix > 0);
738                         need_mix--;
739                         if(need_mix < 0) need_mix = 0;
740                 }
741                 dev_need_mix[device->this_device_id] = flag;
742         }
743 }
744
745 void EVENT::event_callback(int event_id, int err)
746 {
747         if(event_id == EVENT_MIX) {
748                 // mix sound
749                 if(prev_skip && dont_skip_frames == 0 && !sound_changed) {
750                         buffer_ptr = 0;
751                 }
752                 int remain = sound_tmp_samples - buffer_ptr;
753                 
754                 if(remain > 0) {
755                         int samples = mix_counter;
756                         
757                         if(config.sound_strict_rendering || (need_mix > 0)) {
758                                 if(samples < 1) {
759                                         samples = 1;
760                                 }
761                         }
762                         if(samples >= remain) {
763                                 samples = remain;
764                         }
765                         if(config.sound_strict_rendering || (need_mix > 0)) {
766                                 if(samples > 0) {
767                                         mix_sound(samples);
768                                 }
769                                 mix_counter = 1;
770                         } else {
771                                 if(samples > 0 && mix_counter >= mix_limit) {
772                                         mix_sound(samples);
773                                         mix_counter -= samples;
774                                 }
775                                 mix_counter++;
776                         }
777                 }
778         } else if(event_id == EVENT_VLINE) {
779                 if(cur_vline + 1 < lines_per_frame) {
780                         cur_vline++;
781                         vline_start_clock = get_current_clock();
782                         
783                         for(int i = 0; i < vline_event_count; i++) {
784                                 vline_event[i]->event_vline(cur_vline, vline_clocks[cur_vline]);
785                         }
786                         
787                         // do not register if next vline is the first vline of next frame
788                         if(cur_vline + 1 < lines_per_frame) {
789                                 this->register_event_by_clock(this, EVENT_VLINE, vline_clocks[cur_vline], false, NULL);
790                         }
791                 }
792         }
793 }
794
795 void EVENT::mix_sound(int samples)
796 {
797         if(samples > 0) {
798                 int32_t* buffer = sound_tmp + buffer_ptr * 2;
799                 memset(buffer, 0, samples * sizeof(int32_t) * 2);
800                 for(int i = 0; i < dcount_sound; i++) {
801                         d_sound[i]->mix(buffer, samples);
802                 }
803                 if(!sound_changed) {
804                         for(int i = 0; i < samples * 2; i += 2) {
805                                 if(buffer[i] != sound_tmp[0] || buffer[i + 1] != sound_tmp[1]) {
806                                         sound_changed = true;
807                                         break;
808                                 }
809                         }
810                 }
811                 buffer_ptr += samples;
812         } else {
813                 // notify to sound devices
814                 for(int i = 0; i < dcount_sound; i++) {
815                         d_sound[i]->mix(sound_tmp + buffer_ptr * 2, 0);
816                 }
817         }
818 }
819
820 uint16_t* EVENT::create_sound(int* extra_frames)
821 {
822         if(prev_skip && dont_skip_frames == 0 && !sound_changed) {
823                 memset(sound_buffer, 0, sound_samples * sizeof(uint16_t) * 2);
824                 *extra_frames = 0;
825                 return sound_buffer;
826         }
827         int frames = 0;
828         
829         // drive extra frames to fill the sound buffer
830         while(sound_samples > buffer_ptr) {
831                 drive();
832                 frames++;
833         }
834 #ifdef LOW_PASS_FILTER
835         // low-pass filter
836         for(int i = 0; i < sound_samples - 1; i++) {
837                 sound_tmp[i * 2    ] = (sound_tmp[i * 2    ] + sound_tmp[i * 2 + 2]) / 2; // L
838                 sound_tmp[i * 2 + 1] = (sound_tmp[i * 2 + 1] + sound_tmp[i * 2 + 3]) / 2; // R
839         }
840 #endif
841         // copy to buffer
842         for(int i = 0; i < sound_samples * 2; i++) {
843                 int dat = sound_tmp[i];
844                 uint16_t highlow = (uint16_t)(dat & 0x0000ffff);
845                 
846                 if((dat > 0) && (highlow >= 0x8000)) {
847                         sound_buffer[i] = 0x7fff;
848                         continue;
849                 }
850                 if((dat < 0) && (highlow < 0x8000)) {
851                         sound_buffer[i] = 0x8000;
852                         continue;
853                 }
854                 sound_buffer[i] = highlow;
855         }
856         if(buffer_ptr > sound_samples) {
857                 buffer_ptr -= sound_samples;
858                 memcpy(sound_tmp, sound_tmp + sound_samples * 2, buffer_ptr * sizeof(int32_t) * 2);
859         } else {
860                 buffer_ptr = 0;
861         }
862         *extra_frames = frames;
863         return sound_buffer;
864 }
865
866 int EVENT::get_sound_buffer_ptr()
867 {
868         return buffer_ptr;
869 }
870
871 bool EVENT::is_sound_in_source_exists(int bank)
872 {
873         bool f = true;
874         if(bank < 0) return false;
875         if(bank >= MAX_SOUND_IN_BUFFERS) return false;
876         
877         // ToDo: Lock Mutex
878         if(sound_in_tmp_buffer[bank] == NULL) f = false;
879         // ToDo: UnLock Mutex
880         if(sound_in_samples[bank] <= 0) f = false;
881         if(sound_in_rate[bank] <= 0) f = false;
882         if(sound_in_channels[bank] <= 0) f = false;
883         return f;
884 }
885
886 int EVENT::get_sound_in_buffers_count()
887 {
888         int _n = 0;
889         for(int i = 0; i < MAX_SOUND_IN_BUFFERS; i++) {
890                 if(sound_in_samples[i] == 0) break;
891                 _n++;
892         }
893         return _n;
894 }
895
896 int EVENT::get_sound_in_samples(int bank)
897 {
898         if(bank < 0) return 0;
899         if(bank >= MAX_SOUND_IN_BUFFERS) return 0;
900         return sound_in_samples[bank];
901 }
902
903 int EVENT::get_sound_in_rate(int bank)
904 {
905         if(bank < 0) return 0;
906         if(bank >= MAX_SOUND_IN_BUFFERS) return 0;
907         return sound_in_rate[bank];
908 }
909
910 int EVENT::get_sound_in_channels(int bank)
911 {
912         if(bank < 0) return 0;
913         if(bank >= MAX_SOUND_IN_BUFFERS) return 0;
914         return sound_in_channels[bank];
915 }
916
917 int16_t* EVENT::get_sound_in_buf_ptr(int bank)
918 {
919         if(bank < 0) return NULL;
920         if(bank >= MAX_SOUND_IN_BUFFERS) return NULL;
921         return &(sound_in_tmp_buffer[bank][sound_in_writeptr[bank]]);
922 }
923
924 int EVENT::write_sound_in_buffer(int bank, int32_t* src, int samples)
925 {
926         if(bank < 0) return 0;
927         if(bank >= MAX_SOUND_IN_BUFFERS) return 0;
928         if(samples <= 0) return 0;
929         if(sound_in_tmp_buffer[bank] == NULL) return 0;
930         if(samples >= sound_in_samples[bank]) samples = sound_in_samples[bank];
931
932         int n_samples = samples;
933         int16_t* dst = &(sound_in_tmp_buffer[bank][sound_in_writeptr[bank] * sound_in_channels[bank]]);
934         // ToDo: Lock Mutex
935         if((sound_in_writeptr[bank] + samples) >= sound_in_samples[bank]) {
936                 if((sound_in_samples[bank] - sound_in_writeptr[bank] - 1) > 0) {
937                         memcpy(dst, src, (sound_in_samples[bank] - sound_in_writeptr[bank] - 1) * sound_in_channels[bank] * sizeof(int16_t));
938                         src =  src + (sound_in_samples[bank] - sound_in_writeptr[bank] - 1) * sound_in_channels[bank];
939                         sound_in_writeptr[bank] = 0;
940                         n_samples = n_samples - (sound_in_samples[bank] - sound_in_writeptr[bank] - 1);
941                 } else {
942                         sound_in_writeptr[bank] = 0;
943                 }
944                 dst = &(sound_in_tmp_buffer[bank][sound_in_writeptr[bank] * sound_in_channels[bank]]);
945         }
946         memcpy(dst, src, n_samples * sound_in_channels[bank] * sizeof(int16_t));
947         // ToDo: UnLock Mutex
948         sound_in_writeptr[bank] = sound_in_writeptr[bank] + n_samples;
949         if(sound_in_writeptr[bank] >= sound_in_samples[bank]) sound_in_writeptr[bank] = 0;
950         return samples;
951 }
952
953 // Add sampled values to sample buffer;value may be -32768 to +32767.
954 int EVENT::get_sound_in_samples(int bank, int32_t* dst, int expect_samples, int expect_rate, int expect_channels)
955 {
956         if(bank < 0) return -1;
957         if(bank >= MAX_SOUND_IN_BUFFERS) return -1;
958         if(sound_in_tmp_buffer[bank] == NULL) return -1;
959         if(dst == NULL) return -1;
960
961         int n_samples = 0;
962         int32_t tmpbuf[expect_samples * expect_channels];
963         int gave_samples = 0;
964         int sound_div = 1;
965         int sound_mod = 0;
966         if(sound_in_channels[bank] > expect_channels) {
967                 sound_div = sound_in_channels[bank] / expect_channels;
968                 sound_mod = sound_in_channels[bank] % expect_channels;
969         } else if(sound_in_channels[bank] < expect_channels) {
970                 sound_div = expect_channels / sound_in_channels[bank];
971                 sound_mod = expect_channels % sound_in_channels[bank];
972         }
973         // ToDo: Lock Mutex
974         if(sound_in_channels[bank] == expect_channels) {
975                 int32_t* p = tmpbuf;
976                 for(int i = 0; i < expect_samples; i++) {
977                         if(sound_in_write_size[bank] <= 0) break;
978                         int16_t* q = &(sound_in_tmp_buffer[bank][sound_in_readptr[bank] * sound_in_channels[bank]]);
979                         for(int j = 0; j < expect_channels; j++) {
980                                 p[j] = q[j];
981                                         
982                         }
983                         sound_in_readptr[bank] = sound_in_readptr[bank] + 1;
984                         if(sound_in_readptr[bank] >= sound_in_samples[bank]) sound_in_readptr[bank] = 0;
985                         n_samples++;
986                         sound_in_write_size[bank] = sound_in_write_size[bank] - 1;
987                 }
988         } else if(sound_in_channels[bank] > expect_channels) {
989                 for(int i = 0; i < expect_samples; i++) {
990                         int chp = 0;
991                         int chq = 0;
992                         int32_t* p = &(tmpbuf[i * expect_channels]);
993                         int16_t* q = &(sound_in_tmp_buffer[bank][sound_in_readptr[bank] * sound_in_channels[bank]]);
994                         if(sound_in_write_size[bank] <= 0) break;
995                                         
996                         for(int j = 0; j < sound_in_channels[bank]; j++) {
997                                 *p = *p + q[j];
998                                 chp++;
999                                 if(sound_mod == 0) {
1000                                         if(chp >= sound_div) {
1001                                                 p++;
1002                                                 chp = 0;
1003                                                 chq++;
1004                                         }
1005                                 } else {
1006                                         if((chp >= sound_div) && (chq < (expect_channels - 1))){
1007                                                 p++;
1008                                                 chp = 0;
1009                                                 chq++;
1010                                         }       
1011                                 }                                               
1012                         }
1013                         p = &(dst[i * expect_channels]);
1014                         if(sound_mod != 0) {
1015                                 for(int j = 0; j < (expect_channels - 1); j++) {
1016                                         *p = *p / sound_div;
1017                                         p++;
1018                                 }
1019                                 *p = *p / (sound_div + sound_mod);
1020                         } else {
1021                                 for(int j = 0; j < expect_channels; j++) {
1022                                         *p = *p / sound_div;
1023                                         p++;
1024                                 }
1025                         }
1026                         sound_in_readptr[bank] = sound_in_readptr[bank] + 1;
1027                         if(sound_in_readptr[bank] >= sound_in_samples[bank]) sound_in_readptr[bank] = 0;
1028                         n_samples++;
1029                         sound_in_write_size[bank] = sound_in_write_size[bank] - 1;
1030                 }
1031         } else { // sound_in_channels[bank] < expect_channels
1032                 int32_t* p = tmpbuf;
1033                 for(int i = 0; i < expect_samples; i++) {
1034                         if(sound_in_write_size[bank] <= 0) break;
1035                         int chp = 0;
1036                         int chq = 0;
1037                         int16_t* q = &(sound_in_tmp_buffer[bank][sound_in_readptr[bank] * sound_in_channels[bank]]);
1038                         for(int j = 0; j < sound_div; j++) {
1039                                 *p++ = *q;
1040                                 chp++;
1041                         }
1042                         if(sound_mod == 0) {
1043                                 q++;
1044                         } else if(chp < (expect_channels - sound_mod - 1)) {
1045                                 q++;
1046                         } else {
1047                                 for(int j = 0; j < sound_mod; j++) {
1048                                         *p++ = *q;
1049                                 }
1050                                 q++;
1051                         }
1052                         sound_in_readptr[bank] = sound_in_readptr[bank] + 1;
1053                         if(sound_in_readptr[bank] >= sound_in_samples[bank]) sound_in_readptr[bank] = 0;
1054                         n_samples++;
1055                         sound_in_write_size[bank] = sound_in_write_size[bank] - 1;
1056                 }
1057                                 
1058         }
1059         // ToDo: UnLock Mutex
1060         // Got to TMP Buffer
1061         if(expect_rate == sound_in_rate[bank]) {
1062                 int32_t* p = tmpbuf;
1063                 int32_t* q = dst;
1064                 int32_t tval;
1065                 for(int i = 0; i < n_samples; i++) {
1066                         for(int j = 0; j < expect_channels; j++) {
1067                                 tval = *q;
1068                                 tval = tval + *p;
1069                                 if(tval >= 32768) tval = 32767;
1070                                 if(tval < -32768) tval = -32768;
1071                                 *q = tval;
1072                                 q++;
1073                                 p++;
1074                         }
1075                 }
1076                 return n_samples;
1077         } else if(expect_rate > sound_in_rate[bank]) {
1078                 int32_t* p = tmpbuf;
1079                 int32_t* q = dst;
1080                 int32_t tval;
1081                 // ToDo: Interpollate
1082                 gave_samples = 0;
1083                 for(int i = 0; i < n_samples;) {
1084                         for(int j = 0; j < expect_channels; j++) {
1085                                 tval = *q;
1086                                 tval = tval + p[j];
1087                                 if(tval >= 32768) tval = 32767;
1088                                 if(tval < -32768) tval = -32768;
1089                                 *q = tval;
1090                                 q++;
1091                         }
1092                         sound_in_read_mod[bank] = sound_in_read_mod[bank] + sound_in_rate[bank];
1093                         if(sound_in_read_mod[bank] >= expect_rate) {
1094                                 sound_in_read_mod[bank] = sound_in_read_mod[bank] - expect_rate;
1095                                 p = p + expect_channels;
1096                                 i++;
1097                                 gave_samples++;
1098                         }
1099                 }
1100                 return gave_samples + 1;
1101         } else { // expect_rate < sound_in_rate[bank]
1102                 int32_t* p = tmpbuf;
1103                 int32_t* q = dst;
1104                 int32_t tval;
1105                 // ToDo: Interpollate
1106                 gave_samples = 0;
1107                 for(int i = 0; i < n_samples; i++) {
1108                         if(i == 0) {
1109                                 for(int j = 0; j < expect_channels; j++) {
1110                                         tval = *q;
1111                                         tval = tval + p[j];
1112                                         if(tval >= 32768) tval = 32767;
1113                                         if(tval < -32768) tval = -32768;
1114                                         *q = tval;
1115                                         q++;
1116                                 }
1117                         }
1118                         p = p + expect_channels;
1119                         sound_in_read_mod[bank] = sound_in_read_mod[bank] + expect_rate;
1120                         if(sound_in_read_mod[bank] >= sound_in_rate[bank]) {
1121                                 sound_in_read_mod[bank] = sound_in_read_mod[bank] - sound_in_rate[bank];
1122                                 for(int j = 0; j < expect_channels; j++) {
1123                                         tval = *q;
1124                                         tval = tval + p[j];
1125                                         if(tval >= 32768) tval = 32767;
1126                                         if(tval < -32768) tval = -32768;
1127                                         *q = tval;
1128                                         q++;
1129                                 }
1130                                 gave_samples++;
1131                         }
1132                 }
1133                 return gave_samples + 1;
1134         }
1135         return 0;
1136 }
1137 void EVENT::request_skip_frames()
1138 {
1139         next_skip = true;
1140 }
1141
1142 bool EVENT::is_frame_skippable()
1143 {
1144         bool value = next_skip;
1145         
1146         if(sound_changed || (prev_skip && !next_skip)) {
1147                 dont_skip_frames = (int)frames_per_sec;
1148         }
1149         if(dont_skip_frames > 0) {
1150                 value = false;
1151                 dont_skip_frames--;
1152         }
1153         prev_skip = next_skip;
1154         next_skip = false;
1155         sound_changed = false;
1156         
1157         return value;
1158 }
1159
1160 void EVENT::update_config()
1161 {
1162         if(power != config.cpu_power) {
1163                 power = config.cpu_power;
1164                 cpu_accum = 0;
1165         }
1166 }
1167
1168 #define STATE_VERSION   4
1169
1170 bool EVENT::process_state(FILEIO* state_fio, bool loading)
1171 {
1172         if(!state_fio->StateCheckUint32(STATE_VERSION)) {
1173                 return false;
1174         }
1175         if(!state_fio->StateCheckInt32(this_device_id)) {
1176                 return false;
1177         }
1178         if(!state_fio->StateCheckInt32(dcount_cpu)) {
1179                 return false;
1180         }
1181         for(int i = 0; i < dcount_cpu; i++) {
1182                 state_fio->StateValue(d_cpu[i].cpu_clocks);
1183                 state_fio->StateValue(d_cpu[i].update_clocks);
1184                 state_fio->StateValue(d_cpu[i].accum_clocks);
1185         }
1186         state_fio->StateValue(frame_clocks);
1187         state_fio->StateArray(vline_clocks, sizeof(vline_clocks), 1);
1188         state_fio->StateValue(event_remain);
1189         state_fio->StateValue(event_extra);
1190         state_fio->StateValue(cpu_remain);
1191         state_fio->StateValue(cpu_accum);
1192         state_fio->StateValue(cpu_done);
1193         state_fio->StateValue(event_clocks);
1194         for(int i = 0; i < MAX_EVENT; i++) {
1195                 if(loading) {
1196                         event[i].device = vm->get_device(state_fio->FgetInt32_LE());
1197                 } else {
1198                         state_fio->FputInt32_LE(event[i].device != NULL ? event[i].device->this_device_id : -1);
1199                 }
1200                 state_fio->StateValue(event[i].event_id);
1201                 state_fio->StateValue(event[i].expired_clock);
1202                 state_fio->StateValue(event[i].loop_clock);
1203                 state_fio->StateValue(event[i].accum_clocks);
1204                 state_fio->StateValue(event[i].active);
1205                 if(loading) {
1206                         event[i].next = (event_t *)get_event(state_fio->FgetInt32_LE());
1207                         event[i].prev = (event_t *)get_event(state_fio->FgetInt32_LE());
1208                 } else {
1209                         state_fio->FputInt32_LE(event[i].next != NULL ? event[i].next->index : -1);
1210                         state_fio->FputInt32_LE(event[i].prev != NULL ? event[i].prev->index : -1);
1211                 }
1212         }
1213         if(loading) {
1214                 first_free_event = (event_t *)get_event(state_fio->FgetInt32_LE());
1215                 first_fire_event = (event_t *)get_event(state_fio->FgetInt32_LE());
1216         } else {
1217                 state_fio->FputInt32_LE(first_free_event != NULL ? first_free_event->index : -1);
1218                 state_fio->FputInt32_LE(first_fire_event != NULL ? first_fire_event->index : -1);
1219         }
1220         state_fio->StateValue(frames_per_sec);
1221         state_fio->StateValue(next_frames_per_sec);
1222         state_fio->StateValue(lines_per_frame);
1223         state_fio->StateValue(next_lines_per_frame);
1224         state_fio->StateArray(dev_need_mix, sizeof(dev_need_mix), 1);
1225         state_fio->StateValue(need_mix);
1226         
1227         // post process
1228         if(loading) {
1229                 if(sound_buffer) {
1230                         memset(sound_buffer, 0, sound_samples * sizeof(uint16_t) * 2);
1231                 }
1232                 if(sound_tmp) {
1233                         memset(sound_tmp, 0, sound_tmp_samples * sizeof(int32_t) * 2);
1234                 }
1235                 buffer_ptr = 0;
1236                 mix_counter = 1;
1237                 mix_limit = (int)((double)(emu->get_sound_rate() / 2000.0));  // per 0.5ms.
1238         }
1239         return true;
1240 }
1241
1242 void* EVENT::get_event(int index)
1243 {
1244         if(index >= 0 && index < MAX_EVENT) {
1245                 return &event[index];
1246         }
1247         return NULL;
1248 }