Class: Rubyboy::ApuChannels::Channel4

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyboy/apu_channels/channel4.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

Instance Method Summary collapse

Constructor Details

#initializeChannel4

Returns a new instance of Channel4.



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
42
# File 'lib/rubyboy/apu_channels/channel4.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

  @lfsr = 0x7fff
  @width_mode = false
  @shift_amount = 0
  @divisor_code = 0
end

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



6
7
8
# File 'lib/rubyboy/apu_channels/channel4.rb', line 6

def enabled
  @enabled
end

#wave_duty_positionObject

Returns the value of attribute wave_duty_position.



6
7
8
# File 'lib/rubyboy/apu_channels/channel4.rb', line 6

def wave_duty_position
  @wave_duty_position
end

Instance Method Details

#calculate_frequencyObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rubyboy/apu_channels/channel4.rb', line 88

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_outputObject



100
101
102
103
104
105
# File 'lib/rubyboy/apu_channels/channel4.rb', line 100

def dac_output
  return 0.0 unless @dac_enabled && @enabled

  ret = (@lfsr & 0x01) * @current_volume
  (ret / 7.5) - 1.0
end

#envelopeObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rubyboy/apu_channels/channel4.rb', line 72

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

#lengthObject



65
66
67
68
69
70
# File 'lib/rubyboy/apu_channels/channel4.rb', line 65

def length
  return unless @length_enabled && @length_timer > 0

  @length_timer -= 1
  @enabled &= @length_timer > 0
end

#read_nr4x(x) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/rubyboy/apu_channels/channel4.rb', line 107

def read_nr4x(x)
  case x
  when 0 then 0xff
  when 1 then 0xff
  when 2 then (@initial_volume << 4) | (@is_upwards ? 0x08 : 0x00) | @period
  when 3 then (@shift_amount << 4) | (@width_mode ? 0x08 : 0x00) | @divisor_code
  when 4 then (@length_enabled ? 0x40 : 0x00) | 0xbf
  else 0xff
  end
end

#step(cycles) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubyboy/apu_channels/channel4.rb', line 44

def step(cycles)
  @cycles += cycles

  return if @cycles < @frequency_timer

  @cycles -= @frequency_timer
  @frequency_timer = [8, @divisor_code << 4].max << @shift_amount

  xor = (@lfsr & 0x01) ^ ((@lfsr & 0b10) >> 1)
  @lfsr = (@lfsr >> 1) | (xor << 14)
  return unless @width_mode

  @lfsr &= ~(1 << 6)
  @lfsr |= xor << 6
end

#step_fs(fs) ⇒ Object



60
61
62
63
# File 'lib/rubyboy/apu_channels/channel4.rb', line 60

def step_fs(fs)
  length if fs & 0x01 == 0
  envelope if fs == 7
end

#write_nr4x(x, val) ⇒ Object



118
119
120
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
# File 'lib/rubyboy/apu_channels/channel4.rb', line 118

def write_nr4x(x, val)
  case x
  when 0
    # nop
  when 1
    @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
    @shift_amount = (val >> 4) & 0x0f
    @width_mode = (val & 0x08) > 0
    @divisor_code = val & 0x07
  when 4
    @length_enabled = (val & 0x40) > 0
    @length_timer = 64 if @length_timer == 0
    return unless (val & 0x80) > 0

    @enabled = true if @dac_enabled

    @lfsr = 0x7fff
    @period_timer = @period
    @current_volume = @initial_volume
  end
end