zba/src/Bus.zig

163 lines
6.7 KiB
Zig
Raw Normal View History

2022-01-07 23:49:58 +00:00
const std = @import("std");
const Bios = @import("bus/Bios.zig");
2022-01-10 11:59:21 +00:00
const Ewram = @import("bus/Ewram.zig");
2022-01-07 23:49:58 +00:00
const GamePak = @import("bus/GamePak.zig");
2022-01-08 00:00:42 +00:00
const Io = @import("bus/io.zig").Io;
2022-01-10 11:59:21 +00:00
const Iwram = @import("bus/Iwram.zig");
2022-01-07 23:49:58 +00:00
const Ppu = @import("ppu.zig").Ppu;
2022-01-08 00:00:42 +00:00
const Scheduler = @import("scheduler.zig").Scheduler;
2022-01-07 23:49:58 +00:00
const Allocator = std.mem.Allocator;
2022-01-10 03:34:33 +00:00
const Self = @This();
2022-01-07 23:49:58 +00:00
pak: GamePak,
bios: Bios,
ppu: Ppu,
2022-01-10 11:59:21 +00:00
iwram: Iwram,
ewram: Ewram,
2022-01-07 23:49:58 +00:00
io: Io,
2022-01-10 03:34:33 +00:00
pub fn init(alloc: Allocator, sched: *Scheduler, path: []const u8) !Self {
return Self{
2022-01-07 23:49:58 +00:00
.pak = try GamePak.init(alloc, path),
.bios = try Bios.init(alloc, "./bin/gba_bios.bin"), // TODO: don't hardcode this + bundle open-sorce Boot ROM
.ppu = try Ppu.init(alloc, sched),
2022-01-10 11:59:21 +00:00
.iwram = try Iwram.init(alloc),
.ewram = try Ewram.init(alloc),
2022-01-07 23:49:58 +00:00
.io = Io.init(),
};
}
2022-01-10 03:34:33 +00:00
pub fn deinit(self: Self) void {
2022-01-10 11:59:21 +00:00
self.iwram.deinit();
self.ewram.deinit();
2022-01-07 23:49:58 +00:00
self.pak.deinit();
self.bios.deinit();
self.ppu.deinit();
}
2022-01-10 03:34:33 +00:00
pub fn read32(self: *const Self, addr: u32) u32 {
2022-01-07 23:49:58 +00:00
return switch (addr) {
// General Internal Memory
0x0000_0000...0x0000_3FFF => self.bios.get32(@as(usize, addr)),
2022-01-10 11:59:21 +00:00
0x0200_0000...0x0203_FFFF => self.iwram.get32(addr - 0x0200_0000),
0x0300_0000...0x0300_7FFF => self.ewram.get32(addr - 0x0300_0000),
2022-01-11 01:25:45 +00:00
0x0400_0000...0x0400_03FE => self.io.read32(addr),
2022-01-07 23:49:58 +00:00
// Internal Display Memory
0x0500_0000...0x0500_03FF => self.ppu.palette.get32(@as(usize, addr - 0x0500_0000)),
0x0600_0000...0x0601_7FFF => self.ppu.vram.get32(@as(usize, addr - 0x0600_0000)),
0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:32] read from 0x{X:} in OAM", .{addr}),
// External Memory (Game Pak)
0x0800_0000...0x09FF_FFFF => self.pak.get32(@as(usize, addr - 0x0800_0000)),
0x0A00_0000...0x0BFF_FFFF => self.pak.get32(@as(usize, addr - 0x0A00_0000)),
0x0C00_0000...0x0DFF_FFFF => self.pak.get32(@as(usize, addr - 0x0C00_0000)),
else => {
std.log.warn("[Bus:32] ZBA tried to read from 0x{X:}", .{addr});
return 0x0000_0000;
},
};
}
2022-01-10 03:34:33 +00:00
pub fn write32(self: *Self, addr: u32, word: u32) void {
2022-01-07 23:49:58 +00:00
// TODO: write32 can write to GamePak Flash
switch (addr) {
// General Internal Memory
2022-01-10 11:59:21 +00:00
0x0200_0000...0x0203_FFFF => self.iwram.set32(addr - 0x0200_0000, word),
0x0300_0000...0x0300_7FFF => self.ewram.set32(addr - 0x0300_0000, word),
2022-01-12 06:19:26 +00:00
0x0400_0000...0x0400_03FE => self.io.write32(addr, word),
2022-01-07 23:49:58 +00:00
// Internal Display Memory
0x0500_0000...0x0500_03FF => self.ppu.palette.set32(@as(usize, addr - 0x0500_0000), word),
0x0600_0000...0x0601_7FFF => self.ppu.vram.set32(@as(usize, addr - 0x0600_0000), word),
0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:32] wrote 0x{X:} to 0x{X:} in OAM", .{ word, addr }),
else => std.log.warn("[Bus:32] ZBA tried to write 0x{X:} to 0x{X:}", .{ word, addr }),
}
}
2022-01-10 03:34:33 +00:00
pub fn read16(self: *const Self, addr: u32) u16 {
2022-01-07 23:49:58 +00:00
return switch (addr) {
// General Internal Memory
0x0000_0000...0x0000_3FFF => self.bios.get16(@as(usize, addr)),
2022-01-10 11:59:21 +00:00
0x0200_0000...0x0203_FFFF => self.iwram.get16(addr - 0x0200_0000),
0x0300_0000...0x0300_7FFF => self.ewram.get16(addr - 0x0300_0000),
2022-01-07 23:49:58 +00:00
0x0400_0000...0x0400_03FE => self.io.read16(addr),
// Internal Display Memory
0x0500_0000...0x0500_03FF => self.ppu.palette.get16(@as(usize, addr - 0x0500_0000)),
0x0600_0000...0x0601_7FFF => self.ppu.vram.get16(@as(usize, addr - 0x0600_0000)),
0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:16] read from 0x{X:} in OAM", .{addr}),
// External Memory (Game Pak)
0x0800_0000...0x09FF_FFFF => self.pak.get16(@as(usize, addr - 0x0800_0000)),
0x0A00_0000...0x0BFF_FFFF => self.pak.get16(@as(usize, addr - 0x0A00_0000)),
0x0C00_0000...0x0DFF_FFFF => self.pak.get16(@as(usize, addr - 0x0C00_0000)),
else => {
std.log.warn("[Bus:16] ZBA tried to read from 0x{X:}", .{addr});
return 0x0000;
},
};
}
2022-01-10 03:34:33 +00:00
pub fn write16(self: *Self, addr: u32, halfword: u16) void {
2022-01-07 23:49:58 +00:00
// TODO: write16 can write to GamePak Flash
switch (addr) {
// General Internal Memory
2022-01-10 11:59:21 +00:00
0x0200_0000...0x0203_FFFF => self.iwram.set16(addr - 0x0200_0000, halfword),
0x0300_0000...0x0300_7FFF => self.ewram.set16(addr - 0x0300_0000, halfword),
2022-01-07 23:49:58 +00:00
0x0400_0000...0x0400_03FE => self.io.write16(addr, halfword),
// Internal Display Memory
0x0500_0000...0x0500_03FF => self.ppu.palette.set16(@as(usize, addr - 0x0500_0000), halfword),
0x0600_0000...0x0601_7FFF => self.ppu.vram.set16(@as(usize, addr - 0x0600_0000), halfword),
0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:16] write 0x{X:} to 0x{X:} in OAM", .{ halfword, addr }),
else => std.log.warn("[Bus:16] ZBA tried to write 0x{X:} to 0x{X:}", .{ halfword, addr }),
}
}
2022-01-10 03:34:33 +00:00
pub fn read8(self: *const Self, addr: u32) u8 {
2022-01-07 23:49:58 +00:00
return switch (addr) {
// General Internal Memory
0x0000_0000...0x0000_3FFF => self.bios.get8(@as(usize, addr)),
2022-01-10 11:59:21 +00:00
0x0200_0000...0x0203_FFFF => self.iwram.get8(addr - 0x0200_0000),
0x0300_0000...0x0300_7FFF => self.ewram.get8(addr - 0x0300_0000),
2022-01-07 23:49:58 +00:00
0x0400_0000...0x0400_03FE => self.io.read8(addr),
// Internal Display Memory
0x0500_0000...0x0500_03FF => self.ppu.palette.get8(@as(usize, addr - 0x0500_0000)),
0x0600_0000...0x0601_7FFF => self.ppu.vram.get8(@as(usize, addr - 0x0600_0000)),
0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:8] read from 0x{X:} in OAM", .{addr}),
// External Memory (Game Pak)
0x0800_0000...0x09FF_FFFF => self.pak.get8(@as(usize, addr - 0x0800_0000)),
0x0A00_0000...0x0BFF_FFFF => self.pak.get8(@as(usize, addr - 0x0A00_0000)),
0x0C00_0000...0x0DFF_FFFF => self.pak.get8(@as(usize, addr - 0x0C00_0000)),
0x0E00_0000...0x0E00_FFFF => std.debug.panic("[Bus:8] read from 0x{X:} in Game Pak SRAM", .{addr}),
else => {
std.log.warn("[Bus:8] ZBA tried to read from 0x{X:}", .{addr});
return 0x00;
},
};
}
2022-01-10 11:59:21 +00:00
pub fn write8(self: *Self, addr: u32, byte: u8) void {
2022-01-07 23:49:58 +00:00
switch (addr) {
// General Internal Memory
2022-01-10 11:59:21 +00:00
0x0200_0000...0x0203_FFFF => self.iwram.set8(addr - 0x0200_0000, byte),
0x0300_0000...0x0300_7FFF => self.ewram.set8(addr - 0x0300_0000, byte),
2022-01-12 06:19:26 +00:00
0x0400_0000...0x0400_03FE => self.io.write8(addr, byte),
2022-01-07 23:49:58 +00:00
// External Memory (Game Pak)
0x0E00_0000...0x0E00_FFFF => std.debug.panic("[Bus:8] write 0x{X:} to 0x{X:} in Game Pak SRAM", .{ byte, addr }),
else => std.log.warn("[Bus:8] ZBA tried to write 0x{X:} to 0x{X:}", .{ byte, addr }),
}
}