Skip to content
Open
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
10 changes: 5 additions & 5 deletions lib/apu.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,36 @@ fn initAPU() -> APU {
let frameCounterRate : Double = cpuFrequency / 240.0

///|
let lengthTable : FixedArray[Int] = [
let lengthTable : FixedArray[Int32] = [
10, 254, 20, 2, 40, 4, 80, 6, 160, 8, 60, 10, 14, 12, 26, 14, 12, 16, 24, 18, 48,
20, 96, 22, 192, 24, 72, 26, 16, 28, 32, 30,
]

///|
let dutyTable : FixedArray[FixedArray[Int]] = [
let dutyTable : FixedArray[FixedArray[Int32]] = [
[0, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 0, 0, 0],
[1, 0, 0, 1, 1, 1, 1, 1],
]

///|
let triangleTable : FixedArray[Int] = [
let triangleTable : FixedArray[Int32] = [
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15,
]

// uint16

///|
let noiseTable : FixedArray[Int] = [
let noiseTable : FixedArray[Int32] = [
4, 8, 16, 32, 64, 96, 128, 160, 202, 254, 380, 508, 762, 1016, 2034, 4068,
]

// uint8

///|
let dmcTable : FixedArray[Int] = [
let dmcTable : FixedArray[Int32] = [
214, 190, 170, 160, 143, 127, 113, 107, 95, 80, 71, 64, 53, 42, 36, 27,
]

Expand Down
8 changes: 4 additions & 4 deletions lib/cartridge.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ let iNESMagic = 0x1A53454E

///|
struct INESHeader {
magic : Int // iNES magic number
magic : Int32 // iNES magic number
numPRG : UInt8 // number of PRG-ROM banks (16KB each)
numCHR : UInt8 // number of CHR-ROM banks (8KB each)
control1 : UInt8 // control bits
Expand All @@ -58,7 +58,7 @@ fn MemoryReader::readINESHeader(self : MemoryReader) -> INESHeader {
println("control2: \{control2})")
println("numRAM: \{numRAM})")
let padding = self.readUInt8Array(7)
{ magic, numPRG, numCHR, control1, control2, numRAM, padding }
{ magic, numPRG, numCHR, control1, control2, numRAM, padding, }
}

// http://wiki.nesdev.com/w/index.php/INES
Expand All @@ -76,7 +76,7 @@ fn MemoryReader::readINESHeader(self : MemoryReader) -> INESHeader {
// ++++----- Lower nybble of mapper number

///|
fn newCartridge(data : FixedArray[Int]) -> Cartridge {
fn newCartridge(data : FixedArray[Int32]) -> Cartridge {
let reader = MemoryReader::new(data)
let header = reader.readINESHeader()
if header.magic != iNESMagic {
Expand Down Expand Up @@ -126,5 +126,5 @@ fn newCartridge(data : FixedArray[Int]) -> Cartridge {
reader.readUInt8Array(chr_len)
}
let sram : FixedArray[UInt8] = FixedArray::make(0x2000, 0)
{ prg, chr, sram, mapper, mirror, battery }
{ prg, chr, sram, mapper, mirror, battery, }
}
16 changes: 8 additions & 8 deletions lib/common.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@

///|
struct MemoryReader {
data : FixedArray[Int]
mut pos : Int
data : FixedArray[Int32]
mut pos : Int32
}

///|
fn MemoryReader::new(data : FixedArray[Int]) -> MemoryReader {
{ data, pos: 0 }
fn MemoryReader::new(data : FixedArray[Int32]) -> MemoryReader {
{ data, pos: 0, }
}

// copied from official example `bytes-buffer`

///|
fn bytes_to_int_be(bytes : FixedArray[Int], start : Int) -> Int {
fn bytes_to_int_be(bytes : FixedArray[Int32], start : Int32) -> Int32 {
((bytes[start] & 0xff) << 24) |
((bytes[start + 1] & 0xff) << 16) |
((bytes[start + 2] & 0xff) << 8) |
(bytes[start + 3] & 0xff)
}

///|
fn bytes_to_int_le(bytes : FixedArray[Int], start : Int) -> Int {
fn bytes_to_int_le(bytes : FixedArray[Int32], start : Int32) -> Int32 {
((bytes[start + 3] & 0xff) << 24) |
((bytes[start + 2] & 0xff) << 16) |
((bytes[start + 1] & 0xff) << 8) |
Expand All @@ -36,15 +36,15 @@ fn bytes_to_int_le(bytes : FixedArray[Int], start : Int) -> Int {
// big endian

///|
fn MemoryReader::readIntBE(self : MemoryReader) -> Int {
fn MemoryReader::readIntBE(self : MemoryReader) -> Int32 {
self.pos = self.pos + 4
bytes_to_int_be(self.data, self.pos - 4)
}

// little endian

///|
fn MemoryReader::readIntLE(self : MemoryReader) -> Int {
fn MemoryReader::readIntLE(self : MemoryReader) -> Int32 {
self.pos = self.pos + 4
bytes_to_int_le(self.data, self.pos - 4)
}
6 changes: 3 additions & 3 deletions lib/console.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ struct NES {
controllers : FixedArray[Controller]
mut mapper : Mapper?
ram : FixedArray[UInt8] // 2048 bytes
mut step_cnt : Int
mut step_cnt : Int32
}

///|
fn NES::new(data : FixedArray[Int]) -> NES {
fn NES::new(data : FixedArray[Int32]) -> NES {
let controllers : FixedArray[_] = [Controller::new(), Controller::new()]
let nes : NES = {
cpu: initCPU(),
Expand Down Expand Up @@ -47,7 +47,7 @@ fn NES::reset(self : NES) -> Unit {
}

///|
fn NES::step(self : NES, trace_log : Bool) -> Int {
fn NES::step(self : NES, trace_log : Bool) -> Int32 {
let step_cnt = self.step_cnt
//log_trace("step: \(step_cnt)")
let cpuCycles = self.cpu.step(trace_log)
Expand Down
4 changes: 2 additions & 2 deletions lib/controller.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
///|
struct Controller {
mut buttons : FixedArray[Bool]
mut index : Int
mut index : Int32
mut strobe : UInt8
}

///|
fn Controller::new() -> Controller {
{ buttons: FixedArray::make(8, false), index: 0, strobe: 0 }
{ buttons: FixedArray::make(8, false), index: 0, strobe: 0, }
}

// nim and go has different logic?
Expand Down
34 changes: 18 additions & 16 deletions lib/cpu.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct CPUMemory {

///|
fn CPUMemory::new() -> CPUMemory {
{ nes: None }
{ nes: None, }
}

///|
Expand Down Expand Up @@ -43,7 +43,7 @@ struct CPU {
mut n : Bool // negative flag
// other
mut interrupt : Interrupt // interrupt type to perform
mut stall : Int // number of cycles to stall
mut stall : Int32 // number of cycles to stall
}

// constructors must be capitalized
Expand All @@ -70,12 +70,12 @@ enum AddressingMode {
} derive(Eq)

///|
impl Default for AddressingMode with default() -> AddressingMode {
impl Default for AddressingMode with fn default() -> AddressingMode {
Absolute
}

///|
fn addressingMode(mode : Int) -> AddressingMode {
fn addressingMode(mode : Int32) -> AddressingMode {
match mode {
0 => Absolute
1 => AbsoluteX
Expand All @@ -94,7 +94,7 @@ fn addressingMode(mode : Int) -> AddressingMode {
}

///|
fn AddressingMode::to_int(self : AddressingMode) -> Int {
fn AddressingMode::to_int(self : AddressingMode) -> Int32 {
match self {
Absolute => 0
AbsoluteX => 1
Expand Down Expand Up @@ -122,7 +122,8 @@ struct StepInfo {
} derive(Default)

///|
fn CPUMemory::op_get(self : CPUMemory, adr : UInt16) -> UInt8 {
#alias("_[_]")
fn CPUMemory::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]
Expand All @@ -145,7 +146,8 @@ fn CPUMemory::op_get(self : CPUMemory, adr : UInt16) -> UInt8 {
}

///|
fn CPUMemory::op_set(self : CPUMemory, adr : UInt16, val : UInt8) -> Unit {
#alias("_[_]=_")
fn CPUMemory::set(self : CPUMemory, adr : UInt16, val : UInt8) -> Unit {
let n = self.nes.unwrap()
if adr.to_int() >= 0x0000 && adr.to_int() <= 0x1FFF {
// let addr = adr._
Expand Down Expand Up @@ -385,7 +387,7 @@ fn CPU::formatAssembly(self : CPU) -> String {
}

///|
fn isSpecialOpcode(op : Int) -> Bool {
fn isSpecialOpcode(op : Int32) -> Bool {
instructionUnofficial[op] == 1
}

Expand Down Expand Up @@ -448,7 +450,7 @@ fn CPU::formatInstructionWithMesen2Extended(self : CPU) -> String {
}

///|
fn CPU::formatMemoryDump(self : CPU, base : UInt16, cnt : Int) -> String {
fn CPU::formatMemoryDump(self : CPU, base : UInt16, cnt : Int32) -> String {
for i = 0, ret = ""
i < cnt
i = i + 1, ret = ret + self.mem[base + (i |> to_u16)].formatHex() + " " {
Expand Down Expand Up @@ -1446,7 +1448,7 @@ let instructionNames : FixedArray[String] = [
]

///|
let instructionModes : FixedArray[Int] = [ // Addressing modes
let instructionModes : FixedArray[Int32] = [ // Addressing modes
5, 6, 5, 6, 10, 10, 10, 10, 5, 4, 3, 4, 0, 0, 0, 0, 9, 8, 5, 8, 11, 11, 11, 11,
5, 2, 5, 2, 1, 1, 1, 1, 0, 6, 5, 6, 10, 10, 10, 10, 5, 4, 3, 4, 0, 0, 0, 0, 9,
8, 5, 8, 11, 11, 11, 11, 5, 2, 5, 2, 1, 1, 1, 1, 5, 6, 5, 6, 10, 10, 10, 10, 5,
Expand All @@ -1461,7 +1463,7 @@ let instructionModes : FixedArray[Int] = [ // Addressing modes
]

///|
let instructionSizes : FixedArray[Int] = [ // Size in bytes
let instructionSizes : FixedArray[Int32] = [ // Size in bytes
1, 2, 0, 2, 2, 2, 2, 2, 1, 2, 1, 0, 3, 3, 3, 3, 2, 2, 0, 2, 2, 2, 2, 2, 1, 3, 1,
3, 3, 3, 3, 3, 3, 2, 0, 2, 2, 2, 2, 2, 1, 2, 1, 0, 3, 3, 3, 3, 2, 2, 0, 2, 2, 2,
2, 2, 1, 3, 1, 3, 3, 3, 3, 3, 1, 2, 0, 2, 2, 2, 2, 2, 1, 2, 1, 0, 3, 3, 3, 3, 2,
Expand All @@ -1475,7 +1477,7 @@ let instructionSizes : FixedArray[Int] = [ // Size in bytes
]

///|
let instructionCycles : FixedArray[Int] = [ // Number of cycles used
let instructionCycles : FixedArray[Int32] = [ // Number of cycles used
7, 6, 2, 8, 3, 3, 5, 5, 3, 2, 2, 2, 4, 4, 6, 6, 2, 5, 2, 8, 4, 4, 6, 6, 2, 4, 2,
7, 4, 4, 7, 7, 6, 6, 2, 8, 3, 3, 5, 5, 4, 2, 2, 2, 4, 4, 6, 6, 2, 5, 2, 8, 4, 4,
6, 6, 2, 4, 2, 7, 4, 4, 7, 7, 6, 6, 2, 8, 3, 3, 5, 5, 3, 2, 2, 2, 3, 4, 6, 6, 2,
Expand All @@ -1489,7 +1491,7 @@ let instructionCycles : FixedArray[Int] = [ // Number of cycles used
]

///|
let instructionPageCycles : FixedArray[Int] = [ // Cycles used on a page cross
let instructionPageCycles : FixedArray[Int32] = [ // Cycles used on a page cross
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
Expand All @@ -1503,7 +1505,7 @@ let instructionPageCycles : FixedArray[Int] = [ // Cycles used on a page cross
]

///|
let instructionUnofficial : FixedArray[Int] = [ // 1 if official
let instructionUnofficial : FixedArray[Int32] = [ // 1 if official
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, //0
0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, //1
Expand Down Expand Up @@ -1581,7 +1583,7 @@ fn CPU::get_addr(self : CPU, mode : AddressingMode) -> UInt16 {
}

///|
fn CPU::step(self : CPU, trace_log : Bool) -> Int {
fn CPU::step(self : CPU, trace_log : Bool) -> Int32 {
if self.stall > 0 {
if trace_log {
println("wait for stall" + self.stall.to_string())
Expand Down Expand Up @@ -1620,7 +1622,7 @@ fn CPU::step(self : CPU, trace_log : Bool) -> Int {
}
self.pc += instructionSizes[opcode.to_int()] |> to_u16
self.cycles += instructionCycles[opcode.to_int()].to_int64()
let info : StepInfo = { address: adr, pc: self.pc, mode }
let info : StepInfo = { address: adr, pc: self.pc, mode, }
instructions[opcode.to_int()](self, info)
(self.cycles - cycles).to_int()
}
5 changes: 5 additions & 0 deletions lib/extends.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
///|
pub extend AddressingMode with Eq::{not_equal, equal}

///|
pub extend StepInfo with Default::{default}
Loading
Loading