-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterrupt.c
431 lines (364 loc) · 12.3 KB
/
interrupt.c
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
/*
* interrupt.c - Implementation of CPU interrupts and alarms.
*
* Written by
* Ettore Perazzoli <ettore@comm2000.it>
* André 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->global_pending_int = IK_NONE;
cs->nmi_trap_func = NULL;
cs->reset_trap_func = NULL;
}
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_stralloc(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);
}
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;
} 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;
}
}
}
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 (cs->needs_global_restore) {
/* old snapshot restore; global_pending_int needs to be set */
if (value) {
if (!(cs->pending_int[int_num] & IK_IRQ)) {
cs->nirq++;
cs->global_pending_int |= IK_IRQ;
cs->pending_int[int_num] |= IK_IRQ;
}
} else {
if (cs->pending_int[int_num] & IK_IRQ) {
if (cs->nirq > 0) {
cs->pending_int[int_num] &= ~IK_IRQ;
if (--cs->nirq == 0)
cs->global_pending_int &= ~IK_IRQ;
}
}
}
} else {
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 (cs->needs_global_restore) {
/* old snapshot restore; global_pending_int needs to be set */
if (value) {
if (!(cs->pending_int[int_num] & IK_NMI)) {
if (cs->nnmi == 0 && !(cs->global_pending_int & IK_NMI))
cs->global_pending_int |= IK_NMI;
cs->nnmi++;
cs->pending_int[int_num] |= IK_NMI;
}
} else {
if (cs->pending_int[int_num] & IK_NMI) {
if (cs->nnmi > 0) {
cs->nnmi--;
cs->pending_int[int_num] &= ~IK_NMI;
if (maincpu_clk == cs->nmi_clk)
cs->global_pending_int &= ~IK_NMI;
}
}
}
} else {
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)
{
unsigned int num_cycles_left = 0, last_num_cycles_left = 0, 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;
/*
{
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]);
}
*/
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;
/*log_debug("TAKENLEFT %i", num_cycles_left);*/
*int_clk = cs->last_stolen_cycles_clk;
if (num_cycles_left >= cycles_left_to_trigger_irq)
*int_clk -= (cycles_left_to_trigger_irq + 1);
/*log_debug("INTCLK %i", *int_clk);*/
}
/* ------------------------------------------------------------------------- */
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. This is a special condition that can be used for
debugging. `trap_func' will be called with PC as the argument when this
condition is detected. */
void interrupt_maincpu_trigger_trap(void (*trap_func)(WORD, void *data),
void *data)
{
interrupt_cpu_status_t *cs = maincpu_int_status;
cs->global_pending_int |= IK_TRAP;
cs->trap_func = trap_func;
cs->trap_data = data;
}
/* Dispatch the TRAP condition. */
void interrupt_do_trap(interrupt_cpu_status_t *cs, WORD address)
{
cs->global_pending_int &= ~IK_TRAP;
cs->trap_func(address, cs->trap_data);
}
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_DW(m, cs->irq_clk) < 0
|| SMW_DW(m, cs->nmi_clk) < 0
|| SMW_DW(m, (DWORD)cs->num_last_stolen_cycles) < 0
|| SMW_DW(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)
{
/*
unsigned int i;
*/
if (SMW_DW(m, cs->nirq) < 0
|| SMW_DW(m, cs->nnmi) < 0
|| SMW_DW(m, cs->global_pending_int) < 0)
return -1;
/*
if (SMW_DW(m, cs->num_dma_per_opcode) < 0)
return -1;
for (i = 0; i < cs->num_dma_per_opcode; i++) {
if (SMW_DW(m, cs->dma_start_clk[i]) < 0)
return -1;
if (SMW_DW(m, cs->num_cycles_left[i]) < 0)
return -1;
}
*/
return 0;
}
int interrupt_read_snapshot(interrupt_cpu_status_t *cs, snapshot_module_t *m)
{
unsigned int i;
DWORD dw;
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 (SMR_DW(m, &cs->irq_clk) < 0
|| SMR_DW(m, &cs->nmi_clk) < 0)
return -1;
if (SMR_DW(m, &dw) < 0)
return -1;
cs->num_last_stolen_cycles = dw;
if (SMR_DW(m, &dw) < 0)
return -1;
cs->last_stolen_cycles_clk = dw;
/* old-style snapshot need restore of the global interrupt settings */
cs->needs_global_restore = 1;
return 0;
}
int interrupt_read_new_snapshot(interrupt_cpu_status_t *cs, snapshot_module_t *m)
{
DWORD dw;
if (SMR_DW(m, &dw) < 0)
return 0;
cs->nirq = dw;
if (SMR_DW(m, &dw) < 0)
return 0;
cs->nnmi = dw;
if (SMR_DW(m, &dw) < 0)
return 0;
cs->global_pending_int = dw;
/* new-style snapshot need no restore of the global interrupt settings */
cs->needs_global_restore = 0;
return 0;
}