~ubuntu-branches/ubuntu/quantal/vice/quantal

« back to all changes in this revision

Viewing changes to src/c64/c64.c

  • Committer: Bazaar Package Importer
  • Author(s): Zed Pobre
  • Date: 2005-03-27 13:06:04 UTC
  • mfrom: (4 hoary)
  • mto: This revision was merged to the branch mainline in revision 5.
  • Revision ID: james.westby@ubuntu.com-20050327130604-zodmv0i60dbbmcik
Tags: 1.16-3
Apply patch from Andreas Jochens <aj@andaco.de> to correct building on
AMD64 and GCC 4.x (closes: #300936)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 * c64.c
3
3
 *
4
4
 * Written by
 
5
 *  Andreas Boose <viceteam@t-online.de>
5
6
 *  Ettore Perazzoli <ettore@comm2000.it>
6
7
 *  Teemu Rantanen <tvr@cs.hut.fi>
7
 
 *  Andreas Boose <boose@linux.rz.fh-hannover.de>
8
8
 *
9
9
 * This file is part of VICE, the Versatile Commodore Emulator.
10
10
 * See README for copyright notice.
31
31
#include <stdio.h>
32
32
#include <stdlib.h>
33
33
 
34
 
#include "asm.h"
35
34
#include "attach.h"
36
35
#include "autostart.h"
37
36
#include "c64-cmdline-options.h"
38
37
#include "c64-resources.h"
39
38
#include "c64-snapshot.h"
40
39
#include "c64.h"
 
40
#include "c64acia.h"
41
41
#include "c64cart.h"
42
42
#include "c64cia.h"
43
 
#include "c64mem.h"
 
43
#include "c64export.h"
 
44
#include "c64fastiec.h"
 
45
#include "c64iec.h"
 
46
#include "c64keyboard.h"
 
47
#include "c64memrom.h"
 
48
#include "c64rsuser.h"
44
49
#include "c64tpi.h"
45
50
#include "c64ui.h"
46
51
#include "cartridge.h"
47
 
#include "ciatimer.h"
 
52
#include "cia.h"
48
53
#include "clkguard.h"
49
 
#include "console.h"
50
54
#include "datasette.h"
 
55
#include "debug.h"
51
56
#include "drive-cmdline-options.h"
52
57
#include "drive-resources.h"
53
 
#include "drive-snapshot.h"
54
58
#include "drive.h"
55
59
#include "drivecpu.h"
56
 
#include "iecdrive.h"
57
 
#include "interrupt.h"
 
60
#include "imagecontents.h"
58
61
#include "kbdbuf.h"
59
62
#include "keyboard.h"
60
63
#include "log.h"
 
64
#include "machine-drive.h"
 
65
#include "machine-printer.h"
 
66
#include "machine-video.h"
61
67
#include "machine.h"
62
68
#include "maincpu.h"
63
69
#include "mem.h"
64
 
#include "mon.h"
 
70
#include "monitor.h"
 
71
#include "parallel.h"
65
72
#include "patchrom.h"
66
73
#include "printer.h"
67
74
#include "psid.h"
68
75
#include "resources.h"
69
76
#include "reu.h"
 
77
#include "rs232drv.h"
 
78
#include "rsuser.h"
70
79
#include "screenshot.h"
71
80
#include "serial.h"
72
81
#include "sid-cmdline-options.h"
73
82
#include "sid-resources.h"
74
 
#include "sid-snapshot.h"
75
83
#include "sid.h"
76
 
#include "snapshot.h"
77
84
#include "sound.h"
78
85
#include "tape.h"
 
86
#include "tpi.h"
79
87
#include "traps.h"
80
88
#include "types.h"
81
 
#include "utils.h"
82
89
#include "vicii.h"
83
90
#include "video.h"
84
91
#include "vsidui.h"
85
92
#include "vsync.h"
86
93
 
87
 
#ifdef HAVE_RS232
88
 
#include "c64acia.h"
89
 
#include "c64rsuser.h"
90
 
#include "rs232.h"
91
 
#include "rsuser.h"
92
 
#endif
93
 
 
94
94
#ifdef HAVE_MOUSE
95
95
#include "mouse.h"
96
96
#endif
97
97
 
 
98
#ifdef HAVE_TFE
 
99
#include "tfe.h"
 
100
#endif
 
101
 
 
102
 
 
103
machine_context_t machine_context;
 
104
 
 
105
#define NUM_KEYBOARD_MAPPINGS 3
 
106
 
 
107
const char *machine_keymap_res_name_list[NUM_KEYBOARD_MAPPINGS] = {
 
108
    "KeymapSymFile", "KeymapSymDeFile", "KeymapPosFile"
 
109
};
 
110
 
 
111
char *machine_keymap_file_list[NUM_KEYBOARD_MAPPINGS] = {
 
112
    NULL, NULL, NULL
 
113
};
98
114
 
99
115
const char machine_name[] = "C64";
 
116
int machine_class = VICE_MACHINE_C64;
100
117
 
101
118
static void machine_vsync_hook(void);
102
119
 
103
120
/* ------------------------------------------------------------------------- */
104
121
 
105
 
/* Serial traps.  */
106
 
static trap_t c64_serial_traps[] = {
 
122
static const trap_t c64_serial_traps[] = {
107
123
    {
108
124
        "SerialListen",
109
125
        0xED24,
110
126
        0xEDAB,
111
127
        { 0x20, 0x97, 0xEE },
112
 
        serialattention,
113
 
        rom_read,
114
 
        rom_store
 
128
        serial_trap_attention,
 
129
        c64memrom_trap_read,
 
130
        c64memrom_trap_store
115
131
    },
116
132
    {
117
133
        "SerialSaListen",
118
134
        0xED36,
119
135
        0xEDAB,
120
136
        { 0x78, 0x20, 0x8E },
121
 
        serialattention,
122
 
        rom_read,
123
 
        rom_store
 
137
        serial_trap_attention,
 
138
        c64memrom_trap_read,
 
139
        c64memrom_trap_store
124
140
    },
125
141
    {
126
142
        "SerialSendByte",
127
143
        0xED40,
128
144
        0xEDAB,
129
145
        { 0x78, 0x20, 0x97 },
130
 
        serialsendbyte,
131
 
        rom_read,
132
 
        rom_store
 
146
        serial_trap_send,
 
147
        c64memrom_trap_read,
 
148
        c64memrom_trap_store
133
149
    },
134
150
    {
135
151
        "SerialReceiveByte",
136
152
        0xEE13,
137
153
        0xEDAB,
138
154
        { 0x78, 0xA9, 0x00 },
139
 
        serialreceivebyte,
140
 
        rom_read,
141
 
        rom_store
 
155
        serial_trap_receive,
 
156
        c64memrom_trap_read,
 
157
        c64memrom_trap_store
142
158
    },
143
159
    {
144
160
        "SerialReady",
145
161
        0xEEA9,
146
162
        0xEDAB,
147
163
        { 0xAD, 0x00, 0xDD },
148
 
        trap_serial_ready,
149
 
        rom_read,
150
 
        rom_store
 
164
        serial_trap_ready,
 
165
        c64memrom_trap_read,
 
166
        c64memrom_trap_store
151
167
    },
152
168
    {
153
169
        NULL,
161
177
};
162
178
 
163
179
/* Tape traps.  */
164
 
static trap_t c64_tape_traps[] = {
 
180
static const trap_t c64_tape_traps[] = {
165
181
    {
166
182
        "TapeFindHeader",
167
183
        0xF72F,
168
184
        0xF732,
169
 
        {0x20, 0x41, 0xF8},
 
185
        { 0x20, 0x41, 0xF8 },
170
186
        tape_find_header_trap,
171
 
        rom_read,
172
 
        rom_store
 
187
        c64memrom_trap_read,
 
188
        c64memrom_trap_store
173
189
    },
174
190
    {
175
191
        "TapeReceive",
176
192
        0xF8A1,
177
193
        0xFC93,
178
 
        {0x20, 0xBD, 0xFC},
 
194
        { 0x20, 0xBD, 0xFC },
179
195
        tape_receive_trap,
180
 
        rom_read,
181
 
        rom_store
 
196
        c64memrom_trap_read,
 
197
        c64memrom_trap_store
182
198
    },
183
199
    {
184
200
        NULL,
185
201
        0,
186
202
        0,
187
 
        {0, 0, 0},
 
203
        { 0, 0, 0 },
188
204
        NULL,
189
205
        NULL,
190
206
        NULL
191
207
    }
192
208
};
193
209
 
 
210
static const tape_init_t tapeinit = {
 
211
    0xb2,
 
212
    0x90,
 
213
    0x93,
 
214
    0x29f,
 
215
    0,
 
216
    0xc1,
 
217
    0xae,
 
218
    0x277,
 
219
    0xc6,
 
220
    c64_tape_traps,
 
221
    36 * 8,
 
222
    54 * 8,
 
223
    55 * 8,
 
224
    73 * 8,
 
225
    74 * 8,
 
226
    100 * 8
 
227
};
 
228
 
194
229
static log_t c64_log = LOG_ERR;
195
 
 
196
 
static long cycles_per_sec = C64_PAL_CYCLES_PER_SEC;
197
 
static long cycles_per_rfsh = C64_PAL_CYCLES_PER_RFSH;
198
 
static double rfsh_per_sec = C64_PAL_RFSH_PER_SEC;
 
230
static machine_timing_t machine_timing;
199
231
 
200
232
/* ------------------------------------------------------------------------ */
201
233
 
202
234
/* C64-specific resource initialization.  This is called before initializing
203
235
   the machine itself with `machine_init()'.  */
204
 
int machine_init_resources(void)
 
236
int machine_resources_init(void)
205
237
{
206
238
    if (traps_resources_init() < 0
207
239
        || vsync_resources_init() < 0
208
 
        || video_resources_init(VIDEO_RESOURCES_PAL) < 0
 
240
        || machine_video_resources_init() < 0
209
241
        || c64_resources_init() < 0
 
242
        || c64export_resources_init() < 0
210
243
        || reu_resources_init() < 0
211
 
        || vic_ii_resources_init() < 0
 
244
#ifdef HAVE_TFE
 
245
        || tfe_resources_init() < 0
 
246
#endif
 
247
        || vicii_resources_init() < 0
212
248
        || sound_resources_init() < 0
213
249
        || sid_resources_init() < 0
214
 
#ifdef HAVE_RS232
215
250
        || acia1_resources_init() < 0
216
 
        || rs232_resources_init() < 0
 
251
        || rs232drv_resources_init() < 0
217
252
        || rsuser_resources_init() < 0
218
 
#endif
 
253
        || serial_resources_init() < 0
219
254
        || printer_resources_init() < 0
220
255
#ifdef HAVE_MOUSE
221
256
        || mouse_resources_init() < 0
222
257
#endif
 
258
#ifndef COMMON_KBD
223
259
        || kbd_resources_init() < 0
 
260
#endif
224
261
        || drive_resources_init() < 0
225
262
        || datasette_resources_init() < 0
226
263
        || cartridge_resources_init() < 0
233
270
    return 0;
234
271
}
235
272
 
 
273
void machine_resources_shutdown(void)
 
274
{
 
275
    serial_shutdown();
 
276
    video_resources_shutdown();
 
277
    c64_resources_shutdown();
 
278
    reu_resources_shutdown();
 
279
    sound_resources_shutdown();
 
280
    rs232drv_resources_shutdown();
 
281
    printer_resources_shutdown();
 
282
    drive_resources_shutdown();
 
283
    cartridge_resources_shutdown();
 
284
}
 
285
 
236
286
/* C64-specific command-line option initialization.  */
237
 
int machine_init_cmdline_options(void)
 
287
int machine_cmdline_options_init(void)
238
288
{
239
289
    if (vsid_mode) {
240
290
        if (sound_cmdline_options_init() < 0
251
301
        || video_init_cmdline_options() < 0
252
302
        || c64_cmdline_options_init() < 0
253
303
        || reu_cmdline_options_init() < 0
254
 
        || vic_ii_cmdline_options_init() < 0
 
304
#ifdef HAVE_TFE
 
305
        || tfe_cmdline_options_init() < 0
 
306
#endif
 
307
        || vicii_cmdline_options_init() < 0
255
308
        || sound_cmdline_options_init() < 0
256
309
        || sid_cmdline_options_init() < 0
257
 
#ifdef HAVE_RS232
258
310
        || acia1_cmdline_options_init() < 0
259
 
        || rs232_cmdline_options_init() < 0
 
311
        || rs232drv_cmdline_options_init() < 0
260
312
        || rsuser_cmdline_options_init() < 0
261
 
#endif
 
313
        || serial_cmdline_options_init() < 0
262
314
        || printer_cmdline_options_init() < 0
263
315
#ifdef HAVE_MOUSE
264
316
        || mouse_cmdline_options_init() < 0
265
317
#endif
 
318
#ifndef COMMON_KBD
266
319
        || kbd_cmdline_options_init() < 0
 
320
#endif
267
321
        || drive_cmdline_options_init() < 0
268
322
        || datasette_cmdline_options_init() < 0
269
323
        || cartridge_cmdline_options_init() < 0
275
329
 
276
330
static void c64_monitor_init(void)
277
331
{
 
332
    unsigned int dnr;
278
333
    monitor_cpu_type_t asm6502;
279
 
    monitor_cpu_type_t *asmarray[2];
 
334
    monitor_cpu_type_t *asmarray[2] = { &asm6502, NULL };
 
335
    monitor_interface_t *drive_interface_init[DRIVE_NUM];
280
336
 
281
337
    asm6502_init(&asm6502);
282
338
 
283
 
    asmarray[0] = &asm6502;
284
 
    asmarray[1] = NULL;
 
339
    for (dnr = 0; dnr < DRIVE_NUM; dnr++)
 
340
        drive_interface_init[dnr] = drivecpu_monitor_interface_get(dnr);
285
341
 
286
342
    /* Initialize the monitor.  */
287
 
    monitor_init(&maincpu_monitor_interface, drive0_get_monitor_interface_ptr(),
288
 
                 drive1_get_monitor_interface_ptr(), asmarray);
 
343
    monitor_init(maincpu_monitor_interface_get(), drive_interface_init,
 
344
                 asmarray);
 
345
}
 
346
 
 
347
void machine_setup_context(void)
 
348
{
 
349
    cia1_setup_context(&machine_context);
 
350
    cia2_setup_context(&machine_context);
 
351
    tpi_setup_context(&machine_context);
 
352
    machine_printer_setup_context(&machine_context);
289
353
}
290
354
 
291
355
/* C64-specific initialization.  */
292
 
int machine_init(void)
 
356
int machine_specific_init(void)
293
357
{
294
358
    c64_log = log_open("C64");
295
359
 
296
 
    maincpu_init();
297
 
 
298
360
    if (mem_load() < 0)
299
361
        return -1;
300
362
 
301
 
    if (vsid_mode)
302
 
    {
 
363
    if (vsid_mode) {
303
364
        psid_init_driver();
304
365
    }
305
366
 
306
 
    if (!vsid_mode)
307
 
    {
 
367
    if (!vsid_mode) {
308
368
        /* Setup trap handling.  */
309
369
        traps_init();
310
370
 
311
371
        /* Initialize serial traps.  */
312
 
        if (serial_init(c64_serial_traps, 0xa4) < 0)
 
372
        if (serial_init(c64_serial_traps) < 0)
313
373
            return -1;
314
374
 
 
375
        serial_trap_init(0xa4);
 
376
        serial_iec_bus_init();
 
377
 
315
378
        /* Initialize drives. */
316
379
        file_system_init();
317
380
 
318
 
#ifdef HAVE_RS232
319
381
        /* Initialize RS232 handler.  */
320
 
        rs232_init();
 
382
        rs232drv_init();
321
383
        c64_rsuser_init();
322
 
#endif
323
384
 
324
385
        /* Initialize print devices.  */
325
386
        printer_init();
326
387
 
327
388
        /* Initialize the tape emulation.  */
328
 
        tape_init(0xb2, 0x90, 0x93, 0x29f, 0, 0xc1, 0xae, 0x277, 0xc6,
329
 
                  c64_tape_traps);
 
389
        tape_init(&tapeinit);
330
390
 
331
391
        /* Initialize the datasette emulation.  */
332
392
        datasette_init();
333
393
 
334
394
        /* Fire up the hardware-level drive emulation.  */
335
 
        drive_init(C64_PAL_CYCLES_PER_SEC, C64_NTSC_CYCLES_PER_SEC);
 
395
        drive_init();
336
396
 
337
397
        /* Initialize autostart.  */
338
 
        autostart_init((CLOCK)(3 * rfsh_per_sec * cycles_per_rfsh),
 
398
        autostart_init((CLOCK)(3 * C64_PAL_RFSH_PER_SEC
 
399
                       * C64_PAL_CYCLES_PER_RFSH),
339
400
                       1, 0xcc, 0xd1, 0xd3, 0xd5);
340
401
    }
341
402
 
342
 
    /* Initialize the VIC-II emulation. */
343
 
    if (!vic_ii_init() && !console_mode && !vsid_mode)
 
403
    if (vicii_init(VICII_STANDARD) == NULL && !console_mode && !vsid_mode)
344
404
        return -1;
345
405
 
346
 
    vic_ii_enable_extended_keyboard_rows(0);
347
 
 
348
 
    cia1_enable_extended_keyboard_rows(0);
349
 
 
350
 
    ciat_init_table();
351
 
    cia1_init();
352
 
    cia2_init();
353
 
 
354
 
    if (!vsid_mode)
355
 
    {
356
 
        tpi_init();
357
 
 
358
 
#ifdef HAVE_RS232
 
406
    cia1_init(machine_context.cia1);
 
407
    cia2_init(machine_context.cia2);
 
408
 
 
409
    if (!vsid_mode) {
 
410
        tpi_init(machine_context.tpi1);
 
411
 
359
412
        acia1_init();
360
 
#endif
361
413
 
 
414
#ifndef COMMON_KBD
362
415
        /* Initialize the keyboard.  */
363
416
        if (c64_kbd_init() < 0)
364
417
            return -1;
 
418
#endif
 
419
 
 
420
        c64keyboard_init();
365
421
    }
366
422
 
367
423
    c64_monitor_init();
368
424
 
369
425
    /* Initialize vsync and register our hook function.  */
370
426
    vsync_init(machine_vsync_hook);
371
 
    vsync_set_machine_parameter(rfsh_per_sec, cycles_per_sec);
 
427
    vsync_set_machine_parameter(machine_timing.rfsh_per_sec,
 
428
                                machine_timing.cycles_per_sec);
372
429
 
373
430
    /* Initialize sound.  Notice that this does not really open the audio
374
431
       device yet.  */
375
 
    sound_init(cycles_per_sec, cycles_per_rfsh);
 
432
    sound_init(machine_timing.cycles_per_sec, machine_timing.cycles_per_rfsh);
376
433
 
377
434
    /* Initialize keyboard buffer.  */
378
 
    kbd_buf_init(631, 198, 10,
379
 
                 (CLOCK)(rfsh_per_sec * cycles_per_rfsh));
 
435
    kbdbuf_init(631, 198, 10, (CLOCK)(machine_timing.rfsh_per_sec
 
436
                * machine_timing.cycles_per_rfsh));
380
437
 
381
438
    /* Initialize the C64-specific part of the UI.  */
382
439
    if (!console_mode) {
384
441
        if (vsid_mode)
385
442
            vsid_ui_init();
386
443
        else
387
 
            c64_ui_init();
 
444
            c64ui_init();
388
445
    }
389
446
 
390
447
    if (!vsid_mode)
392
449
        /* Initialize the REU.  */
393
450
        reu_init();
394
451
 
 
452
#ifdef HAVE_TFE
 
453
        /* Initialize the TFE.  */
 
454
        tfe_init();
 
455
#endif
 
456
 
395
457
#ifdef HAVE_MOUSE
396
458
        /* Initialize mouse support (if present).  */
397
459
        mouse_init();
398
460
#endif
399
461
 
400
 
        iec_init();
 
462
        c64iec_init();
 
463
        c64fastiec_init();
401
464
 
402
465
        cartridge_init();
403
466
    }
404
467
 
 
468
    machine_drive_stub();
 
469
 
405
470
    return 0;
406
471
}
407
472
 
408
473
/* C64-specific reset sequence.  */
409
474
void machine_specific_reset(void)
410
475
{
411
 
    serial_reset();
 
476
    serial_traps_reset();
412
477
 
413
 
    cia1_reset();
414
 
    cia2_reset();
 
478
    ciacore_reset(machine_context.cia1);
 
479
    ciacore_reset(machine_context.cia2);
415
480
    sid_reset();
416
481
 
417
 
    if (!vsid_mode)
418
 
    {
419
 
        tpi_reset();
 
482
    if (!vsid_mode) {
 
483
        tpicore_reset(machine_context.tpi1);
420
484
 
421
 
#ifdef HAVE_RS232
422
485
        acia1_reset();
423
 
        rs232_reset();
 
486
        rs232drv_reset();
424
487
        rsuser_reset();
425
 
#endif
426
488
 
427
489
        printer_reset();
428
490
 
431
493
    }
432
494
 
433
495
    /* The VIC-II must be the *last* to be reset.  */
434
 
    vic_ii_reset();
 
496
    vicii_reset();
435
497
 
436
 
    if (vsid_mode)
437
 
    {
 
498
    if (vsid_mode) {
438
499
        psid_init_tune();
439
500
        return;
440
501
    }
441
502
 
442
 
    autostart_reset();
 
503
    cartridge_reset();
443
504
    drive_reset();
444
505
    datasette_reset();
445
506
    reu_reset();
446
507
}
447
508
 
448
 
void machine_powerup(void)
 
509
void machine_specific_powerup(void)
449
510
{
450
 
    /* Hard reset unloads PSID. */
451
 
    if (vsid_mode) {
452
 
        machine_play_psid(-1);
453
 
    }
454
 
 
455
 
    mem_powerup();
456
 
    vic_ii_reset_registers();
457
 
    maincpu_trigger_reset();
 
511
    vicii_reset_registers();
458
512
}
459
513
 
460
 
void machine_shutdown(void)
 
514
void machine_specific_shutdown(void)
461
515
{
462
516
    /* Detach all disks.  */
463
517
    if (!vsid_mode) {
465
519
    }
466
520
 
467
521
    /* and the tape */
468
 
    tape_detach_image();
469
 
 
470
 
    console_close_all();
 
522
    tape_image_detach_internal(1);
 
523
 
 
524
    /* and cartridge */
 
525
    cartridge_detach_image();
 
526
 
 
527
    ciacore_shutdown(machine_context.cia1);
 
528
    ciacore_shutdown(machine_context.cia2);
 
529
    tpicore_shutdown(machine_context.tpi1);
471
530
 
472
531
    /* close the video chip(s) */
473
 
    vic_ii_free();
 
532
    vicii_shutdown();
474
533
 
475
534
    reu_shutdown();
476
535
 
 
536
#ifdef HAVE_TFE
 
537
    /* Shutdown the TFE.  */
 
538
    tfe_shutdown();
 
539
#endif
 
540
 
477
541
    if (vsid_mode) {
478
542
        vsid_ui_close();
479
543
    }
 
544
 
 
545
    c64ui_shutdown();
480
546
}
481
547
 
482
548
void machine_handle_pending_alarms(int num_write_cycles)
483
549
{
484
 
     vic_ii_handle_pending_alarms(num_write_cycles);
 
550
    vicii_handle_pending_alarms_external(num_write_cycles);
485
551
}
486
552
 
487
553
/* ------------------------------------------------------------------------- */
493
559
 
494
560
    if (vsid_mode) {
495
561
        unsigned int playtime;
496
 
        static unsigned int time=0;
 
562
        static unsigned int time = 0;
497
563
 
498
 
        playtime = (psid_increment_frames() * cycles_per_rfsh) / cycles_per_sec;        if (playtime!=time)
499
 
        {
 
564
        playtime = (psid_increment_frames() * machine_timing.cycles_per_rfsh)
 
565
                   / machine_timing.cycles_per_sec;
 
566
        if (playtime != time) {
500
567
            vsid_ui_display_time(playtime);
501
 
            time=playtime;
 
568
            time = playtime;
502
569
        }
503
 
        clk_guard_prevent_overflow(&maincpu_clk_guard);
 
570
        clk_guard_prevent_overflow(maincpu_clk_guard);
504
571
        return;
505
572
    }
506
573
 
508
575
 
509
576
    autostart_advance();
510
577
 
511
 
    sub = clk_guard_prevent_overflow(&maincpu_clk_guard);
 
578
    screenshot_record();
 
579
 
 
580
    sub = clk_guard_prevent_overflow(maincpu_clk_guard);
512
581
 
513
582
    /* The drive has to deal both with our overflowing and its own one, so
514
583
       it is called even when there is no overflowing in the main CPU.  */
515
 
    /* FIXME: Do we have to check drive_enabled here?  */
516
 
    drive_prevent_clk_overflow(sub, 0);
517
 
    drive_prevent_clk_overflow(sub, 1);
 
584
    drivecpu_prevent_clk_overflow_all(sub);
518
585
}
519
586
 
520
587
int machine_set_restore_key(int v)
521
588
{
522
 
    maincpu_set_nmi(I_RESTORE, v ? 1 : 0);
 
589
    c64keyboard_restore_key(v);
523
590
    return 1;
524
591
}
525
592
 
527
594
 
528
595
long machine_get_cycles_per_second(void)
529
596
{
530
 
    return cycles_per_sec;
 
597
    return machine_timing.cycles_per_sec;
 
598
}
 
599
 
 
600
void machine_get_line_cycle(unsigned int *line, unsigned int *cycle)
 
601
{
 
602
    *line = (unsigned int)((maincpu_clk) / machine_timing.cycles_per_line
 
603
            % machine_timing.screen_lines);
 
604
 
 
605
    *cycle = (unsigned int)((maincpu_clk) % machine_timing.cycles_per_line);
531
606
}
532
607
 
533
608
void machine_change_timing(int timeval)
534
609
{
535
 
    maincpu_trigger_reset();
536
 
 
537
610
    switch (timeval) {
538
 
      case DRIVE_SYNC_PAL:
539
 
        cycles_per_sec = C64_PAL_CYCLES_PER_SEC;
540
 
        cycles_per_rfsh = C64_PAL_CYCLES_PER_RFSH;
541
 
        rfsh_per_sec = C64_PAL_RFSH_PER_SEC;
542
 
        break;
543
 
      case DRIVE_SYNC_NTSC:
544
 
        cycles_per_sec = C64_NTSC_CYCLES_PER_SEC;
545
 
        cycles_per_rfsh = C64_NTSC_CYCLES_PER_RFSH;
546
 
        rfsh_per_sec = C64_NTSC_RFSH_PER_SEC;
547
 
        break;
548
 
      case DRIVE_SYNC_NTSCOLD:
549
 
        cycles_per_sec = C64_NTSCOLD_CYCLES_PER_SEC;
550
 
        cycles_per_rfsh = C64_NTSCOLD_CYCLES_PER_RFSH;
551
 
        rfsh_per_sec = C64_NTSCOLD_RFSH_PER_SEC;
 
611
      case MACHINE_SYNC_PAL:
 
612
        machine_timing.cycles_per_sec = C64_PAL_CYCLES_PER_SEC;
 
613
        machine_timing.cycles_per_rfsh = C64_PAL_CYCLES_PER_RFSH;
 
614
        machine_timing.rfsh_per_sec = C64_PAL_RFSH_PER_SEC;
 
615
        machine_timing.cycles_per_line = C64_PAL_CYCLES_PER_LINE;
 
616
        machine_timing.screen_lines = C64_PAL_SCREEN_LINES;
 
617
        break;
 
618
      case MACHINE_SYNC_NTSC:
 
619
        machine_timing.cycles_per_sec = C64_NTSC_CYCLES_PER_SEC;
 
620
        machine_timing.cycles_per_rfsh = C64_NTSC_CYCLES_PER_RFSH;
 
621
        machine_timing.rfsh_per_sec = C64_NTSC_RFSH_PER_SEC;
 
622
        machine_timing.cycles_per_line = C64_NTSC_CYCLES_PER_LINE;
 
623
        machine_timing.screen_lines = C64_NTSC_SCREEN_LINES;
 
624
        break;
 
625
      case MACHINE_SYNC_NTSCOLD:
 
626
        machine_timing.cycles_per_sec = C64_NTSCOLD_CYCLES_PER_SEC;
 
627
        machine_timing.cycles_per_rfsh = C64_NTSCOLD_CYCLES_PER_RFSH;
 
628
        machine_timing.rfsh_per_sec = C64_NTSCOLD_RFSH_PER_SEC;
 
629
        machine_timing.cycles_per_line = C64_NTSCOLD_CYCLES_PER_LINE;
 
630
        machine_timing.screen_lines = C64_NTSCOLD_SCREEN_LINES;
552
631
        break;
553
632
      default:
554
633
        log_error(c64_log, "Unknown machine timing.");
555
634
    }
556
635
 
557
 
    vsync_set_machine_parameter(rfsh_per_sec, cycles_per_sec);
558
 
    sound_set_machine_parameter(cycles_per_sec, cycles_per_rfsh);
 
636
    vsync_set_machine_parameter(machine_timing.rfsh_per_sec,
 
637
                                machine_timing.cycles_per_sec);
 
638
    sound_set_machine_parameter(machine_timing.cycles_per_sec,
 
639
                                machine_timing.cycles_per_rfsh);
 
640
    debug_set_machine_parameter(machine_timing.cycles_per_line,
 
641
                                machine_timing.screen_lines);
 
642
    drive_set_machine_parameter(machine_timing.cycles_per_sec);
 
643
    serial_iec_device_set_machine_parameter(machine_timing.cycles_per_sec);
 
644
    sid_set_machine_parameter(machine_timing.cycles_per_sec);
 
645
    clk_guard_set_clk_base(maincpu_clk_guard, machine_timing.cycles_per_rfsh);
 
646
 
 
647
    vicii_change_timing(&machine_timing);
 
648
 
 
649
    machine_trigger_reset(MACHINE_RESET_MODE_HARD);
559
650
}
560
651
 
561
652
/* ------------------------------------------------------------------------- */
562
653
 
563
 
#define SNAP_MAJOR 1
564
 
#define SNAP_MINOR 0
565
 
 
566
 
int machine_class = VICE_MACHINE_C64;
567
 
 
568
 
int machine_write_snapshot(const char *name, int save_roms, int save_disks)
 
654
int machine_write_snapshot(const char *name, int save_roms, int save_disks,
 
655
                           int event_mode)
569
656
{
570
 
    snapshot_t *s;
571
 
 
572
 
    s = snapshot_create(name, ((BYTE)(SNAP_MAJOR)), ((BYTE)(SNAP_MINOR)),
573
 
                        machine_name);
574
 
    if (s == NULL)
575
 
        return -1;
576
 
 
577
 
    /* Execute drive CPUs to get in sync with the main CPU.  */
578
 
    if (drive[0].enable)
579
 
        drive0_cpu_execute(clk);
580
 
    if (drive[1].enable)
581
 
        drive1_cpu_execute(clk);
582
 
 
583
 
    if (maincpu_snapshot_write_module(s) < 0
584
 
        || c64_snapshot_write_module(s, save_roms) < 0
585
 
        || cia1_snapshot_write_module(s) < 0
586
 
        || cia2_snapshot_write_module(s) < 0
587
 
        || sid_snapshot_write_module(s) < 0
588
 
        || drive_snapshot_write_module(s, save_disks, save_roms) < 0
589
 
        || vic_ii_snapshot_write_module(s) < 0) {
590
 
        snapshot_close(s);
591
 
        util_file_remove(name);
592
 
        return -1;
593
 
    }
594
 
 
595
 
    snapshot_close(s);
596
 
    return 0;
 
657
    return c64_snapshot_write(name, save_roms, save_disks, event_mode);
597
658
}
598
659
 
599
 
int machine_read_snapshot(const char *name)
 
660
int machine_read_snapshot(const char *name, int event_mode)
600
661
{
601
 
    snapshot_t *s;
602
 
    BYTE minor, major;
603
 
 
604
 
    s = snapshot_open(name, &major, &minor, machine_name);
605
 
    if (s == NULL)
606
 
        return -1;
607
 
 
608
 
    if (major != SNAP_MAJOR || minor != SNAP_MINOR) {
609
 
        log_error(c64_log,
610
 
                  "Snapshot version (%d.%d) not valid: expecting %d.%d.",
611
 
                  major, minor, SNAP_MAJOR, SNAP_MINOR);
612
 
        goto fail;
613
 
    }
614
 
 
615
 
    vic_ii_prepare_for_snapshot();
616
 
 
617
 
    if (maincpu_snapshot_read_module(s) < 0
618
 
        || c64_snapshot_read_module(s) < 0
619
 
        || cia1_snapshot_read_module(s) < 0
620
 
        || cia2_snapshot_read_module(s) < 0
621
 
        || sid_snapshot_read_module(s) < 0
622
 
        || drive_snapshot_read_module(s) < 0
623
 
        || vic_ii_snapshot_read_module(s) < 0)
624
 
        goto fail;
625
 
 
626
 
    snapshot_close(s);
627
 
 
628
 
    return 0;
629
 
 
630
 
fail:
631
 
    if (s != NULL)
632
 
        snapshot_close(s);
633
 
    maincpu_trigger_reset();
634
 
    return -1;
 
662
    return c64_snapshot_read(name, event_mode);
635
663
}
636
664
 
637
665
/* ------------------------------------------------------------------------- */
650
678
    psid_set_tune(tune);
651
679
}
652
680
 
653
 
int machine_screenshot(screenshot_t *screenshot, unsigned int wn)
654
 
{
655
 
    if (wn == 0)
656
 
        return vic_ii_screenshot(screenshot);
657
 
    return -1;
658
 
}
659
 
 
660
 
int machine_canvas_screenshot(screenshot_t *screenshot,
661
 
                              struct video_canvas_s *canvas)
662
 
{
663
 
    if (canvas == vic_ii_get_canvas())
664
 
        return vic_ii_screenshot(screenshot);
665
 
    return -1;
666
 
}
667
 
 
668
 
void machine_video_refresh(void)
669
 
{
670
 
     vic_ii_video_refresh();
 
681
int machine_screenshot(screenshot_t *screenshot, struct video_canvas_s *canvas)
 
682
{
 
683
    if (canvas != vicii_get_canvas())
 
684
        return -1;
 
685
 
 
686
    vicii_screenshot(screenshot);
 
687
    return 0;
 
688
}
 
689
 
 
690
int machine_canvas_async_refresh(struct canvas_refresh_s *refresh,
 
691
                                 struct video_canvas_s *canvas)
 
692
{
 
693
    if (canvas != vicii_get_canvas())
 
694
        return -1;
 
695
 
 
696
    vicii_async_refresh(refresh);
 
697
    return 0;
671
698
}
672
699
 
673
700
void machine_update_memory_ptrs(void)
674
701
{
675
 
     vic_ii_update_memory_ptrs_external();
 
702
    vicii_update_memory_ptrs_external();
676
703
}
677
704
 
678
705
int machine_sid2_check_range(unsigned int sid2_adr)
686
713
    return -1;
687
714
}
688
715
 
 
716
unsigned int machine_num_keyboard_mappings(void)
 
717
{
 
718
    return NUM_KEYBOARD_MAPPINGS;
 
719
}
 
720
 
 
721
struct image_contents_s *machine_diskcontents_bus_read(unsigned int unit)
 
722
{
 
723
    return diskcontents_iec_read(unit);
 
724
}
 
725
 
 
726
BYTE machine_tape_type_default(void)
 
727
{
 
728
    return TAPE_CAS_TYPE_BAS;
 
729
}
 
730