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

📄 pgbmemory.java

📁 一个用java写成的gb模拟器的源代码。
💻 JAVA
📖 第 1 页 / 共 3 页
字号:

		// empty GBC registers?
		if (address < 0xFF55) {
			System.out.println(
				"read GBC register:" + Integer.toHexString(address));
			return 0;
		}

		// empty GBC registers?
		if (address < 0xFF70) {
			System.out.println(
				"read GBC register:" + Integer.toHexString(address));
			return 0;
		}
		// GBC RAM bank

		// empty GBC registers?
		if (address < 0xFF7F) {
			System.out.println(
				"read GBC register:" + Integer.toHexString(address));
			return 0;
		}

		// internal (high) RAM
		if (address < 0xFFFF) {
			return hiRAM[address - 0xFF80];
		}

		System.out.println(
			"Read from unmapped memory:" + Integer.toHexString(address));
		//PgbSettings.paused = true;
		return 0;
	}

	/**
	 * Write bytes into the gameboy memory.
	 * 
	 * @param address the gameboy memory address to write to.
	 * @param towrite the byte to write into memory.
	 */
	public final void write(int address, byte towrite) {
		switch (address) {

		// Joypad Register (R/W) [P1]
		case 0xFF00 :
			//System.out.println("write to Joypad Register:" + Integer.toHexString(towrite & 0xFF));
			if (towrite == 0x30
				&& PgbSettings.system == PgbSettings.SYS_SGB) {
				sgbCommandBit(joy.selected);
			}
			joy.write(towrite);
			return;
			// Serial transfer data (R/W) [SB]
		case 0xFF01 :
			//System.out.println("write to Serial transfer data:" + Integer.toHexString(towrite & 0xFF));
			net.setSerialData(towrite);
			return;
			// SIO control  (R/W) [SC]
		case 0xFF02 :
			//System.out.println("write to SIO control:" + Integer.toHexString(towrite & 0xFF));
			net.setSerialControl(towrite);
			recalcCyclesLeft();
			return;
			// Divider Register (R/W) [DIV]
		case 0xFF04 :
			//System.out.println("write to Divider Register:" + Integer.toHexString(towrite & 0xFF));
			div = 0;
			return;
			// Timer counter (R/W) [TIMA]
		case 0xFF05 :
			//System.out.println("write to Timer counter:" + Integer.toHexString(towrite & 0xFF));
			tima = towrite;
			return;
			// Timer Modulo (R/W) [TMA]
		case 0xFF06 :
			//System.out.println("write to Timer Modulo:" + Integer.toHexString(towrite & 0xFF));
			tma = towrite;
			return;
			// Timer Control [TAC]
		case 0xFF07 :
			//System.out.println("write to Timer Control:" + Integer.toHexString(towrite & 0xFF));
			setTac(towrite);
			recalcCyclesLeft();
			return;
			// sound by retroK modified and fixed by XTale ;)
		case 0xFF10 :
			if (soundOn)
				soundChip.channel1.setSweep(
					(unsign(towrite) & 0x70) >> 4,
					unsign(towrite) & 7,
					(unsign(towrite) & 8) == 1);
			soundIO[16] = towrite;
			return;
		case 0xFF11 :
			if (soundOn) {
				soundChip.channel1.setDutyCycle(
					(unsign(towrite) & 0xc0) >> 6);
				//use length if Bit6 in 20 is set
				if ((soundIO[20] & 0x40) == 1)
					soundChip.channel1.setLength(
						unsign(towrite) & 0x3f);
			}
			soundIO[17] = towrite;
			return;
		case 0xFF12 :
			if (soundOn)
				soundChip.channel1.setEnvelope(
					(unsign(towrite) & 0xf0) >> 4,
					unsign(towrite) & 7,
					(unsign(towrite) & 8) == 8);
			soundIO[18] = towrite;
			return;
		case 0xFF13 :
			soundIO[19] = towrite;
			if (soundOn)
				soundChip.channel1.setFrequency(
					((unsign(soundIO[20]) & 7) << 8)
						+ unsign(soundIO[19]));
			return;
		case 0xFF14 :
			soundIO[20] = towrite;
			if (soundOn) {
				if ((soundIO[20] & 0x80) != 0) {
					soundChip.channel1.setLength(
						unsign(soundIO[17]) & 0x3f);
					soundChip.channel1.setEnvelope(
						(unsign(soundIO[18]) & 0xf0) >> 4,
						unsign(soundIO[18]) & 7,
						(unsign(soundIO[18]) & 8) == 8);
					//update statusregister
					soundIO[38] |= 0x01;
				}
				if ((soundIO[20] & 0x40) == 0) { //XXX ==1?
					soundChip.channel1.setLength(-1);
					soundIO[38] &= ~ 0x01;
				}
				soundChip.channel1.setFrequency(
					((unsign(soundIO[20]) & 7) << 8)
						+ unsign(soundIO[19]));
			}

			return;
		case 0xFF15 :
			soundIO[21] = towrite;
			return;
		case 0xFF16 :
			if (soundOn) {
				soundChip.channel2.setDutyCycle(
					(unsign(towrite) & 0xc0) >> 6);
				soundChip.channel2.setLength(unsign(towrite) & 0x3f);
			}
			soundIO[22] = towrite;
			return;
		case 0xFF17 :
			if (soundOn)
				soundChip.channel2.setEnvelope(
					(unsign(towrite) & 0xf0) >> 4,
					unsign(towrite) & 7,
					(unsign(towrite) & 8) == 8);
			soundIO[23] = towrite;
			return;
		case 0xFF18 :
			soundIO[24] = towrite;
			if (soundOn)
				soundChip.channel2.setFrequency(
					((unsign(soundIO[25]) & 7) << 8)
						+ unsign(soundIO[24]));
			return;
		case 0xFF19 :
			soundIO[25] = towrite;
			if (soundOn) {
				if ((soundIO[25] & 0x80) != 0) {
					soundChip.channel2.setLength(
						unsign(soundIO[22]) & 0x3f);
					soundChip.channel2.setEnvelope(
						(unsign(soundIO[23]) & 0xf0) >> 4,
						unsign(soundIO[23]) & 7,
						(unsign(soundIO[23]) & 8) == 8);
					//update statusregister
					soundIO[38] |= 0x02;
				}
				if ((soundIO[25] & 0x40) == 0) { ///XXX ==1? 
					soundChip.channel2.setLength(-1);
					soundIO[39] &= ~0x02;
					}
				soundChip.channel2.setFrequency(
					((unsign(soundIO[25]) & 7) << 8)
						+ unsign(soundIO[24]));
			}
				
			return;
		case 0xFF1A :
			if (soundOn)
				if ((unsign(towrite) & 0x80) != 0)
					soundChip.channel3.setVolume(
						(unsign(soundIO[28]) & 0x60) >> 5);
				else
					soundChip.channel3.setVolume(0);
			soundIO[26] = towrite;
			return;
		case 0xFF1B :
			soundIO[27] = towrite;
			if (soundOn)
				//only use if Bit6 in 30 is set
				if ((soundIO[30] & 0x40) == 1)
					soundChip.channel3.setLength(unsign(towrite));
			return;
		case 0xFF1C :
			soundIO[28] = towrite;
			if (soundOn)
				soundChip.channel3.setVolume(
					(unsign(soundIO[28]) & 0x60) >> 5);
			return;
		case 0xFF1D :
			soundIO[29] = towrite;
			if (soundOn)
				soundChip.channel3.setFrequency(
					((unsign(soundIO[30]) & 7) << 8)
						+ unsign(soundIO[29]));
			return;
		case 0xFF1E :
			soundIO[30] = towrite;
			if (soundOn) {
				if ((soundIO[25] & 0x80) != 0) {
					soundChip.channel3.setLength(unsign(soundIO[27]));
					//update status
					soundIO[38] |= 0x04;
				}
				soundChip.channel3.setFrequency(
					((unsign(soundIO[30]) & 7) << 8)
						+ unsign(soundIO[29]));
				// if bit 6 = stop???
				if ((soundIO[30] & 0x40) == 0) { //XXX ==1?
					soundChip.channel3.setLength(-1);
					soundIO[38] &= ~0x04;
				}
			}

			return;
		case 0xFF20 :
			if (soundOn)
				soundChip.channel4.setLength(unsign(towrite) & 0x3f);
			soundIO[32] = towrite;
			return;
		case 0xFF21 :
			if (soundOn)
				soundChip.channel4.setEnvelope(
					(unsign(towrite) & 0xf0) >> 4,
					unsign(towrite) & 7,
					(unsign(towrite) & 8) == 8);
			soundIO[33] = towrite;
			return;
		case 0xFF22 :
			soundIO[34] = towrite;
			//Channel4 plynomial counter
			return;
		case 0xFF23 :
			soundIO[35] = towrite;
			if (soundOn) {
				if ((soundIO[35] & 0x80) != 0) {
					soundChip.channel4.setLength(
						unsign(soundIO[32]) & 0x3f);
					//update status
					soundIO[38] |= 0x08;
				}
				if ((soundIO[35] & 0x40) == 0) { //XXX ==1?
					soundChip.channel4.setLength(-1);
					soundIO[38] &= ~0x08;
				}
			}
			
			return;
		case 0xFF24 :
			//Channel Controll - useless in emu ,)
			//System.out.println("call with" + Integer.toBinaryString((towrite & 0xff)));
			soundIO[36] = towrite;
			return;
		case 0xFF25 :
			//System.out.println(Integer.toBinaryString(unsign(towrite)));
			soundIO[37] = towrite;
			if (soundOn) {
				//Channel 1 = Bit 0 (left) or Bit 4 (right)
				int j = 0;
				if ((unsign(towrite) & 1) != 0)
					j |= 1;
				if ((unsign(towrite) & 0x10) != 0)
					j |= 2;
				soundChip.channel1.setChannel(j);
				//				Channel 2 = Bit 1 (left) or Bit 5 (right)
				j = 0;
				if ((unsign(towrite) & 2) != 0)
					j |= 1;
				if ((unsign(towrite) & 0x20) != 0)
					j |= 2;
				soundChip.channel2.setChannel(j);
				//				Channel 3 = Bit 2 (left) or Bit 6 (right)
				j = 0;
				if ((unsign(towrite) & 4) != 0)
					j |= 1;
				if ((unsign(towrite) & 0x40) != 0)
					j |= 2;
				soundChip.channel3.setChannel(j);
				//				Channel 4 = Bit 3 (left) or Bit 7 (right)
				j = 0;
				if ((unsign(towrite) & 8) != 0)
					j |= 1;
				if ((unsign(towrite) & 0x80) != 0)
					j |= 2;
				soundChip.channel4.setChannel(j);
			}
			return;
		case 0xFF26 :
			//bit 7 - sound off
			if ((towrite & 0x80) == 0) {
				soundChip.channel1.setVolume3(0);
				soundChip.channel2.setVolume3(0);
				soundChip.channel3.setVolume(0);
				soundChip.channel4.setLength(-1);
			}
			soundIO[38] = towrite;
			return;
			
			// Interrupt Flag (R/W)
		case 0xFF0F :
			//System.out.println("write to Interrupt Flag: " + Integer.toHexString(towrite & 0xFF));
			IF = towrite;
			return;
			// LCD Control (R/W)
		case 0xFF40 :
			//System.out.println("write to LCD Control:" + Integer.toHexString(towrite & 0xFF));
			video.setLcdc(towrite);
			recalcCyclesLeft();
			return;
			// LCDC Status   (R/W)
		case 0xFF41 :
			//System.out.println("write to LCDC Status:" + Integer.toHexString(towrite & 0xFF));
			video.setStat(towrite);
			recalcCyclesLeft();
			return;
			// Scroll Y   (R/W)
		case 0xFF42 :
			video.scy = towrite & 0xFF;
			//System.out.println("write to scrolly:" + (towrite & 0xFF));
			return;
			// Scroll X   (R/W)
		case 0xFF43 :
			video.scx = towrite & 0xFF;
			//System.out.println("write to scrollx:" + (towrite & 0xFF) + " sxo:" + ((towrite & 0xFF) % 8));
			return;
			// LCDC Y-Coordinate (R)
		case 0xFF44 :
			//System.out.println("write to LCDC Y-Coordinate:" + (towrite & 0xFF));
			video.ly = 0;
			recalcCyclesLeft();
			return;
			// LY Compare  (R/W)
		case 0xFF45 :
			//System.out.println("write to LY Compare:" + (towrite & 0xFF));
			video.lyc = towrite & 0xFF;
			return;
			// OAM-DMA Transfer and Start Address (W)
		case 0xFF46 :
			oamDMA(towrite);
			return;
			// BG Palette Data  (W)
		case 0xFF47 :
			//System.out.println("write to BG Palette Data:" + (towrite & 0xFF));
			video.setBgPal(towrite & 0xFF);
			return;
			// Object Palette 0 Data (W)
		case 0xFF48 :
			//System.out.println("write to Object Palette 0 Data:" + (towrite & 0xFF));
			video.setObjPal0(towrite & 0xFF);
			return;
			// Object Palette 1 Data (W)
		case 0xFF49 :
			//System.out.println("write to Object Palette 1 Data:" + (towrite & 0xFF));
			video.setObjPal1(towrite & 0xFF);
			return;
			// Window Y Position  (R/W)
		case 0xFF4A :
			video.wy = towrite & 0xFF;
			return;
			// Window X Position  (R/W)
		case 0xFF4B :
			//System.out.println("write to Window X Position:" + (towrite & 0xFF));
			video.wx = towrite & 0xFF;
			return;
			// GBC CPU speed [KEY1]
		case 0xFF4D :
			//System.out.println("write to GBC CPU speed:" + (towrite & 0xFF));
			gbcSetSpeed(towrite);
			return;
			// GBC VRAM bank [VBK]
		case 0xFF4F :
			//System.out.println("write to GBC VRAM bank:" + (towrite & 0xFF));
			video.gbcSetVram(towrite);
			return;
			// GBC rHDMA1 (bit 7-0 of Source MSB) [HDMA1]
		case 0xFF51 :
			//System.out.println("write to GBC rHDMA1:" + (towrite & 0xFF));
			rHDMA1 = towrite;
			return;
			// GBC rHDMA2 (bit 7-4 of Source LSB) [HDMA2]
		case 0xFF52 :
			//System.out.println("write to GBC rHDMA2:" + (towrite & 0xFF));
			rHDMA2 = towrite;
			return;
			// GBC rHDMA3 (bit 4-0 of Destination MSB) [HDMA3]
		case 0xFF53 :
			//System.out.println("write to GBC rHDMA3:" + (towrite & 0xFF));
			rHDMA3 = towrite;
			return;
			// GBC rHDMA4 (bit 7-4 of Destination LSB) [HDMA4]
		case 0xFF54 :
			//System.out.println("write to GBC rHDMA4:" + (towrite & 0xFF));
			rHDMA4 = towrite;
			return;
			// GBC rHDMA5 (DMA Mode / Control) [HDMA5]
		case 0xFF55 :
			//System.out.println("write to GBC rHDMA5 (DMA Mode / Control):" + (towrite & 0xFF));
			setHDMAControl(towrite);
			return;
			// GBC IR port (R/W) [RP]
		case 0xFF56 :
			net.setIR(towrite);
			return;
			// Color BG Palette Index (W) [BCPS]
		case 0xFF68 :
			// only set this on GBC
			if (PgbSettings.system == PgbSettings.SYS_GBC) {
				video.gbcSetBgpi(towrite);
			}
			return;

⌨️ 快捷键说明

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