summaryrefslogtreecommitdiff
path: root/trunk/users/sai/newcomms/procsim.c
blob: 8c7b4cfbb7fb69e807f7ca0260f81a33ca9994b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include "shared.h"

int exitAfter = -1;
int receiveCount = 0;

byte address;
int rfd, wfd;

byte RCREG;

byte action = ACT_NONE;
byte action_value = 0;
int act_after = 0;

void *timer(void *arg)
{
  for(;;) {
    usleep(20000);
    timer_ping();
  }
  return NULL;
}

void *comms(void *arg)
{  
  ssize_t len;

  for(;;) {
    len = read(rfd, &RCREG, 1);
    receiveCount++;
    if (exitAfter != -1 && receiveCount >= exitAfter)
	break;
    if (receiveCount == act_after && action == ACT_FLIP)
      RCREG ^= action_value;
    if (len == 0) break;
    if (receiveCount != act_after || action != ACT_DROP)
      uartNotifyReceive();
    if (receiveCount == act_after && action == ACT_INSERT) {
      RCREG = action_value;
      uartNotifyReceive();
    }
  }
  exit(0);
  return NULL;
}

void uartTransmit(byte c)
{
  //if (address) printf("                  ");
  //printf("--> %d tx %02x\n", address, c);
  //fflush(stdout);
  usleep(10000);

  if (write(wfd, &c, 1) == -1) {
    printf("WRITE FAILURE!!\n");
    fflush(stdout);
  }
}