Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/quicknes/nes_emu/Nes_Mmc1.cpp
2 views
1
2
// Nes_Emu 0.7.0. http://www.slack.net/~ant/
3
4
#include "Nes_Mapper.h"
5
6
#include <string.h>
7
8
/* Copyright (C) 2004-2006 Shay Green. This module is free software; you
9
can redistribute it and/or modify it under the terms of the GNU Lesser
10
General Public License as published by the Free Software Foundation; either
11
version 2.1 of the License, or (at your option) any later version. This
12
module is distributed in the hope that it will be useful, but WITHOUT ANY
13
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
15
more details. You should have received a copy of the GNU Lesser General
16
Public License along with this module; if not, write to the Free Software
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18
19
#include "blargg_source.h"
20
21
class Mapper_Mmc1 : public Nes_Mapper, mmc1_state_t {
22
public:
23
Mapper_Mmc1()
24
{
25
mmc1_state_t* state = this;
26
register_state( state, sizeof *state );
27
}
28
29
virtual void reset_state()
30
{
31
regs [0] = 0x0f;
32
regs [1] = 0x00;
33
regs [2] = 0x01;
34
regs [3] = 0x00;
35
}
36
37
void register_changed( int );
38
39
virtual void apply_mapping()
40
{
41
enable_sram(); // early MMC1 always had SRAM enabled
42
register_changed( 0 );
43
}
44
45
virtual void write( nes_time_t, nes_addr_t addr, int data )
46
{
47
if ( !(data & 0x80) )
48
{
49
buf |= (data & 1) << bit;
50
bit++;
51
52
if ( bit >= 5 )
53
{
54
int reg = addr >> 13 & 3;
55
regs [reg] = buf & 0x1f;
56
57
bit = 0;
58
buf = 0;
59
60
register_changed( reg );
61
}
62
}
63
else
64
{
65
bit = 0;
66
buf = 0;
67
regs [0] |= 0x0c;
68
register_changed( 0 );
69
}
70
}
71
};
72
73
void Mapper_Mmc1::register_changed( int reg )
74
{
75
// Mirroring
76
if ( reg == 0 )
77
{
78
int mode = regs [0] & 3;
79
if ( mode < 2 )
80
mirror_single( mode & 1 );
81
else if ( mode == 2 )
82
mirror_vert();
83
else
84
mirror_horiz();
85
}
86
87
// CHR
88
if ( reg < 3 && cart().chr_size() > 0 )
89
{
90
if ( regs [0] & 0x10 )
91
{
92
set_chr_bank( 0x0000, bank_4k, regs [1] );
93
set_chr_bank( 0x1000, bank_4k, regs [2] );
94
}
95
else
96
{
97
set_chr_bank( 0, bank_8k, regs [1] >> 1 );
98
}
99
}
100
101
// PRG
102
int bank = (regs [1] & 0x10) | (regs [3] & 0x0f);
103
if ( !(regs [0] & 0x08) )
104
{
105
set_prg_bank( 0x8000, bank_32k, bank >> 1 );
106
}
107
else if ( regs [0] & 0x04 )
108
{
109
set_prg_bank( 0x8000, bank_16k, bank );
110
set_prg_bank( 0xC000, bank_16k, bank | 0x0f );
111
}
112
else
113
{
114
set_prg_bank( 0x8000, bank_16k, bank & ~0x0f );
115
set_prg_bank( 0xC000, bank_16k, bank );
116
}
117
}
118
119
Nes_Mapper* Nes_Mapper::make_mmc1()
120
{
121
return BLARGG_NEW Mapper_Mmc1;
122
}
123
124
125