How to get sound out of a moonsound?

By Creepy

Champion (335)

Creepy's picture

31-08-2021, 22:50

I'm trying to play a wave sample from ROM on the Moonsound but I cannot seem to get any sound out of it.

This is what I'm trying to do:
- Enable wave register of the moonsound
- set toneheader to 100b
- set a wave (tried a couply of different values 0,1,2 18, 300)
- set octave (1) and fnum (0)
- set volume
- key on
- key off

I've also tried it without the key off. But this gives me no sound at all. Is my logic correct or am I missing something?

; Init Moonsound
    ; Enable the wave register and extra status bit
    ld a, 5
    ld b, 3
    call MS_WRITE_FM2_REG

    ; Use instruments from ROM ??
    ld a, 2
    ld b, 00010000b
    call MS_WRITE_WAVE

; Set wave 300 on channel 0
    ld a, 20h
    ld b, 1
    call MS_WRITE_WAVE
    ld a, 08h
    ld b, 44
    call MS_WRITE_WAVE
    
 ; set octave 1, fnum 0 (aka 44.1Khz) + wave 1 on channel 1
    ld a,38h
    ld b, 00010000b  ; 4 bits octave, 4 bits fnum
    call MS_WRITE_WAVE

 ; Set volume
    ld a, 50h
    ld b, (2 << 1) | 1
    call MS_WRITE_WAVE  

 ; Key on (1), damp off (1) , reset LFO (1), output to channel (1), panning (4)
    ld a, 68h
    ld b, 10010000b;
    call MS_WRITE_WAVE

 ; Key off (1), damp off (1) , reset LFO (1), output to channel (1), panning (4)
    ld a, 68h
    ld b, 00010000b 
    call MS_WRITE_WAVE

    ret
Login or register to post comments

By Grauw

Ascended (10821)

Grauw's picture

31-08-2021, 23:27

You have to wait after setting the wave number, it takes a while (300 µs / 1074 cycles) for it to load. You can poll the status register LD flag which signals when it is done.

See the 08-37H WAVE TABLE NUMBER section of Yamaha’s documentation.

To do something useful with your cycles while waiting, you can set the wave numbers of other channels in the mean while, as well as channel parameters like volume and frequency (except for 80H-F7H). I would expect though that key on will not function until after the wave is loaded, although it is not mentioned explicitly.

By Creepy

Champion (335)

Creepy's picture

01-09-2021, 09:25

I've added the wait, and I can see in de debugger that for a couple of runs the LD flag is set so that seems to be working. But even after waiting until the LD flag is back to zero I still hear no sound at all. I am testing this in OpenMSX on the Boosted MSX 2 EN machine so no 7Mhz or Turbo R running which seem to need more waits added. I have no clue what could be the problem right now.

I've added the complete code here.

BDOS: EQU 0x05
_STROUT:	EQU 0x09

    org 100h
START:
    call MS_DETECT
    jp nz, detected
    ld de, TXT_MS_NOT_FOUND
    call PUTSTR
    ret

detected:
    ld de, TXT_MS_FOUND
    call PUTSTR

; Init Moonsound
    ; Enable the wave register and extra status bit
    ld a, 5
    ld b, 3
    call MS_WRITE_FM2_REG

    ; Use instruments from ROM ??
    ld a, 2
    ld b, 00010000b
    call MS_WRITE_WAVE

; Set wave 300 on channel 0
    ld a, 20h
    ld b, 1
    call MS_WRITE_WAVE
    ld a, 08h
    ld b, 44
    call MS_WRITE_WAVE
    call MS_WAIT_WAVE

 ; set octave 1, fnum 0 (aka 44.1Khz) + wave 1 on channel 0
    ld a,38h
    ld b, 00010000b  ; 4 bits octave, 4 bits fnum
    call MS_WRITE_WAVE
 ; Set volume
    ld a, 50h
    ld b, 33
    call MS_WRITE_WAVE  
 ; Key on (1), damp off (1) , reset LFO (1), output to channel (1), panning (4)
    ld a, 68h
    ld b, 10010000b;
    call MS_WRITE_WAVE
 ; Key off (1), damp off (1) , reset LFO (1), output to channel (1), panning (4)
    ;ld a, 68h
    ;ld b, 00010000b 
    ;call MS_WRITE_WAVE
    ret

; Moonsound routines etc
MS_WAVE_REG:        EQU 07Eh
MS_WAVE_DATA:       EQU 07Fh
MS_FM_BANK1_REG:    EQU 0C4h
MS_FM_DATA:         EQU 0C5h
MS_FM_BANK2_REG:    EQU 0C6h
MS_STATUS_REG:      EQU 0C4h

; Check if there is a Moonsound
; Returns:
;   NZ if detected
;   Z if not detected
MS_DETECT:
    in  a, (MS_STATUS_REG)
    cp 0xFF
    ret

; Initialize / reset the moonsound
MS_INIT:
    ; Enable the wave register and extra status bit
    ld a, 5
    ld b, 3
    call MS_WRITE_FM2_REG

    ; Use instruments from ROM ??
    ld a, 2
    ld b, 00010000b
    call MS_WRITE_WAVE

    ret

; Write to register in FM2
; Input:
;   A = Register
;   B = Value
MS_WRITE_FM2_REG
    out (MS_FM_BANK2_REG), a
    ld a,d  ; wait 56 / 33.87 µs
    ld a,b
    out (MS_FM_DATA), a
    ret

; Write to register in Wave
; Input:
;   A = Register
;   B = Value
MS_WRITE_WAVE
    out (MS_WAVE_REG), a
    cp (ix)  ; wait 88 / 33.87 µs (9.3 bus cycles, but 9 works)
    ld a,b
    out (MS_WAVE_DATA), a
    ret

MS_WAIT_WAVE:
    in  a, (MS_STATUS_REG)
    and 2
    jp nz, MS_WAIT_WAVE
    ret 

PUTSTR:
	PUSH AF
	PUSH BC
	PUSH DE
	PUSH HL
	LD C,_STROUT
	CALL BDOS
	POP HL
	POP DE
	POP BC
	POP AF
	RET

TXT_MS_FOUND:	db "Moonsound detected. Playing a sound?$"
TXT_MS_NOT_FOUND:	db "Moonsound not detected.$"

By Grauw

Ascended (10821)

Grauw's picture

01-09-2021, 09:36

Check your fnum calculation. Using fnum 0 is not correct, the playback will halt on the first sample.

Edit: never mind, fnum for PCM is different from fnum for FM....

By Creepy

Champion (335)

Creepy's picture

01-09-2021, 15:57

I found the problem. But now I'm not sure if its the documentation (opl4tech.txt) or openMSX being wrond here, my guess is the documentation.

+ 68H-7FH CH

  Output channel:            +-----+----+----+----+----+----+----+----+----+
                             |     | D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
  0: Outputs to MIX          | 68- +----+----+----+----+----+----+----+----+
                             | 7FH |              | CH |                   |
  1: Outputs to PCM-EXT      +-----+--------------+----+-------------------+

  For the MoonSound all channels need to be set to PCM-EXT.

It states that D4 needs to be 1 to set is to PCM-ext, and that for all channels this must be set. I changed this to zero with the code above and I got a sound.

So

; Key on (1), damp off (1) , reset LFO (1), output to channel (1), panning (4)
    ld a, 68h
    ld b, 10010000b;
    call MS_WRITE_WAVE

became

; Key on (1), damp off (1) , reset LFO (1), output to channel (1), panning (4)
    ld a, 68h
    ld b, 10000000b;
    call MS_WRITE_WAVE

By msd

Paragon (1532)

msd's picture

01-09-2021, 21:27

The OPL4 has 3 stereo output pins . FM, PCM and mix (PCM+MIX) . Only mix is connected to the dac

By msd

Paragon (1532)

msd's picture

01-09-2021, 21:57

Correction . MIX = PCM+FM