/* bin2mon.c
 * binary file to Wozmon file by Andre Adrian, DL1ADR
 * based on bintomon.c from Dave Schmenk, see
 * https://github.com/dschmenk/apple2pi/blob/master/share/bintomon.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main() {
	printf("Enter Address Filename(without extension) e.g. '7F00 memfill':\n");

	unsigned int start_addr;
	char fname[256] = "", ifname[256], ofname[256];
	scanf("%x %s", &start_addr, fname);

	strcpy(ifname, fname);
	strcat(ifname, ".bin");
	strcpy(ofname, fname);
	strcat(ofname, ".mon");

//	printf("ifname %s\n", ifname);
//	printf("ofname %s\n", ofname);
    getchar();

	FILE *fpin = fopen(ifname, "rb");
	if (NULL == fpin) {
 		fprintf(stderr, "Can't open input file '%s'. Press RETURN for exit.\n", ifname);
 		getchar();
		exit(1);
	}

	FILE *fpout = fopen(ofname, "w");
	if (NULL == fpout) {
 		fprintf(stderr, "Can't open output file '%s'. Press RETURN for exit.\n", ofname);
 		getchar();
		exit(1);
	}

    fprintf(fpout, "%04X:", start_addr);
	unsigned char b;
    while (fread(&b, 1, 1, fpin) == 1) {
        fprintf(fpout, " %02X", b);
        if (!(++start_addr & 0x07)) {
            fprintf(fpout, "\n:");
        }
    }
    fprintf(fpout, "\n");

    fclose(fpin);
	fclose(fpout);

	printf("Successful. Press RETURN for exit.\n");
	getchar();
	return (0);
}
