Add cmake build files.

* Add files necessary to build vec target with cmake.
* Change path the springbok library files.
* Modify paths in Makefile to match changes to project structure.

Change-Id: I90d18492a62c654da5c06b94cc12048422697821
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..378eac2
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+build
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..1c33457
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,16 @@
+
+cmake_minimum_required (VERSION 3.1)
+
+set(CMAKE_C_ABI_COMPILED ON)
+set(CMAKE_CXX_ABI_COMPILED ON)
+
+set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/cmake/riscv_baremetal.cmake")
+
+project(springbok_project)
+
+enable_language(ASM)
+
+set(LINKER_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/springbok/matcha.ld")
+
+add_subdirectory(springbok)
+add_subdirectory(hello_vec)
diff --git a/cmake/riscv_baremetal.cmake b/cmake/riscv_baremetal.cmake
new file mode 100644
index 0000000..745d0ad
--- /dev/null
+++ b/cmake/riscv_baremetal.cmake
@@ -0,0 +1,42 @@
+set(SHODAN_TOOLCHAIN_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../../cache/toolchain_vp/bin CACHE STRING "Shodan toolchain path")
+message (STATUS "Shodan toolchain path is ${SHODAN_TOOLCHAIN_PATH}")
+find_file( RISCV_GCC_COMPILER "riscv32-unknown-elf-gcc" HINTS ${SHODAN_TOOLCHAIN_PATH} PATHS ENV INCLUDE)
+
+# Check compiler found
+if (EXISTS ${RISCV_GCC_COMPILER})
+message(STATUS "RISC-V GCC found. Using ${RISCV_GCC_COMPILER}")
+else()
+message(FATAL_ERROR "RISC-V GCC not found. ${RISCV_GCC_COMPILER}")
+endif()
+
+get_filename_component(RISCV_TOOLCHAIN_BIN_PATH ${RISCV_GCC_COMPILER} DIRECTORY)
+get_filename_component(RISCV_TOOLCHAIN_BIN_GCC ${RISCV_GCC_COMPILER} NAME_WE)
+get_filename_component(RISCV_TOOLCHAIN_BIN_EXT ${RISCV_GCC_COMPILER} EXT)
+
+message( "RISC-V GCC Path: ${RISCV_TOOLCHAIN_BIN_PATH}" )
+
+STRING(REGEX REPLACE "\-gcc" "-" CROSS_COMPILE ${RISCV_GCC_COMPILER})
+message( "RISC-V Cross Compile Prefix: ${CROSS_COMPILE}" )
+
+set( CMAKE_SYSTEM_NAME          Generic )
+set( CMAKE_SYSTEM_PROCESSOR     rv32imfv )
+set( CMAKE_SYSTEM_ABI           ilp32 )
+set( CMAKE_EXECUTABLE_SUFFIX    ".elf")
+
+set(CMAKE_AR ${CROSS_COMPILE}ar)
+set(CMAKE_ASM_COMPILER ${RISCV_GCC_COMPILER})
+set(CMAKE_C_COMPILER ${RISCV_GCC_COMPILER})
+set(CMAKE_CXX_COMPILER ${CROSS_COMPILE}g++)
+
+set( CMAKE_OBJCOPY      ${RISCV_TOOLCHAIN_BIN_PATH}/${CROSS_COMPILE}objcopy
+     CACHE FILEPATH "The toolchain objcopy command " FORCE )
+
+set( CMAKE_OBJDUMP      ${RISCV_TOOLCHAIN_BIN_PATH}/${CROSS_COMPILE}objdump
+     CACHE FILEPATH "The toolchain objdump command " FORCE )
+
+set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=${CMAKE_SYSTEM_PROCESSOR}  -mabi=${CMAKE_SYSTEM_ABI}")
+
+set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "" )
+set( CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "" )
+set( CMAKE_ASM_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "" )
+set( CMAKE_EXE_LINKER_FLAGS   "${CMAKE_EXE_LINKER_FLAGS}  -nostartfiles   " )
diff --git a/hello_vec/CMakeLists.txt b/hello_vec/CMakeLists.txt
new file mode 100644
index 0000000..ee14d41
--- /dev/null
+++ b/hello_vec/CMakeLists.txt
@@ -0,0 +1,36 @@
+cmake_minimum_required(VERSION 3.10)
+
+project(hello_vec)
+
+set(TARGET hello_vec)
+set(ELF ${TARGET}.elf)
+
+add_executable(${ELF} main.c)
+
+target_include_directories(${ELF} PUBLIC include)
+
+set_target_properties(${ELF} PROPERTIES LINK_DEPENDS "${LINKER_SCRIPT}")
+
+target_link_libraries(${ELF} springbok)
+
+set_target_properties(
+	${ELF}
+	PROPERTIES
+	LINK_FLAGS
+     "-specs=nano.specs \
+    -Wl,--gc-sections \
+    -Wl,--print-memory-usage \
+    -Wl,-Map=${PROJECT_NAME}.map \
+    -T${LINKER_SCRIPT}")
+
+target_compile_options(${ELF} PUBLIC
+    -Wall
+    -Werror
+    -std=c11
+    -O3
+    -g3
+    -ggdb
+    -ffreestanding
+    -ffunction-sections
+    -fstack-usage
+    -mstrict-align)
diff --git a/hello_vec/Makefile b/hello_vec/Makefile
index 7a09484..c30c5c7 100644
--- a/hello_vec/Makefile
+++ b/hello_vec/Makefile
@@ -7,21 +7,22 @@
 CFLAGS   += -Wall
 CFLAGS   += -Werror
 CFLAGS   += -std=gnu11
-CFLAGS   += -march=rv32imv
-CFLAGS   += -I${ROOTDIR}/build/
-CFLAGS   += -O3 -g
+CFLAGS   += -march=rv32imfv
+CFLAGS   += -I${ROOTDIR}/springbok/include
+CFLAGS   += -O3 -g3 -ggdb
 
 CFLAGS   += -ffreestanding
 CFLAGS   += -ffunction-sections
-CFLAGS   += --sysroot=${ROOTDIR}/build/libc/
 CFLAGS   += -fstack-usage
+CFLAGS   += -mstrict-align
 
 LDFLAGS  += -nostartfiles
 LDFLAGS  += -specs=nano.specs
 LDFLAGS  += -Wl,--gc-sections
 LDFLAGS  += -Wl,--print-memory-usage
 LDFLAGS  += -Wl,-Map=${BUILDDIR}/hello_vec.map
-LDFLAGS  += -T../build/matcha.ld
+LDFLAGS  += -T../springbok/matcha.ld
+
 
 CC       := ${SHODANROOTDIR}/cache/toolchain_vp/bin/riscv32-unknown-elf-gcc
 
@@ -34,15 +35,15 @@
 	mkdir -p $(dir $@)
 	${CC} ${CFLAGS} -c $< -o $@
 
-${BUILDDIR}/springbok_gloss.o: ${ROOTDIR}/build/springbok_gloss.c ${MAKEFILE_LIST}
+${BUILDDIR}/springbok_gloss.o: ${ROOTDIR}/springbok/springbok_gloss.c ${MAKEFILE_LIST}
 	mkdir -p $(dir $@)
 	${CC} ${CFLAGS} -c $< -o $@
 
-${BUILDDIR}/Reset_Handler.o: ${ROOTDIR}/build/Reset_Handler.c ${MAKEFILE_LIST}
+${BUILDDIR}/Reset_Handler.o: ${ROOTDIR}/springbok/Reset_Handler.c ${MAKEFILE_LIST}
 	mkdir -p $(dir $@)
 	${CC} ${CFLAGS} -c $< -o $@
 
-${BUILDDIR}/crt0.o: ${ROOTDIR}/build/crt0.s ${MAKEFILE_LIST}
+${BUILDDIR}/crt0.o: ${ROOTDIR}/springbok/crt0.s ${MAKEFILE_LIST}
 	mkdir -p $(dir $@)
 	${CC} ${CFLAGS} -c $< -o $@
 
diff --git a/hello_vec/main.c b/hello_vec/main.c
index 9fff79a..b1ff42f 100644
--- a/hello_vec/main.c
+++ b/hello_vec/main.c
@@ -1,5 +1,5 @@
 #include <stdio.h>
-#include <springbok_intrinsics.h>
+#include <springbok.h>
 
 int main(void) {
   unsigned int start = springbok_ccount();
diff --git a/build/.gitignore b/springbok/.gitignore
similarity index 100%
rename from build/.gitignore
rename to springbok/.gitignore
diff --git a/springbok/CMakeLists.txt b/springbok/CMakeLists.txt
new file mode 100644
index 0000000..944bc8b
--- /dev/null
+++ b/springbok/CMakeLists.txt
@@ -0,0 +1,34 @@
+cmake_minimum_required(VERSION 3.1)
+
+enable_language(ASM)
+
+add_library(springbok INTERFACE)
+add_library(springbok_intrinsic STATIC)
+target_sources(springbok_intrinsic PRIVATE
+    crt0.s
+    Reset_Handler.c
+    springbok_gloss.c)
+
+target_include_directories(springbok_intrinsic PUBLIC include)
+
+target_link_libraries(springbok
+INTERFACE
+    springbok_intrinsic
+)
+
+target_compile_options(springbok_intrinsic PUBLIC
+    -Wall
+    -Werror
+    -std=c11
+    -O3
+    -g3
+    -ggdb
+    -ffreestanding
+    -ffunction-sections
+    -fstack-usage
+    -mstrict-align)
+
+target_link_options(springbok
+INTERFACE
+-Wl,--whole-archive ${CMAKE_CURRENT_BINARY_DIR}/libspringbok_intrinsic.a -Wl,--no-whole-archive
+)
\ No newline at end of file
diff --git a/build/Reset_Handler.c b/springbok/Reset_Handler.c
similarity index 70%
rename from build/Reset_Handler.c
rename to springbok/Reset_Handler.c
index c2e0f36..e76a0e8 100644
--- a/build/Reset_Handler.c
+++ b/springbok/Reset_Handler.c
@@ -11,25 +11,25 @@
   // one, number of constructors) depending upon the compiler, but the list is
   // guaranteed to be null terminated. So let's just ignore the first element
   // and take advantage of the null termination.
-  int dtor_count = 1;
-  while (__DTOR_LIST__[dtor_count] != NULL) {
-    __DTOR_LIST__[dtor_count]();
-    dtor_count++;
-  }
+//   int dtor_count = 1;
+//   while (__DTOR_LIST__[dtor_count] != NULL) {
+//     __DTOR_LIST__[dtor_count]();
+//     dtor_count++;
+//   }
 }
 
 int Reset_Handler(void) {
   // Constructors are called in reverse order
   // The first element in the constructor has the same weird meaning as the
   // destructor list, so best to just ignore it, too.
-  int ctor_count = 0;
-  while (__CTOR_LIST__[ctor_count+1] != NULL) {
-    ctor_count++;
-  }
-  while (ctor_count > 0) {
-    __CTOR_LIST__[ctor_count]();
-    ctor_count--;
-  }
+//   int ctor_count = 0;
+//   while (__CTOR_LIST__[ctor_count+1] != NULL) {
+//     ctor_count++;
+//   }
+//   while (ctor_count > 0) {
+//     __CTOR_LIST__[ctor_count]();
+//     ctor_count--;
+//   }
 
   // Call any other C-friendly initialization routines here.
 
diff --git a/build/crt0.s b/springbok/crt0.s
similarity index 100%
rename from build/crt0.s
rename to springbok/crt0.s
diff --git a/springbok/include/springbok.h b/springbok/include/springbok.h
new file mode 100644
index 0000000..b15e3c7
--- /dev/null
+++ b/springbok/include/springbok.h
@@ -0,0 +1,29 @@
+#ifndef SPRINGBOK_H
+#define SPRINGBOK_H
+#include <springbok_intrinsics.h>
+#include <stdio.h>
+
+#define ERROR_TAG "ERROR"
+#define INFO_TAG "INFO"
+#define DEBUG_TAG "DEBUG"
+
+#define ENDLINE "\n"
+#define LOG_FMT "%s |"
+#define LOG_ARGS(LOG_TAG) LOG_TAG
+
+#define LOG_MAX_SZ  128
+
+#define SIMLOG(sim_log_level,fmt, ...) \
+    do { \
+        char tmp_log_msg[LOG_MAX_SZ]; \
+        snprintf(tmp_log_msg, LOG_MAX_SZ, fmt, __VA_ARGS__); \
+        springbok_simprint_##sim_log_level(tmp_log_msg, 0); \
+    } while (0)
+
+#define LOG_ERROR(msg, args...) SIMLOG(error, LOG_FMT msg ENDLINE, LOG_ARGS(ERROR_TAG), ## args)
+#define LOG_WARN(msg, args...) SIMLOG(warning, LOG_FMT msg ENDLINE, LOG_ARGS(ERROR_TAG), ## args)
+#define LOG_INFO(msg, args...) SIMLOG(info, LOG_FMT msg ENDLINE, LOG_ARGS(INFO_TAG), ## args)
+#define LOG_DEBUG(msg, args...) SIMLOG(debug, LOG_FMT msg ENDLINE, LOG_ARGS(DEBUG_TAG), ## args)
+#define LOG_NOISY(msg, args...) SIMLOG(noisy, LOG_FMT msg ENDLINE, LOG_ARGS(ERROR_TAG), ## args)
+
+#endif
\ No newline at end of file
diff --git a/build/springbok_intrinsics.h b/springbok/include/springbok_intrinsics.h
similarity index 79%
rename from build/springbok_intrinsics.h
rename to springbok/include/springbok_intrinsics.h
index 739de90..6f18e94 100644
--- a/build/springbok_intrinsics.h
+++ b/springbok/include/springbok_intrinsics.h
@@ -26,10 +26,10 @@
 //   none
 static inline void springbok_simprint(int _loglevel, const char *_string, int _number) {
   // simprint a0, a1, a2 # "-------[rs2][rs1]000[rd ]1111011"
-  register int         loglevel asm ("a0") = _loglevel;
-  register const char *string   asm ("a1") = _string;
-  register int         number   asm ("a2") = _number;
-  __asm volatile ("\t.word 0x00C5857B\n" :
+  register int         loglevel __asm__ ("a0") = _loglevel;
+  register const char *string   __asm__ ("a1") = _string;
+  register int         number   __asm__ ("a2") = _number;
+  __asm__ volatile ("\t.word 0x00C5857B\n" :
                   /* no outputs */ :
                   "r"(loglevel), "r"(string), "r"(number) :
                   /* no clobbers */);
@@ -43,12 +43,8 @@
 // Outputs:
 //   the number of instructions executed since reset
 static inline unsigned int springbok_icount(void) {
-  // icount a0 # "------------00000001[rd ]1111011"
-  register unsigned int retval asm ("a0");
-  __asm volatile ("\t.word 0x0000157B\n" :
-                  "=r"(retval) :
-                  /* no inputs */ :
-                  /* no clobbers */);
+  int retval;
+  __asm__ volatile("csrr %0, 0x7c0;" : "=r"(retval));
   return retval;
 }
 
@@ -61,11 +57,8 @@
 //   the number of unhalted cycles since reset
 static inline unsigned int springbok_ccount(void) {
   // ccount a0 # "------------00001001[rd ]1111011"
-  register unsigned int retval asm ("a0");
-  __asm volatile ("\t.word 0x0000957B\n" :
-                  "=r"(retval) :
-                  /* no inputs */ :
-                  /* no clobbers */);
+  int retval;
+  __asm__ volatile("csrr %0, 0x7c1;" : "=r"(retval));
   return retval;
 }
 
@@ -78,7 +71,7 @@
 //   none
 static inline void springbok_hostreq(void) {
   // hostreq # "-----------------010-----1111011"
-  __asm volatile ("\t.word 0x0000207B\n" :
+    __asm__ volatile ("\t.word 0x0000207B\n" :
                   /* no outputs */ :
                   /* no inputs */ :
                   /* no clobbers */);
@@ -95,7 +88,7 @@
 //   none
 __attribute__((noreturn)) static inline void springbok_finish(void) {
   // finish # "-----------------011-----1111011"
-  __asm volatile ("\t.word 0x0000307B\n" :
+    __asm__ volatile ("\t.word 0x0000307B\n" :
                   /* no outputs */ :
                   /* no inputs */ :
                   /* no clobbers */);
diff --git a/build/matcha.ld b/springbok/matcha.ld
similarity index 100%
rename from build/matcha.ld
rename to springbok/matcha.ld
diff --git a/build/springbok_gloss.c b/springbok/springbok_gloss.c
similarity index 100%
rename from build/springbok_gloss.c
rename to springbok/springbok_gloss.c