/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-â
âvi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8                                :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¡
â Copyright 2020 Justine Alexandra Roberts Tunney                              â
â                                                                              â
â Permission to use, copy, modify, and/or distribute this software for         â
â any purpose with or without fee is hereby granted, provided that the         â
â above copyright notice and this permission notice appear in all copies.      â
â                                                                              â
â THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL                â
â WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED                â
â WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE             â
â AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL         â
â DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR        â
â PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER               â
â TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR             â
â PERFORMANCE OF THIS SOFTWARE.                                                â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "tool/build/emubin/poke.h"
#include "tool/build/emubin/real.h"
#include "tool/build/emubin/realstart.inc"

#define signbit(x) __builtin_signbit(x)

static const unsigned char kBlocks[] = {
    [0b1111] = 0xdb,  // â
    [0b0110] = 0xdb,  // â
    [0b1001] = 0xdb,  // â
    [0b0111] = 0xdb,  // â
    [0b1011] = 0xdb,  // â
    [0b1110] = 0xdb,  // â
    [0b1101] = 0xdb,  // â
    [0b1100] = 0xdc,  // â
    [0b0100] = 0xdc,  // â
    [0b1000] = 0xdc,  // â
    [0b0101] = 0xdd,  // â
    [0b1010] = 0xde,  // â
    [0b0011] = 0xdf,  // â
    [0b0010] = 0xdf,  // â
    [0b0001] = 0xdf,  // â
};

forceinline void *SetMemory(void *di, int al, unsigned long cx) {
  asm("rep stosb"
      : "=D"(di), "=c"(cx), "=m"(*(char(*)[cx])di)
      : "0"(di), "1"(cx), "a"(al));
  return di;
}

forceinline long double pi(void) {
  long double x;
  asm("fldpi" : "=t"(x));
  return x;
}

forceinline long double tau(void) {
  return pi() * 2;
}

forceinline void sincosl_(long double x, long double *sin, long double *cos) {
  asm("fsincos" : "=t"(*sin), "=u"(*cos) : "0"(x));
}

forceinline long double atan2l_(long double x, long double y) {
  asm("fpatan" : "+t"(x) : "u"(y) : "st(1)");
  return x;
}

forceinline long lrintl_(long double x) {
  long i;
  asm("fistp%z0\t%0" : "=m"(i) : "t"(x) : "st");
  return i;
}

forceinline long double truncl_(long double x) {
  asm("frndint" : "+t"(x));
  return x;
}

forceinline long double fabsl_(long double x) {
  asm("fabs" : "+t"(x));
  return x;
}

forceinline long lroundl_(long double x) {
  int s = signbit(x);
  x = truncl_(fabsl_(x) + .5);
  if (s) x = -x;
  return lrintl_(x);
}

static unsigned short GetFpuControlWord(void) {
  unsigned short cw;
  asm("fnstcw\t%0" : "=m"(cw));
  return cw;
}

static void SetFpuControlWord(unsigned short cw) {
  asm volatile("fldcw\t%0" : /* no outputs */ : "m"(cw));
}

static void InitializeFpu(void) {
  asm("fninit");
}

static void SetTruncationRounding(void) {
  SetFpuControlWord(GetFpuControlWord() | 0x0c00);
}

static void spiral(unsigned char p[25][80][2], unsigned char B[25][80], int g) {
  int i, x, y;
  long double a, b, u, v, h;
  for (a = b = i = 0; i < 1000; ++i) {
    sincosl_(a, &u, &v);
    h = atan2l_(u, v) - .333L * g;
    x = lroundl_(80 + u * b);
    y = lroundl_(25 + v * b * (1. / ((266 / 64.) * (900 / 1600.))));
    B[y >> 1][x >> 1] |= 1 << ((y & 1) << 1 | (x & 1));
    POKE(p[y >> 1][x >> 1][0], kBlocks[B[y >> 1][x >> 1]]);
    POKE(p[y >> 1][x >> 1][1], (lrintl_((h + tau()) * (8 / tau())) & 7) + 8);
    a += .05;
    b += .05;
  }
}

int main() {
  int g;
  InitializeFpu();
  SetTruncationRounding();
  SetVideoMode(3);
  for (g = 0;; ++g) {
    SetEs(0);
    SetMemory((void *)(0x7c00 + 512), 0, 25 * 80);
    SetEs(0xb8000 >> 4);
    /* SetMemory((void *)0, 0, 25 * 80 * 2); */
    spiral((void *)0, (void *)(0x7c00 + 512), g);
  }
}
