2018-02-14 18:03:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "6502_emulate.h"
|
|
|
|
|
|
|
|
/* 128kB of RAM */
|
|
|
|
#define RAMSIZE 128*1024
|
|
|
|
unsigned char ram[RAMSIZE];
|
|
|
|
|
|
|
|
/* Registers */
|
|
|
|
unsigned char a,y,x;
|
2018-02-14 18:10:01 +00:00
|
|
|
unsigned short sp;
|
|
|
|
|
|
|
|
/* Flags */
|
|
|
|
unsigned int n,z,c,v;
|
|
|
|
|
|
|
|
int init_6502(void) {
|
|
|
|
|
|
|
|
a=0;
|
|
|
|
y=0;
|
|
|
|
x=0;
|
|
|
|
|
|
|
|
sp=0x1ff;
|
|
|
|
|
|
|
|
n=0; z=0; c=0; v=0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
2018-02-14 18:03:15 +00:00
|
|
|
|
|
|
|
unsigned short y_indirect(unsigned char base, unsigned char y) {
|
|
|
|
|
|
|
|
unsigned short addr;
|
|
|
|
|
|
|
|
addr=(((short)(ram[base+1]))<<8) | (short)ram[base];
|
|
|
|
|
|
|
|
//if (debug) printf("Address=%x\n",addr+y);
|
|
|
|
|
|
|
|
return addr+y;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-14 18:10:01 +00:00
|
|
|
void adc(int value) {
|
|
|
|
|
|
|
|
int temp_a;
|
|
|
|
int temp_value;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
temp_a=a&0xff;
|
|
|
|
temp_value=value&0xff;
|
|
|
|
|
|
|
|
result=(temp_a+temp_value+c);
|
|
|
|
|
|
|
|
c=(result&0x100)>>8;
|
|
|
|
n=(result&0x80)>>7;
|
|
|
|
|
|
|
|
v=!!((a^result)&(value^result)&0x80);
|
|
|
|
|
|
|
|
a=result&0xff;
|
|
|
|
z=(a==0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void sbc(int value) {
|
|
|
|
int temp_a;
|
|
|
|
int result;
|
|
|
|
int temp_value;
|
|
|
|
|
|
|
|
temp_a=a&0xff;
|
2018-02-14 19:09:54 +00:00
|
|
|
temp_value=(~value)&0xff;
|
|
|
|
|
|
|
|
result=temp_a+temp_value+c;
|
2018-02-14 18:10:01 +00:00
|
|
|
|
2018-02-14 19:09:54 +00:00
|
|
|
// printf("SBC: %x - %x (%x) = %x\n",a,value,c,result);
|
2018-02-14 18:10:01 +00:00
|
|
|
|
|
|
|
c=(result&0x100)>>8;
|
|
|
|
n=(result&0x80)>>7;
|
|
|
|
|
|
|
|
v=!!((a^result)&((255-value)^result)&0x80);
|
|
|
|
|
|
|
|
a=result&0xff;
|
|
|
|
z=(a==0);
|
2018-02-14 19:09:54 +00:00
|
|
|
|
|
|
|
|
2018-02-14 18:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void cmp(int value) {
|
|
|
|
|
|
|
|
int temp_a;
|
|
|
|
int temp_value;
|
2018-02-14 20:30:57 +00:00
|
|
|
int result;
|
2018-02-14 18:10:01 +00:00
|
|
|
|
|
|
|
temp_a=a&0xff;
|
2018-02-14 20:30:57 +00:00
|
|
|
temp_value=(~value)&0xff;
|
|
|
|
|
|
|
|
result=temp_a+temp_value+1;
|
|
|
|
|
|
|
|
c=(result&0x100)>>8;
|
|
|
|
|
|
|
|
result&=0xff;
|
|
|
|
|
|
|
|
n=(result&0x80)>>7;
|
|
|
|
z=(result==0);
|
2018-02-14 18:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pha(void) {
|
|
|
|
|
|
|
|
sp--;
|
|
|
|
ram[sp]=a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void pla(void) {
|
|
|
|
|
|
|
|
a=ram[sp];
|
|
|
|
sp++;
|
|
|
|
}
|
2018-02-14 20:30:57 +00:00
|
|
|
|
|
|
|
void lsr(void) {
|
|
|
|
int temp_a;
|
|
|
|
|
|
|
|
temp_a=a;
|
|
|
|
temp_a&=0xff;
|
|
|
|
temp_a=temp_a>>1;
|
|
|
|
|
|
|
|
a=(temp_a&0xff);
|
|
|
|
// printf("LSR A=%x\n",a);
|
|
|
|
}
|
2018-02-23 05:16:43 +00:00
|
|
|
|
|
|
|
unsigned char high(int value) {
|
|
|
|
return (value>>8)&0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char low(int value) {
|
|
|
|
return (value&0xff);
|
|
|
|
}
|
|
|
|
|