add nes cartridge subsystem and 41 ines mapper variants
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
const types = @import("types.zig");
|
||||
const Mirroring = types.Mirroring;
|
||||
|
||||
const Mapper89 = @This();
|
||||
|
||||
prg_bank: u8 = 0,
|
||||
chr_bank: u8 = 0,
|
||||
mirroring_mode: Mirroring = .single_screen_lower,
|
||||
|
||||
prg_bank_count: usize,
|
||||
chr_bank_count: usize,
|
||||
|
||||
pub fn init(
|
||||
prg_size: usize,
|
||||
chr_size: usize,
|
||||
) Mapper89 {
|
||||
return .{
|
||||
.prg_bank_count = prg_size / 0x4000,
|
||||
.chr_bank_count = if (chr_size > 0) chr_size / 0x2000 else 1,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn cpuMapRead(
|
||||
self: *const Mapper89,
|
||||
address: u16,
|
||||
) ?usize {
|
||||
if (address < 0x8000) return null;
|
||||
|
||||
if (address < 0xC000) {
|
||||
const offset = @as(usize, address - 0x8000);
|
||||
const bank = @as(usize, self.prg_bank) % self.prg_bank_count;
|
||||
return bank * 0x4000 + offset;
|
||||
} else {
|
||||
const offset = @as(usize, address - 0xC000);
|
||||
const fixed_bank = if (self.prg_bank_count > 0) self.prg_bank_count - 1 else 0;
|
||||
return fixed_bank * 0x4000 + offset;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cpuWrite(
|
||||
self: *Mapper89,
|
||||
address: u16,
|
||||
value: u8,
|
||||
) void {
|
||||
if (address >= 0x8000) {
|
||||
self.prg_bank = (value >> 4) & 0x07;
|
||||
self.chr_bank = (value & 0x07) | ((value & 0x80) >> 4);
|
||||
self.mirroring_mode = if ((value & 0x08) != 0) .single_screen_upper else .single_screen_lower;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ppuMapRead(
|
||||
self: *const Mapper89,
|
||||
address: u16,
|
||||
) ?usize {
|
||||
if (address >= 0x2000) return null;
|
||||
|
||||
const bank = @as(usize, self.chr_bank) % self.chr_bank_count;
|
||||
return bank * 0x2000 + @as(usize, address);
|
||||
}
|
||||
|
||||
pub fn ppuMapWrite(
|
||||
_: *const Mapper89,
|
||||
address: u16,
|
||||
) ?usize {
|
||||
if (address >= 0x2000) return null;
|
||||
return address;
|
||||
}
|
||||
|
||||
pub fn mirroring(
|
||||
self: *const Mapper89,
|
||||
) Mirroring {
|
||||
return self.mirroring_mode;
|
||||
}
|
||||
|
||||
pub fn irqAsserted(_: *const Mapper89) bool {
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user