1 // license:BSD-3-Clause
2 // copyright-holders:Fabio Priuli
3 /***********************************************************************************************************
4 
5     V-Tech CreatiVision cart emulation
6     (through slot devices)
7 
8  ***********************************************************************************************************/
9 
10 
11 #include "emu.h"
12 #include "slot.h"
13 
14 //**************************************************************************
15 //  GLOBAL VARIABLES
16 //**************************************************************************
17 
18 DEFINE_DEVICE_TYPE(CRVISION_CART_SLOT, crvision_cart_slot_device, "crvision_cart_slot", "CreatiVision Cartridge Slot")
19 
20 //**************************************************************************
21 //    CreatiVision Cartridges Interface
22 //**************************************************************************
23 
24 //-------------------------------------------------
25 //  device_crvision_cart_interface - constructor
26 //-------------------------------------------------
27 
device_crvision_cart_interface(const machine_config & mconfig,device_t & device)28 device_crvision_cart_interface::device_crvision_cart_interface(const machine_config &mconfig, device_t &device) :
29 	device_interface(device, "crvisioncart"),
30 	m_rom(nullptr),
31 	m_rom_size(0)
32 {
33 }
34 
35 
36 //-------------------------------------------------
37 //  ~device_crvision_cart_interface - destructor
38 //-------------------------------------------------
39 
~device_crvision_cart_interface()40 device_crvision_cart_interface::~device_crvision_cart_interface()
41 {
42 }
43 
44 //-------------------------------------------------
45 //  rom_alloc - alloc the space for the cart
46 //-------------------------------------------------
47 
rom_alloc(uint32_t size,const char * tag)48 void device_crvision_cart_interface::rom_alloc(uint32_t size, const char *tag)
49 {
50 	if (m_rom == nullptr)
51 	{
52 		m_rom = device().machine().memory().region_alloc(std::string(tag).append(CRVSLOT_ROM_REGION_TAG).c_str(), size, 1, ENDIANNESS_LITTLE)->base();
53 		m_rom_size = size;
54 	}
55 }
56 
57 
58 //**************************************************************************
59 //  LIVE DEVICE
60 //**************************************************************************
61 
62 //-------------------------------------------------
63 //  crvision_cart_slot_device - constructor
64 //-------------------------------------------------
crvision_cart_slot_device(const machine_config & mconfig,const char * tag,device_t * owner,uint32_t clock)65 crvision_cart_slot_device::crvision_cart_slot_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock) :
66 	device_t(mconfig, CRVISION_CART_SLOT, tag, owner, clock),
67 	device_image_interface(mconfig, *this),
68 	device_single_card_slot_interface<device_crvision_cart_interface>(mconfig, *this),
69 	m_type(CRV_4K), m_cart(nullptr)
70 {
71 }
72 
73 
74 //-------------------------------------------------
75 //  crvision_cart_slot_device - destructor
76 //-------------------------------------------------
77 
~crvision_cart_slot_device()78 crvision_cart_slot_device::~crvision_cart_slot_device()
79 {
80 }
81 
82 //-------------------------------------------------
83 //  device_start - device-specific startup
84 //-------------------------------------------------
85 
device_start()86 void crvision_cart_slot_device::device_start()
87 {
88 	m_cart = get_card_device();
89 }
90 
91 
92 //-------------------------------------------------
93 //  APF PCB
94 //-------------------------------------------------
95 
96 struct crvision_slot
97 {
98 	int                     pcb_id;
99 	const char              *slot_option;
100 };
101 
102 // Here, we take the feature attribute from .xml (i.e. the PCB name) and we assign a unique ID to it
103 static const crvision_slot slot_list[] =
104 {
105 	{ CRV_4K,  "crv_rom4k" },
106 	{ CRV_6K,  "crv_rom6k" },
107 	{ CRV_8K,  "crv_rom8k" },
108 	{ CRV_10K, "crv_rom10k" },
109 	{ CRV_12K, "crv_rom12k" },
110 	{ CRV_16K, "crv_rom16k" },
111 	{ CRV_18K, "crv_rom18k" }
112 };
113 
crvision_get_pcb_id(const char * slot)114 static int crvision_get_pcb_id(const char *slot)
115 {
116 	for (auto & elem : slot_list)
117 	{
118 		if (!core_stricmp(elem.slot_option, slot))
119 			return elem.pcb_id;
120 	}
121 
122 	return 0;
123 }
124 
crvision_get_slot(int type)125 static const char *crvision_get_slot(int type)
126 {
127 	for (auto & elem : slot_list)
128 	{
129 		if (elem.pcb_id == type)
130 			return elem.slot_option;
131 	}
132 
133 	return "crv_rom4k";
134 }
135 
136 
137 /*-------------------------------------------------
138  call load
139  -------------------------------------------------*/
140 
call_load()141 image_init_result crvision_cart_slot_device::call_load()
142 {
143 	if (m_cart)
144 	{
145 		uint32_t size = !loaded_through_softlist() ? length() : get_software_region_length("rom");
146 
147 		if (size > 0x4800)
148 		{
149 			seterror(IMAGE_ERROR_UNSPECIFIED, "Image extends beyond the expected size for an APF cart");
150 			return image_init_result::FAIL;
151 		}
152 
153 		m_cart->rom_alloc(size, tag());
154 
155 		if (!loaded_through_softlist())
156 			fread(m_cart->get_rom_base(), size);
157 		else
158 			memcpy(m_cart->get_rom_base(), get_software_region("rom"), size);
159 
160 		if (!loaded_through_softlist())
161 		{
162 			m_type = CRV_4K;
163 
164 			switch (size)
165 			{
166 				case 0x4800:
167 					m_type = CRV_18K;
168 					break;
169 				case 0x4000:
170 					m_type = CRV_16K;
171 					break;
172 				case 0x3000:
173 					m_type = CRV_12K;
174 					break;
175 				case 0x2800:
176 					m_type = CRV_10K;
177 					break;
178 				case 0x2000:
179 					m_type = CRV_8K;
180 					break;
181 				case 0x1800:
182 					m_type = CRV_6K;
183 					break;
184 				case 0x1000:
185 				default:
186 					break;
187 			}
188 		}
189 		else
190 		{
191 			const char *pcb_name = get_feature("slot");
192 			if (pcb_name)
193 				m_type = crvision_get_pcb_id(pcb_name);
194 		}
195 
196 		printf("Type: %s\n", crvision_get_slot(m_type));
197 
198 		return image_init_result::PASS;
199 	}
200 
201 	return image_init_result::PASS;
202 }
203 
204 
205 /*-------------------------------------------------
206  get default card software
207  -------------------------------------------------*/
208 
get_default_card_software(get_default_card_software_hook & hook) const209 std::string crvision_cart_slot_device::get_default_card_software(get_default_card_software_hook &hook) const
210 {
211 	if (hook.image_file())
212 	{
213 		const char *slot_string;
214 		uint32_t size = hook.image_file()->size();
215 		int type = CRV_4K;
216 
217 		switch (size)
218 		{
219 			case 0x4800:
220 				type = CRV_18K;
221 				break;
222 			case 0x4000:
223 				type = CRV_16K;
224 				break;
225 			case 0x3000:
226 				type = CRV_12K;
227 				break;
228 			case 0x2800:
229 				type = CRV_10K;
230 				break;
231 			case 0x2000:
232 				type = CRV_8K;
233 				break;
234 			case 0x1800:
235 				type = CRV_6K;
236 				break;
237 			case 0x1000:
238 			default:
239 				break;
240 		}
241 
242 		slot_string = crvision_get_slot(type);
243 
244 		//printf("type: %s\n", slot_string);
245 
246 		return std::string(slot_string);
247 	}
248 
249 	return software_get_default_slot("crv_rom4k");
250 }
251 
252 /*-------------------------------------------------
253  read_rom
254  -------------------------------------------------*/
255 
read_rom40(offs_t offset)256 uint8_t crvision_cart_slot_device::read_rom40(offs_t offset)
257 {
258 	if (m_cart)
259 		return m_cart->read_rom40(offset);
260 	else
261 		return 0xff;
262 }
263 
read_rom80(offs_t offset)264 uint8_t crvision_cart_slot_device::read_rom80(offs_t offset)
265 {
266 	if (m_cart)
267 		return m_cart->read_rom80(offset);
268 	else
269 		return 0xff;
270 }
271