Add loading programs from roms

This commit is contained in:
g1n 2021-10-04 19:06:10 +03:00
parent 55fb75f6ad
commit 6b167f6cdc
3 changed files with 36 additions and 31 deletions

View File

@ -7,15 +7,24 @@ typedef unsigned short word; // 16 bit
#define INS_LDA_IM 0xA9 // LDA Immediate
#define INS_LDA_ZP 0xA5 // LDA Zero Page
#define INS_LDA_ZPX 0xB5 // LDA Zero Page.X
#define INS_LDA_ZPX 0xB5 // LDA Zero Page,X
#define INS_LDA_ABS 0xAD // LDA Absolute
#define INS_LDA_ABSX 0xBD // LDA Absolute,X
#define INS_LDA_ABSY 0xB9 // LDA Absolute,Y
#define INS_LDA_INX 0xA1 // LDA Indirect,X
#define INS_LDA_INY 0xB1 // LDA Indirect,Y
#define INS_LDX_IM 0xA2 // LDX Immediate
#define INS_LDX_ZP 0xA6 // LDX Zero Page
#define INS_LDX_ZPY 0xB6 // LDX Zero Page.Y
#define INS_LDX_ZPY 0xB6 // LDX Zero Page,Y
#define INS_LDX_ABS 0xAE // LDX Absolute
#define INS_LDX_ABSY 0xBE // LDX Absolute,Y
#define INS_LDY_IM 0xA0 // LDY Immediate
#define INS_LDY_ZP 0xA4 // LDY Zero Page
#define INS_LDY_ZPX 0xB4 // LDY Zero Page.X
#define INS_LDY_ZPX 0xB4 // LDY Zero Page,X
#define INS_LDY_ABS 0xAC // LDY Absolute
#define INS_LDY_ABSX 0xBC // LDY Absolute,X
#define INS_JSR 0x20 // JSR
#define INS_RTS 0x60 // RTS

View File

@ -6,13 +6,13 @@ struct CPU cpu;
struct MEMORY mem;
byte cycles;
void memory_init (){ // FIXME: Warning: mem not used
void memory_init () {
for (int i = 0; i < MAX_MEMORY; i++){
mem.memory[i] = 0;
}
}
void reset () {
cpu.PC = 0xFFFC;
cpu.PC = 0x0000; // FIXME //0xFFFC;
cpu.SP = 0x0000; //FIXME
cpu.A = cpu.X = cpu.Y = 0;
cpu.C = cpu.Z = cpu.I = cpu.D = cpu.B = cpu.V = cpu.N = 0;
@ -47,15 +47,18 @@ word fetch_word() {
return data;
}
byte read_byte(byte addr) {
byte read_byte(word addr) {
byte data = mem.memory[addr];
cycles--;
cpu.PC++;
return data;
}
word read_word(byte addr) {
word read_word(word addr) {
word data = mem.memory[addr];
data |= (mem.memory[addr+1] << 8);
cpu.PC++;
cpu.PC++;
cycles -= 2;
return data;
}
@ -118,7 +121,6 @@ void execute(byte exec_cycles) {
cpu.X = read_byte(zero_page_y_addr);
cpu.Z = (cpu.X == 0);
cpu.N = (cpu.X & 0b1000000) > 0;
printf("test\n");
break;
case INS_LDY_IM:
@ -152,33 +154,26 @@ void execute(byte exec_cycles) {
// TODO
break;
default:
printf("Unhandled instruction: %04X\n", instruction);
cycles--;
printf("%04X: Unhandled instruction: %04X\n", cpu.PC, instruction);
cycles = 0;
}
}
}
int main() {
int main(int argc, char *argv[]) {
reset();
mem.memory[0xFFFC] = INS_JSR;
mem.memory[0xFFFD] = 0x42;
mem.memory[0xFFFE] = 0x42;
mem.memory[0x4242] = INS_LDX_IM;
mem.memory[0x4243] = 0x66;
mem.memory[0x4244] = INS_LDY_IM;
mem.memory[0x4245] = 0x11;
mem.memory[0x4246] = INS_LDX_ZP;
mem.memory[0x4247] = 0x68;
mem.memory[0x68] = 0x75;
mem.memory[0x4248] = INS_LDY_ZP;
mem.memory[0x4249] = 0x69;
mem.memory[0x69] = 0x88;
execute(18);
// TODO make loading from rom
FILE *infile = fopen(argv[1], "rb");
char ch;
int pos = 0;
while (1) { // FIXME
ch = fgetc(infile);
if (ch == EOF)
break;
mem.memory[pos] = ch;
pos++;
}
execute(5); // FIXME: count how many circles is needed based on instructions
dump_cpu(cpu);
return 0;
}

1
src/6502/test.bin Normal file
View File

@ -0,0 +1 @@
ゥVV