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

📄 make6502.c

📁 十七种模拟器源代码 非常有用的作课程设计不可缺少的
💻 C
📖 第 1 页 / 共 5 页
字号:
	fprintf(fp, "		mov	[_%ss], dl	; Store our new stack area\n", cpubasename);	fprintf(fp, "		xor	esi, esi	; Zero our program counter\n");	fprintf(fp, "		mov	si, [ebp+0fffeh]	; Get our break handler\n");	if (bBankswitch)	{		fprintf(fp, "		mov	[_%spc], si\n", cpubasename);		PCToPage();	}	else		fprintf(fp, "		add	esi, ebp	; Add our base pointer back in\n");	++dwGlobalLabel;	Flags6502toX86();	FetchAndExec(bTimingTable[dwOpcode]);	ProcEnd(string);}// JSRvoid JsrHandler(UINT16 dwOpcode){	fprintf(fp, ";\n");	fprintf(fp, "; JSR\n");	fprintf(fp, ";\n");	sprintf(string, "RegInst%.2x", dwOpcode);	ProcBegin(string, dwOpcode);	FetchInstructionWord();	if (bBankswitch)		PageToPC();	else	{		fprintf(fp, "		sub	esi, ebp	 ; Get our real program counter\n");	}	fprintf(fp, "		dec	si		; Our offset to return to\n");	fprintf(fp, "		sub	[_%ss], byte 2	; Back up 2 byte for stack push\n", cpubasename);	fprintf(fp, "		mov	di, word [_%ss]	; Our stack area\n", cpubasename);	fprintf(fp, "		and	edi, 0ffh	; Only the lower byte matters\n");	fprintf(fp, "		add	edi, 101h	; Make sure it's on the stack page\n");	fprintf(fp, "		mov	[edi+ebp], si	; Store our return address\n");	if (bBankswitch)	{		fprintf(fp, "		xor	esi, esi\n");		fprintf(fp, "		mov	si, dx	; Get our address\n");		fprintf(fp, "		mov	[_%spc], si	; Store our new PC\n", cpubasename);		PCToPage();	}	else	{		fprintf(fp, "		add	edx, ebp	; Our new address\n");		fprintf(fp, "		mov	esi, edx	; Put it in here for the fetch\n");	}		dwGlobalLabel++;	FetchAndExec(bTimingTable[dwOpcode]);	ProcEnd(string);}// RTSvoid RtsHandler(UINT16 dwOpcode){	fprintf(fp, ";\n");	fprintf(fp, "; RTS\n");	fprintf(fp, ";\n");	sprintf(string, "RegInst%.2x", dwOpcode);	ProcBegin(string, dwOpcode);	fprintf(fp, "		xor	esi, esi	; Zero ESI for later\n");	fprintf(fp, "		mov	dl, [_%ss]	; Get our stack\n", cpubasename);	fprintf(fp, "		add	[_%ss], byte 2	; Pop off a word\n", cpubasename);	fprintf(fp, "		inc	dl	; Increment our stack page\n");	fprintf(fp, "		inc	dh	; Our stack page\n");	fprintf(fp, "		mov	si, [ebp+edx]	; Get our stack area\n");	fprintf(fp, "		inc	si	; Increment!\n");	if (bBankswitch)	{		fprintf(fp, "		mov	[_%spc], si\n", cpubasename);		PCToPage();	}	else		fprintf(fp, "		add	esi, ebp	; Add in our base address\n");		++dwGlobalLabel;	FetchAndExec(bTimingTable[dwOpcode]);	ProcEnd(string);}// CMP Handlervoid CmpHandler(UINT16 dwOpcode){	fprintf(fp, ";\n");	fprintf(fp, "; CMP\n");	fprintf(fp, ";\n");	sprintf(string, "RegInst%.2x", dwOpcode);	ProcBegin(string, dwOpcode);	if (0xcd == dwOpcode)		Absolute();	else	if (0xc5 == dwOpcode)		ZeroPage();	else	if (0xc9 == dwOpcode)		Immediate("dl");	else	if (0xdd == dwOpcode)		AbsoluteX();	else	if (0xd9 == dwOpcode)		AbsoluteY();	else	if (0xc1 == dwOpcode)		IndirectX();	else	if (0xd1 == dwOpcode)		IndirectY();	else	if (0xd5 == dwOpcode)		ZeroPageX();	else		assert(0);	// Now that we have the address of what we want in DX, let's go fetch the	// data	if (0xc9 != dwOpcode)	// Make sure it's not immediate	{		if (((0xc5 == dwOpcode) || (0xd5 == dwOpcode)) && bZeroDirect)			fprintf(fp, "		mov	dl, [ebp+edx]	; Get our zero page data\n");		else			ReadMemoryByte("dl", "dx");	}	// We have our value in DL. Now do the work!	fprintf(fp, "		mov	dh, ah	; Get our flags\n");	fprintf(fp, "		cmp	al, dl	; Compare!\n");	fprintf(fp, "		cmc			; Compliment carry flag\n");	fprintf(fp, "		lahf\n");	fprintf(fp, "		and	ah, 0c1h	; Sign, zero, and carry\n");	fprintf(fp, "		and	dh, 03eh	; Everything but sign, zero and carry\n");	fprintf(fp, "		or		ah, dh	; OR In our new flags\n");	FetchAndExec(bTimingTable[dwOpcode]);	ProcEnd(string);}// CPY Handlervoid CpyHandler(UINT16 dwOpcode){	fprintf(fp, ";\n");	fprintf(fp, "; CPY\n");	fprintf(fp, ";\n");	sprintf(string, "RegInst%.2x", dwOpcode);	ProcBegin(string, dwOpcode);	if (0xcc == dwOpcode)		Absolute();	else	if (0xc4 == dwOpcode)		ZeroPage();	else	if (0xc0 == dwOpcode)		Immediate("dl");	else		assert(0);	// Now that we have the address of what we want in DX, let's go fetch the	// data	if (0xc0 != dwOpcode)	// Make sure it's not immediate	{		if (0xc4 == dwOpcode && bZeroDirect)			fprintf(fp, "		mov	dl, [ebp+edx]	; Get our zero page data\n");		else			ReadMemoryByte("dl", "dx");	}	// We have our value in DL. Now do the work!	fprintf(fp, "		mov	dh, ah	; Get our flags\n");	fprintf(fp, "		cmp	cl, dl	; Compare with Y!\n");	fprintf(fp, "		cmc			; Compliment carry flag\n");	fprintf(fp, "		lahf\n");	fprintf(fp, "		and	ah, 0c1h	; Sign, zero, and carry\n");	fprintf(fp, "		and	dh, 03eh	; Everything but sign, zero and carry\n");	fprintf(fp, "		or		ah, dh	; OR In our new flags\n");	FetchAndExec(bTimingTable[dwOpcode]);	ProcEnd(string);}// CPX Handlervoid CpxHandler(UINT16 dwOpcode){	fprintf(fp, ";\n");	fprintf(fp, "; CPX\n");	fprintf(fp, ";\n");	sprintf(string, "RegInst%.2x", dwOpcode);	ProcBegin(string, dwOpcode);	if (0xec == dwOpcode)		Absolute();	else	if (0xe4 == dwOpcode)		ZeroPage();	else	if (0xe0 == dwOpcode)		Immediate("dl");	else		assert(0);	// Now that we have the address of what we want in DX, let's go fetch the	// data	if (0xe0 != dwOpcode)	// Make sure it's not immediate	{		if (0xe4 == dwOpcode && bZeroDirect)			fprintf(fp, "		mov	dl, [ebp+edx]	; Get our zero page data\n");		else			ReadMemoryByte("dl", "dx");	}	// We have our value in DL. Now do the work!	fprintf(fp, "		mov	dh, ah	; Get our flags\n");	fprintf(fp, "		cmp	bl, dl	; Compare with X!\n");	fprintf(fp, "		cmc			; Compliment carry flag\n");	fprintf(fp, "		lahf\n");	fprintf(fp, "		and	ah, 0c1h	; Sign, zero, and carry\n");	fprintf(fp, "		and	dh, 03eh	; Everything but sign, zero and carry\n");	fprintf(fp, "		or		ah, dh	; OR In our new flags\n");	FetchAndExec(bTimingTable[dwOpcode]);	ProcEnd(string);}void ClcCldCliClvHandler(UINT16 dwOpcode){	fprintf(fp, ";\n");	if (0x58 == dwOpcode)		fprintf(fp, "; CLI\n");	else	if (0xd8 == dwOpcode)		fprintf(fp, "; CLD\n");	else	if (0xb8 == dwOpcode)		fprintf(fp, "; CLV\n");	else	if (0x18 == dwOpcode)		fprintf(fp, "; CLC\n");	else	if (0x38 == dwOpcode)		fprintf(fp, "; SEC\n");	else	if (0xf8 == dwOpcode)		fprintf(fp, "; SED\n");	else	if (0x78 == dwOpcode)		fprintf(fp, "; SEI\n");	else		assert(0);						  		fprintf(fp, ";\n");	sprintf(string, "RegInst%.2x", dwOpcode);	ProcBegin(string, dwOpcode);	if (0x58 == dwOpcode)	{			fprintf(fp, "		and	[_altFlags], byte 0fbh	; No interrupts!\n");		fprintf(fp, "		cmp	[_irqPending], byte 0 ; IRQ pending?\n");		fprintf(fp, "		je		near notEnabledCli\n");		if (bBankswitch)			PageToPC();		else			fprintf(fp, "		sub	esi, ebp	 ; Get our real program counter\n");		fprintf(fp, "		mov	[_%spc], si\n", cpubasename);		FlagsX86to6502();		fprintf(fp, "		mov	[_%saf], ax	; Save this\n", cpubasename);				fprintf(fp, "		push	eax		; Save this - we need it\n");		fprintf(fp, "		call	_%sint	; Go do an interrupt\n", cpubasename);		fprintf(fp, "		pop	eax		; Restore this - we need it!\n");		fprintf(fp, "		mov	si, [_%spc]	; Get our program counter\n", cpubasename);		fprintf(fp, "		and	esi, 0ffffh	; Only the lower 16 bits\n");		if (bBankswitch)		{			fprintf(fp, "		mov	[_%spc], si\n", cpubasename);			PCToPage();		}		else			fprintf(fp, "		add	esi, ebp ; So it properly points to the code\n");		fprintf(fp, "		mov	ax, [_%saf] ; Restore this\n", cpubasename);		Flags6502toX86();		fprintf(fp, "notEnabledCli:\n");	}	else	if (0xd8 == dwOpcode)		fprintf(fp, "		and	[_altFlags], byte 0f7h	; Binary mode\n");	else	if (0xb8 == dwOpcode)		fprintf(fp, "		and	ah, 0efh	; Clear out overflow\n");	else	if (0x18 == dwOpcode)		fprintf(fp, "		and	ah, 0feh	; No carry\n");	else	if (0x38 == dwOpcode)		fprintf(fp, "		or	ah, 01h	; Carry!\n");	else	if (0xf8 == dwOpcode)		fprintf(fp, "		or		[_altFlags], byte 08h	; Decimal mode\n");	else	if (0x78 == dwOpcode)		fprintf(fp, "		or		[_altFlags], byte 04h	; Interrupts!\n");	else		assert(0);	FetchAndExec(bTimingTable[dwOpcode]);	ProcEnd(string);}void AdcHandler(UINT16 dwOpcode){	fprintf(fp, ";\n");	fprintf(fp, "; ADC\n");	fprintf(fp, ";\n");	sprintf(string, "RegInst%.2x", dwOpcode);	ProcBegin(string, dwOpcode);	if (0x6d == dwOpcode)		Absolute();	else	if (0x65 == dwOpcode)		ZeroPage();	else	if (0x69 == dwOpcode)		Immediate("dl");	else	if (0x7d == dwOpcode)		AbsoluteX();	else	if (0x79 == dwOpcode)		AbsoluteY();	else	if (0x61 == dwOpcode)		IndirectX();	else	if (0x71 == dwOpcode)		IndirectY();	else	if (0x75 == dwOpcode)		ZeroPageX();	else		assert(0);	// We've got our address (or byte if immediate) in DL now	if (0x69 != dwOpcode)	// Make sure it's not immediate	{		if (((0x65 == dwOpcode) || (0x75 == dwOpcode)) && bZeroDirect)			fprintf(fp, "		mov	dl, [ebp+edx]	; Get our zero page data\n");		else			ReadMemoryByte("dl", "dx");	}	if (FALSE == bNoDecimal)	{		fprintf(fp, "		test	[_altFlags], byte 08h ; Are we in decimal mode?\n");		fprintf(fp, "		je		binaryMode%ld	; It's binary mode\n", dwAnotherLabel);		fprintf(fp, "		jmp	decimalMode%ld		; Yup - go handle dec mode\n", dwAnotherLabel);		fprintf(fp, "binaryMode%ld:\n", dwAnotherLabel); 	}	// Now let's ADC, man!	fprintf(fp, "		sahf		; Restore our flags for the adc\n");	fprintf(fp, "		adc	al, dl	; Add in our value\n");	fprintf(fp, "		o16 pushf	; Push our flags (and overflow)\n");	fprintf(fp, "		and	ah, 02eh	; No carry, overflow, zero or sign\n");	fprintf(fp, "		pop	dx	; Restore our flags into DX\n");	fprintf(fp, "		shl	dh, 1	; Shift overflow into position\n");	fprintf(fp, "		and	dh, 10h	; Only the overflow\n");	fprintf(fp, "		and	dl, 0c1h	; Only carry, sign, and zero\n");	fprintf(fp, "		or		ah, dl	; OR In our new flags\n");	fprintf(fp, "		or		ah, dh	; OR In overflow\n");	FetchAndExec(bTimingTable[dwOpcode]);	if (FALSE == bNoDecimal)	{		fprintf(fp, "decimalMode%ld:\n", dwAnotherLabel);// On the way in:// AH=x86 Flags (0x01 = Carry)// AL=Accumulator value// DL=Value to add to		fprintf(fp, "		xor	di, di	; Zero DI for later\n");		fprintf(fp, "		sahf\n");		fprintf(fp, "		adc	di, 0		; Increment if carry is set\n");		fprintf(fp, "		and	ah, 03ch	; Knock out carry, zero, sign, and overflow\n");		fprintf(fp, "		mov	[_%sx], bl	; Save X\n", cpubasename);		fprintf(fp, "		mov	[_%sy], cl	; Save Y\n", cpubasename);			fprintf(fp, "		mov	bl, al	; Get Accumulator\n");		fprintf(fp, "		mov	bh, dl	; Original value\n");		fprintf(fp, "		mov	cx, bx	; Put it here for later\n");		fprintf(fp, "		and	bx, 0f0fh	; Only the nibbles\n");		fprintf(fp, "		add	bl, bh	; Add it to the lower value\n");		fprintf(fp, "		add	bx, di	; Add in our carry\n");			fprintf(fp, "		shr	cx, 4		; Upper nibbles only\n");		fprintf(fp, "		and	cx, 0f0fh	; Only the lower nibbles now\n");		fprintf(fp, "		add	cl, ch	; Add in the original value\n");		fprintf(fp, "		mov	bh, cl	; Put our upper nibble in BH\n");		fprintf(fp, "		xor	cx, cx	; Zero the upper part\n");			fprintf(fp, "		cmp	bl, 9		; Digit overflow?\n");		fprintf(fp, "		jbe	notOverflowed%ld\n", dwAnotherLabel);		fprintf(fp, "		inc	bh			; Increment - we've overflowed\n");		fprintf(fp, "		add	bl, 6		; Fix the lower nibble\n");		fprintf(fp, "notOverflowed%ld:\n", dwAnotherLabel);			fprintf(fp, "		mov	cl, al	; Get the accumulator\n");		fprintf(fp, "		xor	cl, dl	; XOR It with the original value\n");		fprintf(fp, "		not	cl			; Invert & add 1\n");			fprintf(fp, "		mov	ch, bh	; Get our high BCD\n");		fprintf(fp, "		shl	ch, 4		; Move into position\n");		fprintf(fp, "		and	ch, cl	; And 'em together\n");		fprintf(fp, "		or		ch, ch	; See if we've overflowed\n");		fprintf(fp, "		jns	noOv%ld\n", dwAnotherLabel);		fprintf(fp, "		or		ah, 040h	; Set overflow\n");		fprintf(fp, "noOv%ld:\n", dwAnotherLabel);			fprintf(fp, "		cmp	bh, 9		; Greater than 9?\n");		fprintf(fp, "		jbe	noOvTwo%ld\n", dwAnotherLabel);		fprintf(fp, "		add	bh, 6		; Digit fixup\n");		fprintf(fp, "noOvTwo%ld:\n", dwAnotherLabel);			fprintf(fp, "		mov	al, bh	; Get most significant nibble\n");		fprintf(fp, "		shl	al, 4		; Put it into position\n");		fprintf(fp, "		and	bl, 0fh	; Only the lower nibble matters now\n");		fprintf(fp, "		or		al, bl	; Put it in the accumulator\n");			fprintf(fp, "		test	bh, 0f0h	; Carry?\n");		fprintf(fp, "		jz		noCarry%ld\n", dwAnotherLabel);		fprintf(fp, "		or		ah, 01h	; Set carry\n");		fprintf(fp, "noCarry%ld:\n", dwAnotherLabel);			SetZeroSign("al");				fprintf(fp, "		xor	bx, bx	; Zero\n");		fprintf(fp, "		xor	cx, cx\n");		fprintf(fp, "		mov	bl, [_%sx]	; X!\n", cpubasename);		fprintf(fp, "		mov	cl, [_%sy]	; Y!\n", cpubasename);			FetchAndExec(bTimingTable[dwOpcode]);	}	dwAnotherLabel++;	ProcEnd(string);}void SbcHandler(UINT16 dwOpcode){	fprintf(fp, ";\n");	fprintf(fp, "; SBC\n");	fprintf(fp, ";\n");	sprintf(string, "RegInst%.2x", dwOpcode);	ProcBegin(string, dwOpcode);	if (0xed == dwOpcode)		Absolute();	else	if (0xe5 == dwOpcode)		ZeroPage();	else	if (0xe9 == dwOpcode)		Immediate("dl");	else	if (0xfd == dwOpcode)		AbsoluteX();	else	if (0xf9 == dwOpcode)		AbsoluteY();	el

⌨️ 快捷键说明

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