-
Notifications
You must be signed in to change notification settings - Fork 0
/
machdep-old.c
executable file
·535 lines (468 loc) · 10.7 KB
/
machdep-old.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
#include "misc_func.h"
#include "comm-lpt.h"
#define DEBUG_PIEC 0
#ifdef __FreeBSD__
/* chip-bash includes */
#include <machine/sysarch.h>
#include <machine/cpufunc.h>
#endif
#ifdef LINUX
#include <sys/io.h>
#endif
#ifdef AMIGA
#include "amiga/my_exec_types.h"
#include <exec/semaphores.h>
#include <clib/exec_protos.h>
#include <clib/misc_protos.h>
#include <hardware/cia.h>
#include <resources/misc.h>
extern volatile struct CIA ciaa;
extern volatile struct CIA ciab;
#endif
#ifdef WINDOWS
#include <windows.h>
typedef void (CALLBACK * outb_t)(int,int);
typedef unsigned char (CALLBACK * inb_t)(int);
HINSTANCE dll_handle;
WSADATA wsa_data;
outb_t outb;
inb_t inb;
#endif
/* Faxe port access XXX */
#ifdef SOLARIS
int outb(int p,int v) { return 0; }
int inb(int p) { return 255; }
#endif /* SOLARIS */
#ifdef BEOS
#define DRV_READ_IO_8 'r'
#define DRV_WRITE_IO_8 'w'
#define DRV_READ_IO_16 'r16'
#define DRV_WRITE_IO_16 'w16'
int parport_fd;
typedef struct IO_Tuple {
unsigned long Port;
unsigned char Data;
unsigned short Data16;
} IO_Tuple;
int inb (int port) {
IO_Tuple temp;
temp.Port = port;
ioctl(parport_fd, DRV_READ_IO_8, &temp, 0);
return (temp.Data);
}
void outb(int port, int value) {
IO_Tuple temp;
temp.Port = port;
temp.Data = value;
ioctl(parport_fd, DRV_WRITE_IO_8, &temp, 0);
}
#endif /* BEOS */
/* Low-level functions definitions for each architecture */
#ifdef AMIGA
#define PAROUT ciaa.ciaddrb = 0xFF
#define PARIN ciaa.ciaddrb = 0x00
#define POUTHIGH ciab.ciapra = ((ciab.ciapra & 0xFD) | 0x02)
#define POUTLOW ciab.ciapra = ciab.ciapra & 0xFD
#define POUTRHIGH POUTHIGH
#define POUTWHIGH POUTHIGH
#define POUTRLOW POUTLOW
#define POUTWLOW POUTLOW
#define BUSY (!(ciab.ciapra & 0x01)) /* Inverted 'cause BUSY is active low on PC(!) */
#define PARW(x) ciaa.ciaprb = (x)
#define PARR ciaa.ciaprb
#define SETINITDDR ciab.ciaddra = (ciab.ciaddra & 0xF8) | 0x02
#else
#define POUTHIGH
#define POUTLOW
#define PARR inb(portout)
#define BUSY (inb(portin) & 0x80)
#define ACK (inb(portin) & 0x40)
#ifdef LINUX
#define PAROUT
#define PARIN
#define POUTRHIGH outb(0xa0, portout+2)
#define POUTWHIGH outb(0x40, portout+2)
#define POUTRLOW outb(0xa1, portout+2)
#define POUTWLOW outb(0x41, portout+2)
#define PARW(x) outb(x, portout)
#else
#define PAROUT
#define PARIN
#define POUTRHIGH outb(portout+2, 0xa0)
#define POUTWHIGH outb(portout+2, 0x40)
#define POUTRLOW outb(portout+2, 0xa1)
#define POUTWLOW outb(portout+2, 0x41)
#define AUTOLFLOW outb(portout+2, inb(portout+2)&(255-2))
#define AUTOLFHIGH outb(portout+2, inb(portout+2)|2)
#define PARW(x) outb(portout, x)
#endif /* LINUX */
#endif /* AMIGA */
int piec_lastresponse=0;
int piec_delaycount=0;
void
init_hw (void)
{
#ifdef __FreeBSD__
/* This is old privilege scheme, maybe it works - test it */
/*
FILE *f;
if ((f = fopen ("/dev/io", "rw")) == NULL) {
fatal_error ("Cannot get chip-bash privilege.\
\nPlease setgid to kmem (or similar)\n");
exit (1);
}
*/
/* This is new one */
int a;
debug_msg("Asking for chip-bash privileges... ");
a = i386_set_ioperm(portout,3,1);
debug_msg("i386_set_ioperm returned = %i\n", a);
if (a != 0) {
fatal_error ("Cannot get io space privilege. Please setsgid to kmem (or similar)\n");
exit (1);
}
/* Enable EPP non-FIFO mode */
/* outb(portout+0x402,0x80); */
/* Clear EPP timeout bit */
outb(portout+1,0xfe); /* Status port */
fflush(stdout);
#endif /* FreeBSD */
#ifdef BEOS
debug_msg("Opening ioport device to access parallel ports.\n");
parport_fd = open("/dev/misc/ioport", O_RDWR | O_NONBLOCK);
if (parport_fd < 0) {
fatal_error ("Cannot open /dev/misc/ioport, install ioport driver first.\n");
exit(1);
}
fflush(stdout);
#endif /* BEOS */
#ifdef LINUX
int a;
/* Enable port access to the range [portout] to [portout+3] */
debug_msg("Asking for chip-bash privileges... ");
a = ioperm(portout,3,3);
debug_msg("ioperm returned = %i\n", a);
if (a != 0) {
fatal_error ("Cannot get io space privilege. Please setsgid to root (or similar)\n");
exit (1);
}
fflush(stdout);
#endif /* LINUX */
#ifdef WINDOWS
if (no_net != 1) {
printf("Initializing wsock32.dll\n");
WSAStartup(0x0101, &wsa_data);
}
dll_handle = LoadLibrary("inpout32");
if (dll_handle!=NULL) {
debug_msg("Opened inpout32.dll\n");
outb = (outb_t)GetProcAddress(dll_handle,"Out32");
inb = (inb_t)GetProcAddress(dll_handle,"Inp32");
if (!outb || !inb)
fatal_error("Failed to get functions adresses from Inpout32.dll");
} else {
fatal_error("Could not open inpout32.dll");
}
#endif /* WINDOWS */
#ifdef AMIGA
const char *user;
char myname[] = "64NET/2 - Parallel";
if (steal_parallel_port == 0)
{
debug_msg ("Opening misc resource\n");
if ((MiscBase = OpenResource ("misc.resource")) == NULL)
fatal_error ("Can't open misc.resource\n");
debug_msg ("Allocating parallel port\n");
user = AllocMiscResource (MR_PARALLELPORT, myname);
if (user != NULL && user != myname)
fatal_error ("Printer data lines already in use by %s.\nUnable to lock parallel port\n", user);
}
debug_msg ("Setting DDRs\n");
PAROUT; /* Set data lines for output initially */
/* Set following control line DDR stuff
Bit 2 - Sel - Input
Bit 1 - Pout - Out
Bit 0 - Busy - Input
*/
SETINITDDR;
#endif /* AMIGA */
}
void
starthw(void)
{
/* Parallel=in, Pout=high */
/* ddr = in, strobe = high */
PARIN;
POUTRHIGH;
}
int
syncchar (void)
{
int value;
long c,d;
PARIN;
sync_loop:
POUTRHIGH;
c = 0; d = 0;
/* Set parallel port DDR = in, POUT=high */
#ifdef DEBUG2
debug_msg ("syncchar - Waiting for 64 ACK (!BUSY)\n"); fflush (stdout);
#endif
/* Wait for 64 ACK */
while (!(BUSY))
{
c++;
if (c > max_tries) {
c=0;
// debug_msg ("z"); fflush (stdout);
usleep (snooz_time); /* dont hog cpu */
}
}
/* read value */
value = PARR;
/* Add 256 if the character was received under attention */
if (!ACK) value|=0x100;
/* Set POUT low, Parallel port DDR = in */
POUTRLOW;
#ifdef DEBUG2
printf ("syncchar - Waiting for 64 ACK (BUSY)\n");
fflush (stdout);
#endif
/* Wait for 64 ACK */
while (BUSY)
{
c++;
if (c > max_tries) {
debug_msg ("x"); fflush (stdout);
usleep (snooz_time_2); /* dont hog cpu */
c=0; d++;
if (d > synctolerance_2) goto sync_loop;
}
}
/* second toggle */
POUTRHIGH;
POUTRLOW;
return (value);
}
/* receive a character after a connection has been established */
int
charget (void)
{
int value;
#ifdef DEBUG2
debug_msg ("Trying to get char..."); fflush (stdout);
#endif
/* Parallel port DDR = in , POUT = high */
PARIN;
POUTRHIGH;
#ifdef DEBUG2
debug_msg ("getchar - Waiting for 64 ACK (!BUSY)\n"); fflush (stdout);
#endif
/* Wait for 64 ACK */
while (!(BUSY));
/* read value */
value = PARR;
/* Add 256 if the character was received under attention */
if (!ACK) value|=0x100;
/* Toggle POUT low, Parallel port DDR = in */
POUTRLOW;
#ifdef DEBUG2
debug_msg ("getchar - Waiting for 64 ACK (BUSY)\n"); fflush (stdout);
#endif
/* Wait for 64 ACK */
while (BUSY);
/* Second toggle */
POUTRHIGH;
POUTRLOW;
#ifdef DEBUG2
debug_msg ("Got %02x\n", value); fflush (stdout);
#endif
return (value);
}
/* send a single character back over the link */
int
sendchar (int byte)
{
#ifdef DEBUG2
debug_msg ("Trying to send %02x...", byte); fflush (stdout);
debug_msg ("sendchar - Waiting for 64 ACK (!BUSY)\n"); fflush (stdout);
#endif
/* wait for 64 ready (93) */
while (!(BUSY));
/* POUT = high, Parallel port DDR = Out */
POUTWHIGH;
PAROUT;
/* Write byte */
PARW (byte);
/* wait for 64 ready (97) */
/* Bring POUT low */
POUTWLOW;
#ifdef DEBUG2
debug_msg ("sendchar - Waiting for 64 ACK (BUSY)\n"); fflush (stdout);
#endif
while (BUSY);
PARIN;
POUTRHIGH;
POUTRLOW;
POUTRHIGH;
#ifdef DEBUG2
debug_msg ("Sent\n");
#endif
return (0);
}
//#define DEBUG2
int
fishsendblock (int size, uchar * block)
{
int n = 0;
PAROUT;
POUTWHIGH; /* set port DDR out, don't toggle strobe, this is for non-AMIGA */
while (n < size)
{
/* wait for 64 ACK */
while (!(BUSY));
/* send char & toggle strobe */
PARW (block[n]);
POUTWLOW;
POUTWHIGH; /* raise here before busy loop to reduce port accesses */
n++;
#ifdef DEBUG2
printf("$%02x ",n);
#endif
/* wait for 64 NACK */
while (BUSY);
PARW (block[n]);
POUTWLOW;
POUTWHIGH;
n++;
#ifdef DEBUG2
printf("$%02x ",n);
#endif
}
/* close off */
while (!(BUSY));
/* send ack */
/* Parallel port DDR = in, POUT = low */
PARIN;
POUTRLOW;
POUTRHIGH;
/* FIXME - how to handle it??? it must be here and it must have \n at the end (flushing) */
printf("delay\n");
return (0);
}
int
fishgetblock (int size, uchar * block)
{
int n = 0;
PARIN;
POUTRHIGH; /* set port DDR in, don't toggle strobe, this is for non-AMIGA */
usleep(1);
while (n < size)
{
/* wait for 64 NACK */
while (!(BUSY));
/* get char & toggle POUT */
block[n] = PARR;
n++;
#ifdef DEBUG2
printf("$%02x ",n);
#endif
POUTRLOW;
POUTRHIGH; /* raise here before busy loop to reduce port accesses */
/* wait for ACK */
while (BUSY);
/* get char & toggle POUT */
block[n] = PARR;
n++;
#ifdef DEBUG2
printf("$%02x ",n);
#endif
POUTRLOW;
POUTRHIGH;
}
/* close off */
while (!(BUSY));
/* send ack */
/* Parallel port DDR = in, POUT = low */
PARIN;
POUTRLOW;
POUTRHIGH;
usleep(5);
return (0);
}
void piec_acknowledge()
{
/* Acknowledge a char sent in parallel IEC mode */
POUTRHIGH;
POUTRLOW;
POUTRHIGH;
printf("(ack)\n");
#if DEBUG_PIEC>2
{
char temp[1024];
printf("Hit Enter..."); fflush(stdout);
fgets(temp,1024,stdin);
}
#endif
piec_lastresponse=1;
piec_delaycount=0;
return;
}
void piec_wacknowledge()
{
/* Acknowledge a char sent in parallel IEC mode */
POUTWHIGH;
POUTWLOW;
POUTWHIGH;
printf("(wack)\n");
#if DEBUG_PIEC>2
{
char temp[1024];
printf("Hit Enter..."); fflush(stdout);
fgets(temp,1024,stdin);
}
#endif
piec_lastresponse=3;
piec_delaycount=0;
return;
}
void piec_repeatlastresponse()
{
if (piec_lastresponse==1)
piec_acknowledge();
else
if (piec_lastresponse==3)
piec_wacknowledge();
else
if (piec_lastresponse==2)
piec_eof();
}
void piec_eof()
{
int i;
/* Toggle pin14 (AutoLF) 8 times to trigger
EOF at c64 end */
for(i=0;i<8;i++)
{
AUTOLFHIGH; AUTOLFLOW;
}
piec_lastresponse=2;
piec_delaycount=0;
}
int piec_busy_status()
{
/* Read busy status */
if (BUSY) return 1; else return 0;
}
int piec_readport()
{
/* Read value from port, with ATN adding 0x100 */
PARIN; POUTRHIGH;
return PARR|(ACK ? 0 : 0x100);
}
void piec_presentbyte(int byte)
{
POUTWHIGH;
/* Write byte */
PARW (byte);
}