Compare commits

..

No commits in common. "166bc6fc6dd6f39da10aa13b3d82579ba9996558" and "3a51707280bcb35f3397c8a6da8441f95629bf07" have entirely different histories.

9 changed files with 34 additions and 45 deletions

5
.gitignore vendored
View File

@ -6,9 +6,6 @@
**/*.log
**/*.bin
# Build on Windows
# Build on WIndows
/.build_config
/lib/SDL2
# Any Custom Scripts for Debugging purposes
*.sh

View File

@ -1 +0,0 @@
Built using Zig v0.10.0-dev.548+4ca9a8d19

View File

@ -7,11 +7,8 @@ buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator) !Self {
const buf = try alloc.alloc(u8, 0x8000);
std.mem.set(u8, buf, 0);
return Self{
.buf = buf,
.buf = try alloc.alloc(u8, 0x8000),
.alloc = alloc,
};
}

View File

@ -7,11 +7,8 @@ buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator) !Self {
const buf = try alloc.alloc(u8, 0x40000);
std.mem.set(u8, buf, 0);
return Self{
.buf = buf,
.buf = try alloc.alloc(u8, 0x40000),
.alloc = alloc,
};
}

View File

@ -30,6 +30,7 @@ pub const Io = struct {
0x0400_0000 => @as(u32, self.dispcnt.raw),
0x0400_0004 => @as(u32, self.dispstat.raw),
0x0400_0006 => @as(u32, self.vcount.raw),
0x0400_0130 => @as(u32, self.keyinput.raw),
0x0400_0200 => @as(u32, self.ie.raw),
0x0400_0208 => @boolToInt(self.ime),
else => std.debug.panic("[I/O:32] tried to read from {X:}", .{addr}),

View File

@ -28,28 +28,26 @@ pub fn format78(comptime op: u2, comptime T: bool) InstrFn {
const address = cpu.r[rb] + cpu.r[ro];
if (T) {
// Format 8
switch (op) {
0b00 => {
// STRH
bus.write16(address & 0xFFFF_FFFE, @truncate(u16, cpu.r[rd]));
},
0b01 => {
// LDSB
cpu.r[rd] = u32SignExtend(8, @as(u32, bus.read8(address)));
},
0b10 => {
// LDRH
const value = bus.read16(address & 0xFFFF_FFFE);
cpu.r[rd] = std.math.rotr(u32, @as(u32, value), 8 * (address & 1));
},
0b10 => {
// LDSB
cpu.r[rd] = u32SignExtend(8, @as(u32, bus.read8(address)));
},
0b11 => {
// LDSH
cpu.r[rd] = u32SignExtend(16, @as(u32, bus.read16(address & 0xFFFF_FFFE)));
},
}
} else {
// Format 7
switch (op) {
0b00 => {
// STR

View File

@ -15,7 +15,7 @@ const File = std.fs.File;
const window_scale = 3;
const gba_width = @import("ppu.zig").width;
const gba_height = @import("ppu.zig").height;
const framebuf_pitch = @import("ppu.zig").framebuf_pitch;
const buf_pitch = @import("ppu.zig").buf_pitch;
pub const enable_logging: bool = false;
const is_binary: bool = false;
@ -164,8 +164,8 @@ pub fn main() anyerror!void {
}
// FIXME: Is it OK just to copy the Emulator's Frame Buffer to SDL?
const buf_ptr = bus.ppu.framebuf.ptr;
_ = SDL.SDL_UpdateTexture(texture, null, buf_ptr, framebuf_pitch);
const buf_ptr = bus.ppu.frame_buf.ptr;
_ = SDL.SDL_UpdateTexture(texture, null, buf_ptr, buf_pitch);
_ = SDL.SDL_RenderCopy(renderer, texture, null, null);
SDL.SDL_RenderPresent(renderer);

View File

@ -7,7 +7,8 @@ const Scheduler = @import("scheduler.zig").Scheduler;
const Allocator = std.mem.Allocator;
pub const width = 240;
pub const height = 160;
pub const framebuf_pitch = width * @sizeOf(u16);
pub const buf_pitch = width * @sizeOf(u16);
const buf_len = buf_pitch * height;
pub const Ppu = struct {
const Self = @This();
@ -15,27 +16,24 @@ pub const Ppu = struct {
vram: Vram,
palette: Palette,
sched: *Scheduler,
framebuf: []u8,
frame_buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator, sched: *Scheduler) !Self {
// Queue first Hblank
sched.push(.Draw, sched.tick + (240 * 4));
const framebuf = try alloc.alloc(u8, framebuf_pitch * height);
std.mem.set(u8, framebuf, 0);
return Self{
.vram = try Vram.init(alloc),
.palette = try Palette.init(alloc),
.sched = sched,
.framebuf = framebuf,
.frame_buf = try alloc.alloc(u8, buf_len),
.alloc = alloc,
};
}
pub fn deinit(self: Self) void {
self.alloc.free(self.framebuf);
self.alloc.free(self.frame_buf);
self.vram.deinit();
self.palette.deinit();
}
@ -46,10 +44,10 @@ pub const Ppu = struct {
switch (bg_mode) {
0x3 => {
const start = framebuf_pitch * @as(usize, scanline);
const end = start + framebuf_pitch;
const start = buf_pitch * @as(usize, scanline);
const end = start + buf_pitch;
std.mem.copy(u8, self.framebuf[start..end], self.vram.buf[start..end]);
std.mem.copy(u8, self.frame_buf[start..end], self.vram.buf[start..end]);
},
0x4 => {
const select = io.dispcnt.frame_select.read();
@ -64,8 +62,8 @@ pub const Ppu = struct {
const id = byte * 2;
const j = i * @sizeOf(u16);
self.framebuf[buf_start + j + 1] = self.palette.buf[id + 1];
self.framebuf[buf_start + j] = self.palette.buf[id];
self.frame_buf[buf_start + j + 1] = self.palette.buf[id + 1];
self.frame_buf[buf_start + j] = self.palette.buf[id];
}
},
else => {}, // std.debug.panic("[PPU] TODO: Implement BG Mode {}", .{bg_mode}),
@ -80,11 +78,8 @@ const Palette = struct {
alloc: Allocator,
fn init(alloc: Allocator) !Self {
const buf = try alloc.alloc(u8, 0x400);
std.mem.set(u8, buf, 0);
return Self{
.buf = buf,
.buf = try alloc.alloc(u8, 0x400),
.alloc = alloc,
};
}
@ -123,8 +118,13 @@ const Vram = struct {
alloc: Allocator,
fn init(alloc: Allocator) !Self {
// In Modes 3 and 4, parts of the VRAM are copied to the
// frame buffer, therefore we want to zero-initialize Vram
//
// some programs like Armwrestler assume that VRAM is zeroed-out.
const black = std.mem.zeroes([0x18000]u8);
const buf = try alloc.alloc(u8, 0x18000);
std.mem.set(u8, buf, 0);
std.mem.copy(u8, buf, &black);
return Self{
.buf = buf,

View File

@ -1,10 +1,6 @@
const std = @import("std");
pub fn u32SignExtend(comptime bits: usize, value: u32) u32 {
return @bitCast(u32, signExtend(i32, bits, @bitCast(i32, value)));
}
fn signExtend(comptime T: type, comptime bits: usize, value: anytype) T {
pub fn signExtend(comptime T: type, comptime bits: usize, value: anytype) T {
const ValT = comptime @TypeOf(value);
comptime std.debug.assert(isInteger(ValT));
comptime std.debug.assert(isSigned(ValT));
@ -20,6 +16,10 @@ fn signExtend(comptime T: type, comptime bits: usize, value: anytype) T {
return ((value & ((1 << bits) - 1)) << bit_diff) >> bit_diff;
}
pub fn u32SignExtend(comptime bits: usize, value: u32) u32 {
return @bitCast(u32, signExtend(i32, bits, @bitCast(i32, value)));
}
fn isInteger(comptime T: type) bool {
return @typeInfo(T) == .Int;
}