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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/**********************************************************************
Commodore 64 Expansion Port emulation
Copyright MESS Team.
Visit http://mamedev.org for licensing and usage restrictions.
**********************************************************************/
#include "machine/c64exp.h"
//**************************************************************************
// MACROS/CONSTANTS
//**************************************************************************
#define LOG 0
//**************************************************************************
// DEVICE DEFINITIONS
//**************************************************************************
const device_type C64_EXPANSION_SLOT = &device_creator<c64_expansion_slot_device>;
//**************************************************************************
// DEVICE C64_EXPANSION CARD INTERFACE
//**************************************************************************
//-------------------------------------------------
// device_c64_expansion_card_interface - constructor
//-------------------------------------------------
device_c64_expansion_card_interface::device_c64_expansion_card_interface(const machine_config &mconfig, device_t &device)
: device_slot_card_interface(mconfig, device),
m_roml(NULL),
m_romh(NULL),
m_ram(NULL),
m_nvram(NULL),
m_nvram_size(0),
m_roml_mask(0),
m_romh_mask(0),
m_ram_mask(0),
m_game(1),
m_exrom(1)
{
m_slot = dynamic_cast<c64_expansion_slot_device *>(device.owner());
}
//-------------------------------------------------
// ~device_c64_expansion_card_interface - destructor
//-------------------------------------------------
device_c64_expansion_card_interface::~device_c64_expansion_card_interface()
{
}
//-------------------------------------------------
// c64_roml_pointer - get low ROM pointer
//-------------------------------------------------
UINT8* device_c64_expansion_card_interface::c64_roml_pointer(running_machine &machine, size_t size)
{
if (m_roml == NULL)
{
m_roml = auto_alloc_array(machine, UINT8, size);
m_roml_mask = size - 1;
}
return m_roml;
}
//-------------------------------------------------
// c64_romh_pointer - get high ROM pointer
//-------------------------------------------------
UINT8* device_c64_expansion_card_interface::c64_romh_pointer(running_machine &machine, size_t size)
{
if (m_romh == NULL)
{
m_romh = auto_alloc_array(machine, UINT8, size);
m_romh_mask = size - 1;
}
return m_romh;
}
//-------------------------------------------------
// c64_ram_pointer - get RAM pointer
//-------------------------------------------------
UINT8* device_c64_expansion_card_interface::c64_ram_pointer(running_machine &machine, size_t size)
{
if (m_ram == NULL)
{
m_ram = auto_alloc_array(machine, UINT8, size);
m_ram_mask = size - 1;
}
return m_ram;
}
//-------------------------------------------------
// c64_ram_pointer - get NVRAM pointer
//-------------------------------------------------
UINT8* device_c64_expansion_card_interface::c64_nvram_pointer(running_machine &machine, size_t size)
{
if (m_nvram == NULL)
{
m_nvram = auto_alloc_array(machine, UINT8, size);
m_nvram_mask = size - 1;
m_nvram_size = size;
}
return m_nvram;
}
//**************************************************************************
// LIVE DEVICE
//**************************************************************************
//-------------------------------------------------
// c64_expansion_slot_device - constructor
//-------------------------------------------------
c64_expansion_slot_device::c64_expansion_slot_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) :
device_t(mconfig, C64_EXPANSION_SLOT, "C64 expansion port", tag, owner, clock),
device_slot_interface(mconfig, *this),
device_image_interface(mconfig, *this)
{
}
//-------------------------------------------------
// c64_expansion_slot_device - destructor
//-------------------------------------------------
c64_expansion_slot_device::~c64_expansion_slot_device()
{
}
//-------------------------------------------------
// device_config_complete - perform any
// operations now that the configuration is
// complete
//-------------------------------------------------
void c64_expansion_slot_device::device_config_complete()
{
// inherit a copy of the static data
const c64_expansion_slot_interface *intf = reinterpret_cast<const c64_expansion_slot_interface *>(static_config());
if (intf != NULL)
{
*static_cast<c64_expansion_slot_interface *>(this) = *intf;
}
// or initialize to defaults if none provided
else
{
memset(&m_in_dma_cd_cb, 0, sizeof(m_in_dma_cd_cb));
memset(&m_out_dma_cd_cb, 0, sizeof(m_out_dma_cd_cb));
memset(&m_out_irq_cb, 0, sizeof(m_out_irq_cb));
memset(&m_out_nmi_cb, 0, sizeof(m_out_nmi_cb));
memset(&m_out_dma_cb, 0, sizeof(m_out_dma_cb));
memset(&m_out_reset_cb, 0, sizeof(m_out_reset_cb));
}
// set brief and instance name
update_names();
}
//-------------------------------------------------
// device_start - device-specific startup
//-------------------------------------------------
void c64_expansion_slot_device::device_start()
{
m_cart = dynamic_cast<device_c64_expansion_card_interface *>(get_card_device());
// resolve callbacks
m_in_dma_cd_func.resolve(m_in_dma_cd_cb, *this);
m_out_dma_cd_func.resolve(m_out_dma_cd_cb, *this);
m_out_irq_func.resolve(m_out_irq_cb, *this);
m_out_nmi_func.resolve(m_out_nmi_cb, *this);
m_out_dma_func.resolve(m_out_dma_cb, *this);
m_out_reset_func.resolve(m_out_reset_cb, *this);
// inherit bus clock
if (clock() == 0)
{
c64_expansion_slot_device *root = machine().device<c64_expansion_slot_device>(C64_EXPANSION_SLOT_TAG);
assert(root);
set_unscaled_clock(root->clock());
}
}
//-------------------------------------------------
// device_reset - device-specific reset
//-------------------------------------------------
void c64_expansion_slot_device::device_reset()
{
port_reset_w(ASSERT_LINE);
port_reset_w(CLEAR_LINE);
}
//-------------------------------------------------
// call_load -
//-------------------------------------------------
bool c64_expansion_slot_device::call_load()
{
if (m_cart)
{
size_t size = 0;
if (software_entry() == NULL)
{
size = length();
if (!mame_stricmp(filetype(), "80"))
{
fread(m_cart->c64_roml_pointer(machine(), size), size);
m_cart->m_exrom = (0);
if (size == 0x4000)
{
m_cart->m_game = 0;
}
}
else if (!mame_stricmp(filetype(), "a0"))
{
fread(m_cart->c64_romh_pointer(machine(), 0x2000), 0x2000);
m_cart->m_exrom = 0;
m_cart->m_game = 0;
}
else if (!mame_stricmp(filetype(), "e0"))
{
fread(m_cart->c64_romh_pointer(machine(), 0x2000), 0x2000);
m_cart->m_game = 0;
}
else if (!mame_stricmp(filetype(), "crt"))
{
size_t roml_size = 0;
size_t romh_size = 0;
int exrom = 1;
int game = 1;
if (cbm_crt_read_header(m_file, &roml_size, &romh_size, &exrom, &game))
{
UINT8 *roml = NULL;
UINT8 *romh = NULL;
if (roml_size) roml = m_cart->c64_roml_pointer(machine(), roml_size);
if (romh_size) romh = m_cart->c64_romh_pointer(machine(), romh_size);
cbm_crt_read_data(m_file, roml, romh);
}
m_cart->m_exrom = exrom;
m_cart->m_game = game;
}
}
else
{
size = get_software_region_length("uprom");
if (size)
{
// Ultimax (VIC-10) cartridge
memcpy(m_cart->c64_romh_pointer(machine(), size), get_software_region("uprom"), size);
size = get_software_region_length("lorom");
if (size) memcpy(m_cart->c64_roml_pointer(machine(), size), get_software_region("lorom"), size);
m_cart->m_exrom = 1;
m_cart->m_game = 0;
}
else
{
// Commodore 64/128 cartridge
size = get_software_region_length("roml");
if (size) memcpy(m_cart->c64_roml_pointer(machine(), size), get_software_region("roml"), size);
size = get_software_region_length("romh");
if (size) memcpy(m_cart->c64_romh_pointer(machine(), size), get_software_region("romh"), size);
size = get_software_region_length("ram");
if (size) memset(m_cart->c64_ram_pointer(machine(), size), 0, size);
size = get_software_region_length("nvram");
if (size) memset(m_cart->c64_nvram_pointer(machine(), size), 0, size);
m_cart->m_exrom = atol(get_feature("exrom"));
m_cart->m_game = atol(get_feature("game"));
}
}
}
return IMAGE_INIT_PASS;
}
//-------------------------------------------------
// call_softlist_load -
//-------------------------------------------------
bool c64_expansion_slot_device::call_softlist_load(char *swlist, char *swname, rom_entry *start_entry)
{
load_software_part_region(this, swlist, swname, start_entry);
return true;
}
//-------------------------------------------------
// get_default_card_software -
//-------------------------------------------------
const char * c64_expansion_slot_device::get_default_card_software(const machine_config &config, emu_options &options)
{
if (open_image_file(options))
{
if (!mame_stricmp(filetype(), "crt"))
{
return cbm_crt_get_card(m_file);
}
clear();
}
return software_get_default_slot(config, options, this, "standard");
}
//-------------------------------------------------
// cd_r - cartridge data read
//-------------------------------------------------
UINT8 c64_expansion_slot_device::cd_r(address_space &space, offs_t offset, UINT8 data, int sphi2, int ba, int roml, int romh, int io1, int io2)
{
if (m_cart != NULL)
{
data = m_cart->c64_cd_r(space, offset, data, sphi2, ba, roml, romh, io1, io2);
}
return data;
}
//-------------------------------------------------
// cd_w - cartridge data write
//-------------------------------------------------
void c64_expansion_slot_device::cd_w(address_space &space, offs_t offset, UINT8 data, int sphi2, int ba, int roml, int romh, int io1, int io2)
{
if (m_cart != NULL)
{
m_cart->c64_cd_w(space, offset, data, sphi2, ba, roml, romh, io1, io2);
}
}
//-------------------------------------------------
// game_r - GAME read
//-------------------------------------------------
int c64_expansion_slot_device::game_r(offs_t offset, int sphi2, int ba, int rw, int hiram)
{
int state = 1;
if (m_cart != NULL)
{
state = m_cart->c64_game_r(offset, sphi2, ba, rw, hiram);
}
return state;
}
//-------------------------------------------------
// exrom_r - EXROM read
//-------------------------------------------------
int c64_expansion_slot_device::exrom_r(offs_t offset, int sphi2, int ba, int rw, int hiram)
{
int state = 1;
if (m_cart != NULL)
{
state = m_cart->c64_exrom_r(offset, sphi2, ba, rw, hiram);
}
return state;
}
WRITE_LINE_MEMBER( c64_expansion_slot_device::port_reset_w ) { if (m_cart != NULL) m_cart->c64_reset_w(state); }
//-------------------------------------------------
// dma_cd_r - DMA read
//-------------------------------------------------
UINT8 c64_expansion_slot_device::dma_cd_r(offs_t offset)
{
return m_in_dma_cd_func(offset);
}
//-------------------------------------------------
// dma_cd_w - DMA write
//-------------------------------------------------
void c64_expansion_slot_device::dma_cd_w(offs_t offset, UINT8 data)
{
m_out_dma_cd_func(offset, data);
}
WRITE_LINE_MEMBER( c64_expansion_slot_device::irq_w ) { m_out_irq_func(state); }
WRITE_LINE_MEMBER( c64_expansion_slot_device::nmi_w ) { m_out_nmi_func(state); }
WRITE_LINE_MEMBER( c64_expansion_slot_device::dma_w ) { m_out_dma_func(state); }
WRITE_LINE_MEMBER( c64_expansion_slot_device::reset_w ) { m_out_reset_func(state); }
//-------------------------------------------------
// phi2 - system clock frequency
//-------------------------------------------------
int c64_expansion_slot_device::phi2()
{
return clock();
}
//-------------------------------------------------
// dotclock - dot clock frequency
//-------------------------------------------------
int c64_expansion_slot_device::dotclock()
{
return phi2() * 8;
}
|