Skip to content

Commit

Permalink
Rewrite pulse sweepUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
miochen1226 committed Oct 10, 2023
1 parent a89327d commit c41df79
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 65 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -94,38 +94,6 @@
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "4954BD1C-EEAA-49FF-84C4-291B36DEE5FD"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/NES/CPU/Cpu.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "33"
endingLineNumber = "33"
landmarkName = "handleCpuRead(_:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "C0968AEC-978F-4FBB-A82E-638F8B9B4268"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/NES/CPU/Cpu.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "40"
endingLineNumber = "40"
landmarkName = "handleCpuRead(_:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
Expand Down
64 changes: 31 additions & 33 deletions Shared/NES/Apu/Channel/PulseChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ class PulseChannel:BaseChannel {
if c {
volumeEnvelope.setConstantVolumeMode(true)
volumeEnvelope.setConstantVolume(UInt16(vvvv))
//print("volumeEnvelope c true->" + String(vvvv))
}
else {
volumeEnvelope.setConstantVolumeMode(false)
volumeEnvelope.setCounter(UInt16(vvvv))
volumeEnvelope.restart()
}
}

Expand All @@ -85,7 +87,7 @@ class PulseChannel:BaseChannel {
let SSS = readBits8( target:BITS([0,1,2]),value:value)
sweepUnit.setShiftCount(SSS)

sweepUnit.restart()
//sweepUnit.restart()
}

func handle40024006(cpuAddress:UInt16, value:UInt8)
Expand All @@ -104,11 +106,12 @@ class PulseChannel:BaseChannel {
//timer 的高 3 位,和 0x4002 / 0x4006 组成完整的计数
let HHH = readBits8(target: BITS([0,1,2]),value:value)
timer.setPeriodHigh3(UInt16(HHH))
timer.reset()

let LLLL = readBits8( target:BITS([3,4,5,6,7]),value:value) >> 3
lengthCounter.loadCounterFromLUT(LLLL)

volumeEnvelope.restart()
//volumeEnvelope.restart()
pulseWaveGenerator.restart()
}

Expand Down Expand Up @@ -137,6 +140,7 @@ class PulseChannel:BaseChannel {
if sweepUnit.silenceChannel() {
return 0
}

if lengthCounter.silenceChannel() {
return 0
}
Expand Down Expand Up @@ -238,61 +242,55 @@ class SweepUnit {

// Clocked by FrameCounter
func clock(_ timer: inout ChannelComponent.Timer) {
if !enabled {
return
}

if reload {
if enabled {
timer.reset()
computeTargetPeriod(timer: &timer)
if divider.clock() {
adjustTimerPeriod(timer:&timer)
}
}

divider.resetCounter()
reload = false
}
else {
computeTargetPeriod(timer: &timer)
if enabled {
if divider.clock() {
adjustTimerPeriod(timer:&timer)
}
}

computeTargetPeriod(timer: &timer)
//Counter is 0
if divider.clock() == true {
adjustTimerPeriod(timer:&timer)
}
}

func silenceChannel() -> Bool {
if !enabled {
return false
}
return isSilenceChannel
return false//isSilenceChannel
}

func computeTargetPeriod(timer: inout ChannelComponent.Timer) {
if !enabled {
return
}
assert(shiftCount < 8)

let currPeriod = timer.getPeriod()
let shiftedPeriod = currPeriod >> shiftCount
if (negate) {

if negate {
let changeAmount = currPeriod >> shiftCount

// Pulse 1's adder's carry is hardwired, so the subtraction adds the one's complement
// instead of the expected two's complement (as pulse 2 does)
//print(String(shiftedPeriod))

if shiftedPeriod > subtractExtra {
let del = (shiftedPeriod - subtractExtra)
if currPeriod > del {
targetPeriod = currPeriod - (shiftedPeriod - subtractExtra)
if changeAmount > subtractExtra {
if currPeriod < changeAmount + subtractExtra {
targetPeriod = 0
}
else {
targetPeriod = currPeriod - changeAmount - subtractExtra
}
}
}
else {
targetPeriod = currPeriod + shiftedPeriod

let changeAmount = currPeriod >> shiftCount
if shiftCount == 0 && changeAmount == currPeriod {
targetPeriod = currPeriod + currPeriod
}

else {
targetPeriod = currPeriod + changeAmount
}
}

isSilenceChannel = (currPeriod < 8 || targetPeriod > 0x7FF)
Expand Down

0 comments on commit c41df79

Please sign in to comment.