Page 1 of 2

Programming Challenge

Posted: Wed May 10, 2017 11:45 am
by RWAP
Has anyone thought about a ZX81 entry for the programming challenge at:

https://codegolf.stackexchange.com/ques ... aele-cecco

Re: Programming Challenge

Posted: Thu May 11, 2017 12:46 am
by Lardo Boffin
I think I will give it a go in Z80 and see how far I get!

I am currently up to 118 bytes to get the message on the screen as required (including code for a pause).

Now I just need to get the message text to shift one character every time the pause ends...

Or put another way - just the hard bit to do!

Re: Programming Challenge

Posted: Thu May 11, 2017 4:33 pm
by Lardo Boffin
Well, here is my attempt.

I make it 164 bytes based on the start of the line 0 REM and ending with the second line's end of line $76 character. I'm sure I could reduce this by 10 to 20 bytes but maybe another day.... Its just occurred to me I probably don't need the $FF at the end of the text string either so that should 163 bytes. :oops:

Still, its slightly lower than the Spectrum BASIC 187 bytes! :P

This runs on a 1K zeddy in EightyOne emulator but I have not had time to try it on actual hardware yet.

I don't have an account on the code golf website so I guess this is as far as it goes!

The source code: -

Code: Select all

main	
	ld hl,happyBirthday
	ld bc,(Line2Text+7)
	add hl,bc
	ex de,hl
	ld b,12
 	ld hl,(D_FILE)
	inc hl
firstLine
	ld a,(de)
	ld (hl),a
	inc hl
	inc de
	djnz firstLine
	ld b,4
	dec hl
rightVertical
	push de
	ld de,13
	add hl,de
	pop de
	ld a,(de)
	ld (hl),a	
	inc de	
	djnz rightVertical
	dec hl
	ld b,11
lastLine
	ld a,(de)
	ld (hl),a
	dec hl
	inc de
	djnz lastLine
	ld b,3
	inc hl
leftVertical
	push de
	ld de,13
	sbc hl,de
	pop de
	ld a,(de)
	ld (hl),a	
	inc de	
	djnz leftVertical
	
delayCode
	ld   hl,FRAMES         ; fetch timer                 
   	ld   a,(hl)                                          
    	sub  10                ; wait 10 full frames (0.1 of a second)
delayLoop        
	cp  (hl) 
    	jr   nz,delayLoop
endOfMain	
	ld a,(Line2Text+7)
	inc a
	cp 30
	jr nz,notLooping
	ld a,0
notLooping	
	ld (Line2Text+7),a
	jp main
happyBirthday
	DEFB	_H,_A,_P,_P,_Y,__,_B,_I,_R,_T,_H,_D,_A,_Y,__,_R,_A,_F,_F,_A,_E,_L,_E,__,_C,_E,_C,_C,_O,__,_H,_A,_P,_P,_Y,__,_B,_I,_R,_T,_H,_D,_A,_Y,__,_R,_A,_F,_F,_A,_E,_L,_E,__,_C,_E,_C,_C,_O,__,$ff
I haven't included the bits to add in line 1 and line 2 etc. but have included the bytes they take up in the byte count.

Lardo

Re: Programming Challenge

Posted: Thu May 11, 2017 5:15 pm
by PokeMon
Well you could spar a few bytes with using LDIR or at least LDI instruction for moving data. See Z80 manual/datasheet. And the last part contains two identical strings - this can be shortened with resetting pointers. ;)

Re: Programming Challenge

Posted: Thu May 11, 2017 5:25 pm
by Lardo Boffin
PokeMon wrote: Thu May 11, 2017 5:15 pm Well you could spar a few bytes with using LDIR or at least LDI instruction for moving data. See Z80 manual/datasheet. And the last part contains two identical strings - this can be shortened with resetting pointers. ;)
Yes - the string was where I figured I could save a good few bytes. I doubled the size to make it easier to code and avoid wrap arounds. In my defence I got to that bit at around midnight last night and had run out of brain power! :shock:

Re: Programming Challenge

Posted: Fri May 12, 2017 12:42 pm
by dr beep
Lardo Boffin wrote: Thu May 11, 2017 4:33 pm

Code: Select all

	ld a,(Line2Text+7)
	inc a
	cp 30
	jr nz,notLooping
	ld a,0
notLooping	
	ld (Line2Text+7),a
	jp main
I haven't included the bits to add in line 1 and line 2 etc. but have included the bytes they take up in the byte count.

Lardo
Above part Can be (-2)

Code: Select all

	ld a,(Line2Text+7)
	inc a
seta	ld (Line2Text+7),a
	sub 30
	jr z,seta
	jp main

Re: Programming Challenge

Posted: Fri May 12, 2017 5:20 pm
by Lardo Boffin
dr beep wrote: Fri May 12, 2017 12:42 pm
Lardo Boffin wrote: Thu May 11, 2017 4:33 pm

Code: Select all

	ld a,(Line2Text+7)
	inc a
	cp 30
	jr nz,notLooping
	ld a,0
notLooping	
	ld (Line2Text+7),a
	jp main
I haven't included the bits to add in line 1 and line 2 etc. but have included the bytes they take up in the byte count.

Lardo
Above part Can be (-2)

Code: Select all

	ld a,(Line2Text+7)
	inc a
seta	ld (Line2Text+7),a
	sub 30
	jr z,seta
	jp main
That's clever!

Re: Programming Challenge

Posted: Fri May 12, 2017 6:37 pm
by PokeMon
Anyway - never use LD A,0 - XOR A instead.
Except you need the flags ...

Re: Programming Challenge

Posted: Fri May 12, 2017 10:01 pm
by Lardo Boffin
PokeMon wrote: Fri May 12, 2017 6:37 pm Anyway - never use LD A,0 - XOR A instead.
Except you need the flags ...
You are correct - I had since changed that one!

Re: Programming Challenge

Posted: Fri May 12, 2017 10:25 pm
by kmurta
An other option, with 144 bytes in two Basic lines (123 bytes of z80 codes):

Code: Select all

upside:
        ld hl,msg
        ld de,(16396)   ; screen address
        inc de
        ld bc,12
        ldir

rightside:
        ex de,hl
        dec hl
        ld b,4
rsloop:
        push bc
        ld bc,33
        add hl,bc
        ld a,(de)
        ld (hl),a
        inc de
        pop bc
        djnz rsloop

underside:
        ex de,hl
        ld bc,13
        add hl,bc
        dec bc
        lddr

leftside:
        ex de,hl
        inc hl
        ld b,3

lsloop:
        push bc
        ld bc,33
        sbc hl,bc
        ld a,(de)
        ld (hl),a
        dec de
        pop bc
        djnz lsloop

rotatemsg:
        ld hl,msg
        ld d,h
        ld e,l
        ld a,(hl)
        push af
        ld a,(msg+29)
        inc hl
        ld bc,14
        ldir
        ld hl,msg+28
        ld d,h
        ld e,l
        inc de
        ld bc,14
        lddr
        ld (hl),a
        pop af
        ld (de),a

delay:
        ld hl,FRAMES
        ld a,(hl)
        sub 10
dlyloop:
        cp (hl)
        jr nz,dlyloop

        jr upside

msg:    dbzx 'HAPPY BIRTHDAY  OCCEC ELEAFFAR'
rcecco.p
(1022 Bytes) Downloaded 245 times