int InstrumentRequest::serialize(char* buf) { int bytes = 0; UNSIGNED_CHARACTER tmpChar = 0; UNSIGNED_SHORT tmpShort = 0; bytes = sizeof(UNSIGNED_SHORT); // Leave 2 bytes for packet size // Put category of command SERIALIZE_8(tmpChar, (UNSIGNED_CHARACTER)(CMD::CommandCategory_INSTRUMENT_SEARCH_REQUEST), buf, bytes); //Command Category memcpy(buf + bytes, getMnemonic(), MNEMONIC_LENGTH); bytes += MNEMONIC_LENGTH; UNSIGNED_SHORT dummyBytes = 0; // Put size as the first field after deducting 2 bytes reserved for size SERIALIZE_16(tmpShort, (UNSIGNED_SHORT)(bytes-sizeof(UNSIGNED_SHORT)), buf, dummyBytes); return bytes; }
//Checks if the instruction is valid char* checkInstruction(char line[], int currentAddress) { static char command[50], opcode[6]; char data[16], function[6]; static int nRegisters, nConstants, nLabels; char type; int i = 0; strcpy(command, line); //Take the mnemonic features strcpy(data, findInstruction(getMnemonic(command))); data[16] = '\0'; //If the mnemonic not exists if(strcmp(data, "NULL") == 0) return "4"; //Take the instruction type, n of regs, n of consts and n of labels. type = data[0]; nRegisters = data[1] - 48; nConstants = data[2] - 48; nLabels = data[3] - 48; //Take the opcode for(i = 0; i<6; i++) opcode[i] = data[i+4]; opcode[i] ='\0'; if(type == 'R'){ //Take the function for(i = 0; i<6; i++) function[i] = data[i+10]; function[i] ='\0'; } return mountBinary(command, nRegisters, nConstants, nLabels, type, opcode, function, currentAddress); }
char * CPU::disassemble() { char buf[64], msg[128]; int i, op; uint16_t pc = PC_at_cycle_0; uint8_t opcode = mem->peek(pc); strcpy(msg, ""); // Program counter sprintf(buf, "%04X: ", pc); strcat(msg, buf); // Hex dump for (i = 0; i < 3; i++) { if (i < getLengthOfInstruction(opcode)) { sprintf(buf, "%02X ", mem->peek(pc+i)); strcat(msg, buf); } else { sprintf(buf, " "); strcat(msg, buf); } } // Register sprintf(buf, " %02X %02X %02X %02X ", A, X, Y, SP); strcat(msg, buf); // Flags sprintf(buf, "%c%c%c%c%c%c%c%c ", N ? 'N' : 'n', V ? 'V' : 'v', '-', B ? 'B' : 'b', D ? 'D' : 'd', I ? 'I' : 'i', Z ? 'Z' : 'z', C ? 'C' : 'c'); strcat(msg, buf); // Mnemonic sprintf(buf, "%s ", getMnemonic(opcode)); strcat(msg, buf); // Get operand as number switch (addressingMode[opcode]) { case CPU::ADDR_IMMEDIATE: case CPU::ADDR_ZERO_PAGE: case CPU::ADDR_ZERO_PAGE_X: case CPU::ADDR_ZERO_PAGE_Y: case CPU::ADDR_INDIRECT_X: case CPU::ADDR_INDIRECT_Y: op = mem->peek(pc+1); break; case CPU::ADDR_DIRECT: case CPU::ADDR_INDIRECT: case CPU::ADDR_ABSOLUTE: case CPU::ADDR_ABSOLUTE_X: case CPU::ADDR_ABSOLUTE_Y: op = mem->peekWord(pc+1); break; case CPU::ADDR_RELATIVE: op = pc + 2 + (int8_t)mem->peek(pc+1); break; default: op = -1; } // Format operand switch (addressingMode[opcode]) { case CPU::ADDR_IMPLIED: case CPU::ADDR_ACCUMULATOR: sprintf(buf, " "); break; case CPU::ADDR_IMMEDIATE: sprintf(buf, "#%02X", op); break; case CPU::ADDR_ZERO_PAGE: sprintf(buf, "%02X", op); break; case CPU::ADDR_ZERO_PAGE_X: sprintf(buf, "%02X,X", op); break; case CPU::ADDR_ZERO_PAGE_Y: sprintf(buf, "%02X,Y", op); break; case CPU::ADDR_ABSOLUTE: case CPU::ADDR_DIRECT: sprintf(buf, "%04X", op); break; case CPU::ADDR_ABSOLUTE_X: sprintf(buf, "%04X,X", op); break; case CPU::ADDR_ABSOLUTE_Y: sprintf(buf, "%04X,Y", op); break; case CPU::ADDR_INDIRECT: sprintf(buf, "(%04X)", op); break; case CPU::ADDR_INDIRECT_X: sprintf(buf, "(%04X,X)", op); break; case CPU::ADDR_INDIRECT_Y: sprintf(buf, "(%04X),Y", op); break; case CPU::ADDR_RELATIVE: sprintf(buf, "%04X", op); break; default: sprintf(buf, "???"); } strcat(msg, buf); return strdup(msg); }