⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pgbvideo.java

📁 一个用java写成的gb模拟器的源代码。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		// docs are not very clear on this subject
		if(!old_lcd && lcd_on) {
			ly = 0;
			stat_mode = STAT_OAM;
			cycles = OAM_CYCLES;
		}
		if(!lcd_on) {
			ly = 0;
			stat_mode = STAT_HBLANK;
			cycles = HBLANK_CYCLES;
		}
	}
	public byte getLcdc() {
		byte lcdc = 0;
		lcdc |= lcd_on		? 0x80 : 0x00;
		lcdc |= win_mode	? 0x40 : 0x00;
		lcdc |= win_on		? 0x20 : 0x00;
		lcdc |= chr_mode	? 0x10 : 0x00;
		lcdc |= bg_mode		? 0x08 : 0x00;
		lcdc |= obj_mode	? 0x04 : 0x00;
		lcdc |= obj_on		? 0x02 : 0x00;
		lcdc |= bg_on		? 0x01 : 0x00;
		return lcdc;
	}
	
	public void setStat(byte sval) {
		int_lyc		= (sval & 0x40) == 0x40;
		int_oam		= (sval & 0x20) == 0x20;
		int_vblank	= (sval & 0x10) == 0x10;
		int_hblank	= (sval & 0x08) == 0x08;
	}
	public byte getStat() {
		byte stat = 0;
		stat |= int_lyc		? 0x40 : 0x00;
		stat |= int_oam		? 0x20 : 0x00;
		stat |= int_hblank	? 0x10 : 0x00;
		stat |= int_vblank	? 0x08 : 0x00;
		stat |= (ly == lyc)	? 0x04 : 0x00;
		stat |= stat_mode;
		return stat;
	}
	
	public void gbcSetVram(byte setting) {
		gbcVram = setting;
		vramOffset = ((gbcVram & 0x01) != 0) ? 0x6000 : 0x8000;
	}
	public byte gbcGetVram() {
		return gbcVram;
	}
	
	public void gbcSetBgpi(byte index) {
		//System.out.println("write to Color BG Palette Index:" + Integer.toHexString(index & 0xFF));
		bgpi = index;
	}
	public void gbcSetBgpd(byte data) {
		//System.out.println("write to Color BG Palette Data:" + Integer.toHexString(data & 0xFF));
		gbcPalette[bgpi & 0x3F] = data;
		if((bgpi & 0x80) == 0x80) {
			bgpi++;
		}
	}
	public void gbcSetObpi(byte index) {
		//System.out.println("write to Color OBJ Palette Index:" + Integer.toHexString(index & 0xFF));
		obpi = index;
	}
	public void gbcSetObpd(byte data) {
		//System.out.println("write to Color OBJ Palette Data:" + Integer.toHexString(data & 0xFF));
		gbcPalette[(obpi & 0x3F) + 0x40] = data;
		if((obpi & 0x80) == 0x80) {
			obpi++;
		}
	}
	public byte gbcGetBgpi() {
		//System.out.println("read Color BG Palette Index:" + Integer.toHexString(bgpi));
		return bgpi;
	}
	public byte gbcGetBgpd() {
		//System.out.println("read Color BG Palette Data:" + Integer.toHexString(gbcPalette[bgpi & 0x3F]));
		return gbcPalette[bgpi & 0x3F];
	}
	public byte gbcGetObpi() {
		//System.out.println("read Color OBJ Palette Index:" + Integer.toHexString(obpi));
		return obpi;
	}
	public byte gbcGetObpd() {
		//System.out.println("read Color OBJ Palette Data:" + Integer.toHexString(gbcPalette[(obpi & 0x3F) + 0x40]));
		return gbcPalette[(obpi & 0x3F) + 0x40];
	}
	
	public void sgbSetPalette(int pal, int color, byte hi, byte low) {
		sgbPalette[pal * 8 + color * 2 + 0] = low;
		sgbPalette[pal * 8 + color * 2 + 1] = hi;
	}
	public void sgbSetPaletteIndirect(int pal0, int pal1, int pal2, int pal3, int atf) {
		System.arraycopy(sgbSystemPalette, pal0 * 8, sgbPalette, 0, 8);
		System.arraycopy(sgbSystemPalette, pal1 * 8, sgbPalette, 8, 8);
		System.arraycopy(sgbSystemPalette, pal2 * 8, sgbPalette, 16, 8);
		System.arraycopy(sgbSystemPalette, pal3 * 8, sgbPalette, 24, 8);
		if(atf != 0) {
			sgbSetOverlayFromAtf(atf);
		}
	}
	public void sgbSetPaletteOverlay(int index, int palette) {
		int i = index / 4, o = (3 - (index & 3)) * 2;
		sgbPaletteOverlay[i] &= ~(byte)(0x03 << o);
		sgbPaletteOverlay[i] |= (byte)((palette & 0x03) << o);
	}

	public void sgbSetPaletteOverlayByte(int index, byte palette) {
		sgbPaletteOverlay[index] = palette;
	}

	public void sgbPictureTransfer() {
		System.arraycopy(vram, 0x800, sgbPicture, 0, 0x1000);
	}
	public void sgbCharsetTransfer(boolean type, boolean range) {
		System.arraycopy(vram, 0x0800, sgbCharset, range ? 0x1000 : 0, 0x1000);
	}
	public void sgbPaletteTransfer() {
		System.arraycopy(vram, 0x800, sgbSystemPalette, 0, 0x1000);
	}
	public void sgbAtfTransfer() {
		System.arraycopy(vram, 0x800, sgbAtfData, 0, 0x1800);
		sgbSetOverlayFromAtf(1);
	}
	
	public void sgbSetOverlayFromAtf(int atf) {
		System.arraycopy(sgbAtfData, (atf & 0x3F) * 90, sgbPaletteOverlay, 0, 90);
	}
	
	public void sgbBlockDesignate(byte control, byte palettes, byte startx, byte starty, byte endx, byte endy) {
		int x, y;
		byte in, on, out;
		boolean useIn, useOn, useOut;
		in		= (byte)((palettes & 0x03) >> 0);
		on		= (byte)((palettes & 0x0C) >> 2);
		out		= (byte)((palettes & 0x30) >> 4);
		useIn	= (control & 0x01) == 0x01;
		useOn	= (control & 0x02) == 0x02;
		useOut	= (control & 0x04) == 0x04;
		if(PgbSettings.DEBUG) {
			System.out.println(" - sgb block[in:" + in + (useIn ? "+" : "-") + ", on:" + on + (useOn ? "+" : "-") + ", out:" + out + (useOut ? "+" : "-") + ", from(" + startx + ", " + starty + ") to(" + endx + ", " + endy + ")]");
		}
		// the ugliest code ever:
		for(y = 0; y < 18; y++) {
			for(x = 0; x < 20; x++) {
				if(y < starty || y > endy) {
					// y is outside the block
					if(useOut) {
						sgbSetPaletteOverlay(y * 20 + x, out);
					}
				}
				if(y == starty || y == endy) {
					// y is on the line
					if(x < startx || x > endx) {
						// but x is off the line
						if(useOut) {
							sgbSetPaletteOverlay(y * 20 + x, out);
						}
					} else {
						// and x is on the line
						if(useOn) {
							sgbSetPaletteOverlay(y * 20 + x, on);
						}
					}
				}
				if(y > starty && y < endy) {
					// y is inside the block
					if(x < startx || x > endx) {
						// but x is outside the block
						if(useOut) {
							sgbSetPaletteOverlay(y * 20 + x, out);
						}
					} else {
						if(x == startx || x == endx) {
							// and x is on the line
							if(useOn) {
								sgbSetPaletteOverlay(y * 20 + x, on);
							}
						} else {
							// and x is inside the block
							if(useIn) {
								sgbSetPaletteOverlay(y * 20 + x, in);
							}
						}
					}
				}
			}
		}
	}
	public void sgbLineDesignate(byte control) {
		int line, i;
		byte pal;
		boolean mode;
		line = (control & 0x1F);
		pal = (byte)((control & 0x60) >> 5);
		mode = (control & 0x80) == 0x80;
		if(mode) {
			// vertical line
			for(i = 0; i < 18; i++) {
				sgbSetPaletteOverlay(i * 20 + line, pal);
			}
		} else {
			// horizontal line
			for(i = 0; i < 20; i++) {
				sgbSetPaletteOverlay(line * 20 + i, pal);
			}
		}
	}

	public void sgbDivideDesignate(byte control, byte line) {
		int x, y;
		byte on, before, after;
		boolean mode;
		on = (byte)((control & 0x03) >> 0);
		before = (byte)((control & 0x0C) >> 2);
		after = (byte)((control & 0x30) >> 4);
		mode = (control & 0x40) == 0x40;
		if(mode) {
			// divide vertical
			for(y = 0; y < 18; y++) {
				for(x = 0; x < 20; x++) {
					if(x < line) {
						sgbSetPaletteOverlay(y * 20 + x, before);
					}
					if(x == line) {
						sgbSetPaletteOverlay(y * 20 + x, on);
					}
					if(x > line) {
						sgbSetPaletteOverlay(y * 20 + x, after);
					}
				}
			}
		} else {
			// divide horizontal
			for(y = 0; y < 18; y++) {
				for(x = 0; x < 20; x++) {
					if(y < line) {
						sgbSetPaletteOverlay(y * 20 + x, before);
					}
					if(y == line) {
						sgbSetPaletteOverlay(y * 20 + x, on);
					}
					if(y > line) {
						sgbSetPaletteOverlay(y * 20 + x, after);
					}
				}
			}
		}
	}
	
	public void dumpGbcPalette() {
		OutputStream os;
		File dumpfile;
		
		dumpfile = new File("gbcPalette");
		
		try {
			os = new FileOutputStream(dumpfile);
			os.write(gbcPalette);
			
			os.close();
		} catch(Exception e) {
			System.out.println("error!");
			System.out.println(e.getMessage());
		}
	}
	public void dumpSgbPaletteOverlay() {
		OutputStream os;
		File dumpfile;
		
		dumpfile = new File("sgbPaletteOverlay");
		
		try {
			os = new FileOutputStream(dumpfile);
			os.write(sgbPaletteOverlay);
			
			os.close();
		} catch(Exception e) {
			System.out.println("error!");
			System.out.println(e.getMessage());
		}
	}
	public void dumpVram() {
		OutputStream os;
		File dumpfile;
		dumpfile = new File("vram");
		
		try {
			os = new FileOutputStream(dumpfile);
			os.write(vram);
			
			os.close();
		} catch(Exception e) {
			System.out.println("error!");
			System.out.println(e.getMessage());
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -