Skip to content

Commit

Permalink
initial program counter increment
Browse files Browse the repository at this point in the history
  • Loading branch information
tnibert committed Oct 20, 2019
1 parent b940b79 commit 00b88eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
2 changes: 2 additions & 0 deletions 6502.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
int Famicom::emulate6502op()
{
// increment program counter for instruction read
// may need to post increment, but we'll start with this
// http://6502.org/tutorials/6502opcodes.html#PC
cpu->pc++;
uint8_t opcode = memory->readmem(cpu->pc);

Expand Down
12 changes: 12 additions & 0 deletions addressingmodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@
*/

// immediate addressing is passing the value specified in code
uint8_t immediate(cpustate * cpu, Memory * mem)
{
cpu->pc++;
return mem->readmem(cpu->pc);
}

uint8_t indexedindirect(cpustate * cpu, Memory * mem)
{
// todo: what to increment pc by?
// (Indirect,X) aka (zero page,X)
uint8_t zpageaddr = mem->readmem(cpu->pc+1);
uint16_t addr = zpageaddr + cpu->x;
Expand All @@ -35,22 +41,26 @@ uint8_t zeropage(cpustate * cpu, Memory * mem)

uint8_t absolute(cpustate * cpu, Memory * mem)
{
cpu->pc+=2;
// is revlendianbytes() necessary?
return mem->readmem(revlendianbytes(mem->readmem(cpu->pc + 1), mem->readmem(cpu->pc + 2)));
}

uint8_t zeropagex(cpustate * cpu, Memory * mem)
{
cpu->pc++;
return mem->readmem(cpu->pc+1)+cpu->x;
}

uint8_t zeropagey(cpustate * cpu, Memory * mem)
{
cpu->pc++;
return mem->readmem(cpu->pc+1)+cpu->y;
}

uint8_t indirectindexed(cpustate * cpu, Memory * mem)
{
// todo: what to increment pc by?
// (Indirect),Y aka (zero page),Y
uint8_t zpageaddr = mem->readmem(cpu->pc+1);
uint16_t addr = (revlendianbytes(mem->readmem(zpageaddr), mem->readmem(zpageaddr+1))) + cpu->y;
Expand All @@ -59,13 +69,15 @@ uint8_t indirectindexed(cpustate * cpu, Memory * mem)

uint8_t absolutey(cpustate * cpu, Memory * mem)
{
cpu->pc+=2;
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1), mem->readmem(cpu->pc+2));
addr += cpu->y;
return addr;
}

uint8_t absolutex(cpustate * cpu, Memory * mem)
{
cpu->pc+=2;
uint16_t addr = revlendianbytes(mem->readmem(cpu->pc+1), mem->readmem(cpu->pc+2));
addr += cpu->x;
return addr;
Expand Down
10 changes: 2 additions & 8 deletions decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ InstructionDecoder::InstructionDecoder(cpustate * c, Memory * m)
*/
int InstructionDecoder::decode_and_execute(uint8_t opcode)
{
printf("In decode and execute\n");
printf("In decode and execute: op: %x, pc: %x\n", opcode, cpu->pc);
uint8_t cc = opcode & 0b00000011; // control code
uint8_t bbb = opcode & 0b00011100; // addressing mode
uint8_t aaa = opcode & 0b11100000; // operation
Expand Down Expand Up @@ -70,6 +70,7 @@ int InstructionDecoder::decode_and_execute(uint8_t opcode)
// todo: add addressing functions for immediate and absolute
case 0b00000000:
// (zero page,X) aka (indirect,X)
data = indexedindirect(cpu, mem);
// will these inner breaks break out of the entire nested switch?
break;
case 0b00001000: // immediate
Expand Down Expand Up @@ -131,12 +132,5 @@ int InstructionDecoder::decode_and_execute(uint8_t opcode)
// case 0b11 only used for illegal opcodes
}

// increment the program counter
// todo: remove all other program counter increments in code
// todo: do not increment counter in case of jump
// todo: confirm amount to increment by - http://6502.org/tutorials/6502opcodes.html#PC
//
//cpu->pc += 2;

return 1; // replace this with cycles used
}

0 comments on commit 00b88eb

Please sign in to comment.