src/mame/machine/snessdd1.c
| r0 | r5570 | |
| 1 | static UINT8 sdd1_read(running_machine* machine, UINT16 addr); |
| 2 | |
| 3 | typedef struct //Input Manager |
| 4 | { |
| 5 | running_machine* machine; |
| 6 | UINT32 byte_ptr; |
| 7 | UINT8 bit_count; |
| 8 | } SDD1_IM; |
| 9 | |
| 10 | static SDD1_IM* SDD1_IM_ctor(running_machine* machine) |
| 11 | { |
| 12 | SDD1_IM* newclass = (SDD1_IM*)auto_alloc_array(machine, UINT8, sizeof(SDD1_IM)); |
| 13 | newclass->machine = machine; |
| 14 | return newclass; |
| 15 | } |
| 16 | |
| 17 | static void SDD1_IM_prepareDecomp(SDD1_IM* this, UINT32 in_buf) |
| 18 | { |
| 19 | this->byte_ptr = in_buf; |
| 20 | this->bit_count = 4; |
| 21 | } |
| 22 | |
| 23 | static UINT8 SDD1_IM_getCodeword(SDD1_IM* this, const UINT8 code_len) |
| 24 | { |
| 25 | UINT8 codeword; |
| 26 | |
| 27 | codeword = sdd1_read(this->machine, this->byte_ptr) << this->bit_count; |
| 28 | |
| 29 | ++this->bit_count; |
| 30 | |
| 31 | if (codeword & 0x80) |
| 32 | { |
| 33 | codeword |= sdd1_read(this->machine, this->byte_ptr + 1) >> (9 - this->bit_count); |
| 34 | this->bit_count += code_len; |
| 35 | } |
| 36 | |
| 37 | if (this->bit_count & 0x08) |
| 38 | { |
| 39 | this->byte_ptr++; |
| 40 | this->bit_count &= 0x07; |
| 41 | } |
| 42 | |
| 43 | return codeword; |
| 44 | } |
| 45 | |
| 46 | typedef struct //Golomb-Code Decoder |
| 47 | { |
| 48 | running_machine* machine; |
| 49 | SDD1_IM* IM; |
| 50 | } SDD1_GCD; |
| 51 | |
| 52 | static SDD1_GCD* SDD1_GCD_ctor(running_machine* machine, SDD1_IM* associatedIM) |
| 53 | { |
| 54 | SDD1_GCD* newclass = (SDD1_GCD*)auto_alloc_array(machine, UINT8, sizeof(SDD1_GCD)); |
| 55 | newclass->machine = machine; |
| 56 | newclass->IM = associatedIM; |
| 57 | return newclass; |
| 58 | } |
| 59 | |
| 60 | static void SDD1_GCD_getRunCount(SDD1_GCD* this, UINT8 code_num, UINT8* MPScount, UINT8* LPSind) |
| 61 | { |
| 62 | const UINT8 run_count[] = |
| 63 | { |
| 64 | 0x00, 0x00, 0x01, 0x00, 0x03, 0x01, 0x02, 0x00, |
| 65 | 0x07, 0x03, 0x05, 0x01, 0x06, 0x02, 0x04, 0x00, |
| 66 | 0x0f, 0x07, 0x0b, 0x03, 0x0d, 0x05, 0x09, 0x01, |
| 67 | 0x0e, 0x06, 0x0a, 0x02, 0x0c, 0x04, 0x08, 0x00, |
| 68 | 0x1f, 0x0f, 0x17, 0x07, 0x1b, 0x0b, 0x13, 0x03, |
| 69 | 0x1d, 0x0d, 0x15, 0x05, 0x19, 0x09, 0x11, 0x01, |
| 70 | 0x1e, 0x0e, 0x16, 0x06, 0x1a, 0x0a, 0x12, 0x02, |
| 71 | 0x1c, 0x0c, 0x14, 0x04, 0x18, 0x08, 0x10, 0x00, |
| 72 | 0x3f, 0x1f, 0x2f, 0x0f, 0x37, 0x17, 0x27, 0x07, |
| 73 | 0x3b, 0x1b, 0x2b, 0x0b, 0x33, 0x13, 0x23, 0x03, |
| 74 | 0x3d, 0x1d, 0x2d, 0x0d, 0x35, 0x15, 0x25, 0x05, |
| 75 | 0x39, 0x19, 0x29, 0x09, 0x31, 0x11, 0x21, 0x01, |
| 76 | 0x3e, 0x1e, 0x2e, 0x0e, 0x36, 0x16, 0x26, 0x06, |
| 77 | 0x3a, 0x1a, 0x2a, 0x0a, 0x32, 0x12, 0x22, 0x02, |
| 78 | 0x3c, 0x1c, 0x2c, 0x0c, 0x34, 0x14, 0x24, 0x04, |
| 79 | 0x38, 0x18, 0x28, 0x08, 0x30, 0x10, 0x20, 0x00, |
| 80 | 0x7f, 0x3f, 0x5f, 0x1f, 0x6f, 0x2f, 0x4f, 0x0f, |
| 81 | 0x77, 0x37, 0x57, 0x17, 0x67, 0x27, 0x47, 0x07, |
| 82 | 0x7b, 0x3b, 0x5b, 0x1b, 0x6b, 0x2b, 0x4b, 0x0b, |
| 83 | 0x73, 0x33, 0x53, 0x13, 0x63, 0x23, 0x43, 0x03, |
| 84 | 0x7d, 0x3d, 0x5d, 0x1d, 0x6d, 0x2d, 0x4d, 0x0d, |
| 85 | 0x75, 0x35, 0x55, 0x15, 0x65, 0x25, 0x45, 0x05, |
| 86 | 0x79, 0x39, 0x59, 0x19, 0x69, 0x29, 0x49, 0x09, |
| 87 | 0x71, 0x31, 0x51, 0x11, 0x61, 0x21, 0x41, 0x01, |
| 88 | 0x7e, 0x3e, 0x5e, 0x1e, 0x6e, 0x2e, 0x4e, 0x0e, |
| 89 | 0x76, 0x36, 0x56, 0x16, 0x66, 0x26, 0x46, 0x06, |
| 90 | 0x7a, 0x3a, 0x5a, 0x1a, 0x6a, 0x2a, 0x4a, 0x0a, |
| 91 | 0x72, 0x32, 0x52, 0x12, 0x62, 0x22, 0x42, 0x02, |
| 92 | 0x7c, 0x3c, 0x5c, 0x1c, 0x6c, 0x2c, 0x4c, 0x0c, |
| 93 | 0x74, 0x34, 0x54, 0x14, 0x64, 0x24, 0x44, 0x04, |
| 94 | 0x78, 0x38, 0x58, 0x18, 0x68, 0x28, 0x48, 0x08, |
| 95 | 0x70, 0x30, 0x50, 0x10, 0x60, 0x20, 0x40, 0x00, |
| 96 | }; |
| 97 | |
| 98 | UINT8 codeword = SDD1_IM_getCodeword(this->IM, code_num); |
| 99 | |
| 100 | if (codeword & 0x80) |
| 101 | { |
| 102 | *LPSind = 1; |
| 103 | *MPScount = run_count[codeword >> (code_num ^ 0x07)]; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | *MPScount = (1 << code_num); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | typedef struct // Bits Generator |
| 112 | { |
| 113 | running_machine* machine; |
| 114 | UINT8 code_num; |
| 115 | UINT8 MPScount; |
| 116 | UINT8 LPSind; |
| 117 | SDD1_GCD* GCD; |
| 118 | } SDD1_BG; |
| 119 | |
| 120 | static SDD1_BG* SDD1_BG_ctor(running_machine* machine, SDD1_GCD* associatedGCD, UINT8 code) |
| 121 | { |
| 122 | SDD1_BG* newclass = (SDD1_BG*)auto_alloc_array(machine, UINT8, sizeof(SDD1_BG)); |
| 123 | newclass->machine = machine; |
| 124 | newclass->code_num = code; |
| 125 | newclass->GCD = associatedGCD; |
| 126 | return newclass; |
| 127 | } |
| 128 | |
| 129 | static void SDD1_BG_prepareDecomp(SDD1_BG* this) |
| 130 | { |
| 131 | this->MPScount = 0; |
| 132 | this->LPSind = 0; |
| 133 | } |
| 134 | |
| 135 | static UINT8 SDD1_BG_getBit(SDD1_BG* this, UINT8* endOfRun) |
| 136 | { |
| 137 | UINT8 bit; |
| 138 | |
| 139 | if (!(this->MPScount || this->LPSind)) |
| 140 | { |
| 141 | SDD1_GCD_getRunCount(this->GCD, this->code_num, &(this->MPScount), &(this->LPSind)); |
| 142 | } |
| 143 | |
| 144 | if (this->MPScount) |
| 145 | { |
| 146 | bit = 0; |
| 147 | this->MPScount--; |
| 148 | } |
| 149 | else |
| 150 | { |
| 151 | bit = 1; |
| 152 | this->LPSind = 0; |
| 153 | } |
| 154 | |
| 155 | if (this->MPScount || this->LPSind) |
| 156 | { |
| 157 | (*endOfRun) = 0; |
| 158 | } |
| 159 | else |
| 160 | { |
| 161 | (*endOfRun) = 1; |
| 162 | } |
| 163 | |
| 164 | return bit; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | typedef struct |
| 169 | { |
| 170 | UINT8 code_num; |
| 171 | UINT8 nextIfMPS; |
| 172 | UINT8 nextIfLPS; |
| 173 | } SDD1_PEM_state; |
| 174 | |
| 175 | SDD1_PEM_state SDD1_PEM_evolution_table[33] = |
| 176 | { |
| 177 | { 0,25,25}, |
| 178 | { 0, 2, 1}, |
| 179 | { 0, 3, 1}, |
| 180 | { 0, 4, 2}, |
| 181 | { 0, 5, 3}, |
| 182 | { 1, 6, 4}, |
| 183 | { 1, 7, 5}, |
| 184 | { 1, 8, 6}, |
| 185 | { 1, 9, 7}, |
| 186 | { 2,10, 8}, |
| 187 | { 2,11, 9}, |
| 188 | { 2,12,10}, |
| 189 | { 2,13,11}, |
| 190 | { 3,14,12}, |
| 191 | { 3,15,13}, |
| 192 | { 3,16,14}, |
| 193 | { 3,17,15}, |
| 194 | { 4,18,16}, |
| 195 | { 4,19,17}, |
| 196 | { 5,20,18}, |
| 197 | { 5,21,19}, |
| 198 | { 6,22,20}, |
| 199 | { 6,23,21}, |
| 200 | { 7,24,22}, |
| 201 | { 7,24,23}, |
| 202 | { 0,26, 1}, |
| 203 | { 1,27, 2}, |
| 204 | { 2,28, 4}, |
| 205 | { 3,29, 8}, |
| 206 | { 4,30,12}, |
| 207 | { 5,31,16}, |
| 208 | { 6,32,18}, |
| 209 | { 7,24,22} |
| 210 | }; |
| 211 | |
| 212 | typedef struct |
| 213 | { |
| 214 | UINT8 status; |
| 215 | UINT8 MPS; |
| 216 | } SDD1_PEM_ContextInfo; |
| 217 | |
| 218 | typedef struct //Probability Estimation Module |
| 219 | { |
| 220 | running_machine* machine; |
| 221 | SDD1_PEM_ContextInfo contextInfo[32]; |
| 222 | SDD1_BG* BG[8]; |
| 223 | } SDD1_PEM; |
| 224 | |
| 225 | static SDD1_PEM* SDD1_PEM_ctor(running_machine* machine, |
| 226 | SDD1_BG* associatedBG0, SDD1_BG* associatedBG1, |
| 227 | SDD1_BG* associatedBG2, SDD1_BG* associatedBG3, |
| 228 | SDD1_BG* associatedBG4, SDD1_BG* associatedBG5, |
| 229 | SDD1_BG* associatedBG6, SDD1_BG* associatedBG7) |
| 230 | { |
| 231 | SDD1_PEM* newclass = (SDD1_PEM*)auto_alloc_array(machine, UINT8, sizeof(SDD1_PEM)); |
| 232 | newclass->machine = machine; |
| 233 | newclass->BG[0] = associatedBG0; |
| 234 | newclass->BG[1] = associatedBG1; |
| 235 | newclass->BG[2] = associatedBG2; |
| 236 | newclass->BG[3] = associatedBG3; |
| 237 | newclass->BG[4] = associatedBG4; |
| 238 | newclass->BG[5] = associatedBG5; |
| 239 | newclass->BG[6] = associatedBG6; |
| 240 | newclass->BG[7] = associatedBG7; |
| 241 | return newclass; |
| 242 | } |
| 243 | |
| 244 | static void SDD1_PEM_prepareDecomp(SDD1_PEM* this) |
| 245 | { |
| 246 | UINT8 i; |
| 247 | for(i = 0; i < 32; i++) |
| 248 | { |
| 249 | this->contextInfo[i].status = 0; |
| 250 | this->contextInfo[i].MPS = 0; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | static UINT8 SDD1_PEM_getBit(SDD1_PEM* this, UINT8 context) |
| 255 | { |
| 256 | UINT8 endOfRun; |
| 257 | UINT8 bit; |
| 258 | |
| 259 | SDD1_PEM_ContextInfo *pContInfo = &(this->contextInfo)[context]; |
| 260 | UINT8 currStatus = pContInfo->status; |
| 261 | SDD1_PEM_state* pState = &(SDD1_PEM_evolution_table[currStatus]); |
| 262 | UINT8 currentMPS = pContInfo->MPS; |
| 263 | |
| 264 | bit = SDD1_BG_getBit(this->BG[pState->code_num], &endOfRun); |
| 265 | |
| 266 | if (endOfRun) |
| 267 | { |
| 268 | if (bit) |
| 269 | { |
| 270 | if (!(currStatus & 0xfe)) |
| 271 | { |
| 272 | (pContInfo->MPS) ^= 0x01; |
| 273 | } |
| 274 | pContInfo->status = pState->nextIfLPS; |
| 275 | } |
| 276 | else |
| 277 | { |
| 278 | pContInfo->status = pState->nextIfMPS; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return bit ^ currentMPS; |
| 283 | } |
| 284 | |
| 285 | typedef struct |
| 286 | { |
| 287 | running_machine* machine; |
| 288 | UINT8 bitplanesInfo; |
| 289 | UINT8 contextBitsInfo; |
| 290 | UINT8 bit_number; |
| 291 | UINT8 currBitplane; |
| 292 | UINT16 prevBitplaneBits[8]; |
| 293 | SDD1_PEM* PEM; |
| 294 | } SDD1_CM; |
| 295 | |
| 296 | static SDD1_CM* SDD1_CM_ctor(running_machine* machine, SDD1_PEM* associatedPEM) |
| 297 | { |
| 298 | SDD1_CM* newclass = (SDD1_CM*)auto_alloc_array(machine, UINT8, sizeof(SDD1_CM)); |
| 299 | newclass->machine = machine; |
| 300 | newclass->PEM = associatedPEM; |
| 301 | return newclass; |
| 302 | } |
| 303 | |
| 304 | static void SDD1_CM_prepareDecomp(SDD1_CM* this, UINT32 first_byte) |
| 305 | { |
| 306 | INT32 i = 0; |
| 307 | this->bitplanesInfo = sdd1_read(this->machine, first_byte) & 0xc0; |
| 308 | this->contextBitsInfo = sdd1_read(this->machine, first_byte) & 0x30; |
| 309 | this->bit_number = 0; |
| 310 | for (i = 0; i < 8; i++) |
| 311 | { |
| 312 | this->prevBitplaneBits[i] = 0; |
| 313 | } |
| 314 | switch(this->bitplanesInfo) |
| 315 | { |
| 316 | case 0x00: |
| 317 | this->currBitplane = 1; |
| 318 | break; |
| 319 | case 0x40: |
| 320 | this->currBitplane = 7; |
| 321 | break; |
| 322 | case 0x80: |
| 323 | this->currBitplane = 3; |
| 324 | break; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | static UINT8 SDD1_CM_getBit(SDD1_CM* this) |
| 329 | { |
| 330 | UINT8 currContext; |
| 331 | UINT16 *context_bits; |
| 332 | UINT8 bit = 0; |
| 333 | |
| 334 | switch (this->bitplanesInfo) |
| 335 | { |
| 336 | case 0x00: |
| 337 | this->currBitplane ^= 0x01; |
| 338 | break; |
| 339 | case 0x40: |
| 340 | this->currBitplane ^= 0x01; |
| 341 | if (!(this->bit_number & 0x7f)) |
| 342 | { |
| 343 | this->currBitplane = ((this->currBitplane + 2) & 0x07); |
| 344 | } |
| 345 | break; |
| 346 | case 0x80: |
| 347 | this->currBitplane ^= 0x01; |
| 348 | if (!(this->bit_number & 0x7f)) |
| 349 | { |
| 350 | this->currBitplane ^= 0x02; |
| 351 | } |
| 352 | break; |
| 353 | case 0xc0: |
| 354 | this->currBitplane = this->bit_number & 0x07; |
| 355 | break; |
| 356 | } |
| 357 | |
| 358 | context_bits = &(this->prevBitplaneBits)[this->currBitplane]; |
| 359 | |
| 360 | currContext = (this->currBitplane & 0x01) << 4; |
| 361 | switch (this->contextBitsInfo) |
| 362 | { |
| 363 | case 0x00: |
| 364 | currContext |= ((*context_bits & 0x01c0) >> 5) | (*context_bits & 0x0001); |
| 365 | break; |
| 366 | case 0x10: |
| 367 | currContext |= ((*context_bits & 0x0180) >> 5) | (*context_bits & 0x0001); |
| 368 | break; |
| 369 | case 0x20: |
| 370 | currContext |= ((*context_bits & 0x00c0) >> 5) | (*context_bits & 0x0001); |
| 371 | break; |
| 372 | case 0x30: |
| 373 | currContext |= ((*context_bits & 0x0180) >> 5) | (*context_bits & 0x0003); |
| 374 | break; |
| 375 | } |
| 376 | |
| 377 | bit = SDD1_PEM_getBit(this->PEM, currContext); |
| 378 | |
| 379 | *context_bits <<= 1; |
| 380 | *context_bits |= bit; |
| 381 | |
| 382 | this->bit_number++; |
| 383 | |
| 384 | return bit; |
| 385 | } |
| 386 | |
| 387 | typedef struct |
| 388 | { |
| 389 | running_machine* machine; |
| 390 | UINT8 bitplanesInfo; |
| 391 | UINT16 length; |
| 392 | UINT8* buffer; |
| 393 | SDD1_CM* CM; |
| 394 | } SDD1_OL; |
| 395 | |
| 396 | static SDD1_OL* SDD1_OL_ctor(running_machine* machine, SDD1_CM* associatedCM) |
| 397 | { |
| 398 | SDD1_OL* newclass = (SDD1_OL*)auto_alloc_array(machine, UINT8, sizeof(SDD1_OL)); |
| 399 | newclass->machine = machine; |
| 400 | newclass->CM = associatedCM; |
| 401 | return newclass; |
| 402 | } |
| 403 | |
| 404 | static void SDD1_OL_prepareDecomp(SDD1_OL* this, UINT32 first_byte, UINT16 out_len, UINT8 *out_buf) |
| 405 | { |
| 406 | this->bitplanesInfo = sdd1_read(this->machine, first_byte) & 0xc0; |
| 407 | this->length = out_len; |
| 408 | this->buffer = out_buf; |
| 409 | } |
| 410 | |
| 411 | static void SDD1_OL_launch(SDD1_OL* this) |
| 412 | { |
| 413 | UINT8 i; |
| 414 | UINT8 register1, register2; |
| 415 | |
| 416 | switch(this->bitplanesInfo) |
| 417 | { |
| 418 | case 0x00: |
| 419 | case 0x40: |
| 420 | case 0x80: |
| 421 | i = 1; |
| 422 | do |
| 423 | { // if length == 0, we output 2^16 bytes |
| 424 | if(!i) |
| 425 | { |
| 426 | *(this->buffer++) = register2; |
| 427 | i = ~i; |
| 428 | } |
| 429 | else |
| 430 | { |
| 431 | for(register1 = register2 = 0, i = 0x80; i; i >>= 1) |
| 432 | { |
| 433 | if(SDD1_CM_getBit(this->CM)) |
| 434 | { |
| 435 | register1 |= i; |
| 436 | } |
| 437 | if(SDD1_CM_getBit(this->CM)) |
| 438 | { |
| 439 | register2 |= i; |
| 440 | } |
| 441 | } |
| 442 | *(this->buffer++) = register1; |
| 443 | } |
| 444 | } while(--(this->length)); |
| 445 | break; |
| 446 | case 0xc0: |
| 447 | do |
| 448 | { |
| 449 | for(register1 = 0, i = 0x01; i; i <<= 1) |
| 450 | { |
| 451 | if(SDD1_CM_getBit(this->CM)) |
| 452 | { |
| 453 | register1 |= i; |
| 454 | } |
| 455 | } |
| 456 | *(this->buffer++) = register1; |
| 457 | } while(--(this->length)); |
| 458 | break; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | typedef struct |
| 463 | { |
| 464 | running_machine* machine; |
| 465 | SDD1_IM* IM; |
| 466 | SDD1_GCD* GCD; |
| 467 | SDD1_BG* BG0; SDD1_BG* BG1; SDD1_BG* BG2; SDD1_BG* BG3; |
| 468 | SDD1_BG* BG4; SDD1_BG* BG5; SDD1_BG* BG6; SDD1_BG* BG7; |
| 469 | SDD1_PEM* PEM; |
| 470 | SDD1_CM* CM; |
| 471 | SDD1_OL* OL; |
| 472 | } SDD1emu; |
| 473 | |
| 474 | static SDD1emu* SDD1emu_ctor(running_machine *machine) |
| 475 | { |
| 476 | SDD1emu* newclass = (SDD1emu*)auto_alloc_array(machine, UINT8, sizeof(SDD1emu)); |
| 477 | newclass->machine = machine; |
| 478 | |
| 479 | newclass->IM = SDD1_IM_ctor(machine); |
| 480 | newclass->GCD = SDD1_GCD_ctor(machine, newclass->IM); |
| 481 | newclass->BG0 = SDD1_BG_ctor(machine, newclass->GCD, 0); |
| 482 | newclass->BG1 = SDD1_BG_ctor(machine, newclass->GCD, 1); |
| 483 | newclass->BG2 = SDD1_BG_ctor(machine, newclass->GCD, 2); |
| 484 | newclass->BG3 = SDD1_BG_ctor(machine, newclass->GCD, 3); |
| 485 | newclass->BG4 = SDD1_BG_ctor(machine, newclass->GCD, 4); |
| 486 | newclass->BG5 = SDD1_BG_ctor(machine, newclass->GCD, 5); |
| 487 | newclass->BG6 = SDD1_BG_ctor(machine, newclass->GCD, 6); |
| 488 | newclass->BG7 = SDD1_BG_ctor(machine, newclass->GCD, 7); |
| 489 | newclass->PEM = SDD1_PEM_ctor(machine, newclass->BG0, newclass->BG1, newclass->BG2, newclass->BG3, |
| 490 | newclass->BG4, newclass->BG5, newclass->BG6, newclass->BG7); |
| 491 | newclass->CM = SDD1_CM_ctor(machine, newclass->PEM); |
| 492 | newclass->OL = SDD1_OL_ctor(machine, newclass->CM); |
| 493 | return newclass; |
| 494 | } |
| 495 | |
| 496 | static void SDD1emu_decompress(SDD1emu* this, UINT32 in_buf, UINT16 out_len, UINT8 *out_buf) |
| 497 | { |
| 498 | SDD1_IM_prepareDecomp(this->IM, in_buf); |
| 499 | SDD1_BG_prepareDecomp(this->BG0); |
| 500 | SDD1_BG_prepareDecomp(this->BG1); |
| 501 | SDD1_BG_prepareDecomp(this->BG2); |
| 502 | SDD1_BG_prepareDecomp(this->BG3); |
| 503 | SDD1_BG_prepareDecomp(this->BG4); |
| 504 | SDD1_BG_prepareDecomp(this->BG5); |
| 505 | SDD1_BG_prepareDecomp(this->BG6); |
| 506 | SDD1_BG_prepareDecomp(this->BG7); |
| 507 | SDD1_PEM_prepareDecomp(this->PEM); |
| 508 | SDD1_CM_prepareDecomp(this->CM, in_buf); |
| 509 | SDD1_OL_prepareDecomp(this->OL, in_buf, out_len, out_buf); |
| 510 | |
| 511 | SDD1_OL_launch(this->OL); |
| 512 | } |
| 513 | |
| 514 | typedef struct |
| 515 | { |
| 516 | UINT8 sdd1_enable; // channel bit-mask |
| 517 | UINT8 xfer_enable; // channel bit-mask |
| 518 | UINT32 mmc[4]; // memory map controller ROM indices |
| 519 | |
| 520 | struct |
| 521 | { |
| 522 | UINT32 addr; // $43x2-$43x4 -- DMA transfer address |
| 523 | UINT16 size; // $43x5-$43x6 -- DMA transfer size |
| 524 | } dma[8]; |
| 525 | |
| 526 | SDD1emu* sdd1emu; |
| 527 | struct |
| 528 | { |
| 529 | UINT8 *data; // pointer to decompressed S-DD1 data (65536 bytes) |
| 530 | UINT16 offset; // read index into S-DD1 decompression buffer |
| 531 | UINT32 size; // length of data buffer; reads decrement counter, set ready to false at 0 |
| 532 | UINT8 ready; // 1 when data[] is valid; 0 to invoke sdd1emu.decompress() |
| 533 | } buffer; |
| 534 | } _snes_sdd1_t; |
| 535 | |
| 536 | _snes_sdd1_t snes_sdd1; |
| 537 | |
| 538 | static void sdd1_init(running_machine* machine) |
| 539 | { |
| 540 | snes_sdd1.sdd1emu = SDD1emu_ctor(machine); |
| 541 | } |
| 542 | |
| 543 | static UINT8 sdd1_mmio_read(running_machine* machine, UINT16 addr) |
| 544 | { |
| 545 | addr &= 0xffff; |
| 546 | |
| 547 | switch(addr) |
| 548 | { |
| 549 | case 0x4804: |
| 550 | return (snes_sdd1.mmc[0] >> 20) & 7; |
| 551 | case 0x4805: |
| 552 | return (snes_sdd1.mmc[1] >> 20) & 7; |
| 553 | case 0x4806: |
| 554 | return (snes_sdd1.mmc[2] >> 20) & 7; |
| 555 | case 0x4807: |
| 556 | return (snes_sdd1.mmc[3] >> 20) & 7; |
| 557 | } |
| 558 | |
| 559 | return 0xff; |
| 560 | } |
| 561 | |
| 562 | static void sdd1_mmio_write(running_machine* machine, UINT16 addr, UINT8 data) |
| 563 | { |
| 564 | addr &= 0xffff; |
| 565 | |
| 566 | if((addr & 0x4380) == 0x4300) |
| 567 | { |
| 568 | UINT8 channel = (addr >> 4) & 7; |
| 569 | switch(addr & 15) |
| 570 | { |
| 571 | case 2: |
| 572 | snes_sdd1.dma[channel].addr = (snes_sdd1.dma[channel].addr & 0xffff00) + (data << 0); |
| 573 | break; |
| 574 | case 3: |
| 575 | snes_sdd1.dma[channel].addr = (snes_sdd1.dma[channel].addr & 0xff00ff) + (data << 8); |
| 576 | break; |
| 577 | case 4: |
| 578 | snes_sdd1.dma[channel].addr = (snes_sdd1.dma[channel].addr & 0x00ffff) + (data << 16); |
| 579 | break; |
| 580 | |
| 581 | case 5: |
| 582 | snes_sdd1.dma[channel].addr = (snes_sdd1.dma[channel].addr & 0xff00) + (data << 0); |
| 583 | break; |
| 584 | case 6: |
| 585 | snes_sdd1.dma[channel].addr = (snes_sdd1.dma[channel].addr & 0x00ff) + (data << 8); |
| 586 | break; |
| 587 | } |
| 588 | snes_ram[addr] = data; |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | switch(addr) |
| 593 | { |
| 594 | case 0x4800: |
| 595 | snes_sdd1.sdd1_enable = data; |
| 596 | break; |
| 597 | case 0x4801: |
| 598 | snes_sdd1.xfer_enable = data; |
| 599 | break; |
| 600 | |
| 601 | case 0x4804: |
| 602 | snes_sdd1.mmc[0] = (data & 7) << 20; |
| 603 | break; |
| 604 | case 0x4805: |
| 605 | snes_sdd1.mmc[1] = (data & 7) << 20; |
| 606 | break; |
| 607 | case 0x4806: |
| 608 | snes_sdd1.mmc[2] = (data & 7) << 20; |
| 609 | break; |
| 610 | case 0x4807: |
| 611 | snes_sdd1.mmc[3] = (data & 7) << 20; |
| 612 | break; |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | static UINT8 sdd1_read(running_machine* machine, UINT16 addr) |
| 617 | { |
| 618 | if(snes_sdd1.sdd1_enable & snes_sdd1.xfer_enable) |
| 619 | { |
| 620 | // at least one channel has S-DD1 decompression enabled... |
| 621 | UINT32 i; |
| 622 | for(i = 0; i < 8; i++) |
| 623 | { |
| 624 | if(snes_sdd1.sdd1_enable & snes_sdd1.xfer_enable & (1 << i)) |
| 625 | { |
| 626 | // S-DD1 always uses fixed transfer mode, so address will not change during transfer |
| 627 | if(addr == snes_sdd1.dma[i].addr) |
| 628 | { |
| 629 | UINT8 data; |
| 630 | if(!snes_sdd1.buffer.ready) |
| 631 | { |
| 632 | UINT8 temp; |
| 633 | // first byte read for channel performs full decompression. |
| 634 | // this really should stream byte-by-byte, but it's not necessary since the size is known |
| 635 | snes_sdd1.buffer.offset = 0; |
| 636 | snes_sdd1.buffer.size = snes_sdd1.dma[i].size ? snes_sdd1.dma[i].size : 65536; |
| 637 | |
| 638 | // sdd1emu calls this function; it needs to access uncompressed data; |
| 639 | // so temporarily disable decompression mode for decompress() call. |
| 640 | temp = snes_sdd1.sdd1_enable; |
| 641 | snes_sdd1.sdd1_enable = 0; |
| 642 | SDD1emu_decompress(snes_sdd1.sdd1emu, addr, snes_sdd1.buffer.size, snes_sdd1.buffer.data); |
| 643 | snes_sdd1.sdd1_enable = temp; |
| 644 | |
| 645 | snes_sdd1.buffer.ready = 1; |
| 646 | } |
| 647 | |
| 648 | // fetch a decompressed byte; once buffer is depleted, disable channel and invalidate buffer |
| 649 | data = snes_sdd1.buffer.data[(UINT16)snes_sdd1.buffer.offset++]; |
| 650 | if(snes_sdd1.buffer.offset >= snes_sdd1.buffer.size) |
| 651 | { |
| 652 | snes_sdd1.buffer.ready = 0; |
| 653 | snes_sdd1.xfer_enable &= ~(1 << i); |
| 654 | } |
| 655 | |
| 656 | return data; |
| 657 | } // address matched |
| 658 | } // channel enabled |
| 659 | } // channel loop |
| 660 | } // S-DD1 decompressor enabled |
| 661 | |
| 662 | return memory_read_byte(cputag_get_address_space(machine, "maincpu", ADDRESS_SPACE_PROGRAM), snes_sdd1.mmc[(addr >> 20) & 3] + (addr & 0x0fffff)); |
| 663 | } |
| 664 | |
| 665 | static void sdd1_reset(running_machine *machine) |
| 666 | { |
| 667 | } |