2017-08-18 05:08:22 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
|
|
|
|
#include "Assembler.h"
|
|
|
|
#include "Project64-core\N64System\Mips\OpCode.h"
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
ASM_PARSE_ERROR CAssembler::m_ParseError = ERR_NONE;
|
|
|
|
uint32_t CAssembler::m_Address = 0;
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
char* CAssembler::m_TokContext = NULL;
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
bool CAssembler::AssembleLine(char* line, uint32_t* opcode, uint32_t address)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
m_ParseError = ERR_NONE;
|
|
|
|
m_Address = address;
|
|
|
|
|
|
|
|
char line_c[128];
|
|
|
|
strncpy(line_c, line, 128);
|
|
|
|
StrToLower(line_c);
|
|
|
|
|
|
|
|
if (line_c[0] == '\0')
|
|
|
|
{
|
|
|
|
*opcode = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* name = strtok_s(line_c, " \t", &m_TokContext);
|
|
|
|
|
|
|
|
// attempt to assemble the line
|
|
|
|
// if a syntax error occurs, check if the command has alternative syntax forms and retry
|
|
|
|
|
|
|
|
for(int nFallback = 0;; nFallback++)
|
|
|
|
{
|
|
|
|
const ASM_INSTRUCTION* instruction = LookupInstruction(name, nFallback);
|
|
|
|
|
|
|
|
if (instruction == NULL)
|
|
|
|
{
|
|
|
|
m_ParseError = ERR_UNKNOWN_CMD;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_ParseError = ERR_NONE;
|
|
|
|
|
|
|
|
if (nFallback > 0)
|
|
|
|
{
|
|
|
|
// prepare for re-tokenization
|
|
|
|
strncpy(line_c, line, 128);
|
|
|
|
StrToLower(line_c);
|
|
|
|
name = strtok_s(line_c, " \t", &m_TokContext);
|
|
|
|
}
|
|
|
|
|
|
|
|
*opcode = instruction->base(instruction->val);
|
|
|
|
|
|
|
|
if (instruction->syntax == NULL)
|
|
|
|
{
|
|
|
|
// No parameters
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; instruction->syntax[i]; i++)
|
|
|
|
{
|
|
|
|
instruction->syntax[i](opcode);
|
|
|
|
|
|
|
|
if (m_ParseError != ERR_NONE)
|
|
|
|
{
|
|
|
|
goto next_fallback;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assembled without errors
|
|
|
|
return true;
|
|
|
|
|
|
|
|
next_fallback:;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const ASM_INSTRUCTION* CAssembler::LookupInstruction(char* name, int nFallback)
|
|
|
|
{
|
|
|
|
for (int i = 0; m_Instructions[i].name != NULL; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(name, m_Instructions[i].name) == 0)
|
|
|
|
{
|
|
|
|
if (nFallback != 0)
|
|
|
|
{
|
|
|
|
nFallback--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return &m_Instructions[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
const ASM_REGISTER* CAssembler::LookupRegister(char* name)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
for (int i = 0; m_Registers[i].name != NULL; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(name, m_Registers[i].name) == 0)
|
|
|
|
{
|
|
|
|
return &m_Registers[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAssembler::StrToLower(char* str)
|
|
|
|
{
|
|
|
|
while (*str)
|
|
|
|
{
|
|
|
|
if (*str >= 'A' && *str <= 'Z')
|
|
|
|
{
|
|
|
|
*str |= 0x20;
|
|
|
|
}
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CAssembler::pop_reg()
|
|
|
|
{
|
|
|
|
char* r = strtok_s(NULL, " \t,()", &m_TokContext);
|
2017-08-18 05:08:22 +00:00
|
|
|
|
|
|
|
if (r == NULL)
|
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
m_ParseError = ERR_EXPECTED_REG;
|
2017-08-18 05:08:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
const ASM_REGISTER* reg = LookupRegister(r);
|
2017-08-18 05:08:22 +00:00
|
|
|
|
|
|
|
if (reg == NULL)
|
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
m_ParseError = ERR_INVALID_REG;
|
2017-08-18 05:08:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return reg->val;
|
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
uint32_t CAssembler::pop_val()
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
char* v = strtok_s(NULL, " \t,()", &m_TokContext);
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2017-08-19 19:35:38 +00:00
|
|
|
if (v == NULL)
|
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
m_ParseError = ERR_EXPECTED_VAL;
|
2017-08-19 19:35:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
//if (isalpha(*v))
|
|
|
|
//{
|
|
|
|
// // todo lookup label value
|
|
|
|
// return 0;
|
|
|
|
//}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
|
|
|
int base = 0; // hex or dec
|
|
|
|
|
|
|
|
if (*v == '$')
|
|
|
|
{
|
|
|
|
base = 16; // hex
|
|
|
|
v++;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* endptr;
|
|
|
|
|
|
|
|
uint32_t val = strtoul(v, &endptr, base);
|
|
|
|
|
|
|
|
if (*endptr != '\0')
|
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
m_ParseError = ERR_EXPECTED_VAL;
|
2017-08-18 05:08:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
uint32_t CAssembler::base_op(uint32_t val)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
|
|
|
return val << 26;
|
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
uint32_t CAssembler::base_spec(uint32_t val)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
uint32_t CAssembler::base_spec_jalr_ra(uint32_t val)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
return (31 << 11) | val;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
uint32_t CAssembler::base_regimm(uint32_t val)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
return (R4300i_REGIMM << 26) | (val << 16);
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
uint32_t CAssembler::base_cop0_co(uint32_t val)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
return (R4300i_CP0 << 26) | (1 << 25) | val;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
uint32_t CAssembler::base_cop0_mv(uint32_t val)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
return (R4300i_CP0 << 26) | (val << 21);
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
uint32_t CAssembler::base_cop1_mv(uint32_t val)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
return (R4300i_CP1 << 26) | (val << 21);
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
uint32_t CAssembler::base_cop1_s(uint32_t val)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
return (R4300i_CP1 << 26) | (R4300i_COP1_S << 21) | val;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
uint32_t CAssembler::base_cop1_d(uint32_t val)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
return (R4300i_CP1 << 26) | (R4300i_COP1_D << 21) | val;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
uint32_t CAssembler::base_cop1_bc(uint32_t val)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
return (R4300i_CP1 << 26) | (R4300i_COP1_BC << 21) | (val << 16);
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
void CAssembler::arg_reg_t(uint32_t* opcode)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
*opcode |= pop_reg() << 16;
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
void CAssembler::arg_reg_s(uint32_t* opcode)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
*opcode |= pop_reg() << 21;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
void CAssembler::arg_reg_d(uint32_t* opcode)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
*opcode |= pop_reg() << 11;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
void CAssembler::arg_reg_ft(uint32_t* opcode)
|
2017-08-18 05:08:22 +00:00
|
|
|
{
|
2018-01-21 09:36:20 +00:00
|
|
|
*opcode |= pop_reg() << 16;
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
void CAssembler::arg_reg_fs(uint32_t* opcode)
|
|
|
|
{
|
|
|
|
*opcode |= pop_reg() << 11;
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
void CAssembler::arg_reg_fd(uint32_t* opcode)
|
|
|
|
{
|
|
|
|
*opcode |= pop_reg() << 6;
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
void CAssembler::arg_jump(uint32_t* opcode)
|
|
|
|
{
|
|
|
|
*opcode |= (pop_val() / 4) & 0x3FFFFFF;
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
void CAssembler::arg_imm16(uint32_t* opcode)
|
|
|
|
{
|
|
|
|
*opcode |= (pop_val() & 0xFFFF);
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
void CAssembler::arg_bra_target(uint32_t* opcode)
|
|
|
|
{
|
|
|
|
uint16_t relTarget = (((pop_val() - m_Address) / 4) & 0xFFFF) - 1;
|
|
|
|
*opcode |= relTarget;
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
void CAssembler::arg_shamt(uint32_t* opcode)
|
|
|
|
{
|
|
|
|
*opcode |= (pop_val() & 0x1F) << 6;
|
|
|
|
}
|
2017-08-18 05:08:22 +00:00
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
void CAssembler::arg_cache_op(uint32_t* opcode)
|
|
|
|
{
|
|
|
|
*opcode |= (pop_val() & 0x1F) << 16;
|
|
|
|
}
|
2017-08-19 19:35:38 +00:00
|
|
|
|
2018-01-21 09:36:20 +00:00
|
|
|
void CAssembler::arg_syscall_code(uint32_t* opcode)
|
|
|
|
{
|
|
|
|
*opcode |= (pop_val() & 0xFFFFF) << 6;
|
2017-08-18 05:08:22 +00:00
|
|
|
}
|