-
Notifications
You must be signed in to change notification settings - Fork 8
/
mc6850.cpp
218 lines (190 loc) · 4.89 KB
/
mc6850.cpp
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
// m6850.cpp - Motorola 6850 UART simulation via console terminal
//
////////////////////////////////////////////////////////////////////////////////
//
// c-simple-emu-cbm (C++ Portable Version)
// C64/6502 Emulator for Microsoft Windows Console
//
// MIT License
//
// Copyright (c) 2024 by David R. Van Wagner
// davevw.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////
#include "mc6850.h"
#include <stdio.h>
#ifdef WINDOWS
#include <conio.h>
#else
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
static byte readbyte;
static byte readbyte_count = 0;
static bool _kbhit()
{
if (readbyte_count > 0)
return true;
int result = read(STDIN_FILENO, &readbyte, 1);
if (result >= 0)
readbyte_count = result;
return (bool)readbyte_count;
}
static byte _getch()
{
if (readbyte_count == 0 && !_kbhit())
return 0;
readbyte_count = 0; // mark read
return readbyte;
}
struct termios save_term;
#endif
MC6850::MC6850(bool line_editor)
{
this->line_editor = line_editor;
#ifndef WINDOWS
if (!line_editor)
{
struct termios term;
tcgetattr(STDIN_FILENO, &save_term);
cfmakeraw(&term);
term.c_cc[VMIN] = 0;
term.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &term);
}
#endif
status.value = 0;
control.value = 0;
control.rts_tint = RTS_TINT::low_disabled;
control.clk = CLOCK::RESET;
// require reset by user before use
clear_receive_data_register_full();
clear_transmit_data_register_empty();
}
MC6850::~MC6850()
{
#ifndef WINDOWS
if (!line_editor)
tcsetattr(STDIN_FILENO, TCSANOW, &save_term);
#endif
}
byte MC6850::read_data()
{
// TODO: on async receive (simulated?) set receive_data_register_full, set interrupt if enabled
clear_irq();
byte data = 0;
bool keypressed = false;
if (line_editor)
{
data = getchar();
keypressed = true;
}
else if (_kbhit())
{
data = _getch();
keypressed = true;
}
if (control.mode == MODE::b7e1
|| control.mode == MODE::b7e2
|| control.mode == MODE::b7o1
|| control.mode == MODE::b7o2
)
data = data & 0x7F; // TODO: parity
if (keypressed)
clear_receive_data_register_full();
// standardize backspace so firmware can be consistent
if (data == 0x7F)
data = 8;
return data;
}
void MC6850::write_data(byte value)
{
if (control.clk == CLOCK::RESET)
return; // require configuration
// TODO: parity
if (control.mode == MODE::b7e1
|| control.mode == MODE::b7e2
|| control.mode == MODE::b7o1
|| control.mode == MODE::b7o2)
{
value &= 0x7F;
}
if (value == '\n')
putchar('\r'); // insert carriage return before newline
if (value >= ' ' || value == '\r' || value == '\n' || value == 8 || value == 7) // skip most other control codes
putchar(value);
if (value == '\r')
putchar('\n'); // add newline after carriage return
fflush(stdout);
clear_irq();
clear_transmit_data_register_empty();
// TODO: delay and set interrupt per transmission time
set_transmit_data_register_empty();
}
byte MC6850::read_status()
{
if (!line_editor)
status.rdrf = _kbhit() ? 1 : 0;
return status.value;
}
void MC6850::write_control(byte value)
{
CONTROL incoming { };
incoming.value = value;
if (control.clk == CLOCK::RESET && incoming.clk != CLOCK::RESET) {
if (line_editor)
set_receive_data_register_full(); // simulate always have byte to read // TODO: check
set_transmit_data_register_empty();
}
else if (incoming.clk == CLOCK::RESET) {
status.value = 0;
status.tdre = 1;
}
control.value = value;
}
bool MC6850::read_irq() const
{
return (bool)(status.irqn);
}
void MC6850::clear_irq()
{
status.irqn = 0;
}
void MC6850::set_irq()
{
status.irqn = 1;
}
void MC6850::clear_receive_data_register_full()
{
status.rdrf = 0;
}
void MC6850::set_receive_data_register_full()
{
status.rdrf = 1;
}
void MC6850::clear_transmit_data_register_empty()
{
status.tdre = 0;
}
void MC6850::set_transmit_data_register_empty()
{
status.tdre = 1;
}