#include <stdio.h>
#include <stdlib.h>
#include "cpu.h"

//#define TEST


int main()
{
cpu_t cpu;

    init(&cpu);


#ifdef TEST

    //Testing CPU
    dumpCPU(cpu);

    // Testar splitOpcode
  /*  word_t opcode = 0x000F0010;
    word_t operation,operand;

    splitOpcode(opcode, &operation, &operand);

    printf("%08x %08x\n",operation,operand);
    printf("%08x\n",buildOpcode(operation,operand));

    // Testar parseInstructionLine
    char line[80] = "LOAD 0x30";
    char operation_str[80], operand_str[80];

    parseInstructionLine(line, operation_str, operand_str);
    printf("%s %s\n",operation_str,operand_str);


    //Testar decodeOperation
    operation = decodeOperation(decode_list,operation_str);
    printf("%08x\n",operation);

    //Testar decodeOperand
    operand = decodeOperand(operand_str);
    printf("%08x\n",operand);


    // Testar buildOpcode
    opcode = buildOpcode(operation,operand);
    printf("%08x\n",opcode);

    int argc,i;
    char argv[10][80];
    char command_line[80] = "compile fat.ty fat.obj";
    splitCommand(command_line,&argc,argv);

    for (i = 0;i<argc;i++){
        printf("%s ",argv[i]);
    }
    printf("\n");

    // Testa compilação
    assembleProgram(".\\first.ty",".\\first.obj");

    // Testa carregar o programa
    loadProgram(&cpu,".\\first.obj");
    dumpCPU(cpu);

    // Testa executar
    run(&cpu);


/*
    float a=1;
    int i,*p; p=&a;


    for ( i = sizeof(int)*8-1;i>=sizeof(int)*8-8;i--)
        printf("%d",(*p)>>i&1);
    printf("\n");
    for ( i = sizeof(int)*8-9;i>=sizeof(int)*8-16;i--)
        printf("%d",(*p)>>i&1);
    printf("\n");
    for ( i = sizeof(int)*8-17;i>=sizeof(int)*8-24;i--)
        printf("%d",(*p)>>i&1);
    printf("\n");
    for ( i = sizeof(int)*8-25;i>=0;i--)
        printf("%d",(*p)>>i&1);
    printf("\n");
    return 0;

*/




#else
    exec(&cpu);
#endif
    return 0;

}
