feat(bus): implement IWRAM and EWRAM

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:11:53 -03:00
parent e0c4b3b407
commit 670347d4a0
4 changed files with 116 additions and 14 deletions

View File

@ -1,8 +1,10 @@
const std = @import("std"); const std = @import("std");
const Bios = @import("bus/Bios.zig"); const Bios = @import("bus/Bios.zig");
const Ewram = @import("bus/Ewram.zig");
const GamePak = @import("bus/GamePak.zig"); const GamePak = @import("bus/GamePak.zig");
const Io = @import("bus/io.zig").Io; const Io = @import("bus/io.zig").Io;
const Iwram = @import("bus/Iwram.zig");
const Ppu = @import("ppu.zig").Ppu; const Ppu = @import("ppu.zig").Ppu;
const Scheduler = @import("scheduler.zig").Scheduler; const Scheduler = @import("scheduler.zig").Scheduler;
@ -12,6 +14,8 @@ const Self = @This();
pak: GamePak, pak: GamePak,
bios: Bios, bios: Bios,
ppu: Ppu, ppu: Ppu,
iwram: Iwram,
ewram: Ewram,
io: Io, io: Io,
pub fn init(alloc: Allocator, sched: *Scheduler, path: []const u8) !Self { pub fn init(alloc: Allocator, sched: *Scheduler, path: []const u8) !Self {
@ -19,11 +23,15 @@ pub fn init(alloc: Allocator, sched: *Scheduler, path: []const u8) !Self {
.pak = try GamePak.init(alloc, path), .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 .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), .ppu = try Ppu.init(alloc, sched),
.iwram = try Iwram.init(alloc),
.ewram = try Ewram.init(alloc),
.io = Io.init(), .io = Io.init(),
}; };
} }
pub fn deinit(self: Self) void { pub fn deinit(self: Self) void {
self.iwram.deinit();
self.ewram.deinit();
self.pak.deinit(); self.pak.deinit();
self.bios.deinit(); self.bios.deinit();
self.ppu.deinit(); self.ppu.deinit();
@ -33,8 +41,8 @@ pub fn read32(self: *const Self, addr: u32) u32 {
return switch (addr) { return switch (addr) {
// General Internal Memory // General Internal Memory
0x0000_0000...0x0000_3FFF => self.bios.get32(@as(usize, addr)), 0x0000_0000...0x0000_3FFF => self.bios.get32(@as(usize, addr)),
0x0200_0000...0x0203_FFFF => std.debug.panic("[Bus:32] read from 0x{X:} in IWRAM", .{addr}), 0x0200_0000...0x0203_FFFF => self.iwram.get32(addr - 0x0200_0000),
0x0300_0000...0x0300_7FFF => std.debug.panic("[Bus:32] read from 0x{X:} in EWRAM", .{addr}), 0x0300_0000...0x0300_7FFF => self.ewram.get32(addr - 0x0300_0000),
0x0400_0000...0x0400_03FE => self.read32(addr), 0x0400_0000...0x0400_03FE => self.read32(addr),
// Internal Display Memory // Internal Display Memory
@ -59,8 +67,8 @@ pub fn write32(self: *Self, addr: u32, word: u32) void {
switch (addr) { switch (addr) {
// General Internal Memory // General Internal Memory
0x0200_0000...0x0203_FFFF => std.debug.panic("[Bus:32] wrote 0x{X:} to 0x{X:} in IWRAM", .{ word, addr }), 0x0200_0000...0x0203_FFFF => self.iwram.set32(addr - 0x0200_0000, word),
0x0300_0000...0x0300_7FFF => std.debug.panic("[Bus:32] wrote 0x{X:} to 0x{X:} in EWRAM", .{ word, addr }), 0x0300_0000...0x0300_7FFF => self.ewram.set32(addr - 0x0300_0000, word),
0x0400_0000...0x0400_03FE => std.debug.panic("[Bus:32] wrote 0x{X:} to 0x{X:} in I/O", .{ word, addr }), 0x0400_0000...0x0400_03FE => std.debug.panic("[Bus:32] wrote 0x{X:} to 0x{X:} in I/O", .{ word, addr }),
// Internal Display Memory // Internal Display Memory
@ -76,8 +84,8 @@ pub fn read16(self: *const Self, addr: u32) u16 {
return switch (addr) { return switch (addr) {
// General Internal Memory // General Internal Memory
0x0000_0000...0x0000_3FFF => self.bios.get16(@as(usize, addr)), 0x0000_0000...0x0000_3FFF => self.bios.get16(@as(usize, addr)),
0x0200_0000...0x0203_FFFF => std.debug.panic("[Bus:16] read from 0x{X:} in IWRAM", .{addr}), 0x0200_0000...0x0203_FFFF => self.iwram.get16(addr - 0x0200_0000),
0x0300_0000...0x0300_7FFF => std.debug.panic("[Bus:16] read from 0x{X:} in EWRAM", .{addr}), 0x0300_0000...0x0300_7FFF => self.ewram.get16(addr - 0x0300_0000),
0x0400_0000...0x0400_03FE => self.io.read16(addr), 0x0400_0000...0x0400_03FE => self.io.read16(addr),
// Internal Display Memory // Internal Display Memory
@ -101,8 +109,8 @@ pub fn write16(self: *Self, addr: u32, halfword: u16) void {
// TODO: write16 can write to GamePak Flash // TODO: write16 can write to GamePak Flash
switch (addr) { switch (addr) {
// General Internal Memory // General Internal Memory
0x0200_0000...0x0203_FFFF => std.debug.panic("[Bus:16] write 0x{X:} to 0x{X:} in IWRAM", .{ halfword, addr }), 0x0200_0000...0x0203_FFFF => self.iwram.set16(addr - 0x0200_0000, halfword),
0x0300_0000...0x0300_7FFF => std.debug.panic("[Bus:16] write 0x{X:} to 0x{X:} in EWRAM", .{ halfword, addr }), 0x0300_0000...0x0300_7FFF => self.ewram.set16(addr - 0x0300_0000, halfword),
0x0400_0000...0x0400_03FE => self.io.write16(addr, halfword), 0x0400_0000...0x0400_03FE => self.io.write16(addr, halfword),
// Internal Display Memory // Internal Display Memory
@ -118,8 +126,8 @@ pub fn read8(self: *const Self, addr: u32) u8 {
return switch (addr) { return switch (addr) {
// General Internal Memory // General Internal Memory
0x0000_0000...0x0000_3FFF => self.bios.get8(@as(usize, addr)), 0x0000_0000...0x0000_3FFF => self.bios.get8(@as(usize, addr)),
0x0200_0000...0x0203_FFFF => std.debug.panic("[Bus:8] read from 0x{X:} in IWRAM", .{addr}), 0x0200_0000...0x0203_FFFF => self.iwram.get8(addr - 0x0200_0000),
0x0300_0000...0x0300_7FFF => std.debug.panic("[Bus:8] read from 0x{X:} in EWRAM", .{addr}), 0x0300_0000...0x0300_7FFF => self.ewram.get8(addr - 0x0300_0000),
0x0400_0000...0x0400_03FE => self.io.read8(addr), 0x0400_0000...0x0400_03FE => self.io.read8(addr),
// Internal Display Memory // Internal Display Memory
@ -140,12 +148,12 @@ pub fn read8(self: *const Self, addr: u32) u8 {
}; };
} }
pub fn write8(_: *Self, addr: u32, byte: u8) void { pub fn write8(self: *Self, addr: u32, byte: u8) void {
switch (addr) { switch (addr) {
// General Internal Memory // General Internal Memory
0x0200_0000...0x0203_FFFF => std.debug.panic("[Bus:8] write 0x{X:} to 0x{X:} in IWRAM", .{ byte, addr }), 0x0200_0000...0x0203_FFFF => self.iwram.set8(addr - 0x0200_0000, byte),
0x0300_0000...0x0300_7FFF => std.debug.panic("[Bus:8] write 0x{X:} to 0x{X:} in EWRAM", .{ byte, addr }), 0x0300_0000...0x0300_7FFF => self.ewram.set8(addr - 0x0300_0000, byte),
0x0400_0000...0x0400_03FE => std.debug.panic("[Bus:8] write 0x{X:} to 0x{X:} in I/O", .{ byte, addr }), 0x0400_0000...0x0400_03FE => self.io.set8(addr - 0x0400_0000, byte),
// External Memory (Game Pak) // 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 }), 0x0E00_0000...0x0E00_FFFF => std.debug.panic("[Bus:8] write 0x{X:} to 0x{X:} in Game Pak SRAM", .{ byte, addr }),

46
src/bus/Ewram.zig Normal file
View File

@ -0,0 +1,46 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const Self = @This();
buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator) !Self {
return Self{
.buf = try alloc.alloc(u8, 0x8000),
.alloc = alloc,
};
}
pub fn deinit(self: Self) void {
self.alloc.free(self.buf);
}
pub fn get32(self: *const Self, idx: usize) u32 {
return (@as(u32, self.buf[idx + 3]) << 24) | (@as(u32, self.buf[idx + 2]) << 16) | (@as(u32, self.buf[idx + 1]) << 8) | (@as(u32, self.buf[idx]));
}
pub fn set32(self: *Self, idx: usize, word: u32) void {
self.buf[idx + 3] = @truncate(u8, word >> 24);
self.buf[idx + 2] = @truncate(u8, word >> 16);
self.buf[idx + 1] = @truncate(u8, word >> 8);
self.buf[idx] = @truncate(u8, word);
}
pub fn get16(self: *const Self, idx: usize) u16 {
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
}
pub fn set16(self: *Self, idx: usize, halfword: u16) void {
self.buf[idx + 1] = @truncate(u8, halfword >> 8);
self.buf[idx] = @truncate(u8, halfword);
}
pub fn get8(self: *const Self, idx: usize) u8 {
return self.buf[idx];
}
pub fn set8(self: *Self, idx: usize, byte: u8) void {
self.buf[idx] = byte;
}

44
src/bus/Iwram.zig Normal file
View File

@ -0,0 +1,44 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const Self = @This();
buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator) !Self {
return Self{
.buf = try alloc.alloc(u8, 0x40000),
.alloc = alloc,
};
}
pub fn deinit(self: Self) void {
self.alloc.free(self.buf);
}
pub fn get32(self: *const Self, idx: usize) u32 {
return (@as(u32, self.get16(idx + 2)) << 16) | @as(u32, self.get16(idx));
}
pub fn set32(self: *Self, idx: usize, word: u32) void {
self.set16(idx + 2, @truncate(u16, word >> 16));
self.set16(idx, @truncate(u16, word));
}
pub fn get16(self: *const Self, idx: usize) u16 {
return (@as(u16, self.buf[idx + 1]) << 8) | @as(u16, self.buf[idx]);
}
pub fn set16(self: *Self, idx: usize, halfword: u16) void {
self.buf[idx + 1] = @truncate(u8, halfword >> 8);
self.buf[idx] = @truncate(u8, halfword);
}
pub fn get8(self: *const Self, idx: usize) u8 {
return self.buf[idx];
}
pub fn set8(self: *Self, idx: usize, byte: u8) void {
self.buf[idx] = byte;
}

View File

@ -52,6 +52,10 @@ pub const Io = struct {
else => std.debug.panic("[I/O:8] tried to read from {X:}", .{addr}), else => std.debug.panic("[I/O:8] tried to read from {X:}", .{addr}),
}; };
} }
pub fn set8(_: *Self, addr: u32, byte: u8) void {
std.debug.panic("[I/0:8] tried to write 0x{X:} to 0x{X:}", .{ byte, addr });
}
}; };
const DispCnt = extern union { const DispCnt = extern union {