Short beeper routines in assembly

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
djnzx48
Manic Miner
Posts: 730
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Short beeper routines in assembly

Post by djnzx48 »

Interesting that reducing the delays helped. I tried to keep the time between OUTs a constant 56 T-states to ensure regular output - 56 is also a multiple of 8 to avoid IO contention. I assume that more irregular delays would reduce the harmonics but in turn raise the noise floor.

I think the problem may be due to how the OUTs are performed in groups of 16, so for values that don't divide 16 the resulting pulse wave is irregular. I don't know for sure though.
ixn
Drutt
Posts: 4
Joined: Thu Feb 22, 2018 4:44 am

Re: Short beeper routines in assembly

Post by ixn »

If you put this after the two ld a, r instructions in the code posted by djnzx48, you can get some arcing power lines in the background:

Code: Select all

        inc ix
        ld a, (ix)
        out ($fe), a
ixn
Drutt
Posts: 4
Joined: Thu Feb 22, 2018 4:44 am

Re: Short beeper routines in assembly

Post by ixn »

You can get another "interesting" sound by also sticking

Code: Select all

        inc iy
        ld c, (iy)
at the start of pwm_loop.

It sounds good assembled at about 50000.
User avatar
djnzx48
Manic Miner
Posts: 730
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Short beeper routines in assembly

Post by djnzx48 »

Any better?

Code: Select all

org 32768
main:
        di

        ld hl, 0
        ld (volume+1), hl

        ld hl, 0
        ld (time+1), hl

        ld hl, 0xC0DE

        ld de, 0

        ld b, 16

        xor a
        ld c, a

        ld ixl, %00101101

        jp mainloop_init

        ; ---------------------

        ; runtime = 10 * 56 + 51 = 611
pwm_loop:                       ;    /  37
        xor d                   ;  4 /  41 [/  97]
        add a, e                ;  4 /  45 [/ 101]
        ld d, a                 ;  4 /  49 [/ 105]

        and 16                  ;  7 /  56 [/ 112]

        out ($fe), a            ; 11 /  67 [ 56]

        jp pwm_delay            ; 10 /  77

pwm_delay:
        dec c                   ;  4 /  81
        jr nz, pwm_loop         ; 12 /  93 [taken]

                                ;611 / 648

        ; ---------------------

        xor d                   ;  4 / 652
        add a, e                ;  4 / 656
        ld d, a                 ;  4 / 660

        and 16                  ;  7 / 667

        ret c                   ;  5 / 672

        ; ---------------------

        out ($fe), a            ; 11 / 683      ; OUT 12

        xor d                   ;  4 / 687
        add a, e                ;  4 / 691
        ld d, a                 ;  4 / 695

        and b                   ;  4 / 699      ; OUT 13 value
        ld c, a                 ;  4 / 703
        xor d                   ;  4 / 707

        add a, e                ;  4 / 711
        ld d, a                 ;  4 / 715

        ld a, r                 ;  9 / 724

        ld a, c                 ;  4 / 728      ; OUT 13 value

        ; ---------------------

        out ($fe), a            ; 11 / 739      ; OUT 13

        ld a, d                 ;  4 / 743

        and b                   ;  4 / 747      ; OUT 14 value
        ld c, a                 ;  4 / 751
        xor d                   ;  4 / 755

        add a, e                ;  4 / 759
        ld d, a                 ;  4 / 763

        and b                   ;  4 / 767      ; OUT 15 value
        ld e, a                 ;  4 / 771

        ld a, r                 ;  9 / 780

        ld a, c                 ;  4 / 784      ; OUT 14 value

        out ($fe), a            ; 11 / 795 [784] [next 840]

        ld a, e                 ;  4 / 799
        xor d                   ;  4 / 803

        ; 2 spare T-states!

mainloop_init:
        add hl, hl              ; 11 / 816
        jr nc, no_carry         ;  7 / 823 [828 taken]

        ld (volume+2), a        ; 13 / 836

mainloop:
        ld a, e                 ;  4 / 840
        out ($fe), a            ; 11 / 851 [840] [next 896]

        ld a, l                 ;  4 / 855
        xor ixl                 ;  8 / 863
        ld l, a                 ;  4 / 867

        ; OUTs:
        ;
        ;  0      0
        ;  1     56
        ;  2    112
        ;  3    168
        ;  4    224
        ;  5    280
        ;  6    336
        ;  7    392
        ;  8    448
        ;  9    504
        ; 10    560
        ; 11    616
        ; 12    672
        ; 13    728
        ; 14    784
        ; 15    840
        ; ---------
        ; 16    896

out_loop:

volume:
        ld de, 0                ; 10 / 877

        ; ---------------------

        ld a, d                 ;  4 / 881
        add a, e                ;  4 / 885
        ld d, a                 ;  4 / 889

        and 16                  ;  7 / 896

        ; OUT 0
        out ($fe), a            ; 11 /  11 [896] [0]

        ld a, r                 ;  9 /  20
        ld c, 11                ;  7 /  27

        jp pwm_loop             ; 10 /  37

; ============================================

no_carry:                       ;    / 828

        nop                     ;  4 / 832
        ex af, af'              ;  4 / 836

        ld a, e                 ;  4 / 840
        out ($fe), a            ; 11 / 851 [840] [next 896]

        ex af, af'              ;  4 / 855
        ld (volume+2), a        ; 13 / 868

        ld a, 0                 ;  7 / 875
        ld a, 0                 ;  7 / 882
        ld a, 0                 ;  7 / 889

        ld a, 0                 ;  7 / 896
        out ($fe), a            ; 11 /  11 [896] [0]

        ld c, a                 ;  4 /  15

        ; ---------------------

time:
        ld de, 0                ; 10 /  25

        inc de                  ;  6 /  31
        ld (time+1), de         ; 20 /  51

        ld a, d                 ;  4 /  55
        and 15                  ;  7 /  62

        bit 4, d                ;  8 /  70
        jr nz, reverse_wave     ;  7 /  77 [82 taken]

        ld de, 0                ; 10 /  87
        jp continue_wave        ; 10 /  97

reverse_wave:

        sub 15                  ;  7 /  89
        neg                     ;  8 /  97

continue_wave:

        inc a                   ;  4 / 101
        ld (volume+1), a        ; 13 / 114

        ; ---------------------

        ld a, 41                ;  7 / 121

        ; total time = 40 * (4 + 12) + (4 + 7) = 651
delay_loop:                     ;651 / 772
        dec a
        jr nz, delay_loop

        nop                     ;  4 / 776

        xor a                   ;  4 / 780
        ld e, a                 ;  4 / 784

        out ($fe), a            ; 11 / 795 [784] [next 840]

        jp extra_delay          ; 19 / 805

extra_delay:
        add hl, hl              ; 11 / 816
        jr nc, no_carry         ;  7 / 823 [828 taken]

        jp mainloop             ; 10 / 836
User avatar
R-Tape
Site Admin
Posts: 6402
Joined: Thu Nov 09, 2017 11:46 am

Re: Short beeper routines in assembly

Post by R-Tape »

djnzx48 wrote: Wed Nov 06, 2019 8:52 pm Any better?
It's incredible. And 176 bytes. Hell.

I see the code is very different. There is the slightest memory of the ghost sounds from before, but they wouldn't even be noticed (and even if they were, they could be taken as soothing) if this was the first iteration. Do I take it that you understand what was causing the spikes previously?

How do you do it?!
User avatar
djnzx48
Manic Miner
Posts: 730
Joined: Wed Dec 06, 2017 2:13 am
Location: New Zealand

Re: Short beeper routines in assembly

Post by djnzx48 »

No idea. All I did was replace the table I was using previously with dynamically calculated values.
User avatar
R-Tape
Site Admin
Posts: 6402
Joined: Thu Nov 09, 2017 11:46 am

Re: Short beeper routines in assembly

Post by R-Tape »

djnzx48 wrote: Wed Nov 06, 2019 10:01 pm No idea.
Slightly reassuring!
All I did was replace the table I was using previously with dynamically calculated values.
I haven't pulled it apart yet, but it looks like there's extra code too. Anyway, it's a brilliant piece of work.
catmeows
Manic Miner
Posts: 716
Joined: Tue May 28, 2019 12:02 pm
Location: Prague

Re: Short beeper routines in assembly

Post by catmeows »

Hi. By any chance, does anyone have idea how to produce a metal (ish) sound on beeper ? Something like when two swords clash each other. I cannot afford sample due memory limits.
Proud owner of Didaktik M
User avatar
Morkin
Bugaboo
Posts: 3266
Joined: Mon Nov 13, 2017 8:50 am
Location: Bristol, UK

Re: Short beeper routines in assembly

Post by Morkin »

catmeows wrote: Mon Nov 18, 2019 8:07 am Hi. By any chance, does anyone have idea how to produce a metal (ish) sound on beeper ? Something like when two swords clash each other. I cannot afford sample due memory limits.
I don't have a routine, but the helmet sound when you die in Firelord might be a good one to steal borrow...
My Speccy site: thirdharmoniser.com
catmeows
Manic Miner
Posts: 716
Joined: Tue May 28, 2019 12:02 pm
Location: Prague

Re: Short beeper routines in assembly

Post by catmeows »

Morkin wrote: Mon Nov 18, 2019 6:34 pm
catmeows wrote: Mon Nov 18, 2019 8:07 am Hi. By any chance, does anyone have idea how to produce a metal (ish) sound on beeper ? Something like when two swords clash each other. I cannot afford sample due memory limits.
I don't have a routine, but the helmet sound when you die in Firelord might be a good one to steal borrow...
I know what you mean but I failed (twice) to extract the routine. I may give it another try.
Proud owner of Didaktik M
jetsetdanny
Dizzy
Posts: 85
Joined: Thu May 02, 2019 10:22 pm
Contact:

Re: Short beeper routines in assembly

Post by jetsetdanny »

utz wrote: 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

Hello,

I have used the "simple laser effect in 13 bytes" in a project related to a Jet Set Willy remake (to be released in early March if all goes well).

I want to give due credit in the Readme.

At the moment I can credit it as created by "utz". Utz, would you like to be credited in any other way? (like by your real name, for example). Please let me know :).
Website: JSW Central
User avatar
utz
Microbot
Posts: 116
Joined: Wed Nov 15, 2017 9:04 am
Contact:

Re: Short beeper routines in assembly

Post by utz »

Very kind of you wanting to credit me for this ;) Just "utz" is fine, or if you like you could throw in a link to my website, https://irrlichtproject.de.
jetsetdanny
Dizzy
Posts: 85
Joined: Thu May 02, 2019 10:22 pm
Contact:

Re: Short beeper routines in assembly

Post by jetsetdanny »

Thank you :). I will do as you suggest. When the game is released a month from now (if all goes well), apart from an announcement in the proper place on this forum I will also post a message here to give you the link so that you can see your code in action.
Website: JSW Central
jetsetdanny
Dizzy
Posts: 85
Joined: Thu May 02, 2019 10:22 pm
Contact:

Re: Short beeper routines in assembly

Post by jetsetdanny »

And so here it is, the game in which utz's "laser effect" routine has been used: "Madam Blavskja's Carnival Macabre 48K", a (slightly enhanced) port of Fabián Álvarez López (Adban de Corcy's) classic 128K Jet Set Willy game to 48K.

Utz is duly credited in Section II "Acknowledgements and thanks" of the Readme included in the ZIP file with said game. The laser effect, which is used in the game twice (in different ways), is then dissassembled (with a link to this thread) in Subsection 3 ("Playing the original game or with extra rooms") of Section X "Technical notes" of the Readme, and again in Subsection 6 ("Custom-made title screen") of Section X of the Readme. It is also mentioned in Subsection 19 ("Memory layout") of Section X, where it is indicated where the code which operates it resides in memory.

The ZIP with the game file (if you want to see the laser effect in action, besides discovering various other facets of this atmospheric JSW game, or if you want to have a look at its references in the Readme) can be downloaded freely e.g. from here.
Website: JSW Central
User avatar
R-Tape
Site Admin
Posts: 6402
Joined: Thu Nov 09, 2017 11:46 am

Re: Short beeper routines in assembly

Post by R-Tape »

Can anyone help with a short bit of asm code that sounds vaguely like the whistling sound of something falling?

It needs to be less than a frame, and perhaps can be CALLed with a different value so that the whistle can change in tone (or should that be pitch?) as the ground approaches.
User avatar
Joefish
Rick Dangerous
Posts: 2058
Joined: Tue Nov 14, 2017 10:26 am

Re: Short beeper routines in assembly

Post by Joefish »

Incidentally, for opening the treasure chests in Waterbelle End I wrote a sound routine that takes a delay, then reads a byte from ROM, does an AND 16, and uses that in an XOR to swap the speaker bit. So it's kind of a random element as to whether the speaker bit flips on every cycle of the beeper function. Vary the length of the delay loop, and it sounds very much like the ch-Ch-CH sound from Transformers, so I guess that's how that was done.

Whereas in Buzzsaw+ I randomly toggled bits of a steady high/low pattern; this gave a combination of noise and whining tone which sounds more like noisy machinery.

But the former is a more pleasant sound and probably better suited to simulating more natural noise effects. Unfortunately the one thing the beeper doesn't have is any sort of volume control. I know you can simulate it with very high speed pulsing but that can add a high pitch overtone if you're not fast enough so I haven't got the hang of that yet.

What I could do with for Waterbelle End is a more convincing 'fart' effect. You'd think that'd be an easy one for the Speccy, given its reputation!
Post Reply