caudioc64

How to include a SID-File into a cc65 program on a C64?


I want to include and play .sid files (music for C64 chiptunes) in a cc65 program. Usually sid-files contain a play routine that starts at $1000, how do I link this to my cc65-program? At the moment I compile my code with cc65 using this command:

cl65 -O -o C64test.prg -t c64 C64test.c

Solution

  • I found a solution:

    1. Create an .asm file which generates the following code:

      .export _setupAndStartPlayer
      
      sid_init = $2000
      sid_play = $2003
      siddata = $2000
      
      .segment "CODE"
      
      .proc _setupAndStartPlayer: near
              lda #$00     ; select first tune
              jsr sid_init ; init music
              ; now set the new interrupt pointer
              sei
              lda #<_interrupt ; point IRQ Vector to our custom irq routine
              ldx #>_interrupt
              sta $314 ; store in $314/$315
              stx $315
      
              cli ; clear interrupt disable flag
              rts     
      .endproc        
      
      .proc _interrupt
              jsr sid_play
              ;dec 53280 ; flash border to see we are live
              jmp $EA31 ; do the normal interrupt service routine
      .endproc
      
    2. Call the asm function from C:

      #include <stdio.h>
      #include <stdlib.h>
      #include <conio.h>
      #include <c64.h>
      
      extern int setupAndStartPlayer();
      
      int main(void) {
              printf("Setting up player\n");
              setupAndStartPlayer();
              return 0;
      }
      
    3. Compile both files using the standard cc65 Makefile, this gives you a .c64 file with your code, but without the SID data

    4. Relocate the SID file using sidreloc (the option -p defines the new start page, in this case 20 means $2000)

      ./sidreloc -r 10-1f -p 20 sidfile.sid sidfile2000.sid
      
    5. Convert the SID file to C64 .prg using psid64:

      psid –n sidfile2000.sid
      
    6. Link the file sidfile2000.prg together with the compiled C program using exomizer (the number 2061 is the start address of the program, 2061 is the default for cc65):

      exomizer sfx 2061 music.c64 sidfile2000.prg -o final.prg