Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes and improvements to the APU #716

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion cli/lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@ async function start (cartFile, opts) {
}

// Serve the WASM-4 developer runtime.
app.use(express.static(path.resolve(__dirname, "../assets/runtime/developer-build")));
app.use(express.static(
path.resolve(__dirname, "../assets/runtime/developer-build"),
{
setHeaders: function (res, path, stat) {
// These COOP and COEP headers allow us to get high-precision time.
// https://developer.mozilla.org/en-US/docs/Web/API/Performance/now#security_requirements
res.set("Cross-Origin-Opener-Policy", "same-origin");
res.set("Cross-Origin-Embedder-Policy", "require-corp");
}
}
));


const first_port = opts.port;
Expand Down
259 changes: 184 additions & 75 deletions runtimes/native/src/apu.c

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions runtimes/native/src/apu.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>

void w4_apuInit ();
typedef struct {
bool active;
int frequency;
int duration;
int volume;
int flags;
} MaybeToneCall;

void w4_apuTick ();
void w4_apuInit ();

void w4_apuTone (int frequency, int duration, int volume, int flags);
void w4_apuTick (MaybeToneCall toneCalls[4]);

void w4_apuWriteSamples (int16_t* output, unsigned long frames);
16 changes: 14 additions & 2 deletions runtimes/native/src/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ static Memory* memory;
static w4_Disk* disk;
static bool firstFrame;

static MaybeToneCall bufferedToneCalls[4] = {
{false, 0, 0, 0, 0},
{false, 0, 0, 0, 0},
{false, 0, 0, 0, 0},
{false, 0, 0, 0, 0},
};

void w4_runtimeInit (uint8_t* memoryBytes, w4_Disk* diskBytes) {
memory = (Memory*)memoryBytes;
disk = diskBytes;
Expand Down Expand Up @@ -128,7 +135,12 @@ void w4_runtimeTextUtf16 (const uint16_t* str, int byteLength, int x, int y) {

void w4_runtimeTone (int frequency, int duration, int volume, int flags) {
// printf("tone: %d, %d, %d, %d\n", frequency, duration, volume, flags);
w4_apuTone(frequency, duration, volume, flags);
int channelIdx = flags & 0x3;
bufferedToneCalls[channelIdx].active = true;
bufferedToneCalls[channelIdx].frequency = frequency;
bufferedToneCalls[channelIdx].duration = duration;
bufferedToneCalls[channelIdx].volume = volume;
bufferedToneCalls[channelIdx].flags = flags;
Comment on lines +138 to +143
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if keeping APU state outside of the APU is desired here.

The APU already has internal global state, so maybe this logic could be placed in apu.c instead to keep it isolated?

}

int w4_runtimeDiskr (uint8_t* dest, int size) {
Expand Down Expand Up @@ -223,7 +235,7 @@ void w4_runtimeUpdate () {
w4_framebufferClear();
}
w4_wasmCallUpdate();
w4_apuTick();
w4_apuTick(bufferedToneCalls);
uint32_t palette[4] = {
w4_read32LE(&memory->palette[0]),
w4_read32LE(&memory->palette[1]),
Expand Down
2 changes: 1 addition & 1 deletion runtimes/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
<link rel="shortcut icon" href="https://wasm4.org/img/favicon.ico">
<link rel="icon" href="favicon.ico">
<title>WASM-4 web runtime dev</title>
</head>
<body>
Expand Down
1 change: 0 additions & 1 deletion runtimes/web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion runtimes/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"lit": "^3.1.2"
},
"devDependencies": {
"@types/node": "^20.11.16",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"concurrently": "8.2.2",
Expand Down
1 change: 1 addition & 0 deletions runtimes/web/public/favicon.ico
2 changes: 1 addition & 1 deletion runtimes/web/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no">
<link rel="shortcut icon" href="https://wasm4.org/img/favicon.ico">
<link rel="shortcut icon" href="favicon.ico">
<title>WASM-4 Cart</title>
<link rel="stylesheet" href="wasm4.css">
</head>
Expand Down
3 changes: 3 additions & 0 deletions runtimes/web/src/apu-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type MaybeToneCall = [number, number, number, number] | null;

type BufferedToneCalls = [MaybeToneCall, MaybeToneCall, MaybeToneCall, MaybeToneCall];
Loading
Loading