tape.cpp 20 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright 2003-2015 (C) Raster Software Vigo (Sergio Costas)
 * This file is part of FBZX
 *
 * FBZX is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * FBZX is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

Sergio Costas's avatar
Sergio Costas committed
20 21 22 23 24 25 26
/*
 * tape.cpp
 *
 *  Created on: 15/03/2015
 *      Author: raster
 */

Sergio Costas's avatar
Sergio Costas committed
27
#include "tape.hh"
28
#include "z80free/Z80free.h"
Sergio Costas's avatar
Sergio Costas committed
29

Sergio Costas's avatar
Sergio Costas committed
30 31 32 33 34 35 36 37
#include <stdio.h>
#include <string.h>

class TapeBlock {

private:
	uint8_t signal;
protected:
38
	class TapeBlock *next;
Sergio Costas's avatar
Sergio Costas committed
39 40 41
	uint32_t counter0;
	uint32_t counter1;
public:
42

Sergio Costas's avatar
Sergio Costas committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56
	TapeBlock() {
		this->next = NULL;
		this->signal = 0;
		this->counter0 = 0;
		this->counter1 = 0;
	}

	virtual ~TapeBlock() {

		if (this->next != NULL) {
			delete (this->next);
		}
	}

57 58 59 60 61
	virtual bool fast_load(uint8_t *data, uint16_t &size, uint8_t &flag) {
		size = 0;
		flag = 0;
		return false;
	}
62

Sergio Costas's avatar
Sergio Costas committed
63 64 65 66
	/**
	 * Adds a new block to the end of the list
	 * @param block The block to add to the list
	 */
67
	virtual void add_block(class TapeBlock *new_block) {
Sergio Costas's avatar
Sergio Costas committed
68 69 70 71 72 73 74 75 76 77 78 79

		if (this->next == NULL) {
			this->next = new_block;
		} else {
			this->next->add_block(new_block);
		}
	}

	/**
	 * Returns the next block in the list
	 * @return The block next to this one
	 */
80
	virtual class TapeBlock *next_block() {
Sergio Costas's avatar
Sergio Costas committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		return this->next;
	}

	/**
	 * Returns the tape signal at the current instant
	 * @return Tape signal
	 */
	uint8_t read_signal() {

		return this->signal;
	}

	/**
	 * Emulates the block for the specified time
	 * @param tstates The number of tstates to emulate
	 * @return The number of tstates that remained to emulate because the block ended prematurely
	 */
	uint32_t play(uint32_t tstates) {

100 101 102 103 104 105 106 107
		if (this->counter0 > 0) {
			if (this->counter0 >= tstates) {
				this->counter0 -= tstates;
				this->signal = 0;
				return 0;
			} else {
				tstates -= this->counter0;
				this->counter0 = 0;
Sergio Costas's avatar
Sergio Costas committed
108
			}
109 110 111 112 113 114 115 116 117
		}
		if (this->counter1 > 0) {
			if (this->counter1 >= tstates) {
				this->counter1 -= tstates;
				this->signal = 1;
				return 0;
			} else {
				tstates -= this->counter1;
				this->counter1 = 0;
Sergio Costas's avatar
Sergio Costas committed
118 119
			}
		}
120
		return tstates;
Sergio Costas's avatar
Sergio Costas committed
121 122 123 124 125 126
	}

	/**
	 * Sets the time values for the next bit to be read from the tape
	 * @return TRUE if there is a new bit; FALSE if there are no more bits in this block
	 */
127
	virtual bool next_bit() = 0;
Sergio Costas's avatar
Sergio Costas committed
128 129 130 131 132 133 134 135

	/**
	 * Resets a block to its initial state. Mandatory before starting working with it
	 */
	virtual void reset() {
	}
};

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
class EndLoopBlock  : public TapeBlock {

private:
	uint16_t counter;
	class TapeBlock *loop;
public:
	virtual bool next_bit() {
		return false;
	}

	void reset_counter(uint16_t value) {
		this->counter = value;
	}

	virtual class TapeBlock *next_block() {
		if (this->counter != 0) {
152
			this->counter--;
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
			return (this->loop->next_block());
		} else {
			return this->next;
		}
	}

	void set_loop_block(class TapeBlock *block) {
		this->loop = block;
	}
};

class LoopBlock  : public TapeBlock {

private:
	uint16_t loop;
	class EndLoopBlock *endblock;
public:

	LoopBlock(uint16_t reps) {
		this->loop = reps;
		this->endblock = NULL;
	}
	/**
	 * Adds a new block to the end of the list
	 * @param block The block to add to the list
	 */
	virtual void add_block(class TapeBlock *new_block) {

		if (this->next == NULL) {
			this->next = new_block;
		} else {
			this->next->add_block(new_block);
		}
		if (this->endblock == NULL) {
			this->endblock = dynamic_cast<EndLoopBlock *>(new_block);
			if (this->endblock != NULL) {
				this->endblock->set_loop_block(this);
			}
		}
	}

	virtual void reset() {
		if (this->endblock != NULL) {
			this->endblock->reset_counter(this->loop);
		}
	}

	/**
	 * Returns the next block in the list
	 * @return The block next to this one
	 */
	virtual class TapeBlock *next_block() {

		return this->next;
	}

	virtual bool next_bit() {
		return false;
	}
};

214
class FullBlock : public TapeBlock {
215 216 217 218 219 220 221 222 223 224 225 226 227

	uint8_t *data;
	uint32_t data_size;
	uint16_t pilot;
	uint16_t sync0;
	uint16_t sync1;
	uint16_t zero;
	uint16_t one;
	uint16_t lpilot;
	uint8_t bits_at_end;
	uint32_t pause;

	uint8_t bit;
228
	uint8_t bit_counter;
229
	uint32_t loop;
230
	bool allow_fast_load;
231 232 233 234 235 236 237 238 239 240 241 242 243 244

	int pointer;
	enum TURBOBLOCK_states {TURBOBLOCK_GUIDE, TURBOBLOCK_DATA, TURBOBLOCK_PAUSE} current_state;

	void set_state(enum TURBOBLOCK_states new_state) {

		this->current_state = new_state;
		switch (new_state) {
		case TURBOBLOCK_GUIDE:
			this->loop = this->lpilot/2;
		break;
		case TURBOBLOCK_DATA:
			this->pointer = 0;
			this->bit = 0x80;
245
			this->bit_counter = (this->data_size == 1) ? this->bits_at_end : 8;
246 247 248 249 250 251 252 253
		break;
		case TURBOBLOCK_PAUSE:
		break;
		}
	}

public:

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
	FullBlock(uint8_t *data,uint32_t size, uint16_t pause) :TapeBlock() {

		this->allow_fast_load = true;
		this->data = new uint8_t[size];
		this->data_size = size;
		this->pilot = 2168;
		this->sync0 = 667;
		this->sync1 = 735;
		this->zero = 855;
		this->one = 1710;
		if (!(0x80 & data[0])) {
			this->lpilot = 8063; // 5 seconds (aprox)
		} else {
			this->lpilot = 3223; // 2 seconds (aprox)
		}
		this->bits_at_end = 8;
		this->pause = (uint32_t)pause;

		memcpy(this->data,data,size);
		this->current_state = TURBOBLOCK_GUIDE;
		this->loop = this->lpilot;
		this->pointer = 0;
		this->bit = 0x80;
		this->bit_counter = 8;
		this->set_state(TURBOBLOCK_GUIDE);
	}

	FullBlock(uint8_t *data,uint32_t size, uint16_t pilot, uint16_t sync0, uint16_t sync1, uint16_t zero, uint16_t one, uint16_t lpilot, uint8_t bits_at_end, uint16_t pause) :TapeBlock() {

		this->allow_fast_load = false;
284 285 286 287 288 289 290 291 292 293 294 295 296
		this->data = new uint8_t[size];
		this->data_size = size;
		this->pilot = pilot;
		this->sync0 = sync0;
		this->sync1 = sync1;
		this->zero = zero;
		this->one = one;
		this->lpilot = lpilot;
		this->bits_at_end = bits_at_end;
		this->pause = (uint32_t)pause;

		memcpy(this->data,data,size);
		this->current_state = TURBOBLOCK_GUIDE;
297
		this->loop = lpilot;
298 299
		this->pointer = 0;
		this->bit = 0x80;
300
		this->bit_counter = 8;
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
		this->set_state(TURBOBLOCK_GUIDE);
	}

	void reset() {
		this->counter0 = 0;
		this->counter1 = 0;
		this->set_state(TURBOBLOCK_GUIDE);
	}

	bool next_bit() {

		bool current_bit;

		switch (this->current_state) {
		case TURBOBLOCK_GUIDE:
			if (this->loop > 0) {
				// guide tone loop
				this->counter0 = this->pilot;
				this->counter1 = this->pilot;
				this->loop--;
			} else {
				// sync bit
				this->counter0 = this->sync0;
				this->counter1 = this->sync1;
				this->set_state(TURBOBLOCK_DATA);
			}
			return true;
		break;
		case TURBOBLOCK_DATA:
330
			if (this->bit_counter == 0) {
331 332
				this->bit = 0x80;
				this->pointer++;
333 334 335 336 337
				if ((this->data_size-1) == this->pointer) {
					this->bit_counter = this->bits_at_end;
				} else {
					this->bit_counter = 8;
				}
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
				if (this->pointer == this->data_size) {
					this->counter0 = this->pause * 3500;
					this->counter1 = 0;
					this->set_state(TURBOBLOCK_PAUSE);
					return true;
				}
			}
			current_bit = ((*(this->data+this->pointer)) & this->bit) == 0;
			if (current_bit) {
				this->counter0 = this->zero;
				this->counter1 = this->zero;
			} else {
				this->counter0 = this->one;
				this->counter1 = this->one;
			}
			this->bit /=2;
354
			this->bit_counter--;
355 356 357 358 359 360 361 362
			return true;
		break;
		case TURBOBLOCK_PAUSE:
			return false; // end of data
		break;
		}
		return true;
	}
363 364 365 366 367 368 369 370 371 372 373

	bool fast_load(uint8_t *data, uint16_t &size, uint8_t &flag) {
		if (this->allow_fast_load) {
			flag = this->data[0];
			size = this->data_size - 2;
			memcpy(data,this->data+1,size);
			return true;
		} else {
			return false;
		}
	}
374 375
};

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
class PureDataBlock : public TapeBlock {

	uint8_t *data;
	uint32_t data_size;
	uint16_t zero;
	uint16_t one;
	uint8_t bits_at_end;
	uint32_t pause;

	uint8_t bit;
	uint8_t bit_counter;

	int pointer;

public:

	PureDataBlock(uint16_t zero, uint16_t one, uint8_t bits_at_end, uint16_t pause, uint32_t size, uint8_t *data) :TapeBlock() {

		this->data = new uint8_t[size];
		this->data_size = size;
		this->zero = zero;
		this->one = one;
		this->bits_at_end = bits_at_end;
		this->pause = (uint32_t)pause;

		memcpy(this->data,data,size);
		this->pointer = 0;
		this->bit = 0x80;
404
		this->bit_counter = (this->data_size == 1) ? this->bits_at_end : 8;
405 406 407 408 409 410 411
	}

	void reset() {
		this->counter0 = 0;
		this->counter1 = 0;
		this->bit = 0x80;
		this->pointer = 0;
412 413
		this->bit_counter = (this->data_size == 1) ? this->bits_at_end : 8;
		this->next_bit();
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
	}

	bool next_bit() {

		bool current_bit;

		if (this->data_size == this->pointer) {
			return false;
		}

		if (this->bit_counter == 0) {
			this->bit = 0x80;
			this->pointer++;
			if ((this->data_size-1) == this->pointer) {
				this->bit_counter = this->bits_at_end;
			} else {
				this->bit_counter = 8;
			}
			if (this->pointer == this->data_size) {
				this->counter0 = this->pause * 3500;
				this->counter1 = 0;
				return true;
			}
		}
		current_bit = ((*(this->data+this->pointer)) & this->bit) == 0;
		if (current_bit) {
			this->counter0 = this->zero;
			this->counter1 = this->zero;
		} else {
			this->counter0 = this->one;
			this->counter1 = this->one;
		}
446
		this->bit /= 2;
447 448 449 450 451 452
		this->bit_counter--;
		return true;
	}
};


453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
class ToneBlock : public TapeBlock {

	uint16_t pilot;
	uint16_t lpilot;
	uint32_t loop;

public:

	ToneBlock(uint16_t pilot, uint16_t lpilot) :TapeBlock() {

		this->pilot = pilot;
		this->lpilot = lpilot;
		this->loop = lpilot;
	}

	void reset() {
		this->counter0 = 0;
		this->counter1 = 0;
471
		this->loop = lpilot/2;
472
		this->next_bit();
473 474 475
		if (lpilot%2) {
			printf("odd number of pulses in Pure Tone Block\n");
		}
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
	}

	bool next_bit() {

		if (this->loop > 0) {
			// guide tone loop
			this->counter0 = this->pilot;
			this->counter1 = this->pilot;
			this->loop--;
			return true;
		} else {
			return false;
		}
	}
};

492 493 494 495 496 497 498 499 500 501 502 503 504 505
class PulsesBlock : public TapeBlock {

	uint16_t pulses[255];
	uint8_t npulses;
	uint8_t loop;
	uint8_t counter;

public:

	PulsesBlock(uint8_t npulses, uint16_t *pulses) :TapeBlock() {

		this->npulses = npulses;
		this->loop = npulses;
		this->counter = 0;
506
		memcpy(this->pulses,pulses,npulses*2);
507 508 509
	}

	void reset() {
510
		this->counter = 0;
511 512 513
		this->counter0 = 0;
		this->counter1 = 0;
		this->loop = this->npulses/2;
514
		this->next_bit();
515 516 517 518 519 520 521
		if (this->npulses%2) {
			printf("Odd number of pulses in Pulses Block\n");
		}
	}

	bool next_bit() {

522
		if (this->loop != 0) {
523 524 525 526 527 528 529 530 531 532 533
			// guide tone loop
			this->counter0 = this->pulses[this->counter++];
			this->counter1 = this->pulses[this->counter++];
			this->loop--;
			return true;
		} else {
			return false;
		}
	}
};

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
class PauseBlock : public TapeBlock {

	uint32_t length;
	Tape *tape;

public:

	PauseBlock(Tape *tape, uint16_t length) :TapeBlock() {

		this->length = length;
		this->tape = tape;
	}

	void reset() {
		this->counter0 = this->length;
		this->counter1 = 0;
	}

	bool next_bit() {

		if (this->length == 0) {
			this->tape->set_pause(true);
		} else {
			this->counter0 = this->length;
			this->counter1 = 0;
		}
		return false;
	}
};

Sergio Costas's avatar
Sergio Costas committed
564 565 566 567
Tape::Tape() {
	this->blocks = NULL;
	this->current_block = NULL;
	this->paused = true;
568
	this->block_accesed = false;
Sergio Costas's avatar
Sergio Costas committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
}

Tape::~Tape() {
	this->delete_blocks();
}

void Tape::add_block(class TapeBlock *new_block) {

	if (this->blocks == NULL) {
		this->blocks = new_block;
	} else {
		this->blocks->add_block(new_block);
	}
}

void Tape::delete_blocks() {

	delete (this->blocks);
	this->blocks = NULL;
	this->current_block = NULL;
589
	this->block_accesed = false;
Sergio Costas's avatar
Sergio Costas committed
590 591
}

592 593
bool Tape::load_file(string filename) {

Sergio Costas's avatar
Sergio Costas committed
594
	char char_id[10];
Sergio Costas's avatar
Sergio Costas committed
595
	this->delete_blocks();
Sergio Costas's avatar
Sergio Costas committed
596

597
	this->paused = true;
598
	FILE *file = fopen(filename.c_str(),"rb");
Sergio Costas's avatar
Sergio Costas committed
599 600 601
	if (file == NULL) {
		return true; // error while opening the file
	}
Sergio Costas's avatar
Sergio Costas committed
602
	fread(char_id,10,1,file); // read the (maybe) TZX header
Sergio Costas's avatar
Sergio Costas committed
603 604
	fclose(file);
	if((!strncmp(char_id,"ZXTape!",7)) && (char_id[7]==0x1A)&&(char_id[8]==1)) {
Sergio Costas's avatar
Sergio Costas committed
605
		return this->load_tzx(filename);
Sergio Costas's avatar
Sergio Costas committed
606 607
	} else {
		return this->load_tap(filename);
Sergio Costas's avatar
Sergio Costas committed
608 609 610
	}
}

611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
bool Tape::read_8bit(FILE *file, uint8_t &value) {

	uint8_t data;
	size_t retval;

	retval = fread (&data, 1, 1, file);
	if (retval < 1) {
		fclose(file); // end-of-file
		return true;
	}
	value = data;
	return false;
}

bool Tape::read_16bit(FILE *file, uint16_t &value) {

	uint8_t data[2];
	size_t retval;

	retval = fread (data, 2, 1, file);
	if (retval < 1) {
		fclose(file); // end-of-file
		return true;
	}
	value = ((uint16_t) data[0]) + 256 * ((uint16_t) data[1]);
	return false;
}

bool Tape::read_24bit(FILE *file, uint32_t &value) {

	uint8_t data[3];
	size_t retval;

	retval = fread (data, 3, 1, file);
	if (retval < 1) {
		fclose(file); // end-of-file
		return true;
	}
	value = ((uint32_t) data[0]) + 256 * ((uint32_t) data[1]) + 65536 * ((uint32_t) data[2]);
	return false;
}

653
bool Tape::load_tap(string filename) {
Sergio Costas's avatar
Sergio Costas committed
654 655 656 657 658 659

	FILE *file;
	uint8_t data[65536];
	uint16_t size;
	size_t retval;

660
	file = fopen(filename.c_str(),"rb");
Sergio Costas's avatar
Sergio Costas committed
661 662 663 664 665
	if (file == NULL) {
		return true; // error while opening the file
	}
	do {
		// read block size
666
		if (this->read_16bit(file,size)) {
667
			return false;
Sergio Costas's avatar
Sergio Costas committed
668 669 670 671 672 673
		}
		retval = fread (data, size, 1, file);
		if (retval != 1) {
			fclose(file);
			return (true); // end-of-file and error
		}
674
		this->add_block(new FullBlock(data,size,1000));
675
	} while(!feof(file));
Sergio Costas's avatar
Sergio Costas committed
676 677 678 679 680 681

	fclose(file);
	this->current_block = this->blocks;
	return false;
}

682
bool Tape::load_tzx(string filename) {
Sergio Costas's avatar
Sergio Costas committed
683

684 685 686 687 688
	FILE *file;
	uint8_t block_type;
	uint8_t tmpdata[10];
	size_t retval;

689
	file = fopen(filename.c_str(),"rb");
690 691 692 693 694 695 696 697 698 699 700 701
	if (file == NULL) {
		return true; // error while opening the file
	}
	fread(tmpdata,10,1,file); // read TZX header
	while(true) {
		// read block type
		retval = fread (&block_type, 1, 1, file);
		if (retval < 1) {
			break; // end-of-file
		}
		switch(block_type) {
		case 0x10: // standard block
702 703 704 705 706 707 708 709 710 711 712 713
		{
			uint8_t data[65536];
			uint16_t size;
			uint16_t pause;
			size_t retval;

			// read pause duration
			if (this->read_16bit(file,pause)) {
				return true;
			}
			// read block size
			if (this->read_16bit(file,size)) {
714 715
				return true;
			}
716 717 718 719 720 721 722
			retval = fread (data, size, 1, file);
			if (retval != 1) {
				fclose(file);
				return (true); // end-of-file and error
			}
			this->add_block(new FullBlock(data,size,pause));
		}
723
		break;
724
		case 0x11: // turbo block
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
		{
			uint8_t *data;
			uint32_t size;
			uint16_t pause;
			uint16_t pilot;
			uint16_t sync0;
			uint16_t sync1;
			uint16_t zero;
			uint16_t one;
			uint16_t lpilot;
			uint8_t bits_at_end;
			size_t retval;

			// read pilot pulse duration
			if (this->read_16bit(file,pilot)) {
				return true;
			}
			// read sync duration
			if (this->read_16bit(file,sync0)) {
				return true;
			}
			if (this->read_16bit(file,sync1)) {
				return true;
			}
			// read bit duration
			if (this->read_16bit(file,zero)) {
				return true;
			}
			if (this->read_16bit(file,one)) {
				return true;
			}
			// read pilot duration
			if (this->read_16bit(file,lpilot)) {
				return true;
			}
			// read number of bits at end
			if (this->read_8bit(file,bits_at_end)) {
				return true;
			}
			// read pause duration
			if (this->read_16bit(file,pause)) {
				return true;
			}
			// read block size
			if (this->read_24bit(file,size)) {
770 771
				return true;
			}
772 773 774 775 776 777 778 779 780
			data = new uint8_t[size];
			retval = fread (data, size, 1, file);
			if (retval != 1) {
				fclose(file);
				return (true); // end-of-file and error
			}
			this->add_block(new FullBlock(data,size,pilot,sync0,sync1,zero,one,lpilot,bits_at_end,pause));
		}

781
		break;
782
		case 0x12: // tone block
783 784 785 786 787 788 789 790 791 792
		{
			uint16_t pilot;
			uint16_t lpilot;

			// read pilot pulse duration
			if (this->read_16bit(file,pilot)) {
				return true;
			}
			// read pilot duration
			if (this->read_16bit(file,lpilot)) {
793 794
				return true;
			}
795 796
				this->add_block(new ToneBlock(pilot,lpilot));
		}
797 798
		break;
		case 0x20: // Pause block
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
		{
			uint16_t length;
			// read pilot pulse duration
			if (this->read_16bit(file,length)) {
				return true;
			}
			this->add_block(new PauseBlock(this,length));
		}
		break;
		case 0x32: // Archive Info block
			if (this->tzx_generic_16_block(file)) {
				return true;
			}
		break;
		case 0x21: // group start
			if (this->tzx_generic_8_block(file)) {
				return true;
			}
		break;
		case 0x22: // group end
		break;
820
		case 0x24: // Loop Start
821 822 823 824 825 826 827 828 829
		{
			uint16_t repetitions;
			// read repetitions
			if (this->read_16bit(file,repetitions)) {
				return true;
			}
			this->add_block(new LoopBlock(repetitions));
		}
		break;
830
		case 0x25: // Loop End
831 832 833 834 835 836 837 838 839 840 841
		{
			this->add_block(new EndLoopBlock());
		}
		break;
		case 0x13:
		{
			uint8_t npulses;
			uint8_t loop;
			uint16_t pulses[255];
			// read repetitions
			if (this->read_8bit(file,npulses)) {
842 843
				return true;
			}
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
			for(loop=0;loop<npulses;loop++) {
				if (this->read_16bit(file,pulses[loop])) {
					return true;
				}
			}
			this->add_block(new PulsesBlock(npulses,pulses));
		}
		break;
		case 0x14:
		{
			uint32_t npulses;
			uint32_t loop;
			uint8_t *data;
			uint16_t pulse_0;
			uint16_t pulse_1;
			uint8_t final_bits;
			uint16_t pause;
			// read pulse lengths
			if (this->read_16bit(file,pulse_0)) {
				return true;
			}
			if (this->read_16bit(file,pulse_1)) {
				return true;
			}
			// read final bit number
			if (this->read_8bit(file,final_bits)) {
				return true;
			}
			// read pause
			if (this->read_16bit(file,pause)) {
				return true;
			}
			// read repetitions
			if (this->read_24bit(file,npulses)) {
				return true;
			}
			data = new uint8_t[npulses];
			for(loop=0;loop<npulses;loop++) {
				if (this->read_8bit(file,data[loop])) {
					return true;
				}
			}
			this->add_block(new PureDataBlock(pulse_0, pulse_1, final_bits, pause, npulses, data));
		}
888
		break;
889 890 891 892 893 894 895 896 897 898 899 900
		default:
			printf("Block unknown: %X\n",block_type);
			return true;
		break;
		}
	}

	fclose(file);
	this->current_block = this->blocks;
	return false;
}

901
bool Tape::tzx_generic_8_block(FILE *file) {
902

903 904
	uint8_t length;
	uint8_t data[256];
905

906 907
	// read block length
	if (this->read_8bit(file,length)) {
908 909
		return true;
	}
910
	if (1!=fread(data,length,1,file)) {
911 912 913 914 915
		return true;
	}
	return false;
}

916
bool Tape::tzx_generic_16_block(FILE *file) {
917 918

	uint16_t length;
919
	uint8_t data[65536];
920

921
	// read block length
922 923 924
	if (this->read_16bit(file,length)) {
		return true;
	}
925 926 927
	if (1!=fread(data,length,1,file)) {
		return true;
	}
928 929 930
	return false;
}

Sergio Costas's avatar
Sergio Costas committed
931 932 933 934
void Tape::play(uint32_t tstates) {

	uint32_t residue = tstates;

Sergio Costas's avatar
Sergio Costas committed
935 936 937 938 939 940 941 942
	if (this->current_block == NULL) {
		return;
	}

	if (this->paused) {
		return;
	}

943
	this->block_accesed = true;
Sergio Costas's avatar
Sergio Costas committed
944 945 946
	while(true) {
		residue = this->current_block->play(residue);
		if (residue != 0) {
Sergio Costas's avatar
Sergio Costas committed
947 948 949 950 951 952
			if (this->current_block->next_bit() == false) {
				this->current_block = this->current_block->next_block();
				if (this->current_block == NULL) {
					this->current_block = this->blocks;
				}
				this->current_block->reset();
953
				this->block_accesed = false;
Sergio Costas's avatar
Sergio Costas committed
954 955 956 957
			}
		} else {
			break;
		}
958 959 960
		if (this->paused) {
			break;
		}
Sergio Costas's avatar
Sergio Costas committed
961 962 963 964 965 966 967 968 969 970 971
	}
}

uint8_t Tape::read_signal() {

	if (this->current_block == NULL) {
		return 0;
	} else {
		return this->current_block->read_signal();
	}
}
Sergio Costas's avatar
Sergio Costas committed
972 973 974 975 976 977

void Tape::rewind() {
	this->current_block = this->blocks;
	if (this->current_block != NULL) {
		this->current_block->reset();
	}
978
	this->block_accesed = false;
Sergio Costas's avatar
Sergio Costas committed
979 980 981 982
}

void Tape::set_pause(bool pause) {
	this->paused = pause;
983 984 985
	if (pause) {
		this->send_signal("tape_paused");
	}
Sergio Costas's avatar
Sergio Costas committed
986
}
987

988 989 990 991
bool Tape::get_pause() {
	return this->paused;
}

992
enum FastLoadReturn Tape::fast_read(uint8_t *data, uint16_t &length,uint8_t flag) {
993

994
	uint8_t block_flag;
995 996

	if (this->blocks == NULL) {
997
		return FASTLOAD_NO_TAPE;
998 999 1000 1001 1002 1003 1004
	}

	if (this->block_accesed) {
		if (this->current_block != NULL) {
			this->current_block = this->current_block->next_block();
		}
	}
1005 1006 1007
	if (this->current_block == NULL) {
		return FASTLOAD_END_TAPE;
	}
1008
	this->block_accesed = false;
1009

1010
	if (!this->current_block->fast_load(data,length,block_flag)) {
1011 1012
		return FASTLOAD_NO_BLOCK;
	}
1013 1014 1015 1016 1017
	this->current_block = this->current_block->next_block();
	if (this->current_block != NULL) {
		this->current_block->reset();
	}

1018
	if (block_flag != flag) {
1019 1020
		return FASTLOAD_NO_FLAG;
	}
1021

1022
	return FASTLOAD_OK;
1023
}