Skip to content

Instantly share code, notes, and snippets.

@superctr
Created May 20, 2020 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save superctr/e4b15c66374b44c85f649e867027c7b7 to your computer and use it in GitHub Desktop.
Save superctr/e4b15c66374b44c85f649e867027c7b7 to your computer and use it in GitHub Desktop.
unpacker for Software Creations .SRC files
#include <stdio.h>
#include <string.h>
// decompression tool for Software Creations source code
// created by ctr
// using data from "edit_sfx.com"
//---------------------------------------------------------------------------
// The tables could be modified to retarget the code to a different assembler
// > at beginning of line starts a multi line comment until <
FILE* in;
FILE* out;
char *command;
char buffer[256];
int position = 0;
char* directive_table[] =
{
"ORG ", //80
"DFS ", //81
"DFB ", //82
"DFW ", //83
"DFM ", //84
"DFH ", //85
"ENT ", //86
"EQU ", //87
"DEF ", //88
"DFL ", //89
"HEX ", //8a
"DFN ", //8b
"OUT ", //8c
"END ", //8d
"INCL ", //8e
"INF ", //8f
"IF ", //90
"ELSE", //91
"ENDIF", //92
"REPEAT", //93
"UNTIL ", //94
"MACRO ", //95
"ENDM ", //96
"LOCKTABLE ", //97
"CLRTABLE ", //98
"INCLBIN " //99
};
// opcode table (SPC700 only). 65c816/6502 should have a different table.
char* opcode_table[] =
{
// ---0--- ---1--- ---2--- ---3--- ---4--- ---5--- ---6--- ---7---
"MOV ", "ADC ", "SBC ", "CMP ", "CPX ", "CPY ", "AND ", //81-87
"OR ", "EOR ", "INC ", "DEC ", "ASL ", "LSR ", "ROL ", "XCN ", //88-8f
"MOVW ", "INCW ", "DECW ", "ADDW ", "SUBW ", "CMPW ", "MUL ", "DI ", //90-97
"DAA ", "DAS ", "BRA ", "BEQ ", "BNE ", "BCS ", "BCC ", "BVS ", //98-9f
"BVC ", "BMI ", "BPL ", "BBS ", "BBC ", "CBNE ", "DBNZ ", "JMP ", //a0-a7
"CALL ", "PCALL ", "TCALL ", "BRK ", "RET ", "RETI ", "PUSH ", "POP ", //a8-af
"SETI ", "CLRI ", "TSETI ", "TCLRI ", "ANDI ", "ORI ", "EORI ", "NOTI ", //b0-b7
"MOVI ", "CLRC ", "SETC ", "NOTC ", "CLRV ", "CLRP ", "SETP ", "EI ", //b8-bf
"DIV ", "NOP ", "SLEEP ", "STOP", "ROR ", "DIRECT ", "", "", //c0-c7
"", "", "", "", "", "", "", "", //c8-cf
"", "", "", "", "", "", "", "", //d0-d7
"", "", "", "", "", "", "", "", //d8-df
"+X", "+Y", "(X)", "(Y)", "(X)+", "+X)", ")+Y", "(X),(Y)",//e0-e7
"", "", "", "", "", "", "", "", //e8-ef
"", "", "", "", "", ",X", ",Y", ",X)", //f0-f7
"),Y", ",S", "", "XOR ", "NOT ", "],Y ", ",S),Y", "" //f8-ff
};
void expand_tab()
{
if(position == 18)
fputc(' ',out);
while (position < 18)
{
fputc(' ',out);
position++;
}
}
void directive(int c)
{
if(c >= 0x80 && c <= 0x99)
{
command = directive_table[c - 0x80];
}
else
{
sprintf(buffer, "[d:%02x] ", c);
command = buffer;
}
expand_tab();
fputs(command, out);
position += strlen(command);
}
void opcode(int c)
{
if(c >= 0x81 && c <= 0xff)
{
command = opcode_table[c - 0x81];
}
else
{
sprintf(buffer, "[c:%02x] ", c);
command = buffer;
}
expand_tab();
fputs(command, out);
position += strlen(command);
}
int main(int argc, char** argv)
{
if(argc < 2)
{
printf("usage: %s input.src", argv[0]);
return;
}
in = fopen(argv[1], "rb");
if(!in)
{
printf("couldn't open file %s\n", argv[1]);
return -1;
}
command = strrchr(argv[1], '.');
if(command)
{
memcpy(buffer, argv[1], command-argv[1]);
strcpy(buffer+(command-argv[1]),".s");
}
else
{
sprintf(buffer, "%s.s", argv[1]);
}
out = fopen(buffer, "w");
if(!out)
{
printf("couldn't open file %s\n", buffer);
}
int c = fgetc(in);
while(c != EOF)
{
switch(c)
{
case 0x80:
c = fgetc(in);
if(c != EOF)
directive(c);
else
fprintf(stderr, "file ended with 0x80 !");
break;
case 0x00:
fputs("\n", out);
position = 0;
break;
case ';':
fputc(c, out);
position++;
c = fgetc(in);
if((c == '*' || c == '-') && position == 1)
{
while(position < 77)
{
fputc(c, out);
position++;
}
break;
}
else if(c == EOF)
{
break;
}
continue;
default:
if(c >= 0x80)
{
opcode(c);
}
else
{
fputc(c, out);
position++;
}
}
if(c != EOF)
c = fgetc(in);
}
fclose(in);
fclose(out);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment