}
}
-static FILE *exportfile = 0;
-static int exportbits = 0;
-static int exportcount = 0;
-static int exportseek = 0;
-
-void putbit(int x) {
- if(x) {
- exportbits |= (1 << exportcount);
- }
- exportcount++;
- if(exportcount == 8) {
- if(exportfile) {
- fprintf(exportfile, "\t.byte\t0x%02x\n", exportbits);
- }
- exportseek++;
- exportbits = 0;
- exportcount = 0;
- }
-}
-
-void exportchunk(int data, int bits) {
- int i;
-
- for(i = 0; i < bits; i++) {
- putbit(!!(data & (1 << i)));
- }
-}
-
-int alignbyte() {
- if(exportcount) {
- if(exportfile) {
- fprintf(exportfile, "\t.byte\t0x%02x\n", exportbits);
- }
- exportseek++;
- exportbits = 0;
- exportcount = 0;
- }
- if(exportfile) fprintf(exportfile, "\n");
- return exportseek;
-}
-
-int packcmd(u8 ch) {
- if(!ch) return 0;
- if(strchr(validcmds, ch)) {
- return strchr(validcmds, ch) - validcmds;
- }
- return 0;
-}
-
-void exportdata(FILE *f, int maxtrack, int *resources) {
- int i, j;
- int nres = 0;
-
- exportfile = f;
- exportbits = 0;
- exportcount = 0;
- exportseek = 0;
-
- for(i = 0; i < 16 + maxtrack; i++) {
- // if(exportfile) fprintf(exportfile, "songdata_resources%d:\n", i);
- exportchunk(resources[i], PACKSIZE_RESOURCE);
- }
-
- resources[nres++] = alignbyte();
-
- if(exportfile) fprintf(exportfile, "songdata_song:\n");
- for(i = 0; i < songlen; i++) {
- for(j = 0; j < NR_CHAN; j++) {
- if(song[i].transp[j]) {
- exportchunk(1, 1);
- exportchunk(song[i].track[j], PACKSIZE_SONGTRACK);
- exportchunk(song[i].transp[j], PACKSIZE_SONGTRANS);
- } else {
- exportchunk(0, 1);
- exportchunk(song[i].track[j], PACKSIZE_SONGTRACK);
- }
- }
- }
-
- for(i = 1; i < 16; i++) {
- resources[nres++] = alignbyte();
-
- // if(exportfile) fprintf(exportfile, "songdata_instrument%d:\n", i);
- if(instrument[i].length > 1) {
- for(j = 0; j < instrument[i].length; j++) {
- exportchunk(packcmd(instrument[i].line[j].cmd), PACKSIZE_INSTRCMD);
- exportchunk(instrument[i].line[j].param, PACKSIZE_INSTRPAR);
- }
- }
-
- exportchunk(0, PACKSIZE_INSTRCMD);
- }
-
- for(i = 1; i <= maxtrack; i++) {
- resources[nres++] = alignbyte();
-
- // if(exportfile) fprintf(exportfile, "songdata_track%d:\n", i);
- for(j = 0; j < tracklen; j++) {
- u8 cmd = packcmd(track[i].line[j].cmd[0]);
-
- exportchunk(!!track[i].line[j].note, 1);
- exportchunk(!!track[i].line[j].instr, 1);
- exportchunk(!!cmd, 1);
-
- if(track[i].line[j].note) {
- exportchunk(track[i].line[j].note, PACKSIZE_TRACKNOTE);
- }
-
- if(track[i].line[j].instr) {
- exportchunk(track[i].line[j].instr, PACKSIZE_TRACKINST);
- }
-
- if(cmd) {
- exportchunk(cmd, PACKSIZE_TRACKCMD);
- exportchunk(track[i].line[j].param[0], PACKSIZE_TRACKPAR);
- }
- }
- }
-}
-
-void export(char *stem) {
- int blen = strlen(stem) + 5;
- char *buf = alloca(blen);
- if(!buf) {
- return;
- }
- snprintf(buf, blen, "%s.s", stem);
- FILE *f = fopen(buf, "w");
-
- snprintf(buf, blen, "%s.h", stem);
- FILE *hf = fopen(buf, "w");
-
- int i, j;
- int maxtrack = 0;
- int resources[256];
-
- exportfile = 0;
- exportbits = 0;
- exportcount = 0;
- exportseek = 0;
-
- for(i = 0; i < songlen; i++) {
- for(j = 0; j < 4; j++) {
- if(maxtrack < song[i].track[j]) maxtrack = song[i].track[j];
- }
- }
-
- fprintf(f, "\t.global\tsongdata\n\n");
-
- fprintf(hf, "#define MAXTRACK\t0x%02x\n", maxtrack);
- fprintf(hf, "#define SONGLEN\t\t0x%02x\n", songlen);
-
- fprintf(f, "songdata:\n");
-
- exportdata(0, maxtrack, resources);
-
- fprintf(f, "# ");
- for(i = 0; i < 16 + maxtrack; i++) {
- fprintf(f, "%04x ", resources[i]);
- }
- fprintf(f, "\n");
-
- exportdata(f, maxtrack, resources);
-
- fclose(f);
- fclose(hf);
-}
-
void handleinput() {
int c, x;
case '#':
optimize();
break;
- case '%':
- export("export");
- break;
case KEY_LEFT:
switch(currtab) {
case 0: