Skip to content

Small Device C Compiler – First Try

September 9, 2015

SDCC is a very nice C compiler which I have looked at before but never used. It supports the Z80 so I’ve downloaded it from here

SDCC comes with a full library including things like printf(). In order to adapt it for the Z80 Membership Card I have to do some coding. I’m following a guide someone wrote for a Z80-based Amstrad system which I found here. Basically I have to customize a module called crt0.s and a putchar routine – I’ve called mine crt0_ZMC.s and putchar_ZMC.s. I compile those with sdasz80 -o -g crt0_ZMC.s and sdasz80 -o putchar_zmc.s
That produces crt0_ZMC.rel and putchar_ZMC.rel

;--------------------------------------------------------------------------
;  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.
;--------------------------------------------------------------------------

        .area   _CODE
_putchar::
_putchar_rr_s::
        ld      hl,#2
        add     hl,sp

;        ld      l,(hl)
;        ld      a,#1
;        rst     0x08
        ld      a,(hl)
        call    0x0FE9
;        rst     0x08

        ret

_putchar_rr_dbs::
;        ld      l,e
;        ld      a,#1
;        rst     0x08
	ld	a,e
	call	0x0FE9
        ret

;--------------------------------------------------------------------------
;  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
;;first try for the Z80 Membership Card and ZMC 1.1 monitor
	.module crt0
	.globl	_main

	.area	_HEADER (ABS)
	;; Reset vector
	.org 	0x8000	;RAM starts at 32K
	jp	init

	.org	0x08
	reti
	.org	0x10
	reti
	.org	0x18
	reti
	.org	0x20
	reti
	.org	0x28
	reti
	.org	0x30
	reti
	.org	0x38
	reti

	.org	0x8100
init:
	;; Set stack pointer directly above top of memory.
	ld	sp,#0x0000

	;; Initialise global variables
	call	gsinit
	call	_main
	jp	_exit

	;; 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
__clock::
	ld	a,#2
	rst	0x08
	ret

_exit::
	;; Exit - special code to the emulator
	ld	a,#0
	rst	0x08
1$:
	halt
	jr	1$

	.area   _GSINIT
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

Not wanting to push my luck, I made a very simple C program that calls putchar

#include <stdio.h>
main()
{
  putchar('H');
  while(1) {};
}

I compile that with
C:\Users\Bill\Desktop\olduinoZ>sdcc -mz80 –code-loc 0x8000 –no-std-crt0 crt0_
zmc.rel putchar_ZMC.rel sdccmule.c

The resulting .ihx file is not right but it’s close(I’ve inserted spacing and notes). I see my main routine compiled, I see the putchar routine, and I see the code of crt0 but i think crt0 is on top of main.

: 0A 8000 00 3E48F533 CD148033 18FE 1E ***MAIN at 8000**
: 03 8000 00 C30081 39  **CRT0 at 8000**
: 02 0008 00 ED4DBC
: 02 0010 00 ED4DB4
: 02 0018 00 ED4DAC
: 02 0020 00 ED4DA4
: 02 0028 00 ED4D9C
: 02 0030 00 ED4D94
: 02 0038 00 ED4D8C
: 0C 8100 00 310000CD0080CD0080C30E8057
: 0A 800A 00 3E02CFC93E00CF7618FDFC
: 10 8000 00 01000078B12808110080211080EDB0C96E
: 0E 8014 00 210200 39 7E CDE90F C9 7BCDE90FC9ED **PUTCHAR at 8014**
: 00000001FF

I compile again with
C:\Users\Bill\Desktop\olduinoZ>sdcc -mz80 –code-loc 0x8000 –no-std-crt0 crt0_
zmc.rel putchar_ZMC.rel sdccmule.c
but still not right – tomorrow I guess.

:0A9000003E48F533CD14903318FEFE
:03800000C3008139 **jump to init code**
:02000800ED4DBC **probably shouldn't be at 08**
:02001000ED4DB4
:02001800ED4DAC
:02002000ED4DA4
:02002800ED4D9C
:02003000ED4D94
:02003800ED4D8C
:0C810000310000CD0080CD0090C30E9037  **correct at 8100**
:0A900A003E02CFC93E00CF7618FDEC  **correct at 9000**
: 10 8000 00 010000 78 B12808110080211080EDB0C96E  **whoops - back to 8000 for GSINIT: **
:0E901400210200397ECDE90FC97BCDE90FC9DD
:00000001FF
Advertisements

From → Olduino/Z

One Comment

Trackbacks & Pingbacks

  1. Small Device C Compiler – Still Trying | olduino

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: