From d67f57b7527d9a420cfc3ffae7629a8b5d427920 Mon Sep 17 00:00:00 2001 From: Thiago Auler dos Santos Date: Thu, 16 Nov 2017 20:12:06 -0200 Subject: [PATCH] implementing relative addressing mode --- src/6502.c | 12 ++++++------ src/inc/opcodes.h | 3 ++- src/opcodes.c | 37 ++++++++++++++++++++----------------- 3 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/6502.c b/src/6502.c index 6ef6820..db64b35 100644 --- a/src/6502.c +++ b/src/6502.c @@ -9,12 +9,6 @@ void init() { // pc is set using 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() @@ -65,6 +59,12 @@ void decode() address_mode = absolute_y; } } + + if ((ir & 0b00011111) == 0b00010000) + { + // adjust the addressing mode for branch intructions + address_mode = relative; + } } void execute() diff --git a/src/inc/opcodes.h b/src/inc/opcodes.h index 67a5962..3c73ac8 100644 --- a/src/inc/opcodes.h +++ b/src/inc/opcodes.h @@ -44,7 +44,8 @@ enum address_mode absolute_y = 0b110, absolute_x = 0b111, indirect_x, - zero_page_y + zero_page_y, + relative }; typedef enum opcodes_table oct; diff --git a/src/opcodes.c b/src/opcodes.c index 79b63ad..12ddf7c 100644 --- a/src/opcodes.c +++ b/src/opcodes.c @@ -75,6 +75,17 @@ void fetch_operand() operand = read_byte(address); pc = pc + 1; 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 if(!C_IS_SET) { - address = read_byte(pc); - address = address + pc; + fetch_operand(); pc = address; - } + } } void bcs() @@ -152,8 +162,7 @@ void bcs() // branch on carry set if(C_IS_SET) { - address = read_byte(pc); - address = address + pc; + fetch_operand(); pc = address; } } @@ -163,8 +172,7 @@ void beq() // branch on result zero if(Z_IS_SET) { - address = read_byte(pc); - address = address + pc; + fetch_operand(); pc = address; } } @@ -179,8 +187,7 @@ void bmi() // branch on result minus if(N_IS_SET) { - address = read_byte(pc); - address = address + pc; + fetch_operand(); pc = address; } } @@ -190,8 +197,7 @@ void bne() // branch on result not zero if(!Z_IS_SET) { - address = read_byte(pc); - address = address + pc; + fetch_operand(); pc = address; } } @@ -201,8 +207,7 @@ void bpl() // branch on result plus if(!N_IS_SET) { - address = read_byte(pc); - address = address + pc; + fetch_operand(); pc = address; } } @@ -217,8 +222,7 @@ void bvc() // branch on overflow clear if(!V_IS_SET) { - address = read_byte(pc); - address = address + pc; + fetch_operand(); pc = address; } } @@ -228,8 +232,7 @@ void bvs() // branch on overflow set if(V_IS_SET) { - address = read_byte(pc); - address = address + pc; + fetch_operand(); pc = address; } }