diff --git a/build.sh b/build.sh index cbc6e60..f0e845a 100755 --- a/build.sh +++ b/build.sh @@ -16,8 +16,8 @@ then clang-format -i src/uxn.c clang-format -i src/devices/screen.h clang-format -i src/devices/screen.c - clang-format -i src/devices/apu.h - clang-format -i src/devices/apu.c + clang-format -i src/devices/audio.h + clang-format -i src/devices/audio.c clang-format -i src/devices/file.h clang-format -i src/devices/file.c clang-format -i src/devices/mouse.h @@ -62,7 +62,7 @@ fi echo "Building.." ${CC} ${CFLAGS} src/uxnasm.c -o bin/uxnasm -${CC} ${CFLAGS} ${CORE} src/devices/file.c src/devices/mouse.c src/devices/controller.c src/devices/screen.c src/devices/apu.c src/uxnemu.c ${UXNEMU_LDFLAGS} -o bin/uxnemu +${CC} ${CFLAGS} ${CORE} src/devices/file.c src/devices/mouse.c src/devices/controller.c src/devices/screen.c src/devices/audio.c src/uxnemu.c ${UXNEMU_LDFLAGS} -o bin/uxnemu ${CC} ${CFLAGS} ${CORE} src/devices/file.c src/uxncli.c -o bin/uxncli if [ -d "$HOME/bin" ] diff --git a/mkfile b/mkfile index ef30cda..8a010b5 100644 --- a/mkfile +++ b/mkfile @@ -6,7 +6,7 @@ ROM=${USM:%.tal=%.rom} CFLAGS=$CFLAGS -D__plan9__ -I/sys/include/npe -I/sys/include/npe/SDL2 HFILES=\ /sys/include/npe/stdio.h\ - src/devices/apu.h\ + src/devices/audio.h\ src/devices/file.h\ src/devices/screen.h\ src/uxn.h\ @@ -35,13 +35,13 @@ bin/uxncli: file.$O uxncli.$O uxn.$O bin/uxnasm: uxnasm.$O $LD $LDFLAGS -o $target $prereq -bin/uxnemu: uxnemu.$O apu.$O file.$O screen.$O uxn.$O +bin/uxnemu: uxnemu.$O audio.$O file.$O screen.$O uxn.$O $LD $LDFLAGS -o $target $prereq (uxnasm|uxncli|uxnemu|uxn)\.$O:R: src/\1.c $CC $CFLAGS -Isrc -o $target src/$stem1.c -(apu|file|screen)\.$O:R: src/devices/\1.c +(audio|file|screen)\.$O:R: src/devices/\1.c $CC $CFLAGS -Isrc -o $target src/devices/$stem1.c nuke:V: clean diff --git a/src/devices/apu.c b/src/devices/audio.c similarity index 89% rename from src/devices/apu.c rename to src/devices/audio.c index 9c55f06..8d71220 100644 --- a/src/devices/apu.c +++ b/src/devices/audio.c @@ -1,5 +1,5 @@ #include "../uxn.h" -#include "apu.h" +#include "audio.h" /* Copyright (c) 2021 Devine Lu Linvega @@ -23,10 +23,12 @@ static Uint32 advances[12] = { 0xb504f, 0xbfc88, 0xcb2ff, 0xd7450, 0xe411f, 0xf1a1c }; +Audio audio[POLYPHONY]; + /* clang-format on */ static Sint32 -envelope(Apu *c, Uint32 age) +envelope(Audio *c, Uint32 age) { if(!c->r) return 0x0888; if(age < c->a) return 0x0888 * age / c->a; @@ -38,7 +40,7 @@ envelope(Apu *c, Uint32 age) } int -apu_render(Apu *c, Sint16 *sample, Sint16 *end) +audio_render(Audio *c, Sint16 *sample, Sint16 *end) { Sint32 s; if(!c->advance || !c->period) return 0; @@ -57,12 +59,12 @@ apu_render(Apu *c, Sint16 *sample, Sint16 *end) *sample++ += s * c->volume[0] / 0x180; *sample++ += s * c->volume[1] / 0x180; } - if(!c->advance) apu_finished_handler(c); + if(!c->advance) audio_finished_handler(c); return 1; } void -apu_start(Apu *c, Uint16 adsr, Uint8 pitch) +audio_start(Audio *c, Uint16 adsr, Uint8 pitch) { if(pitch < 108 && c->len) c->advance = advances[pitch % 12] >> (8 - pitch / 12); @@ -83,7 +85,7 @@ apu_start(Apu *c, Uint16 adsr, Uint8 pitch) } Uint8 -apu_get_vu(Apu *c) +audio_get_vu(Audio *c) { int i; Sint32 sum[2] = {0, 0}; diff --git a/src/devices/apu.h b/src/devices/audio.h similarity index 71% rename from src/devices/apu.h rename to src/devices/audio.h index 019e6e1..288b291 100644 --- a/src/devices/apu.h +++ b/src/devices/audio.h @@ -13,6 +13,7 @@ WITH REGARD TO THIS SOFTWARE. typedef signed int Sint32; #define SAMPLE_FREQUENCY 44100 +#define POLYPHONY 4 typedef struct { Uint8 *addr; @@ -20,9 +21,11 @@ typedef struct { Uint16 i, len; Sint8 volume[2]; Uint8 pitch, repeat; -} Apu; +} Audio; -int apu_render(Apu *c, Sint16 *sample, Sint16 *end); -void apu_start(Apu *c, Uint16 adsr, Uint8 pitch); -Uint8 apu_get_vu(Apu *c); -void apu_finished_handler(Apu *c); +extern Audio audio[POLYPHONY]; + +int audio_render(Audio *c, Sint16 *sample, Sint16 *end); +void audio_start(Audio *c, Uint16 adsr, Uint8 pitch); +Uint8 audio_get_vu(Audio *c); +void audio_finished_handler(Audio *c); \ No newline at end of file diff --git a/src/devices/screen.c b/src/devices/screen.c index bd1e490..0406595 100644 --- a/src/devices/screen.c +++ b/src/devices/screen.c @@ -45,8 +45,7 @@ screen_write(Screen *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color) { if(x < p->width && y < p->height) { Uint32 i = x + y * p->width; - Uint8 prev = layer->pixels[i]; - if(color != prev) { + if(color != layer->pixels[i]) { layer->pixels[i] = color; layer->changed = 1; } diff --git a/src/uxnemu.c b/src/uxnemu.c index 3d76674..a852271 100644 --- a/src/uxnemu.c +++ b/src/uxnemu.c @@ -9,7 +9,7 @@ #pragma clang diagnostic ignored "-Wtypedef-redefinition" #include #include "devices/screen.h" -#include "devices/apu.h" +#include "devices/audio.h" #include "devices/file.h" #include "devices/controller.h" #include "devices/mouse.h" @@ -30,7 +30,6 @@ WITH REGARD TO THIS SOFTWARE. #define WIDTH 64 * 8 #define HEIGHT 40 * 8 #define PAD 4 -#define POLYPHONY 4 #define BENCH 0 static SDL_Window *gWindow; @@ -41,7 +40,6 @@ static SDL_Rect gRect; /* devices */ -static Apu apu[POLYPHONY]; static Device *devsystem, *devscreen, *devmouse, *devctrl, *devaudio0, *devconsole; static Uint8 zoom = 1; static Uint32 stdin_event, audio0_event; @@ -68,17 +66,17 @@ audio_callback(void *u, Uint8 *stream, int len) Sint16 *samples = (Sint16 *)stream; SDL_memset(stream, 0, len); for(i = 0; i < POLYPHONY; ++i) - running += apu_render(&apu[i], samples, samples + len / 2); + running += audio_render(&audio[i], samples, samples + len / 2); if(!running) SDL_PauseAudioDevice(audio_id, 1); (void)u; } void -apu_finished_handler(Apu *c) +audio_finished_handler(Audio *c) { SDL_Event event; - event.type = audio0_event + (c - apu); + event.type = audio0_event + (c - audio); SDL_PushEvent(&event); } @@ -212,10 +210,10 @@ console_deo(Device *d, Uint8 port) static Uint8 audio_dei(Device *d, Uint8 port) { - Apu *c = &apu[d - devaudio0]; + Audio *c = &audio[d - devaudio0]; if(!audio_id) return d->dat[port]; switch(port) { - case 0x4: return apu_get_vu(c); + case 0x4: return audio_get_vu(c); case 0x2: poke16(d->dat, 0x2, c->i); /* fall through */ default: return d->dat[port]; } @@ -224,7 +222,7 @@ audio_dei(Device *d, Uint8 port) static void audio_deo(Device *d, Uint8 port) { - Apu *c = &apu[d - devaudio0]; + Audio *c = &audio[d - devaudio0]; if(!audio_id) return; if(port == 0xf) { SDL_LockAudioDevice(audio_id); @@ -233,7 +231,7 @@ audio_deo(Device *d, Uint8 port) c->volume[0] = d->dat[0xe] >> 4; c->volume[1] = d->dat[0xe] & 0xf; c->repeat = !(d->dat[0xf] & 0x80); - apu_start(c, peek16(d->dat, 0x8), d->dat[0xf] & 0x7f); + audio_start(c, peek16(d->dat, 0x8), d->dat[0xf] & 0x7f); SDL_UnlockAudioDevice(audio_id); SDL_PauseAudioDevice(audio_id, 0); }