-
Notifications
You must be signed in to change notification settings - Fork 6
/
int-8018x.c
256 lines (227 loc) · 5.52 KB
/
int-8018x.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
//------------------------------------------------------------------------------
// EMU86 - 8018X interrupt controller
//------------------------------------------------------------------------------
#include "emu-int.h"
#include "int-8018x.h"
#include <stdio.h>
#define INT_REG_EOI 0
#define INT_REG_MASK 3
#define INT_REG_PRIMSK 4
#define INT_REG_REQST 6
#define INT_REG_TCUCON 8
#define INT_REG_SCUCON 9
#define INT_REG_I4CON 10
#define INT_REG_I0CON 11
#define INT_REG_I1CON 12
#define INT_REG_I2CON 13
#define INT_REG_I3CON 14
// REQST and MASK register bits
#define BIT_TMR 0
// Bit 1 is reserved on all models
// Bits 2 & 3 are for DMA in XL variant
// Bits 2 & 3 are for serial and INT4 for EB variant
#define BIT_SER 2
#define BIT_INT4 3
#define BIT_INT0 4
#define BIT_INT1 5
#define BIT_INT2 6
#define BIT_INT3 7
int _int_line_max = INT_LINE_MAX;
int _int_prio_max = INT_PRIO_MAX;
int _pri_mask = 7;
// Timers & serial are edge triggered
int _int_mode [INT_LINE_MAX] =
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1};
// Default priority for timers is 0
// Default priority for serial is 1
int _int_prio [INT_LINE_MAX] =
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1};
int _int_vect [INT_LINE_MAX] =
{ 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21};
int _int_mask [INT_LINE_MAX] =
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
// Initialized to zero by CRT
int _int_req [INT_LINE_MAX];
int _int_serv [INT_LINE_MAX];
word_t imask;
// PIC I/O read
int int_io_read (word_t p, word_t * w)
{
int err = 0;
int r = p >> 1;
if (r == INT_REG_REQST)
{
word_t out = 0;
for (size_t i = 0; i < 8; i++) {
if (i == BIT_TMR) {
if (_int_req[INT_LINE_TIMER0] || _int_req[INT_LINE_TIMER1] || _int_req[INT_LINE_TIMER2]) {
out |= 1 << i;
}
} else if (i == BIT_SER) {
if (_int_req[INT_LINE_SERIAL_RX] || _int_req[INT_LINE_SERIAL_TX]) {
out |= 1 << i;
}
} else if (_int_req[i]) {
out |= 1 << i;
}
}
*w = out;
}
else if (r == INT_REG_MASK)
{
word_t out = 0;
for (size_t i = 0; i < 8; i++) {
if (i == BIT_TMR) {
if (_int_mask[INT_LINE_TIMER0]) {
out |= 1 << i;
}
} else if (i == BIT_SER) {
if (_int_mask[INT_LINE_SERIAL_RX]) {
out |= 1 << i;
}
} else if (_int_mask[i]) {
out |= 1 << i;
}
}
// printf("int read IMASK: %08x\n", out);
*w = out;
}
else
{
printf ("\nerror: read from int register %02x %i", p+INT_REG_BASE, r);
err = -1;
}
return err;
}
// PIC I/O write
int int_io_write (word_t p, word_t w)
{
int err = 0;
int r = p >> 1;
if (r == INT_REG_EOI)
{
// printf("EOI %d\n", w);
if (w == 0x8000)
{
int_end_prio ();
}
else
{
switch (w & 0x001F)
{
case 8:
int_end_line (INT_LINE_TIMER0);
int_end_line (INT_LINE_TIMER1);
int_end_line (INT_LINE_TIMER2);
break;
case 12:
int_end_line (INT_LINE_INT0);
break;
case 13:
int_end_line (INT_LINE_INT1);
break;
case 14:
int_end_line (INT_LINE_INT2);
break;
case 15:
int_end_line (INT_LINE_INT3);
break;
case 17:
int_end_line (INT_LINE_INT4);
break;
case 20:
int_end_line (INT_LINE_SERIAL_RX);
int_end_line (INT_LINE_SERIAL_TX);
break;
default:
printf("nemam %d\n", w & 0x1f);
}
}
}
else if (r == INT_REG_MASK)
{
for (int i = 0; i < 8; i++) {
int mask = (w & 0x01) ? 1 : 0;
if (i == BIT_TMR) {
_int_mask[INT_LINE_TIMER0] = mask;
_int_mask[INT_LINE_TIMER1] = mask;
_int_mask[INT_LINE_TIMER2] = mask;
}
else if (i == BIT_SER) {
_int_mask[INT_LINE_SERIAL_RX] = mask;
_int_mask[INT_LINE_SERIAL_TX] = mask;
}
else {
_int_mask[i] = mask;
}
w >>= 1;
}
}
else if (r == INT_REG_PRIMSK)
{
// printf("int primask %d\n", w & 7);
_pri_mask = w & 7;
}
else if (r == INT_REG_TCUCON)
{
// TODO: level ?
_int_mask[INT_LINE_TIMER0] = (p >> 3) & 1;
_int_prio[INT_LINE_TIMER0] = p & 7;
_int_mask[INT_LINE_TIMER1] = (p >> 3) & 1;
_int_prio[INT_LINE_TIMER1] = p & 7;
_int_mask[INT_LINE_TIMER2] = (p >> 3) & 1;
_int_prio[INT_LINE_TIMER2] = p & 7;
// printf("int tcu pri %d mask %d\n", p & 7, (p >> 3) & 1);
}
else if (r == INT_REG_SCUCON)
{
// TODO: level ?
_int_mask[INT_LINE_SERIAL_RX] = (p >> 3) & 1;
_int_mask[INT_LINE_SERIAL_TX] = (p >> 3) & 1;
_int_prio[INT_LINE_SERIAL_RX] = p & 7;
_int_prio[INT_LINE_SERIAL_TX] = p & 7;
// printf("int scu pri %d mask %d\n", p & 7, (p >> 3) & 1);
}
else if (r == INT_REG_I4CON)
{
// TODO: level ?
_int_mask[INT_LINE_INT4] = (p >> 3) & 1;
_int_prio[INT_LINE_INT4] = p & 7;
// printf("int int4 pri %d mask %d\n", p & 7, (p >> 3) & 1);
}
else if (r == INT_REG_I0CON)
{
// TODO: level ?
_int_mask[INT_LINE_INT0] = (p >> 3) & 1;
_int_prio[INT_LINE_INT0] = p & 7;
// printf("int int0 pri %d mask %d\n", p & 7, (p >> 3) & 1);
}
else if (r == INT_REG_I1CON)
{
_int_mask[INT_LINE_INT1] = (p >> 3) & 1;
_int_prio[INT_LINE_INT1] = p & 7;
// printf("int int1 pri %d mask %d\n", p & 7, (p >> 3) & 1);
}
else if (r == INT_REG_I2CON)
{
_int_mask[INT_LINE_INT2] = (p >> 3) & 1;
_int_prio[INT_LINE_INT2] = p & 7;
// printf("int int2 pri %d mask %d\n", p & 7, (p >> 3) & 1);
}
else if (r == INT_REG_I3CON)
{
_int_mask[INT_LINE_INT3] = (p >> 3) & 1;
_int_prio[INT_LINE_INT3] = p & 7;
// printf("int int3 pri %d mask %d\n", p & 7, (p >> 3) & 1);
}
else
{
printf ("\nerror: write %hXh to int register %02x %i", w, p+INT_REG_BASE, r);
err = -1;
}
return err;
}
// PIC initialization
void int_init (void)
{
}