File: traps.c

package info (click to toggle)
vice 3.7.1%2Bdfsg1-2
  • links: PTS
  • area: contrib
  • in suites: bookworm
  • size: 49,876 kB
  • sloc: ansic: 431,530; cpp: 35,357; sh: 9,747; makefile: 7,010; perl: 1,822; yacc: 1,197; lex: 589; sed: 184; objc: 115; xml: 36
file content (426 lines) | stat: -rw-r--r-- 13,572 bytes parent folder | download
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
/*
 * traps.c - Allow VICE to replace ROM code with C function calls.
 *
 * Written by
 *  Teemu Rantanen <tvr@cs.hut.fi>
 *  Jarkko Sonninen <sonninen@lut.fi>
 *  Ettore Perazzoli <ettore@comm2000.it>
 *  Andreas Boose <viceteam@t-online.de>
 *
 * This file is part of VICE, the Versatile Commodore Emulator.
 * See README for copyright notice.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307  USA.
 *
 */

/* #define DEBUG_TRAPS */

#include "vice.h"

#include <stdio.h>
#include <stdlib.h>

#include "cmdline.h"
#include "lib.h"
#include "log.h"
#include "machine.h"
#include "machine-bus.h"
#include "maincpu.h"
#include "mem.h"
#include "resources.h"
#include "traps.h"
#include "types.h"
#include "wdc65816.h"

#ifdef DEBUG_TRAPS
#define DBG(x)  log_debug x
#else
#define DBG(x)
#endif

typedef struct traplist_s {
    struct traplist_s *next;
    const trap_t *trap;
} traplist_t;

static traplist_t *traplist = NULL;

static int install_trap(const trap_t *t);
static int remove_trap(const trap_t *t);

static log_t traps_log = LOG_ERR;

/* ------------------------------------------------------------------------- */

/* Trap-related resources.  */

/* Flag: Should we avoid installing traps at all?  */
static int traps_enabled = 0;

#define MAX_DEVICES 15  /* FIXME: is there another constant we can use instead? */
int traps_enabled_device[MAX_DEVICES];

static void set_traps_status(int enabled)
{
    int new_value = enabled ? 1 : 0;

    DBG(("set_traps_status: %d", new_value));

    if ((!traps_enabled && new_value) || (traps_enabled && !new_value)) {
        if (!new_value) {
            /* Traps have been disabled.  */
            traplist_t *p;

            for (p = traplist; p != NULL; p = p->next) {
                remove_trap(p->trap);
            }
        } else {
            /* Traps have been enabled.  */
            traplist_t *p;

            for (p = traplist; p != NULL; p = p->next) {
                install_trap(p->trap);
            }
        }
    }

    traps_enabled = new_value;
}

static int set_traps_enabled(int val, void *param)
{
    unsigned int enabled = 0;
    unsigned int unit = vice_ptr_to_int(param);
    unsigned int i;

    DBG(("set_traps_enabled %d device: %u", val, unit));
    traps_enabled_device[unit] = val ? 1 : 0;

    /* check all devices, enable traps if any of them is enabled */
    for (i = 1; i < MAX_DEVICES; i++) {
        enabled |= traps_enabled_device[i];
    }
    set_traps_status(enabled);

    machine_bus_status_virtualdevices_set(unit, enabled);
    return 0;
}

static resource_int_t resources_int[] = {
    /* tape */
    { "VirtualDevice1", 0, RES_EVENT_SAME, NULL,
      &traps_enabled_device[1], set_traps_enabled, (void*)1 },
    { "VirtualDevice2", 0, RES_EVENT_SAME, NULL,
      &traps_enabled_device[2], set_traps_enabled, (void*)2 },
    /* printers */
    { "VirtualDevice4", 0, RES_EVENT_SAME, NULL,
      &traps_enabled_device[4], set_traps_enabled, (void*)4 },
    { "VirtualDevice5", 0, RES_EVENT_SAME, NULL,
      &traps_enabled_device[5], set_traps_enabled, (void*)5 },
    { "VirtualDevice6", 0, RES_EVENT_SAME, NULL,
      &traps_enabled_device[6], set_traps_enabled, (void*)6 },
    { "VirtualDevice7", 0, RES_EVENT_SAME, NULL,
      &traps_enabled_device[7], set_traps_enabled, (void*)7 },
    /* disk drives */
    { "VirtualDevice8", 0, RES_EVENT_SAME, NULL,
      &traps_enabled_device[8], set_traps_enabled, (void*)8 },
    { "VirtualDevice9", 0, RES_EVENT_SAME, NULL,
      &traps_enabled_device[9], set_traps_enabled, (void*)9 },
    { "VirtualDevice10", 0, RES_EVENT_SAME, NULL,
      &traps_enabled_device[10], set_traps_enabled, (void*)10 },
    { "VirtualDevice11", 0, RES_EVENT_SAME, NULL,
      &traps_enabled_device[11], set_traps_enabled, (void*)11 },
    RESOURCE_INT_LIST_END
};

#if 0
static resource_int_t resources_int_ieee[] = {
    /* tape */
    { "VirtualDevice1", 1, RES_EVENT_SAME, NULL,
      &traps_enabled_device[1], set_traps_enabled, (void*)1 },
    { "VirtualDevice2", 1, RES_EVENT_SAME, NULL,
      &traps_enabled_device[2], set_traps_enabled, (void*)2 },
    /* printers */
    { "VirtualDevice4", 1, RES_EVENT_SAME, NULL,
      &traps_enabled_device[4], set_traps_enabled, (void*)4 },
    { "VirtualDevice5", 1, RES_EVENT_SAME, NULL,
      &traps_enabled_device[5], set_traps_enabled, (void*)5 },
    { "VirtualDevice6", 1, RES_EVENT_SAME, NULL,
      &traps_enabled_device[6], set_traps_enabled, (void*)6 },
    { "VirtualDevice7", 1, RES_EVENT_SAME, NULL,
      &traps_enabled_device[7], set_traps_enabled, (void*)7 },
    /* disk drives */
    { "VirtualDevice8", 1, RES_EVENT_SAME, NULL,
      &traps_enabled_device[8], set_traps_enabled, (void*)8 },
    { "VirtualDevice9", 1, RES_EVENT_SAME, NULL,
      &traps_enabled_device[9], set_traps_enabled, (void*)9 },
    { "VirtualDevice10", 1, RES_EVENT_SAME, NULL,
      &traps_enabled_device[10], set_traps_enabled, (void*)10 },
    { "VirtualDevice11", 1, RES_EVENT_SAME, NULL,
      &traps_enabled_device[11], set_traps_enabled, (void*)11 },
    RESOURCE_INT_LIST_END
};
#endif

int traps_resources_init(void)
{
#if 0
    /* the IEEE488 based machines do not use "device traps" (ROM patches),
       instead the virtual devices are actually interfaced to the IEEE bus.
       this makes them much more reliably, which is why we can enable them
       by default here. */
    if ((machine_class == VICE_MACHINE_PET) ||
        (machine_class == VICE_MACHINE_CBM5x0) ||
        (machine_class == VICE_MACHINE_CBM6x0)) {
        return resources_register_int(resources_int_ieee);
    }
#endif
    return resources_register_int(resources_int);
}

/* ------------------------------------------------------------------------- */

/* Trap-related command-line options.  */

static const cmdline_option_t cmdline_options[] =
{
    /* tape */
    { "-virtualdev1", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice1", (resource_value_t)1,
      NULL, "Enable general mechanisms for fast disk/tape emulation" },
    { "+virtualdev1", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice1", (resource_value_t)0,
      NULL, "Disable general mechanisms for fast disk/tape emulation" },
    { "-virtualdev2", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice2", (resource_value_t)1,
      NULL, "Enable general mechanisms for fast disk/tape emulation" },
    { "+virtualdev2", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice2", (resource_value_t)0,
      NULL, "Disable general mechanisms for fast disk/tape emulation" },
    /* printers */
    { "-virtualdev4", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice4", (resource_value_t)1,
      NULL, "Enable general mechanisms for fast disk/tape emulation" },
    { "+virtualdev4", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice4", (resource_value_t)0,
      NULL, "Disable general mechanisms for fast disk/tape emulation" },
    { "-virtualdev5", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice5", (resource_value_t)1,
      NULL, "Enable general mechanisms for fast disk/tape emulation" },
    { "+virtualdev5", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice5", (resource_value_t)0,
      NULL, "Disable general mechanisms for fast disk/tape emulation" },
    { "-virtualdev6", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice6", (resource_value_t)1,
      NULL, "Enable general mechanisms for fast disk/tape emulation" },
    { "+virtualdev6", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice6", (resource_value_t)0,
      NULL, "Disable general mechanisms for fast disk/tape emulation" },
    { "-virtualdev7", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice7", (resource_value_t)1,
      NULL, "Enable general mechanisms for fast disk/tape emulation" },
    { "+virtualdev7", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice7", (resource_value_t)0,
      NULL, "Disable general mechanisms for fast disk/tape emulation" },
    /* disk drives */
    { "-virtualdev8", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice8", (resource_value_t)1,
      NULL, "Enable general mechanisms for fast disk/tape emulation" },
    { "+virtualdev8", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice8", (resource_value_t)0,
      NULL, "Disable general mechanisms for fast disk/tape emulation" },
    { "-virtualdev9", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice9", (resource_value_t)1,
      NULL, "Enable general mechanisms for fast disk/tape emulation" },
    { "+virtualdev9", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice9", (resource_value_t)0,
      NULL, "Disable general mechanisms for fast disk/tape emulation" },
    { "-virtualdev10", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice10", (resource_value_t)1,
      NULL, "Enable general mechanisms for fast disk/tape emulation" },
    { "+virtualdev10", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice10", (resource_value_t)0,
      NULL, "Disable general mechanisms for fast disk/tape emulation" },
    { "-virtualdev11", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice11", (resource_value_t)1,
      NULL, "Enable general mechanisms for fast disk/tape emulation" },
    { "+virtualdev11", SET_RESOURCE, CMDLINE_ATTRIB_NONE,
      NULL, NULL, "VirtualDevice11", (resource_value_t)0,
      NULL, "Disable general mechanisms for fast disk/tape emulation" },
    CMDLINE_LIST_END
};

int traps_cmdline_options_init(void)
{
    return cmdline_register_options(cmdline_options);
}

/* ------------------------------------------------------------------------- */

void traps_init(void)
{
    traps_log = log_open("Traps");
}

void traps_shutdown(void)
{
    traplist_t *list, *list_next;

    list = traplist;

    while (list != NULL) {
        list_next = list->next;
        lib_free(list);
        list = list_next;
    }
}

static int install_trap(const trap_t *t)
{
    int i;

    for (i = 0; i < 3; i++) {
        if ((t->readfunc)((uint16_t)(t->address + i)) != t->check[i]) {
            log_error(traps_log,
                      "Incorrect checkbyte for trap `%s'.  Not installed.",
                      t->name);
            return -1;
        }
    }

    log_verbose("Trap '%s' installed.", t->name);
    (t->storefunc)(t->address, TRAP_OPCODE);

    return 0;
}

int traps_add(const trap_t *trap)
{
    traplist_t *p;

    p = lib_malloc(sizeof(traplist_t));
    p->next = traplist;
    p->trap = trap;
    traplist = p;

    if (traps_enabled) {
        install_trap(trap);
    } else {
        log_verbose("Traps are disabled, trap '%s' not installed.", trap->name);
    }

    return 0;
}

static int remove_trap(const trap_t *trap)
{
    if ((trap->readfunc)(trap->address) != TRAP_OPCODE) {
        log_error(traps_log, "No trap `%s' installed?", trap->name);
        return -1;
    }
    log_verbose("Trap '%s' disabled.", trap->name);

    (trap->storefunc)(trap->address, trap->check[0]);
    return 0;
}

int traps_remove(const trap_t *trap)
{
    traplist_t *p = traplist, *prev = NULL;

    while (p) {
        if (p->trap->address == trap->address) {
            break;
        }
        prev = p;
        p = p->next;
    }

    if (!p) {
        log_error(traps_log, "Trap `%s' not found.", trap->name);
        return -1;
    }

    if (prev) {
        prev->next = p->next;
    } else {
        traplist = p->next;
    }

    lib_free(p);

    if (traps_enabled) {
        remove_trap(trap);
    }

    return 0;
}

void traps_refresh(void)
{
    if (traps_enabled) {
        traplist_t *p;

        for (p = traplist; p != NULL; p = p->next) {
            remove_trap(p->trap);
            install_trap(p->trap);
        }
    }
    return;
}

uint32_t traps_handler(void)
{
    traplist_t *p = traplist;
    unsigned int pc;
    int result;

    pc = maincpu_get_pc();

    while (p) {
        if (p->trap->address == pc) {
            /* This allows the trap function to remove traps.  */
            uint16_t resume_address = p->trap->resume_address;

            result = (*p->trap->func)();
            if (!result) {
                return (p->trap->check[0] | (p->trap->check[1] << 8) | (p->trap->check[2] << 16));
            }
            /* XXX ALERT!  `p' might not be valid anymore here, because
               `p->trap->func()' might have removed all the traps.  */
            maincpu_set_pc(resume_address);
            return 0;
        }
        p = p->next;
    }

    return (uint32_t)-1;
}

int traps_checkaddr(unsigned int addr)
{
    traplist_t *p = traplist;

    while (p) {
        if (p->trap->address == addr) {
            return 1;
        }
        p = p->next;
    }

    return 0;
}