|
|
1.1 root 1: /*
1.1.1.4 root 2: Hatari - rs232.c
1.1 root 3:
1.1.1.4 root 4: This file is distributed under the GNU Public License, version 2 or at
5: your option any later version. Read the file gpl.txt for details.
1.1 root 6:
1.1.1.4 root 7: RS-232 Communications
1.1.1.3 root 8:
1.1.1.4 root 9: This is similar to the printing functions, we open a direct file
10: (e.g. /dev/ttyS0) and send bytes over it.
11: Using such method mimicks the ST exactly, and even allows us to connect
12: to an actual ST! To wait for incoming data, we create a thread which copies
13: the bytes into an input buffer. This method fits in with the internet code
14: which also reads data into a buffer.
15: */
1.1.1.13 root 16: const char RS232_fileid[] = "Hatari rs232.c : " __DATE__ " " __TIME__;
1.1.1.4 root 17:
1.1.1.9 root 18: #include <config.h>
1.1.1.4 root 19:
20: #if HAVE_TERMIOS_H
21: # include <termios.h>
22: # include <unistd.h>
23: #endif
24:
25: #include <SDL.h>
26: #include <SDL_thread.h>
27: #include <errno.h>
1.1.1.3 root 28:
1.1 root 29: #include "main.h"
1.1.1.4 root 30: #include "configuration.h"
1.1.1.10 root 31: #include "ioMem.h"
1.1.1.8 root 32: #include "m68000.h"
1.1.1.4 root 33: #include "mfp.h"
1.1 root 34: #include "rs232.h"
1.1.1.2 root 35:
1.1 root 36:
1.1.1.4 root 37: #define RS232_DEBUG 0
38:
39: #if RS232_DEBUG
40: #define Dprintf(a) printf a
41: #else
42: #define Dprintf(a)
43: #endif
44:
45:
1.1.1.13 root 46: bool bConnectedRS232 = false; /* Connection to RS232? */
1.1.1.8 root 47: static FILE *hComIn = NULL; /* Handle to file for reading */
48: static FILE *hComOut = NULL; /* Handle to file for writing */
1.1.1.4 root 49:
1.1.1.8 root 50: static unsigned char InputBuffer_RS232[MAX_RS232INPUT_BUFFER];
51: static int InputBuffer_Head=0, InputBuffer_Tail=0;
1.1.1.4 root 52:
1.1.1.8 root 53:
54: #if HAVE_TERMIOS_H
55:
56: #if !HAVE_CFMAKERAW
1.1.1.4 root 57: static inline void cfmakeraw(struct termios *termios_p)
58: {
59: termios_p->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
60: termios_p->c_oflag &= ~OPOST;
61: termios_p->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
62: termios_p->c_cflag &= ~(CSIZE|PARENB);
63: termios_p->c_cflag |= CS8;
64: }
65: #endif
66:
1.1.1.9 root 67:
68: #if defined(__AMIGAOS4__)
69:
70: // dummy functions. REMOVE THEM LATER
71:
72: int tcgetattr(int file_descriptor,struct termios *tios_p)
73: {
74: return -1;
75: }
76:
77: int tcsetattr(int file_descriptor,int action,struct termios *tios_p)
78: {
79: return -1;
80: }
81:
82: int cfsetospeed(struct termios *tios, speed_t ospeed)
83: {
84:
85: tios->c_ospeed = ospeed;
86:
87: return 0;
88: }
89:
90:
91: int cfsetispeed(struct termios *tios,speed_t ispeed)
92: {
93: tios->c_ispeed = ispeed;
94:
95: return 0;
96: }
97:
98: #endif /* __AMIGAOS4__ */
99:
100:
1.1.1.4 root 101: /*-----------------------------------------------------------------------*/
1.1.1.9 root 102: /**
103: * Set serial line parameters to "raw" mode.
104: */
1.1.1.11 root 105: static bool RS232_SetRawMode(FILE *fhndl)
1.1 root 106: {
1.1.1.8 root 107: struct termios termmode;
108: int fd;
1.1 root 109:
1.1.1.8 root 110: memset (&termmode, 0, sizeof(termmode)); /* Init with zeroes */
111: fd = fileno(fhndl); /* Get file descriptor */
1.1.1.4 root 112:
1.1.1.8 root 113: if (isatty(fd))
1.1.1.4 root 114: {
1.1.1.8 root 115: if (tcgetattr(fd, &termmode) != 0)
1.1.1.13 root 116: return false;
1.1.1.8 root 117:
118: /* Set "raw" mode: */
119: termmode.c_cc[VMIN] = 1;
120: termmode.c_cc[VTIME] = 0;
121: cfmakeraw(&termmode);
122: if (tcsetattr(fd, TCSADRAIN, &termmode) != 0)
1.1.1.13 root 123: return false;
1.1.1.4 root 124: }
125:
1.1.1.13 root 126: return true;
1.1.1.8 root 127: }
1.1.1.4 root 128:
129: /*-----------------------------------------------------------------------*/
1.1.1.9 root 130: /**
131: * Set hardware configuration of RS-232:
132: * - Bits per character
133: * - Parity
134: * - Start/stop bits
135: */
1.1.1.14! root 136: static bool RS232_SetBitsConfig(FILE *fhndl, int nCharSize, int nStopBits, bool bUseParity, bool bEvenParity)
1.1.1.4 root 137: {
138: struct termios termmode;
1.1.1.14! root 139: int fd;
1.1.1.4 root 140:
141: memset (&termmode, 0, sizeof(termmode)); /* Init with zeroes */
1.1.1.14! root 142: fd = fileno(fhndl);
1.1.1.4 root 143:
144: if (isatty(fd))
145: {
146: if (tcgetattr(fd, &termmode) != 0)
1.1.1.8 root 147: {
148: Dprintf(("RS232_SetBitsConfig: tcgetattr failed.\n"));
1.1.1.13 root 149: return false;
1.1.1.8 root 150: }
1.1.1.4 root 151:
1.1.1.8 root 152: /* Set the character size: */
153: termmode.c_cflag &= ~CSIZE;
154: switch (nCharSize)
155: {
156: case 8: termmode.c_cflag |= CS8; break;
157: case 7: termmode.c_cflag |= CS7; break;
158: case 6: termmode.c_cflag |= CS6; break;
159: case 5: termmode.c_cflag |= CS5; break;
160: }
161:
162: /* Set stop bits: */
163: if (nStopBits >= 2)
164: termmode.c_oflag |= CSTOPB;
165: else
166: termmode.c_oflag &= ~CSTOPB;
167:
168: /* Parity bit: */
169: if (bUseParity)
170: termmode.c_cflag |= PARENB;
171: else
172: termmode.c_cflag &= ~PARENB;
173:
174: if (bEvenParity)
175: termmode.c_cflag &= ~PARODD;
176: else
177: termmode.c_cflag |= PARODD;
178:
179: /* Now store the configuration: */
1.1.1.4 root 180: if (tcsetattr(fd, TCSADRAIN, &termmode) != 0)
1.1.1.8 root 181: {
182: Dprintf(("RS232_SetBitsConfig: tcsetattr failed.\n"));
1.1.1.13 root 183: return false;
1.1.1.8 root 184: }
1.1.1.4 root 185: }
186:
1.1.1.13 root 187: return true;
1.1 root 188: }
1.1.1.8 root 189:
1.1.1.4 root 190: #endif /* HAVE_TERMIOS_H */
1.1 root 191:
1.1.1.4 root 192:
193: /*-----------------------------------------------------------------------*/
1.1.1.9 root 194: /**
195: * Open file on COM port.
196: */
1.1.1.11 root 197: static bool RS232_OpenCOMPort(void)
1.1 root 198: {
1.1.1.13 root 199: bConnectedRS232 = false;
1.1.1.3 root 200:
1.1.1.4 root 201: /* Create our COM file for output */
1.1.1.9 root 202: hComOut = fopen(ConfigureParams.RS232.szOutFileName, "wb");
1.1.1.4 root 203: if (hComOut == NULL)
204: {
1.1.1.14! root 205: Log_Printf(LOG_WARN, "RS232: Failed to open output file %s\n",
! 206: ConfigureParams.RS232.szOutFileName);
1.1.1.13 root 207: return false;
1.1.1.4 root 208: }
209: setvbuf(hComOut, NULL, _IONBF, 0);
210:
211: /* Create our COM file for input */
1.1.1.9 root 212: hComIn = fopen(ConfigureParams.RS232.szInFileName, "rb");
1.1.1.4 root 213: if (hComIn == NULL)
214: {
1.1.1.14! root 215: Log_Printf(LOG_WARN, "RS232: Failed to open input file %s\n",
! 216: ConfigureParams.RS232.szInFileName);
1.1.1.4 root 217: fclose(hComOut); hComOut = NULL;
1.1.1.13 root 218: return false;
1.1.1.4 root 219: }
220: setvbuf(hComIn, NULL, _IONBF, 0);
221:
222: #if HAVE_TERMIOS_H
223: /* First set the output parameters to "raw" mode */
224: if (!RS232_SetRawMode(hComOut))
225: {
1.1.1.14! root 226: Log_Printf(LOG_WARN, "Can't set raw mode for %s\n",
1.1.1.4 root 227: ConfigureParams.RS232.szOutFileName);
228: }
229:
230: /* Now set the input parameters to "raw" mode */
231: if (!RS232_SetRawMode(hComIn))
232: {
1.1.1.14! root 233: Log_Printf(LOG_WARN, "Can't set raw mode for %s\n",
1.1.1.4 root 234: ConfigureParams.RS232.szInFileName);
235: }
236: #endif
1.1 root 237:
1.1.1.4 root 238: /* Set all OK */
1.1.1.13 root 239: bConnectedRS232 = true;
1.1 root 240:
1.1.1.4 root 241: Dprintf(("Successfully opened RS232 files.\n"));
1.1 root 242:
1.1.1.13 root 243: return true;
1.1 root 244: }
245:
1.1.1.4 root 246:
247: /*-----------------------------------------------------------------------*/
1.1.1.9 root 248: /**
249: * Close file on COM port
250: */
1.1.1.8 root 251: static void RS232_CloseCOMPort(void)
1.1 root 252: {
1.1.1.4 root 253: /* Do have file open? */
254: if (bConnectedRS232)
255: {
1.1.1.13 root 256: bConnectedRS232 = false;
1.1.1.4 root 257:
258: /* Close */
259: fclose(hComIn);
260: hComIn = NULL;
1.1 root 261:
1.1.1.4 root 262: fclose(hComOut);
263: hComOut = NULL;
264:
265: Dprintf(("Closed RS232 files.\n"));
266: }
1.1 root 267: }
268:
1.1.1.4 root 269:
1.1.1.8 root 270: /* thread stuff */
271: static SDL_sem* pSemFreeBuf; /* Semaphore to sync free space in InputBuffer_RS232 */
272: static SDL_Thread *RS232Thread = NULL; /* Thread handle for reading incoming data */
273:
274:
1.1.1.4 root 275: /*-----------------------------------------------------------------------*/
1.1.1.9 root 276: /**
277: * Add incoming bytes from other machine into our input buffer
278: */
1.1.1.8 root 279: static void RS232_AddBytesToInputBuffer(unsigned char *pBytes, int nBytes)
1.1.1.4 root 280: {
1.1.1.8 root 281: int i;
1.1 root 282:
1.1.1.8 root 283: /* Copy bytes into input buffer */
284: for (i=0; i<nBytes; i++)
285: {
286: SDL_SemWait(pSemFreeBuf); /* Wait for free space in buffer */
287: InputBuffer_RS232[InputBuffer_Tail] = *pBytes++;
288: InputBuffer_Tail = (InputBuffer_Tail+1) % MAX_RS232INPUT_BUFFER;
289: }
290: }
1.1.1.4 root 291:
1.1.1.8 root 292:
293: /*-----------------------------------------------------------------------*/
1.1.1.9 root 294: /**
295: * Thread to read incoming RS-232 data, and pass to emulator input buffer
296: */
1.1.1.8 root 297: static int RS232_ThreadFunc(void *pData)
298: {
299: int iInChar;
300: unsigned char cInChar;
301:
302: /* Check for any RS-232 incoming data */
1.1.1.13 root 303: while (true)
1.1.1.4 root 304: {
1.1.1.8 root 305: if (hComIn)
1.1.1.4 root 306: {
1.1.1.8 root 307: /* Read the bytes in, if we have any */
308: iInChar = fgetc(hComIn);
309: if (iInChar != EOF)
310: {
311: /* Copy into our internal queue */
312: cInChar = iInChar;
313: RS232_AddBytesToInputBuffer(&cInChar, 1);
314: /* FIXME: Use semaphores to lock MFP variables? */
315: MFP_InputOnChannel(MFP_RCVBUFFULL_BIT, MFP_IERA, &MFP_IPRA);
316: Dprintf(("RS232: Read character $%x\n", iInChar));
317: }
318: else
319: {
320: /*Dprintf(("RS232: Reached end of input file!\n"));*/
321: clearerr(hComIn);
322: SDL_Delay(20);
323: }
1.1.1.4 root 324:
1.1.1.8 root 325: /* Sleep for a while */
326: SDL_Delay(2);
327: }
328: else
1.1.1.4 root 329: {
1.1.1.8 root 330: /* No RS-232 connection, sleep for 20ms */
331: SDL_Delay(20);
1.1.1.4 root 332: }
1.1.1.8 root 333: }
1.1.1.4 root 334:
1.1.1.13 root 335: return true;
1.1.1.8 root 336: }
1.1.1.4 root 337:
338:
1.1.1.8 root 339: /*-----------------------------------------------------------------------*/
1.1.1.9 root 340: /**
341: * Initialize RS-232, start thread to wait for incoming data
342: * (we will open a connection when first bytes are sent).
343: */
1.1.1.8 root 344: void RS232_Init(void)
345: {
346: if (ConfigureParams.RS232.bEnableRS232)
347: {
348: /* Create semaphore */
349: if (pSemFreeBuf == NULL)
350: pSemFreeBuf = SDL_CreateSemaphore(MAX_RS232INPUT_BUFFER);
351: if (pSemFreeBuf == NULL)
352: {
1.1.1.14! root 353: Log_Printf(LOG_WARN, "RS232_Init: Can't create semaphore!\n");
1.1.1.8 root 354: return;
355: }
1.1.1.4 root 356:
1.1.1.8 root 357: if (!bConnectedRS232)
358: RS232_OpenCOMPort();
359:
360: /* Create thread to wait for incoming bytes over RS-232 */
361: if (!RS232Thread)
1.1.1.4 root 362: {
1.1.1.8 root 363: RS232Thread = SDL_CreateThread(RS232_ThreadFunc, NULL);
364: Dprintf(("RS232 thread has been created.\n"));
1.1.1.4 root 365: }
366: }
1.1.1.8 root 367: }
1.1.1.4 root 368:
1.1.1.8 root 369:
370: /*-----------------------------------------------------------------------*/
1.1.1.9 root 371: /**
372: * Close RS-232 connection and stop checking for incoming data.
373: */
1.1.1.8 root 374: void RS232_UnInit(void)
375: {
376: /* Close, kill thread and free resource */
377: if (RS232Thread)
378: {
379: /* Instead of killing the thread directly, we should probably better
380: inform it via IPC so that it can terminate gracefully... */
381: Dprintf(("Killing RS232 thread...\n"));
382: SDL_KillThread(RS232Thread);
383: RS232Thread = NULL;
384: }
385: RS232_CloseCOMPort();
1.1.1.9 root 386:
1.1.1.8 root 387: if (pSemFreeBuf)
388: {
389: SDL_DestroySemaphore(pSemFreeBuf);
390: pSemFreeBuf = NULL;
391: }
1.1.1.4 root 392: }
1.1 root 393:
1.1.1.4 root 394:
395: /*-----------------------------------------------------------------------*/
1.1.1.9 root 396: /**
397: * Set hardware configuration of RS-232 according to the USART control register.
398: *
399: * ucr: USART Control Register
400: * Bit 0: unused
401: * Bit 1: 0-Odd Parity, 1-Even Parity
402: * Bit 2: 0-No Parity, 1-Parity
403: * Bits 3,4: Start/Stop bits
404: * 0 0 : 0-Start, 0-Stop Synchronous
405: * 0 1 : 0-Start, 1-Stop Asynchronous
406: * 1 0 : 1-Start, 1.5-Stop Asynchronous
407: * 1 1 : 1-Start, 2-Stop Asynchronous
408: * Bits 5,6: 'WordLength'
409: * 0 0 : 8 Bits
410: * 0 1 : 7 Bits
411: * 1 0 : 6 Bits
412: * 1 1 : 5 Bits
413: * Bit 7: Frequency from TC and RC
414: */
1.1.1.14! root 415: void RS232_HandleUCR(Sint16 ucr)
1.1.1.4 root 416: {
417: #if HAVE_TERMIOS_H
418: int nCharSize; /* Bits per character: 5, 6, 7 or 8 */
419: int nStopBits; /* Stop bits: 0=0 bits, 1=1 bit, 2=1.5 bits, 3=2 bits */
420:
421: nCharSize = 8 - ((ucr >> 5) & 3);
422: nStopBits = (ucr >> 3) & 3;
423:
424: Dprintf(("RS232_HandleUCR(%i) : character size=%i , stop bits=%i\n",
425: ucr, nCharSize, nStopBits));
426:
427: if (hComOut != NULL)
428: {
1.1.1.14! root 429: if (!RS232_SetBitsConfig(hComOut, nCharSize, nStopBits, ucr&4, ucr&2))
! 430: Log_Printf(LOG_WARN, "RS232_HandleUCR: failed to set bits configuration for %s\n", ConfigureParams.RS232.szOutFileName);
1.1.1.4 root 431: }
432:
433: if (hComIn != NULL)
434: {
1.1.1.14! root 435: if (!RS232_SetBitsConfig(hComIn, nCharSize, nStopBits, ucr&4, ucr&2))
! 436: Log_Printf(LOG_WARN, "RS232_HandleUCR: failed to set bits configuration for %s\n", ConfigureParams.RS232.szInFileName);
1.1.1.4 root 437: }
1.1.1.5 root 438: #endif /* HAVE_TERMIOS_H */
1.1.1.4 root 439: }
440:
441:
442: /*-----------------------------------------------------------------------*/
1.1.1.9 root 443: /**
444: * Set baud rate configuration of RS-232.
445: */
1.1.1.11 root 446: bool RS232_SetBaudRate(int nBaud)
1.1.1.4 root 447: {
448: #if HAVE_TERMIOS_H
449: int i;
450: int fd;
451: speed_t baudtype;
452: struct termios termmode;
1.1.1.8 root 453: static const int baudtable[][2] =
1.1.1.4 root 454: {
455: { 50, B50 },
456: { 75, B75 },
457: { 110, B110 },
458: { 134, B134 },
459: { 150, B150 },
460: { 200, B200 },
461: { 300, B300 },
462: { 600, B600 },
463: { 1200, B1200 },
464: { 1800, B1800 },
465: { 2400, B2400 },
466: { 4800, B4800 },
467: { 9600, B9600 },
468: { 19200, B19200 },
469: { 38400, B38400 },
470: { 57600, B57600 },
471: { 115200, B115200 },
1.1.1.8 root 472: #ifdef B230400 /* B230400 is not defined on all systems */
1.1.1.4 root 473: { 230400, B230400 },
1.1.1.8 root 474: #endif
1.1.1.4 root 475: { -1, -1 }
476: };
477:
478: Dprintf(("RS232_SetBaudRate(%i)\n", nBaud));
479:
480: /* Convert baud number to baud termios constant: */
481: baudtype = -1;
482: for (i = 0; baudtable[i][0] != -1; i++)
483: {
484: if (baudtable[i][0] == nBaud)
485: {
486: baudtype = baudtable[i][1];
487: break;
488: }
489: }
490:
1.1.1.5 root 491: if (baudtype == (speed_t)-1)
1.1.1.4 root 492: {
493: Dprintf(("RS232_SetBaudRate: Unsupported baud rate %i.\n", nBaud));
1.1.1.13 root 494: return false;
1.1.1.4 root 495: }
496:
497: /* Set ouput speed: */
498: if (hComOut != NULL)
499: {
500: memset (&termmode, 0, sizeof(termmode)); /* Init with zeroes */
501: fd = fileno(hComOut);
502: if (isatty(fd))
503: {
504: if (tcgetattr(fd, &termmode) != 0)
1.1.1.13 root 505: return false;
1.1.1.4 root 506:
507: cfsetospeed(&termmode, baudtype);
508:
509: if (tcsetattr(fd, TCSADRAIN, &termmode) != 0)
1.1.1.13 root 510: return false;
1.1.1.4 root 511: }
512: }
513:
514: /* Set input speed: */
515: if (hComIn != NULL)
516: {
517: memset (&termmode, 0, sizeof(termmode)); /* Init with zeroes */
518: fd = fileno(hComIn);
519: if (isatty(fd))
520: {
521: if (tcgetattr(fd, &termmode) != 0)
1.1.1.13 root 522: return false;
1.1.1.4 root 523:
524: cfsetispeed(&termmode, baudtype);
525:
526: if (tcsetattr(fd, TCSADRAIN, &termmode) != 0)
1.1.1.13 root 527: return false;
1.1.1.4 root 528: }
529: }
530: #endif /* HAVE_TERMIOS_H */
531:
1.1.1.13 root 532: return true;
1.1.1.4 root 533: }
534:
535:
536: /*-----------------------------------------------------------------------*/
1.1.1.9 root 537: /**
538: * Set baud rate configuration of RS-232 according to the Timer-D hardware
539: * registers.
540: */
1.1.1.4 root 541: void RS232_SetBaudRateFromTimerD(void)
1.1 root 542: {
1.1.1.4 root 543: int nTimerD_CR, nTimerD_DR, nBaudRate;
544:
1.1.1.10 root 545: nTimerD_CR = IoMem[0xfffa1d] & 0x07;
546: nTimerD_DR = IoMem[0xfffa25];
1.1.1.4 root 547:
548: if (!nTimerD_CR)
549: return;
550:
1.1.1.13 root 551: if ( nTimerD_DR == 0 )
552: nTimerD_DR = 256; /* In MFP, a data register=0 is in fact 256 */
553:
1.1.1.4 root 554: /* Calculate baud rate: (MFP/Timer-D is supplied with 2.4576 MHz) */
555: nBaudRate = 2457600 / nTimerD_DR / 2;
1.1.1.11 root 556:
557: /*if (IoMem[0xfffa29] & 0x80)*/ /* We only support the by-16 prescaler */
558: nBaudRate /= 16;
559:
1.1.1.4 root 560: switch (nTimerD_CR)
561: {
562: case 1: nBaudRate /= 4; break;
563: case 2: nBaudRate /= 10; break;
564: case 3: nBaudRate /= 16; break;
565: case 4: nBaudRate /= 50; break;
566: case 5: nBaudRate /= 64; break;
567: case 6: nBaudRate /= 100; break;
568: case 7: nBaudRate /= 200; break;
569: }
570:
571: /* Adjust some ugly baud rates from TOS to more reasonable values: */
572: switch (nBaudRate)
573: {
574: case 80: nBaudRate = 75; break;
575: case 109: nBaudRate = 110; break;
576: case 120: nBaudRate = 110; break;
577: case 1745: nBaudRate = 1800; break;
578: case 1920: nBaudRate = 1800; break;
579: }
1.1 root 580:
1.1.1.4 root 581: RS232_SetBaudRate(nBaudRate);
582: }
583:
584:
585: /*-----------------------------------------------------------------------*/
1.1.1.9 root 586: /**
587: * Set flow control configuration of RS-232.
588: */
1.1.1.14! root 589: void RS232_SetFlowControl(Sint16 ctrl)
1.1.1.4 root 590: {
591: Dprintf(("RS232_SetFlowControl(%i)\n", ctrl));
592:
593: /* Not yet written */
1.1 root 594: }
595:
1.1.1.4 root 596:
597: /*----------------------------------------------------------------------- */
1.1.1.9 root 598: /**
599: * Pass bytes from emulator to RS-232
600: */
1.1.1.14! root 601: bool RS232_TransferBytesTo(Uint8 *pBytes, int nBytes)
1.1 root 602: {
1.1.1.4 root 603: /* Do need to open a connection to RS232? */
604: if (!bConnectedRS232)
605: {
606: /* Do have RS-232 enabled? */
607: if (ConfigureParams.RS232.bEnableRS232)
608: bConnectedRS232 = RS232_OpenCOMPort();
609: }
610:
611: /* Have we connected to the RS232? */
612: if (bConnectedRS232)
613: {
614: /* Send bytes directly to the COM file */
615: if (fwrite(pBytes, 1, nBytes, hComOut))
616: {
617: Dprintf(("RS232: Sent %i bytes ($%x ...)\n", nBytes, *pBytes));
618: MFP_InputOnChannel(MFP_TRNBUFEMPTY_BIT, MFP_IERA, &MFP_IPRA);
619:
1.1.1.13 root 620: return true; /* OK */
1.1.1.4 root 621: }
622: }
623:
1.1.1.13 root 624: return false; /* Failed */
1.1.1.4 root 625: }
626:
627:
628: /*-----------------------------------------------------------------------*/
1.1.1.9 root 629: /**
630: * Read characters from our internal input buffer (bytes from other machine)
631: */
1.1.1.14! root 632: bool RS232_ReadBytes(Uint8 *pBytes, int nBytes)
1.1.1.4 root 633: {
634: int i;
635:
636: /* Connected? */
637: if (bConnectedRS232 && InputBuffer_Head != InputBuffer_Tail)
638: {
639: /* Read bytes out of input buffer */
640: for (i=0; i<nBytes; i++)
641: {
642: *pBytes++ = InputBuffer_RS232[InputBuffer_Head];
643: InputBuffer_Head = (InputBuffer_Head+1) % MAX_RS232INPUT_BUFFER;
644: SDL_SemPost(pSemFreeBuf); /* Signal free space */
645: }
1.1.1.13 root 646: return true;
1.1.1.4 root 647: }
648:
1.1.1.13 root 649: return false;
1.1.1.4 root 650: }
651:
652:
653: /*-----------------------------------------------------------------------*/
1.1.1.9 root 654: /**
1.1.1.13 root 655: * Return true if bytes waiting!
1.1.1.9 root 656: */
1.1.1.11 root 657: bool RS232_GetStatus(void)
1.1.1.4 root 658: {
659: /* Connected? */
660: if (bConnectedRS232)
661: {
662: /* Do we have bytes in the input buffer? */
663: if (InputBuffer_Head != InputBuffer_Tail)
1.1.1.13 root 664: return true;
1.1.1.4 root 665: }
1.1 root 666:
1.1.1.4 root 667: /* No, none */
1.1.1.13 root 668: return false;
1.1.1.4 root 669: }
1.1 root 670:
671:
1.1.1.4 root 672: /*-----------------------------------------------------------------------*/
1.1.1.9 root 673: /**
674: * Read from the Syncronous Character Register.
675: */
1.1.1.4 root 676: void RS232_SCR_ReadByte(void)
1.1 root 677: {
1.1.1.8 root 678: M68000_WaitState(4);
679:
680: /* nothing */
1.1.1.4 root 681: }
682:
683: /*-----------------------------------------------------------------------*/
1.1.1.9 root 684: /**
685: * Write to the Syncronous Character Register.
686: */
1.1.1.4 root 687: void RS232_SCR_WriteByte(void)
688: {
1.1.1.8 root 689: M68000_WaitState(4);
690:
1.1.1.10 root 691: /*Dprintf(("RS232: Write to SCR: $%x\n", (int)IoMem[0xfffa27]));*/
1.1.1.4 root 692: }
1.1 root 693:
1.1.1.4 root 694:
695: /*-----------------------------------------------------------------------*/
1.1.1.9 root 696: /**
697: * Read from the USART Control Register.
698: */
1.1.1.4 root 699: void RS232_UCR_ReadByte(void)
700: {
1.1.1.8 root 701: M68000_WaitState(4);
702:
1.1.1.10 root 703: Dprintf(("RS232: Read from UCR: $%x\n", (int)IoMem[0xfffa29]));
1.1.1.4 root 704: }
1.1 root 705:
1.1.1.4 root 706: /*-----------------------------------------------------------------------*/
1.1.1.9 root 707: /**
708: * Write to the USART Control Register.
709: */
1.1.1.4 root 710: void RS232_UCR_WriteByte(void)
711: {
1.1.1.8 root 712: M68000_WaitState(4);
713:
1.1.1.10 root 714: Dprintf(("RS232: Write to UCR: $%x\n", (int)IoMem[0xfffa29]));
1.1.1.4 root 715:
716: if (bConnectedRS232)
1.1.1.10 root 717: RS232_HandleUCR(IoMem[0xfffa29]);
1.1 root 718: }
719:
1.1.1.4 root 720:
721: /*-----------------------------------------------------------------------*/
1.1.1.9 root 722: /**
723: * Read from the Receiver Status Register.
724: */
1.1.1.4 root 725: void RS232_RSR_ReadByte(void)
1.1 root 726: {
1.1.1.8 root 727: M68000_WaitState(4);
728:
1.1.1.4 root 729: if (RS232_GetStatus())
1.1.1.10 root 730: IoMem[0xfffa2b] |= 0x80; /* Buffer full */
1.1.1.4 root 731: else
1.1.1.10 root 732: IoMem[0xfffa2b] &= ~0x80; /* Buffer not full */
1.1.1.4 root 733:
1.1.1.10 root 734: Dprintf(("RS232: Read from RSR: $%x\n", (int)IoMem[0xfffa2b]));
1.1.1.4 root 735: }
736:
737: /*-----------------------------------------------------------------------*/
1.1.1.9 root 738: /**
739: * Write to the Receiver Status Register.
740: */
1.1.1.4 root 741: void RS232_RSR_WriteByte(void)
742: {
1.1.1.8 root 743: M68000_WaitState(4);
744:
1.1.1.10 root 745: Dprintf(("RS232: Write to RSR: $%x\n", (int)IoMem[0xfffa2b]));
1.1 root 746: }
747:
1.1.1.4 root 748:
749: /*-----------------------------------------------------------------------*/
1.1.1.9 root 750: /**
751: * Read from the Transmitter Status Register.
752: */
1.1.1.4 root 753: void RS232_TSR_ReadByte(void)
1.1 root 754: {
1.1.1.8 root 755: M68000_WaitState(4);
756:
1.1.1.4 root 757: if (ConfigureParams.RS232.bEnableRS232)
1.1.1.10 root 758: IoMem[0xfffa2d] |= 0x80; /* Buffer empty */
1.1.1.4 root 759: else
1.1.1.10 root 760: IoMem[0xfffa2d] &= ~0x80; /* Buffer not empty */
1.1 root 761:
1.1.1.10 root 762: Dprintf(("RS232: Read from TSR: $%x\n", (int)IoMem[0xfffa2d]));
1.1 root 763: }
764:
1.1.1.4 root 765: /*-----------------------------------------------------------------------*/
1.1.1.9 root 766: /**
767: * Write to the Transmitter Status Register.
768: */
1.1.1.4 root 769: void RS232_TSR_WriteByte(void)
770: {
1.1.1.8 root 771: M68000_WaitState(4);
772:
1.1.1.10 root 773: Dprintf(("RS232: Write to TSR: $%x\n", (int)IoMem[0xfffa2d]));
1.1.1.4 root 774: }
775:
776:
777: /*-----------------------------------------------------------------------*/
1.1.1.9 root 778: /**
779: * Read from the USART Data Register.
780: */
1.1.1.4 root 781: void RS232_UDR_ReadByte(void)
1.1 root 782: {
1.1.1.14! root 783: Uint8 InByte = 0;
1.1 root 784:
1.1.1.8 root 785: M68000_WaitState(4);
786:
1.1.1.4 root 787: RS232_ReadBytes(&InByte, 1);
1.1.1.10 root 788: IoMem[0xfffa2f] = InByte;
789: Dprintf(("RS232: Read from UDR: $%x\n", (int)IoMem[0xfffa2f]));
1.1.1.4 root 790:
791: if (RS232_GetStatus()) /* More data waiting? */
792: {
793: /* Yes, generate another interrupt. */
794: MFP_InputOnChannel(MFP_RCVBUFFULL_BIT, MFP_IERA, &MFP_IPRA);
795: }
1.1 root 796: }
1.1.1.4 root 797:
798: /*-----------------------------------------------------------------------*/
1.1.1.9 root 799: /**
800: * Write to the USART Data Register.
801: */
1.1.1.4 root 802: void RS232_UDR_WriteByte(void)
803: {
1.1.1.14! root 804: Uint8 OutByte;
1.1.1.4 root 805:
1.1.1.8 root 806: M68000_WaitState(4);
807:
1.1.1.10 root 808: OutByte = IoMem[0xfffa2f];
1.1.1.4 root 809: RS232_TransferBytesTo(&OutByte, 1);
1.1.1.10 root 810: Dprintf(("RS232: Write to UDR: $%x\n", (int)IoMem[0xfffa2f]));
1.1.1.4 root 811: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.