usynth

simple midi synth for sndio
git clone git://nsmpr.xyz/usynth.git
Log | Files | Refs

commit 68e312370a8980b7ffa844bd234fe534d0bf3321
parent c4980b3ce46dc737361a291de8916fcdb6b40386
Author: Pavel Renev <an2qzavok@gmail.com>
Date:   Thu, 12 Aug 2021 17:19:33 +0300

rearrange how machine instructions are stored for easier future modifications

Diffstat:
Mbuild.c | 23++++++++---------------
Mmachine.c | 56+++++++++++++++++++-------------------------------------
Mmachine.h | 23++++++++++++++++++++---
Musynth.c | 2+-
4 files changed, 48 insertions(+), 56 deletions(-)

diff --git a/build.c b/build.c @@ -8,7 +8,7 @@ #include "build.h" const Label builtin[] = { - /* memory mapping */ + /* vm memory mapping */ {"VM_PC", VM_PC}, {"VM_ACTIVE", VM_ACTIVE}, {"VM_HALTED", VM_HALTED}, @@ -19,19 +19,6 @@ const Label builtin[] = { {"VM_STACK", VM_STACK}, {"VM_PROG_START", VM_PROG_START}, {"VM_MEM_END", VM_MEM_END}, - /* processor instructions */ - {"halt", I_HALT}, - {"lit", I_LIT}, - {"fetch", I_FETCH}, - {"op", I_OP}, - {"adsr", I_ADSR}, - {"wave", I_WAVE}, - {"2dt", I_2DT}, - {"dup", I_DUP}, - {"drop", I_DROP}, - {"swap", I_SWAP}, - {"add", I_ADD}, - {"comb", I_COMB}, }; @@ -151,7 +138,7 @@ run2(Array *labels, Array *words, int16_t *prog) int build(char *dat, int16_t *prog) { - int r; + int i, r; Array *tokens; Array *labels; Array *words; @@ -166,6 +153,12 @@ build(char *dat, int16_t *prog) labels->p = realloc(labels->p, sizeof(builtin)); memcpy(labels->p, builtin, sizeof(builtin)); + for (i = 0; vm_iset[i].instr != NULL; i++) { + Label *l; + l = arrayinc(labels); + *l = (Label) {vm_iset[i].name, i}; + } + splittokens(dat, tokens); if ((run1(tokens, labels, words) != 0) || (run2(labels, words, prog) != 0)) { diff --git a/machine.c b/machine.c @@ -11,56 +11,38 @@ #define STOS(x) (x[VM_STACK + x[VM_SP] - 2]) #define DATA(x) (x[x[VM_PC]]) -typedef void (*instruction)(int16_t*, uint8_t); +struct Iset vm_iset[] = { + [I_HALT] = { i_halt, "halt" }, + [I_LIT] = { i_lit, "lit" }, + [I_FETCH] = { i_fetch, "fetch" }, + [I_OP] = { i_op, "op" }, + [I_ADSR] = { i_adsr, "adsr" }, + [I_WAVE] = { i_wave, "wave" }, + [I_2DT] = { i_2dt, "2dt" }, + [I_DUP] = { i_dup, "dup" }, + [I_DROP] = { i_drop, "drop" }, + [I_SWAP] = { i_swap, "swap" }, + [I_ADD] = { i_add, "add" }, + [I_COMB] = { i_comb, "comb" }, + { NULL, NULL }, +}; -void vm_fatal(int16_t*, char*); -void vm_adsr(int16_t*); -void i_halt(int16_t*, uint8_t); -void i_lit(int16_t*, uint8_t); -void i_fetch(int16_t*, uint8_t); -void i_op(int16_t*, uint8_t); -void i_adsr(int16_t*, uint8_t); -void i_wave(int16_t*, uint8_t); -void i_2dt(int16_t*, uint8_t); -void i_dup(int16_t*, uint8_t); -void i_drop(int16_t*, uint8_t); -void i_swap(int16_t*, uint8_t); -void i_add(int16_t*, uint8_t); -void i_comb(int16_t*, uint8_t); - -instruction instructions[128] = { - [I_HALT] = i_halt, - [I_LIT] = i_lit, - [I_FETCH] = i_fetch, - [I_OP] = i_op, - [I_ADSR] = i_adsr, - [I_WAVE] = i_wave, - [I_2DT] = i_2dt, - [I_DUP] = i_dup, - [I_DROP] = i_drop, - [I_SWAP] = i_swap, - [I_ADD] = i_add, - [I_COMB] = i_comb, -}; -void -vm_set(int16_t *vm) -{ - vm[VM_ACTIVE] = 0; -} +void vm_fatal(int16_t*, char*); +void vm_adsr(int16_t*); int16_t vm_run(int16_t *vm) { - instruction inst; + void (*inst)(int16_t*, uint8_t); if (vm[VM_ACTIVE] == 0) return 0; vm[VM_PC] = VM_PROG_START; vm[VM_HALTED] = 0; vm[VM_SP] = 0; //vm_adsr(vm); while (vm[VM_HALTED] == 0) { - inst = instructions[0xff & DATA(vm)]; + inst = vm_iset[0xff & DATA(vm)].instr; if (inst == 0) vm_fatal(vm, "illegal instruction"); vm[VM_PC]++; inst(vm, 0); diff --git a/machine.h b/machine.h @@ -17,6 +17,26 @@ enum { I_COMB, }; +void i_halt(int16_t*, uint8_t); +void i_lit(int16_t*, uint8_t); +void i_fetch(int16_t*, uint8_t); +void i_op(int16_t*, uint8_t); +void i_adsr(int16_t*, uint8_t); +void i_wave(int16_t*, uint8_t); +void i_2dt(int16_t*, uint8_t); +void i_dup(int16_t*, uint8_t); +void i_drop(int16_t*, uint8_t); +void i_swap(int16_t*, uint8_t); +void i_add(int16_t*, uint8_t); +void i_comb(int16_t*, uint8_t); + +struct Iset{ + void (*instr)(int16_t*, uint8_t); + char *name; +}; + +extern struct Iset vm_iset[]; + /* VM's memory state */ enum { VM_PC, @@ -31,7 +51,4 @@ enum { VM_MEM_END = 4096, }; -int16_t* vm_malloc(void); -void vm_set(int16_t*); int16_t vm_run(int16_t*); - diff --git a/usynth.c b/usynth.c @@ -94,7 +94,7 @@ main(int argc, char *argv[]) m_vector.note_off = note_off; vm = malloc(VM_MEM_END); - vm_set(vm); + vm[VM_ACTIVE] = 0; buildfile(fp, vm); while (1){