From 2903eac409cf3024df68f5acb15842c0e7041594 Mon Sep 17 00:00:00 2001 From: Luigi Thirty Date: Wed, 26 Jul 2017 00:44:53 -0400 Subject: [PATCH] adding read and write override handlers i guess? --- FruitMachine.xcodeproj/project.pbxproj | 22 +++++++++++++++++- FruitMachine/DebuggerViewController.swift | 4 ++-- FruitMachine/M6502/CPU.swift | 3 +-- .../M6502/{ => Memory}/MemoryInterface.swift | 18 +++++++++++++++ .../M6502/Memory/MemoryOverride.swift | 23 +++++++++++++++++++ FruitMachine/M6502/Memory/ReadOverride.swift | 16 +++++++++++++ FruitMachine/M6502/Memory/WriteOverride.swift | 16 +++++++++++++ 7 files changed, 97 insertions(+), 5 deletions(-) rename FruitMachine/M6502/{ => Memory}/MemoryInterface.swift (62%) create mode 100644 FruitMachine/M6502/Memory/MemoryOverride.swift create mode 100644 FruitMachine/M6502/Memory/ReadOverride.swift create mode 100644 FruitMachine/M6502/Memory/WriteOverride.swift diff --git a/FruitMachine.xcodeproj/project.pbxproj b/FruitMachine.xcodeproj/project.pbxproj index b05db01..0831fb8 100644 --- a/FruitMachine.xcodeproj/project.pbxproj +++ b/FruitMachine.xcodeproj/project.pbxproj @@ -17,6 +17,9 @@ 2AD458E31F20661300F05121 /* CPUInstructions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AD458E21F20661300F05121 /* CPUInstructions.swift */; }; 2AD458E51F2070DF00F05121 /* Opcodes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AD458E41F2070DF00F05121 /* Opcodes.swift */; }; 2AD6D5841F26E6BF008F3CF5 /* DebuggerCommands.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AD6D5831F26E6BF008F3CF5 /* DebuggerCommands.swift */; }; + 2AE42E081F2850F400C4900E /* ReadOverride.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AE42E071F2850F400C4900E /* ReadOverride.swift */; }; + 2AE42E0A1F28521E00C4900E /* WriteOverride.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AE42E091F28521E00C4900E /* WriteOverride.swift */; }; + 2AE42E0C1F28522D00C4900E /* MemoryOverride.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AE42E0B1F28522D00C4900E /* MemoryOverride.swift */; }; 2AE5BA041F23DE4400FAA343 /* Disassembly.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AE5BA031F23DE4400FAA343 /* Disassembly.swift */; }; 2AE5BA061F2469EB00FAA343 /* AddressConversions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AE5BA051F2469EB00FAA343 /* AddressConversions.swift */; }; /* End PBXBuildFile section */ @@ -35,6 +38,9 @@ 2AD458E21F20661300F05121 /* CPUInstructions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CPUInstructions.swift; sourceTree = ""; }; 2AD458E41F2070DF00F05121 /* Opcodes.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Opcodes.swift; sourceTree = ""; }; 2AD6D5831F26E6BF008F3CF5 /* DebuggerCommands.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebuggerCommands.swift; sourceTree = ""; }; + 2AE42E071F2850F400C4900E /* ReadOverride.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadOverride.swift; sourceTree = ""; }; + 2AE42E091F28521E00C4900E /* WriteOverride.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WriteOverride.swift; sourceTree = ""; }; + 2AE42E0B1F28522D00C4900E /* MemoryOverride.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MemoryOverride.swift; sourceTree = ""; }; 2AE5BA031F23DE4400FAA343 /* Disassembly.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Disassembly.swift; sourceTree = ""; }; 2AE5BA051F2469EB00FAA343 /* AddressConversions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddressConversions.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -84,17 +90,28 @@ 2AD458DD1F205F0D00F05121 /* M6502 */ = { isa = PBXGroup; children = ( + 2AE42E061F2850E100C4900E /* Memory */, 2AD458DE1F205F4500F05121 /* CPU.swift */, 2AD458E21F20661300F05121 /* CPUInstructions.swift */, 2AE5BA031F23DE4400FAA343 /* Disassembly.swift */, 2AD458E41F2070DF00F05121 /* Opcodes.swift */, 2AE5BA051F2469EB00FAA343 /* AddressConversions.swift */, - 2AD458E01F2064CB00F05121 /* MemoryInterface.swift */, 2A22EBFA1F21A7A700A36A61 /* IntegerExtensions.swift */, ); path = M6502; sourceTree = ""; }; + 2AE42E061F2850E100C4900E /* Memory */ = { + isa = PBXGroup; + children = ( + 2AD458E01F2064CB00F05121 /* MemoryInterface.swift */, + 2AE42E071F2850F400C4900E /* ReadOverride.swift */, + 2AE42E091F28521E00C4900E /* WriteOverride.swift */, + 2AE42E0B1F28522D00C4900E /* MemoryOverride.swift */, + ); + path = Memory; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -168,11 +185,14 @@ 2AD458E31F20661300F05121 /* CPUInstructions.swift in Sources */, 2AD458D01F205EB700F05121 /* DebuggerViewController.swift in Sources */, 2AD458CE1F205EB700F05121 /* AppDelegate.swift in Sources */, + 2AE42E0C1F28522D00C4900E /* MemoryOverride.swift in Sources */, 2A22EBFB1F21A7A700A36A61 /* IntegerExtensions.swift in Sources */, 2AD6D5841F26E6BF008F3CF5 /* DebuggerCommands.swift in Sources */, 2AE5BA041F23DE4400FAA343 /* Disassembly.swift in Sources */, 2AD458E51F2070DF00F05121 /* Opcodes.swift in Sources */, + 2AE42E081F2850F400C4900E /* ReadOverride.swift in Sources */, 2AE5BA061F2469EB00FAA343 /* AddressConversions.swift in Sources */, + 2AE42E0A1F28521E00C4900E /* WriteOverride.swift in Sources */, 2AD458E11F2064CB00F05121 /* MemoryInterface.swift in Sources */, 2AD458DF1F205F4500F05121 /* CPU.swift in Sources */, ); diff --git a/FruitMachine/DebuggerViewController.swift b/FruitMachine/DebuggerViewController.swift index c365b5a..c455fa8 100644 --- a/FruitMachine/DebuggerViewController.swift +++ b/FruitMachine/DebuggerViewController.swift @@ -46,7 +46,7 @@ class DebuggerViewController: NSViewController { text_CPU_SR.stringValue = String(format:"%02X", cpuInstance.stack_pointer) text_CPU_Flags.stringValue = String(cpuInstance.status_register.asString()) - disassembly = cpuInstance.disassemble(fromAddress: 0, length: 40000) + disassembly = cpuInstance.disassemble(fromAddress: 0, length: 65535) highlightCurrentInstruction() } @@ -60,7 +60,7 @@ class DebuggerViewController: NSViewController { cpuInstance.performReset() cpuInstance.program_counter = 0x400 //entry point for the test program updateCPUStatusFields() - disassembly = cpuInstance.disassemble(fromAddress: 0, length: 65000) + disassembly = cpuInstance.disassemble(fromAddress: 0, length: 65535) debuggerTableView.reloadData() cpuInstance.breakpoints.append(0x34E8) //failing at $34FD SBC test diff --git a/FruitMachine/M6502/CPU.swift b/FruitMachine/M6502/CPU.swift index 843f417..b3f8068 100644 --- a/FruitMachine/M6502/CPU.swift +++ b/FruitMachine/M6502/CPU.swift @@ -156,8 +156,7 @@ class CPU: NSObject { //Branches incur a 1-cycle penalty if taken plus the page boundary penalty if necessary. branch_was_taken = false - breakpoints = [UInt16]() - + breakpoints = [UInt16]() } func getOperandByte() -> UInt8 { diff --git a/FruitMachine/M6502/MemoryInterface.swift b/FruitMachine/M6502/Memory/MemoryInterface.swift similarity index 62% rename from FruitMachine/M6502/MemoryInterface.swift rename to FruitMachine/M6502/Memory/MemoryInterface.swift index b899198..7425f73 100644 --- a/FruitMachine/M6502/MemoryInterface.swift +++ b/FruitMachine/M6502/Memory/MemoryInterface.swift @@ -12,15 +12,33 @@ class MemoryInterface: NSObject { fileprivate var memory: [UInt8] + var read_overrides: [ReadOverride] + var write_overrides: [WriteOverride] + override init() { memory = [UInt8](repeating: 0x00, count: 65536) + read_overrides = [ReadOverride]() + write_overrides = [WriteOverride]() } func readByte(offset: UInt16) -> UInt8 { + for override in read_overrides { + if case override.rangeStart ... override.rangeEnd = offset { + override.action(CPU.sharedInstance, nil) + } + } + + //No match. return memory[Int(offset)] } func writeByte(offset: UInt16, value: UInt8) { + for override in read_overrides { + if case override.rangeStart ... override.rangeEnd = offset { + override.action(CPU.sharedInstance, value) + } + } + memory[Int(offset)] = value } diff --git a/FruitMachine/M6502/Memory/MemoryOverride.swift b/FruitMachine/M6502/Memory/MemoryOverride.swift new file mode 100644 index 0000000..03a716d --- /dev/null +++ b/FruitMachine/M6502/Memory/MemoryOverride.swift @@ -0,0 +1,23 @@ +// +// MemoryOverride.swift +// FruitMachine +// +// Created by Christopher Rohl on 7/26/17. +// Copyright © 2017 Christopher Rohl. All rights reserved. +// + +import Cocoa + +class MemoryOverride: NSObject { + let rangeStart: UInt16 + let rangeEnd: UInt16 + + let action: (CPU, UInt8?) -> Void + + init(start: UInt16, end: UInt16, action: @escaping (CPU, UInt8?) -> Void) { + rangeStart = start + rangeEnd = end + + self.action = action + } +} diff --git a/FruitMachine/M6502/Memory/ReadOverride.swift b/FruitMachine/M6502/Memory/ReadOverride.swift new file mode 100644 index 0000000..852b20e --- /dev/null +++ b/FruitMachine/M6502/Memory/ReadOverride.swift @@ -0,0 +1,16 @@ +// +// ReadOverride.swift +// FruitMachine +// +// Created by Christopher Rohl on 7/26/17. +// Copyright © 2017 Christopher Rohl. All rights reserved. +// + +import Cocoa + +/* A ReadOverride is special behavior that occurs when a memory address is read. + Memory-mapped registers, peripherals, etc. */ + +class ReadOverride: MemoryOverride { + +} diff --git a/FruitMachine/M6502/Memory/WriteOverride.swift b/FruitMachine/M6502/Memory/WriteOverride.swift new file mode 100644 index 0000000..ec65521 --- /dev/null +++ b/FruitMachine/M6502/Memory/WriteOverride.swift @@ -0,0 +1,16 @@ +// +// WriteOverride.swift +// FruitMachine +// +// Created by Christopher Rohl on 7/26/17. +// Copyright © 2017 Christopher Rohl. All rights reserved. +// + +import Cocoa + +/* A ReadOverride is special behavior that occurs when a memory address is written. + Memory-mapped registers, peripherals, etc. */ + +class WriteOverride: MemoryOverride { + +}