gbdk-2020 | GameBoy Development Kit |
| download: https://git.y1.nz/archives/gbdk.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit 55725d6810a3358d7d8254a83a7360ab9a53ff57 parent 1dfcae9201da133a300d8b983ffc9c70ea6234b1 Author: Toxa <untoxa@mail.ru> Date: Mon, 15 Apr 2024 21:27:09 +0300 SMS/GG/MSX Z80 implementation for the bcd.h functions Diffstat:
| A | gbdk-lib/examples/cross-platform/bcd/Makefile | 93 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| A | gbdk-lib/examples/cross-platform/bcd/Makefile.targets | 54 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| R | gbdk-lib/examples/gb/bcd/bcd.c -> gbdk-lib/examples/cross-platform/bcd/src/bcd.c | 0 | |
| D | gbdk-lib/examples/gb/bcd/Makefile | 29 | ----------------------------- |
| M | gbdk-lib/include/gbdk/bcd.h | 2 | +- |
| A | gbdk-lib/include/sms/bcd.h | 61 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | gbdk-lib/libc/asm/z80/Makefile | 3 | ++- |
| A | gbdk-lib/libc/asm/z80/bcd.s | 212 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
8 files changed, 423 insertions(+), 31 deletions(-)
diff --git a/gbdk-lib/examples/cross-platform/bcd/Makefile b/gbdk-lib/examples/cross-platform/bcd/Makefile @@ -0,0 +1,93 @@ +SHELL := /bin/bash + +# If you move this project you can change the directory +# to match your GBDK root directory (ex: GBDK_HOME = "C:/GBDK/" +ifndef GBDK_HOME + GBDK_HOME = ../../../ +endif + +LCC = $(GBDK_HOME)bin/lcc + +# Set platforms to build here, spaced separated. (These are in the separate Makefile.targets) +# They can also be built/cleaned individually: "make gg" and "make gg-clean" +# Possible are: gb gbc pocket megaduck sms gg +TARGETS=gb pocket megaduck sms gg + +# Configure platform specific LCC flags here: +LCCFLAGS_gb = -Wl-yt0x1B # Set an MBC for banking (1B-ROM+MBC5+RAM+BATT) +LCCFLAGS_pocket = -Wl-yt0x1B # Usually the same as required for .gb +LCCFLAGS_duck = -Wl-yt0x1B # Usually the same as required for .gb +LCCFLAGS_gbc = -Wl-yt0x1B -Wm-yc # Same as .gb with: -Wm-yc (gb & gbc) or Wm-yC (gbc exclusive) +LCCFLAGS_sms = +LCCFLAGS_gg = +LCCFLAGS_nes = + +LCCFLAGS += $(LCCFLAGS_$(EXT)) # This adds the current platform specific LCC Flags + +LCCFLAGS += -Wl-j -Wm-yoA -Wm-ya4 -autobank -Wb-ext=.rel -Wb-v # MBC + Autobanking related flags + +# GBDK_DEBUG = ON +ifdef GBDK_DEBUG + LCCFLAGS += -debug -v -Wl-u +endif + + +# You can set the name of the ROM file here +PROJECTNAME = bcd + +# EXT?=gb # Only sets extension to default (game boy .gb) if not populated +SRCDIR = src +OBJDIR = obj/$(EXT) +RESDIR = res +BINDIR = build/$(EXT) +MKDIRS = $(OBJDIR) $(BINDIR) # See bottom of Makefile for directory auto-creation + +BINS = $(OBJDIR)/$(PROJECTNAME).$(EXT) +CSOURCES = $(foreach dir,$(SRCDIR),$(notdir $(wildcard $(dir)/*.c))) $(foreach dir,$(RESDIR),$(notdir $(wildcard $(dir)/*.c))) +ASMSOURCES = $(foreach dir,$(SRCDIR),$(notdir $(wildcard $(dir)/*.s))) +OBJS = $(CSOURCES:%.c=$(OBJDIR)/%.o) $(ASMSOURCES:%.s=$(OBJDIR)/%.o) + +# Builds all targets sequentially +all: $(TARGETS) + +# Compile .c files in "src/" to .o object files +$(OBJDIR)/%.o: $(SRCDIR)/%.c + $(eval BOFLAG = $(shell echo "$<" | sed -n 's/.*\.bo\([0-9]\+\).*/\-Wf-bo\1/p')) +# RAM bank flag is not supported for the NES, the other platforms can use it +ifneq ($(strip $(PLAT)),nes) + $(eval BAFLAG = $(shell echo "$<" | sed -n 's/.*\.ba\([0-9]\+\).*/\-Wf-ba\1/p')) +endif + $(LCC) $(LCCFLAGS) $(CFLAGS) $(BOFLAG) $(BAFLAG) -c -o $@ $< + +# Compile .c files in "res/" to .o object files +$(OBJDIR)/%.o: $(RESDIR)/%.c + $(LCC) $(LCCFLAGS) $(CFLAGS) -c -o $@ $< + +# Compile .s assembly files in "src/" to .o object files +$(OBJDIR)/%.o: $(SRCDIR)/%.s + $(LCC) $(LCCFLAGS) $(CFLAGS) -c -o $@ $< + +# If needed, compile .c files in "src/" to .s assembly files +# (not required if .c is compiled directly to .o) +$(OBJDIR)/%.s: $(SRCDIR)/%.c + $(LCC) $(LCCFLAGS) $(CFLAGS) -S -o $@ $< + +# Link the compiled object files into a .gb ROM file +$(BINS): $(OBJS) + $(LCC) $(LCCFLAGS) $(CFLAGS) -o $(BINDIR)/$(PROJECTNAME).$(EXT) $(OBJS) + +clean: + @echo Cleaning + @for target in $(TARGETS); do \ + $(MAKE) $$target-clean; \ + done + +# Include available build targets +include Makefile.targets + + +# create necessary directories after Makefile is parsed but before build +# info prevents the command from being pasted into the makefile +ifneq ($(strip $(EXT)),) # Only make the directories if EXT has been set by a target +$(info $(shell mkdir -p $(MKDIRS))) +endif diff --git a/gbdk-lib/examples/cross-platform/bcd/Makefile.targets b/gbdk-lib/examples/cross-platform/bcd/Makefile.targets @@ -0,0 +1,54 @@ + +# Platform specific flags for compiling (only populate if they're both present) +ifneq ($(strip $(PORT)),) +ifneq ($(strip $(PLAT)),) +CFLAGS += -m$(PORT):$(PLAT) +endif +endif + +# Called by the individual targets below to build a ROM +build-target: $(BINS) + +clean-target: + rm -rf $(OBJDIR) + rm -rf $(BINDIR) + +gb-clean: + ${MAKE} clean-target EXT=gb +gb: + ${MAKE} build-target PORT=sm83 PLAT=gb EXT=gb + + +gbc-clean: + ${MAKE} clean-target EXT=gbc +gbc: + ${MAKE} build-target PORT=sm83 PLAT=gb EXT=gbc + + +pocket-clean: + ${MAKE} clean-target EXT=pocket +pocket: + ${MAKE} build-target PORT=sm83 PLAT=ap EXT=pocket + + +megaduck-clean: + ${MAKE} clean-target EXT=duck +megaduck: + ${MAKE} build-target PORT=sm83 PLAT=duck EXT=duck + + +sms-clean: + ${MAKE} clean-target EXT=sms +sms: + ${MAKE} build-target PORT=z80 PLAT=sms EXT=sms + + +gg-clean: + ${MAKE} clean-target EXT=gg +gg: + ${MAKE} build-target PORT=z80 PLAT=gg EXT=gg + +nes-clean: + ${MAKE} clean-target EXT=nes +nes: + ${MAKE} build-target PORT=mos6502 PLAT=nes EXT=nes diff --git a/gbdk-lib/examples/gb/bcd/bcd.c b/gbdk-lib/examples/cross-platform/bcd/src/bcd.c diff --git a/gbdk-lib/examples/gb/bcd/Makefile b/gbdk-lib/examples/gb/bcd/Makefile @@ -1,29 +0,0 @@ -# If you move this project you can change the directory -# to match your GBDK root directory (ex: GBDK_HOME = "C:/GBDK/" -ifndef GBDK_HOME - GBDK_HOME = ../../../ -endif - -LCC = $(GBDK_HOME)bin/lcc -Wa-l -Wl-m -Wl-j - -BINS = bcd.gb - -# GBDK_DEBUG = ON -ifdef GBDK_DEBUG - LCCFLAGS += -debug -v -Wl-u -endif - - -all: $(BINS) - -compile.bat: Makefile - @echo "REM Automatically generated from Makefile" > compile.bat - @make -sn | sed y/\\//\\\\/ | sed s/mkdir\ -p\/mkdir\/ | grep -v make >> compile.bat - -# Compile and link single file in one pass -%.gb: %.c - $(LCC) $(LCCFLAGS) -o $@ $< - -clean: - rm -f *.o *.lst *.map *.gb *~ *.rel *.cdb *.ihx *.lnk *.sym *.asm *.noi *.rst - diff --git a/gbdk-lib/include/gbdk/bcd.h b/gbdk-lib/include/gbdk/bcd.h @@ -4,7 +4,7 @@ #if defined(__TARGET_gb) || defined(__TARGET_ap) || defined(__TARGET_duck) #include <gb/bcd.h> #elif defined(__TARGET_sms) || defined(__TARGET_gg) || defined(__TARGET_msxdos) - #error Not implemented yet + #include <sms/bcd.h> #else #error Unrecognized port #endif diff --git a/gbdk-lib/include/sms/bcd.h b/gbdk-lib/include/sms/bcd.h @@ -0,0 +1,61 @@ +#ifndef __BCD_H_INCLUDE +#define __BCD_H_INCLUDE + +#include <types.h> +#include <stdint.h> + +/** @file bcd.h + Support for working with BCD (Binary Coded Decimal) + + See the example BCD project for additional details. +*/ + +// macro for creating BCD constants +#define BCD_HEX(v) ((BCD)(v)) + +/** Converts an integer value into BCD format + + A maximum of 8 digits may be used +*/ +#define MAKE_BCD(v) BCD_HEX(0x ## v) + +typedef uint32_t BCD; + +/** Converts integer __i__ into BCD format (Binary Coded Decimal) + @param i Numeric value to convert + @param value Pointer to a BCD variable to store the converted result +*/ +void uint2bcd(uint16_t i, BCD * value); + +/** Adds two numbers in BCD format: __sour__ += __value__ + @param sour Pointer to a BCD value to add to (and where the result is stored) + @param value Pointer to the BCD value to add to __sour__ +*/ +void bcd_add(BCD * sour, const BCD * value); + +/** Subtracts two numbers in BCD format: __sour__ -= __value__ + @param sour Pointer to a BCD value to subtract from (and where the result is stored) + @param value Pointer to the BCD value to subtract from __sour__ +*/ +void bcd_sub(BCD * sour, const BCD * value); + +/** Convert a BCD number into an asciiz (null terminated) string and return the length + @param bcd Pointer to BCD value to convert + @param tile_offset Optional per-character offset value to add (use 0 for none) + @param buffer Buffer to store the result in + + Returns: Length in characters (always 8) + + __buffer__ should be large enough to store the converted string + (9 bytes: 8 characters + 1 for terminator) + + There are a couple different ways to use __tile_offset__. + For example: + \li It can be the Index of the Font Tile '0' in VRAM to + allow the buffer to be used directly with @ref set_bkg_tiles. + \li It can also be set to the ascii value for character '0' + so that the buffer is a normal string that can be passed to @ref printf. +*/ +uint8_t bcd2text(const BCD * bcd, uint8_t tile_offset, uint8_t * buffer); + +#endif diff --git a/gbdk-lib/libc/asm/z80/Makefile b/gbdk-lib/libc/asm/z80/Makefile @@ -16,7 +16,8 @@ ASSRC = __strreverse.s strcpy.s strlen.s \ rand.s arand.s \ __sdcc_call_hl.s __sdcc_call_iy.s \ atomic_flag_test_and_set.s __sdcc_critical.s \ - crtenter.s + crtenter.s \ + bcd.s include $(TOPDIR)/Makefile.common diff --git a/gbdk-lib/libc/asm/z80/bcd.s b/gbdk-lib/libc/asm/z80/bcd.s @@ -0,0 +1,212 @@ +;-------------------------------------------------------------------------- +; bcd.s +; +; Copyright (C) 2020, Tony Pavlov +; +; This library is free software; you can redistribute it and/or modify it +; under the terms of the GNU General Public License as published by the +; Free Software Foundation; either version 2, or (at your option) any +; later version. +; +; This library is distributed in the hope that it will be useful, +; but WITHOUT ANY WARRANTY; without even the implied warranty of +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +; GNU General Public License for more details. +; +; You should have received a copy of the GNU General Public License +; along with this library; see the file COPYING. If not, write to the +; Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, +; MA 02110-1301, USA. +; +; As a special exception, if you link this library with other files, +; some of which are compiled with SDCC, to produce an executable, +; this library does not by itself cause the resulting executable to +; be covered by the GNU General Public License. This exception does +; not however invalidate any other reasons why the executable file +; might be covered by the GNU General Public License. +;-------------------------------------------------------------------------- + + .module bcd + + .area _HOME + .ez80 + +; void uint2bcd(uint16_t i, BCD * value) __naked +_uint2bcd:: + ld iyh, d + ld iyl, e + + ex DE, HL + + ; clear value + xor A + ld 0(iy), A + ld 1(iy), A + ld 2(iy), A + ld 3(iy), A + + ld B, #16 +1$: + sla E + rl D + + ld A, 0(iy) + adc A + daa + ld 0(iy), A + ld A, 1(iy) + adc A + daa + ld 1(iy), A + ld A, 2(iy) + adc A + daa + ld 2(iy), A + + dec B + jr NZ, 1$ + + ret + +;void bcd_add(BCD * sour, BCD * value) __naked +_bcd_add:: + or A ; clear C, HC + + ld A,(DE) + add (HL) + daa + ld (HL), A + inc HL + inc DE + + ld A,(DE) + adc (HL) + daa + ld (HL), A + inc HL + inc DE + + ld A,(DE) + adc (HL) + daa + ld (HL), A + inc HL + inc DE + + ld A,(DE) + adc (HL) + daa + ld (HL), A + + ret + +; void bcd_sub(BCD * sour, BCD * value) __naked +_bcd_sub:: + ex DE, HL + or A ; clear C, HC + + ld A,(DE) + sub (HL) + daa + ld (DE), A + inc DE + inc HL + + ld A,(DE) + sbc (HL) + daa + ld (DE), A + inc DE + inc HL + + ld A,(DE) + sbc (HL) + daa + ld (DE), A + inc DE + inc HL + + ld A,(DE) + sbc (HL) + daa + ld (DE), A + + ret + +;uint8_t bcd2text(BCD * bcd, uint8_t tile_offset, uint8_t * buffer) __naked +_bcd2text:: + ex DE, HL ; DE: bcd + pop IY + dec SP + pop BC + ld C, B ; C: digit offset + ex (SP), IY ; HL: buffer + + inc DE + inc DE + inc DE + + ld B, #0x0f + + ld A, (DE) + rlca + rlca + rlca + rlca + and B + add C + ld 0(IY), A + ld A, (DE) + and B + add C + ld 1(IY), A + dec DE + + ld A, (DE) + rlca + rlca + rlca + rlca + and B + add C + ld 2(IY), A + ld A, (DE) + and B + add C + ld 3(IY), A + dec DE + + ld A, (DE) + rlca + rlca + rlca + rlca + and B + add C + ld 4(IY), A + ld A, (DE) + and B + add C + ld 5(IY), A + dec DE + + ld A, (DE) + rlca + rlca + rlca + rlca + and B + add C + ld 6(IY), A + ld A, (DE) + and B + add C + ld 7(IY), A + dec DE + + xor A + ld 8(IY), A + + ld A, #0x08 + + ret
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.