2022-10-21 08:11:46 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
const Bit = @import("bitfield").Bit;
|
|
|
|
const Bitfield = @import("bitfield").Bitfield;
|
2022-10-21 08:11:46 +00:00
|
|
|
|
|
|
|
pub const Io = struct {
|
2022-10-21 08:11:50 +00:00
|
|
|
const Self = @This();
|
|
|
|
|
2022-10-21 08:11:54 +00:00
|
|
|
dispcnt: DisplayControl,
|
|
|
|
dispstat: DisplayStatus,
|
2022-10-21 08:11:49 +00:00
|
|
|
vcount: VCount,
|
2022-10-21 08:11:54 +00:00
|
|
|
/// Read / Write
|
|
|
|
ime: bool,
|
|
|
|
ie: InterruptEnable,
|
2022-10-21 08:12:07 +00:00
|
|
|
keyinput: KeyInput,
|
2022-10-21 08:11:46 +00:00
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
pub fn init() Self {
|
2022-10-21 08:11:46 +00:00
|
|
|
return .{
|
2022-10-21 08:12:07 +00:00
|
|
|
.dispcnt = .{ .raw = 0x0000 },
|
|
|
|
.dispstat = .{ .raw = 0x0000 },
|
|
|
|
.vcount = .{ .raw = 0x0000 },
|
2022-10-21 08:11:54 +00:00
|
|
|
.ime = false,
|
2022-10-21 08:12:07 +00:00
|
|
|
.ie = .{ .raw = 0x0000 },
|
2022-10-21 08:12:09 +00:00
|
|
|
.keyinput = .{ .raw = 0x03FF },
|
2022-10-21 08:11:46 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
pub fn read32(self: *const Self, addr: u32) u32 {
|
2022-10-21 08:11:46 +00:00
|
|
|
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:52 +00:00
|
|
|
0x0400_0006 => @as(u32, self.vcount.raw),
|
2022-10-21 08:11:54 +00:00
|
|
|
0x0400_0200 => @as(u32, self.ie.raw),
|
|
|
|
0x0400_0208 => @boolToInt(self.ime),
|
2022-10-21 08:11:46 +00:00
|
|
|
else => std.debug.panic("[I/O:32] tried to read from {X:}", .{addr}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:54 +00:00
|
|
|
pub fn write32(self: *Self, addr: u32, word: u32) void {
|
|
|
|
switch (addr) {
|
2022-10-21 08:12:07 +00:00
|
|
|
0x0400_0000 => self.dispcnt.raw = @truncate(u16, word),
|
2022-10-21 08:11:54 +00:00
|
|
|
0x0400_0200 => self.ie.raw = @truncate(u16, word),
|
|
|
|
0x0400_0208 => self.ime = word & 1 == 1,
|
|
|
|
else => std.debug.panic("[I/O:32] tried to write 0x{X:} to 0x{X:}", .{ word, addr }),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
pub fn read16(self: *const Self, addr: u32) u16 {
|
2022-10-21 08:11:46 +00:00
|
|
|
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:52 +00:00
|
|
|
0x0400_0006 => self.vcount.raw,
|
2022-10-21 08:12:07 +00:00
|
|
|
0x0400_0130 => self.keyinput.raw,
|
2022-10-21 08:11:54 +00:00
|
|
|
0x0400_0200 => self.ie.raw,
|
|
|
|
0x0400_0208 => @boolToInt(self.ime),
|
2022-10-21 08:11:46 +00:00
|
|
|
else => std.debug.panic("[I/O:16] tried to read from {X:}", .{addr}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
pub fn write16(self: *Self, addr: u32, halfword: u16) void {
|
2022-10-21 08:11:46 +00:00
|
|
|
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:54 +00:00
|
|
|
0x0400_0200 => self.ie.raw = halfword,
|
|
|
|
0x0400_0208 => self.ime = halfword & 1 == 1,
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
pub fn read8(self: *const Self, addr: u32) u8 {
|
2022-10-21 08:11:46 +00:00
|
|
|
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:54 +00:00
|
|
|
0x0400_0200 => @truncate(u8, self.ie.raw),
|
2022-10-21 08:11:52 +00:00
|
|
|
0x0400_0006 => @truncate(u8, self.vcount.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:53 +00:00
|
|
|
|
2022-10-21 08:11:54 +00:00
|
|
|
pub fn write8(_: *Self, addr: u32, byte: u8) void {
|
2022-10-21 08:11:53 +00:00
|
|
|
std.debug.panic("[I/0:8] tried to write 0x{X:} to 0x{X:}", .{ byte, addr });
|
|
|
|
}
|
2022-10-21 08:11:46 +00:00
|
|
|
};
|
|
|
|
|
2022-10-21 08:11:54 +00:00
|
|
|
/// Read / Write
|
|
|
|
const DisplayControl = 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:52 +00:00
|
|
|
hblank_interval_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:54 +00:00
|
|
|
/// Read / Write
|
|
|
|
const DisplayStatus = extern union {
|
2022-10-21 08:11:48 +00:00
|
|
|
vblank: Bit(u16, 0),
|
|
|
|
hblank: Bit(u16, 1),
|
2022-10-21 08:11:52 +00:00
|
|
|
coincidence: Bit(u16, 2),
|
2022-10-21 08:11:48 +00:00
|
|
|
vblank_irq: Bit(u16, 3),
|
|
|
|
hblank_irq: Bit(u16, 4),
|
|
|
|
vcount_irq: Bit(u16, 5),
|
2022-10-21 08:11:52 +00:00
|
|
|
vcount_trigger: Bitfield(u16, 8, 8),
|
2022-10-21 08:11:48 +00:00
|
|
|
raw: u16,
|
2022-10-21 08:11:46 +00:00
|
|
|
};
|
2022-10-21 08:11:49 +00:00
|
|
|
|
2022-10-21 08:11:54 +00:00
|
|
|
/// Read Only
|
2022-10-21 08:11:49 +00:00
|
|
|
const VCount = extern union {
|
|
|
|
scanline: Bitfield(u16, 0, 8),
|
|
|
|
raw: u16,
|
|
|
|
};
|
2022-10-21 08:11:54 +00:00
|
|
|
|
|
|
|
/// Read / Write
|
|
|
|
const InterruptEnable = extern union {
|
|
|
|
vblank: Bit(u16, 0),
|
|
|
|
hblank: Bit(u16, 1),
|
|
|
|
coincidence: Bit(u16, 2),
|
|
|
|
tm0_overflow: Bit(u16, 3),
|
|
|
|
tm1_overflow: Bit(u16, 4),
|
|
|
|
tm2_overflow: Bit(u16, 5),
|
|
|
|
tm3_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,
|
|
|
|
};
|
2022-10-21 08:12:07 +00:00
|
|
|
|
|
|
|
/// Read Only
|
|
|
|
/// 0 = Pressed, 1 = Released
|
|
|
|
const KeyInput = extern union {
|
|
|
|
a: Bit(u16, 0),
|
|
|
|
b: Bit(u16, 1),
|
|
|
|
select: Bit(u16, 2),
|
|
|
|
start: Bit(u16, 3),
|
|
|
|
right: Bit(u16, 4),
|
|
|
|
left: Bit(u16, 5),
|
|
|
|
up: Bit(u16, 6),
|
|
|
|
down: Bit(u16, 7),
|
|
|
|
shoulder_r: Bit(u16, 8),
|
|
|
|
shoulder_l: Bit(u16, 9),
|
|
|
|
raw: u16,
|
|
|
|
};
|