trunk/src/mame/machine/decocass_tape.c
| r24773 | r24774 | |
| 8 | 8 | #include "cpu/m6502/m6502.h" |
| 9 | 9 | #include "cpu/mcs48/mcs48.h" |
| 10 | 10 | #include "machine/decocass_tape.h" |
| 11 | | #include "devlegcy.h" |
| 12 | 11 | |
| 13 | 12 | #define LOG_CASSETTE_STATE 0 |
| 14 | 13 | |
| r24773 | r24774 | |
| 16 | 15 | CASSETTE DEVICE INTERFACE |
| 17 | 16 | ***************************************************************************/ |
| 18 | 17 | |
| 19 | | /* regions within the virtual tape */ |
| 20 | | enum tape_region |
| 21 | | { |
| 22 | | REGION_LEADER, /* in clear leader section */ |
| 23 | | REGION_LEADER_GAP, /* in gap between leader and BOT */ |
| 24 | | REGION_BOT, /* in BOT hole */ |
| 25 | | REGION_BOT_GAP, /* in gap between BOT hole and data */ |
| 26 | | REGION_DATA_BLOCK_0, /* in data block 0 */ |
| 27 | | REGION_DATA_BLOCK_255 = REGION_DATA_BLOCK_0 + 255, |
| 28 | | REGION_EOT_GAP, /* in gap between data and EOT hole */ |
| 29 | | REGION_EOT, /* in EOT hole */ |
| 30 | | REGION_TRAILER_GAP, /* in gap between trailer and EOT */ |
| 31 | | REGION_TRAILER /* in clear trailer section */ |
| 32 | | }; |
| 33 | | |
| 34 | | |
| 35 | | /* bytes within a data block on a virtual tape */ |
| 36 | | enum tape_byte |
| 37 | | { |
| 38 | | BYTE_PRE_GAP_0, /* 34 bytes of gap, clock held to 0, no data */ |
| 39 | | BYTE_PRE_GAP_33 = BYTE_PRE_GAP_0 + 33, |
| 40 | | BYTE_LEADIN, /* 1 leadin byte, clocked value 0x00 */ |
| 41 | | BYTE_HEADER, /* 1 header byte, clocked value 0xAA */ |
| 42 | | BYTE_DATA_0, /* 256 bytes of data, clocked */ |
| 43 | | BYTE_DATA_255 = BYTE_DATA_0 + 255, |
| 44 | | BYTE_CRC16_MSB, /* 2 bytes of CRC, clocked MSB first, then LSB */ |
| 45 | | BYTE_CRC16_LSB, |
| 46 | | BYTE_TRAILER, /* 1 trailer byte, clocked value 0xAA */ |
| 47 | | BYTE_LEADOUT, /* 1 leadout byte, clocked value 0x00 */ |
| 48 | | BYTE_LONGCLOCK, /* 1 longclock byte, clock held to 1, no data */ |
| 49 | | BYTE_POSTGAP_0, /* 34 bytes of gap, no clock, no data */ |
| 50 | | BYTE_POSTGAP_33 = BYTE_POSTGAP_0 + 33, |
| 51 | | BYTE_BLOCK_TOTAL /* total number of bytes in block */ |
| 52 | | }; |
| 53 | | |
| 54 | | |
| 55 | | /* state of the tape */ |
| 56 | | struct tape_state |
| 57 | | { |
| 58 | | running_machine * machine; /* pointer back to the machine */ |
| 59 | | emu_timer * timer; /* timer for running the tape */ |
| 60 | | INT8 speed; /* speed: <-1=fast rewind, -1=reverse, 0=stopped, 1=normal, >1=fast forward */ |
| 61 | | tape_region region; /* current region */ |
| 62 | | tape_byte bytenum; /* byte number within a datablock */ |
| 63 | | UINT8 bitnum; /* bit number within a byte */ |
| 64 | | UINT32 clockpos; /* the current clock position of the tape */ |
| 65 | | UINT32 numclocks; /* total number of clocks on the entire tape */ |
| 66 | | UINT16 crc16[256]; /* CRC16 for each block */ |
| 67 | | }; |
| 68 | | |
| 69 | | |
| 70 | 18 | /* number of tape clock pulses per second */ |
| 71 | 19 | #define TAPE_CLOCKRATE 4800 |
| 72 | 20 | #define TAPE_CLOCKS_PER_BIT 2 |
| r24773 | r24774 | |
| 104 | 52 | #define REGION_BOT_GAP_LEN_CLOCKS TAPE_MSEC_TO_CLOCKS(300) /* 300ms */ |
| 105 | 53 | #define REGION_BOT_GAP_END_CLOCK (REGION_BOT_GAP_START_CLOCK+REGION_BOT_GAP_LEN_CLOCKS) |
| 106 | 54 | |
| 55 | static UINT16 tape_crc16_byte(UINT16 crc, UINT8 data); |
| 107 | 56 | |
| 108 | | /*------------------------------------------------- |
| 109 | | get_safe_token - makes sure that the passed |
| 110 | | in device is, in fact, an IDE controller |
| 111 | | -------------------------------------------------*/ |
| 57 | const device_type DECOCASS_TAPE = &device_creator<decocass_tape_device>; |
| 112 | 58 | |
| 113 | | INLINE tape_state *get_safe_token(device_t *device) |
| 59 | decocass_tape_device::decocass_tape_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) |
| 60 | : device_t(mconfig, DECOCASS_TAPE, "DECO Cassette Tape", tag, owner, clock, "decocass_tape", __FILE__), |
| 61 | m_tape_timer(NULL), |
| 62 | m_speed(0), |
| 63 | m_bitnum(0), |
| 64 | m_clockpos(0), |
| 65 | m_numclocks(0) |
| 114 | 66 | { |
| 115 | | assert(device != NULL); |
| 116 | | assert(device->type() == DECOCASS_TAPE); |
| 67 | for (int i = 0; i < 256; i++) |
| 68 | m_crc16[i] = 0; |
| 69 | } |
| 117 | 70 | |
| 118 | | return (tape_state *)downcast<decocass_tape_device *>(device)->token(); |
| 71 | //------------------------------------------------- |
| 72 | // device_config_complete - perform any |
| 73 | // operations now that the configuration is |
| 74 | // complete |
| 75 | //------------------------------------------------- |
| 76 | |
| 77 | void decocass_tape_device::device_config_complete() |
| 78 | { |
| 119 | 79 | } |
| 120 | 80 | |
| 81 | //------------------------------------------------- |
| 82 | // device_start - device-specific startup |
| 83 | //------------------------------------------------- |
| 121 | 84 | |
| 85 | void decocass_tape_device::device_start() |
| 86 | { |
| 87 | int curblock, offs, numblocks; |
| 88 | |
| 89 | /* fetch the data pointer */ |
| 90 | m_tape_timer = machine().scheduler().timer_alloc(timer_expired_delegate(FUNC(decocass_tape_device::tape_clock_callback), this)); |
| 91 | if (region() == NULL) |
| 92 | return; |
| 93 | UINT8 *regionbase = *region(); |
| 94 | |
| 95 | /* scan for the first non-empty block in the image */ |
| 96 | for (offs = region()->bytes() - 1; offs >= 0; offs--) |
| 97 | if (regionbase[offs] != 0) |
| 98 | break; |
| 99 | numblocks = ((offs | 0xff) + 1) / 256; |
| 100 | assert(numblocks < ARRAY_LENGTH(m_crc16)); |
| 101 | |
| 102 | /* compute the total length */ |
| 103 | m_numclocks = REGION_BOT_GAP_END_CLOCK + numblocks * BYTE_BLOCK_TOTAL * 16 + REGION_BOT_GAP_END_CLOCK; |
| 104 | |
| 105 | /* compute CRCs for each block */ |
| 106 | for (curblock = 0; curblock < numblocks; curblock++) |
| 107 | { |
| 108 | UINT16 crc = 0; |
| 109 | int testval; |
| 110 | |
| 111 | /* first CRC the 256 bytes of data */ |
| 112 | for (offs = 256 * curblock; offs < 256 * curblock + 256; offs++) |
| 113 | crc = tape_crc16_byte(crc, regionbase[offs]); |
| 114 | |
| 115 | /* then find a pair of bytes that will bring the CRC to 0 (any better way than brute force?) */ |
| 116 | for (testval = 0; testval < 0x10000; testval++) |
| 117 | if (tape_crc16_byte(tape_crc16_byte(crc, testval >> 8), testval) == 0) |
| 118 | break; |
| 119 | m_crc16[curblock] = testval; |
| 120 | } |
| 121 | |
| 122 | /* register states */ |
| 123 | save_item(NAME(m_speed)); |
| 124 | save_item(NAME(m_bitnum)); |
| 125 | save_item(NAME(m_clockpos)); |
| 126 | } |
| 127 | |
| 128 | //------------------------------------------------- |
| 129 | // device_reset - device-specific reset |
| 130 | //------------------------------------------------- |
| 131 | |
| 132 | void decocass_tape_device::device_reset() |
| 133 | { |
| 134 | /* turn the tape off */ |
| 135 | change_speed(0); |
| 136 | } |
| 137 | |
| 122 | 138 | /*------------------------------------------------- |
| 123 | 139 | tape_crc16_byte - accumulate 8 bits worth of |
| 124 | 140 | CRC data |
| r24773 | r24774 | |
| 139 | 155 | return crc; |
| 140 | 156 | } |
| 141 | 157 | |
| 142 | | |
| 143 | 158 | /*------------------------------------------------- |
| 144 | 159 | tape_describe_state - create a string that |
| 145 | 160 | describes the state of the tape |
| 146 | 161 | -------------------------------------------------*/ |
| 147 | 162 | |
| 148 | | static const char *tape_describe_state(tape_state *tape) |
| 163 | const char *decocass_tape_device::describe_state() |
| 149 | 164 | { |
| 150 | 165 | static char buffer[40]; |
| 151 | 166 | char temprname[40]; |
| 152 | 167 | const char *rname = temprname; |
| 153 | 168 | |
| 154 | | if (tape->region == REGION_LEADER) |
| 169 | if (m_region == REGION_LEADER) |
| 155 | 170 | rname = "LEAD"; |
| 156 | | else if (tape->region == REGION_LEADER_GAP) |
| 171 | else if (m_region == REGION_LEADER_GAP) |
| 157 | 172 | rname = "LGAP"; |
| 158 | | else if (tape->region == REGION_BOT) |
| 173 | else if (m_region == REGION_BOT) |
| 159 | 174 | rname = "BOT "; |
| 160 | | else if (tape->region == REGION_BOT_GAP) |
| 175 | else if (m_region == REGION_BOT_GAP) |
| 161 | 176 | rname = "BGAP"; |
| 162 | | else if (tape->region == REGION_TRAILER) |
| 177 | else if (m_region == REGION_TRAILER) |
| 163 | 178 | rname = "TRLR"; |
| 164 | | else if (tape->region == REGION_TRAILER_GAP) |
| 179 | else if (m_region == REGION_TRAILER_GAP) |
| 165 | 180 | rname = "TGAP"; |
| 166 | | else if (tape->region == REGION_EOT) |
| 181 | else if (m_region == REGION_EOT) |
| 167 | 182 | rname = "EOT "; |
| 168 | | else if (tape->region == REGION_EOT_GAP) |
| 183 | else if (m_region == REGION_EOT_GAP) |
| 169 | 184 | rname = "EGAP"; |
| 170 | 185 | else |
| 171 | 186 | { |
| r24773 | r24774 | |
| 173 | 188 | const char *bname = tempbname; |
| 174 | 189 | int clk; |
| 175 | 190 | |
| 176 | | if (tape->bytenum <= BYTE_PRE_GAP_33) |
| 177 | | sprintf(tempbname, "PR%02d", tape->bytenum - BYTE_PRE_GAP_0); |
| 178 | | else if (tape->bytenum == BYTE_LEADIN) |
| 191 | if (m_bytenum <= BYTE_PRE_GAP_33) |
| 192 | sprintf(tempbname, "PR%02d", m_bytenum - BYTE_PRE_GAP_0); |
| 193 | else if (m_bytenum == BYTE_LEADIN) |
| 179 | 194 | bname = "LDIN"; |
| 180 | | else if (tape->bytenum == BYTE_HEADER) |
| 195 | else if (m_bytenum == BYTE_HEADER) |
| 181 | 196 | bname = "HEAD"; |
| 182 | | else if (tape->bytenum <= BYTE_DATA_255) |
| 183 | | sprintf(tempbname, "BY%02X", tape->bytenum - BYTE_DATA_0); |
| 184 | | else if (tape->bytenum == BYTE_CRC16_MSB) |
| 197 | else if (m_bytenum <= BYTE_DATA_255) |
| 198 | sprintf(tempbname, "BY%02X", m_bytenum - BYTE_DATA_0); |
| 199 | else if (m_bytenum == BYTE_CRC16_MSB) |
| 185 | 200 | bname = "CRCM"; |
| 186 | | else if (tape->bytenum == BYTE_CRC16_LSB) |
| 201 | else if (m_bytenum == BYTE_CRC16_LSB) |
| 187 | 202 | bname = "CRCL"; |
| 188 | | else if (tape->bytenum == BYTE_TRAILER) |
| 203 | else if (m_bytenum == BYTE_TRAILER) |
| 189 | 204 | bname = "TRLR"; |
| 190 | | else if (tape->bytenum == BYTE_LEADOUT) |
| 205 | else if (m_bytenum == BYTE_LEADOUT) |
| 191 | 206 | bname = "LOUT"; |
| 192 | | else if (tape->bytenum == BYTE_LONGCLOCK) |
| 207 | else if (m_bytenum == BYTE_LONGCLOCK) |
| 193 | 208 | bname = "LONG"; |
| 194 | 209 | else |
| 195 | | sprintf(tempbname, "PO%02d", tape->bytenum - BYTE_POSTGAP_0); |
| 210 | sprintf(tempbname, "PO%02d", m_bytenum - BYTE_POSTGAP_0); |
| 196 | 211 | |
| 197 | 212 | /* in the main data area, the clock alternates at the clock rate */ |
| 198 | | if (tape->bytenum >= BYTE_LEADIN && tape->bytenum <= BYTE_LEADOUT) |
| 199 | | clk = ((UINT32)(tape->clockpos - REGION_BOT_GAP_END_CLOCK) & 1) ? 0 : 1; |
| 200 | | else if (tape->bytenum == BYTE_LONGCLOCK) |
| 213 | if (m_bytenum >= BYTE_LEADIN && m_bytenum <= BYTE_LEADOUT) |
| 214 | clk = ((UINT32)(m_clockpos - REGION_BOT_GAP_END_CLOCK) & 1) ? 0 : 1; |
| 215 | else if (m_bytenum == BYTE_LONGCLOCK) |
| 201 | 216 | clk = 1; |
| 202 | 217 | else |
| 203 | 218 | clk = 0; |
| 204 | 219 | |
| 205 | | sprintf(temprname, "BL%02X.%4s.%d.%d", tape->region - REGION_DATA_BLOCK_0, bname, tape->bitnum, clk); |
| 220 | sprintf(temprname, "BL%02X.%4s.%d.%d", m_region - REGION_DATA_BLOCK_0, bname, m_bitnum, clk); |
| 206 | 221 | } |
| 207 | 222 | |
| 208 | | sprintf(buffer, "{%9d=%s}", tape->clockpos, rname); |
| 223 | sprintf(buffer, "{%9d=%s}", m_clockpos, rname); |
| 209 | 224 | return buffer; |
| 210 | 225 | } |
| 211 | 226 | |
| r24773 | r24774 | |
| 215 | 230 | to increment/decrement the tape location |
| 216 | 231 | -------------------------------------------------*/ |
| 217 | 232 | |
| 218 | | static TIMER_CALLBACK( tape_clock_callback ) |
| 233 | TIMER_CALLBACK_MEMBER( decocass_tape_device::tape_clock_callback ) |
| 219 | 234 | { |
| 220 | | device_t *device = (device_t *)ptr; |
| 221 | | tape_state *tape = get_safe_token(device); |
| 222 | | |
| 223 | 235 | /* advance by one clock in the desired direction */ |
| 224 | | if (tape->speed < 0 && tape->clockpos > 0) |
| 225 | | tape->clockpos--; |
| 226 | | else if (tape->speed > 0 && tape->clockpos < tape->numclocks) |
| 227 | | tape->clockpos++; |
| 236 | if (m_speed < 0 && m_clockpos > 0) |
| 237 | m_clockpos--; |
| 238 | else if (m_speed > 0 && m_clockpos < m_numclocks) |
| 239 | m_clockpos++; |
| 228 | 240 | |
| 229 | 241 | /* look for states before the start of data */ |
| 230 | | if (tape->clockpos < REGION_LEADER_END_CLOCK) |
| 231 | | tape->region = REGION_LEADER; |
| 232 | | else if (tape->clockpos < REGION_LEADER_GAP_END_CLOCK) |
| 233 | | tape->region = REGION_LEADER_GAP; |
| 234 | | else if (tape->clockpos < REGION_BOT_END_CLOCK) |
| 235 | | tape->region = REGION_BOT; |
| 236 | | else if (tape->clockpos < REGION_BOT_GAP_END_CLOCK) |
| 237 | | tape->region = REGION_BOT_GAP; |
| 242 | if (m_clockpos < REGION_LEADER_END_CLOCK) |
| 243 | m_region = REGION_LEADER; |
| 244 | else if (m_clockpos < REGION_LEADER_GAP_END_CLOCK) |
| 245 | m_region = REGION_LEADER_GAP; |
| 246 | else if (m_clockpos < REGION_BOT_END_CLOCK) |
| 247 | m_region = REGION_BOT; |
| 248 | else if (m_clockpos < REGION_BOT_GAP_END_CLOCK) |
| 249 | m_region = REGION_BOT_GAP; |
| 238 | 250 | |
| 239 | 251 | /* look for states after the end of data */ |
| 240 | | else if (tape->clockpos >= tape->numclocks - REGION_LEADER_END_CLOCK) |
| 241 | | tape->region = REGION_TRAILER; |
| 242 | | else if (tape->clockpos >= tape->numclocks - REGION_LEADER_GAP_END_CLOCK) |
| 243 | | tape->region = REGION_TRAILER_GAP; |
| 244 | | else if (tape->clockpos >= tape->numclocks - REGION_BOT_END_CLOCK) |
| 245 | | tape->region = REGION_EOT; |
| 246 | | else if (tape->clockpos >= tape->numclocks - REGION_BOT_GAP_END_CLOCK) |
| 247 | | tape->region = REGION_EOT_GAP; |
| 252 | else if (m_clockpos >= m_numclocks - REGION_LEADER_END_CLOCK) |
| 253 | m_region = REGION_TRAILER; |
| 254 | else if (m_clockpos >= m_numclocks - REGION_LEADER_GAP_END_CLOCK) |
| 255 | m_region = REGION_TRAILER_GAP; |
| 256 | else if (m_clockpos >= m_numclocks - REGION_BOT_END_CLOCK) |
| 257 | m_region = REGION_EOT; |
| 258 | else if (m_clockpos >= m_numclocks - REGION_BOT_GAP_END_CLOCK) |
| 259 | m_region = REGION_EOT_GAP; |
| 248 | 260 | |
| 249 | 261 | /* everything else is data */ |
| 250 | 262 | else |
| 251 | 263 | { |
| 252 | | UINT32 dataclock = tape->clockpos - REGION_BOT_GAP_END_CLOCK; |
| 264 | UINT32 dataclock = m_clockpos - REGION_BOT_GAP_END_CLOCK; |
| 253 | 265 | |
| 254 | 266 | /* compute the block number */ |
| 255 | | tape->region = (tape_region)(REGION_DATA_BLOCK_0 + dataclock / (TAPE_CLOCKS_PER_BYTE * BYTE_BLOCK_TOTAL)); |
| 256 | | dataclock -= (tape->region - REGION_DATA_BLOCK_0) * TAPE_CLOCKS_PER_BYTE * BYTE_BLOCK_TOTAL; |
| 267 | m_region = (tape_region)(REGION_DATA_BLOCK_0 + dataclock / (TAPE_CLOCKS_PER_BYTE * BYTE_BLOCK_TOTAL)); |
| 268 | dataclock -= (m_region - REGION_DATA_BLOCK_0) * TAPE_CLOCKS_PER_BYTE * BYTE_BLOCK_TOTAL; |
| 257 | 269 | |
| 258 | 270 | /* compute the byte within the block */ |
| 259 | | tape->bytenum = (tape_byte)(dataclock / TAPE_CLOCKS_PER_BYTE); |
| 260 | | dataclock -= tape->bytenum * TAPE_CLOCKS_PER_BYTE; |
| 271 | m_bytenum = (tape_byte)(dataclock / TAPE_CLOCKS_PER_BYTE); |
| 272 | dataclock -= m_bytenum * TAPE_CLOCKS_PER_BYTE; |
| 261 | 273 | |
| 262 | 274 | /* compute the bit within the byte */ |
| 263 | | tape->bitnum = dataclock / TAPE_CLOCKS_PER_BIT; |
| 275 | m_bitnum = dataclock / TAPE_CLOCKS_PER_BIT; |
| 264 | 276 | } |
| 265 | 277 | |
| 266 | 278 | /* log */ |
| 267 | 279 | if (LOG_CASSETTE_STATE) |
| 268 | | tape_describe_state(tape); |
| 280 | describe_state(); |
| 269 | 281 | } |
| 270 | 282 | |
| 271 | 283 | |
| r24773 | r24774 | |
| 274 | 286 | bits from the tape |
| 275 | 287 | -------------------------------------------------*/ |
| 276 | 288 | |
| 277 | | UINT8 tape_get_status_bits(device_t *device) |
| 289 | UINT8 decocass_tape_device::get_status_bits() |
| 278 | 290 | { |
| 279 | | tape_state *tape = get_safe_token(device); |
| 280 | 291 | UINT8 tape_bits = 0; |
| 281 | 292 | |
| 282 | 293 | /* bit 0x20 is the BOT/EOT signal, which is also set in the leader/trailer area */ |
| 283 | | if (tape->region == REGION_LEADER || tape->region == REGION_BOT || tape->region == REGION_EOT || tape->region == REGION_TRAILER) |
| 294 | if (m_region == REGION_LEADER || m_region == REGION_BOT || m_region == REGION_EOT || m_region == REGION_TRAILER) |
| 284 | 295 | tape_bits |= 0x20; |
| 285 | 296 | |
| 286 | 297 | /* bit 0x40 is the clock, which is only valid in some areas of the data block */ |
| 287 | 298 | /* bit 0x80 is the data, which is only valid in some areas of the data block */ |
| 288 | | if (tape->region >= REGION_DATA_BLOCK_0 && tape->region <= REGION_DATA_BLOCK_255) |
| 299 | if (m_region >= REGION_DATA_BLOCK_0 && m_region <= REGION_DATA_BLOCK_255) |
| 289 | 300 | { |
| 290 | | int blocknum = tape->region - REGION_DATA_BLOCK_0; |
| 301 | int blocknum = m_region - REGION_DATA_BLOCK_0; |
| 291 | 302 | UINT8 byteval = 0x00; |
| 292 | 303 | |
| 293 | 304 | /* in the main data area, the clock alternates at the clock rate */ |
| 294 | | if (tape->bytenum >= BYTE_LEADIN && tape->bytenum <= BYTE_LEADOUT) |
| 295 | | tape_bits |= ((UINT32)(tape->clockpos - REGION_BOT_GAP_END_CLOCK) & 1) ? 0x00 : 0x40; |
| 305 | if (m_bytenum >= BYTE_LEADIN && m_bytenum <= BYTE_LEADOUT) |
| 306 | tape_bits |= ((UINT32)(m_clockpos - REGION_BOT_GAP_END_CLOCK) & 1) ? 0x00 : 0x40; |
| 296 | 307 | |
| 297 | 308 | /* in the longclock area, the clock holds high */ |
| 298 | | else if (tape->bytenum == BYTE_LONGCLOCK) |
| 309 | else if (m_bytenum == BYTE_LONGCLOCK) |
| 299 | 310 | tape_bits |= 0x40; |
| 300 | 311 | |
| 301 | 312 | /* everywhere else, the clock holds to 0 */ |
| r24773 | r24774 | |
| 303 | 314 | ; |
| 304 | 315 | |
| 305 | 316 | /* lead-in and lead-out bytes are 0xAA */ |
| 306 | | if (tape->bytenum == BYTE_HEADER || tape->bytenum == BYTE_TRAILER) |
| 317 | if (m_bytenum == BYTE_HEADER || m_bytenum == BYTE_TRAILER) |
| 307 | 318 | byteval = 0xaa; |
| 308 | 319 | |
| 309 | 320 | /* data block bytes are data */ |
| 310 | | else if (tape->bytenum >= BYTE_DATA_0 && tape->bytenum <= BYTE_DATA_255) |
| 311 | | byteval = static_cast<UINT8 *>(*device->region())[blocknum * 256 + (tape->bytenum - BYTE_DATA_0)]; |
| 321 | else if (m_bytenum >= BYTE_DATA_0 && m_bytenum <= BYTE_DATA_255) |
| 322 | byteval = static_cast<UINT8 *>(*region())[blocknum * 256 + (m_bytenum - BYTE_DATA_0)]; |
| 312 | 323 | |
| 313 | 324 | /* CRC MSB */ |
| 314 | | else if (tape->bytenum == BYTE_CRC16_MSB) |
| 315 | | byteval = tape->crc16[blocknum] >> 8; |
| 325 | else if (m_bytenum == BYTE_CRC16_MSB) |
| 326 | byteval = m_crc16[blocknum] >> 8; |
| 316 | 327 | |
| 317 | 328 | /* CRC LSB */ |
| 318 | | else if (tape->bytenum == BYTE_CRC16_LSB) |
| 319 | | byteval = tape->crc16[blocknum]; |
| 329 | else if (m_bytenum == BYTE_CRC16_LSB) |
| 330 | byteval = m_crc16[blocknum]; |
| 320 | 331 | |
| 321 | 332 | /* select the appropriate bit from the byte and move to the upper bit */ |
| 322 | | if ((byteval >> tape->bitnum) & 1) |
| 333 | if ((byteval >> m_bitnum) & 1) |
| 323 | 334 | tape_bits |= 0x80; |
| 324 | 335 | } |
| 325 | 336 | return tape_bits; |
| r24773 | r24774 | |
| 331 | 342 | present |
| 332 | 343 | -------------------------------------------------*/ |
| 333 | 344 | |
| 334 | | UINT8 tape_is_present(device_t *device) |
| 345 | UINT8 decocass_tape_device::is_present() |
| 335 | 346 | { |
| 336 | | return device->region() != NULL; |
| 347 | return region() != NULL; |
| 337 | 348 | } |
| 338 | 349 | |
| 339 | 350 | |
| r24773 | r24774 | |
| 342 | 353 | playback |
| 343 | 354 | -------------------------------------------------*/ |
| 344 | 355 | |
| 345 | | void tape_change_speed(device_t *device, INT8 newspeed) |
| 356 | void decocass_tape_device::change_speed(INT8 newspeed) |
| 346 | 357 | { |
| 347 | | tape_state *tape = get_safe_token(device); |
| 348 | 358 | attotime newperiod; |
| 349 | 359 | INT8 absnewspeed; |
| 350 | 360 | |
| 351 | 361 | /* do nothing if speed has not changed */ |
| 352 | | if (tape->speed == newspeed) |
| 362 | if (m_speed == newspeed) |
| 353 | 363 | return; |
| 354 | 364 | |
| 355 | 365 | /* compute how fast to run the tape timer */ |
| r24773 | r24774 | |
| 360 | 370 | newperiod = attotime::from_hz(TAPE_CLOCKRATE * absnewspeed); |
| 361 | 371 | |
| 362 | 372 | /* set the new speed */ |
| 363 | | tape->timer->adjust(newperiod, 0, newperiod); |
| 364 | | tape->speed = newspeed; |
| 373 | m_tape_timer->adjust(newperiod, 0, newperiod); |
| 374 | m_speed = newspeed; |
| 365 | 375 | } |
| 366 | | |
| 367 | | |
| 368 | | /*------------------------------------------------- |
| 369 | | device start callback |
| 370 | | -------------------------------------------------*/ |
| 371 | | |
| 372 | | static DEVICE_START( decocass_tape ) |
| 373 | | { |
| 374 | | tape_state *tape = get_safe_token(device); |
| 375 | | int curblock, offs, numblocks; |
| 376 | | |
| 377 | | /* validate some basic stuff */ |
| 378 | | assert(device != NULL); |
| 379 | | assert(device->static_config() == NULL); |
| 380 | | |
| 381 | | /* fetch the data pointer */ |
| 382 | | tape->timer = device->machine().scheduler().timer_alloc(FUNC(tape_clock_callback), (void *)device); |
| 383 | | if (device->region() == NULL) |
| 384 | | return; |
| 385 | | UINT8 *regionbase = *device->region(); |
| 386 | | |
| 387 | | /* scan for the first non-empty block in the image */ |
| 388 | | for (offs = device->region()->bytes() - 1; offs >= 0; offs--) |
| 389 | | if (regionbase[offs] != 0) |
| 390 | | break; |
| 391 | | numblocks = ((offs | 0xff) + 1) / 256; |
| 392 | | assert(numblocks < ARRAY_LENGTH(tape->crc16)); |
| 393 | | |
| 394 | | /* compute the total length */ |
| 395 | | tape->numclocks = REGION_BOT_GAP_END_CLOCK + numblocks * BYTE_BLOCK_TOTAL * 16 + REGION_BOT_GAP_END_CLOCK; |
| 396 | | |
| 397 | | /* compute CRCs for each block */ |
| 398 | | for (curblock = 0; curblock < numblocks; curblock++) |
| 399 | | { |
| 400 | | UINT16 crc = 0; |
| 401 | | int testval; |
| 402 | | |
| 403 | | /* first CRC the 256 bytes of data */ |
| 404 | | for (offs = 256 * curblock; offs < 256 * curblock + 256; offs++) |
| 405 | | crc = tape_crc16_byte(crc, regionbase[offs]); |
| 406 | | |
| 407 | | /* then find a pair of bytes that will bring the CRC to 0 (any better way than brute force?) */ |
| 408 | | for (testval = 0; testval < 0x10000; testval++) |
| 409 | | if (tape_crc16_byte(tape_crc16_byte(crc, testval >> 8), testval) == 0) |
| 410 | | break; |
| 411 | | tape->crc16[curblock] = testval; |
| 412 | | } |
| 413 | | |
| 414 | | /* register states */ |
| 415 | | device->save_item(NAME(tape->speed)); |
| 416 | | device->save_item(NAME(tape->bitnum)); |
| 417 | | device->save_item(NAME(tape->clockpos)); |
| 418 | | } |
| 419 | | |
| 420 | | |
| 421 | | /*------------------------------------------------- |
| 422 | | device reset callback |
| 423 | | -------------------------------------------------*/ |
| 424 | | |
| 425 | | static DEVICE_RESET( decocass_tape ) |
| 426 | | { |
| 427 | | /* turn the tape off */ |
| 428 | | tape_change_speed(device, 0); |
| 429 | | } |
| 430 | | |
| 431 | | |
| 432 | | const device_type DECOCASS_TAPE = &device_creator<decocass_tape_device>; |
| 433 | | |
| 434 | | decocass_tape_device::decocass_tape_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock) |
| 435 | | : device_t(mconfig, DECOCASS_TAPE, "DECO Cassette Tape", tag, owner, clock, "decocass_tape", __FILE__) |
| 436 | | { |
| 437 | | m_token = global_alloc_clear(tape_state); |
| 438 | | } |
| 439 | | |
| 440 | | //------------------------------------------------- |
| 441 | | // device_config_complete - perform any |
| 442 | | // operations now that the configuration is |
| 443 | | // complete |
| 444 | | //------------------------------------------------- |
| 445 | | |
| 446 | | void decocass_tape_device::device_config_complete() |
| 447 | | { |
| 448 | | } |
| 449 | | |
| 450 | | //------------------------------------------------- |
| 451 | | // device_start - device-specific startup |
| 452 | | //------------------------------------------------- |
| 453 | | |
| 454 | | void decocass_tape_device::device_start() |
| 455 | | { |
| 456 | | DEVICE_START_NAME( decocass_tape )(this); |
| 457 | | } |
| 458 | | |
| 459 | | //------------------------------------------------- |
| 460 | | // device_reset - device-specific reset |
| 461 | | //------------------------------------------------- |
| 462 | | |
| 463 | | void decocass_tape_device::device_reset() |
| 464 | | { |
| 465 | | DEVICE_RESET_NAME( decocass_tape )(this); |
| 466 | | } |