-
Notifications
You must be signed in to change notification settings - Fork 5
/
port.c
310 lines (270 loc) · 6.11 KB
/
port.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
#ifdef DEBUG_PORT
#define USE_SERIAL_OUTPUT
#endif
#include "port.h"
#include <stdlib.h>
#include <string.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <clib/debug_protos.h>
#include <clib/alib_protos.h>
#include <clib/exec_protos.h>
#include <clib/intuition_protos.h>
#include <devices/timer.h>
#include <intuition/intuition.h>
#include <inline/intuition.h>
#include <exec/io.h>
#include <exec/execbase.h>
#include "device.h"
#include "printf.h"
#include "attach.h"
#ifdef DEBUG_CALLOUT
#define PRINTF_CALLOUT(args...) printf(args)
#else
#define PRINTF_CALLOUT(args...)
#endif
// amiga-gcc falls over when the data segment is entirely
// empty. Put one short there until we know what is going on.
//
// Compiling with -fbaserel will result in a linker error
// otherwise:
//
// relocation truncated to fit: DREL16 against `.bss'
//
short bug=TRUE;
/* Interrupt Level, >0 means interrupts are disabled */
static int bsd_ilevel = 0;
void
panic(const char *fmt, ...)
{
va_list ap;
struct Library *IntuitionBase;
struct EasyStruct es = {
sizeof (es),
0,
"A4091 Panic",
(char *) fmt,
"OK",
};
printf("PANIC: ");
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
printf("\n\n");
IntuitionBase = OpenLibrary("intuition.library", 37);
if (IntuitionBase != NULL) {
va_start(ap, fmt);
(void) EasyRequestArgs(NULL, &es, NULL, ap);
va_end(ap);
CloseLibrary(IntuitionBase);
}
}
static void wait_for_timer(struct timerequest *tr, struct timeval *tv)
{
tr->tr_node.io_Command = TR_ADDREQUEST;
tr->tr_time = *tv;
DoIO((struct IORequest *)tr);
}
static void delete_timer(struct timerequest *tr)
{
struct MsgPort *tp;
if (tr != NULL) {
tp = tr->tr_node.io_Message.mn_ReplyPort;
CloseDevice((struct IORequest *)tr);
DeleteExtIO((struct IORequest *)tr);
if (tp != NULL)
DeletePort(tp);
}
}
static struct timerequest *create_timer(ULONG unit)
{
LONG error;
struct MsgPort *tp;
struct timerequest *tr;
tp = CreatePort(NULL, 0);
if (tp == NULL)
return (NULL);
tr = (struct timerequest *)
CreateExtIO(tp, sizeof(struct timerequest));
if (tr == NULL) {
DeletePort(tp);
return (NULL);
}
error = OpenDevice(TIMERNAME, unit, (struct IORequest *)tr, 0L);
if (error) {
delete_timer(tr);
return (NULL);
}
return tr;
}
void
delay(int usecs)
{
struct timerequest *tr;
struct timeval tv;
if(bsd_ilevel > 0) {
printf("delay(%d): Interrupts disabled, using delay loop.\n", usecs);
for (unsigned long i = 0; i < ((unsigned long)usecs << 3); i++)
asm("nop");
return;
}
if (usecs < 20000)
tr = create_timer(UNIT_MICROHZ);
else
tr = create_timer(UNIT_VBLANK);
if (tr == NULL) {
printf("timer.device handle invalid.\n");
return;
}
tv.tv_secs = usecs / 1000000;
tv.tv_micro = usecs % 1000000;
wait_for_timer(tr, &tv);
delete_timer(tr);
}
/* Block (nesting) interrupts */
int
bsd_splbio(void)
{
Disable();
return (bsd_ilevel++);
}
/* Enable (nesting) interrupts */
void
bsd_splx(int ilevel)
{
bsd_ilevel = ilevel;
if (bsd_ilevel == 0)
Enable();
}
const char *
device_xname(void *ptr)
{
return ("A4091");
}
#ifdef USE_BASEREL
void
__restore_a4(void)
{
__asm volatile("\tlea ___a4_init, a4");
}
#endif
char *itoa(int value, char *string, int radix)
{
int n,r,a,b = 0;
n = (value<0)?-value:value;
b = 0;
while (n)
{
r = n % radix;
if (r >= 10)
string[b++] = 'a' + (r - 10);
else
string[b++] = '0' + r;
n /= radix;
}
if (b == 0)
string[b++] = '0';
if (value < 0 && radix == 10)
string[b++] = '-';
string[b] = '\0';
a=0; b--;
while (a < b) {
char temp = string[a];
string[a] = string[b];
string[b] = temp;
a++;b--;
}
return string;
}
/* callout */
callout_t *callout_head = NULL;
static void
callout_add(callout_t *c)
{
c->co_prev = NULL;
c->co_next = callout_head;
if (callout_head != NULL)
callout_head->co_prev = c;
callout_head = c;
}
static void
callout_remove(callout_t *c)
{
if (c == callout_head) {
callout_head = c->co_next;
if (callout_head != NULL)
callout_head->co_prev = NULL;
return;
}
if (c->co_prev != NULL)
c->co_prev->co_next = c->co_next;
if (c->co_next != NULL)
c->co_next->co_prev = c->co_prev;
}
void
callout_init(callout_t *c, u_int flags)
{
c->func = NULL;
}
#ifdef DEBUG
void
callout_list(void)
{
callout_t *cur;
for (cur = callout_head; cur != NULL; cur = cur->co_next) {
printf("%c %d %p(%p)\n", (cur == callout_head) ? '>' : ' ',
cur->ticks, cur->func, cur->arg);
}
}
#endif
int
callout_pending(callout_t *c)
{
PRINTF_CALLOUT("callout %spending\n", (c->func == NULL) ? "not " : "");
return (c->func != NULL);
}
int
callout_stop(callout_t *c)
{
int pending = (c->func != NULL);
PRINTF_CALLOUT("callout stop %p\n", c->func);
c->func = NULL;
callout_remove(c);
return (pending);
}
void
callout_reset(callout_t *c, int ticks, void (*func)(void *), void *arg)
{
c->ticks = ticks;
c->func = func;
c->arg = arg;
callout_remove(c);
callout_add(c);
PRINTF_CALLOUT("callout_reset %p(%x) at %d\n",
c->func, (uint32_t) c->arg, ticks);
}
void
callout_call(callout_t *c)
{
if (c->func == NULL) {
PRINTF_CALLOUT("callout to NULL function\n");
return;
}
PRINTF_CALLOUT("callout_call %p(%x)\n", c->func, (uint32_t) c->arg);
c->func(c->arg);
}
void
callout_run_timeouts(void)
{
callout_t *cur;
for (cur = callout_head; cur != NULL; cur = cur->co_next) {
if (cur->ticks == 1) {
cur->ticks = 0;
callout_call(cur);
} else if (cur->ticks != 0) {
cur->ticks -= TICKS_PER_SECOND;
if (cur->ticks < 1)
cur->ticks = 1;
}
}
}