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

    Casio Loopy cart slot emulation

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

#include "emu.h"
#include "slot.h"

//**************************************************************************
//  GLOBAL VARIABLES
//**************************************************************************

DEFINE_DEVICE_TYPE(CASLOOPY_CART_SLOT, casloopy_cart_slot_device, "casloopy_cart_slot", "Casio Loopy Cartridge Slot")

//**************************************************************************
//    casloopy Cartridges Interface
//**************************************************************************

//-------------------------------------------------
//  device_casloopy_cart_interface - constructor
//-------------------------------------------------

device_casloopy_cart_interface::device_casloopy_cart_interface(const machine_config &mconfig, device_t &device)
	: device_interface(device, "casloopy_cart")
	, m_slot(dynamic_cast<casloopy_cart_slot_device *>(device.owner()))
{
}


//-------------------------------------------------
//  ~device_casloopy_cart_interface - destructor
//-------------------------------------------------

device_casloopy_cart_interface::~device_casloopy_cart_interface()
{
}


void device_casloopy_cart_interface::battery_load(void *buffer, int length, int fill)
{
	assert(m_slot);
	m_slot->battery_load(buffer, length, fill);
}

void device_casloopy_cart_interface::battery_load(void *buffer, int length, void *def_buffer)
{
	assert(m_slot);
	m_slot->battery_load(buffer, length, def_buffer);
}

void device_casloopy_cart_interface::battery_save(const void *buffer, int length)
{
	assert(m_slot);
	m_slot->battery_save(buffer, length);
}


//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  casloopy_cart_slot_device - constructor
//-------------------------------------------------
casloopy_cart_slot_device::casloopy_cart_slot_device(const machine_config &mconfig, const char *tag, device_t *owner, u32 clock) :
	device_t(mconfig, CASLOOPY_CART_SLOT, tag, owner, clock),
	device_cartrom_image_interface(mconfig, *this),
	device_single_card_slot_interface<device_casloopy_cart_interface>(mconfig, *this),
	m_cart(nullptr)
{
}


//-------------------------------------------------
//  casloopy_cart_slot_device - destructor
//-------------------------------------------------

casloopy_cart_slot_device::~casloopy_cart_slot_device()
{
}

//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void casloopy_cart_slot_device::device_start()
{
	m_cart = get_card_device();
}



/*-------------------------------------------------
 call load
 -------------------------------------------------*/

std::pair<std::error_condition, std::string> casloopy_cart_slot_device::call_load()
{
	if (!m_cart)
		return std::make_pair(std::error_condition(), std::string());

	memory_region *romregion = loaded_through_softlist() ? memregion("rom") : nullptr;
	if (loaded_through_softlist() && !romregion)
		return std::make_pair(image_error::INVALIDLENGTH, "Software list item has no 'rom' data area");

	const u32 len = loaded_through_softlist() ? romregion->bytes() : length();

	if (!loaded_through_softlist())
	{
		romregion = machine().memory().region_alloc(subtag("rom"), len, 2, ENDIANNESS_BIG);
		u16 *const rombase = reinterpret_cast<u16 *>(romregion->base());
		const u32 cnt = fread(rombase, len);
		if (cnt != len)
			return std::make_pair(std::errc::io_error, "Error reading cartridge file");

		// CPU is big Endian, but conventional ROM dump format is little Endian
		if (ENDIANNESS_NATIVE != ENDIANNESS_LITTLE)
		{
			for (u32 i = 0; (len / 2) > i; ++i)
				rombase[i] = swapendian_int16(rombase[i]);
		}
	}

	return std::make_pair(m_cart->load(), std::string());
}


/*-------------------------------------------------
 call unload
 -------------------------------------------------*/

void casloopy_cart_slot_device::call_unload()
{
	if (m_cart)
		m_cart->unload();
}


/*-------------------------------------------------
 get default card software
 -------------------------------------------------*/

std::string casloopy_cart_slot_device::get_default_card_software(get_default_card_software_hook &hook) const
{
	return software_get_default_slot("std");
}