Skip to content

Commit

Permalink
code refector
Browse files Browse the repository at this point in the history
  • Loading branch information
miochen1226 committed Oct 17, 2023
1 parent 359678e commit baeba62
Show file tree
Hide file tree
Showing 27 changed files with 300 additions and 316 deletions.
30 changes: 12 additions & 18 deletions NES_EMU.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
3530B88826C7FBE600A2858C /* ICpu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3530B88626C7FBE600A2858C /* ICpu.swift */; };
3530B88E26C7FC4200A2858C /* ICartridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3530B88D26C7FC4200A2858C /* ICartridge.swift */; };
3530B88F26C7FC4200A2858C /* ICartridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3530B88D26C7FC4200A2858C /* ICartridge.swift */; };
3530B89426C7FE4B00A2858C /* OpCodeDef.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3530B89326C7FE4B00A2858C /* OpCodeDef.swift */; };
3530B89526C7FE4B00A2858C /* OpCodeDef.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3530B89326C7FE4B00A2858C /* OpCodeDef.swift */; };
3530B89D26C8006100A2858C /* CpuRegDef.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3530B89C26C8006100A2858C /* CpuRegDef.swift */; };
3530B89E26C8006100A2858C /* CpuRegDef.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3530B89C26C8006100A2858C /* CpuRegDef.swift */; };
3530FA3326BF6C8B00CF18DD /* Nes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3530FA3226BF6C8B00CF18DD /* Nes.swift */; };
Expand Down Expand Up @@ -106,8 +104,8 @@
356CF0902A87BFD70093A24F /* GameScene+GameController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 356CF08E2A87BFD70093A24F /* GameScene+GameController.swift */; };
356CF0922A87C0BF0093A24F /* GameScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 356CF0912A87C0BF0093A24F /* GameScene.swift */; };
356CF0932A87C0BF0093A24F /* GameScene.swift in Sources */ = {isa = PBXBuildFile; fileRef = 356CF0912A87C0BF0093A24F /* GameScene.swift */; };
356CF0952A87FF780093A24F /* PpuControl2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 356CF0942A87FF780093A24F /* PpuControl2.swift */; };
356CF0962A87FF780093A24F /* PpuControl2.swift in Sources */ = {isa = PBXBuildFile; fileRef = 356CF0942A87FF780093A24F /* PpuControl2.swift */; };
356CF0952A87FF780093A24F /* PpuMask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 356CF0942A87FF780093A24F /* PpuMask.swift */; };
356CF0962A87FF780093A24F /* PpuMask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 356CF0942A87FF780093A24F /* PpuMask.swift */; };
35714DD22AD3E5FB004A7A3F /* DmcChannel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35714DD12AD3E5FB004A7A3F /* DmcChannel.swift */; };
35714DD32AD3E5FB004A7A3F /* DmcChannel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35714DD12AD3E5FB004A7A3F /* DmcChannel.swift */; };
358D403526C66CEB005AAAD8 /* SpriteFetchData.swift in Sources */ = {isa = PBXBuildFile; fileRef = 358D403426C66CEB005AAAD8 /* SpriteFetchData.swift */; };
Expand All @@ -116,8 +114,8 @@
3592DB1E2AD96E51003D52AD /* IApu.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3592DB1C2AD96E51003D52AD /* IApu.swift */; };
35B78E2E2A86091100B1E42F /* Mapper4.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35B78E2D2A86091100B1E42F /* Mapper4.swift */; };
35B78E2F2A86091100B1E42F /* Mapper4.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35B78E2D2A86091100B1E42F /* Mapper4.swift */; };
35B9C57926C28F9A00E13D57 /* PpuControl1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35B9C57826C28F9A00E13D57 /* PpuControl1.swift */; };
35B9C57A26C28F9A00E13D57 /* PpuControl1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35B9C57826C28F9A00E13D57 /* PpuControl1.swift */; };
35B9C57926C28F9A00E13D57 /* PpuControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35B9C57826C28F9A00E13D57 /* PpuControl.swift */; };
35B9C57A26C28F9A00E13D57 /* PpuControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35B9C57826C28F9A00E13D57 /* PpuControl.swift */; };
35B9C57E26C291C700E13D57 /* PpuMemory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35B9C57D26C291C700E13D57 /* PpuMemory.swift */; };
35B9C57F26C291C700E13D57 /* PpuMemory.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35B9C57D26C291C700E13D57 /* PpuMemory.swift */; };
35B9C58326C29A5400E13D57 /* PpuMemoryBus.swift in Sources */ = {isa = PBXBuildFile; fileRef = 35B9C58226C29A5400E13D57 /* PpuMemoryBus.swift */; };
Expand Down Expand Up @@ -160,7 +158,6 @@
3530B87F26C7FAC400A2858C /* IPpu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IPpu.swift; sourceTree = "<group>"; };
3530B88626C7FBE600A2858C /* ICpu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ICpu.swift; sourceTree = "<group>"; };
3530B88D26C7FC4200A2858C /* ICartridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ICartridge.swift; sourceTree = "<group>"; };
3530B89326C7FE4B00A2858C /* OpCodeDef.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OpCodeDef.swift; sourceTree = "<group>"; };
3530B89C26C8006100A2858C /* CpuRegDef.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CpuRegDef.swift; sourceTree = "<group>"; };
3530FA3226BF6C8B00CF18DD /* Nes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Nes.swift; sourceTree = "<group>"; };
3530FA3726BF6DCF00CF18DD /* Cpu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Cpu.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -192,12 +189,12 @@
356CF08B2A87935C0093A24F /* AudioController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AudioController.swift; sourceTree = "<group>"; };
356CF08E2A87BFD70093A24F /* GameScene+GameController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "GameScene+GameController.swift"; sourceTree = "<group>"; };
356CF0912A87C0BF0093A24F /* GameScene.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GameScene.swift; sourceTree = "<group>"; };
356CF0942A87FF780093A24F /* PpuControl2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PpuControl2.swift; sourceTree = "<group>"; };
356CF0942A87FF780093A24F /* PpuMask.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PpuMask.swift; sourceTree = "<group>"; };
35714DD12AD3E5FB004A7A3F /* DmcChannel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DmcChannel.swift; sourceTree = "<group>"; };
358D403426C66CEB005AAAD8 /* SpriteFetchData.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SpriteFetchData.swift; sourceTree = "<group>"; };
3592DB1C2AD96E51003D52AD /* IApu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IApu.swift; sourceTree = "<group>"; };
35B78E2D2A86091100B1E42F /* Mapper4.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Mapper4.swift; sourceTree = "<group>"; };
35B9C57826C28F9A00E13D57 /* PpuControl1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PpuControl1.swift; sourceTree = "<group>"; };
35B9C57826C28F9A00E13D57 /* PpuControl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PpuControl.swift; sourceTree = "<group>"; };
35B9C57D26C291C700E13D57 /* PpuMemory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PpuMemory.swift; sourceTree = "<group>"; };
35B9C58226C29A5400E13D57 /* PpuMemoryBus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PpuMemoryBus.swift; sourceTree = "<group>"; };
35B9C5A126C3191600E13D57 /* Mapper0.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Mapper0.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -386,8 +383,8 @@
isa = PBXGroup;
children = (
3530FA4526BF70BB00CF18DD /* Ppu.swift */,
35B9C57826C28F9A00E13D57 /* PpuControl1.swift */,
356CF0942A87FF780093A24F /* PpuControl2.swift */,
35B9C57826C28F9A00E13D57 /* PpuControl.swift */,
356CF0942A87FF780093A24F /* PpuMask.swift */,
35B9C57D26C291C700E13D57 /* PpuMemory.swift */,
35B9C58226C29A5400E13D57 /* PpuMemoryBus.swift */,
35265B3F26C566540031E1A6 /* Bitfield8WithPpuRegister.swift */,
Expand Down Expand Up @@ -446,7 +443,6 @@
3530B89226C7FE3F00A2858C /* Define */ = {
isa = PBXGroup;
children = (
3530B89326C7FE4B00A2858C /* OpCodeDef.swift */,
3530B89C26C8006100A2858C /* CpuRegDef.swift */,
);
path = Define;
Expand Down Expand Up @@ -569,11 +565,11 @@
3547C22D26BECE62008DB9B9 /* Memory.swift in Sources */,
3530B88026C7FAC400A2858C /* IPpu.swift in Sources */,
3530B89D26C8006100A2858C /* CpuRegDef.swift in Sources */,
356CF0952A87FF780093A24F /* PpuControl2.swift in Sources */,
356CF0952A87FF780093A24F /* PpuMask.swift in Sources */,
3505EAB52A6F83AE00B14943 /* BaseChannel.swift in Sources */,
3530FA3326BF6C8B00CF18DD /* Nes.swift in Sources */,
3505EABB2A6F89F300B14943 /* NoiseChannel.swift in Sources */,
35B9C57926C28F9A00E13D57 /* PpuControl1.swift in Sources */,
35B9C57926C28F9A00E13D57 /* PpuControl.swift in Sources */,
352575F726BD4829005AB816 /* ContentView.swift in Sources */,
3547C22826BEBDB0008DB9B9 /* BaseDef.swift in Sources */,
35265B4026C566540031E1A6 /* Bitfield8WithPpuRegister.swift in Sources */,
Expand All @@ -595,7 +591,6 @@
35B9C57E26C291C700E13D57 /* PpuMemory.swift in Sources */,
3530FA5726BF880E00CF18DD /* Mapper.swift in Sources */,
35714DD22AD3E5FB004A7A3F /* DmcChannel.swift in Sources */,
3530B89426C7FE4B00A2858C /* OpCodeDef.swift in Sources */,
3538AD5A2AD91F0000787E9A /* Nes+GetSprite.swift in Sources */,
3539627C2A83619800CF1E97 /* VirtualConteollerView.swift in Sources */,
3530FA6126C0590300CF18DD /* OpCodeTable.swift in Sources */,
Expand Down Expand Up @@ -630,7 +625,7 @@
files = (
3530FA5326BF7A6B00CF18DD /* OpCodeEntry.swift in Sources */,
3547C22E26BECE62008DB9B9 /* Memory.swift in Sources */,
356CF0962A87FF780093A24F /* PpuControl2.swift in Sources */,
356CF0962A87FF780093A24F /* PpuMask.swift in Sources */,
35DEF54E2A84FF9D00AE8149 /* OAM.swift in Sources */,
3530B88126C7FAC400A2858C /* IPpu.swift in Sources */,
3530B89E26C8006100A2858C /* CpuRegDef.swift in Sources */,
Expand All @@ -639,7 +634,7 @@
3505EAB62A6F83AE00B14943 /* BaseChannel.swift in Sources */,
3530FA3426BF6C8B00CF18DD /* Nes.swift in Sources */,
3505EABC2A6F89F300B14943 /* NoiseChannel.swift in Sources */,
35B9C57A26C28F9A00E13D57 /* PpuControl1.swift in Sources */,
35B9C57A26C28F9A00E13D57 /* PpuControl.swift in Sources */,
352575F826BD4829005AB816 /* ContentView.swift in Sources */,
3547C22926BEBDB0008DB9B9 /* BaseDef.swift in Sources */,
35265B4126C566540031E1A6 /* Bitfield8WithPpuRegister.swift in Sources */,
Expand All @@ -657,7 +652,6 @@
35B9C57F26C291C700E13D57 /* PpuMemory.swift in Sources */,
3530FA5826BF880E00CF18DD /* Mapper.swift in Sources */,
356883212ADA377C0009AE7C /* IRenderer.swift in Sources */,
3530B89526C7FE4B00A2858C /* OpCodeDef.swift in Sources */,
3530FA6226C0590300CF18DD /* OpCodeTable.swift in Sources */,
3538AD5E2AD91FA200787E9A /* Nes+SaveState.swift in Sources */,
352AFFE52A6E6DD100F811F8 /* Apu.swift in Sources */,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,37 @@
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "F2DB7122-C24C-4F67-A945-286E95EBA2D5"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/NES/Apu/Apu.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "61"
endingLineNumber = "61"
landmarkName = "handleCpuRead(_:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "0BFC8264-6162-4F0C-B1E7-9040108697D3"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/NES/PPU/Ppu.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "476"
endingLineNumber = "476"
landmarkName = "readPpuRegister(_:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
67 changes: 37 additions & 30 deletions Shared/NES/Apu/Apu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,45 @@ extension Apu: IApu {
break
}
}

func enableSound() {
pulseChannel0?.getLengthCounter().setEnabled(true)
pulseChannel1?.getLengthCounter().setEnabled(true)
triangleChannel?.getLengthCounter().setEnabled(true)
noiseChannel?.getLengthCounter().setEnabled(true)

}
}

class Apu: NSObject {
enum ApuChannel {
case PulseChannel1
case PulseChannel2
case TriangleChannel
case NoiseChannel
case DmcChannel
}

static let kAvgNumScreenPpuCycles:Float32 = 89342 - 0.5
static let kCpuCyclesPerSec:Float32 = (kAvgNumScreenPpuCycles / 3) * 60.0
static let sampleRate: Double = 44100
static let kCpuCyclesPerSample:Float32 = Apu.kCpuCyclesPerSec/Float(sampleRate)


var audioDriver: AudioDriver?
var frameCounter: FrameCounter?

var pulseChannel0: PulseChannel?
var pulseChannel1: PulseChannel?
var triangleChannel: TriangleChannel?
var noiseChannel: NoiseChannel?
var dmcChannel:DmcChannel?
var elapsedCpuCycles: Float = 0
var sampleSum = 0
var numSamples = 0
var channelVolumes:[ApuChannel:Float32] = [:]
var evenFrame = false

func stopPlayer() {
audioDriver?.audioUnitPlayer.stop()
}
Expand Down Expand Up @@ -184,8 +220,7 @@ class Apu: NSObject {

channelVolume = channelVolumes[ApuChannel.PulseChannel1] ?? 0

switch (channel)
{
switch channel {
case .PulseChannel1:
channelValue = pulseChannel0?.getValue() ?? 0
break
Expand Down Expand Up @@ -225,32 +260,4 @@ class Apu: NSObject {
let interrupt = frameCounter?.enableIRQ ?? 0
return interrupt
}

enum ApuChannel {
case PulseChannel1
case PulseChannel2
case TriangleChannel
case NoiseChannel
case DmcChannel
}

static let kAvgNumScreenPpuCycles:Float32 = 89342 - 0.5
static let kCpuCyclesPerSec:Float32 = (kAvgNumScreenPpuCycles / 3) * 60.0
static let sampleRate: Double = 44100
static let kCpuCyclesPerSample:Float32 = Apu.kCpuCyclesPerSec/Float(sampleRate)


var audioDriver: AudioDriver?
var frameCounter: FrameCounter?

var pulseChannel0: PulseChannel?
var pulseChannel1: PulseChannel?
var triangleChannel: TriangleChannel?
var noiseChannel: NoiseChannel?
var dmcChannel:DmcChannel?
var elapsedCpuCycles: Float = 0
var sampleSum = 0
var numSamples = 0
var channelVolumes:[ApuChannel:Float32] = [:]
var evenFrame = false
}
18 changes: 8 additions & 10 deletions Shared/NES/Apu/AudioDriver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
import AVFAudio
import AVFoundation

class NesFrameProvider : FrameProvider{
class NesFrameProvider : IFrameProvider{
var frames:[Float32] = []
let lockInput = NSLock()

Expand All @@ -19,16 +19,14 @@ class NesFrameProvider : FrameProvider{
lockInput.unlock()
}

func dequeue(byteSize: UInt32)-> FrameObj {
let frameObj = FrameObj()
func dequeue(byteSize: UInt32)-> AudioFrameObj {
let frameObj = AudioFrameObj()
let floatCount:Int = Int(byteSize/4)

lockInput.lock()

if(frames.count >= byteSize/4)
{
for _ in 0..<floatCount
{
if frames.count >= byteSize/4 {
for _ in 0..<floatCount {
frameObj.arrayFloat.append(frames.removeFirst())
}

Expand All @@ -40,22 +38,22 @@ class NesFrameProvider : FrameProvider{
return frameObj
}

func getNextFrame(_ byteSize: UInt32) -> FrameObj {
func getNextFrame(_ byteSize: UInt32) -> AudioFrameObj {
return dequeue(byteSize:byteSize)
}
}

class AudioDriver: NSObject {
let nesFrameProvider = NesFrameProvider()
var audioUnitPlayer: AudioUnitPlayer!

required override init() {
super.init()
audioUnitPlayer = AudioUnitPlayer(frameProvider: nesFrameProvider)
audioUnitPlayer.start()
}

func enqueue(inputFrame: Float32)
{
func enqueue(inputFrame: Float32) {
nesFrameProvider.enqueue(inputFrame: inputFrame)
}
}
1 change: 1 addition & 0 deletions Shared/NES/Apu/IApu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ protocol IApu: HandleCpuReadWriteProtocol {
func execute(_ cpuCycles: UInt32)
func startPlayer()
func stopPlayer()
func enableSound()
}
36 changes: 10 additions & 26 deletions Shared/NES/AudioDriveImp/AudioController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import Foundation
import AVFoundation

class AudioController: NSObject
{
class AudioController: NSObject {
static var sharedInstance = AudioController()
var remoteIOUnit: AudioComponentInstance!
var frameProvider: IFrameProvider?

func start() {
AudioOutputUnitStart(remoteIOUnit)
}
Expand All @@ -18,8 +21,7 @@ class AudioController: NSObject
AudioOutputUnitStop(remoteIOUnit)
}

func getAudioUnitSubType()->UInt32
{
func getAudioUnitSubType() -> UInt32 {
#if os(iOS)
return kAudioUnitSubType_RemoteIO
#else
Expand All @@ -28,10 +30,7 @@ class AudioController: NSObject
}

func initAudioComponent() {

let audioUnitSubType:UInt32 = getAudioUnitSubType()


let audioUnitSubType: UInt32 = getAudioUnitSubType()
var audioComponentDesc = AudioComponentDescription(componentType: OSType(kAudioUnitType_Output),
componentSubType: OSType(audioUnitSubType),
componentManufacturer: OSType(kAudioUnitManufacturer_Apple),
Expand Down Expand Up @@ -83,15 +82,6 @@ class AudioController: NSObject
if frameObj!.isFloat {
let floatCount = Int(frameObj?.countFloat ?? 0)
if floatCount==0 {
/*
var empty:[Float32] = []
for _ in 0...reqByteSize-1
{
empty.append(0)
}
abl?[0].mDataByteSize = 10
memcpy(abl?[0].mData, &empty, Int(reqByteSize))
*/
return 0
}

Expand All @@ -101,15 +91,13 @@ class AudioController: NSObject
}
else {
let byteCount = Int(frameObj?.byteCount ?? 0)
if(byteCount==0)
{
if byteCount==0 {
return 0
}

let pointer = UnsafeRawPointer(frameObj!.buffer!)
var randBuffer:[Float32] = []
for index in 0..<byteCount/4
{
for index in 0..<byteCount/4 {
let floatValue = pointer.load(fromByteOffset: index*4, as: Float32.self)
randBuffer.append(floatValue)
}
Expand All @@ -127,12 +115,8 @@ class AudioController: NSObject
}
}

func setUp(frameProvider: FrameProvider) {
func setUp(frameProvider: IFrameProvider) {
self.frameProvider = frameProvider
initAudioComponent()
}

static var sharedInstance = AudioController()
var remoteIOUnit: AudioComponentInstance!
var frameProvider: FrameProvider?
}
Loading

0 comments on commit baeba62

Please sign in to comment.