Skip to content

Commit

Permalink
add .FILL size,value; remove magic numbers; fix .END behaviour
Browse files Browse the repository at this point in the history
```
0000    00                  nop
0001    AA AA AA AA         .fill   2 * 3, $55 << 1
0005    AA AA
0007    00                  nop
```

Signed-off-by: Martin <[email protected]>
  • Loading branch information
Ho-Ro committed Sep 24, 2024
1 parent 5c9c6b8 commit 831dd4a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 43 deletions.
2 changes: 1 addition & 1 deletion z80_assembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ int main( int argc, char **argv ) {
memset( RAM, fill, RAMSIZE ); // erase 64K RAM
PC = 0x0000; // default start address of the code

while ( true ) {
while ( !reachedEnd ) {
uint32_t prevPC = PC;
oneLine = fgets( LineBuf, sizeof( LineBuf ), infile ); // read a single line
if ( !oneLine )
Expand Down
3 changes: 2 additions & 1 deletion z80_assembler.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typedef enum {
STRING // a string
} Type;

typedef enum { DEFB = 0x100, DEFM, DEFS, DEFW, END, EQU, ORG, IF, ENDIF, ELSE, PRINT } Pseudo_t;
typedef enum { DEFB = 0x100, DEFM, DEFS, DEFW, END, EQU, ORG, IF, ENDIF, ELSE, PRINT, FILL } Pseudo_t;

// encoded opcode
typedef struct {
Expand Down Expand Up @@ -52,6 +52,7 @@ extern uint32_t minPC;
extern uint32_t maxPC;
extern bool listing;
extern int verboseMode;
extern bool reachedEnd;
extern void checkPC( uint32_t pc );

extern RecalcListP LastRecalc; // to patch the type for incomplete formulas
Expand Down
2 changes: 1 addition & 1 deletion z80_calc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int32_t GetValue( CommandP *c ) {
Error( "Closing bracket is missing" );
}
} else
default:
default:
Error( "Illegal symbol in a formula" );
}
( *c )++; // skip value, symbol or bracket
Expand Down
48 changes: 34 additions & 14 deletions z80_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ void DoPseudo( CommandP *cp );
void DoOpcode( CommandP *cp );
int16_t GetOperand( CommandP *cp, int32_t *value );

bool reachedEnd = false;


/***
* get operands for an opcode
Expand Down Expand Up @@ -88,7 +90,7 @@ int16_t GetOperand( CommandP *c, int32_t *value ) {
* test for an opcode
***/
void DoOpcode( CommandP *cp ) {
MSG( 2, "DoOpcode( %X )\n", (*cp)->val );
MSG( 2, "DoOpcode( %X )\n", ( *cp )->val );
CommandP c = *cp;
uint8_t *iRAM = RAM + PC;
uint32_t op0;
Expand Down Expand Up @@ -146,10 +148,10 @@ void DoOpcode( CommandP *cp ) {
op2Recalc = nullptr; // processing done
}
*iRAM++ = value2;
} else if ( !(Op0_24 & 0x01) && ( op1 == 0x501 ) && ( op2 == 0 ) ) { // undoc: IN (C))
} else if ( !( Op0_24 & 0x01 ) && ( op1 == 0x501 ) && ( op2 == 0 ) ) { // undoc: IN (C))
*iRAM++ = 0xED;
*iRAM++ = 0x70;
} else if ( (Op0_24 & 0x01) && ( op1 == 0x281 ) && ( op2 == 0x501 ) && value1 == 0 ) { // undoc: OUT (C),0
} else if ( ( Op0_24 & 0x01 ) && ( op1 == 0x281 ) && ( op2 == 0x501 ) && value1 == 0 ) { // undoc: OUT (C),0
*iRAM++ = 0xED;
*iRAM++ = 0x71;
} else
Expand Down Expand Up @@ -867,7 +869,7 @@ void DoOpcode( CommandP *cp ) {
bool IgnoreUntilIF = false; // ignore all lines till next "ENDIF" (this could be a stack for nesting support)

void DoPseudo( CommandP *cp ) {
MSG( 2, "DoPseudo( %d, %X )\n", (*cp)->typ, (*cp)->val );
MSG( 2, "DoPseudo( %d, %X )\n", ( *cp )->typ, ( *cp )->val );
CommandP c = *cp;
CommandP cptr;
uint16_t iPC = PC;
Expand All @@ -877,7 +879,7 @@ void DoPseudo( CommandP *cp ) {
case DEFM:
c--;
do {
c++; // skip opcode or comma
c++; // skip opcode or comma
if ( c->typ != STRING ) {
cptr = c;
checkPC( iPC );
Expand All @@ -903,6 +905,26 @@ void DoPseudo( CommandP *cp ) {
if ( LastRecalc )
Error( "symbol not defined" );
break;
case FILL: {
uint16_t size, fill = 0;
cptr = c;
size = CalcTerm( &cptr ); // get the amount
c = cptr;
if ( LastRecalc )
Error( "symbol not defined" );
if ( c->typ == OPCODE && c->val == ',' ) { // ", val" part?
cptr = ++c;
fill = CalcTerm( &cptr ); // get the fill value
c = cptr;
if ( LastRecalc )
Error( "symbol not defined" );
} else
fill = 0;
checkPC( iPC + size - 1 );
while ( size-- )
RAM[ iPC++ ] = fill;
break;
}
case DEFW:
c--;
do {
Expand All @@ -923,8 +945,8 @@ void DoPseudo( CommandP *cp ) {
case END:
if ( IgnoreUntilIF )
Error( "IF without ENDIF" );
Error( "Reached the end of the source code -> exit" );
exit( 0 );
reachedEnd = true;
break;
case ORG:
cptr = c;
iPC = CalcTerm( &cptr ); // set the PC
Expand Down Expand Up @@ -972,13 +994,12 @@ void CompileLine( void ) {
s = (SymbolP)c->val; // value = ptr to the symbol
if ( s->defined ) {
Error( "symbol already defined" );
return;
}
c++; // next command
if ( ( c->typ == OPCODE ) && ( c->val == ':' ) )
c++; // ignore a ":" after a symbol
if ( ( c->typ == OPCODE ) && ( c->val == 0x105 ) ) { // EQU?
c++; // skip EQU
c++; // ignore a ":" after a symbol
if ( ( c->typ == OPCODE ) && ( c->val == EQU ) ) { // EQU?
c++; // skip EQU
cptr = c;
s->val = CalcTerm( &cptr ); // calculate the expression
c = cptr;
Expand All @@ -987,7 +1008,6 @@ void CompileLine( void ) {
s->defined = true; // symbol now defined
if ( c->typ != ILLEGAL ) {
Error( "EQU is followed by illegal data" );
return;
}
} else {
s->val = PC; // adresse = current PC
Expand Down Expand Up @@ -1032,10 +1052,10 @@ void CompileLine( void ) {
if ( IgnoreUntilIF ) { // inside an IFs?
if ( c->typ == OPCODE ) {
switch ( c->val ) {
case 0x108: // ENDIF reached?
case ENDIF: // ENDIF reached?
IgnoreUntilIF = false; // start compiling
break;
case 0x109: // ELSE reached?
case ELSE: // ELSE reached?
IgnoreUntilIF = !IgnoreUntilIF; // toggle IF flag
break;
}
Expand Down
26 changes: 14 additions & 12 deletions z80_disassembler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static uint32_t RAM_high_addr = 0;

static int verboseMode = 0;

static char label = '$';

static void MSG( int mode, const char *format, ... ) {
if ( verboseMode >= mode ) {
Expand Down Expand Up @@ -397,13 +398,13 @@ void Disassemble( uint16_t adr, char *s, size_t ssize ) {
G( "EX AF,AF'" );
break;
case 0x02:
G( "DJNZ $%4.4X", adr + 2 + BYTE_1 );
G( "DJNZ %c%4.4X", label, adr + 2 + BYTE_1 );
break;
case 0x03:
G( "JR $%4.4X", adr + 2 + BYTE_1 );
G( "JR %c%4.4X", label, adr + 2 + BYTE_1 );
break;
default:
G( "JR %s,$%4.4X", cond[ d & 3 ], adr + 2 + BYTE_1 );
G( "JR %s,%c%4.4X", cond[ d & 3 ], label, adr + 2 + BYTE_1 );
break;
}
break;
Expand Down Expand Up @@ -502,12 +503,12 @@ void Disassemble( uint16_t adr, char *s, size_t ssize ) {
}
break;
case 0x02:
G( "JP %s,$%4.4X", cond[ d ], WORD_1_2 );
G( "JP %s,%c%4.4X", cond[ d ], label, WORD_1_2 );
break;
case 0x03:
switch ( d ) {
case 0x00:
G( "JP $%4.4X", WORD_1_2 );
G( "JP %c%4.4X", label, WORD_1_2 );
break;
case 0x01: // 0xCB
CB = a;
Expand Down Expand Up @@ -553,13 +554,13 @@ void Disassemble( uint16_t adr, char *s, size_t ssize ) {
}
break;
case 0x04:
G( "CALL %s,$%4.4X", cond[ d ], WORD_1_2 );
G( "CALL %s,%c%4.4X", cond[ d ], label, WORD_1_2 );
break;
case 0x05:
if ( d & 1 ) {
switch ( d >> 1 ) {
case 0x00:
G( "CALL $%4.4X", WORD_1_2 );
G( "CALL %c%4.4X", label, WORD_1_2 );
break;
case 0x02: // 0xED
ED = a;
Expand Down Expand Up @@ -786,7 +787,7 @@ int main( int argc, char *argv[] ) {
uint32_t adr = 0;
char oneLine[ 256 ]; // output string

fprintf( stderr, "TurboDis Z80 - small disassembler for Z80 code\n" );
fprintf( stderr, "TurboDis Z80 - small disassembler for Z80 code\n\n" );

int i, j;
uint32_t offset = 0;
Expand Down Expand Up @@ -838,6 +839,7 @@ int main( int argc, char *argv[] ) {
break;
case 'p': // parse program flow
parse = true;
label = 'L';
break;
case 'r': // parse program flow
rst_parse = true;
Expand Down Expand Up @@ -925,18 +927,17 @@ int main( int argc, char *argv[] ) {
len = OpcodeLen( adr ); // get length of opcode
if ( !hexdump ) {
if ( OpcodesFlags[ adr ] & 0x10 )
fprintf( outfile, "L%4.4X: ", adr );
fprintf( outfile, "%c%4.4X: ", parse ? 'L' : '$', adr );
else
fprintf( outfile, " " );
} else {
fprintf( outfile, "%4.4X ", (uint16_t)adr );
fprintf( outfile, "%c%4.4X ", parse ? 'L' : '$', (uint16_t)adr );
for ( i = 0; i < len; i++ )
fprintf( outfile, "%2.2X ", Opcodes[ adr + i ] );
for ( i = 4; i > len; i-- )
fprintf( outfile, " " );
fprintf( outfile, " " );
}

Disassemble( adr, oneLine, sizeof( oneLine ) );
fprintf( outfile, "%s\n", oneLine );
adr += len;
Expand Down Expand Up @@ -1047,7 +1048,8 @@ ihex_bool_t ihex_data_read( struct ihex_state *ihex, ihex_record_type_t type, ih
hex_data_size += ihex->length;
} else if ( type == IHEX_END_OF_FILE_RECORD ) {
MSG( 4, "IHEX EOF\n" );
MSG( 1, "Loaded %d data bytes from hexfile into RAM region [0x%04X...0x%04X]\n", hex_data_size, RAM_low_addr, RAM_high_addr );
MSG( 1, "Loaded %d data bytes from hexfile into RAM region [0x%04X...0x%04X]\n", hex_data_size, RAM_low_addr,
RAM_high_addr );
if ( hex_data_size != RAM_high_addr + 1 - RAM_low_addr )
MSG( 1, "(size: %d Bytes)\n", RAM_high_addr + 1 - RAM_low_addr );
else
Expand Down
33 changes: 19 additions & 14 deletions z80_tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ typedef struct {


static const ShortSym Pseudo[] = {
{ DEFB,"DEFB",0x0000 }, { DEFB,"DB",0x0000 },
{ DEFM,"DEFM",0x0000 }, { DEFM,"DM",0x0000 },
{ DEFS,"DEFS",0x0000 }, { DEFS,"DS",0x0000 },
{ DEFW,"DEFW",0x0000 }, { DEFW,"DW",0x0000 },
{ ELSE,"ELSE",0x0000 }, { END,"END",0x0000 },
{ ENDIF,"ENDIF",0x0000 },{ EQU,"EQU",0x0000 }, { IF,"IF",0x0000 },
{ ORG,"ORG",0x0000 }, { PRINT,"PRINT",0x0000 }
{ DEFB, "DEFB", 0x0000 }, { DEFB,"DB",0x0000 }, // 0x100
{ DEFM, "DEFM", 0x0000 }, { DEFM,"DM",0x0000 }, // 0x101
{ DEFS, "DEFS", 0x0000 }, { DEFS,"DS",0x0000 }, // 0x102
{ DEFW, "DEFW", 0x0000 }, { DEFW,"DW",0x0000 }, // 0x103
{ END, "END", 0x0000 }, // 0x104
{ EQU, "EQU", 0x0000 }, // 0x105
{ ORG, "ORG", 0x0000 }, // 0x106
{ IF, "IF", 0x0000 }, // 0x107
{ ENDIF,"ENDIF",0x0000 }, // 0x108
{ ELSE, "ELSE", 0x0000 }, // 0x109
{ PRINT,"PRINT",0x0000 }, // 0x10A
{ FILL, "FILL", 0x0000 } // 0x10B
};


Expand Down Expand Up @@ -223,7 +228,7 @@ void TokenizeLine( char *sp ) {
base = 0;
dot = false; // pseudo opcodes can start with '.'
dollar = false; // $ = PC
if ( c == '.') {
if ( c == '.' ) {
c = *sp++;
dot = true;
} else if ( c == '$' ) { // PC or the beginning of a hex number
Expand Down Expand Up @@ -292,9 +297,9 @@ void TokenizeLine( char *sp ) {
sym->defined = false; // symbol value not defined
}
} else {
typ = OPCODE; // an opcode
val = sym->val; // parameter, ID
if ( dot && ( val < 0x100 || val >= 0x200 ) ) // only pseudo opcodes
typ = OPCODE; // an opcode
val = sym->val; // parameter, ID
if ( dot && ( val < 0x100 || val >= 0x200 ) ) // only pseudo opcodes
Error( "opcodes can't start with '.'" );
}
} else
Expand All @@ -315,8 +320,8 @@ void TokenizeLine( char *sp ) {
sp++;
}
break;
case '=':
val = 0x105; // = matches EQU
case '=': // = matches EQU
val = EQU;
break;
case '\'': // an ASCII character with '.'
val = AktLine[ sp - AktUpLine ]; // not capitalized ASCII character
Expand Down Expand Up @@ -354,7 +359,7 @@ void TokenizeLine( char *sp ) {
cp++;

if ( verboseMode >= 3 )
switch( typ ) {
switch ( typ ) {
case ILLEGAL:
MSG( 3, "ILLEGAL\n" );
break;
Expand Down

0 comments on commit 831dd4a

Please sign in to comment.