AY tunes on a ZX81?

Any discussions related to the creation of new hardware or software for the ZX80 or ZX81
User avatar
BrunoFlorindo
Posts: 290
Joined: Sat May 10, 2008 2:46 am
Location: Anaheim, CA, USA

AY tunes on a ZX81?

Post by BrunoFlorindo »

It seems many, if not all of the sound interfaces for early Sinclair machines used a compatible chip with the one used on later 128K Spectrums. If this is the case, what would we need to play .AY files on a real ZX81 with one of these interfaces, or an emulated one?

The program currently used to play .AY files on a real Spectrum is SpecAY, which can be downloaded from here:

http://www.specay.co.uk/download/

Maybe someone would know how to adapt this machine code program so that we could use it on a zx81?

Code: Select all

; SpecAY Machine Code Routines
;
; Version 0.3-dev

                 ORG 32652
;                 ENT

; First Routine:
; Copies zero-terminated string into BASIC variable. 
;
; Slightly modified version of...
; zx_setstr
; by Stefano Bodrato, 29/06/2006
;
 
                 LD   A,255           ; ASCII code of variable name (97=c)
                 AND  95              ; convert to upper-case
                 LD   HL,65535        ; 65535 is the start address of the zero-terminated string

                 ld   (morevar+1),a
                 ld   (pointer+1),hl
 

                 ld   hl,($5c4b)      ; VARS
loop:            ld   a,(hl)
                 cp   128
                 jr   nz,morevar

                 jr   store           ; variable not found

morevar:
                 cp   0
                 jr   nz,nextvar
 
                 call $19b8           ; get next variable start
                 call $19e8           ; reclaim space (delete)
 
store:
                 ld   bc,0
pointer:
                 ld   de,0            ; point to the string
                 push de
lenloop:
                 inc  bc              ; string length counter
                 inc  de
                 ld   a,(de)
                 and  a
                 jr   nz,lenloop

                 push hl
                 push bc
                 inc  bc
                 inc  bc
                 inc  bc
                 call $1655           ; MAKE-ROOM
                 pop  bc
                 pop  hl

                 ld   a,(morevar+1)
                 ld   (hl),a
                 inc  hl
                 ld   (hl),c
                 inc  hl
                 ld   (hl),b
                 inc  hl
                 pop  de
                 
                 ex   de,hl
                 ldir            
 
                 ret

nextvar:
                 call $19b8           ;get next variable start
                 ex   de,hl
                 jr   loop


; Second Routine:
; Executes code stored within a BASIC string variable

; Put ASCII codes for "c$" into address 32766+32767
; This can be omitted if these bytes are set before run time

;                LD A,99
;                LD (32766),A
;                LD A,36
;                LD (32767),A   

                 ; Save current contents of CH ADD
                 LD BC,(23645)
                 PUSH BC 

                 ; Put 32766 into CH ADD
                 LD BC,32766
                 LD (23645),BC
                
                 ; Invoke LOOKVAR
                 CALL 10418
                
                 ; Restore old CH ADD
                 POP BC
                 LD (23645),BC
                
                 ; LDIR the code into screen memory.
                 ; HL = Source address
                 ; DE = Destination address
                 ; BC = Bytes to copy

                 INC HL
;                 LD BC,(HL)     ; HL points to size of variable
                 LD C,(HL)
                 INC HL
                 LD B,(HL)
                 INC HL
                 LD DE,20480

                 LDIR

                 JP 20480

                 ; Code jumped to ends with RET. 
There are hundreds of nice .AY tunes available. If it's just a matter of different ports, it should be easy to adapt the program. The people from TS2068 also did something similar. But instead of developing a player, they modified games to make the AY sound work.
Mike
Posts: 52
Joined: Sun May 11, 2008 5:38 pm

Re: AY tunes on a ZX81?

Post by Mike »

It's not quite that simple...

Yes, you could get music by simply changing all the port references. However, since the AY chip in the various sound boards use a different clock frequency to the 128k Spectrum, any music created will sound out of tune. To sound right, you'd have to modify the AY data for the different clock frequency.

Try it and see what happens... maybe it won't sound too bad :)

Mike
User avatar
BrunoFlorindo
Posts: 290
Joined: Sat May 10, 2008 2:46 am
Location: Anaheim, CA, USA

Re: AY tunes on a ZX81?

Post by BrunoFlorindo »

Thanks! I guess it's time to start looking for a list of clock frequencies and ports used on each board. Let me see what I can find. :)
Mike
Posts: 52
Joined: Sun May 11, 2008 5:38 pm

Re: AY tunes on a ZX81?

Post by Mike »

From my source code (Which may or may not be correct ;) )

128k spectrum 1.7734 MHz
Timex 1.764 MHz
ZON-X 1.625 MHz
Fuller 1.625 MHz
QuickSilva 0.8125MHz

As you can see, the Spectrum and Timex are quite close, whereas the ZX81 boards use a slightly slower clock - probably as a result of the spectrum using a 7MHz crystal and the ZX81 6.5MHz.

Mike
Mike
Posts: 52
Joined: Sun May 11, 2008 5:38 pm

Re: AY tunes on a ZX81?

Post by Mike »

... and here's a little table with all the ports on it:

Code: Select all


Board		Frequency	Register Select		Data
Quicksilva	0.8125MHz	0x7fff			0x7ffe  (Memory Mapped, Write Only)
Zon-X		1.625MHz	0xdf			0x0f
Fuller		1.625MHz	0x3f			0x5f

Sinclair	1.7734MHz	0xfdff			0xfdbf
Timex		1.764MHz	0xf5			0xf6
Mike
User avatar
BrunoFlorindo
Posts: 290
Joined: Sat May 10, 2008 2:46 am
Location: Anaheim, CA, USA

Re: AY tunes on a ZX81?

Post by BrunoFlorindo »

Thanks Mike!

It's in times like these that I wish I knew machine code. I have ideas and I try to imagine how to do it, but I have to ask here and there and hope that I'll be able to get a result. I've contacted SpecAY's author and asked if he could give us his input and permission to use his code (if we're successful). I could also ask a few Spectrum musicians if they can help me adapt the assembly listing. My machine code knowledge is reduced to ldirs, rets, loading registers, and jps . :(

If I'm not mistaken, a simple BASIC program could provide a menu to choose which type of AY interface or machine is being used, an option to load the AY file from tape, and an option to play the AY file. If using an emulator, the AY file could simply be imported to the appropriate address. A few POKEs should be enough to adapt the routine to the chosen interface.

If someone would like to try it, please download the AY file or the collection of AY files.

ftp://ftp.worldofspectrum.org/pub/sincl ... vie.ay.zip
http://projectay.dl.spilsby.net/ProjectAY.zip

To hear the AY files using Windows you can use AyEmul. To hear them on the Spectrum or a Spectrum emulator you can use SpecAY.

AyEmul's webpage: http://bulba.at.kz/emulator_e.htm
SpecAY's webpage: http://www.specay.co.uk/

If this works, I will upload a video to YouTube and let everyone know that the ZX81 can sound as good as a Spectrum 128K!! :mrgreen:
Mike
Posts: 52
Joined: Sun May 11, 2008 5:38 pm

Re: AY tunes on a ZX81?

Post by Mike »

I've just been looking into the specifications of the .AY file format :o

It seems it's not really a file format as such - but a z80 program the player "runs". As such, all the port addresses are hard coded into the .ay file, and it makes assumptions about the memory map that it's running in. I don't think a generic player would work on a ZX81 - for all .AY files to play, you'd need to have at least 48k of memory that can execute machine code as well as searching for all the OUT instructions in the AY file, replacing them as necessary.

Also, most AY tunes are interupt driven which poses more issues on the ZX81...

Mike
User avatar
XavSnap
Posts: 1940
Joined: Sat May 10, 2008 4:23 pm
Location: 'Zx81 France' Fb group.

Re: AY tunes on a ZX81? What about the VTR sound card.

Post by XavSnap »

Hi!
Do you know the VTR card ?
http://zx81.ordi5.free.fr/vtr/
Sorry it's a french page !

All the best ... ;-))
Xavier ...on the Facebook groupe : "Zx81 France"(fr)
User avatar
yerzmyey
Posts: 1240
Joined: Thu May 15, 2008 10:11 am
Location: Rubber Planet
Contact:

Re: AY tunes on a ZX81?

Post by yerzmyey »

Hi all!!!!!!!!!!!!!!

Generally:

Well, actually there exist AY interface for ZX-81. However it's not beeing produced contemporary (pity).
But ZX81 guys should be easily able to get its schemes and solder them. Spectrum people does (my friend made this one http://ay-riders.speccy.cz/concert/full/05.JPG ).

Port references:
True. ZX81's zx-bus looks the same like ours but actually it's different. As far as I know Spectrum's AY-interfaces will not work on ZX81. But making native AY for ZX81 should be easy for "soldering guys" ;) .
But it would also require changing in playin-procedures, I suspect.

Different clock frequency:
I don't think it's a problem. It could be a problem in reffering to Z80 music (beeper) but reffering to AY - music will simply play a bit higher/ a bit lower, that's all. Nobody pay attantion on it.
The only exception is Amstrad, where Spectrum AY music play lower by factor of 12 semitones (one octave lower!!!!) and it sound like crap.

Specifications of the .AY file format:
actually I don't think AY format is good in this case. I think the best way would be to use native formats from Spectrum (for instance compiled song from Spectrum Sound-Tracker). But of course somebody would have to write a convertor from ZX to ZX81. From *.TAP to *.P or something.

Guys from Amstrad converts ZX music often (after fine-tunning) so it can't be *so* hard.
But the engine itself would require coder's attantion surely.

AY tunes are interrupt driven:
Sadly I'm not a coder at all. But I suspect that the most simple and basic way of playing compiled AY songs from SoundTracker is not based on interrupts. One tip from me: when I play compiled AY music on my Spectrum 48K with AY interface, I use such prog:
(let's say the tune is compiled into RANDOMIZE USR 40000) -

10 RANDOMIZE USR 40000 (and that's so-called "init")
20 RANDOMIZE USR 40006 (and that's so-called "play")
30 PAUSE 1 (this is standard delay/tempo)
40 GOTO 20

And that's all.
So if somebody could convert ZX data into ZX81 data and relocate music address, it shouldn't use any interrupts, I suspect.

Ah, and one more thing - such AY song is about 3Kb - 6Kb big. Should work fine on ZX81 16Kb.

What do You think, guys?
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/
gasman
Posts: 2
Joined: Thu May 15, 2008 10:57 am

Re: AY tunes on a ZX81?

Post by gasman »

Hello, I've been summoned here by Bruno too :-)

Yep, the .AY format isn't really one that can be transformed into something else at the AY data level - it's more like a Spectrum snapshot format. (Which is not a bad thing in itself, but the .AY format is dreadful for other reasons too.) You'd be better off using the 'tracker' file formats as your starting point - STC, SQT, PT3 (and a few other rarely-used ones), as introduced by AY_Emul. Each of these is a raw binary dump of the music data (not the code) for a particular Spectrum tracker, so each one is a well-defined format for which Z80 player code is readily available. I'll boldly guess that porting those three routines (Soundtracker, SQ Tracker, Protracker 3) will cover 80-90% of all AY music written for the Spectrum...

Recently (last week in fact) I wrote a utility for converting these formats to .TAP, .TZX and .AY, called tracker2ay - basically all it does is bundle the music file with the appropriate Z80 player, inside the requested container format. I'd be happy to add ZX81 support if there's interest (and people are willing to feed me the ZX81-specific details, specifically what address to put it at and how to write a Basic loader for it) - it would just be a case of hacking the Spectrum player routines to output to the right ports (dead easy for STC, probably easy for the others although I haven't looked at them closely enough to be sure) and implementing another container format (I recall that writing .P files was totally straightforward when I wrote ZXpaintyONE, so no problem there.)

About tweaking AY data to correct for the frequency - I agree with Yerz, it's not worth it. Firstly, the ZON-X and Fuller are only 8% off from the 128's frequency, which in musical terms equates to about one semitone; only people with perfect pitch will be able to tell the difference, unless you have the two playing side by side. Secondly, a lot of AY tunes won't survive the transformation, because of the way they abuse the AY's envelope generator to generate triangular waves: for this to work, the musician has to match up the envelope pitches by ear, and if you adjust the tone table, there's no guarantee that you'll be able to readjust the envelopes to sound like the original again, let alone be able do it automatically. Having said that, if you were really intent on supporting the Quicksilva interface you *might* get acceptable results by halving all the timing values you send to the AY chip. No promises though!
Post Reply