AddIn-Tools/src/main.c

160 lines
5.6 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define HI_NIBBLE(b) (((b) >> 4) & 0x0F)
#define LO_NIBBLE(b) ((b) & 0x0F)
typedef struct{
/*char FileIdentifier[8];
char FileType;
char Controlbytes[5];
char ControlbyteLSB;
char Controlbyte2;
unsigned int FileSize;
char ControlbyteLSB2;
char Unknown;
char Checksum[2];
char Unknown2[6];
short Objects;*/
char CasioHeader[0x20];
char InternalName[8];
char Numberestrips;
char VersionForm[10];
char CreationDateForm[14];
char MenuIcon[68];
char ProgramTitle[8];
unsigned long TotalFileSize;
}addin_header_t;
typedef struct{
unsigned char b1;
unsigned char b2;
unsigned char n1;
unsigned char n2;
unsigned char n3;
unsigned char n4;
}shcommand_t;
void bin(unsigned n){
unsigned i;
for (i = 1 << 31; i > 0; i = i / 2){
(n & i) ? printf("1") : printf("0");
}
}
int main(int argc, char **argv){
if(argc == 1){
printf("\033[1;31m");
printf("error: ");
printf("\033[0;37m");
printf("no input file!, please put graph 75 ADD-IN file\n");
return 1;
}
if(access(argv[1], F_OK ) == 0) { //check if file exists
//none
} else {
printf("\033[1;31m");
printf("error: ");
printf("\033[0;37m");
printf("cannot open file!\n");
return 2;
}
printf("\033[0;37m"); //white output text
//unsigned char ROM[8388608] = {0}; //ROM start: 80000000
//unsigned char RAM[524288] = {0}; //RAM start: 88000000
unsigned int Index = 0x200; //index
unsigned char Byte = 0xAF;
addin_header_t AddInHeader; //add-in header
shcommand_t Command;
FILE * OSFile; //file pointer
OSFile = fopen(argv[1], "rb"); //open file
fseek(OSFile, 0x020L,SEEK_SET); //read Add-In header
fread(&AddInHeader.InternalName, sizeof(AddInHeader.InternalName), 1, OSFile);
fseek(OSFile, 0x030L,SEEK_SET);
fread(&AddInHeader.VersionForm, sizeof(AddInHeader.VersionForm), 1, OSFile);
fseek(OSFile, 0x03CL,SEEK_SET);
fread(&AddInHeader.CreationDateForm, sizeof(AddInHeader.CreationDateForm), 1, OSFile);
fseek(OSFile, 0x1D4L,SEEK_SET);
fread(&AddInHeader.ProgramTitle, sizeof(AddInHeader.ProgramTitle), 1, OSFile);
fseek(OSFile, 0x1F0L,SEEK_SET);
fread(&AddInHeader.TotalFileSize, sizeof(AddInHeader.TotalFileSize), 1, OSFile);
AddInHeader.TotalFileSize = ((AddInHeader.TotalFileSize>>24)&0xff) |
((AddInHeader.TotalFileSize<<8)&0xff0000) |
((AddInHeader.TotalFileSize>>8)&0xff00) |
((AddInHeader.TotalFileSize<<24)&0xff000000);
printf("internal name : %.*s\n", 8, AddInHeader.InternalName);
printf("program name : %.*s\n", 8, AddInHeader.ProgramTitle);
printf("version (form) : %.*s\n", 10, AddInHeader.VersionForm);
printf("creation date : %.*s\n", 14, AddInHeader.CreationDateForm);
printf("File size : %lu\n", AddInHeader.TotalFileSize);
for(; Index<AddInHeader.TotalFileSize+0x200; Index=Index+0x2){
fseek(OSFile, Index, SEEK_SET); //extract nibbles
fread(&Command.b1, sizeof(Command.b1), 1, OSFile);
Command.n1 = HI_NIBBLE(Command.b1);
Command.n2 = LO_NIBBLE(Command.b1);
fseek(OSFile, Index+0x01, SEEK_SET);
fread(&Command.b2, sizeof(Command.b2), 1, OSFile);
Command.n3 = HI_NIBBLE(Command.b2);
Command.n4 = LO_NIBBLE(Command.b2);
printf("\e[90m%06x - %02x %02x \e[35m", Index, Command.b1, Command.b2);
if(Command.n1 == 0x0E){
printf("mov #%d, R%d", Command.b2, Command.n2);
}
else if(Command.n1 == 0x06 && Command.n4 == 0x03){
printf("mov R%d, R%d", Command.n2, Command.n3);
}
else if(Command.n1 == 0x0D){
printf("mov.l @%06x, R%d",Index+Command.b2*4+4, Command.n2);
}
else if(Command.n1 == 0x02 && Command.n4 == 0x06){
printf("mov.l R%d, @R%d",Command.n3, Command.n2);
}
else if(Command.n1 == 0x07){
printf("add #%d, R%d", Command.b2, Command.n2);
}
else if(Command.n1 == 0x06 && Command.n4 == 0x0B){
printf("neg R%d, R%d", Command.n3, Command.n2);
}
else if(Command.n1 == 0x02 && Command.n4 == 0x0B){
printf("or R%d, R%d", Command.n3, Command.n2);
}
else if(Command.n1 == 0x06 && Command.n4 == 0x07){
printf("not R%d, R%d", Command.n3, Command.n2);
}
else if(Command.n1 == 0x02 && Command.n4 == 0x0a){
printf("xor R%d, R%d", Command.n3, Command.n2);
}
else if(Command.n1 == 0x04 && Command.n4 == 0x0c){
printf("shad R%d, R%d", Command.n3, Command.n2);
}
else if(Command.n1 == 0x04 && Command.b2 == 0x0B){
printf("jsr @R%d", Command.n2);
}
else if(Command.b1 == 0xA0){
printf("bra @%06x", Index+Command.b2*4+4);
}
else if(Command.n1 == 0x04 && Command.b2 == 0x2B){
printf("\e[33mjmp \e[35m@R%d", Command.n2);
}
else if(Command.b1 == 0x00 && Command.b2 == 0x0B){
printf("rts");
}
else if(Command.b1 == 0x00 && Command.b2 == 0x09){
printf("\e[31mnop");
}
printf("\n");
}
fclose(OSFile);
return 0;
}