diff --git a/src/common/common.zig b/src/common/common.zig index 9c660da..8801ead 100644 --- a/src/common/common.zig +++ b/src/common/common.zig @@ -3,3 +3,4 @@ pub const memory = @import("memory.zig"); pub const glue = @import("glue.zig"); pub const clock = @import("clock.zig"); pub const filter = @import("filter.zig"); +pub const utils = @import("utils.zig"); diff --git a/src/common/utils.zig b/src/common/utils.zig new file mode 100644 index 0000000..fa7c22b --- /dev/null +++ b/src/common/utils.zig @@ -0,0 +1,29 @@ +const std = @import("std"); +const assert = std.debug.assert; + +// shorthand for std.mem.copyForwards +pub fn cp(src: []const u8, dst: []u8) void { + std.mem.copyForwards(u8, dst, src); +} + +// 32-bit xorshifter +pub fn xorshift32(in_x: u32) u32 { + var x = in_x; + x ^= x << 13; + x ^= x >> 17; + x ^= x << 5; + return x; +} + +pub fn fillNoise(slice: []u8, seed: u32) u32 { + assert((slice.len & 3) == 0); + var x = seed; + for (0..(slice.len >> 2)) |i| { + x = xorshift32(x); + slice[i * 4] = @truncate(x); + slice[i * 4 + 1] = @truncate(x >> 8); + slice[i * 4 + 2] = @truncate(x >> 16); + slice[i * 4 + 3] = @truncate(x >> 24); + } + return x; +} diff --git a/src/systems/bombjack.zig b/src/systems/bombjack.zig index 8b99b13..07e0dcd 100644 --- a/src/systems/bombjack.zig +++ b/src/systems/bombjack.zig @@ -7,6 +7,7 @@ const common = @import("common"); const memory = common.memory; const clock = common.clock; const pin = common.bitutils.pin; +const cp = common.utils.cp; const AudioCallback = common.glue.AudioCallback; const AudioOptions = common.glue.AudioOptions; const DisplayInfo = common.glue.DisplayInfo; @@ -842,10 +843,6 @@ pub const Bombjack = struct { self.main_board.palette[pal_index] = c; } - fn cp(src: []const u8, dst: []u8) void { - std.mem.copyForwards(u8, dst, src); - } - fn initMainRom(opts: Options) [5][0x2000]u8 { var rom: [5][0x2000]u8 = undefined; cp(opts.roms.main_0000_1FFF, &rom[0]); diff --git a/src/systems/kc85.zig b/src/systems/kc85.zig index 55ce931..c579a89 100644 --- a/src/systems/kc85.zig +++ b/src/systems/kc85.zig @@ -9,6 +9,8 @@ const common = @import("common"); const memory = common.memory; const clock = common.clock; const pin = common.bitutils.pin; +const cp = common.utils.cp; +const fillNoise = common.utils.fillNoise; const AudioCallback = common.glue.AudioCallback; const AudioOptions = common.glue.AudioOptions; const DisplayInfo = common.glue.DisplayInfo; @@ -287,7 +289,7 @@ pub fn Type(comptime model: Model) type { mem: Memory, // memory buffers - ram: [8][0x4000]u8, // up to 8 16-KByte RAM banks + ram: [8][0x4000]u8, rom: Rom, ext_buf: [EXP.BUF_SIZE]u8, fb: [DISPLAY.FB_SIZE]u8 align(128), @@ -303,8 +305,20 @@ pub fn Type(comptime model: Model) type { .junk_page = &self.junk_page, .unmapped_page = &self.unmapped_page, }), - // FIXME: on KC85/2, /3 fill with noise - .ram = std.mem.zeroes(@TypeOf(self.ram)), + .ram = init: { + var arr: [8][0x4000]u8 = undefined; + if (model == .KC854) { + // on KC85/4, RAM is filled with zeroes + arr = std.mem.zeroes(@TypeOf(self.ram)); + } else { + // on KC85/2, /3 RAM is filled with noise + var x: u32 = 0x6D98302B; // seed for xorshift32 + inline for (0..8) |i| { + x = fillNoise(&arr[i], x); + } + } + break :init arr; + }, .rom = initRoms(opts), .ext_buf = std.mem.zeroes(@TypeOf(self.ext_buf)), .fb = std.mem.zeroes(@TypeOf(self.fb)), @@ -349,10 +363,6 @@ pub fn Type(comptime model: Model) type { }; } - fn cp(src: []const u8, dst: []u8) void { - std.mem.copyForwards(u8, dst, src); - } - fn initRoms(opts: Options) Rom { var rom: Rom = undefined; switch (model) { diff --git a/src/systems/namco.zig b/src/systems/namco.zig index db95700..c6b856f 100644 --- a/src/systems/namco.zig +++ b/src/systems/namco.zig @@ -40,6 +40,7 @@ const memory = common.memory; const clock = common.clock; const filter = common.filter; const pin = common.bitutils.pin; +const cp = common.utils.cp; const AudioCallback = common.glue.AudioCallback; const AudioOptions = common.glue.AudioOptions; const DisplayInfo = common.glue.DisplayInfo; @@ -765,10 +766,6 @@ pub fn Type(comptime sys: System) type { } } - fn cp(src: []const u8, dst: []u8) void { - std.mem.copyForwards(u8, dst, src); - } - fn initSysRom(opts: Options) [MEMMAP.CPU_ROM_SIZE]u8 { var rom: [MEMMAP.CPU_ROM_SIZE]u8 = undefined; if (sys == .Pacman) {