Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
RishiRecon
GitHub Repository: RishiRecon/exploits
Path: blob/main/misc/emulator/xnes/snem/exec.c
28515 views
1
#include "snes.h"
2
3
int padready;
4
unsigned short oldspcpc;
5
int spcpchi=1;
6
unsigned char spuram[0x10080];
7
int cycs;
8
int soundupdate;
9
unsigned short sobjaddr;
10
int curline;
11
int curframe;
12
int spcemu;
13
unsigned char spctctrl;
14
unsigned char spctimer[3],spclatch[3],spccount[3];
15
int spctimer2[3];
16
17
struct
18
{
19
union
20
{
21
unsigned short ya;
22
struct
23
{
24
unsigned char a,y;
25
} b;
26
} ya;
27
unsigned char x;
28
unsigned char s;
29
unsigned short pc;
30
struct
31
{
32
int c,z,h,p,v,n;
33
} p;
34
unsigned short opc[8];
35
} spc;
36
37
unsigned char spuram[0x10080];
38
39
void (*opcodes[256][5])();
40
void (*spcopcodes[256])();
41
42
int slines;
43
int irqon,fastrom,nmipend,palf;
44
int cycles=0,spccycles=0;
45
unsigned char opcode;
46
int ins;
47
int gcycs=0,spccycs=0;
48
49
#define readspc(a) ((a&0xFF00)?spuram[a]:readspcl(a))
50
51
int cyclookup[3][10]=
52
{
53
{0,8,16,24,32,40,48,56,64,72},
54
{0,6,12,18,24,30,36,42,48,54},
55
{0,10,20,30,40,50,60,70,80,90},
56
};
57
58
/*Main loop - switches between 65816,SPC,and PPU
59
VERY loose timing here*/
60
void mainloop(int linenum)
61
{
62
int lines;
63
int ocycles;
64
for (lines=0;lines<linenum;lines++)
65
{
66
/*Deal with IRQs*/
67
curline=slines;
68
gcycs+=1366; //best timing I could find
69
spccycs+=1366;
70
/*Execute two instructions before processing NMI - helps Chrono Trigger and Puzzle Bobble*/
71
opcode=readmem(pbr,pc); pc++;
72
opcodes[opcode][mode]();
73
opcode=readmem(pbr,pc); pc++;
74
opcodes[opcode][mode]();
75
opcode=readmem(pbr,pc); pc++;
76
opcodes[opcode][mode]();
77
opcode=readmem(pbr,pc); pc++;
78
opcodes[opcode][mode]();
79
ins+=2;
80
// if (nmipend&&nmiena) { nmipend=0; nmi65816(); }
81
while (gcycs>0)
82
{
83
cycles=0;
84
opcode=readmem(pbr,pc); pc++;
85
opcodes[opcode][mode]();
86
gcycs-=cyclookup[fastrom][-cycles];
87
if (irqon && !struct_p.i) int65816();
88
if (spcemu)
89
{
90
while (spccycs>gcycs)
91
{
92
opcode=readspc(spc.pc); spc.pc++;
93
spcopcodes[opcode]();
94
spccycs-=cyclookup[2][cycs];
95
}
96
}
97
}
98
UPDATE_SOUND;
99
if (spcemu)
100
{
101
if (spctctrl&1)
102
{
103
spctimer2[0]++;
104
if (spctimer2[0]==2)
105
{
106
spctimer2[0]=0;
107
spctimer[0]++;
108
if (spctimer[0]==spclatch[0])
109
{
110
spctimer[0]=0;
111
spccount[0]++;
112
}
113
}
114
}
115
if (spctctrl&2)
116
{
117
spctimer2[1]++;
118
if (spctimer2[1]==2)
119
{
120
spctimer2[1]=0;
121
spctimer[1]++;
122
if (spctimer[1]==spclatch[1])
123
{
124
spctimer[1]=0;
125
spccount[1]++;
126
}
127
}
128
}
129
if (spctctrl&4)
130
{
131
spctimer[2]+=4;
132
if (spctimer[2]>=spclatch[2])
133
{
134
spctimer[2]=0;
135
spccount[2]++;
136
}
137
}
138
}
139
if (slines==231) nmiocc=nmipend=1;
140
if (slines==231 && nmiena) nmi65816();
141
if (slines==224)
142
{
143
vbl=1;
144
vblankhdma();
145
curframe++;
146
padready=1;
147
}
148
if (slines==227) padready=0;
149
if (slines<224) doline(slines);
150
if ((vertint && (slines==vertline)) || (horint && !vertint))
151
irqon=1;
152
else
153
irqon=0;
154
slines++;
155
if (palf)
156
{
157
if (slines==312)
158
{
159
slines=0;
160
objaddr=sobjaddr;
161
vbl=nmiocc=nmipend=0;
162
decodesprites();
163
}
164
}
165
else
166
{
167
if (slines==262)
168
{
169
slines=0;
170
objaddr=sobjaddr;
171
vbl=nmiocc=nmipend=0;
172
decodesprites();
173
}
174
}
175
}
176
}
177
178