From 0c4882e658b2698e2ce3610c0ded6487fd82a9e9 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Fri, 21 Oct 2022 05:12:19 -0300 Subject: [PATCH] feat: impelemnt BG0,1,2CNT and IF --- src/bus/io.zig | 54 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/src/bus/io.zig b/src/bus/io.zig index 64a1884..66561ec 100644 --- a/src/bus/io.zig +++ b/src/bus/io.zig @@ -10,8 +10,19 @@ pub const Io = struct { dispstat: DisplayStatus, vcount: VCount, /// Read / Write + bg0cnt: BackgroundControl, + /// Read / Write + bg1cnt: BackgroundControl, + /// Read / Write + bg2cnt: BackgroundControl, + /// Read / Write + bg3cnt: BackgroundControl, + /// Read / Write ime: bool, ie: InterruptEnable, + /// Read / Write + irq: InterruptRequest, + keyinput: KeyInput, pub fn init() Self { @@ -19,8 +30,13 @@ pub const Io = struct { .dispcnt = .{ .raw = 0x0000 }, .dispstat = .{ .raw = 0x0000 }, .vcount = .{ .raw = 0x0000 }, + .bg0cnt = .{ .raw = 0x0000 }, + .bg1cnt = .{ .raw = 0x0000 }, + .bg2cnt = .{ .raw = 0x0000 }, + .bg3cnt = .{ .raw = 0x0000 }, .ime = false, .ie = .{ .raw = 0x0000 }, + .irq = .{ .raw = 0x0000 }, .keyinput = .{ .raw = 0x03FF }, }; } @@ -61,7 +77,9 @@ pub const Io = struct { switch (addr) { 0x0400_0000 => self.dispcnt.raw = halfword, 0x0400_0004 => self.dispstat.raw = halfword, + 0x0400_0008 => self.bg0cnt.raw = halfword, 0x0400_0200 => self.ie.raw = halfword, + 0x0400_0202 => self.irq.raw = halfword, 0x0400_0208 => self.ime = halfword & 1 == 1, else => std.debug.panic("[I/O:16] tried to write 0x{X:} to 0x{X:}", .{ halfword, addr }), } @@ -77,8 +95,11 @@ pub const Io = struct { }; } - pub fn write8(_: *Self, addr: u32, byte: u8) void { - std.debug.panic("[I/0:8] tried to write 0x{X:} to 0x{X:}", .{ byte, addr }); + pub fn write8(self: *Self, addr: u32, byte: u8) void { + switch (addr) { + 0x0400_0208 => self.ime = byte & 1 == 1, + else => std.debug.panic("[I/0:8] tried to write 0x{X:} to 0x{X:}", .{ byte, addr }), + } } }; @@ -148,3 +169,32 @@ const KeyInput = extern union { shoulder_l: Bit(u16, 9), raw: u16, }; + +const BackgroundControl = extern union { + bg_priority: Bitfield(u16, 0, 2), + char_base: Bitfield(u16, 2, 2), + mosaic_enable: Bit(u16, 6), + palette_type: Bit(u16, 7), + screen_base: Bitfield(u16, 8, 5), + display_overflow: Bit(u16, 13), + screen_size: Bitfield(u16, 14, 2), + raw: u16, +}; + +const InterruptRequest = extern union { + vblank: Bit(u16, 0), + hblank: Bit(u16, 1), + coincidence: Bit(u16, 2), + tim0_overflow: Bit(u16, 3), + tim1_overflow: Bit(u16, 4), + tim2_overflow: Bit(u16, 5), + tim3_overflow: Bit(u16, 6), + serial: Bit(u16, 7), + dma0: Bit(u16, 8), + dma1: Bit(u16, 9), + dma2: Bit(u16, 10), + dma3: Bit(u16, 11), + keypad: Bit(u16, 12), + game_pak: Bit(u16, 13), + raw: u16, +};