Short beeper routines in assembly

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
R-Tape
Site Admin
Posts: 276
Joined: Thu Nov 09, 2017 11:46 am

Short beeper routines in assembly

Post by R-Tape » Wed Nov 15, 2017 8:43 pm

I always end up using the same spot effects, usually nabbed from the DKTronics SoundFX tape. I know we've got BeepFX for the more elaborate ones, but I thought this would be a good place to share shortish beeper routines people can use.

Here's a simple zap.

Code: Select all

	;
zap:
	ld d,16		;speaker = bit 4
	ld e,0		;distance between speaker move counter
	ld b,255	;overall length counter
blp0:	ld a,d
	and 248		;keep border colour the same
	out (254),a	;move the speaker in or out depending on bit 4
	cpl		;toggle, so we alternative between speaker in and out to make sound
	ld d,a		;store it
	ld c,e		;now a pause
blp1:	dec c
	jr nz,blp1
	dec e		;change to inc e to reverse the sound, or remove to make it a note
	djnz blp0	;repeat B=255 times
	ret
	;
	
3 x

User avatar
utz
Berk
Posts: 15
Joined: Wed Nov 15, 2017 9:04 am
Contact:

Re: Short beeper routines in assembly

Post by utz » Wed Nov 15, 2017 9:08 pm

Wrote a complete beeper music player/sequencer in 64 byte earlier this year: url=https://github.com/utz82/ZX-Spectrum-1- ... /nanobeep2
In it's most basic setup, it has just two square wave channels, and 8-bit frequency resolution (approx. 2.5 octaves note range). The code is modular, so you can activate extra features like increased note range and PWM sweeps, at the cost of increasing the player size.

The player is open source and free for use by anyone, but there's no editor for the music available yet. There's a short demo tune included in the download package, if anybody is curious about how it sounds.
3 x

Ralf
Microbot
Posts: 137
Joined: Mon Nov 13, 2017 11:59 am
Location: Poland

Re: Short beeper routines in assembly

Post by Ralf » Wed Nov 15, 2017 9:15 pm

Utz, if I may ask I recall there was some forum about making music and effects on zx beeper - https://web.archive.org/web/20150423235 ... i-demo.pl/

Was it your forum? Do you know anything why it disappeared?
0 x

User avatar
utz
Berk
Posts: 15
Joined: Wed Nov 15, 2017 9:04 am
Contact:

Re: Short beeper routines in assembly

Post by utz » Wed Nov 15, 2017 9:19 pm

Some drama happened and the former admin shut down the place. :roll:
We've got a new place at http://www.randomflux.info/1bit/ nowadays.
2 x

User avatar
Morkin
Berk
Posts: 43
Joined: Mon Nov 13, 2017 8:50 am
Location: Bristol

Re: Short beeper routines in assembly

Post by Morkin » Wed Nov 15, 2017 10:40 pm

utz wrote:
Wed Nov 15, 2017 9:19 pm
Some drama happened and the former admin shut down the place. :roll:
We've got a new place at http://www.randomflux.info/1bit/ nowadays.
Ah cool, I wondered what happened to that site. I was just starting to learn the basics from scratch (the beeper, frequencies etc.) and then the site seemed to disappear - will have to get back to it..!

Here's a sound effect almost identical to the 'pick up object' sound I used in Catacombs of Balachor. It uses the ROM beeper, which I used at the time because I had no idea how to use OUT properly to make sound effects..!

Code: Select all

sound_setup:
	ld de, 5
	ld b,3
	ld hl, 938 	; pitch
sound_loop:
	dec h
	push bc
	push de
	push hl
	call 949 	; call ROM beeper routine
	pop hl
	pop de
	pop bc
	djnz sound_loop
	ret
I liked the way you can just keep mucking about with the registers (DE, HL, BC), and the INCs and DECs to get very different effects.
0 x

User avatar
utz
Berk
Posts: 15
Joined: Wed Nov 15, 2017 9:04 am
Contact:

Re: Short beeper routines in assembly

Post by utz » Thu Nov 16, 2017 11:05 pm

Morkin wrote:
Wed Nov 15, 2017 10:40 pm
I liked the way you can just keep mucking about with the registers (DE, HL, BC), and the INCs and DECs to get very different effects.
Yeah, that's actually one of the things that I really like about the beeper. You can just throw almost random code at it and it will often do something interesting.

Anyway, here's a simple laser effect in 13 bytes. By initializing D with different values, you can control the length. 0x28 is approximately one full zap.

Code: Select all

	di
	ld d,#28		;length/iterations
loop
	add hl,de
	dec de
	ld a,h
	out (#fe),a
	ld a,e
	or d
	jr nz,loop
	
	ei
	ret

Another nice thing to have is (pseudo-)white noise in all forms. My preferred method nowadays, provided that I have the cycles and registers available, is this poor man's XORshift PRNG:

Code: Select all

	di
;HL must be !0 aka set HL to 0 for silence
loop
	add hl,hl
	sbc a,a
	xor l
	ld l,a
	
	cp #10       ;cp n -  n = volume
	sbc a,a
	
	out (#fe),a
	jr loop
In this form, it will produce a pretty good rendition of "white noise" on the beeper. If you want to control the "pitch" of the noise, you can wrap the above in a prescaler, like so:

Code: Select all

	ld d,%01010101    ;prescaler mask
loop
	rlc d
	jr nc,skip
	add hl,hl
	...
skip	
	cp #10       ;cp n -  n = volume
	sbc a,a
	
	out (#fe),a
	jr loop
2 x

User avatar
R-Tape
Site Admin
Posts: 276
Joined: Thu Nov 09, 2017 11:46 am

Re: Short beeper routines in assembly

Post by R-Tape » Fri Dec 01, 2017 8:38 am

Has anyone got, or is anyone willing to do a beep routine that sounds vaguely like a paper page turning? No samples, just few hundred byte routine max.

Also preferably a routine using OUT rather than CALL 949.
0 x

User avatar
Ast A. Moore
Dizzy
Posts: 78
Joined: Mon Nov 13, 2017 3:16 pm

Re: Short beeper routines in assembly

Post by Ast A. Moore » Fri Dec 01, 2017 11:30 am

A page turning sound? Are you insane? Do you want us all get killed?
Image
0 x
Every man should plant a tree, build a house, and write a ZX Spectrum game.

Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.

HexTank
Berk
Posts: 12
Joined: Sun Nov 12, 2017 9:11 pm

Re: Short beeper routines in assembly

Post by HexTank » Fri Dec 01, 2017 12:10 pm

Well, it would inolve noise, so try a few RNGs with some adsr for amplitude.
0 x

User avatar
R-Tape
Site Admin
Posts: 276
Joined: Thu Nov 09, 2017 11:46 am

Re: Short beeper routines in assembly

Post by R-Tape » Fri Dec 01, 2017 3:44 pm

Too teary?

Code: Select all

org 32768
	ld hl,0
	ld c,255
lp1:	inc hl
	ld a,(hl)
	and 248
	or 7
	out (254),a
	ld b,c
lp0:	djnz lp0
	dec c
	jr nz,lp1
	ret
	;
	
Yep too teary.
0 x

Post Reply