src/mess/devices/bitbngr.c
| r2360 | r2361 | |
| 13 | 13 | #include "printer.h" |
| 14 | 14 | |
| 15 | 15 | |
| 16 | | static TIMER_CALLBACK(bitbanger_overthreshhold); |
| 17 | 16 | |
| 18 | | struct bitbanger_info |
| 17 | /*************************************************************************** |
| 18 | TYPE DEFINITIONS |
| 19 | ***************************************************************************/ |
| 20 | |
| 21 | typedef struct _bitbanger_token bitbanger_token; |
| 22 | struct _bitbanger_token |
| 19 | 23 | { |
| 20 | | const struct bitbanger_config *config; |
| 21 | 24 | double last_pulse_time; |
| 22 | 25 | double *pulses; |
| 23 | 26 | int *factored_pulses; |
| 24 | 27 | int recorded_pulses; |
| 25 | 28 | int value; |
| 26 | | void *timeout_timer; |
| 29 | emu_timer *timeout_timer; |
| 27 | 30 | int over_threshhold; |
| 28 | 31 | }; |
| 29 | 32 | |
| 30 | | static struct bitbanger_info *bitbangers[MAX_PRINTER]; |
| 31 | 33 | |
| 34 | |
| 35 | /*************************************************************************** |
| 36 | FUNCTION PROTOTYPES |
| 37 | ***************************************************************************/ |
| 38 | |
| 39 | static TIMER_CALLBACK(bitbanger_overthreshhold); |
| 40 | |
| 41 | |
| 42 | |
| 43 | /*************************************************************************** |
| 44 | INLINE FUNCTIONS |
| 45 | ***************************************************************************/ |
| 46 | |
| 47 | /*------------------------------------------------- |
| 48 | get_token - safely gets the bitbanger data |
| 49 | -------------------------------------------------*/ |
| 50 | |
| 51 | INLINE bitbanger_token *get_token(const device_config *device) |
| 52 | { |
| 53 | assert(device != NULL); |
| 54 | assert(device->token != NULL); |
| 55 | assert(device->type == BITBANGER); |
| 56 | return (bitbanger_token *) device->token; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | |
| 61 | /*------------------------------------------------- |
| 62 | get_config - safely gets the bitbanger config |
| 63 | -------------------------------------------------*/ |
| 64 | |
| 65 | INLINE const bitbanger_config *get_config(const device_config *device) |
| 66 | { |
| 67 | assert(device != NULL); |
| 68 | assert(device->token != NULL); |
| 69 | assert(device->type == BITBANGER); |
| 70 | return (const bitbanger_config *) device->static_config; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | |
| 75 | /*************************************************************************** |
| 76 | IMPLEMENTATION |
| 77 | ***************************************************************************/ |
| 78 | |
| 79 | /*------------------------------------------------- |
| 80 | DEVICE_START(bitbanger) |
| 81 | -------------------------------------------------*/ |
| 82 | |
| 32 | 83 | static DEVICE_START(bitbanger) |
| 33 | 84 | { |
| 34 | | int id = image_index_in_device(device); |
| 35 | | struct bitbanger_info *bi; |
| 36 | | const struct bitbanger_config *config; |
| 37 | | const struct IODevice *dev; |
| 85 | bitbanger_token *bi; |
| 86 | const bitbanger_config *config = get_config(device); |
| 38 | 87 | |
| 39 | | dev = mess_device_from_core_device(device); |
| 40 | | config = (const struct bitbanger_config *) mess_device_get_info_ptr(&dev->devclass, MESS_DEVINFO_PTR_BITBANGER_CONFIG); |
| 41 | | |
| 42 | | bi = (struct bitbanger_info *) auto_malloc(sizeof(struct bitbanger_info)); |
| 88 | bi = get_token(device); |
| 43 | 89 | bi->pulses = (double *) auto_malloc(config->maximum_pulses * sizeof(double)); |
| 44 | 90 | bi->factored_pulses = (int *) auto_malloc(config->maximum_pulses * sizeof(int)); |
| 45 | | bi->config = config; |
| 46 | 91 | bi->last_pulse_time = 0.0; |
| 47 | 92 | bi->recorded_pulses = 0; |
| 48 | 93 | bi->value = config->initial_value; |
| 49 | | bi->timeout_timer = timer_alloc(bitbanger_overthreshhold, NULL); |
| 94 | bi->timeout_timer = timer_alloc(bitbanger_overthreshhold, (void *) device); |
| 50 | 95 | bi->over_threshhold = 1; |
| 51 | | |
| 52 | | bitbangers[id] = bi; |
| 53 | 96 | } |
| 54 | 97 | |
| 55 | 98 | |
| 56 | 99 | |
| 57 | | static void bitbanger_analyze(int id, struct bitbanger_info *bi) |
| 100 | /*------------------------------------------------- |
| 101 | bitbanger_analyze |
| 102 | -------------------------------------------------*/ |
| 103 | |
| 104 | static void bitbanger_analyze(const device_config *device) |
| 58 | 105 | { |
| 59 | 106 | int i, factor, total_duration; |
| 60 | 107 | double smallest_pulse; |
| 61 | 108 | double d; |
| 109 | bitbanger_token *bi = get_token(device); |
| 110 | const bitbanger_config *config = get_config(device); |
| 62 | 111 | |
| 63 | 112 | /* compute the smallest pulse */ |
| 64 | | smallest_pulse = bi->config->pulse_threshhold; |
| 113 | smallest_pulse = config->pulse_threshhold; |
| 65 | 114 | for (i = 0; i < bi->recorded_pulses; i++) |
| 66 | 115 | if (smallest_pulse > bi->pulses[i]) |
| 67 | 116 | smallest_pulse = bi->pulses[i]; |
| r2360 | r2361 | |
| 75 | 124 | |
| 76 | 125 | for (i = 0; i < bi->recorded_pulses; i++) |
| 77 | 126 | { |
| 78 | | d = bi->pulses[i] / smallest_pulse * factor + bi->config->pulse_tolerance; |
| 79 | | if ((i < (bi->recorded_pulses-1)) && (d - floor(d)) >= (bi->config->pulse_tolerance * 2)) |
| 127 | d = bi->pulses[i] / smallest_pulse * factor + config->pulse_tolerance; |
| 128 | if ((i < (bi->recorded_pulses-1)) && (d - floor(d)) >= (config->pulse_tolerance * 2)) |
| 80 | 129 | { |
| 81 | 130 | i = -1; |
| 82 | 131 | break; |
| r2360 | r2361 | |
| 85 | 134 | total_duration += (int) d; |
| 86 | 135 | } |
| 87 | 136 | } |
| 88 | | while((i == -1) && (factor < bi->config->maximum_pulses)); |
| 137 | while((i == -1) && (factor < config->maximum_pulses)); |
| 89 | 138 | if (i == -1) |
| 90 | 139 | return; |
| 91 | 140 | |
| 92 | 141 | /* filter the output */ |
| 93 | | if (bi->config->filter(image_from_devtype_and_index(IO_BITBANGER, id), bi->factored_pulses, bi->recorded_pulses, total_duration)) |
| 142 | if (config->filter(device, bi->factored_pulses, bi->recorded_pulses, total_duration)) |
| 94 | 143 | bi->recorded_pulses = 0; |
| 95 | 144 | } |
| 96 | 145 | |
| 97 | 146 | |
| 98 | 147 | |
| 99 | | static void bitbanger_addpulse(int id, struct bitbanger_info *bi, double pulse_width) |
| 148 | /*------------------------------------------------- |
| 149 | bitbanger_addpulse |
| 150 | -------------------------------------------------*/ |
| 151 | |
| 152 | static void bitbanger_addpulse(const device_config *device, double pulse_width) |
| 100 | 153 | { |
| 154 | bitbanger_token *bi = get_token(device); |
| 155 | const bitbanger_config *config = get_config(device); |
| 156 | |
| 101 | 157 | /* exceeded total countable pulses? */ |
| 102 | | if (bi->recorded_pulses == bi->config->maximum_pulses) |
| 158 | if (bi->recorded_pulses == config->maximum_pulses) |
| 103 | 159 | memmove(bi->pulses, bi->pulses + 1, (--bi->recorded_pulses) * sizeof(double)); |
| 104 | 160 | |
| 105 | 161 | /* record the pulse */ |
| 106 | 162 | bi->pulses[bi->recorded_pulses++] = pulse_width; |
| 107 | 163 | |
| 108 | 164 | /* analyze, if necessary */ |
| 109 | | if (bi->recorded_pulses >= bi->config->minimum_pulses) |
| 110 | | bitbanger_analyze(id, bi); |
| 165 | if (bi->recorded_pulses >= config->minimum_pulses) |
| 166 | bitbanger_analyze(device); |
| 111 | 167 | } |
| 112 | 168 | |
| 113 | 169 | |
| 114 | 170 | |
| 171 | /*------------------------------------------------- |
| 172 | TIMER_CALLBACK(bitbanger_overthreshhold) |
| 173 | -------------------------------------------------*/ |
| 174 | |
| 115 | 175 | static TIMER_CALLBACK(bitbanger_overthreshhold) |
| 116 | 176 | { |
| 117 | | int id = param; |
| 118 | | struct bitbanger_info *bi = bitbangers[id]; |
| 119 | | bitbanger_addpulse(id, bi, attotime_to_double(timer_get_time()) - bi->last_pulse_time); |
| 177 | const device_config *device = (const device_config *) ptr; |
| 178 | bitbanger_token *bi = get_token(device); |
| 179 | |
| 180 | bitbanger_addpulse(device, attotime_to_double(timer_get_time()) - bi->last_pulse_time); |
| 120 | 181 | bi->over_threshhold = 1; |
| 121 | 182 | bi->recorded_pulses = 0; |
| 122 | 183 | } |
| 123 | 184 | |
| 124 | 185 | |
| 125 | 186 | |
| 126 | | void bitbanger_output(const device_config *img, int value) |
| 187 | /*------------------------------------------------- |
| 188 | bitbanger_output - outputs data to a bitbanger |
| 189 | port |
| 190 | -------------------------------------------------*/ |
| 191 | |
| 192 | void bitbanger_output(const device_config *device, int value) |
| 127 | 193 | { |
| 128 | | int id = image_index_in_device(img); |
| 129 | | struct bitbanger_info *bi = bitbangers[id]; |
| 194 | bitbanger_token *bi = get_token(device); |
| 195 | const bitbanger_config *config = get_config(device); |
| 196 | |
| 130 | 197 | double current_time; |
| 131 | 198 | double pulse_width; |
| 132 | 199 | |
| r2360 | r2361 | |
| 141 | 208 | |
| 142 | 209 | assert(pulse_width >= 0); |
| 143 | 210 | |
| 144 | | if (!bi->over_threshhold && ((bi->recorded_pulses > 0) || (bi->value == bi->config->initial_value))) |
| 145 | | bitbanger_addpulse(id, bi, pulse_width); |
| 211 | if (!bi->over_threshhold && ((bi->recorded_pulses > 0) || (bi->value == config->initial_value))) |
| 212 | bitbanger_addpulse(device, pulse_width); |
| 146 | 213 | |
| 147 | 214 | /* update state */ |
| 148 | 215 | bi->value = value; |
| r2360 | r2361 | |
| 150 | 217 | bi->over_threshhold = 0; |
| 151 | 218 | |
| 152 | 219 | /* update timeout timer */ |
| 153 | | timer_reset(bi->timeout_timer, double_to_attotime(bi->config->pulse_threshhold)); |
| 220 | timer_reset(bi->timeout_timer, double_to_attotime(config->pulse_threshhold)); |
| 154 | 221 | } |
| 155 | 222 | } |
| 156 | 223 | |
| 157 | 224 | |
| 158 | 225 | |
| 159 | | void bitbanger_device_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info) |
| 226 | /*------------------------------------------------- |
| 227 | DEVICE_GET_INFO(bitbanger) - device getinfo |
| 228 | function |
| 229 | -------------------------------------------------*/ |
| 230 | |
| 231 | DEVICE_GET_INFO(bitbanger) |
| 160 | 232 | { |
| 161 | 233 | switch(state) |
| 162 | 234 | { |
| 163 | 235 | /* --- the following bits of info are returned as 64-bit signed integers --- */ |
| 164 | | case MESS_DEVINFO_INT_TYPE: info->i = IO_BITBANGER; break; |
| 165 | | case MESS_DEVINFO_INT_READABLE: info->i = 1; break; |
| 166 | | case MESS_DEVINFO_INT_WRITEABLE: info->i = 1; break; |
| 167 | | case MESS_DEVINFO_INT_CREATABLE: info->i = 1; break; |
| 236 | case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(bitbanger_token); break; |
| 237 | case DEVINFO_INT_INLINE_CONFIG_BYTES: info->i = 0; break; |
| 238 | case DEVINFO_INT_CLASS: info->i = DEVICE_CLASS_PERIPHERAL; break; |
| 239 | case DEVINFO_INT_IMAGE_TYPE: info->i = IO_PRINTER; break; |
| 240 | case DEVINFO_INT_IMAGE_READABLE: info->i = 0; break; |
| 241 | case DEVINFO_INT_IMAGE_WRITEABLE: info->i = 1; break; |
| 242 | case DEVINFO_INT_IMAGE_CREATABLE: info->i = 1; break; |
| 168 | 243 | |
| 169 | 244 | /* --- the following bits of info are returned as pointers to data or functions --- */ |
| 170 | | case MESS_DEVINFO_PTR_START: info->start = DEVICE_START_NAME(bitbanger); break; |
| 245 | case DEVINFO_FCT_START: info->start = DEVICE_START_NAME(bitbanger); break; |
| 171 | 246 | |
| 172 | 247 | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 173 | | case MESS_DEVINFO_STR_DEV_FILE: strcpy(info->s = device_temp_str(), __FILE__); break; |
| 174 | | case MESS_DEVINFO_STR_FILE_EXTENSIONS: strcpy(info->s = device_temp_str(), "prn"); break; |
| 248 | case DEVINFO_STR_NAME: info->s = "Bitbanger"; break; |
| 249 | case DEVINFO_STR_FAMILY: info->s = "Bitbanger"; break; |
| 250 | case DEVINFO_STR_SOURCE_FILE: info->s = __FILE__; break; |
| 251 | case DEVINFO_STR_IMAGE_FILE_EXTENSIONS: info->s = "prn"; break; |
| 175 | 252 | } |
| 176 | 253 | } |
src/mess/drivers/coco.c
| r2360 | r2361 | |
| 563 | 563 | PORT_INCLUDE( cart_autostart ) |
| 564 | 564 | INPUT_PORTS_END |
| 565 | 565 | |
| 566 | /*************************************************************************** |
| 567 | Bitbanger port |
| 568 | ***************************************************************************/ |
| 569 | |
| 570 | static int coco_bitbanger_filter(const device_config *img, const int *pulses, int total_pulses, int total_duration) |
| 571 | { |
| 572 | int i; |
| 573 | int result = 0; |
| 574 | int word; |
| 575 | int pos; |
| 576 | int pulse_type; |
| 577 | int c; |
| 578 | |
| 579 | if (total_duration >= 11) |
| 580 | { |
| 581 | word = 0; |
| 582 | pos = 0; |
| 583 | pulse_type = 0; |
| 584 | result = 1; |
| 585 | |
| 586 | for (i = 0; i < total_pulses; i++) |
| 587 | { |
| 588 | if (pulse_type) |
| 589 | word |= ((1 << pulses[i]) - 1) << pos; |
| 590 | pulse_type ^= 1; |
| 591 | pos += pulses[i]; |
| 592 | } |
| 593 | |
| 594 | c = (word >> 1) & 0xff; |
| 595 | printer_output(img, c); |
| 596 | } |
| 597 | return result; |
| 598 | } |
| 599 | |
| 600 | static const bitbanger_config coco_bitbanger_config = |
| 601 | { |
| 602 | coco_bitbanger_filter, |
| 603 | 1.0 / 10.0, |
| 604 | 0.2, |
| 605 | 2, |
| 606 | 10, |
| 607 | 0, |
| 608 | 0 |
| 609 | }; |
| 610 | |
| 611 | static MACHINE_DRIVER_START( coco_bitbanger ) |
| 612 | MDRV_DEVICE_ADD("bitbanger", BITBANGER) |
| 613 | MDRV_DEVICE_CONFIG(coco_bitbanger_config) |
| 614 | MACHINE_DRIVER_END |
| 615 | |
| 616 | /* ----------------------------------------------------------------------- */ |
| 617 | |
| 566 | 618 | /* AY-8912 for Dragon Alpha, the AY-8912 simply an AY-8910 with only one io port. */ |
| 567 | 619 | static const struct AY8910interface ay8912_interface = |
| 568 | 620 | { |
| r2360 | r2361 | |
| 717 | 769 | |
| 718 | 770 | /* sound hardware */ |
| 719 | 771 | MDRV_IMPORT_FROM( coco_sound ) |
| 772 | |
| 773 | /* bitbanger/printer */ |
| 774 | MDRV_IMPORT_FROM( coco_bitbanger ) |
| 720 | 775 | MACHINE_DRIVER_END |
| 721 | 776 | |
| 722 | 777 | static MACHINE_DRIVER_START( coco2 ) |
| r2360 | r2361 | |
| 737 | 792 | |
| 738 | 793 | /* sound hardware */ |
| 739 | 794 | MDRV_IMPORT_FROM( coco_sound ) |
| 795 | |
| 796 | /* bitbanger/printer */ |
| 797 | MDRV_IMPORT_FROM( coco_bitbanger ) |
| 740 | 798 | MACHINE_DRIVER_END |
| 741 | 799 | |
| 742 | 800 | static MACHINE_DRIVER_START( coco2b ) |
| r2360 | r2361 | |
| 757 | 815 | |
| 758 | 816 | /* sound hardware */ |
| 759 | 817 | MDRV_IMPORT_FROM( coco_sound ) |
| 818 | |
| 819 | /* bitbanger/printer */ |
| 820 | MDRV_IMPORT_FROM( coco_bitbanger ) |
| 760 | 821 | MACHINE_DRIVER_END |
| 761 | 822 | |
| 762 | 823 | static MACHINE_DRIVER_START( coco3 ) |
| r2360 | r2361 | |
| 787 | 848 | /* sound hardware */ |
| 788 | 849 | MDRV_IMPORT_FROM( coco_sound ) |
| 789 | 850 | |
| 851 | /* bitbanger/printer */ |
| 852 | MDRV_IMPORT_FROM( coco_bitbanger ) |
| 853 | |
| 790 | 854 | /* devices */ |
| 791 | 855 | MDRV_DEVICE_ADD("vhd", COCO_VHD) |
| 792 | 856 | MACHINE_DRIVER_END |
| r2360 | r2361 | |
| 895 | 959 | |
| 896 | 960 | #define rom_coco3h rom_coco3 |
| 897 | 961 | |
| 898 | | /*************************************************************************** |
| 899 | | Bitbanger port |
| 900 | | ***************************************************************************/ |
| 901 | | |
| 902 | | static int coco_bitbanger_filter(const device_config *img, const int *pulses, int total_pulses, int total_duration) |
| 903 | | { |
| 904 | | int i; |
| 905 | | int result = 0; |
| 906 | | int word; |
| 907 | | int pos; |
| 908 | | int pulse_type; |
| 909 | | int c; |
| 910 | | |
| 911 | | if (total_duration >= 11) |
| 912 | | { |
| 913 | | word = 0; |
| 914 | | pos = 0; |
| 915 | | pulse_type = 0; |
| 916 | | result = 1; |
| 917 | | |
| 918 | | for (i = 0; i < total_pulses; i++) |
| 919 | | { |
| 920 | | if (pulse_type) |
| 921 | | word |= ((1 << pulses[i]) - 1) << pos; |
| 922 | | pulse_type ^= 1; |
| 923 | | pos += pulses[i]; |
| 924 | | } |
| 925 | | |
| 926 | | c = (word >> 1) & 0xff; |
| 927 | | printer_output(img, c); |
| 928 | | } |
| 929 | | return result; |
| 930 | | } |
| 931 | | |
| 932 | | static const struct bitbanger_config coco_bitbanger_config = |
| 933 | | { |
| 934 | | coco_bitbanger_filter, |
| 935 | | 1.0 / 10.0, |
| 936 | | 0.2, |
| 937 | | 2, |
| 938 | | 10, |
| 939 | | 0, |
| 940 | | 0 |
| 941 | | }; |
| 942 | | |
| 943 | | /* ----------------------------------------------------------------------- */ |
| 944 | | |
| 945 | 962 | /************************************* |
| 946 | 963 | * |
| 947 | 964 | * CoCo device getinfo functions |
| 948 | 965 | * |
| 949 | 966 | *************************************/ |
| 950 | 967 | |
| 951 | | static void coco_bitbanger_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info) |
| 952 | | { |
| 953 | | /* bitbanger port */ |
| 954 | | switch(state) |
| 955 | | { |
| 956 | | /* --- the following bits of info are returned as 64-bit signed integers --- */ |
| 957 | | case MESS_DEVINFO_INT_COUNT: info->i = 1; break; |
| 958 | | |
| 959 | | /* --- the following bits of info are returned as pointers to data or functions --- */ |
| 960 | | case MESS_DEVINFO_PTR_BITBANGER_CONFIG: info->p = (void *) &coco_bitbanger_config; break; |
| 961 | | |
| 962 | | default: bitbanger_device_getinfo(devclass, state, info); break; |
| 963 | | } |
| 964 | | } |
| 965 | | |
| 966 | | |
| 967 | | |
| 968 | 968 | static void coco_cassette_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info) |
| 969 | 969 | { |
| 970 | 970 | /* cassette */ |
| r2360 | r2361 | |
| 1114 | 1114 | |
| 1115 | 1115 | |
| 1116 | 1116 | SYSTEM_CONFIG_START( generic_coco ) |
| 1117 | | CONFIG_DEVICE( coco_bitbanger_getinfo ) |
| 1118 | 1117 | CONFIG_DEVICE( coco_cassette_getinfo ) |
| 1119 | 1118 | CONFIG_DEVICE( coco_floppy_getinfo ) |
| 1120 | 1119 | CONFIG_DEVICE( coco_quickload_getinfo ) |