src/mess/machine/68901mfp.c
| r1347 | r1348 | |
| 4 | 4 | |
| 5 | 5 | - poll TAI/TBI pins |
| 6 | 6 | - daisy chaining |
| 7 | | - correct irq_timer period |
| 7 | - correct gpio_timer period |
| 8 | 8 | - disable GPIO3/4 interrupts when timer A/B in pulse mode |
| 9 | 9 | - spurious interrupt |
| 10 | 10 | |
| r1347 | r1348 | |
| 45 | 45 | |
| 46 | 46 | UINT16 ier, ipr, isr, imr; |
| 47 | 47 | UINT8 vr; |
| 48 | | emu_timer *irq_timer; |
| 48 | int irqlevel; |
| 49 | 49 | |
| 50 | 50 | UINT8 tacr, tbcr, tcdcr; |
| 51 | 51 | UINT8 tdr[MFP68901_MAX_TIMERS]; |
| r1347 | r1348 | |
| 63 | 63 | int rx_parity, tx_parity; |
| 64 | 64 | int rx_state, tx_state, xmit_state; |
| 65 | 65 | int rxtx_word, rxtx_start, rxtx_stop; |
| 66 | | |
| 67 | | emu_timer *rx_timer, *tx_timer; |
| 68 | 66 | } mfp_68901; |
| 69 | 67 | |
| 70 | 68 | static mfp_68901 mfp[MAX_MFP]; |
| r1347 | r1348 | |
| 92 | 90 | |
| 93 | 91 | static const int PRESCALER[] = { 0, 4, 10, 16, 50, 64, 100, 200 }; |
| 94 | 92 | |
| 95 | | static void mfp68901_check_interrupts(int which) |
| 93 | INLINE void mfp68901_check_interrupts(int which) |
| 96 | 94 | { |
| 97 | 95 | mfp_68901 *mfp_p = &mfp[which]; |
| 98 | 96 | |
| 99 | | int intr = (mfp_p->ipr & mfp_p->imr) ? HOLD_LINE : CLEAR_LINE; |
| 100 | | mfp_p->intf->irq_callback(which, intr); |
| 97 | int irqlevel = (mfp_p->ipr & mfp_p->imr) ? HOLD_LINE : CLEAR_LINE; |
| 98 | |
| 99 | if (irqlevel != mfp_p->irqlevel) |
| 100 | { |
| 101 | mfp_p->intf->irq_callback(which, irqlevel); |
| 102 | } |
| 101 | 103 | } |
| 102 | 104 | |
| 103 | | static void mfp68901_poll_gpio(int which) |
| 105 | INLINE void mfp68901_interrupt(int which, UINT16 mask) |
| 104 | 106 | { |
| 105 | 107 | mfp_68901 *mfp_p = &mfp[which]; |
| 106 | 108 | |
| 107 | | UINT8 gpio = 0, gpold, gpnew; |
| 108 | | int bit; |
| 109 | mfp_p->ipr |= mask; |
| 109 | 110 | |
| 110 | | if (mfp_p->intf->gpio_r) |
| 111 | | { |
| 112 | | gpio = mfp_p->intf->gpio_r(0); // read the state of GPIO the pins from the driver |
| 113 | | } |
| 111 | mfp68901_check_interrupts(which); |
| 112 | } |
| 114 | 113 | |
| 115 | | gpold = (mfp_p->gpip & ~mfp_p->ddr) ^ mfp_p->aer; |
| 116 | | gpnew = (gpio & ~mfp_p->ddr) ^ mfp_p->aer; |
| 114 | static TIMER_CALLBACK( mfp68901_gpio_poll_tick ) |
| 115 | { |
| 116 | mfp_68901 *mfp_p = &mfp[param]; |
| 117 | 117 | |
| 118 | UINT8 gpio = mfp_p->intf->gpio_r(0); |
| 119 | |
| 120 | UINT8 gpold = (mfp_p->gpip & ~mfp_p->ddr) ^ mfp_p->aer; |
| 121 | UINT8 gpnew = (gpio & ~mfp_p->ddr) ^ mfp_p->aer; |
| 122 | |
| 123 | int bit; |
| 124 | |
| 118 | 125 | for (bit = 0; bit < 8; bit++) // loop thru each bit from 0 to 7 |
| 119 | 126 | { |
| 120 | 127 | if ((BIT(gpold, bit) == 1) && (BIT(gpnew, bit) == 0)) // if transition from 1 to 0 is detected... |
| 121 | 128 | { |
| 122 | | logerror("MFP68901 #%u Edge Transition Detected on GPIO%u\n", which, bit); |
| 129 | logerror("MFP68901 #%u Edge Transition Detected on GPIO%u\n", param, bit); |
| 123 | 130 | |
| 124 | 131 | if (mfp_p->ier & MFP68901_INT_MASK_GPIO[bit]) // AND interrupt enabled bit is set... |
| 125 | 132 | { |
| 126 | | logerror("MFP68901 #%u Interrupt Pending for GPIO%u\n", which, bit); |
| 133 | logerror("MFP68901 #%u Interrupt Pending for GPIO%u\n", param, bit); |
| 127 | 134 | |
| 128 | | mfp_p->ipr |= MFP68901_INT_MASK_GPIO[bit]; // set interrupt pending bit |
| 135 | mfp68901_interrupt(param, MFP68901_INT_MASK_GPIO[bit]); // set interrupt pending bit |
| 129 | 136 | } |
| 130 | 137 | } |
| 131 | 138 | } |
| r1347 | r1348 | |
| 133 | 140 | mfp_p->gpip = (gpio & ~mfp_p->ddr) | (mfp_p->gpip & mfp_p->ddr); |
| 134 | 141 | } |
| 135 | 142 | |
| 136 | | static TIMER_CALLBACK( mfp68901_tick ) |
| 137 | | { |
| 138 | | mfp68901_poll_gpio(param); |
| 139 | | mfp68901_check_interrupts(param); |
| 140 | | } |
| 141 | | |
| 142 | 143 | /* USART */ |
| 143 | 144 | |
| 144 | | static void mfp68901_rx_buffer_full(int which) |
| 145 | INLINE void mfp68901_rx_buffer_full(int which) |
| 145 | 146 | { |
| 146 | 147 | mfp_68901 *mfp_p = &mfp[which]; |
| 147 | 148 | |
| 148 | 149 | if (mfp_p->ier & MFP68901_IR_RCV_BUFFER_FULL) |
| 149 | 150 | { |
| 150 | | mfp_p->ipr |= MFP68901_IR_RCV_BUFFER_FULL; |
| 151 | mfp68901_interrupt(which, MFP68901_IR_RCV_BUFFER_FULL); |
| 151 | 152 | } |
| 152 | 153 | } |
| 153 | 154 | |
| 154 | | static void mfp68901_rx_error(int which) |
| 155 | INLINE void mfp68901_rx_error(int which) |
| 155 | 156 | { |
| 156 | 157 | mfp_68901 *mfp_p = &mfp[which]; |
| 157 | 158 | |
| 158 | 159 | if (mfp_p->ier & MFP68901_IR_RCV_ERROR) |
| 159 | 160 | { |
| 160 | | mfp_p->ipr |= MFP68901_IR_RCV_ERROR; |
| 161 | mfp68901_interrupt(which, MFP68901_IR_RCV_ERROR); |
| 161 | 162 | } |
| 162 | 163 | else |
| 163 | 164 | { |
| r1347 | r1348 | |
| 165 | 166 | } |
| 166 | 167 | } |
| 167 | 168 | |
| 168 | | static void mfp68901_tx_buffer_empty(int which) |
| 169 | INLINE void mfp68901_tx_buffer_empty(int which) |
| 169 | 170 | { |
| 170 | 171 | mfp_68901 *mfp_p = &mfp[which]; |
| 171 | 172 | |
| 172 | 173 | if (mfp_p->ier & MFP68901_IR_XMIT_BUFFER_EMPTY) |
| 173 | 174 | { |
| 174 | | mfp_p->ipr |= MFP68901_IR_XMIT_BUFFER_EMPTY; |
| 175 | mfp68901_interrupt(which, MFP68901_IR_XMIT_BUFFER_EMPTY); |
| 175 | 176 | } |
| 176 | 177 | } |
| 177 | 178 | |
| 178 | | static void mfp68901_tx_error(int which) |
| 179 | INLINE void mfp68901_tx_error(int which) |
| 179 | 180 | { |
| 180 | 181 | mfp_68901 *mfp_p = &mfp[which]; |
| 181 | 182 | |
| 182 | 183 | if (mfp_p->ier & MFP68901_IR_XMIT_ERROR) |
| 183 | 184 | { |
| 184 | | mfp_p->ipr |= MFP68901_IR_XMIT_ERROR; |
| 185 | mfp68901_interrupt(which, MFP68901_IR_XMIT_ERROR); |
| 185 | 186 | } |
| 186 | 187 | else |
| 187 | 188 | { |
| r1347 | r1348 | |
| 189 | 190 | } |
| 190 | 191 | } |
| 191 | 192 | |
| 192 | | static int mfp68901_parity(UINT8 b) |
| 193 | INLINE int mfp68901_parity(UINT8 b) |
| 193 | 194 | { |
| 194 | 195 | b ^= b >> 4; |
| 195 | 196 | b ^= b >> 2; |
| r1347 | r1348 | |
| 197 | 198 | return b & 1; |
| 198 | 199 | } |
| 199 | 200 | |
| 200 | | static TIMER_CALLBACK( rx_tick ) |
| 201 | INLINE void mfp68901_serial_rx(int which) |
| 201 | 202 | { |
| 202 | | mfp_68901 *mfp_p = &mfp[param]; |
| 203 | mfp_68901 *mfp_p = &mfp[which]; |
| 203 | 204 | |
| 204 | 205 | if (mfp_p->rsr & MFP68901_RSR_RCV_ENABLE) |
| 205 | 206 | { |
| r1347 | r1348 | |
| 274 | 275 | // incoming word received and receive buffer is empty |
| 275 | 276 | mfp_p->rsr |= MFP68901_RSR_BUFFER_FULL; |
| 276 | 277 | mfp_p->udr = mfp_p->rx_buffer; |
| 277 | | mfp68901_rx_buffer_full(param); |
| 278 | mfp68901_rx_buffer_full(which); |
| 278 | 279 | } |
| 279 | 280 | } |
| 280 | 281 | } |
| r1347 | r1348 | |
| 294 | 295 | } |
| 295 | 296 | } |
| 296 | 297 | |
| 297 | | static void mfp68901_transmit_disabled(int which) |
| 298 | static TIMER_CALLBACK( rx_tick ) |
| 298 | 299 | { |
| 300 | mfp68901_serial_rx(param); |
| 301 | } |
| 302 | |
| 303 | INLINE void mfp68901_transmit_disabled(int which) |
| 304 | { |
| 299 | 305 | mfp_68901 *mfp_p = &mfp[which]; |
| 300 | 306 | |
| 301 | 307 | switch (mfp_p->tsr & MFP68901_TSR_OUTPUT_MASK) |
| r1347 | r1348 | |
| 313 | 319 | } |
| 314 | 320 | } |
| 315 | 321 | |
| 316 | | static void mfp68901_transmit(int which) |
| 322 | INLINE void mfp68901_transmit(int which) |
| 317 | 323 | { |
| 318 | 324 | mfp_68901 *mfp_p = &mfp[which]; |
| 319 | 325 | |
| r1347 | r1348 | |
| 419 | 425 | } |
| 420 | 426 | } |
| 421 | 427 | |
| 422 | | static TIMER_CALLBACK( tx_tick ) |
| 428 | INLINE void mfp68901_serial_tx(int which) |
| 423 | 429 | { |
| 424 | | mfp_68901 *mfp_p = &mfp[param]; |
| 430 | mfp_68901 *mfp_p = &mfp[which]; |
| 425 | 431 | |
| 426 | 432 | switch (mfp_p->xmit_state) |
| 427 | 433 | { |
| 428 | 434 | case MFP68901_XMIT_OFF: |
| 429 | | mfp68901_transmit_disabled(param); |
| 435 | mfp68901_transmit_disabled(which); |
| 430 | 436 | break; |
| 431 | 437 | |
| 432 | 438 | case MFP68901_XMIT_STARTING: |
| r1347 | r1348 | |
| 462 | 468 | break; |
| 463 | 469 | |
| 464 | 470 | case MFP68901_XMIT_ON: |
| 465 | | mfp68901_transmit(param); |
| 471 | mfp68901_transmit(which); |
| 466 | 472 | break; |
| 467 | 473 | } |
| 468 | 474 | } |
| 469 | 475 | |
| 476 | static TIMER_CALLBACK( tx_tick ) |
| 477 | { |
| 478 | mfp68901_serial_tx(param); |
| 479 | } |
| 480 | |
| 470 | 481 | static UINT8 mfp68901_register_r(int which, int reg) |
| 471 | 482 | { |
| 472 | 483 | mfp_68901 *mfp_p = &mfp[which]; |
| r1347 | r1348 | |
| 976 | 987 | } |
| 977 | 988 | } |
| 978 | 989 | |
| 979 | | void mfp68901_timer_count(int which, int index) |
| 990 | INLINE void mfp68901_timer_count(int which, int index) |
| 980 | 991 | { |
| 981 | 992 | mfp_68901 *mfp_p = &mfp[which]; |
| 982 | 993 | |
| r1347 | r1348 | |
| 988 | 999 | { |
| 989 | 1000 | if (mfp_p->intf->rx_clock == MFP68901_LOOPBACK_TIMER[index]) |
| 990 | 1001 | { |
| 991 | | timer_adjust(mfp_p->rx_timer, attotime_zero, which, attotime_never); |
| 1002 | mfp68901_serial_rx(which); |
| 992 | 1003 | } |
| 993 | 1004 | |
| 994 | 1005 | if (mfp_p->intf->tx_clock == MFP68901_LOOPBACK_TIMER[index]) |
| 995 | 1006 | { |
| 996 | | timer_adjust(mfp_p->tx_timer, attotime_zero, which, attotime_never); |
| 1007 | mfp68901_serial_tx(which); |
| 997 | 1008 | } |
| 998 | 1009 | } |
| 999 | 1010 | |
| r1347 | r1348 | |
| 1004 | 1015 | |
| 1005 | 1016 | if (mfp_p->ier & MFP68901_INT_MASK_TIMER[index]) |
| 1006 | 1017 | { |
| 1007 | | mfp_p->ipr |= MFP68901_INT_MASK_TIMER[index]; |
| 1018 | mfp68901_interrupt(which, MFP68901_INT_MASK_TIMER[index]); |
| 1008 | 1019 | } |
| 1009 | 1020 | |
| 1010 | 1021 | mfp_p->tmc[index] = mfp_p->tdr[index]; |
| r1347 | r1348 | |
| 1015 | 1026 | } |
| 1016 | 1027 | } |
| 1017 | 1028 | |
| 1018 | | void mfp68901_ti_w(int which, int index, int value) |
| 1029 | INLINE void mfp68901_ti_w(int which, int index, int value) |
| 1019 | 1030 | { |
| 1020 | 1031 | mfp_68901 *mfp_p = &mfp[which]; |
| 1032 | |
| 1021 | 1033 | int bit = MFP68901_GPIO_TIMER[index]; |
| 1022 | 1034 | int aer = BIT(mfp_p->aer, bit); |
| 1023 | 1035 | int cr = index ? mfp_p->tbcr : mfp_p->tacr; |
| r1347 | r1348 | |
| 1040 | 1052 | case MFP68901_TCR_TIMER_PULSE_64: |
| 1041 | 1053 | case MFP68901_TCR_TIMER_PULSE_100: |
| 1042 | 1054 | case MFP68901_TCR_TIMER_PULSE_200: |
| 1043 | | timer_enable(mfp_p->timer[index], (value == aer) ? 1 : 0); |
| 1055 | timer_enable(mfp_p->timer[index], (value == aer)); |
| 1044 | 1056 | |
| 1045 | 1057 | if (((mfp_p->ti[index] ^ aer) == 0) && ((value ^ aer) == 1)) |
| 1046 | 1058 | { |
| 1047 | 1059 | if (mfp_p->ier & MFP68901_INT_MASK_GPIO[bit]) |
| 1048 | 1060 | { |
| 1049 | | mfp_p->ipr |= MFP68901_INT_MASK_GPIO[bit]; |
| 1061 | mfp68901_interrupt(which, MFP68901_INT_MASK_GPIO[bit]); |
| 1050 | 1062 | } |
| 1051 | 1063 | } |
| 1052 | 1064 | |
| r1347 | r1348 | |
| 1103 | 1115 | } |
| 1104 | 1116 | |
| 1105 | 1117 | mfp_p->ipr &= ~(1 << ch); |
| 1106 | | |
| 1107 | 1118 | mfp68901_check_interrupts(which); |
| 1108 | 1119 | |
| 1109 | 1120 | return (mfp_p->vr & 0xf0) | ch; |
| r1347 | r1348 | |
| 1139 | 1150 | { |
| 1140 | 1151 | mfp_68901 *mfp_p = &mfp[which]; |
| 1141 | 1152 | |
| 1142 | | if (which >= MAX_MFP) |
| 1143 | | { |
| 1144 | | return; |
| 1145 | | } |
| 1153 | assert(which < MAX_MFP); |
| 1146 | 1154 | |
| 1155 | // initial settings |
| 1156 | |
| 1147 | 1157 | memset(mfp_p, 0, sizeof(mfp)); |
| 1148 | 1158 | |
| 1149 | 1159 | mfp_p->tsr = MFP68901_TSR_BUFFER_EMPTY; |
| 1150 | 1160 | |
| 1151 | 1161 | mfp_p->intf = intf; |
| 1152 | 1162 | |
| 1163 | // counter timers |
| 1164 | |
| 1153 | 1165 | mfp_p->timer[MFP68901_TIMER_A] = timer_alloc(timer_a); |
| 1154 | 1166 | mfp_p->timer[MFP68901_TIMER_B] = timer_alloc(timer_b); |
| 1155 | 1167 | mfp_p->timer[MFP68901_TIMER_C] = timer_alloc(timer_c); |
| 1156 | 1168 | mfp_p->timer[MFP68901_TIMER_D] = timer_alloc(timer_d); |
| 1157 | 1169 | |
| 1158 | | mfp_p->rx_timer = timer_alloc(rx_tick); |
| 1159 | | |
| 1170 | // serial receive timer |
| 1171 | |
| 1160 | 1172 | if (mfp_p->intf->rx_clock > 0) |
| 1161 | 1173 | { |
| 1162 | | timer_adjust(mfp_p->rx_timer, attotime_zero, which, ATTOTIME_IN_HZ(mfp_p->intf->rx_clock)); |
| 1174 | timer_pulse(ATTOTIME_IN_HZ(mfp_p->intf->rx_clock), which, rx_tick); |
| 1163 | 1175 | } |
| 1164 | 1176 | |
| 1165 | | mfp_p->tx_timer = timer_alloc(tx_tick); |
| 1177 | // serial transmit timer |
| 1166 | 1178 | |
| 1167 | 1179 | if (mfp_p->intf->tx_clock > 0) |
| 1168 | 1180 | { |
| 1169 | | timer_adjust(mfp_p->tx_timer, attotime_zero, which, ATTOTIME_IN_HZ(mfp_p->intf->tx_clock)); |
| 1181 | timer_pulse(ATTOTIME_IN_HZ(mfp_p->intf->tx_clock), which, tx_tick); |
| 1170 | 1182 | } |
| 1171 | 1183 | |
| 1172 | | mfp_p->irq_timer = timer_alloc(mfp68901_tick); |
| 1173 | | timer_adjust(mfp_p->irq_timer, attotime_zero, which, ATTOTIME_IN_HZ(mfp_p->intf->chip_clock / 4)); |
| 1184 | // general purpose I/O poll timer |
| 1174 | 1185 | |
| 1186 | if (mfp_p->intf->gpio_r) |
| 1187 | { |
| 1188 | timer_pulse(ATTOTIME_IN_HZ(mfp_p->intf->chip_clock / 4), which, mfp68901_gpio_poll_tick); |
| 1189 | } |
| 1190 | |
| 1191 | // save state support |
| 1192 | |
| 1175 | 1193 | state_save_register_item("mfp68901", which, mfp_p->gpip); |
| 1176 | 1194 | state_save_register_item("mfp68901", which, mfp_p->aer); |
| 1177 | 1195 | state_save_register_item("mfp68901", which, mfp_p->ddr); |
| r1347 | r1348 | |
| 1206 | 1224 | state_save_register_item("mfp68901", which, mfp_p->rxtx_stop); |
| 1207 | 1225 | state_save_register_item("mfp68901", which, mfp_p->rsr_read); |
| 1208 | 1226 | state_save_register_item("mfp68901", which, mfp_p->next_rsr); |
| 1227 | state_save_register_item("mfp68901", which, mfp_p->irqlevel); |
| 1209 | 1228 | } |
| 1210 | 1229 | |
| 1211 | 1230 | /* Read/Write Handlers */ |