Skip to content

Commit

Permalink
Z80: Some code clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeakohn committed Oct 17, 2024
1 parent 4529fb9 commit ea01c05
Show file tree
Hide file tree
Showing 6 changed files with 268 additions and 279 deletions.
41 changes: 21 additions & 20 deletions asm/z80.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Web: https://www.mikekohn.net/
* License: GPLv3
*
* Copyright 2010-2023 by Michael Kohn
* Copyright 2010-2024 by Michael Kohn
*
*/

Expand Down Expand Up @@ -273,23 +273,21 @@ int parse_instruction_z80(AsmContext *asm_context, char *instr)
int token_type;
char instr_case[TOKENLEN];
struct _operand operands[3];
int operand_count=0;
int offset=0;
int matched=0;
int operand_count = 0;
int offset = 0;
bool matched = false;
int instr_enum;
int num;
int n,reg;
int n, reg;

lower_copy(instr_case, instr);

memset(&operands, 0, sizeof(operands));

// Find instruction
n = 0;
while (table_instr_z80[n].instr != NULL)
for (n = 0; table_instr_z80[n].instr != NULL; n++)
{
if (strcmp(instr_case, table_instr_z80[n].instr) == 0) { break; }
n++;
}

if (table_instr_z80[n].instr == NULL)
Expand All @@ -301,9 +299,10 @@ int parse_instruction_z80(AsmContext *asm_context, char *instr)
//instr_index = n;
instr_enum = table_instr_z80[n].instr_enum;

while (1)
while (true)
{
token_type = tokens_get(asm_context, token, TOKENLEN);

if (token_type == TOKEN_EOL || token_type == TOKEN_EOF)
{
break;
Expand All @@ -318,6 +317,7 @@ int parse_instruction_z80(AsmContext *asm_context, char *instr)
if (IS_TOKEN(token,'('))
{
token_type = tokens_get(asm_context, token, TOKENLEN);

if ((n = get_reg16(token)) != -1)
{
operands[operand_count].type = OPERAND_INDEX_REG16;
Expand All @@ -334,8 +334,10 @@ int parse_instruction_z80(AsmContext *asm_context, char *instr)
{
operands[operand_count].type = OPERAND_INDEX_REG16_XY;
operands[operand_count].value = n;

token_type = tokens_get(asm_context, token, TOKENLEN);
tokens_push(asm_context, token, token_type);

if (IS_NOT_TOKEN(token,')'))
{
if (eval_expression(asm_context, &num) != 0)
Expand All @@ -357,12 +359,14 @@ int parse_instruction_z80(AsmContext *asm_context, char *instr)
else
{
tokens_push(asm_context, token, token_type);

if (eval_expression(asm_context, &num) != 0)
{
if (asm_context->pass == 1)
{
int paren_count = 1;
while (1)

while (true)
{
token_type = tokens_get(asm_context, token, TOKENLEN);
if (token_type == TOKEN_EOL || token_type == TOKEN_EOF) { break; }
Expand Down Expand Up @@ -492,12 +496,12 @@ printf("-- %d %d %d\n", operands[n].type, operands[n].value, operands[n].offset)

// Instruction is parsed, now find matching opcode

n = 0;
while (table_z80[n].instr_enum != Z80_NONE)
for (n = 0; table_z80[n].instr_enum != Z80_NONE; n++)
{
if (table_z80[n].instr_enum == instr_enum)
{
matched = 1;
matched = true;

switch (table_z80[n].type)
{
case OP_NONE:
Expand Down Expand Up @@ -1581,15 +1585,14 @@ printf("-- %d %d %d\n", operands[n].type, operands[n].value, operands[n].offset)
}
}
}
n++;
}

n = 0;
while (table_z80_4_byte[n].instr_enum != Z80_NONE)
for (n = 0; table_z80_4_byte[n].instr_enum != Z80_NONE; n++)
{
if (table_z80_4_byte[n].instr_enum == instr_enum)
{
matched = 1;
matched = true;

switch (table_z80_4_byte[n].type)
{
case OP_BIT_INDEX_V2:
Expand Down Expand Up @@ -1628,11 +1631,9 @@ printf("-- %d %d %d\n", operands[n].type, operands[n].value, operands[n].offset)
}
}
}

n++;
}

if (matched==1)
if (matched)
{
print_error_unknown_operand_combo(asm_context, instr);
}
Expand Down
2 changes: 1 addition & 1 deletion asm/z80.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Web: https://www.mikekohn.net/
* License: GPLv3
*
* Copyright 2010-2023 by Michael Kohn
* Copyright 2010-2024 by Michael Kohn
*
*/

Expand Down
25 changes: 6 additions & 19 deletions disasm/z80.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@ static const char *cond[] = { "nz", "z", "nc", "c", "po", "pe", "p", "m" };

static const char *get_instruction(int instr_enum)
{
int n;

n = 0;
while (table_instr_z80[n].instr != NULL)
for (int n = 0; table_instr_z80[n].instr != NULL; n++)
{
if (table_instr_z80[n].instr_enum == instr_enum)
{
return table_instr_z80[n].instr;
}
n++;
}

return "";
Expand Down Expand Up @@ -85,10 +81,9 @@ int disasm_z80(
opcode = READ_RAM(address);
opcode16 = READ_RAM16(address);

n = 0;
while (table_z80[n].instr_enum != Z80_NONE)
for (n = 0; table_z80[n].instr_enum != Z80_NONE; n++)
{
if (table_z80[n].mask > 0xff) { n++; continue; }
if (table_z80[n].mask > 0xff) { continue; }
if (table_z80[n].opcode == (opcode & table_z80[n].mask))
{
*cycles_min = table_z80[n].cycles_min;
Expand Down Expand Up @@ -299,15 +294,11 @@ int disasm_z80(
}
}
}
else

n++;
}

n = 0;
while (table_z80[n].instr_enum != Z80_NONE)
for (n = 0; table_z80[n].instr_enum != Z80_NONE; n++)
{
if (table_z80[n].mask <= 0xff) { n++; continue; }
if (table_z80[n].mask <= 0xff) { continue; }
if (table_z80[n].opcode == (opcode16 & table_z80[n].mask))
{
*cycles_min = table_z80[n].cycles_min;
Expand Down Expand Up @@ -599,16 +590,13 @@ int disasm_z80(
}
}
}

n++;
}

if ((opcode16 & 0xdfff) == 0xddcb)
{
i = READ_RAM(address + 3);

n = 0;
while (table_z80_4_byte[n].instr_enum != Z80_NONE)
for (n = 0; table_z80_4_byte[n].instr_enum != Z80_NONE; n++)
{
if ((i & table_z80_4_byte[n].mask) == table_z80_4_byte[n].opcode)
{
Expand Down Expand Up @@ -636,7 +624,6 @@ int disasm_z80(
}
}
}
n++;
}
}

Expand Down
2 changes: 1 addition & 1 deletion disasm/z80.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Web: https://www.mikekohn.net/
* License: GPLv3
*
* Copyright 2010-2023 by Michael Kohn
* Copyright 2010-2024 by Michael Kohn
*
*/

Expand Down
Loading

0 comments on commit ea01c05

Please sign in to comment.