PSG music to FM

Por NYYRIKKI

Enlighted (5688)

Imagen del NYYRIKKI

17-01-2021, 18:44

I needed a small size routine to convert PSG frequencies to MSX-MUSIC, but I couldn't find any ready made. After some iterations I came up with this horrific piece of assembly nightmare... I wanted to share it, because I must say I feel pretty proud of how small I was able to squeeze it in the end. Smile

PSG2FM:
        ; PSG to MSX-MUSIC frequency converter
        ; Made By: NYYRIKKI 2021
        ;
        ; IN:  DE  = MSX PSG channel frequency (12bit)
        ; OUT: B:A = MSX-MUSIC 3bit floating point octave(*512) + 9bit F-number mantissa
        ; Changes: AF,BC,HL

        XOR A
	LD H,A
        LD L,9
	LD B,16
        LD C,41
	SBC HL,DE
	JR NC,$+4
	ADD HL,DE
	SCF
        CCF
	ADC A,A
	JR C,$+4
        DJNZ $-11
        INC B
        RL B
        RET
Login sesión o register para postear comentarios

Por Grauw

Ascended (9568)

Imagen del Grauw

17-01-2021, 19:58

Nice! Crafty use of jumping in the middle of the LD C,41 Smile.

(Spoilers: Op-code 41 (29H) is ADD HL,HL. And the whole is a division routine.)

Por NYYRIKKI

Enlighted (5688)

Imagen del NYYRIKKI

17-01-2021, 22:20

Yes... Practically the trick is 589824/DE... (stripped down from 3579545/DE*32*2^18/3579545/72)

Por NYYRIKKI

Enlighted (5688)

Imagen del NYYRIKKI

18-01-2021, 08:19

The last part of the trick (and why this problem can be seen as special case of conditionally terminated 16bit divide) is based on two observations: 589824 happens to be very conveniently same as 9*2^16 and when DE < 10 it is such a high note that it is out of the scale anyway. This allows me to preload the number 9 as reminder and generate rest of the number out of thin air along the way.