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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
/*********************************************************************
bitbngr.c
TRS style "bitbanger" serial port
*********************************************************************/
#include "emu.h"
#include "bitbngr.h"
/***************************************************************************
TYPE DEFINITIONS
***************************************************************************/
typedef struct _bitbanger_token bitbanger_token;
struct _bitbanger_token
{
emu_timer *bitbanger_output_timer;
emu_timer *bitbanger_input_timer;
int output_value;
int build_count;
int build_byte;
attotime idle_delay;
attotime current_baud;
UINT32 input_buffer_size;
UINT32 input_buffer_cursor;
int mode;
int baud;
int tune;
UINT8 input_buffer[1000];
};
/***************************************************************************
FUNCTION PROTOTYPES
***************************************************************************/
static TIMER_CALLBACK(bitbanger_output_timer);
static TIMER_CALLBACK(bitbanger_input_timer);
/***************************************************************************
INLINE FUNCTIONS
***************************************************************************/
/*-------------------------------------------------
get_token - safely gets the bitbanger data
-------------------------------------------------*/
INLINE bitbanger_token *get_token(device_t *device)
{
assert(device != NULL);
assert(device->type() == BITBANGER);
return (bitbanger_token *) downcast<legacy_device_base *>(device)->token();
}
/*-------------------------------------------------
get_config - safely gets the bitbanger config
-------------------------------------------------*/
INLINE const bitbanger_config *get_config(device_t *device)
{
assert(device != NULL);
assert(device->type() == BITBANGER);
return (const bitbanger_config *) device->static_config();
}
/***************************************************************************
IMPLEMENTATION
***************************************************************************/
/*-------------------------------------------------
native_output - outputs data to a file
-------------------------------------------------*/
static void native_output(device_t *bitbanger, UINT8 data)
{
device_image_interface *image = dynamic_cast<device_image_interface *>(bitbanger);
if (image->exists())
{
image->fwrite(&data, 1);
}
}
/*-------------------------------------------------
native_input - inputs data from a file
-------------------------------------------------*/
static UINT32 native_input(device_t *bitbanger, void *buffer, UINT32 length)
{
device_image_interface *image = dynamic_cast<device_image_interface *>(bitbanger);
if (image->exists())
{
return image->fread(buffer, length);
}
return 0;
}
/*-------------------------------------------------
bitbanger_mode_string
-------------------------------------------------*/
const char *bitbanger_mode_string(device_t *device)
{
bitbanger_token *bi = get_token(device);
static const char *const modes[] = {"Printer", "Modem"};
return(modes[bi->mode]);
}
/*-------------------------------------------------
bitbanger_inc_mode
-------------------------------------------------*/
bool bitbanger_inc_mode(device_t *device, bool test)
{
bitbanger_token *bi = get_token(device);
int adjust_mode = (int)bi->mode + 1;
if( adjust_mode >= BITBANGER_MODE_MAX )
return FALSE;
if( !test)
bi->mode = adjust_mode;
return TRUE;
}
/*-------------------------------------------------
bitbanger_dec_mode
-------------------------------------------------*/
bool bitbanger_dec_mode(device_t *device, bool test)
{
bitbanger_token *bi = get_token(device);
int adjust_mode = bi->mode - 1;
if( adjust_mode < 0 )
return FALSE;
if( !test)
bi->mode = adjust_mode;
return TRUE;
}
/*-------------------------------------------------
bitbanger_tune_string
-------------------------------------------------*/
const char *bitbanger_tune_string(device_t *device)
{
bitbanger_token *bi = get_token(device);
static const char *const tunes[] = {"-2.0%", "-1.75%", "-1.5%", "-1.25%", "-1.0%", "-0.75%", "-0.5", "-0.25%", "\xc2\xb1""0%",
"+0.25%", "+0.5%", "+0.75%", "+1.0%", "+1.25%", "+1.5%", "+1.75%", "+2.0%"};
return(tunes[bi->tune]);
}
/*-------------------------------------------------
bitbanger_tune_value
-------------------------------------------------*/
static float bitbanger_tune_value(device_t *device)
{
bitbanger_token *bi = get_token(device);
static const float tunes[] = {0.97f, 0.9825f, 0.985f, 0.9875f, 0.99f, 0.9925f, 0.995f, 0.9975f, 1.0f,
1.0025f, 1.005f, 1.0075f, 1.01f, 1.0125f, 1.015f, 1.0175f, 1.02f};
return(tunes[bi->tune]);
}
/*-------------------------------------------------
bitbanger_baud_value
-------------------------------------------------*/
static UINT32 bitbanger_baud_value(device_t *device)
{
bitbanger_token *bi = get_token(device);
static const float bauds[] = { 150.0f, 300.0f, 600.0f, 1200.0f, 2400.0f, 4800.0f, 9600.0f,
14400.0f, 28800.0f, 38400.0f, 57600.0f, 115200.0f};
float result = bitbanger_tune_value(device) * bauds[bi->baud];
return (UINT32)result;
}
/*-------------------------------------------------
bitbanger_baud_string
-------------------------------------------------*/
const char *bitbanger_baud_string(device_t *device)
{
bitbanger_token *bi = get_token(device);
static const char *const bauds[] = { "150", "300", "600", "1200", "2400", "4800",
"9600", "14400", "28800", "38400", "57600", "115200"};
return(bauds[bi->baud]);
}
/*-------------------------------------------------
bitbanger_inc_baud
-------------------------------------------------*/
bool bitbanger_inc_baud(device_t *device, bool test)
{
bitbanger_token *bi = get_token(device);
int adjust_baud = (int)bi->baud + 1;
if( adjust_baud >= BITBANGER_BAUD_MAX )
return FALSE;
if( !test)
{
bi->baud = adjust_baud;
bi->current_baud = attotime::from_hz(bitbanger_baud_value(device));
}
return TRUE;
}
/*-------------------------------------------------
bitbanger_dec_baud
-------------------------------------------------*/
bool bitbanger_dec_baud(device_t *device, bool test)
{
bitbanger_token *bi = get_token(device);
int adjust_baud = bi->baud - 1;
if( adjust_baud < 0 )
return FALSE;
if( !test)
{
bi->baud = adjust_baud;
bi->current_baud = attotime::from_hz(bitbanger_baud_value(device));
}
return TRUE;
}
/*-------------------------------------------------
bitbanger_inc_tune
-------------------------------------------------*/
bool bitbanger_inc_tune(device_t *device, bool test)
{
bitbanger_token *bi = get_token(device);
int adjust_tune = (int)bi->tune + 1;
if( adjust_tune >= BITBANGER_TUNE_MAX )
return FALSE;
if( !test)
{
bi->tune = adjust_tune;
bi->current_baud = attotime::from_hz(bitbanger_baud_value(device));
}
return TRUE;
}
/*-------------------------------------------------
bitbanger_dec_tune
-------------------------------------------------*/
bool bitbanger_dec_tune(device_t *device, bool test)
{
bitbanger_token *bi = get_token(device);
int adjust_tune = bi->tune - 1;
if( adjust_tune < 0 )
return FALSE;
if( !test)
{
bi->tune = adjust_tune;
bi->current_baud = attotime::from_hz(bitbanger_baud_value(device));
}
return TRUE;
}
/*-------------------------------------------------
bitbanger_bytes_to_bits_81N
-------------------------------------------------*/
static void bitbanger_bytes_to_bits_81N(device_t *img)
{
bitbanger_token *bi = get_token(img);
UINT8 byte_buffer[100];
UINT32 byte_buffer_size, bit_buffer_size;
int i, j;
static const UINT8 bitmask[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
bit_buffer_size = 0;
byte_buffer_size = native_input(img, byte_buffer, sizeof(byte_buffer));
/* Translate byte buffer into bit buffer using: 1 start bit, 8 data bits, 1 stop bit, no parity */
for( i=0; i<byte_buffer_size; i++ )
{
bi->input_buffer[bit_buffer_size++] = 0;
for( j=0; j<8; j++ )
{
if( byte_buffer[i] & bitmask[j] )
bi->input_buffer[bit_buffer_size++] = 1;
else
bi->input_buffer[bit_buffer_size++] = 0;
}
bi->input_buffer[bit_buffer_size++] = 1;
}
bi->input_buffer_size = bit_buffer_size;
bi->input_buffer_cursor = 0;
}
/*-------------------------------------------------
DEVICE_START(bitbanger)
-------------------------------------------------*/
static DEVICE_START(bitbanger)
{
bitbanger_token *bi;
const bitbanger_config *config = get_config(device);
bi = get_token(device);
/* output config */
bi->build_count = 0;
bi->bitbanger_output_timer = device->machine().scheduler().timer_alloc(FUNC(bitbanger_output_timer), (void *) device);
/* input config */
bi->bitbanger_input_timer = device->machine().scheduler().timer_alloc(FUNC(bitbanger_input_timer), (void *) device );
bi->idle_delay = attotime::from_seconds(1);
bi->input_buffer_size = 0;
bi->input_buffer_cursor = 0;
bi->mode = config->default_mode;
bi->baud = config->default_baud;
bi->tune = config->default_tune;
bi->current_baud = attotime::from_hz(bitbanger_baud_value(device));
/* test callback */
if(!config->input_callback)
fatalerror("Misconfigured bitbanger device: input_callback cannot be NULL\n");
}
/*-------------------------------------------------
TIMER_CALLBACK(bitbanger_output_timer)
-------------------------------------------------*/
static TIMER_CALLBACK(bitbanger_output_timer)
{
device_t *device = (device_t *) ptr;
bitbanger_token *bi = get_token(device);
/* this ia harded coded for 8-1-N */
if( bi->output_value )
bi->build_byte |= 0x200;
bi->build_byte >>= 1;
bi->build_count--;
if(bi->build_count == 0)
{
if( bi->output_value == 1 )
native_output( device, bi->build_byte );
else
logerror("Bitbanger: Output framing error.\n" );
bi->bitbanger_output_timer->reset();
}
}
/*-------------------------------------------------
TIMER_CALLBACK(bitbanger_input_timer)
-------------------------------------------------*/
static TIMER_CALLBACK(bitbanger_input_timer)
{
device_t *device = (device_t *) ptr;
bitbanger_token *bi = get_token(device);
const bitbanger_config *config = get_config(device);
if(bi->input_buffer_cursor == bi->input_buffer_size)
{
/* get more data */
bitbanger_bytes_to_bits_81N(device);
if(bi->input_buffer_size == 0)
{
/* no more data, wait and try again */
bi->idle_delay = min(bi->idle_delay + attotime::from_msec(100), attotime::from_seconds(1));
bi->bitbanger_input_timer->adjust(bi->idle_delay);
if( bi->mode == BITBANGER_MODEM )
config->input_callback(machine, 1);
else
config->input_callback(machine, 0);
return;
}
else
{
bi->idle_delay = bi->current_baud;
bi->bitbanger_input_timer->adjust(bi->idle_delay, 0, bi->idle_delay);
}
}
/* send bit to driver */
config->input_callback(machine, bi->input_buffer[(bi->input_buffer_cursor)++]);
}
/*-------------------------------------------------
bitbanger_output - outputs data to a bitbanger
port
-------------------------------------------------*/
void bitbanger_output(device_t *device, int value)
{
bitbanger_token *bi = get_token(device);
attotime one_point_five_baud;
if( bi->build_count == 0 && bi->output_value == 1 && value == 0 )
{
/* we found our start bit */
/* eight bits of data, plus one of stop */
bi->build_count = 9;
bi->build_byte = 0;
one_point_five_baud = bi->current_baud + bi->current_baud / 2;
bi->bitbanger_output_timer->adjust(one_point_five_baud, 0, bi->current_baud);
}
//fprintf(stderr,"%s, %d\n", device->machine().time().as_string(9), value);
bi->output_value = value;
}
/*-------------------------------------------------
DEVICE_IMAGE_LOAD( bitbanger )
-------------------------------------------------*/
static DEVICE_IMAGE_LOAD( bitbanger )
{
device_t *device = &image.device();
bitbanger_token *bi;
bi = get_token(device);
bi->bitbanger_input_timer->enable(true);
bi->bitbanger_input_timer->adjust(attotime::zero, 0, attotime::from_seconds(1));
/* we don't need to do anything special */
return IMAGE_INIT_PASS;
}
/*-------------------------------------------------
DEVICE_IMAGE_UNLOAD( bitbanger )
-------------------------------------------------*/
static DEVICE_IMAGE_UNLOAD( bitbanger )
{
device_t *device = &image.device();
bitbanger_token *bi;
bi = get_token(device);
bi->bitbanger_input_timer->enable(false);
}
/*-------------------------------------------------
DEVICE_GET_INFO(bitbanger) - device getinfo
function
-------------------------------------------------*/
DEVICE_GET_INFO(bitbanger)
{
switch(state)
{
/* --- the following bits of info are returned as 64-bit signed integers --- */
case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(bitbanger_token); break;
case DEVINFO_INT_INLINE_CONFIG_BYTES: info->i = 0; break;
case DEVINFO_INT_IMAGE_TYPE: info->i = IO_PRINTER; break;
case DEVINFO_INT_IMAGE_READABLE: info->i = 1; break;
case DEVINFO_INT_IMAGE_WRITEABLE: info->i = 1; break;
case DEVINFO_INT_IMAGE_CREATABLE: info->i = 1; break;
/* --- the following bits of info are returned as pointers to data or functions --- */
case DEVINFO_FCT_START: info->start = DEVICE_START_NAME(bitbanger); break;
case DEVINFO_FCT_IMAGE_LOAD: info->f = (genf *) DEVICE_IMAGE_LOAD_NAME(bitbanger); break;
case DEVINFO_FCT_IMAGE_UNLOAD: info->f = (genf *) DEVICE_IMAGE_UNLOAD_NAME(bitbanger); break;
/* --- the following bits of info are returned as NULL-terminated strings --- */
case DEVINFO_STR_NAME: strcpy(info->s, "Bitbanger"); break;
case DEVINFO_STR_FAMILY: strcpy(info->s, "Bitbanger"); break;
case DEVINFO_STR_SOURCE_FILE: strcpy(info->s, __FILE__); break;
case DEVINFO_STR_IMAGE_FILE_EXTENSIONS: strcpy(info->s, "prn"); break;
}
}
DEFINE_LEGACY_IMAGE_DEVICE(BITBANGER, bitbanger);
|