Compare commits

...

4 commits

4 changed files with 148 additions and 5 deletions

View file

@ -6,9 +6,9 @@ Instructions:
INC 0b0101 | no Adress
DEC 0b0110 | no Adress
JPP 0b0111 | +Adress/Label
JPZ 0b1000 | +Adress
JPN 0b1001 | +Adress
JPU 0b1010 | +Adress
JPZ 0b1000 | +Adress/Label
JPN 0b1001 | +Adress/Label
JPU 0b1010 | +Adress/Label
EOJ 0b1011 | no Adress
Instruction format, [] means optional:

View file

@ -0,0 +1,31 @@
#ifndef _INSTRUCTION_TABLE_H_
#define _INSTRUCTION_TABLE_H_
#include "../header/shash/shash.h"
#include <sys/types.h>
#define MAX_INSTRUCTION_COUNT 16
// Create a hashtable that contains informations about the instructions.
shash_hashtable_t create_instruction_information_hastable(void);
// Destroy the information table
void destroy_instruction_information_hashtable(shash_hashtable_t *instruction_information_table);
typedef struct
{
unsigned int requires_adress;
unsigned int supports_adress_label;
u_int8_t opcode;
} instruction_information_t;
typedef struct
{
char *name;
unsigned int name_length;
unsigned int requires_adress;
unsigned int supports_adress_label;
u_int8_t opcode;
} instruction_information_config_t;
#endif

114
src/instruction_table.c Normal file
View file

@ -0,0 +1,114 @@
#include "../header/instruction_table.h"
#include "../header/common.h"
#include "../header/shash/shash.h"
#include <stdio.h>
// Change this code if you want to configure changes to the instruction format
instruction_information_config_t config[] = {
{.name = "INC",
.name_length = 3,
.requires_adress = 1,
.supports_adress_label = 0,
.opcode = 0b0001},
{.name = "OUT",
.name_length = 3,
.requires_adress = 1,
.supports_adress_label = 0,
.opcode = 0b0010},
{.name = "LDA",
.name_length = 3,
.requires_adress = 1,
.supports_adress_label = 0,
.opcode = 0b0011},
{.name = "STA",
.name_length = 3,
.requires_adress = 1,
.supports_adress_label = 0,
.opcode = 0b0100},
{.name = "INC",
.name_length = 3,
.requires_adress = 0,
.supports_adress_label = 0,
.opcode = 0b0101},
{.name = "DEC",
.name_length = 3,
.requires_adress = 0,
.supports_adress_label = 0,
.opcode = 0b0110},
{.name = "JPP",
.name_length = 3,
.requires_adress = 1,
.supports_adress_label = 1,
.opcode = 0b0111},
{.name = "JPZ",
.name_length = 3,
.requires_adress = 1,
.supports_adress_label = 1,
.opcode = 0b1000},
{.name = "JPN",
.name_length = 3,
.requires_adress = 1,
.supports_adress_label = 1,
.opcode = 0b1001},
{.name = "JPU",
.name_length = 3,
.requires_adress = 1,
.supports_adress_label = 1,
.opcode = 0b1010},
{.name = "EOJ",
.name_length = 3,
.requires_adress = 0,
.supports_adress_label = 0,
.opcode = 0b1011},
{0}
};
shash_hashtable_t create_instruction_information_hastable(void)
{
shash_hashtable_t instruction_information_table;
// We make the hashtable bigger than needed to avoid collosions
shash_init_hashtable(&instruction_information_table, MAX_INSTRUCTION_COUNT * 10);
instruction_information_config_t current_instr_config;
unsigned int i = 0;
do
{
current_instr_config = config[i];
instruction_information_t *current_instruction_info = malloc(sizeof(instruction_information_t));
current_instruction_info->requires_adress = current_instr_config.requires_adress;
current_instruction_info->supports_adress_label = current_instr_config.supports_adress_label;
current_instruction_info->opcode = current_instr_config.opcode;
shash_set(current_instr_config.name, current_instr_config.name_length, current_instruction_info, &instruction_information_table);
i++;
} while (strncmp(current_instr_config.name, "", MAX_TOKEN_SIZE) != 0);
return instruction_information_table;
}
void destroy_instruction_information_hashtable(shash_hashtable_t *instruction_information_table)
{
//Destroy all the information structs stored in the hashtable
instruction_information_config_t current_instr_config;
unsigned int i = 0;
do
{
current_instr_config = config[i];
free(shash_get(current_instr_config.name, current_instr_config.name_length, instruction_information_table));
i++;
} while (strncmp(current_instr_config.name, "", MAX_TOKEN_SIZE) != 0);
// Destroy the actual hashtable
shash_destroy_hashtable(instruction_information_table);
}

View file

@ -133,8 +133,6 @@ int main(int argc, char **argv)
if (assembly_file == NULL)
return EXIT_FAILURE;
if(tokens == NULL){
printf("Error: Could not allocate memory for the tokens array\n");