2022-10-21 08:11:46 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const bitfield = @import("bitfield");
|
|
|
|
|
|
|
|
const Bitfield = bitfield.Bitfield;
|
|
|
|
const Bit = bitfield.Bit;
|
|
|
|
|
|
|
|
pub const Io = struct {
|
2022-10-21 08:11:49 +00:00
|
|
|
dispcnt: DispCnt,
|
|
|
|
dispstat: DispStat,
|
|
|
|
vcount: VCount,
|
2022-10-21 08:11:46 +00:00
|
|
|
|
|
|
|
pub fn init() @This() {
|
|
|
|
return .{
|
2022-10-21 08:11:48 +00:00
|
|
|
.dispcnt = .{ .raw = 0x0000_0000 },
|
|
|
|
.dispstat = .{ .raw = 0x0000_0000 },
|
2022-10-21 08:11:49 +00:00
|
|
|
.vcount = .{ .raw = 0x0000_0000 },
|
2022-10-21 08:11:46 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read32(self: *const @This(), addr: u32) u32 {
|
|
|
|
return switch (addr) {
|
2022-10-21 08:11:48 +00:00
|
|
|
0x0400_0000 => @as(u32, self.dispcnt.raw),
|
|
|
|
0x0400_0004 => @as(u32, self.dispstat.raw),
|
2022-10-21 08:11:46 +00:00
|
|
|
else => std.debug.panic("[I/O:32] tried to read from {X:}", .{addr}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read16(self: *const @This(), addr: u32) u16 {
|
|
|
|
return switch (addr) {
|
2022-10-21 08:11:48 +00:00
|
|
|
0x0400_0000 => self.dispcnt.raw,
|
|
|
|
0x0400_0004 => self.dispstat.raw,
|
2022-10-21 08:11:46 +00:00
|
|
|
else => std.debug.panic("[I/O:16] tried to read from {X:}", .{addr}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write16(self: *@This(), addr: u32, halfword: u16) void {
|
|
|
|
switch (addr) {
|
2022-10-21 08:11:48 +00:00
|
|
|
0x0400_0000 => self.dispcnt.raw = halfword,
|
|
|
|
0x0400_0004 => self.dispstat.raw = halfword,
|
2022-10-21 08:11:46 +00:00
|
|
|
else => std.debug.panic("[I/O:16] tried to write 0x{X:} to 0x{X:}", .{ halfword, addr }),
|
2022-10-21 08:11:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read8(self: *const @This(), addr: u32) u8 {
|
|
|
|
return switch (addr) {
|
2022-10-21 08:11:48 +00:00
|
|
|
0x0400_0000 => @truncate(u8, self.dispcnt.raw),
|
|
|
|
0x0400_0004 => @truncate(u8, self.dispstat.raw),
|
2022-10-21 08:11:46 +00:00
|
|
|
else => std.debug.panic("[I/O:8] tried to read from {X:}", .{addr}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
const DispCnt = extern union {
|
2022-10-21 08:11:46 +00:00
|
|
|
bg_mode: Bitfield(u16, 0, 3),
|
|
|
|
frame_select: Bit(u16, 4),
|
2022-10-21 08:11:48 +00:00
|
|
|
hblank_interraw_free: Bit(u16, 5),
|
2022-10-21 08:11:46 +00:00
|
|
|
obj_mapping: Bit(u16, 6),
|
|
|
|
forced_blank: Bit(u16, 7),
|
|
|
|
bg_enable: Bitfield(u16, 8, 4),
|
|
|
|
obj_enable: Bit(u16, 12),
|
|
|
|
win_enable: Bitfield(u16, 13, 2),
|
|
|
|
obj_win_enable: Bit(u16, 15),
|
2022-10-21 08:11:48 +00:00
|
|
|
raw: u16,
|
|
|
|
};
|
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
const DispStat = extern union {
|
2022-10-21 08:11:48 +00:00
|
|
|
vblank: Bit(u16, 0),
|
|
|
|
hblank: Bit(u16, 1),
|
|
|
|
vcount: Bit(u16, 2),
|
|
|
|
vblank_irq: Bit(u16, 3),
|
|
|
|
hblank_irq: Bit(u16, 4),
|
|
|
|
vcount_irq: Bit(u16, 5),
|
|
|
|
vcount_setting: Bitfield(u16, 8, 7),
|
|
|
|
raw: u16,
|
2022-10-21 08:11:46 +00:00
|
|
|
};
|
2022-10-21 08:11:49 +00:00
|
|
|
|
|
|
|
const VCount = extern union {
|
|
|
|
scanline: Bitfield(u16, 0, 8),
|
|
|
|
raw: u16,
|
|
|
|
};
|