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

📄 printinstrtostring.cpp

📁 nes游戏模拟器
💻 CPP
📖 第 1 页 / 共 4 页
字号:
//------------------------------------------------------------------------------
// Name: PrintInstrToString.cpp
// Desc: Holds the functions involved in printing an instruction
//       from the 6502 into a string.
//------------------------------------------------------------------------------


// Includes
#include <windows.h>
#include <stdio.h>

#include "PrintInstrToString.h"


//------------------------------------------------------------------------------
// Name: PrintAddrMode_Absolute()
// Desc: Prints the the instruction with the absolute addressing mode
//       and returns the number of bytes this addressing mode uses.
//------------------------------------------------------------------------------
BYTE PrintAddrMode_Absolute(LPSTR strDest, LPSTR strInstrName, WORD wPC)
{
	// Print the memory byte.
	sprintf(strDest, " %02X %02X", GetMemoryByte(wPC+1), GetMemoryByte(wPC+2));

	// Print the instruction name to the instruction string.
	memcpy(&strDest[7], strInstrName, 3);

	// Print the operand for the instruction.
	sprintf(&strDest[10], " $%02X%02X", GetMemoryByte(wPC+2), GetMemoryByte(wPC+1));

	// Return the number of bytes for this addressing mode.
	return 2;
} // end PrintAddrMode_Absolute()


//------------------------------------------------------------------------------
// Name: PrintAddrMode_AbsoluteX()
// Desc: Prints the the instruction with the absolute X addressing mode
//       and returns the number of bytes this addressing mode uses.
//------------------------------------------------------------------------------
BYTE PrintAddrMode_AbsoluteX(LPSTR strDest, LPSTR strInstrName, WORD wPC)
{
	// Print the memory byte.
	sprintf(strDest, " %02X %02X", GetMemoryByte(wPC+1), GetMemoryByte(wPC+2));

	// Print the instruction name to the instruction string.
	memcpy(&strDest[7], strInstrName, 3);

	// Print the operand for the instruction.
	sprintf(&strDest[10], " $%02X%02X, X", GetMemoryByte(wPC+2), GetMemoryByte(wPC+1));

	// Return the number of bytes for this addressing mode.
	return 2;
} // end PrintAddrMode_AbsoluteX()


//------------------------------------------------------------------------------
// Name: PrintAddrMode_AbsoluteY()
// Desc: Prints the the instruction with the absolute Y addressing mode
//       and returns the number of bytes this addressing mode uses.
//------------------------------------------------------------------------------
BYTE PrintAddrMode_AbsoluteY(LPSTR strDest, LPSTR strInstrName, WORD wPC)
{
	// Print the memory byte.
	sprintf(strDest, " %02X %02X", GetMemoryByte(wPC+1), GetMemoryByte(wPC+2));

	// Print the instruction name to the instruction string.
	memcpy(&strDest[7], strInstrName, 3);

	// Print the operand for the instruction.
	sprintf(&strDest[10], " $%02X%02X, Y", GetMemoryByte(wPC+2), GetMemoryByte(wPC+1));

	// Return the number of bytes for this addressing mode.
	return 2;
} // end PrintAddrMode_AbsoluteY()


//------------------------------------------------------------------------------
// Name: PrintAddrMode_Accumulator()
// Desc: Prints the the instruction with the accumulator addressing mode
//       and returns the number of bytes this addressing mode uses.
//------------------------------------------------------------------------------
BYTE PrintAddrMode_Accumulator(LPSTR strDest, LPSTR strInstrName, WORD wPC)
{
	// Print the memory byte.
	sprintf(strDest, " %02X", GetMemoryByte(wPC+1));

	// Print the instruction name to the instruction string.
	memcpy(&strDest[7], strInstrName, 3);

	// Print the operand for the instruction.
	sprintf(&strDest[10], " A");

	// Return the number of bytes for this addressing mode.
	return 0;
} // end PrintAddrMode_Accumulator()


//------------------------------------------------------------------------------
// Name: PrintAddrMode_Immediate()
// Desc: Prints the the instruction with the immediate addressing mode
//       and returns the number of bytes this addressing mode uses.
//------------------------------------------------------------------------------
BYTE PrintAddrMode_Immediate(LPSTR strDest, LPSTR strInstrName, WORD wPC)
{
	// Print the memory byte.
	sprintf(strDest, " %02X", GetMemoryByte(wPC+1));

	// Print the instruction name to the instruction string.
	memcpy(&strDest[7], strInstrName, 3);

	// Print the operand for the instruction.
	sprintf(&strDest[10], " #$%02X", GetMemoryByte(wPC+1));

	// Return the number of bytes for this addressing mode.
	return 1;
} // end PrintAddrMode_Immediate()


//------------------------------------------------------------------------------
// Name: PrintAddrMode_Implied()
// Desc: Prints the the instruction with the implied addressing mode
//       and returns the number of bytes this addressing mode uses.
//------------------------------------------------------------------------------
BYTE PrintAddrMode_Implied(LPSTR strDest, LPSTR strInstrName, WORD wPC)
{
	// Print the memory byte.
	sprintf(strDest, "   ");

	// Print the instruction name to the instruction string.
	memcpy(&strDest[7], strInstrName, 3);

	// Print the operand for the instruction.
	sprintf(&strDest[10], " ");

	// Return the number of bytes for this addressing mode.
	return 0;
} // end PrintAddrMode_Implied()


//------------------------------------------------------------------------------
// Name: PrintAddrMode_PostIndexedIndirect()
// Desc: Prints the the instruction with the post indexed indirect addressing mode
//       and returns the number of bytes this addressing mode uses.
//------------------------------------------------------------------------------
BYTE PrintAddrMode_PostIndexedIndirect(LPSTR strDest, LPSTR strInstrName, WORD wPC)
{
	// Print the memory byte.
	sprintf(strDest, " %02X", GetMemoryByte(wPC+1));

	// Print the instruction name to the instruction string.
	memcpy(&strDest[7], strInstrName, 3);

	// Print the operand for the instruction.
	sprintf(&strDest[10], " ($%02X), Y", GetMemoryByte(wPC+1));

	// Return the number of bytes for this addressing mode.
	return 1;
} // end PrintAddrMode_PostIndexedIndirect()


//------------------------------------------------------------------------------
// Name: PrintAddrMode_PreIndexedIndirect()
// Desc: Prints the the instruction with the pre indexed indirect addressing mode
//       and returns the number of bytes this addressing mode uses.
//------------------------------------------------------------------------------
BYTE PrintAddrMode_PreIndexedIndirect(LPSTR strDest, LPSTR strInstrName, WORD wPC)
{
	// Print the memory byte.
	sprintf(strDest, " %02X", GetMemoryByte(wPC+1));

	// Print the instruction name to the instruction string.
	memcpy(&strDest[7], strInstrName, 3);

	// Print the operand for the instruction.
	sprintf(&strDest[10], " ($%02X, X)", GetMemoryByte(wPC+1));

	// Return the number of bytes for this addressing mode.
	return 1;
} // end PrintAddrMode_PreIndexedIndirect()


//------------------------------------------------------------------------------
// Name: PrintAddrMode_Indirect()
// Desc: Prints the the instruction with the indirect addressing mode
//       and returns the number of bytes this addressing mode uses.
//------------------------------------------------------------------------------
BYTE PrintAddrMode_Indirect(LPSTR strDest, LPSTR strInstrName, WORD wPC)
{
	// Print the memory byte.
	sprintf(strDest, " %02X %02X", GetMemoryByte(wPC+1), GetMemoryByte(wPC+2));

	// Print the instruction name to the instruction string.
	memcpy(&strDest[7], strInstrName, 3);

	// Print the operand for the instruction.
	sprintf(&strDest[10], " ($%02X%02X)", GetMemoryByte(wPC+2), GetMemoryByte(wPC+1));

	// Return the number of bytes for this addressing mode.
	return 2;
} // end PrintAddrMode_Indirect()


//------------------------------------------------------------------------------
// Name: PrintAddrMode_Relative()
// Desc: Prints the the instruction with the relative addressing mode
//       and returns the number of bytes this addressing mode uses.
//------------------------------------------------------------------------------
BYTE PrintAddrMode_Relative(LPSTR strDest, LPSTR strInstrName, WORD wPC)
{
	// Print the memory byte.
	sprintf(strDest, " %02X", GetMemoryByte(wPC+1));

	// Print the instruction name to the instruction string.
	memcpy(&strDest[7], strInstrName, 3);

	// Print the operand for the instruction.
	sprintf(&strDest[10], " $%02X", GetMemoryByte(wPC+1));

	// Return the number of bytes for this addressing mode.
	return 1;
} // end PrintAddrMode_Relative()


//------------------------------------------------------------------------------
// Name: PrintAddrMode_ZeroPage()
// Desc: Prints the the instruction with the zero page addressing mode
//       and returns the number of bytes this addressing mode uses.
//------------------------------------------------------------------------------
BYTE PrintAddrMode_ZeroPage(LPSTR strDest, LPSTR strInstrName, WORD wPC)
{
	// Print the memory byte.
	sprintf(strDest, " %02X", GetMemoryByte(wPC+1));

	// Print the instruction name to the instruction string.
	memcpy(&strDest[7], strInstrName, 3);

	// Print the operand for the instruction.
	sprintf(&strDest[10], " $%02X", GetMemoryByte(wPC+1));

	// Return the number of bytes for this addressing mode.
	return 1;
} // end PrintAddrMode_ZeroPage()


//------------------------------------------------------------------------------
// Name: PrintAddrMode_ZeroPageX()
// Desc: Prints the the instruction with the zero page X addressing mode
//       and returns the number of bytes this addressing mode uses.
//------------------------------------------------------------------------------
BYTE PrintAddrMode_ZeroPageX(LPSTR strDest, LPSTR strInstrName, WORD wPC)
{
	// Print the memory byte.
	sprintf(strDest, " %02X", GetMemoryByte(wPC+1));

	// Print the instruction name to the instruction string.
	memcpy(&strDest[7], strInstrName, 3);

	// Print the operand for the instruction.
	sprintf(&strDest[10], " $%02X, X", GetMemoryByte(wPC+1));

	// Return the number of bytes for this addressing mode.
	return 1;
} // end PrintAddrMode_ZeroPageX()


//------------------------------------------------------------------------------
// Name: PrintAddrMode_ZeroPageY()
// Desc: Prints the the instruction with the zero page Y addressing mode
//       and returns the number of bytes this addressing mode uses.
//------------------------------------------------------------------------------
BYTE PrintAddrMode_ZeroPageY(LPSTR strDest, LPSTR strInstrName, WORD wPC)
{
	// Print the memory byte.
	sprintf(strDest, " %02X", GetMemoryByte(wPC+1));

	// Print the instruction name to the instruction string.
	memcpy(&strDest[7], strInstrName, 3);

	// Print the operand for the instruction.
	sprintf(&strDest[10], " $%02X, Y", GetMemoryByte(wPC+1));

	// Return the number of bytes for this addressing mode.
	return 1;
} // end PrintAddrMode_ZeroPageY()


//------------------------------------------------------------------------------
// Name: PrintADC()
// Desc: Prints the ADC instruction to a string and returns the number
//       of bytes for the entire instruction.
//------------------------------------------------------------------------------
BYTE PrintADC(LPSTR strDest, WORD wPC, BYTE byOpcode)
{
	LPSTR strInstrName = "ADC"; // Name of the instruction being printed.

	// Depending on the opcode of the instruction, different addressing
	// modes apply. This means that different strings must be printed
	// depending on the addressing mode.
	switch (byOpcode)
	{
		case 0x61:
			return (1 + PrintAddrMode_PreIndexedIndirect(strDest, strInstrName, wPC));
		case 0x65:
			return (1 + PrintAddrMode_ZeroPage(strDest, strInstrName, wPC));
		case 0x69:
			return (1 + PrintAddrMode_Immediate(strDest, strInstrName, wPC));
		case 0x6D:
			return (1 + PrintAddrMode_Absolute(strDest, strInstrName, wPC));
		case 0x71:
			return (1 + PrintAddrMode_PostIndexedIndirect(strDest, strInstrName, wPC));
		case 0x75:
			return (1 + PrintAddrMode_ZeroPageX(strDest, strInstrName, wPC));
		case 0x79:
			return (1 + PrintAddrMode_AbsoluteY(strDest, strInstrName, wPC));
		case 0x7D:
			return (1 + PrintAddrMode_AbsoluteX(strDest, strInstrName, wPC));
		default:
			return 0;
	}
} // end PrintADC()


//------------------------------------------------------------------------------
// Name: PrintAND()
// Desc: Prints the AND instruction to a string and returns the number
//       of bytes for the entire instruction.
//------------------------------------------------------------------------------
BYTE PrintAND(LPSTR strDest, WORD wPC, BYTE byOpcode)
{
	LPSTR strInstrName = "AND"; // Name of the instruction being printed.

	// Depending on the opcode of the instruction, different addressing
	// modes apply. This means that different strings must be printed
	// depending on the addressing mode.
	switch (byOpcode)
	{
		case 0x21:
			return (1 + PrintAddrMode_PreIndexedIndirect(strDest, strInstrName, wPC));
		case 0x25:
			return (1 + PrintAddrMode_ZeroPage(strDest, strInstrName, wPC));
		case 0x29:
			return (1 + PrintAddrMode_Immediate(strDest, strInstrName, wPC));
		case 0x2D:
			return (1 + PrintAddrMode_Absolute(strDest, strInstrName, wPC));
		case 0x31:
			return (1 + PrintAddrMode_PostIndexedIndirect(strDest, strInstrName, wPC));
		case 0x35:
			return (1 + PrintAddrMode_ZeroPageX(strDest, strInstrName, wPC));
		case 0x39:
			return (1 + PrintAddrMode_AbsoluteY(strDest, strInstrName, wPC));
		case 0x3D:
			return (1 + PrintAddrMode_AbsoluteX(strDest, strInstrName, wPC));
		default:
			return 0;
	}
} // end PrintAND()


//------------------------------------------------------------------------------
// Name: PrintASL()
// Desc: Prints the ASL instruction to a string and returns the number
//       of bytes for the entire instruction.
//------------------------------------------------------------------------------
BYTE PrintASL(LPSTR strDest, WORD wPC, BYTE byOpcode)
{
	LPSTR strInstrName = "ASL"; // Name of the instruction being printed.

	// Depending on the opcode of the instruction, different addressing
	// modes apply. This means that different strings must be printed
	// depending on the addressing mode.
	switch (byOpcode)
	{
		case 0x06:
			return (1 + PrintAddrMode_ZeroPage(strDest, strInstrName, wPC));
		case 0x0A:
			return (1 + PrintAddrMode_Accumulator(strDest, strInstrName, wPC));
		case 0x0E:
			return (1 + PrintAddrMode_Absolute(strDest, strInstrName, wPC));
		case 0x16:
			return (1 + PrintAddrMode_ZeroPageX(strDest, strInstrName, wPC));
		case 0x1E:
			return (1 + PrintAddrMode_AbsoluteX(strDest, strInstrName, wPC));
		default:
			return 0;
	}
} // end PrintASL()


//------------------------------------------------------------------------------
// Name: PrintBCC()
// Desc: Prints the BCC instruction to a string and returns the number
//       of bytes for the entire instruction.
//------------------------------------------------------------------------------
BYTE PrintBCC(LPSTR strDest, WORD wPC, BYTE byOpcode)
{
	LPSTR strInstrName = "BCC"; // Name of the instruction being printed.

	return (1 + PrintAddrMode_Relative(strDest, strInstrName, wPC));
} // end PrintBCC()
  

//------------------------------------------------------------------------------
// Name: PrintBCS()
// Desc: Prints the BCS instruction to a string and returns the number
//       of bytes for the entire instruction.
//------------------------------------------------------------------------------
BYTE PrintBCS(LPSTR strDest, WORD wPC, BYTE byOpcode)
{
	LPSTR strInstrName = "BCS"; // Name of the instruction being printed.

	return (1 + PrintAddrMode_Relative(strDest, strInstrName, wPC));
} // end PrintBCS()


//------------------------------------------------------------------------------
// Name: PrintBEQ()
// Desc: Prints the BEQ instruction to a string and returns the number
//       of bytes for the entire instruction.
//------------------------------------------------------------------------------
BYTE PrintBEQ(LPSTR strDest, WORD wPC, BYTE byOpcode)
{
	LPSTR strInstrName = "BEQ"; // Name of the instruction being printed.

	return (1 + PrintAddrMode_Relative(strDest, strInstrName, wPC));
} // end PrintBEQ()

⌨️ 快捷键说明

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