2017-07-20 06:50:28 +00:00
|
|
|
//
|
|
|
|
// MemoryInterface.swift
|
|
|
|
// FruitMachine
|
|
|
|
//
|
|
|
|
// Created by Christopher Rohl on 7/20/17.
|
|
|
|
// Copyright © 2017 Christopher Rohl. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
class MemoryInterface: NSObject {
|
2017-07-22 02:13:53 +00:00
|
|
|
|
|
|
|
fileprivate var memory: [UInt8]
|
2017-07-20 06:50:28 +00:00
|
|
|
|
2017-07-26 04:44:53 +00:00
|
|
|
var read_overrides: [ReadOverride]
|
|
|
|
var write_overrides: [WriteOverride]
|
|
|
|
|
2017-07-20 06:50:28 +00:00
|
|
|
override init() {
|
|
|
|
memory = [UInt8](repeating: 0x00, count: 65536)
|
2017-07-26 04:44:53 +00:00
|
|
|
read_overrides = [ReadOverride]()
|
|
|
|
write_overrides = [WriteOverride]()
|
2017-07-20 06:50:28 +00:00
|
|
|
}
|
2017-07-21 07:25:48 +00:00
|
|
|
|
|
|
|
func readByte(offset: UInt16) -> UInt8 {
|
2017-07-26 04:44:53 +00:00
|
|
|
for override in read_overrides {
|
|
|
|
if case override.rangeStart ... override.rangeEnd = offset {
|
|
|
|
override.action(CPU.sharedInstance, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//No match.
|
2017-07-21 07:25:48 +00:00
|
|
|
return memory[Int(offset)]
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeByte(offset: UInt16, value: UInt8) {
|
2017-07-26 04:44:53 +00:00
|
|
|
for override in read_overrides {
|
|
|
|
if case override.rangeStart ... override.rangeEnd = offset {
|
|
|
|
override.action(CPU.sharedInstance, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 07:25:48 +00:00
|
|
|
memory[Int(offset)] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func readWord(offset: UInt16) -> UInt16 {
|
2017-07-22 07:11:14 +00:00
|
|
|
let low: UInt8 = memory[Int(offset)]
|
|
|
|
let high: UInt8 = memory[Int(offset+1)]
|
|
|
|
return (UInt16(high) << 8) | UInt16(low)
|
2017-07-21 07:25:48 +00:00
|
|
|
}
|
2017-07-22 02:13:53 +00:00
|
|
|
|
|
|
|
func loadBinary(path: String) {
|
|
|
|
do {
|
|
|
|
let fileContent: NSData = try NSData(contentsOfFile: path)
|
|
|
|
fileContent.getBytes(&memory, range: NSRange(location: 0, length: 65536))
|
|
|
|
} catch {
|
|
|
|
print(error)
|
|
|
|
}
|
|
|
|
}
|
2017-07-20 06:50:28 +00:00
|
|
|
}
|