home *** CD-ROM | disk | FTP | other *** search
/ Dave Lowe: Disk Number 4 / Lowe_DiskNumber4.img / SNES / SNES34.DOC < prev    next >
Encoding:
Text File  |  1993-12-07  |  17.4 KB  |  458 lines

  1. -------------------------------------------------------------------------------
  2. SUPER N.E.S. MUSIC DRIVER - (c) Martin Walker 1992/1993
  3. -------------------------------------------------------------------------------
  4.  
  5. UPDATED:
  6. 07/12/93 - Additional problem solving text
  7. 20/10/93 - TRK0call added to ReceiveStatus
  8. 21/09/93 - Info mode added for status/clock switching
  9. 14/09/93 - Interrupts disabled during Sound_Init/status bit added
  10. 26/08/93 - Mini SNES-CLR power-up routine added to Sound_Init
  11. 18/08/93 - Improved handshaking routines/ new clock routine
  12. 06/08/93 - Boot_APU anti hang-up mod added as per REV.D CAUTION #7
  13.            Updated Sound_Init/Send_Data
  14. 26/07/93 - external volume of sound calls/ problem solving section at end.
  15. 19/07/93 - to clarify initialisation procedure.
  16.  
  17.  
  18.  
  19.  
  20. SNES MUSIC DEMO
  21. ---------------
  22. To help test the music files, a file named 'SNESMUS.ROM' may be supplied.
  23. You can download this to the SNES for a demo of the music and effects.
  24. The joypad keys to use are as follows:
  25.  
  26. UP/DOWN          Increment/Decrement current call number.
  27. SELECT           Stop all sound calls.
  28. START            Start music/effect.
  29. Y                Fades all sound calls.
  30.  
  31.  
  32.  
  33.  
  34. OVERVIEW
  35. --------
  36. The player and music/effect data needs to be downloaded from its chosen ROM
  37. position to the SOUND CPU during the initialisation of the game. Suitable
  38. source code to do this appears below, along with the Sound Boot Loader routine
  39. taken from the SNES Manual (Appendix D with REV.D CAUTION #7).
  40.  
  41.  
  42.  
  43. 1. WHEN INITIALISING THE GAME USE 'Sound_Init'.
  44.  
  45. The Sound_Init routine needs to be called at power up and when 'RESET' is
  46. pressed. At both these times the SNES forces the Sound CPU to reset and then
  47. jump into its internal IPLROM routine. It is then waiting for data to be sent
  48. to it via the ports. First, a tiny routine is downloaded to clear all RAM, set
  49. up all DSP registers and initialise the Echo to minimise any stray sounds on
  50. power up. The driver is then downloaded; control then passes to the driver,
  51. and all subsequent downloads in this routine are made using the $82 DOWNLOAD
  52. command. (Sound_Init uses this technique to download the Musicdata and both
  53. sample Banks to the SOUND CPU memory).
  54.  
  55.  
  56.  
  57. 2. TO START ANY DESIRED MUSIC OR EFFECT USE 'Send_Sound'.
  58.  
  59. The following calls can be made during the game using Send_Sound. They are all
  60. made in the form:
  61.  
  62.                  lda      #N       ;N = sound call number
  63.                  ldx      #V       ;V = volume (use $FF=full volume normally)
  64.                  jsr      SendSound
  65.  
  66.  
  67.  
  68. N = $00-$7F      START
  69.                  Music or Effect call (See accompanying PROJECT.DOC)
  70.                  The 'X' register volume is only used by START calls.
  71.                  It may be omitted with other calls.
  72.  
  73. N = $80          INIT
  74.                  stops all current sounds.
  75.                  resets master volume to maximum.
  76.                  (Use after fade if you require effects only).
  77.  
  78. N = $81          FADE
  79.                  fades music and current effects to zero volume.
  80.  
  81. N = $82          DOWNLOAD
  82.                  Stops driver processing to download music data or samples.
  83.  
  84. N = $83          INFOMODE
  85.                  Sets mode to either  status byte (default) or realtime clock.
  86.  
  87.  
  88.  
  89. 3. FOR SUBSEQUENT LEVELS OF THE GAME USE 'Send_Data'.
  90.  
  91. Send_Data stops all sound processing and then allows additional Musicdata
  92. and/or sample Banks to be downloaded. Normally both a Musicdata (.SND) file
  93. and at least 1 sample Bank (.BIN) file will need to be sent. Full details of
  94. particular files will be found in the file 'PROJECT.DOC'.
  95.  
  96.  
  97.  
  98.  
  99. -------------------------------------------------------------------------------
  100.  SOURCE CODE DETAILS
  101. -------------------------------------------------------------------------------
  102.  
  103. Since the SOUND CPU in the SNES is a completely separate processor, sending
  104. commands to it relies either on the SNES code looping until the sound code
  105. calls its 'check for external commands' routine (this can waste a lot of time
  106. that could be more usefully spent on the SNES side), or the way implemented
  107. in 'Send_Sound' below. This relies on the fact that once an external command
  108. from the SNES side has been processed by the music driver, it writes a unique
  109. number back to PORT0. By reading PORT0 from the SNES side and checking for
  110. this value the SNES programmer will only send a sound call when the sound
  111. driver has finished processing the previous one. By placing all sound calls
  112. into a buffer (16 bytes should normally be sufficient), you can call
  113. 'SendSound' at intervals without waiting each time for a response. The worst
  114. case wait will be 8mS (hardware loop time). Normally 1 sound call per VBL
  115. (20mS) should be fast enough (50 new sounds maximum per second!).
  116.  
  117. KEY:         < = byte (8 bit value)
  118.              | = word (16 bit value)
  119.              >  = long word (24 bit value)
  120.              >> = bank (top 8 bits of 24 bit value)
  121.  
  122.  
  123. ;Macro Definitions
  124.  
  125. SET16            MACRO
  126.                  rep      %00110000         ;Enable 16-bit mode ie clear M&X
  127.                  ENDM
  128.  
  129. CLR16            MACRO
  130.                  sep      %00110000         ;Disable 16-bit mode ie set M&X
  131.                  ENDM
  132.  
  133. SETLONGA         MACRO
  134.                  rep      %100000           ;16 Bit Accumulator
  135.                  ENDM
  136.  
  137. CLRLONGA         MACRO
  138.                  sep      %100000           ;8 bit Accumulator
  139.                  ENDM
  140.  
  141.  
  142. ;EQUATES
  143.  
  144. APU_PORT0        equ      $2140
  145. APU_PORT1        equ      $2141
  146. APU_PORT2        equ      $2142
  147. APU_PORT3        equ      $2143
  148.  
  149. SonyAddr         equ      $10      ;any 4 byte zero page address
  150. callnum          equ      $14      ;any 1 byte address
  151. SONYclock        equ      $15      ;word value
  152. SONYstatus       equ      $17      ;8 bits (1 = chan active/0 = chan stopped)
  153. SONYtrk0call     equ      $18      ;byte value (FF = chan stopped)
  154.  
  155.  
  156.  
  157. SendSound:       CLR16
  158.                  pha                        ;save call number
  159.                  lda      >APU_PORT0        ;latest value sent by SOUND CPU
  160.                  cmp      <callnum
  161.                  bne      SoundAbort        ;previous call still not finished
  162.                  pla
  163.                  sta      >APU_PORT0        ;store new call
  164.                  txa
  165.                  sta      >APU_PORT1        ;store new volume
  166.                  inc      <callnum
  167.                  lda      <callnum
  168.                  sta      >APU_PORT3        ;confirms a new call
  169.                  rts
  170. SoundAbort:      pla
  171.                  rts
  172.  
  173.  
  174. -------------------------------------------------------------------------------
  175.  Sound_Init should be called as a subroutine once only during the
  176.  Initialisation of the game, or after a user reset. It downloads the driver,
  177.  music data  and sample data to the SOUND CPU memory.
  178. -------------------------------------------------------------------------------
  179.  
  180. Sound_Init:      php
  181.                  sei                        ;disable any other interrupts
  182.                  SET16
  183.                  lda      #MusicInit        ;download SNES_CLR.BIN (24 bit address)
  184.                  sta      <SonyAddr                                                                                 lda      #RamII
  185.                  lda      #MusicInit>>16
  186.                  sta      <SonyAddr+2
  187.                  jsr      Boot_APU          ;exits straight to IPLROM
  188.  
  189.                  SET16
  190.                  lda      #MusicDriver      ;download driver (24 bit address)
  191.                  sta      <SonyAddr                                                                                 lda      #RamII
  192.                  lda      #MusicDriver>>16
  193.                  sta      <SonyAddr+2
  194.                  jsr      SONY_send
  195.  
  196.                  lda      #$82              ;DOWNLOAD data (music)
  197.                  sta      >APU_PORT0
  198.                  SET16
  199.                  lda      #Musicdata
  200.                  sta      <SonyAddr
  201.                  lda      #Musicdata>>16
  202.                  sta      <SonyAddr+2
  203.                  jsr      SONY_send
  204.  
  205.                  lda      #$82              ;DOWNLOAD data (samples)
  206.                  sta      >APU_PORT0
  207.                  SET16
  208.                  lda      #Samples1
  209.                  sta      <SonyAddr
  210.                  lda      #Samples1>>16
  211.                  sta      <SonyAddr+2
  212.                  jsr      SONY_send
  213.  
  214.                  lda      #$82              ;DOWNLOAD data (samples)
  215.                  sta      >APU_PORT0
  216.                  SET16
  217.                  lda      #Samples2
  218.                  sta      <SonyAddr
  219.                  lda      #Samples2>>16
  220.                  sta      <SonyAddr+2
  221.                  jsr      SONY_send
  222.  
  223.                  sta      >APU_PORT3
  224.                  plp                        ;restore any interrupts
  225.                  rts
  226.  
  227.  
  228. ;-----------------------------------------------------------------------------
  229. SONY_send:       jsr      Boot_APU
  230.                  CLR16
  231. @wait0:          lda      >APU_PORT0        ;wait for handshake
  232.                  bne      @wait0
  233.                  dec      a
  234.                  sta      >APU_PORT2        ;acknowledge handshake
  235.                  sta      <callnum          ;init call number to $FF
  236.                  rts
  237.  
  238.  
  239. -------------------------------------------------------------------------------
  240.  Once the Sound_Init routine has been called, the following routine can be
  241.  used to download additional music data (or sample data with '#Musicdata'
  242.  replaced by '#Samples') which is required for later levels of the game.
  243. -------------------------------------------------------------------------------
  244.  
  245. Send_Data:       CLR16
  246.                  php
  247.                  sei                        ;disable any other interrupts
  248. S_D0:            lda      >APU_PORT0        ;latest value sent by SOUND CPU
  249.                  cmp      <callnum
  250.                  bne      S_D0              ;previous call still not finished
  251.  
  252.                  lda      #$82              ;DOWNLOAD data (music)
  253.                  sta      >APU_PORT0
  254.                  inc      <callnum
  255.                  lda      <callnum
  256.                  sta      >APU_PORT3        ;forces a new call
  257.                  SET16
  258.                  lda      #Musicdata
  259.                  sta      <SonyAddr
  260.                  lda      #Musicdata>>16
  261.                  sta      <SonyAddr+2
  262.                  jsr      SONY_send
  263.  
  264.                  sta      >APU_PORT3
  265.                  plp                        ;restore any interrupts
  266.                  rts
  267.  
  268.  
  269. -------------------------------------------------------------------------------
  270. Info_Mode is used to set the mode. It only needs to be called once with the
  271. appropriate mode number in PORT1. After this the appropriate routine (either
  272. ReceiveStatus or ReceiveClock can be called at any time for the latest values.
  273. Normally, after any download the info mode defaults to 0 (status)
  274.  
  275. MODE = 0 (STATUS=default)
  276. In this mode PORT1 is updated with current status of all 8 channels (bit high
  277. = channel active). This can be used to check for the end of a particular sound.
  278. PORT2 is updated with the current call in channel 0, and can be used to
  279. monitor the progress of specially written music ($FF = no call active)
  280.  
  281. MODE=1 (CLOCK)
  282. In this mode PORT1 & PORT2 are updated with the current value of an internal
  283. music 'CLOCK' to allow syncing of action to music with specially designed
  284. tracks.
  285. -------------------------------------------------------------------------------
  286.  
  287. Info_Mode:       CLR16
  288.                  php
  289.                  sei                        ;disable any other interrupts
  290. I_M0:            lda      >APU_PORT0        ;latest value sent by SOUND CPU
  291.                  cmp      <callnum
  292.                  bne      I_M0              ;previous call still not finished
  293.  
  294.                  lda      #$83              ;INFOMODE
  295.                  sta      >APU_PORT0
  296.                  lda      #0                ;0=status(default)
  297.                  sta      >APU_PORT1        ;1=clock mode
  298.                  inc      <callnum
  299.                  lda      <callnum
  300.                  sta      >APU_PORT3        ;forces a new call
  301.                  plp                        ;restore any interrupts
  302.                  rts
  303.  
  304.  
  305. ReceiveStatus:   SETLONGA
  306.                  lda      >APU_PORT1        ;16 bit (get data word)
  307.                  sta      <SONYstatus       ;8 bits of channel status
  308.                  CLRLONGA                   ;1 byte TRK0call
  309.                  rts
  310.  
  311.  
  312. ReceiveClock:    SETLONGA                   ;clock is waiting in PORT1 & 2
  313.                  lda      >APU_PORT1        ;16 bit (get data word)
  314.                  sta      <SONYclock
  315.                  CLRLONGA
  316.                  rts
  317.  
  318.  
  319. -------------------------------------------------------------------------------
  320.  SOUND BOOT LOADER V1.1 (from Appendix D of the SNES manual)
  321. -------------------------------------------------------------------------------
  322.  
  323. Boot_APU:        php
  324.                  SET16
  325.                  ldy      #|$0000
  326.                  lda      #$bbaa
  327. boot_initial:    cmp      >APU_PORT0
  328.                  bne      boot_initial
  329.                  CLRLONGA
  330.                  lda      #$cc
  331.                  bra      boot_entry1
  332.  
  333. boot_repeat:     lda      [SonyAddr],y
  334.                  iny
  335.                  xba
  336.                  lda      #0
  337.                  bra      boot_entry2
  338. boot_loop:       xba
  339.                  lda      [SonyAddr],y
  340.                  iny
  341.                  xba
  342. boot_wait1:      cmp      >APU_PORT0
  343.                  bne      boot_wait1
  344.                  inc      a
  345. boot_entry2:     SETLONGA
  346.                  sta      >APU_PORT0
  347.                  CLRLONGA
  348.                  dex
  349.                  bne      boot_loop
  350. boot_wait2:      cmp      >APU_PORT0
  351.                  bne      boot_wait2
  352. boot_zero:       adc      #3
  353.                  beq      boot_zero
  354. boot_entry1:     pha
  355.                  SETLONGA
  356.                  lda      [SonyAddr],y
  357.                  iny
  358.                  iny
  359.                  tax
  360.                  lda      [SonyAddr],y
  361.                  iny
  362.                  iny
  363.                  sta      >APU_PORT2
  364.                  CLRLONGA
  365.                  cpx      #|1
  366.                  lda      #0
  367.                  rol      a
  368.                  sta      >APU_PORT1
  369.                  adc      #$7f
  370.                  pla
  371.                  sta      >APU_PORT0
  372. boot_wait3:      cmp      >APU_PORT0
  373.                  bne      boot_wait3
  374.                  bvs      boot_repeat
  375. boot_ret:        plp
  376.                  rts
  377.  
  378.  
  379.  
  380.  
  381. ------------------------------------------------------------------------------
  382. SNES MUSIC PROBLEM SOLVING
  383. ------------------------------------------------------------------------------
  384. PROBLEM 1:
  385. Music crashes after using the 'reset' button
  386.  
  387. REASON:
  388. At this time the SNES hardware forces the Sound CPU to reset and then jump
  389. into its internal IPLROM routine. It is then waiting for data to be sent to it
  390. via the ports, using the Boot_APU routine (above) which is reprinted from the
  391. SNES manual. Although on 'RESET' the sound driver and data are still in sound
  392. memory, you MUST treat this button the same as a power up and use 'Sound_Init'
  393.  
  394. CURE:
  395. ALWAYS use 'Sound_Init' after a 'RESET', rather then Send_Data.
  396.  
  397.  
  398.  
  399. PROBLEM 2:
  400. Corruption of sample data after loading new BANK1.
  401.  
  402. REASON:
  403. If after loading a new bank of sounds you get some noises garbled, it is
  404. possible that the musician has inadvertantly allowed BANK2 to overwrite the
  405. end of BANK1 data by a few bytes. Normally a BANK1 load followed by BANK2 will
  406. ensure that the sample pointer table at the beginning of each bank is
  407. preserved, and a tiny corruption at the end of the final sample in BANK1 may
  408. be inaudible, but loading a new BANK1 may overwrite the table at the beginning
  409. of BANK2. This will cause every sound in BANK2 to emerge as gibberish. You can
  410. check for this by re-sending the banks in the opposite order. If the problem
  411. disappears, contact the musician for a BANK update file!
  412.  
  413. CURE:
  414. Send BANKS in the order BANK1 then BANK2 for a temporary cure, and contact the
  415. musician for an amended BANK file.
  416.  
  417.  
  418.  
  419. PROBLEM 3:
  420. Occasional hang-up after sending new sound data.
  421.  
  422. REASON:
  423. Bug in Nintendo's own Boot_APU routine (see REV.D CAUTION #7). It is possible
  424. that an interrupt will strike during a download of sound data - if this
  425. happens during the 300-400 microseconds of handshake it is possible that the
  426. SNES side will miss the ackowledgement and then wait for indefinitely (crash).
  427.  
  428. CURE:
  429. Make sure you disable SNES interrupts during Sound_Init as shown.
  430.  
  431.  
  432.  
  433. PROBLEM 4:
  434. Sound always hangs when game is aborted.
  435.  
  436. REASON:
  437. Only on power up or by pressing RESET will the Sound CPU be in a state to use
  438. Sound_Init. Restarting the game as a user option will not work in this case.
  439.  
  440. CURE:
  441. Re-arrange the code so that Sound_Init occurs before the game restart
  442. point, and use Send_Data to re-send the first music data required.
  443.  
  444.  
  445.  
  446. PROBLEM 5:
  447. Occasional crackle or 'break-up' in sound with lots of simultaneous sounds.
  448.  
  449. REASON:
  450. It is possible to overload the sound chip, but sometimes this only appears
  451. during playtesting when everyting is happening at once.
  452.  
  453. CURE:
  454. First confirm that it is overload by inserting a temporary '/2' command before
  455. the volume setting in SendSound to drastically reduce all volumes. If this
  456. cures the problem then remove the '/2' command and set all volumes to a
  457. significantly lower setting (e.g. from $FF to $D0) and retest.
  458.