Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <unistd.h>
- #include <assert.h>
- #include <string.h>
- #define BRAM_0_ADDRESS 0x0000000
- #define BRAM_1_ADDRESS 0x0010000
- #define MEM_DDRC 0x80000000
- #define TOTAL_MEMORY 3
- const unsigned mem_addr[TOTAL_MEMORY] = {BRAM_0_ADDRESS, BRAM_1_ADDRESS, MEM_DDRC};
- // randomize a string of size <size>
- static char *rand_str(char *str, size_t size)
- {
- const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRTSUVWXYZ1234567890";
- int i;
- for (i = 0; i < size; i++)
- {
- int key = rand() % (int)(sizeof charset - 1);
- str[i] = charset[key];
- }
- str[size - 1] = '\0';
- return str;
- }
- int main(int argc, char *argv[])
- {
- char *srcBuf = NULL;
- char *dstBuf = NULL;
- int read_fd;
- int write_fd;
- int ret;
- int buf_size = 20;
- int n_transfers = 10;
- if (argc >= 2)
- {
- n_transfers = atoi(argv[1]);
- }
- if (argc == 3)
- {
- buf_size = atoi(argv[2]);
- }
- printf("n_transfers = %d, buf_size = %d\n", n_transfers, buf_size);
- srcBuf = (char *)malloc(buf_size * sizeof(char));
- dstBuf = (char *)malloc(buf_size * sizeof(char));
- /* Open a XDMA write channel (Host to Core) */
- if ((write_fd = open("/dev/xdma0_h2c_0", O_WRONLY)) == -1)
- {
- perror("open failed with errno");
- }
- /* Open a XDMA read channel (Core to Host) */
- if ((read_fd = open("/dev/xdma0_c2h_0", O_RDONLY)) == -1)
- {
- perror("open failed with errno");
- }
- for (int i = 0; i < n_transfers; i++)
- {
- /* Initialize srcBuf */
- rand_str(srcBuf, buf_size);
- u_int32_t address = mem_addr[i % TOTAL_MEMORY];
- /* Write the entire source buffer to offset OFFSET_IN_FPGA_DRAM */
- ret = pwrite(write_fd, srcBuf, buf_size, address);
- if (ret < 0)
- {
- printf("PWrite failed to address 0x%x with code %d\n", address, errno);
- printf("Error: %s\n", strerror(errno));
- }
- printf("Tried to write %u bytes to address 0x%x, succeeded in writing %u bytes\n", buf_size, address, ret);
- ret = pread(read_fd, dstBuf, buf_size, address);
- if (ret < 0)
- {
- printf("PRead failed to address 0x%x with code %d\n", address, errno);
- printf("Error: %s\n", strerror(errno));
- }
- // printf("Tried to read %u bytes to address 0x%x, succeeded in reading %u bytes\n", buf_size, address, ret);
- if (strcmp(srcBuf, dstBuf) != 0)
- {
- printf("Strings don't match, address 0x%x\n", address);
- }
- }
- if (close(write_fd) < 0)
- {
- perror("write_fd close failed with errno");
- }
- if (close(read_fd) < 0)
- {
- perror("read_fd close failed with errno");
- }
- return 0;
- }
- /*
- n_transfers = 10, buf_size = 20
- Tried to write 20 bytes to address 0x0, succeeded in writing 20 bytes
- Strings don't match, address 0x0
- Tried to write 20 bytes to address 0x10000, succeeded in writing 20 bytes
- Strings don't match, address 0x10000
- Tried to write 20 bytes to address 0x80000000, succeeded in writing 20 bytes
- Tried to write 20 bytes to address 0x0, succeeded in writing 20 bytes
- Strings don't match, address 0x0
- Tried to write 20 bytes to address 0x10000, succeeded in writing 20 bytes
- Strings don't match, address 0x10000
- Tried to write 20 bytes to address 0x80000000, succeeded in writing 20 bytes
- Tried to write 20 bytes to address 0x0, succeeded in writing 20 bytes
- Strings don't match, address 0x0
- Tried to write 20 bytes to address 0x10000, succeeded in writing 20 bytes
- Strings don't match, address 0x10000
- Tried to write 20 bytes to address 0x80000000, succeeded in writing 20 bytes
- Tried to write 20 bytes to address 0x0, succeeded in writing 20 bytes
- Strings don't match, address 0x0
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement