Skip to content

Commit

Permalink
Works but slow and crunch
Browse files Browse the repository at this point in the history
PCM test file not included for copyright reasons
  • Loading branch information
SuperAuguste committed May 19, 2024
1 parent dc5ea82 commit b708d02
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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"));
Expand Down
1 change: 1 addition & 0 deletions simulator/src/apu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class APU {
}

send(left: number[], right: number[]) {
console.log(left, right);
this.processorPort!.postMessage({left, right});
}

Expand Down
2 changes: 1 addition & 1 deletion simulator/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions simulator/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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)],
);
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/badge/feature_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/cart/api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit b708d02

Please sign in to comment.