chore: simply some zig fmt builtin changes

This commit is contained in:
2023-07-17 01:07:18 -05:00
parent d96c9c01ff
commit d4b7167e29
22 changed files with 587 additions and 589 deletions

View File

@@ -27,7 +27,7 @@ pub fn read(self: *Self, comptime T: type, r15: u32, address: u32) T {
log.warn("Open Bus! Read from 0x{X:0>8}, but PC was 0x{X:0>8}", .{ address, r15 });
const value = self._read(u32, self.addr_latch);
return @as(T, @truncate(rotr(u32, value, 8 * rotateBy(T, address))));
return @truncate(rotr(u32, value, 8 * rotateBy(T, address)));
}
fn rotateBy(comptime T: type, address: u32) u32 {
@@ -43,7 +43,7 @@ pub fn dbgRead(self: *const Self, comptime T: type, r15: u32, address: u32) T {
if (r15 < Self.size) return self._read(T, forceAlign(T, address));
const value = self._read(u32, self.addr_latch);
return @as(T, @truncate(rotr(u32, value, 8 * rotateBy(T, address))));
return @truncate(rotr(u32, value, 8 * rotateBy(T, address)));
}
/// Read without the GBA safety checks

View File

@@ -77,7 +77,7 @@ inline fn get(self: *const Self, i: u32) u8 {
if (i < self.buf.len) return self.buf[i];
const lhs = i >> 1 & 0xFFFF;
return @as(u8, @truncate(lhs >> 8 * @as(u5, @truncate(i & 1))));
return @truncate(lhs >> 8 * @as(u5, @truncate(i & 1)));
}
pub fn dbgRead(self: *const Self, comptime T: type, address: u32) T {
@@ -139,7 +139,7 @@ pub fn write(self: *Self, comptime T: type, word_count: u16, address: u32, value
const addr = address & 0x1FF_FFFF;
if (self.backup.kind == .Eeprom) {
const bit = @as(u1, @truncate(value));
const bit: u1 = @truncate(value);
if (self.buf.len > 0x100_0000) { // Large
// Addresses 0x1FF_FF00 to 0x1FF_FFFF are reserved from EEPROM accesses if

View File

@@ -77,7 +77,7 @@ pub const Backup = struct {
switch (addr) {
0x0000 => if (self.kind == .Flash1M and self.flash.set_bank) {
self.flash.bank = @as(u1, @truncate(byte));
self.flash.bank = @truncate(byte);
},
0x5555 => {
if (self.flash.state == .Command) {

View File

@@ -108,7 +108,7 @@ pub const Eeprom = struct {
switch (self.state) {
.Ready => {
if (self.writer.len() == 2) {
const req = @as(u2, @intCast(self.writer.finish()));
const req: u2 = @intCast(self.writer.finish());
switch (req) {
0b11 => self.state = .Read,
0b10 => self.state = .Write,
@@ -120,7 +120,7 @@ pub const Eeprom = struct {
switch (self.kind) {
.Large => {
if (self.writer.len() == 14) {
const addr = @as(u10, @intCast(self.writer.finish()));
const addr: u10 = @intCast(self.writer.finish());
const value = std.mem.readIntSliceLittle(u64, buf[@as(u13, addr) * 8 ..][0..8]);
self.reader.configure(value);
@@ -130,7 +130,7 @@ pub const Eeprom = struct {
.Small => {
if (self.writer.len() == 6) {
// FIXME: Duplicated code from above
const addr = @as(u6, @intCast(self.writer.finish()));
const addr: u6 = @intCast(self.writer.finish());
const value = std.mem.readIntSliceLittle(u64, buf[@as(u13, addr) * 8 ..][0..8]);
self.reader.configure(value);
@@ -186,11 +186,9 @@ const Reader = struct {
fn read(self: *Self) u1 {
if (!self.enabled) return 1;
const bit = if (self.i < 4) blk: {
break :blk 0;
} else blk: {
const idx = @as(u6, @intCast(63 - (self.i - 4)));
break :blk @as(u1, @truncate(self.data >> idx));
const bit: u1 = if (self.i < 4) 0 else blk: {
const idx: u6 = @intCast(63 - (self.i - 4));
break :blk @truncate(self.data >> idx);
};
self.i = (self.i + 1) % (64 + 4);
@@ -202,11 +200,11 @@ const Reader = struct {
fn dbgRead(self: *const Self) u1 {
if (!self.enabled) return 1;
const bit = if (self.i < 4) blk: {
const bit: u1 = if (self.i < 4) blk: {
break :blk 0;
} else blk: {
const idx = @as(u6, @intCast(63 - (self.i - 4)));
break :blk @as(u1, @truncate(self.data >> idx));
const idx: u6 = @intCast(63 - (self.i - 4));
break :blk @truncate(self.data >> idx);
};
return bit;
@@ -230,7 +228,7 @@ const Writer = struct {
}
fn requestWrite(self: *Self, bit: u1) void {
const idx = @as(u1, @intCast(1 - self.i));
const idx: u1 = @intCast(1 - self.i);
self.data = (self.data & ~(@as(u64, 1) << idx)) | (@as(u64, bit) << idx);
self.i += 1;
}
@@ -244,13 +242,13 @@ const Writer = struct {
.Unknown => unreachable,
};
const idx = @as(u4, @intCast(size - self.i));
const idx: u4 = @intCast(size - self.i);
self.data = (self.data & ~(@as(u64, 1) << idx)) | (@as(u64, bit) << idx);
self.i += 1;
}
fn dataWrite(self: *Self, bit: u1) void {
const idx = @as(u6, @intCast(63 - self.i));
const idx: u6 = @intCast(63 - self.i);
self.data = (self.data & ~(@as(u64, 1) << idx)) | (@as(u64, bit) << idx);
self.i += 1;
}

View File

@@ -20,7 +20,7 @@ pub fn create() DmaTuple {
}
pub fn read(comptime T: type, dma: *const DmaTuple, addr: u32) ?T {
const byte_addr = @as(u8, @truncate(addr));
const byte_addr: u8 = @truncate(addr);
return switch (T) {
u32 => switch (byte_addr) {
@@ -55,19 +55,19 @@ pub fn read(comptime T: type, dma: *const DmaTuple, addr: u32) ?T {
u8 => switch (byte_addr) {
0xB0...0xB7 => null, // DMA0SAD, DMA0DAD
0xB8, 0xB9 => 0x00, // DMA0CNT_L
0xBA, 0xBB => @as(T, @truncate(dma.*[0].dmacntH() >> getHalf(byte_addr))),
0xBA, 0xBB => @truncate(dma.*[0].dmacntH() >> getHalf(byte_addr)),
0xBC...0xC3 => null, // DMA1SAD, DMA1DAD
0xC4, 0xC5 => 0x00, // DMA1CNT_L
0xC6, 0xC7 => @as(T, @truncate(dma.*[1].dmacntH() >> getHalf(byte_addr))),
0xC6, 0xC7 => @truncate(dma.*[1].dmacntH() >> getHalf(byte_addr)),
0xC8...0xCF => null, // DMA2SAD, DMA2DAD
0xD0, 0xD1 => 0x00, // DMA2CNT_L
0xD2, 0xD3 => @as(T, @truncate(dma.*[2].dmacntH() >> getHalf(byte_addr))),
0xD2, 0xD3 => @truncate(dma.*[2].dmacntH() >> getHalf(byte_addr)),
0xD4...0xDB => null, // DMA3SAD, DMA3DAD
0xDC, 0xDD => 0x00, // DMA3CNT_L
0xDE, 0xDF => @as(T, @truncate(dma.*[3].dmacntH() >> getHalf(byte_addr))),
0xDE, 0xDF => @truncate(dma.*[3].dmacntH() >> getHalf(byte_addr)),
else => util.io.read.err(T, log, "unexpected {} read from 0x{X:0>8}", .{ T, addr }),
},
else => @compileError("DMA: Unsupported read width"),
@@ -75,7 +75,7 @@ pub fn read(comptime T: type, dma: *const DmaTuple, addr: u32) ?T {
}
pub fn write(comptime T: type, dma: *DmaTuple, addr: u32, value: T) void {
const byte_addr = @as(u8, @truncate(addr));
const byte_addr: u8 = @truncate(addr);
switch (T) {
u32 => switch (byte_addr) {
@@ -209,7 +209,7 @@ fn DmaController(comptime id: u2) type {
}
pub fn setDmacntL(self: *Self, halfword: u16) void {
self.word_count = @as(@TypeOf(self.word_count), @truncate(halfword));
self.word_count = @truncate(halfword);
}
pub fn dmacntH(self: *const Self) u16 {
@@ -233,16 +233,16 @@ fn DmaController(comptime id: u2) type {
}
pub fn setDmacnt(self: *Self, word: u32) void {
self.setDmacntL(@as(u16, @truncate(word)));
self.setDmacntH(@as(u16, @truncate(word >> 16)));
self.setDmacntL(@truncate(word));
self.setDmacntH(@truncate(word >> 16));
}
pub fn step(self: *Self, cpu: *Arm7tdmi) void {
const bus_ptr: *Bus = @ptrCast(@alignCast(cpu.bus.ptr));
const is_fifo = (id == 1 or id == 2) and self.cnt.start_timing.read() == 0b11;
const sad_adj = @as(Adjustment, @enumFromInt(self.cnt.sad_adj.read()));
const dad_adj = if (is_fifo) .Fixed else @as(Adjustment, @enumFromInt(self.cnt.dad_adj.read()));
const sad_adj: Adjustment = @enumFromInt(self.cnt.sad_adj.read());
const dad_adj: Adjustment = if (is_fifo) .Fixed else @enumFromInt(self.cnt.dad_adj.read());
const transfer_type = is_fifo or self.cnt.transfer_type.read();
const offset: u32 = if (transfer_type) @sizeOf(u32) else @sizeOf(u16);

View File

@@ -32,7 +32,7 @@ pub const Gpio = struct {
return switch (self.kind) {
.Rtc => blk: {
const clock: *Clock = @ptrCast(@alignCast(self.ptr.?));
break :blk clock.step(Clock.Data{ .raw = value });
break :blk clock.step(.{ .raw = value });
},
.None => value,
};
@@ -146,16 +146,16 @@ pub const Clock = struct {
/// 2. A `count`, which keeps track of which byte is currently being read
/// 3. An index, which keeps track of which bit of the byte determined by `count` is being read
fn read(self: *Reader, clock: *const Clock, register: Register) u1 {
const idx = @as(u3, @intCast(self.i));
const idx: u3 = @intCast(self.i);
defer self.i += 1;
// FIXME: What do I do about the unused bits?
return switch (register) {
.Control => @as(u1, @truncate(switch (self.count) {
.Control => @truncate(switch (self.count) {
0 => clock.cnt.raw >> idx,
else => std.debug.panic("Tried to read from byte #{} of {} (hint: there's only 1 byte)", .{ self.count, register }),
})),
.DateTime => @as(u1, @truncate(switch (self.count) {
}),
.DateTime => @truncate(switch (self.count) {
// Date
0 => clock.year >> idx,
1 => @as(u8, clock.month) >> idx,
@@ -167,13 +167,13 @@ pub const Clock = struct {
5 => @as(u8, clock.minute) >> idx,
6 => @as(u8, clock.second) >> idx,
else => std.debug.panic("Tried to read from byte #{} of {} (hint: there's only 7 bytes)", .{ self.count, register }),
})),
.Time => @as(u1, @truncate(switch (self.count) {
}),
.Time => @truncate(switch (self.count) {
0 => @as(u8, clock.hour) >> idx,
1 => @as(u8, clock.minute) >> idx,
2 => @as(u8, clock.second) >> idx,
else => std.debug.panic("Tried to read from byte #{} of {} (hint: there's only 3 bytes)", .{ self.count, register }),
})),
}),
};
}
@@ -207,7 +207,7 @@ pub const Clock = struct {
/// Append a bit to the internal bit buffer (aka an integer)
fn push(self: *Writer, value: u1) void {
const idx = @as(u3, @intCast(self.i));
const idx: u3 = @intCast(self.i);
self.buf = (self.buf & ~(@as(u8, 1) << idx)) | @as(u8, value) << idx;
self.i += 1;
}
@@ -299,13 +299,13 @@ pub const Clock = struct {
sched_ptr.push(.RealTimeClock, (1 << 24) -| late); // Reschedule
const now = DateTime.now();
self.year = bcd(@as(u8, @intCast(now.date.year - 2000)));
self.month = @as(u5, @truncate(bcd(now.date.month)));
self.day = @as(u6, @truncate(bcd(now.date.day)));
self.weekday = @as(u3, @truncate(bcd((now.date.weekday() + 1) % 7))); // API is Monday = 0, Sunday = 6. We want Sunday = 0, Saturday = 6
self.hour = @as(u6, @truncate(bcd(now.time.hour)));
self.minute = @as(u7, @truncate(bcd(now.time.minute)));
self.second = @as(u7, @truncate(bcd(now.time.second)));
self.year = bcd(@intCast(now.date.year - 2000));
self.month = @truncate(bcd(now.date.month));
self.day = @truncate(bcd(now.date.day));
self.weekday = @truncate(bcd((now.date.weekday() + 1) % 7)); // API is Monday = 0, Sunday = 6. We want Sunday = 0, Saturday = 6
self.hour = @truncate(bcd(now.time.hour));
self.minute = @truncate(bcd(now.time.minute));
self.second = @truncate(bcd(now.time.second));
}
fn step(self: *Self, value: Data) u4 {
@@ -321,7 +321,7 @@ pub const Clock = struct {
}
}
break :blk @as(u4, @truncate(value.raw));
break :blk @truncate(value.raw);
},
.Command => blk: {
if (!value.cs.read()) log.err("Expected CS to be set during {}, however CS was cleared", .{self.state});
@@ -338,7 +338,7 @@ pub const Clock = struct {
}
}
break :blk @as(u4, @truncate(value.raw));
break :blk @truncate(value.raw);
},
.Write => |register| blk: {
if (!value.cs.read()) log.err("Expected CS to be set during {}, however CS was cleared", .{self.state});
@@ -364,7 +364,7 @@ pub const Clock = struct {
}
}
break :blk @as(u4, @truncate(value.raw));
break :blk @truncate(value.raw);
},
.Read => |register| blk: {
if (!value.cs.read()) log.err("Expected CS to be set during {}, however CS was cleared", .{self.state});
@@ -390,7 +390,7 @@ pub const Clock = struct {
}
}
break :blk @as(u4, @truncate(ret.raw));
break :blk @truncate(ret.raw);
},
};
}
@@ -429,7 +429,7 @@ pub const Clock = struct {
log.debug("Handling Command 0x{X:0>2} [0b{b:0>8}]", .{ command, command });
const is_write = command & 1 == 0;
const rtc_register = @as(u3, @truncate(command >> 1 & 0x7));
const rtc_register: u3 = @truncate(command >> 1 & 0x7);
if (is_write) {
return switch (rtc_register) {

View File

@@ -43,7 +43,7 @@ pub const Io = struct {
}
fn setIrqs(self: *Io, word: u32) void {
self.ie.raw = @as(u16, @truncate(word));
self.ie.raw = @truncate(word);
self.irq.raw &= ~@as(u16, @truncate(word >> 16));
}
};
@@ -141,11 +141,11 @@ pub fn read(bus: *const Bus, comptime T: type, address: u32) ?T {
0x0400_015A, 0x0400_015B => 0x00,
// Interrupts
0x0400_0200, 0x0400_0201 => @as(T, @truncate(bus.io.ie.raw >> getHalf(@as(u8, @truncate(address))))),
0x0400_0202, 0x0400_0203 => @as(T, @truncate(bus.io.irq.raw >> getHalf(@as(u8, @truncate(address))))),
0x0400_0204, 0x0400_0205 => @as(T, @truncate(bus.io.waitcnt.raw >> getHalf(@as(u8, @truncate(address))))),
0x0400_0200, 0x0400_0201 => @truncate(bus.io.ie.raw >> getHalf(@truncate(address))),
0x0400_0202, 0x0400_0203 => @truncate(bus.io.irq.raw >> getHalf(@truncate(address))),
0x0400_0204, 0x0400_0205 => @truncate(bus.io.waitcnt.raw >> getHalf(@truncate(address))),
0x0400_0206, 0x0400_0207 => 0x00,
0x0400_0208, 0x0400_0209 => @as(T, @truncate(@as(u16, @intFromBool(bus.io.ime)) >> getHalf(@as(u8, @truncate(address))))),
0x0400_0208, 0x0400_0209 => @truncate(@as(u16, @intFromBool(bus.io.ime)) >> getHalf(@truncate(address))),
0x0400_020A, 0x0400_020B => 0x00,
0x0400_0300 => @intFromEnum(bus.io.postflg),
0x0400_0301 => null,
@@ -196,10 +196,10 @@ pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
// Interrupts
0x0400_0200 => bus.io.setIrqs(value),
0x0400_0204 => bus.io.waitcnt.set(@as(u16, @truncate(value))),
0x0400_0204 => bus.io.waitcnt.set(@truncate(value)),
0x0400_0208 => bus.io.ime = value & 1 == 1,
0x0400_0300 => {
bus.io.postflg = @as(PostFlag, @enumFromInt(value & 1));
bus.io.postflg = @enumFromInt(value & 1);
bus.io.haltcnt = if (value >> 15 & 1 == 0) .Halt else @panic("TODO: Implement STOP");
},
else => util.io.write.undef(log, "Tried to write 0x{X:0>8}{} to 0x{X:0>8}", .{ value, T, address }),
@@ -246,7 +246,7 @@ pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
0x0400_0208 => bus.io.ime = value & 1 == 1,
0x0400_020A => {},
0x0400_0300 => {
bus.io.postflg = @as(PostFlag, @enumFromInt(value & 1));
bus.io.postflg = @enumFromInt(value & 1);
bus.io.haltcnt = if (value >> 15 & 1 == 0) .Halt else @panic("TODO: Implement STOP");
},
else => util.io.write.undef(log, "Tried to write 0x{X:0>4}{} to 0x{X:0>8}", .{ value, T, address }),
@@ -273,16 +273,16 @@ pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
0x0400_0140 => log.debug("Wrote 0x{X:0>2} to JOYCNT_L", .{value}),
// Interrupts
0x0400_0200, 0x0400_0201 => bus.io.ie.raw = setHalf(u16, bus.io.ie.raw, @as(u8, @truncate(address)), value),
0x0400_0200, 0x0400_0201 => bus.io.ie.raw = setHalf(u16, bus.io.ie.raw, @truncate(address), value),
0x0400_0202 => bus.io.irq.raw &= ~@as(u16, value),
0x0400_0203 => bus.io.irq.raw &= ~@as(u16, value) << 8, // TODO: Is this good?
0x0400_0204, 0x0400_0205 => bus.io.waitcnt.set(setHalf(u16, @as(u16, @truncate(bus.io.waitcnt.raw)), @as(u8, @truncate(address)), value)),
0x0400_0204, 0x0400_0205 => bus.io.waitcnt.set(setHalf(u16, bus.io.waitcnt.raw, @truncate(address), value)),
0x0400_0206, 0x0400_0207 => {},
0x0400_0208 => bus.io.ime = value & 1 == 1,
0x0400_0209 => {},
0x0400_020A, 0x0400_020B => {},
0x0400_0300 => bus.io.postflg = @as(PostFlag, @enumFromInt(value & 1)),
0x0400_0300 => bus.io.postflg = @enumFromInt(value & 1),
0x0400_0301 => bus.io.haltcnt = if (value >> 7 & 1 == 0) .Halt else std.debug.panic("TODO: Implement STOP", .{}),
0x0400_0410 => log.debug("Wrote 0x{X:0>2} to the common yet undocumented 0x{X:0>8}", .{ value, address }),

View File

@@ -19,7 +19,7 @@ pub fn create(sched: *Scheduler) TimerTuple {
}
pub fn read(comptime T: type, tim: *const TimerTuple, addr: u32) ?T {
const nybble_addr = @as(u4, @truncate(addr));
const nybble_addr: u4 = @truncate(addr);
return switch (T) {
u32 => switch (nybble_addr) {
@@ -44,24 +44,24 @@ pub fn read(comptime T: type, tim: *const TimerTuple, addr: u32) ?T {
else => util.io.read.err(T, log, "unaligned {} read from 0x{X:0>8}", .{ T, addr }),
},
u8 => switch (nybble_addr) {
0x0, 0x1 => @as(T, @truncate(tim.*[0].timcntL() >> getHalf(nybble_addr))),
0x2, 0x3 => @as(T, @truncate(tim.*[0].cnt.raw >> getHalf(nybble_addr))),
0x0, 0x1 => @truncate(tim.*[0].timcntL() >> getHalf(nybble_addr)),
0x2, 0x3 => @truncate(tim.*[0].cnt.raw >> getHalf(nybble_addr)),
0x4, 0x5 => @as(T, @truncate(tim.*[1].timcntL() >> getHalf(nybble_addr))),
0x6, 0x7 => @as(T, @truncate(tim.*[1].cnt.raw >> getHalf(nybble_addr))),
0x4, 0x5 => @truncate(tim.*[1].timcntL() >> getHalf(nybble_addr)),
0x6, 0x7 => @truncate(tim.*[1].cnt.raw >> getHalf(nybble_addr)),
0x8, 0x9 => @as(T, @truncate(tim.*[2].timcntL() >> getHalf(nybble_addr))),
0xA, 0xB => @as(T, @truncate(tim.*[2].cnt.raw >> getHalf(nybble_addr))),
0x8, 0x9 => @truncate(tim.*[2].timcntL() >> getHalf(nybble_addr)),
0xA, 0xB => @truncate(tim.*[2].cnt.raw >> getHalf(nybble_addr)),
0xC, 0xD => @as(T, @truncate(tim.*[3].timcntL() >> getHalf(nybble_addr))),
0xE, 0xF => @as(T, @truncate(tim.*[3].cnt.raw >> getHalf(nybble_addr))),
0xC, 0xD => @truncate(tim.*[3].timcntL() >> getHalf(nybble_addr)),
0xE, 0xF => @truncate(tim.*[3].cnt.raw >> getHalf(nybble_addr)),
},
else => @compileError("TIM: Unsupported read width"),
};
}
pub fn write(comptime T: type, tim: *TimerTuple, addr: u32, value: T) void {
const nybble_addr = @as(u4, @truncate(addr));
const nybble_addr: u4 = @truncate(addr);
return switch (T) {
u32 => switch (nybble_addr) {
@@ -151,8 +151,8 @@ fn Timer(comptime id: u2) type {
/// TIMCNT_L & TIMCNT_H
pub fn setTimcnt(self: *Self, word: u32) void {
self.setTimcntL(@as(u16, @truncate(word)));
self.setTimcntH(@as(u16, @truncate(word >> 16)));
self.setTimcntL(@truncate(word));
self.setTimcntH(@truncate(word >> 16));
}
/// TIMCNT_H
@@ -167,7 +167,7 @@ fn Timer(comptime id: u2) type {
self.sched.removeScheduledEvent(.{ .TimerOverflow = id });
// Counter should hold the value it stopped at meaning we have to calculate it now
self._counter +%= @as(u16, @truncate((self.sched.now() - self._start_timestamp) / self.frequency()));
self._counter +%= @truncate((self.sched.now() - self._start_timestamp) / self.frequency());
}
// the timer has always been enabled, but the cascade bit which was blocking the timer has been unset