Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target/
roms/
.mooncakes/
.moonbit-lsp.json
.moonbit-lsp.json
_build/
22 changes: 11 additions & 11 deletions lib/apu.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -291,19 +291,19 @@ fn APU::writeControl(self : APU, val : UInt8) -> Unit {
self.triangle.enabled = (value & 4) == 4
self.noise.enabled = (value & 8) == 8
self.dmc.enabled = (value & 16) == 16
if not(self.pulse[0].enabled) {
if !self.pulse[0].enabled {
self.pulse[0].lengthValue = 0
}
if not(self.pulse[1].enabled) {
if !self.pulse[1].enabled {
self.pulse[1].lengthValue = 0
}
if not(self.triangle.enabled) {
if !self.triangle.enabled {
self.triangle.lengthValue = 0
}
if not(self.noise.enabled) {
if !self.noise.enabled {
self.noise.lengthValue = 0
}
if not(self.dmc.enabled) {
if !self.dmc.enabled {
self.dmc.currentLength = 0
} else if self.dmc.currentLength == 0 {
self.dmc.restart()
Expand Down Expand Up @@ -405,7 +405,7 @@ fn Pulse::writeTimerLow(self : Pulse, val : UInt8) -> Unit {
fn Pulse::writeTimerHigh(self : Pulse, val : UInt8) -> Unit {
let value = val.to_int()
self.lengthValue = lengthTable[value >> 3] |> to_u8
self.timerPeriod = (self.timerPeriod & (0x00FF)) |
self.timerPeriod = (self.timerPeriod & 0x00FF) |
(((value & 7) << 8) |> to_u16) // CHECKME
self.envelopeStart = true
self.dutyValue = 0
Expand Down Expand Up @@ -479,7 +479,7 @@ fn Pulse::sweep(self : Pulse) -> Unit {

///|
fn Pulse::output(self : Pulse) -> UInt8 {
if not(self.enabled) {
if !self.enabled {
return 0
}
if self.lengthValue == 0 {
Expand Down Expand Up @@ -545,7 +545,7 @@ fn Triangle::writeTimerLow(self : Triangle, val : UInt8) -> Unit {
fn Triangle::writeTimerHigh(self : Triangle, val : UInt8) -> Unit {
let value = val.to_int()
self.lengthValue = lengthTable[(value & 0xFF) >> 3] |> to_u8
self.timerPeriod = (self.timerPeriod & (0x00FF)) |
self.timerPeriod = (self.timerPeriod & 0x00FF) |
(((value & 7) << 8) |> to_u16) // CHECKME
self.timerValue = self.timerPeriod
self.counterReload = true
Expand Down Expand Up @@ -584,7 +584,7 @@ fn Triangle::stepCounter(self : Triangle) -> Unit {

///|
fn Triangle::output(self : Triangle) -> UInt8 {
if not(self.enabled) {
if !self.enabled {
return 0
}
if self.timerPeriod < 3 {
Expand Down Expand Up @@ -704,7 +704,7 @@ fn Noise::stepLength(self : Noise) -> Unit {

///|
fn Noise::output(self : Noise) -> UInt8 {
if not(self.enabled) {
if !self.enabled {
return 0
}
if self.lengthValue == 0 {
Expand Down Expand Up @@ -791,7 +791,7 @@ fn DMC::restart(self : DMC) -> Unit {

///|
fn DMC::stepTimer(self : DMC) -> Unit {
if not(self.enabled) {
if !self.enabled {
return
}
self.stepReader()
Expand Down
8 changes: 6 additions & 2 deletions lib/console.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ fn NES::step(self : NES, trace_log : Bool) -> Int {

///|
fn NES::run_a_while(self : NES, seconds : Double) -> Unit {
loop (cpuFrequency * seconds).to_int() {
cycles => if cycles > 0 { continue cycles - self.step(false) }
for cycles = (cpuFrequency * seconds).to_int() {
if cycles > 0 {
continue cycles - self.step(false)
} else {
break
}
}
}
97 changes: 48 additions & 49 deletions lib/cpu.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ fn CPUMemory::op_get(self : CPUMemory, adr : UInt16) -> UInt8 {
let n = self.nes.unwrap()
if adr.to_int() >= 0x0000 && adr.to_int() <= 0x1FFF {
n.ram[adr.to_int() % 0x0800]
} else if adr >= (0x2000) && adr <= (0x3FFF) {
n.ppu.readRegister((0x2000) + ((adr.to_int() % 8) |> to_u16))
} else if adr >= 0x2000 && adr <= 0x3FFF {
n.ppu.readRegister(0x2000 + ((adr.to_int() % 8) |> to_u16))
} else if adr.to_int() == 0x4014 {
n.ppu.readRegister(adr)
} else if adr.to_int() == 0x4015 {
Expand All @@ -153,10 +153,7 @@ fn CPUMemory::op_set(self : CPUMemory, adr : UInt16, val : UInt8) -> Unit {
// log_string("[CPUMemory] set \(addr) to \(v)")
n.ram[adr.to_int() % 0x0800] = val
} else if adr.to_int() >= 0x2000 && adr.to_int() <= 0x3FFF {
n.ppu.writeRegister(
(0x2000) + ((adr.to_int() % 8) |> to_u16),
val,
)
n.ppu.writeRegister(0x2000 + ((adr.to_int() % 8) |> to_u16), val)
} else if adr.to_int() == 0x4014 {
n.ppu.writeRegister(adr, val)
} else if (adr.to_int() >= 0x4000 && adr.to_int() <= 0x4013) ||
Expand All @@ -183,22 +180,22 @@ fn CPUMemory::read16(self : CPUMemory, adr : UInt16) -> UInt16 {
fn CPUMemory::read16bug(self : CPUMemory, adr : UInt16) -> UInt16 {
// Low byte wraps without incrementing high byte
let a = adr
let b = (a & (0xFF00)) | (((adr |> to_u8()) + 1) |> to_u16)
let b = (a & 0xFF00) | (((adr |> to_u8()) + 1) |> to_u16)
let lo = self[a]
let hi = self[b]
((hi |> to_u16) << 8) | (lo |> to_u16)
}

///|
fn CPU::push(self : CPU, val : UInt8) -> Unit {
self.mem[(self.sp |> to_u16) | (0x100)] = val
self.mem[(self.sp |> to_u16) | 0x100] = val
self.sp = self.sp - 1
}

///|
fn CPU::pull(self : CPU) -> UInt8 {
self.sp = self.sp + 1
self.mem[(self.sp |> to_u16) | (0x100)]
self.mem[(self.sp |> to_u16) | 0x100]
}

///|
Expand All @@ -221,7 +218,7 @@ fn CPU::setZ(self : CPU, val : UInt8) -> Unit {

///|
fn CPU::setN(self : CPU, val : UInt8) -> Unit {
self.n = (val & (0x80)) != 0
self.n = (val & 0x80) != 0
}

///|
Expand Down Expand Up @@ -254,7 +251,7 @@ fn CPU::triggerNMI(self : CPU) -> Unit {

///|
fn CPU::triggerIRQ(self : CPU) -> Unit {
if not(self.i) {
if !self.i {
self.interrupt = Interrupt::IRQ
}
}
Expand Down Expand Up @@ -296,9 +293,9 @@ fn CPU::formatAssemblySuffix(self : CPU) -> String {
}
let (mem_addr_hex, stored_value_hex) = (
mem_addr.to_hex(),
stored_value.to_hex(),
stored_value.formatHex(),
)
let mem_addr8_hex = (mem_addr |> to_u8()).to_hex()
let mem_addr8_hex = (mem_addr |> to_u8()).formatHex()
match bytes {
1 =>
match opcode.to_int() {
Expand All @@ -307,7 +304,7 @@ fn CPU::formatAssemblySuffix(self : CPU) -> String {
}
2 => {
let address = self.mem[self.pc + 1]
let address_hex = address.to_hex()
let address_hex = address.formatHex()
match mode {
Immediate => "#$\{address_hex}"
ZeroPage => "$\{mem_addr8_hex} = \{stored_value_hex}"
Expand All @@ -317,7 +314,7 @@ fn CPU::formatAssemblySuffix(self : CPU) -> String {
"$\{address_hex},Y @ \{mem_addr8_hex} = \{stored_value_hex}"
// Indirect_X
IndexedIndirect => {
let new_addr_hex = (address + self.x).to_hex()
let new_addr_hex = (address + self.x).formatHex()
"($\{address_hex},X) @ \{new_addr_hex} = \{mem_addr_hex} = \{stored_value_hex}"
}
// Indirect_Y
Expand Down Expand Up @@ -378,8 +375,12 @@ fn CPU::formatAssemblySuffix(self : CPU) -> String {
fn CPU::formatAssembly(self : CPU) -> String {
let opcode = self.mem[self.pc]
let name = instructionNames[opcode.to_int()]
loop name + " " + self.formatAssemblySuffix() {
s => if s.length() < 32 { continue s + " " } else { s }
for s = name + " " + self.formatAssemblySuffix() {
if s.length() < 32 {
continue s + " "
} else {
break s
}
}
}

Expand All @@ -395,9 +396,9 @@ fn CPU::formatInstruction(self : CPU) -> String {
let opcode = self.mem[self.pc]
let bytes = instructionSizes[opcode.to_int()]
let name = instructionNames[opcode.to_int()]
let w0 = self.mem[self.pc + 0].to_hex()
let mut w1 = self.mem[self.pc + 1].to_hex()
let mut w2 = self.mem[self.pc + 2].to_hex()
let w0 = self.mem[self.pc + 0].formatHex()
let mut w1 = self.mem[self.pc + 1].formatHex()
let mut w2 = self.mem[self.pc + 2].formatHex()
if bytes < 2 {
w1 = " "
}
Expand All @@ -407,11 +408,11 @@ fn CPU::formatInstruction(self : CPU) -> String {
let uf = if isSpecialOpcode(opcode.to_int()) { "*" } else { " " }
let asm_ = self.formatAssembly()
let pc = self.pc.to_hex()
let a = self.a.to_hex()
let x = self.x.to_hex()
let y = self.y.to_hex()
let p = self.flags().to_hex()
let sp = self.sp.to_hex()
let a = self.a.formatHex()
let x = self.x.formatHex()
let y = self.y.formatHex()
let p = self.flags().formatHex()
let sp = self.sp.formatHex()
"\{pc} \{w0} \{w1} \{w2} \{uf}\{asm_}A:\{a} X:\{x} Y:\{y} P:\{p} SP:\{sp}"
}

Expand All @@ -426,9 +427,9 @@ fn CPU::formatInstructionWithMesen2Extended(self : CPU) -> String {
let opcode = self.mem[self.pc]
let bytes = instructionSizes[opcode.to_int()]
let name = instructionNames[opcode.to_int()]
let w0 = self.mem[self.pc + 0].to_hex()
let mut w1 = self.mem[self.pc + 1].to_hex()
let mut w2 = self.mem[self.pc + 2].to_hex()
let w0 = self.mem[self.pc + 0].formatHex()
let mut w1 = self.mem[self.pc + 1].formatHex()
let mut w2 = self.mem[self.pc + 2].formatHex()
if bytes < 2 {
w1 = " "
}
Expand All @@ -438,21 +439,21 @@ fn CPU::formatInstructionWithMesen2Extended(self : CPU) -> String {
let uf = if isSpecialOpcode(opcode.to_int()) { "*" } else { " " }
let asm_ = self.formatAssembly()
let pc = self.pc.to_hex()
let a = self.a.to_hex()
let x = self.x.to_hex()
let y = self.y.to_hex()
let p = self.flags().to_hex()
let sp = self.sp.to_hex()
let a = self.a.formatHex()
let x = self.x.formatHex()
let y = self.y.formatHex()
let p = self.flags().formatHex()
let sp = self.sp.formatHex()
"\{pc} \{w0} \{w1} \{w2} \{uf}\{asm_}A:\{a} X:\{x} Y:\{y} P:\{p} SP:\{sp}"
}

///|
fn CPU::formatMemoryDump(self : CPU, base : UInt16, cnt : Int) -> String {
for i = 0, ret = ""
i < cnt
i = i + 1, ret = ret + self.mem[base + (i |> to_u16)].to_hex() + " " {
i = i + 1, ret = ret + self.mem[base + (i |> to_u16)].formatHex() + " " {

} else {
} nobreak {
ret
}
}
Expand Down Expand Up @@ -490,7 +491,7 @@ fn initCPU() -> CPU {

///|
fn pagesDiffer(a : UInt16, b : UInt16) -> Bool {
(a & (0xFF00)) != (b & (0xFF00))
(a & 0xFF00) != (b & 0xFF00)
}

// branch, then adds a cycle for taking a branch and adds another cycle
Expand Down Expand Up @@ -524,8 +525,7 @@ fn CPU::opADC(self : CPU, info : StepInfo) -> Unit {
self.setZN(self.a)
self.c = a.to_int() + b.to_int() + c.to_int() > 0xFF
// (a^b)&0x80 == 0 && (a^self.a)&0x80 != 0
self.v = ((a ^ b) & (0x80)) == 0 &&
((a ^ self.a) & (0x80)) != 0
self.v = ((a ^ b) & 0x80) == 0 && ((a ^ self.a) & 0x80) != 0
}

// AND - Logical and
Expand Down Expand Up @@ -561,7 +561,7 @@ fn CPU::opASL(self : CPU, info : StepInfo) -> Unit {

///|
fn CPU::opBCC(self : CPU, info : StepInfo) -> Unit {
if not(self.c) {
if !self.c {
self.branch(info)
}
}
Expand Down Expand Up @@ -607,7 +607,7 @@ fn CPU::opBMI(self : CPU, info : StepInfo) -> Unit {

///|
fn CPU::opBNE(self : CPU, info : StepInfo) -> Unit {
if not(self.z) {
if !self.z {
self.branch(info)
}
}
Expand All @@ -616,7 +616,7 @@ fn CPU::opBNE(self : CPU, info : StepInfo) -> Unit {

///|
fn CPU::opBPL(self : CPU, info : StepInfo) -> Unit {
if not(self.n) {
if !self.n {
self.branch(info)
}
}
Expand All @@ -635,7 +635,7 @@ fn CPU::opBRK(self : CPU, info : StepInfo) -> Unit {

///|
fn CPU::opBVC(self : CPU, info : StepInfo) -> Unit {
if not(self.v) {
if !self.v {
self.branch(info)
}
}
Expand Down Expand Up @@ -845,7 +845,7 @@ fn CPU::opPHA(self : CPU, _info : StepInfo) -> Unit {

///|
fn CPU::opPHP(self : CPU, _info : StepInfo) -> Unit {
self.push(self.flags() | (0x10))
self.push(self.flags() | 0x10)
}

// PLA - Pull accumulator
Expand All @@ -860,7 +860,7 @@ fn CPU::opPLA(self : CPU, _info : StepInfo) -> Unit {

///|
fn CPU::opPLP(self : CPU, _info : StepInfo) -> Unit {
self.set_flags((self.pull() & (0xEF)) | (0x20)) // pull() & 0xEF | 0x20
self.set_flags((self.pull() & 0xEF) | 0x20) // pull() & 0xEF | 0x20
}

// ROL - Rotate left
Expand Down Expand Up @@ -909,7 +909,7 @@ fn CPU::opROR(self : CPU, info : StepInfo) -> Unit {

///|
fn CPU::opRTI(self : CPU, info : StepInfo) -> Unit {
self.set_flags((self.pull() & (0xEF)) | (0x20)) // pull() & 0xEF | 0x20
self.set_flags((self.pull() & 0xEF) | 0x20) // pull() & 0xEF | 0x20
self.pc = self.pull16()
}

Expand All @@ -930,8 +930,7 @@ fn CPU::opSBC(self : CPU, info : StepInfo) -> Unit {
self.a = a - b - (1 - c)
self.setZN(self.a)
self.c = a.to_int() - b.to_int() - ((1 : UInt8) - c).to_int() >= 0
self.v = ((a ^ b) & (0x80)) != 0 &&
((a ^ self.a) & (0x80)) != 0
self.v = ((a ^ b) & 0x80) != 0 && ((a ^ self.a) & 0x80) != 0
}

// SEC - Set carry flag
Expand Down Expand Up @@ -1572,10 +1571,10 @@ fn CPU::get_addr(self : CPU, mode : AddressingMode) -> UInt16 {
Accumulator | Implied => 0
Relative => {
let offset = self.mem[self.pc + 1] |> to_u16
if offset < (0x80) {
if offset < 0x80 {
self.pc + 2 + offset
} else {
self.pc + 2 + offset - (0x100)
self.pc + 2 + offset - 0x100
}
}
}
Expand Down
Loading
Loading