How to minimize ZX81 binaries?

Any discussions related to the creation of new hardware or software for the ZX80 or ZX81
Post Reply
Sdw
Posts: 17
Joined: Mon Nov 09, 2009 1:39 pm

How to minimize ZX81 binaries?

Post by Sdw »

Lately there has been kind of an new fad over at demoscene site Pouet, where people have been implementing the classic TV-noise effect on different platforms, and trying to get it as small as possible.
Of course I wanted to give it a shot on the ZX81, you can find my attempt here:
TV noise ZX81 by Noice

Here's the code for the effect itself, 34 bytes:

Code: Select all

; TV-noise for the ZX81
; by Andreas 'Shadow/Noice' Gustafsson
; 2010-11-13
   
   ld    hl,(400ch)       
   ld    de,24*33         
   add   hl,de            
   ld    de,7618h    ; d=halt opcode, e=y loop counter
   ld    (hl),d      ; Ending halt
yloop:                   
   ld    b,32   
xloop:         
   dec   hl     
   and   63          ; Make sure we have a legal char
   ld    (hl),a       
   inc   a
   djnz  xloop
   add   a,e         ; Add y to char to get more 'randomness'
   dec   hl                
   ld    (hl),d     
   dec   e                 
   jr    nz,yloop
infloop:   
   and   15          ; Stick with lowest 4k of ROM so we don't get the real charset, just random data
   inc   a
   ld    i,a  
   jp    infloop
However, the final binary ends up at a whopping 125 bytes, which leads me to believe that my "linker" isn't all that optimal. What I do is actually have a bunch of binary data that I have no idea on what it does, and then inject my binary at the right place. I'm guessing it is all about the BASIC setup etc. that is needed for the ZX81 to run the machine code.
I'd like to know how to strip it down to the bare minimum. What is the smallest size that you can get the code above up and running on a ZX81?
User avatar
BrunoFlorindo
Posts: 290
Joined: Sat May 10, 2008 2:46 am
Location: Anaheim, CA, USA

Re: How to minimize ZX81 binaries?

Post by BrunoFlorindo »

Bumping this one because it would be nice to see what other ZX81 coders can come up with. I had no idea there was a place for ZX81 stuff on Pouet!

I'm not a coder but I love when someone comes up with an effect or demo for the ZX81.
User avatar
bobs
Posts: 325
Joined: Thu Aug 27, 2009 10:49 pm
Location: UK
Contact:

Re: How to minimize ZX81 binaries?

Post by bobs »

Sdw wrote:the final binary ends up at a whopping 125 bytes, which leads me to believe that my "linker" isn't all that optimal. What I do is actually have a bunch of binary data that I have no idea on what it does, and then inject my binary at the right place. I'm guessing it is all about the BASIC setup etc. that is needed for the ZX81 to run the machine code.
I'd like to know how to strip it down to the bare minimum. What is the smallest size that you can get the code above up and running on a ZX81?
When you say "final binary" do you mean a .P file? If so, that will include all the system variables, the BASIC program itself, and also the display buffer. I'm not sure of the exact numbers, but I think the system variables will be probably be 30+ bytes, and the smallest possible display buffer - an empty screen - is still 25 bytes, so it all adds up.
User avatar
bobs
Posts: 325
Joined: Thu Aug 27, 2009 10:49 pm
Location: UK
Contact:

Re: How to minimize ZX81 binaries?

Post by bobs »

I've now compiled your program as a .P file, so here are the actual values:

The system variables, including the printer buffer and calculator scratch-pad, and BASIC variables, takes 117 bytes.
The BASIC program to launch the machine code takes 30 bytes.
The machine code (your program) takes 34 bytes.
The display buffer takes 25 bytes.

Which totals 206 bytes, which obviously isn't the 125 bytes you mention, so I guess you were taking about a pure binary for just the code, in which case I've no idea where the additional 91 bytes are coming from - perhaps you have some default libraries included? Which compiler are you using?
Sdw
Posts: 17
Joined: Mon Nov 09, 2009 1:39 pm

Re: How to minimize ZX81 binaries?

Post by Sdw »

bobs wrote: When you say "final binary" do you mean a .P file? If so, that will include all the system variables, the BASIC program itself, and also the display buffer. I'm not sure of the exact numbers, but I think the system variables will be probably be 30+ bytes, and the smallest possible display buffer - an empty screen - is still 25 bytes, so it all adds up.
Yes, I mean the final .P file that can be loaded in an emulator or converted to WAV and transferred to a real ZX81.

My "linker" takes these bytes:

Code: Select all

0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0xff,0x5d,0x40,0x00,0x02,0x00,0x00,0xff,0xff,0x00,0x37,
0x3c,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0xff,0x00,0x00,0x3c,
0x21,0x00,0x40,0x00,0x01,0x0a,0x00,0xf0,0xd4,0x20,0x7e,0x8f,0x00,0x94,0x00,0x00,
0x76
Then changes a few bytes to point to the dispfile according to the filesize, then adds the compiled Z80 code (34 bytes in this case) and finally adds a screen of 25 0x76 bytes at the end.
The total is a 125 bytes P file that seems to work. But I have no idea exactly how the BASIC loader works from those 65 bytes in the beginning, I just took them from an exisiting .P file and reverse-engineered where to inject my own code! :)
That's why I thought perhaps it could be optimized even more.
User avatar
yerzmyey
Posts: 1240
Joined: Thu May 15, 2008 10:11 am
Location: Rubber Planet
Contact:

Re: How to minimize ZX81 binaries?

Post by yerzmyey »

bobs wrote: When you say "final binary" do you mean a .P file? If so, that will include all the system variables, the BASIC program itself, and also the display buffer. I'm not sure of the exact numbers, but I think the system variables will be probably be 30+ bytes, and the smallest possible display buffer - an empty screen - is still 25 bytes, so it all adds up.
Argh, yes. I remember this matter from our ZX81 1Kb Demo Compo http://zx81.republika.pl/
it was highly annoying when we found that screen and other stuff are beeing saved into P, hehe.
And some other rules, like "to make the prog smaller, use only upper/left corner of the screen" and so on... ;)
IN NIHILUM REVERTERIS - a big text-adventure game for ZX81: http://tiny.pl/g2m6m
"MONOCHROME" issue 5 - (Spring 2014) free paper/PDF magazine about ZX81: http://tiny.pl/q2m44
ZX81 COMPETITIONS 2007/2009: http://zx81.republika.pl/
Post Reply