src/emu/imagedev/serial.c
| r0 | r12778 | |
| 1 | /**************************************************************************** |
| 2 | |
| 3 | serial.c |
| 4 | |
| 5 | Code for handling serial port image devices |
| 6 | |
| 7 | ****************************************************************************/ |
| 8 | |
| 9 | #include "emu.h" |
| 10 | #include "serial.h" |
| 11 | |
| 12 | |
| 13 | // device type definition |
| 14 | const device_type SERIAL = &device_creator<serial_image_device>; |
| 15 | |
| 16 | //------------------------------------------------- |
| 17 | // serial_image_device - constructor |
| 18 | //------------------------------------------------- |
| 19 | |
| 20 | serial_image_device::serial_image_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) |
| 21 | : device_t(mconfig, SERIAL, "Serial", tag, owner, clock), |
| 22 | device_serial_interface(mconfig, *this), |
| 23 | device_image_interface(mconfig, *this) |
| 24 | { |
| 25 | |
| 26 | } |
| 27 | |
| 28 | //------------------------------------------------- |
| 29 | // serial_image_device - destructor |
| 30 | //------------------------------------------------- |
| 31 | |
| 32 | serial_image_device::~serial_image_device() |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | //------------------------------------------------- |
| 37 | // device_config_complete - perform any |
| 38 | // operations now that the configuration is |
| 39 | // complete |
| 40 | //------------------------------------------------- |
| 41 | |
| 42 | void serial_image_device::device_config_complete() |
| 43 | { |
| 44 | // inherit a copy of the static data |
| 45 | const serial_image_interface *intf = reinterpret_cast<const serial_image_interface *>(static_config()); |
| 46 | if (intf != NULL) |
| 47 | *static_cast<serial_image_interface *>(this) = *intf; |
| 48 | |
| 49 | // or initialize to defaults if none provided |
| 50 | else |
| 51 | { |
| 52 | m_baud_rate = 2400; |
| 53 | m_data_bits = 8; |
| 54 | m_stop_bits = 2; |
| 55 | m_parity = SERIAL_PARITY_NONE; |
| 56 | m_transmit_on_start = FALSE; |
| 57 | m_tag_connected = NULL; |
| 58 | } |
| 59 | |
| 60 | // set brief and instance name |
| 61 | update_names(); |
| 62 | } |
| 63 | |
| 64 | static TIMER_CALLBACK(serial_device_baud_rate_callback) |
| 65 | { |
| 66 | reinterpret_cast<serial_image_device *>(ptr)->timer_callback(); |
| 67 | } |
| 68 | |
| 69 | /*------------------------------------------------- |
| 70 | input_callback - called when other side |
| 71 | has updated state |
| 72 | -------------------------------------------------*/ |
| 73 | void serial_image_device::input_callback(UINT8 state) |
| 74 | { |
| 75 | m_input_state = state; |
| 76 | } |
| 77 | |
| 78 | //------------------------------------------------- |
| 79 | // device_start - device-specific startup |
| 80 | //------------------------------------------------- |
| 81 | |
| 82 | void serial_image_device::device_start() |
| 83 | { |
| 84 | set_data_frame(m_data_bits, m_stop_bits,m_parity); |
| 85 | |
| 86 | m_timer = machine().scheduler().timer_alloc(FUNC(serial_device_baud_rate_callback), this); |
| 87 | |
| 88 | /* signal to other end it is clear to send! */ |
| 89 | /* data is initially high state */ |
| 90 | /* set /rts */ |
| 91 | m_connection_state |= SERIAL_STATE_RTS; |
| 92 | /* signal to other end data is ready to be accepted */ |
| 93 | /* set /dtr */ |
| 94 | m_connection_state |= SERIAL_STATE_DTR; |
| 95 | |
| 96 | set_out_data_bit(1); |
| 97 | serial_connection_out(); |
| 98 | transmit_register_reset(); |
| 99 | receive_register_reset(); |
| 100 | |
| 101 | set_transmit_state(m_transmit_on_start ? 1 :0); |
| 102 | |
| 103 | device_serial_interface *intf = NULL; |
| 104 | device_t *dev = machine().device(m_tag_connected); |
| 105 | if (dev!=NULL && dev->interface(intf)) { |
| 106 | intf->connect(this); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /*************************************************************************** |
| 111 | IMPLEMENTATION |
| 112 | ***************************************************************************/ |
| 113 | |
| 114 | /* reset position in stream */ |
| 115 | void serial_image_device::data_stream_reset(serial_data_stream *stream) |
| 116 | { |
| 117 | /* reset byte offset */ |
| 118 | stream->ByteCount= 0; |
| 119 | /* reset bit count */ |
| 120 | stream->BitCount = 0; |
| 121 | } |
| 122 | |
| 123 | /* free stream */ |
| 124 | void serial_image_device::data_stream_free(serial_data_stream *stream) |
| 125 | { |
| 126 | if (stream->pData!=NULL) |
| 127 | { |
| 128 | free(stream->pData); |
| 129 | stream->pData = NULL; |
| 130 | } |
| 131 | stream->DataLength = 0; |
| 132 | } |
| 133 | |
| 134 | /* initialise stream */ |
| 135 | void serial_image_device::data_stream_init(serial_data_stream *stream, unsigned char *pData, unsigned long DataLength) |
| 136 | { |
| 137 | stream->pData = pData; |
| 138 | stream->DataLength = DataLength; |
| 139 | data_stream_reset(stream); |
| 140 | } |
| 141 | |
| 142 | /* get a bit from input stream */ |
| 143 | |
| 144 | int serial_image_device::data_stream_get_data_bit_from_data_byte(serial_data_stream *stream) |
| 145 | { |
| 146 | int data_bit; |
| 147 | int data_byte; |
| 148 | |
| 149 | if (stream->ByteCount<stream->DataLength) |
| 150 | { |
| 151 | /* get data from buffer */ |
| 152 | data_byte= stream->pData[stream->ByteCount]; |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | /* over end of buffer, so return 0 */ |
| 157 | data_byte= 0; |
| 158 | } |
| 159 | |
| 160 | /* get bit from data */ |
| 161 | data_bit = (data_byte>>(7-stream->BitCount)) & 0x01; |
| 162 | |
| 163 | /* update bit count */ |
| 164 | stream->BitCount++; |
| 165 | /* ripple overflow onto byte count */ |
| 166 | stream->ByteCount+=stream->BitCount>>3; |
| 167 | /* lock bit count into range */ |
| 168 | stream->BitCount &=0x07; |
| 169 | |
| 170 | /* do not let it read over end of data */ |
| 171 | if (stream->ByteCount>=stream->DataLength) |
| 172 | { |
| 173 | stream->ByteCount = stream->DataLength-1; |
| 174 | } |
| 175 | |
| 176 | return data_bit; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | void serial_image_device::set_transmit_state(int state) |
| 181 | { |
| 182 | int previous_state = m_transmit_state; |
| 183 | |
| 184 | m_transmit_state = state; |
| 185 | |
| 186 | if ((state^previous_state)!=0) |
| 187 | { |
| 188 | if (state) |
| 189 | { |
| 190 | /* start timer */ |
| 191 | m_timer->adjust(attotime::zero, 0, attotime::from_hz(m_baud_rate)); |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | /* remove timer */ |
| 196 | m_timer->reset(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | |
| 202 | void serial_image_device::sent_char() |
| 203 | { |
| 204 | int bit; |
| 205 | UINT8 data_byte; |
| 206 | |
| 207 | /* generate byte to transmit */ |
| 208 | data_byte = 0; |
| 209 | for (int i=0; i< m_data_bits; i++) |
| 210 | { |
| 211 | data_byte = data_byte<<1; |
| 212 | bit = data_stream_get_data_bit_from_data_byte(&m_transmit); |
| 213 | data_byte = data_byte|bit; |
| 214 | } |
| 215 | /* setup register */ |
| 216 | transmit_register_setup(data_byte); |
| 217 | |
| 218 | logerror("serial device transmitted char: %02x\n",data_byte); |
| 219 | } |
| 220 | |
| 221 | /*------------------------------------------------- |
| 222 | timer_callback |
| 223 | -------------------------------------------------*/ |
| 224 | void serial_image_device::timer_callback() |
| 225 | { |
| 226 | /* receive data into receive register */ |
| 227 | receive_register_update_bit(get_in_data_bit()); |
| 228 | |
| 229 | if (is_receive_register_full()) |
| 230 | { |
| 231 | //logerror("SERIAL DEVICE\n"); |
| 232 | receive_register_extract(); |
| 233 | |
| 234 | logerror("serial device receive char: %02x\n",get_received_char()); |
| 235 | } |
| 236 | |
| 237 | /* is transmit empty? */ |
| 238 | if (is_transmit_register_empty()) |
| 239 | { |
| 240 | /* char has been sent, execute callback */ |
| 241 | sent_char(); |
| 242 | } |
| 243 | |
| 244 | /* other side says it is clear to send? */ |
| 245 | if (m_connection_state & SERIAL_STATE_CTS) |
| 246 | { |
| 247 | /* send bit */ |
| 248 | transmit_register_send_bit(); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | int serial_image_device::load_internal(unsigned char **ptr, int *pDataSize) |
| 253 | { |
| 254 | int datasize; |
| 255 | unsigned char *data; |
| 256 | |
| 257 | /* get file size */ |
| 258 | datasize = length(); |
| 259 | |
| 260 | if (datasize!=0) |
| 261 | { |
| 262 | /* malloc memory for this data */ |
| 263 | data = (unsigned char *)malloc(datasize); |
| 264 | |
| 265 | if (data!=NULL) |
| 266 | { |
| 267 | /* read whole file */ |
| 268 | fread(data, datasize); |
| 269 | |
| 270 | *ptr = data; |
| 271 | *pDataSize = datasize; |
| 272 | |
| 273 | logerror("File loaded!\r\n"); |
| 274 | |
| 275 | /* ok! */ |
| 276 | return 1; |
| 277 | } |
| 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /*------------------------------------------------- |
| 283 | call_load |
| 284 | -------------------------------------------------*/ |
| 285 | bool serial_image_device::call_load() |
| 286 | { |
| 287 | int data_length; |
| 288 | unsigned char *data; |
| 289 | /* load file and setup transmit data */ |
| 290 | if (load_internal(&data, &data_length)) |
| 291 | { |
| 292 | data_stream_init(&m_transmit, data, data_length); |
| 293 | return IMAGE_INIT_PASS; |
| 294 | } |
| 295 | |
| 296 | return IMAGE_INIT_FAIL; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /*------------------------------------------------- |
| 301 | call_unload |
| 302 | -------------------------------------------------*/ |
| 303 | void serial_image_device::call_unload() |
| 304 | { |
| 305 | /* stop transmit */ |
| 306 | set_transmit_state(0); |
| 307 | |
| 308 | /* free streams */ |
| 309 | data_stream_free(&m_transmit); |
| 310 | data_stream_free(&m_receive); |
| 311 | } |
| 312 | |
src/mess/machine/serial.c
| r12777 | r12778 | |
| 1 | | /* internal serial transmission */ |
| 2 | | |
| 3 | | /* select a file on host filesystem to transfer using serial method, |
| 4 | | setup serial interface software in driver and let the transfer begin */ |
| 5 | | |
| 6 | | /* this is used in the Amstrad NC Notepad emulation */ |
| 7 | | |
| 8 | | |
| 9 | | |
| 10 | | /* |
| 11 | | |
| 12 | | the output starts at 1 level. It changes to 0 when the start bit has been transmitted. |
| 13 | | This therefore signals that data is following. |
| 14 | | When all data bits have been received, stop bits are transmitted with a value of 1. |
| 15 | | |
| 16 | | i8251 expects this in asynchronous mode: |
| 17 | | packet format: |
| 18 | | |
| 19 | | bit count function value |
| 20 | | 1 start bit 0 |
| 21 | | note 1 data bits x |
| 22 | | note 2 parity bit x |
| 23 | | note 3 stop bits 1 |
| 24 | | |
| 25 | | Note: |
| 26 | | 1. Data size can be defined (usual value is 8) |
| 27 | | 2. Parity bit (if parity is set to odd or even). Value of bit |
| 28 | | is defined by data parity. |
| 29 | | 3. There should be at least 1 stop bit. |
| 30 | | */ |
| 31 | | |
| 32 | | #include "emu.h" |
| 33 | | #include "serial.h" |
| 34 | | |
| 35 | | |
| 36 | | #define VERBOSE 0 |
| 37 | | #define LOG(x) do { if (VERBOSE) logerror x; } while (0) |
| 38 | | |
| 39 | | /* a read/write bit stream. used to transmit data and to receive data */ |
| 40 | | typedef struct _data_stream data_stream; |
| 41 | | struct _data_stream |
| 42 | | { |
| 43 | | /* pointer to buffer */ |
| 44 | | unsigned char *pData; |
| 45 | | /* length of buffer */ |
| 46 | | unsigned long DataLength; |
| 47 | | |
| 48 | | /* bit offset within current byte */ |
| 49 | | unsigned long BitCount; |
| 50 | | /* byte offset within data */ |
| 51 | | unsigned long ByteCount; |
| 52 | | }; |
| 53 | | |
| 54 | | typedef struct _serial_t serial_t; |
| 55 | | struct _serial_t |
| 56 | | { |
| 57 | | /* transmit data bit-stream */ |
| 58 | | data_stream transmit; |
| 59 | | /* receive data bit-stream */ |
| 60 | | data_stream receive; |
| 61 | | |
| 62 | | /* register to receive data */ |
| 63 | | serial_receive_register receive_reg; |
| 64 | | /* register to transmit data */ |
| 65 | | serial_transmit_register transmit_reg; |
| 66 | | |
| 67 | | /* connection to transmit/receive data over */ |
| 68 | | serial_connection connection; |
| 69 | | |
| 70 | | /* data form to transmit/receive */ |
| 71 | | data_form form; |
| 72 | | |
| 73 | | int transmit_state; |
| 74 | | |
| 75 | | /* baud rate */ |
| 76 | | unsigned long BaudRate; |
| 77 | | |
| 78 | | /* baud rate timer */ |
| 79 | | emu_timer *timer; |
| 80 | | }; |
| 81 | | |
| 82 | | INLINE serial_t *get_safe_token(device_t *device) |
| 83 | | { |
| 84 | | assert(device != NULL); |
| 85 | | //assert(device->type() == SERIAL); |
| 86 | | |
| 87 | | return (serial_t *)downcast<legacy_device_base *>(device)->token(); |
| 88 | | } |
| 89 | | |
| 90 | | |
| 91 | | /* the serial streams */ |
| 92 | | static TIMER_CALLBACK(serial_device_baud_rate_callback); |
| 93 | | |
| 94 | | |
| 95 | | /*********************************************************/ |
| 96 | | |
| 97 | | static unsigned char serial_parity_table[256]; |
| 98 | | |
| 99 | | void serial_helper_setup(void) |
| 100 | | { |
| 101 | | int i; |
| 102 | | |
| 103 | | /* if sum of all bits in the byte is even, then the data |
| 104 | | has even parity, otherwise it has odd parity */ |
| 105 | | for (i=0; i<256; i++) |
| 106 | | { |
| 107 | | int data; |
| 108 | | int sum; |
| 109 | | int b; |
| 110 | | |
| 111 | | sum = 0; |
| 112 | | data = i; |
| 113 | | |
| 114 | | for (b=0; b<8; b++) |
| 115 | | { |
| 116 | | sum+=data & 0x01; |
| 117 | | |
| 118 | | data = data>>1; |
| 119 | | } |
| 120 | | |
| 121 | | serial_parity_table[i] = sum & 0x01; |
| 122 | | } |
| 123 | | } |
| 124 | | |
| 125 | | static unsigned char serial_helper_get_parity(unsigned char data) |
| 126 | | { |
| 127 | | return serial_parity_table[data & 0x0ff]; |
| 128 | | } |
| 129 | | |
| 130 | | static void serial_device_in_callback(running_machine &machine, int id, unsigned long status) |
| 131 | | { |
| 132 | | /* serial_t *ser = get_safe_token(device); |
| 133 | | |
| 134 | | ser->connection.input_state = status;*/ |
| 135 | | } |
| 136 | | |
| 137 | | /***** SERIAL DEVICE ******/ |
| 138 | | void serial_device_setup(device_t *device, int baud_rate, int num_data_bits, int stop_bit_count, int parity_code) |
| 139 | | { |
| 140 | | serial_t *ser = get_safe_token(device); |
| 141 | | |
| 142 | | ser->BaudRate = baud_rate; |
| 143 | | ser->form.word_length = num_data_bits; |
| 144 | | ser->form.stop_bit_count = stop_bit_count; |
| 145 | | ser->form.parity = parity_code; |
| 146 | | ser->timer = device->machine().scheduler().timer_alloc(FUNC(serial_device_baud_rate_callback), (void *)device); |
| 147 | | |
| 148 | | serial_connection_init(device->machine(),&ser->connection); |
| 149 | | serial_connection_set_in_callback(device->machine(),&ser->connection, serial_device_in_callback); |
| 150 | | |
| 151 | | /* signal to other end it is clear to send! */ |
| 152 | | /* data is initially high state */ |
| 153 | | /* set /rts */ |
| 154 | | ser->connection.State |= SERIAL_STATE_RTS; |
| 155 | | /* signal to other end data is ready to be accepted */ |
| 156 | | /* set /dtr */ |
| 157 | | ser->connection.State |= SERIAL_STATE_DTR; |
| 158 | | set_out_data_bit(ser->connection.State, 1); |
| 159 | | serial_connection_out(device->machine(),&ser->connection); |
| 160 | | transmit_register_reset(&ser->transmit_reg); |
| 161 | | receive_register_reset(&ser->receive_reg); |
| 162 | | receive_register_setup(&ser->receive_reg, &ser->form); |
| 163 | | } |
| 164 | | |
| 165 | | |
| 166 | | unsigned long serial_device_get_state(device_t *device) |
| 167 | | { |
| 168 | | serial_t *ser = get_safe_token(device); |
| 169 | | |
| 170 | | return ser->connection.State; |
| 171 | | } |
| 172 | | |
| 173 | | void serial_device_set_transmit_state(device_t *device, int state) |
| 174 | | { |
| 175 | | int previous_state; |
| 176 | | serial_t *ser = get_safe_token(device); |
| 177 | | |
| 178 | | previous_state = ser->transmit_state; |
| 179 | | |
| 180 | | ser->transmit_state = state; |
| 181 | | |
| 182 | | if ((state^previous_state)!=0) |
| 183 | | { |
| 184 | | if (state) |
| 185 | | { |
| 186 | | /* start timer */ |
| 187 | | ser->timer->adjust(attotime::zero, 0, attotime::from_hz(ser->BaudRate)); |
| 188 | | } |
| 189 | | else |
| 190 | | { |
| 191 | | /* remove timer */ |
| 192 | | ser->timer->reset(); |
| 193 | | } |
| 194 | | } |
| 195 | | |
| 196 | | } |
| 197 | | |
| 198 | | /* get a bit from input stream */ |
| 199 | | static int data_stream_get_data_bit_from_data_byte(data_stream *stream) |
| 200 | | { |
| 201 | | int data_bit; |
| 202 | | int data_byte; |
| 203 | | |
| 204 | | if (stream->ByteCount<stream->DataLength) |
| 205 | | { |
| 206 | | /* get data from buffer */ |
| 207 | | data_byte= stream->pData[stream->ByteCount]; |
| 208 | | } |
| 209 | | else |
| 210 | | { |
| 211 | | /* over end of buffer, so return 0 */ |
| 212 | | data_byte= 0; |
| 213 | | } |
| 214 | | |
| 215 | | /* get bit from data */ |
| 216 | | data_bit = (data_byte>>(7-stream->BitCount)) & 0x01; |
| 217 | | |
| 218 | | /* update bit count */ |
| 219 | | stream->BitCount++; |
| 220 | | /* ripple overflow onto byte count */ |
| 221 | | stream->ByteCount+=stream->BitCount>>3; |
| 222 | | /* lock bit count into range */ |
| 223 | | stream->BitCount &=0x07; |
| 224 | | |
| 225 | | /* do not let it read over end of data */ |
| 226 | | if (stream->ByteCount>=stream->DataLength) |
| 227 | | { |
| 228 | | stream->ByteCount = stream->DataLength-1; |
| 229 | | } |
| 230 | | |
| 231 | | return data_bit; |
| 232 | | } |
| 233 | | |
| 234 | | |
| 235 | | void receive_register_setup(serial_receive_register *receive, data_form *data_form) |
| 236 | | { |
| 237 | | receive->bit_count = data_form->word_length + data_form->stop_bit_count; |
| 238 | | |
| 239 | | if (data_form->parity != SERIAL_PARITY_NONE) |
| 240 | | { |
| 241 | | receive->bit_count++; |
| 242 | | } |
| 243 | | } |
| 244 | | |
| 245 | | |
| 246 | | /* this is generic code to be used in serial chip implementations */ |
| 247 | | /* the majority of serial chips work in the same way and this code will work */ |
| 248 | | /* for them */ |
| 249 | | |
| 250 | | /* receive a bit */ |
| 251 | | void receive_register_update_bit(serial_receive_register *receive, int bit) |
| 252 | | { |
| 253 | | int previous_bit; |
| 254 | | |
| 255 | | LOG(("receive register receive bit: %1x\n",bit)); |
| 256 | | previous_bit = receive->register_data & 1; |
| 257 | | |
| 258 | | /* shift previous bit 7 out */ |
| 259 | | receive->register_data = receive->register_data<<1; |
| 260 | | /* shift new bit in */ |
| 261 | | receive->register_data = (receive->register_data & 0xfffe) | bit; |
| 262 | | /* update bit count received */ |
| 263 | | receive->bit_count_received++; |
| 264 | | |
| 265 | | /* asyncrhonouse mode */ |
| 266 | | if (receive->flags & RECEIVE_REGISTER_WAITING_FOR_START_BIT) |
| 267 | | { |
| 268 | | /* the previous bit is stored in uart.receive char bit 0 */ |
| 269 | | /* has the bit state changed? */ |
| 270 | | if (((previous_bit ^ bit) & 0x01)!=0) |
| 271 | | { |
| 272 | | /* yes */ |
| 273 | | if (bit==0) |
| 274 | | { |
| 275 | | //logerror("receive register saw start bit\n"); |
| 276 | | |
| 277 | | /* seen start bit! */ |
| 278 | | /* not waiting for start bit now! */ |
| 279 | | receive->flags &=~RECEIVE_REGISTER_WAITING_FOR_START_BIT; |
| 280 | | receive->flags |=RECEIVE_REGISTER_SYNCHRONISED; |
| 281 | | /* reset bit count received */ |
| 282 | | receive->bit_count_received = 0; |
| 283 | | } |
| 284 | | } |
| 285 | | } |
| 286 | | else |
| 287 | | if (receive->flags & RECEIVE_REGISTER_SYNCHRONISED) |
| 288 | | { |
| 289 | | /* received all bits? */ |
| 290 | | if (receive->bit_count_received==receive->bit_count) |
| 291 | | { |
| 292 | | receive->bit_count_received = 0; |
| 293 | | receive->flags &=~RECEIVE_REGISTER_SYNCHRONISED; |
| 294 | | receive->flags |= RECEIVE_REGISTER_WAITING_FOR_START_BIT; |
| 295 | | //logerror("receive register full\n"); |
| 296 | | receive->flags |= RECEIVE_REGISTER_FULL; |
| 297 | | } |
| 298 | | } |
| 299 | | } |
| 300 | | |
| 301 | | void receive_register_reset(serial_receive_register *receive_reg) |
| 302 | | { |
| 303 | | receive_reg->bit_count_received = 0; |
| 304 | | receive_reg->flags &=~RECEIVE_REGISTER_FULL; |
| 305 | | receive_reg->flags &=~RECEIVE_REGISTER_SYNCHRONISED; |
| 306 | | receive_reg->flags |= RECEIVE_REGISTER_WAITING_FOR_START_BIT; |
| 307 | | } |
| 308 | | |
| 309 | | void receive_register_extract(serial_receive_register *receive_reg, data_form *data_form) |
| 310 | | { |
| 311 | | unsigned long data_shift; |
| 312 | | UINT8 data; |
| 313 | | |
| 314 | | receive_register_reset(receive_reg); |
| 315 | | |
| 316 | | data_shift = 0; |
| 317 | | |
| 318 | | /* if parity is even or odd, there should be a parity bit in the stream! */ |
| 319 | | if (data_form->parity!=SERIAL_PARITY_NONE) |
| 320 | | { |
| 321 | | data_shift++; |
| 322 | | } |
| 323 | | |
| 324 | | data_shift+=data_form->stop_bit_count; |
| 325 | | |
| 326 | | /* strip off stop bits and parity */ |
| 327 | | data = receive_reg->register_data>>data_shift; |
| 328 | | |
| 329 | | /* mask off other bits so data byte has 0's in unused bits */ |
| 330 | | data = data & (0x0ff |
| 331 | | >> |
| 332 | | (8-(data_form->word_length))); |
| 333 | | |
| 334 | | receive_reg->byte_received = data; |
| 335 | | |
| 336 | | /* parity enable? */ |
| 337 | | switch (data_form->parity) |
| 338 | | { |
| 339 | | case SERIAL_PARITY_NONE: |
| 340 | | break; |
| 341 | | |
| 342 | | /* check parity */ |
| 343 | | case SERIAL_PARITY_ODD: |
| 344 | | case SERIAL_PARITY_EVEN: |
| 345 | | { |
| 346 | | //unsigned char computed_parity; |
| 347 | | //unsigned char parity_received; |
| 348 | | |
| 349 | | /* get state of parity bit received */ |
| 350 | | //parity_received = (receive_reg->register_data>>data_form->stop_bit_count) & 0x01; |
| 351 | | |
| 352 | | /* compute parity for received bits */ |
| 353 | | //computed_parity = serial_helper_get_parity(data); |
| 354 | | |
| 355 | | if (data_form->parity == SERIAL_PARITY_ODD) |
| 356 | | { |
| 357 | | /* odd parity */ |
| 358 | | |
| 359 | | |
| 360 | | } |
| 361 | | else |
| 362 | | { |
| 363 | | /* even parity */ |
| 364 | | |
| 365 | | |
| 366 | | } |
| 367 | | |
| 368 | | } |
| 369 | | break; |
| 370 | | } |
| 371 | | } |
| 372 | | |
| 373 | | /***** TRANSMIT REGISTER *****/ |
| 374 | | |
| 375 | | void transmit_register_reset(serial_transmit_register *transmit_reg) |
| 376 | | { |
| 377 | | transmit_reg->flags |=TRANSMIT_REGISTER_EMPTY; |
| 378 | | } |
| 379 | | |
| 380 | | /* used to construct data in stream format */ |
| 381 | | static void transmit_register_add_bit(serial_transmit_register *transmit_reg, int bit) |
| 382 | | { |
| 383 | | /* combine bit */ |
| 384 | | transmit_reg->register_data = transmit_reg->register_data<<1; |
| 385 | | transmit_reg->register_data &=~1; |
| 386 | | transmit_reg->register_data|=(bit & 0x01); |
| 387 | | transmit_reg->bit_count++; |
| 388 | | } |
| 389 | | |
| 390 | | |
| 391 | | /* generate data in stream format ready for transfer */ |
| 392 | | void transmit_register_setup(serial_transmit_register *transmit_reg, data_form *data_form,unsigned char data_byte) |
| 393 | | { |
| 394 | | int i; |
| 395 | | unsigned char transmit_data; |
| 396 | | |
| 397 | | |
| 398 | | transmit_reg->bit_count_transmitted = 0; |
| 399 | | transmit_reg->bit_count = 0; |
| 400 | | transmit_reg->flags &=~TRANSMIT_REGISTER_EMPTY; |
| 401 | | |
| 402 | | /* start bit */ |
| 403 | | transmit_register_add_bit(transmit_reg,0); |
| 404 | | |
| 405 | | /* data bits */ |
| 406 | | transmit_data = data_byte; |
| 407 | | for (i=0; i<data_form->word_length; i++) |
| 408 | | { |
| 409 | | int databit; |
| 410 | | |
| 411 | | /* get bit from data */ |
| 412 | | databit = (transmit_data>>(data_form->word_length-1)) & 0x01; |
| 413 | | /* add bit to formatted byte */ |
| 414 | | transmit_register_add_bit(transmit_reg, databit); |
| 415 | | transmit_data = transmit_data<<1; |
| 416 | | } |
| 417 | | |
| 418 | | /* parity */ |
| 419 | | if (data_form->parity!=SERIAL_PARITY_NONE) |
| 420 | | { |
| 421 | | /* odd or even parity */ |
| 422 | | unsigned char parity; |
| 423 | | |
| 424 | | /* get parity */ |
| 425 | | /* if parity = 0, data has even parity - i.e. there is an even number of one bits in the data */ |
| 426 | | /* if parity = 1, data has odd parity - i.e. there is an odd number of one bits in the data */ |
| 427 | | parity = serial_helper_get_parity(data_byte); |
| 428 | | |
| 429 | | transmit_register_add_bit(transmit_reg, parity); |
| 430 | | } |
| 431 | | |
| 432 | | /* stop bit(s) */ |
| 433 | | for (i=0; i<data_form->stop_bit_count; i++) |
| 434 | | { |
| 435 | | transmit_register_add_bit(transmit_reg,1); |
| 436 | | } |
| 437 | | } |
| 438 | | |
| 439 | | |
| 440 | | /* get a bit from the transmit register */ |
| 441 | | static int transmit_register_get_data_bit(serial_transmit_register *transmit_reg) |
| 442 | | { |
| 443 | | int bit; |
| 444 | | |
| 445 | | bit = (transmit_reg->register_data>> |
| 446 | | (transmit_reg->bit_count - 1 - |
| 447 | | transmit_reg->bit_count_transmitted)) & 0x01; |
| 448 | | |
| 449 | | transmit_reg->bit_count_transmitted++; |
| 450 | | |
| 451 | | /* have all bits of this stream formatted byte been sent? */ |
| 452 | | if (transmit_reg->bit_count_transmitted==transmit_reg->bit_count) |
| 453 | | { |
| 454 | | /* yes - generate a new byte to send */ |
| 455 | | transmit_reg->flags |= TRANSMIT_REGISTER_EMPTY; |
| 456 | | } |
| 457 | | |
| 458 | | return bit; |
| 459 | | } |
| 460 | | |
| 461 | | |
| 462 | | void transmit_register_send_bit(running_machine &machine, serial_transmit_register *transmit_reg, serial_connection *connection) |
| 463 | | { |
| 464 | | int data; |
| 465 | | |
| 466 | | data = transmit_register_get_data_bit(transmit_reg); |
| 467 | | |
| 468 | | /* set tx data bit */ |
| 469 | | set_out_data_bit(connection->State, data); |
| 470 | | |
| 471 | | /* state out through connection */ |
| 472 | | serial_connection_out(machine, connection); |
| 473 | | } |
| 474 | | |
| 475 | | static void serial_protocol_none_sent_char(device_t *device) |
| 476 | | { |
| 477 | | int i; |
| 478 | | int bit; |
| 479 | | unsigned char data_byte; |
| 480 | | serial_t *ser = get_safe_token(device); |
| 481 | | |
| 482 | | /* generate byte to transmit */ |
| 483 | | data_byte = 0; |
| 484 | | for (i=0; i<ser->form.word_length; i++) |
| 485 | | { |
| 486 | | data_byte = data_byte<<1; |
| 487 | | bit = data_stream_get_data_bit_from_data_byte(&ser->transmit); |
| 488 | | data_byte = data_byte|bit; |
| 489 | | } |
| 490 | | /* setup register */ |
| 491 | | transmit_register_setup(&ser->transmit_reg,&ser->form, data_byte); |
| 492 | | |
| 493 | | logerror("serial device transmitted char: %02x\n",data_byte); |
| 494 | | } |
| 495 | | |
| 496 | | static TIMER_CALLBACK(serial_device_baud_rate_callback) |
| 497 | | { |
| 498 | | serial_t *ser = get_safe_token((device_t*)ptr); |
| 499 | | |
| 500 | | /* receive data into receive register */ |
| 501 | | receive_register_update_bit(&ser->receive_reg, get_in_data_bit(ser->connection.input_state)); |
| 502 | | |
| 503 | | if (ser->receive_reg.flags & RECEIVE_REGISTER_FULL) |
| 504 | | { |
| 505 | | //logerror("SERIAL DEVICE\n"); |
| 506 | | receive_register_extract(&ser->receive_reg, &ser->form); |
| 507 | | |
| 508 | | logerror("serial device receive char: %02x\n",ser->receive_reg.byte_received); |
| 509 | | } |
| 510 | | |
| 511 | | /* is transmit empty? */ |
| 512 | | if (ser->transmit_reg.flags & TRANSMIT_REGISTER_EMPTY) |
| 513 | | { |
| 514 | | /* char has been sent, execute callback */ |
| 515 | | serial_protocol_none_sent_char((device_t*)ptr); |
| 516 | | } |
| 517 | | |
| 518 | | /* other side says it is clear to send? */ |
| 519 | | if (ser->connection.input_state & SERIAL_STATE_CTS) |
| 520 | | { |
| 521 | | /* send bit */ |
| 522 | | transmit_register_send_bit(machine, &ser->transmit_reg, &ser->connection); |
| 523 | | } |
| 524 | | } |
| 525 | | |
| 526 | | /* connect the specified connection to this serial device */ |
| 527 | | void serial_device_connect(device_t *device, serial_connection *connection) |
| 528 | | { |
| 529 | | serial_t *ser = get_safe_token(device); |
| 530 | | serial_connection_link(device->machine(), connection, &ser->connection); |
| 531 | | } |
| 532 | | |
| 533 | | |
| 534 | | /* load image */ |
| 535 | | static int serial_device_load_internal(device_image_interface &image, unsigned char **ptr, int *pDataSize) |
| 536 | | { |
| 537 | | int datasize; |
| 538 | | unsigned char *data; |
| 539 | | |
| 540 | | /* get file size */ |
| 541 | | datasize = image.length(); |
| 542 | | |
| 543 | | if (datasize!=0) |
| 544 | | { |
| 545 | | /* malloc memory for this data */ |
| 546 | | data = (unsigned char *)malloc(datasize); |
| 547 | | |
| 548 | | if (data!=NULL) |
| 549 | | { |
| 550 | | /* read whole file */ |
| 551 | | image.fread(data, datasize); |
| 552 | | |
| 553 | | *ptr = data; |
| 554 | | *pDataSize = datasize; |
| 555 | | |
| 556 | | logerror("File loaded!\r\n"); |
| 557 | | |
| 558 | | /* ok! */ |
| 559 | | return 1; |
| 560 | | } |
| 561 | | } |
| 562 | | return 0; |
| 563 | | } |
| 564 | | |
| 565 | | /* reset position in stream */ |
| 566 | | static void data_stream_reset(data_stream *stream) |
| 567 | | { |
| 568 | | /* reset byte offset */ |
| 569 | | stream->ByteCount= 0; |
| 570 | | /* reset bit count */ |
| 571 | | stream->BitCount = 0; |
| 572 | | } |
| 573 | | |
| 574 | | /* free stream */ |
| 575 | | static void data_stream_free(data_stream *stream) |
| 576 | | { |
| 577 | | if (stream->pData!=NULL) |
| 578 | | { |
| 579 | | free(stream->pData); |
| 580 | | stream->pData = NULL; |
| 581 | | } |
| 582 | | stream->DataLength = 0; |
| 583 | | } |
| 584 | | |
| 585 | | /* initialise stream */ |
| 586 | | static void data_stream_init(data_stream *stream, unsigned char *pData, unsigned long DataLength) |
| 587 | | { |
| 588 | | stream->pData = pData; |
| 589 | | stream->DataLength = DataLength; |
| 590 | | data_stream_reset(stream); |
| 591 | | } |
| 592 | | |
| 593 | | DEVICE_START(serial) |
| 594 | | { |
| 595 | | //serial_t *ser = get_safe_token(device); |
| 596 | | } |
| 597 | | |
| 598 | | DEVICE_RESET(serial) |
| 599 | | { |
| 600 | | // serial_t *ser = get_safe_token(device); |
| 601 | | } |
| 602 | | |
| 603 | | DEVICE_IMAGE_LOAD(serial) |
| 604 | | { |
| 605 | | int data_length; |
| 606 | | unsigned char *data; |
| 607 | | device_t *device = &image.device(); |
| 608 | | |
| 609 | | serial_t *ser = get_safe_token(device); |
| 610 | | |
| 611 | | /* load file and setup transmit data */ |
| 612 | | if (serial_device_load_internal(image, &data, &data_length)) |
| 613 | | { |
| 614 | | data_stream_init(&ser->transmit, data, data_length); |
| 615 | | return IMAGE_INIT_PASS; |
| 616 | | } |
| 617 | | |
| 618 | | return IMAGE_INIT_FAIL; |
| 619 | | } |
| 620 | | |
| 621 | | |
| 622 | | DEVICE_IMAGE_UNLOAD(serial) |
| 623 | | { |
| 624 | | device_t *device = &image.device(); |
| 625 | | serial_t *ser = get_safe_token(device); |
| 626 | | |
| 627 | | /* stop transmit */ |
| 628 | | serial_device_set_transmit_state(device, 0); |
| 629 | | /* free streams */ |
| 630 | | data_stream_free(&ser->transmit); |
| 631 | | data_stream_free(&ser->receive); |
| 632 | | } |
| 633 | | |
| 634 | | |
| 635 | | DEVICE_GET_INFO( serial ) |
| 636 | | { |
| 637 | | switch ( state ) |
| 638 | | { |
| 639 | | case DEVINFO_INT_TOKEN_BYTES: info->i = sizeof(serial_t); break; |
| 640 | | case DEVINFO_INT_INLINE_CONFIG_BYTES: info->i = 0; break; |
| 641 | | case DEVINFO_INT_IMAGE_TYPE: info->i = IO_SERIAL; break; |
| 642 | | case DEVINFO_INT_IMAGE_READABLE: info->i = 1; break; |
| 643 | | case DEVINFO_INT_IMAGE_WRITEABLE: info->i = 1; break; |
| 644 | | case DEVINFO_INT_IMAGE_CREATABLE: info->i = 1; break; |
| 645 | | |
| 646 | | case DEVINFO_FCT_START: info->start = DEVICE_START_NAME( serial ); break; |
| 647 | | case DEVINFO_FCT_RESET: info->reset = DEVICE_RESET_NAME( serial ); break; |
| 648 | | case DEVINFO_FCT_IMAGE_LOAD: info->f = (genf *) DEVICE_IMAGE_LOAD_NAME( serial ); break; |
| 649 | | case DEVINFO_FCT_IMAGE_UNLOAD: info->f = (genf *) DEVICE_IMAGE_UNLOAD_NAME(serial ); break; |
| 650 | | case DEVINFO_STR_NAME: strcpy( info->s, "Serial port"); break; |
| 651 | | case DEVINFO_STR_FAMILY: strcpy(info->s, "Serial port"); break; |
| 652 | | case DEVINFO_STR_IMAGE_FILE_EXTENSIONS: strcpy(info->s, ""); break; |
| 653 | | case DEVINFO_STR_VERSION: strcpy(info->s, "1.0"); break; |
| 654 | | case DEVINFO_STR_SOURCE_FILE: strcpy(info->s, __FILE__); break; |
| 655 | | case DEVINFO_STR_CREDITS: strcpy(info->s, "Copyright the MESS Team"); break; |
| 656 | | } |
| 657 | | } |
| 658 | | |
| 659 | | /*******************************************************************************/ |
| 660 | | /*******************************************************************************/ |
| 661 | | /********* SERIAL CONNECTION ***********/ |
| 662 | | |
| 663 | | |
| 664 | | /* this converts state at this end to a state the other end can accept */ |
| 665 | | /* e.g. CTS at this end becomes RTS at other end. |
| 666 | | RTS at this end becomes CTS at other end. |
| 667 | | TX at this end becomes RX at other end. |
| 668 | | RX at this end becomes TX at other end. |
| 669 | | etc |
| 670 | | |
| 671 | | The same thing is done inside the serial null-terminal lead */ |
| 672 | | |
| 673 | | static unsigned long serial_connection_spin_bits(unsigned long input_status) |
| 674 | | { |
| 675 | | |
| 676 | | return |
| 677 | | /* cts -> rts */ |
| 678 | | (((input_status & 0x01)<<1) | |
| 679 | | /* rts -> cts */ |
| 680 | | ((input_status>>1) & 0x01) | |
| 681 | | /* dsr -> dtr */ |
| 682 | | (((input_status>>2) & 0x01)<<3) | |
| 683 | | /* dtr -> dsr */ |
| 684 | | (((input_status>>3) & 0x01)<<2) | |
| 685 | | /* rx -> tx */ |
| 686 | | (((input_status>>4) & 0x01)<<5) | |
| 687 | | /* tx -> rx */ |
| 688 | | (((input_status>>5) & 0x01)<<4)); |
| 689 | | } |
| 690 | | |
| 691 | | |
| 692 | | /* setup callbacks for connection */ |
| 693 | | void serial_connection_init(running_machine &machine, serial_connection *connection) |
| 694 | | { |
| 695 | | connection->out_callback = NULL; |
| 696 | | connection->in_callback = NULL; |
| 697 | | } |
| 698 | | |
| 699 | | /* set callback which will be executed when in status has changed */ |
| 700 | | void serial_connection_set_in_callback(running_machine &machine, serial_connection *connection, void (*in_cb)(running_machine &machine, int id, unsigned long status)) |
| 701 | | { |
| 702 | | connection->in_callback = in_cb; |
| 703 | | } |
| 704 | | |
| 705 | | /* output new state through connection */ |
| 706 | | void serial_connection_out(running_machine &machine, serial_connection *connection) |
| 707 | | { |
| 708 | | if (connection->out_callback!=NULL) |
| 709 | | { |
| 710 | | unsigned long state_at_other_end; |
| 711 | | |
| 712 | | state_at_other_end = serial_connection_spin_bits(connection->State); |
| 713 | | |
| 714 | | connection->out_callback(machine, connection->id, state_at_other_end); |
| 715 | | } |
| 716 | | } |
| 717 | | |
| 718 | | /* join two serial connections together */ |
| 719 | | void serial_connection_link(running_machine &machine, serial_connection *connection_a, serial_connection *connection_b) |
| 720 | | { |
| 721 | | /* both connections should have their in connection setup! */ |
| 722 | | /* the in connection is the callback they use to update their state based |
| 723 | | on the output from the other side */ |
| 724 | | connection_a->out_callback = connection_b->in_callback; |
| 725 | | connection_b->out_callback = connection_a->in_callback; |
| 726 | | |
| 727 | | /* let b know the state of a */ |
| 728 | | serial_connection_out(machine,connection_a); |
| 729 | | /* let a know the state of b */ |
| 730 | | serial_connection_out(machine,connection_b); |
| 731 | | } |
| 732 | | |
| 733 | | DEFINE_LEGACY_IMAGE_DEVICE(SERIAL, serial); |
src/mess/machine/serial.h
| r12777 | r12778 | |
| 1 | | /***************************************************************************** |
| 2 | | * |
| 3 | | * machine/serial.h |
| 4 | | * |
| 5 | | * internal serial transmission |
| 6 | | * |
| 7 | | * This code is used to transmit a file stored on the host filesystem |
| 8 | | * (e.g. PC harddrive) to an emulated system. |
| 9 | | * |
| 10 | | * The file is converted into a serial bit-stream which can be received |
| 11 | | * by the emulated serial chip in the emulated system. |
| 12 | | * |
| 13 | | * The file can be transmitted using different protocols. |
| 14 | | * |
| 15 | | * A and B are two computers linked with a serial connection |
| 16 | | * A and B can transmit and receive data, through the same connection |
| 17 | | * |
| 18 | | * These flags apply to A and B, and give the state of the input & output |
| 19 | | * signals at each side. |
| 20 | | * |
| 21 | | ****************************************************************************/ |
| 22 | | |
| 23 | | #ifndef SERIAL_H_ |
| 24 | | #define SERIAL_H_ |
| 25 | | |
| 26 | | |
| 27 | | /*******************************************************************************/ |
| 28 | | /**** SERIAL CONNECTION ***/ |
| 29 | | |
| 30 | | |
| 31 | | /* this structure represents a serial connection */ |
| 32 | | typedef struct _serial_connection serial_connection; |
| 33 | | struct _serial_connection |
| 34 | | { |
| 35 | | int id; |
| 36 | | /* state of this side */ |
| 37 | | unsigned long State; |
| 38 | | |
| 39 | | /* state of other side - store here */ |
| 40 | | unsigned long input_state; |
| 41 | | |
| 42 | | /* this callback is executed when this side has refreshed it's state, |
| 43 | | to let the other end know */ |
| 44 | | void (*out_callback)(running_machine &machine, int id, unsigned long state); |
| 45 | | /* this callback is executed when the other side has refreshed it's state, |
| 46 | | to let the other end know */ |
| 47 | | void (*in_callback)(running_machine &machine, int id, unsigned long state); |
| 48 | | }; |
| 49 | | |
| 50 | | /* this macro is used to extract the received data from the status */ |
| 51 | | #define get_in_data_bit(x) ((x & SERIAL_STATE_RX_DATA)>>4) |
| 52 | | |
| 53 | | /* this macro is used to set the transmitted data in the status */ |
| 54 | | #define set_out_data_bit(x, data) \ |
| 55 | | x&=~SERIAL_STATE_TX_DATA; \ |
| 56 | | x|=(data<<5) |
| 57 | | |
| 58 | | |
| 59 | | |
| 60 | | |
| 61 | | /*----------- defined in machine/serial.c -----------*/ |
| 62 | | |
| 63 | | /* setup out and in callbacks */ |
| 64 | | void serial_connection_init(running_machine &machine, serial_connection *connection); |
| 65 | | |
| 66 | | /* set callback which will be executed when in status has changed */ |
| 67 | | void serial_connection_set_in_callback(running_machine &machine, serial_connection *connection, void (*in_cb)(running_machine &machine, int id, unsigned long status)); |
| 68 | | |
| 69 | | /* output status, if callback is setup it will be executed with the new status */ |
| 70 | | void serial_connection_out(running_machine &machine, serial_connection *connection); |
| 71 | | |
| 72 | | /* join two serial connections */ |
| 73 | | void serial_connection_link(running_machine &machine, serial_connection *connection_a, serial_connection *connection_b); |
| 74 | | |
| 75 | | |
| 76 | | /*******************************************************************************/ |
| 77 | | |
| 78 | | /* form of data being transmitted and received */ |
| 79 | | typedef struct _data_form data_form; |
| 80 | | struct _data_form |
| 81 | | { |
| 82 | | /* length of word in bits */ |
| 83 | | unsigned long word_length; |
| 84 | | /* parity state */ |
| 85 | | unsigned long parity; |
| 86 | | /* number of stop bits */ |
| 87 | | unsigned long stop_bit_count; |
| 88 | | }; |
| 89 | | |
| 90 | | /*******************************************************************************/ |
| 91 | | |
| 92 | | /*******************************************************************************/ |
| 93 | | /**** RECEIVE AND TRANSMIT GENERIC CODE ****/ |
| 94 | | |
| 95 | | /* this can be used by most of the serial chip implementations, |
| 96 | | because they all work in roughly the same way. |
| 97 | | There is generic code to send and receive data in the specified form */ |
| 98 | | |
| 99 | | /* receive is waiting for start bit. The transition from high-low indicates |
| 100 | | start of start bit. This is used to synchronise with the data being transfered */ |
| 101 | | #define RECEIVE_REGISTER_WAITING_FOR_START_BIT 0x01 |
| 102 | | /* receive is synchronised with data, data bits will be clocked in */ |
| 103 | | #define RECEIVE_REGISTER_SYNCHRONISED 0x02 |
| 104 | | /* set if receive register has been filled */ |
| 105 | | #define RECEIVE_REGISTER_FULL 0x04 |
| 106 | | |
| 107 | | |
| 108 | | /* the receive register holds data in receive form! */ |
| 109 | | /* this must be extracted to get the data byte received */ |
| 110 | | typedef struct _serial_receive_register serial_receive_register; |
| 111 | | struct _serial_receive_register |
| 112 | | { |
| 113 | | /* data */ |
| 114 | | unsigned long register_data; |
| 115 | | /* flags */ |
| 116 | | unsigned long flags; |
| 117 | | /* bit count received */ |
| 118 | | unsigned long bit_count_received; |
| 119 | | /* length of data to receive - includes data bits, parity bit and stop bit */ |
| 120 | | unsigned long bit_count; |
| 121 | | |
| 122 | | /* the byte of data received */ |
| 123 | | unsigned char byte_received; |
| 124 | | }; |
| 125 | | |
| 126 | | void receive_register_setup(serial_receive_register *receive, data_form *data_form); |
| 127 | | void receive_register_update_bit(serial_receive_register *receive, int bit_state); |
| 128 | | void receive_register_extract(serial_receive_register *receive_reg, data_form *data_form); |
| 129 | | void receive_register_reset(serial_receive_register *receive_reg); |
| 130 | | |
| 131 | | /* the transmit register is the final stage |
| 132 | | in the serial transmit procedure */ |
| 133 | | /* normally, data is written to the transmit reg, |
| 134 | | then it is assembled into transmit form and transmitted */ |
| 135 | | /* the transmit register holds data in transmit form */ |
| 136 | | |
| 137 | | /* register is empty and ready to be filled with data */ |
| 138 | | #define TRANSMIT_REGISTER_EMPTY 0x0001 |
| 139 | | |
| 140 | | typedef struct _serial_transmit_register serial_transmit_register; |
| 141 | | struct _serial_transmit_register |
| 142 | | { |
| 143 | | /* data */ |
| 144 | | unsigned long register_data; |
| 145 | | /* flags */ |
| 146 | | unsigned long flags; |
| 147 | | /* number of bits transmitted */ |
| 148 | | unsigned long bit_count_transmitted; |
| 149 | | /* length of data to send */ |
| 150 | | unsigned long bit_count; |
| 151 | | }; |
| 152 | | |
| 153 | | /* setup transmit reg ready for transmit */ |
| 154 | | void transmit_register_setup(serial_transmit_register *transmit_reg, data_form *data_form,unsigned char data_byte); |
| 155 | | void transmit_register_send_bit(running_machine &machine, serial_transmit_register *transmit_reg, serial_connection *connection); |
| 156 | | void transmit_register_reset(serial_transmit_register *transmit_reg); |
| 157 | | |
| 158 | | /*******************************************************************************/ |
| 159 | | /**** SERIAL HELPER ****/ |
| 160 | | |
| 161 | | void serial_helper_setup(void); |
| 162 | | |
| 163 | | /*******************************************************************************/ |
| 164 | | /**** SERIAL DEVICE ****/ |
| 165 | | |
| 166 | | unsigned long serial_device_get_state(device_t *device); |
| 167 | | |
| 168 | | /* connect this device to the emulated serial chip */ |
| 169 | | /* id is the serial device to connect to */ |
| 170 | | /* connection is the serial connection to connect to the serial device */ |
| 171 | | void serial_device_connect(device_t *image, serial_connection *connection); |
| 172 | | |
| 173 | | DECLARE_LEGACY_IMAGE_DEVICE(SERIAL, serial); |
| 174 | | |
| 175 | | #define MCFG_SERIAL_ADD(_tag) \ |
| 176 | | MCFG_DEVICE_ADD(_tag, SERIAL, 0) |
| 177 | | |
| 178 | | DEVICE_START(serial); |
| 179 | | DEVICE_IMAGE_LOAD(serial); |
| 180 | | |
| 181 | | void serial_device_setup(device_t *image, int baud_rate, int num_data_bits, int stop_bit_count, int parity_code); |
| 182 | | |
| 183 | | /* set the transmit state of the serial device */ |
| 184 | | void serial_device_set_transmit_state(device_t *image, int state); |
| 185 | | |
| 186 | | #endif /* SERIAL_H_ */ |
src/mess/machine/thomson.c
| r12777 | r12778 | |
| 374 | 374 | output_set_value( "led0", led ); |
| 375 | 375 | } |
| 376 | 376 | |
| 377 | | |
| 378 | | |
| 379 | | /* ------------ serial (not functionnal yet) ------------ */ |
| 380 | | |
| 381 | | |
| 382 | | |
| 383 | | static serial_connection to7_io_line; |
| 384 | | |
| 385 | | static DEVICE_START( thom_serial_cc90323 ) |
| 386 | | { |
| 387 | | DEVICE_START_CALL(serial); |
| 388 | | LOG(( "thom_serial_init: init CD 90-320 RS232 device\n" )); |
| 389 | | serial_device_connect( device, &to7_io_line ); |
| 390 | | serial_device_setup( device, 2400, 7, 2, SERIAL_PARITY_NONE ); |
| 391 | | serial_device_set_transmit_state( device, 1 ); |
| 392 | | } |
| 393 | | |
| 394 | | static DEVICE_START( thom_serial_rf57232 ) |
| 395 | | { |
| 396 | | //acia6551_device *acia = device->machine().device<acia6551_device>("acia"); |
| 397 | | DEVICE_START_CALL(serial); |
| 398 | | LOG(( "thom_serial_init: init RF 57-232 RS232 device\n" )); |
| 399 | | //acia->connect_to_serial_device(device); |
| 400 | | serial_device_setup( device, 2400, 7, 2, SERIAL_PARITY_NONE ); |
| 401 | | serial_device_set_transmit_state( device, 1 ); |
| 402 | | } |
| 403 | | |
| 404 | | static DEVICE_START( thom_serial_modem ) |
| 405 | | { |
| 406 | | DEVICE_START_CALL(serial); |
| 407 | | LOG(( "thom_serial_init: init MODEM (MD 90-120) device\n" )); |
| 408 | | serial_device_setup( device, 2400, 7, 2, SERIAL_PARITY_NONE ); |
| 409 | | serial_device_set_transmit_state( device, 1 ); |
| 410 | | } |
| 411 | | |
| 412 | | static DEVICE_IMAGE_LOAD( thom_serial ) |
| 413 | | { |
| 414 | | if ( device_load_serial( image ) != IMAGE_INIT_PASS ) |
| 415 | | { |
| 416 | | logerror( "thom_serial_load: could not load serial image in device\n"); |
| 417 | | return IMAGE_INIT_FAIL; |
| 418 | | } |
| 419 | | |
| 420 | | return IMAGE_INIT_PASS; |
| 421 | | } |
| 422 | | |
| 423 | | DEVICE_GET_INFO( thom_serial_cc90323 ) |
| 424 | | { |
| 425 | | switch ( state ) |
| 426 | | { |
| 427 | | case DEVINFO_FCT_START: info->start = DEVICE_START_NAME( thom_serial_cc90323 ); break; |
| 428 | | case DEVINFO_FCT_IMAGE_LOAD: info->f = (genf *) DEVICE_IMAGE_LOAD_NAME( thom_serial );break; |
| 429 | | case DEVINFO_STR_NAME: strcpy(info->s, "CD 90-320 RS232"); break; |
| 430 | | case DEVINFO_STR_IMAGE_FILE_EXTENSIONS: strcpy(info->s, "txt"); break; |
| 431 | | default: DEVICE_GET_INFO_CALL(serial); break; |
| 432 | | } |
| 433 | | } |
| 434 | | |
| 435 | | DEVICE_GET_INFO( thom_serial_rf57232 ) |
| 436 | | { |
| 437 | | switch ( state ) |
| 438 | | { |
| 439 | | case DEVINFO_FCT_START: info->start = DEVICE_START_NAME( thom_serial_rf57232 ); break; |
| 440 | | case DEVINFO_FCT_IMAGE_LOAD: info->f = (genf *) DEVICE_IMAGE_LOAD_NAME( thom_serial );break; |
| 441 | | case DEVINFO_STR_NAME: strcpy(info->s, "RF 57-232 RS232"); break; |
| 442 | | case DEVINFO_STR_IMAGE_FILE_EXTENSIONS: strcpy(info->s, "txt"); break; |
| 443 | | default: DEVICE_GET_INFO_CALL(serial); break; |
| 444 | | } |
| 445 | | } |
| 446 | | |
| 447 | | |
| 448 | | DEVICE_GET_INFO( thom_serial_modem ) |
| 449 | | { |
| 450 | | switch ( state ) |
| 451 | | { |
| 452 | | case DEVINFO_FCT_START: info->start = DEVICE_START_NAME( thom_serial_modem ); break; |
| 453 | | case DEVINFO_FCT_IMAGE_LOAD: info->f = (genf *) DEVICE_IMAGE_LOAD_NAME( thom_serial );break; |
| 454 | | case DEVINFO_STR_NAME: strcpy(info->s, "MODEM (MD 90-120)"); break; |
| 455 | | case DEVINFO_STR_IMAGE_FILE_EXTENSIONS: strcpy(info->s, "txt"); break; |
| 456 | | default: DEVICE_GET_INFO_CALL(serial); break; |
| 457 | | } |
| 458 | | } |
| 459 | | |
| 460 | 377 | /* ------------ 6850 defines ------------ */ |
| 461 | 378 | |
| 462 | 379 | #define ACIA_6850_RDRF 0x01 /* Receive data register full */ |
| r12777 | r12778 | |
| 789 | 706 | //LOG_IO (( "%f to7_io_ack: CENTRONICS new state $%02X (ack=%i)\n", machine.time().as_double(), data, ack )); |
| 790 | 707 | } |
| 791 | 708 | |
| 709 | const device_type TO7_IO_LINE = &device_creator<to7_io_line_device>; |
| 792 | 710 | |
| 711 | //------------------------------------------------- |
| 712 | // to7_io_line_device - constructor |
| 713 | //------------------------------------------------- |
| 793 | 714 | |
| 794 | | static WRITE8_DEVICE_HANDLER ( to7_io_porta_out ) |
| 715 | to7_io_line_device::to7_io_line_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) |
| 716 | : device_t(mconfig, TO7_IO_LINE, "Serial source", tag, owner, clock), |
| 717 | device_serial_interface(mconfig, *this) |
| 795 | 718 | { |
| 719 | } |
| 720 | |
| 721 | void to7_io_line_device::device_start() |
| 722 | { |
| 723 | } |
| 724 | |
| 725 | WRITE8_MEMBER( to7_io_line_device::porta_out ) |
| 726 | { |
| 796 | 727 | int tx = data & 1; |
| 797 | 728 | int dtr = ( data & 2 ) ? 1 : 0; |
| 798 | 729 | |
| 799 | | LOG_IO(( "$%04x %f to7_io_porta_out: tx=%i, dtr=%i\n", cpu_get_previouspc(device->machine().device("maincpu")), device->machine().time().as_double(), tx, dtr )); |
| 730 | LOG_IO(( "$%04x %f to7_io_porta_out: tx=%i, dtr=%i\n", cpu_get_previouspc(machine().device("maincpu")), machine().time().as_double(), tx, dtr )); |
| 800 | 731 | if ( dtr ) |
| 801 | | to7_io_line.State |= SERIAL_STATE_DTR; |
| 732 | m_connection_state |= SERIAL_STATE_DTR; |
| 802 | 733 | else |
| 803 | | to7_io_line.State &= ~SERIAL_STATE_DTR; |
| 734 | m_connection_state &= ~SERIAL_STATE_DTR; |
| 804 | 735 | |
| 805 | | set_out_data_bit( to7_io_line.State, tx ); |
| 806 | | serial_connection_out( device->machine(), &to7_io_line ); |
| 736 | set_out_data_bit(tx); |
| 737 | serial_connection_out(); |
| 807 | 738 | } |
| 808 | 739 | |
| 809 | 740 | |
| 810 | 741 | |
| 811 | | static READ8_DEVICE_HANDLER( to7_io_porta_in ) |
| 742 | READ8_MEMBER( to7_io_line_device::porta_in ) |
| 812 | 743 | { |
| 813 | | device_t *printer = device->machine().device("centronics"); |
| 744 | device_t *printer = machine().device("centronics"); |
| 814 | 745 | int cts = 1; |
| 815 | | int dsr = ( to7_io_line.input_state & SERIAL_STATE_DSR ) ? 0 : 1; |
| 816 | | int rd = get_in_data_bit( to7_io_line.input_state ); |
| 746 | int dsr = ( m_input_state & SERIAL_STATE_DSR ) ? 0 : 1; |
| 747 | int rd = get_in_data_bit(); |
| 817 | 748 | |
| 818 | | if ( to7_io_mode(device->machine()) == TO7_IO_RS232 ) |
| 819 | | cts = to7_io_line.input_state & SERIAL_STATE_CTS ? 0 : 1; |
| 749 | if ( to7_io_mode(machine()) == TO7_IO_RS232 ) |
| 750 | cts = m_input_state & SERIAL_STATE_CTS ? 0 : 1; |
| 820 | 751 | else |
| 821 | 752 | cts = !centronics_busy_r(printer); |
| 822 | 753 | |
| 823 | | LOG_IO(( "$%04x %f to7_io_porta_in: mode=%i cts=%i, dsr=%i, rd=%i\n", cpu_get_previouspc(device->machine().device("maincpu")), device->machine().time().as_double(), to7_io_mode(device->machine()), cts, dsr, rd )); |
| 754 | LOG_IO(( "$%04x %f to7_io_porta_in: mode=%i cts=%i, dsr=%i, rd=%i\n", cpu_get_previouspc(machine().device("maincpu")), machine().time().as_double(), to7_io_mode(machine()), cts, dsr, rd )); |
| 824 | 755 | |
| 825 | 756 | return (dsr ? 0x20 : 0) | (cts ? 0x40 : 0) | (rd ? 0x80: 0); |
| 826 | 757 | } |
| r12777 | r12778 | |
| 850 | 781 | } |
| 851 | 782 | |
| 852 | 783 | |
| 784 | void to7_io_line_device::input_callback(UINT8 state) |
| 785 | { |
| 786 | m_input_state = state; |
| 853 | 787 | |
| 854 | | static void to7_io_in_cb ( running_machine &machine, int id, unsigned long state ) |
| 855 | | { |
| 856 | | /* our peer's state has changed */ |
| 857 | | to7_io_line.input_state = state; |
| 858 | | |
| 859 | | LOG_IO(( "%f to7_io_in_callback: cts=%i dsr=%i rd=%i\n", machine.time().as_double(), (state & SERIAL_STATE_CTS) ? 1 : 0, (state & SERIAL_STATE_DSR) ? 1 : 0, (int)get_in_data_bit( state ) )); |
| 788 | LOG_IO(( "%f to7_io_in_callback: cts=%i dsr=%i rd=%i\n", machine().time().as_double(), (state & SERIAL_STATE_CTS) ? 1 : 0, (state & SERIAL_STATE_DSR) ? 1 : 0, (int)get_in_data_bit() )); |
| 860 | 789 | } |
| 861 | 790 | |
| 862 | 791 | |
| 863 | 792 | |
| 864 | 793 | const pia6821_interface to7_pia6821_io = |
| 865 | 794 | { |
| 866 | | DEVCB_HANDLER(to7_io_porta_in), |
| 795 | DEVCB_DEVICE_MEMBER("to7_io", to7_io_line_device, porta_in), |
| 867 | 796 | DEVCB_NULL, |
| 868 | 797 | DEVCB_NULL, |
| 869 | 798 | DEVCB_NULL, |
| 870 | 799 | DEVCB_NULL, |
| 871 | 800 | DEVCB_NULL, |
| 872 | | DEVCB_HANDLER(to7_io_porta_out), |
| 801 | DEVCB_DEVICE_MEMBER("to7_io", to7_io_line_device, porta_out), |
| 873 | 802 | DEVCB_HANDLER(to7_io_portb_out), |
| 874 | 803 | DEVCB_NULL, |
| 875 | 804 | DEVCB_HANDLER(to7_io_cb2_out), |
| r12777 | r12778 | |
| 887 | 816 | }; |
| 888 | 817 | |
| 889 | 818 | |
| 890 | | static void to7_io_reset( running_machine &machine ) |
| 819 | void to7_io_line_device::device_reset() |
| 891 | 820 | { |
| 892 | | pia6821_device *io_pia = machine.device<pia6821_device>(THOM_PIA_IO); |
| 821 | pia6821_device *io_pia = machine().device<pia6821_device>(THOM_PIA_IO); |
| 893 | 822 | |
| 894 | 823 | LOG (( "to7_io_reset called\n" )); |
| 895 | 824 | |
| 896 | 825 | io_pia->set_port_a_z_mask(0x03 ); |
| 897 | | to7_io_line.input_state = SERIAL_STATE_CTS; |
| 898 | | to7_io_line.State &= ~SERIAL_STATE_DTR; |
| 899 | | to7_io_line.State |= SERIAL_STATE_RTS; /* always ready to send */ |
| 900 | | set_out_data_bit( to7_io_line.State, 1 ); |
| 901 | | serial_connection_out( machine, &to7_io_line ); |
| 826 | m_input_state = SERIAL_STATE_CTS; |
| 827 | m_connection_state &= ~SERIAL_STATE_DTR; |
| 828 | m_connection_state |= SERIAL_STATE_RTS; /* always ready to send */ |
| 829 | set_out_data_bit(1); |
| 830 | serial_connection_out(); |
| 902 | 831 | } |
| 903 | 832 | |
| 904 | | |
| 905 | | |
| 906 | | static void to7_io_init( running_machine &machine ) |
| 907 | | { |
| 908 | | LOG (( "to7_io_init: CC 90-323 serial / parallel extension\n" )); |
| 909 | | serial_connection_init( machine, &to7_io_line ); |
| 910 | | serial_connection_set_in_callback( machine, &to7_io_line, to7_io_in_cb ); |
| 911 | | } |
| 912 | | |
| 913 | | |
| 914 | | |
| 915 | 833 | /* ------------ RF 57-932 RS232 extension ------------ */ |
| 916 | 834 | |
| 917 | 835 | /* Features: |
| r12777 | r12778 | |
| 1531 | 1449 | thom_irq_reset(machine); |
| 1532 | 1450 | to7_game_reset(machine); |
| 1533 | 1451 | to7_floppy_reset(machine); |
| 1534 | | to7_io_reset(machine); |
| 1535 | 1452 | to7_modem_reset(machine); |
| 1536 | 1453 | to7_midi_reset(machine); |
| 1537 | 1454 | |
| r12777 | r12778 | |
| 1566 | 1483 | thom_irq_init(machine); |
| 1567 | 1484 | to7_game_init(machine); |
| 1568 | 1485 | to7_floppy_init(machine, mem + 0x20000); |
| 1569 | | to7_io_init(machine); |
| 1570 | 1486 | to7_modem_init(machine); |
| 1571 | 1487 | to7_midi_init(machine); |
| 1572 | 1488 | |
| r12777 | r12778 | |
| 1788 | 1704 | thom_irq_reset(machine); |
| 1789 | 1705 | to7_game_reset(machine); |
| 1790 | 1706 | to7_floppy_reset(machine); |
| 1791 | | to7_io_reset(machine); |
| 1792 | 1707 | to7_modem_reset(machine); |
| 1793 | 1708 | to7_midi_reset(machine); |
| 1794 | 1709 | |
| r12777 | r12778 | |
| 1824 | 1739 | thom_irq_init(machine); |
| 1825 | 1740 | to7_game_init(machine); |
| 1826 | 1741 | to7_floppy_init( machine, mem + 0x20000 ); |
| 1827 | | to7_io_init(machine); |
| 1828 | 1742 | to7_modem_init(machine); |
| 1829 | 1743 | to7_midi_init(machine); |
| 1830 | 1744 | |
| r12777 | r12778 | |
| 2163 | 2077 | sys_pia->set_port_a_z_mask(0x5f ); |
| 2164 | 2078 | to7_game_reset(machine); |
| 2165 | 2079 | to7_floppy_reset(machine); |
| 2166 | | to7_io_reset(machine); |
| 2167 | 2080 | to7_modem_reset(machine); |
| 2168 | 2081 | to7_midi_reset(machine); |
| 2169 | 2082 | mo5_init_timer(machine); |
| r12777 | r12778 | |
| 2198 | 2111 | thom_irq_init(machine); |
| 2199 | 2112 | to7_game_init(machine); |
| 2200 | 2113 | to7_floppy_init( machine, mem + 0x20000 ); |
| 2201 | | to7_io_init(machine); |
| 2202 | 2114 | to7_modem_init(machine); |
| 2203 | 2115 | to7_midi_init(machine); |
| 2204 | 2116 | mo5_periodic_timer = machine.scheduler().timer_alloc(FUNC(mo5_periodic_cb)); |