-
Notifications
You must be signed in to change notification settings - Fork 6
/
serial-r8810.c
158 lines (108 loc) · 2.7 KB
/
serial-r8810.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
//-------------------------------------------------------------------------------
// EMU86 - R8810 serial port
//-------------------------------------------------------------------------------
#include "emu-serial.h"
#include "emu-int.h"
#include "int-r8810.h"
#include <stdio.h>
#define SERIAL_MODE 0
#define SERIAL_STAT 1
#define SERIAL_TDATA 2
#define SERIAL_RDATA 3
#define SERIAL_RATE 4
static word_t serial_regs [5];
#define SERIAL_MODE_TXIE 0x0800
#define SERIAL_MODE_RXIE 0x0400
#define SERIAL_STAT_TEMT 0x0040
#define SERIAL_STAT_THRE 0x0020
#define SERIAL_STAT_RDR 0x0010
// Set the serial interrupt line
void serial_int (void)
{
int stat = 0;
if ((serial_regs [SERIAL_STAT] & SERIAL_STAT_RDR)
&& (serial_regs [SERIAL_MODE] & SERIAL_MODE_RXIE))
stat = 1;
if ((serial_regs [SERIAL_STAT] & SERIAL_STAT_THRE)
&& (serial_regs [SERIAL_MODE] & SERIAL_MODE_TXIE))
stat = 1;
int_line_set (INT_LINE_SERIAL, stat);
}
// Serial device procedure
// Called from main emulator loop
static int loop_count = 0;
#define LOOP_MAX 5000
int serial_proc (void)
{
int err = 0;
while (1) {
// Input stream
while (1)
{
// Moderate the poll rate
// for more instruction processing
loop_count++;
if (loop_count < LOOP_MAX) break;
loop_count = 0;
int res = serial_poll ();
if (!res) break;
byte_t c;
err = serial_recv (&c);
if (err) break;
// Overflow detection
if (serial_regs [SERIAL_STAT] & SERIAL_STAT_RDR)
{
puts ("\nwarning: serial port input overflow");
// TODO: set overflow flag in status register
}
serial_regs [SERIAL_RDATA] = (word_t) c;
serial_regs [SERIAL_STAT] |= SERIAL_STAT_RDR;
serial_int ();
break;
}
if (err) break;
// Output stream
while (1) {
if (serial_regs [SERIAL_STAT] & SERIAL_STAT_THRE) break;
err = serial_send ((byte_t) serial_regs [SERIAL_TDATA]);
if (err) break;
serial_regs [SERIAL_STAT] |= SERIAL_STAT_TEMT | SERIAL_STAT_THRE;
serial_int ();
break;
}
break;
}
return err;
}
// Serial I/O read
int serial_io_read (word_t p, word_t * w)
{
int r = p >> 1;
*w = serial_regs [r];
if (r == SERIAL_RDATA) {
serial_regs [SERIAL_STAT] &= ~SERIAL_STAT_RDR;
serial_int ();
}
return 0;
}
// Serial I/O write
int serial_io_write (word_t p, word_t w)
{
int r = p >> 1;
serial_regs [r] = w;
if (r == SERIAL_TDATA) {
serial_regs [SERIAL_STAT] &= ~(SERIAL_STAT_TEMT | SERIAL_STAT_THRE);
serial_int ();
}
return 0;
}
// Serial device initialization
void serial_dev_init ()
{
// Initialize registers
serial_regs [SERIAL_MODE] = SERIAL_MODE_RXIE;
serial_regs [SERIAL_STAT] = SERIAL_STAT_TEMT | SERIAL_STAT_THRE;
}
void serial_dev_term ()
{
}