3 #include "blargg_source.h"
5 /* NOTE: Removed unused chips ~ gama */
7 blargg_err_t
Opl_init( struct Opl_Apu
* this, long clock
, long rate
, blip_time_t period
, enum opl_type_t type
)
9 Synth_init( &this->synth
);
14 this->period_
= period
;
15 Opl_set_output( this, 0 );
16 Opl_volume( this, 1.0 );
23 OPLL_new ( &this->opll
, clock
, rate
);
24 OPLL_reset_patch( &this->opll
, OPLL_2413_TONE
);
27 OPLL_new ( &this->opll
, clock
, rate
);
28 OPLL_reset_patch( &this->opll
, OPLL_VRC7_TONE
);
31 OPL_init( &this->opl
, this->opl_memory
, sizeof this->opl_memory
);
32 OPL_setSampleRate( &this->opl
, rate
, clock
);
33 OPL_setInternalVolume(&this->opl
, 1 << 13);
41 void Opl_shutdown( struct Opl_Apu
* this )
49 OPLL_delete( &this->opll
);
51 case type_msxaudio
: break;
55 void Opl_reset( struct Opl_Apu
* this )
67 OPLL_reset( &this->opll
);
68 OPLL_setMask( &this->opll
, 0 );
71 OPL_reset( &this->opl
);
76 static void run_until( struct Opl_Apu
* this, blip_time_t end_time
);
77 void Opl_write_data( struct Opl_Apu
* this, blip_time_t time
, int data
)
79 run_until( this, time
);
86 OPLL_writeIO( &this->opll
, 0, this->addr
);
87 OPLL_writeIO( &this->opll
, 1, data
);
90 OPL_writeReg( &this->opl
, this->addr
, data
);
95 int Opl_read( struct Opl_Apu
* this, blip_time_t time
, int port
)
97 run_until( this, time
);
104 return OPLL_read( &this->opll
, port
);
106 return OPL_readStatus( &this->opl
);
112 void Opl_end_frame( struct Opl_Apu
* this, blip_time_t time
)
114 run_until( this, time
);
115 this->next_time
-= time
;
118 Blip_set_modified( this->output_
);
121 static void run_until( struct Opl_Apu
* this, blip_time_t end_time
)
123 if ( end_time
> this->next_time
)
125 blip_time_t time_delta
= end_time
- this->next_time
;
126 blip_time_t time
= this->next_time
;
127 unsigned count
= time_delta
/ this->period_
+ 1;
135 OPLL
* opll
= &this->opll
; // cache
136 struct Blip_Buffer
* const output
= this->output_
;
139 unsigned todo
= count
;
140 if ( todo
> 1024 ) todo
= 1024;
141 short *buffer
= OPLL_update_buffer(opll
, todo
);
143 if ( output
&& buffer
)
145 int last_amp
= this->last_amp
;
147 for ( i
= 0; i
< todo
; i
++ )
149 int amp
= buffer
[i
];
150 int delta
= amp
- last_amp
;
154 Synth_offset_inline( &this->synth
, time
, delta
, output
);
156 time
+= this->period_
;
158 this->last_amp
= last_amp
;
166 struct Y8950
* opl
= &this->opl
;
167 struct Blip_Buffer
* const output
= this->output_
;
170 unsigned todo
= count
;
171 if ( todo
> 1024 ) todo
= 1024;
172 int *buffer
= OPL_updateBuffer(opl
, todo
);
174 if ( output
&& buffer
)
176 int last_amp
= this->last_amp
;
178 for ( i
= 0; i
< todo
; i
++ )
180 int amp
= buffer
[i
];
181 int delta
= amp
- last_amp
;
185 Synth_offset_inline( &this->synth
, time
, delta
, output
);
187 time
+= this->period_
;
189 this->last_amp
= last_amp
;
196 this->next_time
= time
;