2021-01-29 15:14:37 -05:00
# include <stdio.h>
/*
Copyright ( c ) 2021 Devine Lu Linvega
Permission to use , copy , modify , and distribute this software for any
purpose with or without fee is hereby granted , provided that the above
copyright notice and this permission notice appear in all copies .
THE SOFTWARE IS PROVIDED " AS IS " AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE .
*/
2021-04-20 17:30:26 -04:00
# define TRIM 0x0100
2021-10-06 12:21:27 -04:00
# define LENGTH 0x10000 - TRIM
2021-03-28 14:06:36 -04:00
2021-01-29 15:14:37 -05:00
typedef unsigned char Uint8 ;
2021-02-12 19:18:52 -05:00
typedef signed char Sint8 ;
2021-02-04 15:22:08 -05:00
typedef unsigned short Uint16 ;
2021-01-29 15:14:37 -05:00
2021-03-14 16:30:17 -04:00
typedef struct {
2021-09-30 22:21:04 -04:00
char name [ 64 ] , items [ 64 ] [ 64 ] ;
2021-06-18 00:20:19 -04:00
Uint8 len ;
2021-03-14 16:30:17 -04:00
} Macro ;
2021-03-13 23:51:43 -05:00
typedef struct {
2021-04-20 13:31:50 -04:00
char name [ 64 ] ;
2021-06-18 00:20:19 -04:00
Uint16 addr , refs ;
2021-01-31 00:31:49 -05:00
} Label ;
2021-03-13 17:55:29 -05:00
typedef struct {
2021-10-06 12:21:27 -04:00
Uint8 data [ LENGTH ] , mlen ;
2021-04-23 10:42:07 -04:00
Uint16 ptr , length , llen ;
Label labels [ 512 ] ;
2021-03-14 16:30:17 -04:00
Macro macros [ 256 ] ;
2021-03-13 17:55:29 -05:00
} Program ;
2021-02-23 01:15:02 -05:00
2021-02-07 13:21:41 -05:00
Program p ;
2021-08-28 02:51:48 -04:00
static Uint16 addr = 0 ;
2021-01-30 17:25:48 -05:00
2021-02-01 23:21:27 -05:00
/* clang-format off */
2021-06-28 17:42:36 -04:00
static char ops [ ] [ 4 ] = {
2021-08-17 17:48:48 -04:00
" LIT " , " INC " , " POP " , " DUP " , " NIP " , " SWP " , " OVR " , " ROT " ,
2021-05-11 14:12:07 -04:00
" EQU " , " NEQ " , " GTH " , " LTH " , " JMP " , " JCN " , " JSR " , " STH " ,
2021-05-11 14:14:52 -04:00
" LDZ " , " STZ " , " LDR " , " STR " , " LDA " , " STA " , " DEI " , " DEO " ,
2021-03-21 13:24:44 -04:00
" ADD " , " SUB " , " MUL " , " DIV " , " AND " , " ORA " , " EOR " , " SFT "
2021-02-04 15:22:08 -05:00
} ;
2021-02-01 23:21:27 -05:00
2021-09-16 12:11:53 -04:00
static int cpos ( char * s , char a ) { int i = 0 ; char c ; while ( ( c = s [ i + + ] ) ) if ( c = = a ) return i ; return - 1 ; }
2021-08-29 14:43:00 -04:00
static int scmp ( char * a , char * b , int len ) { int i = 0 ; while ( a [ i ] = = b [ i ] ) if ( ! a [ i ] | | + + i > = len ) return 1 ; return 0 ; } /* string compare */
2021-06-28 17:42:36 -04:00
static int sihx ( char * s ) { int i = 0 ; char c ; while ( ( c = s [ i + + ] ) ) if ( ! ( c > = ' 0 ' & & c < = ' 9 ' ) & & ! ( c > = ' a ' & & c < = ' f ' ) ) return 0 ; return i > 1 ; } /* string is hexadecimal */
static int shex ( char * s ) { int n = 0 , i = 0 ; char c ; while ( ( c = s [ i + + ] ) ) if ( c > = ' 0 ' & & c < = ' 9 ' ) n = n * 16 + ( c - ' 0 ' ) ; else if ( c > = ' a ' & & c < = ' f ' ) n = n * 16 + 10 + ( c - ' a ' ) ; return n ; } /* string to num */
static int slen ( char * s ) { int i = 0 ; while ( s [ i ] & & s [ + + i ] ) ; return i ; } /* string length */
static char * scpy ( char * src , char * dst , int len ) { int i = 0 ; while ( ( dst [ i ] = src [ i ] ) & & i < len - 2 ) i + + ; dst [ i + 1 ] = ' \0 ' ; return dst ; } /* string copy */
static char * scat ( char * dst , const char * src ) { char * ptr = dst + slen ( dst ) ; while ( * src ) * ptr + + = * src + + ; * ptr = ' \0 ' ; return dst ; } /* string cat */
2021-01-29 16:59:16 -05:00
# pragma mark - Helpers
2021-02-07 13:21:41 -05:00
/* clang-format on */
2021-01-30 17:25:48 -05:00
2021-02-04 15:22:08 -05:00
# pragma mark - I / O
2021-06-28 17:42:36 -04:00
static Macro *
2021-03-14 16:30:17 -04:00
findmacro ( char * name )
{
int i ;
for ( i = 0 ; i < p . mlen ; + + i )
if ( scmp ( p . macros [ i ] . name , name , 64 ) )
return & p . macros [ i ] ;
return NULL ;
}
2021-06-28 17:42:36 -04:00
static Label *
2021-04-20 00:00:14 -04:00
findlabel ( char * name )
2021-01-31 00:31:49 -05:00
{
2021-04-20 00:00:14 -04:00
int i ;
2021-03-13 17:55:29 -05:00
for ( i = 0 ; i < p . llen ; + + i )
if ( scmp ( p . labels [ i ] . name , name , 64 ) )
return & p . labels [ i ] ;
2021-01-31 00:31:49 -05:00
return NULL ;
}
2021-06-28 17:42:36 -04:00
static Uint8
2021-02-13 16:21:05 -05:00
findopcode ( char * s )
2021-02-06 13:39:13 -05:00
{
int i ;
2021-02-07 13:21:41 -05:00
for ( i = 0 ; i < 0x20 ; + + i ) {
2021-02-06 13:39:13 -05:00
int m = 0 ;
2021-08-29 14:43:00 -04:00
if ( ! scmp ( ops [ i ] , s , 3 ) )
2021-02-07 13:21:41 -05:00
continue ;
2021-02-06 13:39:13 -05:00
while ( s [ 3 + m ] ) {
2021-08-30 13:19:33 -04:00
if ( s [ 3 + m ] = = ' 2 ' )
i | = ( 1 < < 5 ) ; /* mode: short */
else if ( s [ 3 + m ] = = ' r ' )
i | = ( 1 < < 6 ) ; /* mode: return */
2021-05-12 13:42:24 -04:00
else if ( s [ 3 + m ] = = ' k ' )
i | = ( 1 < < 7 ) ; /* mode: keep */
2021-03-27 14:04:05 -04:00
else
return 0 ; /* failed to match */
2021-02-06 13:39:13 -05:00
m + + ;
}
2021-08-17 17:48:48 -04:00
if ( ! i ) i | = ( 1 < < 7 ) ; /* force LIT nonzero (keep is ignored) */
2021-02-06 13:39:13 -05:00
return i ;
}
return 0 ;
}
2021-08-29 14:25:58 -04:00
static void
pushbyte ( Uint8 b , int lit )
{
if ( lit ) pushbyte ( findopcode ( " LIT " ) , 0 ) ;
p . data [ p . ptr + + ] = b ;
p . length = p . ptr ;
}
static void
pushshort ( Uint16 s , int lit )
{
if ( lit ) pushbyte ( findopcode ( " LIT2 " ) , 0 ) ;
pushbyte ( ( s > > 8 ) & 0xff , 0 ) ;
pushbyte ( s & 0xff , 0 ) ;
}
static void
pushword ( char * s )
{
int i = 0 ;
char c ;
while ( ( c = s [ i + + ] ) ) pushbyte ( c , 0 ) ;
}
2021-06-28 17:42:36 -04:00
static char *
2021-03-11 18:47:28 -05:00
sublabel ( char * src , char * scope , char * name )
{
2021-04-24 20:12:25 -04:00
return scat ( scat ( scpy ( scope , src , 64 ) , " / " ) , name ) ;
2021-03-11 18:47:28 -05:00
}
2021-02-04 15:22:08 -05:00
# pragma mark - Parser
2021-01-31 00:31:49 -05:00
2021-06-28 17:42:36 -04:00
static int
2021-07-24 20:09:46 -04:00
error ( char * name , char * msg )
2021-01-30 17:25:48 -05:00
{
2021-07-24 20:09:46 -04:00
fprintf ( stderr , " %s: %s \n " , name , msg ) ;
2021-02-04 16:49:03 -05:00
return 0 ;
}
2021-06-28 17:42:36 -04:00
static int
2021-03-14 16:30:17 -04:00
makemacro ( char * name , FILE * f )
{
Macro * m ;
char word [ 64 ] ;
if ( findmacro ( name ) )
return error ( " Macro duplicate " , name ) ;
if ( sihx ( name ) & & slen ( name ) % 2 = = 0 )
return error ( " Macro name is hex number " , name ) ;
2021-08-28 13:49:51 -04:00
if ( findopcode ( name ) | | scmp ( name , " BRK " , 4 ) | | ! slen ( name ) | | scmp ( name , " include " , 8 ) )
2021-03-14 16:30:17 -04:00
return error ( " Macro name is invalid " , name ) ;
m = & p . macros [ p . mlen + + ] ;
scpy ( name , m - > name , 64 ) ;
2021-08-27 18:47:05 -04:00
while ( fscanf ( f , " %63s " , word ) = = 1 ) {
2021-03-14 16:30:17 -04:00
if ( word [ 0 ] = = ' { ' ) continue ;
if ( word [ 0 ] = = ' } ' ) break ;
2021-04-20 13:31:50 -04:00
if ( m - > len > 64 )
2021-04-15 13:19:59 -04:00
return error ( " Macro too large " , name ) ;
2021-03-14 16:30:17 -04:00
scpy ( word , m - > items [ m - > len + + ] , 64 ) ;
}
return 1 ;
}
2021-06-28 17:42:36 -04:00
static int
2021-08-28 02:51:48 -04:00
makelabel ( char * name )
2021-02-04 16:49:03 -05:00
{
Label * l ;
2021-02-07 13:21:41 -05:00
if ( findlabel ( name ) )
return error ( " Label duplicate " , name ) ;
2021-03-11 15:19:59 -05:00
if ( sihx ( name ) & & slen ( name ) % 2 = = 0 )
2021-02-12 19:18:52 -05:00
return error ( " Label name is hex number " , name ) ;
2021-08-17 17:48:48 -04:00
if ( findopcode ( name ) | | scmp ( name , " BRK " , 4 ) | | ! slen ( name ) )
2021-02-13 11:38:23 -05:00
return error ( " Label name is invalid " , name ) ;
2021-03-13 17:55:29 -05:00
l = & p . labels [ p . llen + + ] ;
2021-02-04 16:49:03 -05:00
l - > addr = addr ;
2021-03-01 12:16:40 -05:00
l - > refs = 0 ;
2021-02-07 13:21:41 -05:00
scpy ( name , l - > name , 64 ) ;
2021-02-04 16:49:03 -05:00
return 1 ;
}
2021-09-16 12:11:53 -04:00
static int
2021-09-25 12:39:27 -04:00
addref ( Label * l , Uint8 rel )
2021-09-16 12:11:53 -04:00
{
2021-09-25 12:39:27 -04:00
if ( rel ) {
int pos = cpos ( l - > name , ' / ' ) ;
if ( pos ! = - 1 ) {
char root [ 64 ] ;
Label * rl = findlabel ( scpy ( l - > name , root , pos ) ) ;
+ + rl - > refs ;
}
2021-09-16 12:11:53 -04:00
}
return + + l - > refs ;
}
2021-06-28 17:42:36 -04:00
static int
2021-02-23 01:15:02 -05:00
skipblock ( char * w , int * cap , char a , char b )
2021-02-15 17:04:58 -05:00
{
2021-02-23 01:15:02 -05:00
if ( w [ 0 ] = = b ) {
2021-02-15 17:04:58 -05:00
* cap = 0 ;
return 1 ;
}
2021-02-23 01:15:02 -05:00
if ( w [ 0 ] = = a ) * cap = 1 ;
2021-02-15 17:04:58 -05:00
if ( * cap ) return 1 ;
return 0 ;
}
2021-06-28 17:42:36 -04:00
static int
2021-03-14 16:30:17 -04:00
walktoken ( char * w )
{
Macro * m ;
if ( findopcode ( w ) | | scmp ( w , " BRK " , 4 ) )
return 1 ;
switch ( w [ 0 ] ) {
2021-04-20 13:31:50 -04:00
case ' [ ' : return 0 ;
case ' ] ' : return 0 ;
2021-04-21 14:12:42 -04:00
case ' \' ' : return 1 ;
2021-04-20 00:33:52 -04:00
case ' . ' : return 2 ; /* zero-page: LIT addr-lb */
2021-04-20 13:31:50 -04:00
case ' , ' : return 2 ; /* relative: LIT addr-rel */
2021-04-20 16:05:34 -04:00
case ' : ' : return 2 ; /* absolute: addr-hb addr-lb */
2021-04-20 13:31:50 -04:00
case ' ; ' : return 3 ; /* absolute: LIT addr-hb addr-lb */
case ' $ ' : return shex ( w + 1 ) ;
case ' # ' : return slen ( w + 1 ) = = 4 ? 3 : 2 ;
2021-04-21 14:12:42 -04:00
case ' " ' : return slen ( w + 1 ) ;
2021-03-14 16:30:17 -04:00
}
2021-03-14 16:47:09 -04:00
if ( ( m = findmacro ( w ) ) ) {
int i , res = 0 ;
for ( i = 0 ; i < m - > len ; + + i )
2021-04-24 20:12:25 -04:00
res + = walktoken ( m - > items [ i ] ) ;
2021-03-14 16:47:09 -04:00
return res ;
}
2021-07-03 23:24:36 -04:00
return error ( " Invalid token " , w ) ;
2021-03-14 16:30:17 -04:00
}
2021-06-28 17:42:36 -04:00
static int
2021-03-14 16:30:17 -04:00
parsetoken ( char * w )
{
Label * l ;
Macro * m ;
2021-04-20 00:33:52 -04:00
if ( w [ 0 ] = = ' . ' & & ( l = findlabel ( w + 1 ) ) ) { /* zero-page */
2021-04-21 16:01:50 -04:00
if ( l - > addr > 0xff )
return error ( " Address is not in zero page " , w ) ;
2021-04-20 00:33:52 -04:00
pushbyte ( l - > addr , 1 ) ;
2021-09-25 12:39:27 -04:00
return addref ( l , 1 ) ;
2021-04-25 00:18:15 -04:00
} else if ( w [ 0 ] = = ' , ' & & ( l = findlabel ( w + 1 ) ) ) { /* relative */
2021-04-20 00:33:52 -04:00
int off = l - > addr - p . ptr - 3 ;
if ( off < - 126 | | off > 126 )
return error ( " Address is too far " , w ) ;
pushbyte ( ( Sint8 ) off , 1 ) ;
2021-09-25 12:39:27 -04:00
return addref ( l , 0 ) ;
2021-04-25 00:18:15 -04:00
} else if ( w [ 0 ] = = ' : ' & & ( l = findlabel ( w + 1 ) ) ) { /* raw */
2021-04-20 16:05:34 -04:00
pushshort ( l - > addr , 0 ) ;
2021-09-25 12:39:27 -04:00
return addref ( l , 1 ) ;
2021-04-20 00:33:52 -04:00
} else if ( w [ 0 ] = = ' ; ' & & ( l = findlabel ( w + 1 ) ) ) { /* absolute */
2021-04-20 00:00:14 -04:00
pushshort ( l - > addr , 1 ) ;
2021-09-25 12:39:27 -04:00
return addref ( l , 1 ) ;
2021-08-18 02:45:51 -04:00
} else if ( findopcode ( w ) | | scmp ( w , " BRK " , 4 ) ) { /* opcode */
2021-04-20 13:31:50 -04:00
pushbyte ( findopcode ( w ) , 0 ) ;
2021-03-14 16:30:17 -04:00
return 1 ;
2021-04-25 00:18:15 -04:00
} else if ( w [ 0 ] = = ' " ' ) { /* string */
2021-04-21 14:12:42 -04:00
pushword ( w + 1 ) ;
return 1 ;
2021-04-25 00:18:15 -04:00
} else if ( w [ 0 ] = = ' \' ' ) { /* char */
2021-04-21 14:12:42 -04:00
pushbyte ( ( Uint8 ) w [ 1 ] , 0 ) ;
return 1 ;
2021-04-25 00:18:15 -04:00
} else if ( w [ 0 ] = = ' # ' ) { /* immediate */
2021-04-13 00:16:31 -04:00
if ( slen ( w + 1 ) = = 1 )
pushbyte ( ( Uint8 ) w [ 1 ] , 1 ) ;
if ( sihx ( w + 1 ) & & slen ( w + 1 ) = = 2 )
2021-03-14 16:30:17 -04:00
pushbyte ( shex ( w + 1 ) , 1 ) ;
2021-04-13 00:16:31 -04:00
else if ( sihx ( w + 1 ) & & slen ( w + 1 ) = = 4 )
2021-03-14 16:30:17 -04:00
pushshort ( shex ( w + 1 ) , 1 ) ;
else
2021-06-17 20:41:55 -04:00
return error ( " Invalid hexadecimal literal " , w ) ;
2021-03-14 16:30:17 -04:00
return 1 ;
2021-04-25 00:18:15 -04:00
} else if ( sihx ( w ) ) { /* raw */
2021-04-20 13:31:50 -04:00
if ( slen ( w ) = = 2 )
pushbyte ( shex ( w ) , 0 ) ;
else if ( slen ( w ) = = 4 )
pushshort ( shex ( w ) , 0 ) ;
2021-04-25 00:18:15 -04:00
else
2021-06-08 11:20:00 -04:00
return error ( " Invalid hexadecimal value " , w ) ;
2021-04-20 13:31:50 -04:00
return 1 ;
2021-04-24 20:12:25 -04:00
} else if ( ( m = findmacro ( w ) ) ) {
int i ;
for ( i = 0 ; i < m - > len ; + + i )
if ( ! parsetoken ( m - > items [ i ] ) )
2021-06-17 20:41:55 -04:00
return error ( " Invalid macro " , m - > name ) ;
2021-08-28 13:30:50 -04:00
return 1 ;
2021-03-14 16:30:17 -04:00
}
2021-06-17 20:41:55 -04:00
return error ( " Invalid token " , w ) ;
2021-03-14 16:30:17 -04:00
}
2021-08-27 18:47:05 -04:00
static int
doinclude ( FILE * f , int ( * pass ) ( FILE * ) )
{
char word [ 64 ] ;
FILE * finc ;
int ret ;
if ( fscanf ( f , " %63s " , word ) ! = 1 )
return error ( " End of input " , " include " ) ;
if ( ! ( finc = fopen ( word , " r " ) ) )
return error ( " Include failed to open " , word ) ;
ret = pass ( finc ) ;
fclose ( finc ) ;
return ret ;
}
2021-06-28 17:42:36 -04:00
static int
2021-01-29 15:14:37 -05:00
pass1 ( FILE * f )
{
2021-04-20 13:31:50 -04:00
int ccmnt = 0 ;
2021-03-11 18:47:28 -05:00
char w [ 64 ] , scope [ 64 ] , subw [ 64 ] ;
2021-08-25 17:56:24 -04:00
while ( fscanf ( f , " %63s " , w ) = = 1 ) {
2021-02-23 01:15:02 -05:00
if ( skipblock ( w , & ccmnt , ' ( ' , ' ) ' ) ) continue ;
2021-08-29 13:51:14 -04:00
if ( slen ( w ) > = 63 )
return error ( " Pass 1 - Invalid token " , w ) ;
2021-06-08 10:37:12 -04:00
if ( w [ 0 ] = = ' | ' ) {
if ( ! sihx ( w + 1 ) )
2021-06-27 13:03:09 -04:00
return error ( " Pass 1 - Invalid padding " , w ) ;
2021-04-20 13:31:50 -04:00
addr = shex ( w + 1 ) ;
2021-06-08 10:37:12 -04:00
} else if ( w [ 0 ] = = ' % ' ) {
2021-03-14 16:30:17 -04:00
if ( ! makemacro ( w + 1 , f ) )
2021-06-27 13:03:09 -04:00
return error ( " Pass 1 - Invalid macro " , w ) ;
2021-02-23 16:49:36 -05:00
} else if ( w [ 0 ] = = ' @ ' ) {
2021-08-28 02:51:48 -04:00
if ( ! makelabel ( w + 1 ) )
2021-06-27 13:03:09 -04:00
return error ( " Pass 1 - Invalid label " , w ) ;
2021-03-11 18:47:28 -05:00
scpy ( w + 1 , scope , 64 ) ;
2021-04-20 13:31:50 -04:00
} else if ( w [ 0 ] = = ' & ' ) {
2021-08-28 02:51:48 -04:00
if ( ! makelabel ( sublabel ( subw , scope , w + 1 ) ) )
2021-06-27 13:03:09 -04:00
return error ( " Pass 1 - Invalid sublabel " , w ) ;
2021-08-27 18:47:05 -04:00
} else if ( scmp ( w , " include " , 8 ) ) {
if ( ! doinclude ( f , pass1 ) )
return 0 ;
2021-04-20 13:31:50 -04:00
} else if ( sihx ( w ) )
addr + = slen ( w ) / 2 ;
2021-04-20 00:33:52 -04:00
else
2021-03-14 16:30:17 -04:00
addr + = walktoken ( w ) ;
2021-01-30 17:25:48 -05:00
}
rewind ( f ) ;
2021-02-04 15:22:08 -05:00
return 1 ;
2021-01-30 17:25:48 -05:00
}
2021-06-28 17:42:36 -04:00
static int
2021-01-30 17:25:48 -05:00
pass2 ( FILE * f )
{
2021-04-24 20:12:25 -04:00
int ccmnt = 0 , cmacr = 0 ;
2021-03-11 18:47:28 -05:00
char w [ 64 ] , scope [ 64 ] , subw [ 64 ] ;
2021-08-25 17:56:24 -04:00
while ( fscanf ( f , " %63s " , w ) = = 1 ) {
2021-03-14 16:30:17 -04:00
if ( w [ 0 ] = = ' % ' ) continue ;
2021-04-19 20:23:30 -04:00
if ( w [ 0 ] = = ' & ' ) continue ;
2021-04-20 13:31:50 -04:00
if ( w [ 0 ] = = ' [ ' ) continue ;
if ( w [ 0 ] = = ' ] ' ) continue ;
2021-03-13 17:55:29 -05:00
if ( skipblock ( w , & ccmnt , ' ( ' , ' ) ' ) ) continue ;
2021-04-24 20:12:25 -04:00
if ( skipblock ( w , & cmacr , ' { ' , ' } ' ) ) continue ;
2021-03-14 16:30:17 -04:00
if ( w [ 0 ] = = ' | ' ) {
2021-04-21 08:45:17 -04:00
if ( p . length & & shex ( w + 1 ) < p . ptr )
2021-07-03 23:24:36 -04:00
return error ( " Pass 2 - Memory overwrite " , w ) ;
2021-03-14 16:30:17 -04:00
p . ptr = shex ( w + 1 ) ;
continue ;
2021-04-20 13:31:50 -04:00
} else if ( w [ 0 ] = = ' $ ' ) {
2021-04-20 00:33:52 -04:00
p . ptr + = shex ( w + 1 ) ;
continue ;
2021-04-20 13:31:50 -04:00
} else if ( w [ 0 ] = = ' @ ' ) {
2021-03-11 18:47:28 -05:00
scpy ( w + 1 , scope , 64 ) ;
continue ;
2021-08-27 18:47:05 -04:00
} else if ( scmp ( w , " include " , 8 ) ) {
if ( ! doinclude ( f , pass2 ) )
return 0 ;
continue ;
2021-03-11 18:47:28 -05:00
}
2021-09-01 16:04:56 -04:00
if ( w [ 1 ] = = ' & ' & & ( w [ 0 ] = = ' . ' | | w [ 0 ] = = ' , ' | | w [ 0 ] = = ' ; ' | | w [ 0 ] = = ' : ' ) )
2021-03-14 16:30:17 -04:00
scpy ( sublabel ( subw , scope , w + 2 ) , w + 1 , 64 ) ;
2021-04-20 13:31:50 -04:00
if ( ! parsetoken ( w ) )
2021-06-27 13:03:09 -04:00
return error ( " Pass 2 - Unknown label " , w ) ;
2021-01-29 15:14:37 -05:00
}
2021-02-04 15:22:08 -05:00
return 1 ;
2021-01-29 15:14:37 -05:00
}
2021-06-28 17:42:36 -04:00
static void
2021-03-13 20:34:08 -05:00
cleanup ( char * filename )
2021-03-01 12:16:40 -05:00
{
int i ;
2021-03-13 17:55:29 -05:00
for ( i = 0 ; i < p . llen ; + + i )
2021-04-22 22:57:47 -04:00
if ( p . labels [ i ] . name [ 0 ] > = ' A ' & & p . labels [ i ] . name [ 0 ] < = ' Z ' )
continue ; /* Ignore capitalized labels(devices) */
2021-05-04 13:57:57 -04:00
else if ( ! p . labels [ i ] . refs )
2021-06-27 13:03:09 -04:00
fprintf ( stderr , " --- Unused label: %s \n " , p . labels [ i ] . name ) ;
2021-10-06 12:21:27 -04:00
printf ( " Assembled %s in %.2fkb(%.2f%% used), %d labels, %d macros. \n " ,
filename ,
( p . length - TRIM ) / 1000.0 ,
p . length / 655.360 ,
p . llen ,
p . mlen ) ;
2021-03-01 12:16:40 -05:00
}
2021-01-29 15:14:37 -05:00
int
main ( int argc , char * argv [ ] )
{
FILE * f ;
2021-04-19 12:29:39 -04:00
if ( argc < 3 )
2021-07-24 20:09:46 -04:00
return ! error ( " usage " , " input.tal output.rom " ) ;
2021-04-19 12:29:39 -04:00
if ( ! ( f = fopen ( argv [ 1 ] , " r " ) ) )
2021-07-24 20:09:46 -04:00
return ! error ( " Load " , " Failed to open source. " ) ;
2021-04-19 12:29:39 -04:00
if ( ! pass1 ( f ) | | ! pass2 ( f ) )
2021-07-24 20:09:46 -04:00
return ! error ( " Assembly " , " Failed to assemble rom. " ) ;
2021-04-20 15:56:43 -04:00
fwrite ( p . data + TRIM , p . length - TRIM , 1 , fopen ( argv [ 2 ] , " wb " ) ) ;
2021-01-29 16:59:16 -05:00
fclose ( f ) ;
2021-03-13 20:34:08 -05:00
cleanup ( argv [ 2 ] ) ;
2021-01-29 15:14:37 -05:00
return 0 ;
}