forked from emestee/bk-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.c
211 lines (172 loc) · 3.84 KB
/
service.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
/*
* Used to be
* Copyright 1994, Eric A. Edwards
* After substantial changes
* Copyright 2003, Leonid A. Broukhis
*/
/*
* service.c - Event handler system and interrupt service.
*/
#include "defines.h"
#include <libintl.h>
#define _(String) gettext (String)
/* 1 if interrupt requested while WAIT was in effect */
unsigned char in_wait_instr = 0;
/* Allows up to 32 different HW interrupts,
* there can be no more than 1 pending interrupt of each kind.
*/
unsigned long pending_interrupts = 0;
double earliest = 0.0;
event events[NUM_PRI];
/*
* ev_init() - Initialize the event system.
*/
ev_init()
{
int x;
pending_interrupts = 0;
}
/*
* ev_register() - Register an event.
*/
int
ev_register( priority, handler, delay, info )
unsigned priority;
int (*handler)();
unsigned long delay; /* in clock ticks */
d_word info;
{
if (pending_interrupts & (1 << priority)) {
/* There is one pending already */
return;
}
/*
* Fill in the info.
*/
events[priority].handler = handler;
events[priority].info = info;
events[priority].when = ticks + delay;
if (!pending_interrupts || ticks + delay < earliest) {
earliest = ticks + delay;
}
pending_interrupts |= 1 << priority;
/* fprintf(stderr, "Registering pri %d @ %g\n", priority, earliest); */
}
/*
* ev_fire() - Fire off any pending event by the mask
* priority list.
*/
ev_fire( int priority )
{
int x;
unsigned long mask;
switch (priority) {
case 0: mask = ~0; break;
case 1: mask = ~0; break;
case 2: mask = ~0; break;
case 3: mask = ~0; break;
/* BK-0010 uses MTPS #200 to block keyboard interrupts */
case 4: mask = 1; break;
case 5: mask = 0; break;
case 6: mask = 0; break;
case 7: mask = 0; break;
}
if (!(mask & pending_interrupts) || ticks < earliest) {
return;
}
earliest = 10.0e38;
for( x = 0; x < NUM_PRI && (1<<x) <= pending_interrupts; ++x ) {
if (mask & pending_interrupts & (1<<x) &&
ticks >= events[x].when) {
events[x].handler(events[x].info);
/* fprintf(stderr, "Firing pri %d\n", x); */
pending_interrupts &= ~(1 << x);
mask = 0;
} else if (pending_interrupts & (1<<x) &&
events[x].when < earliest) {
earliest = events[x].when;
}
}
/* fprintf(stderr, "Earliest is %g\n", earliest); */
}
/*
* service() - Handle a Trap.
*/
int
service( vector )
d_word vector;
{
register pdp_regs *p = &pdp;
int result;
d_word oldpsw;
d_word oldpc;
d_word oldmode;
d_word newmode;
c_addr vaddr;
d_word newpsw;
d_word newpc;
last_branch = p->regs[PC];
oldmode = ( p->psw & 0140000 ) >> 14;
oldpsw = p->psw;
oldpc = p->regs[PC];
/* If we're servicing an interrupt while a WAIT instruction
* was executing, we need to return after the WAIT.
*/
if (in_wait_instr) {
oldpc += 2;
in_wait_instr = 0;
}
if (( result = lc_word( vector, &newpc)) != OK)
return result;
if (( result = lc_word( vector + 2, &newpsw)) != OK)
return result;
// fprintf(stderr, "Int %o to %06o\n", vector, p->regs[PC]);
addtocybuf(-vector);
if (( result = push( p, oldpsw )) != OK )
return result;
if (( result = push( p, oldpc )) != OK )
return result;
p->psw = newpsw;
p->regs[PC] = newpc;
return OK;
}
/*
* rti() - Return from Interrupt Instruction.
*/
rti( p )
register pdp_regs *p;
{
d_word newpsw;
d_word newpc;
d_word oldmode;
d_word newmode;
int result;
last_branch = p->regs[PC];
if (( result = pop( p, &newpc )) != OK )
return result;
if (( result = pop( p, &newpsw )) != OK )
return result;
p->psw = newpsw;
p->regs[PC] = newpc;
return OK;
}
/*
* rtt() - Return from Interrupt Instruction.
*/
rtt( p )
register pdp_regs *p;
{
d_word newpsw;
d_word newpc;
d_word oldmode;
d_word newmode;
int result;
last_branch = p->regs[PC];
if (( result = pop( p, &newpc )) != OK )
return result;
if (( result = pop( p, &newpsw )) != OK )
return result;
p->psw = newpsw;
p->regs[PC] = newpc;
return CPU_RTT;
}