diff --git a/target/sim/Makefile b/target/sim/Makefile index a532a052f..aa1b9d3fd 100644 --- a/target/sim/Makefile +++ b/target/sim/Makefile @@ -59,7 +59,6 @@ CLANG_FORMAT ?= $(shell which clang-format-10.0.1) ######################### BIN_DIR ?= bin -PLATFORM_HEADERS_DIR ?= sw/shared/platform/generated TARGET_ADDRMAP_DIR ?= $(abspath addrmap) SOURCE_ADDRMAP_DIR ?= $(ROOT)/util/addrmap DOCS_DIR ?= $(ROOT)/docs @@ -75,6 +74,8 @@ SOURCE_HBMCTRL_DIR ?= $(ROOT)/hw/occamy/hbm_xbar_ctrl SOURCE_QUADCTRL_DIR ?= $(ROOT)/hw/occamy/quadrant_s1_ctrl SNITCH_TEST_DIR ?= $(SNITCH_ROOT)/target/common/test TARGET_TEST_DIR ?= $(ROOT)/target/sim/test +SW_DIR = $(ROOT)/target/sim/sw +PLATFORM_HEADERS_DIR = $(SW_DIR)/shared/platform CVA6_TRACE = trace_hart_0.log # If the configuration file is overriden on the command-line (through @@ -423,49 +424,7 @@ $(ADDRMAP_PDF): $(ADDRMAP_TEX) | $(TARGET_ADDRMAP_DIR) # Software # ############ -IDMAROOT = $(shell $(BENDER) path idma) - -PLATFORM_HEADERS = $(PLATFORM_HEADERS_DIR)/occamy_cfg.h -PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/occamy_base_addr.h -PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/clint.h -PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/occamy_soc_ctrl.h -PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/snitch_cluster_peripheral.h -PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/snitch_quad_peripheral.h -PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/snitch_hbm_xbar_peripheral.h -PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/idma.h - -.PHONY: sw clean-headers clean-sw - -sw: $(PLATFORM_HEADERS) - $(MAKE) -C sw/ all - -clean-headers: - rm -f $(PLATFORM_HEADERS) - -clean-sw: clean-headers - $(MAKE) -C sw/ clean - -# REGGEN headers -$(PLATFORM_HEADERS_DIR)/clint.h: $(TARGET_CLINT_DIR)/clint.hjson - $(call reggen_generate_header,$@,$<) -$(PLATFORM_HEADERS_DIR)/occamy_soc_ctrl.h: $(TARGET_SOCCTRL_DIR)/occamy_soc_reg.hjson - $(call reggen_generate_header,$@,$<) -$(PLATFORM_HEADERS_DIR)/snitch_cluster_peripheral.h: $(SNITCH_ROOT)/hw/snitch_cluster/src/snitch_cluster_peripheral/snitch_cluster_peripheral_reg.hjson - $(call reggen_generate_header,$@,$<) -$(PLATFORM_HEADERS_DIR)/snitch_quad_peripheral.h: $(TARGET_QUADCTRL_DIR)/occamy_quadrant_s1_reg.hjson - $(call reggen_generate_header,$@,$<) -$(PLATFORM_HEADERS_DIR)/snitch_hbm_xbar_peripheral.h: $(TARGET_HBMCTRL_DIR)/occamy_hbm_xbar_reg.hjson - $(call reggen_generate_header,$@,$<) -$(PLATFORM_HEADERS_DIR)/idma.h: $(IDMAROOT)/target/rtl/idma_reg64_1d.hjson - $(call reggen_generate_header,$@,$<) - -# OCCAMYGEN headers -$(PLATFORM_HEADERS_DIR)/occamy_cfg.h: $(PLATFORM_HEADERS_DIR)/occamy_cfg.h.tpl $(CFG) - @echo "[OCCAMYGEN] Generating $@" - @$(OCCAMYGEN) -c $(CFG) --outdir $(PLATFORM_HEADERS_DIR) --cheader $< -$(PLATFORM_HEADERS_DIR)/occamy_base_addr.h: $(CFG) - @echo "[OCCAMYGEN] Generating $@" - @$(OCCAMYGEN) -c $(CFG) --outdir $(PLATFORM_HEADERS_DIR) -D $@ +include sw.mk ########## # Traces # diff --git a/target/sim/sw.mk b/target/sim/sw.mk new file mode 100644 index 000000000..da96aa955 --- /dev/null +++ b/target/sim/sw.mk @@ -0,0 +1,76 @@ +# Copyright 2024 ETH Zurich and University of Bologna. +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 +# +# Luca Colagrande + +################### +# General targets # +################### + +.PHONY: sw clean-sw + +all: sw +clean: clean-sw + +#################### +# Platform headers # +#################### + +IDMAROOT = $(shell $(BENDER) path idma) + +PLATFORM_HEADERS = $(PLATFORM_HEADERS_DIR)/occamy_cfg.h +PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/occamy_base_addr.h +PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/clint.h +PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/occamy_soc_ctrl.h +PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/snitch_cluster_peripheral.h +PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/snitch_quad_peripheral.h +PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/snitch_hbm_xbar_peripheral.h +PLATFORM_HEADERS += $(PLATFORM_HEADERS_DIR)/idma.h + +# REGGEN headers +$(PLATFORM_HEADERS_DIR)/clint.h: $(TARGET_CLINT_DIR)/clint.hjson + $(call reggen_generate_header,$@,$<) +$(PLATFORM_HEADERS_DIR)/occamy_soc_ctrl.h: $(TARGET_SOCCTRL_DIR)/occamy_soc_reg.hjson + $(call reggen_generate_header,$@,$<) +$(PLATFORM_HEADERS_DIR)/snitch_cluster_peripheral.h: $(SNITCH_ROOT)/hw/snitch_cluster/src/snitch_cluster_peripheral/snitch_cluster_peripheral_reg.hjson + $(call reggen_generate_header,$@,$<) +$(PLATFORM_HEADERS_DIR)/snitch_quad_peripheral.h: $(TARGET_QUADCTRL_DIR)/occamy_quadrant_s1_reg.hjson + $(call reggen_generate_header,$@,$<) +$(PLATFORM_HEADERS_DIR)/snitch_hbm_xbar_peripheral.h: $(TARGET_HBMCTRL_DIR)/occamy_hbm_xbar_reg.hjson + $(call reggen_generate_header,$@,$<) +$(PLATFORM_HEADERS_DIR)/idma.h: $(IDMAROOT)/target/rtl/idma_reg64_1d.hjson + $(call reggen_generate_header,$@,$<) + +# OCCAMYGEN headers +$(PLATFORM_HEADERS_DIR)/occamy_cfg.h: $(PLATFORM_HEADERS_DIR)/occamy_cfg.h.tpl $(CFG) + @echo "[OCCAMYGEN] Generating $@" + @$(OCCAMYGEN) -c $(CFG) --outdir $(PLATFORM_HEADERS_DIR) --cheader $< +$(PLATFORM_HEADERS_DIR)/occamy_base_addr.h: $(CFG) + @echo "[OCCAMYGEN] Generating $@" + @$(OCCAMYGEN) -c $(CFG) --outdir $(PLATFORM_HEADERS_DIR) -D $@ + +.PHONY: clean-headers +clean-sw: clean-headers +clean-headers: + rm -f $(PLATFORM_HEADERS) + +################## +# Subdirectories # +################## + +include $(SNITCH_ROOT)/target/snitch_cluster/sw/toolchain.mk +include sw/host/toolchain.mk +include sw/device/runtime/runtime.mk + +HOST_APPS = sw/host/apps/hello_world +HOST_APPS += sw/host/apps/test_sys_dma +HOST_APPS += sw/host/apps/offload + +DEVICE_APPS = sw/device/apps/blas/axpy +DEVICE_APPS += sw/device/apps/blas/gemm + +# Include Makefile from each app subdirectory +$(foreach app,$(HOST_APPS) $(DEVICE_APPS), \ + $(eval include $(app)/app.mk) \ +) diff --git a/target/sim/sw/Makefile b/target/sim/sw/Makefile deleted file mode 100644 index 855e32b8c..000000000 --- a/target/sim/sw/Makefile +++ /dev/null @@ -1,16 +0,0 @@ -# Copyright 2023 ETH Zurich and University of Bologna. -# Licensed under the Apache License, Version 2.0, see LICENSE for details. -# SPDX-License-Identifier: Apache-2.0 -# -# Luca Colagrande - -.PHONY: all clean - -all: - $(MAKE) -C host TARGET=partial-build - $(MAKE) -C device TARGET=sw - $(MAKE) -C host TARGET=finalize-build - -clean: - $(MAKE) -C device TARGET=clean-sw - $(MAKE) -C host TARGET=clean diff --git a/target/sim/sw/device/Makefile b/target/sim/sw/device/Makefile deleted file mode 100644 index 21f2aab7b..000000000 --- a/target/sim/sw/device/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright 2023 ETH Zurich and University of Bologna. -# Licensed under the Apache License, Version 2.0, see LICENSE for details. -# SPDX-License-Identifier: Apache-2.0 -# -# Luca Colagrande - -# Add user applications to APPS variable -APPS = blas/axpy -APPS += blas/gemm - -TARGET ?= sw - -APP_SUBDIRS = $(addprefix apps/,$(APPS)) - -SUBDIRS = runtime -SUBDIRS += $(APP_SUBDIRS) - -SUBMK_FLAGS = SNRT_TARGET_DIR=$(abspath runtime) - -.PHONY: sw $(SUBDIRS) - -sw: $(SUBDIRS) - -$(SUBDIRS): - $(SUBMK_FLAGS) $(MAKE) -C $@ $(TARGET) - -# Explicit dependency of apps on runtime -$(APP_SUBDIRS): runtime diff --git a/target/sim/sw/device/apps/blas/axpy/Makefile b/target/sim/sw/device/apps/blas/axpy/app.mk similarity index 67% rename from target/sim/sw/device/apps/blas/axpy/Makefile rename to target/sim/sw/device/apps/blas/axpy/app.mk index 92ca4186b..705064952 100644 --- a/target/sim/sw/device/apps/blas/axpy/Makefile +++ b/target/sim/sw/device/apps/blas/axpy/app.mk @@ -4,12 +4,10 @@ # # Luca Colagrande -include $(abspath ../../common.mk) - APP := axpy -$(APP)_BUILD_DIR ?= $(SNITCH_ROOT)/target/snitch_cluster/sw/apps/blas/$(APP)/build +$(APP)_BUILD_DIR ?= $(SW_DIR)/device/apps/blas/$(APP)/build SRC_DIR := $(SNITCH_ROOT)/sw/blas/$(APP)/src SRCS := $(SRC_DIR)/main.c include $(SNITCH_ROOT)/sw/apps/common.mk -include $(SNITCH_ROOT)/target/snitch_cluster/sw/apps/common.mk +include $(SW_DIR)/device/apps/common.mk diff --git a/target/sim/sw/device/apps/blas/gemm/Makefile b/target/sim/sw/device/apps/blas/gemm/app.mk similarity index 69% rename from target/sim/sw/device/apps/blas/gemm/Makefile rename to target/sim/sw/device/apps/blas/gemm/app.mk index e93b6f345..eaeb21a73 100644 --- a/target/sim/sw/device/apps/blas/gemm/Makefile +++ b/target/sim/sw/device/apps/blas/gemm/app.mk @@ -4,14 +4,12 @@ # # Luca Colagrande -include $(abspath ../../common.mk) - APP := gemm -$(APP)_BUILD_DIR ?= $(SNITCH_ROOT)/target/snitch_cluster/sw/apps/blas/$(APP)/build +$(APP)_BUILD_DIR ?= $(SW_DIR)/device/apps/blas/$(APP)/build SRC_DIR := $(SNITCH_ROOT)/sw/blas/$(APP)/src SRCS := $(SRC_DIR)/main.c $(APP)_INCDIRS := $(SNITCH_ROOT)/sw/blas include $(SNITCH_ROOT)/sw/apps/common.mk -include $(SNITCH_ROOT)/target/snitch_cluster/sw/apps/common.mk +include $(SW_DIR)/device/apps/common.mk diff --git a/target/sim/sw/device/apps/common.mk b/target/sim/sw/device/apps/common.mk index 761f6e058..92cecb371 100644 --- a/target/sim/sw/device/apps/common.mk +++ b/target/sim/sw/device/apps/common.mk @@ -4,32 +4,26 @@ # # Luca Colagrande -# Usage of absolute paths is required to externally include -# this Makefile from multiple different locations -MK_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) -ROOT = $(abspath $(MK_DIR)/../../../../../) +$(APP)_RISCV_CFLAGS += -I$(SW_DIR)/shared/runtime +MEMORY_LD = $(SW_DIR)/device/apps/memory.ld +ORIGIN_LD = $(SW_DIR)/host/apps/offload/build/origin.ld +$(APP)_RISCV_LDFLAGS += -L$(dir $(ORIGIN_LD)) +LD_DEPS = $(ORIGIN_LD) -SNITCH_ROOT = $(shell bender path snitch_cluster) -include $(SNITCH_ROOT)/target/snitch_cluster/sw/toolchain.mk +include $(SNITCH_ROOT)/target/snitch_cluster/sw/apps/common.mk -SNRT_DIR = $(SNITCH_ROOT)/sw/snRuntime -SW_DIR = $(ROOT)/target/sim/sw -SNRT_BUILDDIR = $(SW_DIR)/device/runtime/build -# Path relative to the app including this Makefile -BUILDDIR = $(abspath build) +RISCV_OBJCOPY_FLAGS = -O binary +RISCV_OBJCOPY_FLAGS += --remove-section=.comment +RISCV_OBJCOPY_FLAGS += --remove-section=.riscv.attributes +RISCV_OBJCOPY_FLAGS += --remove-section=.debug_info +RISCV_OBJCOPY_FLAGS += --remove-section=.debug_abbrev +RISCV_OBJCOPY_FLAGS += --remove-section=.debug_line +RISCV_OBJCOPY_FLAGS += --remove-section=.debug_str +RISCV_OBJCOPY_FLAGS += --remove-section=.debug_aranges -RISCV_CFLAGS += -I$(SNRT_DIR)/src -RISCV_CFLAGS += -I$(SNRT_DIR)/api -RISCV_CFLAGS += -I$(SNRT_DIR)/src/omp -RISCV_CFLAGS += -I$(SNRT_DIR)/api/omp -RISCV_CFLAGS += -I$(SNRT_DIR)/vendor/riscv-opcodes -RISCV_CFLAGS += -I$(SW_DIR)/device/runtime/src -RISCV_CFLAGS += -I$(SW_DIR)/shared/platform/generated -RISCV_CFLAGS += -I$(SW_DIR)/shared/platform -RISCV_CFLAGS += -I$(SW_DIR)/shared/runtime +BIN = $(abspath $(addprefix $($(APP)_BUILD_DIR)/,$(addsuffix .bin,$(APP)))) -# Linker paths -MEMORY_LD = $(SW_DIR)/device/apps/memory.ld -$(info memory.ld path: $(MEMORY_LD)) -ORIGIN_LD = $(BUILDDIR)/origin.ld -RISCV_LDFLAGS += -L$(dir $(ORIGIN_LD)) +$(BIN): $(ELF) | $($(APP)_BUILD_DIR) + $(RISCV_OBJCOPY) $(RISCV_OBJCOPY_FLAGS) $< $@ + +$(APP): $(BIN) diff --git a/target/sim/sw/device/runtime/Makefile b/target/sim/sw/device/runtime/Makefile deleted file mode 100644 index c6940c4ca..000000000 --- a/target/sim/sw/device/runtime/Makefile +++ /dev/null @@ -1,75 +0,0 @@ -# Copyright 2023 ETH Zurich and University of Bologna. -# Licensed under the Apache License, Version 2.0, see LICENSE for details. -# SPDX-License-Identifier: Apache-2.0 -# -# Luca Colagrande - -include ../toolchain.mk - -################### -# Build variables # -################### - -# Directories -SNITCH_ROOT = $(shell bender path snitch_cluster) -BUILDDIR = $(abspath build/) -SRC_DIR = $(abspath src/) -SW_DIR = $(abspath ../../) -SNRT_DIR = $(SNITCH_ROOT)/sw/snRuntime - -# Dependencies -INCDIRS += $(SNRT_DIR)/src -INCDIRS += $(SNRT_DIR)/api -INCDIRS += $(SNRT_DIR)/vendor/riscv-opcodes -INCDIRS += $(SW_DIR)/shared/platform -INCDIRS += $(SW_DIR)/shared/platform/generated -INCDIRS += $(SW_DIR)/shared/runtime - -RISCV_CFLAGS += $(addprefix -I,$(INCDIRS)) - -# Sources -SRCS += $(SRC_DIR)/occamy_start.S -SRCS += $(SRC_DIR)/snrt.c - -########### -# Outputs # -########### - -OBJS = $(addprefix $(BUILDDIR)/,$(addsuffix .o,$(basename $(notdir $(SRCS))))) -DEPS = $(addprefix $(BUILDDIR)/,$(addsuffix .d,$(basename $(notdir $(SRCS))))) -LIB = $(BUILDDIR)/libsnRuntime.a -DUMP = $(BUILDDIR)/libsnRuntime.dump -ALL_OUTPUTS = $(DEPS) $(LIB) $(DUMP) - -######### -# Rules # -######### - -.PHONY: sw -sw: $(ALL_OUTPUTS) - -.PHONY: clean-sw -clean-sw: - rm -rf $(BUILDDIR) - -$(BUILDDIR): - mkdir -p $@ - -$(BUILDDIR)/%.o: $(SRC_DIR)/%.S | $(BUILDDIR) - $(RISCV_CC) $(RISCV_CFLAGS) -c $< -o $@ - -$(BUILDDIR)/%.o: $(SRC_DIR)/%.c | $(BUILDDIR) - $(RISCV_CC) $(RISCV_CFLAGS) -c $< -o $@ - -$(BUILDDIR)/%.d: $(SRC_DIR)/%.c | $(BUILDDIR) - $(RISCV_CC) $(RISCV_CFLAGS) -MM -MT '$(@:.d=.o)' $< > $@ - -$(LIB): $(OBJS) | $(BUILDDIR) - $(RISCV_AR) $(RISCV_ARFLAGS) $@ $^ - -$(DUMP): $(LIB) | $(BUILDDIR) - $(RISCV_OBJDUMP) -D $< > $@ - -ifneq ($(MAKECMDGOALS),clean-sw) --include $(DEPS) -endif diff --git a/target/sim/sw/device/runtime/runtime.mk b/target/sim/sw/device/runtime/runtime.mk new file mode 100644 index 000000000..cc00ce058 --- /dev/null +++ b/target/sim/sw/device/runtime/runtime.mk @@ -0,0 +1,12 @@ +# Copyright 2023 ETH Zurich and University of Bologna. +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 +# +# Luca Colagrande + +SNRT_TARGET_DIR = $(SW_DIR)/device/runtime +SNRT_HAL_HDRS_DIR = $(PLATFORM_HEADERS_DIR) +SNRT_HAL_HDRS = $(PLATFORM_HEADERS) +SNRT_INCDIRS = $(SW_DIR)/shared/runtime + +include $(SNITCH_ROOT)/target/snitch_cluster/sw/runtime/runtime.mk diff --git a/target/sim/sw/device/runtime/src/occamy_start.S b/target/sim/sw/device/runtime/src/snrt.S similarity index 100% rename from target/sim/sw/device/runtime/src/occamy_start.S rename to target/sim/sw/device/runtime/src/snrt.S diff --git a/target/sim/sw/host/Makefile b/target/sim/sw/host/Makefile deleted file mode 100644 index 6db859961..000000000 --- a/target/sim/sw/host/Makefile +++ /dev/null @@ -1,21 +0,0 @@ -# Copyright 2023 ETH Zurich and University of Bologna. -# Licensed under the Apache License, Version 2.0, see LICENSE for details. -# SPDX-License-Identifier: Apache-2.0 -# -# Luca Colagrande - -# Add user applications to APPS variable -APPS = hello_world -APPS += test_sys_dma -APPS += offload - -TARGET ?= all - -SUBDIRS = $(addprefix apps/,$(APPS)) - -.PHONY: all $(SUBDIRS) - -all: $(SUBDIRS) - -$(SUBDIRS): - $(MAKE) -C $@ $(TARGET) diff --git a/target/sim/sw/host/apps/common.mk b/target/sim/sw/host/apps/common.mk index 5c80bdf3b..628344954 100644 --- a/target/sim/sw/host/apps/common.mk +++ b/target/sim/sw/host/apps/common.mk @@ -4,102 +4,107 @@ # # Luca Colagrande -###################### -# Invocation options # -###################### - -DEBUG ?= OFF # ON to turn on debugging symbols - ################### # Build variables # ################### -# Usage of absolute paths is required to externally include this Makefile -MK_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST)))) - -include $(MK_DIR)/../toolchain.mk - -# Directories -BUILDDIR = $(abspath build) -HOST_DIR = $(abspath ../../) -RUNTIME_DIR = $(abspath $(HOST_DIR)/runtime) -DEVICE_DIR = $(abspath $(HOST_DIR)/../device) - -# Dependencies -INCDIRS += $(RUNTIME_DIR) -INCDIRS += $(HOST_DIR)/../shared/platform/generated -INCDIRS += $(HOST_DIR)/../shared/platform -INCDIRS += $(HOST_DIR)/../shared/runtime -SRCS += $(RUNTIME_DIR)/start.S - -# Device binary -ifeq ($(INCL_DEVICE_BINARY),true) -DEVICE_BUILDDIR = $(DEVICE_DIR)/apps/$(APP)/build -DEVICE_BINARY = $(DEVICE_BUILDDIR)/$(APP).bin -ORIGIN_LD = $(DEVICE_BUILDDIR)/origin.ld -FINAL_CFLAGS = -DDEVICEBIN=\"$(DEVICE_BINARY)\" -endif +HOST_DIR = $(SW_DIR)/host +BUILDDIR = $(HOST_DIR)/apps/$(APP)/build +HOST_RUNTIME_DIR = $(HOST_DIR)/runtime + +HOST_INCDIRS = $(HOST_RUNTIME_DIR) +HOST_INCDIRS += $(PLATFORM_HEADERS_DIR) +HOST_INCDIRS += $(SW_DIR)/shared/runtime +SRCS += $(HOST_RUNTIME_DIR)/start.S +HEADERS += $(PLATFORM_HEADERS) + +$(APP)_HOST_RISCV_CFLAGS += $(HOST_RISCV_CFLAGS) +$(APP)_HOST_RISCV_CFLAGS += $(addprefix -I,$(HOST_INCDIRS)) + +HOST_LINKER_SCRIPT = $(HOST_RUNTIME_DIR)/host.ld + +$(APP)_HOST_RISCV_LDFLAGS += $(HOST_RISCV_LDFLAGS) +$(APP)_HOST_RISCV_LDFLAGS += -T$(HOST_LINKER_SCRIPT) + +LD_DEPS = $(HOST_LINKER_SCRIPT) ########### # Outputs # ########### -PARTIAL_ELF = $(abspath $(BUILDDIR)/$(APP).part.elf) -ELF = $(abspath $(BUILDDIR)/$(APP).elf) -DEP = $(abspath $(BUILDDIR)/$(APP).d) -PARTIAL_DUMP = $(abspath $(BUILDDIR)/$(APP).part.dump) -DUMP = $(abspath $(BUILDDIR)/$(APP).dump) -DWARF = $(abspath $(BUILDDIR)/$(APP).dwarf) -PARTIAL_OUTPUTS = $(PARTIAL_ELF) $(PARTIAL_DUMP) $(ORIGIN_LD) -FINAL_OUTPUTS = $(ELF) $(DUMP) $(DWARF) +ELF = $(BUILDDIR)/$(APP).elf +DEP = $(BUILDDIR)/$(APP).d +DUMP = $(BUILDDIR)/$(APP).dump +ORIGIN_LD = $(BUILDDIR)/origin.ld +HETEROGENEOUS_ELFS = $(addsuffix .elf, $(addprefix $(BUILDDIR)/$(APP)-, $(notdir $($(APP)_DEVICE_APPS)))) +HETEROGENEOUS_DUMPS = $(addsuffix .dump, $(addprefix $(BUILDDIR)/$(APP)-, $(notdir $($(APP)_DEVICE_APPS)))) +ALL_OUTPUTS = $(ELF) $(HETEROGENEOUS_ELFS) $(ORIGIN_LD) + +ifeq ($(DEBUG), ON) +ALL_OUTPUTS += $(DUMP) $(HETEROGENEOUS_DUMPS) +endif ######### # Rules # ######### -.PHONY: partial-build -partial-build: $(PARTIAL_OUTPUTS) +.PHONY: $(APP) clean-$(APP) + +sw: $(APP) +clean-sw: clean-$(APP) -.PHONY: finalize-build -finalize-build: $(FINAL_OUTPUTS) +$(APP): $(ALL_OUTPUTS) -.PHONY: clean -clean: +clean-$(APP): BUILDDIR := $(BUILDDIR) +clean-$(APP): rm -rf $(BUILDDIR) - rm -f $(OFFSET_LD) $(BUILDDIR): mkdir -p $@ -$(DEVICE_BUILDDIR): - mkdir -p $@ - -$(DEP): $(SRCS) | $(BUILDDIR) - $(RISCV_CC) $(RISCV_CFLAGS) -MM -MT '$(PARTIAL_ELF)' $< > $@ - $(RISCV_CC) $(RISCV_CFLAGS) -MM -MT '$(ELF)' $< >> $@ +$(DEP): ELF := $(ELF) +$(ELF): SRCS := $(SRCS) +# Guarantee that variables used in rule recipes (thus subject to deferred expansion) +# have unique values, despite depending on variables with the same name across +# applications, but which could have different values (e.g. the APP variable itself) +$(DEP) $(ELF): HOST_RISCV_CFLAGS := $($(APP)_HOST_RISCV_CFLAGS) +$(ELF): HOST_RISCV_LDFLAGS := $($(APP)_HOST_RISCV_LDFLAGS) -# Partially linked object -$(PARTIAL_ELF): $(DEP) $(LD_SRCS) | $(BUILDDIR) - $(RISCV_CC) $(RISCV_CFLAGS) $(RISCV_LDFLAGS) $(SRCS) -o $@ - -$(PARTIAL_DUMP): $(PARTIAL_ELF) | $(BUILDDIR) - $(RISCV_OBJDUMP) -D $< > $@ +$(DEP): $(SRCS) $(HEADERS) | $(BUILDDIR) + $(HOST_RISCV_CC) $(HOST_RISCV_CFLAGS) -MM -MT '$(ELF)' $< >> $@ # Device object relocation address -$(ORIGIN_LD): $(PARTIAL_ELF) | $(DEVICE_BUILDDIR) - @RELOC_ADDR=$$($(RISCV_OBJDUMP) -t $< | grep snitch_main | cut -c9-16); \ +$(ORIGIN_LD): $(ELF) | $(BUILDDIR) + @RELOC_ADDR=$$($(HOST_RISCV_OBJDUMP) -t $< | grep snitch_main | cut -c9-16); \ echo "Writing device object relocation address 0x$$RELOC_ADDR to $@"; \ echo "L3_ORIGIN = 0x$$RELOC_ADDR;" > $@ -$(ELF): $(DEP) $(LD_SRCS) $(DEVICE_BINARY) | $(BUILDDIR) - $(RISCV_CC) $(RISCV_CFLAGS) $(FINAL_CFLAGS) $(RISCV_LDFLAGS) $(SRCS) -o $@ +# $(ELF): $(DEP) $(LD_DEPS) $(DEVICE_BINARY) | $(BUILDDIR) +# $(HOST_RISCV_CC) $(HOST_RISCV_CFLAGS) $(FINAL_CFLAGS) $(HOST_RISCV_LDFLAGS) $(SRCS) -o $@ +$(ELF): $(DEP) $(LD_DEPS) | $(BUILDDIR) + $(HOST_RISCV_CC) $(HOST_RISCV_CFLAGS) $(HOST_RISCV_LDFLAGS) $(SRCS) -o $@ $(DUMP): $(ELF) | $(BUILDDIR) - $(RISCV_OBJDUMP) -D $< > $@ - -$(DWARF): $(ELF) | $(BUILDDIR) - $(RISCV_READELF) --debug-dump $< > $@ + $(HOST_RISCV_OBJDUMP) -D $< > $@ -ifneq ($(MAKECMDGOALS),clean) --include $(DEP) +# Generates a rule which looks somewhat like: +# +# $(BUILDDIR)/$(APP)-%.elf: $(DEVICE_APP)/build/%.bin $(DEP) $(LD_SRCS) | $(BUILDDIR) +# $(RISCV_CC) $(RISCV_CFLAGS) -DDEVICEBIN=\"$<\" $(RISCV_LDFLAGS) $(SRCS) -o $@ +# +# This approach is required cause you can't use multiple %-signs in a prerequisite +define elf_rule_template = + $$(BUILDDIR)/$$(APP)-$(notdir $(1)).elf: SRCS := $$(SRCS) + $$(BUILDDIR)/$$(APP)-$(notdir $(1)).elf: HOST_RISCV_CFLAGS := $$($$(APP)_HOST_RISCV_CFLAGS) + $$(BUILDDIR)/$$(APP)-$(notdir $(1)).elf: HOST_RISCV_LDFLAGS := $$($$(APP)_HOST_RISCV_LDFLAGS) + $$(BUILDDIR)/$$(APP)-$(notdir $(1)).elf: $(1)/build/$(notdir $(1)).bin $$(ELF) $$(LD_DEPS) | $$(BUILDDIR) + $$(HOST_RISCV_CC) $$(HOST_RISCV_CFLAGS) -DDEVICEBIN=\"$$<\" $$(HOST_RISCV_LDFLAGS) $$(SRCS) -o $$@ +endef +$(foreach f,$($(APP)_DEVICE_APPS),$(eval $(call elf_rule_template,$(f)))) + +$(HETEROGENEOUS_DUMPS): $(BUILDDIR)/$(APP)-%.dump: $(BUILDDIR)/$(APP)-%.elf | $(BUILDDIR) + $(HOST_RISCV_OBJDUMP) -D $< > $@ + +ifneq ($(filter-out clean%,$(MAKECMDGOALS)),) + -include $(DEP) endif diff --git a/target/sim/sw/host/apps/hello_world/Makefile b/target/sim/sw/host/apps/hello_world/app.mk similarity index 62% rename from target/sim/sw/host/apps/hello_world/Makefile rename to target/sim/sw/host/apps/hello_world/app.mk index a83b5880b..aad9b8bb3 100644 --- a/target/sim/sw/host/apps/hello_world/Makefile +++ b/target/sim/sw/host/apps/hello_world/app.mk @@ -4,7 +4,8 @@ # # Luca Colagrande -APP = hello_world -SRCS = src/hello_world.c +APP = hello_world +SRC_DIR = $(SW_DIR)/host/apps/$(APP)/src +SRCS = $(SRC_DIR)/main.c -include ../common.mk +include $(SW_DIR)/host/apps/common.mk diff --git a/target/sim/sw/host/apps/hello_world/src/hello_world.c b/target/sim/sw/host/apps/hello_world/src/main.c similarity index 100% rename from target/sim/sw/host/apps/hello_world/src/hello_world.c rename to target/sim/sw/host/apps/hello_world/src/main.c diff --git a/target/sim/sw/host/apps/offload/app.mk b/target/sim/sw/host/apps/offload/app.mk new file mode 100644 index 000000000..b737f5b31 --- /dev/null +++ b/target/sim/sw/host/apps/offload/app.mk @@ -0,0 +1,13 @@ +# Copyright 2023 ETH Zurich and University of Bologna. +# Licensed under the Apache License, Version 2.0, see LICENSE for details. +# SPDX-License-Identifier: Apache-2.0 +# +# Luca Colagrande + +APP = offload +SRC_DIR = $(SW_DIR)/host/apps/$(APP)/src +SRCS = $(SRC_DIR)/main.c +$(APP)_DEVICE_APPS = $(SW_DIR)/device/apps/blas/axpy +$(APP)_DEVICE_APPS += $(SW_DIR)/device/apps/blas/gemm + +include $(SW_DIR)/host/apps/common.mk diff --git a/target/sim/sw/host/apps/offload/src/offload.c b/target/sim/sw/host/apps/offload/src/main.c similarity index 100% rename from target/sim/sw/host/apps/offload/src/offload.c rename to target/sim/sw/host/apps/offload/src/main.c diff --git a/target/sim/sw/host/apps/test_sys_dma/Makefile b/target/sim/sw/host/apps/test_sys_dma/Makefile deleted file mode 100644 index 206c287da..000000000 --- a/target/sim/sw/host/apps/test_sys_dma/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -# Copyright 2024 ETH Zurich and University of Bologna. -# Licensed under the Apache License, Version 2.0, see LICENSE for details. -# SPDX-License-Identifier: Apache-2.0 -# -# Luca Colagrande - -APP = test_sys_dma -SRCS = src/test_sys_dma.c - -include ../common.mk diff --git a/target/sim/sw/device/toolchain.mk b/target/sim/sw/host/apps/test_sys_dma/app.mk similarity index 62% rename from target/sim/sw/device/toolchain.mk rename to target/sim/sw/host/apps/test_sys_dma/app.mk index 9e83aad99..e00c79d1c 100644 --- a/target/sim/sw/device/toolchain.mk +++ b/target/sim/sw/host/apps/test_sys_dma/app.mk @@ -4,6 +4,8 @@ # # Luca Colagrande -BENDER ?= bender -SNITCH_ROOT = $(shell $(BENDER) path snitch_cluster) -include $(SNITCH_ROOT)/target/snitch_cluster/sw/toolchain.mk \ No newline at end of file +APP = test_sys_dma +SRC_DIR = $(SW_DIR)/host/apps/$(APP)/src +SRCS = $(SRC_DIR)/main.c + +include $(SW_DIR)/host/apps/common.mk diff --git a/target/sim/sw/host/apps/test_sys_dma/src/test_sys_dma.c b/target/sim/sw/host/apps/test_sys_dma/src/main.c similarity index 100% rename from target/sim/sw/host/apps/test_sys_dma/src/test_sys_dma.c rename to target/sim/sw/host/apps/test_sys_dma/src/main.c diff --git a/target/sim/sw/host/toolchain.mk b/target/sim/sw/host/toolchain.mk index 73febae7f..321168255 100644 --- a/target/sim/sw/host/toolchain.mk +++ b/target/sim/sw/host/toolchain.mk @@ -4,45 +4,29 @@ # # Luca Colagrande -###################### -# Invocation options # -###################### - -DEBUG ?= OFF # ON to turn on debugging symbols - -################### -# Build variables # -################### - # Compiler toolchain -RISCV_GCC_BINROOT ?= $(dir $(shell which riscv32-unknown-elf-gcc)) -RISCV_CC = $(RISCV_GCC_BINROOT)/riscv64-unknown-elf-gcc -RISCV_OBJCOPY = $(RISCV_GCC_BINROOT)/riscv64-unknown-elf-objcopy -RISCV_OBJDUMP = $(RISCV_GCC_BINROOT)/riscv64-unknown-elf-objdump -RISCV_READELF = $(RISCV_GCC_BINROOT)/riscv64-unknown-elf-readelf +HOST_RISCV_GCC_BINROOT ?= $(dir $(shell which riscv32-unknown-elf-gcc)) +HOST_RISCV_CC = $(HOST_RISCV_GCC_BINROOT)/riscv64-unknown-elf-gcc +HOST_RISCV_OBJCOPY = $(HOST_RISCV_GCC_BINROOT)/riscv64-unknown-elf-objcopy +HOST_RISCV_OBJDUMP = $(HOST_RISCV_GCC_BINROOT)/riscv64-unknown-elf-objdump +HOST_RISCV_READELF = $(HOST_RISCV_GCC_BINROOT)/riscv64-unknown-elf-readelf # Compiler flags -RISCV_CFLAGS += $(addprefix -I,$(INCDIRS)) -RISCV_CFLAGS += -march=rv64imafdc -RISCV_CFLAGS += -mabi=lp64d -RISCV_CFLAGS += -mcmodel=medany -RISCV_CFLAGS += -ffast-math -RISCV_CFLAGS += -fno-builtin-printf -RISCV_CFLAGS += -fno-common -RISCV_CFLAGS += -O3 -RISCV_CFLAGS += -ffunction-sections -RISCV_CFLAGS += -Wextra -RISCV_CFLAGS += -Werror +HOST_RISCV_CFLAGS = -march=rv64imafdc +HOST_RISCV_CFLAGS += -mabi=lp64d +HOST_RISCV_CFLAGS += -mcmodel=medany +HOST_RISCV_CFLAGS += -ffast-math +HOST_RISCV_CFLAGS += -fno-builtin-printf +HOST_RISCV_CFLAGS += -fno-common +HOST_RISCV_CFLAGS += -O3 +HOST_RISCV_CFLAGS += -ffunction-sections +HOST_RISCV_CFLAGS += -Wextra +HOST_RISCV_CFLAGS += -Werror ifeq ($(DEBUG), ON) -RISCV_CFLAGS += -g +HOST_RISCV_CFLAGS += -g endif -# Linking sources -LINKER_SCRIPT = $(abspath $(HOST_DIR)/runtime/host.ld) -LD_SRCS = $(LINKER_SCRIPT) - # Linker flags -RISCV_LDFLAGS += -nostartfiles -RISCV_LDFLAGS += -lm -RISCV_LDFLAGS += -lgcc -RISCV_LDFLAGS += -T$(LINKER_SCRIPT) +HOST_RISCV_LDFLAGS += -nostartfiles +HOST_RISCV_LDFLAGS += -lm +HOST_RISCV_LDFLAGS += -lgcc