Skip to content

Commit

Permalink
Correct apu implement.
Browse files Browse the repository at this point in the history
  • Loading branch information
miochen1226 committed Oct 11, 2023
1 parent edca5ba commit cc38c35
Show file tree
Hide file tree
Showing 12 changed files with 237 additions and 153 deletions.
4 changes: 2 additions & 2 deletions NES_EMU.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_IDENTITY = "iPhone Distribution";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 6;
CURRENT_PROJECT_VERSION = 7;
DEVELOPMENT_TEAM = 82Q375NBS9;
ENABLE_PREVIEWS = YES;
GCC_OPTIMIZATION_LEVEL = 3;
Expand Down Expand Up @@ -791,7 +791,7 @@
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_IDENTITY = "iPhone Distribution";
CODE_SIGN_STYLE = Manual;
CURRENT_PROJECT_VERSION = 6;
CURRENT_PROJECT_VERSION = 7;
DEVELOPMENT_TEAM = 82Q375NBS9;
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = iOS/Info.plist;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@
filePath = "Shared/NES/Apu/FrameCounter/FrameCounter.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "118"
endingLineNumber = "118"
startingLineNumber = "115"
endingLineNumber = "115"
landmarkName = "Clock()"
landmarkType = "7">
</BreakpointContent>
Expand All @@ -200,8 +200,8 @@
filePath = "Shared/NES/Apu/FrameCounter/FrameCounter.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "127"
endingLineNumber = "127"
startingLineNumber = "124"
endingLineNumber = "124"
landmarkName = "Clock()"
landmarkType = "7">
</BreakpointContent>
Expand Down Expand Up @@ -254,5 +254,84 @@
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "503968DD-4DFF-4A9D-8D0C-AA6D9EE7DC18"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/NES/Cartridge/Mapper/Mapper1.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "18"
endingLineNumber = "18"
landmarkName = "reset()"
landmarkType = "7">
<Locations>
<Location
uuid = "503968DD-4DFF-4A9D-8D0C-AA6D9EE7DC18 - d758eddee50c1be9"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "NES_EMU.Mapper1.OnCpuWrite(cpuAddress: Swift.UInt16, value: Swift.UInt8) -&gt; ()"
moduleName = "NES_EMU"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/mio/Documents/GitHub/NES_EMU/Shared/NES/Cartridge/Mapper/Mapper1.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "17"
endingLineNumber = "17"
offsetFromSymbolStart = "218">
</Location>
<Location
uuid = "503968DD-4DFF-4A9D-8D0C-AA6D9EE7DC18 - d758eddee50c1be9"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "NES_EMU.Mapper1.OnCpuWrite(cpuAddress: Swift.UInt16, value: Swift.UInt8) -&gt; ()"
moduleName = "NES_EMU"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/mio/Documents/GitHub/NES_EMU/Shared/NES/Cartridge/Mapper/Mapper1.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "17"
endingLineNumber = "17"
offsetFromSymbolStart = "396">
</Location>
<Location
uuid = "503968DD-4DFF-4A9D-8D0C-AA6D9EE7DC18 - 56f5f9552b8cd7a9"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "NES_EMU.Mapper1.postInitialize() -&gt; ()"
moduleName = "NES_EMU"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/mio/Documents/GitHub/NES_EMU/Shared/NES/Cartridge/Mapper/Mapper1.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "17"
endingLineNumber = "17"
offsetFromSymbolStart = "25">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "C4871E06-1BBE-4427-8B7B-F0489D69B756"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/NES/Nes.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "220"
endingLineNumber = "220"
landmarkName = "SignalCpuIrq()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
4 changes: 2 additions & 2 deletions Shared/NES/Apu/Apu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,12 @@ class Apu: NSObject {
evenFrame = !evenFrame

triangleChannel?.clockTimer()
//if evenFrame {
if evenFrame {
pulseChannel0?.clockTimer()
pulseChannel1?.clockTimer()
noiseChannel?.clockTimer()
dmcChannel?.clockTimer()
//}
}

elapsedCpuCycles += 1

Expand Down
86 changes: 69 additions & 17 deletions Shared/NES/Apu/Channel/BaseChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,15 @@ class ChannelComponent {

func setPeriodLow8(_ value: UInt8) {
var period:UInt16 = divider.getPeriod()
//period = (period & BITS16([8,9,10])) | UInt16(value)
period = (period & BITS16([8,9,10])) | UInt16(value)
//periodTemp = UInt16(value)
setPeriod(period)
//print("periodTemp->" + String(periodTemp))
//setPeriod(UInt16(value))
//divider.resetCounter()
}

//var periodTemp:UInt16 = 0
func setPeriodHigh3(_ value: UInt16) {
assert(value < BIT(3))

var period:UInt16 = divider.getPeriod()
period = (value << 8) | (period & 0xFF)

//periodTemp = (value << 8) | (periodTemp & 0xFF)
setPeriod(period)
//print("periodTemp->" + String(periodTemp))
//setPeriod(UInt16(400))
//divider.resetCounter()
}

func setMinPeriod(_ minPeriod: Int){
Expand All @@ -85,10 +73,9 @@ class ChannelComponent {
// Returns true when output chip should be clocked
func clock()-> Bool {
// Avoid popping and weird noises from ultra sonic frequencies
//if (m_divider.GetPeriod() < m_minPeriod)
//{
// return false
//}
if divider.getPeriod() < minPeriod {
return false
}

if divider.clock() {
return true
Expand Down Expand Up @@ -131,7 +118,7 @@ class ChannelComponent {
}

if counter > 0 {
counter = counter-1
counter = counter - 1
}
}

Expand Down Expand Up @@ -199,6 +186,71 @@ class ChannelComponent {
var reload: Bool = true
var control: Bool = true
}

class VolumeEnvelope {
var testDisable = false
func restart() {
wantRestart = true
}

func setLoop(_ loop: Bool) {
self.loop = loop
}

func setConstantVolumeMode(_ mode: Bool) {
constantVolumeMode = mode
}

func setCounter(_ value: UInt16) {
divider.setPeriod(value)
}

func setConstantVolume(_ value: UInt16) {
constantVolume = value
divider.setPeriod(constantVolume)
}

func getVolume() -> UInt16 {
if testDisable {
return 15
}

if constantVolumeMode {
return constantVolume
}
else {
return counter
}
}

func clock() {
if testDisable {
return
}
if wantRestart {
wantRestart = false
counter = 15
divider.resetCounter();
}
else {
if divider.clock() {
if counter > 0 {
counter = counter - 1
}
else if loop {
counter = 15
}
}
}
}

var loop = false
var wantRestart = true
var counter: UInt16 = 0
var constantVolumeMode: Bool = false
var constantVolume: UInt16 = 0
let divider = ChannelComponent.Divider()
}
}

class BaseChannel {
Expand Down
8 changes: 6 additions & 2 deletions Shared/NES/Apu/Channel/NoiseChannel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
class NoiseChannel: BaseChannel {
override func getValue() -> Float32 {
if shiftRegister.silenceChannel() || lengthCounter.silenceChannel() {
return 0;
return 0
}

return Float32(volumeEnvelope.getVolume())
Expand All @@ -36,6 +36,9 @@ class NoiseChannel: BaseChannel {
lengthCounter.setHalt(testBits(target: UInt16(BIT(5)), value: value))
volumeEnvelope.setConstantVolumeMode(testBits(target: UInt16(BIT(4)), value: value))
volumeEnvelope.setConstantVolume(readBits(target: BITS16([0, 1, 2, 3]), value: value))

//Side effect
volumeEnvelope.restart()
break

case 0x400E:
Expand All @@ -45,6 +48,7 @@ class NoiseChannel: BaseChannel {

case 0x400F:
lengthCounter.loadCounterFromLUT(value >> 3)
//Side effect
volumeEnvelope.restart()
break

Expand All @@ -60,7 +64,7 @@ class NoiseChannel: BaseChannel {
}

let ntscPeriods:[UInt16] = [ 4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4068 ]
let volumeEnvelope = VolumeEnvelope()
let volumeEnvelope = ChannelComponent.VolumeEnvelope()
let shiftRegister = LinearFeedbackShiftRegister()
}

Expand Down
Loading

0 comments on commit cc38c35

Please sign in to comment.