This repository has been archived by the owner on Nov 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPU.CPP
executable file
·238 lines (186 loc) · 4.91 KB
/
CPU.CPP
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//CPU handler starts/stops cpu etc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <process.h>
#include "dd.h"
#include "types.h"
#include "message.h"
#include "file.h"
#include "keyb.h"
#include "m6502.h"
#include "nes.h"
#include "nesvideo.h"
#include "slist.h"
#include "prof.h"
int HBLANKCYCLES=115;
int VBLANKLINES=33;
int TIMERSPEED=60;
byte CPUpaused=0; //flag for when cpu is paused
byte CPURunning=0; //flag for when cpu is running
int CPUtickframe=0; //tick a frame at a time
byte CPUtrace=0; //trace one instruction
volatile int cycles;
void startframe();
void startvblank();
#include "timing.h"
extern volatile byte frame;
//are we currently in a virtual frame ? (emulating)
volatile byte inemu=0;
void m_showmessages();
void disasm(char *s,byte *base,word pc);
void cpuabort()
{
word addr=m6502pc;
msg.error("Unrecognized opcode %X at addr: %X",
(addr<0x8000) ? ram[addr] : m6502rom[addr],addr);
inemu=0;
m_stop();
m_showmessages();
pf.cpu_timer.leave();
int numrom8k=numrom*2;
for (int i=0; i<numrom8k; i++)
{
char s[128];
disasm(s,((byte *)ROM)+i*0x2000,m6502pc&0x1FFF);
msg.printf(1,"bank[%X] %s",i,s);
}
}
//get current virtual scanline being draw
//line 0 = scanline 8 line 224=scanline 232
int getscanline()
{
if (!frame) return 0;
// return (m6502clockticks+HBLANKCYCLES/2)/HBLANKCYCLES;
return m6502clockticks/HBLANKCYCLES;
}
//--------------------
//irq stuff
int doIRQ=0;
int IRQline=0;
void setIRQ(int line)
{
if (line<=0 || line>240) {doIRQ=0; IRQline=0; return;}
doIRQ=1;
if (frame)
{
IRQline=line+getscanline();
cyclesRemaining=0; //abort!
} else IRQline=line-STARTFRAME;
// msg.printf(1,"line=%d IRQline=%X line=%d",line,IRQline,getscanline());
}
//--------------------------------------
//emu heartbeat (once per virtual frame)
void tickemu()
{
if (CPUtrace)
{
char s[128];
// disasm((char *)s,(byte *)((m6502pc<0x8000) ? ram : m6502rom),m6502pc);
disasm(s,ram,m6502pc);
msg.printf(1,s);
m6502exec(1);
CPUtrace=0;
return;
}
if (inemu) return; inemu=1;
pf.cpu_timer.enter();
//reset event list
nv->sl.reset();
//execute during vblank
startvblank();
// msg.printf(1,"startvblank");
m6502clockticks=0;
if (ram[0x2000]&0x80) m6502nmi(); //generate vblank?
if (m6502exec(VBLANKCYCLES)!=0x10000) {cpuabort(); return;}
//execute during virtual frame...
m6502clockticks=0; //STARTFRAME*HBLANKCYCLES; //reset clock ticks
startframe();
// msg.printf(1,"startframe");
if (!doIRQ || !IRQline)
{ //dont bother with IRQ
if (m6502exec(FRAMECYCLES)!=0x10000) {cpuabort(); return;}
} else
{ //do IRQ
int framecycles=FRAMECYCLES;
while (m6502clockticks<framecycles) //until we've done all the cycles
{
int line=getscanline(); //current scanline
if (line<IRQline)
{
m6502exec((IRQline-line)*HBLANKCYCLES); //execute until IRQ
m6502int(); //int!
nv->sl.addevent(SE_IRQLINE,0);
// msg.printf(1,"IRQ");
} else m6502exec(framecycles-m6502clockticks); //just execute rest of frame
}
}
//create context list based on event list
nv->sc.create(nv->sl);
//done
pf.cpu_timer.leave();
pf.cycles.add(CYCLESPERTICK);
pf.vframes.inc();
inemu=0;
}
//void m_execute()
void m_reset()
{
if (!romloaded) {msg.error("ROM not loaded"); return;}
// if (CPURunning) {msg.error("CPU already running"); return;}
if (resetNEShardware()!=0)
{ msg.error("Unable to reset NES hardware"); return;}
setIRQ(0); //disable IRQs
msg.printf(3,"CPU emulation started");
// msg.printf(1,"%X",m6502pc);
CPURunning=1;
CPUtrace=0;
CPUpaused=0;
}
void m_stop()
{
if (!romloaded) return;
CPUpaused=0;
if (CPURunning) msg.printf(3,"CPU emulation stopped");
CPURunning=0;
CPUtrace=0;
inemu=0;
//copy battery backed ram
if (batterymem) batterymem->copyfromRAM();
}
void m_resume()
{
if (!romloaded) {msg.error("ROM not loaded"); return;}
if (CPURunning) {msg.error("CPU already running"); return;}
if (!CPUpaused) {msg.error("CPU not paused"); return;}
msg.printf(3,"CPU emulation resumed");
CPURunning=1;
CPUpaused=0;
}
void m_pause()
{
if (!romloaded) return;
if (!CPURunning) {msg.error("CPU not running"); return;}
CPUpaused=1;
CPURunning=0;
msg.printf(3,"CPU emulation paused");
}
void m_advanceframe()
{
if (!romloaded) return;
if (!CPURunning && !CPUpaused) m_reset();
if (!CPUpaused) m_pause();
if (!CPUpaused) return;
CPUtickframe++;
}
void m_step()
{
if (!romloaded) return;
if (!CPURunning && !CPUpaused) m_reset();
if (!CPUpaused) m_pause();
if (!CPUpaused) return;
CPUtrace=1;
}
//-----------------------------------------------------------
//-----------------------------------------------------------