1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#!/usr/bin/perl
use warnings;
use strict;
sub write_byte {
my( $b ) = @_;
printf "%c", $b;
}
sub write_word {
my( $w ) = @_;
write_byte( $w % 0x100 );
write_byte( $w / 0x100 );
}
sub write_three {
my( $three ) = @_;
write_byte( $three % 0x100 );
write_word( $three / 0x100 );
}
sub write_dword {
my( $dw ) = @_;
write_word( $dw % 0x10000 );
write_word( $dw / 0x10000 );
}
sub write_header {
print "ZXTape!\x1a"; # Signature
write_byte( 1 ); # Major version number
write_byte( 20 ); # Minor version number
}
sub write_standard_speed_data_block {
my( $data, $pause ) = @_;
write_byte( 0x10 );
write_word( $pause );
write_word( length $data );
print $data;
}
sub write_turbo_speed_data_block {
my( $pilot_length, $pilot_count, $sync1_length, $sync2_length,
$zero_length, $one_length, $data, $bits_in_last_byte, $pause ) = @_;
write_byte( 0x11 );
write_word( $pilot_length );
write_word( $sync1_length );
write_word( $sync2_length );
write_word( $zero_length );
write_word( $one_length );
write_word( $pilot_count );
write_byte( $bits_in_last_byte );
write_word( $pause );
write_three( length $data );
print $data;
}
sub write_pure_tone_block {
my( $length, $count ) = @_;
write_byte( 0x12 );
write_word( $length );
write_word( $count );
}
sub write_pulse_sequence_block {
my( @data ) = @_;
write_byte( 0x13 );
write_byte( scalar @data );
write_word( $_ ) foreach @data;
}
sub write_pure_data_block {
my( $zero_length, $one_length, $data, $bits_in_last_byte, $pause ) = @_;
write_byte( 0x14 );
write_word( $zero_length );
write_word( $one_length );
write_byte( $bits_in_last_byte );
write_word( $pause );
write_three( length $data );
print $data;
}
sub write_pause_block {
my( $pause ) = @_;
write_byte( 0x20 );
write_word( $pause );
}
sub write_group_start_block {
my( $name ) = @_;
write_byte( 0x21 );
write_byte( length $name );
print $name;
}
sub write_group_end_block {
my( $name ) = @_;
write_byte( 0x22 );
}
sub write_jump_block {
my( $offset ) = @_;
write_byte( 0x23 );
write_word( $offset );
}
sub write_loop_start_block {
my( $iterations ) = @_;
write_byte( 0x24 );
write_word( $iterations );
}
sub write_loop_end_block {
write_byte( 0x25 );
}
sub write_stop_tape_if_in_48k_mode_block {
write_byte( 0x2a );
write_dword( 0 );
}
sub write_text_description_block {
my( $text ) = @_;
write_byte( 0x30 );
write_byte( length $text );
print $text;
}
sub write_message_block {
my( $text, $time ) = @_;
write_byte( 0x31 );
write_byte( $time );
write_byte( length $text );
print $text;
}
sub write_archive_info_block {
my( @strings ) = @_;
write_byte( 0x32 );
my $data;
foreach my $string ( @strings ) {
$data .= chr( $string->{id} );
$data .= chr( length $string->{text} );
$data .= $string->{text};
}
write_word( length $data );
write_byte( scalar @strings );
print $data;
}
sub write_hardware_type_block {
my( @info ) = @_;
write_byte( 0x33 );
write_byte( scalar @info );
foreach my $info ( @info ) {
write_byte( $info->{type} );
write_byte( $info->{id} );
write_byte( $info->{used} );
}
}
sub write_custom_info_block {
my( $id, $data ) = @_;
write_byte( 0x35 );
if( length $id < 16 ) {
$id .= ' ' x ( 16 - length $id );
} elsif( length $id > 16 ) {
$id = substr( $id, 0, 16 );
}
print $id;
write_dword( length $data );
print $data;
}
write_header();
write_standard_speed_data_block( "\xaa", 2345 );
write_turbo_speed_data_block( 1000, 5, 123, 456, 789, 400, "\x00\xff\x55\xa0",
4, 987 );
write_pure_tone_block( 535, 666 );
write_pulse_sequence_block( 772, 297, 692 );
write_pure_data_block( 552, 1639, "\xff\x00\xfc", 6, 554 );
write_pause_block( 618 );
write_group_start_block( "Group Start" );
write_group_end_block();
write_jump_block( 2 );
write_pure_tone_block( 303, 678 );
write_loop_start_block( 3 );
write_pure_tone_block( 837, 185 );
write_loop_end_block();
write_stop_tape_if_in_48k_mode_block();
write_text_description_block( "Comment here" );
write_message_block( "A message", 1 );
write_archive_info_block( { id => 0x00, text => "Full title" },
{ id => 0x03, text => "Year" } );
write_hardware_type_block( { type => 0x00, id => 0x01, used => 0x00 },
{ type => 0x02, id => 0x02, used => 0x03 } );
write_custom_info_block( "Complete TZX", "Arbitrary custom data" );
write_pure_tone_block( 820, 941 );
|