Advertisement
Guest User

Untitled

a guest
May 18th, 2021
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.30 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <math.h>
  5. #include <assert.h>
  6. #include "fpga_pci.h"
  7.  
  8. void fail_on(int got, int exp, char *fn)
  9. {
  10.     printf("%s: expected 0x%x, got %d\n", fn, exp, got);
  11. }
  12.  
  13. #define BRAM_0_ADDRESS 0x0000000
  14. #define BRAM_1_ADDRESS 0x0010000
  15. #define MEM_DDRC       0x80000000
  16. #define TOTAL_MEMORY 3
  17. const unsigned mem_addr[TOTAL_MEMORY] = {BRAM_0_ADDRESS, BRAM_1_ADDRESS, MEM_DDRC};
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21.     printf("To run this test: ./app_name <n_transfers>\n");
  22.     uint32_t n_transfers = 10;
  23.     uint32_t w_val = 0;
  24.     uint32_t r_val = 0;
  25.  
  26.     srand(time(NULL));
  27.  
  28.     if (argc == 2)
  29.     {
  30.         n_transfers = atoi(argv[1]);
  31.     }
  32.  
  33.     // Initialize the pci library.
  34.     int res = fpga_pci_init();
  35.     fail_on(res, 0, "fpga_pci_init");
  36.  
  37.     pci_bar_handle_t pcis_handle;
  38.     int pcis_slot_id = 0;
  39.     int pcis_pf_id = FPGA_APP_PF;
  40.     int pcis_bar_id = APP_PF_BAR4;
  41.     uint32_t pcis_flags = 0;
  42.  
  43.     // Produces same output
  44.     // pci_bar_handle_t ocl_handle;
  45.     // int ocl_slot_id = 0;
  46.     // int ocl_pf_id = FPGA_APP_PF;
  47.     // int ocl_bar_id = APP_PF_BAR0;
  48.     // uint32_t ocl_flags = 0;
  49.  
  50.  
  51.     // Attach to an FPGA memory space.
  52.     res = fpga_pci_attach(pcis_slot_id, pcis_pf_id, pcis_bar_id, pcis_flags, &pcis_handle);
  53.     // res = fpga_pci_attach(ocl_slot_id, ocl_pf_id, ocl_bar_id, ocl_flags, &ocl_handle);
  54.     fail_on(res, 0, "fpga_pci_attach");
  55.  
  56.     for (int i = 0; i < n_transfers; i++)
  57.     {
  58.         w_val = rand() % (int)pow(2, 8);
  59.         int addr_offset = mem_addr[i % TOTAL_MEMORY];
  60.  
  61.         res = fpga_pci_poke(pcis_handle, addr_offset, w_val);
  62.         fail_on(res, 0, "fpga_pci_poke");
  63.  
  64.         res = fpga_pci_peek(pcis_handle, addr_offset, &r_val);
  65.         fail_on(res, 0, "fpga_pci_peek");
  66.  
  67.         assert(w_val == r_val);
  68.         printf("Value %d was w/r to address 0x%x\n", w_val, addr_offset);
  69.     }
  70.  
  71.     // Detach from an FPGA memory space.
  72.     res = fpga_pci_detach(pcis_handle);
  73.     fail_on(res, 0, "fpga_pci_detach");
  74.  
  75.     return res;
  76. }
  77.  
  78. /*
  79. fpga_pci_init: expected 0x0, got 0
  80. fpga_pci_attach: expected 0x0, got 25
  81. fpga_pci_poke: expected 0x0, got -22
  82. fpga_pci_peek: expected 0x0, got -22
  83. c_pcie_lib_test: c_pcie_lib_test.c:65: main: Assertion `w_val == r_val' failed.
  84. Aborted
  85. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement