implementing relative addressing mode

This commit is contained in:
Thiago Auler dos Santos 2017-11-16 20:12:06 -02:00
parent c9624f1971
commit d67f57b752
3 changed files with 28 additions and 24 deletions

View File

@ -9,12 +9,6 @@ void init()
{ {
// pc is set using 0xFFFC // pc is set using 0xFFFC
pc = read_word(0xFFFC); pc = read_word(0xFFFC);
write_mem(0xD010, 'T');
write_mem(0xD010, 'E');
write_mem(0xD010, 'S');
write_mem(0xD010, 'T');
write_mem(0xD010, 'S');
} }
void fetch() void fetch()
@ -65,6 +59,12 @@ void decode()
address_mode = absolute_y; address_mode = absolute_y;
} }
} }
if ((ir & 0b00011111) == 0b00010000)
{
// adjust the addressing mode for branch intructions
address_mode = relative;
}
} }
void execute() void execute()

View File

@ -44,7 +44,8 @@ enum address_mode
absolute_y = 0b110, absolute_y = 0b110,
absolute_x = 0b111, absolute_x = 0b111,
indirect_x, indirect_x,
zero_page_y zero_page_y,
relative
}; };
typedef enum opcodes_table oct; typedef enum opcodes_table oct;

View File

@ -75,6 +75,17 @@ void fetch_operand()
operand = read_byte(address); operand = read_byte(address);
pc = pc + 1; pc = pc + 1;
break; break;
case relative:
address = read_byte(pc);
if ((address >> 7) == 0)
{
address = pc + address;
}
else
{
address = pc - address;
}
break;
} }
} }
@ -141,10 +152,9 @@ void bcc()
// branch on carry clear // branch on carry clear
if(!C_IS_SET) if(!C_IS_SET)
{ {
address = read_byte(pc); fetch_operand();
address = address + pc;
pc = address; pc = address;
} }
} }
void bcs() void bcs()
@ -152,8 +162,7 @@ void bcs()
// branch on carry set // branch on carry set
if(C_IS_SET) if(C_IS_SET)
{ {
address = read_byte(pc); fetch_operand();
address = address + pc;
pc = address; pc = address;
} }
} }
@ -163,8 +172,7 @@ void beq()
// branch on result zero // branch on result zero
if(Z_IS_SET) if(Z_IS_SET)
{ {
address = read_byte(pc); fetch_operand();
address = address + pc;
pc = address; pc = address;
} }
} }
@ -179,8 +187,7 @@ void bmi()
// branch on result minus // branch on result minus
if(N_IS_SET) if(N_IS_SET)
{ {
address = read_byte(pc); fetch_operand();
address = address + pc;
pc = address; pc = address;
} }
} }
@ -190,8 +197,7 @@ void bne()
// branch on result not zero // branch on result not zero
if(!Z_IS_SET) if(!Z_IS_SET)
{ {
address = read_byte(pc); fetch_operand();
address = address + pc;
pc = address; pc = address;
} }
} }
@ -201,8 +207,7 @@ void bpl()
// branch on result plus // branch on result plus
if(!N_IS_SET) if(!N_IS_SET)
{ {
address = read_byte(pc); fetch_operand();
address = address + pc;
pc = address; pc = address;
} }
} }
@ -217,8 +222,7 @@ void bvc()
// branch on overflow clear // branch on overflow clear
if(!V_IS_SET) if(!V_IS_SET)
{ {
address = read_byte(pc); fetch_operand();
address = address + pc;
pc = address; pc = address;
} }
} }
@ -228,8 +232,7 @@ void bvs()
// branch on overflow set // branch on overflow set
if(V_IS_SET) if(V_IS_SET)
{ {
address = read_byte(pc); fetch_operand();
address = address + pc;
pc = address; pc = address;
} }
} }