ref: c4982a97396bc85953177c51eccfa00e3900889a
parent: 9ab87c2dcc0c3cdea05193197c473512d7d138e1
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon Feb 10 14:11:18 EST 2020
add plan 9 stuff
--- a/.gitignore
+++ b/.gitignore
@@ -44,3 +44,5 @@
# Default build dir
build/
+[a0125678vqki].out
+*.[o0125678vqki]
--- a/base.h
+++ b/base.h
@@ -1,3 +1,6 @@
+#ifdef __plan9__
+#include "plan9.h"
+#else
#pragma once
#include <assert.h>
#include <limits.h>
@@ -112,3 +115,4 @@
}
return false;
}
+#endif
--- /dev/null
+++ b/mkfile
@@ -1,0 +1,15 @@
+</$objtype/mkfile
+
+TARG=orca
+CFLAGS=$CFLAGS -p -D__plan9__
+BIN=/$objtype/bin/audio
+OFILES=\
+ field.$O\
+ gbuffer.$O\
+ plan9.$O\
+ sim.$O\
+ vmio.$O\
+
+default:V: all
+
+</sys/src/cmd/mkone
--- /dev/null
+++ b/plan9.c
@@ -1,0 +1,111 @@
+#include "plan9.h"
+#include "field.h"
+#include "gbuffer.h"
+#include "sim.h"
+
+Usz orca_round_up_power2(Usz x) {
+ assert(x <= SIZE_MAX / 2 + 1);
+ x -= 1;
+ x |= x >> 1;
+ x |= x >> 2;
+ x |= x >> 4;
+ x |= x >> 8;
+ x |= x >> 16;
+#if SIZE_MAX > UINT32_MAX
+ x |= x >> 32;
+#endif
+ return x + 1;
+}
+
+bool orca_is_valid_glyph(Glyph c) {
+ if (c >= '0' && c <= '9')
+ return true;
+ if (c >= 'A' && c <= 'Z')
+ return true;
+ if (c >= 'a' && c <= 'z')
+ return true;
+ switch (c) {
+ case '!':
+ case '#':
+ case '%':
+ case '*':
+ case '.':
+ case ':':
+ case ';':
+ case '=':
+ case '?':
+ return true;
+ }
+ return false;
+}
+
+static void
+usage(void)
+{
+ print("usage: orca [-t ticks] [-f file]\n");
+ exits("usage");
+}
+
+void
+main(int argc, char **argv)
+{
+ const char *input_file = "/fd/0";
+ int ticks = 1;
+ bool print_output = true;
+ Field field;
+
+ ARGBEGIN{
+ case 't':
+ ticks = atoi(EARGF(usage()));
+ break;
+ case 'f':
+ input_file = EARGF(usage());
+ break;
+ }ARGEND;
+
+ field_init(&field);
+ Field_load_error fle = field_load_file(input_file, &field);
+ if (fle != Field_load_error_ok) {
+ field_deinit(&field);
+ char const *errstr = "Unknown";
+ switch (fle) {
+ case Field_load_error_ok:
+ break;
+ case Field_load_error_cant_open_file:
+ errstr = "Unable to open file";
+ break;
+ case Field_load_error_too_many_columns:
+ errstr = "Grid file has too many columns";
+ break;
+ case Field_load_error_too_many_rows:
+ errstr = "Grid file has too many rows";
+ break;
+ case Field_load_error_no_rows_read:
+ errstr = "Grid file has no rows";
+ break;
+ case Field_load_error_not_a_rectangle:
+ errstr = "Grid file is not a rectangle";
+ break;
+ }
+ exits(errstr);
+ }
+ Mbuf_reusable mbuf_r;
+ mbuf_reusable_init(&mbuf_r);
+ mbuf_reusable_ensure_size(&mbuf_r, field.height, field.width);
+ Oevent_list oevent_list;
+ oevent_list_init(&oevent_list);
+ Usz max_ticks = (Usz)ticks;
+ for (Usz i = 0; i < max_ticks; ++i) {
+ mbuffer_clear(mbuf_r.buffer, field.height, field.width);
+ oevent_list_clear(&oevent_list);
+ orca_run(field.buffer, mbuf_r.buffer, field.height, field.width, i,
+ &oevent_list, 0);
+ }
+ mbuf_reusable_deinit(&mbuf_r);
+ oevent_list_deinit(&oevent_list);
+ if (print_output)
+ field_fput(&field, stdout);
+ field_deinit(&field);
+
+ exits(nil);
+}
--- /dev/null
+++ b/plan9.h
@@ -1,0 +1,40 @@
+#ifndef __PLAN9_H__
+#define __PLAN9_H__
+#include <u.h>
+#include <libc.h>
+
+typedef enum { false, true } bool;
+#define ORCA_NOINLINE
+#define ORCA_FORCEINLINE
+#define ORCA_PURE
+#define ORCA_OK_IF_UNUSED
+#define ORCA_LIKELY
+#define UINT8_MAX 0xff
+#define UINT32_C(x) ((u32int)(x))
+
+#define UINT16_MAX 0xffff
+#define UINT32_MAX 0xffffffffL
+#define SIZE_MAX UINT32_MAX
+#define NULL nil
+
+#define ORCA_Y_MAX UINT16_MAX
+#define ORCA_X_MAX UINT16_MAX
+
+typedef u8int U8;
+typedef s8int I8;
+typedef u16int U16;
+typedef s16int I16;
+typedef u32int U32;
+typedef s32int I32;
+typedef u64int U64;
+typedef s64int I64;
+typedef usize Usz;
+typedef long Isz;
+
+typedef char Glyph;
+typedef U8 Mark;
+
+Usz orca_round_up_power2(Usz x);
+bool orca_is_valid_glyph(Glyph c);
+
+#endif
--- a/sim.c
+++ b/sim.c
@@ -61,6 +61,7 @@
// Returns UINT8_MAX if not a valid note.
static U8 midi_note_number_of(Glyph g) {
+ static const I8 off[] = {0, 2, 4, 5, 7, 9, 11};
int sharp = (g & 1 << 5) >> 5; // sharp=1 if lowercase
g &= (Glyph) ~(1 << 5); // make uppercase
if (g < 'A' || g > 'Z') // A through Z only
@@ -67,7 +68,7 @@
return UINT8_MAX;
// We want C=0, D=1, E=2, etc. A and B are equivalent to H and I.
int deg = g <= 'B' ? 'G' - 'B' + g - 'A' : g - 'C';
- return (U8)(deg / 7 * 12 + (I8[]){0, 2, 4, 5, 7, 9, 11}[deg % 7] + sharp);
+ return (U8)(deg / 7 * 12 + off[deg % 7] + sharp);
}
typedef struct {