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

 Mega Duck cartridge slot

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

#include "emu.h"
#include "mdslot.h"

#include "carts.h"

#include "emuopts.h"
#include "romload.h"
#include "softlist.h"

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


//**************************************************************************
//  megaduck_cart_slot_device
//**************************************************************************

megaduck_cart_slot_device::megaduck_cart_slot_device(machine_config const &mconfig, char const *tag, device_t *owner, u32 clock) :
	gb_cart_slot_device_base(mconfig, MEGADUCK_CART_SLOT, tag, owner, clock)
{
}


std::string megaduck_cart_slot_device::get_default_card_software(get_default_card_software_hook &hook) const
{
	using namespace bus::gameboy;

	if (hook.image_file())
	{
		// loading from file - guess banking scheme using heuristics
		u64 length;
		if (!hook.image_file()->length(length))
		{
			if (0x8000 < length)
			{
				osd_printf_verbose("[%s] 0x%X-byte cartridge, requires ROM banking\n", tag(), length);
				return slotoptions::MEGADUCK_BANKED;
			}
			else
			{
				osd_printf_verbose("[%s] 0x%X-byte cartridge, assuming flat ROM\n", tag(), length);
				return slotoptions::MEGADUCK_STD;
			}
		}
		else
		{
			osd_printf_warning("[%s] Error getting cartridge ROM length - assuming banked ROM\n", tag());
			return slotoptions::MEGADUCK_BANKED;
		}
	}
	else
	{
		// loading from software list - try to find matching software part
		std::string const image_name(mconfig().options().image_option(instance_name()).value());
		software_part const *const part(!image_name.empty() ? find_software_item(image_name, true) : nullptr);
		if (part)
		{
			osd_printf_verbose("[%s] Found software part for image name '%s'\n", tag(), image_name);

			// if there's an explicit "slot" feature, use it
			char const *const slot(part->feature("slot"));
			if (slot)
			{
				osd_printf_verbose("[%s] Using specified cartridge device '%s'\n", tag(), slot);
				return slot;
			}

			// presence or absence of a fixed bank implies banking is in use
			char const *const fixedbank(part->feature("fixedbank"));
			if (fixedbank)
			{
				osd_printf_verbose("[%s] fixedbank='%s' specified, assuming banked ROM\n", tag(), fixedbank);
				return slotoptions::MEGADUCK_BANKED;
			}

			// fall back to guessing based on length
			for (rom_entry const &entry : part->romdata())
			{
				if (ROMENTRY_ISREGION(entry) && (entry.name() == "rom"))
				{
					auto const length(ROMREGION_GETLENGTH(entry));
					if (0x8000 < length)
					{
						osd_printf_verbose("[%s] Found 0x%X-byte 'rom' region, requires ROM banking\n", tag(), length);
						return slotoptions::MEGADUCK_BANKED;
					}
					else
					{
						osd_printf_verbose("[%s] Found 0x%X-byte 'rom' region, assuming flat ROM\n", tag(), length);
						return slotoptions::MEGADUCK_STD;
					}
				}
			}

			// a flat ROM cartridge with nothing in it is harmless
			osd_printf_verbose("[%s] No ROM region found\n", tag());
			return slotoptions::MEGADUCK_STD;
		}
		else
		{
			osd_printf_verbose("[%s] No software part found for image name '%s'\n", tag(), image_name);
		}
	}

	// leave the slot empty
	return std::string();
}


std::pair<std::error_condition, std::string> megaduck_cart_slot_device::load_image_file(util::random_read &file)
{
	auto const len = length();

	if (len)
	{
		LOG("Allocating %u byte cartridge ROM region\n", len);
		memory_region *const romregion = machine().memory().region_alloc(subtag("rom"), len, 1, ENDIANNESS_LITTLE);
		auto const [err, actual] = read_at(file, 0, romregion->base(), len);
		if (err || (len != actual))
			return std::make_pair(err ? err : std::errc::io_error, "Error reading cartridge file");
	}

	return std::make_pair(std::error_condition(), std::string());
}



//**************************************************************************
//  Device type definitions
//**************************************************************************

DEFINE_DEVICE_TYPE(MEGADUCK_CART_SLOT, megaduck_cart_slot_device, "megaduck_cart_slot", "Mega Duck Cartridge Slot")