How to use scratchpad memory

Hi,

I want to know whether the following SRAM is the scratchpad memory:

If it is, how to use it by programming?

I noticed the reply,

but I am confused, does it mean any vector load instruction would access data via scratchpad memory naturally? If so, it sounds not like scratchpad memory, because it should be controlled by programmer, right?

Thank you in advance.

Best,

Related post: 如何利用K1开发板的TCM缓存 - K1 - 进迭RISC-V论坛

我也有这个问题,根据我查到的资料:


TCM在基地址0xd8000000,然后簇0的4个core各自有各自的一段作为scratchpad
tcm@0xd8000000
├── core0_tcm@0
├── core1_tcm@20000
├── core2_tcm@40000
├── core3_tcm@60000
但是我找不到CPUx与corex是怎么对应的,我先尝试mmap把这段物理地址手动映射到虚拟地址,然后在程序里往这个地址上写,但是报错,这是我的代码:

#include <stdio.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#define TCM_PHYS 0xd8000000UL
#define TCM_SIZE 0x20000

int main() {
    printf("Opening /dev/mem...\n");

    int fd = open("/dev/mem", O_RDWR | O_SYNC);
    if (fd < 0) {
        perror("open /dev/mem");
        return 1;
    }

    printf("Mapping TCM phys 0x%lx...\n", TCM_PHYS);

    void *va = mmap(NULL, TCM_SIZE, PROT_READ | PROT_WRITE,
                    MAP_SHARED, fd, TCM_PHYS);

    if (va == MAP_FAILED) {
        perror("mmap TCM");
        close(fd);
        return 1;
    }

    printf("TCM VA = %p\n", va);

    volatile uint32_t *p = (volatile uint32_t *)va;

    printf("Writing 0x12345678 to TCM...\n");
    p[0] = 0x12345678;   // <-- 关键测试点

    printf("Reading back...\n");
    uint32_t v = p[0];

    printf("TCM[0] = 0x%08x\n", v);

    munmap(va, TCM_SIZE);
    close(fd);

    return 0;
}

然后这是运行结果:


我上面的理解对吗?
我想要通过手写汇编手动的控制数据的读写,把scratchpad作为一个手动控制的快于cache的空间,来提升load/store的速度,我该怎么做