# msxbuild.mk - Makefile helper to use with msx projects.
#
# note: this needs more work.
#


# Required tools paths
PATH_HEX2BIN     ?= /usr/bin
PATH_SDCC        ?= /usr/bin
PATH_MSXBUILD    ?= $(dir $(lastword $(MAKEFILE_LIST)))../..

include $(PATH_MSXBUILD)/lib/make/mb_base.mk
include $(PATH_MSXBUILD)/lib/make/mb_autoexec.mk
include $(PATH_MSXBUILD)/lib/make/mb_openmsx.mk
include $(PATH_MSXBUILD)/lib/make/mb_assert.mk

CMD_H2B              ?= $(PATH_HEX2BIN)/hex2bin
CMD_SDCC             ?= $(PATH_SDCC)/sdcc
CMD_SDASM            ?= $(PATH_SDCC)/sdasz80
CMD_SDAR             ?= $(PATH_SDCC)/sdar

# Define build flags
MB_SDCC_FLAG_CPU    ?= -mz80
MB_SDCC_FLAG_LD     ?= --nostdinc
MB_SDASM_FLAGS      ?= -g -l -c -o
MB_SDAR_FLAGS       ?= -rc
#$(MB_SDASM) -I$(MB_LIBASM_SRC)/include

define mb_compile_asm
	@echo === Compile module asm.
	$(CMD_SDASM) $(MB_SDASM_FLAGS) $(1) $(2)
endef

define mb_link_asm_lib
	@echo === Link module asm lib.
	$(CMD_SDAR) $(MB_SDAR_FLAGS) $(1) $(2)
endef

define mb_link_asm
	@echo === Link asm module at $(3)
	$(CMD_SDCC) $(MB_SDCC_FLAG_CPU) --no-std-crt0 --code-loc $(3) -o $(1) $(2)
endef
define mb_link_asm_0000
	$(call mb_link_asm,$(1),$(2),0x0000)
endef
define mb_link_asm_dos
	$(call mb_link_asm,$(1),$(2),0x0100)
endef
define mb_link_asm_1000
	$(call mb_link_asm,$(1),$(2),0x1000)
endef
define mb_link_asm_4000
	$(call mb_link_asm,$(1),$(2),0x4000)
endef
define mb_link_asm_8000
	$(call mb_link_asm,$(1),$(2),0x8000)
endef
define mb_link_asm_C000
	$(call mb_link_asm,$(1),$(2),0xC000)
endef
#-l$(LIBASM_LINK)

define mb_hex2com
	@echo === Convert to binary: $(notdir $(1)).com
	cd $(dir $(1)) && $(CMD_H2B) -e com $(notdir $(1))
	$(call mb_file_info,$(2))
	@echo === Done $(notdir $(2))
endef
define mb_hex2dat
	@echo === Convert to binary $(notdir $(1)).dat
	cd $(dir $(1)) && $(CMD_H2B) -e dat $(notdir $(1))
	$(call mb_file_info,$(2))
	@echo === Done $(notdir $(2))
endef