From 2317121ee8107bf88887ade7f34162c7d50d10c5 Mon Sep 17 00:00:00 2001
From: Sivert Sliper <sivert.sliper@arm.com>
Date: Fri, 6 Mar 2020 11:59:21 +0000
Subject: [PATCH] support, iclib and aes now builds. Not tested.

---
 CMakeLists.txt                  |  2 +-
 aes/CMakeLists.txt              | 18 +++++-----
 aes/TI_aes_128_encr_only.c      |  6 +---
 aes/generate-reference-output.c |  1 -
 aes/lipsum.h                    |  1 +
 aes/main.c                      | 28 +++++++++------
 cmake/common.cmake              | 63 +++++++++++++++------------------
 cmake/tail.cmake                | 41 ++++++++++-----------
 iclib/CMakeLists.txt            |  4 +--
 iclib/cm0_ic.c                  |  3 +-
 iclib/cm0_ic.h                  |  5 +++
 iclib/config.h                  |  2 +-
 iclib/memory_management.h       |  2 --
 support/CMakeLists.txt          |  6 ++--
 support/cm0-FS.ld               |  9 ++---
 support/cm0-support.c           |  5 +--
 support/cm0-support.h           |  2 +-
 support/cm0-vectors.c           |  1 +
 18 files changed, 101 insertions(+), 98 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 434d18b..f15a650 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,7 +23,7 @@ include_directories(cmsis)
 
 add_subdirectory(support)
 add_subdirectory(iclib)
-#add_subdirectory(aes)
+add_subdirectory(aes)
 #add_subdirectory(matmul)
 #add_subdirectory(matmul-tiled)
 #add_subdirectory(crc)
diff --git a/aes/CMakeLists.txt b/aes/CMakeLists.txt
index 78c5c37..13e7384 100644
--- a/aes/CMakeLists.txt
+++ b/aes/CMakeLists.txt
@@ -1,15 +1,15 @@
 cmake_minimum_required(VERSION 3.0)
 
 FOREACH(METHOD "MS" "AS" "QR")
-    set(TESTNAME "aes-${METHOD}")
-    add_executable(
-        ${TESTNAME}
-        main.c
-        TI_aes_128_encr_only.c
-        TI_aes_128_encr_only.h
-        lipsum.h
-    )
-    include(${CMAKE_CURRENT_LIST_DIR}/../cmake/tail.cmake)
+  set(TESTNAME "aes-${METHOD}-${TARGET_ARCH}")
+  add_executable(
+      ${TESTNAME}
+      main.c
+      TI_aes_128_encr_only.c
+      TI_aes_128_encr_only.h
+      lipsum.h
+  )
+  include(${CMAKE_CURRENT_LIST_DIR}/../cmake/tail.cmake)
 ENDFOREACH()
 
 
diff --git a/aes/TI_aes_128_encr_only.c b/aes/TI_aes_128_encr_only.c
index 453ee3f..2b9a3fb 100644
--- a/aes/TI_aes_128_encr_only.c
+++ b/aes/TI_aes_128_encr_only.c
@@ -120,7 +120,7 @@ void aes_encrypt(unsigned char *state, unsigned char *key)
 	buf3 = state[buf4+3]^buf2;     buf3=galois_mul2(buf3); state[buf4+3] = state[buf4+3] ^ buf3 ^ buf1;
 	}
     }
-	  
+
     //key schedule
     // compute the 16 next round key bytes
     key[0] = sbox[key[13]]^key[0]^rcon;
@@ -139,7 +139,3 @@ void aes_encrypt(unsigned char *state, unsigned char *key)
     state[i]=state[i] ^ key[i];
   }
 }
-
-
-
-
diff --git a/aes/generate-reference-output.c b/aes/generate-reference-output.c
index 924af5c..bb19fd1 100644
--- a/aes/generate-reference-output.c
+++ b/aes/generate-reference-output.c
@@ -2,7 +2,6 @@
 
 #include <stdint.h>
 #include <stdio.h>
-
 #include "TI_aes_128_encr_only.h"
 
 #define MMDATA  // Empty macro
diff --git a/aes/lipsum.h b/aes/lipsum.h
index 3e0c825..232ccfe 100644
--- a/aes/lipsum.h
+++ b/aes/lipsum.h
@@ -1,4 +1,5 @@
 #include <stdint.h>
+#include "iclib/cm0_ic.h"
 
 uint8_t input[2048] MMDATA =
     "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo "
diff --git a/aes/main.c b/aes/main.c
index 1a70b03..14986fd 100644
--- a/aes/main.c
+++ b/aes/main.c
@@ -1,7 +1,12 @@
-#include "test-common.h"
-#include <ic.h>
-#include <memory_management.h>
+#include "support/test-common.h"
+
+#ifdef MSP430_ARCH
 #include <msp430fr5994.h>
+#include "iclib/msp430_ic.h"
+#include "iclib/memory_management.h"
+#elif defined(CM0_ARCH)
+#include "support/cm0-support.h"
+#endif
 
 /* Benchmark includes */
 #include "TI_aes_128_encr_only.h"
@@ -15,14 +20,15 @@ static unsigned char key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
 /* ------ Function Declarations ---------------------------------------------*/
 
 int main(void) {
+  target_init();
   while (1) {
-    P1OUT |= BIT2;
+    indicate_begin();
     // AES128 in Cipher Block Chaining mode
     uint8_t *prevBlock;
     uint8_t *ptr = input;
 
     // Acquire and encrypt first block
-    mm_acquire_array(ptr, AES_BLOCK_SIZE, MM_READWRITE);
+    //mm_acquire_array(ptr, AES_BLOCK_SIZE, MM_READWRITE);
     aes_encrypt(ptr, key);
     prevBlock = ptr;
     ptr += AES_BLOCK_SIZE;
@@ -30,7 +36,7 @@ int main(void) {
     // Encrypt remaining blocks
     while (ptr < input + sizeof(input)) {
       // Acquire current block
-      mm_acquire_array(ptr, AES_BLOCK_SIZE, MM_READWRITE);
+      //mm_acquire_array(ptr, AES_BLOCK_SIZE, MM_READWRITE);
 
       // CBC - Cipher Block Chaining mode
       for (int i = 0; i < AES_BLOCK_SIZE; i++) {
@@ -38,7 +44,7 @@ int main(void) {
       }
 
       // Release previous block
-      mm_release_array(prevBlock, AES_BLOCK_SIZE);
+      //mm_release_array(prevBlock, AES_BLOCK_SIZE);
 
       // Encrypt current block
       aes_encrypt(ptr, key);
@@ -48,12 +54,12 @@ int main(void) {
     }
 
     // Release last block
-    mm_release_array(prevBlock, AES_BLOCK_SIZE);
+    //mm_release_array(prevBlock, AES_BLOCK_SIZE);
 
-    P1OUT &= ~BIT2;
-    mm_flush();
+    indicate_end();
+    //mm_flush();
 
     // Delay
-    WAIT
+    wait();
   }
 }
diff --git a/cmake/common.cmake b/cmake/common.cmake
index 61e806c..2c5b749 100644
--- a/cmake/common.cmake
+++ b/cmake/common.cmake
@@ -2,39 +2,39 @@
 enable_language(C ASM)
 
 IF(${TARGET_ARCH} STREQUAL "msp430")
-include_directories($ENV{MSP430_INC}/include) # MSP430 headers
+  include_directories($ENV{MSP430_INC}/include) # MSP430 headers
 
-add_compile_options(
-    -std=c99
-    -mcpu=msp430
-    -msmall
-    #-mhwmult=none
-    -mhwmult=f5series
-    -fno-common
-    -Wall
-    -fno-zero-initialized-in-bss # We don't want to zero out whole bss on every boot
-    )
+  add_compile_options(
+      -std=c99
+      -mcpu=msp430
+      -msmall
+      #-mhwmult=none
+      -mhwmult=f5series
+      -fno-common
+      -Wall
+      -fno-zero-initialized-in-bss # We don't want to zero out whole bss on every boot
+      )
 
-# Linker scripts
-set(LSCRIPTS "-T$ENV{MSP430_INC}/include/msp430fr5994_symbols.ld")
-set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LSCRIPTS}")
+  # Linker scripts
+  set(LSCRIPTS "-T$ENV{MSP430_INC}/include/msp430fr5994_symbols.ld")
+  set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LSCRIPTS}")
 
-# Add to search path for linker scripts (xx_symbols.ld, included by main linker script)
-link_directories(
-    $ENV{MSP430_GCC_ROOT}/msp430-elf/lib/430/
-    $ENV{MSP430_GCC_ROOT}/lib/gcc/msp430-elf/8.2.0/430
-    $ENV{MSP430_INC}/include
-    $ENV{HOME}/git-arm/fused/include/
-    )
+  # Add to search path for linker scripts (xx_symbols.ld, included by main linker script)
+  link_directories(
+      $ENV{MSP430_GCC_ROOT}/msp430-elf/lib/430/
+      $ENV{MSP430_GCC_ROOT}/lib/gcc/msp430-elf/8.2.0/430
+      $ENV{MSP430_INC}/include
+      $ENV{HOME}/git-arm/fused/include/
+      )
 
-link_libraries( # Global link flags
-    # Removing standard libs and startup code to prevent unnecessary initialization
-    -nostdlib
-    -ffreestanding
-    )
+  link_libraries( # Global link flags
+      # Removing standard libs and startup code to prevent unnecessary initialization
+      -nostdlib
+      -ffreestanding
+      )
 
-# Utility for linking targets to std libs
-set(SUPPORT_LIBS mul_f5 gcc c)
+  # Utility for linking targets to std libs
+  set(SUPPORT_LIBS support-${TARGET_ARCH} mul_f5 gcc c)
 
 ELSEIF(${TARGET_ARCH} STREQUAL "cm0")
 
@@ -66,12 +66,7 @@ ELSEIF(${TARGET_ARCH} STREQUAL "cm0")
     -mcpu=cortex-m0
     )
 
-set(SUPPORT_LIBS
-  support
-  nosys
-  m
-  gcc
-  c)
+set(SUPPORT_LIBS nosys m gcc c)
 
 ELSE()
   message(ERROR "Invalid TARGET_ARCH: ${TARGET_ARCH}")
diff --git a/cmake/tail.cmake b/cmake/tail.cmake
index 4e6e261..e4539eb 100644
--- a/cmake/tail.cmake
+++ b/cmake/tail.cmake
@@ -1,38 +1,39 @@
 cmake_minimum_required(VERSION 3.13)
 
 # Commmon function to add linker script and definitions for each target
-  message("Testname ${TESTNAME}")
 
-target_link_libraries(
-  ${TESTNAME}
-  LINK_PUBLIC ic-${TARGET_ARCH}-${METHOD}
+target_link_libraries( ${TESTNAME}
+  LINK_PUBLIC support-${TARGET_ARCH}
+  LINK_PUBLIC ic-${METHOD}-${TARGET_ARCH}
   ${SUPPORT_LIBS}
   )
 
-set_target_properties(${TESTNAME} PROPERTIES SUFFIX ".elf")
+IF(${METHOD} STREQUAL "QR")
+  target_compile_definitions( ${TESTNAME} PRIVATE -DQUICKRECALL)
+ELSEIF(${METHOD} STREQUAL "AS")
+  target_compile_definitions( ${TESTNAME} PRIVATE -DALLOCATEDSTATE)
+ELSEIF(${METHOD} STREQUAL "MS")
+  target_compile_definitions( ${TESTNAME} PRIVATE -DMANAGEDSTATE)
+ENDIF()
+
 
 IF(${TARGET_ARCH} STREQUAL "msp430")
-  #add_msp_upload(${TESTNAME})
+  target_compile_definitions( ${TESTNAME} PRIVATE -DMSP430_ARCH)
   IF (${METHOD} STREQUAL "QR")
-      target_link_options(
-          ${TESTNAME}
-          PRIVATE -T${CMAKE_CURRENT_LIST_DIR}/support/msp430fr5994-fram-only.ld
-          )
+      target_link_options( ${TESTNAME}
+          PRIVATE -T${CMAKE_CURRENT_LIST_DIR}/support/msp430fr5994-fram-only.ld)
   ELSE ()
-      target_link_options(
-          ${TESTNAME}
-          PRIVATE -T${CMAKE_CURRENT_LIST_DIR}/support/msp430fr5994.ld
-          )
+      target_link_options( ${TESTNAME}
+          PRIVATE -T${CMAKE_CURRENT_LIST_DIR}/support/msp430fr5994.ld)
   ENDIF()
-
 ELSEIF(${TARGET_ARCH} STREQUAL "cm0")
-  message("Testname ${TESTNAME}")
-  target_link_options(
-      ${TESTNAME}
-      PRIVATE -T${CMAKE_CURRENT_LIST_DIR}/support/cm0-FS.ld
-      )
+  target_compile_definitions( ${TESTNAME} PRIVATE -DCM0_ARCH)
+  target_link_options( ${TESTNAME}
+      PRIVATE -T${CMAKE_CURRENT_LIST_DIR}/../support/cm0-FS.ld)
 ENDIF()
 
+set_target_properties(${TESTNAME} PROPERTIES SUFFIX ".elf")
+
 # Emit map, listing and hex
 add_custom_command(TARGET ${TESTNAME} POST_BUILD
   COMMAND ${TC-SIZE} -A -x "$<TARGET_FILE:${TESTNAME}>" > ${TESTNAME}.map
diff --git a/iclib/CMakeLists.txt b/iclib/CMakeLists.txt
index 4f0513d..669bc35 100644
--- a/iclib/CMakeLists.txt
+++ b/iclib/CMakeLists.txt
@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.0)
 FOREACH(METHOD "MS" "AS" "QR")
     string(COMPARE EQUAL ${METHOD} "QR" QUICKRECALL)
     include(${CMAKE_CURRENT_LIST_DIR}/../cmake/common.cmake)
-    set(TESTNAME "ic-${TARGET_ARCH}-${METHOD}")
+    set(TESTNAME "ic-${METHOD}-${TARGET_ARCH}")
 
     IF(${TARGET_ARCH} STREQUAL "cm0")
       add_library(
@@ -12,7 +12,7 @@ FOREACH(METHOD "MS" "AS" "QR")
         cm0_ic.h
         cm0_ic.S
         )
-      target_link_libraries(${TESTNAME} support)
+      target_link_libraries(${TESTNAME} support-${TARGET_ARCH})
     ELSEIF(${TARGET_ARCH} STREQUAL "msp430")
       add_library(
         ${TESTNAME}
diff --git a/iclib/cm0_ic.c b/iclib/cm0_ic.c
index 8738dd5..2c9c64f 100644
--- a/iclib/cm0_ic.c
+++ b/iclib/cm0_ic.c
@@ -21,11 +21,10 @@ extern uint8_t __boot_stack_high;
 // ------------- Globals -------------------------------------------------------
 
 // ------------- PERSISTENT VARIABLES ------------------------------------------
-#define PERSISTENT __attribute__((section(".persistent")))
 
 // Snapshots
 uint32_t saved_stack_pointer PERSISTENT;
-uint32_t stack_snapshot[STACK_SIZE] PERSISTENT;
+uint32_t stack_snapshot[STACK_SIZE / sizeof(uint32_t)] PERSISTENT;
 
 int suspending PERSISTENT;        /*! Flag to determine whether returning from
                                  suspend() or restore() */
diff --git a/iclib/cm0_ic.h b/iclib/cm0_ic.h
index 147d152..73859f0 100644
--- a/iclib/cm0_ic.h
+++ b/iclib/cm0_ic.h
@@ -1,7 +1,12 @@
 #ifndef IC_SRC
 #define IC_SRC
 
+#define PERSISTENT __attribute__((section(".persistent")))
+#define MMDATA __attribute__((section(".mmdata")))
+
 #include <stdint.h>
 #include "iclib/config.h"
 
+void _start();
+
 #endif /* IC_SRC */
diff --git a/iclib/config.h b/iclib/config.h
index 070a71d..0c612b3 100644
--- a/iclib/config.h
+++ b/iclib/config.h
@@ -13,7 +13,7 @@
 /* ------ Section sizes -----------------------------------------------------*/
 /* Allocates (plenty of) space for snapshot in FRAM. Note that these can be made
  * smaller for most apps*/
-#define STACK_SIZE 0x1000
+#define STACK_SIZE 0x400
 #define BSS_SIZE 0x1000
 #define DATA_SIZE 0x2000
 #define MMDATA_SIZE 0x2000
diff --git a/iclib/memory_management.h b/iclib/memory_management.h
index 2f8409f..01de055 100644
--- a/iclib/memory_management.h
+++ b/iclib/memory_management.h
@@ -8,8 +8,6 @@
 #include "config.h"
 
 /***************** Macros (Inline Functions) Definitions *********************/
-// Utility to put variables in the mmdata section
-#define MMDATA __attribute__((section(".mmdata")))
 
 /************************** Constant Definitions *****************************/
 
diff --git a/support/CMakeLists.txt b/support/CMakeLists.txt
index 2510d76..f642358 100644
--- a/support/CMakeLists.txt
+++ b/support/CMakeLists.txt
@@ -1,8 +1,8 @@
 cmake_minimum_required(VERSION 3.13)
 
-add_library(support "")
+add_library(support-${TARGET_ARCH} "")
 
-target_sources(support
-  PRIVATE cm0-vectors.c
+target_sources(support-${TARGET_ARCH}
+  PRIVATE cm0-vectors.c cm0-support.c
   PUBLIC cm0.h
   )
diff --git a/support/cm0-FS.ld b/support/cm0-FS.ld
index 56fc191..548d6f2 100644
--- a/support/cm0-FS.ld
+++ b/support/cm0-FS.ld
@@ -3,14 +3,15 @@ EXTERN (__VECTOR_TRABLE)
 ENTRY(Reset_Handler)
 
 __stack_size = 0x200;
+__boot_stack_size = 0x40;
 
 MEMORY
 {
         /*rom (rwx) : ORIGIN = 0x08000000, LENGTH = 16K */
         /*ram (rwx) : ORIGIN = 0x20000000, LENGTH = 4K */
         /*invm (rx) : ORIGIN = 0x20000000, LENGTH = 8K */
-        rom (rx)   : ORIGIN = 0x08000000, LENGTH = 8K
-        nvram (rwx): ORIGIN = 0x20000000, LENGTH = 8K
+        rom (rx)   : ORIGIN = 0x08000000, LENGTH = 16K
+        nvram (rwx): ORIGIN = 0x20000000, LENGTH = 16K
         sram (rwx) : ORIGIN = 0x20000000 + 16K, LENGTH = 8K
 }
 
@@ -123,9 +124,9 @@ SECTIONS
 .boot_stack (NOLOAD) :
 {
   PROVIDE(__boot_stack_low = .);
-  . = ALIGN(4)
+  . = ALIGN(4);
   *(.stack)
-  . += 0x100;
+  . += __boot_stack_size;
   PROVIDE(__boot_stack_high = .);
 } > sram
 
diff --git a/support/cm0-support.c b/support/cm0-support.c
index 08bd162..fefe667 100644
--- a/support/cm0-support.c
+++ b/support/cm0-support.c
@@ -1,8 +1,8 @@
 #include <stdint.h>
 #include <string.h>
+#include <fused.h>
 #include "support/cm0.h"
 #include "support/cm0-support.h"
-#include <fused.h>
 
 void indicate_begin() {
    SIMPLE_MONITOR = SIMPLE_MONITOR_INDICATE_BEGIN;
@@ -31,7 +31,7 @@ void fused_assert (bool c) {
 
 void target_init() { return; }
 
-// Boot function
+/*
 __attribute__((optimize(1), naked, used, section(".ftext"))) void _start() {
   // Boot data (if necessary)
   extern uint8_t __data_low, __data_high, __data_loadLow;
@@ -42,3 +42,4 @@ __attribute__((optimize(1), naked, used, section(".ftext"))) void _start() {
   int main();
   main();
 }
+*/
diff --git a/support/cm0-support.h b/support/cm0-support.h
index 760bae6..04e27a6 100644
--- a/support/cm0-support.h
+++ b/support/cm0-support.h
@@ -8,4 +8,4 @@ void indicate_begin();
 void indicate_end();
 void fused_assert(bool c);
 void fused_end_simulation();
-void _start();
+// void _start();
diff --git a/support/cm0-vectors.c b/support/cm0-vectors.c
index c3d948a..3a061f4 100644
--- a/support/cm0-vectors.c
+++ b/support/cm0-vectors.c
@@ -24,6 +24,7 @@
 
 #include <fused.h>
 #include "support/cm0.h"
+#include "iclib/cm0_ic.h"
 
 /*----------------------------------------------------------------------------
   External References
-- 
GitLab