<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
  int fd, sock_server, sock_client;
  struct sockaddr_in addr;
  int i, j, counter, position, size, yes = 1;
  uint32_t command, code, data, period, pulses, shdelay, shtime;
  uint32_t *coordinates;
  uint64_t buffer[1024], tmp;
  volatile void *cfg, *sts;
  volatile uint8_t *rst;
  volatile uint16_t *rd_cntr, *wr_cntr;
  volatile uint32_t *dac;
  volatile uint64_t *adc;

  size = 0;
  coordinates = malloc(4194304);

  if((fd = open("/dev/mem", O_RDWR)) < 0)
  {
    perror("open");
    return EXIT_FAILURE;
  }

  cfg = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x40000000);
  sts = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x40001000);
  dac = mmap(NULL, 16*sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x40010000);
  adc = mmap(NULL, 32*sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x40020000);

  wr_cntr = (uint16_t *)(sts + 0);
  rd_cntr = (uint16_t *)(sts + 2);

  rst = (uint8_t *)(cfg + 0);

  /* stop pulse generators */
  *rst &= ~1;

  /* configure trigger edge (0 for negative, 1 for positive) */
  *(uint8_t *)(cfg + 1) = 1;

  /* set trigger mask */
  *(uint8_t *)(cfg + 2) = 1;

  /* set trigger level */
  *(uint8_t *)(cfg + 3) = 1;

  /* set number of ADC samples per pulse */
  *(uint8_t *)(cfg + 4) = 32 - 1;

  /* set number of pulses per pixel */
  pulses = 2;
  *(uint8_t *)(cfg + 5) = pulses - 1;

  period = 25000;

  /* trigger pulse generator */
  /* number of clocks before 1 */
  *(uint32_t *)(cfg + 8) = 0;
  /* number of clocks before 0 */
  *(uint32_t *)(cfg + 12) = 200;
  /* total number of clocks (200e-6 * 125e6) */
  *(uint32_t *)(cfg + 16) = period - 1;

  /* S&H pulse generator */
  /* number of clocks before 1 */
  shdelay = 400;
  *(uint32_t *)(cfg + 20) = shdelay;
  /* number of clocks before 0 */
  shtime = 30;
  *(uint32_t *)(cfg + 24) = shdelay + shtime;
  /* total number of clocks (200e-6 * 143e6) */
  *(uint32_t *)(cfg + 28) = period - 1;

  /* ADC pulse generator */
  /* number of clocks before 1 */
  *(uint32_t *)(cfg + 32) = 2000;
  /* number of clocks before 0 (+1) */
  *(uint32_t *)(cfg + 36) = 2001;
  /* total number of clocks */
  *(uint32_t *)(cfg + 40) = 2001;

  if((sock_server = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  {
    perror("socket");
    return EXIT_FAILURE;
  }

  setsockopt(sock_server, SOL_SOCKET, SO_REUSEADDR, (void *)&yes , sizeof(yes));

  /* setup listening address */
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(1001);

  if(bind(sock_server, (struct sockaddr *)&addr, sizeof(addr)) < 0)
  {
    perror("bind");
    return EXIT_FAILURE;
  }

  listen(sock_server, 1024);

  while(1)
  {
    if((sock_client = accept(sock_server, NULL, NULL)) < 0)
    {
      perror("accept");
      return EXIT_FAILURE;
    }

    while(1)
    {
      if(recv(sock_client, &command, 4, MSG_WAITALL) <= 0) break;
      code = command >> 28;
      data = command & 0xfffffff;
      switch(code)
      {
        case 0:
          /* set trigger period */
          period = data;
          *(uint32_t *)(cfg + 16) = period - 1;
          *(uint32_t *)(cfg + 28) = period - 1;
          break;
        case 1:
          /* set trigger duration */
          *(uint32_t *)(cfg + 12) = data;
          break;
        case 2:
          /* set trigger polarity */
          if(data == 0) *rst &= ~4;
          else if(data == 1) *rst |= 4;
          break;
        case 3:
          /* set S&H delay */
          shdelay = data;
          *(uint32_t *)(cfg + 20) = shdelay;
          *(uint32_t *)(cfg + 24) = shdelay + shtime;
          break;
        case 4:
          /* set S&H duration */
          shtime = data;
          *(uint32_t *)(cfg + 24) = shdelay + shtime;
          break;
        case 5:
          /* set S&H polarity */
          if(data == 0) *rst &= ~8;
          else if(data == 1) *rst |= 8;
          break;
        case 6:
          /* set acquisition delay */
          *(uint32_t *)(cfg + 32) = data;
          *(uint32_t *)(cfg + 36) = data + 1;
          *(uint32_t *)(cfg + 40) = data + 1;
          break;
        case 7:
          /* set number of ADC samples per pulse */
          *(uint8_t *)(cfg + 4) = data - 1;
          break;
        case 8:
          /* set number of pulses per pixel */
          pulses = data;
          *(uint8_t *)(cfg + 5) = pulses - 1;
          break;
        case 9:
          /* clear coordinates */
          size = 0;
          break;
        case 10:
          /* add coordinates */
          coordinates[size] = (data & 0xfffc000) << 2 | (data & 0x3fff);
          ++size;
          break;
        case 11:
          /* start pulse generators */
          *rst |= 1;
          break;
        case 12:
          /* start scanning */
          counter = 0;
          position = 0;

          /* stop pulse generators */
          *rst &= ~1;

          /* reset DAC and ADC FIFO */<commit_msg>add verification of coordinates size to scanner.c<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
  int fd, sock_server, sock_client;
  struct sockaddr_in addr;
  int i, j, counter, position, size, yes = 1;
  uint32_t command, code, data, period, pulses, shdelay, shtime;
  uint32_t *coordinates;
  uint64_t buffer[1024], tmp;
  volatile void *cfg, *sts;
  volatile uint8_t *rst;
  volatile uint16_t *rd_cntr, *wr_cntr;
  volatile uint32_t *dac;
  volatile uint64_t *adc;

  size = 0;
  coordinates = malloc(4194304);

  if((fd = open("/dev/mem", O_RDWR)) < 0)
  {
    perror("open");
    return EXIT_FAILURE;
  }

  cfg = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x40000000);
  sts = mmap(NULL, sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x40001000);
  dac = mmap(NULL, 16*sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x40010000);
  adc = mmap(NULL, 32*sysconf(_SC_PAGESIZE), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0x40020000);

  wr_cntr = (uint16_t *)(sts + 0);
  rd_cntr = (uint16_t *)(sts + 2);

  rst = (uint8_t *)(cfg + 0);

  /* stop pulse generators */
  *rst &= ~1;

  /* configure trigger edge (0 for negative, 1 for positive) */
  *(uint8_t *)(cfg + 1) = 1;

  /* set trigger mask */
  *(uint8_t *)(cfg + 2) = 1;

  /* set trigger level */
  *(uint8_t *)(cfg + 3) = 1;

  /* set number of ADC samples per pulse */
  *(uint8_t *)(cfg + 4) = 32 - 1;

  /* set number of pulses per pixel */
  pulses = 2;
  *(uint8_t *)(cfg + 5) = pulses - 1;

  period = 25000;

  /* trigger pulse generator */
  /* number of clocks before 1 */
  *(uint32_t *)(cfg + 8) = 0;
  /* number of clocks before 0 */
  *(uint32_t *)(cfg + 12) = 200;
  /* total number of clocks (200e-6 * 125e6) */
  *(uint32_t *)(cfg + 16) = period - 1;

  /* S&H pulse generator */
  /* number of clocks before 1 */
  shdelay = 400;
  *(uint32_t *)(cfg + 20) = shdelay;
  /* number of clocks before 0 */
  shtime = 30;
  *(uint32_t *)(cfg + 24) = shdelay + shtime;
  /* total number of clocks (200e-6 * 143e6) */
  *(uint32_t *)(cfg + 28) = period - 1;

  /* ADC pulse generator */
  /* number of clocks before 1 */
  *(uint32_t *)(cfg + 32) = 2000;
  /* number of clocks before 0 (+1) */
  *(uint32_t *)(cfg + 36) = 2001;
  /* total number of clocks */
  *(uint32_t *)(cfg + 40) = 2001;

  if((sock_server = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  {
    perror("socket");
    return EXIT_FAILURE;
  }

  setsockopt(sock_server, SOL_SOCKET, SO_REUSEADDR, (void *)&yes , sizeof(yes));

  /* setup listening address */
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  addr.sin_port = htons(1001);

  if(bind(sock_server, (struct sockaddr *)&addr, sizeof(addr)) < 0)
  {
    perror("bind");
    return EXIT_FAILURE;
  }

  listen(sock_server, 1024);

  while(1)
  {
    if((sock_client = accept(sock_server, NULL, NULL)) < 0)
    {
      perror("accept");
      return EXIT_FAILURE;
    }

    while(1)
    {
      if(recv(sock_client, &command, 4, MSG_WAITALL) <= 0) break;
      code = command >> 28;
      data = command & 0xfffffff;
      switch(code)
      {
        case 0:
          /* set trigger period */
          period = data;
          *(uint32_t *)(cfg + 16) = period - 1;
          *(uint32_t *)(cfg + 28) = period - 1;
          break;
        case 1:
          /* set trigger duration */
          *(uint32_t *)(cfg + 12) = data;
          break;
        case 2:
          /* set trigger polarity */
          if(data == 0) *rst &= ~4;
          else if(data == 1) *rst |= 4;
          break;
        case 3:
          /* set S&H delay */
          shdelay = data;
          *(uint32_t *)(cfg + 20) = shdelay;
          *(uint32_t *)(cfg + 24) = shdelay + shtime;
          break;
        case 4:
          /* set S&H duration */
          shtime = data;
          *(uint32_t *)(cfg + 24) = shdelay + shtime;
          break;
        case 5:
          /* set S&H polarity */
          if(data == 0) *rst &= ~8;
          else if(data == 1) *rst |= 8;
          break;
        case 6:
          /* set acquisition delay */
          *(uint32_t *)(cfg + 32) = data;
          *(uint32_t *)(cfg + 36) = data + 1;
          *(uint32_t *)(cfg + 40) = data + 1;
          break;
        case 7:
          /* set number of ADC samples per pulse */
          *(uint8_t *)(cfg + 4) = data - 1;
          break;
        case 8:
          /* set number of pulses per pixel */
          pulses = data;
          *(uint8_t *)(cfg + 5) = pulses - 1;
          break;
        case 9:
          /* clear coordinates */
          size = 0;
          break;
        case 10:
          /* add coordinates */
          if(size >= 1048576) continue;
          coordinates[size] = (data & 0xfffc000) << 2 | (data & 0x3fff);
          ++size;
          break;
        case 11:
          /* start pulse generators */
          *rst |= 1;
          break;
        case 12:
          /* start scanning */
          counter = 0;
          position = 0;

          /* stop pulse generators */
          *rst &= ~1;

          /* reset DAC and ADC FIFO */
{'fraction_non_alphanumeric': 0.15494816211121584, 'fraction_numerical': 0.05419415645617342, 'mean_word_length': 2.0720903300521134, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4918036', 'n_tokens_mistral': 4395, 'n_tokens_neox': 3808, 'n_words': 1053}