summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/gameboy/ntnew.cpp
blob: fa54065bf2ecaa1e988d0db983e7fefe82fa98ec (plain) (blame)
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
// license:BSD-3-Clause
// copyright-holders:Vas Crabb
/***************************************************************************

 Memory controller used for newer NT/Makon games

 Appears to start in a mode similar to Nintendo MBC3, with a fixed 16 KiB
 ROM bank at 0x0000 and a switchable 16 KiB ROM bank at 0x4000, but allows
 switching to a mode with selectable 8 KiB ROM banks at 0x4000 and 0x6000.

 It isn't clear how many static RAM pages are actually supported.  Many
 cartridges included 32K*8 static RAMs despite the headers declaring smaller
 sizes and the games using no more than 8 KiB.  The maximum supported ROM
 size is also unknown.

 Pretty much everything here is guessed based on the games' behaviour.  The
 exact address ranges the memory controller responds to are unknown.  There
 may be additional features that are not emulated.

 ***************************************************************************/

#include "emu.h"
#include "ntnew.h"

#include "cartbase.ipp"

//#define VERBOSE 1
//#define LOG_OUTPUT_FUNC osd_printf_info
#include "logmacro.h"


namespace bus::gameboy {

namespace {

class ntnew_device : public mbc_ram_device_base<mbc_8k_device_base>
{
public:
	ntnew_device(machine_config const &mconfig, char const *tag, device_t *owner, u32 clock);

	virtual std::error_condition load(std::string &message) override ATTR_COLD;

protected:
	virtual void device_start() override ATTR_COLD;
	virtual void device_reset() override ATTR_COLD;

private:
	void enable_ram(offs_t offset, u8 data);
	void bank_switch_rom(offs_t offset, u8 data);
	void bank_switch_ram(u8 data);

	memory_view m_view_ram;

	u8 m_bank_8k;
};


ntnew_device::ntnew_device(
		machine_config const &mconfig,
		char const *tag,
		device_t *owner,
		u32 clock) :
	mbc_ram_device_base<mbc_8k_device_base>(mconfig, GB_ROM_NTNEW, tag, owner, clock),
	m_view_ram(*this, "ram"),
	m_bank_8k(0U)
{
}


std::error_condition ntnew_device::load(std::string &message)
{
	// set up ROM and RAM
	set_bank_bits_rom(8);
	set_bank_bits_ram(4);
	if (!check_rom(message) || !check_ram(message))
		return image_error::BADSOFTWARE;
	cart_space()->install_view(0xa000, 0xbfff, m_view_ram);
	install_rom(*cart_space(), *cart_space(), *cart_space());
	install_ram(m_view_ram[0]);

	// install handlers
	cart_space()->install_write_handler(
			0x0000, 0x1fff,
			emu::rw_delegate(*this, FUNC(ntnew_device::enable_ram)));
	cart_space()->install_write_handler(
			0x2000, 0x2fff,
			emu::rw_delegate(*this, FUNC(ntnew_device::bank_switch_rom)));
	cart_space()->install_write_handler(
			0x4000, 0x5fff,
			emu::rw_delegate(*this, FUNC(ntnew_device::bank_switch_ram)));

	// all good
	return std::error_condition();
}


void ntnew_device::device_start()
{
	mbc_ram_device_base<mbc_8k_device_base>::device_start();

	save_item(NAME(m_bank_8k));
}


void ntnew_device::device_reset()
{
	mbc_ram_device_base<mbc_8k_device_base>::device_reset();

	m_view_ram.disable();
	m_bank_8k = 0U;

	set_bank_rom_low(2);
	set_bank_rom_high(3);
	set_bank_ram(0);
}


void ntnew_device::enable_ram(offs_t offset, u8 data)
{
	// TODO: what range actually triggers this, and does it still trigger RAM enable?
	if (((offset & 0x1f00) == 0x1400) & (0x55 == data))
	{
		LOG("%s: 8K ROM banking enabled\n", machine().describe_context());
		m_bank_8k = 1U;
	}

	// TODO: how many bits are checked?
	bool const enable(0x0a == (data & 0x0f));
	LOG(
			"%s: Cartridge RAM %s\n",
			machine().describe_context(),
			enable ? "enabled" : "disabled");
	if (enable)
		m_view_ram.select(0);
	else
		m_view_ram.disable();
}


void ntnew_device::bank_switch_rom(offs_t offset, u8 data)
{
	if (m_bank_8k)
	{
		// TODO: what ranges does the controller actually respond to?
		switch (offset & 0x0f00)
		{
		case 0x0000:
			if (!(data & 0xfe))
				data |= 0x02;
			set_bank_rom_low(data);
			return;

		case 0x0400:
			if (!(data & 0xfe))
				data |= 0x02;
			set_bank_rom_high(data);
			return;
		}
	}

	data <<= 1;
	if (!(data & 0xfe))
		data |= 0x02;
	set_bank_rom_low(data);
	set_bank_rom_high(data | 0x01);
}


void ntnew_device::bank_switch_ram(u8 data)
{
	set_bank_ram(data & 0x0f);
}

} // anonymous namespace

} // namespace bus::gameboy


DEFINE_DEVICE_TYPE_PRIVATE(GB_ROM_NTNEW, device_gb_cart_interface, bus::gameboy::ntnew_device, "gb_rom_ntnew", "Game Boy newer Kasheng/Makon Cartridge")