src/mess/machine/ins8154.c
| r1984 | r1985 | |
| 13 | 13 | #include "driver.h" |
| 14 | 14 | #include "ins8154.h" |
| 15 | 15 | |
| 16 | | #define MAX_INS8154 2 |
| 17 | 16 | |
| 18 | 17 | #define VERBOSE 1 |
| 19 | 18 | #define LOG(x) do { if (VERBOSE) logerror x; } while (0) |
| r1984 | r1985 | |
| 29 | 28 | }; |
| 30 | 29 | |
| 31 | 30 | |
| 32 | | typedef struct _ins8154_state ins8154_state; |
| 33 | | struct _ins8154_state |
| 31 | struct _ins8154_t |
| 34 | 32 | { |
| 35 | | /* Pointer to our interface */ |
| 36 | | const ins8154_interface *intf; |
| 37 | | |
| 33 | const ins8154_interface *intf; /* Pointer to our interface */ |
| 34 | |
| 38 | 35 | UINT8 in_a; /* Input Latch Port A */ |
| 39 | 36 | UINT8 in_b; /* Input Latch Port B */ |
| 40 | 37 | UINT8 out_a; /* Output Latch Port A */ |
| r1984 | r1985 | |
| 44 | 41 | UINT8 odrb; /* Output Definition Register Port B */ |
| 45 | 42 | }; |
| 46 | 43 | |
| 47 | | static ins8154_state ins8154[MAX_INS8154]; |
| 48 | 44 | |
| 49 | | |
| 50 | | /* Config */ |
| 51 | | void ins8154_config(running_machine *machine, int which, const ins8154_interface *intf) |
| 45 | static DEVICE_START( ins8154 ) |
| 52 | 46 | { |
| 53 | | assert_always(mame_get_phase(machine) == MAME_PHASE_INIT, |
| 54 | | "Can only call ins8154_config at init time!"); |
| 55 | | assert_always(which < MAX_INS8154, |
| 56 | | "'which' exceeds maximum number of configured INS8154s!"); |
| 47 | ins8154_t *ins8154; |
| 48 | char unique_tag[30]; |
| 57 | 49 | |
| 58 | | /* Assign interface */ |
| 59 | | ins8154[which].intf = intf; |
| 50 | /* validate arguments */ |
| 51 | assert(machine != NULL); |
| 52 | assert(tag != NULL); |
| 53 | assert(strlen(tag) < 20); |
| 60 | 54 | |
| 61 | | /* Initialize */ |
| 62 | | ins8154_reset(which); |
| 55 | /* allocate the object that holds the state */ |
| 56 | ins8154 = auto_malloc(sizeof(*ins8154)); |
| 57 | memset(ins8154, 0, sizeof(*ins8154)); |
| 58 | |
| 59 | /* assign interface */ |
| 60 | ins8154->intf = static_config; |
| 61 | |
| 62 | /* register for state saving */ |
| 63 | state_save_combine_module_and_tag(unique_tag, "ins8154", tag); |
| 64 | state_save_register_item(unique_tag, 0, ins8154->in_a); |
| 65 | state_save_register_item(unique_tag, 0, ins8154->in_b); |
| 66 | state_save_register_item(unique_tag, 0, ins8154->out_a); |
| 67 | state_save_register_item(unique_tag, 0, ins8154->out_b); |
| 68 | state_save_register_item(unique_tag, 0, ins8154->mdr); |
| 69 | state_save_register_item(unique_tag, 0, ins8154->odra); |
| 70 | state_save_register_item(unique_tag, 0, ins8154->odrb); |
| 71 | |
| 72 | return ins8154; |
| 63 | 73 | } |
| 64 | 74 | |
| 65 | 75 | |
| 66 | | /* Reset */ |
| 67 | | void ins8154_reset(int which) |
| 76 | static DEVICE_RESET( ins8154 ) |
| 68 | 77 | { |
| 69 | | ins8154[which].in_a = 0; |
| 70 | | ins8154[which].in_b = 0; |
| 71 | | ins8154[which].out_a = 0; |
| 72 | | ins8154[which].out_b = 0; |
| 73 | | ins8154[which].mdr = 0; |
| 74 | | ins8154[which].odra = 0; |
| 75 | | ins8154[which].odrb = 0; |
| 78 | ins8154_t *ins8154 = token; |
| 79 | |
| 80 | ins8154->in_a = 0; |
| 81 | ins8154->in_b = 0; |
| 82 | ins8154->out_a = 0; |
| 83 | ins8154->out_b = 0; |
| 84 | ins8154->mdr = 0; |
| 85 | ins8154->odra = 0; |
| 86 | ins8154->odrb = 0; |
| 76 | 87 | } |
| 77 | 88 | |
| 78 | 89 | |
| 79 | | static UINT8 ins8154_read(running_machine *machine, int which, offs_t offset) |
| 90 | READ8_DEVICE_HANDLER( ins8154_r ) |
| 80 | 91 | { |
| 81 | | ins8154_state *i = &ins8154[which]; |
| 92 | ins8154_t *i = device->token; |
| 82 | 93 | UINT8 val = 0xff; |
| 83 | | |
| 94 | |
| 84 | 95 | if (offset > 0x24) |
| 85 | 96 | { |
| 86 | | logerror("INS8154 chip #%d (%08x): Read from unknown offset %02x!\n", |
| 87 | | which, safe_activecpu_get_pc(), offset); |
| 97 | logerror("INS8154 (%08x): Read from unknown offset %02x!\n", |
| 98 | safe_activecpu_get_pc(), offset); |
| 88 | 99 | return 0xff; |
| 89 | 100 | } |
| 90 | 101 | |
| r1984 | r1985 | |
| 92 | 103 | { |
| 93 | 104 | case 0x20: |
| 94 | 105 | if (i->intf->in_a_func) |
| 95 | | val = i->intf->in_a_func(machine, 0); |
| 106 | val = i->intf->in_a_func(device, 0); |
| 96 | 107 | i->in_a = val; |
| 97 | 108 | break; |
| 98 | 109 | |
| 99 | 110 | case 0x21: |
| 100 | 111 | if (i->intf->in_b_func) |
| 101 | | val = i->intf->in_b_func(machine, 0); |
| 112 | val = i->intf->in_b_func(device, 0); |
| 102 | 113 | i->in_b = val; |
| 103 | 114 | break; |
| 104 | 115 | |
| r1984 | r1985 | |
| 106 | 117 | if (offset < 0x08) |
| 107 | 118 | { |
| 108 | 119 | if (i->intf->in_a_func) |
| 109 | | val = (i->intf->in_a_func(machine, 0) << (8 - offset)) & 0x80; |
| 120 | val = (i->intf->in_a_func(device, 0) << (8 - offset)) & 0x80; |
| 110 | 121 | i->in_a = val; |
| 111 | 122 | } |
| 112 | 123 | else |
| 113 | 124 | { |
| 114 | 125 | if (i->intf->in_a_func) |
| 115 | | val = (i->intf->in_a_func(machine, 0) << (8 - (offset >> 4))) & 0x80; |
| 126 | val = (i->intf->in_a_func(device, 0) << (8 - (offset >> 4))) & 0x80; |
| 116 | 127 | i->in_b = val; |
| 117 | 128 | } |
| 118 | 129 | break; |
| r1984 | r1985 | |
| 121 | 132 | return val; |
| 122 | 133 | } |
| 123 | 134 | |
| 124 | | |
| 125 | | static void ins8154_write_port_a(running_machine *machine, int which, int data) |
| 135 | WRITE8_DEVICE_HANDLER( ins8154_porta_w ) |
| 126 | 136 | { |
| 127 | | ins8154_state *i = &ins8154[which]; |
| 137 | ins8154_t *i = device->token; |
| 128 | 138 | |
| 129 | 139 | i->out_a = data; |
| 130 | 140 | |
| r1984 | r1985 | |
| 132 | 142 | if (i->odra) |
| 133 | 143 | { |
| 134 | 144 | if (i->intf->out_a_func) |
| 135 | | i->intf->out_a_func(machine, 0, (data & i->odra) | (i->odra ^ 0xff)); |
| 145 | i->intf->out_a_func(device, 0, (data & i->odra) | (i->odra ^ 0xff)); |
| 136 | 146 | else |
| 137 | | logerror("INS8154 chip #%d (%08x): Write to port A but no write handler defined!\n", |
| 138 | | which, safe_activecpu_get_pc()); |
| 147 | logerror("INS8154 (%08x): Write to port A but no write handler defined!\n", |
| 148 | safe_activecpu_get_pc()); |
| 139 | 149 | } |
| 140 | 150 | } |
| 141 | 151 | |
| 142 | 152 | |
| 143 | | static void ins8154_write_port_b(running_machine *machine, int which, int data) |
| 153 | WRITE8_DEVICE_HANDLER( ins8154_portb_w ) |
| 144 | 154 | { |
| 145 | | ins8154_state *i = &ins8154[which]; |
| 155 | ins8154_t *i = device->token; |
| 146 | 156 | |
| 147 | 157 | i->out_b = data; |
| 148 | 158 | |
| r1984 | r1985 | |
| 150 | 160 | if (i->odrb) |
| 151 | 161 | { |
| 152 | 162 | if (i->intf->out_b_func) |
| 153 | | i->intf->out_b_func(machine, 0, (data & i->odrb) | (i->odrb ^ 0xff)); |
| 163 | i->intf->out_b_func(device, 0, (data & i->odrb) | (i->odrb ^ 0xff)); |
| 154 | 164 | else |
| 155 | | logerror("INS8154 chip #%d (%08x): Write to port B but no write handler defined!\n", |
| 156 | | which, safe_activecpu_get_pc()); |
| 165 | logerror("INS8154 (%08x): Write to port B but no write handler defined!\n", |
| 166 | safe_activecpu_get_pc()); |
| 157 | 167 | } |
| 158 | 168 | } |
| 159 | 169 | |
| 160 | 170 | |
| 161 | | static void ins8154_write(running_machine *machine, int which, offs_t offset, UINT8 data) |
| 171 | WRITE8_DEVICE_HANDLER( ins8154_w ) |
| 162 | 172 | { |
| 163 | | ins8154_state *i = &ins8154[which]; |
| 173 | ins8154_t *i = device->token; |
| 164 | 174 | |
| 165 | 175 | if (offset > 0x24) |
| 166 | 176 | { |
| 167 | | logerror("INS8154 chip %d (%04x): Write %02x to invalid offset %02x!\n", |
| 168 | | which, safe_activecpu_get_pc(), data, offset); |
| 177 | logerror("INS8154 (%04x): Write %02x to invalid offset %02x!\n", |
| 178 | safe_activecpu_get_pc(), data, offset); |
| 169 | 179 | return; |
| 170 | 180 | } |
| 171 | 181 | |
| 172 | 182 | switch (offset) |
| 173 | 183 | { |
| 174 | 184 | case 0x20: |
| 175 | | ins8154_write_port_a(machine, which, data); |
| 185 | ins8154_porta_w(device, 0, data); |
| 176 | 186 | break; |
| 177 | 187 | |
| 178 | 188 | case 0x21: |
| 179 | | ins8154_write_port_b(machine, which, data); |
| 189 | ins8154_portb_w(device, 0, data); |
| 180 | 190 | break; |
| 181 | 191 | |
| 182 | 192 | case 0x22: |
| 183 | | LOG(("INS8154 chip %d (%04x): ODR for port A set to %02x\n", |
| 184 | | which, safe_activecpu_get_pc(), data)); |
| 193 | LOG(("INS8154 (%04x): ODR for port A set to %02x\n", |
| 194 | safe_activecpu_get_pc(), data)); |
| 185 | 195 | i->odra = data; |
| 186 | 196 | break; |
| 187 | 197 | |
| 188 | 198 | case 0x23: |
| 189 | | LOG(("INS8154 chip %d (%04x): ODR for port B set to %02x\n", |
| 190 | | which, safe_activecpu_get_pc(), data)); |
| 199 | LOG(("INS8154 (%04x): ODR for port B set to %02x\n", |
| 200 | safe_activecpu_get_pc(), data)); |
| 191 | 201 | i->odrb = data; |
| 192 | 202 | break; |
| 193 | 203 | |
| 194 | 204 | case 0x24: |
| 195 | | LOG(("INS8154 chip %d (%04x): MDR set to %02x\n", |
| 196 | | which, safe_activecpu_get_pc(), data)); |
| 205 | LOG(("INS8154 (%04x): MDR set to %02x\n", |
| 206 | safe_activecpu_get_pc(), data)); |
| 197 | 207 | i->mdr = data; |
| 198 | 208 | break; |
| 199 | 209 | |
| r1984 | r1985 | |
| 203 | 213 | /* Set bit */ |
| 204 | 214 | if (offset < 0x08) |
| 205 | 215 | { |
| 206 | | ins8154_write_port_a(machine, which, i->out_a |= offset & 0x07); |
| 216 | ins8154_porta_w(device, 0, i->out_a |= offset & 0x07); |
| 207 | 217 | } |
| 208 | 218 | else |
| 209 | 219 | { |
| 210 | | ins8154_write_port_b(machine, which, i->out_b |= (offset >> 4) & 0x07); |
| 220 | ins8154_portb_w(device, 0, i->out_b |= (offset >> 4) & 0x07); |
| 211 | 221 | } |
| 212 | 222 | } |
| 213 | 223 | else |
| r1984 | r1985 | |
| 215 | 225 | /* Clear bit */ |
| 216 | 226 | if (offset < 0x08) |
| 217 | 227 | { |
| 218 | | ins8154_write_port_a(machine, which, i->out_a & ~(offset & 0x07)); |
| 228 | ins8154_porta_w(device, 0, i->out_a & ~(offset & 0x07)); |
| 219 | 229 | } |
| 220 | 230 | else |
| 221 | 231 | { |
| 222 | | ins8154_write_port_b(machine, which, i->out_b & ~((offset >> 4) & 0x07)); |
| 232 | ins8154_portb_w(device, 0, i->out_b & ~((offset >> 4) & 0x07)); |
| 223 | 233 | } |
| 224 | 234 | } |
| 225 | 235 | |
| r1984 | r1985 | |
| 228 | 238 | } |
| 229 | 239 | |
| 230 | 240 | |
| 241 | static DEVICE_SET_INFO( ins8154 ) |
| 242 | { |
| 243 | switch (state) |
| 244 | { |
| 245 | /* no parameters to set */ |
| 246 | } |
| 247 | } |
| 231 | 248 | |
| 232 | | /******************* Standard 8-bit CPU interfaces, D0-D7 *******************/ |
| 233 | 249 | |
| 234 | | WRITE8_HANDLER( ins8154_0_w ) { ins8154_write(machine, 0, offset, data); } |
| 235 | | WRITE8_HANDLER( ins8154_1_w ) { ins8154_write(machine, 1, offset, data); } |
| 250 | DEVICE_GET_INFO( ins8154 ) |
| 251 | { |
| 252 | switch (state) |
| 253 | { |
| 254 | /* --- the following bits of info are returned as 64-bit signed integers --- */ |
| 255 | case DEVINFO_INT_INLINE_CONFIG_BYTES: info->i = 0; break; |
| 256 | case DEVINFO_INT_CLASS: info->i = DEVICE_CLASS_PERIPHERAL; break; |
| 236 | 257 | |
| 237 | | READ8_HANDLER( ins8154_0_r ) { return ins8154_read(machine, 0, offset); } |
| 238 | | READ8_HANDLER( ins8154_1_r ) { return ins8154_read(machine, 1, offset); } |
| 258 | /* --- the following bits of info are returned as pointers to data or functions --- */ |
| 259 | case DEVINFO_FCT_SET_INFO: info->set_info = DEVICE_SET_INFO_NAME(ins8154); break; |
| 260 | case DEVINFO_FCT_START: info->start = DEVICE_START_NAME(ins8154); break; |
| 261 | case DEVINFO_FCT_STOP: /* Nothing */ break; |
| 262 | case DEVINFO_FCT_RESET: info->reset = DEVICE_RESET_NAME(ins8154); break; |
| 263 | |
| 264 | /* --- the following bits of info are returned as NULL-terminated strings --- */ |
| 265 | case DEVINFO_STR_NAME: info->s = "National Semiconductor INS8154"; break; |
| 266 | case DEVINFO_STR_FAMILY: info->s = "INS8154"; break; |
| 267 | case DEVINFO_STR_VERSION: info->s = "1.0"; break; |
| 268 | case DEVINFO_STR_SOURCE_FILE: info->s = __FILE__; break; |
| 269 | case DEVINFO_STR_CREDITS: info->s = "Copyright MESS Team"; break; |
| 270 | } |
| 271 | } |
src/mess/machine/ins8154.h
| r1984 | r1985 | |
| 9 | 9 | #ifndef INS8154_H_ |
| 10 | 10 | #define INS8154_H_ |
| 11 | 11 | |
| 12 | #define INS8154 DEVICE_GET_INFO_NAME(ins8154) |
| 12 | 13 | |
| 14 | |
| 13 | 15 | /******************* Interface **********************************************/ |
| 14 | 16 | |
| 17 | typedef struct _ins8154_t ins8154_t; |
| 15 | 18 | typedef struct _ins8154_interface ins8154_interface; |
| 19 | |
| 16 | 20 | struct _ins8154_interface |
| 17 | 21 | { |
| 18 | | read8_machine_func in_a_func; |
| 19 | | read8_machine_func in_b_func; |
| 20 | | write8_machine_func out_a_func; |
| 21 | | write8_machine_func out_b_func; |
| 22 | read8_device_func in_a_func; |
| 23 | read8_device_func in_b_func; |
| 24 | write8_device_func out_a_func; |
| 25 | write8_device_func out_b_func; |
| 22 | 26 | void (*irq_func)(int state); |
| 23 | 27 | }; |
| 24 | 28 | |
| 29 | DEVICE_GET_INFO( ins8154 ); |
| 25 | 30 | |
| 26 | | /******************* Configuration ******************************************/ |
| 27 | 31 | |
| 28 | | void ins8154_config(running_machine *machine, int which, const ins8154_interface *intf); |
| 29 | | void ins8154_reset(int which); |
| 30 | | |
| 31 | | |
| 32 | 32 | /******************* Standard 8-bit CPU interfaces, D0-D7 *******************/ |
| 33 | 33 | |
| 34 | | READ8_HANDLER( ins8154_0_r ); |
| 35 | | READ8_HANDLER( ins8154_1_r ); |
| 36 | | WRITE8_HANDLER( ins8154_0_w ); |
| 37 | | WRITE8_HANDLER( ins8154_1_w ); |
| 34 | READ8_DEVICE_HANDLER( ins8154_r ); |
| 35 | WRITE8_DEVICE_HANDLER( ins8154_w ); |
| 38 | 36 | |
| 39 | 37 | |
| 40 | 38 | /******************* 8-bit A/B port interfaces ******************************/ |
| 41 | 39 | |
| 42 | | READ8_HANDLER( ins8154_0_porta_r ); |
| 43 | | READ8_HANDLER( ins8154_0_portb_r ); |
| 44 | | READ8_HANDLER( ins8154_1_porta_r ); |
| 45 | | READ8_HANDLER( ins8154_1_portb_r ); |
| 46 | | WRITE8_HANDLER( ins8154_0_porta_w ); |
| 47 | | WRITE8_HANDLER( ins8154_0_portb_w ); |
| 48 | | WRITE8_HANDLER( ins8154_1_porta_w ); |
| 49 | | WRITE8_HANDLER( ins8154_1_portb_w ); |
| 40 | READ8_DEVICE_HANDLER( ins8154_porta_r ); |
| 41 | READ8_DEVICE_HANDLER( ins8154_portb_r ); |
| 42 | WRITE8_DEVICE_HANDLER( ins8154_porta_w ); |
| 43 | WRITE8_DEVICE_HANDLER( ins8154_portb_w ); |
| 50 | 44 | |
| 51 | 45 | |
| 52 | | #endif /*INS8154_H_*/ |
| 46 | #endif /* INS8154_H_ */ |