-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
emu.c
210 lines (194 loc) · 5.1 KB
/
emu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/****************************************************
*Part of SPC2IT, read readme.md for more information*
****************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sound.h"
#include "spc2ittypes.h"
#include "sneese_spc.h"
u8 SPC_DSP[256];
u8 SPCRAM[65536];
u32 Map_Address;
u32 SPC_DSP_DATA;
u32 Map_Byte;
u8 In_CPU;
u32 SPC_CPU_cycle_divisor;
u32 SPC_CPU_cycle_multiplicand;
u32 SPC_CPU_cycles;
u32 SPC_CPU_cycles_mul;
u32 sound_cycle_latch;
void (*SPC_Write_DSP_Hook)(u8); // function pointer
// ID tag stuff
s32 SPCtime;
SPCFileInformation *SPCInfo;
static s32 LoadZState(char *fn)
{
SPCFile *sFile = calloc(1, sizeof(SPCFile));
if (sFile == NULL)
{
printf("Error: could not allocate memory for SPCFile struct\n");
exit(1);
}
FILE *f = fopen(fn, "rb");
if (f == NULL)
{
printf("Error: can't open file\n");
exit(1);
}
fseek(f, 0, SEEK_SET);
fread(sFile, sizeof(SPCFile), 1, f);
fclose(f);
if (strncmp("SNES-SPC700 Sound File Data", sFile->FileTag, 27))
{
printf("Error: invalid file format\n");
exit(1);
}
memcpy(&active_context->PC.w, sFile->Registers.PC, 2);
active_context->YA.b.l = sFile->Registers.A;
active_context->X = sFile->Registers.X;
active_context->YA.b.h = sFile->Registers.Y;
active_context->SP = 0x100 + sFile->Registers.SP;
active_context->PSW = sFile->Registers.PSW;
memcpy(SPCRAM, sFile->RAM, 65536);
memcpy(SPC_DSP, sFile->DSPBuffer, 128);
SPCInfo = calloc(1, sizeof(SPCFileInformation));
if (SPCInfo == NULL)
{
printf("Error: could not allocate memory for SPCInfo struct\n");
exit(1);
}
memcpy(SPCInfo, &sFile->Information, sizeof(SPCFileInformation));
char songLen[4];
strncpy(songLen, SPCInfo->SongLength, 3);
if (songLen[0] >= 0)
SPCtime = atoi(songLen);
else
SPCtime = 0;
if (0 == (SPC_CTRL & 0x80))
active_context->FFC0_Address = SPCRAM;
active_context->timers[0].target = (u8)(SPCRAM[0xFA] - 1) + 1;
active_context->timers[1].target = (u8)(SPCRAM[0xFB] - 1) + 1;
active_context->timers[2].target = (u8)(SPCRAM[0xFC] - 1) + 1;
active_context->timers[0].counter = SPCRAM[0xFD] & 0xF;
active_context->timers[1].counter = SPCRAM[0xFE] & 0xF;
active_context->timers[2].counter = SPCRAM[0xFF] & 0xF;
active_context->PORT_R[0] = SPCRAM[0xF4];
active_context->PORT_R[1] = SPCRAM[0xF5];
active_context->PORT_R[2] = SPCRAM[0xF6];
active_context->PORT_R[3] = SPCRAM[0xF7];
SPC_CPU_cycle_multiplicand = 1;
SPC_CPU_cycle_divisor = 1;
SPC_CPU_cycles_mul = 0;
spc_restore_flags();
free(sFile);
return (0);
}
// PUBLIC (non-static) functions
s32 SPCInit(char *fn)
{
Reset_SPC();
if (LoadZState(fn))
return 1;
return 0;
}
void SPCAddWriteDSPCallback(void (*ToAddCallback)(u8))
{
SPC_Write_DSP_Hook = ToAddCallback;
}
// Called from SPC 700 engine
void DisplaySPC()
{
}
void InvalidSPCOpcode()
{
exit(-1);
}
void SPC_READ_DSP()
{
if ((SPC_DSP_ADDR & 0xf) == 8) // ENVX
SPC_DSP[SPC_DSP_ADDR] = SNDDoEnv(SPC_DSP_ADDR >> 4) >> 24;
}
void SPC_WRITE_DSP()
{
s32 addr_lo = SPC_DSP_ADDR & 0xF, addr_hi = SPC_DSP_ADDR >> 4;
switch (addr_lo)
{
case 3: // Pitch hi
SPC_DSP_DATA &= 0x3F;
break;
case 5: // ADSR1
if ((SPC_DSP[0x4C] & (1 << addr_hi)) && ((SPC_DSP_DATA & 0x80) != (SPC_DSP[SPC_DSP_ADDR] & 0x80)))
{
s32 i;
// First of all, in case anything was already
// going on, finish it up
SNDDoEnv(addr_hi);
if (SPC_DSP_DATA & 0x80)
{
// switch to ADSR--not sure what to do
i = SPC_DSP[(addr_hi << 4) + 6];
SNDvoices[addr_hi].envstate = ATTACK;
SNDvoices[addr_hi].ar = SPC_DSP_DATA & 0xF;
SNDvoices[addr_hi].dr = SPC_DSP_DATA >> 4 & 7;
SNDvoices[addr_hi].sr = i & 0x1f;
SNDvoices[addr_hi].sl = i >> 5;
}
else
{
// switch to a GAIN mode
i = SPC_DSP[(addr_hi << 4) + 7];
if (i & 0x80)
{
SNDvoices[addr_hi].envstate = i >> 5;
SNDvoices[addr_hi].gn = i & 0x1F;
}
else
{
SNDvoices[addr_hi].envx = (i & 0x7F) << 24;
SNDvoices[addr_hi].envstate = DIRECT;
}
}
}
break;
case 6: // ADSR2
// Finish up what was going on
SNDDoEnv(addr_hi);
SNDvoices[addr_hi].sr = SPC_DSP_DATA & 0x1f;
SNDvoices[addr_hi].sl = SPC_DSP_DATA >> 5;
break;
case 7: // GAIN
if ((SPC_DSP[0x4C] & (1 << addr_hi)) && (SPC_DSP_DATA != SPC_DSP[SPC_DSP_ADDR]) &&
!(SPC_DSP[(addr_hi << 4) + 5] & 0x80))
{
if (SPC_DSP_DATA & 0x80)
{
// Finish up what was going on
SNDDoEnv(addr_hi);
SNDvoices[addr_hi].envstate = SPC_DSP_DATA >> 5;
SNDvoices[addr_hi].gn = SPC_DSP_DATA & 0x1F;
}
else
{
SNDvoices[addr_hi].envx = (SPC_DSP_DATA & 0x7F) << 24;
SNDvoices[addr_hi].envstate = DIRECT;
}
}
break;
// These are general registers
case 12: // 0xc
switch (addr_hi)
{
case 4: // Key on
SNDNoteOn(SPC_DSP_DATA);
SPC_DSP_DATA = SPC_DSP[0x4C];
break;
case 5: // Key off
SNDNoteOff(SPC_DSP_DATA);
SPC_DSP_DATA = 0;
break;
}
break;
}
SPC_DSP[SPC_DSP_ADDR] = SPC_DSP_DATA;
}