Skip to main content
  • Home
  • Development
  • Documentation
  • Donate
  • Operational login
  • Browse the archive

swh logo
SoftwareHeritage
Software
Heritage
Archive
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

swh:1:snp:608d2763677e9f99035674512504b69493191ab0
  • Code
  • Branches (22)
  • Releases (0)
    • Branches
    • Releases
    • HEAD
    • refs/heads/aimer
    • refs/heads/benchmarkupdate
    • refs/heads/biscuit
    • refs/heads/dependabot/submodules/libopencm3-201f5bc
    • refs/heads/efm32gg11b
    • refs/heads/faster-ml-dsa
    • refs/heads/gcc14.2
    • refs/heads/github-actions-test
    • refs/heads/haetae-fix
    • refs/heads/master
    • refs/heads/meds
    • refs/heads/mirith
    • refs/heads/mkannwischer-patch-1
    • refs/heads/openocdtcp
    • refs/heads/outoftree
    • refs/heads/remove-broken-symlinks
    • refs/heads/tuov
    • refs/remotes/amin/kyberintt
    • refs/tags/Round1
    • refs/tags/Round2
    • refs/tags/Round3
    • refs/tags/SignatureRound1
    No releases to show
  • acb37f7
  • /
  • common
  • /
  • test.c
Raw File Download
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
  • revision
  • snapshot
content badge Iframe embedding
swh:1:cnt:1a24c275f002b0964f126b4a61f63238365aa70a
directory badge Iframe embedding
swh:1:dir:d376b3f4e65914f88ce37b5814b27eec41c0664d
revision badge
swh:1:rev:d702a740bdf97208a6853fbe44a5b5890481006c
snapshot badge
swh:1:snp:608d2763677e9f99035674512504b69493191ab0
Citations

This interface enables to generate software citations, provided that the root directory of browsed objects contains a citation.cff or codemeta.json file.
Select below a type of object currently browsed in order to generate citations for them.

  • content
  • directory
  • revision
  • snapshot
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Generate software citation in BibTex format (requires biblatex-software package)
Generating citation ...
Tip revision: d702a740bdf97208a6853fbe44a5b5890481006c authored by Matthias J. Kannwischer on 28 November 2024, 05:21:42 UTC
remove broken symlinks
Tip revision: d702a74
test.c
// SPDX-License-Identifier: Apache-2.0 or CC0-1.0
#include <hal.h>
#include <randombytes.h>
#include <sendfn.h>
#include <stdint.h>

#if defined(SRAM_TIMING_TEST)
#define TEST_BLOCK_SIZE 4096

/* Don't use opencm3 here, since not all platforms might use opencm3, but all
   have these DWT registers */
#define DWT_CTRL           (*(volatile uint32_t*)(0xE0001000u + 0x00))
#define DWT_CYCCNT         (*(volatile uint32_t*)(0xE0001000u + 0x04))
#define DWT_CTRL_CYCCNTENA (1 << 0)
#define SCS_DEMCR          (*(volatile uint32_t*)(0xE000E000u + 0xDFC))
#define SCS_DEMCR_TRCENA   (1 << 24)
/* Need a really precise cycle counter. */
static void cyccnt_enable()
{
	SCS_DEMCR |= SCS_DEMCR_TRCENA;
	DWT_CYCCNT = 0;
	DWT_CTRL |= DWT_CTRL_CYCCNTENA;
}
static inline void cyccnt_start()
{
	DWT_CYCCNT = 0;
}
static inline uint32_t cyccnt_get()
{
  return DWT_CYCCNT;
}

__attribute__((noinline))
static uint32_t test_load(volatile unsigned* ram_block)
{
  asm volatile("cpsid if");
  cyccnt_start();
#define NL "\n\t"
  asm volatile("_MEMLOOP%=:" NL
               "ldr r12, [%0], #4" NL
               "cmp %0, %1" NL
               "bne _MEMLOOP%=" NL
               :"+r" (ram_block): "r" (ram_block + (TEST_BLOCK_SIZE / sizeof(unsigned))): "r12", "cc");
  uint32_t result = cyccnt_get();
  asm volatile("cpsie if");
  return result;
}

__attribute__((noinline))
static uint32_t test_unalignedload(volatile void* ram_block)
{
  volatile unsigned char* ram_block8 = ram_block;
  ram_block8 += 2;
  asm volatile("cpsid if");
  cyccnt_start();
#define NL "\n\t"
  asm volatile("_MEMLOOP%=:" NL
               "ldr r12, [%0], #4" NL
               "cmp %0, %1" NL
               "blt _MEMLOOP%=" NL
               :"+r" (ram_block8): "r" (ram_block8 + TEST_BLOCK_SIZE): "r12", "cc");
  uint32_t result = cyccnt_get();
  asm volatile("cpsie if");
  return result;
}

__attribute__((noinline))
static uint32_t test_store(volatile unsigned* ram_block)
{
  cyccnt_start();
#define NL "\n\t"
  asm volatile("_MEMLOOP%=:" NL
               "str r12, [%0], #4" NL
               "cmp %0, %1" NL
               "bne _MEMLOOP%=" NL
               :"+r" (ram_block): "r" (ram_block + (TEST_BLOCK_SIZE / sizeof(unsigned))): "r12", "cc");
  return cyccnt_get();
}

__attribute__((noinline))
static uint32_t test_loadstore(volatile unsigned* ram_block)
{
  cyccnt_start();
#define NL "\n\t"
  asm volatile("_MEMLOOP%=:" NL
               "str r12, [%0]" NL
               "add r12, r12, #1" NL
               "ldr r12, [%0], #4" NL
               "cmp %0, %1" NL
               "bne _MEMLOOP%=" NL
               :"+r" (ram_block): "r" (ram_block + (TEST_BLOCK_SIZE / sizeof(unsigned))): "r12", "cc");
  return cyccnt_get();
}

static void memory_timing_test(void)
{
  cyccnt_enable();
#define RAMBLK(BLK)                                                       \
  static volatile unsigned ram ## BLK ## _block[TEST_BLOCK_SIZE / sizeof(unsigned) + 1] __attribute__((section(".ram" #BLK)))

#define TEST(BLK) \
  test_load(ram ## BLK ## _block); \
  test_unalignedload(ram ## BLK ## _block);  \
  test_store(ram ## BLK ## _block); \
  test_loadstore(ram ## BLK ## _block); \
  send_unsigned("ram" #BLK " load", test_load(ram ## BLK ## _block)); \
  send_unsigned("ram" #BLK " unalignedload", test_unalignedload(ram ## BLK ## _block));   \
  send_unsigned("ram" #BLK " store", test_store(ram ## BLK ## _block)); \
  send_unsigned("ram" #BLK " loadstore", test_loadstore(ram ## BLK ## _block));

  static volatile unsigned ram1_block[TEST_BLOCK_SIZE / sizeof(unsigned) + 1];
  TEST(1);
#if defined(HAS_SRAM2)
  RAMBLK(2);
  TEST(2);
#endif
#if defined(HAS_SRAM3)
  RAMBLK(3);
  TEST(3);
#endif
#if defined(HAS_CCM)
  static volatile unsigned ramccm_block[TEST_BLOCK_SIZE / sizeof(unsigned) + 1] __attribute__((section(".ccmram")));
  TEST(ccm);
#endif
}
#endif

#ifndef CLOCK_TEST
#define CLOCK_TEST CLOCK_BENCHMARK
#endif

void stacktest(size_t size)
{
  volatile uint32_t mem[size] __attribute__((unused));
  for (unsigned i = 0; i < size; ++i) {
    mem[i] = 0;
  }
}

int main(void)
{
  hal_setup(CLOCK_TEST);
  hal_send_str("Hello world");
  send_unsigned("Stack Size", hal_get_stack_size());
  unsigned rnd;
  randombytes((unsigned char*) &rnd, sizeof(unsigned));
  send_unsigned("Random number", rnd);
  size_t stack;
  hal_spraystack();
  stacktest(100);
  stack = hal_checkstack();
  send_unsigned("stackusage1", stack);
  hal_spraystack();
  stacktest(200);
  stack = hal_checkstack();
  send_unsigned("stackusage2", stack);
#if defined(SRAM_TIMING_TEST)
  memory_timing_test();
#endif
  return 0;
}

back to top

Software Heritage — Copyright (C) 2015–2025, The Software Heritage developers. License: GNU AGPLv3+.
The source code of Software Heritage itself is available on our development forge.
The source code files archived by Software Heritage are available under their own copyright and licenses.
Terms of use: Archive access, API— Contact— JavaScript license information— Web API