Class: GBRb::Timer::Counter
- Inherits:
-
Object
- Object
- GBRb::Timer::Counter
- Defined in:
- lib/gbrb/timer.rb
Instance Attribute Summary collapse
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(name, initial_frequency) ⇒ Counter
constructor
A new instance of Counter.
- #reset ⇒ Object
- #set_frequency(frequency) ⇒ Object
- #step(cycles) ⇒ Object
- #to_s ⇒ Object
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
#value ⇒ Object
Returns the value of attribute value.
80 81 82 |
# File 'lib/gbrb/timer.rb', line 80 def value @value end |
Instance Method Details
#reset ⇒ Object
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_s ⇒ Object
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 |