Skip to content

Commit

Permalink
Nearing a solution
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperAuguste committed May 20, 2024
1 parent 754b9d9 commit 49cd7b5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 26 deletions.
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_BATTERY_LEVEL = 0x1e;
export const ADDR_FRAMEBUFFER = 0x1e;
export const ADDR_FRAMEBUFFER = 0x20;
export const ADDR_AUDIO_BUFFER = 0xa020;

export const CONTROLS_START = 1;
Expand Down
10 changes: 9 additions & 1 deletion src/badge.zig
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ pub fn main() !void {
});

timer.init();
audio.init(&cart.call_audio);
audio.init();

// Light sensor adc
microzig.board.A6_LIGHT.set_mux(.B);
Expand Down Expand Up @@ -254,6 +254,14 @@ pub fn main() !void {

cart.tick();

if (!audio.audio_ready) {
// microzig.cpu.disable_interrupts();
// defer microzig.cpu.enable_interrupts();

cart.call_audio();
audio.audio_ready = true;
}

var pixels: [5]board.NeopixelColor = undefined;
for (&pixels, cart.api.neopixels) |*local, pixel|
local.* = .{
Expand Down
10 changes: 5 additions & 5 deletions src/badge/cart.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ const libcart = struct {
extern fn audio() void;

export fn __return_thunk__() linksection(".text.cart") noreturn {
asm volatile (" svc #12");
asm volatile (" svc #11");
unreachable;
}
};

pub fn svcall_handler() callconv(.Naked) void {
asm volatile (
\\ mvns r0, lr, lsl #31 - 2
\\ bcc 12f
\\ bcc 11f
\\ ite mi
\\ movmi r1, sp
\\ mrspl r1, psp
\\ ldr r2, [r1, #6 * 4]
\\ subs r2, #2
\\ ldrb r3, [r2, #1 * 1]
\\ cmp r3, #0xDF
\\ bne 12f
\\ bne 11f
\\ ldrb r3, [r2, #0 * 1]
\\ cmp r3, #11
\\ bhi 12f
Expand Down Expand Up @@ -168,7 +168,7 @@ fn call(func: *const fn () callconv(.C) void) void {
frame[7] = 1 << 24;
asm volatile (
\\ msr psp, %[process_stack]
\\ svc #12
\\ svc #11
:
: [process_stack] "r" (frame.ptr),
: "r0", "r1", "r2", "r3", "memory"
Expand All @@ -191,7 +191,7 @@ fn call(func: *const fn () callconv(.C) void) void {
}

pub fn call_audio() void {
call(libcart.audio);
call(&libcart.audio);
}

fn User(comptime T: type) type {
Expand Down
12 changes: 6 additions & 6 deletions src/badge/feature_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,12 @@ export fn update() void {
var wave_t: f32 = 0;

export fn audio() void {
// for (&cart.audio_buffer[0], &cart.audio_buffer[1]) |*l, *r| {
// const val: i16 = @intFromFloat(@sin(wave_t) * std.math.maxInt(i16));
for (&cart.audio_buffer[0], &cart.audio_buffer[1]) |*l, *r| {
const val: i16 = @intFromFloat(@sin(wave_t) * std.math.maxInt(i16));

// l.* = val;
// r.* = val;
l.* = val;
r.* = val;

// wave_t += 0.1;
// }
wave_t += 0.1;
}
}
22 changes: 11 additions & 11 deletions src/board/audio.zig
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
pub var audio_ready = false;
pub const sample_buffer: *volatile [2][512]i16 = @ptrFromInt(0x20000000 + 0xa020);
var call_audio: ?*const fn () void = null;

pub fn init(call_audio_fn: *const fn () void) void {
pub fn init() void {
@setCold(true);
call_audio = call_audio_fn;

board.A0_SPKR.set_dir(.out);
board.A1_VCC.set_dir(.in);
Expand Down Expand Up @@ -185,16 +184,17 @@ pub fn init(call_audio_fn: *const fn () void) void {
}

pub fn mix() callconv(.C) void {
// var speaker_enable: port.Level = .low;
const speaker_enable: port.Level = .low;
var speaker_enable: port.Level = .low;

// if (call_audio) |ca| ca();
if (audio_ready) {
for (&sample_buffer[
(dma.get_audio_part() + sample_buffer.len - 1) % sample_buffer.len
]) |sample| {
if (sample != 0) speaker_enable = .high;
}

// for (&sample_buffer[
// (dma.get_audio_part() + sample_buffer.len - 1) % sample_buffer.len
// ]) |sample| {
// if (sample != 0) speaker_enable = .high;
// }
audio_ready = false;
}

board.SPKR_EN.write(speaker_enable);
dma.ack_audio();
Expand Down
4 changes: 2 additions & 2 deletions 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]i16) bool {}`
//! - `export fn audio() void {}`

const std = @import("std");
const builtin = @import("builtin");
Expand Down Expand Up @@ -86,7 +86,7 @@ pub const neopixels: *[5]NeopixelColor = @ptrFromInt(base + 0x08);
pub const red_led: *bool = @ptrFromInt(base + 0x1c);
pub const battery_level: *u12 = @ptrFromInt(base + 0x1e);
pub const framebuffer: *volatile [screen_width][screen_height]Pixel = @ptrFromInt(base + 0x20);
// pub const audio_buffer: *volatile [2][512]i16 = @ptrFromInt(base + 0xa020);
pub const audio_buffer: *volatile [2][512]i16 = @ptrFromInt(base + 0xa020);

pub const BlitOptions = struct {
pub const Flags = packed struct(u32) {
Expand Down

0 comments on commit 49cd7b5

Please sign in to comment.