Class: GBRb::Timer::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/gbrb/timer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, initial_frequency) ⇒ Counter

Returns a new instance of Counter.



82
83
84
85
86
# File 'lib/gbrb/timer.rb', line 82

def initialize name, initial_frequency
  reset
  @name = name
  set_frequency initial_frequency
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



80
81
82
# File 'lib/gbrb/timer.rb', line 80

def value
  @value
end

Instance Method Details

#resetObject



88
89
90
91
# File 'lib/gbrb/timer.rb', line 88

def reset
  @frequency = FREQUENCIES[:freq4096]
  @value = 0
end

#set_frequency(frequency) ⇒ Object



93
94
95
96
# File 'lib/gbrb/timer.rb', line 93

def set_frequency frequency
  @frequency = FREQUENCIES[frequency] if FREQUENCIES.has_key? frequency
  @clock_counter = @frequency.cycles
end

#step(cycles) ⇒ Object



102
103
104
105
106
107
108
109
110
111
# File 'lib/gbrb/timer.rb', line 102

def step cycles
  @clock_counter -= cycles

  while @clock_counter <= 0
    @value = (@value + 1) & 0xff
    @clock_counter += @frequency.cycles
    return true if @value == 0
  end
  false
end

#to_sObject



98
99
100
# File 'lib/gbrb/timer.rb', line 98

def to_s
  "#{@name}: Frequency: #{@frequency.string} (#{@frequency.cycles} cycles) | Current Counter: #{@clock_counter} | Value: #{@value}"
end