Skip to content

Ok, MUCH Better Results With SDCC

September 13, 2015

Thanks to David Taylor’s Blog OrangeShed I had much more success today getting code from SDCC to work on the Z80 membership card.

My Crt0 and putchar routines are simpler and actually seem to work. As a STUNNING improvement I’m able to use the standard library that’s shipped with SDCC – for my 1802 compiler I had to write my own support libraries from strcmp() to printf(); Printf was particularly painful but the version shipped with SDCC just worked. The Hello World program is 3K long(which compares very favourably with 7K for LCC1802).

;--------------------------------------------------------------------------
;  crt0.s - Generic crt0.s for a Z80
;
;  Copyright (C) 2000, Michael Hope
;
;  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 crt0 from SDCC
;;for the Z80 Membership Card and ZMC 1.1 monitor
;;15-09-13 thanks to David Taylor http://oshed.blogspot.ca/2015/05/as-much-fun-as-assembly-coding-is.html
	.module crt0
	.globl	_main
	.globl l__INITIALIZER
	.globl s__INITIALIZED
	.globl s__INITIALIZER

	.area	_HEADER (ABS)

	.org 	0x8000	;RAM starts at 32K
init:
	;; Set stack pointer directly above top of memory.
;	ld	sp,#0x0000	;***NOT SETTING STACK***

	;; Initialise global variables
	call	gsinit
	call	_main
	rst	0	;restart josh's monitor

	;; Ordering of segments for the linker.
	.area	_HOME
	.area	_CODE
	.area	_INITIALIZER
	.area   _GSINIT
	.area   _GSFINAL

	.area	_DATA
	.area	_INITIALIZED
	.area	_BSEG
	.area   _BSS
	.area   _HEAP

	.area   _CODE

	.area   _GSINIT ;wjr
gsinit::
	ld	bc, #l__INITIALIZER
	ld	a, b
	or	a, c
	jr	Z, gsinit_next
	ld	de, #s__INITIALIZED
	ld	hl, #s__INITIALIZER
	ldir
gsinit_next:

	.area   _GSFINAL 
	ret

;--------------------------------------------------------------------------
;  putchar.s
;
;  Copyright (C) 2000, Michael Hope
;
;  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.
;--------------------------------------------------------------------------
;15-09-13 thanks to David Taylor http://oshed.blogspot.ca/2015/05/stdio-functions-via-cpuville-monitor.html
; Josh's ROM entry points.
write_char      =    0x0FE9
get_char        =    0x0FDD

    .area _CODE
_putchar::
    ld      hl, #2
    add     hl, sp
    ld      a, (hl)
    cp	    #0x0a	;check for linefeed
    jp	    nz,doit	;no, just send it
    ld	    a,#0x0d	;prep cr
    call    write_char
    ld	    a,#0x0a	;prep lf
doit:
    call    write_char
    ret
_getchar::
    call    get_char
    ld      l,a
    ret
#include <stdio.h>

void main()
{
	printf("\nHello World\n");
}

The compile sequence is a bit convoluted but replacing the shipped versions of crt0 and putchar with mine should clean it up. The first two commands ran once to prep the support routines; the third actually compiles the C program; and the last one combines all the object modules.

C:\Users\Bill\Desktop\olduinoZ>sdasz80 -l -o -g putchar_ZMC.s
C:\Users\Bill\Desktop\olduinoZ>sdasz80 -l -o -g crt0_ZMC.s
C:\Users\Bill\Desktop\olduinoZ>sdcc -mz80 -c sdccmule.c
C:\Users\Bill\Desktop\olduinoZ>sdcc -mz80 --code-loc 0x8007 --no-std-crt0 crt0_ZMC.rel sdccmule.rel putchar_ZMC.rel

and the money shot:
15-09-13 HW

I also had to switch from putty to TeraTerm to get a program that would pace the transfer the hex file to not cause errors at the Z80 end.

Cleaning up, I renamed the shipped versions of crt0.rel and z80.lib and updated them with my work:

C:\Users\Bill\Desktop\olduinoZ>copy crt0_zmc.rel "H:\Program Files\SDCC\lib\z80\
crt0.rel"

C:\Users\Bill\Desktop\olduinoZ>ren putchar_ZMC.rel putchar.rel

C:\Users\Bill\Desktop\olduinoZ>sdar rv "H:\Program Files\SDCC\lib\z80\z80.lib" p
utchar.rel

And now the compile is as simple as “sdcc -mz80 –code-loc 0x8007 sdccmule.c”. I’ll probably adopt some ide for this that will hold the command lines or just make a textpad macro.

Advertisements

From → Olduino/Z

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: