src/mess/devices/bitbngr.c
| r9429 | r9430 | |
| 6 | 6 | |
| 7 | 7 | *********************************************************************/ |
| 8 | 8 | |
| 9 | | #include <math.h> |
| 10 | | |
| 11 | 9 | #include "emu.h" |
| 12 | 10 | #include "bitbngr.h" |
| 13 | | #include "printer.h" |
| 14 | 11 | |
| 15 | 12 | |
| 16 | 13 | |
| r9429 | r9430 | |
| 21 | 18 | typedef struct _bitbanger_token bitbanger_token; |
| 22 | 19 | struct _bitbanger_token |
| 23 | 20 | { |
| 24 | | double last_pulse_time; |
| 25 | | double *pulses; |
| 26 | | int *factored_pulses; |
| 27 | | int recorded_pulses; |
| 28 | | int value; |
| 29 | | emu_timer *timeout_timer; |
| 30 | | int over_threshhold; |
| 21 | emu_timer *bitbanger_output_timer; |
| 22 | emu_timer *bitbanger_input_timer; |
| 23 | int output_value; |
| 24 | int build_count; |
| 25 | int build_byte; |
| 26 | attotime idle_delay; |
| 27 | attotime current_baud; |
| 28 | UINT8 *input_buffer; |
| 29 | UINT32 input_buffer_size; |
| 30 | UINT32 input_buffer_cursor; |
| 31 | int mode; |
| 32 | int baud; |
| 33 | int tune; |
| 31 | 34 | }; |
| 32 | 35 | |
| 33 | 36 | |
| r9429 | r9430 | |
| 36 | 39 | FUNCTION PROTOTYPES |
| 37 | 40 | ***************************************************************************/ |
| 38 | 41 | |
| 39 | | static TIMER_CALLBACK(bitbanger_overthreshhold); |
| 42 | static TIMER_CALLBACK(bitbanger_output_timer); |
| 43 | static TIMER_CALLBACK(bitbanger_input_timer); |
| 40 | 44 | |
| 41 | 45 | |
| 42 | 46 | |
| r9429 | r9430 | |
| 44 | 48 | INLINE FUNCTIONS |
| 45 | 49 | ***************************************************************************/ |
| 46 | 50 | |
| 51 | |
| 47 | 52 | /*------------------------------------------------- |
| 48 | 53 | get_token - safely gets the bitbanger data |
| 49 | 54 | -------------------------------------------------*/ |
| r9429 | r9430 | |
| 56 | 61 | } |
| 57 | 62 | |
| 58 | 63 | |
| 59 | | |
| 60 | 64 | /*------------------------------------------------- |
| 61 | 65 | get_config - safely gets the bitbanger config |
| 62 | 66 | -------------------------------------------------*/ |
| r9429 | r9430 | |
| 74 | 78 | IMPLEMENTATION |
| 75 | 79 | ***************************************************************************/ |
| 76 | 80 | |
| 81 | |
| 77 | 82 | /*------------------------------------------------- |
| 78 | | DEVICE_START(bitbanger) |
| 83 | native_output - outputs data to a file |
| 79 | 84 | -------------------------------------------------*/ |
| 80 | 85 | |
| 81 | | static DEVICE_START(bitbanger) |
| 86 | void native_output(running_device *bitbanger, UINT8 data) |
| 82 | 87 | { |
| 83 | | bitbanger_token *bi; |
| 84 | | const bitbanger_config *config = get_config(device); |
| 88 | device_image_interface *image = dynamic_cast<device_image_interface *>(bitbanger); |
| 89 | if (image->exists()) |
| 90 | { |
| 91 | image->fwrite(&data, 1); |
| 92 | } |
| 93 | } |
| 85 | 94 | |
| 86 | | bi = get_token(device); |
| 87 | | bi->pulses = auto_alloc_array(device->machine, double, config->maximum_pulses); |
| 88 | | bi->factored_pulses = auto_alloc_array(device->machine, int, config->maximum_pulses); |
| 89 | | bi->last_pulse_time = 0.0; |
| 90 | | bi->recorded_pulses = 0; |
| 91 | | bi->value = config->initial_value; |
| 92 | | bi->timeout_timer = timer_alloc(device->machine, bitbanger_overthreshhold, (void *) device); |
| 93 | | bi->over_threshhold = 1; |
| 95 | |
| 96 | /*------------------------------------------------- |
| 97 | native_input - inputs data from a file |
| 98 | -------------------------------------------------*/ |
| 99 | |
| 100 | UINT32 native_input(running_device *bitbanger, void *buffer, UINT32 length) |
| 101 | { |
| 102 | device_image_interface *image = dynamic_cast<device_image_interface *>(bitbanger); |
| 103 | if (image->exists()) |
| 104 | { |
| 105 | return image->fread(buffer, length); |
| 106 | } |
| 107 | |
| 108 | return 0; |
| 94 | 109 | } |
| 95 | 110 | |
| 96 | 111 | |
| 112 | /*------------------------------------------------- |
| 113 | bitbanger_mode_string |
| 114 | -------------------------------------------------*/ |
| 115 | const char *bitbanger_mode_string(running_device *device) |
| 116 | { |
| 117 | bitbanger_token *bi = get_token(device); |
| 118 | static const char *modes[] = {"Printer", "Modem"}; |
| 97 | 119 | |
| 120 | return(modes[bi->mode]); |
| 121 | } |
| 122 | |
| 123 | |
| 98 | 124 | /*------------------------------------------------- |
| 99 | | bitbanger_analyze |
| 125 | bitbanger_inc_mode |
| 100 | 126 | -------------------------------------------------*/ |
| 127 | bool bitbanger_inc_mode(running_device *device, bool test) |
| 128 | { |
| 129 | bitbanger_token *bi = get_token(device); |
| 130 | int adjust_mode = (int)bi->mode + 1; |
| 101 | 131 | |
| 102 | | static void bitbanger_analyze(running_device *device) |
| 132 | if( adjust_mode >= BITBANGER_MODE_MAX ) |
| 133 | return FALSE; |
| 134 | |
| 135 | if( !test) |
| 136 | bi->mode = adjust_mode; |
| 137 | |
| 138 | return TRUE; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /*------------------------------------------------- |
| 143 | bitbanger_dec_mode |
| 144 | -------------------------------------------------*/ |
| 145 | bool bitbanger_dec_mode(running_device *device, bool test) |
| 103 | 146 | { |
| 104 | | int i, factor, total_duration; |
| 105 | | double smallest_pulse; |
| 106 | | double d; |
| 107 | 147 | bitbanger_token *bi = get_token(device); |
| 108 | | const bitbanger_config *config = get_config(device); |
| 148 | int adjust_mode = bi->mode - 1; |
| 109 | 149 | |
| 110 | | /* compute the smallest pulse */ |
| 111 | | smallest_pulse = config->pulse_threshhold; |
| 112 | | for (i = 0; i < bi->recorded_pulses; i++) |
| 113 | | if (smallest_pulse > bi->pulses[i]) |
| 114 | | smallest_pulse = bi->pulses[i]; |
| 150 | if( adjust_mode < 0 ) |
| 151 | return FALSE; |
| 115 | 152 | |
| 116 | | /* figure out what factor the smallest pulse was */ |
| 117 | | factor = 0; |
| 118 | | do |
| 119 | | { |
| 120 | | factor++; |
| 121 | | total_duration = 0; |
| 153 | if( !test) |
| 154 | bi->mode = adjust_mode; |
| 122 | 155 | |
| 123 | | for (i = 0; i < bi->recorded_pulses; i++) |
| 124 | | { |
| 125 | | d = bi->pulses[i] / smallest_pulse * factor + config->pulse_tolerance; |
| 126 | | if ((i < (bi->recorded_pulses-1)) && (d - floor(d)) >= (config->pulse_tolerance * 2)) |
| 127 | | { |
| 128 | | i = -1; |
| 129 | | break; |
| 130 | | } |
| 131 | | bi->factored_pulses[i] = (int) d; |
| 132 | | total_duration += (int) d; |
| 133 | | } |
| 134 | | } |
| 135 | | while((i == -1) && (factor < config->maximum_pulses)); |
| 136 | | if (i == -1) |
| 137 | | return; |
| 156 | return TRUE; |
| 157 | } |
| 138 | 158 | |
| 139 | | /* filter the output */ |
| 140 | | if (config->filter(device, bi->factored_pulses, bi->recorded_pulses, total_duration)) |
| 141 | | bi->recorded_pulses = 0; |
| 159 | |
| 160 | /*------------------------------------------------- |
| 161 | bitbanger_tune_string |
| 162 | -------------------------------------------------*/ |
| 163 | const char *bitbanger_tune_string(running_device *device) |
| 164 | { |
| 165 | bitbanger_token *bi = get_token(device); |
| 166 | static const char *tunes[] = {"-2.0%", "-1.75%", "-1.5%", "-1.25%", "-1.0%", "-0.75%", "-0.5", "-0.25%", "\xc2\xb1""0%", |
| 167 | "+0.25%", "+0.5%", "+0.75%", "+1.0%", "+1.25%", "+1.5%", "+1.75%", "+2.0%"}; |
| 168 | |
| 169 | return(tunes[bi->tune]); |
| 142 | 170 | } |
| 143 | 171 | |
| 144 | 172 | |
| 173 | /*------------------------------------------------- |
| 174 | bitbanger_tune_value |
| 175 | -------------------------------------------------*/ |
| 176 | float bitbanger_tune_value(running_device *device) |
| 177 | { |
| 178 | bitbanger_token *bi = get_token(device); |
| 179 | float tunes[] = {0.97, 0.9825, 0.985, 0.9875, 0.99, 0.9925, 0.995, 0.9975, 1.0, |
| 180 | 1.0025, 1.005, 1.0075, 1.01, 1.0125, 1.015, 1.0175, 1.02}; |
| 145 | 181 | |
| 182 | return(tunes[bi->tune]); |
| 183 | } |
| 184 | |
| 185 | |
| 146 | 186 | /*------------------------------------------------- |
| 147 | | bitbanger_addpulse |
| 187 | bitbanger_baud_value |
| 148 | 188 | -------------------------------------------------*/ |
| 149 | 189 | |
| 150 | | static void bitbanger_addpulse(running_device *device, double pulse_width) |
| 190 | UINT32 bitbanger_baud_value(running_device *device) |
| 151 | 191 | { |
| 152 | 192 | bitbanger_token *bi = get_token(device); |
| 193 | float bauds[] = { 150.0, 300.0, 600.0, 1200.0, 2400.0, 4800.0, 9600.0, |
| 194 | 14400.0, 28800.0, 38400.0, 57600.0, 115200.0}; |
| 195 | float result = bitbanger_tune_value(device) * bauds[bi->baud]; |
| 196 | return (UINT32)result; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | /*------------------------------------------------- |
| 201 | bitbanger_baud_string |
| 202 | -------------------------------------------------*/ |
| 203 | |
| 204 | const char *bitbanger_baud_string(running_device *device) |
| 205 | { |
| 206 | bitbanger_token *bi = get_token(device); |
| 207 | static const char *bauds[] = { "150", "300", "600", "1200", "2400", "4800", |
| 208 | "9600", "14400", "28800", "38400", "57600", "115200"}; |
| 209 | |
| 210 | return(bauds[bi->baud]); |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /*------------------------------------------------- |
| 215 | bitbanger_inc_baud |
| 216 | -------------------------------------------------*/ |
| 217 | bool bitbanger_inc_baud(running_device *device, bool test) |
| 218 | { |
| 219 | bitbanger_token *bi = get_token(device); |
| 220 | int adjust_baud = (int)bi->baud + 1; |
| 221 | |
| 222 | if( adjust_baud >= BITBANGER_BAUD_MAX ) |
| 223 | return FALSE; |
| 224 | |
| 225 | if( !test) |
| 226 | { |
| 227 | bi->baud = adjust_baud; |
| 228 | bi->current_baud = ATTOTIME_IN_HZ(bitbanger_baud_value(device)); |
| 229 | } |
| 230 | |
| 231 | return TRUE; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /*------------------------------------------------- |
| 236 | bitbanger_dec_baud |
| 237 | -------------------------------------------------*/ |
| 238 | bool bitbanger_dec_baud(running_device *device, bool test) |
| 239 | { |
| 240 | bitbanger_token *bi = get_token(device); |
| 241 | int adjust_baud = bi->baud - 1; |
| 242 | |
| 243 | if( adjust_baud < 0 ) |
| 244 | return FALSE; |
| 245 | |
| 246 | if( !test) |
| 247 | { |
| 248 | bi->baud = adjust_baud; |
| 249 | bi->current_baud = ATTOTIME_IN_HZ(bitbanger_baud_value(device)); |
| 250 | } |
| 251 | |
| 252 | return TRUE; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | /*------------------------------------------------- |
| 257 | bitbanger_inc_tune |
| 258 | -------------------------------------------------*/ |
| 259 | bool bitbanger_inc_tune(running_device *device, bool test) |
| 260 | { |
| 261 | bitbanger_token *bi = get_token(device); |
| 262 | int adjust_tune = (int)bi->tune + 1; |
| 263 | |
| 264 | if( adjust_tune >= BITBANGER_TUNE_MAX ) |
| 265 | return FALSE; |
| 266 | |
| 267 | if( !test) |
| 268 | { |
| 269 | bi->tune = adjust_tune; |
| 270 | bi->current_baud = ATTOTIME_IN_HZ(bitbanger_baud_value(device)); |
| 271 | } |
| 272 | |
| 273 | return TRUE; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | /*------------------------------------------------- |
| 278 | bitbanger_dec_tune |
| 279 | -------------------------------------------------*/ |
| 280 | bool bitbanger_dec_tune(running_device *device, bool test) |
| 281 | { |
| 282 | bitbanger_token *bi = get_token(device); |
| 283 | int adjust_tune = bi->tune - 1; |
| 284 | |
| 285 | if( adjust_tune < 0 ) |
| 286 | return FALSE; |
| 287 | |
| 288 | if( !test) |
| 289 | { |
| 290 | bi->tune = adjust_tune; |
| 291 | bi->current_baud = ATTOTIME_IN_HZ(bitbanger_baud_value(device)); |
| 292 | } |
| 293 | |
| 294 | return TRUE; |
| 295 | } |
| 296 | |
| 297 | |
| 298 | /*------------------------------------------------- |
| 299 | bitbanger_bytes_to_bits_81N |
| 300 | -------------------------------------------------*/ |
| 301 | static void bitbanger_bytes_to_bits_81N(running_device *img, UINT8 **buffer, UINT32 *size) |
| 302 | { |
| 303 | UINT8 byte_buffer[100]; |
| 304 | static UINT8 bit_buffer[1000]; |
| 305 | UINT32 byte_buffer_size, bit_buffer_size; |
| 306 | int i, j; |
| 307 | const UINT8 bitmask[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; |
| 308 | |
| 309 | bit_buffer_size = 0; |
| 310 | byte_buffer_size = 100; |
| 311 | byte_buffer_size = native_input(img, &byte_buffer, byte_buffer_size); |
| 312 | |
| 313 | /* Translate byte buffer into bit buffer using: 1 start bit, 8 data bits, 1 stop bit, no parity */ |
| 314 | |
| 315 | for( i=0; i<byte_buffer_size; i++ ) |
| 316 | { |
| 317 | bit_buffer[bit_buffer_size++] = 0; |
| 318 | |
| 319 | for( j=0; j<8; j++ ) |
| 320 | { |
| 321 | if( byte_buffer[i] & bitmask[j] ) |
| 322 | bit_buffer[bit_buffer_size++] = 1; |
| 323 | else |
| 324 | bit_buffer[bit_buffer_size++] = 0; |
| 325 | } |
| 326 | |
| 327 | bit_buffer[bit_buffer_size++] = 1; |
| 328 | } |
| 329 | |
| 330 | *buffer = bit_buffer; |
| 331 | *size = bit_buffer_size; |
| 332 | } |
| 333 | |
| 334 | |
| 335 | /*------------------------------------------------- |
| 336 | DEVICE_START(bitbanger) |
| 337 | -------------------------------------------------*/ |
| 338 | |
| 339 | static DEVICE_START(bitbanger) |
| 340 | { |
| 341 | bitbanger_token *bi; |
| 153 | 342 | const bitbanger_config *config = get_config(device); |
| 154 | 343 | |
| 155 | | /* exceeded total countable pulses? */ |
| 156 | | if (bi->recorded_pulses == config->maximum_pulses) |
| 157 | | memmove(bi->pulses, bi->pulses + 1, (--bi->recorded_pulses) * sizeof(double)); |
| 344 | bi = get_token(device); |
| 158 | 345 | |
| 159 | | /* record the pulse */ |
| 160 | | bi->pulses[bi->recorded_pulses++] = pulse_width; |
| 346 | /* output config */ |
| 347 | bi->build_count = 0; |
| 348 | bi->bitbanger_output_timer = timer_alloc(device->machine, bitbanger_output_timer, (void *) device); |
| 161 | 349 | |
| 162 | | /* analyze, if necessary */ |
| 163 | | if (bi->recorded_pulses >= config->minimum_pulses) |
| 164 | | bitbanger_analyze(device); |
| 350 | /* input config */ |
| 351 | bi->bitbanger_input_timer = timer_alloc(device->machine, bitbanger_input_timer, (void *) device ); |
| 352 | bi->idle_delay = ATTOTIME_IN_SEC(1); |
| 353 | bi->input_buffer = 0; |
| 354 | bi->input_buffer_size = 0; |
| 355 | bi->input_buffer_cursor = 0; |
| 356 | |
| 357 | bi->mode = config->default_mode; |
| 358 | bi->baud = config->default_baud; |
| 359 | bi->tune = config->default_tune; |
| 360 | bi->current_baud = ATTOTIME_IN_HZ(bitbanger_baud_value(device)); |
| 361 | |
| 362 | /* test callback */ |
| 363 | if(!config->input_callback) |
| 364 | fatalerror("Misconfigured bitbanger device: input_callback cannot be NULL\n"); |
| 165 | 365 | } |
| 166 | 366 | |
| 167 | 367 | |
| 368 | /*------------------------------------------------- |
| 369 | TIMER_CALLBACK(bitbanger_output_timer) |
| 370 | -------------------------------------------------*/ |
| 168 | 371 | |
| 372 | static TIMER_CALLBACK(bitbanger_output_timer) |
| 373 | { |
| 374 | running_device *device = (running_device *) ptr; |
| 375 | bitbanger_token *bi = get_token(device); |
| 376 | |
| 377 | /* this ia harded coded for 8-1-N */ |
| 378 | if( bi->output_value ) |
| 379 | bi->build_byte |= 0x200; |
| 380 | |
| 381 | bi->build_byte >>= 1; |
| 382 | bi->build_count--; |
| 383 | |
| 384 | if(bi->build_count == 0) |
| 385 | { |
| 386 | if( bi->output_value == 1 ) |
| 387 | native_output( device, bi->build_byte ); |
| 388 | else |
| 389 | logerror("Bitbanger: Output framing error.\n" ); |
| 390 | |
| 391 | timer_reset(bi->bitbanger_output_timer, attotime_never); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | |
| 169 | 396 | /*------------------------------------------------- |
| 170 | | TIMER_CALLBACK(bitbanger_overthreshhold) |
| 397 | TIMER_CALLBACK(bitbanger_input_timer) |
| 171 | 398 | -------------------------------------------------*/ |
| 172 | 399 | |
| 173 | | static TIMER_CALLBACK(bitbanger_overthreshhold) |
| 400 | static TIMER_CALLBACK(bitbanger_input_timer) |
| 174 | 401 | { |
| 175 | 402 | running_device *device = (running_device *) ptr; |
| 176 | 403 | bitbanger_token *bi = get_token(device); |
| 404 | const bitbanger_config *config = get_config(device); |
| 177 | 405 | |
| 178 | | bitbanger_addpulse(device, attotime_to_double(timer_get_time(machine)) - bi->last_pulse_time); |
| 179 | | bi->over_threshhold = 1; |
| 180 | | bi->recorded_pulses = 0; |
| 406 | if(bi->input_buffer_cursor == bi->input_buffer_size) |
| 407 | { |
| 408 | /* get more data */ |
| 409 | bitbanger_bytes_to_bits_81N(device, &(bi->input_buffer), &(bi->input_buffer_size)); |
| 410 | bi->input_buffer_cursor = 0; |
| 411 | |
| 412 | if(bi->input_buffer_size == 0) |
| 413 | { |
| 414 | /* no more data, wait and try again */ |
| 415 | bi->idle_delay = attotime_min(attotime_add(bi->idle_delay, ATTOTIME_IN_MSEC(100)), ATTOTIME_IN_SEC(1)); |
| 416 | timer_adjust_oneshot(bi->bitbanger_input_timer, bi->idle_delay, 0); |
| 417 | |
| 418 | |
| 419 | if( bi->mode == BITBANGER_MODEM ) |
| 420 | config->input_callback(machine, 1); |
| 421 | else |
| 422 | config->input_callback(machine, 0); |
| 423 | |
| 424 | return; |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | bi->idle_delay = bi->current_baud; |
| 429 | timer_adjust_periodic(bi->bitbanger_input_timer, bi->idle_delay, 0, bi->idle_delay); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /* send bit to driver */ |
| 434 | config->input_callback(machine, bi->input_buffer[(bi->input_buffer_cursor)++]); |
| 181 | 435 | } |
| 182 | 436 | |
| 183 | 437 | |
| 184 | | |
| 185 | 438 | /*------------------------------------------------- |
| 186 | 439 | bitbanger_output - outputs data to a bitbanger |
| 187 | 440 | port |
| r9429 | r9430 | |
| 190 | 443 | void bitbanger_output(running_device *device, int value) |
| 191 | 444 | { |
| 192 | 445 | bitbanger_token *bi = get_token(device); |
| 193 | | const bitbanger_config *config = get_config(device); |
| 446 | attotime one_point_five_baud; |
| 194 | 447 | |
| 195 | | double current_time; |
| 196 | | double pulse_width; |
| 448 | if( bi->build_count == 0 && bi->output_value == 1 && value == 0 ) |
| 449 | { |
| 450 | /* we found our start bit */ |
| 451 | /* eight bits of data, plus one of stop */ |
| 452 | bi->build_count = 9; |
| 453 | bi->build_byte = 0; |
| 197 | 454 | |
| 198 | | /* normalize input */ |
| 199 | | value = value ? 1 : 0; |
| 455 | one_point_five_baud = attotime_add(bi->current_baud, attotime_div(bi->current_baud,2)); |
| 456 | timer_adjust_periodic(bi->bitbanger_output_timer, one_point_five_baud, 0, bi->current_baud); |
| 457 | } |
| 200 | 458 | |
| 201 | | /* only meaningful if we change */ |
| 202 | | if (bi->value != value) |
| 203 | | { |
| 204 | | current_time = attotime_to_double(timer_get_time(device->machine)); |
| 205 | | pulse_width = current_time - bi->last_pulse_time; |
| 459 | //fprintf(stderr,"%s, %d\n", attotime_string(timer_get_time(device->machine),9), value); |
| 460 | bi->output_value = value; |
| 461 | } |
| 206 | 462 | |
| 207 | | assert(pulse_width >= 0); |
| 208 | 463 | |
| 209 | | if (!bi->over_threshhold && ((bi->recorded_pulses > 0) || (bi->value == config->initial_value))) |
| 210 | | bitbanger_addpulse(device, pulse_width); |
| 464 | /*------------------------------------------------- |
| 465 | DEVICE_IMAGE_LOAD( bitbanger ) |
| 466 | -------------------------------------------------*/ |
| 211 | 467 | |
| 212 | | /* update state */ |
| 213 | | bi->value = value; |
| 214 | | bi->last_pulse_time = current_time; |
| 215 | | bi->over_threshhold = 0; |
| 468 | static DEVICE_IMAGE_LOAD( bitbanger ) |
| 469 | { |
| 470 | device_t *device = &image.device(); |
| 471 | bitbanger_token *bi; |
| 472 | bi = get_token(device); |
| 216 | 473 | |
| 217 | | /* update timeout timer */ |
| 218 | | timer_reset(bi->timeout_timer, double_to_attotime(config->pulse_threshhold)); |
| 219 | | } |
| 474 | timer_enable(bi->bitbanger_input_timer, TRUE); |
| 475 | timer_adjust_periodic(bi->bitbanger_input_timer, attotime_zero, 0, ATTOTIME_IN_SEC(1)); |
| 476 | |
| 477 | /* we don't need to do anything special */ |
| 478 | return IMAGE_INIT_PASS; |
| 220 | 479 | } |
| 221 | 480 | |
| 222 | 481 | |
| 482 | /*------------------------------------------------- |
| 483 | DEVICE_IMAGE_UNLOAD( bitbanger ) |
| 484 | -------------------------------------------------*/ |
| 223 | 485 | |
| 486 | static DEVICE_IMAGE_UNLOAD( bitbanger ) |
| 487 | { |
| 488 | device_t *device = &image.device(); |
| 489 | bitbanger_token *bi; |
| 490 | bi = get_token(device); |
| 491 | |
| 492 | timer_enable(bi->bitbanger_input_timer, FALSE); |
| 493 | } |
| 494 | |
| 495 | |
| 224 | 496 | /*------------------------------------------------- |
| 225 | 497 | DEVICE_GET_INFO(bitbanger) - device getinfo |
| 226 | 498 | function |
| r9429 | r9430 | |
| 234 | 506 | case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(bitbanger_token); break; |
| 235 | 507 | case DEVINFO_INT_INLINE_CONFIG_BYTES: info->i = 0; break; |
| 236 | 508 | case DEVINFO_INT_IMAGE_TYPE: info->i = IO_PRINTER; break; |
| 237 | | case DEVINFO_INT_IMAGE_READABLE: info->i = 0; break; |
| 509 | case DEVINFO_INT_IMAGE_READABLE: info->i = 1; break; |
| 238 | 510 | case DEVINFO_INT_IMAGE_WRITEABLE: info->i = 1; break; |
| 239 | 511 | case DEVINFO_INT_IMAGE_CREATABLE: info->i = 1; break; |
| 240 | 512 | |
| 241 | 513 | /* --- the following bits of info are returned as pointers to data or functions --- */ |
| 242 | 514 | case DEVINFO_FCT_START: info->start = DEVICE_START_NAME(bitbanger); break; |
| 515 | case DEVINFO_FCT_IMAGE_LOAD: info->f = (genf *) DEVICE_IMAGE_LOAD_NAME(bitbanger); break; |
| 516 | case DEVINFO_FCT_IMAGE_UNLOAD: info->f = (genf *) DEVICE_IMAGE_UNLOAD_NAME(bitbanger); break; |
| 243 | 517 | |
| 244 | 518 | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 245 | 519 | case DEVINFO_STR_NAME: strcpy(info->s, "Bitbanger"); break; |
src/mess/uimess.c
| r9429 | r9430 | |
| 12 | 12 | #include "uiinput.h" |
| 13 | 13 | #include "input.h" |
| 14 | 14 | #include "devices/cassette.h" |
| 15 | #include "devices/bitbngr.h" |
| 15 | 16 | |
| 16 | 17 | |
| 17 | 18 | /*************************************************************************** |
| r9429 | r9430 | |
| 26 | 27 | #define TAPECMD_SLIDER ((void *) 0x0006) |
| 27 | 28 | #define TAPECMD_SELECT ((void *) 0x0007) |
| 28 | 29 | |
| 30 | #define BITBANGERCMD_SELECT ((void *) 0x0000) |
| 31 | #define BITBANGERCMD_MODE ((void *) 0x0001) |
| 32 | #define BITBANGERCMD_BAUD ((void *) 0x0002) |
| 33 | #define BITBANGERCMD_TUNE ((void *) 0x0003) |
| 29 | 34 | |
| 30 | 35 | |
| 31 | 36 | typedef struct _tape_control_menu_state tape_control_menu_state; |
| r9429 | r9430 | |
| 37 | 42 | |
| 38 | 43 | |
| 39 | 44 | |
| 45 | typedef struct _bitbanger_control_menu_state bitbanger_control_menu_state; |
| 46 | struct _bitbanger_control_menu_state |
| 47 | { |
| 48 | int index; |
| 49 | device_image_interface *device; |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | |
| 40 | 54 | /*************************************************************************** |
| 41 | 55 | IMPLEMENTATION |
| 42 | 56 | ***************************************************************************/ |
| r9429 | r9430 | |
| 50 | 64 | /* add tape control menu */ |
| 51 | 65 | if (machine->m_devicelist.first(CASSETTE)) |
| 52 | 66 | ui_menu_item_append(menu, "Tape Control", NULL, 0, (void*)ui_mess_menu_tape_control); |
| 67 | |
| 68 | /* add bitbanger control menu */ |
| 69 | if (machine->m_devicelist.first(BITBANGER)) |
| 70 | ui_menu_item_append(menu, "Bitbanger Control", NULL, 0, (void*)ui_mess_menu_bitbanger_control); |
| 53 | 71 | } |
| 54 | 72 | |
| 55 | 73 | |
| r9429 | r9430 | |
| 61 | 79 | INLINE int cassette_count( running_machine *machine ) |
| 62 | 80 | { |
| 63 | 81 | int count = 0; |
| 64 | | running_device *device = machine->m_devicelist.first(CASSETTE ); |
| 82 | running_device *device = machine->m_devicelist.first(CASSETTE); |
| 65 | 83 | |
| 66 | 84 | while ( device ) |
| 67 | 85 | { |
| r9429 | r9430 | |
| 72 | 90 | } |
| 73 | 91 | |
| 74 | 92 | /*------------------------------------------------- |
| 93 | bitbanger_count - returns the number of bitbanger |
| 94 | devices in the machine |
| 95 | -------------------------------------------------*/ |
| 96 | |
| 97 | INLINE int bitbanger_count( running_machine *machine ) |
| 98 | { |
| 99 | int count = 0; |
| 100 | running_device *device = machine->m_devicelist.first(BITBANGER); |
| 101 | |
| 102 | while ( device ) |
| 103 | { |
| 104 | count++; |
| 105 | device = device->typenext(); |
| 106 | } |
| 107 | return count; |
| 108 | } |
| 109 | |
| 110 | /*------------------------------------------------- |
| 75 | 111 | tapecontrol_gettime - returns a textual |
| 76 | 112 | representation of the time |
| 77 | 113 | -------------------------------------------------*/ |
| r9429 | r9430 | |
| 176 | 212 | |
| 177 | 213 | |
| 178 | 214 | /*------------------------------------------------- |
| 215 | menu_bitbanger_control_populate - populates the |
| 216 | main bitbanger control menu |
| 217 | -------------------------------------------------*/ |
| 218 | |
| 219 | static void menu_bitbanger_control_populate(running_machine *machine, ui_menu *menu, bitbanger_control_menu_state *menustate) |
| 220 | { |
| 221 | int count = bitbanger_count(machine); |
| 222 | UINT32 flags = 0, mode_flags = 0, baud_flags = 0, tune_flags = 0; |
| 223 | |
| 224 | if( count > 0 ) |
| 225 | { |
| 226 | if( menustate->index == (count-1) ) |
| 227 | flags |= MENU_FLAG_LEFT_ARROW; |
| 228 | else |
| 229 | flags |= MENU_FLAG_RIGHT_ARROW; |
| 230 | } |
| 231 | |
| 232 | if (bitbanger_inc_mode(&menustate->device->device(), TRUE)) |
| 233 | mode_flags |= MENU_FLAG_RIGHT_ARROW; |
| 234 | |
| 235 | if (bitbanger_dec_mode(&menustate->device->device(), TRUE)) |
| 236 | mode_flags |= MENU_FLAG_LEFT_ARROW; |
| 237 | |
| 238 | if (bitbanger_inc_baud(&menustate->device->device(), TRUE)) |
| 239 | baud_flags |= MENU_FLAG_RIGHT_ARROW; |
| 240 | |
| 241 | if (bitbanger_dec_baud(&menustate->device->device(), TRUE)) |
| 242 | baud_flags |= MENU_FLAG_LEFT_ARROW; |
| 243 | |
| 244 | if (bitbanger_inc_tune(&menustate->device->device(), TRUE)) |
| 245 | tune_flags |= MENU_FLAG_RIGHT_ARROW; |
| 246 | |
| 247 | if (bitbanger_dec_tune(&menustate->device->device(), TRUE)) |
| 248 | tune_flags |= MENU_FLAG_LEFT_ARROW; |
| 249 | |
| 250 | |
| 251 | if (menustate->device->exists()) |
| 252 | { |
| 253 | /* name of bitbanger file */ |
| 254 | ui_menu_item_append(menu, menustate->device->image_config().devconfig().name(), menustate->device->filename(), flags, BITBANGERCMD_SELECT); |
| 255 | ui_menu_item_append(menu, "Device Mode:", bitbanger_mode_string(&menustate->device->device()), mode_flags, BITBANGERCMD_MODE); |
| 256 | ui_menu_item_append(menu, "Baud:", bitbanger_baud_string(&menustate->device->device()), baud_flags, BITBANGERCMD_BAUD); |
| 257 | ui_menu_item_append(menu, "Baud Tune:", bitbanger_tune_string(&menustate->device->device()), tune_flags, BITBANGERCMD_TUNE); |
| 258 | ui_menu_item_append(menu, "Protocol:", "8-1-N", 0, NULL); |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | /* no tape loaded */ |
| 263 | ui_menu_item_append(menu, "No Bitbanger Image loaded", NULL, flags, NULL); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | |
| 268 | /*------------------------------------------------- |
| 179 | 269 | menu_tape_control - main tape control menu |
| 180 | 270 | -------------------------------------------------*/ |
| 181 | 271 | |
| r9429 | r9430 | |
| 269 | 359 | } |
| 270 | 360 | } |
| 271 | 361 | } |
| 362 | |
| 363 | |
| 364 | /*------------------------------------------------- |
| 365 | menu_bitbanger_control - main bitbanger |
| 366 | control menu |
| 367 | -------------------------------------------------*/ |
| 368 | |
| 369 | void ui_mess_menu_bitbanger_control(running_machine *machine, ui_menu *menu, void *parameter, void *state) |
| 370 | { |
| 371 | bitbanger_control_menu_state *menustate; |
| 372 | const ui_menu_event *event; |
| 373 | |
| 374 | /* if no state, allocate some */ |
| 375 | if (state == NULL) |
| 376 | state = ui_menu_alloc_state(menu, sizeof(*menustate), NULL); |
| 377 | menustate = (bitbanger_control_menu_state *) state; |
| 378 | |
| 379 | /* do we have to load the device? */ |
| 380 | if (menustate->device == NULL) |
| 381 | { |
| 382 | int index = menustate->index; |
| 383 | device_image_interface *device = NULL; |
| 384 | for (bool gotone = machine->m_devicelist.first(device); gotone; gotone = device->next(device)) |
| 385 | { |
| 386 | if(device->device().type() == BITBANGER) { |
| 387 | if (index==0) break; |
| 388 | index--; |
| 389 | } |
| 390 | } |
| 391 | menustate->device = device; |
| 392 | ui_menu_reset(menu, (ui_menu_reset_options)0); |
| 393 | } |
| 394 | |
| 395 | /* rebuild the menu */ |
| 396 | ui_menu_reset(menu, UI_MENU_RESET_REMEMBER_POSITION); |
| 397 | menu_bitbanger_control_populate(machine, menu, (bitbanger_control_menu_state*)state); |
| 398 | |
| 399 | /* process the menu */ |
| 400 | event = ui_menu_process(machine, menu, UI_MENU_PROCESS_LR_REPEAT); |
| 401 | if (event != NULL) |
| 402 | { |
| 403 | switch(event->iptkey) |
| 404 | { |
| 405 | case IPT_UI_LEFT: |
| 406 | if (event->itemref==BITBANGERCMD_SELECT) |
| 407 | { |
| 408 | /* left arrow - rotate left through cassette devices */ |
| 409 | if (menustate->index > 0) |
| 410 | menustate->index--; |
| 411 | else |
| 412 | menustate->index = bitbanger_count(machine) - 1; |
| 413 | menustate->device = NULL; |
| 414 | } |
| 415 | else if (event->itemref==BITBANGERCMD_MODE) |
| 416 | { |
| 417 | bitbanger_dec_mode(&menustate->device->device(), FALSE); |
| 418 | } |
| 419 | else if (event->itemref==BITBANGERCMD_BAUD) |
| 420 | { |
| 421 | bitbanger_dec_baud(&menustate->device->device(), FALSE); |
| 422 | } |
| 423 | else if (event->itemref==BITBANGERCMD_TUNE) |
| 424 | { |
| 425 | bitbanger_dec_tune(&menustate->device->device(), FALSE); |
| 426 | } |
| 427 | break; |
| 428 | |
| 429 | case IPT_UI_RIGHT: |
| 430 | if (event->itemref==BITBANGERCMD_SELECT) |
| 431 | { |
| 432 | /* right arrow - rotate right through cassette devices */ |
| 433 | if (menustate->index < bitbanger_count(machine) - 1) |
| 434 | menustate->index++; |
| 435 | else |
| 436 | menustate->index = 0; |
| 437 | menustate->device = NULL; |
| 438 | } |
| 439 | else if (event->itemref==BITBANGERCMD_MODE) |
| 440 | { |
| 441 | bitbanger_inc_mode(&menustate->device->device(), FALSE); |
| 442 | } |
| 443 | else if (event->itemref==BITBANGERCMD_BAUD) |
| 444 | { |
| 445 | bitbanger_inc_baud(&menustate->device->device(), FALSE); |
| 446 | } |
| 447 | else if (event->itemref==BITBANGERCMD_TUNE) |
| 448 | { |
| 449 | bitbanger_inc_tune(&menustate->device->device(), FALSE); |
| 450 | } |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
src/mess/machine/coco.c
| r9429 | r9430 | |
| 20 | 20 | |
| 21 | 21 | TODO: |
| 22 | 22 | - Implement unimplemented SAM registers |
| 23 | | - Implement unimplemented interrupts (serial) |
| 24 | 23 | - Choose and implement more appropriate ratios for the speed up poke |
| 25 | 24 | - Handle resets correctly |
| 26 | 25 | |
| r9429 | r9430 | |
| 76 | 75 | should go into coco3.c and dragon.c which should (hopefully) make the code |
| 77 | 76 | easier to manage. |
| 78 | 77 | P.Harvey-Smith, Dec 2006-Feb 2007 |
| 78 | |
| 79 | Added bi-directional bitbanger support. Also fixed reading PIA 1, port A. The |
| 80 | DAC and bitbanger values written should be reflected in the read. |
| 81 | tim lindner, October 2010 |
| 82 | |
| 79 | 83 | ***************************************************************************/ |
| 80 | 84 | |
| 81 | 85 | #include <math.h> |
| r9429 | r9430 | |
| 125 | 129 | static emu_timer *mux_sel1_timer; |
| 126 | 130 | static emu_timer *mux_sel2_timer; |
| 127 | 131 | static UINT8 mux_sel1, mux_sel2; |
| 132 | static UINT8 bitbanger_output_value; |
| 133 | static UINT8 bitbanger_input_value; |
| 134 | static UINT8 dac_value; |
| 128 | 135 | |
| 129 | 136 | static WRITE8_DEVICE_HANDLER ( d_pia1_pb_w ); |
| 130 | 137 | static WRITE8_DEVICE_HANDLER ( d_pia1_pa_w ); |
| r9429 | r9430 | |
| 1536 | 1543 | /* Printer output for the CoCo, output to bitbanger port */ |
| 1537 | 1544 | static void printer_out_coco(running_machine *machine, int data) |
| 1538 | 1545 | { |
| 1539 | | bitbanger_output(bitbanger_image(machine), (data & 2) >> 1); |
| 1546 | bitbanger_output_value = (data & 2) >> 1; |
| 1547 | bitbanger_output(bitbanger_image(machine), bitbanger_output_value); |
| 1540 | 1548 | } |
| 1541 | 1549 | |
| 1542 | 1550 | /* Printer output for the Dragon, output to Paralel port */ |
| r9429 | r9430 | |
| 1564 | 1572 | static int dclg_previous_bit; |
| 1565 | 1573 | UINT8 ctrl = input_port_read_safe(device->machine, "ctrl_sel", 0x00); |
| 1566 | 1574 | UINT8 hires = input_port_read_safe(device->machine, "hires_intf", 0x00); |
| 1575 | dac_value = dac >> 2; |
| 1567 | 1576 | |
| 1568 | 1577 | coco_sound_update(device->machine); |
| 1569 | 1578 | |
| r9429 | r9430 | |
| 1828 | 1837 | CASSETTE_MASK_MOTOR); |
| 1829 | 1838 | } |
| 1830 | 1839 | |
| 1840 | static READ8_DEVICE_HANDLER ( d_pia1_pa_r ) |
| 1841 | { |
| 1842 | UINT8 result; |
| 1831 | 1843 | |
| 1844 | result = cassette_input(cassette_device_image(device->machine)) >= 0 ? 1 : 0; |
| 1845 | result |= bitbanger_output_value << 1; |
| 1846 | result |= dac_value << 2; |
| 1832 | 1847 | |
| 1833 | | static READ8_DEVICE_HANDLER ( d_pia1_pa_r ) |
| 1848 | return result; |
| 1849 | } |
| 1850 | |
| 1851 | void coco_bitbanger_callback(running_machine *machine, UINT8 bit) |
| 1834 | 1852 | { |
| 1835 | | return (cassette_input(cassette_device_image(device->machine)) >= 0) ? 1 : 0; |
| 1853 | bitbanger_input_value = bit; |
| 1836 | 1854 | } |
| 1837 | 1855 | |
| 1856 | void coco3_bitbanger_callback(running_machine *machine, UINT8 bit) |
| 1857 | { |
| 1858 | /* rant: this interrupt is next to useless. It would be useful information to know when a |
| 1859 | start bit (high to low) occurs, but this interrupt activates in the opposite situation. |
| 1860 | */ |
| 1838 | 1861 | |
| 1862 | if( bitbanger_input_value == 0 && bit == 1) |
| 1863 | { |
| 1864 | bitbanger_input_value = bit; |
| 1865 | coco3_raise_interrupt(machine, COCO3_INT_EI2, 0); |
| 1866 | coco3_raise_interrupt(machine, COCO3_INT_EI2, 1); |
| 1867 | } |
| 1868 | else |
| 1869 | { |
| 1870 | bitbanger_input_value = bit; |
| 1871 | } |
| 1872 | } |
| 1839 | 1873 | |
| 1840 | 1874 | static READ8_DEVICE_HANDLER ( d_pia1_pb_r_coco ) |
| 1841 | 1875 | { |
| 1842 | 1876 | coco_state *state = device->machine->driver_data<coco_state>(); |
| 1843 | 1877 | |
| 1844 | 1878 | /* This handles the reading of the memory sense switch (pb2) for the CoCo 1, |
| 1845 | | * and serial-in (pb0). Serial-in not yet implemented. */ |
| 1879 | * and serial-in (pb0). */ |
| 1846 | 1880 | int result; |
| 1847 | 1881 | |
| 1848 | 1882 | /* For the CoCo 1, the logic has been changed to only select 64K rams |
| r9429 | r9430 | |
| 1859 | 1893 | else |
| 1860 | 1894 | result = 0x00; /* 4K Rams */ |
| 1861 | 1895 | |
| 1896 | result += bitbanger_input_value; |
| 1897 | |
| 1862 | 1898 | return result; |
| 1863 | 1899 | } |
| 1864 | 1900 | |
| r9429 | r9430 | |
| 1886 | 1922 | coco_state *state = device->machine->driver_data<coco_state>(); |
| 1887 | 1923 | |
| 1888 | 1924 | /* This handles the reading of the memory sense switch (pb2) for the CoCo 2 and 3, |
| 1889 | | * and serial-in (pb0). Serial-in not yet implemented. |
| 1925 | * and serial-in (pb0). |
| 1890 | 1926 | */ |
| 1891 | 1927 | int result; |
| 1892 | 1928 | |
| r9429 | r9430 | |
| 1896 | 1932 | result = 0x04; /* 16K: wire pia1_pb2 high */ |
| 1897 | 1933 | else |
| 1898 | 1934 | result = (pia6821_get_output_b(state->pia_0) & 0x40) >> 4; /* 32/64K: wire output of pia0_pb6 to input pia1_pb2 */ |
| 1935 | |
| 1936 | result += bitbanger_input_value; |
| 1937 | |
| 1899 | 1938 | return result; |
| 1900 | 1939 | } |
| 1901 | 1940 | |
| r9429 | r9430 | |
| 2827 | 2866 | machine->device<cpu_device>("maincpu")->debug()->set_dasm_override(coco_dasm_override); |
| 2828 | 2867 | } |
| 2829 | 2868 | |
| 2869 | /* setup printer input line to "space", this is what a Tandy printer would do */ |
| 2870 | bitbanger_output_value = 1; |
| 2871 | bitbanger_input_value = 0; |
| 2872 | dac_value = 0; |
| 2873 | |
| 2830 | 2874 | state_save_register_global(machine, mux_sel1); |
| 2831 | 2875 | state_save_register_global(machine, mux_sel2); |
| 2832 | 2876 | } |