PRG Creator

I started to learn assembler for the C64. Assembler for the 650x CPUs is easier to
learn than assembler for the x86 CPUs. They have less registers and instructions. I wouldn’t learn assembler to use it on the PC, that would be a waste of time, but I ordered a video game console and assembler on small systems is a good choice.

Most cross-assemblers just produce a memory dump of the compiled source, so I wrote this conversion programm. Inserting the two bytes everytime before the start of the programm was just too tedious :) .


#include <iostream>
#include <cstdlib>
#include <fcntl.h>
using namespace std;

/*******************************************
 *This programm converts the output file of* 
 *a 650x (cross) assembler                 *
 *to a PRG file suitable for C64 emulators *
 *******************************************/
int main(int argc, char *argv[])
{
  if (argc!=2){
    cout<<argv[0]<<" FILE"<<endl;
    exit(1);
  }
  FILE * pFile;
  pFile = fopen (argv[1],"rb");
  if (pFile!=NULL){
   long lSize;
   char * buffer;
   // obtain file size.
   fseek (pFile , 0 , SEEK_END);
   lSize = ftell (pFile);
   rewind (pFile);
   // allocate memory to contain the whole file.
   buffer = (char*) malloc (lSize+2);
   if (buffer == NULL) exit (2);
   // copy the file into the buffer.
   fread (buffer+2,1,lSize,pFile);
   fclose (pFile);
   char start[2];
   char end[2];
   start[0]=buffer[2];
   start[1]=buffer[3];
   end[0]=buffer[lSize];
   end[1]=buffer[lSize+1];
   if(end[0]&start[0]==end[0]&&end[1]&start[1]==end[1]){
    cout<<"File is already in PRG format."<<endl;
    exit(1);
   }
   buffer[0]=end[0];
   buffer[1]=end[1];
   pFile = fopen (argv[1] , "wb");
   fwrite(buffer,1,lSize+2,pFile);
 }
 return EXIT_SUCCESS;
}