2018-09-11 00:44:54 +08:00
|
|
|
|
|
|
|
#include "CPU.h"
|
|
|
|
|
|
|
|
SC_HAS_PROCESS(CPU);
|
2018-09-20 05:44:38 +08:00
|
|
|
CPU::CPU(sc_module_name name, uint32_t PC): sc_module(name)
|
2018-09-11 00:44:54 +08:00
|
|
|
, instr_bus("instr_bus")
|
2018-09-17 18:21:26 +08:00
|
|
|
{
|
2018-09-11 00:44:54 +08:00
|
|
|
register_bank = new Registers();
|
2018-09-21 19:05:42 +08:00
|
|
|
exec = new Execute("Execute", register_bank);
|
2018-09-11 00:44:54 +08:00
|
|
|
perf = Performance::getInstance();
|
|
|
|
log = Log::getInstance();
|
|
|
|
|
2018-09-20 05:44:38 +08:00
|
|
|
register_bank->setPC(PC);
|
2018-09-11 00:44:54 +08:00
|
|
|
SC_THREAD(CPU_thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
CPU::~CPU() {
|
|
|
|
cout << "*********************************************" << endl;
|
|
|
|
register_bank->dump();
|
2018-09-21 15:24:25 +08:00
|
|
|
cout << "end time: " << sc_time_stamp() << endl;
|
2018-09-11 00:44:54 +08:00
|
|
|
perf->dump();
|
|
|
|
cout << "*********************************************" << endl;
|
|
|
|
}
|
2018-09-20 05:44:38 +08:00
|
|
|
|
2018-10-10 18:08:53 +08:00
|
|
|
bool CPU::process_default_instruction(Instruction &inst) {
|
|
|
|
bool PC_not_affected = true;
|
|
|
|
|
|
|
|
switch(inst.decode()) {
|
|
|
|
case OP_LUI:
|
|
|
|
exec->LUI(inst);
|
|
|
|
break;
|
|
|
|
case OP_AUIPC:
|
|
|
|
exec->AUIPC(inst);
|
|
|
|
break;
|
|
|
|
case OP_JAL:
|
|
|
|
exec->JAL(inst);
|
|
|
|
PC_not_affected = false;
|
|
|
|
break;
|
|
|
|
case OP_JALR:
|
|
|
|
exec->JALR(inst);
|
|
|
|
PC_not_affected = false;
|
|
|
|
break;
|
|
|
|
case OP_BEQ:
|
|
|
|
exec->BEQ(inst);
|
|
|
|
PC_not_affected = false;
|
|
|
|
break;
|
|
|
|
case OP_BNE:
|
|
|
|
exec->BNE(inst);
|
|
|
|
PC_not_affected = false;
|
|
|
|
break;
|
|
|
|
case OP_BLT:
|
|
|
|
exec->BLT(inst);
|
|
|
|
PC_not_affected = false;
|
|
|
|
break;
|
|
|
|
case OP_BGE:
|
|
|
|
exec->BGE(inst);
|
|
|
|
PC_not_affected = false;
|
|
|
|
break;
|
|
|
|
case OP_BLTU:
|
|
|
|
exec->BLTU(inst);
|
|
|
|
PC_not_affected = false;
|
|
|
|
break;
|
|
|
|
case OP_BGEU:
|
|
|
|
exec->BGEU(inst);
|
|
|
|
PC_not_affected = false;
|
|
|
|
break;
|
|
|
|
case OP_LB:
|
|
|
|
exec->LB(inst);
|
|
|
|
break;
|
|
|
|
case OP_LH:
|
|
|
|
exec->LB(inst);
|
|
|
|
break;
|
|
|
|
case OP_LW:
|
|
|
|
exec->LW(inst);
|
|
|
|
break;
|
|
|
|
case OP_LBU:
|
|
|
|
exec->LBU(inst);
|
|
|
|
break;
|
|
|
|
case OP_LHU:
|
|
|
|
exec->LHU(inst);
|
|
|
|
break;
|
|
|
|
case OP_SB:
|
|
|
|
exec->SB(inst);
|
|
|
|
break;
|
|
|
|
case OP_SH:
|
|
|
|
exec->SH(inst);
|
|
|
|
break;
|
|
|
|
case OP_SW:
|
|
|
|
exec->SW(inst);
|
|
|
|
break;
|
|
|
|
case OP_ADDI:
|
|
|
|
exec->ADDI(inst);
|
|
|
|
break;
|
|
|
|
case OP_SLTI:
|
|
|
|
exec->SLTI(inst);
|
|
|
|
break;
|
|
|
|
case OP_SLTIU:
|
|
|
|
exec->SLTIU(inst);
|
|
|
|
break;
|
|
|
|
case OP_XORI:
|
|
|
|
exec->XORI(inst);
|
|
|
|
break;
|
|
|
|
case OP_ORI:
|
|
|
|
exec->ORI(inst);
|
|
|
|
break;
|
|
|
|
case OP_ANDI:
|
|
|
|
exec->ANDI(inst);
|
|
|
|
break;
|
|
|
|
case OP_SLLI:
|
|
|
|
exec->SLLI(inst);
|
|
|
|
break;
|
|
|
|
case OP_SRLI:
|
|
|
|
exec->SRLI(inst);
|
|
|
|
break;
|
|
|
|
case OP_SRAI:
|
|
|
|
exec->SRAI(inst);
|
|
|
|
break;
|
|
|
|
case OP_ADD:
|
|
|
|
exec->ADD(inst);
|
|
|
|
break;
|
|
|
|
case OP_SUB:
|
|
|
|
exec->SUB(inst);
|
|
|
|
break;
|
|
|
|
case OP_SLL:
|
|
|
|
exec->SLL(inst);
|
|
|
|
break;
|
|
|
|
case OP_SLT:
|
|
|
|
exec->SLT(inst);
|
|
|
|
break;
|
|
|
|
case OP_SLTU:
|
|
|
|
exec->SLTU(inst);
|
|
|
|
break;
|
|
|
|
case OP_XOR:
|
|
|
|
exec->XOR(inst);
|
|
|
|
break;
|
|
|
|
case OP_SRL:
|
|
|
|
exec->SRL(inst);
|
|
|
|
break;
|
|
|
|
case OP_SRA:
|
|
|
|
exec->SRA(inst);
|
|
|
|
break;
|
|
|
|
case OP_OR:
|
|
|
|
exec->OR(inst);
|
|
|
|
break;
|
|
|
|
case OP_AND:
|
|
|
|
exec->AND(inst);
|
|
|
|
break;
|
|
|
|
#if 0
|
|
|
|
case OP_CSRRW:
|
|
|
|
exec->CSRRW(inst);
|
|
|
|
break;
|
|
|
|
case OP_CSRRS:
|
|
|
|
exec->CSRRS(inst);
|
|
|
|
break;
|
|
|
|
case OP_CSRRC:
|
|
|
|
exec->CSRRC(inst);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PC_not_affected;
|
|
|
|
}
|
|
|
|
|
2018-09-11 00:44:54 +08:00
|
|
|
/**
|
|
|
|
* main thread for CPU simulation
|
|
|
|
* @brief CPU mai thread
|
|
|
|
*/
|
|
|
|
void CPU::CPU_thread(void) {
|
|
|
|
|
|
|
|
tlm::tlm_generic_payload* trans = new tlm::tlm_generic_payload;
|
2018-10-10 18:08:53 +08:00
|
|
|
uint32_t INSTR;
|
2018-09-11 00:44:54 +08:00
|
|
|
sc_time delay = SC_ZERO_TIME;
|
2018-10-10 18:08:53 +08:00
|
|
|
bool PC_not_affected;
|
2018-09-11 00:44:54 +08:00
|
|
|
|
|
|
|
trans->set_command( tlm::TLM_READ_COMMAND );
|
|
|
|
trans->set_data_ptr( reinterpret_cast<unsigned char*>(&INSTR) );
|
|
|
|
trans->set_data_length( 4 );
|
|
|
|
trans->set_streaming_width( 4 ); // = data_length to indicate no streaming
|
|
|
|
trans->set_byte_enable_ptr( 0 ); // 0 indicates unused
|
|
|
|
trans->set_dmi_allowed( false ); // Mandatory initial value
|
|
|
|
trans->set_response_status( tlm::TLM_INCOMPLETE_RESPONSE );
|
|
|
|
|
|
|
|
register_bank->dump();
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
/* Get new PC value */
|
|
|
|
trans->set_address( register_bank->getPC() );
|
|
|
|
instr_bus->b_transport( *trans, delay);
|
2018-09-21 15:24:25 +08:00
|
|
|
|
2018-09-11 00:44:54 +08:00
|
|
|
perf->codeMemoryRead();
|
|
|
|
|
|
|
|
if ( trans->is_response_error() ) {
|
|
|
|
SC_REPORT_ERROR("CPU base", "Read memory");
|
|
|
|
} else {
|
2018-09-20 05:44:38 +08:00
|
|
|
log->SC_log(Log::INFO) << "PC: " << hex << register_bank->getPC()
|
|
|
|
<< dec << endl;
|
2018-10-10 18:08:53 +08:00
|
|
|
|
|
|
|
|
2018-09-11 00:44:54 +08:00
|
|
|
Instruction inst(INSTR);
|
|
|
|
|
2018-10-10 18:08:53 +08:00
|
|
|
/* check what type of instruction is and execute it */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PC_not_affected = process_default_instruction(inst);
|
|
|
|
|
|
|
|
|
|
|
|
// default:
|
|
|
|
// cout << endl << "Instruction not implemented: ";
|
|
|
|
// inst.dump();
|
|
|
|
// exec->NOP(inst);
|
2018-09-11 00:44:54 +08:00
|
|
|
}
|
|
|
|
perf->instructionsInc();
|
|
|
|
|
2018-09-21 15:24:25 +08:00
|
|
|
if (PC_not_affected == true) {
|
|
|
|
register_bank->incPC();
|
|
|
|
}
|
2018-09-11 00:44:54 +08:00
|
|
|
} // while(1)
|
|
|
|
} // CPU_thread
|