From dbfe4d7f73efea77d8c026b17fca3de2df2b0b1b Mon Sep 17 00:00:00 2001
From: Yuya Hamamachi <yuya.hamamachi.sx@renesas.com>
Date: Mon, 22 May 2023 17:46:36 +0900
Subject: [PATCH 21/21] WIP: Add benchmark project

Signed-off-by: Yuya Hamamachi <yuya.hamamachi.sx@renesas.com>
---
 .../armv8/spider/benchmark/benchmark.c        | 130 ++++++++++++++++++
 .../armv8/spider/benchmark/benchmark.oil      |  65 +++++++++
 .../cortex-a/armv8/spider/benchmark/build.sh  |  15 ++
 3 files changed, 210 insertions(+)
 create mode 100644 examples/cortex-a/armv8/spider/benchmark/benchmark.c
 create mode 100644 examples/cortex-a/armv8/spider/benchmark/benchmark.oil
 create mode 100755 examples/cortex-a/armv8/spider/benchmark/build.sh

diff --git a/examples/cortex-a/armv8/spider/benchmark/benchmark.c b/examples/cortex-a/armv8/spider/benchmark/benchmark.c
new file mode 100644
index 00000000..15c3e2b2
--- /dev/null
+++ b/examples/cortex-a/armv8/spider/benchmark/benchmark.c
@@ -0,0 +1,130 @@
+#include "tpl_os.h"
+
+#define APP_Task_benchmark_START_SEC_CODE
+#include "tpl_memmap.h"
+
+uint8 shared_memory[16][16];
+uint8 result_array[4][17];
+
+uint32 g_counter = 0;
+
+extern volatile TickType tpl_time_counter; // 10msec/count 
+#define TICK_TO_MSEC(t)	(t*10)
+#define TICK_TO_SEC(t)	(t/100)
+void __nop(void) {}
+
+/**
+ * System Call function for newlib
+ */
+#include <stddef.h>
+#include <errno.h>
+#define HEAP_SIZE 4096
+static char _HEAP[HEAP_SIZE];
+static char* const _heap_region_start = &_HEAP[0];
+static char* const _heap_region_end = &_HEAP[HEAP_SIZE-1];
+void* _sbrk(int size) {
+    static char *curr_heap_end = _heap_region_start;
+    char        *prev_heap_end;
+
+    if( (curr_heap_end+size) > _heap_region_end ) {
+        return (char*)-1;
+    }
+    prev_heap_end  = curr_heap_end;
+    curr_heap_end  += size;
+
+    return (void *)prev_heap_end;
+}
+void _write(){}
+void _read(){}
+void _open(){}
+void _close(){}
+void _lseek(){}
+
+/**
+ * external functions
+ */
+extern void dhrystone_main(void);
+extern void coremark_main(void);
+
+void float_to_char(uint8 input_array[], float input) {
+  uint8 i, n;
+  uint8 ascii_offset = 0x20; // 0x30 = '0' / 0x20 = ' '
+  float tmp;
+
+  for (i=0; i<16; ++i){
+    tmp = input;
+    if (i < 8) {
+      for(n=i+1; n<8;++n)
+        tmp = tmp / 10;
+    }
+    else {
+      for(n=7; n<i; ++n)
+        tmp = tmp * 10;
+    }
+
+    if((int)tmp%10 != 0 || i == 7)
+      ascii_offset = 0x30; // changed at once
+
+    input_array[i+(i/8)] = (ascii_offset + (int)tmp%10); // if (i==8) i+1, else i
+  }
+  input_array[8] = '.';
+  input_array[16] = '\0';
+}
+void int_to_char(uint8 input_array[], int input) {
+  uint8 i, n;
+  uint8 ascii_offset = 0x20; // 0x30 = '0' / 0x20 = ' '
+  int tmp;
+
+  for (i=0; i<16; ++i){
+    tmp = input;
+      for(n=i+1; n<16;++n)
+        tmp = tmp / 10;
+    if((int)tmp%10 != 0)
+      ascii_offset = 0x30; // changed at once
+
+    input_array[i] = (ascii_offset + (int)tmp%10); // if (i==8) i+1, else i
+  }
+}
+
+// return msec
+uint32 ms_time() { return TICK_TO_MSEC(tpl_time_counter); }
+// return sec
+uint32 time() { return TICK_TO_SEC(tpl_time_counter); }
+
+void sec_sleep(int sec)
+{
+	uint32 end;
+	end = time() + sec;
+	while(end > time());
+}
+
+void ms_sleep(int ms)
+{
+	uint32 end;
+	end = ms_time() + ms;
+	while(end > ms_time());
+}
+
+FUNC(int, OS_APPL_CODE) main(void)
+{
+	StartOS(OSDEFAULTAPPMODE);
+	return 0;
+}
+
+TASK(benchmark)
+{
+	dhrystone_main();
+	ms_sleep(10000);
+
+	coremark_main();
+	sec_sleep(1);
+
+	while(1) {
+		g_counter = time();
+		sec_sleep(1);
+	}
+	TerminateTask();
+}
+
+#define APP_Task_benchmark_STOP_SEC_CODE
+#include "tpl_memmap.h"
diff --git a/examples/cortex-a/armv8/spider/benchmark/benchmark.oil b/examples/cortex-a/armv8/spider/benchmark/benchmark.oil
new file mode 100644
index 00000000..c0c04360
--- /dev/null
+++ b/examples/cortex-a/armv8/spider/benchmark/benchmark.oil
@@ -0,0 +1,65 @@
+OIL_VERSION = "4.2";
+
+IMPLEMENTATION trampoline {
+
+    /* This fix the default STACKSIZE of tasks */
+    TASK {
+        UINT32 STACKSIZE = 1000 ;
+    } ;
+
+    /* This fix the default STACKSIZE of ISRs */
+    ISR {
+        UINT32 STACKSIZE = 1000 ;
+    } ;
+};
+
+CPU benchmark {
+  OS config {
+    STATUS = EXTENDED;
+
+    BUILD = TRUE {
+      TRAMPOLINE_BASE_PATH = "../../../../..";
+      APP_SRC = "benchmark.c";
+      APP_SRC = "dhrystone/dhry_1.c";
+      APP_SRC = "dhrystone/dhry_2.c";
+      APP_SRC = "coremark/barebones/core_portme.c";
+      APP_SRC = "coremark/core_list_join.c";
+      APP_SRC = "coremark/core_main.c";
+      APP_SRC = "coremark/core_matrix.c";
+      APP_SRC = "coremark/core_state.c";
+      APP_SRC = "coremark/core_util.c";
+      APP_NAME = "benchmark_exe.elf";
+      // CFLAGS  = "-O0"; // for debug
+      CFLAGS  = "-O3";
+      LDFLAGS = "-Map=benchmark.map";
+      COMPILER = "arm-none-eabi-gcc";
+      CPPCOMPILER = "arm-none-eabi-g++";
+      ASSEMBLER = "arm-none-eabi-as";
+      LINKER = "arm-none-eabi-ld";
+      COPIER = "arm-none-eabi-objcopy";
+      SYSTEM = PYTHON;
+      CFLAGS="-Wno-stringop-truncation"; // Avoid strncpy warnings
+      CFLAGS="-Wno-return-type -Wno-implicit-int -Wno-implicit-function-declaration"; // Avoid dhrystone warnings
+      CFLAGS="-DTIME=TIME";
+      // CFLAGS="-DCOREMARK_DEBUG"; // for Debug
+      CFLAGS="-I./coremark -I./coremark/barebones -DITERATIONS=30000";
+    };
+    SYSTEM_CALL = TRUE;
+    MEMMAP = TRUE {
+      COMPILER = gcc;
+      LINKER = gnu_ld { SCRIPT = "script.ld"; };
+      ASSEMBLER = gnu_as;
+      MEMORY_PROTECTION = FALSE;
+    };
+  };
+
+  APPMODE std {};
+
+  TASK benchmark { 
+    PRIORITY = 1;
+    AUTOSTART = TRUE { APPMODE = std; };
+    ACTIVATION = 1;
+    SCHEDULE = FULL;
+  };
+};
+
diff --git a/examples/cortex-a/armv8/spider/benchmark/build.sh b/examples/cortex-a/armv8/spider/benchmark/build.sh
new file mode 100755
index 00000000..952134ca
--- /dev/null
+++ b/examples/cortex-a/armv8/spider/benchmark/build.sh
@@ -0,0 +1,15 @@
+#!/usr/bin/env bash
+
+#stop on errors
+set -e
+
+if [[ ! -d "_build" ]]
+then
+    mkdir _build
+fi
+
+echo "*** Run Goil ***"
+goil --target=cortex-a/armv8/spider --templates=../../../../../goil/templates/ benchmark.oil
+
+echo "*** Run Make ***"
+./make.py
-- 
2.34.1

