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

powered by: WebSVN 2.1.0

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