forked from emestee/bk-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.c
87 lines (80 loc) · 1.74 KB
/
serial.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
#include "defines.h"
#define READY_IN 0200
#define READY_OUT 040
#define DATA_BIT 020
#define PROBE 0xBC
unsigned char retbyte;
unsigned char * data_ptr = 0;
char bitnum = 8;
unsigned char ready_out;
FILE * ser_in = 0;
/* Returns the ready bit and the data bit in the appropriate positions;
* the emulator is always ready to accept data. The data bits are only
* returned when ready_out is true.
*/
d_word
serial_read() {
unsigned char ret = READY_IN;
if (!data_ptr || !ready_out) {
/* Idle state - continuous stop bits */
return READY_IN | DATA_BIT;
}
if (bitnum == -1) {
bitnum = 0;
/* start bit is 0 */
return READY_IN & ~DATA_BIT;
}
if (bitnum == 8) {
/* Byte ended - stop bit is 1 */
return READY_IN | DATA_BIT;
}
ret |= *data_ptr & (1<<bitnum++) ? DATA_BIT : 0;
if (!ser_in) {
ser_in = fopen("serial.in", "r");
}
if (bitnum == 8) {
int c = getc(ser_in);
if (c == EOF) data_ptr = 0;
retbyte = c;
bitnum = -1;
}
return ret;
}
unsigned char ser_out = 1;
unsigned char forming;
char curbit = -1;
/* This is a cheat: we know that for each data bit there is a write
* to the port: no need to compare time marks.
*/
void
serial_write(d_word w) {
unsigned char cur = (w & DATA_BIT) != 0;
if (ready_out != (w & READY_OUT)) {
ready_out = w & READY_OUT;
}
if (!ready_out) {
/* Idle state */
bitnum = 8;
} else {
if (!data_ptr) {
/* Set up the probe byte */
retbyte = PROBE;
data_ptr = &retbyte;
}
bitnum = -1;
}
if (curbit >= 0) {
forming |= cur << curbit++;
if (curbit == 8) {
curbit = -1;
if (forming != PROBE) {
data_ptr = 0;
fprintf(stderr, "Serial line probe failed\n");
}
}
} else if (ser_out && !cur) {
/* Next write will be data bit */
curbit = 0;
}
ser_out = cur;
}