|  | 
|  | 1 | +# Licensed to the Apache Software Foundation (ASF) under one | 
|  | 2 | +# or more contributor license agreements.  See the NOTICE file | 
|  | 3 | +# distributed with this work for additional information | 
|  | 4 | +# regarding copyright ownership.  The ASF licenses this file | 
|  | 5 | +# to you under the Apache License, Version 2.0 (the | 
|  | 6 | +# "License"); you may not use this file except in compliance | 
|  | 7 | +# with the License.  You may obtain a copy of the License at | 
|  | 8 | +# | 
|  | 9 | +#   http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 10 | +# | 
|  | 11 | +# Unless required by applicable law or agreed to in writing, | 
|  | 12 | +# software distributed under the License is distributed on an | 
|  | 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | 
|  | 14 | +# KIND, either express or implied.  See the License for the | 
|  | 15 | +# specific language governing permissions and limitations | 
|  | 16 | +# under the License. | 
|  | 17 | + | 
|  | 18 | +if(MSVC) | 
|  | 19 | +  message(STATUS "TSIM build is skipped in Windows..") | 
|  | 20 | +else() | 
|  | 21 | +  find_program(PYTHON NAMES python python3 python3.6) | 
|  | 22 | +  find_program(VERILATOR NAMES verilator) | 
|  | 23 | + | 
|  | 24 | +  if (VERILATOR AND PYTHON) | 
|  | 25 | + | 
|  | 26 | +    if (TSIM_TOP_NAME STREQUAL "") | 
|  | 27 | +      message(FATAL_ERROR "TSIM_TOP_NAME should be defined") | 
|  | 28 | +    endif() | 
|  | 29 | + | 
|  | 30 | +    if (TSIM_BUILD_NAME STREQUAL "") | 
|  | 31 | +      message(FATAL_ERROR "TSIM_BUILD_NAME should be defined") | 
|  | 32 | +    endif() | 
|  | 33 | + | 
|  | 34 | +    set(TSIM_CONFIG ${PYTHON} ${CMAKE_CURRENT_SOURCE_DIR}/python/tsim/config.py) | 
|  | 35 | + | 
|  | 36 | +    execute_process(COMMAND ${TSIM_CONFIG} --get-target OUTPUT_VARIABLE __TSIM_TARGET) | 
|  | 37 | +    execute_process(COMMAND ${TSIM_CONFIG} --get-top-name OUTPUT_VARIABLE __TSIM_TOP_NAME) | 
|  | 38 | +    execute_process(COMMAND ${TSIM_CONFIG} --get-build-name OUTPUT_VARIABLE __TSIM_BUILD_NAME) | 
|  | 39 | +    execute_process(COMMAND ${TSIM_CONFIG} --get-use-trace OUTPUT_VARIABLE __TSIM_USE_TRACE) | 
|  | 40 | +    execute_process(COMMAND ${TSIM_CONFIG} --get-trace-name OUTPUT_VARIABLE __TSIM_TRACE_NAME) | 
|  | 41 | + | 
|  | 42 | +    string(STRIP ${__TSIM_TARGET} TSIM_TARGET) | 
|  | 43 | +    string(STRIP ${__TSIM_TOP_NAME} TSIM_TOP_NAME) | 
|  | 44 | +    string(STRIP ${__TSIM_BUILD_NAME} TSIM_BUILD_NAME) | 
|  | 45 | +    string(STRIP ${__TSIM_USE_TRACE} TSIM_USE_TRACE) | 
|  | 46 | +    string(STRIP ${__TSIM_TRACE_NAME} TSIM_TRACE_NAME) | 
|  | 47 | + | 
|  | 48 | +    set(TSIM_BUILD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/${TSIM_BUILD_NAME}) | 
|  | 49 | + | 
|  | 50 | +    if (TSIM_TARGET STREQUAL "chisel") | 
|  | 51 | + | 
|  | 52 | +      find_program(SBT NAMES sbt) | 
|  | 53 | + | 
|  | 54 | +      if (SBT) | 
|  | 55 | + | 
|  | 56 | +        # Install Chisel VTA package for DPI modules | 
|  | 57 | +        set(VTA_CHISEL_DIR ${VTA_DIR}/hardware/chisel) | 
|  | 58 | + | 
|  | 59 | +        execute_process(WORKING_DIRECTORY ${VTA_CHISEL_DIR} | 
|  | 60 | +          COMMAND ${SBT} publishLocal RESULT_VARIABLE RETCODE) | 
|  | 61 | + | 
|  | 62 | +        if (NOT RETCODE STREQUAL "0") | 
|  | 63 | +          message(FATAL_ERROR "[TSIM] sbt failed to install VTA scala package") | 
|  | 64 | +        endif() | 
|  | 65 | + | 
|  | 66 | +        # Chisel - Scala to Verilog compilation | 
|  | 67 | +        set(TSIM_CHISEL_DIR ${CMAKE_CURRENT_SOURCE_DIR}/hardware/chisel) | 
|  | 68 | +        set(CHISEL_TARGET_DIR ${TSIM_BUILD_DIR}/chisel) | 
|  | 69 | +        set(CHISEL_OPT "test:runMain test.Elaborate --target-dir ${CHISEL_TARGET_DIR} --top-name ${TSIM_TOP_NAME}") | 
|  | 70 | + | 
|  | 71 | +        execute_process(WORKING_DIRECTORY ${TSIM_CHISEL_DIR} COMMAND ${SBT} ${CHISEL_OPT} RESULT_VARIABLE RETCODE) | 
|  | 72 | + | 
|  | 73 | +        if (NOT RETCODE STREQUAL "0") | 
|  | 74 | +          message(FATAL_ERROR "[TSIM] sbt failed to compile from Chisel to Verilog.") | 
|  | 75 | +        endif() | 
|  | 76 | + | 
|  | 77 | +        file(GLOB VERILATOR_RTL_SRC ${CHISEL_TARGET_DIR}/*.v) | 
|  | 78 | + | 
|  | 79 | +      else() | 
|  | 80 | +        message(FATAL_ERROR "[TSIM] sbt should be installed for Chisel") | 
|  | 81 | +      endif() # sbt | 
|  | 82 | + | 
|  | 83 | +    elseif (TSIM_TARGET STREQUAL "verilog") | 
|  | 84 | + | 
|  | 85 | +      set(VTA_VERILOG_DIR ${VTA_DIR}/hardware/chisel/src/main/resources/verilog) | 
|  | 86 | +      set(TSIM_VERILOG_DIR ${CMAKE_CURRENT_SOURCE_DIR}/hardware/verilog) | 
|  | 87 | +      file(GLOB VERILATOR_RTL_SRC ${VTA_VERILOG_DIR}/*.v ${TSIM_VERILOG_DIR}/*.v) | 
|  | 88 | + | 
|  | 89 | +    else() | 
|  | 90 | +      message(STATUS "[TSIM] target language can be only verilog or chisel...") | 
|  | 91 | +    endif() # TSIM_TARGET | 
|  | 92 | + | 
|  | 93 | +    if (TSIM_TARGET STREQUAL "chisel" OR TSIM_TARGET STREQUAL "verilog") | 
|  | 94 | + | 
|  | 95 | +      # Check if tracing can be enabled | 
|  | 96 | +      if (NOT TSIM_USE_TRACE STREQUAL "OFF") | 
|  | 97 | +        message(STATUS "[TSIM] Verilog enable tracing") | 
|  | 98 | +      else() | 
|  | 99 | +        message(STATUS "[TSIM] Verilator disable tracing") | 
|  | 100 | +      endif() | 
|  | 101 | + | 
|  | 102 | +      # Verilator - Verilog to C++ compilation | 
|  | 103 | +      set(VERILATOR_TARGET_DIR ${TSIM_BUILD_DIR}/verilator) | 
|  | 104 | +      set(VERILATOR_OPT +define+RANDOMIZE_GARBAGE_ASSIGN +define+RANDOMIZE_REG_INIT) | 
|  | 105 | +      list(APPEND VERILATOR_OPT +define+RANDOMIZE_MEM_INIT --x-assign unique) | 
|  | 106 | +      list(APPEND VERILATOR_OPT --output-split 20000 --output-split-cfuncs 20000) | 
|  | 107 | +      list(APPEND VERILATOR_OPT --top-module ${TSIM_TOP_NAME} -Mdir ${VERILATOR_TARGET_DIR}) | 
|  | 108 | +      list(APPEND VERILATOR_OPT --cc ${VERILATOR_RTL_SRC}) | 
|  | 109 | + | 
|  | 110 | +      if (NOT TSIM_USE_TRACE STREQUAL "OFF") | 
|  | 111 | +        list(APPEND VERILATOR_OPT --trace) | 
|  | 112 | +      endif() | 
|  | 113 | + | 
|  | 114 | +      execute_process(COMMAND ${VERILATOR} ${VERILATOR_OPT} RESULT_VARIABLE RETCODE) | 
|  | 115 | + | 
|  | 116 | +      if (NOT RETCODE STREQUAL "0") | 
|  | 117 | +        message(FATAL_ERROR "[TSIM] Verilator failed to compile Verilog to C++...") | 
|  | 118 | +      endif() | 
|  | 119 | + | 
|  | 120 | +      # Build shared library (.so) | 
|  | 121 | +      set(VTA_HW_DPI_DIR ${VTA_DIR}/hardware/dpi) | 
|  | 122 | +      set(VERILATOR_INC_DIR /usr/local/share/verilator/include) | 
|  | 123 | +      set(VERILATOR_LIB_SRC ${VERILATOR_INC_DIR}/verilated.cpp ${VERILATOR_INC_DIR}/verilated_dpi.cpp) | 
|  | 124 | + | 
|  | 125 | +      if (NOT TSIM_USE_TRACE STREQUAL "OFF") | 
|  | 126 | +        list(APPEND VERILATOR_LIB_SRC ${VERILATOR_INC_DIR}/verilated_vcd_c.cpp) | 
|  | 127 | +      endif() | 
|  | 128 | + | 
|  | 129 | +      file(GLOB VERILATOR_GEN_SRC ${VERILATOR_TARGET_DIR}/*.cpp) | 
|  | 130 | +      file(GLOB VERILATOR_SRC ${VTA_HW_DPI_DIR}/tsim_device.cc) | 
|  | 131 | +      add_library(tsim SHARED ${VERILATOR_LIB_SRC} ${VERILATOR_GEN_SRC} ${VERILATOR_SRC}) | 
|  | 132 | + | 
|  | 133 | +      set(VERILATOR_DEF VL_TSIM_NAME=V${TSIM_TOP_NAME} VL_PRINTF=printf VM_COVERAGE=0 VM_SC=0) | 
|  | 134 | +      if (NOT TSIM_USE_TRACE STREQUAL "OFF") | 
|  | 135 | +        list(APPEND VERILATOR_DEF VM_TRACE=1 TSIM_TRACE_FILE=${TSIM_BUILD_DIR}/${TSIM_TRACE_NAME}.vcd) | 
|  | 136 | +      else() | 
|  | 137 | +        list(APPEND VERILATOR_DEF VM_TRACE=0) | 
|  | 138 | +      endif() | 
|  | 139 | +      target_compile_definitions(tsim PRIVATE ${VERILATOR_DEF}) | 
|  | 140 | +      target_compile_options(tsim PRIVATE -Wno-sign-compare -include V${TSIM_TOP_NAME}.h) | 
|  | 141 | +      target_include_directories(tsim PRIVATE ${VERILATOR_TARGET_DIR} ${VERILATOR_INC_DIR} ${VERILATOR_INC_DIR}/vltstd ${VTA_DIR}/include) | 
|  | 142 | + | 
|  | 143 | +      if(APPLE) | 
|  | 144 | +        set_target_properties(tsim PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") | 
|  | 145 | +      endif(APPLE) | 
|  | 146 | + | 
|  | 147 | +    endif() # TSIM_TARGET STREQUAL "chisel" OR TSIM_TARGET STREQUAL "verilog" | 
|  | 148 | + | 
|  | 149 | +  else() | 
|  | 150 | +    message(STATUS "[TSIM] could not find Python or Verilator, build is skipped...") | 
|  | 151 | +  endif() # VERILATOR | 
|  | 152 | +endif() # MSVC | 
0 commit comments