Add target code generator
Rename to Assembler
This commit is contained in:
parent
078172a71a
commit
d880d1cc07
1 changed files with 177 additions and 10 deletions
187
main.c
187
main.c
|
@ -23,7 +23,7 @@
|
|||
|
||||
#define PREFGETC(file) *file->_IO_read_ptr
|
||||
|
||||
void tokenize(FILE *input_file, char tokens[][MAX_TOKEN_SIZE])
|
||||
void lexer(FILE *input_file, char tokens[][MAX_TOKEN_SIZE])
|
||||
{
|
||||
char current_char = 0;
|
||||
unsigned int token_index = 0;
|
||||
|
@ -77,22 +77,184 @@ void tokenize(FILE *input_file, char tokens[][MAX_TOKEN_SIZE])
|
|||
}
|
||||
}
|
||||
tokens[token_index + 1][0] = ';';
|
||||
tokens[token_index + 2][0] = EOF;
|
||||
tokens[token_index + 2][0] = EOF;
|
||||
}
|
||||
|
||||
void printtokens(char tokens[][MAX_TOKEN_SIZE])
|
||||
__uint32_t get_target_instruction(__uint8_t opcode, __uint8_t use_adress, __uint32_t adress)
|
||||
{
|
||||
int i = 0;
|
||||
while(tokens[i][0] != EOF)
|
||||
__uint32_t instruction = 0;
|
||||
instruction = instruction | opcode << 28;
|
||||
if (!use_adress)
|
||||
{
|
||||
printf("%s\n",tokens[i]);
|
||||
i++;
|
||||
return instruction;
|
||||
}
|
||||
if (adress >= 268435456)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
instruction = instruction | adress;
|
||||
return instruction;
|
||||
}
|
||||
|
||||
void print_tokens(char tokens[][MAX_TOKEN_SIZE])
|
||||
{
|
||||
int token_index = 0;
|
||||
while (tokens[token_index][0] != EOF)
|
||||
{
|
||||
printf("%s\n", tokens[token_index]);
|
||||
token_index++;
|
||||
}
|
||||
}
|
||||
|
||||
void gen_target_code(char tokens[][MAX_TOKEN_SIZE], __uint32_t *target_code)
|
||||
{
|
||||
unsigned int token_index = 0;
|
||||
unsigned int instruction_index = 0;
|
||||
while (tokens[token_index][0] != EOF)
|
||||
{
|
||||
if (strcmp(tokens[token_index], ";") == 0)
|
||||
{
|
||||
instruction_index++;
|
||||
}
|
||||
else if (strcmp(tokens[token_index], "INP") == 0)
|
||||
{
|
||||
// Get adress from stored in next token index
|
||||
token_index++;
|
||||
__uint32_t adress;
|
||||
sscanf(tokens[token_index], "%d", &adress);
|
||||
|
||||
target_code[instruction_index] = get_target_instruction(0b0001, 1, adress);
|
||||
if (!target_code)
|
||||
{
|
||||
printf("Adress in %d. Instruction is too large\n", instruction_index);
|
||||
}
|
||||
}
|
||||
else if (strcmp(tokens[token_index], "OUT") == 0)
|
||||
{
|
||||
// Get adress from stored in next token index
|
||||
token_index++;
|
||||
__uint32_t adress;
|
||||
sscanf(tokens[token_index], "%d", &adress);
|
||||
|
||||
target_code[instruction_index] = get_target_instruction(0b0010, 1, adress);
|
||||
if (!target_code)
|
||||
{
|
||||
printf("Adress in %d. Instruction is too large\n", instruction_index);
|
||||
}
|
||||
}
|
||||
else if (strcmp(tokens[token_index], "LDA") == 0)
|
||||
{
|
||||
// Get adress from stored in next token index
|
||||
token_index++;
|
||||
__uint32_t adress;
|
||||
sscanf(tokens[token_index], "%d", &adress);
|
||||
|
||||
target_code[instruction_index] = get_target_instruction(0b0011, 1, adress);
|
||||
if (!target_code)
|
||||
{
|
||||
printf("Adress in %d. Instruction is too large\n", instruction_index);
|
||||
}
|
||||
}
|
||||
else if (strcmp(tokens[token_index], "STA") == 0)
|
||||
{
|
||||
// Get adress from stored in next token index
|
||||
token_index++;
|
||||
__uint32_t adress;
|
||||
sscanf(tokens[token_index], "%d", &adress);
|
||||
|
||||
target_code[instruction_index] = get_target_instruction(0b0100, 1, adress);
|
||||
if (!target_code)
|
||||
{
|
||||
printf("Adress in %d. Instruction is too large\n", instruction_index);
|
||||
}
|
||||
}
|
||||
else if (strcmp(tokens[token_index], "INC") == 0)
|
||||
{
|
||||
target_code[instruction_index] = get_target_instruction(0b0101, 0, 0);
|
||||
}
|
||||
else if (strcmp(tokens[token_index], "DEC") == 0)
|
||||
{
|
||||
target_code[instruction_index] = get_target_instruction(0b0110, 0, 0);
|
||||
}
|
||||
else if (strcmp(tokens[token_index], "JPP") == 0)
|
||||
{
|
||||
// Get adress from stored in next token index
|
||||
token_index++;
|
||||
__uint32_t adress;
|
||||
sscanf(tokens[token_index], "%d", &adress);
|
||||
|
||||
target_code[instruction_index] = get_target_instruction(0b0111, 1, adress);
|
||||
if (!target_code)
|
||||
{
|
||||
printf("Adress in %d. Instruction is too large\n", instruction_index);
|
||||
}
|
||||
}
|
||||
else if (strcmp(tokens[token_index], "JPZ") == 0)
|
||||
{
|
||||
// Get adress from stored in next token index
|
||||
token_index++;
|
||||
__uint32_t adress;
|
||||
sscanf(tokens[token_index], "%d", &adress);
|
||||
|
||||
target_code[instruction_index] = get_target_instruction(0b1000, 1, adress);
|
||||
if (!target_code)
|
||||
{
|
||||
printf("Adress in %d. Instruction is too large\n", instruction_index);
|
||||
}
|
||||
}
|
||||
else if (strcmp(tokens[token_index], "JPN") == 0)
|
||||
{
|
||||
// Get adress from stored in next token index
|
||||
token_index++;
|
||||
__uint32_t adress;
|
||||
sscanf(tokens[token_index], "%d", &adress);
|
||||
|
||||
target_code[instruction_index] = get_target_instruction(0b0101, 1, adress);
|
||||
if (!target_code)
|
||||
{
|
||||
printf("Adress in %d. Instruction is too large\n", instruction_index);
|
||||
}
|
||||
}
|
||||
else if (strcmp(tokens[token_index], "JPU") == 0)
|
||||
{
|
||||
// Get adress from stored in next token index
|
||||
token_index++;
|
||||
__uint32_t adress;
|
||||
sscanf(tokens[token_index], "%d", &adress);
|
||||
|
||||
target_code[instruction_index] = get_target_instruction(0b1010, 1, adress);
|
||||
if (!target_code)
|
||||
{
|
||||
printf("Adress in %d. Instruction is too large\n", instruction_index);
|
||||
};
|
||||
}
|
||||
else if (strcmp(tokens[token_index], "EOJ") == 0)
|
||||
{
|
||||
target_code[instruction_index] = get_target_instruction(0b1011, 0, 0);
|
||||
}
|
||||
|
||||
token_index++;
|
||||
}
|
||||
}
|
||||
|
||||
void print_target_code(__uint32_t *target_code)
|
||||
{
|
||||
int instruction_index = 0;
|
||||
while (target_code[instruction_index] != 0)
|
||||
{
|
||||
int i = 0;
|
||||
for (i = (sizeof(target_code[instruction_index]) * 8) - 1; i >= 0; i--)
|
||||
{
|
||||
putchar(target_code[instruction_index] & (1u << i) ? '1' : '0');
|
||||
}
|
||||
printf("\n");
|
||||
instruction_index++;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
printf("-----------------\nEIPA Interpreter\nVersion: %d.%d.%d\n-----------------\n", VER_MAJOR, VER_MINOR, VER_PATCH);
|
||||
printf("-----------------\nEIPA Assembler\nVersion: %d.%d.%d\n-----------------\n", VER_MAJOR, VER_MINOR, VER_PATCH);
|
||||
|
||||
char assembly_path[] = "test.eipa";
|
||||
char image_path[] = "a.eipaimg";
|
||||
|
@ -108,8 +270,13 @@ int main(int argc, char const *argv[])
|
|||
}
|
||||
|
||||
char(*tokens)[MAX_TOKEN_SIZE] = malloc(sizeof(*tokens) * MAX_MEMORY);
|
||||
tokenize(assembly_file, tokens);
|
||||
printtokens(tokens);
|
||||
lexer(assembly_file, tokens);
|
||||
|
||||
print_tokens(tokens);
|
||||
|
||||
__uint32_t *target_code = malloc(MAX_MEMORY * sizeof(target_code));
|
||||
gen_target_code(tokens, target_code);
|
||||
print_target_code(target_code);
|
||||
|
||||
fclose(assembly_file);
|
||||
return 0;
|
||||
|
|
Loading…
Reference in a new issue