Module: Badline::InstructionSet::Stack

Included in:
Badline::InstructionSet
Defined in:
lib/badline/instruction_set/stack.rb

Instance Method Summary collapse

Instance Method Details

#jmp(addr, _value) ⇒ Object

Jump to absolute address.

Opcodes:

$4C - absolute - 3 cycles


43
44
45
# File 'lib/badline/instruction_set/stack.rb', line 43

def jmp(addr, _value)
  @program_counter = addr
end

#jsr(addr, _value) ⇒ Object

Jump to subroutine. The operand high byte is fetched only after the return address has been pushed, so a JSR executing inside the stack jumps via the value its own push just wrote.

Opcodes:

$20 - absolute - 6 cycles


53
54
55
56
57
58
59
60
# File 'lib/badline/instruction_set/stack.rb', line 53

def jsr(addr, _value)
  return_addr = (program_counter - 1) & 0xffff
  cycle { @memory.peek(stack_address) } # internal cycle: dummy stack read
  write_byte(stack_address, high_byte(return_addr))
  write_byte(stack_address(-1), low_byte(return_addr))
  @stack_pointer = (@stack_pointer - 2) & 0xff
  @program_counter = uint16(addr, read_byte(return_addr))
end

#pha(_addr, _value) ⇒ Object

Push accumulator onto the stack.

Opcodes:

$48 - implied - 3 cycles


10
11
12
# File 'lib/badline/instruction_set/stack.rb', line 10

def pha(_addr, _value)
  stack_push(@a)
end

#php(_addr, _value) ⇒ Object

Push processor status onto the stack.

Opcodes:

$08 - implied - 3 cycles


18
19
20
# File 'lib/badline/instruction_set/stack.rb', line 18

def php(_addr, _value)
  stack_push(p | 0b00010000)
end

#pla(_addr, _value) ⇒ Object

Pull accumulator from stack.

Opcodes:

$68 - implied - 4 cycles


26
27
28
29
# File 'lib/badline/instruction_set/stack.rb', line 26

def pla(_addr, _value)
  cycle { @a = stack_pull }
  update_number_flags(@a)
end

#plp(_addr, _value) ⇒ Object

Pull processor status from stack.

Opcodes:

$28 - implied - 4 cycles


35
36
37
# File 'lib/badline/instruction_set/stack.rb', line 35

def plp(_addr, _value)
  cycle { status.value = stack_pull & 0b11101111 }
end

#rti(_addr, _value) ⇒ Object

Return from interrupt.

Opcodes:

$40 - implied - 6 cycles


66
67
68
69
70
71
72
73
# File 'lib/badline/instruction_set/stack.rb', line 66

def rti(_addr, _value)
  cycle do
    @stack_pointer = (@stack_pointer + 1) & 0xff
    @status.value = memory[stack_address]
    @status.break = false
  end
  @program_counter = uint16(stack_pull, stack_pull)
end

#rts(_addr, _value) ⇒ Object

Return from subroutine.

Opcodes:

$60 - implied - 6 cycles


79
80
81
82
83
# File 'lib/badline/instruction_set/stack.rb', line 79

def rts(_addr, _value)
  cycle do
    @program_counter = (uint16(stack_pull, stack_pull) + 1) & 0xffff
  end
end