OpenCores
URL https://opencores.org/ocsvn/funbase_ip_library/funbase_ip_library/trunk

Subversion Repositories funbase_ip_library

[/] [funbase_ip_library/] [trunk/] [TUT/] [ip.hwp.communication/] [n2h2/] [1.0/] [tb/] [system/] [src_cpu2/] [main.c] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 145 lanttu
/*
2
 * main.c
3
 *
4
 *  Created on: 22.2.2011
5
 *      Author: lehton87
6
 */
7
 
8
#include <stdio.h>
9
#include <stdlib.h>
10
#include <string.h>
11
#include <io.h>
12
#include <unistd.h>
13
 
14
#include "system.h"
15
#include "N2H_registers_and_macros.h"
16
#include "tut_n2h_regs.h"
17
 
18
#include "n2h_isr_fifo.h"
19
 
20
#include "support.h"
21
 
22
 
23
int main()
24
{
25
  int n_received = 0;
26
  int rx_data[20];
27
  int rx_amount = 8;
28
  int tx_amount = 8;
29
  int send      = 0;
30
 
31
  int channels[8] = {0, 0, 0, 0, 0, 0, 0, 0};
32
 
33
  N2H_isr_fifo* n2h_isr_fifo = n2h_isr_fifo_create();
34
 
35
  // Init N2H interrupt
36
  n2h_isr_init(n2h_isr_fifo);
37
 
38
  printf("CPU2: starts\n");
39
 
40
  while(1)
41
    {
42
      if(n2h_isr_fifo_size(n2h_isr_fifo))
43
        {
44
          N2H_isr_info* info = n2h_isr_fifo_pop(n2h_isr_fifo);
45
 
46
          switch(info->isr_type)
47
            {
48
            case RX_READY:
49
 
50
              printf("CPU2: received %ith packet to channel %i\n",
51
                     ++n_received, info->rx_channel);
52
 
53
              if(channels[info->rx_channel] == 2)
54
                {
55
                  channels[info->rx_channel] = 0;
56
                  break;
57
                }
58
 
59
              memcpy((int*)rx_data, (int*)(N2H_REGISTERS_RX_BUFFER_START +
60
                                           info->rx_channel * rx_amount *
61
                                           sizeof(int)),
62
                     rx_amount*sizeof(int));
63
 
64
              printf("CPU2: read data from channel %i : %X %X %X %X %X "
65
                     "%X %X %X\n",
66
                     info->rx_channel, rx_data[0], rx_data[1], rx_data[2],
67
                     rx_data[3], rx_data[4], rx_data[5], rx_data[6],
68
                     rx_data[7]);
69
 
70
              channels[info->rx_channel] = 0;
71
 
72
              // Init rx chan half the time
73
              int src = (rand() % 2 == 0) ? 0x00 : 0x10;
74
 
75
              if(rand() % 2 == 0)
76
                {
77
                  int i = 0;
78
 
79
                  for(i = 0; i < 5; ++i)
80
                    {
81
                      if(channels[i] == 0)
82
                        {
83
                          channels[i] = 1;
84
                          n2h_init_rx(i, 8*i*sizeof(int), 8, 0x400 + i + src);
85
                          break;
86
                        }
87
                    }
88
                }
89
 
90
              send++;
91
 
92
              int fre = 0;
93
              int x = 0;
94
              for(; x < 8; ++x) {if(channels[x]== 0) fre++;}
95
              printf("CPU2: %i to send, %i in FIFO, %i channels free\n",
96
                     send, n2h_isr_fifo_size(n2h_isr_fifo), fre);
97
 
98
              break;
99
 
100
            case RX_UNKNOWN:
101
              {
102
                int cha = 0;
103
 
104
                for(cha = 0; cha < 8; ++cha)
105
                  {
106
                    if(channels[cha] == 0)
107
                      {
108
                        channels[cha] = (info->dst_address == 0x5FF) ? 2 : 1;
109
 
110
                        printf("CPU2: received data to unconfigured "
111
                               "address 0x%X, assigning to channel %i\n",
112
                       info->dst_address, cha);
113
 
114
                        if(!(0x400 <= info->dst_address &&
115
                             info->dst_address <= 0x5FF))
116
                          {
117
                            printf("CPU2: %i is invalid address, FAILURE\n",
118
                                   info->dst_address);
119
                          }
120
 
121
                        // Initialize some channel to receive
122
                        n2h_init_rx(cha, cha * rx_amount*sizeof(int), rx_amount,
123
                                    info->dst_address);
124
 
125
                        break;
126
                      }
127
                  }
128
                int fre = 0;
129
                int x = 0;
130
                for(; x < 8; ++x) {if(channels[x]== 0) fre++;}
131
                printf("CPU2: %i to send, %i in FIFO, %i channels free\n",
132
                       send, n2h_isr_fifo_size(n2h_isr_fifo), fre);
133
 
134
              }
135
              break;
136
 
137
              case TX_IGNORED:
138
              {
139
                printf("CPU2: A transfer was ignored because it overlapped"
140
                       " previous one\n");
141
 
142
                int fre = 0;
143
                int x = 0;
144
                for(; x < 8; ++x) {if(channels[x]== 0) fre++;}
145
                printf("CPU2: %i to send, %i in FIFO, %i channels free\n",
146
                       send, n2h_isr_fifo_size(n2h_isr_fifo), fre);
147
              }
148
              break;
149
 
150
            }
151
 
152
          // Free memory
153
          free(info); info = NULL;
154
        }
155
      else if(send > 0 && (((IORD(N2H2_CHAN_BASE, 4) >> 16) & 0x1) == 1))
156
        {
157
          // Sent packet to a random target's random address
158
          int target_addr;
159
          int tx_data[8];
160
          int tx_slot = (rand() % 8);
161
          int cha;
162
 
163
          if(rand() % 2 == 0)
164
            {
165
              target_addr = 0x020 + tx_slot;
166
            }
167
          else
168
            {
169
              target_addr = 0x220 + tx_slot;
170
            }
171
 
172
          for(cha = 0; cha < 8; ++cha)
173
            tx_data[cha] = 0x82000000 | ((cha+1) << 16) | target_addr;
174
 
175
          memcpy((int*)(N2H_REGISTERS_TX_BUFFER_START +
176
                        tx_slot*tx_amount*sizeof(int)),
177
                 (int*)tx_data, tx_amount*sizeof(int));
178
 
179
          printf("CPU2: sending packet to 0x%X\n", target_addr);
180
 
181
          n2h_send(N2H_REGISTERS_TX_BUFFER_START +
182
                   tx_slot*tx_amount*sizeof(int),
183
                   tx_amount, target_addr);
184
 
185
          send--;
186
 
187
          // Overlapping with previous one
188
          if((rand() % 100) < 50)
189
            {
190
              n2h_send(N2H_REGISTERS_TX_BUFFER_START +
191
                       tx_slot*tx_amount*sizeof(int),
192
                       tx_amount, 0x1FF);
193
              printf("CPU2: sending hazard packet\n");
194
            }
195
          int fre = 0;
196
          int x = 0;
197
          for(; x < 8; ++x) {if(channels[x]== 0) fre++;}
198
          printf("CPU2: %i to send, %i in FIFO, %i channels free\n",
199
                 send, n2h_isr_fifo_size(n2h_isr_fifo), fre);
200
        }
201
    }
202
 
203
  printf("CPU2: retires!\n");
204
  while(1); return 0;
205
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.