Introduction: Z80-mbc2 Z80 Code Flash User LED

This is an example user LED program written in Z80 assembler for the z80-mbc2 computer.

I did this a test and revision exercise for me, this is my first Z80 program for over 35 years.

Enjoy

Supplies

Z80-mbc2 computer

A copy of Z80ASM from SLR Systems Rel. 1.32, downloadable from many cp/m archives

An editor, I used ZDE16, since it was already on the CP/M boot image for the z80-mbc2

Step 1: Install Z80asm on Your Z80-mbc2

I assume some familiarity with the tools you have for your Z80-MBC2, namely whatever version of cpmtools you use. Here are the steps if using the command line versions:

  1. Get the files z80asm.com and z80asm.doc (optional) in a directory some place on your host PC
  2. Put your SD card in the same PC, I assume bellow that it is showing as drive E: below.
  3. I put the assembler on my CP/M disk A, for cp/m 2.2 that would be disk image DS0N00.DSK
  4. The command: cpmcp -f z80mbc2-d0 e:DS0N00.DSK z80asm.com 0: will copy the file z80asm.com to the image
  5. The command cpmcp -f z80mbc2-d0 e:DS0N00.DSK z80asm.doc 0: will copy the documentation to the disk (optional)

When you boot cp/m 2.2 on your z80-mbc, you should now have z80asm.com and z80asm.doc (optional) on drive A

Step 2: Copy the Source Code to You Z80-mbc2

Connect to the z80-mbc using your terminal program

I suggest you use an unpopulated disk to save the source code on, for example f: to choose this drive type:

F: <cr>

after a few seconds you will see the F: prompt.

now use the zde16 program on drive A to edit a blank file:

a:zde16 myled.z80

This will create a file called myled.z80 on the disk F:, note: the .z80 is required in the name for z80asm to work.

Now copy and paste the code bellow, depending on your terminal speed etc you may have to copy and paste in small sections. Or type it in! get that 1980's feel back as well.

;
;Test the user LED and user Key
;
BDOS    equ     05h		;cp/m BDOS entry point
USRLED	equ	0		;opcode for User LED control
USRKEY	equ	80h		;opcode for User Switch control
DPORT	equ	0		;DATA port i/o 
CPORT	equ	1		;Command port output
;
        org     0100h
;
        ld	(estack),sp	;keep existing info for return to cp/m
        ld      sp,stack
;
        ld      hl,msg		;hello message
	call	_puts
	
lp:	ld	a,1		;turn on = 1
	call	led
	call	delay		;delay and wait for key(s)
	jr	nz,finish	;non zero means key/switch pressed
	xor	a		;a=0
	call	led
	call	delay		;delay or wait for key/switch
	jr	z,lp		;if zero, no key/switch, go again
;
finish:	xor	a		;key or switch was pressed, so done
	call	led
	ld	hl,bye		;say bye
	call	_puts		;display msg HL=
	ld	sp,(estack)	;original stack
	ret			;return to cp/m
;
led:	push	af		;save led value 0 or 1
	ld	a,USRLED
	out	(CPORT),a	;optcode sent
	pop	af
	out	(DPORT),a	;data sent
	ret
;	
;Delay loop that check key pressed on terminal or user key 
delay:	ld	bc,00e00h	;aprox 1/2s 8mhz CPU, pure guess work
loop2:	dec	bc		;count the loop
	push	bc		;save regs when calling BDOS
	push	de
	ld	c,6		;BDOS raw i/o check if key pressed
	ld	e,0ffh
	call	BDOS
	pop	de		;get our values back from stack
	pop	bc
	or	a		;check if key pressed on terminal
	ret	nz		;yes return (also keeps nz flag set)
	ld	a,USRKEY	;Now check user key
	out	(CPORT),a	;command port
	in	a,(DPORT)	;read key
	and	a,1		;only interested in bit 0
	ret	nz		;return if userkey pressed
	ld	a,b		;or is bc=0
	or	c
	jr	nz,loop2	;not finished counting, go again
	xor	a		;set the z flag to 0
	ret			;return no key pressed, end of delay
	
;	
_puts:	push	bc		;save our regs ready for bdos calls
	push	de
nxt:	ld	a,(hl)		;get char
	or	a		;test if it's 0, end of msg
	jr	z,done		;yes so done
	ld      e,a		; set up call to BDOS to print a char
        ld      c,2
        push    hl		;save text pointer
        call    BDOS		;print it
        pop     hl
        inc     hl		;next char
        jr      nxt		;keep going
done:	pop	de		;finished restore regs
	pop	bc
	ret			;and return
;
msg    	db      'User LED test, any key to exit'
	db	0ah,0dh,0
bye	db	'Exit'
	db	0ah,0dh,0
estack	dw	0
  	ds      256 - 2
stack: 	dw       0

Save the file using ESC x. See the ZDE16 use guide for more info, it's a bit like wordstar for most editing, except the Ctrl-K options are different.

Step 3: Compile and Run the Program

Once you have your source code on the machine.

Type:

F>
F>a:z80asm myled/a

Z80ASM Copyright (C) 1983-86 by SLR Systems Rel. 1.32

 MYLED/A
End of file Pass 1
 0 Error(s) Detected.
 406 Absolute Bytes. 16 Symbols Detected.

This will look for the file myled.z80 and produce a binary (.com) file, thats what the option /a stands for.

See the z80asm.doc file for more info.

To run the program, type:

F>myled
User LED test, any key to exit
Exit

F>

Press either the user button or a key on the terminal to exit.

Done.