-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpic_pit.cpp
295 lines (271 loc) · 5.37 KB
/
pic_pit.cpp
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
#include "stdafx.h"
#include "cpu.h"
#include "pic_pit.h"
pic_t pic;
pic_t pic2;
pit_t pit = {0, 0, 0, 0, 0, 0, 0xFFFFu, 0xFFFFu, 0xFFFFu, 0xFFFFu, 0xFFFFu, 0xFFFFu};
int current_irq = -1;
void irq(int n)
{
irqs |= (1 << n);
}
int get_next_irq_vector()
{
int m;
int test_irq;
if (current_irq >= 0)
{
return -1;
}
if (irqs == 0)
{
return -1;
}
m = irqs &
(1 << 14) | (1 << 4) | (1 << 1) | (1 << 0) &
(~(pic.imr | (pic2.imr << 8))) &
(~(pic.isr | (pic2.isr << 8)));
if (m == 0)
{
return -1;
}
// Find an active interrupt
// We need to check all the interrupts frequently or Linux 0.01 will not start
test_irq = m & 0xFF ? 0 : 8;
for (; test_irq < 15; test_irq++)
{
if (irqs & (1 << test_irq))
{
if (test_irq < 8)
{
if ((pic.icw[2] & 0xF8) == 0)
return -1;
}
else
{
if ((pic2.icw[2] & 0xF8) == 0)
return -1;
}
current_irq = test_irq;
irqs &= ~(1 << current_irq);
if (current_irq < 8)
{
pic.isr |= 1 << (current_irq & 7);
return (pic.icw[2] & 0xF8) + (current_irq & 7);
}
else
{
pic2.isr |= 1 << (current_irq & 7);
return (pic2.icw[2] & 0xF8) + (current_irq & 7);
}
}
}
return -1;
}
void pit_step()
{
if (pit.value[0] < 10)
{
pit.value[0] = pit.preset[0];
irq(0);
}
else
pit.value[0] -= 2;
if (pit.value[1] < 10)
pit.value[1] = pit.preset[1];
else
pit.value[1] -= 10;
if (pit.value[2] < 10)
pit.value[2] = pit.preset[2];
else
pit.value[2] -= 10;
}
unsigned char pic_read(int port)
{
switch (port)
{
case 0x20:
if (pic.readmode == 0)
return pic.irr;
return pic.isr;
case 0x21:
return pic.imr;
case 0xA0:
if (pic2.readmode == 0)
return pic2.irr;
return pic2.isr;
case 0xA1:
return pic2.imr;
}
return 0;
}
void pic_write(int port, unsigned char value)
{
pic_t *p = port >= 0xA0 ? &pic2 : &pic;
unsigned int base = port >= 0xA0 ? 8 : 0;
switch (port)
{
case 0x20:
case 0xA0:
switch (value & 0x18)
{
case 0x00:
// OCW2
switch (value >> 5)
{
case 0:
// Rotate in automatic EOI mode (clear)
break;
case 1:
// Non-specific EOI
if (current_irq >= 0)
{
if ((current_irq & 8) == base)
{
p->isr &= ~(1 << ((current_irq - base) & 7));
current_irq = -1;
}
}
break;
case 2:
// Nop
break;
case 3:
// Specific EOI command
// irqs &= ~(1 << (base + (value & 7)));
if (current_irq == (base + (value & 7)))
{
p->isr &= ~(1 << (value & 7));
current_irq = -1;
}
break;
case 4:
// Rotate in automatic EOI mode (set)
break;
case 5:
// Rotate on non-specific EOI command
break;
case 6:
// Set priority command
break;
case 7:
// Rotate on specific EOI command
break;
}
break;
case 0x08:
// OCW3
if (value & 2)
p->readmode = value & 1;
break;
case 0x10:
// ICW1
p->icwstep = 1;
// p->imr = 0;
p->icw[p->icwstep++] = value;
break;
}
break;
case 0x21:
case 0xA1:
switch (p->icwstep)
{
case 0:
// OCW1
p->imr = value;
break;
case 2:
p->icw[p->icwstep] = value;
switch (p->icw[1] & 3)
{
case 0:
// cascade, no icw4
case 1:
// cascade, icw4
p->icwstep = 3;
break;
case 2:
// single, no icw4
p->icwstep = 0;
break;
case 3:
// single, icw4
p->icwstep = 4;
break;
}
break;
case 3:
p->icw[p->icwstep] = value;
if (p->icw[1] & 1)
p->icwstep = 4;
else
p->icwstep = 0;
break;
case 4:
p->icw[p->icwstep] = value;
p->icwstep = 0;
break;
}
break;
}
}
unsigned char pit_read(int port)
{
switch (port)
{
case 0x40:
case 0x41:
case 0x42:
switch (pit.access[port & 3])
{
case 0:
pit.toggle[port & 3] = !pit.toggle[port & 3];
return (unsigned char)pit.value[port & 3];
case 1:
return (unsigned char)pit.value[port & 3];
case 2:
return (unsigned char)(pit.value[port & 3] >> 8);
case 3:
pit.toggle[port & 3] = !pit.toggle[port & 3];
if (pit.toggle[port & 3])
return (unsigned char)pit.value[port & 3];
return (pit.value[port & 3] >> 8);
}
break;
}
return 0;
}
void pit_write(int port, unsigned char value)
{
unsigned char n;
switch (port)
{
case 0x40:
n = port & 3;
switch (pit.access[n])
{
case 1:
pit.preset[n] = (pit.preset[n] & 0xFF00) | value;
break;
case 2:
pit.preset[n] = (pit.preset[n] & 0x00FF) | (value << 8);
break;
case 3:
pit.toggle[n] = !pit.toggle[n];
if (pit.toggle[n])
pit.preset[n] = (pit.preset[n] & 0xFF00) | value;
else
pit.preset[n] = (pit.preset[n] & 0x00FF) | (value << 8);
break;
}
if (pit.preset[n] == 0)
pit.preset[n] = 0xFFFF;
break;
case 0x43:
n = value >> 6;
pit.access[n] = (value >> 4) & 3;
if (pit.access[n] == 0)
pit.toggle[n] = 0;
break;
}
}