s502 assembler
A very simple assembler for the 6502 line of processors written in C
|
Go to the documentation of this file. 1 #include "debugmalloc.h"
55 ERROR(
"Mismatched number of arguments for '%s'\n", line[0]);
62 ERROR(
"Can not use undefined labels with define!\n");
63 FAIL(
"Define argument is not valid!\n");
80 FAIL(
"process_define() failed!\n");
95 ERROR(
"Mismatched number of arguments for '%s'\n", line[0]);
102 if (first_as_num < 0 || second_as_num < 0) {
104 ERROR(
"Can not forward-ref labels with ifbeq!\n");
106 FAIL(
"ifbeq argument not defined!\n");
116 FAIL(
"process_ifbeq() failed!\n");
136 if (*(str - 1) == 0) {
137 ERROR(
"Print with empty message!\n");
141 printf(
"\e[44mMESSAGE\e[49m\t%s\n", str);
155 ERROR(
"Mismatched number of arguments for '%s'\n", line[0]);
162 printf(
"\e[44mDEFINE\e[49m:\t%s = ", line[1]);
165 printf(
"\e[31mNOT DEFINED\e[39m\n");
184 ERROR(
"Mismatched number of arguments for '%s'\n", line[0]);
190 FAIL(
"Could not include file!\n");
210 ERROR(
"Mismatched number of arguments for '%s'\n", line[0]);
246 ERROR(
"Mismatched number of arguments for '%s'\n", line[0]);
252 ERROR(
"Can not use undefined labels with org!\n");
254 FAIL(
"Invalid number with .org!\n");
259 LOG(3,
"PC = %d\n", num);
264 FAIL(
"process_org() failed!\n");
280 for (
int i = 1; i < n; i++) {
281 if (arr[i][0] ==
'w')
283 else if (arr[i][0] ==
'"')
284 size += strlen(arr[i]) - 2;
302 if (2 > n || 3 < n) {
303 ERROR(
"Mismatched number of arguments for '%s'\n", line[0]);
309 ERROR(
"Invalid argument in .pad!\n");
312 int size = target - s->
PC;
314 ERROR(
"Negative padding! PC=%x, target=%x\n", s->
PC, target);
380 ERROR(
"Unknown directive: %s\n", directive);
398 char* buff = malloc(t->
binSize);
405 for (
int i = 1; i < n; i++) {
406 LOG(4,
".data entry: '%s'\n", arr[i]);
408 if (arr[i][0] ==
'w') {
414 ERROR(
"Label '%s' is not defined!\n", &arr[i][2]);
415 ERROR(
"Invalid word in .data!\n");
420 buff[bufferIdx++] = num & 0xff;
421 buff[bufferIdx++] = (num >> 8) & 0xff;
423 }
else if (arr[i][0] ==
'"') {
426 if (arr[i][strlen(arr[i]) - 1] !=
'"') {
427 ERROR(
"Malformed string in .data! (no whitespaces allowed even in quotes)\n");
432 for (
int j = 1; j < strlen(arr[i]) - 1; j++)
433 buff[bufferIdx++] = arr[i][j];
439 if (num < 0 || num >> 8) {
441 ERROR(
"Label '%s' is not defined!\n", arr[i]);
442 ERROR(
"Invalid byte in .data!\n");
447 buff[bufferIdx++] = num & 0xff;
479 if (padwith < 0 || padwith >> 8) {
480 ERROR(
"Can not pad with invalid value!\n");
489 char* buff = malloc(t->
binSize);
490 memset(buff, padwith, t->
binSize);
void tokenslist_free(TokensList *list)
free ALL memory associated with the TokensList object
char stripped[TOKEN_BUFFER_SIZE]
stripped text from source file
TokensList * load_file(char *name)
load and parse one file
@ DIR_STOP
An error occured, stop compilation.
char ** util_split_string(char *str, int *n)
split a string into segments on spaces
@ DIR_NOP
Nothing special, do nothing.
int map_set(Map *map, char *name, int value)
Sets a key in the map.
enum DIRCommand process_ifdef(State *s, TokensListElement *ptr)
Process an ifdef directive.
Token * token
Token value.
#define LOG(LVL,...)
logging macro - works like printf
TokensList * tokens
tokens
Compiler pseudo-global state.
enum DIRCommand process_print(State *s, TokensListElement *ptr)
Process a print directive.
@ DIR_ENDIF
An endif was encountered, should pop from state stack.
enum DIRCommand process_printc(State *s, TokensListElement *ptr)
Process a printc directive.
enum DIRCommand process_define(State *s, TokensListElement *ptr)
Process an ifbeq directive.
struct @1 skipProcessors[]
List of tokens to "process" when skipping tokens due to a falsy if.
struct @0 processors[]
The list of all processor functions and their tokens.
enum DIRCommand process_org(State *s, TokensListElement *ptr)
Process an org directive.
char * util_find_string_segment(char *ptr)
find pointer to next space or null in string
step 1 and 3 processing for directive tokens
Map * defines
defined constants
Token type to store token information.
int binSize
number of bytes this token will generate
enum DIRCommand process_include(State *s, TokensListElement *ptr)
Process an include directive.
enum DIRCommand process_pad(State *s, TokensListElement *ptr)
Process a pad directive.
int util_match_string(char *first, char *second, int count)
case-insensitive memcmp
An element of a TokensList.
int compile_data(State *s, Token *t, char **dataptr)
Compile a .data directive into binary data.
enum DIRCommand do_directive_token(State *s, TokensListElement *ptr, int skip)
process a directive token
DIRCommand
Internal command type for directives.
@ DIR_IF_FALSE
A conditional evaluated to false.
Token type member methods.
A doubly-linked list for storing Tokens.
enum DIRCommand process_data(State *s, TokensListElement *ptr)
Process a data directive.
void token_print(Token *token)
Pretty-print one token, with its source and length.
int directive_compile(State *s, Token *t, char **dataptr)
Compile a directive into binary data.
@ DIR_IF_TRUE
A conditional evaluated to true.
int map_get(Map *map, char *name)
Get the value of a key.
enum DIRCommand process_ifndef(State *s, TokensListElement *ptr)
Process an ifndef directive.
logging and fancy-printing
int compile_pad(State *s, Token *t, char **dataptr)
Compile a .pad directive into binary data.
@ MAP_MAX_KEY_LEN
Key buffer size for Map.
iStack class for storing non-negative integers
void tokenslist_insert(TokensList *list, TokensListElement *target, TokensList *src)
Insert the contents of SRC into another list after an element.
enum DIRCommand process_endif(State *s, TokensListElement *ptr)
"Process" an endif directive
Number module to parse numbers.
various utility functions
int number_get_number(State *s, char *str)
interpret a string as a constant, label or number
enum DIRCommand process_ifbeq(State *s, TokensListElement *ptr)
Process an ifbeq directive.
enum DIRCommand(* tokenprocessor)(State *, TokensListElement *ptr)
Token processor function.
#define FAIL(...)
Fancy-print a fail (failed step). Works like printf.
#define ERROR(...)
Fancy-print an error (cause of faliure). Works like printf.
@ NUMBER_LABEL_NODEF
Undefined label.