Skip to content

Commit

Permalink
kernel_headers: add SDCC base makefile to Zeal 8-bit OS programs comp…
Browse files Browse the repository at this point in the history
…ilation

Programs that are compiled with SDCC and meant to be executed on Zeal 8-bit OS
can now include the new `base_sdcc.mk` makefile. The SDCC example has been
updated to reflect this new change.
  • Loading branch information
Zeal8bit committed Nov 13, 2024
1 parent 8b36519 commit d01ae65
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 71 deletions.
98 changes: 27 additions & 71 deletions kernel_headers/examples/sdcc/Makefile
Original file line number Diff line number Diff line change
@@ -1,83 +1,39 @@
SHELL := /bin/bash
# All the variables below are optional. Override their value by uncommenting the corresponding line.

ifeq '$(findstring ;,$(PATH))' ';'
detected_OS := Windows
else
detected_OS := $(shell uname 2>/dev/null || echo Unknown)
detected_OS := $(patsubst CYGWIN%,Cygwin,$(detected_OS))
detected_OS := $(patsubst MSYS%,MSYS,$(detected_OS))
detected_OS := $(patsubst MINGW%,MSYS,$(detected_OS))
endif

STAT_BYTES = stat
ifeq ($(detected_OS),Darwin)
STAT_BYTES += -f %z
# TODO: Support Windows?
else
STAT_BYTES += -c %s
endif
# Specify the shell to use for sub-commands.
# SHELL = /bin/bash

# Specify the files to compile and the name of the final binary
SRCS=main.c str.c
BIN=example.bin
# Specify the directory containing the source files.
# INPUT_DIR=src

# Directory where source files are and where the binaries will be put
INPUT_DIR=src
OUTPUT_DIR=bin
# Specify the build containing the compiled files.
# OUTPUT_DIR=bin

# Include directory containing Zeal 8-bit OS header files.
ifndef ZOS_PATH
$(error "Please define ZOS_PATH environment variable. It must point to Zeal 8-bit OS source code path.")
endif
ZOS_INCLUDE=$(ZOS_PATH)/kernel_headers/sdcc/include/
# Regarding the linking process, we will need to specify the path to the crt0 REL file.
# It contains the boot code for C programs as well as all the C functions performing syscalls.
CRT_REL=$(ZOS_PATH)/kernel_headers/sdcc/bin/zos_crt0.rel
# Specify the files in the src directory to compile and the name of the final binary.
# By default, all the C files inside `INPUT_DIR` are selected, the `INPUT_DIR` prefix must not be part of the files names.
# SRCS=$(notdir $(wildcard $(INPUT_DIR)/*.c))

# Specify the name of the output binary.
# BIN=output.bin

# Compiler, linker and flags related variables
CC=sdcc
# Specify Z80 as the target, compile without linking, and place all the code in TEXT section
# (_CODE must be replace).
CFLAGS=-mz80 -c --codeseg TEXT -I$(ZOS_INCLUDE)
LD=sdldz80
# Make sure the whole program is relocated at 0x4000 as request by Zeal 8-bit OS.
LDFLAGS=-n -mjwx -i -b _HEADER=0x4000 $(SDLD_FLAGS) -k $(ZOS_PATH)/kernel_headers/sdcc/lib -l z80
# Binary used to convert ihex to binary
OBJCOPY=objcopy
ifeq ($(detected_OS),Darwin)
OBJCOPY=gobjcopy
endif
# Specify the C compiler to use.
# ZOS_CC=sdcc

# Generate the intermediate Intel Hex binary name
BIN_HEX=$(patsubst %.bin,%.ihx,$(BIN))
# Generate the rel names for C source files. Only keep the file names, and add output dir prefix.
SRCS_OUT_DIR=$(addprefix $(OUTPUT_DIR)/,$(SRCS))
SRCS_REL=$(patsubst %.c,%.rel,$(SRCS_OUT_DIR))
# Specify the linker to use.
# ZOS_LD=sdldz80

# Specify additional flags to pass to the compiler.
# ZOS_CFLAGS=

.PHONY: all clean
# Specify additional flags to pass to the linker.
# ZOS_LDFLAGS=

all: clean $(OUTPUT_DIR) $(OUTPUT_DIR)/$(BIN_HEX) $(OUTPUT_DIR)/$(BIN)
@bash -c 'echo -e "\x1b[32;1mSuccess, binary generated: $(OUTPUT_DIR)/$(BIN)\x1b[0m"'
@echo "uartrcv $$($(STAT_BYTES) $(OUTPUT_DIR)/$(BIN)) $(BIN)"
# Specify the `objcopy` binary that performs the ihex to bin conversion.
# By default it uses `sdobjcopy` or `objcopy` depending on which one is installed.
# OBJCOPY=$(shell which sdobjcopy objcopy | head -1)

$(OUTPUT_DIR):
mkdir -p $(OUTPUT_DIR)

# Generate a REL file for each source file. In fact, SDCC doesn't support compiling multiple source file
# at once. We have to create the same directory structure in output dir too.
$(SRCS_REL): $(OUTPUT_DIR)/%.rel : $(INPUT_DIR)/%.c
@mkdir -p $(OUTPUT_DIR)/$(dir $*)
$(CC) $(CFLAGS) -o $(OUTPUT_DIR)/$(dir $*) $<

# Generate the final Intel HEX binary.
$(OUTPUT_DIR)/$(BIN_HEX): $(CRT_REL) $(SRCS_REL)
$(LD) $(LDFLAGS) $(OUTPUT_DIR)/$(BIN_HEX) $(CRT_REL) $(SRCS_REL)

# Convert the Intel HEX file to an actual binary.
$(OUTPUT_DIR)/$(BIN):
$(OBJCOPY) --input-target=ihex --output-target=binary $(OUTPUT_DIR)/$(BIN_HEX) $(OUTPUT_DIR)/$(BIN)
ifndef ZOS_PATH
$(error "Failure: ZOS_PATH variable not found. It must point to Zeal 8-bit OS path.")
endif

clean:
rm -fr bin/
include $(ZOS_PATH)/kernel_headers/sdcc/base_sdcc.mk
78 changes: 78 additions & 0 deletions kernel_headers/sdcc/base_sdcc.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#
# SPDX-FileCopyrightText: 2024 Zeal 8-bit Computer <[email protected]>
#
# SPDX-License-Identifier: Apache-2.0
#

# This file is means to be included by programs based on SDCC.
# This will ease writing a Makefile for a new project that is meant to be
# compiled for Zeal 8-bit OS.
# This file can included by adding this line to any Makefile:
# include $(ZOS_PATH)/kernel_headers/sdcc/base_sdcc.mk

SHELL ?= /bin/bash

# Directory where source files are and where the binaries will be put
INPUT_DIR ?= src
OUTPUT_DIR ?= bin

# Specify the files to compile and the name of the final binary
SRCS ?= $(notdir $(wildcard $(INPUT_DIR)/*.c))
BIN ?= output.bin

# Include directory containing Zeal 8-bit OS header files.
ifndef ZOS_PATH
$(error "Please define ZOS_PATH environment variable. It must point to Zeal 8-bit OS source code path.")
endif

ZOS_INCLUDE=$(ZOS_PATH)/kernel_headers/sdcc/include/
# Regarding the linking process, we will need to specify the path to the crt0 REL file.
# It contains the boot code for C programs as well as all the C functions performing syscalls.
CRT_REL=$(ZOS_PATH)/kernel_headers/sdcc/bin/zos_crt0.rel


# Compiler, linker and flags related variables
ZOS_CC ?= sdcc
ZOS_LD ?= sdldz80

# Specify Z80 as the target, compile without linking, and place all the code in TEXT section
# (_CODE must be replace).
CFLAGS = -mz80 -c --codeseg TEXT -I$(ZOS_INCLUDE) $(ZOS_CFLAGS)

# Make sure the whole program is relocated at 0x4000 as request by Zeal 8-bit OS.
LDFLAGS = -n -mjwx -i -b _HEADER=0x4000 -k $(ZOS_PATH)/kernel_headers/sdcc/lib -l z80 $(ZOS_LDFLAGS)

# Binary used to convert ihex to binary
OBJCOPY ?= $(shell which sdobjcopy objcopy gobjcopy | head -1)

# Generate the intermediate Intel Hex binary name
BIN_HEX=$(patsubst %.bin,%.ihx,$(BIN))
# Generate the rel names for C source files. Only keep the file names, and add output dir prefix.
SRCS_OUT_DIR=$(addprefix $(OUTPUT_DIR)/,$(SRCS))
SRCS_REL=$(patsubst %.c,%.rel,$(SRCS_OUT_DIR))


.PHONY: all clean

all: clean $(OUTPUT_DIR) $(OUTPUT_DIR)/$(BIN_HEX) $(OUTPUT_DIR)/$(BIN)
@bash -c 'echo -e "\x1b[32;1mSuccess, binary generated: $(OUTPUT_DIR)/$(BIN)\x1b[0m"'

$(OUTPUT_DIR):
mkdir -p $(OUTPUT_DIR)

# Generate a REL file for each source file. In fact, SDCC doesn't support compiling multiple source file
# at once. We have to create the same directory structure in output dir too.
$(SRCS_REL): $(OUTPUT_DIR)/%.rel : $(INPUT_DIR)/%.c
@mkdir -p $(OUTPUT_DIR)/$(dir $*)
$(ZOS_CC) $(CFLAGS) -o $(OUTPUT_DIR)/$(dir $*) $<

# Generate the final Intel HEX binary.
$(OUTPUT_DIR)/$(BIN_HEX): $(CRT_REL) $(SRCS_REL)
$(ZOS_LD) $(LDFLAGS) $(OUTPUT_DIR)/$(BIN_HEX) $(CRT_REL) $(SRCS_REL)

# Convert the Intel HEX file to an actual binary.
$(OUTPUT_DIR)/$(BIN):
$(OBJCOPY) --input-target=ihex --output-target=binary $(OUTPUT_DIR)/$(BIN_HEX) $(OUTPUT_DIR)/$(BIN)

clean:
rm -fr ./$(OUTPUT_DIR)

0 comments on commit d01ae65

Please sign in to comment.