#include "sys.h"

int main() {
  uint16_t addr;
  uint8_t len;
  uint16_t val;

  led0->output = 1;

  while(1) {
    uint8_t c = uart_read(uart0);
    if (c == 'c') {
      c = uart_read(uart0);
      addr = c << 8;
      c = uart_read(uart0);
      addr |= c;

      len = uart_read(uart0);
      for (int i = 0; i < len; i += 2) {
        c = uart_read(uart0);
        val = c << 8;
        c = uart_read(uart0);
        val |= c;

        *((uint16_t*)(addr + i)) = val;
      }

      uart_write(uart0, 'a');
      uart_write(uart0, len);
      led0->output = 0xf0;
    } else if (c == 'j') {
      c = uart_read(uart0);
      addr = c << 8;
      c = uart_read(uart0);
      addr |= c;

      ((void (*)())addr)();
    }
  }
}
