src/mess/machine/serial.c
| r6174 | r6175 | |
| 43 | 43 | /* the serial streams */ |
| 44 | 44 | static struct serial_device serial_devices[MAX_SERIAL_DEVICES]; |
| 45 | 45 | static TIMER_CALLBACK(serial_device_baud_rate_callback); |
| 46 | | static void xmodem_init(void); |
| 47 | 46 | |
| 48 | 47 | |
| 49 | 48 | /*********************************************************/ |
| r6174 | r6175 | |
| 86 | 85 | serial_devices[id].connection.input_state = status; |
| 87 | 86 | } |
| 88 | 87 | |
| 89 | | /*********************************************************/ |
| 90 | | |
| 91 | | static void serial_protocol_none_sent_char(int id); |
| 92 | | |
| 93 | | static const struct serial_protocol_interface serial_protocol_none_interface= |
| 94 | | { |
| 95 | | NULL, |
| 96 | | serial_protocol_none_sent_char, |
| 97 | | }; |
| 98 | | |
| 99 | | |
| 100 | | static void serial_protocol_xmodem_receive_char(int id, unsigned char ch); |
| 101 | | static void serial_protocol_xmodem_sent_char(int id); |
| 102 | | |
| 103 | | static const struct serial_protocol_interface serial_protocol_xmodem_interface= |
| 104 | | { |
| 105 | | serial_protocol_xmodem_receive_char, |
| 106 | | serial_protocol_xmodem_sent_char |
| 107 | | }; |
| 108 | | |
| 109 | | |
| 110 | | /**********************************************************/ |
| 111 | | |
| 112 | 88 | /***** SERIAL DEVICE ******/ |
| 113 | 89 | void serial_device_setup(const device_config *image, int baud_rate, int num_data_bits, int stop_bit_count, int parity_code) |
| 114 | 90 | { |
| r6174 | r6175 | |
| 138 | 114 | serial_connection_out(image->machine,&serial_devices[id].connection); |
| 139 | 115 | transmit_register_reset(&serial_devices[id].transmit_reg); |
| 140 | 116 | receive_register_reset(&serial_devices[id].receive_reg); |
| 141 | | receive_register_setup(&serial_devices[id].receive_reg, &serial_devices[id].data_form); |
| 142 | | memset(&serial_devices[id].protocol_interface, 0, sizeof(struct serial_protocol_interface)); |
| 143 | | /* temp here */ |
| 144 | | xmodem_init(); |
| 117 | receive_register_setup(&serial_devices[id].receive_reg, &serial_devices[id].data_form); |
| 145 | 118 | } |
| 146 | 119 | |
| 147 | 120 | |
| r6174 | r6175 | |
| 152 | 125 | return serial_devices[id].connection.State; |
| 153 | 126 | } |
| 154 | 127 | |
| 155 | | |
| 156 | | static const char *const protocol_names[]= |
| 157 | | { |
| 158 | | "None", |
| 159 | | "XModem" |
| 160 | | }; |
| 161 | | |
| 162 | | /* get name of current protocol */ |
| 163 | | const char *serial_device_get_protocol_name(int protocol_id) |
| 164 | | { |
| 165 | | return protocol_names[protocol_id]; |
| 166 | | } |
| 167 | | |
| 168 | | void serial_device_set_protocol(const device_config *image, int protocol_id) |
| 169 | | { |
| 170 | | int id = image_index_in_device(image); |
| 171 | | |
| 172 | | if ((id<0) || (id>=MAX_SERIAL_DEVICES)) |
| 173 | | return; |
| 174 | | |
| 175 | | serial_devices[id].protocol = protocol_id; |
| 176 | | |
| 177 | | switch (serial_devices[id].protocol) |
| 178 | | { |
| 179 | | default: |
| 180 | | case SERIAL_PROTOCOL_NONE: |
| 181 | | { |
| 182 | | memcpy(&serial_devices[id].protocol_interface,&serial_protocol_none_interface,sizeof(struct serial_protocol_interface)); |
| 183 | | |
| 184 | | } |
| 185 | | break; |
| 186 | | |
| 187 | | case SERIAL_PROTOCOL_XMODEM: |
| 188 | | { |
| 189 | | memcpy(&serial_devices[id].protocol_interface,&serial_protocol_xmodem_interface,sizeof(struct serial_protocol_interface)); |
| 190 | | } |
| 191 | | break; |
| 192 | | } |
| 193 | | } |
| 194 | | |
| 195 | 128 | void serial_device_set_transmit_state(const device_config *image, int state) |
| 196 | 129 | { |
| 197 | 130 | int previous_state; |
| r6174 | r6175 | |
| 256 | 189 | return data_bit; |
| 257 | 190 | } |
| 258 | 191 | |
| 259 | | static int data_stream_get_byte(struct data_stream *stream) |
| 260 | | { |
| 261 | | int data_byte; |
| 262 | 192 | |
| 263 | | if (stream->ByteCount>=stream->DataLength) |
| 264 | | { |
| 265 | | return 0; |
| 266 | | } |
| 267 | | |
| 268 | | data_byte = stream->pData[stream->ByteCount]; |
| 269 | | stream->ByteCount++; |
| 270 | | |
| 271 | | return data_byte; |
| 272 | | } |
| 273 | | |
| 274 | 193 | void receive_register_setup(struct serial_receive_register *receive, struct data_form *data_form) |
| 275 | 194 | { |
| 276 | 195 | receive->bit_count = data_form->word_length + data_form->stop_bit_count; |
| r6174 | r6175 | |
| 541 | 460 | receive_register_extract(&serial_devices[id].receive_reg, &serial_devices[id].data_form); |
| 542 | 461 | |
| 543 | 462 | logerror("serial device receive char: %02x\n",serial_devices[id].receive_reg.byte_received); |
| 544 | | |
| 545 | | /* if callback setup, execute it */ |
| 546 | | if (serial_devices[id].protocol_interface.character_received_callback) |
| 547 | | serial_devices[id].protocol_interface.character_received_callback(id, serial_devices[id].receive_reg.byte_received); |
| 548 | 463 | } |
| 549 | 464 | |
| 550 | 465 | /* is transmit empty? */ |
| 551 | 466 | if (serial_devices[id].transmit_reg.flags & TRANSMIT_REGISTER_EMPTY) |
| 552 | 467 | { |
| 553 | 468 | /* char has been sent, execute callback */ |
| 554 | | if (serial_devices[id].protocol_interface.character_sent_callback) |
| 555 | | serial_devices[id].protocol_interface.character_sent_callback(id); |
| 469 | serial_protocol_none_sent_char(id); |
| 556 | 470 | } |
| 557 | 471 | |
| 558 | 472 | /* other side says it is clear to send? */ |
| r6174 | r6175 | |
| 673 | 587 | data_stream_free(&serial_devices[id].receive); |
| 674 | 588 | } |
| 675 | 589 | |
| 676 | | /********************************************************************************************************/ |
| 677 | | /***** XMODEM protocol ****/ |
| 678 | | |
| 679 | | static void serial_update_crc(unsigned short *crc, unsigned char value) |
| 680 | | { |
| 681 | | UINT8 l, h; |
| 682 | | |
| 683 | | l = value ^ (*crc >> 8); |
| 684 | | *crc = (*crc & 0xff) | (l << 8); |
| 685 | | l >>= 4; |
| 686 | | l ^= (*crc >> 8); |
| 687 | | *crc <<= 8; |
| 688 | | *crc = (*crc & 0xff00) | l; |
| 689 | | l = (l << 4) | (l >> 4); |
| 690 | | h = l; |
| 691 | | l = (l << 2) | (l >> 6); |
| 692 | | l &= 0x1f; |
| 693 | | *crc = *crc ^ (l << 8); |
| 694 | | l = h & 0xf0; |
| 695 | | *crc = *crc ^ (l << 8); |
| 696 | | l = (h << 1) | (h >> 7); |
| 697 | | l &= 0xe0; |
| 698 | | *crc = *crc ^ l; |
| 699 | | } |
| 700 | | |
| 701 | | /* XMODEM protocol */ |
| 702 | | enum |
| 703 | | { |
| 704 | | XMODEM_STATE_WAITING_FOR_LINK, |
| 705 | | XMODEM_STATE_SENDING_PACKET, |
| 706 | | XMODEM_STATE_WAITING_FOR_CONFIRM, |
| 707 | | XMODEM_WAITING_FOR_END_CONFIRM1, |
| 708 | | XMODEM_WAITING_FOR_END_CONFIRM2 |
| 709 | | }; |
| 710 | | |
| 711 | | |
| 712 | | struct xmodem |
| 713 | | { |
| 714 | | int state; |
| 715 | | int packet_id; |
| 716 | | unsigned char packet[1024]; |
| 717 | | unsigned long offset_in_packet; |
| 718 | | unsigned long packet_size; |
| 719 | | }; |
| 720 | | |
| 721 | | static struct xmodem xmodem_transfer; |
| 722 | | |
| 723 | | static void xmodem_set_new_state(int); |
| 724 | | |
| 725 | | void xmodem_init(void) |
| 726 | | { |
| 727 | | xmodem_set_new_state(XMODEM_STATE_WAITING_FOR_LINK); |
| 728 | | xmodem_transfer.packet_id = 1; |
| 729 | | } |
| 730 | | |
| 731 | | static void xmodem_setup_packet(int id) |
| 732 | | { |
| 733 | | int i; |
| 734 | | int count; |
| 735 | | UINT16 crc; |
| 736 | | |
| 737 | | count = 0; |
| 738 | | |
| 739 | | |
| 740 | | /* |
| 741 | | packet: |
| 742 | | |
| 743 | | 1 byte SOH |
| 744 | | 1 byte packet id |
| 745 | | 1 byte packet id (stored 1's complemented) |
| 746 | | 128 bytes data |
| 747 | | 1 byte CRC-16 upper byte |
| 748 | | 1 byte CRC-16 lower byte |
| 749 | | */ |
| 750 | | |
| 751 | | /* soh */ |
| 752 | | xmodem_transfer.packet[count] = XMODEM_SOH; |
| 753 | | count++; |
| 754 | | /* packet id */ |
| 755 | | xmodem_transfer.packet[count] = xmodem_transfer.packet_id & 0x0ff; |
| 756 | | count++; |
| 757 | | /* 1's complement of packet id */ |
| 758 | | xmodem_transfer.packet[count] = (xmodem_transfer.packet_id^0x0ff) & 0x0ff; |
| 759 | | count++; |
| 760 | | |
| 761 | | crc = 0; |
| 762 | | |
| 763 | | for (i=0; i<128; i++) |
| 764 | | { |
| 765 | | xmodem_transfer.packet[count] = data_stream_get_byte(&serial_devices[id].transmit); |
| 766 | | serial_update_crc(&crc, xmodem_transfer.packet[count]); |
| 767 | | count++; |
| 768 | | } |
| 769 | | |
| 770 | | xmodem_transfer.packet[count] = (crc>>8) & 0x0ff; |
| 771 | | count++; |
| 772 | | xmodem_transfer.packet[count] = crc & 0x0ff; |
| 773 | | count++; |
| 774 | | |
| 775 | | xmodem_transfer.packet_size = count; |
| 776 | | xmodem_transfer.offset_in_packet = 0; |
| 777 | | |
| 778 | | } |
| 779 | | |
| 780 | | static void xmodem_resend_block(void) |
| 781 | | { |
| 782 | | xmodem_transfer.offset_in_packet = 0; |
| 783 | | xmodem_set_new_state(XMODEM_STATE_SENDING_PACKET); |
| 784 | | } |
| 785 | | |
| 786 | | |
| 787 | | static void xmodem_set_new_state(int state) |
| 788 | | { |
| 789 | | xmodem_transfer.state = state; |
| 790 | | } |
| 791 | | |
| 792 | | static void serial_protocol_xmodem_receive_char(int id, unsigned char ch) |
| 793 | | { |
| 794 | | switch (xmodem_transfer.state) |
| 795 | | { |
| 796 | | case XMODEM_STATE_WAITING_FOR_LINK: |
| 797 | | { |
| 798 | | if (ch==XMODEM_CRC_NAK) |
| 799 | | { |
| 800 | | logerror("xmodem nak received\n"); |
| 801 | | |
| 802 | | xmodem_setup_packet(id); |
| 803 | | xmodem_set_new_state(XMODEM_STATE_SENDING_PACKET); |
| 804 | | } |
| 805 | | } |
| 806 | | break; |
| 807 | | |
| 808 | | case XMODEM_STATE_WAITING_FOR_CONFIRM: |
| 809 | | { |
| 810 | | switch (ch) |
| 811 | | { |
| 812 | | case XMODEM_ACK: |
| 813 | | { |
| 814 | | logerror("block accepted\n"); |
| 815 | | xmodem_transfer.packet_id++; |
| 816 | | xmodem_setup_packet(id); |
| 817 | | xmodem_set_new_state(XMODEM_STATE_SENDING_PACKET); |
| 818 | | } |
| 819 | | break; |
| 820 | | |
| 821 | | case XMODEM_NAK: |
| 822 | | { |
| 823 | | logerror("resend block\n"); |
| 824 | | xmodem_resend_block(); |
| 825 | | } |
| 826 | | break; |
| 827 | | |
| 828 | | default: |
| 829 | | { |
| 830 | | logerror("unknown response\n"); |
| 831 | | } |
| 832 | | break; |
| 833 | | } |
| 834 | | } |
| 835 | | break; |
| 836 | | |
| 837 | | default: |
| 838 | | break; |
| 839 | | } |
| 840 | | } |
| 841 | | |
| 842 | | static void serial_protocol_xmodem_sent_char(int id) |
| 843 | | { |
| 844 | | switch (xmodem_transfer.state) |
| 845 | | { |
| 846 | | case XMODEM_STATE_SENDING_PACKET: |
| 847 | | { |
| 848 | | unsigned char data_byte; |
| 849 | | |
| 850 | | if (xmodem_transfer.offset_in_packet==xmodem_transfer.packet_size) |
| 851 | | { |
| 852 | | xmodem_set_new_state(XMODEM_STATE_WAITING_FOR_CONFIRM); |
| 853 | | return; |
| 854 | | } |
| 855 | | |
| 856 | | /* generate byte to transmit */ |
| 857 | | data_byte = xmodem_transfer.packet[xmodem_transfer.offset_in_packet]; |
| 858 | | xmodem_transfer.offset_in_packet++; |
| 859 | | logerror("packet byte: %02x\n",data_byte); |
| 860 | | |
| 861 | | /* setup register */ |
| 862 | | transmit_register_setup(&serial_devices[id].transmit_reg,&serial_devices[id].data_form, data_byte); |
| 863 | | |
| 864 | | logerror("serial device transmitted char: %02x\n",data_byte); |
| 865 | | } |
| 866 | | break; |
| 867 | | |
| 868 | | |
| 869 | | |
| 870 | | default: |
| 871 | | break; |
| 872 | | } |
| 873 | | } |
| 874 | | |
| 875 | 590 | /*******************************************************************************/ |
| 591 | /*******************************************************************************/ |
| 876 | 592 | /********* SERIAL CONNECTION ***********/ |
| 877 | 593 | |
| 878 | 594 | |
src/mess/machine/serial.h
| r6174 | r6175 | |
| 75 | 75 | /* TX = Transmit data. (OUTPUT) */ |
| 76 | 76 | #define SERIAL_STATE_TX_DATA 0x00020 |
| 77 | 77 | |
| 78 | | /* protocol selections */ |
| 79 | | |
| 80 | | /* all data is transmitted with a start bit, data bits, optional parity bit, |
| 81 | | and stop bits. These are programmable. */ |
| 82 | | enum |
| 83 | | { |
| 84 | | SERIAL_PROTOCOL_NONE, /* no protocol applied */ |
| 85 | | SERIAL_PROTOCOL_XMODEM /* transfer data using xmodem protocol */ |
| 86 | | }; |
| 87 | | |
| 88 | 78 | /* parity selections */ |
| 89 | 79 | /* if all the bits are added in a byte, if the result is: |
| 90 | 80 | even -> parity is even |
| r6174 | r6175 | |
| 251 | 241 | unsigned long ByteCount; |
| 252 | 242 | }; |
| 253 | 243 | |
| 254 | | /* an internal interface used by serial device */ |
| 255 | | struct serial_protocol_interface |
| 256 | | { |
| 257 | | /* callback executed when character has been received */ |
| 258 | | void (*character_received_callback)(int id,unsigned char ch); |
| 259 | | /* callback executed when character has been transmitted, ready to transmit a new char */ |
| 260 | | void (*character_sent_callback)(int id); |
| 261 | | }; |
| 262 | | |
| 263 | 244 | /* a serial device */ |
| 264 | 245 | struct serial_device |
| 265 | 246 | { |
| r6174 | r6175 | |
| 280 | 261 | struct data_form data_form; |
| 281 | 262 | |
| 282 | 263 | int transmit_state; |
| 283 | | int protocol; |
| 284 | 264 | |
| 285 | 265 | /* baud rate */ |
| 286 | 266 | unsigned long BaudRate; |
| 287 | 267 | |
| 288 | 268 | /* baud rate timer */ |
| 289 | 269 | void *timer; |
| 290 | | |
| 291 | | struct serial_protocol_interface protocol_interface; |
| 292 | 270 | }; |
| 293 | 271 | |
| 294 | 272 | unsigned long serial_device_get_state(int id); |
| r6174 | r6175 | |
| 305 | 283 | |
| 306 | 284 | void serial_device_setup(const device_config *image, int baud_rate, int num_data_bits, int stop_bit_count, int parity_code); |
| 307 | 285 | |
| 308 | | /* get name of protocol identified by specified id */ |
| 309 | | const char *serial_device_get_protocol_name(int protocol_id); |
| 310 | | |
| 311 | | /* set the protocol to be used by the serial device */ |
| 312 | | void serial_device_set_protocol(const device_config *image, int protocol_id); |
| 313 | 286 | /* set the transmit state of the serial device */ |
| 314 | 287 | void serial_device_set_transmit_state(const device_config *image, int state); |
| 315 | 288 | |
| 316 | | /***********************************************************************************************/ |
| 317 | | /* XModem protocol */ |
| 318 | | |
| 319 | | #define XMODEM_CRC_NAK 67 /* begins a xmodem-crc transfer */ |
| 320 | | #define XMODEM_SOH 1 /* id of a 256 byte block */ |
| 321 | | #define XMODEM_EOT 4 |
| 322 | | #define XMODEM_ACK 6 |
| 323 | | #define XMODEM_NAK 21 /* begins a xmodem transfer */ |
| 324 | | #define XMODEM_CAN 24 |
| 325 | | #define XMODEM_STX 2 /* id start of a 1k block */ |
| 326 | | |
| 327 | | |
| 328 | 289 | #endif /* SERIAL_H_ */ |