Skip to content

Commit

Permalink
Z80Converter: WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
nzeemin committed Mar 30, 2024
1 parent 1e859b8 commit d4a5f12
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 13 deletions.
38 changes: 29 additions & 9 deletions Z80Converter/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ void printcommandhex(std::ostream& sout, int addr, int cmdlen)
}
}

// Calculate string width, knowing about 8-char tabs
int getstringwidth(string& str)
{
int width = 0;
for (std::string::iterator it = str.begin(); it != str.end(); ++it)
{
char ch = *it;
if (ch == '\t')
width = (width + 7) / 8 * 8;
else
width++;

}
return width;
}

int main(int argc, char* argv[])
{
std::ios_base::fmtflags coutf(std::cout.flags()); // store flags
Expand Down Expand Up @@ -247,6 +263,7 @@ int main(int argc, char* argv[])
size_t resultspacepos = result.find(" ");
if (resultspacepos != string::npos)
result.replace(resultspacepos, 1, "\t");
// Trim trailing spaces

foutfile << "L" << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << addr << ":\t";
foutfile.flags(ff); // restore flags
Expand All @@ -255,7 +272,7 @@ int main(int argc, char* argv[])

if (result.empty())
{
foutfile << "???"; // Not converted
result = "???"; // Not converted

std::cout << std::hex << std::uppercase << std::setw(4) << std::setfill('0') << addr << ": ";
std::cout.flags(coutf); // restore flags
Expand All @@ -264,17 +281,20 @@ int main(int argc, char* argv[])
std::cout << std::endl;
std::cout.flags(coutf); // restore flags
}
else
{
foutfile << result.c_str();

if (result.find("???") == string::npos) // Fully converted?
convertedCommands++;
}
foutfile << result.c_str();

foutfile << "\t\t; " << std::setw(18) << std::setfill(' ') << std::left << g_commanddisasm.c_str() << " ";
foutfile.flags(ff); // restore flags
if (result.find("???") == string::npos) // Fully converted?
convertedCommands++;

int tabnum = getstringwidth(result) / 8;
tabnum = tabnum >= 3 ? 0 : 3 - tabnum;
for (int i = 0; i < tabnum; i++)
foutfile << "\t";

foutfile << "\t; " << g_commanddisasm.c_str();

foutfile.flags(ff); // restore flags
foutfile << std::endl;

processedCommands++;
Expand Down
34 changes: 30 additions & 4 deletions Z80Converter/recomp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,21 @@ string PatternProc_LD_A_HLADDR()
return "MOVB (R3), R0";
}

string PatternProc_LD_R_HLADDR()
{
switch (g_command[0])
{
case 0x7E: /* ld a,(HL) */ return PatternProc_LD_A_HLADDR();
case 0x46: /* ld b,(HL) */ return "SWAP R1 / BIC #377, R1 / BISB (R3), R1 / SWAP R1";
case 0x4E: /* ld c,(HL) */ return "BIC #377, R1 / BISB (R3), R1";
case 0x56: /* ld d,(HL) */ return "SWAP R2 / BIC #377, R2 / BISB (R3), R2 / SWAP R2";
case 0x5E: /* ld e,(HL) */ return "BIC #377, R2 / BISB (R3), R2";
case 0x66: /* ld h,(HL) */ return "???";
case 0x6E: /* ld l,(HL) */ return "???";
}
return "???";
}

string PatternProc_INCDEC_HLADDR()
{
return (g_command[0] == 0x34) ? "INC (R3)" : "DEC (R3)";
Expand Down Expand Up @@ -399,8 +414,8 @@ string PatternProc_ADD_X()
case 0x83: /* add e */ return "ADD R2 or CLR R5 / MOVB R2, R5 / ADD R5, R0";
case 0x84: /* add h */ return "???";
case 0x85: /* add l */ return "ADD R3 or CLR R5 / MOVB R3, R5 / ADD R5, R0";
case 0x86: /* add (hl) */ return "???";
case 0x87: /* add a */ return "CLR R0";
case 0x86: /* add (hl) */ return "CLR R5 / MOVB (R3), R5 / ADD R5, R0"; // òóò ñëîæíîñòè, ïîòîìó ÷òî äîáàâëÿåòñÿ áàéò
case 0x87: /* add a */ return "ADD R0, R0";
}
return "";
}
Expand All @@ -415,7 +430,7 @@ string PatternProc_SUB_X()
case 0x93: /* sub e */ return "SUB R2 or CLR R5 / MOVB R2, R5 / SUB R5, R0";
case 0x94: /* sub h */ return "???";
case 0x95: /* sub l */ return "SUB R3 or CLR R5 / MOVB R3, R5 / SUB R5, R0";
case 0x96: /* sub (hl) */ return "???";
case 0x96: /* sub (hl) */ return "CLR R5 / MOVB (R3), R5 / SUB R5, R0"; // òóò ñëîæíîñòè, ïîòîìó ÷òî âû÷èòàåòñÿ áàéò
case 0x97: /* sub a */ return "CLR R0";
}
return "";
Expand Down Expand Up @@ -555,6 +570,15 @@ string PatternProc_SET_B_HLADDR()
return buffer;
}

//NOTE: Êîíâåðòàöèÿ â SUB R3, Rx íå ó÷èòûâàåò ñîñòîÿíèå ôëàãà CY
string PatternProc_SBC_HL_SS()
{
const char* rpname = rpnames_bcdehlsp[(g_command[1] >> 4) & 3];
char buffer[40];
_snprintf(buffer, sizeof(buffer), "SUB %s, R3", rpname);
return buffer;
}

string PatternProc_NEG()
{
return "NEGB R0 or NEG R0";
Expand Down Expand Up @@ -723,7 +747,8 @@ Pattern g_patterns[] =
{ 1, { 0x47 }, { 0xC7 }, PatternProc_LD_X_A },
{ 1, { 0x70 }, { 0xF8 }, PatternProc_LD_HLADDR_X },
{ 1, { 0x78 }, { 0xF8 }, PatternProc_LD_A_X },
{ 1, { 0x7E }, { 0xFF }, PatternProc_LD_A_HLADDR },
{ 1, { 0x7E }, { 0xFF }, PatternProc_LD_A_HLADDR }, // ld A,(HL)
{ 1, { 0x46 }, { 0xC7 }, PatternProc_LD_R_HLADDR }, // ld r,(HL)
{ 1, { 0x86 }, { 0xFF }, PatternProc_ADD_A_HLADDR },
{ 1, { 0x87 }, { 0xFF }, PatternProc_ADD_A_A },
{ 1, { 0x80 }, { 0xF8 }, PatternProc_ADD_X },
Expand Down Expand Up @@ -753,6 +778,7 @@ Pattern g_patterns[] =
{ 2, { 0xCB, 0xC6 }, { 0xFF, 0xC7 }, PatternProc_SET_B_HLADDR }, // set b,(HL)

// ED table
{ 2, { 0xED, 0x42 }, { 0xFF, 0xCF }, PatternProc_SBC_HL_SS }, // sbc HL,ss
{ 2, { 0xED, 0x44 }, { 0xFF, 0xFF }, PatternProc_NEG },
{ 2, { 0xED, 0xA0 }, { 0xFF, 0xFF }, PatternProc_LDI },
{ 2, { 0xED, 0xB0 }, { 0xFF, 0xFF }, PatternProc_LDIR },
Expand Down

0 comments on commit d4a5f12

Please sign in to comment.