@@ -538,17 +538,33 @@ void ViaCuda::process_packet() {
538538 }
539539}
540540
541+ void ViaCuda::append_data (uint8_t * src, int len) {
542+ if (len > 0 ) {
543+ std::memcpy (&this ->out_buf [this ->out_count ], src, len);
544+ this ->out_count += len;
545+ }
546+ }
547+
548+ template <class T >
549+ void ViaCuda::append_data (T data) {
550+ switch (sizeof (T)) {
551+ case 1 : this ->out_buf [this ->out_count ] = data;
552+ case 2 : WRITE_WORD_BE_U ( &this ->out_buf [this ->out_count ], data);
553+ case 4 : WRITE_DWORD_BE_U (&this ->out_buf [this ->out_count ], data);
554+ }
555+ this ->out_count += sizeof (T);
556+ }
557+ template void ViaCuda::append_data (uint8_t data);
558+ template void ViaCuda::append_data (uint16_t data);
559+ template void ViaCuda::append_data (uint32_t data);
560+
541561void ViaCuda::process_adb_command () {
542562 uint8_t adb_stat, output_size;
543563
544564 adb_stat = this ->adb_bus_obj ->process_command (&this ->in_buf [1 ],
545565 this ->in_count - 1 );
546566 response_header (CUDA_PKT_ADB, adb_stat);
547- output_size = this ->adb_bus_obj ->get_output_count ();
548- if (output_size) {
549- std::memcpy (&this ->out_buf [3 ], this ->adb_bus_obj ->get_output_buf (), output_size);
550- this ->out_count += output_size;
551- }
567+ this ->append_data (this ->adb_bus_obj ->get_output_buf (), this ->adb_bus_obj ->get_output_count ());
552568}
553569
554570void ViaCuda::autopoll_handler () {
@@ -567,11 +583,7 @@ void ViaCuda::autopoll_handler() {
567583 // prepare autopoll packet
568584 response_header (CUDA_PKT_ADB, ADB_STAT_OK | ADB_STAT_AUTOPOLL);
569585 this ->out_buf [2 ] = poll_command; // put the proper ADB command
570- uint8_t output_size = this ->adb_bus_obj ->get_output_count ();
571- if (output_size) {
572- std::memcpy (&this ->out_buf [3 ], this ->adb_bus_obj ->get_output_buf (), output_size);
573- this ->out_count += output_size;
574- }
586+ this ->append_data (this ->adb_bus_obj ->get_output_buf (), this ->adb_bus_obj ->get_output_count ());
575587
576588 // assert TREQ
577589 this ->via_portb &= ~CUDA_TREQ;
@@ -593,8 +605,7 @@ void ViaCuda::autopoll_handler() {
593605 this ->out_buf [2 ] = CUDA_GET_REAL_TIME;
594606 if (send_time || this ->one_sec_mode == 1 ) {
595607 uint32_t real_time = this_time + this ->time_offset ;
596- WRITE_DWORD_BE_U (&this ->out_buf [3 ], real_time);
597- this ->out_count = 7 ;
608+ this ->append_data (real_time);
598609 }
599610 } else if (this ->one_sec_mode == 3 ) {
600611 one_byte_header (CUDA_PKT_TICK);
@@ -639,20 +650,18 @@ void ViaCuda::pseudo_command() {
639650 this ->next_out_handler = &ViaCuda::pram_out_handler;
640651 } else if (addr >= CUDA_ROM_START) {
641652 // HACK: Cuda ROM dump requsted so let's partially fake it
642- this ->out_buf [3 ] = 0 ; // empty copyright string
643- WRITE_WORD_BE_A (&this ->out_buf [4 ], 0x0019U );
644- WRITE_WORD_BE_A (&this ->out_buf [6 ], CUDA_FW_VERSION_MAJOR);
645- WRITE_WORD_BE_A (&this ->out_buf [8 ], CUDA_FW_VERSION_MINOR);
646- this ->out_count += 7 ;
653+ this ->append_data (uint8_t (0 )); // empty copyright string
654+ this ->append_data (uint16_t (0x0019U ));
655+ this ->append_data (uint16_t (CUDA_FW_VERSION_MAJOR));
656+ this ->append_data (uint16_t (CUDA_FW_VERSION_MINOR));
647657 }
648658 this ->is_open_ended = true ;
649659 break ;
650660 case CUDA_GET_REAL_TIME: {
651661 response_header (CUDA_PKT_PSEUDO, 0 );
652662 uint32_t this_time = this ->calc_real_time ();
653663 uint32_t real_time = this_time + this ->time_offset ;
654- WRITE_DWORD_BE_U (&this ->out_buf [3 ], real_time);
655- this ->out_count = 7 ;
664+ this ->append_data (real_time);
656665 break ;
657666 }
658667 case CUDA_WRITE_MCU_MEM:
@@ -715,17 +724,15 @@ void ViaCuda::pseudo_command() {
715724 break ;
716725 case CUDA_GET_AUTOPOLL_RATE:
717726 response_header (CUDA_PKT_PSEUDO, 0 );
718- this ->out_buf [3 ] = this ->poll_rate ;
719- this ->out_count ++;
727+ this ->append_data (this ->poll_rate );
720728 break ;
721729 case CUDA_SET_DEVICE_BITMAP:
722730 this ->device_mask = READ_WORD_BE_U (&this ->in_buf [2 ]);
723731 response_header (CUDA_PKT_PSEUDO, 0 );
724732 break ;
725733 case CUDA_GET_DEVICE_BITMAP:
726734 response_header (CUDA_PKT_PSEUDO, 0 );
727- WRITE_WORD_BE_U (&this ->out_buf [3 ], this ->device_mask );
728- this ->out_count += 2 ;
735+ this ->append_data (this ->device_mask );
729736 break ;
730737 case CUDA_ONE_SECOND_MODE:
731738 LOG_F (INFO, " Cuda: One Second Interrupt Mode: %d" , this ->in_buf [2 ]);
0 commit comments