-
Notifications
You must be signed in to change notification settings - Fork 25
/
6551.c
309 lines (274 loc) · 8.64 KB
/
6551.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
/*
** Oricutron
** Copyright (C) 2009-2014 Peter Gordon
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation, version 2
** of the License.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
** 6551 ACIA emulation
*/
#include "system.h"
#include "6502.h"
#include "via.h"
#include "8912.h"
#include "gui.h"
#include "disk.h"
#include "monitor.h"
#include "6551.h"
#include "machine.h"
#define DEBUG_ACIA 0
// 3 - Full console logging
#if DEBUG_ACIA == 3
#undef dbg_printf
#define dbg_printf(x...) { printf(x); printf("\n"); }
#define DEBUG_ACIA_STATUS 1
// 2 - Console logging with out noisy status
#elif DEBUG_ACIA == 2
#undef dbg_printf
#define dbg_printf(x...) { printf(x); printf("\n"); }
#define DEBUG_ACIA_STATUS 0
// 1 - Full monitor logging
#elif DEBUG_ACIA == 1
#define DEBUG_ACIA_STATUS 1
// 0 - Disable logging
#else
#define DEBUG_ACIA_STATUS 0
#endif
#if DEBUG_ACIA
static char *writereg_names[] = { "TXDATA", "RESET", "COMMAND", "CONTROL" };
static char *readreg_names[] = { "RXDATA", "STATUS", "COMMAND", "CONTROL" };
static char *parity_modes[] = { "NONE", "ODD", "NONE", "EVEN", "NONE", "MARK", "NONE", "SPACE" };
static char *txcon_modes[] = { "TXIRQ=0,RLEV=H", "TXIRQ=1,RLEV=L", "RXIRQ=0,RLEV=L", "RXIRQ=0,RLEV=L,TXBRK" };
static char *baud_rates[] = { "EXT", "50", "75", "109.92", "134.58", "150", "300", "600", "1200", "1800", "2400", "3600", "4800", "7200", "9600", "19200" };
static char *bit_nums[] = { "8", "7", "6", "5" };
#endif
// NOTE: work with integers 109.92 and 134.58 are approximated
static Uint32 boudrates[] = { 0, 50, 75, 110, 134, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600, 19200 };
static void acia_update_params( struct acia *acia )
{
switch(8 - ((acia->regs[ACIA_CONTROL]&ACONF_WLEN)>>5))
{
case 5: acia->bitmask = 0x1f; break;
case 6: acia->bitmask = 0x3f; break;
case 7: acia->bitmask = 0x7f; break;
case 8: default: acia->bitmask = 0xff; break;
}
acia->rx = (acia->regs[ACIA_COMMAND] & ACOMF_DTR)? SDL_TRUE : SDL_FALSE;
acia->irqrx = (acia->rx && 0 == ( acia->regs[ACIA_COMMAND] & ACOMF_IRQDIS ))? SDL_TRUE : SDL_FALSE;
switch( ( acia->regs[ACIA_COMMAND] & ACOMF_TXCON ) >> 2 )
{
case 0:
acia->tx = SDL_FALSE;
acia->irqtx = SDL_FALSE;
break;
case 1:
acia->tx = SDL_TRUE;
acia->irqtx = SDL_TRUE;
break;
case 2:
acia->tx = SDL_TRUE;
acia->irqtx = SDL_FALSE;
break;
case 3:
// FIXME: ACIA transmit BRK...
acia->tx = SDL_FALSE;
acia->irqtx = SDL_FALSE;
break;
}
acia->echo = (acia->regs[ACIA_COMMAND]&ACOMF_ECHO)? SDL_TRUE : SDL_FALSE;
// FIXME: calculate number of frame bits include 1.5 stop bits
acia->framebits =
(1) + // start bit
(8 - ((acia->regs[ACIA_CONTROL]&ACONF_WLEN)>>5)) + // data bits
((acia->regs[ACIA_COMMAND]>>5)&1) + // parity bit_nums
((acia->regs[ACIA_CONTROL]&ACONF_STOPB) ? 2 : 1); // stop bit(s)
acia->boudrate = boudrates[acia->regs[ACIA_CONTROL]&ACONF_BAUD];
// 1.8432 MHz / 16 (internal divider) = 115,2 kHz
// 115,2 kHz => ~ 8.68 usec
if(0 < acia->boudrate)
acia->framecycle = ( 115200 * 8680 ) / ( acia->boudrate * 1000 );
else
acia->framecycle = 0;
}
void acia_init( struct acia *acia, struct machine *oric )
{
if( acia->done )
acia->done( acia );
acia->oric = oric;
acia->regs[ACIA_RXDATA] = 0;
acia->regs[ACIA_TXDATA] = 0;
acia->regs[ACIA_STATUS] = ASTF_TXEMPTY;
acia->regs[ACIA_COMMAND] = ACOMF_IRQDIS;
acia->regs[ACIA_CONTROL] = 0;
acia->cycles = 0;
acia_update_params( acia );
switch(oric->aciabackend)
{
case ACIA_TYPE_LOOPBACK:
acia_init_loopback( acia );
break;
#ifdef BACKEND_MODEM
case ACIA_TYPE_MODEM:
acia_init_modem( acia );
break;
#endif
#ifdef BACKEND_COM
case ACIA_TYPE_COM:
acia_init_com( acia );
break;
#endif
case ACIA_TYPE_NONE:
default:
if ( oric->type == MACH_TELESTRAT )
{
oric->aciabackend = ACIA_TYPE_LOOPBACK;
acia_init_loopback( acia );
}
else
acia_init_none( acia );
break;
}
}
void acia_clock( struct acia *acia, unsigned int cycles )
{
// EXT clock used - not implemented
if( 0 == ( acia->regs[ACIA_CONTROL] & ACONF_SRC ))
return;
// INT clock
if( 0 == acia->framecycle )
return;
acia->cycles += cycles;
while( acia->framecycle <= acia->cycles )
{
acia->cycles -= acia->framecycle;
// TX enabled
if( acia->tx && 0 == (acia->regs[ACIA_STATUS] & ASTF_TXEMPTY) )
{
if(!acia->put_byte( acia->regs[ACIA_TXDATA] ) )
acia->regs[ACIA_STATUS] |= ASTF_OVRUNERR;
else
{
acia->regs[ACIA_STATUS] |= ASTF_TXEMPTY;
if( acia->irqtx )
{
acia->regs[ACIA_STATUS] |= ASTF_IRQ;
acia->oric->cpu.irq |= IRQF_ACIA;
}
if(acia->echo)
acia->put_byte(acia->regs[ACIA_TXDATA]);
}
}
// RX enabled
if( acia-> rx )
{
Uint8 data = 0;
if( acia->has_byte(&data) )
{
acia->regs[ACIA_STATUS] |= ASTF_RXDATA;
if( acia->irqrx )
{
acia->regs[ACIA_STATUS] |= ASTF_IRQ;
acia->oric->cpu.irq |= IRQF_ACIA;
}
}
}
}
// update modem lines status
if( acia->stat )
acia->regs[ACIA_STATUS] = acia->stat(acia->regs[ACIA_STATUS]);
else
acia->regs[ACIA_STATUS] |= (ASTF_CARRIER|ASTF_DSR);
}
void acia_write( struct acia *acia, Uint16 addr, Uint8 data )
{
#if DEBUG_ACIA
dbg_printf( "ACIA write: (%04X) %02X to %s", acia->oric->cpu.pc-1, data, writereg_names[addr&3] );
#endif
switch( addr & 3 )
{
case ACIA_RXDATA: // Data for TX
acia->regs[ACIA_TXDATA] = data & acia->bitmask;
acia->regs[ACIA_STATUS] &= ~(ASTF_TXEMPTY);
break;
case ACIA_STATUS: // Reset
acia->regs[ACIA_COMMAND] &= ACOMF_PARITY;
acia->regs[ACIA_COMMAND] |= ACOMF_IRQDIS;
acia->regs[ACIA_STATUS] &= ~(ASTF_OVRUNERR);
acia_update_params( acia );
break;
case ACIA_COMMAND:
acia->regs[ACIA_COMMAND] = data;
acia_update_params( acia );
#if DEBUG_ACIA
dbg_printf( " Parity = %s", parity_modes[data>>5] );
dbg_printf( " RX Mode = %s", (data&ACOMF_ECHO) ? "ECHO" : "NORMAL" );
dbg_printf( " TXCon = %s", txcon_modes[(data&ACOMF_TXCON)>>2] );
dbg_printf( " IRQ = %s", (data&ACOMF_IRQDIS) ? "OFF" : "ON" );
dbg_printf( " DTR = %s", (data&ACOMF_DTR) ? "ON" : "OFF" );
#endif
break;
case ACIA_CONTROL:
acia->regs[ACIA_CONTROL] = data;
acia_update_params( acia );
#if DEBUG_ACIA
dbg_printf( " Baud = %s", baud_rates[data&ACONF_BAUD] );
dbg_printf( " RXClock = %s", (data&ACONF_SRC) ? "INT" : "EXT" );
dbg_printf( " Bits = %s", bit_nums[(data&ACONF_WLEN)>>5] );
dbg_printf( " Stopbits = %d", (data&ACONF_STOPB) ? 2 : 1 );
#endif
break;
}
}
Uint8 acia_read( struct acia *acia, Uint16 addr )
{
Uint8 data = acia->regs[addr&3];
switch( addr & 3 )
{
case ACIA_RXDATA:
#if DEBUG_ACIA
dbg_printf( "ACIA read: (%04X) from %s (=%02X)", acia->oric->cpu.pc-1, readreg_names[addr&3], data );
#endif
acia->get_byte( &data );
data &= acia->bitmask;
acia->regs[ACIA_RXDATA] = data;
acia->regs[ACIA_STATUS] &= ~(ASTF_PARITYERR|ASTF_FRAMEERR|ASTF_OVRUNERR);
acia->regs[ACIA_STATUS] &= ~(ASTF_RXDATA);
break;
case ACIA_STATUS:
#if DEBUG_ACIA_STATUS
dbg_printf( "ACIA read: (%04X) from %s (=%02X)", acia->oric->cpu.pc-1, readreg_names[addr&3], data );
#endif
// clear IRQ flag
acia->regs[ACIA_STATUS] &= ~(ASTF_IRQ);
acia->oric->cpu.irq &= ~(IRQF_ACIA);
break;
default:
#if DEBUG_ACIA
dbg_printf( "ACIA read: (%04X) from %s (=%02X)", acia->oric->cpu.pc-1, readreg_names[addr&3], data );
#endif
break;
}
return data;
}
// none - fake back-end, no api function will be called
SDL_bool acia_init_none( struct acia *acia )
{
acia->done = NULL;
acia->stat = NULL;
acia->has_byte = NULL;
acia->get_byte = NULL;
acia->put_byte = NULL;
acia->oric->aciabackend = ACIA_TYPE_NONE;
return SDL_TRUE;
}