Compare commits

..

13 Commits

Author SHA1 Message Date
Rekai Nyangadzayi Musuka f715585867 chore: dont allocate not-small ?Sprite array on stack
use memset like most other allocations in this emu
2022-10-23 04:46:07 -03:00
Rekai Nyangadzayi Musuka cab40efc59 chore: move FrameBuffer struct to util.zig 2022-10-23 04:46:07 -03:00
Rekai Nyangadzayi Musuka a17300a8e0 chore: move OAM, PALRAM and VRAM structs to separate files 2022-10-23 04:46:07 -03:00
Rekai Nyangadzayi Musuka 2ebe1c0b0e fix: 8-bit writes to WIN PPU registers
Advance Wars depends on these registers similar to Mario Kart's 8-bit
writes to Affine Background registers:
2022-10-23 04:46:07 -03:00
Rekai Nyangadzayi Musuka 6db70638fe chore: refactor window 2022-10-23 04:46:07 -03:00
Rekai Nyangadzayi Musuka bc5ab5810a chore: crude background window impl (no affine) 2022-10-23 04:46:07 -03:00
Rekai Nyangadzayi Musuka decf2a01c9 chore: rename function (misspelt until now somehow) 2022-10-23 04:46:07 -03:00
Rekai Nyangadzayi Musuka 4b8ed3cebb fix(io): resolve embarrasingly simple regression
introduced in 21eddac31e
2022-10-23 04:39:31 -03:00
Rekai Nyangadzayi Musuka 928ce674d9 fix(cpu): fix obscure LDRSH behaviour 2022-10-22 22:12:41 -03:00
Rekai Nyangadzayi Musuka 945dbec013 fix(open-bus): don't rotate result
Rotating misaligned reads is the responsibility of the CPU
2022-10-22 21:32:36 -03:00
Rekai Nyangadzayi Musuka dd98066a34 Merge pull request 'feat(dma): Implement DMA Latch' (#5) from dma-latch into main
Reviewed-on: #5
2022-10-22 23:53:21 +00:00
Rekai Nyangadzayi Musuka a2868dfe9e feat(dma): Implement DMA Latch 2022-10-22 20:52:02 -03:00
Rekai Nyangadzayi Musuka 22979d9450 fix(bios): fix regression
was reading addr_latch + 8, which is a remnant from when I was faking
the pipeline
2022-10-22 15:33:36 -03:00
7 changed files with 30 additions and 21 deletions

View File

@ -169,7 +169,7 @@ fn openBus(self: *const Self, comptime T: type, address: u32) T {
} }
}; };
return @truncate(T, rotr(u32, word, 8 * (address & 3))); return @truncate(T, word);
} }
pub fn read(self: *Self, comptime T: type, address: u32) T { pub fn read(self: *Self, comptime T: type, address: u32) T {

View File

@ -19,7 +19,7 @@ pub fn read(self: *Self, comptime T: type, r15: u32, addr: u32) T {
} }
log.debug("Rejected read since r15=0x{X:0>8}", .{r15}); log.debug("Rejected read since r15=0x{X:0>8}", .{r15});
return @truncate(T, self._read(T, self.addr_latch + 8)); return @truncate(T, self._read(T, self.addr_latch));
} }
pub fn dbgRead(self: *const Self, comptime T: type, r15: u32, addr: u32) T { pub fn dbgRead(self: *const Self, comptime T: type, r15: u32, addr: u32) T {

View File

@ -11,6 +11,8 @@ const log = std.log.scoped(.DmaTransfer);
const setHi = util.setHi; const setHi = util.setHi;
const setLo = util.setLo; const setLo = util.setLo;
const rotr = @import("../../util.zig").rotr;
pub fn create() DmaTuple { pub fn create() DmaTuple {
return .{ DmaController(0).init(), DmaController(1).init(), DmaController(2).init(), DmaController(3).init() }; return .{ DmaController(0).init(), DmaController(1).init(), DmaController(2).init(), DmaController(3).init() };
} }
@ -99,6 +101,7 @@ fn DmaController(comptime id: u2) type {
const sad_mask: u32 = if (id == 0) 0x07FF_FFFF else 0x0FFF_FFFF; const sad_mask: u32 = if (id == 0) 0x07FF_FFFF else 0x0FFF_FFFF;
const dad_mask: u32 = if (id != 3) 0x07FF_FFFF else 0x0FFF_FFFF; const dad_mask: u32 = if (id != 3) 0x07FF_FFFF else 0x0FFF_FFFF;
const WordCount = if (id == 3) u16 else u14;
/// Write-only. The first address in a DMA transfer. (DMASAD) /// Write-only. The first address in a DMA transfer. (DMASAD)
/// Note: use writeSrc instead of manipulating src_addr directly /// Note: use writeSrc instead of manipulating src_addr directly
@ -107,17 +110,19 @@ fn DmaController(comptime id: u2) type {
/// Note: Use writeDst instead of manipulatig dst_addr directly /// Note: Use writeDst instead of manipulatig dst_addr directly
dad: u32, dad: u32,
/// Write-only. The Word Count for the DMA Transfer (DMACNT_L) /// Write-only. The Word Count for the DMA Transfer (DMACNT_L)
word_count: if (id == 3) u16 else u14, word_count: WordCount,
/// Read / Write. DMACNT_H /// Read / Write. DMACNT_H
/// Note: Use writeControl instead of manipulating cnt directly. /// Note: Use writeControl instead of manipulating cnt directly.
cnt: DmaControl, cnt: DmaControl,
/// Internal. The last successfully read value
data_latch: u32,
/// Internal. Currrent Source Address /// Internal. Currrent Source Address
sad_latch: u32, sad_latch: u32,
/// Internal. Current Destination Address /// Internal. Current Destination Address
dad_latch: u32, dad_latch: u32,
/// Internal. Word Count /// Internal. Word Count
_word_count: if (id == 3) u16 else u14, _word_count: WordCount,
/// Some DMA Transfers are enabled during Hblank / VBlank and / or /// Some DMA Transfers are enabled during Hblank / VBlank and / or
/// have delays. Thefore bit 15 of DMACNT isn't actually something /// have delays. Thefore bit 15 of DMACNT isn't actually something
@ -134,6 +139,8 @@ fn DmaController(comptime id: u2) type {
// Internals // Internals
.sad_latch = 0, .sad_latch = 0,
.dad_latch = 0, .dad_latch = 0,
.data_latch = 0,
._word_count = 0, ._word_count = 0,
.in_progress = false, .in_progress = false,
}; };
@ -158,7 +165,7 @@ fn DmaController(comptime id: u2) type {
// Reload Internals on Rising Edge. // Reload Internals on Rising Edge.
self.sad_latch = self.sad; self.sad_latch = self.sad;
self.dad_latch = self.dad; self.dad_latch = self.dad;
self._word_count = if (self.word_count == 0) std.math.maxInt(@TypeOf(self._word_count)) else self.word_count; self._word_count = if (self.word_count == 0) std.math.maxInt(WordCount) else self.word_count;
// Only a Start Timing of 00 has a DMA Transfer immediately begin // Only a Start Timing of 00 has a DMA Transfer immediately begin
self.in_progress = new.start_timing.read() == 0b00; self.in_progress = new.start_timing.read() == 0b00;
@ -181,11 +188,19 @@ fn DmaController(comptime id: u2) type {
const offset: u32 = if (transfer_type) @sizeOf(u32) else @sizeOf(u16); const offset: u32 = if (transfer_type) @sizeOf(u32) else @sizeOf(u16);
const mask = if (transfer_type) ~@as(u32, 3) else ~@as(u32, 1); const mask = if (transfer_type) ~@as(u32, 3) else ~@as(u32, 1);
const sad_addr = self.sad_latch & mask;
const dad_addr = self.dad_latch & mask;
if (transfer_type) { if (transfer_type) {
cpu.bus.write(u32, self.dad_latch & mask, cpu.bus.read(u32, self.sad_latch & mask)); if (sad_addr >= 0x0200_0000) self.data_latch = cpu.bus.read(u32, sad_addr);
cpu.bus.write(u32, dad_addr, self.data_latch);
} else { } else {
cpu.bus.write(u16, self.dad_latch & mask, cpu.bus.read(u16, self.sad_latch & mask)); if (sad_addr >= 0x0200_0000) {
const value: u32 = cpu.bus.read(u16, sad_addr);
self.data_latch = value << 16 | value;
}
cpu.bus.write(u16, dad_addr, @truncate(u16, rotr(u32, self.data_latch, 8 * (dad_addr & 3))));
} }
switch (sad_adj) { switch (sad_adj) {

View File

@ -11,8 +11,8 @@ const Bus = @import("../Bus.zig");
const DmaController = @import("dma.zig").DmaController; const DmaController = @import("dma.zig").DmaController;
const Scheduler = @import("../scheduler.zig").Scheduler; const Scheduler = @import("../scheduler.zig").Scheduler;
const setHi = util.setLo; const setHi = util.setHi;
const setLo = util.setHi; const setLo = util.setLo;
const log = std.log.scoped(.@"I/O"); const log = std.log.scoped(.@"I/O");

View File

@ -35,11 +35,8 @@ pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I:
}, },
0b11 => { 0b11 => {
// LDRSH // LDRSH
result = if (address & 1 == 1) blk: { const value = bus.read(u16, address);
break :blk sext(u32, u8, bus.read(u8, address)); result = if (address & 1 == 1) sext(u32, u8, @truncate(u8, value >> 8)) else sext(u32, u16, value);
} else blk: {
break :blk sext(u32, u16, bus.read(u16, address));
};
}, },
0b00 => unreachable, // SWP 0b00 => unreachable, // SWP
} }

View File

@ -46,11 +46,8 @@ pub fn fmt78(comptime op: u2, comptime T: bool) InstrFn {
}, },
0b11 => { 0b11 => {
// LDRSH // LDRSH
cpu.r[rd] = if (address & 1 == 1) blk: { const value = bus.read(u16, address);
break :blk sext(u32, u8, bus.read(u8, address)); cpu.r[rd] = if (address & 1 == 1) sext(u32, u8, @truncate(u8, value >> 8)) else sext(u32, u16, value);
} else blk: {
break :blk sext(u32, u16, bus.read(u16, address));
};
}, },
} }
} else { } else {

View File

@ -276,7 +276,7 @@ pub const audio = struct {
}; };
/// Sets the high bits of an integer to a value /// Sets the high bits of an integer to a value
pub inline fn setHi(comptime T: type, left: T, right: HalfInt(T)) T { pub inline fn setLo(comptime T: type, left: T, right: HalfInt(T)) T {
return switch (T) { return switch (T) {
u32 => (left & 0xFFFF_0000) | right, u32 => (left & 0xFFFF_0000) | right,
u16 => (left & 0xFF00) | right, u16 => (left & 0xFF00) | right,
@ -286,7 +286,7 @@ pub inline fn setHi(comptime T: type, left: T, right: HalfInt(T)) T {
} }
/// sets the low bits of an integer to a value /// sets the low bits of an integer to a value
pub inline fn setLo(comptime T: type, left: T, right: HalfInt(T)) T { pub inline fn setHi(comptime T: type, left: T, right: HalfInt(T)) T {
return switch (T) { return switch (T) {
u32 => (left & 0x0000_FFFF) | @as(u32, right) << 16, u32 => (left & 0x0000_FFFF) | @as(u32, right) << 16,
u16 => (left & 0x00FF) | @as(u16, right) << 8, u16 => (left & 0x00FF) | @as(u16, right) << 8,