Class: Rubyboy::ApuChannels::Channel1
- Inherits:
-
Object
- Object
- Rubyboy::ApuChannels::Channel1
- Defined in:
- lib/rubyboy/apu_channels/channel1.rb
Constant Summary collapse
- WAVE_DUTY =
[ [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0], # 12.5% [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0], # 25% [0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0], # 50% [0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] # 75% ].freeze
Instance Attribute Summary collapse
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#wave_duty_position ⇒ Object
Returns the value of attribute wave_duty_position.
Instance Method Summary collapse
- #calculate_frequency ⇒ Object
- #dac_output ⇒ Object
- #envelope ⇒ Object
-
#initialize ⇒ Channel1
constructor
A new instance of Channel1.
- #length ⇒ Object
- #read_nr1x(x) ⇒ Object
- #step(cycles) ⇒ Object
- #step_fs(fs) ⇒ Object
- #sweep ⇒ Object
- #write_nr1x(x, val) ⇒ Object
Constructor Details
#initialize ⇒ Channel1
Returns a new instance of Channel1.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rubyboy/apu_channels/channel1.rb', line 15 def initialize @cycles = 0 @frequency = 0 @frequency_timer = 0 @wave_duty_position = 0 @enabled = false @dac_enabled = false @length_enabled = false @is_upwards = false @is_decrementing = false @sweep_enabled = false @sweep_period = 0 @sweep_shift = 0 @period = 0 @period_timer = 0 @current_volume = 0 @initial_volume = 0 @shadow_frequency = 0 @sweep_timer = 0 @length_timer = 0 @wave_duty_pattern = 0 end |
Instance Attribute Details
#enabled ⇒ Object
Returns the value of attribute enabled.
6 7 8 |
# File 'lib/rubyboy/apu_channels/channel1.rb', line 6 def enabled @enabled end |
#wave_duty_position ⇒ Object
Returns the value of attribute wave_duty_position.
6 7 8 |
# File 'lib/rubyboy/apu_channels/channel1.rb', line 6 def wave_duty_position @wave_duty_position end |
Instance Method Details
#calculate_frequency ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/rubyboy/apu_channels/channel1.rb', line 91 def calculate_frequency if @is_decrementing if @shadow_frequency >= (@shadow_frequency >> @sweep_shift) @shadow_frequency - (@shadow_frequency >> @sweep_shift) else 0 end else [0x3ff, @shadow_frequency + (@shadow_frequency >> @sweep_shift)].min end end |
#dac_output ⇒ Object
103 104 105 106 107 108 |
# File 'lib/rubyboy/apu_channels/channel1.rb', line 103 def dac_output return 0.0 unless @dac_enabled && @enabled ret = WAVE_DUTY[@wave_duty_pattern][@wave_duty_position] * @current_volume (ret / 7.5) - 1.0 end |
#envelope ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rubyboy/apu_channels/channel1.rb', line 62 def envelope return if @period == 0 @period_timer -= 1 if @period_timer > 0 return if @period_timer != 0 @period_timer = @period if @current_volume < 15 && @is_upwards @current_volume += 1 elsif @current_volume > 0 && !@is_upwards @current_volume -= 1 end end |
#length ⇒ Object
55 56 57 58 59 60 |
# File 'lib/rubyboy/apu_channels/channel1.rb', line 55 def length return unless @length_enabled && @length_timer > 0 @length_timer -= 1 @enabled &= @length_timer > 0 end |
#read_nr1x(x) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/rubyboy/apu_channels/channel1.rb', line 110 def read_nr1x(x) case x when 0 then (@sweep_period << 4) | @sweep_shift | (@is_decrementing ? 0x08 : 0x00) | 0x80 when 1 then (@wave_duty_pattern << 6) | 0x3f when 2 then (@initial_volume << 4) | (@is_upwards ? 0x08 : 0x00) | @period when 3 then 0xff when 4 then (@length_enabled ? 0x40 : 0x00) | 0xbf else 0xff end end |
#step(cycles) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/rubyboy/apu_channels/channel1.rb', line 39 def step(cycles) @cycles += cycles return if @cycles < @frequency_timer @cycles -= @frequency_timer @frequency_timer = (2048 - @frequency) * 4 @wave_duty_position = (@wave_duty_position + 1) % 8 end |
#step_fs(fs) ⇒ Object
49 50 51 52 53 |
# File 'lib/rubyboy/apu_channels/channel1.rb', line 49 def step_fs(fs) length if fs & 0x01 == 0 envelope if fs == 7 sweep if fs == 2 || fs == 6 end |
#sweep ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rubyboy/apu_channels/channel1.rb', line 78 def sweep @sweep_timer -= 1 if @sweep_timer > 0 return if @sweep_timer != 0 @sweep_timer = @sweep_period return unless @sweep_enabled @frequency = calculate_frequency @shadow_frequency = @frequency end |
#write_nr1x(x, val) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/rubyboy/apu_channels/channel1.rb', line 121 def write_nr1x(x, val) case x when 0 @sweep_period = (val >> 4) & 0x07 @is_decrementing = (val & 0x08) > 0 @sweep_shift = val & 0x07 when 1 @wave_duty_pattern = (val >> 6) & 0x03 @length_timer = 64 - (val & 0x3f) when 2 @is_upwards = (val & 0x08) > 0 @initial_volume = (val >> 4) @period = val & 0x07 @dac_enabled = val & 0xf8 > 0 @enabled &= @dac_enabled when 3 @frequency = (@frequency & 0x700) | val when 4 @frequency = (@frequency & 0xff) | ((val & 0x07) << 8) @length_enabled = (val & 0x40) > 0 @length_timer = 64 if @length_timer == 0 return unless (val & 0x80) > 0 && @dac_enabled @enabled = true @period_timer = @period @current_volume = @initial_volume @shadow_frequency = @frequency @sweep_timer = @sweep_period @sweep_enabled = @sweep_period > 0 || @sweep_shift > 0 end end |