screen.cpp 12.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * screen.cpp
 *
 *  Created on: 26/03/2015
 *      Author: raster
 */

#include "llscreen.hh"
#include "screen.hh"
#include "emulator.hh"
#include "computer.hh"
12
#include "keyboard.hh"
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

class Screen *screen;

/* Registers the screen surface where the Spectrum will put the picture,
prepares the palette and creates two arrays (translate and translate2)
that gives the memory address for each scan */

Screen::Screen() {

	//int resx,resy;
	int bucle, bucle2, bucle3, bucle4, bucle5;

	// we prepare the scanline transform arrays

	bucle5 = 0;
	for (bucle = 0; bucle < 3; bucle++)
		for (bucle2 = 0; bucle2 < 8; bucle2++)
			for (bucle3 = 0; bucle3 < 8; bucle3++)
				for (bucle4 = 0; bucle4 < 32; bucle4++) {
					this->translate[bucle5] = 147456 + bucle * 2048 + bucle2 * 32 + bucle3 * 256 + bucle4;
					this->translate2[bucle5] = 153600 + bucle * 256 + bucle2 * 32 + bucle4;
					bucle5++;
				}
	this->tstados_counter = 0;

	this->border = 0;
	this->currline = 0;
	this->currpix = 0;
	this->flash = 0;
42 43
	this->screen_snow = false;
	this->bus_value = 255;
44 45
	int height;
	int width;
46 47 48

	switch (ordenador->zaurus_mini) {
	case 0:
49
		this->init_line = 80;
50 51 52
		this->next_line = 640;
		this->next_scanline = 640;
		this->first_line = 40;
53 54
		this->last_line = 278;
		this->first_column = 140;
55 56
		this->next_pixel = 1;
		this->jump_pixel = 16;
57 58
		width = 640;
		height = 480;
59 60
		break;
	case 1:
61
		this->init_line = 280;
62 63 64
		this->next_line = 160;
		this->next_scanline = 160;
		this->first_line = 40;
65
		this->last_line = 348;
66
		this->first_column = 140;
67 68
		this->next_pixel = 1;
		this->jump_pixel = 8;
69 70
		width = 480;
		height = 240;
71 72
		break;
	case 2:
73
		this->init_line = 480*640;
74 75 76
		this->next_line = -(307202);
		this->next_scanline = -1;
		this->first_line = 40;
77
		this->last_line = 278;
78
		this->first_column = 24;
79 80
		this->next_pixel = 480;
		this->jump_pixel = 7680;
81 82
		width = 480;
		height = 640;
83 84 85 86 87 88
		break;
	case 3:
		this->init_line = 0;
		this->next_line = 0;
		this->next_scanline = 0;
		this->first_line = 40;
89
		this->last_line = 278;
90
		this->first_column = 24;
91 92
		this->next_pixel = 1;
		this->jump_pixel = 4;
93 94
		width = 480;
		height = 240;
95 96 97
		break;
	}

98
	this->last_column = 160 - this->first_column;
99 100 101 102
	this->p_translt = this->translate;
	this->p_translt2 = this->translate2;

	this->contador_flash = 0;
103 104
	this->pixancho = 224;
	this->pixalto = 312;	// values for 48K mode
105 106 107 108 109 110 111 112

	this->ulaplus = false;
	this->ulaplus_reg = 0;
	this->next_line *= llscreen->bpp;
	this->next_scanline *= llscreen->bpp;
	this->init_line *= llscreen->bpp;
	this->next_pixel *= llscreen->bpp;
	this->jump_pixel *= llscreen->bpp;
113
	this->int_counter = 0;
114 115 116 117

	llscreen->set_paletes(ordenador->bw);

	this->base_pixel = ((unsigned char *) (llscreen->llscreen->pixels));
118
	this->max_pixel = this->base_pixel + ((height - 1) * width * llscreen->bpp);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
	this->pixel = this->base_pixel + this->init_line;
}

void Screen::set_memory_pointers () {

	static unsigned int rom, ram;

	// assign the offset for video page

	if (ordenador->mport1 & 0x08)
		ordenador->video_offset = 32768;	// page 7
	else
		ordenador->video_offset = 0;	// page 5

	// assign ROMs and, if in special mode, RAM for the whole blocks

135
	if ((ordenador->current_mode == MODE_P3)) {
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
		if (ordenador->mport2 & 0x01) {		// +2A/+3 special mode
			ordenador->page48k = 0; // no 48K ROM paged in
			ram = (unsigned int) (ordenador->mport1 & 0x06);	// bits 1&2
			switch (ram) {
			case 0:
				ordenador->block0 = ordenador->memoria + 65536;
				ordenador->block1 = ordenador->memoria + 65536;
				ordenador->block2 = ordenador->memoria + 65536;
				ordenador->block3 = ordenador->memoria + 65536;
				break;
			case 2:
				ordenador->block0 = ordenador->memoria + 131072;
				ordenador->block1 = ordenador->memoria + 131072;
				ordenador->block2 = ordenador->memoria + 131072;
				ordenador->block3 = ordenador->memoria + 131072;
				break;
			case 4:
				ordenador->block0 = ordenador->memoria + 131072;
				ordenador->block1 = ordenador->memoria + 131072;
				ordenador->block2 = ordenador->memoria + 131072;
				ordenador->block3 = ordenador->memoria + 65536;
				break;
			case 6:
				ordenador->block0 = ordenador->memoria + 131072;
				ordenador->block1 = ordenador->memoria + 163840;
				ordenador->block2 = ordenador->memoria + 131072;
				ordenador->block3 = ordenador->memoria + 65536;
				break;
			default:
				printf("FullRAM desconocido\n");
			}
			return;
		} else {		// ROMs for +2A/+3 normal mode
			rom = 0;
			if (ordenador->mport1 & 0x10)
				rom++;
			if (ordenador->mport2 & 0x04)
				rom += 2;
			// assign the first block pointer to the right block bank
			ordenador->block0 = ordenador->memoria + (16384 * rom);
Sergio Costas's avatar
Sergio Costas committed
176
			ordenador->page48k = (rom == 3) ? 1 : 0; // 48K ROM is in ROM page 3
177
		}
178
	} else if (ordenador->current_mode == MODE_48K	) {
179 180
		ordenador->block0 = ordenador->memoria;
		ordenador->page48k = 1;
Sergio Costas's avatar
Sergio Costas committed
181
	} else {			// ROMs for 128K/128Kspa/+2 mode
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
		if (ordenador->mport1 & 0x10) {
			ordenador->block0 = ordenador->memoria + 16384;
			ordenador->page48k = 1;
		} else {
			ordenador->block0 = ordenador->memoria;
			ordenador->page48k = 0;
		}
	}

	// RAMs for 128K/+2 mode, and +2A/+3 in normal mode

	ordenador->block1 = ordenador->memoria + 131072;	// page 5 minus 16384
	ordenador->block2 = ordenador->memoria + 65536;	// page 2 minus 32768

	ram = 1 + ((unsigned int) (ordenador->mport1 & 0x07));	// RAM page for block3 plus 1
	ordenador->block3 = ordenador->memoria + (16384 * ram);	// page n minus 49152
}

200 201
uint8_t Screen::get_bus_value(int tstates) {

202
	return this->bus_value;
203 204 205

}

206 207 208 209 210
/* Paints the spectrum screen during the TSTADOS tstates that the Z80 used
to execute last instruction */

void Screen::show_screen (int tstados) {

211
	static uint8_t temporal, ink, paper, fflash, tmp2;
212
	static int loop;
213 214 215 216 217 218 219

	if((procesador.I>=0x40)&&(procesador.I<=0x7F)) {
		this->screen_snow = true;
	} else {
		this->screen_snow = false;
	}

220

221
	this->tstados_counter2 += tstados;
222 223 224 225
	ordenador->cicles_counter += tstados;


	fflash = 0; // flash flag
226 227 228 229 230 231 232
	for(loop = 0; loop < tstados; loop++) {
		if (this->int_counter > 0) {
			this->int_counter --;
			if (this->int_counter == 0) {
				Z80free_INTserved(&procesador);
			}
		}
233 234 235 236
		this->bus_value = 0xFF;
		ordenador->memcontended_zone = 0; // no contention here
		if ((this->tstados_counter % 4) == this->offset) {
			if ((this->tstados_counter < this->tstates_bordertop) || (this->tstados_counter >= this->tstates_borderbottom) || ((this->tstados_counter % this->pixancho) >= 128)) {
237
				// is border
238 239 240 241 242
				if (this->ulaplus) {
					this->paint_pixels(255, this->border+24, 0);	// paint 8 pixels with BORDER color
				} else {
					this->paint_pixels(255, this->border, 0);	// paint 8 pixels with BORDER color
				}
243
			} else {
244

245 246 247 248
				// is user area. We search for ink and paper colours
				this->paint_pixels(this->user_pixels,this->user_ink,this->user_paper);
			}
		}
249

250
		if ((this->tstados_counter >= this->tstate_contention) && (this->tstados_counter < this->tstate_contention2)) {
251
			int p;
252
			if (((this->tstados_counter - this->tstate_contention) % this->pixancho) < 128) {
Sergio Costas's avatar
Sergio Costas committed
253 254 255 256 257
				int zone = ((this->tstados_counter - this->tstate_contention)% this->pixancho) % 8;
				switch(zone) {
					case 0:
					case 5:
						ordenador->memcontended_zone = 6 - zone;
258
						break;
Sergio Costas's avatar
Sergio Costas committed
259 260
					case 1:
					case 3:
Sergio Costas's avatar
Sergio Costas committed
261
						ordenador->memcontended_zone = 6 - zone;
262
						p = *this->p_translt;
263 264 265 266 267
						if ((!this->screen_snow) || (rand() % 8)) {
							this->bus_value = ordenador->memoria[p + ordenador->video_offset];
						} else {
							this->bus_value = ordenador->memoria[p + ordenador->video_offset - 1];
						}
268 269 270
						this->p_translt++;
						this->user_pixels = this->bus_value;
						break;
Sergio Costas's avatar
Sergio Costas committed
271 272
					case 2:
					case 4:
Sergio Costas's avatar
Sergio Costas committed
273
						ordenador->memcontended_zone = 6 - zone;
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
						p = *this->p_translt2;
						this->bus_value = ordenador->memoria[p + ordenador->video_offset];	// attributes
						this->p_translt2++;
						ink = this->bus_value & 0x07;	// ink colour
						paper = (this->bus_value >> 3) & 0x07;	// paper colour
						if (this->ulaplus) {
							tmp2=0x10+((this->bus_value>>2)&0x30);
							ink+=tmp2;
							paper+=8+tmp2;
						} else {
							if (this->bus_value & 0x40) {	// bright flag?
								ink += 8;
								paper += 8;
							}
							fflash = this->bus_value & 0x80;	// flash flag
							if ((fflash) && (this->flash)) {
								//paint_pixels (temporal, paper, ink);	// if FLASH, invert PAPER and INK
								this->user_ink = paper;
								this->user_paper = ink;
							} else {
								//paint_pixels (temporal, ink, paper);
								this->user_ink = ink;
								this->user_paper = paper;
							}
						}
						break;
300
				}
301 302 303
				if (this->offset_p3 != 0) {
					ordenador->memcontended_zone = (15 - (zone + this->offset_p3)) % 8;
				}
304
			}
305
		}
306

307 308 309 310
		this->currpix = this->tstados_counter % this->pixancho;
		this->currline = this->tstados_counter / this->pixancho;
		if ((this->currline > this->first_line)&&(this->currpix == this->first_column)) {
			this->pixel += this->next_line;
311 312
		}

Sergio Costas's avatar
Sergio Costas committed
313
		if (this->tstados_counter == this->tstates_screen) {
314
			this->currline = 0;
315 316 317 318 319 320
			if (osd->get_time() != 0) {
				uint8_t lines;
				string text;
				text = osd->get_text(lines);
				llscreen->print_string(text, -1,-lines, 12, 0);
			} else {
321 322
				keyboard->tab_extended = false;
				keyboard->esc_again = false;
323 324 325 326 327
			}

			llscreen->do_flip();

			ordenador->interr = 1;
328 329
			this->int_counter = 32;
			Z80free_INT(&procesador,ordenador->bus_empty());
330
			ordenador->cicles_counter = 0;
331
			this->pixel = this->base_pixel + this->init_line;
332 333 334
			this->p_translt = this->translate;
			this->p_translt2 = this->translate2;
			this->contador_flash++;
Sergio Costas's avatar
Sergio Costas committed
335
			if (this->contador_flash >= 16) {
336 337 338
				this->flash = 1 - this->flash;
				this->contador_flash = 0;
			}
Sergio Costas's avatar
Sergio Costas committed
339
			this->tstados_counter = 0;
340 341
		} else {
			this->tstados_counter++;
342 343 344 345 346 347 348 349
		}
	}
}


/* PAINT_PIXELS paints one byte with INK color for 1 bits and PAPER color
for 0 bits, and increment acordingly the pointer PIXEL */

350
void Screen::paint_pixels (uint8_t octet, uint8_t ink, uint8_t paper) {
351 352 353 354 355

	static int bucle,valor;
	static unsigned int *p;
	static unsigned char mask;

356
	if (((this->currpix >= this->first_column) && (this->currpix < (this->pixancho-this->last_column))) || (this->currline < this->first_line) || (this->pixel>=this->max_pixel)) {
357
		return;
358
	}
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388

	mask = 0x80;
	for (bucle = 0; bucle < 8; bucle++) {
		valor = (octet & mask) ? (int) ink : (int) paper;
		llscreen->paint_one_pixel(valor,this->pixel);
		if ((ordenador->zaurus_mini!=1)&&(ordenador->zaurus_mini!=3)) {
			if (ordenador->dblscan) {
				llscreen->paint_one_pixel(valor,this->pixel+this->next_scanline);
			} else {
				llscreen->paint_one_pixel(0,this->pixel+this->next_scanline);
			}
		}
		this->pixel+=this->next_pixel;
		if ((ordenador->zaurus_mini!=1)&&(ordenador->zaurus_mini!=3)) {
			llscreen->paint_one_pixel(valor,this->pixel);
			if (ordenador->dblscan) {
				llscreen->paint_one_pixel(valor,this->pixel+this->next_scanline);
			} else {
				llscreen->paint_one_pixel(0,this->pixel+this->next_scanline);
			}
			this->pixel+=this->next_pixel;
		}
		mask = ((mask >> 1) & 0x7F);
	}
}

void Screen::reset(uint8_t model) {

	this->ulaplus = false;
	switch (model) {
389 390 391 392
	case MODE_48K: // 48K
		this->pixancho = 224;
		this->pixalto = 312;
		this->pixborde_top = 64;
393 394 395 396
		this->tstate_contention = 14335;
		this->offset = 2;
		this->offset_p3 = 0;
		break;
397 398 399
	case MODE_128K:
	case MODE_P2:
	case MODE_128K_SPA:
400 401 402
		this->pixborde_top = 63;
		this->pixancho = 228;
		this->pixalto = 311;
403 404 405 406 407 408 409 410 411 412 413 414
		this->tstate_contention = 14361;
		this->offset = 0;
		this->offset_p3 = 0;
		break;
	case MODE_P3:
		this->pixborde_top = 63;
		this->pixancho = 228;
		this->pixalto = 311;
		this->tstate_contention = 14359;
		this->offset = 2;
		this->offset_p3 = 6;
		break;
415
	}
Sergio Costas's avatar
Sergio Costas committed
416 417 418 419 420 421 422 423
	printf("Reset\n");
	this->tstados_counter = 0;
	ordenador->cicles_counter = 0;
	this->pixel = this->base_pixel + this->init_line;
	this->p_translt = this->translate;
	this->p_translt2 = this->translate2;
	this->currpix = 0;
	this->currline = 0;
424

425 426 427
	this->tstate_contention2 = this->tstate_contention + 192 * this->pixancho;
	this->tstates_bordertop = this->pixancho * this->pixborde_top;
	this->tstates_borderbottom = this->tstates_bordertop + 192 * this->pixancho;
Sergio Costas's avatar
Sergio Costas committed
428
	this->tstates_screen = this->pixancho * this->pixalto - 1;
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
}

void Screen::set_ulaplus_register(uint8_t reg) {

	this->ulaplus_reg = reg;
}

void Screen::set_ulaplus_value(uint8_t Value) {

	if ((this->ulaplus_reg & 0xC0)==0x40) { // mode
		this->ulaplus = (Value & 0x01) == 1 ? true : false;
		return;
	}
	if (this->ulaplus_reg < 0x40) { // register set mode
		llscreen->set_palete_entry(this->ulaplus_reg,Value,ordenador->bw);
	}
}

uint8_t Screen::read_ulaplus_value() {

	if ((this->ulaplus_reg & 0xC0)==0x40) { // mode
		return (this->ulaplus ? 1 : 0);
	}
	if (this->ulaplus_reg < 0x40) { // register set mode
		return (llscreen->get_palete_entry(this->ulaplus_reg));
	}
	return 0;
}