stdcomm.c
来自「基于4个mips核的noc设计」· C语言 代码 · 共 208 行
C
208 行
/**************************************************************** * TU Eindhoven, Eindhoven, The Netherlands, November 2003 * Author : Mathijs Visser * (Mathijs.Visser@student.tue.nl) * * >>>> See header file for more information. <<<< ****************************************************************/#include "stdcomm.h"#ifdef DEBUGMODE#include "mtools.h" /* Needed for: mprintf() */#endif#ifdef CC_I386#ifdef DEBUG2FILE#include <stdlib.h> /* for exit() */#include <stdio.h> /* for FILE */#endif#endifint sc_my_address;int try_count_sc_send = -1;int try_count_sc_receive = -1;int sc_send(const int address, const void *data, const int size_in_bytes){ /* It is not necessary for the address of data to be devideable by 4.*/ int i; /* Number of the current byte in data, the first byte has number 0.*/ int ctrlword; /* Contains address and status bits of the control word */ int dataword; /* Temporary for the data contained in one transmission.*/ for(i=0;i<size_in_bytes;i++) { if (i%4 == 0) dataword = 0; /* Clear the data.*/ ((char*)&dataword)[i%4] = ((char*)data)[i]; if (i%4 == 3 || i == size_in_bytes-1) { ctrlword = (i==size_in_bytes-1?address | SC_BIT_SEND_EOP: address); if (sc_send_word(&ctrlword, &dataword, try_count_sc_send)) return (i/4)*4; /* Return number of bytes sent successfully.*/ }; }; return size_in_bytes;}int sc_receive(void *data, const int size_in_bytes){ /* It is not necessary for the address of data to be devideable by 4.*/ int i; /* Number of the current byte in data, the first byte has number 0.*/ int ctrlword; /* Contains the address and status bits of the control word */ int dataword; /* Temporary for the data contained in one reception.*/ for(i=0;i<size_in_bytes;i++) { if (i%4 == 0) \ if (sc_receive_word(&ctrlword, &dataword, try_count_sc_receive)) \ return i; ((char*)data)[i] = ((char*)&dataword)[i%4]; }; return (ctrlword & SC_BIT_RCV_EOP? size_in_bytes: -size_in_bytes);}#ifndef CC_I386 /* LCC LCC LCC LCC LCC LCC *//* ------------------------------------------------------------------------------- * Implementation of sc_send_word and sc_receive_word depends on platform: * 1. LCC on a network of minimipses. * -------------------------------------------------------------------------------*/int sc_send_word(const int *ctrlword, const int *data, int try_count) { /* The address of data MUST BE devideable by 4!*/ #ifdef DEBUGMODE char dumpstr[20]; int try_count_max = try_count; dump4bytes(dumpstr, (void*)data); mprintf("Sending %s, ctrlword=0x%x...", dumpstr, *ctrlword); #endif while (!(SC_CONTROLWORD & SC_BIT_SEND_READY)) if (try_count == 1) { /* Tried sending try_count times.*/ #ifdef DEBUGMODE mprintf(" FAILURE (retried %d times).\n", try_count_max); #endif return SC_COULD_NOT_SEND; } else /* There are still attempts left.*/ if (try_count>1) try_count--; /* Transmitter ready, we can send.*/ SC_DATAWORD = *data; SC_CONTROLWORD = *ctrlword | SC_BIT_SEND; #ifdef DEBUGMODE mprintf(" ok.\n"); #endif return SC_COMMERR_SUCCESS;}int sc_receive_word(int *ctrlword, int *data, int try_count) { /* The address of data MUST BE devideable by 4!*/ #ifdef DEBUGMODE char dumpstr[20]; int try_count_max = try_count; mprintf("Listening..."); #endif while (!((*ctrlword = SC_CONTROLWORD) & SC_BIT_DATA_READY)) if (try_count == 1) {/* Tried receiving try_count times.*/ #ifdef DEBUGMODE mprintf(" FAILURE (retried %d times).\n", try_count_max); #endif return SC_NO_DATA_WAITING; } else /* There are still attempts left.*/ if (try_count>1) try_count--; /* Data waiting, read it.*/ *data = SC_DATAWORD; #ifdef DEBUGMODE dump4bytes(dumpstr, data); mprintf(" received %s.\n", dumpstr); #endif return SC_COMMERR_SUCCESS;}#else /* GCC GCC GCC GCC GCC GCC *//* ------------------------------------------------------------------------------- * Implementation of sc_send_word and sc_receive_word depends on platform: * 2. gcc on a i386 platform. * NOTE for GCC: * - All bits of the ctrlword, except the address bits, are ignored. * - The try_count is ignored since the transmission is always successful. * -------------------------------------------------------------------------------*/#ifdef DEBUG2FILEint sc_send_wordf(const int *ctrlword, const int *data) { FILE *fo; /* File that contains the input for the next processing node.*/ char file_name[50]; sprintf(file_name, "output_to_0x%04x.bin", *ctrlword & 0xFFFF); fo = fopen(file_name,"ab"); if (fo == NULL) { printf("ERROR: fopen() on \"%s\" failed!\n", file_name); exit(1); }; fseek(fo, 0, SEEK_END); fwrite(ctrlword, sizeof(int), 1, fo); fwrite(data, sizeof(int), 1, fo); fflush(fo); fclose(fo); return SC_COMMERR_SUCCESS;}long fi_pos = 0; /* Current position in fi (in bytes). */int sc_receive_wordf(int *ctrlword, int *data) { FILE *fi; /* File that contains the output of the previous processing node.*/ char *file_name; file_name = "input.bin"; fi = fopen(file_name,"rb"); if (fi == NULL) { printf("ERROR: fopen() on \"%s\" failed!\n", file_name); exit(1); }; fseek(fi, 0, SEEK_END); if (fi_pos >= ftell(fi)) { fclose(fi); return SC_NO_DATA_WAITING; } else { fseek(fi, fi_pos, SEEK_SET); fread(ctrlword, sizeof(int), 1, fi); fread(data, sizeof(int), 1, fi); fi_pos += 8; fflush(fi); fclose(fi); return SC_COMMERR_SUCCESS; };}#endifint sc_send_word(const int *ctrlword, const int *data, int try_count) { #ifdef DEBUGMODE char dumpstr[20]; dump4bytes(dumpstr, data); mprintf("Sending %s, ctrlword=0x%x...", dumpstr, *ctrlword); mprintf(" ok.\n"); #endif #ifdef DEBUG2FILE return sc_send_wordf(ctrlword, data); #else return SC_COMMERR_SUCCESS; #endif}int sc_receive_word(int *ctrlword, int *data, int try_count) { #ifdef DEBUG2FILE int ret_val = sc_receive_wordf(ctrlword, data); #ifdef DEBUGMODE char dumpstr[20]; if (ret_val == SC_COMMERR_SUCCESS) { dump4bytes(dumpstr, data); mprintf("Listening... received %s.\n", dumpstr); } else { mprintf("Listening... receive FAILED\n", dumpstr); }; #endif return ret_val; #else *ctrlword = 0; *data = 0; #ifdef DEBUGMODE mprintf("Listening... ok (faked).\n"); #endif return SC_COMMERR_SUCCESS; #endif}#endif
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?