From b708d025af98047e6d128a6e7c9537ef8aa80885 Mon Sep 17 00:00:00 2001 From: SuperAuguste <19855629+SuperAuguste@users.noreply.github.com> Date: Sun, 19 May 2024 16:03:03 +0100 Subject: [PATCH] Works but slow and crunch PCM test file not included for copyright reasons --- build.zig | 4 ++-- simulator/src/apu.ts | 1 + simulator/src/constants.ts | 2 +- simulator/src/runtime.ts | 8 ++++---- src/badge/feature_test.zig | 17 +++++++++-------- src/cart/api.zig | 2 +- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/build.zig b/build.zig index 5fbde48..cdcbd5e 100644 --- a/build.zig +++ b/build.zig @@ -144,7 +144,7 @@ pub const Cart = struct { pub fn install_with_watcher(c: *const Cart, d: *Build.Dependency, b: *Build, opt: CartWatcherOptions) *Build.Step.Run { if (opt.build_firmware) { - c.mz.install_firmware(b, c.fw, .{ .format = .{ .uf2 = .SAMD51 } }); + // c.mz.install_firmware(b, c.fw, .{ .format = .{ .uf2 = .SAMD51 } }); } const install_artifact_step = b.addInstallArtifact(c.wasm, .{}); b.getInstallStep().dependOn(&install_artifact_step.step); @@ -196,7 +196,7 @@ pub fn add_cart( wasm.initial_memory = 64 * 65536; wasm.max_memory = 64 * 65536; wasm.stack_size = 14752; - wasm.global_base = 2 * 512 * 2 + 0xa01e; + wasm.global_base = 100_000; wasm.rdynamic = true; wasm.root_module.addImport("cart-api", d.module("cart-api")); diff --git a/simulator/src/apu.ts b/simulator/src/apu.ts index 9eb7320..ccc1942 100644 --- a/simulator/src/apu.ts +++ b/simulator/src/apu.ts @@ -24,6 +24,7 @@ export class APU { } send(left: number[], right: number[]) { + console.log(left, right); this.processorPort!.postMessage({left, right}); } diff --git a/simulator/src/constants.ts b/simulator/src/constants.ts index 18fa4b1..9b9102c 100644 --- a/simulator/src/constants.ts +++ b/simulator/src/constants.ts @@ -12,7 +12,7 @@ export const ADDR_LIGHT_LEVEL = 0x06; export const ADDR_NEOPIXELS = 0x08; export const ADDR_RED_LED = 0x1c; export const ADDR_FRAMEBUFFER = 0x1e; -export const ADDR_AUDIO_BUFFER = 0xa01e; +export const ADDR_AUDIO_BUFFER = 0xb01e; export const CONTROLS_START = 1; export const CONTROLS_SELECT = 2; diff --git a/simulator/src/runtime.ts b/simulator/src/runtime.ts index 4343338..b1a97c3 100644 --- a/simulator/src/runtime.ts +++ b/simulator/src/runtime.ts @@ -223,7 +223,7 @@ export class Runtime { this.bluescreenOnError(start_function); } - new Uint16Array(this.memory.buffer).slice(constants.ADDR_AUDIO_BUFFER, constants.ADDR_AUDIO_BUFFER + 2 * 512).fill(0); + // new Int16Array(this.memory.buffer).slice(constants.ADDR_AUDIO_BUFFER, constants.ADDR_AUDIO_BUFFER + 2 * 512).fill(0); } update () { @@ -242,10 +242,10 @@ export class Runtime { // this.bluescreenOnError(audio_function); // } - if (audio_function(constants.ADDR_AUDIO_BUFFER, constants.ADDR_AUDIO_BUFFER + 512 * 2)) { + if (audio_function(constants.ADDR_AUDIO_BUFFER)) { this.apu.send( - [...new Uint16Array(this.memory.buffer).slice(constants.ADDR_AUDIO_BUFFER, constants.ADDR_AUDIO_BUFFER + 512)], - [...new Uint16Array(this.memory.buffer).slice(constants.ADDR_AUDIO_BUFFER + 512, constants.ADDR_AUDIO_BUFFER + 512 * 2)], + [...new Int16Array(this.memory.buffer.slice(constants.ADDR_AUDIO_BUFFER)).slice(0, 512)], + [...new Int16Array(this.memory.buffer.slice(constants.ADDR_AUDIO_BUFFER + 512 * 2)).slice(0, 512)], ); } } diff --git a/src/badge/feature_test.zig b/src/badge/feature_test.zig index c0d0c07..60a61aa 100644 --- a/src/badge/feature_test.zig +++ b/src/badge/feature_test.zig @@ -155,14 +155,15 @@ export fn update() void { }); } -var off: f32 = 0; -export fn audio(buffer: *volatile [2][512]u16) bool { - for (&buffer[0], &buffer[1]) |*l, *r| { - l.* = @intFromFloat(@sin(off) * std.math.maxInt(u16)); - r.* = @intFromFloat(@sin(off) * std.math.maxInt(u16)); +const raw_data = std.mem.bytesAsSlice(i16, @embedFile("pepsi.raw")); - off += 0.1; - } +var audio_offset: usize = 0; + +export fn audio(buffer: *volatile [2][512]i16) bool { + @memcpy(&buffer[0], raw_data[audio_offset..][0..512]); + @memcpy(&buffer[1], raw_data[audio_offset..][0..512]); + + audio_offset += 512; - return false; + return true; } diff --git a/src/cart/api.zig b/src/cart/api.zig index 2c1644f..54d4ed7 100644 --- a/src/cart/api.zig +++ b/src/cart/api.zig @@ -3,7 +3,7 @@ //! To get started, define the following functions: //! - `export fn start() void {}` //! - `export fn update() void {}` -//! - `export fn audio(buffer: *volatile [2][512]u16) bool {}` +//! - `export fn audio(buffer: *volatile [2][512]i16) bool {}` const std = @import("std"); const builtin = @import("builtin");