Bug Summary

File:/home/anal/dolphin-emu/Source/Core/Core/Src/HW/DSPHLE/UCodes/UCode_Zelda_Synth.cpp
Location:line 67, column 15
Description:The left operand of '+' is a garbage value

Annotated Source Code

1// Copyright 2013 Dolphin Emulator Project
2// Licensed under GPLv2
3// Refer to the license.txt file included.
4
5#include <math.h>
6
7#include "UCodes.h"
8#include "UCode_Zelda.h"
9#include "AudioCommon.h"
10
11#include "Mixer.h"
12
13void CUCode_Zelda::RenderSynth_RectWave(ZeldaVoicePB &PB, s32* _Buffer, int _Size)
14{
15 float _ratioFactor = 32000.0f / (float)soundStream->GetMixer()->GetSampleRate();
16 u32 _ratio = (PB.RatioInt << 16);
17 s64 ratio = (s64)((_ratio * _ratioFactor) * 16);
18 s64 TrueSamplePosition = PB.CurSampleFrac;
19
20 // PB.Format == 0x3 -> Rectangular Wave, 0x0 -> Square Wave
21 unsigned int mask = PB.Format ? 3 : 1;
1
'?' condition is false
22 // int shift = PB.Format ? 2 : 1; // Unused?
23
24 u32 pos[2] = {0, 0};
25 int i = 0;
26
27 if (PB.KeyOff != 0)
2
Taking false branch
28 return;
29
30 if (PB.NeedsReset)
3
Taking false branch
31 {
32 PB.RemLength = PB.Length - PB.RestartPos;
33 PB.CurAddr = PB.StartAddr + (PB.RestartPos << 1);
34 PB.ReachedEnd = 0;
35 }
36
37_lRestart:
38 if (PB.ReachedEnd)
4
Taking false branch
39 {
40 PB.ReachedEnd = 0;
41
42 if (PB.RepeatMode == 0)
43 {
44 PB.KeyOff = 1;
45 PB.RemLength = 0;
46 PB.CurAddr = PB.StartAddr + (PB.RestartPos << 1) + PB.Length;
47 return;
48 }
49 else
50 {
51 PB.RestartPos = PB.LoopStartPos;
52 PB.RemLength = PB.Length - PB.RestartPos;
53 PB.CurAddr = PB.StartAddr + (PB.RestartPos << 1);
54 pos[1] = 0; pos[0] = 0;
55 }
56 }
57
58 while(i < _Size)
5
Assuming 'i' is < '_Size'
6
Loop condition is true. Entering loop body
59 {
60 s16 sample = ((pos[1] & mask) == mask) ? 0xc000 : 0x4000;
7
'?' condition is false
61
62 TrueSamplePosition += (ratio >> 16);
63
64 _Buffer[i++] = (s32)sample;
65
66 (*(u64*)&pos) += ratio;
67 if ((pos[1] + ((PB.CurAddr - PB.StartAddr) >> 1)) >= PB.Length)
8
The left operand of '+' is a garbage value
68 {
69 PB.ReachedEnd = 1;
70 goto _lRestart;
71 }
72 }
73
74 if (PB.RemLength < pos[1])
75 {
76 PB.RemLength = 0;
77 PB.ReachedEnd = 1;
78 }
79 else
80 {
81 PB.RemLength -= pos[1];
82 }
83
84 PB.CurSampleFrac = TrueSamplePosition & 0xFFFF;
85}
86
87void CUCode_Zelda::RenderSynth_SawWave(ZeldaVoicePB &PB, s32* _Buffer, int _Size)
88{
89 s32 ratio = (s32)ceil((float)PB.RatioInt / 3);
90 s64 pos = PB.CurSampleFrac;
91
92 for (int i = 0; i < _Size; i++)
93 {
94 pos += ratio;
95 _Buffer[i] = pos & 0xFFFF;
96 }
97
98 PB.CurSampleFrac = pos & 0xFFFF;
99}
100
101void CUCode_Zelda::RenderSynth_Constant(ZeldaVoicePB &PB, s32* _Buffer, int _Size)
102{
103 // TODO: Header, footer
104 for (int i = 0; i < _Size; i++)
105 _Buffer[i] = (s32)PB.RatioInt;
106}
107
108// A piece of code from LLE so we can see how the wrap register affects the sound
109
110inline u16 AddValueToReg(u32 ar, s32 ix)
111{
112 u32 wr = 0x3f;
113 u32 mx = (wr | 1) << 1;
114 u32 nar = ar + ix;
115 u32 dar = (nar ^ ar ^ ix) & mx;
116
117 if (ix >= 0)
118 {
119 if (dar > wr) //overflow
120 nar -= wr + 1;
121 }
122 else
123 {
124 if ((((nar + wr + 1) ^ nar) & dar) <= wr) //underflow or below min for mask
125 nar += wr + 1;
126 }
127 return nar;
128}
129
130void CUCode_Zelda::RenderSynth_WaveTable(ZeldaVoicePB &PB, s32* _Buffer, int _Size)
131{
132 u16 address;
133
134 switch(PB.Format)
135 {
136 default:
137 case 0x0004:
138 address = 0x140;
139 break;
140
141 case 0x0007:
142 address = 0x100;
143 break;
144
145 case 0x000b:
146 address = 0x180;
147 break;
148
149 case 0x000c:
150 address = 0x1c0;
151 break;
152 }
153
154 // TODO: Resample this!
155 INFO_LOG(DSPHLE, "Synthesizing the incomplete format 0x%04x", PB.Format)do { { if (LogTypes::LINFO <= 3) GenericLog(LogTypes::LINFO
, LogTypes::DSPHLE, "/home/anal/dolphin-emu/Source/Core/Core/Src/HW/DSPHLE/UCodes/UCode_Zelda_Synth.cpp"
, 155, "Synthesizing the incomplete format 0x%04x", PB.Format
); } } while (0)
;
156
157 u64 ACC0 = PB.CurSampleFrac << 6;
158
159 ACC0 &= 0xffff003fffffULL;
160
161 address = AddValueToReg(address, ((ACC0 >> 16) & 0xffff));
162 ACC0 &= 0xffff0000ffffULL;
163
164 for(int i = 0; i < 0x50; i++)
165 {
166 _Buffer[i] = m_MiscTable[address];
167
168 ACC0 += PB.RatioInt << 5;
169 address = AddValueToReg(address, ((ACC0 >> 16) & 0xffff));
170
171 ACC0 &= 0xffff0000ffffULL;
172 }
173
174 ACC0 += address << 16;
175 PB.CurSampleFrac = (ACC0 >> 6) & 0xffff;
176}
177
178