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
/
NES.OLD
executable file
·577 lines (458 loc) · 12 KB
/
NES.OLD
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//emulation of NES registers....
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <process.h>
#include "types.h"
#include "message.h"
#include "file.h"
#include "keyb.h"
#define FREE(x) if (x) {free(x); x=0;}
#define DELETE(x) if (x) {delete x; x=0;}
#include "m6502.h"
#include "timing.h"
#include "nesvideo.h"
#include "nes.h"
#include "input.h"
#include "dd.h"
extern struct m6502MemoryWrite NESwrite[];
extern struct m6502MemoryRead NESread[];
extern int blah,blah2;
//---------------------------------
//hardware internal registers
word ppumemaddr; //address for next write in ppu memory
char ppureadfuckup; //first mem fuck up
byte spritememaddr; //next write in sprite memory
/*
byte scrollx;
byte scrolly;
int scrollscanline;
int nttoshow; //name table to show....
*/
volatile byte vblank; //are we in a vblank ?
volatile byte hitflag; //are we in hitflag?
volatile byte frame; //are we scanning frame?
byte mirroring; //ppu name table mirroring
byte VROMswitch;
byte KONAMIswitch;
byte oldVROMswitch;
//-------------------------
//reset nes
int resetNEShardware()
{
if (!NESinitialized) return -1;
// scrollx=scrolly=0;
// nttoshow=0;
sl.reset();
ppumemaddr=0; ppureadfuckup=0;
spritememaddr=0;
//erase memory
ppu->clear();
memset(ram,0,0x10000);
memset(spritemem,0,256);
//set default mirroring
mirroring=romhdr.mirroring;
if (!numrom) {msg.error("No rom data!"); return -1;}
//copy last page of rom to end of cpu address space
memcpy(ram+0xC000,ROM[numrom-1],sizeof(ROMPAGE));
//copy first page of rom to 0x8000
if (numrom>=2) memcpy(ram+0x8000,ROM,sizeof(ROMPAGE));
//copy vrom to ppu address space (if it exists)
if (numvrom>0) memcpy(ppu,VROM,sizeof(VROMPAGE));
//set up 6502 CPU emulator
m6502Base=(uchar *)ram; //set base of emu
m6502reset(); //reset cpu
m6502MemoryRead=NESread; //set r/w trap handlers
m6502MemoryWrite=NESwrite;
//MMC type
switch (romhdr.banktype)
{
case MMC_NONE: //no mapping chips
break;
case MMC_SEQ: //sequential mapper
msg.printf(2,"Sequential mapper initialized");
break;
case MMC_VROMSWITCH:
VROMswitch=0;
oldVROMswitch=-1;
msg.printf(2,"VROM switch initialized");
break;
case MMC_KONAMI:
KONAMIswitch=0;
mirroring=VMIRROR;
msg.printf(2,"Konami mapper initialized");
break;
default:
msg.error("Unsupported mapping type");
// return -1;
}
//reset emulated video
nv->resetpalette();
nv->resetpatterncache();
CPUpaused=0;
msg.printf(1,"NES hardware reset");
return 0;
}
//--------------------------------
//PPU emulation
NES_ppumemory::NES_ppumemory()
{
clear();
msg.printf(2,"%dK PPU address space created",sizeof(*this)/1024);
}
void NES_ppumemory::clear()
{
memset(this,0,sizeof(*this));
}
void NES_ppumemory::write(word a,byte d)
{
//msg.printf(1,"ppu data write %X %X",ppumemaddr,d);
if (((byte *)this)[a]==d) return; //no change
//pallete write ?
if (a>=0x3F00 && a<0x3F20)
{
// msg.printf(1,"pal[%X]=%X",a-0x3f00,d);
if (!(a&0xF)) //it's a background palette...
{
for (int i=0; i<8; i++)
{
((byte *)this)[0x3F00+i*4]=d;
nv->palupdateidx[i*4]=1;
}
nv->paletteupdated=1;
} else //normal palette
if (a&3)
{
((byte *)this)[a]=d; //write it then...
nv->palupdateidx[a-0x3F00]=1;
nv->paletteupdated=1;
}
return;
}
((byte *)this)[a]=d; //write byte to ppu memory
//pattern table was written to ?
if (a<0x2000)
{
nv->ptnupdateidx[a/0x1000][(a&0xFFF)/sizeof(NES_pattern)]=1;
nv->patternupdated=1;
// blah2++;
// msg.printf(1,"pattern %d.%d updated",a/0x1000,(a&0xFFF)/sizeof(NES_pattern));
return;
}
//name/attribute table was written to..
if (a<0x3000)
{
//we must mirror it
if (mirroring==HMIRROR)
{
((byte *)this)[a^0x400]=d; //horizontal mirroring
nv->ntc[(a&0x800) ? 1 : 0]->update(a&0x3FF);
}
else
{
((byte *)this)[a^0x800]=d; //vertical mirroring
nv->ntc[(a&0x400) ? 1 : 0]->update(a&0x3FF);
}
return;
}
}
byte NES_ppumemory::read(word a)
{
//msg.printf(1,"ppu data read %X",ppumemaddr);
switch (romhdr.banktype)
{
case MMC_VROMSWITCH:
if (a<0x2000) return VROM[VROMswitch][a]; //if within rom.....
default: return ((byte *)this)[a];
}
}
//---------------------------------------
byte currentnatable=0;
void scrolllist::reset()
{
if (spnum>0) sp[0]=sp[spnum]; //use sp gotten last time
// sp[0].reset();
//reset
sp[0].natable=currentnatable;
sp[0].line=0; //start at line 0
flip=0; //reset flip
spnum=0; //first scrollpos
msg.printf(2,"reset");
}
void scrolllist::writenatable(byte d) {currentnatable=d;}
void scrolllist::writescroll(byte d)
{
sp[spnum].line=getscanline();
//msg.printf(3,"line=%d val=%d",sp[spnum].line,d);
// if (sp[spnum].line) sp[spnum].line++;
if (!flip)
{
if (!spnum || sp[spnum-1].line<sp[spnum].line) //different scanline
{
sp[spnum].natable=currentnatable;
spnum++; //next scrollpos
sp[spnum].reset();
}
sp[spnum].x=d; //read x
}
else
{
if (d<=239) sp[spnum].y=d; //read y
// else sp[spnum].y=spnum>0 ? sp[spnum-1].y : 0;
// sp[spnum].print();
}
flip^=1;
//register it!
//if (!flip)
}
//find heights of all blocks
void scrolllist::resolve(int yw)
{
if (!spnum) spnum++;
sp[spnum].line=yw;
for (int i=0; i<spnum; i++)
sp[i].height=sp[i+1].line-sp[i].line; //find height of bank
sp[spnum-1].natable=currentnatable;
}
void scrollpos::print()
{
msg.printf(1,"pos=%d,%d line=%d height=%d",x,y,line,height);
}
void scrolllist::print()
{
for (int i=0; i<spnum; i++)
{
msg.printf(2,"%d",i);
sp[i].print();
}
}
//-----------------------------------------
void startframe()
{
frame=1; //during scanning of frame
vblank=0; //not in vblank
}
void startvblank()
{
vblank=1; //we are in vblank
hitflag=0; //reset hitflag
frame=0;
}
//-------------------------------------------------------
//memory write trap handlers
//write to lower ports
int neswrite2000(WORD a,BYTE d)
{
switch (a)
{
case 0x2000: //PPU control register 1
// msg.printf(1,"write[%X]=%X",a,d);
sl.writenatable(d&3);
ram[a]=d;
return 0;
case 0x2001: //PPU control register 2
ram[a]=d;
return 0;
case 0x2003: //sprite memory address
spritememaddr=d;
// msg.printf(1,"sprite mem addr set to %X",spritememaddr);
return 0;
case 0x2004: //sprite mem data write
// msg.printf(1,"spritewrite %d",spritememaddr);
((byte *)spritemem)[spritememaddr++]=d;
return 0;
case 0x2005: //Background scroll
sl.writescroll(d); //write to scroll list
return 0;
case 0x2006: //PPU memory address
ram[0x2006]^=1;
((byte *)&ppumemaddr)[ram[0x2006]]=d;
ppureadfuckup=1;
// if (!ram[0x2006]) msg.printf(1,"ppu mem addr set to %X",ppumemaddr);
return 0;
case 0x2007: //PPU data write
ppu->write(ppumemaddr,d);
ppumemaddr+=(ram[0x2000]&4) ? 32 : 1;
return 0;
default:
msg.printf(1,"unsupported write: %X %X",a,d);
return 0;
}
};
//write to upper ports
int neswrite4000(WORD a,BYTE d)
{
// msg.error("upper port write!!");
switch (a)
{
case 0x4014: //DMA sprite copy
// msg.printf(3,"DMA sprite copy from %X",d*0x100);
memcpy(spritemem,ram+(d*0x100),0x100); //copy memory
return 0;
case 0x4015: //sound switch
ram[0x4015]=d;
return 0;
case 0x4016: //joystick
case 0x4017: //joystick
ram[0x4016]=1; //reset joystick
ram[0x4017]=1; //reset joystick
return 0;
default:
// msg.printf(1,"unsupported write: %X %X",a,d);
return 0;
};
}
//write to expansion module
int neswrite5000(WORD a,BYTE d)
{
msg.error("expansion write!!");
return 0;
}
int neswrite0000(WORD a,BYTE d)
{
// if (a>=0x800) msg.error("weird write");
ram[a&0x7FF]=d;
return 0;
}
//write to 0x8000-0xFFFF
int neswrite8000(WORD a,BYTE d)
{
switch (romhdr.banktype)
{
case MMC_NONE: //no memory mapper...
msg.error("ROM write!!");
break;
case MMC_SEQ:
msg.error("Seq[%X]=%X",a,d);
break;
case MMC_VROMSWITCH:
if (d<numvrom) VROMswitch=d;
//else
// if (d&0x80) mirroring=VMIRROR; //??????????
// else
// msg.error("VROM[%X]=%X",a,d); //8D usually
break;
case MMC_KONAMI:
if (d<numrom)
{
if (d==KONAMIswitch) return 0;
//blah++;
KONAMIswitch=d;
memcpy(ram+0x8000,&ROM[KONAMIswitch],sizeof(ROMPAGE));
} else msg.error("KONAMI[%X]=%X",a,d);
break;
}
return 0;
}
//called before each frame is rendered
void doVROMswitch()
{
if (romhdr.banktype==MMC_VROMSWITCH)
if (VROMswitch!=oldVROMswitch)
{
blah++;
memcpy(ppu,&VROM[VROMswitch],sizeof(VROMPAGE)); //copy vrom
memset(nv->ptnupdateidx,1,256*2);
nv->patternupdated=1;
oldVROMswitch=VROMswitch;
}
}
//-------------------------------------------------------
//memory read trap handlers
WORD nesread2000(WORD a)
{
// msg.printf(3,"%X read",a);
switch (a)
{
case 0x2000: //PPU control register 1
return ram[a];
case 0x2001: //PPU control register 2
return ram[a];
case 0x2002: //PPU status register
{
byte s=0;
if (vblank) {s|=0x80; vblank=0;} //reset vblank after read
//see if we've scanned past sprite #0's y coordinate
if (!hitflag)
if (spritemem[0].y-2 < getscanline()) {s|=0x40; hitflag=1; }
// msg.printf(2,"PPU status read %X",ram[0x2002]);
return s;
}
case 0x2004: //sprite mem data read
// msg.printf(1,"spriteread %d",spritememaddr);
return ((byte *)spritemem)[spritememaddr++];
case 0x2005: msg.error("bgscroll read!"); return 0;
case 0x2007: //PPU data read
{
if (ppureadfuckup) {ppureadfuckup=0; return 0;}
byte d=ppu->read(ppumemaddr);
ppumemaddr+=(ram[0x2000]&4) ? 32 : 1;
return d;
}
default: return 0;
}
}
WORD nesread4000(WORD a)
{
// msg.error("upper port read!!");
switch (a)
{
case 0x4016: //joystick
case 0x4017:
{
int joynum=a-0x4016; //get joystick number
int stat=inputdevice[joynum]->stat; //get current status of dirs
int but=inputdevice[joynum]->but; //get current status of buts
switch (ram[a]++)
{
case 1: return (but&ID_BUT1) ? 1 : 0; //A
case 2: return (but&ID_BUT0) ? 1 : 0; //B
case 3: return (but&ID_BUT2) ? 1 : 0; //Select
case 4: return (but&ID_BUT3) ? 1 : 0; //Start
case 5: return (stat&ID_UP ) ? 1 : 0; //
case 6: return (stat&ID_DOWN) ? 1 : 0; //
case 7: return (stat&ID_LEFT) ? 1 : 0; //
case 8: return (stat&ID_RIGHT) ? 1 : 0; //
}
}
}
return 0;
}
WORD nesread0000(WORD a)
{
// if (a>=0x800) msg.error("weird read %X",a);
return ram[a&0x7FF];
}
WORD nesread5000(WORD a)
{
msg.error("expansion read!!");
return 0;
}
WORD nesread8000(WORD a)
{
// msg.error("direct rom read: %X!!",a);
return ram[a];
}
//---------------------------------------------
// trap handler tables....
//standard read trap table
struct m6502MemoryRead NESread[]=
{
{0x0800,0x1FFF, nesread0000}, //low ram
{0x2000,0x3FFF, nesread2000}, //lower ports
{0x4000,0x4FFF, nesread4000}, //higher ports
//{0x5000,0x7FFF, nesread5000}, //expansion
// {0x8000,0xFFFF, nesread8000}, //rom read
{(WORD) -1,(WORD) -1,NULL}
};
//standard write trap table
struct m6502MemoryWrite NESwrite[]=
{
{0x0800,0x1FFF, neswrite0000}, //ram
{0x2000,0x3FFF, neswrite2000}, //lower ports
{0x4000,0x4FFF, neswrite4000}, //higher ports
//{0x5000,0x7FFF, neswrite5000}, //expansion
{0x8000,0xFFFF, neswrite8000}, //rom
{(WORD) -1,(WORD) -1,NULL}
};