emulator.cpp 16.7 KB
Newer Older
Sergio Costas's avatar
Sergio Costas committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright 2003-2009 (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/>.
17
 *
Sergio Costas's avatar
Sergio Costas committed
18 19 20 21 22 23 24
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
Sergio Costas's avatar
Sergio Costas committed
25

26 27
#include "z80free/Z80free.h"
#include "emulator.hh"
Sergio Costas's avatar
Sergio Costas committed
28 29
#include "cargador.hh"
#include "computer.hh"
30
#include "llscreen.hh"
Sergio Costas's avatar
Sergio Costas committed
31 32
#include "menus.hh"
#include "microdrive.hh"
33 34
#include "computer.hh"
#include "cmdline.hh"
35
#include "llsound.hh"
36
#include "keyboard.hh"
37
#include "spk_ay.hh"
38
#include "mouse.hh"
Sergio Costas's avatar
Sergio Costas committed
39

40
bool debug_var = false;
Sergio Costas's avatar
Sergio Costas committed
41 42

Z80FREE procesador;
43
char salir;
Sergio Costas's avatar
Sergio Costas committed
44 45 46
char path_snaps[2049];
char path_taps[2049];
char path_mdrs[2049];
47
unsigned int jump_frames, curr_frames;
48
string filenames[5];
Sergio Costas's avatar
Sergio Costas committed
49

50
string load_a_rom(string *filenames) {
Sergio Costas's avatar
Sergio Costas committed
51
	
52
	string *pointer;
Sergio Costas's avatar
Sergio Costas committed
53
	int offset=0;
54
	ifstream *fichero;
55

56 57 58
	for(pointer=filenames; *pointer != ""; pointer++) {
		fichero=llscreen->myfopen(*pointer,ios::in|ios::binary);
		if(fichero == NULL) {
Sergio Costas's avatar
Sergio Costas committed
59 60
			return (*pointer);
		}
61
		fichero->read((char*)ordenador->memoria+offset,16384);
Sergio Costas's avatar
Sergio Costas committed
62
		offset+=16384;
63 64
		fichero->close();
		delete fichero;
Sergio Costas's avatar
Sergio Costas committed
65
	}
66
	return "";
Sergio Costas's avatar
Sergio Costas committed
67 68 69 70
}

void load_rom(char type) {

71 72
	string retval;
	ifstream *fichero;
Sergio Costas's avatar
Sergio Costas committed
73 74 75 76

	switch(type) {
	case 0:
		filenames[0]="spectrum-roms/48.rom";
77
		filenames[1]="";
Sergio Costas's avatar
Sergio Costas committed
78
		retval=load_a_rom(filenames);
79 80
		if (retval != "") {
			printf("Can't load file %s\n",retval.c_str());
Sergio Costas's avatar
Sergio Costas committed
81 82 83 84 85 86
			exit(1);
		}
	break;
	case 1:
		filenames[0]="spectrum-roms/128-0.rom";
		filenames[1]="spectrum-roms/128-1.rom";
87
		filenames[2]="";
Sergio Costas's avatar
Sergio Costas committed
88
		retval=load_a_rom(filenames);
89 90
		if (retval != "") {
			printf("Can't load file %s\n",retval.c_str());
Sergio Costas's avatar
Sergio Costas committed
91 92 93 94 95 96
			exit(1);
		}
	break;
	case 2:
		filenames[0]="spectrum-roms/plus2-0.rom";
		filenames[1]="spectrum-roms/plus2-1.rom";
97
		filenames[2]="";
Sergio Costas's avatar
Sergio Costas committed
98
		retval=load_a_rom(filenames);
99 100
		if (retval != "") {
			printf("Can't load file %s\n",retval.c_str());
Sergio Costas's avatar
Sergio Costas committed
101 102 103 104 105 106 107 108 109 110
			exit(1);
		}
	break;
	case 3:
		// first, try last version of PLUS3 roms
		
		filenames[0]="spectrum-roms/plus3-41-0.rom";
		filenames[1]="spectrum-roms/plus3-41-1.rom";
		filenames[2]="spectrum-roms/plus3-41-2.rom";
		filenames[3]="spectrum-roms/plus3-41-3.rom";
111
		filenames[4]="";
Sergio Costas's avatar
Sergio Costas committed
112
		retval=load_a_rom(filenames);
113
		if (retval != "") {
Sergio Costas's avatar
Sergio Costas committed
114 115 116 117 118
			printf("Can't load the Spectrum +3 ROM version 4.1. Trying with version 4.0\n");
			filenames[0]="spectrum-roms/plus3-40-0.rom";
			filenames[1]="spectrum-roms/plus3-40-1.rom";
			filenames[2]="spectrum-roms/plus3-40-2.rom";
			filenames[3]="spectrum-roms/plus3-40-3.rom";
119
			filenames[4]="";
Sergio Costas's avatar
Sergio Costas committed
120
			retval=load_a_rom(filenames);
121
			if (retval != "") {
Sergio Costas's avatar
Sergio Costas committed
122 123 124 125 126
				printf("Can't load the Spectrum +3 ROM version 4.0. Trying with legacy filenames\n");
				filenames[0]="spectrum-roms/plus3-0.rom";
				filenames[1]="spectrum-roms/plus3-1.rom";
				filenames[2]="spectrum-roms/plus3-2.rom";
				filenames[3]="spectrum-roms/plus3-3.rom";
127
				filenames[4]="";
Sergio Costas's avatar
Sergio Costas committed
128
				retval=load_a_rom(filenames);
129 130
				if (retval != "") {
					printf("Can't load file %s\n",retval.c_str());
Sergio Costas's avatar
Sergio Costas committed
131 132 133 134 135 136 137 138
					exit(1);
				}
			}
		}
	break;
	case 4:
		filenames[0]="spectrum-roms/128-spanish-0.rom";
		filenames[1]="spectrum-roms/128-spanish-1.rom";
139
		filenames[2]="";
Sergio Costas's avatar
Sergio Costas committed
140
		retval=load_a_rom(filenames);
141 142
		if (retval != "") {
			printf("Can't load file %s\n",retval.c_str());
Sergio Costas's avatar
Sergio Costas committed
143 144 145 146 147
			exit(1);
		}
	break;
	}
  
148 149 150
	fichero=llscreen->myfopen("spectrum-roms/if1-2.rom",ios::in|ios::binary); // load Interface1 ROM
	if(fichero == NULL) {
		delete fichero;
Sergio Costas's avatar
Sergio Costas committed
151
		// try legacy name
152 153 154
		fichero=llscreen->myfopen("spectrum-roms/if1-v2.rom",ios::in|ios::binary);
		if(fichero == NULL) {
			delete fichero;
Sergio Costas's avatar
Sergio Costas committed
155 156 157 158
			printf("Can't open Interface1 ROM file\n");
			exit(1);
		}
	}
159 160 161
	fichero->read((char*)ordenador->shadowrom,8192);
  	fichero->close();
  	delete fichero;
Sergio Costas's avatar
Sergio Costas committed
162 163 164 165
}

void end_system() {
	
166
	delete(llsound);
167
	delete(llscreen);
Sergio Costas's avatar
Sergio Costas committed
168 169 170

}

171
void load_main_game(const char *nombre) {
Sergio Costas's avatar
Sergio Costas committed
172

Sergio Costas's avatar
Sergio Costas committed
173
	int longitud;
Sergio Costas's avatar
Sergio Costas committed
174 175 176 177 178 179
	char *puntero;
	
	longitud=strlen(nombre);
	if (longitud<5) {
		return;
	}
180
	puntero=(char *)(nombre+(longitud-4));
Sergio Costas's avatar
Sergio Costas committed
181 182 183 184 185 186
	if ((0==strcasecmp(".z80",puntero))||(0==strcasecmp(".sna",puntero))) {
		load_z80(nombre);
		return;
	}
	
	if ((0==strcasecmp(".tap",puntero))||(0==strcasecmp(".tzx",puntero))) {
187 188
		ordenador->current_tap = nombre;
		OOTape->load_file(nombre);
Sergio Costas's avatar
Sergio Costas committed
189 190 191 192
		return;
	}
}

193
void save_config() {
Sergio Costas's avatar
Sergio Costas committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207
	
	char config_path[1024];
	int length;
	FILE *fconfig;
	
	strcpy(config_path,getenv("HOME"));
	length=strlen(config_path);
	if ((length>0)&&(config_path[length-1]!='/'))
		strcat(config_path,"/");
	strcat(config_path,".fbzx");
	fconfig = fopen(config_path,"wb");
	if (fconfig==NULL) {
		return;
	}
208 209
	fprintf(fconfig,"mode=%c%c",48+ordenador->mode128k,10);
	fprintf(fconfig,"issue=%c%c",48+ordenador->issue,10);
210
	fprintf(fconfig,"joystick=%c%c",48+keyboard->joystick,10);
211
	fprintf(fconfig,"ay_sound=%c%c",48+spk_ay->ay_emul,10);
212
	fprintf(fconfig,"interface1=%c%c",48+microdrive->mdr_active,10);
213
	fprintf(fconfig,"doublescan=%c%c",ordenador->dblscan ? '1' : '0',10);
214
	fprintf(fconfig,"volume=%c%c",65+(llsound->volume/4),10);
215 216 217
	fprintf(fconfig,"bw=%c%c",ordenador->bw ? '1' : '0',10);
	fprintf(fconfig,"fast=%c%c",ordenador->tape_fast_load ? '1' : '0',10);
	fprintf(fconfig,"turboload=%c%c",ordenador->turbo_play ? '1' : '0',10);
218
	fprintf(fconfig,"mouse=%c%c",mouse->enabled ? '1' : '0',10);
Sergio Costas's avatar
Sergio Costas committed
219 220 221
	fclose(fconfig);
}

222
void load_config() {
Sergio Costas's avatar
Sergio Costas committed
223 224 225 226 227
	
	char config_path[1024];
	char line[1024],carac,done;
	int length,pos;
	FILE *fconfig;
228
	unsigned char volume=255,mode128k=255,issue=255,joystick=255,ay_emul=255,mdr_active=255,dblscan=255,bw=255,fast=255,turboload=255,mouse_enabled=255;
Sergio Costas's avatar
Sergio Costas committed
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 284 285 286 287 288 289 290
	
	strcpy(config_path,getenv("HOME"));
	length=strlen(config_path);
	if ((length>0)&&(config_path[length-1]!='/'))
		strcat(config_path,"/");
	strcat(config_path,".fbzx");
	fconfig = fopen(config_path,"rb");
	if (fconfig==NULL) {
		return;
	}
	
	done=1;
	pos=0;
	line[0]=0;
	while(!feof(fconfig)) {
		if (done) {
			line[0]=0;
			pos=0;
			done=0;
		}
		if (0!=fread(&carac,1,1,fconfig)) {
			if ((carac!=13)&&(carac!=10)) {
				line[pos]=carac;
				if (pos<1023) {
					pos++;
				}
				continue;
			}
		}
		done=1;
		line[pos]=0;
		if (line[0]=='#') { // coment
			continue;
		}
		if (!strncmp(line,"mode=",5)) {
			mode128k=line[5]-'0';
			continue;
		}
		if (!strncmp(line,"issue=",6)) {
			issue=line[6]-'0';
			continue;
		}
		if (!strncmp(line,"joystick=",9)) {
			joystick=line[9]-'0';
			continue;
		}
		if (!strncmp(line,"ay_sound=",9)) {
			ay_emul=line[9]-'0';
			continue;
		}
		if (!strncmp(line,"interface1=",11)) {
			mdr_active=line[11]-'0';
			continue;
		}
		if (!strncmp(line,"doublescan=",11)) {
			dblscan=line[11]-'0';
			continue;
		}
		if (!strncmp(line,"volume=",7)) {
			volume=4*(line[7]-'A');
			continue;
		}
291 292 293 294
		if (!strncmp(line,"bw=",3)) {
			bw=(line[3]-'0');
			continue;
		}
295 296 297 298 299 300 301 302
		if (!strncmp(line,"fast=",5)) {
			fast=(line[5]-'0');
			continue;
		}
		if (!strncmp(line,"turboload=",10)) {
			turboload=(line[10]-'0');
			continue;
		}
303 304 305 306
		if (!strncmp(line,"mouse=",6)) {
			mouse_enabled=(line[6]-'0');
			continue;
		}
Sergio Costas's avatar
Sergio Costas committed
307 308 309
	}
	
	if (mode128k<5) {
310
		ordenador->mode128k=mode128k;
Sergio Costas's avatar
Sergio Costas committed
311 312
	}
	if (issue<4) {
313
		ordenador->issue=issue;
Sergio Costas's avatar
Sergio Costas committed
314 315
	}
	if (joystick<4) {
316 317 318 319 320 321 322 323 324 325 326 327 328 329
		switch (joystick) {
		case 0:
			keyboard->joystick = JOYSTICK_CURSOR;
		break;
		case 1:
			keyboard->joystick = JOYSTICK_KEMPSTON;
		break;
		case 2:
			keyboard->joystick = JOYSTICK_SINCLAIR1;
		break;
		case 3:
			keyboard->joystick = JOYSTICK_SINCLAIR2;
		break;
		}
Sergio Costas's avatar
Sergio Costas committed
330 331
	}
	if (ay_emul<2) {
332
		spk_ay->ay_emul=ay_emul;
Sergio Costas's avatar
Sergio Costas committed
333 334
	}
	if (mdr_active<2) {
335
		microdrive->mdr_active=mdr_active;
Sergio Costas's avatar
Sergio Costas committed
336 337
	}
	if (dblscan<2) {
338
		ordenador->dblscan = dblscan==0 ? false : true;
Sergio Costas's avatar
Sergio Costas committed
339
	}
340
	if (bw<2) {
341 342 343 344 345 346 347
		ordenador->bw = bw==0 ? false : true;
	}
	if (fast<2) {
		ordenador->tape_fast_load = fast==0 ? false : true;
	}
	if (turboload<2) {
		ordenador->turbo_play = turboload==0 ? false : true;
348
	}
349 350 351
	if (mouse_enabled<2) {
		mouse->enabled = mouse_enabled==0 ? false : true;
	}
Sergio Costas's avatar
Sergio Costas committed
352
	if (volume<255) {
353
		llsound->set_volume(volume);
Sergio Costas's avatar
Sergio Costas committed
354 355 356 357 358 359 360
	}
	
	fclose(fconfig);
}

int main(int argc,char *argv[]) {

361
	int bucle,tstados,argumento,length;
362
	string gamefile;
Sergio Costas's avatar
Sergio Costas committed
363 364
	word PC=0;

365 366 367 368 369 370 371 372 373 374 375
	CMDLine parse(argc,argv);

	osd = new OSD();
	if ((!parse.mini) && (!parse.rotate)) {
		llscreen = new LLScreen(640,480,0,0,parse.db,parse.hw);
		llscreen->rotate = false;
	} else {
		llscreen = new LLScreen(480,640,0,0,parse.db,parse.hw);
		llscreen->rotate = true;
	}

Sergio Costas's avatar
Sergio Costas committed
376
	// by default, try all sound modes
377 378 379 380 381 382 383 384 385 386 387 388 389
	enum e_soundtype sound_type = SOUND_AUTOMATIC;
	if (parse.nosound) {
		sound_type = SOUND_NO;
	}
	if (parse.oss) {
		sound_type = SOUND_OSS;
	}
	if (parse.pulse) {
		sound_type = SOUND_PULSEAUDIO;
	}
	if (parse.alsa) {
		sound_type = SOUND_ALSA;
	}
390

391 392
	llsound = new LLSound(sound_type);
	OOTape = new Tape();
393
	keyboard = new Keyboard();
394
	ordenador = new computer();
395
	microdrive = new Microdrive();
396
	spk_ay = new SPK_AY();
397
	mouse = new Mouse();
398

399
	load_config();
400 401 402 403 404 405

	ordenador->zaurus_mini = 0;
	if (parse.mini) {
		ordenador->zaurus_mini = 1;
	} else if (parse.rotate) {
		ordenador->zaurus_mini = 2;
Sergio Costas's avatar
Sergio Costas committed
406
	}
407 408 409

	if (parse.ds) {
		ordenador->dblscan = true;
Sergio Costas's avatar
Sergio Costas committed
410
	}
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
	if (parse.ss) {
		ordenador->dblscan = false;
	}

	if (parse.bw) {
		ordenador->bw = true;
	}
	if (parse.color) {
		ordenador->bw = false;
	}
	screen = new Screen();

	gamefile = "";

	jump_frames = parse.jump;
	curr_frames = 0;

	printf("Computer init\n");
	printf("Modo: %d\n",ordenador->mode128k);

	atexit(end_system);

	if(parse.fs) {
434
		llscreen->fullscreen_switch();
Sergio Costas's avatar
Sergio Costas committed
435
	}
436

Sergio Costas's avatar
Sergio Costas committed
437
	SDL_WM_SetCaption("FBZX","");
438
	ordenador->interr=0;
Sergio Costas's avatar
Sergio Costas committed
439 440 441 442 443

	// assign initial values for PATH variables

	strcpy(path_snaps,getenv("HOME"));
	length=strlen(path_snaps);
444
	if ((length>0)&&(path_snaps[length-1]!='/')) {
Sergio Costas's avatar
Sergio Costas committed
445
		strcat(path_snaps,"/");
446
	}
Sergio Costas's avatar
Sergio Costas committed
447 448
	strcpy(path_taps,path_snaps);
	strcpy(path_mdrs,path_snaps);
449
	ordenador->current_tap = "";
Sergio Costas's avatar
Sergio Costas committed
450 451 452

	// assign random values to the memory before start execution

453
	printf("Reset memory\n");
454
	printf("Modo: %d\n",ordenador->mode128k);
Sergio Costas's avatar
Sergio Costas committed
455
	for(bucle=0;bucle<196608;bucle++)
456
		ordenador->memoria[bucle]=(unsigned char) rand();
Sergio Costas's avatar
Sergio Costas committed
457

458
	printf("Memory resetted\n");
459 460
	printf("Modo: %d\n",ordenador->mode128k);

Sergio Costas's avatar
Sergio Costas committed
461 462
	salir=1;
  
463
	printf("Init microdrive\n");
464

Sergio Costas's avatar
Sergio Costas committed
465

466
	printf("Reset computer\n");
Sergio Costas's avatar
Sergio Costas committed
467 468 469 470
	ResetComputer();

	sleep(1);

471
	printf("Reset screen\n");
472
	llscreen->clear_screen();
Sergio Costas's avatar
Sergio Costas committed
473

474 475
	if (llsound->sound_aborted) {
		osd->set_message("Running without sound (read the FAQ)",2000);
Sergio Costas's avatar
Sergio Costas committed
476 477
	}

478
	printf("Modo: %d\n",ordenador->mode128k);
479
	printf("load main game\n");
480 481
	load_main_game(parse.gamefile.c_str());
	printf("Modo: %d\n",ordenador->mode128k);
Sergio Costas's avatar
Sergio Costas committed
482

483
	osd->set_message("Press F1 for help",4000);
Sergio Costas's avatar
Sergio Costas committed
484

485
	printf("BPP: %d\n",llscreen->bpp);
486
	debug_var = false;
Sergio Costas's avatar
Sergio Costas committed
487 488 489 490 491 492 493 494
	while(salir) {

		do {
			tstados=Z80free_ustep(&procesador);
			if(tstados<0) {
				printf("Error %X\n",procesador.PC);
				exit(1);
			}
495
			ordenador->emulate(tstados); // execute the whole hardware emulation for that number of TSTATES
Sergio Costas's avatar
Sergio Costas committed
496 497
		} while(procesador.Status!=Z80XX);
		PC=procesador.PC;
498

Sergio Costas's avatar
Sergio Costas committed
499 500 501
		/* if PC is 0x0556, a call to LD_BYTES has been made, so if
		FAST_LOAD is 1, we must load the block in memory and return */

502
		if((!microdrive->mdr_paged) && (PC==0x0556) && (ordenador->tape_fast_load) && (ordenador->page48k == 1)) {
503
			if(ordenador->current_tap != "") {
504
				//procesador.Rm.br.F &= ~F_Z;
505 506
				do_fast_load();
			} else {
Sergio Costas's avatar
Sergio Costas committed
507
				osd->set_message("No TAP/TZX file selected",2000);
Sergio Costas's avatar
Sergio Costas committed
508
			}
509
			continue;
510
		}
Sergio Costas's avatar
Sergio Costas committed
511 512 513 514
		
		/* if PC is 0x04C2, a call to SA_BYTES has been made, so if
		we want to save to the TAP file, we do it */
		
515
		if((!microdrive->mdr_paged) && ((PC==0x04C2) || (PC == 0x04C6)) && (ordenador->tape_write==1) && (ordenador->page48k == 1)) {
516

Sergio Costas's avatar
Sergio Costas committed
517 518 519 520 521
			if(ordenador->current_tap == "") {
				osd->set_message("No TAP/TZX file selected",2000);
				continue;
			}

522 523 524 525 526 527
			uint8_t *data;
			uint8_t op_xor;
			uint8_t dato;
			uint32_t length;
			int pointer;

528 529 530
			if (PC == 0x04C2) {
				do_push(0x053F); // return address to SA/LD-RET
			}
531

532 533 534 535 536 537 538 539 540 541 542 543 544 545
			length = (uint32_t)(procesador.Rm.wr.DE);
			length += 2;

			data = new uint8_t[length];
			pointer = 0;
			data[pointer++] = procesador.Rm.br.A; // flag

			op_xor = procesador.Rm.br.A;

			salir = 0;
			do {
				if (procesador.Rm.wr.DE == 0)
					salir = 2;
				if (!salir) {
546
					dato = ordenador->read_memory(procesador.Rm.wr.IX); // read data
547 548 549 550 551 552 553 554 555 556
					op_xor^=dato;
					data[pointer++] = dato;
					procesador.Rm.wr.IX++;
					procesador.Rm.wr.DE--;
				}
			} while (!salir);
			data[pointer] = op_xor;
			procesador.Rm.wr.IX++;
			procesador.Rm.wr.IX++;
			OOTape->add_block(data,length);
557 558
			ordenador->other_ret = 1;	// next instruction must be RET
			continue;
Sergio Costas's avatar
Sergio Costas committed
559
		}
Sergio Costas's avatar
Sergio Costas committed
560
		
561
		/* if ordenador->mdr_paged is 2, we have executed the RET at 0x0700, so
Sergio Costas's avatar
Sergio Costas committed
562 563
		we have to return to the classic ROM */
		
564 565 566
		if(microdrive->mdr_paged == 2) {
			microdrive->mdr_paged = 0;
		}
Sergio Costas's avatar
Sergio Costas committed
567 568 569 570
		
		/* if PC is 0x0008 or 0x1708, and we have a microdrive, we have to page
		the Interface 1 ROM */
		
571
		if(((PC==0x0008)||(PC==0x1708))&&(microdrive->mdr_active)) {
572
			microdrive->mdr_paged = 1;
573
		}
Sergio Costas's avatar
Sergio Costas committed
574 575 576 577
		
		/* if PC is 0x0700 and we have a microdrive, we have to unpage
		the Interface 1 ROM after the last instruction */
		
578
		if((PC==0x0700)&&(microdrive->mdr_active)) {
579
			microdrive->mdr_paged = 2;
580
		}
Sergio Costas's avatar
Sergio Costas committed
581

582
		if(ordenador->interr==1) {
583
			keyboard->read_keyboard (NULL);	// read the physical keyboard
584
			Z80free_INT(&procesador,ordenador->bus_empty());
585
			ordenador->interr=0;
Sergio Costas's avatar
Sergio Costas committed
586 587 588
		}
	}

589
	save_config();
Sergio Costas's avatar
Sergio Costas committed
590 591
	return 0;
}
592

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
void print_status() {

	printf("\nPC: 0x%04X   SP:0x%04X\n",procesador.PC,procesador.Rm.wr.SP);
	printf("AF: 0x%04X   BC: 0x%04X   DE: 0x%04X   HL: 0x%04X\n",procesador.Rm.wr.AF,procesador.Rm.wr.BC,procesador.Rm.wr.DE,procesador.Rm.wr.HL);
	printf("AF': 0x%04X  BC': 0x%04X  DE': 0x%04X  HL': 0x%04X\n",procesador.Ra.wr.AF,procesador.Ra.wr.BC,procesador.Ra.wr.DE,procesador.Ra.wr.HL);
	printf("IX: 0x%04X   IY: 0x%04X   IX': 0x%04X  IY': 0x%04X\n",procesador.Rm.wr.IX,procesador.Rm.wr.IY,procesador.Ra.wr.IX,procesador.Ra.wr.IY);
	printf("IFF1: %X   IFF2: %X   I: %X   R: %X\n\n\n",procesador.IFF1,procesador.IFF2,procesador.I,procesador.R|procesador.R2);

}

void do_push(uint16_t value) {

	procesador.Rm.wr.SP -= 2;
	ordenador->write_memory(procesador.Rm.wr.SP, (uint8_t)(value & 0xFF));
	value >>= 8;
	ordenador->write_memory(procesador.Rm.wr.SP + 1, (uint8_t)(value & 0xFF));
}

611 612 613
void do_fast_load() {

	if (!(procesador.Rm.br.F & F_C)) { // if Carry=0, is VERIFY, so return OK
614
		do_push(0x053F); // LD_BYTES pushes this address in the stack to return through it
615 616 617
		procesador.Rm.br.F |= F_C;	 // verify OK
		procesador.Rm.wr.IX += procesador.Rm.wr.DE;
		procesador.Rm.wr.DE = 0;
618
		ordenador->other_ret = 1;	// next instruction must be RET
619 620 621
		return;
	}

622
	uint16_t size;
623
	uint8_t flag = procesador.Rm.br.A;
624 625
	uint8_t data[65538];
	uint16_t counter;
626 627

	while(true) {
628
		enum FastLoadReturn retval = OOTape->fast_read(data,size,flag);
629 630 631 632 633 634

		switch (retval) {
		case FASTLOAD_NO_TAPE:
			procesador.Rm.br.F &= (~F_C);	// Load error
			procesador.Rm.wr.IX += procesador.Rm.wr.DE;
			procesador.Rm.wr.DE = 0;
635
			osd->set_message("No tape selected",2000);
636
			do_push(0x053F); // LD_BYTES pushes this address in the stack to return through it
637
			ordenador->other_ret = 1;	// next instruction must be RET
638 639 640
			return;
		break;
		case FASTLOAD_NO_BLOCK:
Sergio Costas's avatar
Sergio Costas committed
641 642 643
			if (OOTape->get_pause()) {
				osd->set_message("Can't do fast load. Press F6 to play",2000);
			}
644
			ordenador->other_ret = 0;	// next instruction must NOT be RET
645 646
			return;
		break;
647
		case FASTLOAD_END_TAPE:
648 649 650
			procesador.Rm.br.F &= (~F_C);	// Load error
			procesador.Rm.wr.IX += procesador.Rm.wr.DE;
			procesador.Rm.wr.DE = 0;
651
			osd->set_message("End of tape. Rewind it.",2000);
652
			do_push(0x053F); // LD_BYTES pushes this address in the stack to return through it
653
			ordenador->other_ret = 1;	// next instruction must be RET
654 655
			return;
		case FASTLOAD_OK:
656 657 658 659 660
			counter = 0;
			while (true) {
				if ((size == 0) || (procesador.Rm.wr.DE == 0)) {
					break;
				}
661
				ordenador->write_memory(procesador.Rm.wr.IX, (byte) data[counter]); // store the byte
662 663 664 665 666
				procesador.Rm.wr.IX++;
				procesador.Rm.wr.DE--;
				counter++;
				size--;
			}
667 668 669 670 671 672 673
			procesador.Rm.wr.AF = 0x0093;
			procesador.Rm.br.H = 0;
			procesador.Ra.wr.AF = 0x0145;
			procesador.Rm.wr.BC = 0xB001;
			procesador.IFF1 = 0;
			procesador.IFF2 = 0;
			ordenador->other_ret = 1;	// next instruction must be RET
674
			do_push(0x053F); // LD_BYTES pushes this address in the stack to return through it
675 676 677 678 679 680 681
			if (size == 0) {
				if (procesador.Rm.wr.DE == 0) {
					procesador.Rm.br.F |= (F_C);	// Load OK
					return;
				}
			}
			procesador.Rm.br.F &= (~F_C);	// Load error
682 683 684
			return;
		break;
		case FASTLOAD_NO_FLAG:
685 686 687
			continue;
		break;
		case FASTLOAD_NODATA:
688 689 690 691 692
			continue;
		break;
		}
	}
}