Class: GBRb::InstructionSet::Ret

Inherits:
Instruction show all
Defined in:
lib/gbrb/instruction_set/return.rb

Instance Attribute Summary

Attributes inherited from Instruction

#i, #m, #t

Instance Method Summary collapse

Methods inherited from Instruction

#carry?, #immediate_count

Constructor Details

#initialize(condition, m = 2, t_high = 20, t = 8) ⇒ Ret

Returns a new instance of Ret.



5
6
7
8
9
10
11
# File 'lib/gbrb/instruction_set/return.rb', line 5

def initialize condition, m=2, t_high=20, t=8
  @condition = condition
  @t_high = t_high
  @t_low = t
  @m_low = m
  super m, t, 0
end

Instance Method Details

#call(r, mem) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gbrb/instruction_set/return.rb', line 13

def call r, mem
  condition_met = case @condition
                  when :c
                    r.carry_flag?
                  when :nc
                    not r.carry_flag?
                  when :nz
                    not r.zero_flag?
                  when :z
                    r.zero_flag?
                  when :none
                    true
                  else
                    false
                  end
  if condition_met
    @t = @t_high
    @m = @t_high / 4
    low = mem.read_byte(r.sp.read)
    r.sp.store r.sp.read + 1
    high = mem.read_byte(r.sp.read)
    r.sp.store r.sp.read + 1

    r.pc.store (high << 8) + low
  else
    @t = @t_low
    @m = @m_low
  end
end