Hello, I’m working on writing a gameboy emulator to learn zig. I’m trying to implement the game cartridge and need to model the different types of memory available. I’m trying to use a tagged union, but when switching over the values I keep getting this error and I cannot figure it out. Any help is appreciated.
Error:
src\mbc.zig:36:41: error: expected type '*mbc.Rom', found '*const mbc.Rom'
Self.rom => |rom| return rom.read_byte(address),
~~~^~~~~~~~~~
src\mbc.zig:36:41: note: cast discards const qualifier
src\mbc.zig:64:28: note: parameter type declared here
pub fn read_byte(self: *Rom, address: u16) u8 {
Code:
pub const MBC = union(enum) {
rom: Rom,
mbc1: MBC1,
mbc2: MBC2,
mbc3: MBC3,
mbc5: MBC5,
const Self = @This();
pub fn read_byte(self: Self, address: u16) u8 {
switch (self) {
Self.rom => |rom| return rom.read_byte(address), <-- Error is on this line
Self.mbc1 => |mbc1| return mbc1.read_byte(address),
Self.mbc2 => |mbc2| return mbc2.read_byte(address),
Self.mbc3 => |mbc3| return mbc3.read_byte(address),
Self.mbc5 => |mbc5| return mbc5.read_byte(address),
}
}
};
pub const Rom = struct {
data: []const u8,
pub fn init(data: []const u8) Rom {
return Rom{ .data = data };
}
pub fn read_byte(self: *Rom, address: u16) u8 {
return self.data[address];
}
pub fn write_byte(_: *Rom, _: u16, _: u8) void {}
pub fn size(self: *Rom) usize {
return self.data.len;
}
pub fn save(_: *Rom) void {}
};
// other implementations omitted