src/mess/formats/ti85_ser.c
| r10972 | r10973 | |
| 42 | 42 | #define TI85_PC_END_PACKET_SIZE 4 |
| 43 | 43 | |
| 44 | 44 | static const UINT8 ti85_pc_ok_packet[] = {0x05, 0x56, 0x00, 0x00}; |
| 45 | static const UINT8 ti82_pc_ok_packet[] = {0x02, 0x56, 0x00, 0x00}; |
| 45 | 46 | static const UINT8 ti86_pc_ok_packet[] = {0x06, 0x56, 0x00, 0x00}; |
| 46 | 47 | static const UINT8 ti85_pc_continue_packet[] = {0x05, 0x09, 0x00, 0x00}; |
| 48 | static const UINT8 ti82_pc_continue_packet[] = {0x02, 0x09, 0x00, 0x00}; |
| 47 | 49 | static const UINT8 ti86_pc_continue_packet[] = {0x06, 0x09, 0x00, 0x00}; |
| 48 | 50 | static const UINT8 ti85_pc_screen_request_packet[] = {0x05, 0x6d, 0x00, 0x00}; |
| 49 | 51 | static const UINT8 ti85_pc_end_packet[] = {0x05, 0x92, 0x00, 0x00}; |
| 52 | static const UINT8 ti82_pc_end_packet[] = {0x02, 0x92, 0x00, 0x00}; |
| 50 | 53 | static const UINT8 ti86_pc_end_packet[] = {0x06, 0x92, 0x00, 0x00}; |
| 51 | 54 | |
| 52 | 55 | static const UINT8 ti85_file_signature[] = {0x2a, 0x2a, 0x54, 0x49, 0x38, 0x35, 0x2a, 0x2a, 0x1a, 0x0c, 0x00}; |
| 56 | static const UINT8 ti82_file_signature[] = {0x2a, 0x2a, 0x54, 0x49, 0x38, 0x32, 0x2a, 0x2a, 0x1a, 0x0a, 0x00}; |
| 53 | 57 | static const UINT8 ti86_file_signature[] = {0x2a, 0x2a, 0x54, 0x49, 0x38, 0x36, 0x2a, 0x2a, 0x1a, 0x0a, 0x00}; |
| 54 | 58 | |
| 55 | 59 | typedef struct { |
| r10972 | r10973 | |
| 112 | 116 | INLINE ti85serial_state *get_token(device_t *device) |
| 113 | 117 | { |
| 114 | 118 | assert(device != NULL); |
| 115 | | assert((device->type() == TI85SERIAL) || (device->type() == TI86SERIAL)); |
| 119 | assert((device->type() == TI82SERIAL) || (device->type() == TI85SERIAL) || (device->type() == TI86SERIAL)); |
| 116 | 120 | return (ti85serial_state *) downcast<legacy_device_base *>(device)->token(); |
| 117 | 121 | } |
| 118 | 122 | |
| r10972 | r10973 | |
| 204 | 208 | } |
| 205 | 209 | |
| 206 | 210 | |
| 207 | | static void ti85_variables_read (const UINT8 * ti85_data, unsigned int ti85_data_size, ti85_entry * ti85_entries) |
| 211 | static void ti85_variables_read (device_t *device, const UINT8 * ti85_data, unsigned int ti85_data_size, ti85_entry * ti85_entries) |
| 208 | 212 | { |
| 209 | 213 | unsigned int pos, i=0; |
| 210 | 214 | |
| r10972 | r10973 | |
| 214 | 218 | ti85_entries[i].head_size = ti85_data[pos+0x00] + ti85_data[pos+0x01]*256; |
| 215 | 219 | ti85_entries[i].data_size = ti85_data[pos+0x02] + ti85_data[pos+0x03]*256; |
| 216 | 220 | ti85_entries[i].type = ti85_data[pos+0x04]; |
| 217 | | ti85_entries[i].name_size = ti85_data[pos+0x05]; |
| 221 | ti85_entries[i].name_size = (device->type() == TI82SERIAL) ? 8 : ti85_data[pos+0x05]; |
| 218 | 222 | ti85_entries[i].offset = pos; |
| 219 | 223 | pos += ti85_entries[i].head_size+ti85_entries[i].data_size+4; |
| 220 | 224 | i++; |
| r10972 | r10973 | |
| 319 | 323 | ti85serial_state *ti85serial = get_token( device ); |
| 320 | 324 | UINT16 i; |
| 321 | 325 | UINT16 number_of_entries; |
| 326 | UINT8 backup_id = (device->type() == TI85SERIAL || device->type() == TI86SERIAL) ? 0x1d : 0x13; |
| 322 | 327 | |
| 323 | 328 | ti85_entry* ti85_entries = NULL; |
| 324 | 329 | |
| r10972 | r10973 | |
| 328 | 333 | if (device->type() == TI85SERIAL) |
| 329 | 334 | if (strncmp((char *) file_data, (char *) ti85_file_signature, 11)) |
| 330 | 335 | return 0; |
| 336 | if (device->type() == TI82SERIAL) |
| 337 | if (strncmp((char *) file_data, (char *) ti82_file_signature, 11)) |
| 338 | return 0; |
| 331 | 339 | if (device->type() == TI86SERIAL) |
| 332 | 340 | if (strncmp((char *) file_data, (char *) ti86_file_signature, 11)) |
| 333 | 341 | return 0; |
| r10972 | r10973 | |
| 335 | 343 | /*Serial stream preparing*/ |
| 336 | 344 | serial_data->end = NULL; |
| 337 | 345 | |
| 338 | | number_of_entries = (file_data[0x3b]==0x1d) ? 3 : ti85_variables_count(file_data, file_size); |
| 346 | number_of_entries = (file_data[0x3b]==backup_id) ? 3 : ti85_variables_count(file_data, file_size); |
| 339 | 347 | if (!number_of_entries) return 0; |
| 340 | 348 | |
| 341 | 349 | serial_data->variables = (ti85_serial_variable*)malloc(sizeof(ti85_serial_variable)*number_of_entries); |
| r10972 | r10973 | |
| 353 | 361 | ti85_entries = (ti85_entry*) malloc (sizeof(ti85_entry)*number_of_entries); |
| 354 | 362 | if (!ti85_entries) return 0; |
| 355 | 363 | |
| 356 | | if (file_data[0x3b]==0x1d) |
| 364 | if (file_data[0x3b]==backup_id) |
| 357 | 365 | { |
| 358 | 366 | ti85serial->transfer_type = TI85_SEND_BACKUP; |
| 359 | 367 | ti85_backup_read (file_data, file_size, ti85_entries); |
| r10972 | r10973 | |
| 361 | 369 | else |
| 362 | 370 | { |
| 363 | 371 | ti85serial->transfer_type = TI85_SEND_VARIABLES; |
| 364 | | ti85_variables_read (file_data, file_size, ti85_entries); |
| 372 | ti85_variables_read (device, file_data, file_size, ti85_entries); |
| 365 | 373 | } |
| 366 | 374 | for (i=0; i<number_of_entries; i++) |
| 367 | 375 | { |
| 368 | 376 | /*Header packet*/ |
| 369 | | if (file_data[0x3b]==0x1d) |
| 377 | if (file_data[0x3b]==backup_id) |
| 370 | 378 | { |
| 371 | 379 | if (!i) |
| 372 | 380 | { |
| r10972 | r10973 | |
| 415 | 423 | free (ti85_entries); |
| 416 | 424 | return 0; |
| 417 | 425 | } |
| 418 | | serial_data->variables[i].header_size = (10+ti85_entries[i].name_size)*8; |
| 419 | 426 | |
| 420 | | if (device->type() == TI85SERIAL) |
| 421 | | temp_data_to_convert[0] = 0x05; //PC to TI-85 |
| 422 | | if (device->type() == TI86SERIAL) |
| 423 | | temp_data_to_convert[0] = 0x06; //PC to TI-86 |
| 424 | | temp_data_to_convert[1] = 0x06; //header |
| 425 | | temp_data_to_convert[2] = (4+ti85_entries[i].name_size)&0x00ff; |
| 426 | | temp_data_to_convert[3] = ((4+ti85_entries[i].name_size)&0xff00)>>8; |
| 427 | | temp_data_to_convert[4] = (ti85_entries[i].data_size)&0x00ff; |
| 428 | | temp_data_to_convert[5] = ((ti85_entries[i].data_size)&0xff00)>>8; |
| 429 | | temp_data_to_convert[6] = ti85_entries[i].type; |
| 430 | | temp_data_to_convert[7] = ti85_entries[i].name_size; |
| 431 | | memcpy(temp_data_to_convert+8, file_data+ti85_entries[i].offset+0x06, ti85_entries[i].name_size); |
| 432 | | checksum = ti85_calculate_checksum(temp_data_to_convert+4,ti85_entries[i].name_size+4); |
| 433 | | temp_data_to_convert[10+ti85_entries[i].name_size-2] = checksum&0x00ff; |
| 434 | | temp_data_to_convert[10+ti85_entries[i].name_size-1] = (checksum&0xff00)>>8; |
| 435 | | ti85_convert_data_to_stream(temp_data_to_convert, 10+ti85_entries[i].name_size, serial_data->variables[i].header); |
| 427 | if (device->type() == TI85SERIAL || device->type() == TI86SERIAL) |
| 428 | { |
| 429 | serial_data->variables[i].header_size = (10+ti85_entries[i].name_size)*8; |
| 430 | |
| 431 | if (device->type() == TI85SERIAL) |
| 432 | temp_data_to_convert[0] = 0x05; //PC to TI-85 |
| 433 | if (device->type() == TI86SERIAL) |
| 434 | temp_data_to_convert[0] = 0x06; //PC to TI-86 |
| 435 | temp_data_to_convert[1] = 0x06; //header |
| 436 | temp_data_to_convert[2] = (4+ti85_entries[i].name_size)&0x00ff; |
| 437 | temp_data_to_convert[3] = ((4+ti85_entries[i].name_size)&0xff00)>>8; |
| 438 | temp_data_to_convert[4] = (ti85_entries[i].data_size)&0x00ff; |
| 439 | temp_data_to_convert[5] = ((ti85_entries[i].data_size)&0xff00)>>8; |
| 440 | temp_data_to_convert[6] = ti85_entries[i].type; |
| 441 | temp_data_to_convert[7] = ti85_entries[i].name_size; |
| 442 | memcpy(temp_data_to_convert+8, file_data+ti85_entries[i].offset+0x06, ti85_entries[i].name_size); |
| 443 | checksum = ti85_calculate_checksum(temp_data_to_convert+4,ti85_entries[i].name_size+4); |
| 444 | temp_data_to_convert[10+ti85_entries[i].name_size-2] = checksum&0x00ff; |
| 445 | temp_data_to_convert[10+ti85_entries[i].name_size-1] = (checksum&0xff00)>>8; |
| 446 | ti85_convert_data_to_stream(temp_data_to_convert, 10+ti85_entries[i].name_size, serial_data->variables[i].header); |
| 447 | } |
| 448 | |
| 449 | if (device->type() == TI82SERIAL) |
| 450 | { |
| 451 | serial_data->variables[i].header_size = (9+ti85_entries[i].name_size)*8; |
| 452 | |
| 453 | temp_data_to_convert[0] = 0x02; //PC to TI-82 |
| 454 | temp_data_to_convert[1] = 0x06; //header |
| 455 | temp_data_to_convert[2] = (3+ti85_entries[i].name_size)&0x00ff; |
| 456 | temp_data_to_convert[3] = ((3+ti85_entries[i].name_size)&0xff00)>>8; |
| 457 | temp_data_to_convert[4] = (ti85_entries[i].data_size)&0x00ff; |
| 458 | temp_data_to_convert[5] = ((ti85_entries[i].data_size)&0xff00)>>8; |
| 459 | temp_data_to_convert[6] = ti85_entries[i].type; |
| 460 | memcpy(temp_data_to_convert+7, file_data+ti85_entries[i].offset+0x05, ti85_entries[i].name_size); |
| 461 | checksum = ti85_calculate_checksum(temp_data_to_convert+4,ti85_entries[i].name_size+3); |
| 462 | temp_data_to_convert[9+ti85_entries[i].name_size-2] = checksum&0x00ff; |
| 463 | temp_data_to_convert[9+ti85_entries[i].name_size-1] = (checksum&0xff00)>>8; |
| 464 | ti85_convert_data_to_stream(temp_data_to_convert, 9+ti85_entries[i].name_size, serial_data->variables[i].header); |
| 465 | } |
| 466 | |
| 436 | 467 | free(temp_data_to_convert); |
| 437 | 468 | } |
| 438 | 469 | |
| r10972 | r10973 | |
| 445 | 476 | } |
| 446 | 477 | if (device->type() == TI85SERIAL) |
| 447 | 478 | ti85_convert_data_to_stream(ti85_pc_ok_packet, TI85_PC_OK_PACKET_SIZE, serial_data->variables[i].ok); |
| 479 | if (device->type() == TI82SERIAL) |
| 480 | ti85_convert_data_to_stream(ti82_pc_ok_packet, TI85_PC_OK_PACKET_SIZE, serial_data->variables[i].ok); |
| 448 | 481 | if (device->type() == TI86SERIAL) |
| 449 | 482 | ti85_convert_data_to_stream(ti86_pc_ok_packet, TI85_PC_OK_PACKET_SIZE, serial_data->variables[i].ok); |
| 450 | 483 | serial_data->variables[i].ok_size = TI85_PC_OK_PACKET_SIZE*8; |
| r10972 | r10973 | |
| 467 | 500 | |
| 468 | 501 | if (device->type() == TI85SERIAL) |
| 469 | 502 | temp_data_to_convert[0] = 0x05; //PC to TI-85 |
| 503 | if (device->type() == TI82SERIAL) |
| 504 | temp_data_to_convert[0] = 0x02; //PC to TI-82 |
| 470 | 505 | if (device->type() == TI86SERIAL) |
| 471 | 506 | temp_data_to_convert[0] = 0x06; //PC to TI-86 |
| 472 | 507 | temp_data_to_convert[1] = 0x15; //data |
| 473 | 508 | temp_data_to_convert[2] = (ti85_entries[i].data_size)&0x00ff; |
| 474 | 509 | temp_data_to_convert[3] = ((ti85_entries[i].data_size)&0xff00)>>8; |
| 475 | | if (file_data[0x3b]==0x1d) |
| 510 | |
| 511 | UINT8 name_start = (device->type() == TI82SERIAL) ? 5 : 6; |
| 512 | if (file_data[0x3b]==backup_id) |
| 476 | 513 | memcpy(temp_data_to_convert+4, file_data+ti85_entries[i].offset, ti85_entries[i].data_size); |
| 477 | 514 | else |
| 478 | | memcpy(temp_data_to_convert+4, file_data+ti85_entries[i].offset+0x06+ti85_entries[i].name_size+0x02, ti85_entries[i].data_size); |
| 515 | memcpy(temp_data_to_convert+4, file_data+ti85_entries[i].offset+name_start+ti85_entries[i].name_size+0x02, ti85_entries[i].data_size); |
| 516 | |
| 479 | 517 | checksum = ti85_calculate_checksum(temp_data_to_convert+4,ti85_entries[i].data_size); |
| 480 | 518 | temp_data_to_convert[6+ti85_entries[i].data_size-2] = checksum&0x00ff; |
| 481 | 519 | temp_data_to_convert[6+ti85_entries[i].data_size-1] = (checksum&0xff00)>>8; |
| r10972 | r10973 | |
| 493 | 531 | } |
| 494 | 532 | if (device->type() == TI85SERIAL) |
| 495 | 533 | ti85_convert_data_to_stream(ti85_pc_end_packet, TI85_PC_END_PACKET_SIZE, serial_data->end); |
| 534 | if (device->type() == TI82SERIAL) |
| 535 | ti85_convert_data_to_stream(ti82_pc_end_packet, TI85_PC_END_PACKET_SIZE, serial_data->end); |
| 496 | 536 | if (device->type() == TI86SERIAL) |
| 497 | 537 | ti85_convert_data_to_stream(ti86_pc_end_packet, TI85_PC_END_PACKET_SIZE, serial_data->end); |
| 498 | 538 | serial_data->end_size = TI85_PC_END_PACKET_SIZE*8; |
| r10972 | r10973 | |
| 894 | 934 | |
| 895 | 935 | if (filerr == FILERR_NONE) |
| 896 | 936 | { |
| 897 | | var_file.write(ti85serial->var_file_data, ti85serial->var_file_size); |
| 937 | var_file.write(ti85serial->var_file_data, ti85serial->var_file_size); |
| 898 | 938 | free (ti85serial->var_file_data); |
| 899 | 939 | ti85serial->var_file_data = NULL; |
| 900 | 940 | ti85serial->var_file_size = 0; |
| r10972 | r10973 | |
| 1031 | 1071 | ti85serial->backup_file_data[0x42+0x06+ti85serial->backup_data_size[0]+ti85serial->backup_data_size[1]+ti85serial->backup_data_size[2]] = ti85_calculate_checksum(ti85serial->backup_file_data+0x37, 0x42+ti85serial->backup_data_size[0]+ti85serial->backup_data_size[1]+ti85serial->backup_data_size[2]+0x06-0x37)&0x00ff; |
| 1032 | 1072 | ti85serial->backup_file_data[0x42+0x06+ti85serial->backup_data_size[0]+ti85serial->backup_data_size[1]+ti85serial->backup_data_size[2]+0x01] = (ti85_calculate_checksum(ti85serial->backup_file_data+0x37, 0x42+ti85serial->backup_data_size[0]+ti85serial->backup_data_size[1]+ti85serial->backup_data_size[2]+0x06-0x37)&0xff00)>>8; |
| 1033 | 1073 | sprintf (backup_file_name, "%08d.85b", ti85serial->backup_file_number); |
| 1034 | | |
| 1074 | |
| 1035 | 1075 | emu_file backup_file(device->machine->options().memcard_directory(), OPEN_FLAG_READ | OPEN_FLAG_WRITE | OPEN_FLAG_CREATE); |
| 1036 | 1076 | filerr = backup_file.open(backup_file_name); |
| 1037 | | |
| 1077 | |
| 1038 | 1078 | if (filerr == FILERR_NONE) |
| 1039 | 1079 | { |
| 1040 | 1080 | backup_file.write(ti85serial->backup_file_data, 0x42+0x06+ti85serial->backup_data_size[0]+ti85serial->backup_data_size[1]+ti85serial->backup_data_size[2]+0x02); |
| r10972 | r10973 | |
| 1096 | 1136 | { |
| 1097 | 1137 | ti85_convert_stream_to_data (ti85serial->receive_buffer, 1030*8, ti85serial->receive_data); |
| 1098 | 1138 | sprintf (image_file_name, "%08d.85i", ti85serial->image_file_number); |
| 1099 | | |
| 1139 | |
| 1100 | 1140 | emu_file image_file(device->machine->options().memcard_directory(), OPEN_FLAG_READ | OPEN_FLAG_WRITE | OPEN_FLAG_CREATE); |
| 1101 | 1141 | filerr = image_file.open(image_file_name); |
| 1102 | | |
| 1142 | |
| 1103 | 1143 | if (filerr == FILERR_NONE) |
| 1104 | 1144 | { |
| 1105 | 1145 | image_file_data = (UINT8*)malloc (0x49+1008); |
| r10972 | r10973 | |
| 1128 | 1168 | memcpy(image_file_data+0x47, ti85serial->receive_data+4, 1008); |
| 1129 | 1169 | image_file_data[1008+0x49-2] = ti85_calculate_checksum(image_file_data+0x37, 1008+0x10)&0x00ff; |
| 1130 | 1170 | image_file_data[1008+0x49-1] = (ti85_calculate_checksum(image_file_data+0x37, 1008+0x10)&0xff00)>>8; |
| 1131 | | image_file.write(image_file_data, 1008+0x49); |
| 1171 | image_file.write(image_file_data, 1008+0x49); |
| 1132 | 1172 | free(image_file_data); |
| 1133 | 1173 | ti85serial->image_file_number++; |
| 1134 | 1174 | } |
| r10972 | r10973 | |
| 1362 | 1402 | } |
| 1363 | 1403 | |
| 1364 | 1404 | |
| 1405 | DEVICE_GET_INFO( ti82serial ) |
| 1406 | { |
| 1407 | switch ( state ) |
| 1408 | { |
| 1409 | case DEVINFO_STR_NAME: strcpy(info->s, "TI82 Serial"); break; |
| 1410 | case DEVINFO_STR_IMAGE_FILE_EXTENSIONS: strcpy(info->s, "82p,82s,82i,82n,82c,82l,82k,82m,82v,82d,82e,82r,82g"); break; |
| 1411 | default: DEVICE_GET_INFO_CALL( ti85serial ); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | |
| 1365 | 1416 | DEVICE_GET_INFO( ti86serial ) |
| 1366 | 1417 | { |
| 1367 | 1418 | switch ( state ) |
| r10972 | r10973 | |
| 1373 | 1424 | } |
| 1374 | 1425 | |
| 1375 | 1426 | DEFINE_LEGACY_IMAGE_DEVICE(TI85SERIAL, ti85serial); |
| 1427 | DEFINE_LEGACY_IMAGE_DEVICE(TI82SERIAL, ti82serial); |
| 1376 | 1428 | DEFINE_LEGACY_IMAGE_DEVICE(TI86SERIAL, ti86serial); |