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 467 468 469 470 471 472
|
/*
* interrupt.c - Implementation of CPU interrupts and alarms.
*
* Written by
* Ettore Perazzoli <ettore@comm2000.it>
* Andre Fachat <fachat@physik.tu-chemnitz.de>
* 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.
*
*/
#include "vice.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "6510core.h"
#include "interrupt.h"
#include "lib.h"
#include "log.h"
#include "maincpu.h"
#include "snapshot.h"
#include "types.h"
/* Initialization. */
void interrupt_cpu_status_init(interrupt_cpu_status_t *cs,
unsigned int *last_opcode_info_ptr)
{
cs->num_ints = 0;
cs->pending_int = NULL;
cs->int_name = NULL;
cs->last_opcode_info_ptr = last_opcode_info_ptr;
}
void interrupt_cpu_status_reset(interrupt_cpu_status_t *cs)
{
unsigned int num_ints, *pending_int, *last_opcode_info_ptr;
char **int_name;
num_ints = cs->num_ints;
pending_int = cs->pending_int;
int_name = cs->int_name;
last_opcode_info_ptr = cs->last_opcode_info_ptr;
if (num_ints > 0) {
memset(pending_int, 0, num_ints * sizeof(*(cs->pending_int)));
}
memset(cs, 0, sizeof(interrupt_cpu_status_t));
cs->num_ints = num_ints;
cs->pending_int = pending_int;
cs->int_name = int_name;
cs->last_opcode_info_ptr = last_opcode_info_ptr;
cs->num_last_stolen_cycles = 0;
cs->last_stolen_cycles_clk = (CLOCK)0;
cs->num_dma_per_opcode = 0;
cs->irq_delay_cycles = 0;
cs->nmi_delay_cycles = 0;
cs->global_pending_int = IK_NONE;
cs->nmi_trap_func = NULL;
cs->reset_trap_func = NULL;
cs->irq_pending_clk = CLOCK_MAX;
}
unsigned int interrupt_cpu_status_int_new(interrupt_cpu_status_t *cs,
const char *name)
{
cs->num_ints += 1;
cs->pending_int = lib_realloc(cs->pending_int, cs->num_ints * sizeof(*(cs->pending_int)));
cs->pending_int[cs->num_ints - 1] = 0;
cs->int_name = lib_realloc(cs->int_name, cs->num_ints * sizeof(char *));
cs->int_name[cs->num_ints - 1] = lib_strdup(name);
return cs->num_ints - 1;
}
interrupt_cpu_status_t *interrupt_cpu_status_new(void)
{
return (interrupt_cpu_status_t *)lib_calloc(1, sizeof(interrupt_cpu_status_t));
}
void interrupt_cpu_status_destroy(interrupt_cpu_status_t *cs)
{
if (cs != NULL) {
unsigned int num;
for (num = 0; num < cs->num_ints; num++) {
lib_free(cs->int_name[num]);
}
lib_free(cs->int_name);
lib_free(cs->pending_int);
lib_free(cs->trap_func);
lib_free(cs->trap_data);
}
lib_free(cs);
}
void interrupt_set_nmi_trap_func(interrupt_cpu_status_t *cs,
void (*nmi_trap_func)(void))
{
cs->nmi_trap_func = nmi_trap_func;
}
void interrupt_set_reset_trap_func(interrupt_cpu_status_t *cs, void (*reset_trap_func)(void))
{
cs->reset_trap_func = reset_trap_func;
}
/* Move all the CLOCK time references forward/backward. */
void interrupt_cpu_status_time_warp(interrupt_cpu_status_t *cs,
CLOCK warp_amount, int warp_direction)
{
if (warp_direction == 0) {
return;
}
if (warp_direction > 0) {
cs->irq_clk += warp_amount;
cs->nmi_clk += warp_amount;
cs->last_stolen_cycles_clk += warp_amount;
if (cs->irq_pending_clk < CLOCK_MAX) {
cs->irq_pending_clk += warp_amount;
}
} else {
if (cs->irq_clk > warp_amount) {
cs->irq_clk -= warp_amount;
} else {
cs->irq_clk = (CLOCK) 0;
}
if (cs->nmi_clk > warp_amount) {
cs->nmi_clk -= warp_amount;
} else {
cs->nmi_clk = (CLOCK) 0;
}
if (cs->last_stolen_cycles_clk > warp_amount) {
cs->last_stolen_cycles_clk -= warp_amount;
} else {
cs->last_stolen_cycles_clk = (CLOCK) 0;
}
if (cs->irq_pending_clk < CLOCK_MAX) {
if (cs->irq_pending_clk > warp_amount) {
cs->irq_pending_clk -= warp_amount;
} else {
cs->irq_pending_clk = (CLOCK) 0;
}
}
}
}
void interrupt_log_wrong_nirq(void)
{
log_error(LOG_DEFAULT, "interrupt_set_irq(): wrong nirq!");
}
void interrupt_log_wrong_nnmi(void)
{
log_error(LOG_DEFAULT, "interrupt_set_nmi(): wrong nnmi!");
}
/* ------------------------------------------------------------------------- */
/* These functions are used by snaphots only: they update the IRQ/NMI line
value, but do not update the variables used to emulate the timing. This
is necessary to allow the chip modules to dump their own IRQ/NMI status
information; the global timing status is stored in the CPU module (see
`interrupt_write_snapshot()' and `interrupt_read_snapshot()'). */
void interrupt_restore_irq(interrupt_cpu_status_t *cs, int int_num, int value)
{
if (value) {
cs->pending_int[int_num] |= IK_IRQ;
} else {
cs->pending_int[int_num] &= ~IK_IRQ;
}
}
void interrupt_restore_nmi(interrupt_cpu_status_t *cs, int int_num, int value)
{
if (value) {
cs->pending_int[int_num] |= IK_NMI;
} else {
cs->pending_int[int_num] &= ~IK_NMI;
}
}
int interrupt_get_irq(interrupt_cpu_status_t *cs, int int_num)
{
return cs->pending_int[int_num] & IK_IRQ;
}
int interrupt_get_nmi(interrupt_cpu_status_t *cs, int int_num)
{
return cs->pending_int[int_num] & IK_NMI;
}
void interrupt_fixup_int_clk(interrupt_cpu_status_t *cs, CLOCK cpu_clk,
CLOCK *int_clk)
{
CLOCK num_cycles_left = 0, last_num_cycles_left = 0;
unsigned int num_dma;
unsigned int cycles_left_to_trigger_irq = (OPINFO_DELAYS_INTERRUPT(*cs->last_opcode_info_ptr) ? 2 : 1);
CLOCK last_start_clk = CLOCK_MAX;
#ifdef DEBUGIRQDMA
if (debug.maincpu_traceflg) {
unsigned int i;
log_debug("INTREQ %ld NUMWR %i", (long)cpu_clk,
maincpu_num_write_cycles());
for (i = 0; i < cs->num_dma_per_opcode; i++) {
log_debug("%iCYLEFT %i STCLK %i", i, cs->num_cycles_left[i],
cs->dma_start_clk[i]);
}
}
#endif
num_dma = cs->num_dma_per_opcode;
while (num_dma != 0) {
num_dma--;
num_cycles_left = cs->num_cycles_left[num_dma];
if ((cs->dma_start_clk[num_dma] - 1) <= cpu_clk) {
break;
}
last_num_cycles_left = num_cycles_left;
last_start_clk = cs->dma_start_clk[num_dma];
}
/* if the INTREQ happens between two CPU cycles, we have to interpolate */
if (num_cycles_left - last_num_cycles_left > last_start_clk - cpu_clk - 1) {
num_cycles_left = last_num_cycles_left + last_start_clk - cpu_clk - 1;
}
#ifdef DEBUGIRQDMA
if (debug.maincpu_traceflg) {
log_debug("TAKENLEFT %i LASTSTOLENCYCLECLK %i", num_cycles_left, cs->last_stolen_cycles_clk);
}
#endif
*int_clk = cs->last_stolen_cycles_clk;
if (cs->num_dma_per_opcode > 0 && cs->dma_start_clk[0] > cpu_clk) {
/* interrupt was triggered before end of last opcode */
*int_clk -= (cs->dma_start_clk[0] - cpu_clk);
}
#ifdef DEBUGIRQDMA
if (debug.maincpu_traceflg) {
log_debug("INTCLK dma shifted %i (cs->dma_start_clk[0]=%i", *int_clk, cs->dma_start_clk[0]);
}
#endif
if (num_cycles_left >= cycles_left_to_trigger_irq) {
*int_clk -= (cycles_left_to_trigger_irq + 1);
}
#ifdef DEBUGIRQDMA
if (debug.maincpu_traceflg) {
log_debug("INTCLK fixed %i", *int_clk);
}
#endif
}
/* ------------------------------------------------------------------------- */
void interrupt_trigger_dma(interrupt_cpu_status_t *cs, CLOCK cpu_clk)
{
cs->global_pending_int = (enum cpu_int)(cs->global_pending_int | IK_DMA);
}
void interrupt_ack_dma(interrupt_cpu_status_t *cs)
{
cs->global_pending_int = (enum cpu_int)(cs->global_pending_int & ~IK_DMA);
}
/* ------------------------------------------------------------------------- */
/* Trigger a RESET. This resets the machine. */
void interrupt_trigger_reset(interrupt_cpu_status_t *cs, CLOCK cpu_clk)
{
if (cs == NULL) {
return;
}
cs->global_pending_int |= IK_RESET;
}
/* Acknowledge a RESET condition, by removing it. */
void interrupt_ack_reset(interrupt_cpu_status_t *cs)
{
cs->global_pending_int &= ~IK_RESET;
if (cs->reset_trap_func) {
cs->reset_trap_func();
}
}
/* Trigger a TRAP next cycle */
void interrupt_maincpu_trigger_trap(void (*trap_func)(uint16_t, void *data),
void *data)
{
interrupt_cpu_status_t *cs = maincpu_int_status;
int this_trap_index;
int trap_size_needed;
this_trap_index = cs->traps_next + cs->traps_count;
cs->traps_count++;
trap_size_needed = cs->traps_next + cs->traps_count;
/*
* traps can trigger traps and more than one can be triggered per cycle ...
* so this is not as simple as it might otherwise be.
*/
if (trap_size_needed > cs->traps_size) {
log_message(LOG_DEFAULT, "Increasing trap_func array size to %d with %d to run", trap_size_needed, cs->traps_count);
cs->trap_func = lib_realloc(cs->trap_func, trap_size_needed * sizeof(*cs->trap_func));
cs->trap_data = lib_realloc(cs->trap_data, trap_size_needed * sizeof(*cs->trap_data));
cs->traps_size = trap_size_needed;
}
cs->global_pending_int |= IK_TRAP;
cs->trap_func[this_trap_index] = trap_func;
cs->trap_data[this_trap_index] = data;
}
/* Dispatch the TRAP condition. */
void interrupt_do_trap(interrupt_cpu_status_t *cs, uint16_t address)
{
int i;
int traps_this_cycle = cs->traps_count;
for (i = 0; i < traps_this_cycle; i++) {
cs->trap_func[cs->traps_next](address, cs->trap_data[cs->traps_next]);
/* Don't increment this before the trap func above has exectued! */
cs->traps_next++;
}
if (cs->traps_count > traps_this_cycle) {
/* Executuing a trap scheduled another trap! Run them next cycle */
cs->traps_count -= traps_this_cycle;
} else {
/* All traps have been executed */
cs->global_pending_int &= ~IK_TRAP;
cs->traps_next = 0;
cs->traps_count = 0;
}
}
void interrupt_monitor_trap_on(interrupt_cpu_status_t *cs)
{
cs->global_pending_int |= IK_MONITOR;
}
void interrupt_monitor_trap_off(interrupt_cpu_status_t *cs)
{
cs->global_pending_int &= ~IK_MONITOR;
}
/* ------------------------------------------------------------------------- */
int interrupt_write_snapshot(interrupt_cpu_status_t *cs, snapshot_module_t *m)
{
/* FIXME: could we avoid some of this info? */
if (SMW_CLOCK(m, cs->irq_clk) < 0
|| SMW_CLOCK(m, cs->nmi_clk) < 0
|| SMW_CLOCK(m, cs->irq_pending_clk) < 0
|| SMW_CLOCK(m, cs->num_last_stolen_cycles) < 0
|| SMW_CLOCK(m, cs->last_stolen_cycles_clk) < 0) {
return -1;
}
return 0;
}
int interrupt_write_new_snapshot(interrupt_cpu_status_t *cs, snapshot_module_t *m)
{
if (0
|| SMW_DW(m, cs->nirq) < 0
|| SMW_DW(m, cs->nnmi) < 0
|| SMW_DW(m, cs->global_pending_int) < 0) {
return -1;
}
return 0;
}
int interrupt_write_sc_snapshot(interrupt_cpu_status_t *cs, snapshot_module_t *m)
{
if (0
|| SMW_CLOCK(m, cs->irq_delay_cycles) < 0
|| SMW_CLOCK(m, cs->nmi_delay_cycles) < 0) {
return -1;
}
return 0;
}
int interrupt_read_snapshot(interrupt_cpu_status_t *cs, snapshot_module_t *m)
{
unsigned int i;
CLOCK qw;
for (i = 0; i < cs->num_ints; i++) {
cs->pending_int[i] = IK_NONE;
}
cs->global_pending_int = IK_NONE;
cs->nirq = cs->nnmi = cs->reset = cs->trap = 0;
if (0
|| SMR_CLOCK(m, &cs->irq_clk) < 0
|| SMR_CLOCK(m, &cs->nmi_clk) < 0
|| SMR_CLOCK(m, &cs->irq_pending_clk) < 0) {
return -1;
}
if (SMR_CLOCK(m, &qw) < 0) {
return -1;
}
cs->num_last_stolen_cycles = qw;
if (SMR_CLOCK(m, &qw) < 0) {
return -1;
}
cs->last_stolen_cycles_clk = qw;
return 0;
}
int interrupt_read_new_snapshot(interrupt_cpu_status_t *cs, snapshot_module_t *m)
{
if (0
|| SMR_DW_INT(m, &cs->nirq) < 0
|| SMR_DW_INT(m, &cs->nnmi) < 0
|| SMR_DW_UINT(m, &cs->global_pending_int) < 0) {
return -1;
}
return 0;
}
int interrupt_read_sc_snapshot(interrupt_cpu_status_t *cs, snapshot_module_t *m)
{
if (0
|| SMR_CLOCK(m, &cs->irq_delay_cycles) < 0
|| SMR_CLOCK(m, &cs->nmi_delay_cycles) < 0) {
return -1;
}
return 0;
}
|