-
Notifications
You must be signed in to change notification settings - Fork 17
/
trs_interrupt.c
593 lines (537 loc) · 15 KB
/
trs_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
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
* Copyright (c) 1996-2020, Timothy P. Mann
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*
* Emulate interrupts
*/
#define _XOPEN_SOURCE 500 /* signal.h: SA_RESTART */
#include "z80.h"
#include "trs.h"
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
/*#define IDEBUG 1*/
/*#define IDEBUG2 1*/
/* IRQs */
#define M1_TIMER_BIT 0x80
#define M1_DISK_BIT 0x40
#define M3_UART_ERR_BIT 0x40
#define M3_UART_RCV_BIT 0x20
#define M3_UART_SND_BIT 0x10
#define M3_IOBUS_BIT 0x80 /* not emulated */
#define M3_TIMER_BIT 0x04
#define M3_CASSFALL_BIT 0x02
#define M3_CASSRISE_BIT 0x01
static unsigned char interrupt_latch = 0;
static unsigned char interrupt_mask = 0;
/* NMIs (M3/4/4P only) */
#define M3_INTRQ_BIT 0x80 /* FDC chip INTRQ line */
#define M3_MOTOROFF_BIT 0x40 /* FDC motor timed out (stopped) */
#define M3_RESET_BIT 0x20 /* User pressed Reset button */
static unsigned char nmi_latch = 1; /* ?? One diagnostic program needs this */
static unsigned char nmi_mask = M3_RESET_BIT;
#define TIMER_HZ_1 40
#define TIMER_HZ_3 30
#define TIMER_HZ_4 60
static int timer_hz;
#define CLOCK_MHZ_1 1.77408
#define CLOCK_MHZ_3 2.02752
#define CLOCK_MHZ_4 4.05504
time_t trs_timeoffset;
/* Kludge: LDOS hides the date (not time) in a memory area across reboots. */
/* We put it there on powerup, so LDOS magically knows the date! */
#define LDOS_MONTH 0x4306
#define LDOS_DAY 0x4307
#define LDOS_YEAR 0x4466
#define LDOS3_MONTH 0x442f
#define LDOS3_DAY 0x4457
#define LDOS3_YEAR 0x4413
#define LDOS4_MONTH 0x0035
#define LDOS4_DAY 0x0034
#define LDOS4_YEAR 0x0033
/* Kludge, continued: On NEWDOS/80, both date and time are stored in memory
across reboots, but a test is done on boot to decide whether to use the
stored values. Here's how it works: NEWDOS/80 writes a special byte value
to the memory address right before the stored date and time. On reboot,
this address is checked, and if it contains that special byte, the stored
date and time are considered valid and are therefore used.
By putting this info in memory on powerup, NEWDOS/80 gets initialized
with the system date and time.
*/
#define NEWDOS_DATETIME_VALID_BYTE 0xa5
// Model 1
#define NEWDOS_DATETIME_VALID_ADDR 0x43ab
#define NEWDOS_MONTH 0x43b1
#define NEWDOS_DAY 0x43b0
#define NEWDOS_YEAR 0x43af
#define NEWDOS_HOUR 0x43ae
#define NEWDOS_MIN 0x43ad
#define NEWDOS_SEC 0x43ac
// Model 3
#define NEWDOS3_DATETIME_VALID_ADDR 0x42cb
#define NEWDOS3_MONTH 0x42d1
#define NEWDOS3_DAY 0x42d0
#define NEWDOS3_YEAR 0x42cf
#define NEWDOS3_HOUR 0x42ce
#define NEWDOS3_MIN 0x42cd
#define NEWDOS3_SEC 0x42cc
static int timer_on = 1;
#ifdef IDEBUG
long lost_timer_interrupts = 0;
#endif
/* Note: the independent interrupt latch and mask model is not correct
for all interrupts. The cassette rise/fall interrupt enable is
clocked into the interrupt latch when the event occurs (we get this
right), and is *not* masked against the latch output (we get this
wrong, but it doesn't really matter). */
void
trs_cassette_rise_interrupt(int dummy)
{
interrupt_latch = (interrupt_latch & ~M3_CASSRISE_BIT) |
(interrupt_mask & M3_CASSRISE_BIT);
z80_state.irq = (interrupt_latch & interrupt_mask) != 0;
trs_cassette_update(0);
}
void
trs_cassette_fall_interrupt(int dummy)
{
interrupt_latch = (interrupt_latch & ~M3_CASSFALL_BIT) |
(interrupt_mask & M3_CASSFALL_BIT);
z80_state.irq = (interrupt_latch & interrupt_mask) != 0;
trs_cassette_update(0);
}
void
trs_cassette_clear_interrupts(void)
{
interrupt_latch &= ~(M3_CASSRISE_BIT|M3_CASSFALL_BIT);
z80_state.irq = (interrupt_latch & interrupt_mask) != 0;
}
int
trs_cassette_interrupts_enabled(void)
{
return interrupt_mask & (M3_CASSRISE_BIT|M3_CASSFALL_BIT);
}
void
trs_timer_interrupt(int state)
{
if (trs_model == 1) {
if (state) {
#ifdef IDEBUG
if (interrupt_latch & M1_TIMER_BIT) lost_timer_interrupts++;
#endif
interrupt_latch |= M1_TIMER_BIT;
z80_state.irq = 1;
} else {
interrupt_latch &= ~M1_TIMER_BIT;
}
} else {
if (state) {
#ifdef IDEBUG
if (interrupt_latch & M3_TIMER_BIT) lost_timer_interrupts++;
#endif
interrupt_latch |= M3_TIMER_BIT;
} else {
interrupt_latch &= ~M3_TIMER_BIT;
}
z80_state.irq = (interrupt_latch & interrupt_mask) != 0;
}
}
void
trs_disk_intrq_interrupt(int state)
{
if (trs_model == 1) {
if (state) {
interrupt_latch |= M1_DISK_BIT;
z80_state.irq = 1;
} else {
interrupt_latch &= ~M1_DISK_BIT;
}
} else {
if (state) {
nmi_latch |= M3_INTRQ_BIT;
} else {
nmi_latch &= ~M3_INTRQ_BIT;
}
z80_state.nmi = (nmi_latch & nmi_mask) != 0;
if (!z80_state.nmi) z80_state.nmi_seen = 0;
}
}
void
trs_disk_motoroff_interrupt(int state)
{
/* Drive motor timed out (stopped). */
if (trs_model == 1) {
/* no such interrupt */
} else {
if (state) {
nmi_latch |= M3_MOTOROFF_BIT;
} else {
nmi_latch &= ~M3_MOTOROFF_BIT;
}
z80_state.nmi = (nmi_latch & nmi_mask) != 0;
if (!z80_state.nmi) z80_state.nmi_seen = 0;
}
}
void
trs_disk_drq_interrupt(int state)
{
/* no effect */
}
void
trs_uart_err_interrupt(int state)
{
if (trs_model > 1) {
if (state) {
interrupt_latch |= M3_UART_ERR_BIT;
} else {
interrupt_latch &= ~M3_UART_ERR_BIT;
}
z80_state.irq = (interrupt_latch & interrupt_mask) != 0;
}
}
void
trs_uart_rcv_interrupt(int state)
{
if (trs_model > 1) {
if (state) {
interrupt_latch |= M3_UART_RCV_BIT;
} else {
interrupt_latch &= ~M3_UART_RCV_BIT;
}
z80_state.irq = (interrupt_latch & interrupt_mask) != 0;
}
}
void
trs_uart_snd_interrupt(int state)
{
if (trs_model > 1) {
if (state) {
interrupt_latch |= M3_UART_SND_BIT;
} else {
interrupt_latch &= ~M3_UART_SND_BIT;
}
z80_state.irq = (interrupt_latch & interrupt_mask) != 0;
}
}
void
trs_reset_button_interrupt(int state)
{
if (trs_model == 1) {
z80_state.nmi = state;
} else {
if (state) {
nmi_latch |= M3_RESET_BIT;
} else {
nmi_latch &= ~M3_RESET_BIT;
}
z80_state.nmi = (nmi_latch & nmi_mask) != 0;
}
if (!z80_state.nmi) z80_state.nmi_seen = 0;
}
unsigned char
trs_interrupt_latch_read(void)
{
unsigned char tmp = interrupt_latch;
if (trs_model == 1) {
trs_timer_interrupt(0); /* acknowledge this one (only) */
z80_state.irq = (interrupt_latch != 0);
return tmp;
} else {
return ~tmp;
}
}
void
trs_interrupt_mask_write(unsigned char value)
{
interrupt_mask = value;
z80_state.irq = (interrupt_latch & interrupt_mask) != 0;
}
/* M3 only */
unsigned char
trs_nmi_latch_read(void)
{
return ~nmi_latch;
}
void
trs_nmi_mask_write(unsigned char value)
{
nmi_mask = value | M3_RESET_BIT;
z80_state.nmi = (nmi_latch & nmi_mask) != 0;
#if IDEBUG2
if (z80_state.nmi && !z80_state.nmi_seen) {
debug("mask write caused nmi, mask %02x latch %02x\n",
nmi_mask, nmi_latch);
}
#endif
if (!z80_state.nmi) z80_state.nmi_seen = 0;
}
#if SUSPEND_DELAY
static int saved_delay;
/* Temporarily reduce the delay, until trs_restore_delay is called.
Useful if we know we're about to do something that's emulated more
slowly than most instructions, such as video or real-time sound.
In case the boost is too big or too small, we allow the normal
autodelay algorithm to continue to run and adjust the new delay.
XXX This heuristic seems to have been doing more harm than good, at
least on modern fast systems. Disabled for now. Would be nice to
find a better way to do something like this. Note: despite the
above comment, the code is called only from sound start/stop, not
anything to do with video.
*/
void
trs_suspend_delay(void)
{
if (!saved_delay) {
saved_delay = z80_state.delay;
z80_state.delay /= 2; /* dividing by 2 is arbitrary */
}
}
/* Put back the saved delay */
void
trs_restore_delay(void)
{
if (saved_delay) {
z80_state.delay = saved_delay;
saved_delay = 0;
trs_paused = 1;
}
}
#else
void trs_suspend_delay(void) { }
void trs_restore_delay(void) { }
#endif
#define UP_F 1.50
#define DOWN_F 0.50
void
trs_timer_event(int signo)
{
struct timeval tv;
struct itimerval it;
gettimeofday(&tv, NULL);
if (trs_autodelay) {
static struct timeval oldtv;
static int increment = 1;
static int oldtoofast = 0;
#if __GNUC__
static unsigned long long oldtcount;
#else
static unsigned long oldtcount;
#endif
if (!trs_paused) {
int toofast = (z80_state.t_count - oldtcount) >
((tv.tv_sec*1000000 + tv.tv_usec) -
(oldtv.tv_sec*1000000 + oldtv.tv_usec))*z80_state.clockMHz;
if (toofast == oldtoofast) {
increment = (int)(increment * UP_F + 0.5);
} else {
increment = (int)(increment * DOWN_F + 0.5);
}
oldtoofast = toofast;
if (increment < 1) increment = 1;
if (toofast) {
z80_state.delay += increment;
} else {
z80_state.delay -= increment;
if (z80_state.delay < 0) {
z80_state.delay = 0;
increment = 1;
}
}
}
trs_paused = 0;
oldtv = tv;
oldtcount = z80_state.t_count;
}
if (timer_on) {
trs_timer_interrupt(1); /* generate */
trs_disk_motoroff_interrupt(trs_disk_motoroff());
trs_kb_heartbeat(); /* part of keyboard stretch kludge */
}
x_poll_count = 0; /* be sure to flush and check for X events */
/* Schedule next tick. We do it this way because the host system
probably didn't wake us up at exactly the right time. For
instance, on Linux i386 the real clock ticks at 10ms, but we want
to tick at 25ms. If we ask setitimer to wake us up in 25ms, it
will really wake us up in 30ms. The algorithm below compensates
for such an error by making the next tick shorter. */
it.it_value.tv_sec = 0;
it.it_value.tv_usec =
(1000000/timer_hz) - (tv.tv_usec % (1000000/timer_hz));
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000/timer_hz; /* fail-safe */
setitimer(ITIMER_REAL, &it, NULL);
}
/*
* Initialize time offset. This can useful for TRS-80 operating
* systems that behave better when the year is within a limited range.
*/
void
trs_inityear(int year)
{
time_t real, fake;
struct tm tm;
if (year == 0) {
trs_timeoffset = 0;
} else {
real = time(NULL);
localtime_r(&real, &tm);
tm.tm_year = year - 1900;
fake = mktime(&tm);
trs_timeoffset = fake - real;
}
}
void
trs_timer_init(void)
{
struct sigaction sa;
struct tm *lt;
time_t tt;
if (trs_model == 1) {
timer_hz = TIMER_HZ_1;
z80_state.clockMHz = CLOCK_MHZ_1;
} else {
/* initially... */
timer_hz = TIMER_HZ_3;
z80_state.clockMHz = CLOCK_MHZ_3;
}
sa.sa_handler = trs_timer_event;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGALRM);
sa.sa_flags = SA_RESTART;
sigaction(SIGALRM, &sa, NULL);
trs_timer_event(SIGALRM);
/* Also initialize the clock in memory - hack */
tt = time(NULL) + trs_timeoffset;
lt = localtime(&tt);
if (trs_model == 1) {
mem_write(LDOS_MONTH, (lt->tm_mon + 1) ^ 0x50);
mem_write(LDOS_DAY, lt->tm_mday);
mem_write(LDOS_YEAR, lt->tm_year - 80);
mem_write(NEWDOS_DATETIME_VALID_ADDR, NEWDOS_DATETIME_VALID_BYTE);
mem_write(NEWDOS_MONTH, lt->tm_mon + 1);
mem_write(NEWDOS_DAY, lt->tm_mday);
mem_write(NEWDOS_YEAR, lt->tm_year % 100);
mem_write(NEWDOS_HOUR, lt->tm_hour);
mem_write(NEWDOS_MIN, lt->tm_min);
mem_write(NEWDOS_SEC, lt->tm_sec);
} else {
mem_write(LDOS3_MONTH, (lt->tm_mon + 1) ^ 0x50);
mem_write(LDOS3_DAY, lt->tm_mday);
mem_write(LDOS3_YEAR, lt->tm_year - 80);
mem_write(NEWDOS3_DATETIME_VALID_ADDR, NEWDOS_DATETIME_VALID_BYTE);
mem_write(NEWDOS3_MONTH, lt->tm_mon + 1);
mem_write(NEWDOS3_DAY, lt->tm_mday);
mem_write(NEWDOS3_YEAR, lt->tm_year % 100);
mem_write(NEWDOS3_HOUR, lt->tm_hour);
mem_write(NEWDOS3_MIN, lt->tm_min);
mem_write(NEWDOS3_SEC, lt->tm_sec);
if (trs_model >= 4) {
extern Uchar memory[];
memory[LDOS4_MONTH] = lt->tm_mon + 1;
memory[LDOS4_DAY] = lt->tm_mday;
memory[LDOS4_YEAR] = lt->tm_year;
}
}
}
void
trs_timer_off(void)
{
timer_on = 0;
}
void
trs_timer_on(void)
{
if (!timer_on) {
timer_on = 1;
trs_timer_event(SIGALRM);
}
}
void
trs_timer_speed(int fast)
{
if (trs_model >= 4) {
timer_hz = fast ? TIMER_HZ_4 : TIMER_HZ_3;
z80_state.clockMHz = fast ? CLOCK_MHZ_4 : CLOCK_MHZ_3;
} else if (trs_model == 1) {
/* Typical 2x clock speedup kit */
z80_state.clockMHz = CLOCK_MHZ_1 * ((fast&1) + 1);
}
}
static trs_event_func event_func = NULL;
static int event_arg;
/* Schedule an event to occur after "countdown" more t-states have
* executed. 0 makes the event happen immediately -- that is, at
* the end of the current instruction, but before the emulator checks
* for interrupts. It is legal for an event function to call
* trs_schedule_event.
*
* Only one event can be buffered. If you try to schedule a second
* event while one is still pending, the pending event (along with
* any further events that it schedules) is executed immediately.
*/
void
trs_schedule_event(trs_event_func f, int arg, int countdown)
{
while (event_func) {
#if EDEBUG
error("warning: trying to schedule two events");
#endif
trs_do_event();
}
event_func = f;
event_arg = arg;
z80_state.sched = z80_state.t_count + (tstate_t) countdown;
if (z80_state.sched == 0) z80_state.sched--;
}
/*
* If an event is scheduled, do it now. (If the event function
* schedules a new event, however, leave that one pending.)
*/
void
trs_do_event(void)
{
trs_event_func f = event_func;
if (f) {
event_func = NULL;
z80_state.sched = 0;
f(event_arg);
}
}
/*
* Cancel scheduled event, if any.
*/
void
trs_cancel_event(void)
{
event_func = NULL;
z80_state.sched = 0;
}
/*
* Check event scheduled
*/
trs_event_func
trs_event_scheduled(void)
{
return event_func;
}