Forum Discussion
Altera_Forum
Honored Contributor
11 years agoWhen you enable the caches, you also have to initialize the MMU.
# include <stdio.h> # include <stdlib.h> # include <assert.h> # include "alt_cache.h" # include "alt_mmu.h" int __auto_semihosting; # define N 256 # define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) void mul(const double *in_a, const double *in_b, unsigned n, double *out); /* MMU Page table - 16KB aligned at 16KB boundary */ static uint32_t __attribute__ ((aligned (0x4000))) alt_pt_storage[4096]; static void *alt_pt_alloc(const size_t size, void *context) { return context; } static void mmu_init(void) { uint32_t *ttb1 = NULL; /* Populate the page table with sections (1 MiB regions). */ ALT_MMU_MEM_REGION_t regions[] = { /* Memory area: 1 GiB */ { .va = (void *)0x00000000, .pa = (void *)0x00000000, .size = 0x40000000, .access = ALT_MMU_AP_FULL_ACCESS, .attributes = ALT_MMU_ATTR_WBA, .shareable = ALT_MMU_TTB_S_NON_SHAREABLE, .execute = ALT_MMU_TTB_XN_DISABLE, .security = ALT_MMU_TTB_NS_SECURE }, /* Device area: Everything else */ { .va = (void *)0x40000000, .pa = (void *)0x40000000, .size = 0xc0000000, .access = ALT_MMU_AP_FULL_ACCESS, .attributes = ALT_MMU_ATTR_DEVICE_NS, .shareable = ALT_MMU_TTB_S_NON_SHAREABLE, .execute = ALT_MMU_TTB_XN_ENABLE, .security = ALT_MMU_TTB_NS_SECURE } }; assert(ALT_E_SUCCESS == alt_mmu_init()); assert(alt_mmu_va_space_storage_required(regions, ARRAY_SIZE(regions)) <= sizeof(alt_pt_storage)); assert(ALT_E_SUCCESS == alt_mmu_va_space_create(&ttb1, regions, ARRAY_SIZE(regions), alt_pt_alloc, alt_pt_storage)); assert(ALT_E_SUCCESS == alt_mmu_va_space_enable(ttb1)); } int main(int argc, char** argv) { static double a[N], b[N], c[N]; unsigned i, t; mmu_init(); alt_cache_system_enable(); for (i = 0; i < N; i++) { a = (double)rand();b = (double)rand(); } *(unsigned volatile *)0xFFFEC600 = 0xFFFFFFFF; /* timer reload value */ *(unsigned volatile *)0xFFFEC604 = 0xFFFFFFFF; /* current timer value */ *(unsigned volatile *)0xFFFEC608 = 0x003; /* start timer at 200MHz and automatically reload */ mul(a, b, N, c); t = *(unsigned volatile *)0xFFFEC604; printf("used time for %u multiplications = %u ns\n", N, 5 * (0xFFFFFFFF - t)); return 0; }