summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Pavone <pavone@retrodev.com>2014-02-11 12:45:43 -0800
committerMichael Pavone <pavone@retrodev.com>2014-02-11 12:45:43 -0800
commit94a138521f2ac37d238ca75ee0a052587fcfad87 (patch)
tree5ed56adcdd1909f4754282053851e8bef4e26e6a
parent8a2df056c2612b1ce4748b46a194c3e07722986c (diff)
Fix overflow handling on FM channel output
-rw-r--r--ym2612.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/ym2612.c b/ym2612.c
index 978dc95..feb3a18 100644
--- a/ym2612.c
+++ b/ym2612.c
@@ -470,9 +470,16 @@ void ym_run(ym2612_context * context, uint32_t to_cycle)
context->audio_buffer[context->buffer_pos] = 0;
context->audio_buffer[context->buffer_pos + 1] = 0;
for (int i = 0; i < NUM_CHANNELS; i++) {
- int16_t value = context->channels[i].output & 0x3FE0;
- if (value & 0x2000) {
- value |= 0xC000;
+ int16_t value = context->channels[i].output;
+ if (value > 0x1FE0) {
+ value = 0x1FE0;
+ } else if (value < -0x1FF0) {
+ value = -0x1FF0;
+ } else {
+ value &= 0x3FE0;
+ if (value & 0x2000) {
+ value |= 0xC000;
+ }
}
if (context->channels[i].logfile) {
fwrite(&value, sizeof(value), 1, context->channels[i].logfile);