Skip to content

Commit

Permalink
implement MMC1.
Browse files Browse the repository at this point in the history
  • Loading branch information
miochen1226 committed Oct 9, 2023
1 parent 7185bc5 commit 6f7ca42
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 5 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,90 @@
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "950CC3D5-8506-4D9E-9016-6C7AD3469A23"
uuid = "B498D3E7-53C4-4221-8E2B-3BBCD0B8AD8C"
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/NES/PPU/Ppu.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "1229"
endingLineNumber = "1229">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "A1E72C46-D98A-4186-BAAC-C49BF04A2DEC"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/UI/GameScene.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "187"
endingLineNumber = "187">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "BCE641D7-7956-4DF7-8BF6-E2B63D404DE3"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/NES/PPU/Ppu.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "164"
endingLineNumber = "164"
landmarkName = "handleCpuWrite(_:value:)"
startingLineNumber = "1258"
endingLineNumber = "1258">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "0720870A-9115-43F0-B2A2-935A23656D43"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/NES/Cartridge/Mapper/Mapper.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "31"
endingLineNumber = "31"
landmarkName = "CanWriteSavMemory()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "C9235B1B-2E64-4552-85B2-D4EE3CE2EFB1"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/NES/Cartridge/Cartridge.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "30"
endingLineNumber = "30"
landmarkName = "handlePpuWrite(_:value:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
uuid = "6AAF7B89-632D-47EF-840E-03B2D24F9974"
shouldBeEnabled = "Yes"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Shared/NES/Cartridge/Cartridge.swift"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "208"
endingLineNumber = "208"
landmarkName = "loadRom()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
Expand Down
Binary file not shown.
19 changes: 18 additions & 1 deletion Shared/NES/Cartridge/Cartridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,15 @@ class Cartridge: ICartridge {
//let filepath = bundleUrl!.appendingPathComponent("Super Mario Bros. (Japan, USA).nes")
//let filepath = bundleUrl!.appendingPathComponent("Donkey Kong (Japan).nes")
//let filepath = bundleUrl!.appendingPathComponent("Ice Climber (Japan).nes")
let filepath = bundleUrl!.appendingPathComponent("Super Mario Bros. 3 (USA).nes")
//let filepath = bundleUrl!.appendingPathComponent("Super Mario Bros. 3 (USA).nes")
//Takahashi Meijin no Bouken-jima (Japan).nes
//let filepath = bundleUrl!.appendingPathComponent("Blaster Master (USA).nes")


let filepath = bundleUrl!.appendingPathComponent("Chou-Wakusei Senki - MetaFight (J).nes")
//let filepath = bundleUrl!.appendingPathComponent("Super Mario Bros. 3 (USA).nes")
//Chou-Wakusei Senki - MetaFight (J)

//if let filepath = Bundle.main.path(forResource: "Donkey Kong (Japan)", ofType: "nes")
//if let filepath = Bundle.main.path(forResource: "Circus Charlie (J) [p1]", ofType: "nes")
//if let filepath = Bundle.main.path(forResource: "Ice Climber (Japan)", ofType: "nes")
Expand Down Expand Up @@ -188,6 +196,10 @@ class Cartridge: ICartridge {
mapper = Mapper0()
mapper.Initialize(numPrgBanks: UInt8(numPrgBanks), numChrBanks: UInt8(numChrBanks), numSavBanks: UInt8(numSavBanks))
}
else if mN == 1 {
mapper = Mapper1()
mapper.Initialize(numPrgBanks: UInt8(numPrgBanks), numChrBanks: UInt8(numChrBanks), numSavBanks: UInt8(numSavBanks))
}
else if mN == 4 {
mapper = Mapper4()
mapper.Initialize(numPrgBanks: UInt8(numPrgBanks), numChrBanks: UInt8(numChrBanks), numSavBanks: UInt8(numSavBanks))
Expand All @@ -203,6 +215,11 @@ class Cartridge: ICartridge {
}

func getNameTableMirroring() -> NameTableMirroring {
let result = mapper.GetNameTableMirroring()
if result != NameTableMirroring.Undefined {
return result
}

return cartNameTableMirroring
}

Expand Down
8 changes: 8 additions & 0 deletions Shared/NES/Cartridge/Mapper/Mapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class Mapper {
nametableMirroring = value
}

func GetNameTableMirroring() -> NameTableMirroring {
return nametableMirroring
}

func SetPrgBankIndex16k(cpuBankIndexIn:Int, cartBankIndexIn: UInt8) {
let cpuBankIndex = cpuBankIndexIn * 4
let cartBankIndex = cartBankIndexIn * 4
Expand Down Expand Up @@ -233,6 +237,10 @@ class Mapper {
chrBankIndices[ppuBankIndex_ + 7] = cartBankIndex_ + 7;
}

func PrgMemorySize() -> UInt {
return UInt(numPrgBanks) * Mapper.kPrgBankSize
}

static var kPrgBankCount:UInt = 8
static var kPrgBankSize:UInt = KB(4)
static var kChrBankCount:UInt = 8
Expand Down
208 changes: 208 additions & 0 deletions Shared/NES/Cartridge/Mapper/Mapper1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,214 @@
//

import Foundation
class LoadRegister {

init() {
Reset()
}

//func LoadRegister() {
// Reset()
//}

func Reset() {
m_value.clearAll()
m_bitsWritten = 0
}

func SetBit(bit:UInt8) {

//"All bits already written, must Reset"
assert(m_bitsWritten < 5)

var enable:UInt8 = 0
if (bit & 0x01) != 0 {
enable = 1
}

m_value.setPos(bitPos: m_bitsWritten, enabled: enable)
//m_value.SetPos(m_bitsWritten, (bit & 0x01) != 0);
m_bitsWritten += 1
}

func AllBitsSet() -> Bool {
return m_bitsWritten == 5
}

func Value() -> UInt8 {
return m_value.value()
}

var m_bitsWritten:UInt8 = 0
var m_value:Bitfield8 = Bitfield8()
}

class Mapper1: Mapper {

override func OnCpuWrite(cpuAddress:UInt16, value:UInt8) {
if (cpuAddress < 0x8000) {
return
}

let reset:Bool = (value & BIT(7)) != 0

if reset {
m_loadReg.Reset()
m_controlReg.set(BITS([2,3]))
}
else {
let dataBit:UInt8 = value & BIT(0)
m_loadReg.SetBit(bit: dataBit)

if m_loadReg.AllBitsSet() {
switch (cpuAddress & 0xE000) {
case 0x8000:
m_controlReg.setValue(m_loadReg.Value())
UpdatePrgBanks()
UpdateChrBanks()
UpdateMirroring()

print("Load register")
break

case 0xA000:
let value = m_loadReg.Value()
m_chrReg0.setValue(m_loadReg.Value())

// Hijacks CHR reg bit 4 to select PRG 256k bank
if (m_boardType == BoardType.SUROM) {
UpdatePrgBanks()
}

UpdateChrBanks()
break

case 0xC000:
let value = m_loadReg.Value()
m_chrReg1.setValue(m_loadReg.Value())
//UpdateChrBanks()
break;

case 0xE000:
let value = m_loadReg.Value()
m_prgReg.setValue(m_loadReg.Value())
UpdatePrgBanks()
break

default:
assert(false)
break
}

m_loadReg.Reset()
}
}
}

var m_controlReg:Bitfield8 = Bitfield8()
var m_chrReg0:Bitfield8 = Bitfield8()
var m_chrReg1:Bitfield8 = Bitfield8()
var m_prgReg:Bitfield8 = Bitfield8()

enum BoardType :UInt8{
case DEFAULT
case SUROM
}
var m_boardType:BoardType = BoardType.DEFAULT

var m_loadReg:LoadRegister = LoadRegister()
override func postInitialize() {
m_boardType = BoardType.DEFAULT

if (PrgMemorySize() == KB(512)) {
m_boardType = BoardType.SUROM
}

m_loadReg.Reset()

m_controlReg.setValue(BITS([2,3]))
m_chrReg0.clearAll()
m_chrReg1.clearAll()
m_prgReg.clearAll()

UpdatePrgBanks()
UpdateChrBanks()
UpdateMirroring()
}

func UpdatePrgBanks() {
let bankMode = m_controlReg.read(BITS([2,3])) >> 2

// 32k mode
if (bankMode <= 1) {
let mask = NumPrgBanks32k() - 1
let cartBankIndex = (m_prgReg.read(BITS([0,1,2,3])) >> 1) & mask

SetPrgBankIndex32k(cpuBankIndexIn: 0, cartBankIndexIn: cartBankIndex)
}
// 16k mode
else {
var mask:UInt8 = NumPrgBanks16k() - 1
if mask > 16 {
mask = 16
assert(true)
print("ERROR")
}
var cartBankIndex = m_prgReg.read(BITS([0,1,2,3])) & mask
var firstBankIndex:UInt8 = 0
var lastBankIndex = (NumPrgBanks16k() - 1) & mask

if m_boardType == BoardType.SUROM {
let prgBankSelect256k = m_chrReg0.read(BIT(4))
cartBankIndex |= prgBankSelect256k
firstBankIndex |= prgBankSelect256k
lastBankIndex |= prgBankSelect256k
}

if bankMode == 2 {
SetPrgBankIndex16k(cpuBankIndexIn: 0, cartBankIndexIn: firstBankIndex)
SetPrgBankIndex16k(cpuBankIndexIn: 1, cartBankIndexIn: cartBankIndex)
}
else {
SetPrgBankIndex16k(cpuBankIndexIn: 0, cartBankIndexIn: cartBankIndex)
SetPrgBankIndex16k(cpuBankIndexIn: 1, cartBankIndexIn: lastBankIndex)
}
}

let bSavRamChipEnabled = m_prgReg.readPos(4) == 0
SetCanWriteSavMemory(bSavRamChipEnabled)
}

func UpdateChrBanks() {
let mode8k:Bool = m_controlReg.readPos(4) == 0

if mode8k {
let mask = NumChrBanks8k() - 1

let value = (m_chrReg0.value() >> 1) & mask
SetChrBankIndex8k(ppuBankIndexIn: 0, cartBankIndexIn: value)
}
else {
let mask = NumChrBanks4k() - 1
let value_0 = m_chrReg0.value() & mask
let value_1 = m_chrReg1.value() & mask
SetChrBankIndex4k(ppuBankIndex: 0, cartBankIndex: value_0)
SetChrBankIndex4k(ppuBankIndex: 1, cartBankIndex: value_1)
}
}

func UpdateMirroring() {
let table:[NameTableMirroring] =
[
NameTableMirroring.OneScreenLower,
NameTableMirroring.OneScreenUpper,
NameTableMirroring.Vertical,
NameTableMirroring.Horizontal,
]

let mirroringType = m_controlReg.read(BITS([0,1]))

print("mirroringType->"+String(mirroringType))
SetNameTableMirroring(table[Int(mirroringType)])
}
}

0 comments on commit 6f7ca42

Please sign in to comment.