forked from nejidev/arm-NES-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathK6502_rw.h
executable file
·493 lines (436 loc) · 14.3 KB
/
K6502_rw.h
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
/*===================================================================*/
/* */
/* K6502_RW.h : 6502 Reading/Writing Operation for NES */
/* This file is included in K6502.cpp */
/* */
/* 2000/5/23 InfoNES Project ( based on pNesX ) */
/* */
/*===================================================================*/
#ifndef K6502_RW_H_INCLUDED
#define K6502_RW_H_INCLUDED
#ifdef __cplusplus
extern "C" {
#endif
/*-------------------------------------------------------------------*/
/* Include files */
/*-------------------------------------------------------------------*/
#include "InfoNES.h"
#include "InfoNES_System.h"
#include "InfoNES_pAPU.h"
/*===================================================================*/
/* */
/* K6502_ReadZp() : Reading from the zero page */
/* */
/*===================================================================*/
static inline BYTE K6502_ReadZp( BYTE byAddr )
{
/*
* Reading from the zero page
*
* Parameters
* BYTE byAddr (Read)
* An address inside the zero page
*
* Return values
* Read Data
*/
return RAM[ byAddr ];
}
/*===================================================================*/
/* */
/* K6502_Read() : Reading operation */
/* */
/*===================================================================*/
static inline BYTE K6502_Read( WORD wAddr )
{
/*
* Reading operation
*
* Parameters
* WORD wAddr (Read)
* Address to read
*
* Return values
* Read data
*
* Remarks
* 0x0000 - 0x1fff RAM ( 0x800 - 0x1fff is mirror of 0x0 - 0x7ff )
* 0x2000 - 0x3fff PPU
* 0x4000 - 0x5fff Sound
* 0x6000 - 0x7fff SRAM ( Battery Backed )
* 0x8000 - 0xffff ROM
*
*/
BYTE byRet;
switch ( wAddr & 0xe000 )
{
case 0x0000: /* RAM */
return RAM[ wAddr & 0x7ff ];
case 0x2000: /* PPU */
if ( ( wAddr & 0x7 ) == 0x7 ) /* PPU Memory */
{
WORD addr = PPU_Addr;
// Increment PPU Address
PPU_Addr += PPU_Increment;
addr &= 0x3fff;
// Set return value;
byRet = PPU_R7;
// Read PPU Memory
PPU_R7 = PPUBANK[ addr >> 10 ][ addr & 0x3ff ];
return byRet;
}
else
if ( ( wAddr & 0x7 ) == 0x4 ) /* SPR_RAM I/O Register */
{
return SPRRAM[ PPU_R3++ ];
}
else
if ( ( wAddr & 0x7 ) == 0x2 ) /* PPU Status */
{
// Set return value
byRet = PPU_R2;
// Reset a V-Blank flag
PPU_R2 &= ~R2_IN_VBLANK;
// Reset address latch
PPU_Latch_Flag = 0;
// Make a Nametable 0 in V-Blank
if ( PPU_Scanline >= SCAN_VBLANK_START && !( PPU_R0 & R0_NMI_VB ) )
{
PPU_R0 &= ~R0_NAME_ADDR;
PPU_NameTableBank = NAME_TABLE0;
}
return byRet;
}
break;
case 0x4000: /* Sound */
if ( wAddr == 0x4015 )
{
// APU control
byRet = APU_Reg[ 0x4015 ];
if ( ApuC1Atl > 0 ) byRet |= (1<<0);
if ( ApuC2Atl > 0 ) byRet |= (1<<1);
if ( !ApuC3Holdnote ) {
if ( ApuC3Atl > 0 ) byRet |= (1<<2);
} else {
if ( ApuC3Llc > 0 ) byRet |= (1<<2);
}
if ( ApuC4Atl > 0 ) byRet |= (1<<3);
// FrameIRQ
APU_Reg[ 0x4015 ] &= ~0x40;
return byRet;
}
else
if ( wAddr == 0x4016 )
{
// Set Joypad1 data
byRet = (BYTE)( ( PAD1_Latch >> PAD1_Bit ) & 1 ) | 0x40;
PAD1_Bit = ( PAD1_Bit == 23 ) ? 0 : ( PAD1_Bit + 1 );
return byRet;
}
else
if ( wAddr == 0x4017 )
{
// Set Joypad2 data
byRet = (BYTE)( ( PAD2_Latch >> PAD2_Bit ) & 1 ) | 0x40;
PAD2_Bit = ( PAD2_Bit == 23 ) ? 0 : ( PAD2_Bit + 1 );
return byRet;
}
else
{
/* Return Mapper Register*/
return MapperReadApu( wAddr );
}
break;
// The other sound registers are not readable.
case 0x6000: /* SRAM */
if ( ROM_SRAM )
{
return SRAM[ wAddr & 0x1fff ];
} else { /* SRAM BANK */
return SRAMBANK[ wAddr & 0x1fff ];
}
case 0x8000: /* ROM BANK 0 */
return ROMBANK0[ wAddr & 0x1fff ];
case 0xa000: /* ROM BANK 1 */
return ROMBANK1[ wAddr & 0x1fff ];
case 0xc000: /* ROM BANK 2 */
return ROMBANK2[ wAddr & 0x1fff ];
case 0xe000: /* ROM BANK 3 */
return ROMBANK3[ wAddr & 0x1fff ];
}
return ( wAddr >> 8 ); /* when a register is not readable the upper half
address is returned. */
}
/*===================================================================*/
/* */
/* K6502_Write() : Writing operation */
/* */
/*===================================================================*/
static inline void K6502_Write( WORD wAddr, BYTE byData )
{
/*
* Writing operation
*
* Parameters
* WORD wAddr (Read)
* Address to write
*
* BYTE byData (Read)
* Data to write
*
* Remarks
* 0x0000 - 0x1fff RAM ( 0x800 - 0x1fff is mirror of 0x0 - 0x7ff )
* 0x2000 - 0x3fff PPU
* 0x4000 - 0x5fff Sound
* 0x6000 - 0x7fff SRAM ( Battery Backed )
* 0x8000 - 0xffff ROM
*
*/
switch ( wAddr & 0xe000 )
{
case 0x0000: /* RAM */
RAM[ wAddr & 0x7ff ] = byData;
break;
case 0x2000: /* PPU */
switch ( wAddr & 0x7 )
{
case 0: /* 0x2000 */
PPU_R0 = byData;
PPU_Increment = ( PPU_R0 & R0_INC_ADDR ) ? 32 : 1;
PPU_NameTableBank = NAME_TABLE0 + ( PPU_R0 & R0_NAME_ADDR );
PPU_BG_Base = ( PPU_R0 & R0_BG_ADDR ) ? ChrBuf + 256 * 64 : ChrBuf;
PPU_SP_Base = ( PPU_R0 & R0_SP_ADDR ) ? ChrBuf + 256 * 64 : ChrBuf;
PPU_SP_Height = ( PPU_R0 & R0_SP_SIZE ) ? 16 : 8;
// Account for Loopy's scrolling discoveries
PPU_Temp = ( PPU_Temp & 0xF3FF ) | ( ( ( (WORD)byData ) & 0x0003 ) << 10 );
break;
case 1: /* 0x2001 */
PPU_R1 = byData;
break;
case 2: /* 0x2002 */
#if 0
PPU_R2 = byData; // 0x2002 is not writable
#endif
break;
case 3: /* 0x2003 */
// Sprite RAM Address
PPU_R3 = byData;
break;
case 4: /* 0x2004 */
// Write data to Sprite RAM
SPRRAM[ PPU_R3++ ] = byData;
break;
case 5: /* 0x2005 */
// Set Scroll Register
if ( PPU_Latch_Flag )
{
// V-Scroll Register
PPU_Scr_V_Next = ( byData > 239 ) ? 0 : byData;
PPU_Scr_V_Byte_Next = PPU_Scr_V_Next >> 3;
PPU_Scr_V_Bit_Next = PPU_Scr_V_Next & 7;
// Added : more Loopy Stuff
PPU_Temp = ( PPU_Temp & 0xFC1F ) | ( ( ( (WORD)byData ) & 0xF8 ) << 2);
PPU_Temp = ( PPU_Temp & 0x8FFF ) | ( ( ( (WORD)byData ) & 0x07 ) << 12);
}
else
{
// H-Scroll Register
PPU_Scr_H_Next = byData;
PPU_Scr_H_Byte_Next = PPU_Scr_H_Next >> 3;
PPU_Scr_H_Bit_Next = PPU_Scr_H_Next & 7;
// Added : more Loopy Stuff
PPU_Temp = ( PPU_Temp & 0xFFE0 ) | ( ( ( (WORD)byData ) & 0xF8 ) >> 3 );
}
PPU_Latch_Flag ^= 1;
break;
case 6: /* 0x2006 */
// Set PPU Address
if ( PPU_Latch_Flag )
{
/* Low */
#if 0
PPU_Addr = ( PPU_Addr & 0xff00 ) | ( (WORD)byData );
#else
PPU_Temp = ( PPU_Temp & 0xFF00 ) | ( ( (WORD)byData ) & 0x00FF);
PPU_Addr = PPU_Temp;
#endif
InfoNES_SetupScr();
}
else
{
/* High */
#if 0
PPU_Addr = ( PPU_Addr & 0x00ff ) | ( (WORD)( byData & 0x3f ) << 8 );
InfoNES_SetupScr();
#else
PPU_Temp = ( PPU_Temp & 0x00FF ) | ( ( ((WORD)byData) & 0x003F ) << 8 );
#endif
}
PPU_Latch_Flag ^= 1;
break;
case 7: /* 0x2007 */
{
WORD addr = PPU_Addr;
// Increment PPU Address
PPU_Addr += PPU_Increment;
addr &= 0x3fff;
// Write to PPU Memory
if ( addr < 0x2000 && byVramWriteEnable )
{
// Pattern Data
ChrBufUpdate |= ( 1 << ( addr >> 10 ) );
PPUBANK[ addr >> 10 ][ addr & 0x3ff ] = byData;
}
else
if ( addr < 0x3f00 ) /* 0x2000 - 0x3eff */
{
// Name Table and mirror
PPUBANK[ addr >> 10 ][ addr & 0x3ff ] = byData;
PPUBANK[ ( addr ^ 0x1000 ) >> 10 ][ addr & 0x3ff ] = byData;
}
else
if ( !( addr & 0xf ) ) /* 0x3f00 or 0x3f10 */
{
// Palette mirror
PPURAM[ 0x3f10 ] = PPURAM[ 0x3f14 ] = PPURAM[ 0x3f18 ] = PPURAM[ 0x3f1c ] =
PPURAM[ 0x3f00 ] = PPURAM[ 0x3f04 ] = PPURAM[ 0x3f08 ] = PPURAM[ 0x3f0c ] = byData;
PalTable[ 0x00 ] = PalTable[ 0x04 ] = PalTable[ 0x08 ] = PalTable[ 0x0c ] =
PalTable[ 0x10 ] = PalTable[ 0x14 ] = PalTable[ 0x18 ] = PalTable[ 0x1c ] = NesPalette[ byData ] | 0x8000;
}
else
if ( addr & 3 )
{
// Palette
PPURAM[ addr ] = byData;
PalTable[ addr & 0x1f ] = NesPalette[ byData ];
}
}
break;
}
break;
case 0x4000: /* Sound */
switch ( wAddr & 0x1f )
{
case 0x00:
case 0x01:
case 0x02:
case 0x03:
case 0x04:
case 0x05:
case 0x06:
case 0x07:
case 0x08:
case 0x09:
case 0x0a:
case 0x0b:
case 0x0c:
case 0x0d:
case 0x0e:
case 0x0f:
case 0x10:
case 0x11:
case 0x12:
case 0x13:
// Call Function corresponding to Sound Registers
if ( !APU_Mute )
pAPUSoundRegs[ wAddr & 0x1f ]( wAddr, byData );
break;
case 0x14: /* 0x4014 */
// Sprite DMA
switch ( byData >> 5 )
{
case 0x0: /* RAM */
InfoNES_MemoryCopy( SPRRAM, &RAM[ ( (WORD)byData << 8 ) & 0x7ff ], SPRRAM_SIZE );
break;
case 0x3: /* SRAM */
InfoNES_MemoryCopy( SPRRAM, &SRAM[ ( (WORD)byData << 8 ) & 0x1fff ], SPRRAM_SIZE );
break;
case 0x4: /* ROM BANK 0 */
InfoNES_MemoryCopy( SPRRAM, &ROMBANK0[ ( (WORD)byData << 8 ) & 0x1fff ], SPRRAM_SIZE );
break;
case 0x5: /* ROM BANK 1 */
InfoNES_MemoryCopy( SPRRAM, &ROMBANK1[ ( (WORD)byData << 8 ) & 0x1fff ], SPRRAM_SIZE );
break;
case 0x6: /* ROM BANK 2 */
InfoNES_MemoryCopy( SPRRAM, &ROMBANK2[ ( (WORD)byData << 8 ) & 0x1fff ], SPRRAM_SIZE );
break;
case 0x7: /* ROM BANK 3 */
InfoNES_MemoryCopy( SPRRAM, &ROMBANK3[ ( (WORD)byData << 8 ) & 0x1fff ], SPRRAM_SIZE );
break;
}
break;
case 0x15: /* 0x4015 */
InfoNES_pAPUWriteControl( wAddr, byData );
#if 0
/* Unknown */
if ( byData & 0x10 )
{
byData &= ~0x80;
}
#endif
break;
case 0x16: /* 0x4016 */
// Reset joypad
if ( !( APU_Reg[ 0x16 ] & 1 ) && ( byData & 1 ) )
{
PAD1_Bit = 0;
PAD2_Bit = 0;
}
break;
case 0x17: /* 0x4017 */
// Frame IRQ
FrameStep = 0;
if ( !( byData & 0xc0 ) )
{
FrameIRQ_Enable = 1;
} else {
FrameIRQ_Enable = 0;
}
break;
}
if ( wAddr <= 0x4017 )
{
/* Write to APU Register */
APU_Reg[ wAddr & 0x1f ] = byData;
}
else
{
/* Write to APU */
MapperApu( wAddr, byData );
}
break;
case 0x6000: /* SRAM */
SRAM[ wAddr & 0x1fff ] = byData;
/* Write to SRAM, when no SRAM */
if ( !ROM_SRAM )
{
MapperSram( wAddr, byData );
}
break;
case 0x8000: /* ROM BANK 0 */
case 0xa000: /* ROM BANK 1 */
case 0xc000: /* ROM BANK 2 */
case 0xe000: /* ROM BANK 3 */
// Write to Mapper
MapperWrite( wAddr, byData );
break;
}
}
// Reading/Writing operation (WORD version)
static inline WORD K6502_ReadW( WORD wAddr ){ return K6502_Read( wAddr ) | (WORD)K6502_Read( wAddr + 1 ) << 8; };
static inline void K6502_WriteW( WORD wAddr, WORD wData ){ K6502_Write( wAddr, wData & 0xff ); K6502_Write( wAddr + 1, wData >> 8 ); };
static inline WORD K6502_ReadZpW( BYTE byAddr ){ return K6502_ReadZp( byAddr ) | ( K6502_ReadZp( byAddr + 1 ) << 8 ); };
// 6502's indirect absolute jmp(opcode: 6C) has a bug (added at 01/08/15 )
static inline WORD K6502_ReadW2( WORD wAddr )
{
if ( 0x00ff == ( wAddr & 0x00ff ) )
{
return K6502_Read( wAddr ) | (WORD)K6502_Read( wAddr - 0x00ff ) << 8;
} else {
return K6502_Read( wAddr ) | (WORD)K6502_Read( wAddr + 1 ) << 8;
}
}
#ifdef __cplusplus
}
#endif
#endif /* !K6502_RW_H_INCLUDED */